signature.c revision 1.1 1 1.1 christos /*
2 1.1 christos * Redistribution and use in source and binary forms, with or without
3 1.1 christos * modification, are permitted provided that: (1) source code
4 1.1 christos * distributions retain the above copyright notice and this paragraph
5 1.1 christos * in its entirety, and (2) distributions including binary code include
6 1.1 christos * the above copyright notice and this paragraph in its entirety in
7 1.1 christos * the documentation or other materials provided with the distribution.
8 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 1.1 christos * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10 1.1 christos * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 1.1 christos * FOR A PARTICULAR PURPOSE.
12 1.1 christos *
13 1.1 christos * Functions for signature and digest verification.
14 1.1 christos *
15 1.1 christos * Original code by Hannes Gredler (hannes (at) juniper.net)
16 1.1 christos */
17 1.1 christos
18 1.1 christos #ifndef lint
19 1.1 christos static const char rcsid[] _U_ =
20 1.1 christos "@(#) Header: /tcpdump/master/tcpdump/signature.c,v 1.2 2008-09-22 20:22:10 guy Exp (LBL)";
21 1.1 christos #endif
22 1.1 christos
23 1.1 christos #ifdef HAVE_CONFIG_H
24 1.1 christos #include "config.h"
25 1.1 christos #endif
26 1.1 christos
27 1.1 christos #include <tcpdump-stdinc.h>
28 1.1 christos
29 1.1 christos #include <string.h>
30 1.1 christos
31 1.1 christos #include "interface.h"
32 1.1 christos #include "signature.h"
33 1.1 christos
34 1.1 christos #ifdef HAVE_LIBCRYPTO
35 1.1 christos #include <openssl/md5.h>
36 1.1 christos #endif
37 1.1 christos
38 1.1 christos const struct tok signature_check_values[] = {
39 1.1 christos { SIGNATURE_VALID, "valid"},
40 1.1 christos { SIGNATURE_INVALID, "invalid"},
41 1.1 christos { CANT_CHECK_SIGNATURE, "unchecked"},
42 1.1 christos { 0, NULL }
43 1.1 christos };
44 1.1 christos
45 1.1 christos
46 1.1 christos #ifdef HAVE_LIBCRYPTO
47 1.1 christos /*
48 1.1 christos * Compute a HMAC MD5 sum.
49 1.1 christos * Taken from rfc2104, Appendix.
50 1.1 christos */
51 1.1 christos static void
52 1.1 christos signature_compute_hmac_md5(const u_int8_t *text, int text_len, unsigned char *key,
53 1.1 christos unsigned int key_len, u_int8_t *digest)
54 1.1 christos {
55 1.1 christos MD5_CTX context;
56 1.1 christos unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
57 1.1 christos unsigned char k_opad[65]; /* outer padding - key XORd with opad */
58 1.1 christos unsigned char tk[16];
59 1.1 christos int i;
60 1.1 christos
61 1.1 christos /* if key is longer than 64 bytes reset it to key=MD5(key) */
62 1.1 christos if (key_len > 64) {
63 1.1 christos
64 1.1 christos MD5_CTX tctx;
65 1.1 christos
66 1.1 christos MD5_Init(&tctx);
67 1.1 christos MD5_Update(&tctx, key, key_len);
68 1.1 christos MD5_Final(tk, &tctx);
69 1.1 christos
70 1.1 christos key = tk;
71 1.1 christos key_len = 16;
72 1.1 christos }
73 1.1 christos
74 1.1 christos /*
75 1.1 christos * the HMAC_MD5 transform looks like:
76 1.1 christos *
77 1.1 christos * MD5(K XOR opad, MD5(K XOR ipad, text))
78 1.1 christos *
79 1.1 christos * where K is an n byte key
80 1.1 christos * ipad is the byte 0x36 repeated 64 times
81 1.1 christos * opad is the byte 0x5c repeated 64 times
82 1.1 christos * and text is the data being protected
83 1.1 christos */
84 1.1 christos
85 1.1 christos /* start out by storing key in pads */
86 1.1 christos memset(k_ipad, 0, sizeof k_ipad);
87 1.1 christos memset(k_opad, 0, sizeof k_opad);
88 1.1 christos memcpy(k_ipad, key, key_len);
89 1.1 christos memcpy(k_opad, key, key_len);
90 1.1 christos
91 1.1 christos /* XOR key with ipad and opad values */
92 1.1 christos for (i=0; i<64; i++) {
93 1.1 christos k_ipad[i] ^= 0x36;
94 1.1 christos k_opad[i] ^= 0x5c;
95 1.1 christos }
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * perform inner MD5
99 1.1 christos */
100 1.1 christos MD5_Init(&context); /* init context for 1st pass */
101 1.1 christos MD5_Update(&context, k_ipad, 64); /* start with inner pad */
102 1.1 christos MD5_Update(&context, text, text_len); /* then text of datagram */
103 1.1 christos MD5_Final(digest, &context); /* finish up 1st pass */
104 1.1 christos
105 1.1 christos /*
106 1.1 christos * perform outer MD5
107 1.1 christos */
108 1.1 christos MD5_Init(&context); /* init context for 2nd pass */
109 1.1 christos MD5_Update(&context, k_opad, 64); /* start with outer pad */
110 1.1 christos MD5_Update(&context, digest, 16); /* then results of 1st hash */
111 1.1 christos MD5_Final(digest, &context); /* finish up 2nd pass */
112 1.1 christos }
113 1.1 christos #endif
114 1.1 christos
115 1.1 christos #ifdef HAVE_LIBCRYPTO
116 1.1 christos /*
117 1.1 christos * Verify a cryptographic signature of the packet.
118 1.1 christos * Currently only MD5 is supported.
119 1.1 christos */
120 1.1 christos int
121 1.1 christos signature_verify (const u_char *pptr, u_int plen, u_char *sig_ptr)
122 1.1 christos {
123 1.1 christos u_int8_t rcvsig[16];
124 1.1 christos u_int8_t sig[16];
125 1.1 christos unsigned int i;
126 1.1 christos
127 1.1 christos /*
128 1.1 christos * Save the signature before clearing it.
129 1.1 christos */
130 1.1 christos memcpy(rcvsig, sig_ptr, sizeof(rcvsig));
131 1.1 christos memset(sig_ptr, 0, sizeof(rcvsig));
132 1.1 christos
133 1.1 christos if (!sigsecret) {
134 1.1 christos return (CANT_CHECK_SIGNATURE);
135 1.1 christos }
136 1.1 christos
137 1.1 christos signature_compute_hmac_md5(pptr, plen, (unsigned char *)sigsecret,
138 1.1 christos strlen(sigsecret), sig);
139 1.1 christos
140 1.1 christos if (memcmp(rcvsig, sig, sizeof(sig)) == 0) {
141 1.1 christos return (SIGNATURE_VALID);
142 1.1 christos
143 1.1 christos } else {
144 1.1 christos
145 1.1 christos for (i = 0; i < sizeof(sig); ++i) {
146 1.1 christos (void)printf("%02x", sig[i]);
147 1.1 christos }
148 1.1 christos
149 1.1 christos return (SIGNATURE_INVALID);
150 1.1 christos }
151 1.1 christos }
152 1.1 christos #endif
153 1.1 christos
154 1.1 christos /*
155 1.1 christos * Local Variables:
156 1.1 christos * c-style: whitesmith
157 1.1 christos * c-basic-offset: 4
158 1.1 christos * End:
159 1.1 christos */
160