chiark / gitweb /
site: generate_msg: Introduce `privkey' variable
[secnet.git] / site.c
diff --git a/site.c b/site.c
index 67a3dbcc3815e27a2fc3c7bbdd70fcdafd4e9df5..ce185d4d46aa03ee4e4c2d6b215deb88278351cf 100644 (file)
--- a/site.c
+++ b/site.c
@@ -650,6 +650,7 @@ static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what,
     if (type_is_msg34(type)) {
        buf_append_uint16(&st->buffer,st->mtu_target);
     }
+    struct sigprivkey_if *privkey=st->privkey;
     append_string_xinfo_done(&st->buffer,&xia);
 
     buf_append_string(&st->buffer,st->remotename);
@@ -670,10 +671,10 @@ static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what,
     buf_append_string(&st->buffer,dhpub);
     free(dhpub);
 
-    bool_t ok=st->privkey->sign(st->privkey->st,
-                               st->buffer.start,
-                               st->buffer.size,
-                               &st->buffer);
+    bool_t ok=privkey->sign(privkey->st,
+                           st->buffer.start,
+                           st->buffer.size,
+                           &st->buffer);
     if (!ok) goto fail;
     return True;
 
@@ -2129,6 +2130,17 @@ static void site_childpersist_clearkeys(void *sst, uint32_t newphase)
        crypto operations, but that's a task for another day. */
 }
 
+static void setup_sethash(struct site *st, dict_t *dict,
+                         struct hash_if **hash, struct cloc loc,
+                         sig_sethash_fn *sethash, void *sigkey_st) {
+    if (!*hash) *hash=find_cl_if(dict,"hash",CL_HASH,True,"site",loc);
+    sethash(sigkey_st,*hash);
+}
+#define SETUP_SETHASH(k) do{                                           \
+    if ((k)->sethash)                                                  \
+        setup_sethash(st,dict, &hash,loc, (k)->sethash,(k)->st);       \
+}while(0)
+
 static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
                          list_t *args)
 {
@@ -2221,6 +2233,7 @@ static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
     st->log=find_cl_if(dict,"log",CL_LOG,True,"site",loc);
     st->random=find_cl_if(dict,"random",CL_RANDOMSRC,True,"site",loc);
 
+    struct hash_if *hash=0;
     st->privkey=find_cl_if(dict,"local-key",CL_SIGPRIVKEY,True,"site",loc);
     st->addresses=dict_read_string_array(dict,"address",False,"site",loc,0);
     if (st->addresses)
@@ -2232,11 +2245,8 @@ static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
 
     st->dh=find_cl_if(dict,"dh",CL_DH,True,"site",loc);
 
-    if (st->privkey->sethash || st->pubkey->sethash) {
-       struct hash_if *hash=find_cl_if(dict,"hash",CL_HASH,True,"site",loc);
-       if (st->privkey->sethash) st->privkey->sethash(st->privkey->st,hash);
-       if (st->pubkey->sethash) st->pubkey->sethash(st->pubkey->st,hash);
-    }
+    SETUP_SETHASH(st->privkey);
+    SETUP_SETHASH(st->pubkey);
 
 #define DEFAULT(D) (st->peer_mobile || st->local_mobile        \
                     ? DEFAULT_MOBILE_##D : DEFAULT_##D)