]> git.meshlink.io Git - meshlink/commitdiff
Handle truncated message authentication codes.
authorGuus Sliepen <guus@tinc-vpn.org>
Sat, 6 Jun 2009 17:04:04 +0000 (19:04 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sat, 6 Jun 2009 17:04:04 +0000 (19:04 +0200)
src/net_packet.c
src/net_setup.c
src/node.c
src/node.h
src/openssl/digest.c
src/openssl/digest.h
src/protocol_auth.c
src/protocol_key.c

index 062e0be0a4302a5a68dd2ac91694d2c92e89f192..af71af90459a9df5cb78aef74dbb7fa2eae663ac 100644 (file)
@@ -163,7 +163,7 @@ static void receive_packet(node_t *n, vpn_packet_t *packet) {
 
 static bool try_mac(node_t *n, const vpn_packet_t *inpkt)
 {
-       if(!digest_active(&n->indigest) || !n->inmaclength || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
+       if(!digest_active(&n->indigest) || inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest))
                return false;
 
        return digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len);
index 17eaec2b7475be8ec88b5ccae689cdecec68de6b..224bdf6113f335dde59d19148005c9aaffe93f7b 100644 (file)
@@ -314,21 +314,17 @@ bool setup_myself(void) {
        if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
                digest = xstrdup("sha1");
 
-       if(!digest_open_by_name(&myself->indigest, digest)) {
-               logger(LOG_ERR, _("Unrecognized digest type!"));
+       int maclength = 4;
+       get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &maclength);
+
+       if(maclength < 0) {
+               logger(LOG_ERR, _("Bogus MAC length!"));
                return false;
        }
 
-       if(!get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength))
-
-       if(digest_active(&myself->indigest)) {
-               if(myself->inmaclength > digest_length(&myself->indigest)) {
-                       logger(LOG_ERR, _("MAC length exceeds size of digest!"));
-                       return false;
-               } else if(myself->inmaclength < 0) {
-                       logger(LOG_ERR, _("Bogus MAC length!"));
-                       return false;
-               }
+       if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
+               logger(LOG_ERR, _("Unrecognized digest type!"));
+               return false;
        }
 
        /* Compression */
index 5df8b667467ff4aee7f43ca8815c10d940e8758b..6df236a1a76681ffa5cf1ce8ef47b775acac7625 100644 (file)
@@ -185,7 +185,7 @@ int dump_nodes(struct evbuffer *out) {
                n = node->data;
                if(evbuffer_add_printf(out, _(" %s at %s cipher %d digest %d maclength %d compression %d options %lx status %04x nexthop %s via %s distance %d pmtu %d (min %d max %d)\n"),
                           n->name, n->hostname, cipher_get_nid(&n->outcipher),
-                          digest_get_nid(&n->outdigest), n->outmaclength, n->outcompression,
+                          digest_get_nid(&n->outdigest), digest_length(&n->outdigest), n->outcompression,
                           n->options, *(uint32_t *)&n->status, n->nexthop ? n->nexthop->name : "-",
                           n->via ? n->via->name : "-", n->distance, n->mtu, n->minmtu, n->maxmtu) == -1)
                        return errno;
index 9b0d136f2bc5cc5e34366c50107d1a38b3fd6da1..82294a7fc12ab0bea153040af1889e64e4ad04f9 100644 (file)
@@ -56,11 +56,9 @@ typedef struct node_t {
 
        cipher_t incipher;                        /* Cipher for UDP packets */
        digest_t indigest;                        /* Digest for UDP packets */  
-       int inmaclength;                                /* Portion of digest to use */
 
        cipher_t outcipher;                        /* Cipher for UDP packets */
        digest_t outdigest;                        /* Digest for UDP packets */ 
-       int outmaclength;                               /* Portion of digest to use */
 
        int incompression;                      /* Compressionlevel, 0 = no compression */
        int outcompression;                     /* Compressionlevel, 0 = no compression */
index 1e2557d9490fa84097d643af4df3f1c5f59c9251..e1db9341ad354d76871f9d0a1971ee63136467c2 100644 (file)
 #include "digest.h"
 #include "logger.h"
 
-bool digest_open_by_name(digest_t *digest, const char *name) {
+static void set_maclength(digest_t *digest, int maclength) {
+       int digestlen = EVP_MD_size(digest->digest);
+
+       if(maclength > digestlen || maclength < 0)
+               digest->maclength = digestlen;
+       else
+               digest->maclength = maclength;
+}
+
+bool digest_open_by_name(digest_t *digest, const char *name, int maclength) {
        digest->digest = EVP_get_digestbyname(name);
-       if(digest->digest)
-               return true;
 
-       logger(LOG_DEBUG, _("Unknown digest name '%s'!"), name);
-       return false;
+       if(!digest->digest) {
+               logger(LOG_DEBUG, _("Unknown digest name '%s'!"), name);
+               return false;
+       }
+
+       set_maclength(digest, maclength);
+       return true;
 }
 
-bool digest_open_by_nid(digest_t *digest, int nid) {
+bool digest_open_by_nid(digest_t *digest, int nid, int maclength) {
        digest->digest = EVP_get_digestbynid(nid);
-       if(digest->digest)
-               return true;
 
-       logger(LOG_DEBUG, _("Unknown digest nid %d!"), nid);
-       return false;
+       if(!digest->digest) {
+               logger(LOG_DEBUG, _("Unknown digest nid %d!"), nid);
+               return false;
+       }
+
+       set_maclength(digest, maclength);
+       return true;
 }
 
-bool digest_open_sha1(digest_t *digest) {
+bool digest_open_sha1(digest_t *digest, int maclength) {
        digest->digest = EVP_sha1();
+
+       set_maclength(digest, maclength);
        return true;
 }
 
@@ -53,22 +70,27 @@ void digest_close(digest_t *digest) {
 }
 
 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
+       size_t len = EVP_MD_size(digest->digest);
+       unsigned char tmpdata[len];
+
        EVP_MD_CTX ctx;
 
-       if(EVP_DigestInit(&ctx, digest->digest)
-                       && EVP_DigestUpdate(&ctx, indata, inlen)
-                       && EVP_DigestFinal(&ctx, outdata, NULL))
-               return true;
-       
-       logger(LOG_DEBUG, _("Error creating digest: %s"), ERR_error_string(ERR_get_error(), NULL));
-       return false;
+       if(!EVP_DigestInit(&ctx, digest->digest)
+                       || !EVP_DigestUpdate(&ctx, indata, inlen)
+                       || !EVP_DigestFinal(&ctx, tmpdata, NULL)) {
+               logger(LOG_DEBUG, _("Error creating digest: %s"), ERR_error_string(ERR_get_error(), NULL));
+               return false;
+       }
+
+       memcpy(outdata, tmpdata, digest->maclength);
+       return true;
 }
 
 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
        size_t len = EVP_MD_size(digest->digest);
-       char outdata[len];
+       unsigned char outdata[len];
 
-       return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len);
+       return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
 }
 
 int digest_get_nid(const digest_t *digest) {
@@ -76,7 +98,7 @@ int digest_get_nid(const digest_t *digest) {
 }
 
 size_t digest_length(const digest_t *digest) {
-       return EVP_MD_size(digest->digest);
+       return digest->maclength;
 }
 
 bool digest_active(const digest_t *digest) {
index deba7d3a085db4bbda8de15decbabbeaa745d01d..2b5a171560736d18c64736955fbedfb27f1a0d81 100644 (file)
 
 typedef struct digest {
        const EVP_MD *digest;
+       int maclength;
 } digest_t;
 
-extern bool digest_open_by_name(struct digest *, const char *);
-extern bool digest_open_by_nid(struct digest *, int);
-extern bool digest_open_sha1(struct digest *);
+extern bool digest_open_by_name(struct digest *, const char *name, int maclength);
+extern bool digest_open_by_nid(struct digest *, int nid, int maclength);
+extern bool digest_open_sha1(struct digest *, int maclength);
 extern void digest_close(struct digest *);
 extern bool digest_create(struct digest *, const void *indata, size_t inlen, void *outdata);
 extern bool digest_verify(struct digest *, const void *indata, size_t inlen, const void *digestdata);
index 93fe23ea1692053309204c6f3dab93a31ae02166..aa0fd36d20ab61ce373cc2952477872f41c54136 100644 (file)
@@ -124,7 +124,7 @@ bool send_metakey(connection_t *c) {
        if(!cipher_open_blowfish_ofb(&c->outcipher))
                return false;
        
-       if(!digest_open_sha1(&c->outdigest))
+       if(!digest_open_sha1(&c->outdigest, -1))
                return false;
 
        /* Create a random key */
@@ -224,7 +224,7 @@ bool metakey_h(connection_t *c, char *request) {
                return false;
        }
 
-       if(!digest_open_by_nid(&c->indigest, digest)) {
+       if(!digest_open_by_nid(&c->indigest, digest, -1)) {
                logger(LOG_ERR, _("Error during initialisation of digest from %s (%s)"), c->name, c->hostname);
                return false;
        }
index 06ce733df77c21129c473d100db354bf5a7c450a..2a0f2301d98babf2632ffa9d76f8ce8b75a97c55 100644 (file)
@@ -146,8 +146,7 @@ bool send_ans_key(node_t *to) {
        cp();
 
        cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
-       digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest));
-       to->inmaclength = myself->inmaclength;
+       digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
        to->incompression = myself->incompression;
 
        randomize(key, keylen);
@@ -164,7 +163,8 @@ bool send_ans_key(node_t *to) {
        return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
                                                myself->name, to->name, key,
                                                cipher_get_nid(&to->incipher),
-                                               digest_get_nid(&to->indigest), to->inmaclength,
+                                               digest_get_nid(&to->indigest),
+                                               digest_length(&to->indigest),
                                                to->incompression);
 }
 
@@ -228,14 +228,12 @@ bool ans_key_h(connection_t *c, char *request) {
                return false;
        }
 
-       from->outmaclength = maclength;
-
-       if(!digest_open_by_nid(&from->outdigest, digest)) {
+       if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
                logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
                return false;
        }
 
-       if(from->outmaclength > digest_length(&from->outdigest) || from->outmaclength < 0) {
+       if(maclength != digest_length(&from->outdigest)) {
                logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
                return false;
        }