Include our own key in REQ_PUBKEY requests.
authorGuus Sliepen <guus@meshlink.io>
Fri, 15 May 2020 21:12:34 +0000 (23:12 +0200)
committerGuus Sliepen <guus@meshlink.io>
Fri, 15 May 2020 21:12:34 +0000 (23:12 +0200)
If we don't know a peer's public key, it most likely means the peer
doesn't know our public key, so proactively send it along with the
REQ_PUBKEY request.

src/protocol_key.c

index 2c741be..16e97eb 100644 (file)
@@ -80,7 +80,9 @@ static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data
 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
        if(!node_read_public_key(mesh, to)) {
                logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
-               send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY);
+               char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
+               send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY, pubkey);
+               free(pubkey);
                return true;
        }
 
@@ -110,6 +112,19 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *
                        return false;
                }
 
+               if(!node_read_public_key(mesh, from)) {
+                       char hiskey[MAX_STRING_SIZE];
+
+                       if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, hiskey) == 1) {
+                               from->ecdsa = ecdsa_set_base64_public_key(hiskey);
+
+                               if(!from->ecdsa) {
+                                       logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_PUBKEY", from->name, "invalid pubkey");
+                                       return true;
+                               }
+                       }
+               }
+
                send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
                free(pubkey);
                return true;