]> git.meshlink.io Git - meshlink/blobdiff - src/protocol_auth.c
Ensure consistent use of SPTPS session labels.
[meshlink] / src / protocol_auth.c
index 82254593e36aa0a5982ad447dae801c5baa76f89..4a380e77100c3aa828859316fca67cf6bc51d599 100644 (file)
@@ -36,7 +36,9 @@
 #include "utils.h"
 #include "xalloc.h"
 #include "ed25519/sha512.h"
-    
+
+#include <assert.h>
+
 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
 
 static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
@@ -287,7 +289,7 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                c->protocol_minor = 2;
                c->allow_request = 1;
 
-               return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, "meshlink invitation", 15, send_meta_sptps, receive_invitation_sptps);
+               return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof meshlink_invitation_label, send_meta_sptps, receive_invitation_sptps);
        }
 
        /* Check if identity is a valid name */
@@ -327,11 +329,20 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                        logger(mesh, MESHLINK_ERROR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
                        return false;
                }
+       }
 
-               read_ecdsa_public_key(mesh, c);
-       } else {
-               if(c->protocol_minor && !ecdsa_active(c->ecdsa))
-                       c->protocol_minor = 1;
+       read_ecdsa_public_key(mesh, c);
+
+       if(!ecdsa_active(c->ecdsa)) {
+               logger(mesh, MESHLINK_ERROR, "No key known for peer %s (%s)", c->name, c->hostname);
+
+               node_t *n = lookup_node(mesh, c->name);
+               if(n && !n->status.waitingforkey) {
+                       logger(mesh, MESHLINK_INFO, "Requesting key from peer %s (%s)", c->name, c->hostname);
+                       send_req_key(mesh, n);
+               }
+
+               return false;
        }
 
        /* Forbid version rollback for nodes whose ECDSA key we know */
@@ -343,14 +354,14 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
        }
 
        c->allow_request = ACK;
-       char label[25 + strlen(mesh->self->name) + strlen(c->name)];
+       char label[sizeof meshlink_tcp_label + strlen(mesh->self->name) + strlen(c->name) + 2];
 
        if(c->outgoing)
-               snprintf(label, sizeof label, "meshlink TCP key expansion %s %s", mesh->self->name, c->name);
+               snprintf(label, sizeof label, "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
        else
-               snprintf(label, sizeof label, "meshlink TCP key expansion %s %s", c->name, mesh->self->name);
+               snprintf(label, sizeof label, "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
 
-       return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
+       return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label - 1, send_meta_sptps, receive_meta_sptps);
 }
 
 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
@@ -422,6 +433,8 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
        n->devclass = devclass;
        node_write_devclass(mesh, n);
 
+       n->last_successfull_connection = time(NULL);
+
        n->connection = c;
        c->node = n;
        if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
@@ -444,6 +457,8 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
 
        /* Create an edge_t for this connection */
 
+       assert(devclass >= 0 && devclass <= _DEV_CLASS_MAX);
+
        c->edge = new_edge();
        c->edge->from = mesh->self;
        c->edge->to = n;