]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
6640f374c98965a7b646b4c67e54d8f480844fa4
[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[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.57 2007-11-24 18:13:33 mcr 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 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
42 #endif
43 #endif
44
45 #include <stdio.h>
46
47 #include "ip.h"
48 #include "esp.h"
49 #ifdef INET6
50 #include "ip6.h"
51 #endif
52
53 #include "netdissect.h"
54 #include "addrtoname.h"
55 #include "extract.h"
56
57 #ifndef HAVE_SOCKADDR_STORAGE
58 #ifdef INET6
59 struct sockaddr_storage {
60 union {
61 struct sockaddr_in sin;
62 struct sockaddr_in6 sin6;
63 } un;
64 };
65 #else
66 #define sockaddr_storage sockaddr
67 #endif
68 #endif /* HAVE_SOCKADDR_STORAGE */
69
70 #ifdef HAVE_LIBCRYPTO
71 struct sa_list {
72 struct sa_list *next;
73 struct sockaddr_storage daddr;
74 u_int32_t spi; /* if == 0, then IKEv2 */
75 int initiator;
76 u_char spii[8]; /* for IKEv2 */
77 u_char spir[8];
78 const EVP_CIPHER *evp;
79 int ivlen;
80 int authlen;
81 u_char authsecret[256];
82 int authsecret_len;
83 u_char secret[256]; /* is that big enough for all secrets? */
84 int secretlen;
85 };
86
87 /*
88 * this will adjust ndo_packetp and ndo_snapend to new buffer!
89 */
90 int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
91 int initiator,
92 u_char spii[8], u_char spir[8],
93 u_char *buf, u_char *end)
94 {
95 struct sa_list *sa;
96 u_char *iv;
97 int len;
98 EVP_CIPHER_CTX ctx;
99
100 /* initiator arg is any non-zero value */
101 if(initiator) initiator=1;
102
103 /* see if we can find the SA, and if so, decode it */
104 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
105 if (sa->spi == 0
106 && initiator == sa->initiator
107 && memcmp(spii, sa->spii, 8) == 0
108 && memcmp(spir, sa->spir, 8) == 0)
109 break;
110 }
111
112 if(sa == NULL) return 0;
113 if(sa->evp == NULL) return 0;
114
115 /*
116 * remove authenticator, and see if we still have something to
117 * work with
118 */
119 end = end - sa->authlen;
120 iv = buf;
121 buf = buf + sa->ivlen;
122 len = end-buf;
123
124 if(end <= buf) return 0;
125
126 memset(&ctx, 0, sizeof(ctx));
127 if (EVP_CipherInit(&ctx, sa->evp, sa->secret, NULL, 0) < 0)
128 (*ndo->ndo_warning)(ndo, "espkey init failed");
129 EVP_CipherInit(&ctx, NULL, NULL, iv, 0);
130 EVP_Cipher(&ctx, buf, buf, len);
131
132 ndo->ndo_packetp = buf;
133 ndo->ndo_snapend = end;
134
135 return 1;
136
137 }
138
139 static void esp_print_addsa(netdissect_options *ndo,
140 struct sa_list *sa, int sa_def)
141 {
142 /* copy the "sa" */
143
144 struct sa_list *nsa;
145
146 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
147 if (nsa == NULL)
148 (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
149
150 *nsa = *sa;
151
152 if (sa_def)
153 ndo->ndo_sa_default = nsa;
154
155 nsa->next = ndo->ndo_sa_list_head;
156 ndo->ndo_sa_list_head = nsa;
157 }
158
159
160 static u_int hexdigit(netdissect_options *ndo, char hex)
161 {
162 if (hex >= '0' && hex <= '9')
163 return (hex - '0');
164 else if (hex >= 'A' && hex <= 'F')
165 return (hex - 'A' + 10);
166 else if (hex >= 'a' && hex <= 'f')
167 return (hex - 'a' + 10);
168 else {
169 (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
170 return 0;
171 }
172 }
173
174 static u_int hex2byte(netdissect_options *ndo, char *hexstring)
175 {
176 u_int byte;
177
178 byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
179 return byte;
180 }
181
182 /*
183 * returns size of binary, 0 on failure.
184 */
185 static
186 int espprint_decode_hex(netdissect_options *ndo,
187 u_char *binbuf, unsigned int binbuf_len,
188 char *hex)
189 {
190 unsigned int len;
191 int i;
192
193 len = strlen(hex) / 2;
194
195 if (len > binbuf_len) {
196 (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
197 return 0;
198 }
199
200 i = 0;
201 while (hex[0] != '\0' && hex[1]!='\0') {
202 binbuf[i] = hex2byte(ndo, hex);
203 hex += 2;
204 i++;
205 }
206
207 return i;
208 }
209
210 /*
211 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
212 */
213
214 static int
215 espprint_decode_encalgo(netdissect_options *ndo,
216 char *decode, struct sa_list *sa)
217 {
218 int len;
219 size_t i;
220 const EVP_CIPHER *evp;
221 int authlen = 0;
222 char *colon, *p;
223
224 colon = strchr(decode, ':');
225 if (colon == NULL) {
226 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
227 return 0;
228 }
229 *colon = '\0';
230
231 len = colon - decode;
232 if (strlen(decode) > strlen("-hmac96") &&
233 !strcmp(decode + strlen(decode) - strlen("-hmac96"),
234 "-hmac96")) {
235 p = strstr(decode, "-hmac96");
236 *p = '\0';
237 authlen = 12;
238 }
239 if (strlen(decode) > strlen("-cbc") &&
240 !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
241 p = strstr(decode, "-cbc");
242 *p = '\0';
243 }
244 evp = EVP_get_cipherbyname(decode);
245
246 if (!evp) {
247 (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
248 sa->evp = NULL;
249 sa->authlen = 0;
250 sa->ivlen = 0;
251 return 0;
252 }
253
254 sa->evp = evp;
255 sa->authlen = authlen;
256 sa->ivlen = EVP_CIPHER_iv_length(evp);
257
258 colon++;
259 if (colon[0] == '0' && colon[1] == 'x') {
260 /* decode some hex! */
261
262 colon += 2;
263 sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
264 if(sa->secretlen == 0) return 0;
265 } else {
266 i = strlen(colon);
267
268 if (i < sizeof(sa->secret)) {
269 memcpy(sa->secret, colon, i);
270 sa->secretlen = i;
271 } else {
272 memcpy(sa->secret, colon, sizeof(sa->secret));
273 sa->secretlen = sizeof(sa->secret);
274 }
275 }
276
277 return 1;
278 }
279
280 /*
281 * for the moment, ignore the auth algorith, just hard code the authenticator
282 * length. Need to research how openssl looks up HMAC stuff.
283 */
284 static int
285 espprint_decode_authalgo(netdissect_options *ndo,
286 char *decode, struct sa_list *sa)
287 {
288 char *colon;
289
290 colon = strchr(decode, ':');
291 if (colon == NULL) {
292 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
293 return 0;
294 }
295 *colon = '\0';
296
297 if(strcasecmp(colon,"sha1") == 0 ||
298 strcasecmp(colon,"md5") == 0) {
299 sa->authlen = 12;
300 }
301 return 1;
302 }
303
304 static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
305 const char *file, int lineno)
306 {
307 /* it's an IKEv2 secret, store it instead */
308 struct sa_list sa1;
309
310 char *init;
311 char *icookie, *rcookie;
312 int ilen, rlen;
313 char *authkey;
314 char *enckey;
315
316 init = strsep(&line, " \t");
317 icookie = strsep(&line, " \t");
318 rcookie = strsep(&line, " \t");
319 authkey = strsep(&line, " \t");
320 enckey = strsep(&line, " \t");
321
322 /* if any fields are missing */
323 if(!init || !icookie || !rcookie || !authkey || !enckey) {
324 (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
325 file, lineno);
326
327 return;
328 }
329
330 ilen = strlen(icookie);
331 rlen = strlen(rcookie);
332
333 if((init[0]!='I' && init[0]!='R')
334 || icookie[0]!='0' || icookie[1]!='x'
335 || rcookie[0]!='0' || rcookie[1]!='x'
336 || ilen!=18
337 || rlen!=18) {
338 (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
339 file, lineno);
340
341 (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
342 init, icookie, ilen, rcookie, rlen);
343
344 return;
345 }
346
347 sa1.spi = 0;
348 sa1.initiator = (init[0] == 'I');
349 if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
350 return;
351
352 if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
353 return;
354
355 if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
356
357 if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
358
359 esp_print_addsa(ndo, &sa1, FALSE);
360 }
361
362 /*
363 *
364 * special form: file /name
365 * causes us to go read from this file instead.
366 *
367 */
368 static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
369 const char *file, int lineno)
370 {
371 struct sa_list sa1;
372 int sa_def;
373
374 char *spikey;
375 char *decode;
376
377 spikey = strsep(&line, " \t");
378 sa_def = 0;
379 memset(&sa1, 0, sizeof(struct sa_list));
380
381 /* if there is only one token, then it is an algo:key token */
382 if (line == NULL) {
383 decode = spikey;
384 spikey = NULL;
385 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
386 /* sa1.spi = 0; */
387 sa_def = 1;
388 } else
389 decode = line;
390
391 if (spikey && strcasecmp(spikey, "file") == 0) {
392 /* open file and read it */
393 FILE *secretfile;
394 char fileline[1024];
395 int lineno=0;
396 char *nl;
397 char *filename = line;
398
399 secretfile = fopen(filename, FOPEN_READ_TXT);
400 if (secretfile == NULL) {
401 perror(filename);
402 exit(3);
403 }
404
405 while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
406 lineno++;
407 /* remove newline from the line */
408 nl = strchr(fileline, '\n');
409 if (nl)
410 *nl = '\0';
411 if (fileline[0] == '#') continue;
412 if (fileline[0] == '\0') continue;
413
414 esp_print_decode_onesecret(ndo, fileline, filename, lineno);
415 }
416 fclose(secretfile);
417
418 return;
419 }
420
421 if (spikey && strcasecmp(spikey, "ikev2") == 0) {
422 esp_print_decode_ikeline(ndo, line, file, lineno);
423 return;
424 }
425
426 if (spikey) {
427
428 char *spistr, *foo;
429 u_int32_t spino;
430 struct sockaddr_in *sin;
431 #ifdef INET6
432 struct sockaddr_in6 *sin6;
433 #endif
434
435 spistr = strsep(&spikey, "@");
436
437 spino = strtoul(spistr, &foo, 0);
438 if (spistr == foo || !spikey) {
439 (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
440 return;
441 }
442
443 sa1.spi = spino;
444
445 sin = (struct sockaddr_in *)&sa1.daddr;
446 #ifdef INET6
447 sin6 = (struct sockaddr_in6 *)&sa1.daddr;
448 if (inet_pton(AF_INET6, spikey, &sin6->sin6_addr) == 1) {
449 #ifdef HAVE_SOCKADDR_SA_LEN
450 sin6->sin6_len = sizeof(struct sockaddr_in6);
451 #endif
452 sin6->sin6_family = AF_INET6;
453 } else
454 #endif
455 if (inet_pton(AF_INET, spikey, &sin->sin_addr) == 1) {
456 #ifdef HAVE_SOCKADDR_SA_LEN
457 sin->sin_len = sizeof(struct sockaddr_in);
458 #endif
459 sin->sin_family = AF_INET;
460 } else {
461 (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
462 return;
463 }
464 }
465
466 if (decode) {
467 /* skip any blank spaces */
468 while (isspace((unsigned char)*decode))
469 decode++;
470
471 if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
472 return;
473 }
474 }
475
476 esp_print_addsa(ndo, &sa1, sa_def);
477 }
478
479 static void esp_init(netdissect_options *ndo _U_)
480 {
481
482 OpenSSL_add_all_algorithms();
483 EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
484 }
485
486 void esp_print_decodesecret(netdissect_options *ndo)
487 {
488 char *line;
489 char *p;
490 static int initialized = 0;
491
492 if (!initialized) {
493 esp_init(ndo);
494 initialized = 1;
495 }
496
497 p = ndo->ndo_espsecret;
498
499 while (p && p[0] != '\0') {
500 /* pick out the first line or first thing until a comma */
501 if ((line = strsep(&p, "\n,")) == NULL) {
502 line = p;
503 p = NULL;
504 }
505
506 esp_print_decode_onesecret(ndo, line, "cmdline", 0);
507 }
508
509 ndo->ndo_espsecret = NULL;
510 }
511
512 #endif
513
514 int
515 esp_print(netdissect_options *ndo,
516 const u_char *bp, const int length, const u_char *bp2
517 #ifndef HAVE_LIBCRYPTO
518 _U_
519 #endif
520 ,
521 int *nhdr
522 #ifndef HAVE_LIBCRYPTO
523 _U_
524 #endif
525 ,
526 int *padlen
527 #ifndef HAVE_LIBCRYPTO
528 _U_
529 #endif
530 )
531 {
532 register const struct newesp *esp;
533 register const u_char *ep;
534 #ifdef HAVE_LIBCRYPTO
535 struct ip *ip;
536 struct sa_list *sa = NULL;
537 int espsecret_keylen;
538 #ifdef INET6
539 struct ip6_hdr *ip6 = NULL;
540 #endif
541 int advance;
542 int len;
543 u_char *secret;
544 int ivlen = 0;
545 u_char *ivoff;
546 u_char *p;
547 EVP_CIPHER_CTX ctx;
548 int blocksz;
549 #endif
550
551 esp = (struct newesp *)bp;
552
553 #ifdef HAVE_LIBCRYPTO
554 secret = NULL;
555 advance = 0;
556 #endif
557
558 #if 0
559 /* keep secret out of a register */
560 p = (u_char *)&secret;
561 #endif
562
563 /* 'ep' points to the end of available data. */
564 ep = ndo->ndo_snapend;
565
566 if ((u_char *)(esp + 1) >= ep) {
567 fputs("[|ESP]", stdout);
568 goto fail;
569 }
570 (*ndo->ndo_printf)(ndo, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi));
571 (*ndo->ndo_printf)(ndo, ",seq=0x%x)", EXTRACT_32BITS(&esp->esp_seq));
572 (*ndo->ndo_printf)(ndo, ", length %u", length);
573
574 #ifndef HAVE_LIBCRYPTO
575 goto fail;
576 #else
577 /* initiailize SAs */
578 if (ndo->ndo_sa_list_head == NULL) {
579 if (!ndo->ndo_espsecret)
580 goto fail;
581
582 esp_print_decodesecret(ndo);
583 }
584
585 if (ndo->ndo_sa_list_head == NULL)
586 goto fail;
587
588 ip = (struct ip *)bp2;
589 switch (IP_V(ip)) {
590 #ifdef INET6
591 case 6:
592 ip6 = (struct ip6_hdr *)bp2;
593 /* we do not attempt to decrypt jumbograms */
594 if (!EXTRACT_16BITS(&ip6->ip6_plen))
595 goto fail;
596 /* if we can't get nexthdr, we do not need to decrypt it */
597 len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
598
599 /* see if we can find the SA, and if so, decode it */
600 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
601 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa->daddr;
602 if (sa->spi == ntohl(esp->esp_spi) &&
603 sin6->sin6_family == AF_INET6 &&
604 memcmp(&sin6->sin6_addr, &ip6->ip6_dst,
605 sizeof(struct in6_addr)) == 0) {
606 break;
607 }
608 }
609 break;
610 #endif /*INET6*/
611 case 4:
612 /* nexthdr & padding are in the last fragment */
613 if (EXTRACT_16BITS(&ip->ip_off) & IP_MF)
614 goto fail;
615 len = EXTRACT_16BITS(&ip->ip_len);
616
617 /* see if we can find the SA, and if so, decode it */
618 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
619 struct sockaddr_in *sin = (struct sockaddr_in *)&sa->daddr;
620 if (sa->spi == ntohl(esp->esp_spi) &&
621 sin->sin_family == AF_INET &&
622 sin->sin_addr.s_addr == ip->ip_dst.s_addr) {
623 break;
624 }
625 }
626 break;
627 default:
628 goto fail;
629 }
630
631 /* if we didn't find the specific one, then look for
632 * an unspecified one.
633 */
634 if (sa == NULL)
635 sa = ndo->ndo_sa_default;
636
637 /* if not found fail */
638 if (sa == NULL)
639 goto fail;
640
641 /* if we can't get nexthdr, we do not need to decrypt it */
642 if (ep - bp2 < len)
643 goto fail;
644 if (ep - bp2 > len) {
645 /* FCS included at end of frame (NetBSD 1.6 or later) */
646 ep = bp2 + len;
647 }
648
649 ivoff = (u_char *)(esp + 1) + 0;
650 ivlen = sa->ivlen;
651 secret = sa->secret;
652 espsecret_keylen = sa->secretlen;
653 ep = ep - sa->authlen;
654
655 if (sa->evp) {
656 memset(&ctx, 0, sizeof(ctx));
657 if (EVP_CipherInit(&ctx, sa->evp, secret, NULL, 0) < 0)
658 (*ndo->ndo_warning)(ndo, "espkey init failed");
659
660 blocksz = EVP_CIPHER_CTX_block_size(&ctx);
661
662 p = ivoff;
663 EVP_CipherInit(&ctx, NULL, NULL, p, 0);
664 EVP_Cipher(&ctx, p + ivlen, p + ivlen, ep - (p + ivlen));
665 advance = ivoff - (u_char *)esp + ivlen;
666 } else
667 advance = sizeof(struct newesp);
668
669 /* sanity check for pad length */
670 if (ep - bp < *(ep - 2))
671 goto fail;
672
673 if (padlen)
674 *padlen = *(ep - 2) + 2;
675
676 if (nhdr)
677 *nhdr = *(ep - 1);
678
679 (ndo->ndo_printf)(ndo, ": ");
680 return advance;
681 #endif
682
683 fail:
684 return -1;
685 }
686
687 /*
688 * Local Variables:
689 * c-style: whitesmith
690 * c-basic-offset: 8
691 * End:
692 */