]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
Added support for Win32, based on WinPcap.
[tcpdump] / print-esp.c
1 /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #ifndef lint
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.28 2002-08-01 08:53:05 risso Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34
35 #include <tcpdump-stdinc.h>
36
37 #include <stdlib.h>
38
39 #ifdef HAVE_LIBCRYPTO
40 #include <openssl/des.h>
41 #include <openssl/blowfish.h>
42 #ifdef HAVE_OPENSSL_RC5_H
43 #include <openssl/rc5.h>
44 #endif
45 #ifdef HAVE_OPENSSL_CAST_H
46 #include <openssl/cast.h>
47 #endif
48 #endif
49
50 #include <stdio.h>
51
52 #include "ip.h"
53 #include "esp.h"
54 #ifdef INET6
55 #include "ip6.h"
56 #endif
57
58 #define AVOID_CHURN 1
59 #include "interface.h"
60 #include "addrtoname.h"
61
62 static struct esp_algorithm *espsecret_xform=NULL; /* cache of decoded alg. */
63 static char *espsecret_key=NULL;
64
65
66 enum cipher { NONE,
67 DESCBC,
68 BLOWFISH,
69 RC5,
70 CAST128,
71 DES3CBC};
72
73
74
75 struct esp_algorithm {
76 char *name;
77 enum cipher algo;
78 int ivlen;
79 int authlen;
80 int replaysize;
81 };
82
83 struct esp_algorithm esp_xforms[]={
84 {"none", NONE, 0, 0, 0},
85 {"des-cbc", DESCBC, 8, 0, 0},
86 {"des-cbc-hmac96", DESCBC, 8, 12, 4},
87 {"blowfish-cbc", BLOWFISH,8, 0, 0},
88 {"blowfish-cbc-hmac96", BLOWFISH,8, 12, 4},
89 {"rc5-cbc", RC5, 8, 0, 0},
90 {"rc5-cbc-hmac96", RC5, 8, 12, 4},
91 {"cast128-cbc", CAST128, 8, 0, 0},
92 {"cast128-cbc-hmac96", CAST128, 8, 12, 4},
93 {"3des-cbc-hmac96", DES3CBC, 8, 12, 4},
94 {NULL, NONE, 0, 0, 0}
95 };
96
97 static int hexdigit(char hex)
98 {
99 if(hex >= '0' && hex <= '9') {
100 return (hex - '0');
101 } else if(hex >= 'A' && hex <= 'F') {
102 return (hex - 'A' + 10);
103 } else if(hex >= 'a' && hex <= 'f') {
104 return (hex - 'a' + 10);
105 } else {
106 printf("invalid hex digit %c in espsecret\n", hex);
107 return 0;
108 }
109 }
110
111 static int hex2byte(char *hexstring)
112 {
113 int byte;
114
115 byte = (hexdigit(hexstring[0]) << 4) +
116 hexdigit(hexstring[1]);
117 return byte;
118 }
119
120
121 static void esp_print_decodesecret(void)
122 {
123 char *colon;
124 int len, i;
125 struct esp_algorithm *xf;
126 struct esp_algorithm *null_xf =
127 &esp_xforms[sizeof(esp_xforms)/sizeof(esp_xforms[0]) - 1];
128
129 if(espsecret == NULL) {
130 /* set to NULL transform */
131 espsecret_xform = null_xf;
132 return;
133 }
134
135 if(espsecret_key != NULL) {
136 return;
137 }
138
139 colon = strchr(espsecret, ':');
140 if(colon == NULL) {
141 printf("failed to decode espsecret: %s\n",
142 espsecret);
143 /* set to NULL transform */
144 espsecret_xform = null_xf;
145 return;
146 }
147
148 len = colon - espsecret;
149 xf = esp_xforms;
150 while(xf->name && strncasecmp(espsecret, xf->name, len)!=0) {
151 xf++;
152 }
153 if(xf->name == NULL) {
154 printf("failed to find cipher algo %s\n",
155 espsecret);
156 /* set to NULL transform */
157 espsecret_xform = null_xf;
158 return;
159 }
160 espsecret_xform = xf;
161
162 colon++;
163 if(colon[0]=='0' && colon[1]=='x') {
164 /* decode some hex! */
165 colon+=2;
166 len = strlen(colon) / 2;
167 espsecret_key = (char *)malloc(len);
168 if(espsecret_key == NULL) {
169 fprintf(stderr, "%s: ran out of memory (%d) to allocate secret key\n",
170 program_name, len);
171 exit(2);
172 }
173 i = 0;
174 while(colon[0] != '\0' && colon[1]!='\0') {
175 espsecret_key[i]=hex2byte(colon);
176 colon+=2;
177 i++;
178 }
179 } else {
180 espsecret_key = colon;
181 }
182 }
183
184 int
185 esp_print(register const u_char *bp, register const u_char *bp2,
186 int *nhdr, int *padlen)
187 {
188 register const struct newesp *esp;
189 register const u_char *ep;
190 struct ip *ip = NULL;
191 #ifdef INET6
192 struct ip6_hdr *ip6 = NULL;
193 #endif
194 int advance;
195 int len;
196 char *secret;
197 int ivlen = 0;
198 u_char *ivoff;
199 #ifdef HAVE_LIBCRYPTO
200 u_char *p;
201 #endif
202
203 esp = (struct newesp *)bp;
204 secret = NULL;
205
206 #if 0
207 /* keep secret out of a register */
208 p = (u_char *)&secret;
209 #endif
210
211 /* 'ep' points to the end of available data. */
212 ep = snapend;
213
214 if ((u_char *)(esp + 1) >= ep) {
215 fputs("[|ESP]", stdout);
216 goto fail;
217 }
218 printf("ESP(spi=0x%08x", (u_int32_t)ntohl(esp->esp_spi));
219 printf(",seq=0x%x", (u_int32_t)ntohl(esp->esp_seq));
220 printf(")");
221
222 /* if we don't have decryption key, we can't decrypt this packet. */
223 if (!espsecret)
224 goto fail;
225
226 if(!espsecret_xform) {
227 esp_print_decodesecret();
228 }
229 if(espsecret_xform->name == NULL) {
230 goto fail;
231 }
232
233 ip = (struct ip *)bp2;
234 switch (IP_V(ip)) {
235 #ifdef INET6
236 case 6:
237 ip6 = (struct ip6_hdr *)bp2;
238 ip = NULL;
239 /* we do not attempt to decrypt jumbograms */
240 if (!ntohs(ip6->ip6_plen))
241 goto fail;
242 /* if we can't get nexthdr, we do not need to decrypt it */
243 len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
244 break;
245 #endif /*INET6*/
246 case 4:
247 /* nexthdr & padding are in the last fragment */
248 if (ntohs(ip->ip_off) & IP_MF)
249 goto fail;
250 #ifdef INET6
251 ip6 = NULL;
252 #endif
253 len = ntohs(ip->ip_len);
254 break;
255 default:
256 goto fail;
257 }
258
259 /* if we can't get nexthdr, we do not need to decrypt it */
260 if (ep - bp2 < len)
261 goto fail;
262
263 ivoff = (u_char *)(esp + 1) + espsecret_xform->replaysize;
264 ivlen = espsecret_xform->ivlen;
265 secret = espsecret_key;
266
267 switch (espsecret_xform->algo) {
268 case DESCBC:
269 #ifdef HAVE_LIBCRYPTO
270 {
271 u_char iv[8];
272 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
273 DES_key_schedule schedule;
274 #else
275 des_key_schedule schedule;
276 #endif
277
278 switch (ivlen) {
279 case 4:
280 memcpy(iv, ivoff, 4);
281 memcpy(&iv[4], ivoff, 4);
282 p = &iv[4];
283 *p++ ^= 0xff;
284 *p++ ^= 0xff;
285 *p++ ^= 0xff;
286 *p++ ^= 0xff;
287 break;
288 case 8:
289 memcpy(iv, ivoff, 8);
290 break;
291 default:
292 goto fail;
293 }
294 p = ivoff + ivlen;
295
296 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
297 DES_set_key_unchecked((DES_cblock *)secret, schedule);
298
299 DES_cbc_encrypt((const unsigned char *)p, p,
300 (long)(ep - p), schedule, (DES_cblock *)iv,
301 DES_DECRYPT);
302 #else
303 des_check_key = 0;
304 des_set_key((void *)secret, schedule);
305
306 des_cbc_encrypt((void *)p, (void *)p,
307 (long)(ep - p), schedule, (void *)iv,
308 DES_DECRYPT);
309 #endif
310 advance = ivoff - (u_char *)esp + ivlen;
311 break;
312 }
313 #else
314 goto fail;
315 #endif /*HAVE_LIBCRYPTO*/
316
317 case BLOWFISH:
318 #ifdef HAVE_LIBCRYPTO
319 {
320 BF_KEY schedule;
321
322 BF_set_key(&schedule, strlen(secret), secret);
323
324 p = ivoff + ivlen;
325 BF_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
326 BF_DECRYPT);
327 advance = ivoff - (u_char *)esp + ivlen;
328 break;
329 }
330 #else
331 goto fail;
332 #endif /*HAVE_LIBCRYPTO*/
333
334 case RC5:
335 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
336 {
337 RC5_32_KEY schedule;
338
339 RC5_32_set_key(&schedule, strlen(secret), secret,
340 RC5_16_ROUNDS);
341
342 p = ivoff + ivlen;
343 RC5_32_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
344 RC5_DECRYPT);
345 advance = ivoff - (u_char *)esp + ivlen;
346 break;
347 }
348 #else
349 goto fail;
350 #endif /*HAVE_LIBCRYPTO*/
351
352 case CAST128:
353 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
354 {
355 CAST_KEY schedule;
356
357 CAST_set_key(&schedule, strlen(secret), secret);
358
359 p = ivoff + ivlen;
360 CAST_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
361 CAST_DECRYPT);
362 advance = ivoff - (u_char *)esp + ivlen;
363 break;
364 }
365 #else
366 goto fail;
367 #endif /*HAVE_LIBCRYPTO*/
368
369 case DES3CBC:
370 #if defined(HAVE_LIBCRYPTO)
371 {
372 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
373 DES_key_schedule s1, s2, s3;
374
375 DES_set_odd_parity((DES_cblock *)secret);
376 DES_set_odd_parity((DES_cblock *)(secret + 8));
377 DES_set_odd_parity((DES_cblock *)(secret + 16));
378 if(DES_set_key_checked((DES_cblock *)secret, s1) != 0) {
379 printf("failed to schedule key 1\n");
380 }
381 if(DES_set_key_checked((DES_cblock *)(secret + 8), s2)!=0) {
382 printf("failed to schedule key 2\n");
383 }
384 if(DES_set_key_checked((DES_cblock *)(secret + 16), s3)!=0) {
385 printf("failed to schedule key 3\n");
386 }
387
388 p = ivoff + ivlen;
389 DES_ede3_cbc_encrypt((const unsigned char *)p, p,
390 (long)(ep - p),
391 &s1, &s2, &s3,
392 (DES_cblock *)ivoff, DES_DECRYPT);
393 #else
394 des_key_schedule s1, s2, s3;
395
396 des_check_key = 1;
397 des_set_odd_parity((void *)secret);
398 des_set_odd_parity((void *)(secret + 8));
399 des_set_odd_parity((void *)(secret + 16));
400 if(des_set_key((void *)secret, s1) != 0) {
401 printf("failed to schedule key 1\n");
402 }
403 if(des_set_key((void *)(secret + 8), s2)!=0) {
404 printf("failed to schedule key 2\n");
405 }
406 if(des_set_key((void *)(secret + 16), s3)!=0) {
407 printf("failed to schedule key 3\n");
408 }
409
410 p = ivoff + ivlen;
411 des_ede3_cbc_encrypt((void *)p, (void *)p,
412 (long)(ep - p),
413 s1, s2, s3,
414 (void *)ivoff, DES_DECRYPT);
415 #endif
416 advance = ivoff - (u_char *)esp + ivlen;
417 break;
418 }
419 #else
420 goto fail;
421 #endif /*HAVE_LIBCRYPTO*/
422
423 case NONE:
424 default:
425 advance = sizeof(struct newesp) + espsecret_xform->replaysize;
426 break;
427 }
428
429 ep = ep - espsecret_xform->authlen;
430 /* sanity check for pad length */
431 if (ep - bp < *(ep - 2))
432 goto fail;
433
434 if (padlen)
435 *padlen = *(ep - 2) + 2;
436
437 if (nhdr)
438 *nhdr = *(ep - 1);
439
440 printf(": ");
441 return advance;
442
443 fail:
444 if (nhdr)
445 *nhdr = -1;
446 return 65536;
447 }