1 /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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
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.
24 /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
30 #include "netdissect-stdinc.h"
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.
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
47 #include "netdissect.h"
48 #include "strtoaddr.h"
51 #include "ascii_strcasecmp.h"
57 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
58 * All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
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.
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
86 * RFC1827/2406 Encapsulated Security Payload.
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 */
100 #ifdef HAVE_LIBCRYPTO
106 struct sa_list
*next
;
108 union inaddr_u daddr
;
109 uint32_t spi
; /* if == 0, then IKEv2 */
111 u_char spii
[8]; /* for IKEv2 */
113 const EVP_CIPHER
*evp
;
116 u_char authsecret
[256];
118 u_char secret
[256]; /* is that big enough for all secrets? */
122 #ifndef HAVE_EVP_CIPHER_CTX_NEW
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.
128 static EVP_CIPHER_CTX
*
129 EVP_CIPHER_CTX_new(void)
133 ctx
= malloc(sizeof(*ctx
));
136 memset(ctx
, 0, sizeof(*ctx
));
141 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
143 EVP_CIPHER_CTX_cleanup(ctx
);
148 #ifdef HAVE_EVP_CIPHERINIT_EX
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 in a second call after a first call to EVP_CipherInit_ex()
156 * to set the cipher and the key.
158 * XXX - is there some reason why we need to make two calls?
161 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
162 const unsigned char *key
,
163 const unsigned char *iv
, int enc
)
165 return EVP_CipherInit_ex(ctx
, cipher
, NULL
, key
, iv
, enc
);
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.
173 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
174 const unsigned char *key
,
175 const unsigned char *iv
, int enc
)
177 return EVP_CipherInit(ctx
, cipher
, key
, iv
, enc
);
182 * this will adjust ndo_packetp and ndo_snapend to new buffer!
184 USES_APPLE_DEPRECATED_API
185 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
187 const u_char spii
[8],
188 const u_char spir
[8],
189 const u_char
*buf
, const u_char
*end
)
195 unsigned int block_size
, buffer_size
;
196 u_char
*input_buffer
, *output_buffer
;
198 /* initiator arg is any non-zero value */
199 if(initiator
) initiator
=1;
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
) {
204 && initiator
== sa
->initiator
205 && memcmp(spii
, sa
->spii
, 8) == 0
206 && memcmp(spir
, sa
->spir
, 8) == 0)
210 if(sa
== NULL
) return 0;
211 if(sa
->evp
== NULL
) return 0;
214 * remove authenticator, and see if we still have something to
217 end
= end
- sa
->authlen
;
219 buf
= buf
+ sa
->ivlen
;
222 if(end
<= buf
) return 0;
224 ctx
= EVP_CIPHER_CTX_new();
227 if (set_cipher_parameters(ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0) {
228 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
231 if (set_cipher_parameters(ctx
, NULL
, NULL
, iv
, 0) < 0) {
232 (*ndo
->ndo_warning
)(ndo
, "IV init failed");
236 * Allocate buffers for the encrypted and decrypted data.
237 * Both buffers' sizes must be a multiple of the cipher block
238 * size, and the output buffer must be separate from the input
241 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
242 buffer_size
= len
+ (block_size
- len
% block_size
);
245 * Attempt to allocate the input buffer.
247 input_buffer
= (u_char
*)malloc(buffer_size
);
248 if (input_buffer
== NULL
) {
249 EVP_CIPHER_CTX_free(ctx
);
250 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
251 "can't allocate memory for encrypted data buffer");
254 * Copy the input data to the encrypted data buffer, and pad it
257 memcpy(input_buffer
, buf
, len
);
258 memset(input_buffer
+ len
, 0, buffer_size
- len
);
261 * Attempt to allocate the output buffer.
263 output_buffer
= (u_char
*)malloc(buffer_size
);
264 if (output_buffer
== NULL
) {
266 EVP_CIPHER_CTX_free(ctx
);
267 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
268 "can't allocate memory for decryption buffer");
270 if (!EVP_Cipher(ctx
, output_buffer
, input_buffer
, len
)) {
271 (*ndo
->ndo_warning
)(ndo
, "EVP_Cipher failed");
274 EVP_CIPHER_CTX_free(ctx
);
277 * XXX - of course this is wrong, because buf is a const buffer,
278 * but changing this would require a more complicated fix.
280 memcpy(buf
, output_buffer
, len
);
284 ndo
->ndo_packetp
= buf
;
285 ndo
->ndo_snapend
= end
;
291 static void esp_print_addsa(netdissect_options
*ndo
,
292 struct sa_list
*sa
, int sa_def
)
298 /* malloc() return used in a 'struct sa_list': do not free() */
299 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
301 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
302 "esp_print_addsa: malloc");
307 ndo
->ndo_sa_default
= nsa
;
309 nsa
->next
= ndo
->ndo_sa_list_head
;
310 ndo
->ndo_sa_list_head
= nsa
;
314 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
316 if (hex
>= '0' && hex
<= '9')
318 else if (hex
>= 'A' && hex
<= 'F')
319 return (hex
- 'A' + 10);
320 else if (hex
>= 'a' && hex
<= 'f')
321 return (hex
- 'a' + 10);
323 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_ESP_SECRET
,
324 "invalid hex digit %c in espsecret\n", hex
);
328 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
332 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
337 * returns size of binary, 0 on failure.
340 int espprint_decode_hex(netdissect_options
*ndo
,
341 u_char
*binbuf
, unsigned int binbuf_len
,
347 len
= strlen(hex
) / 2;
349 if (len
> binbuf_len
) {
350 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %u\n", len
);
355 while (hex
[0] != '\0' && hex
[1]!='\0') {
356 binbuf
[i
] = hex2byte(ndo
, hex
);
365 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
368 USES_APPLE_DEPRECATED_API
370 espprint_decode_encalgo(netdissect_options
*ndo
,
371 char *decode
, struct sa_list
*sa
)
374 const EVP_CIPHER
*evp
;
378 colon
= strchr(decode
, ':');
380 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
385 if (strlen(decode
) > strlen("-hmac96") &&
386 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
388 p
= strstr(decode
, "-hmac96");
392 if (strlen(decode
) > strlen("-cbc") &&
393 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
394 p
= strstr(decode
, "-cbc");
397 evp
= EVP_get_cipherbyname(decode
);
400 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
408 sa
->authlen
= authlen
;
409 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
412 if (colon
[0] == '0' && colon
[1] == 'x') {
413 /* decode some hex! */
416 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
417 if(sa
->secretlen
== 0) return 0;
421 if (i
< sizeof(sa
->secret
)) {
422 memcpy(sa
->secret
, colon
, i
);
425 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
426 sa
->secretlen
= sizeof(sa
->secret
);
435 * for the moment, ignore the auth algorithm, just hard code the authenticator
436 * length. Need to research how openssl looks up HMAC stuff.
439 espprint_decode_authalgo(netdissect_options
*ndo
,
440 char *decode
, struct sa_list
*sa
)
444 colon
= strchr(decode
, ':');
446 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
451 if(ascii_strcasecmp(colon
,"sha1") == 0 ||
452 ascii_strcasecmp(colon
,"md5") == 0) {
458 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
459 const char *file
, int lineno
)
461 /* it's an IKEv2 secret, store it instead */
465 char *icookie
, *rcookie
;
470 init
= strsep(&line
, " \t");
471 icookie
= strsep(&line
, " \t");
472 rcookie
= strsep(&line
, " \t");
473 authkey
= strsep(&line
, " \t");
474 enckey
= strsep(&line
, " \t");
476 /* if any fields are missing */
477 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
478 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
484 ilen
= strlen(icookie
);
485 rlen
= strlen(rcookie
);
487 if((init
[0]!='I' && init
[0]!='R')
488 || icookie
[0]!='0' || icookie
[1]!='x'
489 || rcookie
[0]!='0' || rcookie
[1]!='x'
492 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
495 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
496 init
, icookie
, ilen
, rcookie
, rlen
);
502 sa1
.initiator
= (init
[0] == 'I');
503 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
506 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
509 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
511 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
513 esp_print_addsa(ndo
, &sa1
, FALSE
);
518 * special form: file /name
519 * causes us to go read from this file instead.
522 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
523 const char *file
, int lineno
)
531 spikey
= strsep(&line
, " \t");
533 memset(&sa1
, 0, sizeof(struct sa_list
));
535 /* if there is only one token, then it is an algo:key token */
539 /* sa1.daddr.version = 0; */
540 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
546 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
547 /* open file and read it */
550 int subfile_lineno
=0;
552 char *filename
= line
;
554 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
555 if (secretfile
== NULL
) {
556 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
557 "print_esp: can't open %s: %s\n",
558 filename
, strerror(errno
));
561 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
563 /* remove newline from the line */
564 nl
= strchr(fileline
, '\n');
567 if (fileline
[0] == '#') continue;
568 if (fileline
[0] == '\0') continue;
570 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
577 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
578 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
587 spistr
= strsep(&spikey
, "@");
588 if (spistr
== NULL
) {
589 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
593 spino
= strtoul(spistr
, &foo
, 0);
594 if (spistr
== foo
|| !spikey
) {
595 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
601 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
602 sa1
.daddr_version
= 6;
603 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
604 sa1
.daddr_version
= 4;
606 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
612 /* skip any blank spaces */
613 while (isspace((unsigned char)*decode
))
616 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
621 esp_print_addsa(ndo
, &sa1
, sa_def
);
624 USES_APPLE_DEPRECATED_API
625 static void esp_init(netdissect_options
*ndo _U_
)
628 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
629 * we check whether it's undefined or it's less than the
632 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
633 OpenSSL_add_all_algorithms();
635 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
639 void esp_print_decodesecret(netdissect_options
*ndo
)
643 static int initialized
= 0;
650 p
= ndo
->ndo_espsecret
;
652 while (p
&& p
[0] != '\0') {
653 /* pick out the first line or first thing until a comma */
654 if ((line
= strsep(&p
, "\n,")) == NULL
) {
659 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
662 ndo
->ndo_espsecret
= NULL
;
667 #ifdef HAVE_LIBCRYPTO
668 #define USED_IF_LIBCRYPTO
670 #define USED_IF_LIBCRYPTO _U_
673 #ifdef HAVE_LIBCRYPTO
674 USES_APPLE_DEPRECATED_API
677 esp_print(netdissect_options
*ndo
,
678 const u_char
*bp
, u_int length
,
679 const u_char
*bp2 USED_IF_LIBCRYPTO
,
680 u_int ver USED_IF_LIBCRYPTO
,
681 int fragmented USED_IF_LIBCRYPTO
,
682 u_int ttl_hl USED_IF_LIBCRYPTO
)
684 const struct newesp
*esp
;
686 #ifdef HAVE_LIBCRYPTO
688 struct sa_list
*sa
= NULL
;
689 const struct ip6_hdr
*ip6
= NULL
;
697 unsigned int block_size
, buffer_size
;
698 u_char
*input_buffer
, *output_buffer
;
703 ndo
->ndo_protocol
= "esp";
704 esp
= (const struct newesp
*)bp
;
706 #ifdef HAVE_LIBCRYPTO
712 /* keep secret out of a register */
713 p
= (u_char
*)&secret
;
716 /* 'ep' points to the end of available data. */
717 ep
= ndo
->ndo_snapend
;
719 if ((const u_char
*)(esp
+ 1) >= ep
) {
723 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
724 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
725 ND_PRINT(", length %u", length
);
727 #ifdef HAVE_LIBCRYPTO
728 /* initiailize SAs */
729 if (ndo
->ndo_sa_list_head
== NULL
) {
730 if (!ndo
->ndo_espsecret
)
733 esp_print_decodesecret(ndo
);
736 if (ndo
->ndo_sa_list_head
== NULL
)
739 ip
= (const struct ip
*)bp2
;
742 ip6
= (const struct ip6_hdr
*)bp2
;
743 /* we do not attempt to decrypt jumbograms */
744 if (!GET_BE_U_2(ip6
->ip6_plen
))
746 /* XXX - check whether it's fragmented? */
747 /* if we can't get nexthdr, we do not need to decrypt it */
748 len
= sizeof(struct ip6_hdr
) + GET_BE_U_2(ip6
->ip6_plen
);
750 /* see if we can find the SA, and if so, decode it */
751 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
752 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
753 sa
->daddr_version
== 6 &&
754 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
755 sizeof(nd_ipv6
)) == 0) {
761 /* nexthdr & padding are in the last fragment */
764 len
= GET_BE_U_2(ip
->ip_len
);
766 /* see if we can find the SA, and if so, decode it */
767 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
768 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
769 sa
->daddr_version
== 4 &&
770 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
771 sizeof(nd_ipv4
)) == 0) {
780 /* if we didn't find the specific one, then look for
781 * an unspecified one.
784 sa
= ndo
->ndo_sa_default
;
786 /* if not found fail */
790 /* if we can't get nexthdr, we do not need to decrypt it */
793 if (ep
- bp2
> len
) {
794 /* FCS included at end of frame (NetBSD 1.6 or later) */
798 /* pointer to the IV, if there is one */
799 ivoff
= (const u_char
*)(esp
+ 1) + 0;
800 /* length of the IV, if there is one; 0, if there isn't */
804 * Make sure the authentication data/integrity check value length
805 * isn't bigger than the total amount of data available and, if
806 * not, slice that off.
808 if (ep
- bp
< sa
->authlen
)
810 ep
= ep
- sa
->authlen
;
814 ctx
= EVP_CIPHER_CTX_new();
817 * Failed to initialize the cipher context.
818 * From a look at the OpenSSL code, this appears to
819 * mean "couldn't allocate memory for the cipher context";
820 * note that we're not passing any parameters, so there's
821 * not much else it can mean.
823 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
824 "esp_print: can't allocate memory for cipher context");
827 if (set_cipher_parameters(ctx
, sa
->evp
, secret
, NULL
, 0) < 0) {
828 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
833 if (set_cipher_parameters(ctx
, NULL
, NULL
, p
, 0) < 0) {
834 (*ndo
->ndo_warning
)(ndo
, "IV init failed");
837 len
= ep
- (p
+ ivlen
);
840 * Allocate buffers for the encrypted and decrypted
841 * data. Both buffers' sizes must be a multiple of
842 * the cipher block size, and the output buffer must
843 * be separate from the input buffer.
845 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
846 buffer_size
= len
+ (block_size
- len
% block_size
);
849 * Attempt to allocate the input buffer.
851 input_buffer
= (u_char
*)malloc(buffer_size
);
852 if (input_buffer
== NULL
) {
853 EVP_CIPHER_CTX_free(ctx
);
854 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
855 "esp_print: can't allocate memory for encrypted data buffer");
858 * Copy the input data to the encrypted data buffer,
859 * and pad it with zeroes.
861 memcpy(input_buffer
, p
+ ivlen
, len
);
862 memset(input_buffer
+ len
, 0, buffer_size
- len
);
865 * Attempt to allocate the output buffer.
867 output_buffer
= (u_char
*)malloc(buffer_size
);
868 if (output_buffer
== NULL
) {
870 EVP_CIPHER_CTX_free(ctx
);
871 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
872 "esp_print: can't allocate memory for decryption buffer");
875 if (!EVP_Cipher(ctx
, output_buffer
, input_buffer
, len
)) {
877 (*ndo
->ndo_warning
)(ndo
, "EVP_Cipher failed");
881 EVP_CIPHER_CTX_free(ctx
);
883 * XXX - of course this is wrong, because buf is a
884 * const buffer, but changing this would require a
885 * more complicated fix.
887 memcpy(p
+ ivlen
, output_buffer
, len
);
889 advance
= ivoff
- (const u_char
*)esp
+ ivlen
;
892 * Sanity check for pad length.
894 * XXX - the check can fail if the packet is corrupt *or* if
895 * it was not decrypted with the correct key, so that the
896 * "plaintext" is not what was being sent.
898 padlen
= GET_U_1(ep
- 2);
899 if (ep
- bp
< padlen
) {
905 * Sanity check for payload length; +2 is for the pad length
906 * and next header fields.
908 * XXX - the check can fail if the packet is corrupt *or* if
909 * it was not decrypted with the correct key, so that the
910 * "plaintext" is not what was being sent.
912 if (length
<= advance
+ padlen
+ 2) {
917 length
-= advance
+ padlen
+ 2;
919 /* Get the next header */
920 nh
= GET_U_1(ep
- 1);
924 /* Now print the payload. */
925 ip_print_demux(ndo
, bp
, length
, ver
, fragmented
, ttl_hl
, nh
, bp2
);
928 #ifdef HAVE_LIBCRYPTO