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 */
28 #include "netdissect-stdinc.h"
34 #include <openssl/evp.h>
37 #include "netdissect.h"
40 #include "diag-control.h"
43 #include "strtoaddr.h"
44 #include "ascii_strcasecmp.h"
51 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
52 * All rights reserved.
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the project nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 * RFC1827/2406 Encapsulated Security Payload.
84 nd_uint32_t esp_spi
; /* ESP */
85 nd_uint32_t esp_seq
; /* Sequence number */
86 /*variable size*/ /* (IV and) Payload data */
87 /*variable size*/ /* padding */
88 /*8bit*/ /* pad size */
89 /*8bit*/ /* next header */
90 /*8bit*/ /* next header */
91 /*variable size, 32bit bound*/ /* Authentication data */
100 struct sa_list
*next
;
102 union inaddr_u daddr
;
103 uint32_t spi
; /* if == 0, then IKEv2 */
105 u_char spii
[8]; /* for IKEv2 */
107 const EVP_CIPHER
*evp
;
110 u_char authsecret
[256];
112 u_char secret
[256]; /* is that big enough for all secrets? */
116 #ifndef HAVE_EVP_CIPHER_CTX_NEW
118 * Allocate an EVP_CIPHER_CTX.
119 * Used if we have an older version of OpenSSL that doesn't provide
120 * routines to allocate and free them.
122 static EVP_CIPHER_CTX
*
123 EVP_CIPHER_CTX_new(void)
127 ctx
= malloc(sizeof(*ctx
));
130 memset(ctx
, 0, sizeof(*ctx
));
135 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
137 EVP_CIPHER_CTX_cleanup(ctx
);
142 #ifdef HAVE_EVP_DECRYPTINIT_EX
144 * Initialize the cipher by calling EVP_DecryptInit_ex(), because
145 * calling EVP_DecryptInit() will reset the cipher context, clearing
146 * the cipher, so calling it twice, with the second call having a
147 * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(),
148 * however, won't reset the cipher context, so you can use it to specify
149 * the IV in a second call after a first call to EVP_DecryptInit_ex()
150 * to set the cipher and the key.
152 * XXX - is there some reason why we need to make two calls?
155 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
156 const unsigned char *key
,
157 const unsigned char *iv
)
159 return EVP_DecryptInit_ex(ctx
, cipher
, NULL
, key
, iv
);
163 * Initialize the cipher by calling EVP_DecryptInit(), because we don't
164 * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
167 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
168 const unsigned char *key
,
169 const unsigned char *iv
)
171 return EVP_DecryptInit(ctx
, cipher
, key
, iv
);
176 do_decrypt(netdissect_options
*ndo
, const char *caller
, struct sa_list
*sa
,
177 const u_char
*iv
, const u_char
*ct
, unsigned int ctlen
)
180 unsigned int block_size
;
185 ctx
= EVP_CIPHER_CTX_new();
188 * Failed to initialize the cipher context.
189 * From a look at the OpenSSL code, this appears to
190 * mean "couldn't allocate memory for the cipher context";
191 * note that we're not passing any parameters, so there's
192 * not much else it can mean.
194 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
195 "%s: can't allocate memory for cipher context", caller
);
199 if (set_cipher_parameters(ctx
, sa
->evp
, sa
->secret
, NULL
) < 0) {
200 EVP_CIPHER_CTX_free(ctx
);
201 (*ndo
->ndo_warning
)(ndo
, "%s: espkey init failed", caller
);
204 if (set_cipher_parameters(ctx
, NULL
, NULL
, iv
) < 0) {
205 EVP_CIPHER_CTX_free(ctx
);
206 (*ndo
->ndo_warning
)(ndo
, "%s: IV init failed", caller
);
211 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
212 * if the cipher has a block size of which the ciphertext's size must
213 * be a multiple, the payload must be padded to make that happen, so
214 * the ciphertext length must be a multiple of the block size. Fail
215 * if that's not the case.
217 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
218 if ((ctlen
% block_size
) != 0) {
219 EVP_CIPHER_CTX_free(ctx
);
220 (*ndo
->ndo_warning
)(ndo
,
221 "%s: ciphertext size %u is not a multiple of the cipher block size %u",
222 caller
, ctlen
, block_size
);
227 * Attempt to allocate a buffer for the decrypted data, because
228 * we can't decrypt on top of the input buffer.
231 pt
= (u_char
*)calloc(1, ptlen
);
233 EVP_CIPHER_CTX_free(ctx
);
234 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
235 "%s: can't allocate memory for decryption buffer", caller
);
240 * The size of the ciphertext handed to us is a multiple of the
241 * cipher block size, so we don't need to worry about padding.
243 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0)) {
245 EVP_CIPHER_CTX_free(ctx
);
246 (*ndo
->ndo_warning
)(ndo
,
247 "%s: EVP_CIPHER_CTX_set_padding failed", caller
);
250 if (!EVP_DecryptUpdate(ctx
, pt
, &len
, ct
, ctlen
)) {
252 EVP_CIPHER_CTX_free(ctx
);
253 (*ndo
->ndo_warning
)(ndo
, "%s: EVP_DecryptUpdate failed",
257 EVP_CIPHER_CTX_free(ctx
);
262 * This will allocate a new buffer containing the decrypted data.
263 * It returns 1 on success and 0 on failure.
265 * It will push the new buffer and the values of ndo->ndo_packetp and
266 * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
267 * and ndo->ndo_snapend to refer to the new buffer.
269 * Our caller must pop the buffer off the stack when it's finished
270 * dissecting anything in it and before it does any dissection of
271 * anything in the old buffer. That will free the new buffer.
274 int esp_decrypt_buffer_by_ikev2_print(netdissect_options
*ndo
,
276 const u_char spii
[8],
277 const u_char spir
[8],
278 const u_char
*buf
, const u_char
*end
)
286 /* initiator arg is any non-zero value */
287 if(initiator
) initiator
=1;
289 /* see if we can find the SA, and if so, decode it */
290 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
292 && initiator
== sa
->initiator
293 && memcmp(spii
, sa
->spii
, 8) == 0
294 && memcmp(spir
, sa
->spir
, 8) == 0)
298 if(sa
== NULL
) return 0;
299 if(sa
->evp
== NULL
) return 0;
302 * remove authenticator, and see if we still have something to
305 end
= end
- sa
->authlen
;
310 if(end
<= ct
) return 0;
312 pt
= do_decrypt(ndo
, __func__
, sa
, iv
,
318 * Switch to the output buffer for dissection, and save it
319 * on the buffer stack so it can be freed; our caller must
322 if (!nd_push_buffer(ndo
, pt
, pt
, ctlen
)) {
324 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
325 "%s: can't push buffer on buffer stack", __func__
);
332 static void esp_print_addsa(netdissect_options
*ndo
,
333 const struct sa_list
*sa
, int sa_def
)
339 /* malloc() return used in a 'struct sa_list': do not free() */
340 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
342 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
343 "%s: malloc", __func__
);
348 ndo
->ndo_sa_default
= nsa
;
350 nsa
->next
= ndo
->ndo_sa_list_head
;
351 ndo
->ndo_sa_list_head
= nsa
;
355 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
357 if (hex
>= '0' && hex
<= '9')
359 else if (hex
>= 'A' && hex
<= 'F')
360 return (hex
- 'A' + 10);
361 else if (hex
>= 'a' && hex
<= 'f')
362 return (hex
- 'a' + 10);
364 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_ESP_SECRET
,
365 "invalid hex digit %c in espsecret\n", hex
);
369 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
373 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
378 * returns size of binary, 0 on failure.
381 espprint_decode_hex(netdissect_options
*ndo
,
382 u_char
*binbuf
, unsigned int binbuf_len
, char *hex
)
387 len
= strlen(hex
) / 2;
389 if (len
> binbuf_len
) {
390 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %u\n", len
);
395 while (hex
[0] != '\0' && hex
[1]!='\0') {
396 binbuf
[i
] = hex2byte(ndo
, hex
);
405 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
410 espprint_decode_encalgo(netdissect_options
*ndo
,
411 char *decode
, struct sa_list
*sa
)
414 const EVP_CIPHER
*evp
;
417 const char *real_decode
;
419 colon
= strchr(decode
, ':');
421 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
426 if (strlen(decode
) > strlen("-hmac96") &&
427 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
429 p
= strstr(decode
, "-hmac96");
433 if (strlen(decode
) > strlen("-cbc") &&
434 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
435 p
= strstr(decode
, "-cbc");
439 * Not all versions of libcrypto support calls to add aliases
440 * to ciphers - newer versions of libressl don't - so, instead
441 * of making "3des" an alias for "des_ede3_cbc", if attempting
442 * to get the cipher fails and the name is "3des", we try
445 real_decode
= decode
;
446 if (strcmp(real_decode
, "3des") == 0)
447 real_decode
= "des-ede3-cbc";
448 evp
= EVP_get_cipherbyname(real_decode
);
451 if (decode
!= real_decode
)
452 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s (%s)\n", real_decode
, decode
);
454 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
462 sa
->authlen
= authlen
;
463 /* This returns an int, but it should never be negative */
464 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
467 if (colon
[0] == '0' && colon
[1] == 'x') {
468 /* decode some hex! */
471 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
472 if(sa
->secretlen
== 0) return 0;
476 if (i
< sizeof(sa
->secret
)) {
477 memcpy(sa
->secret
, colon
, i
);
480 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
481 sa
->secretlen
= sizeof(sa
->secret
);
490 * for the moment, ignore the auth algorithm, just hard code the authenticator
491 * length. Need to research how openssl looks up HMAC stuff.
494 espprint_decode_authalgo(netdissect_options
*ndo
,
495 char *decode
, struct sa_list
*sa
)
499 colon
= strchr(decode
, ':');
501 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
506 if(ascii_strcasecmp(decode
,"sha1") == 0 ||
507 ascii_strcasecmp(decode
,"md5") == 0) {
513 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
514 const char *file
, int lineno
)
516 /* it's an IKEv2 secret, store it instead */
520 char *icookie
, *rcookie
;
525 init
= strsep(&line
, " \t");
526 icookie
= strsep(&line
, " \t");
527 rcookie
= strsep(&line
, " \t");
528 authkey
= strsep(&line
, " \t");
529 enckey
= strsep(&line
, " \t");
531 /* if any fields are missing */
532 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
533 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
539 ilen
= strlen(icookie
);
540 rlen
= strlen(rcookie
);
542 if((init
[0]!='I' && init
[0]!='R')
543 || icookie
[0]!='0' || icookie
[1]!='x'
544 || rcookie
[0]!='0' || rcookie
[1]!='x'
547 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
550 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
551 init
, icookie
, ilen
, rcookie
, rlen
);
557 sa1
.initiator
= (init
[0] == 'I');
558 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
561 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
564 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
566 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
568 esp_print_addsa(ndo
, &sa1
, FALSE
);
573 * special form: file /name
574 * causes us to go read from this file instead.
577 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
578 const char *file
, int lineno
)
586 spikey
= strsep(&line
, " \t");
588 memset(&sa1
, 0, sizeof(struct sa_list
));
590 /* if there is only one token, then it is an algo:key token */
594 /* sa1.daddr.version = 0; */
595 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
601 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
602 /* open file and read it */
605 int subfile_lineno
=0;
607 char *filename
= line
;
609 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
610 if (secretfile
== NULL
) {
611 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
612 "%s: can't open %s: %s\n",
613 __func__
, filename
, strerror(errno
));
616 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
618 /* remove newline from the line */
619 nl
= strchr(fileline
, '\n');
622 if (fileline
[0] == '#') continue;
623 if (fileline
[0] == '\0') continue;
625 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
632 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
633 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
642 spistr
= strsep(&spikey
, "@");
643 if (spistr
== NULL
) {
644 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
648 spino
= strtoul(spistr
, &foo
, 0);
649 if (spistr
== foo
|| !spikey
) {
650 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
656 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
657 sa1
.daddr_version
= 6;
658 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
659 sa1
.daddr_version
= 4;
661 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
667 /* skip any blank spaces */
668 while (*decode
== ' ' || *decode
== '\t' || *decode
== '\r' || *decode
== '\n')
671 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
676 esp_print_addsa(ndo
, &sa1
, sa_def
);
680 static void esp_init(netdissect_options
*ndo _U_
)
683 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
684 * we check whether it's undefined or it's less than the
687 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
688 OpenSSL_add_all_algorithms();
693 void esp_decodesecret_print(netdissect_options
*ndo
)
697 static int initialized
= 0;
704 p
= ndo
->ndo_espsecret
;
706 while (p
&& p
[0] != '\0') {
707 /* pick out the first line or first thing until a comma */
708 if ((line
= strsep(&p
, "\n,")) == NULL
) {
713 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
716 ndo
->ndo_espsecret
= NULL
;
721 #ifdef HAVE_LIBCRYPTO
722 #define USED_IF_LIBCRYPTO
724 #define USED_IF_LIBCRYPTO _U_
727 #ifdef HAVE_LIBCRYPTO
731 esp_print(netdissect_options
*ndo
,
732 const u_char
*bp
, u_int length
,
733 const u_char
*bp2 USED_IF_LIBCRYPTO
,
734 u_int ver USED_IF_LIBCRYPTO
,
735 int fragmented USED_IF_LIBCRYPTO
,
736 u_int ttl_hl USED_IF_LIBCRYPTO
)
738 const struct newesp
*esp
;
740 #ifdef HAVE_LIBCRYPTO
742 struct sa_list
*sa
= NULL
;
743 const struct ip6_hdr
*ip6
= NULL
;
753 ndo
->ndo_protocol
= "esp";
754 esp
= (const struct newesp
*)bp
;
756 /* 'ep' points to the end of available data. */
757 ep
= ndo
->ndo_snapend
;
759 if ((const u_char
*)(esp
+ 1) >= ep
) {
763 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
764 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
765 ND_PRINT(", length %u", length
);
767 #ifdef HAVE_LIBCRYPTO
769 if (ndo
->ndo_sa_list_head
== NULL
) {
770 if (!ndo
->ndo_espsecret
)
773 esp_decodesecret_print(ndo
);
776 if (ndo
->ndo_sa_list_head
== NULL
)
779 ip
= (const struct ip
*)bp2
;
782 ip6
= (const struct ip6_hdr
*)bp2
;
783 /* we do not attempt to decrypt jumbograms */
784 if (!GET_BE_U_2(ip6
->ip6_plen
))
786 /* XXX - check whether it's fragmented? */
787 /* if we can't get nexthdr, we do not need to decrypt it */
789 /* see if we can find the SA, and if so, decode it */
790 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
791 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
792 sa
->daddr_version
== 6 &&
793 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
794 sizeof(nd_ipv6
)) == 0) {
800 /* nexthdr & padding are in the last fragment */
804 /* see if we can find the SA, and if so, decode it */
805 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
806 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
807 sa
->daddr_version
== 4 &&
808 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
809 sizeof(nd_ipv4
)) == 0) {
818 /* if we didn't find the specific one, then look for
819 * an unspecified one.
822 sa
= ndo
->ndo_sa_default
;
824 /* if not found fail */
828 /* pointer to the IV, if there is one */
829 iv
= (const u_char
*)(esp
+ 1) + 0;
830 /* length of the IV, if there is one; 0, if there isn't */
834 * Get a pointer to the ciphertext.
836 * p points to the beginning of the payload, i.e. to the
837 * initialization vector, so if we skip past the initialization
838 * vector, it points to the beginning of the ciphertext.
843 * Make sure the authentication data/integrity check value length
844 * isn't bigger than the total amount of data available after
845 * the ESP header and initialization vector is removed and,
846 * if not, slice the authentication data/ICV off.
848 if (ep
- ct
< sa
->authlen
) {
852 ep
= ep
- sa
->authlen
;
855 * Calculate the length of the ciphertext. ep points to
856 * the beginning of the authentication data/integrity check
857 * value, i.e. right past the end of the ciphertext;
859 payloadlen
= ep
- ct
;
865 * If the next header value is past the end of the available
866 * data, we won't be able to fetch it once we've decrypted
867 * the ciphertext, so there's no point in decrypting the data.
869 * Report it as truncation.
871 if (!ND_TTEST_1(ep
- 1)) {
876 pt
= do_decrypt(ndo
, __func__
, sa
, iv
, ct
, payloadlen
);
881 * Switch to the output buffer for dissection, and
882 * save it on the buffer stack so it can be freed.
884 if (!nd_push_buffer(ndo
, pt
, pt
, payloadlen
)) {
886 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
887 "%s: can't push buffer on buffer stack", __func__
);
891 * Sanity check for pad length; if it, plus 2 for the pad
892 * length and next header fields, is bigger than the ciphertext
893 * length (which is also the plaintext length), it's too big.
895 * XXX - the check can fail if the packet is corrupt *or* if
896 * it was not decrypted with the correct key, so that the
897 * "plaintext" is not what was being sent.
899 padlen
= GET_U_1(pt
+ payloadlen
- 2);
900 if (padlen
+ 2 > payloadlen
) {
905 /* Get the next header */
906 nh
= GET_U_1(pt
+ payloadlen
- 1);
911 * Don't put padding + padding length(1 byte) + next header(1 byte)
912 * in the buffer because they are not part of the plaintext to decode.
914 if (!nd_push_snaplen(ndo
, pt
, payloadlen
- (padlen
+ 2))) {
915 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
916 "%s: can't push snaplen on buffer stack", __func__
);
919 /* Now dissect the plaintext. */
920 ip_demux_print(ndo
, pt
, payloadlen
- (padlen
+ 2), ver
, fragmented
,
923 /* Pop the buffer, freeing it. */
924 nd_pop_packet_info(ndo
);
925 /* Pop the nd_push_snaplen */
926 nd_pop_packet_info(ndo
);
929 #ifdef HAVE_LIBCRYPTO