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
= calloc(1, sizeof(*ctx
));
132 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
134 EVP_CIPHER_CTX_cleanup(ctx
);
139 #ifdef HAVE_EVP_DECRYPTINIT_EX
141 * Initialize the cipher by calling EVP_DecryptInit_ex(), because
142 * calling EVP_DecryptInit() will reset the cipher context, clearing
143 * the cipher, so calling it twice, with the second call having a
144 * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(),
145 * however, won't reset the cipher context, so you can use it to specify
146 * the IV in a second call after a first call to EVP_DecryptInit_ex()
147 * to set the cipher and the key.
149 * XXX - is there some reason why we need to make two calls?
152 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
153 const unsigned char *key
,
154 const unsigned char *iv
)
156 return EVP_DecryptInit_ex(ctx
, cipher
, NULL
, key
, iv
);
160 * Initialize the cipher by calling EVP_DecryptInit(), because we don't
161 * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
164 set_cipher_parameters(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
165 const unsigned char *key
,
166 const unsigned char *iv
)
168 return EVP_DecryptInit(ctx
, cipher
, key
, iv
);
173 do_decrypt(netdissect_options
*ndo
, const char *caller
, struct sa_list
*sa
,
174 const u_char
*iv
, const u_char
*ct
, unsigned int ctlen
)
177 unsigned int block_size
;
182 ctx
= EVP_CIPHER_CTX_new();
185 * Failed to initialize the cipher context.
186 * From a look at the OpenSSL code, this appears to
187 * mean "couldn't allocate memory for the cipher context";
188 * note that we're not passing any parameters, so there's
189 * not much else it can mean.
191 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
192 "%s: can't allocate memory for cipher context", caller
);
196 if (set_cipher_parameters(ctx
, sa
->evp
, sa
->secret
, NULL
) < 0) {
197 EVP_CIPHER_CTX_free(ctx
);
198 (*ndo
->ndo_warning
)(ndo
, "%s: espkey init failed", caller
);
201 if (set_cipher_parameters(ctx
, NULL
, NULL
, iv
) < 0) {
202 EVP_CIPHER_CTX_free(ctx
);
203 (*ndo
->ndo_warning
)(ndo
, "%s: IV init failed", caller
);
208 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
209 * if the cipher has a block size of which the ciphertext's size must
210 * be a multiple, the payload must be padded to make that happen, so
211 * the ciphertext length must be a multiple of the block size. Fail
212 * if that's not the case.
214 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
215 if ((ctlen
% block_size
) != 0) {
216 EVP_CIPHER_CTX_free(ctx
);
217 (*ndo
->ndo_warning
)(ndo
,
218 "%s: ciphertext size %u is not a multiple of the cipher block size %u",
219 caller
, ctlen
, block_size
);
224 * Attempt to allocate a buffer for the decrypted data, because
225 * we can't decrypt on top of the input buffer.
228 pt
= (u_char
*)calloc(1, ptlen
);
230 EVP_CIPHER_CTX_free(ctx
);
231 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
232 "%s: can't allocate memory for decryption buffer", caller
);
237 * The size of the ciphertext handed to us is a multiple of the
238 * cipher block size, so we don't need to worry about padding.
240 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0)) {
242 EVP_CIPHER_CTX_free(ctx
);
243 (*ndo
->ndo_warning
)(ndo
,
244 "%s: EVP_CIPHER_CTX_set_padding failed", caller
);
247 if (!EVP_DecryptUpdate(ctx
, pt
, &len
, ct
, ctlen
)) {
249 EVP_CIPHER_CTX_free(ctx
);
250 (*ndo
->ndo_warning
)(ndo
, "%s: EVP_DecryptUpdate failed",
254 EVP_CIPHER_CTX_free(ctx
);
259 * This will allocate a new buffer containing the decrypted data.
260 * It returns 1 on success and 0 on failure.
262 * It will push the new buffer and the values of ndo->ndo_packetp and
263 * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
264 * and ndo->ndo_snapend to refer to the new buffer.
266 * Our caller must pop the buffer off the stack when it's finished
267 * dissecting anything in it and before it does any dissection of
268 * anything in the old buffer. That will free the new buffer.
271 int esp_decrypt_buffer_by_ikev2_print(netdissect_options
*ndo
,
273 const u_char spii
[8],
274 const u_char spir
[8],
275 const u_char
*buf
, const u_char
*end
)
283 /* initiator arg is any non-zero value */
284 if(initiator
) initiator
=1;
286 /* see if we can find the SA, and if so, decode it */
287 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
289 && initiator
== sa
->initiator
290 && memcmp(spii
, sa
->spii
, 8) == 0
291 && memcmp(spir
, sa
->spir
, 8) == 0)
295 if(sa
== NULL
) return 0;
296 if(sa
->evp
== NULL
) return 0;
299 * remove authenticator, and see if we still have something to
302 end
= end
- sa
->authlen
;
307 if(end
<= ct
) return 0;
309 pt
= do_decrypt(ndo
, __func__
, sa
, iv
,
315 * Switch to the output buffer for dissection, and save it
316 * on the buffer stack so it can be freed; our caller must
319 if (!nd_push_buffer(ndo
, pt
, pt
, ctlen
)) {
321 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
322 "%s: can't push buffer on buffer stack", __func__
);
329 static void esp_print_addsa(netdissect_options
*ndo
,
330 const struct sa_list
*sa
, int sa_def
)
336 /* malloc() return used in a 'struct sa_list': do not free() */
337 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
339 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
340 "%s: malloc", __func__
);
345 ndo
->ndo_sa_default
= nsa
;
347 nsa
->next
= ndo
->ndo_sa_list_head
;
348 ndo
->ndo_sa_list_head
= nsa
;
352 static int hexdigit(netdissect_options
*ndo
, char hex
)
354 if (hex
>= '0' && hex
<= '9')
356 else if (hex
>= 'A' && hex
<= 'F')
357 return (hex
- 'A' + 10);
358 else if (hex
>= 'a' && hex
<= 'f')
359 return (hex
- 'a' + 10);
361 (*ndo
->ndo_warning
)(ndo
,
362 "invalid hex digit %c in espsecret\n", hex
);
368 * returns size of binary, 0 on failure.
371 espprint_decode_hex(netdissect_options
*ndo
,
372 u_char
*binbuf
, unsigned int binbuf_len
, char *hex
)
378 * XXX - fail if the string length isn't a multiple of 2?
380 len
= strlen(hex
) / 2;
382 if (len
> binbuf_len
) {
383 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %zu\n", len
);
388 while (hex
[0] != '\0' && hex
[1]!='\0') {
389 int upper_nibble
, lower_nibble
;
391 upper_nibble
= hexdigit(ndo
, hex
[0]);
392 if (upper_nibble
< 0) {
394 * Invalid hex digit; a warning has already been
399 lower_nibble
= hexdigit(ndo
, hex
[1]);
400 if (lower_nibble
< 0) {
402 * Invalid hex digit; a warning has already been
407 binbuf
[i
] = (((u_int
)upper_nibble
) << 4) + (((u_int
)lower_nibble
) << 0);
416 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
421 espprint_decode_encalgo(netdissect_options
*ndo
,
422 char *decode
, struct sa_list
*sa
)
425 const EVP_CIPHER
*evp
;
429 colon
= strchr(decode
, ':');
431 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
436 if (strlen(decode
) > strlen("-hmac96") &&
437 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
439 p
= strstr(decode
, "-hmac96");
443 if (strlen(decode
) > strlen("-cbc") &&
444 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
445 p
= strstr(decode
, "-cbc");
448 evp
= EVP_get_cipherbyname(decode
);
451 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
459 sa
->authlen
= authlen
;
460 /* This returns an int, but it should never be negative */
461 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
464 if (colon
[0] == '0' && colon
[1] == 'x') {
465 /* decode some hex! */
468 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
469 if(sa
->secretlen
== 0) return 0;
473 if (i
< sizeof(sa
->secret
)) {
474 memcpy(sa
->secret
, colon
, i
);
477 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
478 sa
->secretlen
= sizeof(sa
->secret
);
487 * for the moment, ignore the auth algorithm, just hard code the authenticator
488 * length. Need to research how openssl looks up HMAC stuff.
491 espprint_decode_authalgo(netdissect_options
*ndo
,
492 char *decode
, struct sa_list
*sa
)
496 colon
= strchr(decode
, ':');
498 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
503 if(ascii_strcasecmp(decode
,"sha1") == 0 ||
504 ascii_strcasecmp(decode
,"md5") == 0) {
510 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
511 const char *file
, int lineno
)
513 /* it's an IKEv2 secret, store it instead */
517 char *icookie
, *rcookie
;
522 init
= strsep(&line
, " \t");
523 icookie
= strsep(&line
, " \t");
524 rcookie
= strsep(&line
, " \t");
525 authkey
= strsep(&line
, " \t");
526 enckey
= strsep(&line
, " \t");
528 /* if any fields are missing */
529 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
530 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
536 ilen
= strlen(icookie
);
537 rlen
= strlen(rcookie
);
539 if((init
[0]!='I' && init
[0]!='R')
540 || icookie
[0]!='0' || icookie
[1]!='x'
541 || rcookie
[0]!='0' || rcookie
[1]!='x'
544 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
547 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
548 init
, icookie
, ilen
, rcookie
, rlen
);
554 sa1
.initiator
= (init
[0] == 'I');
555 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
558 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
561 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
563 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
565 esp_print_addsa(ndo
, &sa1
, FALSE
);
570 * special form: file /name
571 * causes us to go read from this file instead.
574 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
575 const char *file
, int lineno
)
583 spikey
= strsep(&line
, " \t");
585 memset(&sa1
, 0, sizeof(struct sa_list
));
587 /* if there is only one token, then it is an algo:key token */
591 /* sa1.daddr.version = 0; */
592 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
598 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
599 /* open file and read it */
602 int subfile_lineno
=0;
604 char *filename
= line
;
606 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
607 if (secretfile
== NULL
) {
608 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
609 "%s: can't open %s: %s\n",
610 __func__
, filename
, strerror(errno
));
613 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
615 /* remove newline from the line */
616 nl
= strchr(fileline
, '\n');
619 if (fileline
[0] == '#') continue;
620 if (fileline
[0] == '\0') continue;
622 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
629 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
630 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
639 spistr
= strsep(&spikey
, "@");
640 if (spistr
== NULL
) {
641 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
645 spino
= strtoul(spistr
, &foo
, 0);
646 if (spistr
== foo
|| !spikey
) {
647 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
653 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
654 sa1
.daddr_version
= 6;
655 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
656 sa1
.daddr_version
= 4;
658 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
664 /* skip any blank spaces */
665 while (*decode
== ' ' || *decode
== '\t' || *decode
== '\r' || *decode
== '\n')
668 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
673 esp_print_addsa(ndo
, &sa1
, sa_def
);
677 static void esp_init(netdissect_options
*ndo _U_
)
680 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
681 * we check whether it's undefined or it's less than the
684 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
685 OpenSSL_add_all_algorithms();
687 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
691 void esp_decodesecret_print(netdissect_options
*ndo
)
695 static int initialized
= 0;
702 p
= ndo
->ndo_espsecret
;
704 while (p
&& p
[0] != '\0') {
705 /* pick out the first line or first thing until a comma */
706 if ((line
= strsep(&p
, "\n,")) == NULL
) {
711 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
714 ndo
->ndo_espsecret
= NULL
;
719 #ifdef HAVE_LIBCRYPTO
720 #define USED_IF_LIBCRYPTO
722 #define USED_IF_LIBCRYPTO _U_
725 #ifdef HAVE_LIBCRYPTO
729 esp_print(netdissect_options
*ndo
,
730 const u_char
*bp
, u_int length
,
731 const u_char
*bp2 USED_IF_LIBCRYPTO
,
732 u_int ver USED_IF_LIBCRYPTO
,
733 int fragmented USED_IF_LIBCRYPTO
,
734 u_int ttl_hl USED_IF_LIBCRYPTO
)
736 const struct newesp
*esp
;
738 #ifdef HAVE_LIBCRYPTO
740 struct sa_list
*sa
= NULL
;
741 const struct ip6_hdr
*ip6
= NULL
;
751 ndo
->ndo_protocol
= "esp";
752 esp
= (const struct newesp
*)bp
;
754 /* 'ep' points to the end of available data. */
755 ep
= ndo
->ndo_snapend
;
757 if ((const u_char
*)(esp
+ 1) >= ep
) {
761 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
762 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
763 ND_PRINT(", length %u", length
);
765 #ifdef HAVE_LIBCRYPTO
767 if (ndo
->ndo_sa_list_head
== NULL
) {
768 if (!ndo
->ndo_espsecret
)
771 esp_decodesecret_print(ndo
);
774 if (ndo
->ndo_sa_list_head
== NULL
)
777 ip
= (const struct ip
*)bp2
;
780 ip6
= (const struct ip6_hdr
*)bp2
;
781 /* we do not attempt to decrypt jumbograms */
782 if (!GET_BE_U_2(ip6
->ip6_plen
))
784 /* XXX - check whether it's fragmented? */
785 /* if we can't get nexthdr, we do not need to decrypt it */
787 /* see if we can find the SA, and if so, decode it */
788 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
789 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
790 sa
->daddr_version
== 6 &&
791 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
792 sizeof(nd_ipv6
)) == 0) {
798 /* nexthdr & padding are in the last fragment */
802 /* see if we can find the SA, and if so, decode it */
803 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
804 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
805 sa
->daddr_version
== 4 &&
806 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
807 sizeof(nd_ipv4
)) == 0) {
816 /* if we didn't find the specific one, then look for
817 * an unspecified one.
820 sa
= ndo
->ndo_sa_default
;
822 /* if not found fail */
826 /* pointer to the IV, if there is one */
827 iv
= (const u_char
*)(esp
+ 1) + 0;
828 /* length of the IV, if there is one; 0, if there isn't */
832 * Get a pointer to the ciphertext.
834 * p points to the beginning of the payload, i.e. to the
835 * initialization vector, so if we skip past the initialization
836 * vector, it points to the beginning of the ciphertext.
841 * Make sure the authentication data/integrity check value length
842 * isn't bigger than the total amount of data available after
843 * the ESP header and initialization vector is removed and,
844 * if not, slice the authentication data/ICV off.
846 if (ep
- ct
< sa
->authlen
) {
850 ep
= ep
- sa
->authlen
;
853 * Calculate the length of the ciphertext. ep points to
854 * the beginning of the authentication data/integrity check
855 * value, i.e. right past the end of the ciphertext;
857 payloadlen
= ep
- ct
;
863 * If the next header value is past the end of the available
864 * data, we won't be able to fetch it once we've decrypted
865 * the ciphertext, so there's no point in decrypting the data.
867 * Report it as truncation.
869 if (!ND_TTEST_1(ep
- 1)) {
874 pt
= do_decrypt(ndo
, __func__
, sa
, iv
, ct
, payloadlen
);
879 * Switch to the output buffer for dissection, and
880 * save it on the buffer stack so it can be freed.
882 if (!nd_push_buffer(ndo
, pt
, pt
, payloadlen
)) {
884 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
885 "%s: can't push buffer on buffer stack", __func__
);
889 * Sanity check for pad length; if it, plus 2 for the pad
890 * length and next header fields, is bigger than the ciphertext
891 * length (which is also the plaintext length), it's too big.
893 * XXX - the check can fail if the packet is corrupt *or* if
894 * it was not decrypted with the correct key, so that the
895 * "plaintext" is not what was being sent.
897 padlen
= GET_U_1(pt
+ payloadlen
- 2);
898 if (padlen
+ 2 > payloadlen
) {
903 /* Get the next header */
904 nh
= GET_U_1(pt
+ payloadlen
- 1);
909 * Don't put padding + padding length(1 byte) + next header(1 byte)
910 * in the buffer because they are not part of the plaintext to decode.
912 if (!nd_push_snaplen(ndo
, pt
, payloadlen
- (padlen
+ 2))) {
913 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
914 "%s: can't push snaplen on buffer stack", __func__
);
917 /* Now dissect the plaintext. */
918 ip_demux_print(ndo
, pt
, payloadlen
- (padlen
+ 2), ver
, fragmented
,
921 /* Pop the buffer, freeing it. */
922 nd_pop_packet_info(ndo
);
923 /* Pop the nd_push_snaplen */
924 nd_pop_packet_info(ndo
);
927 #ifdef HAVE_LIBCRYPTO