Merge illumos-gate
[unleashed.git] / contrib / tcpdump / signature.c
blob2091afb3e8e5ff0ccc63ab105d49ed76edbbdc6b
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.
13 * Functions for signature and digest verification.
15 * Original code by Hannes Gredler (hannes@gredler.at)
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include <netdissect-stdinc.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "netdissect.h"
28 #include "signature.h"
30 #ifdef HAVE_LIBCRYPTO
31 #include <openssl/md5.h>
32 #endif
34 const struct tok signature_check_values[] = {
35 { SIGNATURE_VALID, "valid"},
36 { SIGNATURE_INVALID, "invalid"},
37 { CANT_ALLOCATE_COPY, "can't allocate memory"},
38 { CANT_CHECK_SIGNATURE, "unchecked"},
39 { 0, NULL }
43 #ifdef HAVE_LIBCRYPTO
45 * Compute a HMAC MD5 sum.
46 * Taken from rfc2104, Appendix.
48 USES_APPLE_DEPRECATED_API
49 static void
50 signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
51 unsigned int key_len, uint8_t *digest)
53 MD5_CTX context;
54 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
55 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
56 unsigned char tk[16];
57 int i;
59 /* if key is longer than 64 bytes reset it to key=MD5(key) */
60 if (key_len > 64) {
62 MD5_CTX tctx;
64 MD5_Init(&tctx);
65 MD5_Update(&tctx, key, key_len);
66 MD5_Final(tk, &tctx);
68 key = tk;
69 key_len = 16;
73 * the HMAC_MD5 transform looks like:
75 * MD5(K XOR opad, MD5(K XOR ipad, text))
77 * where K is an n byte key
78 * ipad is the byte 0x36 repeated 64 times
79 * opad is the byte 0x5c repeated 64 times
80 * and text is the data being protected
83 /* start out by storing key in pads */
84 memset(k_ipad, 0, sizeof k_ipad);
85 memset(k_opad, 0, sizeof k_opad);
86 memcpy(k_ipad, key, key_len);
87 memcpy(k_opad, key, key_len);
89 /* XOR key with ipad and opad values */
90 for (i=0; i<64; i++) {
91 k_ipad[i] ^= 0x36;
92 k_opad[i] ^= 0x5c;
96 * perform inner MD5
98 MD5_Init(&context); /* init context for 1st pass */
99 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
100 MD5_Update(&context, text, text_len); /* then text of datagram */
101 MD5_Final(digest, &context); /* finish up 1st pass */
104 * perform outer MD5
106 MD5_Init(&context); /* init context for 2nd pass */
107 MD5_Update(&context, k_opad, 64); /* start with outer pad */
108 MD5_Update(&context, digest, 16); /* then results of 1st hash */
109 MD5_Final(digest, &context); /* finish up 2nd pass */
111 USES_APPLE_RST
114 * Verify a cryptographic signature of the packet.
115 * Currently only MD5 is supported.
118 signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen,
119 const u_char *sig_ptr, void (*clear_rtn)(void *),
120 const void *clear_arg)
122 uint8_t *packet_copy, *sig_copy;
123 uint8_t sig[16];
124 unsigned int i;
126 if (!ndo->ndo_sigsecret) {
127 return (CANT_CHECK_SIGNATURE);
131 * Do we have all the packet data to be checked?
133 if (!ND_TTEST2(pptr, plen)) {
134 /* No. */
135 return (CANT_CHECK_SIGNATURE);
139 * Do we have the entire signature to check?
141 if (!ND_TTEST2(sig_ptr, sizeof(sig))) {
142 /* No. */
143 return (CANT_CHECK_SIGNATURE);
145 if (sig_ptr + sizeof(sig) > pptr + plen) {
146 /* No. */
147 return (CANT_CHECK_SIGNATURE);
151 * Make a copy of the packet, so we don't overwrite the original.
153 packet_copy = malloc(plen);
154 if (packet_copy == NULL) {
155 return (CANT_ALLOCATE_COPY);
158 memcpy(packet_copy, pptr, plen);
161 * Clear the signature in the copy.
163 sig_copy = packet_copy + (sig_ptr - pptr);
164 memset(sig_copy, 0, sizeof(sig));
167 * Clear anything else that needs to be cleared in the copy.
168 * Our caller is assumed to have vetted the clear_arg pointer.
170 (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr)));
173 * Compute the signature.
175 signature_compute_hmac_md5(packet_copy, plen,
176 (unsigned char *)ndo->ndo_sigsecret,
177 strlen(ndo->ndo_sigsecret), sig);
180 * Free the copy.
182 free(packet_copy);
185 * Does the computed signature match the signature in the packet?
187 if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) {
188 /* Yes. */
189 return (SIGNATURE_VALID);
190 } else {
191 /* No - print the computed signature. */
192 for (i = 0; i < sizeof(sig); ++i) {
193 ND_PRINT((ndo, "%02x", sig[i]));
196 return (SIGNATURE_INVALID);
199 #else
201 signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_,
202 u_int plen _U_, const u_char *sig_ptr _U_,
203 void (*clear_rtn)(void *) _U_, const void *clear_arg _U_)
205 return (CANT_CHECK_SIGNATURE);
207 #endif
210 * Local Variables:
211 * c-style: whitesmith
212 * c-basic-offset: 4
213 * End: