xref: /freebsd/contrib/tcpdump/signature.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that: (1) source code
4  * distributions retain the above copyright notice and this paragraph
5  * in its entirety, and (2) distributions including binary code include
6  * the above copyright notice and this paragraph in its entirety in
7  * the documentation or other materials provided with the distribution.
8  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11  * FOR A PARTICULAR PURPOSE.
12  *
13  * Functions for signature and digest verification.
14  *
15  * Original code by Hannes Gredler (hannes@gredler.at)
16  */
17 
18 #include <config.h>
19 
20 #include "netdissect-stdinc.h"
21 
22 #include <string.h>
23 #include <stdlib.h>
24 
25 #include "netdissect.h"
26 #include "signature.h"
27 #include "diag-control.h"
28 
29 #ifdef HAVE_LIBCRYPTO
30 #include <openssl/md5.h>
31 #endif
32 
33 const struct tok signature_check_values[] = {
34     { SIGNATURE_VALID, "valid"},
35     { SIGNATURE_INVALID, "invalid"},
36     { CANT_ALLOCATE_COPY, "can't allocate memory"},
37     { CANT_CHECK_SIGNATURE, "unchecked"},
38     { 0, NULL }
39 };
40 
41 
42 #ifdef HAVE_LIBCRYPTO
43 /*
44  * Compute a HMAC MD5 sum.
45  * Taken from rfc2104, Appendix.
46  */
47 DIAG_OFF_DEPRECATION
48 static void
49 signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
50                            unsigned int key_len, uint8_t *digest)
51 {
52     MD5_CTX context;
53     unsigned char k_ipad[65];    /* inner padding - key XORd with ipad */
54     unsigned char k_opad[65];    /* outer padding - key XORd with opad */
55     unsigned char tk[16];
56     int i;
57 
58     /* if key is longer than 64 bytes reset it to key=MD5(key) */
59     if (key_len > 64) {
60 
61         MD5_CTX tctx;
62 
63         MD5_Init(&tctx);
64         MD5_Update(&tctx, key, key_len);
65         MD5_Final(tk, &tctx);
66 
67         key = tk;
68         key_len = 16;
69     }
70 
71     /*
72      * the HMAC_MD5 transform looks like:
73      *
74      * MD5(K XOR opad, MD5(K XOR ipad, text))
75      *
76      * where K is an n byte key
77      * ipad is the byte 0x36 repeated 64 times
78      * opad is the byte 0x5c repeated 64 times
79      * and text is the data being protected
80      */
81 
82     /* start out by storing key in pads */
83     memset(k_ipad, 0, sizeof(k_ipad));
84     memset(k_opad, 0, sizeof(k_opad));
85     memcpy(k_ipad, key, key_len);
86     memcpy(k_opad, key, key_len);
87 
88     /* XOR key with ipad and opad values */
89     for (i=0; i<64; i++) {
90         k_ipad[i] ^= 0x36;
91         k_opad[i] ^= 0x5c;
92     }
93 
94     /*
95      * perform inner MD5
96      */
97     MD5_Init(&context);                   /* init context for 1st pass */
98     MD5_Update(&context, k_ipad, 64);     /* start with inner pad */
99     MD5_Update(&context, text, text_len); /* then text of datagram */
100     MD5_Final(digest, &context);          /* finish up 1st pass */
101 
102     /*
103      * perform outer MD5
104      */
105     MD5_Init(&context);                   /* init context for 2nd pass */
106     MD5_Update(&context, k_opad, 64);     /* start with outer pad */
107     MD5_Update(&context, digest, 16);     /* then results of 1st hash */
108     MD5_Final(digest, &context);          /* finish up 2nd pass */
109 }
110 DIAG_ON_DEPRECATION
111 
112 /*
113  * Verify a cryptographic signature of the packet.
114  * Currently only MD5 is supported.
115  */
116 int
117 signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen,
118                  const u_char *sig_ptr, void (*clear_rtn)(void *),
119                  const void *clear_arg)
120 {
121     uint8_t *packet_copy, *sig_copy;
122     uint8_t sig[16];
123     unsigned int i;
124 
125     if (!ndo->ndo_sigsecret) {
126         return (CANT_CHECK_SIGNATURE);
127     }
128 
129     /*
130      * Do we have all the packet data to be checked?
131      */
132     if (!ND_TTEST_LEN(pptr, plen)) {
133         /* No. */
134         return (CANT_CHECK_SIGNATURE);
135     }
136 
137     /*
138      * Do we have the entire signature to check?
139      */
140     if (!ND_TTEST_LEN(sig_ptr, sizeof(sig))) {
141         /* No. */
142         return (CANT_CHECK_SIGNATURE);
143     }
144     if (sig_ptr + sizeof(sig) > pptr + plen) {
145         /* No. */
146         return (CANT_CHECK_SIGNATURE);
147     }
148 
149     /*
150      * Make a copy of the packet, so we don't overwrite the original.
151      */
152     packet_copy = malloc(plen);
153     if (packet_copy == NULL) {
154         return (CANT_ALLOCATE_COPY);
155     }
156 
157     memcpy(packet_copy, pptr, plen);
158 
159     /*
160      * Clear the signature in the copy.
161      */
162     sig_copy = packet_copy + (sig_ptr - pptr);
163     memset(sig_copy, 0, sizeof(sig));
164 
165     /*
166      * Clear anything else that needs to be cleared in the copy.
167      * Our caller is assumed to have vetted the clear_arg pointer.
168      */
169     (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr)));
170 
171     /*
172      * Compute the signature.
173      */
174     signature_compute_hmac_md5(packet_copy, plen,
175                                (unsigned char *)ndo->ndo_sigsecret,
176                                strlen(ndo->ndo_sigsecret), sig);
177 
178     /*
179      * Free the copy.
180      */
181     free(packet_copy);
182 
183     /*
184      * Does the computed signature match the signature in the packet?
185      */
186     if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) {
187         /* Yes. */
188         return (SIGNATURE_VALID);
189     } else {
190         /* No - print the computed signature. */
191         for (i = 0; i < sizeof(sig); ++i) {
192             ND_PRINT("%02x", sig[i]);
193         }
194 
195         return (SIGNATURE_INVALID);
196     }
197 }
198 #else
199 int
200 signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_,
201                  u_int plen _U_, const u_char *sig_ptr _U_,
202                  void (*clear_rtn)(void *) _U_, const void *clear_arg _U_)
203 {
204     return (CANT_CHECK_SIGNATURE);
205 }
206 #endif
207