127df3f5dSRui Paulo /*
227df3f5dSRui Paulo * Redistribution and use in source and binary forms, with or without
327df3f5dSRui Paulo * modification, are permitted provided that: (1) source code
427df3f5dSRui Paulo * distributions retain the above copyright notice and this paragraph
527df3f5dSRui Paulo * in its entirety, and (2) distributions including binary code include
627df3f5dSRui Paulo * the above copyright notice and this paragraph in its entirety in
727df3f5dSRui Paulo * the documentation or other materials provided with the distribution.
827df3f5dSRui Paulo * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
927df3f5dSRui Paulo * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
1027df3f5dSRui Paulo * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1127df3f5dSRui Paulo * FOR A PARTICULAR PURPOSE.
1227df3f5dSRui Paulo *
1327df3f5dSRui Paulo * Functions for signature and digest verification.
1427df3f5dSRui Paulo *
150bff6a5aSEd Maste * Original code by Hannes Gredler (hannes@gredler.at)
1627df3f5dSRui Paulo */
1727df3f5dSRui Paulo
18*ee67461eSJoseph Mingrone #include <config.h>
1927df3f5dSRui Paulo
20*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
2127df3f5dSRui Paulo
2227df3f5dSRui Paulo #include <string.h>
233340d773SGleb Smirnoff #include <stdlib.h>
2427df3f5dSRui Paulo
253340d773SGleb Smirnoff #include "netdissect.h"
2627df3f5dSRui Paulo #include "signature.h"
27*ee67461eSJoseph Mingrone #include "diag-control.h"
2827df3f5dSRui Paulo
2927df3f5dSRui Paulo #ifdef HAVE_LIBCRYPTO
3027df3f5dSRui Paulo #include <openssl/md5.h>
3127df3f5dSRui Paulo #endif
3227df3f5dSRui Paulo
3327df3f5dSRui Paulo const struct tok signature_check_values[] = {
3427df3f5dSRui Paulo { SIGNATURE_VALID, "valid"},
3527df3f5dSRui Paulo { SIGNATURE_INVALID, "invalid"},
363340d773SGleb Smirnoff { CANT_ALLOCATE_COPY, "can't allocate memory"},
3727df3f5dSRui Paulo { CANT_CHECK_SIGNATURE, "unchecked"},
3827df3f5dSRui Paulo { 0, NULL }
3927df3f5dSRui Paulo };
4027df3f5dSRui Paulo
4127df3f5dSRui Paulo
4227df3f5dSRui Paulo #ifdef HAVE_LIBCRYPTO
4327df3f5dSRui Paulo /*
4427df3f5dSRui Paulo * Compute a HMAC MD5 sum.
4527df3f5dSRui Paulo * Taken from rfc2104, Appendix.
4627df3f5dSRui Paulo */
47*ee67461eSJoseph Mingrone DIAG_OFF_DEPRECATION
4827df3f5dSRui Paulo static void
signature_compute_hmac_md5(const uint8_t * text,int text_len,unsigned char * key,unsigned int key_len,uint8_t * digest)493c602fabSXin LI signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
503c602fabSXin LI unsigned int key_len, uint8_t *digest)
5127df3f5dSRui Paulo {
5227df3f5dSRui Paulo MD5_CTX context;
5327df3f5dSRui Paulo unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
5427df3f5dSRui Paulo unsigned char k_opad[65]; /* outer padding - key XORd with opad */
5527df3f5dSRui Paulo unsigned char tk[16];
5627df3f5dSRui Paulo int i;
5727df3f5dSRui Paulo
5827df3f5dSRui Paulo /* if key is longer than 64 bytes reset it to key=MD5(key) */
5927df3f5dSRui Paulo if (key_len > 64) {
6027df3f5dSRui Paulo
6127df3f5dSRui Paulo MD5_CTX tctx;
6227df3f5dSRui Paulo
6327df3f5dSRui Paulo MD5_Init(&tctx);
6427df3f5dSRui Paulo MD5_Update(&tctx, key, key_len);
6527df3f5dSRui Paulo MD5_Final(tk, &tctx);
6627df3f5dSRui Paulo
6727df3f5dSRui Paulo key = tk;
6827df3f5dSRui Paulo key_len = 16;
6927df3f5dSRui Paulo }
7027df3f5dSRui Paulo
7127df3f5dSRui Paulo /*
7227df3f5dSRui Paulo * the HMAC_MD5 transform looks like:
7327df3f5dSRui Paulo *
7427df3f5dSRui Paulo * MD5(K XOR opad, MD5(K XOR ipad, text))
7527df3f5dSRui Paulo *
7627df3f5dSRui Paulo * where K is an n byte key
7727df3f5dSRui Paulo * ipad is the byte 0x36 repeated 64 times
7827df3f5dSRui Paulo * opad is the byte 0x5c repeated 64 times
7927df3f5dSRui Paulo * and text is the data being protected
8027df3f5dSRui Paulo */
8127df3f5dSRui Paulo
8227df3f5dSRui Paulo /* start out by storing key in pads */
83*ee67461eSJoseph Mingrone memset(k_ipad, 0, sizeof(k_ipad));
84*ee67461eSJoseph Mingrone memset(k_opad, 0, sizeof(k_opad));
8527df3f5dSRui Paulo memcpy(k_ipad, key, key_len);
8627df3f5dSRui Paulo memcpy(k_opad, key, key_len);
8727df3f5dSRui Paulo
8827df3f5dSRui Paulo /* XOR key with ipad and opad values */
8927df3f5dSRui Paulo for (i=0; i<64; i++) {
9027df3f5dSRui Paulo k_ipad[i] ^= 0x36;
9127df3f5dSRui Paulo k_opad[i] ^= 0x5c;
9227df3f5dSRui Paulo }
9327df3f5dSRui Paulo
9427df3f5dSRui Paulo /*
9527df3f5dSRui Paulo * perform inner MD5
9627df3f5dSRui Paulo */
9727df3f5dSRui Paulo MD5_Init(&context); /* init context for 1st pass */
9827df3f5dSRui Paulo MD5_Update(&context, k_ipad, 64); /* start with inner pad */
9927df3f5dSRui Paulo MD5_Update(&context, text, text_len); /* then text of datagram */
10027df3f5dSRui Paulo MD5_Final(digest, &context); /* finish up 1st pass */
10127df3f5dSRui Paulo
10227df3f5dSRui Paulo /*
10327df3f5dSRui Paulo * perform outer MD5
10427df3f5dSRui Paulo */
10527df3f5dSRui Paulo MD5_Init(&context); /* init context for 2nd pass */
10627df3f5dSRui Paulo MD5_Update(&context, k_opad, 64); /* start with outer pad */
10727df3f5dSRui Paulo MD5_Update(&context, digest, 16); /* then results of 1st hash */
10827df3f5dSRui Paulo MD5_Final(digest, &context); /* finish up 2nd pass */
10927df3f5dSRui Paulo }
110*ee67461eSJoseph Mingrone DIAG_ON_DEPRECATION
11127df3f5dSRui Paulo
11227df3f5dSRui Paulo /*
11327df3f5dSRui Paulo * Verify a cryptographic signature of the packet.
11427df3f5dSRui Paulo * Currently only MD5 is supported.
11527df3f5dSRui Paulo */
11627df3f5dSRui Paulo int
signature_verify(netdissect_options * ndo,const u_char * pptr,u_int plen,const u_char * sig_ptr,void (* clear_rtn)(void *),const void * clear_arg)1173340d773SGleb Smirnoff signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen,
1183340d773SGleb Smirnoff const u_char *sig_ptr, void (*clear_rtn)(void *),
1193340d773SGleb Smirnoff const void *clear_arg)
12027df3f5dSRui Paulo {
1213340d773SGleb Smirnoff uint8_t *packet_copy, *sig_copy;
1223c602fabSXin LI uint8_t sig[16];
12327df3f5dSRui Paulo unsigned int i;
12427df3f5dSRui Paulo
1253c602fabSXin LI if (!ndo->ndo_sigsecret) {
12627df3f5dSRui Paulo return (CANT_CHECK_SIGNATURE);
12727df3f5dSRui Paulo }
12827df3f5dSRui Paulo
1293340d773SGleb Smirnoff /*
1303340d773SGleb Smirnoff * Do we have all the packet data to be checked?
1313340d773SGleb Smirnoff */
132*ee67461eSJoseph Mingrone if (!ND_TTEST_LEN(pptr, plen)) {
1333340d773SGleb Smirnoff /* No. */
1343340d773SGleb Smirnoff return (CANT_CHECK_SIGNATURE);
1353340d773SGleb Smirnoff }
1363340d773SGleb Smirnoff
1373340d773SGleb Smirnoff /*
1383340d773SGleb Smirnoff * Do we have the entire signature to check?
1393340d773SGleb Smirnoff */
140*ee67461eSJoseph Mingrone if (!ND_TTEST_LEN(sig_ptr, sizeof(sig))) {
1413340d773SGleb Smirnoff /* No. */
1423340d773SGleb Smirnoff return (CANT_CHECK_SIGNATURE);
1433340d773SGleb Smirnoff }
1443340d773SGleb Smirnoff if (sig_ptr + sizeof(sig) > pptr + plen) {
1453340d773SGleb Smirnoff /* No. */
1463340d773SGleb Smirnoff return (CANT_CHECK_SIGNATURE);
1473340d773SGleb Smirnoff }
1483340d773SGleb Smirnoff
1493340d773SGleb Smirnoff /*
1503340d773SGleb Smirnoff * Make a copy of the packet, so we don't overwrite the original.
1513340d773SGleb Smirnoff */
1523340d773SGleb Smirnoff packet_copy = malloc(plen);
1533340d773SGleb Smirnoff if (packet_copy == NULL) {
1543340d773SGleb Smirnoff return (CANT_ALLOCATE_COPY);
1553340d773SGleb Smirnoff }
1563340d773SGleb Smirnoff
1573340d773SGleb Smirnoff memcpy(packet_copy, pptr, plen);
1583340d773SGleb Smirnoff
1593340d773SGleb Smirnoff /*
1603340d773SGleb Smirnoff * Clear the signature in the copy.
1613340d773SGleb Smirnoff */
1623340d773SGleb Smirnoff sig_copy = packet_copy + (sig_ptr - pptr);
1633340d773SGleb Smirnoff memset(sig_copy, 0, sizeof(sig));
1643340d773SGleb Smirnoff
1653340d773SGleb Smirnoff /*
1663340d773SGleb Smirnoff * Clear anything else that needs to be cleared in the copy.
1673340d773SGleb Smirnoff * Our caller is assumed to have vetted the clear_arg pointer.
1683340d773SGleb Smirnoff */
1693340d773SGleb Smirnoff (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr)));
1703340d773SGleb Smirnoff
1713340d773SGleb Smirnoff /*
1723340d773SGleb Smirnoff * Compute the signature.
1733340d773SGleb Smirnoff */
1743340d773SGleb Smirnoff signature_compute_hmac_md5(packet_copy, plen,
1753340d773SGleb Smirnoff (unsigned char *)ndo->ndo_sigsecret,
1763c602fabSXin LI strlen(ndo->ndo_sigsecret), sig);
17727df3f5dSRui Paulo
1783340d773SGleb Smirnoff /*
1793340d773SGleb Smirnoff * Free the copy.
1803340d773SGleb Smirnoff */
1813340d773SGleb Smirnoff free(packet_copy);
1823340d773SGleb Smirnoff
1833340d773SGleb Smirnoff /*
1843340d773SGleb Smirnoff * Does the computed signature match the signature in the packet?
1853340d773SGleb Smirnoff */
1863340d773SGleb Smirnoff if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) {
1873340d773SGleb Smirnoff /* Yes. */
18827df3f5dSRui Paulo return (SIGNATURE_VALID);
18927df3f5dSRui Paulo } else {
1903340d773SGleb Smirnoff /* No - print the computed signature. */
19127df3f5dSRui Paulo for (i = 0; i < sizeof(sig); ++i) {
192*ee67461eSJoseph Mingrone ND_PRINT("%02x", sig[i]);
19327df3f5dSRui Paulo }
19427df3f5dSRui Paulo
19527df3f5dSRui Paulo return (SIGNATURE_INVALID);
19627df3f5dSRui Paulo }
19727df3f5dSRui Paulo }
1983340d773SGleb Smirnoff #else
1993340d773SGleb Smirnoff int
signature_verify(netdissect_options * ndo _U_,const u_char * pptr _U_,u_int plen _U_,const u_char * sig_ptr _U_,void (* clear_rtn)(void *)_U_,const void * clear_arg _U_)2003340d773SGleb Smirnoff signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_,
2013340d773SGleb Smirnoff u_int plen _U_, const u_char *sig_ptr _U_,
2023340d773SGleb Smirnoff void (*clear_rtn)(void *) _U_, const void *clear_arg _U_)
2033340d773SGleb Smirnoff {
2043340d773SGleb Smirnoff return (CANT_CHECK_SIGNATURE);
2053340d773SGleb Smirnoff }
20627df3f5dSRui Paulo #endif
207