]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
7013a0c5e55e44f4dfca116375280d58503a960c
[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 struct in_addr in4;
103 struct in6_addr 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 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_CipherInit_ex(), because
151 * calling EVP_CipherInit() 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_CipherInit_ex(),
154 * however, won't reset the cipher context, so you can use it to specify
155 * the IV oin a second call after a first call to EVP_CipherInit_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, int enc)
164 {
165 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
166 }
167 #else
168 /*
169 * Initialize the cipher by calling EVP_CipherInit(), because we don't
170 * have EVP_CipherInit_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, int enc)
176 {
177 return EVP_CipherInit(ctx, cipher, key, iv, enc);
178 }
179 #endif
180
181 /*
182 * this will adjust ndo_packetp and ndo_snapend to new buffer!
183 */
184 USES_APPLE_DEPRECATED_API
185 int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
186 int initiator,
187 const u_char spii[8],
188 const u_char spir[8],
189 const u_char *buf, const u_char *end)
190 {
191 struct sa_list *sa;
192 const u_char *iv;
193 unsigned int len;
194 EVP_CIPHER_CTX *ctx;
195 unsigned int block_size, output_buffer_size;
196 u_char *output_buffer;
197
198 /* initiator arg is any non-zero value */
199 if(initiator) initiator=1;
200
201 /* see if we can find the SA, and if so, decode it */
202 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
203 if (sa->spi == 0
204 && initiator == sa->initiator
205 && memcmp(spii, sa->spii, 8) == 0
206 && memcmp(spir, sa->spir, 8) == 0)
207 break;
208 }
209
210 if(sa == NULL) return 0;
211 if(sa->evp == NULL) return 0;
212
213 /*
214 * remove authenticator, and see if we still have something to
215 * work with
216 */
217 end = end - sa->authlen;
218 iv = buf;
219 buf = buf + sa->ivlen;
220 len = end-buf;
221
222 if(end <= buf) return 0;
223
224 ctx = EVP_CIPHER_CTX_new();
225 if (ctx == NULL)
226 return 0;
227 if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL, 0) < 0)
228 (*ndo->ndo_warning)(ndo, "espkey init failed");
229 set_cipher_parameters(ctx, NULL, NULL, iv, 0);
230 /*
231 * Allocate a buffer for the decrypted data.
232 * The output buffer must be separate from the input buffer, and
233 * its size must be a multiple of the cipher block size.
234 */
235 block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
236 output_buffer_size = len + (block_size - len % block_size);
237 output_buffer = (u_char *)malloc(output_buffer_size);
238 if (output_buffer == NULL) {
239 (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
240 EVP_CIPHER_CTX_free(ctx);
241 return 0;
242 }
243 EVP_Cipher(ctx, output_buffer, buf, len);
244 EVP_CIPHER_CTX_free(ctx);
245
246 /*
247 * XXX - of course this is wrong, because buf is a const buffer,
248 * but changing this would require a more complicated fix.
249 */
250 memcpy(buf, output_buffer, len);
251 free(output_buffer);
252
253 ndo->ndo_packetp = buf;
254 ndo->ndo_snapend = end;
255
256 return 1;
257 }
258 USES_APPLE_RST
259
260 static void esp_print_addsa(netdissect_options *ndo,
261 struct sa_list *sa, int sa_def)
262 {
263 /* copy the "sa" */
264
265 struct sa_list *nsa;
266
267 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
268 if (nsa == NULL)
269 (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
270
271 *nsa = *sa;
272
273 if (sa_def)
274 ndo->ndo_sa_default = nsa;
275
276 nsa->next = ndo->ndo_sa_list_head;
277 ndo->ndo_sa_list_head = nsa;
278 }
279
280
281 static u_int hexdigit(netdissect_options *ndo, char hex)
282 {
283 if (hex >= '0' && hex <= '9')
284 return (hex - '0');
285 else if (hex >= 'A' && hex <= 'F')
286 return (hex - 'A' + 10);
287 else if (hex >= 'a' && hex <= 'f')
288 return (hex - 'a' + 10);
289 else {
290 (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
291 return 0;
292 }
293 }
294
295 static u_int hex2byte(netdissect_options *ndo, char *hexstring)
296 {
297 u_int byte;
298
299 byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
300 return byte;
301 }
302
303 /*
304 * returns size of binary, 0 on failure.
305 */
306 static
307 int espprint_decode_hex(netdissect_options *ndo,
308 u_char *binbuf, unsigned int binbuf_len,
309 char *hex)
310 {
311 unsigned int len;
312 int i;
313
314 len = strlen(hex) / 2;
315
316 if (len > binbuf_len) {
317 (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
318 return 0;
319 }
320
321 i = 0;
322 while (hex[0] != '\0' && hex[1]!='\0') {
323 binbuf[i] = hex2byte(ndo, hex);
324 hex += 2;
325 i++;
326 }
327
328 return i;
329 }
330
331 /*
332 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
333 */
334
335 USES_APPLE_DEPRECATED_API
336 static int
337 espprint_decode_encalgo(netdissect_options *ndo,
338 char *decode, struct sa_list *sa)
339 {
340 size_t i;
341 const EVP_CIPHER *evp;
342 int authlen = 0;
343 char *colon, *p;
344
345 colon = strchr(decode, ':');
346 if (colon == NULL) {
347 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
348 return 0;
349 }
350 *colon = '\0';
351
352 if (strlen(decode) > strlen("-hmac96") &&
353 !strcmp(decode + strlen(decode) - strlen("-hmac96"),
354 "-hmac96")) {
355 p = strstr(decode, "-hmac96");
356 *p = '\0';
357 authlen = 12;
358 }
359 if (strlen(decode) > strlen("-cbc") &&
360 !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
361 p = strstr(decode, "-cbc");
362 *p = '\0';
363 }
364 evp = EVP_get_cipherbyname(decode);
365
366 if (!evp) {
367 (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
368 sa->evp = NULL;
369 sa->authlen = 0;
370 sa->ivlen = 0;
371 return 0;
372 }
373
374 sa->evp = evp;
375 sa->authlen = authlen;
376 sa->ivlen = EVP_CIPHER_iv_length(evp);
377
378 colon++;
379 if (colon[0] == '0' && colon[1] == 'x') {
380 /* decode some hex! */
381
382 colon += 2;
383 sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
384 if(sa->secretlen == 0) return 0;
385 } else {
386 i = strlen(colon);
387
388 if (i < sizeof(sa->secret)) {
389 memcpy(sa->secret, colon, i);
390 sa->secretlen = i;
391 } else {
392 memcpy(sa->secret, colon, sizeof(sa->secret));
393 sa->secretlen = sizeof(sa->secret);
394 }
395 }
396
397 return 1;
398 }
399 USES_APPLE_RST
400
401 /*
402 * for the moment, ignore the auth algorith, just hard code the authenticator
403 * length. Need to research how openssl looks up HMAC stuff.
404 */
405 static int
406 espprint_decode_authalgo(netdissect_options *ndo,
407 char *decode, struct sa_list *sa)
408 {
409 char *colon;
410
411 colon = strchr(decode, ':');
412 if (colon == NULL) {
413 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
414 return 0;
415 }
416 *colon = '\0';
417
418 if(ascii_strcasecmp(colon,"sha1") == 0 ||
419 ascii_strcasecmp(colon,"md5") == 0) {
420 sa->authlen = 12;
421 }
422 return 1;
423 }
424
425 static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
426 const char *file, int lineno)
427 {
428 /* it's an IKEv2 secret, store it instead */
429 struct sa_list sa1;
430
431 char *init;
432 char *icookie, *rcookie;
433 int ilen, rlen;
434 char *authkey;
435 char *enckey;
436
437 init = strsep(&line, " \t");
438 icookie = strsep(&line, " \t");
439 rcookie = strsep(&line, " \t");
440 authkey = strsep(&line, " \t");
441 enckey = strsep(&line, " \t");
442
443 /* if any fields are missing */
444 if(!init || !icookie || !rcookie || !authkey || !enckey) {
445 (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
446 file, lineno);
447
448 return;
449 }
450
451 ilen = strlen(icookie);
452 rlen = strlen(rcookie);
453
454 if((init[0]!='I' && init[0]!='R')
455 || icookie[0]!='0' || icookie[1]!='x'
456 || rcookie[0]!='0' || rcookie[1]!='x'
457 || ilen!=18
458 || rlen!=18) {
459 (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
460 file, lineno);
461
462 (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
463 init, icookie, ilen, rcookie, rlen);
464
465 return;
466 }
467
468 sa1.spi = 0;
469 sa1.initiator = (init[0] == 'I');
470 if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
471 return;
472
473 if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
474 return;
475
476 if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
477
478 if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
479
480 esp_print_addsa(ndo, &sa1, FALSE);
481 }
482
483 /*
484 *
485 * special form: file /name
486 * causes us to go read from this file instead.
487 *
488 */
489 static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
490 const char *file, int lineno)
491 {
492 struct sa_list sa1;
493 int sa_def;
494
495 char *spikey;
496 char *decode;
497
498 spikey = strsep(&line, " \t");
499 sa_def = 0;
500 memset(&sa1, 0, sizeof(struct sa_list));
501
502 /* if there is only one token, then it is an algo:key token */
503 if (line == NULL) {
504 decode = spikey;
505 spikey = NULL;
506 /* sa1.daddr.version = 0; */
507 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
508 /* sa1.spi = 0; */
509 sa_def = 1;
510 } else
511 decode = line;
512
513 if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
514 /* open file and read it */
515 FILE *secretfile;
516 char fileline[1024];
517 int subfile_lineno=0;
518 char *nl;
519 char *filename = line;
520
521 secretfile = fopen(filename, FOPEN_READ_TXT);
522 if (secretfile == NULL) {
523 (*ndo->ndo_error)(ndo, "print_esp: can't open %s: %s\n",
524 filename, strerror(errno));
525 return;
526 }
527
528 while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
529 subfile_lineno++;
530 /* remove newline from the line */
531 nl = strchr(fileline, '\n');
532 if (nl)
533 *nl = '\0';
534 if (fileline[0] == '#') continue;
535 if (fileline[0] == '\0') continue;
536
537 esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
538 }
539 fclose(secretfile);
540
541 return;
542 }
543
544 if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
545 esp_print_decode_ikeline(ndo, line, file, lineno);
546 return;
547 }
548
549 if (spikey) {
550
551 char *spistr, *foo;
552 uint32_t spino;
553
554 spistr = strsep(&spikey, "@");
555
556 spino = strtoul(spistr, &foo, 0);
557 if (spistr == foo || !spikey) {
558 (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
559 return;
560 }
561
562 sa1.spi = spino;
563
564 if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
565 sa1.daddr_version = 6;
566 } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
567 sa1.daddr_version = 4;
568 } else {
569 (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
570 return;
571 }
572 }
573
574 if (decode) {
575 /* skip any blank spaces */
576 while (isspace((unsigned char)*decode))
577 decode++;
578
579 if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
580 return;
581 }
582 }
583
584 esp_print_addsa(ndo, &sa1, sa_def);
585 }
586
587 USES_APPLE_DEPRECATED_API
588 static void esp_init(netdissect_options *ndo _U_)
589 {
590 /*
591 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
592 * we check whether it's undefined or it's less than the
593 * value for 1.1.0.
594 */
595 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
596 OpenSSL_add_all_algorithms();
597 #endif
598 EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
599 }
600 USES_APPLE_RST
601
602 void esp_print_decodesecret(netdissect_options *ndo)
603 {
604 char *line;
605 char *p;
606 static int initialized = 0;
607
608 if (!initialized) {
609 esp_init(ndo);
610 initialized = 1;
611 }
612
613 p = ndo->ndo_espsecret;
614
615 while (p && p[0] != '\0') {
616 /* pick out the first line or first thing until a comma */
617 if ((line = strsep(&p, "\n,")) == NULL) {
618 line = p;
619 p = NULL;
620 }
621
622 esp_print_decode_onesecret(ndo, line, "cmdline", 0);
623 }
624
625 ndo->ndo_espsecret = NULL;
626 }
627
628 #endif
629
630 #ifdef HAVE_LIBCRYPTO
631 USES_APPLE_DEPRECATED_API
632 #endif
633 int
634 esp_print(netdissect_options *ndo,
635 const u_char *bp, const int length, const u_char *bp2
636 #ifndef HAVE_LIBCRYPTO
637 _U_
638 #endif
639 ,
640 u_int *nhdr
641 #ifndef HAVE_LIBCRYPTO
642 _U_
643 #endif
644 ,
645 u_int *padlen
646 #ifndef HAVE_LIBCRYPTO
647 _U_
648 #endif
649 )
650 {
651 const struct newesp *esp;
652 const u_char *ep;
653 #ifdef HAVE_LIBCRYPTO
654 const struct ip *ip;
655 struct sa_list *sa = NULL;
656 const struct ip6_hdr *ip6 = NULL;
657 int advance;
658 int len;
659 u_char *secret;
660 int ivlen = 0;
661 const u_char *ivoff;
662 const u_char *p;
663 EVP_CIPHER_CTX *ctx;
664 unsigned int block_size, output_buffer_size;
665 u_char *output_buffer;
666 #endif
667
668 esp = (const struct newesp *)bp;
669
670 #ifdef HAVE_LIBCRYPTO
671 secret = NULL;
672 advance = 0;
673 #endif
674
675 #if 0
676 /* keep secret out of a register */
677 p = (u_char *)&secret;
678 #endif
679
680 /* 'ep' points to the end of available data. */
681 ep = ndo->ndo_snapend;
682
683 if ((const u_char *)(esp + 1) >= ep) {
684 ND_PRINT("[|ESP]");
685 goto fail;
686 }
687 ND_PRINT("ESP(spi=0x%08x", EXTRACT_BE_U_4(esp->esp_spi));
688 ND_PRINT(",seq=0x%x)", EXTRACT_BE_U_4(esp->esp_seq));
689 ND_PRINT(", length %u", length);
690
691 #ifndef HAVE_LIBCRYPTO
692 goto fail;
693 #else
694 /* initiailize SAs */
695 if (ndo->ndo_sa_list_head == NULL) {
696 if (!ndo->ndo_espsecret)
697 goto fail;
698
699 esp_print_decodesecret(ndo);
700 }
701
702 if (ndo->ndo_sa_list_head == NULL)
703 goto fail;
704
705 ip = (const struct ip *)bp2;
706 switch (IP_V(ip)) {
707 case 6:
708 ip6 = (const struct ip6_hdr *)bp2;
709 /* we do not attempt to decrypt jumbograms */
710 if (!EXTRACT_BE_U_2(ip6->ip6_plen))
711 goto fail;
712 /* if we can't get nexthdr, we do not need to decrypt it */
713 len = sizeof(struct ip6_hdr) + EXTRACT_BE_U_2(ip6->ip6_plen);
714
715 /* see if we can find the SA, and if so, decode it */
716 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
717 if (sa->spi == EXTRACT_BE_U_4(esp->esp_spi) &&
718 sa->daddr_version == 6 &&
719 UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
720 sizeof(struct in6_addr)) == 0) {
721 break;
722 }
723 }
724 break;
725 case 4:
726 /* nexthdr & padding are in the last fragment */
727 if (EXTRACT_BE_U_2(&ip->ip_off) & IP_MF)
728 goto fail;
729 len = EXTRACT_BE_U_2(&ip->ip_len);
730
731 /* see if we can find the SA, and if so, decode it */
732 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
733 if (sa->spi == EXTRACT_BE_U_4(esp->esp_spi) &&
734 sa->daddr_version == 4 &&
735 UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
736 sizeof(struct in_addr)) == 0) {
737 break;
738 }
739 }
740 break;
741 default:
742 goto fail;
743 }
744
745 /* if we didn't find the specific one, then look for
746 * an unspecified one.
747 */
748 if (sa == NULL)
749 sa = ndo->ndo_sa_default;
750
751 /* if not found fail */
752 if (sa == NULL)
753 goto fail;
754
755 /* if we can't get nexthdr, we do not need to decrypt it */
756 if (ep - bp2 < len)
757 goto fail;
758 if (ep - bp2 > len) {
759 /* FCS included at end of frame (NetBSD 1.6 or later) */
760 ep = bp2 + len;
761 }
762
763 /* pointer to the IV, if there is one */
764 ivoff = (const u_char *)(esp + 1) + 0;
765 /* length of the IV, if there is one; 0, if there isn't */
766 ivlen = sa->ivlen;
767 secret = sa->secret;
768 ep = ep - sa->authlen;
769
770 if (sa->evp) {
771 ctx = EVP_CIPHER_CTX_new();
772 if (ctx != NULL) {
773 if (set_cipher_parameters(ctx, sa->evp, secret, NULL, 0) < 0)
774 (*ndo->ndo_warning)(ndo, "espkey init failed");
775
776 p = ivoff;
777 set_cipher_parameters(ctx, NULL, NULL, p, 0);
778 len = ep - (p + ivlen);
779
780 /*
781 * Allocate a buffer for the decrypted data.
782 * The output buffer must be separate from the
783 * input buffer, and its size must be a multiple
784 * of the cipher block size.
785 */
786 block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
787 output_buffer_size = len + (block_size - len % block_size);
788 output_buffer = (u_char *)malloc(output_buffer_size);
789 if (output_buffer == NULL) {
790 (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
791 EVP_CIPHER_CTX_free(ctx);
792 return -1;
793 }
794
795 EVP_Cipher(ctx, output_buffer, p + ivlen, len);
796 EVP_CIPHER_CTX_free(ctx);
797 /*
798 * XXX - of course this is wrong, because buf is a
799 * const buffer, but changing this would require a
800 * more complicated fix.
801 */
802 memcpy(p + ivlen, output_buffer, len);
803 free(output_buffer);
804 advance = ivoff - (const u_char *)esp + ivlen;
805 } else
806 advance = sizeof(struct newesp);
807 } else
808 advance = sizeof(struct newesp);
809
810 /* sanity check for pad length */
811 if (ep - bp < EXTRACT_U_1(ep - 2))
812 goto fail;
813
814 if (padlen)
815 *padlen = EXTRACT_U_1(ep - 2) + 2;
816
817 if (nhdr)
818 *nhdr = EXTRACT_U_1(ep - 1);
819
820 ND_PRINT(": ");
821 return advance;
822 #endif
823
824 fail:
825 return -1;
826 }
827 #ifdef HAVE_LIBCRYPTO
828 USES_APPLE_RST
829 #endif
830
831 /*
832 * Local Variables:
833 * c-style: whitesmith
834 * c-basic-offset: 8
835 * End:
836 */