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