]> git.meshlink.io Git - meshlink/blobdiff - src/protocol_auth.c
Some more crypto wrapper functions are needed.
[meshlink] / src / protocol_auth.c
index f7e925284ba7e1dac7318ca531cbfeb2e88b2e7e..499e6869ed38c69076dc67b9a4ffd065711b4271 100644 (file)
@@ -27,7 +27,7 @@
 #include <openssl/err.h>
 #include <openssl/evp.h>
 
-#include "avl_tree.h"
+#include "splay_tree.h"
 #include "conf.h"
 #include "connection.h"
 #include "edge.h"
@@ -37,6 +37,7 @@
 #include "netutl.h"
 #include "node.h"
 #include "protocol.h"
+#include "rsa.h"
 #include "utils.h"
 #include "xalloc.h"
 
@@ -47,12 +48,12 @@ bool send_id(connection_t *c) {
                                                myself->connection->protocol_version);
 }
 
-bool id_h(connection_t *c) {
+bool id_h(connection_t *c, char *request) {
        char name[MAX_STRING_SIZE];
 
        cp();
 
-       if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
+       if(sscanf(request, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
                logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
                           c->hostname);
                return false;
@@ -116,12 +117,12 @@ bool id_h(connection_t *c) {
 
 bool send_metakey(connection_t *c) {
        char *buffer;
-       int len;
+       unsigned int len;
        bool x;
 
        cp();
 
-       len = RSA_size(c->rsa_key);
+       len = get_rsa_size(&c->rsa_key);
 
        /* Allocate buffers for the meta key */
 
@@ -163,7 +164,7 @@ bool send_metakey(connection_t *c) {
           with a length equal to that of the modulus of the RSA key.
         */
 
-       if(RSA_public_encrypt(len, (unsigned char *)c->outkey, (unsigned char *)buffer, c->rsa_key, RSA_NO_PADDING) != len) {
+       if(!rsa_public_encrypt(len, (unsigned char *)c->outkey, (unsigned char *)buffer, &c->rsa_key)) {
                logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
                           c->name, c->hostname);
                return false;
@@ -199,20 +200,20 @@ bool send_metakey(connection_t *c) {
        return x;
 }
 
-bool metakey_h(connection_t *c) {
+bool metakey_h(connection_t *c, char *request) {
        char buffer[MAX_STRING_SIZE];
        int cipher, digest, maclength, compression;
        int len;
 
        cp();
 
-       if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
+       if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
                logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
                           c->hostname);
                return false;
        }
 
-       len = RSA_size(myself->connection->rsa_key);
+       len = get_rsa_size(&myself->connection->rsa_key);
 
        /* Check if the length of the meta key is all right */
 
@@ -235,9 +236,8 @@ bool metakey_h(connection_t *c) {
 
        /* Decrypt the meta key */
 
-       if(RSA_private_decrypt(len, (unsigned char *)buffer, (unsigned char *)c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) {  /* See challenge() */
-               logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
-                          c->name, c->hostname);
+       if(!rsa_private_decrypt(len, (unsigned char *)buffer, (unsigned char *)c->inkey, &myself->connection->rsa_key)) {
+               logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
                return false;
        }
 
@@ -306,7 +306,7 @@ bool send_challenge(connection_t *c) {
 
        /* CHECKME: what is most reasonable value for len? */
 
-       len = RSA_size(c->rsa_key);
+       len = get_rsa_size(&c->rsa_key);
 
        /* Allocate buffers for the challenge */
 
@@ -329,19 +329,19 @@ bool send_challenge(connection_t *c) {
        return send_request(c, "%d %s", CHALLENGE, buffer);
 }
 
-bool challenge_h(connection_t *c) {
+bool challenge_h(connection_t *c, char *request) {
        char buffer[MAX_STRING_SIZE];
        int len;
 
        cp();
 
-       if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) {
+       if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
                logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
                           c->hostname);
                return false;
        }
 
-       len = RSA_size(myself->connection->rsa_key);
+       len = get_rsa_size(&myself->connection->rsa_key);
 
        /* Check if the length of the challenge is all right */
 
@@ -376,7 +376,7 @@ bool send_chal_reply(connection_t *c) {
        /* Calculate the hash from the challenge we received */
 
        if(!EVP_DigestInit(&ctx, c->indigest)
-                       || !EVP_DigestUpdate(&ctx, c->mychallenge, RSA_size(myself->connection->rsa_key))
+                       || !EVP_DigestUpdate(&ctx, c->mychallenge, get_rsa_size(&myself->connection->rsa_key))
                        || !EVP_DigestFinal(&ctx, (unsigned char *)hash, NULL)) {
                logger(LOG_ERR, _("Error during calculation of response for %s (%s): %s"),
                        c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
@@ -393,14 +393,14 @@ bool send_chal_reply(connection_t *c) {
        return send_request(c, "%d %s", CHAL_REPLY, hash);
 }
 
-bool chal_reply_h(connection_t *c) {
+bool chal_reply_h(connection_t *c, char *request) {
        char hishash[MAX_STRING_SIZE];
        char myhash[EVP_MAX_MD_SIZE];
        EVP_MD_CTX ctx;
 
        cp();
 
-       if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) {
+       if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
                logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
                           c->hostname);
                return false;
@@ -421,7 +421,7 @@ bool chal_reply_h(connection_t *c) {
        /* Calculate the hash from the challenge we sent */
 
        if(!EVP_DigestInit(&ctx, c->outdigest)
-                       || !EVP_DigestUpdate(&ctx, c->hischallenge, RSA_size(c->rsa_key))
+                       || !EVP_DigestUpdate(&ctx, c->hischallenge, get_rsa_size(&c->rsa_key))
                        || !EVP_DigestFinal(&ctx, (unsigned char *)myhash, NULL)) {
                logger(LOG_ERR, _("Error during calculation of response from %s (%s): %s"),
                        c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
@@ -483,7 +483,7 @@ bool send_ack(connection_t *c) {
 }
 
 static void send_everything(connection_t *c) {
-       avl_node_t *node, *node2;
+       splay_node_t *node, *node2;
        node_t *n;
        subnet_t *s;
        edge_t *e;
@@ -514,7 +514,7 @@ static void send_everything(connection_t *c) {
        }
 }
 
-bool ack_h(connection_t *c) {
+bool ack_h(connection_t *c, char *request) {
        char hisport[MAX_STRING_SIZE];
        char *hisaddress, *dummy;
        int weight, mtu;
@@ -523,7 +523,7 @@ bool ack_h(connection_t *c) {
 
        cp();
 
-       if(sscanf(c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
+       if(sscanf(request, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
                logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
                           c->hostname);
                return false;
@@ -540,8 +540,17 @@ bool ack_h(connection_t *c) {
        } else {
                if(n->connection) {
                        /* Oh dear, we already have a connection to this node. */
-                       ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
-                                          n->name, n->hostname);
+                       ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->connection->name, n->connection->hostname);
+
+                       if(n->connection->outgoing) {
+                               if(c->outgoing)
+                                       logger(LOG_WARNING, _("Two outgoing connections to the same node!"));
+                               else
+                                       c->outgoing = n->connection->outgoing;
+
+                               n->connection->outgoing = NULL;
+                       }
+
                        terminate_connection(n->connection, false);
                        /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
                        graph();