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