]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
Put the crypto stuff into a common routine.
[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 /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include "netdissect-stdinc.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
36 * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
37 * is the simplest way of handling the dependency.
38 */
39 #ifdef HAVE_LIBCRYPTO
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
42 #else
43 #undef HAVE_LIBCRYPTO
44 #endif
45 #endif
46
47 #include "netdissect.h"
48 #include "strtoaddr.h"
49 #include "extract.h"
50
51 #include "ascii_strcasecmp.h"
52
53 #include "ip.h"
54 #include "ip6.h"
55
56 /*
57 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
58 * All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 * 3. Neither the name of the project nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
71 *
72 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82 * SUCH DAMAGE.
83 */
84
85 /*
86 * RFC1827/2406 Encapsulated Security Payload.
87 */
88
89 struct newesp {
90 nd_uint32_t esp_spi; /* ESP */
91 nd_uint32_t esp_seq; /* Sequence number */
92 /*variable size*/ /* (IV and) Payload data */
93 /*variable size*/ /* padding */
94 /*8bit*/ /* pad size */
95 /*8bit*/ /* next header */
96 /*8bit*/ /* next header */
97 /*variable size, 32bit bound*/ /* Authentication data */
98 };
99
100 #ifdef HAVE_LIBCRYPTO
101 union inaddr_u {
102 nd_ipv4 in4;
103 nd_ipv6 in6;
104 };
105 struct sa_list {
106 struct sa_list *next;
107 u_int daddr_version;
108 union inaddr_u daddr;
109 uint32_t spi; /* if == 0, then IKEv2 */
110 int initiator;
111 u_char spii[8]; /* for IKEv2 */
112 u_char spir[8];
113 const EVP_CIPHER *evp;
114 u_int ivlen;
115 int authlen;
116 u_char authsecret[256];
117 int authsecret_len;
118 u_char secret[256]; /* is that big enough for all secrets? */
119 int secretlen;
120 };
121
122 #ifndef HAVE_EVP_CIPHER_CTX_NEW
123 /*
124 * Allocate an EVP_CIPHER_CTX.
125 * Used if we have an older version of OpenSSL that doesn't provide
126 * routines to allocate and free them.
127 */
128 static EVP_CIPHER_CTX *
129 EVP_CIPHER_CTX_new(void)
130 {
131 EVP_CIPHER_CTX *ctx;
132
133 ctx = malloc(sizeof(*ctx));
134 if (ctx == NULL)
135 return (NULL);
136 memset(ctx, 0, sizeof(*ctx));
137 return (ctx);
138 }
139
140 static void
141 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
142 {
143 EVP_CIPHER_CTX_cleanup(ctx);
144 free(ctx);
145 }
146 #endif
147
148 #ifdef HAVE_EVP_CIPHERINIT_EX
149 /*
150 * Initialize the cipher by calling EVP_DecryptInit_ex(), because
151 * calling EVP_DecryptInit() will reset the cipher context, clearing
152 * the cipher, so calling it twice, with the second call having a
153 * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(),
154 * however, won't reset the cipher context, so you can use it to specify
155 * the IV in a second call after a first call to EVP_DecryptInit_ex()
156 * to set the cipher and the key.
157 *
158 * XXX - is there some reason why we need to make two calls?
159 */
160 static int
161 set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
162 const unsigned char *key,
163 const unsigned char *iv)
164 {
165 return EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
166 }
167 #else
168 /*
169 * Initialize the cipher by calling EVP_DecryptInit(), because we don't
170 * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
171 */
172 static int
173 set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
174 const unsigned char *key,
175 const unsigned char *iv)
176 {
177 return EVP_DecryptInit(ctx, cipher, key, iv);
178 }
179 #endif
180
181 static u_char *
182 do_decrypt(netdissect_options *ndo, const char *caller, struct sa_list *sa,
183 const u_char *iv, const u_char *ct, unsigned int ctlen)
184 {
185 EVP_CIPHER_CTX *ctx;
186 unsigned int block_size;
187 unsigned int ptlen;
188 u_char *pt;
189 int len;
190
191 ctx = EVP_CIPHER_CTX_new();
192 if (ctx == NULL) {
193 /*
194 * Failed to initialize the cipher context.
195 * From a look at the OpenSSL code, this appears to
196 * mean "couldn't allocate memory for the cipher context";
197 * note that we're not passing any parameters, so there's
198 * not much else it can mean.
199 */
200 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
201 "%s: can't allocate memory for cipher context", caller);
202 return NULL;
203 }
204
205 if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL) < 0) {
206 EVP_CIPHER_CTX_free(ctx);
207 (*ndo->ndo_warning)(ndo, "%s: espkey init failed", caller);
208 return NULL;
209 }
210 if (set_cipher_parameters(ctx, NULL, NULL, iv) < 0) {
211 EVP_CIPHER_CTX_free(ctx);
212 (*ndo->ndo_warning)(ndo, "%s: IV init failed", caller);
213 return NULL;
214 }
215
216 /*
217 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
218 * if the cipher has a block size of which the ciphertext's size must
219 * be a multiple, the payload must be padded to make that happen, so
220 * the ciphertext length must be a multiple of the block size. Fail
221 * if that's not the case.
222 */
223 block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
224 if ((ctlen % block_size) != 0) {
225 EVP_CIPHER_CTX_free(ctx);
226 (*ndo->ndo_warning)(ndo,
227 "%s: ciphertext size %u is not a multiple of the cipher block size %u",
228 caller, ctlen, block_size);
229 return NULL;
230 }
231
232 /*
233 * Attempt to allocate a buffer for the decrypted data, because
234 * we can't decrypt on top of the input buffer.
235 */
236 ptlen = ctlen;
237 pt = (u_char *)malloc(ptlen);
238 if (pt == NULL) {
239 EVP_CIPHER_CTX_free(ctx);
240 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
241 "%s: can't allocate memory for decryption buffer", caller);
242 return NULL;
243 }
244
245 /*
246 * The size of the ciphertext handed to us is a multiple of the
247 * cipher block size, so we don't need to worry about padding.
248 */
249 if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) {
250 free(pt);
251 EVP_CIPHER_CTX_free(ctx);
252 (*ndo->ndo_warning)(ndo,
253 "%s: EVP_CIPHER_CTX_set_padding failed", caller);
254 return NULL;
255 }
256 if (!EVP_DecryptUpdate(ctx, pt, &len, ct, ctlen)) {
257 free(pt);
258 EVP_CIPHER_CTX_free(ctx);
259 (*ndo->ndo_warning)(ndo, "%s: EVP_DecryptUpdate failed",
260 caller);
261 return NULL;
262 }
263 EVP_CIPHER_CTX_free(ctx);
264 return pt;
265 }
266
267 /*
268 * This will allocate a new buffer containing the decrypted data.
269 * It returns 1 on success and 0 on failure.
270 *
271 * It will push the new buffer and the values of ndo->ndo_packetp and
272 * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
273 * and ndo->ndo_snapend to refer to the new buffer.
274 *
275 * Our caller must pop the buffer off the stack when it's finished
276 * dissecting anything in it and before it does any dissection of
277 * anything in the old buffer. That will free the new buffer.
278 */
279 USES_APPLE_DEPRECATED_API
280 int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
281 int initiator,
282 const u_char spii[8],
283 const u_char spir[8],
284 const u_char *buf, const u_char *end)
285 {
286 struct sa_list *sa;
287 const u_char *iv;
288 const u_char *ct;
289 unsigned int ctlen;
290 u_char *pt;
291
292 /* initiator arg is any non-zero value */
293 if(initiator) initiator=1;
294
295 /* see if we can find the SA, and if so, decode it */
296 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
297 if (sa->spi == 0
298 && initiator == sa->initiator
299 && memcmp(spii, sa->spii, 8) == 0
300 && memcmp(spir, sa->spir, 8) == 0)
301 break;
302 }
303
304 if(sa == NULL) return 0;
305 if(sa->evp == NULL) return 0;
306
307 /*
308 * remove authenticator, and see if we still have something to
309 * work with
310 */
311 end = end - sa->authlen;
312 iv = buf;
313 ct = iv + sa->ivlen;
314 ctlen = end-ct;
315
316 if(end <= ct) return 0;
317
318 pt = do_decrypt(ndo, "esp_print_decrypt_buffer_by_ikev2", sa, iv,
319 ct, ctlen);
320 if (pt == NULL)
321 return 0;
322
323 /*
324 * Switch to the output buffer for dissection, and save it
325 * on the buffer stack so it can be freed; our caller must
326 * pop it when done.
327 */
328 if (!nd_push_buffer(ndo, pt, pt, pt + ctlen)) {
329 free(pt);
330 return 0;
331 }
332
333 return 1;
334 }
335 USES_APPLE_RST
336
337 static void esp_print_addsa(netdissect_options *ndo,
338 struct sa_list *sa, int sa_def)
339 {
340 /* copy the "sa" */
341
342 struct sa_list *nsa;
343
344 /* malloc() return used in a 'struct sa_list': do not free() */
345 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
346 if (nsa == NULL)
347 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
348 "esp_print_addsa: malloc");
349
350 *nsa = *sa;
351
352 if (sa_def)
353 ndo->ndo_sa_default = nsa;
354
355 nsa->next = ndo->ndo_sa_list_head;
356 ndo->ndo_sa_list_head = nsa;
357 }
358
359
360 static u_int hexdigit(netdissect_options *ndo, char hex)
361 {
362 if (hex >= '0' && hex <= '9')
363 return (hex - '0');
364 else if (hex >= 'A' && hex <= 'F')
365 return (hex - 'A' + 10);
366 else if (hex >= 'a' && hex <= 'f')
367 return (hex - 'a' + 10);
368 else {
369 (*ndo->ndo_error)(ndo, S_ERR_ND_ESP_SECRET,
370 "invalid hex digit %c in espsecret\n", hex);
371 }
372 }
373
374 static u_int hex2byte(netdissect_options *ndo, char *hexstring)
375 {
376 u_int byte;
377
378 byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
379 return byte;
380 }
381
382 /*
383 * returns size of binary, 0 on failure.
384 */
385 static
386 int espprint_decode_hex(netdissect_options *ndo,
387 u_char *binbuf, unsigned int binbuf_len,
388 char *hex)
389 {
390 unsigned int len;
391 int i;
392
393 len = strlen(hex) / 2;
394
395 if (len > binbuf_len) {
396 (*ndo->ndo_warning)(ndo, "secret is too big: %u\n", len);
397 return 0;
398 }
399
400 i = 0;
401 while (hex[0] != '\0' && hex[1]!='\0') {
402 binbuf[i] = hex2byte(ndo, hex);
403 hex += 2;
404 i++;
405 }
406
407 return i;
408 }
409
410 /*
411 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
412 */
413
414 USES_APPLE_DEPRECATED_API
415 static int
416 espprint_decode_encalgo(netdissect_options *ndo,
417 char *decode, struct sa_list *sa)
418 {
419 size_t i;
420 const EVP_CIPHER *evp;
421 int authlen = 0;
422 char *colon, *p;
423
424 colon = strchr(decode, ':');
425 if (colon == NULL) {
426 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
427 return 0;
428 }
429 *colon = '\0';
430
431 if (strlen(decode) > strlen("-hmac96") &&
432 !strcmp(decode + strlen(decode) - strlen("-hmac96"),
433 "-hmac96")) {
434 p = strstr(decode, "-hmac96");
435 *p = '\0';
436 authlen = 12;
437 }
438 if (strlen(decode) > strlen("-cbc") &&
439 !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
440 p = strstr(decode, "-cbc");
441 *p = '\0';
442 }
443 evp = EVP_get_cipherbyname(decode);
444
445 if (!evp) {
446 (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
447 sa->evp = NULL;
448 sa->authlen = 0;
449 sa->ivlen = 0;
450 return 0;
451 }
452
453 sa->evp = evp;
454 sa->authlen = authlen;
455 /* This returns an int, but it should never be negative */
456 sa->ivlen = EVP_CIPHER_iv_length(evp);
457
458 colon++;
459 if (colon[0] == '0' && colon[1] == 'x') {
460 /* decode some hex! */
461
462 colon += 2;
463 sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
464 if(sa->secretlen == 0) return 0;
465 } else {
466 i = strlen(colon);
467
468 if (i < sizeof(sa->secret)) {
469 memcpy(sa->secret, colon, i);
470 sa->secretlen = i;
471 } else {
472 memcpy(sa->secret, colon, sizeof(sa->secret));
473 sa->secretlen = sizeof(sa->secret);
474 }
475 }
476
477 return 1;
478 }
479 USES_APPLE_RST
480
481 /*
482 * for the moment, ignore the auth algorithm, just hard code the authenticator
483 * length. Need to research how openssl looks up HMAC stuff.
484 */
485 static int
486 espprint_decode_authalgo(netdissect_options *ndo,
487 char *decode, struct sa_list *sa)
488 {
489 char *colon;
490
491 colon = strchr(decode, ':');
492 if (colon == NULL) {
493 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
494 return 0;
495 }
496 *colon = '\0';
497
498 if(ascii_strcasecmp(decode,"sha1") == 0 ||
499 ascii_strcasecmp(decode,"md5") == 0) {
500 sa->authlen = 12;
501 }
502 return 1;
503 }
504
505 static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
506 const char *file, int lineno)
507 {
508 /* it's an IKEv2 secret, store it instead */
509 struct sa_list sa1;
510
511 char *init;
512 char *icookie, *rcookie;
513 int ilen, rlen;
514 char *authkey;
515 char *enckey;
516
517 init = strsep(&line, " \t");
518 icookie = strsep(&line, " \t");
519 rcookie = strsep(&line, " \t");
520 authkey = strsep(&line, " \t");
521 enckey = strsep(&line, " \t");
522
523 /* if any fields are missing */
524 if(!init || !icookie || !rcookie || !authkey || !enckey) {
525 (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
526 file, lineno);
527
528 return;
529 }
530
531 ilen = strlen(icookie);
532 rlen = strlen(rcookie);
533
534 if((init[0]!='I' && init[0]!='R')
535 || icookie[0]!='0' || icookie[1]!='x'
536 || rcookie[0]!='0' || rcookie[1]!='x'
537 || ilen!=18
538 || rlen!=18) {
539 (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
540 file, lineno);
541
542 (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
543 init, icookie, ilen, rcookie, rlen);
544
545 return;
546 }
547
548 sa1.spi = 0;
549 sa1.initiator = (init[0] == 'I');
550 if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
551 return;
552
553 if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
554 return;
555
556 if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
557
558 if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
559
560 esp_print_addsa(ndo, &sa1, FALSE);
561 }
562
563 /*
564 *
565 * special form: file /name
566 * causes us to go read from this file instead.
567 *
568 */
569 static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
570 const char *file, int lineno)
571 {
572 struct sa_list sa1;
573 int sa_def;
574
575 char *spikey;
576 char *decode;
577
578 spikey = strsep(&line, " \t");
579 sa_def = 0;
580 memset(&sa1, 0, sizeof(struct sa_list));
581
582 /* if there is only one token, then it is an algo:key token */
583 if (line == NULL) {
584 decode = spikey;
585 spikey = NULL;
586 /* sa1.daddr.version = 0; */
587 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
588 /* sa1.spi = 0; */
589 sa_def = 1;
590 } else
591 decode = line;
592
593 if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
594 /* open file and read it */
595 FILE *secretfile;
596 char fileline[1024];
597 int subfile_lineno=0;
598 char *nl;
599 char *filename = line;
600
601 secretfile = fopen(filename, FOPEN_READ_TXT);
602 if (secretfile == NULL) {
603 (*ndo->ndo_error)(ndo, S_ERR_ND_OPEN_FILE,
604 "print_esp: can't open %s: %s\n",
605 filename, strerror(errno));
606 }
607
608 while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
609 subfile_lineno++;
610 /* remove newline from the line */
611 nl = strchr(fileline, '\n');
612 if (nl)
613 *nl = '\0';
614 if (fileline[0] == '#') continue;
615 if (fileline[0] == '\0') continue;
616
617 esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
618 }
619 fclose(secretfile);
620
621 return;
622 }
623
624 if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
625 esp_print_decode_ikeline(ndo, line, file, lineno);
626 return;
627 }
628
629 if (spikey) {
630
631 char *spistr, *foo;
632 uint32_t spino;
633
634 spistr = strsep(&spikey, "@");
635 if (spistr == NULL) {
636 (*ndo->ndo_warning)(ndo, "print_esp: failed to find the @ token");
637 return;
638 }
639
640 spino = strtoul(spistr, &foo, 0);
641 if (spistr == foo || !spikey) {
642 (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
643 return;
644 }
645
646 sa1.spi = spino;
647
648 if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
649 sa1.daddr_version = 6;
650 } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
651 sa1.daddr_version = 4;
652 } else {
653 (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
654 return;
655 }
656 }
657
658 if (decode) {
659 /* skip any blank spaces */
660 while (*decode == ' ' || *decode == '\t' || *decode == '\r' || *decode == '\n')
661 decode++;
662
663 if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
664 return;
665 }
666 }
667
668 esp_print_addsa(ndo, &sa1, sa_def);
669 }
670
671 USES_APPLE_DEPRECATED_API
672 static void esp_init(netdissect_options *ndo _U_)
673 {
674 /*
675 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
676 * we check whether it's undefined or it's less than the
677 * value for 1.1.0.
678 */
679 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
680 OpenSSL_add_all_algorithms();
681 #endif
682 EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
683 }
684 USES_APPLE_RST
685
686 void esp_print_decodesecret(netdissect_options *ndo)
687 {
688 char *line;
689 char *p;
690 static int initialized = 0;
691
692 if (!initialized) {
693 esp_init(ndo);
694 initialized = 1;
695 }
696
697 p = ndo->ndo_espsecret;
698
699 while (p && p[0] != '\0') {
700 /* pick out the first line or first thing until a comma */
701 if ((line = strsep(&p, "\n,")) == NULL) {
702 line = p;
703 p = NULL;
704 }
705
706 esp_print_decode_onesecret(ndo, line, "cmdline", 0);
707 }
708
709 ndo->ndo_espsecret = NULL;
710 }
711
712 #endif
713
714 #ifdef HAVE_LIBCRYPTO
715 #define USED_IF_LIBCRYPTO
716 #else
717 #define USED_IF_LIBCRYPTO _U_
718 #endif
719
720 #ifdef HAVE_LIBCRYPTO
721 USES_APPLE_DEPRECATED_API
722 #endif
723 void
724 esp_print(netdissect_options *ndo,
725 const u_char *bp, u_int length,
726 const u_char *bp2 USED_IF_LIBCRYPTO,
727 u_int ver USED_IF_LIBCRYPTO,
728 int fragmented USED_IF_LIBCRYPTO,
729 u_int ttl_hl USED_IF_LIBCRYPTO)
730 {
731 const struct newesp *esp;
732 const u_char *ep;
733 #ifdef HAVE_LIBCRYPTO
734 const struct ip *ip;
735 struct sa_list *sa = NULL;
736 const struct ip6_hdr *ip6 = NULL;
737 const u_char *iv;
738 u_int ivlen;
739 u_int payloadlen;
740 const u_char *ct;
741 u_char *pt;
742 u_int padlen;
743 u_int nh;
744 #endif
745
746 ndo->ndo_protocol = "esp";
747 esp = (const struct newesp *)bp;
748
749 /* 'ep' points to the end of available data. */
750 ep = ndo->ndo_snapend;
751
752 if ((const u_char *)(esp + 1) >= ep) {
753 nd_print_trunc(ndo);
754 return;
755 }
756 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp->esp_spi));
757 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp->esp_seq));
758 ND_PRINT(", length %u", length);
759
760 #ifdef HAVE_LIBCRYPTO
761 /* initiailize SAs */
762 if (ndo->ndo_sa_list_head == NULL) {
763 if (!ndo->ndo_espsecret)
764 return;
765
766 esp_print_decodesecret(ndo);
767 }
768
769 if (ndo->ndo_sa_list_head == NULL)
770 return;
771
772 ip = (const struct ip *)bp2;
773 switch (ver) {
774 case 6:
775 ip6 = (const struct ip6_hdr *)bp2;
776 /* we do not attempt to decrypt jumbograms */
777 if (!GET_BE_U_2(ip6->ip6_plen))
778 return;
779 /* XXX - check whether it's fragmented? */
780 /* if we can't get nexthdr, we do not need to decrypt it */
781
782 /* see if we can find the SA, and if so, decode it */
783 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
784 if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
785 sa->daddr_version == 6 &&
786 UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
787 sizeof(nd_ipv6)) == 0) {
788 break;
789 }
790 }
791 break;
792 case 4:
793 /* nexthdr & padding are in the last fragment */
794 if (fragmented)
795 return;
796
797 /* see if we can find the SA, and if so, decode it */
798 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
799 if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
800 sa->daddr_version == 4 &&
801 UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
802 sizeof(nd_ipv4)) == 0) {
803 break;
804 }
805 }
806 break;
807 default:
808 return;
809 }
810
811 /* if we didn't find the specific one, then look for
812 * an unspecified one.
813 */
814 if (sa == NULL)
815 sa = ndo->ndo_sa_default;
816
817 /* if not found fail */
818 if (sa == NULL)
819 return;
820
821 /* pointer to the IV, if there is one */
822 iv = (const u_char *)(esp + 1) + 0;
823 /* length of the IV, if there is one; 0, if there isn't */
824 ivlen = sa->ivlen;
825
826 /*
827 * Get a pointer to the ciphertext.
828 *
829 * p points to the beginning of the payload, i.e. to the
830 * initialization vector, so if we skip past the initialization
831 * vector, it points to the beginning of the ciphertext.
832 */
833 ct = iv + ivlen;
834
835 /*
836 * Make sure the authentication data/integrity check value length
837 * isn't bigger than the total amount of data available after
838 * the ESP header and initialization vector is removed and,
839 * if not, slice the authentication data/ICV off.
840 */
841 if (ep - ct < sa->authlen) {
842 nd_print_trunc(ndo);
843 return;
844 }
845 ep = ep - sa->authlen;
846
847 /*
848 * Calculate the length of the ciphertext. ep points to
849 * the beginning of the authentication data/integrity check
850 * value, i.e. right past the end of the ciphertext;
851 */
852 payloadlen = ep - ct;
853
854 if (sa->evp == NULL)
855 return;
856
857 /*
858 * If the next header value is past the end of the available
859 * data, we won't be able to fetch it once we've decrypted
860 * the ciphertext, so there's no point in decrypting the data.
861 *
862 * Report it as truncation.
863 */
864 if (!ND_TTEST_1(ep - 1)) {
865 nd_print_trunc(ndo);
866 return;
867 }
868
869 pt = do_decrypt(ndo, "esp_print", sa, iv, ct, payloadlen);
870 if (pt == NULL)
871 return;
872
873 /*
874 * Switch to the output buffer for dissection, and
875 * save it on the buffer stack so it can be freed.
876 */
877 ep = pt + payloadlen;
878 if (!nd_push_buffer(ndo, pt, pt, ep)) {
879 free(pt);
880 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
881 "esp_print: can't push buffer on buffer stack");
882 }
883
884 /*
885 * Sanity check for pad length; if it, plus 2 for the pad
886 * length and next header fields, is bigger than the ciphertext
887 * length (which is also the plaintext length), it's too big.
888 *
889 * XXX - the check can fail if the packet is corrupt *or* if
890 * it was not decrypted with the correct key, so that the
891 * "plaintext" is not what was being sent.
892 */
893 padlen = GET_U_1(ep - 2);
894 if (padlen + 2 > payloadlen) {
895 nd_print_trunc(ndo);
896 return;
897 }
898
899 /* Get the next header */
900 nh = GET_U_1(ep - 1);
901
902 ND_PRINT(": ");
903
904 /* Now dissect the plaintext. */
905 ip_print_demux(ndo, pt, payloadlen - (padlen + 2), ver, fragmented,
906 ttl_hl, nh, bp2);
907
908 /* Pop the buffer, freeing it. */
909 nd_pop_packet_info(ndo);
910 #endif
911 }
912 #ifdef HAVE_LIBCRYPTO
913 USES_APPLE_RST
914 #endif