]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
47829118960a7dfbfa39bf490b07edaf0aef6fa4
[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.34 2003-03-02 23:19:38 guy 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 #if defined(__MINGW32__) || defined(__WATCOMC__)
59 #include "addrinfo.h"
60 extern char *strsep (char **stringp, const char *delim); /* Missing/strsep.c */
61 #endif
62
63 #define AVOID_CHURN 1
64 #include "interface.h"
65 #include "addrtoname.h"
66 #include "extract.h"
67
68 enum cipher { NONE,
69 DESCBC,
70 BLOWFISH,
71 RC5,
72 CAST128,
73 DES3CBC};
74
75
76
77 struct esp_algorithm {
78 const char *name;
79 enum cipher algo;
80 int ivlen;
81 int authlen;
82 int replaysize; /* number of bytes, in excess of 4,
83 may be negative */
84 };
85
86 struct esp_algorithm esp_xforms[]={
87 {"none", NONE, 0, 0, 0},
88 {"des-cbc", DESCBC, 8, 0, 0},
89 {"des-cbc-hmac96", DESCBC, 8, 12, 0},
90 {"blowfish-cbc", BLOWFISH,8, 0, 0},
91 {"blowfish-cbc-hmac96", BLOWFISH,8, 12, 0},
92 {"rc5-cbc", RC5, 8, 0, 0},
93 {"rc5-cbc-hmac96", RC5, 8, 12, 0},
94 {"cast128-cbc", CAST128, 8, 0, 0},
95 {"cast128-cbc-hmac96", CAST128, 8, 12, 0},
96 {"3des-cbc-hmac96", DES3CBC, 8, 12, 0},
97 {NULL, NONE, 0, 0, 0}
98 };
99
100 struct esp_algorithm *null_xf =
101 &esp_xforms[sizeof(esp_xforms)/sizeof(esp_xforms[0]) - 1];
102
103 struct sa_list {
104 struct sa_list *next;
105 uint32_t daddr;
106 uint32_t spi;
107 struct esp_algorithm *xform;
108 char secret[256]; /* is that big enough for all secrets? */
109 int secretlen;
110 };
111
112 static struct sa_list *sa_list_head=NULL;
113 static struct sa_list *sa_default=NULL;
114
115 static void esp_print_addsa(struct sa_list *sa, int sa_def)
116 {
117 /* copy the "sa" */
118
119 struct sa_list *nsa;
120
121 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
122 if(nsa == NULL ) {
123 fprintf(stderr, "%s: ran out of memory (%d) to allocate sa structure\n",
124 program_name, sizeof(struct sa_list));
125 exit(2);
126 }
127
128 *nsa = *sa;
129
130 if(sa_def) {
131 sa_default = nsa;
132 }
133
134 nsa->next = sa_list_head;
135 sa_list_head = nsa;
136 }
137
138
139 static int hexdigit(char hex)
140 {
141 if(hex >= '0' && hex <= '9') {
142 return (hex - '0');
143 } else if(hex >= 'A' && hex <= 'F') {
144 return (hex - 'A' + 10);
145 } else if(hex >= 'a' && hex <= 'f') {
146 return (hex - 'a' + 10);
147 } else {
148 printf("invalid hex digit %c in espsecret\n", hex);
149 return 0;
150 }
151 }
152
153 static int hex2byte(char *hexstring)
154 {
155 int byte;
156
157 byte = (hexdigit(hexstring[0]) << 4) +
158 hexdigit(hexstring[1]);
159 return byte;
160 }
161
162 /*
163 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
164 *
165 * special form: file /name
166 * causes us to go read from this file instead.
167 *
168 */
169
170 static void esp_print_decode_onesecret(char *line)
171 {
172 struct esp_algorithm *xf;
173 struct sa_list sa1;
174 int sa_def;
175
176 char *spikey;
177 char *decode;
178
179 spikey = strsep(&line, " \t");
180 sa_def = 0;
181 memset(&sa1, 0, sizeof(struct sa_list));
182
183 /* if there is only one token, then it is an algo:key token */
184 if(line == NULL) {
185 decode = spikey;
186 spikey = NULL;
187 sa1.daddr = 0;
188 sa1.spi = 0;
189 sa_def = 1;
190 } else {
191 decode = line;
192 }
193
194 if(spikey && strcasecmp(spikey, "file")==0) {
195 /* open file and read it */
196 FILE *secretfile;
197 char fileline[1024];
198 char *nl;
199
200 secretfile = fopen(line, FOPEN_READ_TXT);
201 if(secretfile == NULL) {
202 perror(line);
203 exit(3);
204 }
205
206 while(fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
207
208 /* remove newline from the line */
209 nl = strchr(fileline, '\n');
210 if(nl) {
211 *nl = '\0';
212 }
213 if(fileline[0]=='#') continue;
214 if(fileline[0]=='\0') continue;
215
216 esp_print_decode_onesecret(fileline);
217 }
218 fclose(secretfile);
219
220 return;
221 }
222
223 if(spikey) {
224 char *spistr, *foo;
225 u_int32_t spino;
226 struct in_addr ina;
227
228 spistr = strsep(&spikey, "@");
229
230 spino = strtoul(spistr, &foo, 0);
231 if(spistr == foo) {
232 printf("print_esp: failed to decode spi# %s\n", foo);
233 return;
234 }
235
236 if(inet_pton(AF_INET, spikey, &ina) <= 0) {
237 printf("print_esp: can not decode IP# %s\n", spikey);
238 return;
239 }
240
241 sa1.daddr = ina.s_addr;
242 sa1.spi = spino;
243 }
244
245 if(decode) {
246 char *colon;
247 char espsecret_key[256];
248 unsigned int len, i;
249
250 /* skip any blank spaces */
251 while(isspace(*decode)) decode++;
252
253 colon = strchr(decode, ':');
254 if(colon == NULL) {
255 printf("failed to decode espsecret: %s\n", decode);
256 return;
257 }
258
259 len = colon - decode;
260 xf = esp_xforms;
261 while(xf->name && strncasecmp(decode, xf->name, len)!=0) {
262 xf++;
263 }
264 if(xf->name == NULL) {
265 printf("failed to find cipher algo %s\n", decode);
266 /* set to NULL transform */
267 return;
268 }
269 sa1.xform = xf;
270
271 colon++;
272 if(colon[0]=='0' && colon[1]=='x') {
273 /* decode some hex! */
274 colon+=2;
275 len = strlen(colon) / 2;
276
277 if(len > 256) {
278 printf("secret is too big: %d\n", len);
279 return;
280 }
281
282 i = 0;
283 while(colon[0] != '\0' && colon[1]!='\0') {
284 espsecret_key[i]=hex2byte(colon);
285 colon+=2;
286 i++;
287 }
288 memcpy(sa1.secret, espsecret_key, i);
289 sa1.secretlen=i;
290 } else {
291 i = strlen(colon);
292 if(i < sizeof(sa1.secret)) {
293 memcpy(sa1.secret, espsecret_key, i);
294 sa1.secretlen = i;
295 } else {
296 memcpy(sa1.secret, espsecret_key, sizeof(sa1.secret));
297 sa1.secretlen = sizeof(sa1.secret);
298 }
299 }
300
301 }
302
303 esp_print_addsa(&sa1, sa_def);
304 }
305
306
307 static void esp_print_decodesecret(void)
308 {
309 char *line;
310 char *p;
311
312 if(espsecret == NULL) {
313 sa_list_head = NULL;
314 return;
315 }
316
317 if(sa_list_head != NULL) {
318 return;
319 }
320
321 p=espsecret;
322
323 while(espsecret && espsecret[0]!='\0') {
324 /* pick out the first line or first thing until a comma */
325 if((line = strsep(&espsecret, "\n,"))==NULL) {
326 line=espsecret;
327 espsecret=NULL;
328 }
329
330 esp_print_decode_onesecret(line);
331 }
332 }
333
334 int
335 esp_print(register const u_char *bp, register const u_char *bp2,
336 int *nhdr, int *padlen)
337 {
338 register const struct newesp *esp;
339 register const u_char *ep;
340 struct ip *ip = NULL;
341 struct sa_list *sa;
342 int espsecret_keylen;
343 #ifdef INET6
344 struct ip6_hdr *ip6 = NULL;
345 #endif
346 int advance;
347 int len;
348 char *secret;
349 int ivlen = 0;
350 u_char *ivoff;
351 #ifdef HAVE_LIBCRYPTO
352 u_char *p;
353 #endif
354
355 esp = (struct newesp *)bp;
356 secret = NULL;
357 advance = 0;
358
359 #if 0
360 /* keep secret out of a register */
361 p = (u_char *)&secret;
362 #endif
363
364 /* 'ep' points to the end of available data. */
365 ep = snapend;
366
367 if ((u_char *)(esp + 1) >= ep) {
368 fputs("[|ESP]", stdout);
369 goto fail;
370 }
371 printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi));
372 printf(",seq=0x%x", EXTRACT_32BITS(&esp->esp_seq));
373 printf(")");
374
375 /* if we don't have decryption key, we can't decrypt this packet. */
376 if(sa_list_head == NULL) {
377 if (!espsecret) {
378 goto fail;
379 }
380 esp_print_decodesecret();
381 }
382
383 if(sa_list_head == NULL) {
384 goto fail;
385 }
386
387 ip = (struct ip *)bp2;
388 switch (IP_V(ip)) {
389 #ifdef INET6
390 case 6:
391 ip6 = (struct ip6_hdr *)bp2;
392 ip = NULL;
393 /* we do not attempt to decrypt jumbograms */
394 if (!EXTRACT_16BITS(&ip6->ip6_plen))
395 goto fail;
396 /* if we can't get nexthdr, we do not need to decrypt it */
397 len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
398 break;
399 #endif /*INET6*/
400 case 4:
401 /* nexthdr & padding are in the last fragment */
402 if (EXTRACT_16BITS(&ip->ip_off) & IP_MF)
403 goto fail;
404 #ifdef INET6
405 ip6 = NULL;
406 #endif
407 len = EXTRACT_16BITS(&ip->ip_len);
408 break;
409 default:
410 goto fail;
411 }
412
413 /* if we can't get nexthdr, we do not need to decrypt it */
414 if (ep - bp2 < len)
415 goto fail;
416 if (ep - bp2 > len) {
417 /* FCS included at end of frame (NetBSD 1.6 or later) */
418 ep = bp2 + len;
419 }
420
421 /* see if we can find the SA, and if so, decode it */
422 sa = sa_list_head;
423 while(sa != NULL &&
424 sa->daddr != ip->ip_dst.s_addr &&
425 sa->spi != ntohl(esp->esp_spi)) {
426 sa = sa->next;
427 }
428
429 /* if we didn't find the specific one, then look for
430 * an unspecified one.
431 */
432 if(sa == NULL) {
433 sa = sa_default;
434 }
435
436 /* if not found fail */
437 if(sa == NULL) goto fail;
438
439 ivoff = (u_char *)(esp + 1) + sa->xform->replaysize;
440 ivlen = sa->xform->ivlen;
441 secret = sa->secret;
442 espsecret_keylen = sa->secretlen;
443
444 switch (sa->xform->algo) {
445 case DESCBC:
446 #ifdef HAVE_LIBCRYPTO
447 {
448 u_char iv[8];
449 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
450 DES_key_schedule schedule;
451 #else
452 des_key_schedule schedule;
453 #endif
454
455 switch (ivlen) {
456 case 4:
457 memcpy(iv, ivoff, 4);
458 memcpy(&iv[4], ivoff, 4);
459 p = &iv[4];
460 *p++ ^= 0xff;
461 *p++ ^= 0xff;
462 *p++ ^= 0xff;
463 *p++ ^= 0xff;
464 break;
465 case 8:
466 memcpy(iv, ivoff, 8);
467 break;
468 default:
469 goto fail;
470 }
471 p = ivoff + ivlen;
472
473 if (espsecret_keylen != 8)
474 goto fail;
475
476 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
477 DES_set_key_unchecked((const_DES_cblock *)secret, &schedule);
478
479 DES_cbc_encrypt((const unsigned char *)p, p,
480 (long)(ep - p), &schedule, (DES_cblock *)iv,
481 DES_DECRYPT);
482
483 #elif OPENSSL_VERSION_NUMBER >= 0x00907000L
484 DES_set_key_unchecked((DES_cblock *)secret, schedule);
485
486 DES_cbc_encrypt((const unsigned char *)p, p,
487 (long)(ep - p), schedule, (DES_cblock *)iv,
488 DES_DECRYPT);
489 #else
490 des_check_key = 0;
491 des_set_key((void *)secret, schedule);
492
493 des_cbc_encrypt((void *)p, (void *)p,
494 (long)(ep - p), schedule, (void *)iv,
495 DES_DECRYPT);
496 #endif
497 advance = ivoff - (u_char *)esp + ivlen;
498 break;
499 }
500 #else
501 goto fail;
502 #endif /*HAVE_LIBCRYPTO*/
503
504 case BLOWFISH:
505 #ifdef HAVE_LIBCRYPTO
506 {
507 BF_KEY schedule;
508
509 if (espsecret_keylen < 5 || espsecret_keylen > 56)
510 goto fail;
511 BF_set_key(&schedule, espsecret_keylen, secret);
512
513 p = ivoff + ivlen;
514 BF_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
515 BF_DECRYPT);
516 advance = ivoff - (u_char *)esp + ivlen;
517 break;
518 }
519 #else
520 goto fail;
521 #endif /*HAVE_LIBCRYPTO*/
522
523 case RC5:
524 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
525 {
526 RC5_32_KEY schedule;
527
528 if (espsecret_keylen < 5 || espsecret_keylen > 255)
529 goto fail;
530 RC5_32_set_key(&schedule, espsecret_keylen, secret,
531 RC5_16_ROUNDS);
532
533 p = ivoff + ivlen;
534 RC5_32_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
535 RC5_DECRYPT);
536 advance = ivoff - (u_char *)esp + ivlen;
537 break;
538 }
539 #else
540 goto fail;
541 #endif /*HAVE_LIBCRYPTO*/
542
543 case CAST128:
544 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
545 {
546 CAST_KEY schedule;
547
548 if (espsecret_keylen < 5 || espsecret_keylen > 16)
549 goto fail;
550 CAST_set_key(&schedule, espsecret_keylen, secret);
551
552 p = ivoff + ivlen;
553 CAST_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
554 CAST_DECRYPT);
555 advance = ivoff - (u_char *)esp + ivlen;
556 break;
557 }
558 #else
559 goto fail;
560 #endif /*HAVE_LIBCRYPTO*/
561
562 case DES3CBC:
563 #if defined(HAVE_LIBCRYPTO)
564 {
565 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
566 DES_key_schedule s1, s2, s3;
567
568 if (espsecret_keylen != 24)
569 goto fail;
570 DES_set_odd_parity((DES_cblock *)secret);
571 DES_set_odd_parity((DES_cblock *)(secret + 8));
572 DES_set_odd_parity((DES_cblock *)(secret + 16));
573 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
574 if(DES_set_key_checked((const_DES_cblock *)secret, &s1) != 0) {
575 printf("failed to schedule key 1\n");
576 }
577 if(DES_set_key_checked((const_DES_cblock *)(secret + 8), &s2)!=0) {
578 printf("failed to schedule key 2\n");
579 }
580 if(DES_set_key_checked((const_DES_cblock *)(secret + 16), &s3)!=0) {
581 printf("failed to schedule key 3\n");
582 }
583 #else
584 if(DES_set_key_checked((DES_cblock *)secret, s1) != 0) {
585 printf("failed to schedule key 1\n");
586 }
587 if(DES_set_key_checked((DES_cblock *)(secret + 8), s2)!=0) {
588 printf("failed to schedule key 2\n");
589 }
590 if(DES_set_key_checked((DES_cblock *)(secret + 16), s3)!=0) {
591 printf("failed to schedule key 3\n");
592 }
593 #endif
594
595 p = ivoff + ivlen;
596 DES_ede3_cbc_encrypt((const unsigned char *)p, p,
597 (long)(ep - p),
598 &s1, &s2, &s3,
599 (DES_cblock *)ivoff, DES_DECRYPT);
600 #else
601 des_key_schedule s1, s2, s3;
602
603 if (espsecret_keylen != 24)
604 goto fail;
605 des_check_key = 1;
606 des_set_odd_parity((void *)secret);
607 des_set_odd_parity((void *)(secret + 8));
608 des_set_odd_parity((void *)(secret + 16));
609 if(des_set_key((void *)secret, s1) != 0) {
610 printf("failed to schedule key 1\n");
611 }
612 if(des_set_key((void *)(secret + 8), s2)!=0) {
613 printf("failed to schedule key 2\n");
614 }
615 if(des_set_key((void *)(secret + 16), s3)!=0) {
616 printf("failed to schedule key 3\n");
617 }
618
619 p = ivoff + ivlen;
620 des_ede3_cbc_encrypt((void *)p, (void *)p,
621 (long)(ep - p),
622 s1, s2, s3,
623 (void *)ivoff, DES_DECRYPT);
624 #endif
625 advance = ivoff - (u_char *)esp + ivlen;
626 break;
627 }
628 #else
629 goto fail;
630 #endif /*HAVE_LIBCRYPTO*/
631
632 case NONE:
633 default:
634 advance = sizeof(struct newesp) + sa->xform->replaysize;
635 break;
636 }
637
638 ep = ep - sa->xform->authlen;
639 /* sanity check for pad length */
640 if (ep - bp < *(ep - 2))
641 goto fail;
642
643 if (padlen)
644 *padlen = *(ep - 2) + 2;
645
646 if (nhdr)
647 *nhdr = *(ep - 1);
648
649 printf(": ");
650 return advance;
651
652 fail:
653 if (nhdr)
654 *nhdr = -1;
655 return 65536;
656 }