Commit 0c4afc22 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Only allow keys configured on a given interface.

We used to accept packets signed by any key.
parent 7de67155
...@@ -242,26 +242,20 @@ add_hmac(struct buffered *buf, struct interface *ifp, ...@@ -242,26 +242,20 @@ add_hmac(struct buffered *buf, struct interface *ifp,
static int static int
compare_hmac(const unsigned char *src, const unsigned char *dst, compare_hmac(const unsigned char *src, const unsigned char *dst,
const unsigned char *packet, int bodylen, const unsigned char *packet, int bodylen,
const unsigned char *hmac, int hmaclen) const unsigned char *hmac, int hmaclen,
struct key *key)
{ {
unsigned char true_hmac[MAX_DIGEST_LEN]; unsigned char buf[MAX_DIGEST_LEN];
int true_hmaclen; int len;
int i;
for(i = 0; i < numkeys; i++) { len = compute_hmac(src, dst, packet, packet + 4, bodylen, key, buf);
true_hmaclen = compute_hmac(src, dst, packet, return len == hmaclen && (memcmp(buf, hmac, hmaclen) == 0);
packet + 4, bodylen, keys[i],
true_hmac);
if(true_hmaclen != hmaclen)
continue;
if(memcmp(true_hmac, hmac, hmaclen) == 0)
return 1;
}
return 0;
} }
int int
check_hmac(const unsigned char *packet, int packetlen, int bodylen, check_hmac(const unsigned char *packet, int packetlen, int bodylen,
const unsigned char *src, const unsigned char *dst) const unsigned char *src, const unsigned char *dst,
struct interface *ifp)
{ {
int i = bodylen + 4; int i = bodylen + 4;
int len; int len;
...@@ -275,15 +269,16 @@ check_hmac(const unsigned char *packet, int packetlen, int bodylen, ...@@ -275,15 +269,16 @@ check_hmac(const unsigned char *packet, int packetlen, int bodylen,
} }
len = packet[i + 1]; len = packet[i + 1];
if(packet[i] == MESSAGE_HMAC) { if(packet[i] == MESSAGE_HMAC) {
int ok;
if(i + len + 2 > packetlen) { if(i + len + 2 > packetlen) {
fprintf(stderr, "Received truncated message.\n"); fprintf(stderr, "Received truncated message.\n");
return -1; return -1;
} }
if(compare_hmac(src, dst, packet, bodylen, ok = compare_hmac(src, dst, packet, bodylen,
packet + i + 2 , len) == 1) { packet + i + 2, len, ifp->key);
if(ok)
return 1; return 1;
} }
}
i += len + 2; i += len + 2;
} }
return 0; return 0;
......
...@@ -29,4 +29,5 @@ struct key *add_key(char *id, int type, int len, unsigned char *value); ...@@ -29,4 +29,5 @@ struct key *add_key(char *id, int type, int len, unsigned char *value);
int add_hmac(struct buffered *buf, struct interface *ifp, int add_hmac(struct buffered *buf, struct interface *ifp,
unsigned char *packet_header); unsigned char *packet_header);
int check_hmac(const unsigned char *packet, int packetlen, int bodylen, int check_hmac(const unsigned char *packet, int packetlen, int bodylen,
const unsigned char *src, const unsigned char *dst); const unsigned char *src, const unsigned char *dst,
struct interface *ifp);
...@@ -588,7 +588,7 @@ parse_packet(const unsigned char *from, struct interface *ifp, ...@@ -588,7 +588,7 @@ parse_packet(const unsigned char *from, struct interface *ifp,
} }
if(ifp->key != NULL && !(ifp->flags & IF_NO_HMAC_VERIFY)) { if(ifp->key != NULL && !(ifp->flags & IF_NO_HMAC_VERIFY)) {
if(check_hmac(packet, packetlen, bodylen, from, to) != 1) { if(check_hmac(packet, packetlen, bodylen, from, to, ifp) != 1) {
fprintf(stderr, "Received wrong hmac.\n"); fprintf(stderr, "Received wrong hmac.\n");
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment