xref: /freebsd/contrib/tcpdump/signature.c (revision 3c602fabf9b894ff79f08a80cbb7ad3b1eb84e62)
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  *
1527df3f5dSRui Paulo  * Original code by Hannes Gredler (hannes@juniper.net)
1627df3f5dSRui Paulo  */
1727df3f5dSRui Paulo 
18*3c602fabSXin LI #define NETDISSECT_REWORKED
1927df3f5dSRui Paulo #ifdef HAVE_CONFIG_H
2027df3f5dSRui Paulo #include "config.h"
2127df3f5dSRui Paulo #endif
2227df3f5dSRui Paulo 
2327df3f5dSRui Paulo #include <tcpdump-stdinc.h>
2427df3f5dSRui Paulo 
2527df3f5dSRui Paulo #include <string.h>
2627df3f5dSRui Paulo 
2727df3f5dSRui Paulo #include "interface.h"
2827df3f5dSRui Paulo #include "signature.h"
2927df3f5dSRui Paulo 
3027df3f5dSRui Paulo #ifdef HAVE_LIBCRYPTO
3127df3f5dSRui Paulo #include <openssl/md5.h>
3227df3f5dSRui Paulo #endif
3327df3f5dSRui Paulo 
3427df3f5dSRui Paulo const struct tok signature_check_values[] = {
3527df3f5dSRui Paulo     { SIGNATURE_VALID, "valid"},
3627df3f5dSRui Paulo     { SIGNATURE_INVALID, "invalid"},
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*3c602fabSXin LI USES_APPLE_DEPRECATED_API
4827df3f5dSRui Paulo static void
49*3c602fabSXin LI signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
50*3c602fabSXin 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 */
8327df3f5dSRui Paulo     memset(k_ipad, 0, sizeof k_ipad);
8427df3f5dSRui Paulo     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*3c602fabSXin LI USES_APPLE_RST
11127df3f5dSRui Paulo #endif
11227df3f5dSRui Paulo 
11327df3f5dSRui Paulo #ifdef HAVE_LIBCRYPTO
11427df3f5dSRui Paulo /*
11527df3f5dSRui Paulo  * Verify a cryptographic signature of the packet.
11627df3f5dSRui Paulo  * Currently only MD5 is supported.
11727df3f5dSRui Paulo  */
11827df3f5dSRui Paulo int
119*3c602fabSXin LI signature_verify(netdissect_options *ndo,
120*3c602fabSXin LI                  const u_char *pptr, u_int plen, u_char *sig_ptr)
12127df3f5dSRui Paulo {
122*3c602fabSXin LI     uint8_t rcvsig[16];
123*3c602fabSXin LI     uint8_t sig[16];
12427df3f5dSRui Paulo     unsigned int i;
12527df3f5dSRui Paulo 
12627df3f5dSRui Paulo     /*
12727df3f5dSRui Paulo      * Save the signature before clearing it.
12827df3f5dSRui Paulo      */
12927df3f5dSRui Paulo     memcpy(rcvsig, sig_ptr, sizeof(rcvsig));
13027df3f5dSRui Paulo     memset(sig_ptr, 0, sizeof(rcvsig));
13127df3f5dSRui Paulo 
132*3c602fabSXin LI     if (!ndo->ndo_sigsecret) {
13327df3f5dSRui Paulo         return (CANT_CHECK_SIGNATURE);
13427df3f5dSRui Paulo     }
13527df3f5dSRui Paulo 
136*3c602fabSXin LI     signature_compute_hmac_md5(pptr, plen, (unsigned char *)ndo->ndo_sigsecret,
137*3c602fabSXin LI                                strlen(ndo->ndo_sigsecret), sig);
13827df3f5dSRui Paulo 
13927df3f5dSRui Paulo     if (memcmp(rcvsig, sig, sizeof(sig)) == 0) {
14027df3f5dSRui Paulo         return (SIGNATURE_VALID);
14127df3f5dSRui Paulo 
14227df3f5dSRui Paulo     } else {
14327df3f5dSRui Paulo 
14427df3f5dSRui Paulo         for (i = 0; i < sizeof(sig); ++i) {
145*3c602fabSXin LI             ND_PRINT((ndo, "%02x", sig[i]));
14627df3f5dSRui Paulo         }
14727df3f5dSRui Paulo 
14827df3f5dSRui Paulo         return (SIGNATURE_INVALID);
14927df3f5dSRui Paulo     }
15027df3f5dSRui Paulo }
15127df3f5dSRui Paulo #endif
15227df3f5dSRui Paulo 
15327df3f5dSRui Paulo /*
15427df3f5dSRui Paulo  * Local Variables:
15527df3f5dSRui Paulo  * c-style: whitesmith
15627df3f5dSRui Paulo  * c-basic-offset: 4
15727df3f5dSRui Paulo  * End:
15827df3f5dSRui Paulo  */
159