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