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
;
428 const char *real_decode
;
430 colon
= strchr(decode
, ':');
432 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
437 if (strlen(decode
) > strlen("-hmac96") &&
438 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
440 p
= strstr(decode
, "-hmac96");
444 if (strlen(decode
) > strlen("-cbc") &&
445 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
446 p
= strstr(decode
, "-cbc");
450 * Not all versions of libcrypto support calls to add aliases
451 * to ciphers - newer versions of libressl don't - so, instead
452 * of making "3des" an alias for "des_ede3_cbc", if attempting
453 * to get the cipher fails and the name is "3des", we try
456 real_decode
= decode
;
457 if (strcmp(real_decode
, "3des") == 0)
458 real_decode
= "des-ede3-cbc";
459 evp
= EVP_get_cipherbyname(real_decode
);
462 if (decode
!= real_decode
)
463 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s (%s)\n", real_decode
, decode
);
465 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
473 sa
->authlen
= authlen
;
474 /* This returns an int, but it should never be negative */
475 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
478 if (colon
[0] == '0' && colon
[1] == 'x') {
479 /* decode some hex! */
482 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
483 if(sa
->secretlen
== 0) return 0;
487 if (i
< sizeof(sa
->secret
)) {
488 memcpy(sa
->secret
, colon
, i
);
491 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
492 sa
->secretlen
= sizeof(sa
->secret
);
501 * for the moment, ignore the auth algorithm, just hard code the authenticator
502 * length. Need to research how openssl looks up HMAC stuff.
505 espprint_decode_authalgo(netdissect_options
*ndo
,
506 char *decode
, struct sa_list
*sa
)
510 colon
= strchr(decode
, ':');
512 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
517 if(ascii_strcasecmp(decode
,"sha1") == 0 ||
518 ascii_strcasecmp(decode
,"md5") == 0) {
524 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
525 const char *file
, int lineno
)
527 /* it's an IKEv2 secret, store it instead */
531 char *icookie
, *rcookie
;
536 init
= strsep(&line
, " \t");
537 icookie
= strsep(&line
, " \t");
538 rcookie
= strsep(&line
, " \t");
539 authkey
= strsep(&line
, " \t");
540 enckey
= strsep(&line
, " \t");
542 /* if any fields are missing */
543 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
544 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
550 ilen
= strlen(icookie
);
551 rlen
= strlen(rcookie
);
553 if((init
[0]!='I' && init
[0]!='R')
554 || icookie
[0]!='0' || icookie
[1]!='x'
555 || rcookie
[0]!='0' || rcookie
[1]!='x'
558 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
561 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
562 init
, icookie
, ilen
, rcookie
, rlen
);
568 sa1
.initiator
= (init
[0] == 'I');
569 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
572 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
575 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
577 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
579 esp_print_addsa(ndo
, &sa1
, FALSE
);
584 * special form: file /name
585 * causes us to go read from this file instead.
588 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
589 const char *file
, int lineno
)
597 spikey
= strsep(&line
, " \t");
599 memset(&sa1
, 0, sizeof(struct sa_list
));
601 /* if there is only one token, then it is an algo:key token */
605 /* sa1.daddr.version = 0; */
606 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
612 if (spikey
&& ascii_strcasecmp(spikey
, "file") == 0) {
613 /* open file and read it */
616 int subfile_lineno
=0;
618 char *filename
= line
;
620 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
621 if (secretfile
== NULL
) {
622 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_OPEN_FILE
,
623 "%s: can't open %s: %s\n",
624 __func__
, filename
, strerror(errno
));
627 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
629 /* remove newline from the line */
630 nl
= strchr(fileline
, '\n');
633 if (fileline
[0] == '#') continue;
634 if (fileline
[0] == '\0') continue;
636 esp_print_decode_onesecret(ndo
, fileline
, filename
, subfile_lineno
);
643 if (spikey
&& ascii_strcasecmp(spikey
, "ikev2") == 0) {
644 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
653 spistr
= strsep(&spikey
, "@");
654 if (spistr
== NULL
) {
655 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find the @ token");
659 spino
= strtoul(spistr
, &foo
, 0);
660 if (spistr
== foo
|| !spikey
) {
661 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
667 if (strtoaddr6(spikey
, &sa1
.daddr
.in6
) == 1) {
668 sa1
.daddr_version
= 6;
669 } else if (strtoaddr(spikey
, &sa1
.daddr
.in4
) == 1) {
670 sa1
.daddr_version
= 4;
672 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
678 /* skip any blank spaces */
679 while (*decode
== ' ' || *decode
== '\t' || *decode
== '\r' || *decode
== '\n')
682 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
687 esp_print_addsa(ndo
, &sa1
, sa_def
);
691 static void esp_init(netdissect_options
*ndo _U_
)
694 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
695 * we check whether it's undefined or it's less than the
698 #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
699 OpenSSL_add_all_algorithms();
704 void esp_decodesecret_print(netdissect_options
*ndo
)
708 static int initialized
= 0;
715 p
= ndo
->ndo_espsecret
;
717 while (p
&& p
[0] != '\0') {
718 /* pick out the first line or first thing until a comma */
719 if ((line
= strsep(&p
, "\n,")) == NULL
) {
724 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
727 ndo
->ndo_espsecret
= NULL
;
732 #ifdef HAVE_LIBCRYPTO
733 #define USED_IF_LIBCRYPTO
735 #define USED_IF_LIBCRYPTO _U_
738 #ifdef HAVE_LIBCRYPTO
742 esp_print(netdissect_options
*ndo
,
743 const u_char
*bp
, u_int length
,
744 const u_char
*bp2 USED_IF_LIBCRYPTO
,
745 u_int ver USED_IF_LIBCRYPTO
,
746 int fragmented USED_IF_LIBCRYPTO
,
747 u_int ttl_hl USED_IF_LIBCRYPTO
)
749 const struct newesp
*esp
;
750 #ifdef HAVE_LIBCRYPTO
753 struct sa_list
*sa
= NULL
;
754 const struct ip6_hdr
*ip6
= NULL
;
764 ndo
->ndo_protocol
= "esp";
765 esp
= (const struct newesp
*)bp
;
767 nd_print_protocol_caps(ndo
);
769 ND_PRINT("(spi=0x%08x", GET_BE_U_4(esp
->esp_spi
));
770 ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp
->esp_seq
));
771 ND_PRINT(", length %u", length
);
773 #ifdef HAVE_LIBCRYPTO
774 /* 'ep' points to the end of available data. */
775 ep
= ndo
->ndo_snapend
;
778 if (ndo
->ndo_sa_list_head
== NULL
) {
779 if (!ndo
->ndo_espsecret
)
782 esp_decodesecret_print(ndo
);
785 if (ndo
->ndo_sa_list_head
== NULL
)
788 ip
= (const struct ip
*)bp2
;
791 ip6
= (const struct ip6_hdr
*)bp2
;
792 /* we do not attempt to decrypt jumbograms */
793 if (!GET_BE_U_2(ip6
->ip6_plen
))
795 /* XXX - check whether it's fragmented? */
796 /* if we can't get nexthdr, we do not need to decrypt it */
798 /* see if we can find the SA, and if so, decode it */
799 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
800 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
801 sa
->daddr_version
== 6 &&
802 UNALIGNED_MEMCMP(&sa
->daddr
.in6
, &ip6
->ip6_dst
,
803 sizeof(nd_ipv6
)) == 0) {
809 /* nexthdr & padding are in the last fragment */
813 /* see if we can find the SA, and if so, decode it */
814 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
815 if (sa
->spi
== GET_BE_U_4(esp
->esp_spi
) &&
816 sa
->daddr_version
== 4 &&
817 UNALIGNED_MEMCMP(&sa
->daddr
.in4
, &ip
->ip_dst
,
818 sizeof(nd_ipv4
)) == 0) {
827 /* if we didn't find the specific one, then look for
828 * an unspecified one.
831 sa
= ndo
->ndo_sa_default
;
833 /* if not found fail */
837 /* pointer to the IV, if there is one */
838 iv
= (const u_char
*)(esp
+ 1) + 0;
839 /* length of the IV, if there is one; 0, if there isn't */
843 * Get a pointer to the ciphertext.
845 * p points to the beginning of the payload, i.e. to the
846 * initialization vector, so if we skip past the initialization
847 * vector, it points to the beginning of the ciphertext.
852 * Make sure the authentication data/integrity check value length
853 * isn't bigger than the total amount of data available after
854 * the ESP header and initialization vector is removed and,
855 * if not, slice the authentication data/ICV off.
857 if (ep
- ct
< sa
->authlen
) {
861 ep
= ep
- sa
->authlen
;
864 * Calculate the length of the ciphertext. ep points to
865 * the beginning of the authentication data/integrity check
866 * value, i.e. right past the end of the ciphertext;
868 payloadlen
= ep
- ct
;
874 * If the next header value is past the end of the available
875 * data, we won't be able to fetch it once we've decrypted
876 * the ciphertext, so there's no point in decrypting the data.
878 * Report it as truncation.
880 if (!ND_TTEST_1(ep
- 1)) {
885 pt
= do_decrypt(ndo
, __func__
, sa
, iv
, ct
, payloadlen
);
890 * Switch to the output buffer for dissection, and
891 * save it on the buffer stack so it can be freed.
893 if (!nd_push_buffer(ndo
, pt
, pt
, payloadlen
)) {
895 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
896 "%s: can't push buffer on buffer stack", __func__
);
900 * Sanity check for pad length; if it, plus 2 for the pad
901 * length and next header fields, is bigger than the ciphertext
902 * length (which is also the plaintext length), it's too big.
904 * XXX - the check can fail if the packet is corrupt *or* if
905 * it was not decrypted with the correct key, so that the
906 * "plaintext" is not what was being sent.
908 padlen
= GET_U_1(pt
+ payloadlen
- 2);
909 if (padlen
+ 2 > payloadlen
) {
914 /* Get the next header */
915 nh
= GET_U_1(pt
+ payloadlen
- 1);
920 * Don't put padding + padding length(1 byte) + next header(1 byte)
921 * in the buffer because they are not part of the plaintext to decode.
923 if (!nd_push_snaplen(ndo
, pt
, payloadlen
- (padlen
+ 2))) {
924 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
925 "%s: can't push snaplen on buffer stack", __func__
);
928 /* Now dissect the plaintext. */
929 ip_demux_print(ndo
, pt
, payloadlen
- (padlen
+ 2), ver
, fragmented
,
932 /* Pop the buffer, freeing it. */
933 nd_pop_packet_info(ndo
);
934 /* Pop the nd_push_snaplen */
935 nd_pop_packet_info(ndo
);
938 #ifdef HAVE_LIBCRYPTO