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"
50 #include "diag-control.h"
53 #include "strtoaddr.h"
54 #include "ascii_strcasecmp.h"
61 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
62 * All rights reserved.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. Neither the name of the project nor the names of its contributors
73 * may be used to endorse or promote products derived from this software
74 * without specific prior written permission.
76 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
77 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * RFC1827/2406 Encapsulated Security Payload.
94 nd_uint32_t esp_spi
; /* ESP */
95 nd_uint32_t esp_seq
; /* Sequence number */
96 /*variable size*/ /* (IV and) Payload data */
97 /*variable size*/ /* padding */
98 /*8bit*/ /* pad size */
99 /*8bit*/ /* next header */
100 /*8bit*/ /* next header */
101 /*variable size, 32bit bound*/ /* Authentication data */
104 #ifdef HAVE_LIBCRYPTO
110 struct sa_list
*next
;
112 union inaddr_u daddr
;
113 uint32_t spi
; /* if == 0, then IKEv2 */
115 u_char spii
[8]; /* for IKEv2 */
117 const EVP_CIPHER
*evp
;
120 u_char authsecret
[256];
122 u_char secret
[256]; /* is that big enough for all secrets? */
126 #ifndef HAVE_EVP_CIPHER_CTX_NEW
128 * Allocate an EVP_CIPHER_CTX.
129 * Used if we have an older version of OpenSSL that doesn't provide
130 * routines to allocate and free them.
132 static EVP_CIPHER_CTX
*
133 EVP_CIPHER_CTX_new(void)
137 ctx
= malloc(sizeof(*ctx
));
140 memset(ctx
, 0, sizeof(*ctx
));
145 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
147 EVP_CIPHER_CTX_cleanup(ctx
);
152 #ifdef HAVE_EVP_DECRYPTINIT_EX
154 * Initialize the cipher by calling EVP_DecryptInit_ex(), because
155 * calling EVP_DecryptInit() will reset the cipher context, clearing
156 * the cipher, so calling it twice, with the second call having a
157 * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(),
158 * however, won't reset the cipher context, so you can use it to specify
159 * the IV in a second call after a first call to EVP_DecryptInit_ex()
160 * to set the cipher and the key.
162 * XXX - is there some reason why we need to make two calls?
165 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
166 const unsigned char *key
,
167 const unsigned char *iv
)
169 return EVP_DecryptInit_ex(ctx
, cipher
, NULL
, key
, iv
);
173 * Initialize the cipher by calling EVP_DecryptInit(), because we don't
174 * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
177 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
178 const unsigned char *key
,
179 const unsigned char *iv
)
181 return EVP_DecryptInit(ctx
, cipher
, key
, iv
);
186 do_decrypt(netdissect_options
*ndo
, const char *caller
, struct sa_list
*sa
,
187 const u_char
*iv
, const u_char
*ct
, unsigned int ctlen
)
190 unsigned int block_size
;
195 ctx
= EVP_CIPHER_CTX_new();
198 * Failed to initialize the cipher context.
199 * From a look at the OpenSSL code, this appears to
200 * mean "couldn't allocate memory for the cipher context";
201 * note that we're not passing any parameters, so there's
202 * not much else it can mean.
204 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
205 "%s: can't allocate memory for cipher context", caller
);
209 if (set_cipher_parameters(ctx
, sa
->evp
, sa
->secret
, NULL
) < 0) {
210 EVP_CIPHER_CTX_free(ctx
);
211 (*ndo
->ndo_warning
)(ndo
, "%s: espkey init failed", caller
);
214 if (set_cipher_parameters(ctx
, NULL
, NULL
, iv
) < 0) {
215 EVP_CIPHER_CTX_free(ctx
);
216 (*ndo
->ndo_warning
)(ndo
, "%s: IV init failed", caller
);
221 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
222 * if the cipher has a block size of which the ciphertext's size must
223 * be a multiple, the payload must be padded to make that happen, so
224 * the ciphertext length must be a multiple of the block size. Fail
225 * if that's not the case.
227 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
228 if ((ctlen
% block_size
) != 0) {
229 EVP_CIPHER_CTX_free(ctx
);
230 (*ndo
->ndo_warning
)(ndo
,
231 "%s: ciphertext size %u is not a multiple of the cipher block size %u",
232 caller
, ctlen
, block_size
);
237 * Attempt to allocate a buffer for the decrypted data, because
238 * we can't decrypt on top of the input buffer.
241 pt
= (u_char
*)malloc(ptlen
);
243 EVP_CIPHER_CTX_free(ctx
);
244 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
245 "%s: can't allocate memory for decryption buffer", caller
);
250 * The size of the ciphertext handed to us is a multiple of the
251 * cipher block size, so we don't need to worry about padding.
253 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0)) {
255 EVP_CIPHER_CTX_free(ctx
);
256 (*ndo
->ndo_warning
)(ndo
,
257 "%s: EVP_CIPHER_CTX_set_padding failed", caller
);
260 if (!EVP_DecryptUpdate(ctx
, pt
, &len
, ct
, ctlen
)) {
262 EVP_CIPHER_CTX_free(ctx
);
263 (*ndo
->ndo_warning
)(ndo
, "%s: EVP_DecryptUpdate failed",
267 EVP_CIPHER_CTX_free(ctx
);
272 * This will allocate a new buffer containing the decrypted data.
273 * It returns 1 on success and 0 on failure.
275 * It will push the new buffer and the values of ndo->ndo_packetp and
276 * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
277 * and ndo->ndo_snapend to refer to the new buffer.
279 * Our caller must pop the buffer off the stack when it's finished
280 * dissecting anything in it and before it does any dissection of
281 * anything in the old buffer. That will free the new buffer.
284 int esp_decrypt_buffer_by_ikev2_print(netdissect_options
*ndo
,
286 const u_char spii
[8],
287 const u_char spir
[8],
288 const u_char
*buf
, const u_char
*end
)
296 /* initiator arg is any non-zero value */
297 if(initiator
) initiator
=1;
299 /* see if we can find the SA, and if so, decode it */
300 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
302 && initiator
== sa
->initiator
303 && memcmp(spii
, sa
->spii
, 8) == 0
304 && memcmp(spir
, sa
->spir
, 8) == 0)
308 if(sa
== NULL
) return 0;
309 if(sa
->evp
== NULL
) return 0;
312 * remove authenticator, and see if we still have something to
315 end
= end
- sa
->authlen
;
320 if(end
<= ct
) return 0;
322 pt
= do_decrypt(ndo
, __func__
, sa
, iv
,
328 * Switch to the output buffer for dissection, and save it
329 * on the buffer stack so it can be freed; our caller must
332 if (!nd_push_buffer(ndo
, pt
, pt
, pt
+ ctlen
)) {
341 static void esp_print_addsa(netdissect_options
*ndo
,
342 const struct sa_list
*sa
, int sa_def
)
348 /* malloc() return used in a 'struct sa_list': do not free() */
349 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
351 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
352 "%s: malloc", __func__
);
357 ndo
->ndo_sa_default
= nsa
;
359 nsa
->next
= ndo
->ndo_sa_list_head
;
360 ndo
->ndo_sa_list_head
= nsa
;
364 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
366 if (hex
>= '0' && hex
<= '9')
368 else if (hex
>= 'A' && hex
<= 'F')
369 return (hex
- 'A' + 10);
370 else if (hex
>= 'a' && hex
<= 'f')
371 return (hex
- 'a' + 10);
373 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_ESP_SECRET
,
374 "invalid hex digit %c in espsecret\n", hex
);
378 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
382 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
387 * returns size of binary, 0 on failure.
390 espprint_decode_hex(netdissect_options
*ndo
,
391 u_char
*binbuf
, unsigned int binbuf_len
, char *hex
)
396 len
= strlen(hex
) / 2;
398 if (len
> binbuf_len
) {
399 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %u\n", len
);
404 while (hex
[0] != '\0' && hex
[1]!='\0') {
405 binbuf
[i
] = hex2byte(ndo
, hex
);
414 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
419 espprint_decode_encalgo(netdissect_options
*ndo
,
420 char *decode
, struct sa_list
*sa
)
423 const EVP_CIPHER
*evp
;
427 colon
= strchr(decode
, ':');
429 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
434 if (strlen(decode
) > strlen("-hmac96") &&
435 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
437 p
= strstr(decode
, "-hmac96");
441 if (strlen(decode
) > strlen("-cbc") &&
442 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
443 p
= strstr(decode
, "-cbc");
446 evp
= EVP_get_cipherbyname(decode
);
449 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
457 sa
->authlen
= authlen
;
458 /* This returns an int, but it should never be negative */
459 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
462 if (colon
[0] == '0' && colon
[1] == 'x') {
463 /* decode some hex! */
466 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
467 if(sa
->secretlen
== 0) return 0;
471 if (i
< sizeof(sa
->secret
)) {
472 memcpy(sa
->secret
, colon
, i
);
475 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
476 sa
->secretlen
= sizeof(sa
->secret
);
485 * for the moment, ignore the auth algorithm, just hard code the authenticator
486 * length. Need to research how openssl looks up HMAC stuff.
489 espprint_decode_authalgo(netdissect_options
*ndo
,
490 char *decode
, struct sa_list
*sa
)
494 colon
= strchr(decode
, ':');
496 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
501 if(ascii_strcasecmp(decode
,"sha1") == 0 ||
502 ascii_strcasecmp(decode
,"md5") == 0) {
508 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
509 const char *file
, int lineno
)
511 /* it's an IKEv2 secret, store it instead */
515 char *icookie
, *rcookie
;
520 init
= strsep(&line
, " \t");
521 icookie
= strsep(&line
, " \t");
522 rcookie
= strsep(&line
, " \t");
523 authkey
= strsep(&line
, " \t");
524 enckey
= strsep(&line
, " \t");
526 /* if any fields are missing */
527 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
528 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
534 ilen
= strlen(icookie
);
535 rlen
= strlen(rcookie
);
537 if((init
[0]!='I' && init
[0]!='R')
538 || icookie
[0]!='0' || icookie
[1]!='x'
539 || rcookie
[0]!='0' || rcookie
[1]!='x'
542 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
545 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
546 init
, icookie
, ilen
, rcookie
, rlen
);
552 sa1
.initiator
= (init
[0] == 'I');
553 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
556 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
559 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
561 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
563 esp_print_addsa(ndo
, &sa1
, FALSE
);
568 * special form: file /name
569 * causes us to go read from this file instead.
572 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
573 const char *file
, int lineno
)
581 spikey
= strsep(&line
, " \t");
583 memset(&sa1
, 0, sizeof(struct sa_list
));
585 /* if there is only one token, then it is an algo:key token */
589 /* sa1.daddr.version = 0; */
590 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
596 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
597 /* open file and read it */
600 int subfile_lineno
=0;
602 char *filename
= line
;
604 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
605 if (secretfile
== NULL
) {
606 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
607 "%s: can't open %s: %s\n",
608 __func__
, filename
, strerror(errno
));
611 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
613 /* remove newline from the line */
614 nl
= strchr(fileline
, '\n');
617 if (fileline
[0] == '#') continue;
618 if (fileline
[0] == '\0') continue;
620 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
627 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
628 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
637 spistr
= strsep(&spikey
, "@");
638 if (spistr
== NULL
) {
639 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
643 spino
= strtoul(spistr
, &foo
, 0);
644 if (spistr
== foo
|| !spikey
) {
645 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
651 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
652 sa1
.daddr_version
= 6;
653 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
654 sa1
.daddr_version
= 4;
656 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
662 /* skip any blank spaces */
663 while (*decode
== ' ' || *decode
== '\t' || *decode
== '\r' || *decode
== '\n')
666 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
671 esp_print_addsa(ndo
, &sa1
, sa_def
);
675 static void esp_init(netdissect_options
*ndo _U_
)
678 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
679 * we check whether it's undefined or it's less than the
682 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
683 OpenSSL_add_all_algorithms();
685 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
689 void esp_decodesecret_print(netdissect_options
*ndo
)
693 static int initialized
= 0;
700 p
= ndo
->ndo_espsecret
;
702 while (p
&& p
[0] != '\0') {
703 /* pick out the first line or first thing until a comma */
704 if ((line
= strsep(&p
, "\n,")) == NULL
) {
709 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
712 ndo
->ndo_espsecret
= NULL
;
717 #ifdef HAVE_LIBCRYPTO
718 #define USED_IF_LIBCRYPTO
720 #define USED_IF_LIBCRYPTO _U_
723 #ifdef HAVE_LIBCRYPTO
727 esp_print(netdissect_options
*ndo
,
728 const u_char
*bp
, u_int length
,
729 const u_char
*bp2 USED_IF_LIBCRYPTO
,
730 u_int ver USED_IF_LIBCRYPTO
,
731 int fragmented USED_IF_LIBCRYPTO
,
732 u_int ttl_hl USED_IF_LIBCRYPTO
)
734 const struct newesp
*esp
;
736 #ifdef HAVE_LIBCRYPTO
738 struct sa_list
*sa
= NULL
;
739 const struct ip6_hdr
*ip6
= NULL
;
749 ndo
->ndo_protocol
= "esp";
750 esp
= (const struct newesp
*)bp
;
752 /* 'ep' points to the end of available data. */
753 ep
= ndo
->ndo_snapend
;
755 if ((const u_char
*)(esp
+ 1) >= ep
) {
759 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
760 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
761 ND_PRINT(", length %u", length
);
763 #ifdef HAVE_LIBCRYPTO
764 /* initiailize SAs */
765 if (ndo
->ndo_sa_list_head
== NULL
) {
766 if (!ndo
->ndo_espsecret
)
769 esp_decodesecret_print(ndo
);
772 if (ndo
->ndo_sa_list_head
== NULL
)
775 ip
= (const struct ip
*)bp2
;
778 ip6
= (const struct ip6_hdr
*)bp2
;
779 /* we do not attempt to decrypt jumbograms */
780 if (!GET_BE_U_2(ip6
->ip6_plen
))
782 /* XXX - check whether it's fragmented? */
783 /* if we can't get nexthdr, we do not need to decrypt it */
785 /* see if we can find the SA, and if so, decode it */
786 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
787 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
788 sa
->daddr_version
== 6 &&
789 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
790 sizeof(nd_ipv6
)) == 0) {
796 /* nexthdr & padding are in the last fragment */
800 /* see if we can find the SA, and if so, decode it */
801 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
802 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
803 sa
->daddr_version
== 4 &&
804 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
805 sizeof(nd_ipv4
)) == 0) {
814 /* if we didn't find the specific one, then look for
815 * an unspecified one.
818 sa
= ndo
->ndo_sa_default
;
820 /* if not found fail */
824 /* pointer to the IV, if there is one */
825 iv
= (const u_char
*)(esp
+ 1) + 0;
826 /* length of the IV, if there is one; 0, if there isn't */
830 * Get a pointer to the ciphertext.
832 * p points to the beginning of the payload, i.e. to the
833 * initialization vector, so if we skip past the initialization
834 * vector, it points to the beginning of the ciphertext.
839 * Make sure the authentication data/integrity check value length
840 * isn't bigger than the total amount of data available after
841 * the ESP header and initialization vector is removed and,
842 * if not, slice the authentication data/ICV off.
844 if (ep
- ct
< sa
->authlen
) {
848 ep
= ep
- sa
->authlen
;
851 * Calculate the length of the ciphertext. ep points to
852 * the beginning of the authentication data/integrity check
853 * value, i.e. right past the end of the ciphertext;
855 payloadlen
= ep
- ct
;
861 * If the next header value is past the end of the available
862 * data, we won't be able to fetch it once we've decrypted
863 * the ciphertext, so there's no point in decrypting the data.
865 * Report it as truncation.
867 if (!ND_TTEST_1(ep
- 1)) {
872 pt
= do_decrypt(ndo
, __func__
, sa
, iv
, ct
, payloadlen
);
877 * Switch to the output buffer for dissection, and
878 * save it on the buffer stack so it can be freed.
880 ep
= pt
+ payloadlen
;
881 if (!nd_push_buffer(ndo
, pt
, pt
, ep
)) {
883 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
884 "%s: can't push buffer on buffer stack", __func__
);
888 * Sanity check for pad length; if it, plus 2 for the pad
889 * length and next header fields, is bigger than the ciphertext
890 * length (which is also the plaintext length), it's too big.
892 * XXX - the check can fail if the packet is corrupt *or* if
893 * it was not decrypted with the correct key, so that the
894 * "plaintext" is not what was being sent.
896 padlen
= GET_U_1(ep
- 2);
897 if (padlen
+ 2 > payloadlen
) {
902 /* Get the next header */
903 nh
= GET_U_1(ep
- 1);
908 * Don't put padding + padding length(1 byte) + next header(1 byte)
909 * in the buffer because they are not part of the plaintext to decode.
911 nd_push_snapend(ndo
, ep
- (padlen
+ 2));
913 /* Now dissect the plaintext. */
914 ip_demux_print(ndo
, pt
, payloadlen
- (padlen
+ 2), ver
, fragmented
,
917 /* Pop the buffer, freeing it. */
918 nd_pop_packet_info(ndo
);
919 /* Pop the nd_push_snapend */
920 nd_pop_packet_info(ndo
);
923 #ifdef HAVE_LIBCRYPTO