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);
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 */
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;
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 */
#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;
}
}
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) {
}
size_t digest_length(const digest_t *digest) {
- return EVP_MD_size(digest->digest);
+ return digest->maclength;
}
bool digest_active(const digest_t *digest) {
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);
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 */
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;
}
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);
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);
}
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;
}