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_DECRYPTINIT_EX
150 * Initialize the cipher by calling EVP_DecryptInit_ex(), because
151 * calling EVP_DecryptInit() 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_DecryptInit_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_DecryptInit_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
)
165 return EVP_DecryptInit_ex(ctx
, cipher
, NULL
, key
, iv
);
169 * Initialize the cipher by calling EVP_DecryptInit(), because we don't
170 * have EVP_DecryptInit_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
)
177 return EVP_DecryptInit(ctx
, cipher
, key
, iv
);
182 do_decrypt(netdissect_options
*ndo
, const char *caller
, struct sa_list
*sa
,
183 const u_char
*iv
, const u_char
*ct
, unsigned int ctlen
)
186 unsigned int block_size
;
191 ctx
= EVP_CIPHER_CTX_new();
194 * Failed to initialize the cipher context.
195 * From a look at the OpenSSL code, this appears to
196 * mean "couldn't allocate memory for the cipher context";
197 * note that we're not passing any parameters, so there's
198 * not much else it can mean.
200 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
201 "%s: can't allocate memory for cipher context", caller
);
205 if (set_cipher_parameters(ctx
, sa
->evp
, sa
->secret
, NULL
) < 0) {
206 EVP_CIPHER_CTX_free(ctx
);
207 (*ndo
->ndo_warning
)(ndo
, "%s: espkey init failed", caller
);
210 if (set_cipher_parameters(ctx
, NULL
, NULL
, iv
) < 0) {
211 EVP_CIPHER_CTX_free(ctx
);
212 (*ndo
->ndo_warning
)(ndo
, "%s: IV init failed", caller
);
217 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
218 * if the cipher has a block size of which the ciphertext's size must
219 * be a multiple, the payload must be padded to make that happen, so
220 * the ciphertext length must be a multiple of the block size. Fail
221 * if that's not the case.
223 block_size
= (unsigned int)EVP_CIPHER_CTX_block_size(ctx
);
224 if ((ctlen
% block_size
) != 0) {
225 EVP_CIPHER_CTX_free(ctx
);
226 (*ndo
->ndo_warning
)(ndo
,
227 "%s: ciphertext size %u is not a multiple of the cipher block size %u",
228 caller
, ctlen
, block_size
);
233 * Attempt to allocate a buffer for the decrypted data, because
234 * we can't decrypt on top of the input buffer.
237 pt
= (u_char
*)malloc(ptlen
);
239 EVP_CIPHER_CTX_free(ctx
);
240 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
241 "%s: can't allocate memory for decryption buffer", caller
);
246 * The size of the ciphertext handed to us is a multiple of the
247 * cipher block size, so we don't need to worry about padding.
249 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0)) {
251 EVP_CIPHER_CTX_free(ctx
);
252 (*ndo
->ndo_warning
)(ndo
,
253 "%s: EVP_CIPHER_CTX_set_padding failed", caller
);
256 if (!EVP_DecryptUpdate(ctx
, pt
, &len
, ct
, ctlen
)) {
258 EVP_CIPHER_CTX_free(ctx
);
259 (*ndo
->ndo_warning
)(ndo
, "%s: EVP_DecryptUpdate failed",
263 EVP_CIPHER_CTX_free(ctx
);
268 * This will allocate a new buffer containing the decrypted data.
269 * It returns 1 on success and 0 on failure.
271 * It will push the new buffer and the values of ndo->ndo_packetp and
272 * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
273 * and ndo->ndo_snapend to refer to the new buffer.
275 * Our caller must pop the buffer off the stack when it's finished
276 * dissecting anything in it and before it does any dissection of
277 * anything in the old buffer. That will free the new buffer.
279 USES_APPLE_DEPRECATED_API
280 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
282 const u_char spii
[8],
283 const u_char spir
[8],
284 const u_char
*buf
, const u_char
*end
)
292 /* initiator arg is any non-zero value */
293 if(initiator
) initiator
=1;
295 /* see if we can find the SA, and if so, decode it */
296 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
298 && initiator
== sa
->initiator
299 && memcmp(spii
, sa
->spii
, 8) == 0
300 && memcmp(spir
, sa
->spir
, 8) == 0)
304 if(sa
== NULL
) return 0;
305 if(sa
->evp
== NULL
) return 0;
308 * remove authenticator, and see if we still have something to
311 end
= end
- sa
->authlen
;
316 if(end
<= ct
) return 0;
318 pt
= do_decrypt(ndo
, "esp_print_decrypt_buffer_by_ikev2", sa
, iv
,
324 * Switch to the output buffer for dissection, and save it
325 * on the buffer stack so it can be freed; our caller must
328 if (!nd_push_buffer(ndo
, pt
, pt
, pt
+ ctlen
)) {
337 static void esp_print_addsa(netdissect_options
*ndo
,
338 struct sa_list
*sa
, int sa_def
)
344 /* malloc() return used in a 'struct sa_list': do not free() */
345 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
347 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
348 "esp_print_addsa: malloc");
353 ndo
->ndo_sa_default
= nsa
;
355 nsa
->next
= ndo
->ndo_sa_list_head
;
356 ndo
->ndo_sa_list_head
= nsa
;
360 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
362 if (hex
>= '0' && hex
<= '9')
364 else if (hex
>= 'A' && hex
<= 'F')
365 return (hex
- 'A' + 10);
366 else if (hex
>= 'a' && hex
<= 'f')
367 return (hex
- 'a' + 10);
369 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_ESP_SECRET
,
370 "invalid hex digit %c in espsecret\n", hex
);
374 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
378 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
383 * returns size of binary, 0 on failure.
386 int espprint_decode_hex(netdissect_options
*ndo
,
387 u_char
*binbuf
, unsigned int binbuf_len
,
393 len
= strlen(hex
) / 2;
395 if (len
> binbuf_len
) {
396 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %u\n", len
);
401 while (hex
[0] != '\0' && hex
[1]!='\0') {
402 binbuf
[i
] = hex2byte(ndo
, hex
);
411 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
414 USES_APPLE_DEPRECATED_API
416 espprint_decode_encalgo(netdissect_options
*ndo
,
417 char *decode
, struct sa_list
*sa
)
420 const EVP_CIPHER
*evp
;
424 colon
= strchr(decode
, ':');
426 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
431 if (strlen(decode
) > strlen("-hmac96") &&
432 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
434 p
= strstr(decode
, "-hmac96");
438 if (strlen(decode
) > strlen("-cbc") &&
439 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
440 p
= strstr(decode
, "-cbc");
443 evp
= EVP_get_cipherbyname(decode
);
446 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
454 sa
->authlen
= authlen
;
455 /* This returns an int, but it should never be negative */
456 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
459 if (colon
[0] == '0' && colon
[1] == 'x') {
460 /* decode some hex! */
463 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
464 if(sa
->secretlen
== 0) return 0;
468 if (i
< sizeof(sa
->secret
)) {
469 memcpy(sa
->secret
, colon
, i
);
472 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
473 sa
->secretlen
= sizeof(sa
->secret
);
482 * for the moment, ignore the auth algorithm, just hard code the authenticator
483 * length. Need to research how openssl looks up HMAC stuff.
486 espprint_decode_authalgo(netdissect_options
*ndo
,
487 char *decode
, struct sa_list
*sa
)
491 colon
= strchr(decode
, ':');
493 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
498 if(ascii_strcasecmp(decode
,"sha1") == 0 ||
499 ascii_strcasecmp(decode
,"md5") == 0) {
505 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
506 const char *file
, int lineno
)
508 /* it's an IKEv2 secret, store it instead */
512 char *icookie
, *rcookie
;
517 init
= strsep(&line
, " \t");
518 icookie
= strsep(&line
, " \t");
519 rcookie
= strsep(&line
, " \t");
520 authkey
= strsep(&line
, " \t");
521 enckey
= strsep(&line
, " \t");
523 /* if any fields are missing */
524 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
525 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
531 ilen
= strlen(icookie
);
532 rlen
= strlen(rcookie
);
534 if((init
[0]!='I' && init
[0]!='R')
535 || icookie
[0]!='0' || icookie
[1]!='x'
536 || rcookie
[0]!='0' || rcookie
[1]!='x'
539 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
542 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
543 init
, icookie
, ilen
, rcookie
, rlen
);
549 sa1
.initiator
= (init
[0] == 'I');
550 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
553 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
556 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
558 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
560 esp_print_addsa(ndo
, &sa1
, FALSE
);
565 * special form: file /name
566 * causes us to go read from this file instead.
569 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
570 const char *file
, int lineno
)
578 spikey
= strsep(&line
, " \t");
580 memset(&sa1
, 0, sizeof(struct sa_list
));
582 /* if there is only one token, then it is an algo:key token */
586 /* sa1.daddr.version = 0; */
587 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
593 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
594 /* open file and read it */
597 int subfile_lineno
=0;
599 char *filename
= line
;
601 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
602 if (secretfile
== NULL
) {
603 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
604 "print_esp: can't open %s: %s\n",
605 filename
, strerror(errno
));
608 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
610 /* remove newline from the line */
611 nl
= strchr(fileline
, '\n');
614 if (fileline
[0] == '#') continue;
615 if (fileline
[0] == '\0') continue;
617 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
624 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
625 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
634 spistr
= strsep(&spikey
, "@");
635 if (spistr
== NULL
) {
636 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
640 spino
= strtoul(spistr
, &foo
, 0);
641 if (spistr
== foo
|| !spikey
) {
642 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
648 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
649 sa1
.daddr_version
= 6;
650 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
651 sa1
.daddr_version
= 4;
653 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
659 /* skip any blank spaces */
660 while (*decode
== ' ' || *decode
== '\t' || *decode
== '\r' || *decode
== '\n')
663 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
668 esp_print_addsa(ndo
, &sa1
, sa_def
);
671 USES_APPLE_DEPRECATED_API
672 static void esp_init(netdissect_options
*ndo _U_
)
675 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
676 * we check whether it's undefined or it's less than the
679 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
680 OpenSSL_add_all_algorithms();
682 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
686 void esp_print_decodesecret(netdissect_options
*ndo
)
690 static int initialized
= 0;
697 p
= ndo
->ndo_espsecret
;
699 while (p
&& p
[0] != '\0') {
700 /* pick out the first line or first thing until a comma */
701 if ((line
= strsep(&p
, "\n,")) == NULL
) {
706 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
709 ndo
->ndo_espsecret
= NULL
;
714 #ifdef HAVE_LIBCRYPTO
715 #define USED_IF_LIBCRYPTO
717 #define USED_IF_LIBCRYPTO _U_
720 #ifdef HAVE_LIBCRYPTO
721 USES_APPLE_DEPRECATED_API
724 esp_print(netdissect_options
*ndo
,
725 const u_char
*bp
, u_int length
,
726 const u_char
*bp2 USED_IF_LIBCRYPTO
,
727 u_int ver USED_IF_LIBCRYPTO
,
728 int fragmented USED_IF_LIBCRYPTO
,
729 u_int ttl_hl USED_IF_LIBCRYPTO
)
731 const struct newesp
*esp
;
733 #ifdef HAVE_LIBCRYPTO
735 struct sa_list
*sa
= NULL
;
736 const struct ip6_hdr
*ip6
= NULL
;
746 ndo
->ndo_protocol
= "esp";
747 esp
= (const struct newesp
*)bp
;
749 /* 'ep' points to the end of available data. */
750 ep
= ndo
->ndo_snapend
;
752 if ((const u_char
*)(esp
+ 1) >= ep
) {
756 ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
757 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
758 ND_PRINT(", length %u", length
);
760 #ifdef HAVE_LIBCRYPTO
761 /* initiailize SAs */
762 if (ndo
->ndo_sa_list_head
== NULL
) {
763 if (!ndo
->ndo_espsecret
)
766 esp_print_decodesecret(ndo
);
769 if (ndo
->ndo_sa_list_head
== NULL
)
772 ip
= (const struct ip
*)bp2
;
775 ip6
= (const struct ip6_hdr
*)bp2
;
776 /* we do not attempt to decrypt jumbograms */
777 if (!GET_BE_U_2(ip6
->ip6_plen
))
779 /* XXX - check whether it's fragmented? */
780 /* if we can't get nexthdr, we do not need to decrypt it */
782 /* see if we can find the SA, and if so, decode it */
783 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
784 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
785 sa
->daddr_version
== 6 &&
786 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
787 sizeof(nd_ipv6
)) == 0) {
793 /* nexthdr & padding are in the last fragment */
797 /* see if we can find the SA, and if so, decode it */
798 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
799 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
800 sa
->daddr_version
== 4 &&
801 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
802 sizeof(nd_ipv4
)) == 0) {
811 /* if we didn't find the specific one, then look for
812 * an unspecified one.
815 sa
= ndo
->ndo_sa_default
;
817 /* if not found fail */
821 /* pointer to the IV, if there is one */
822 iv
= (const u_char
*)(esp
+ 1) + 0;
823 /* length of the IV, if there is one; 0, if there isn't */
827 * Get a pointer to the ciphertext.
829 * p points to the beginning of the payload, i.e. to the
830 * initialization vector, so if we skip past the initialization
831 * vector, it points to the beginning of the ciphertext.
836 * Make sure the authentication data/integrity check value length
837 * isn't bigger than the total amount of data available after
838 * the ESP header and initialization vector is removed and,
839 * if not, slice the authentication data/ICV off.
841 if (ep
- ct
< sa
->authlen
) {
845 ep
= ep
- sa
->authlen
;
848 * Calculate the length of the ciphertext. ep points to
849 * the beginning of the authentication data/integrity check
850 * value, i.e. right past the end of the ciphertext;
852 payloadlen
= ep
- ct
;
858 * If the next header value is past the end of the available
859 * data, we won't be able to fetch it once we've decrypted
860 * the ciphertext, so there's no point in decrypting the data.
862 * Report it as truncation.
864 if (!ND_TTEST_1(ep
- 1)) {
869 pt
= do_decrypt(ndo
, "esp_print", sa
, iv
, ct
, payloadlen
);
874 * Switch to the output buffer for dissection, and
875 * save it on the buffer stack so it can be freed.
877 ep
= pt
+ payloadlen
;
878 if (!nd_push_buffer(ndo
, pt
, pt
, ep
)) {
880 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
881 "esp_print: can't push buffer on buffer stack");
885 * Sanity check for pad length; if it, plus 2 for the pad
886 * length and next header fields, is bigger than the ciphertext
887 * length (which is also the plaintext length), it's too big.
889 * XXX - the check can fail if the packet is corrupt *or* if
890 * it was not decrypted with the correct key, so that the
891 * "plaintext" is not what was being sent.
893 padlen
= GET_U_1(ep
- 2);
894 if (padlen
+ 2 > payloadlen
) {
899 /* Get the next header */
900 nh
= GET_U_1(ep
- 1);
904 /* Now dissect the plaintext. */
905 ip_print_demux(ndo
, pt
, payloadlen
- (padlen
+ 2), ver
, fragmented
,
908 /* Pop the buffer, freeing it. */
909 nd_pop_packet_info(ndo
);
912 #ifdef HAVE_LIBCRYPTO