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