111b3aaa1Schristos /* NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp */ 20f74e101Schristos 30f74e101Schristos /* 40f74e101Schristos * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994 50f74e101Schristos * The Regents of the University of California. All rights reserved. 60f74e101Schristos * 70f74e101Schristos * Redistribution and use in source and binary forms, with or without 80f74e101Schristos * modification, are permitted provided that: (1) source code distributions 90f74e101Schristos * retain the above copyright notice and this paragraph in its entirety, (2) 100f74e101Schristos * distributions including binary code include the above copyright notice and 110f74e101Schristos * this paragraph in its entirety in the documentation or other materials 120f74e101Schristos * provided with the distribution, and (3) all advertising materials mentioning 130f74e101Schristos * features or use of this software display the following acknowledgement: 140f74e101Schristos * ``This product includes software developed by the University of California, 150f74e101Schristos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 160f74e101Schristos * the University nor the names of its contributors may be used to endorse 170f74e101Schristos * or promote products derived from this software without specific prior 180f74e101Schristos * written permission. 190f74e101Schristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 200f74e101Schristos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 210f74e101Schristos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 220f74e101Schristos */ 230f74e101Schristos 2411b3aaa1Schristos #include <sys/cdefs.h> 250f74e101Schristos #ifndef lint 26*26ba0b50Schristos __RCSID("$NetBSD: print-esp.c,v 1.13 2024/09/02 16:15:31 christos Exp $"); 270f74e101Schristos #endif 280f74e101Schristos 29dc860a36Sspz /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */ 30dc860a36Sspz 31c74ad251Schristos #include <config.h> 320f74e101Schristos 33c74ad251Schristos #include "netdissect-stdinc.h" 340f74e101Schristos 35b3a00663Schristos #include <string.h> 360f74e101Schristos #include <stdlib.h> 370f74e101Schristos 380f74e101Schristos #ifdef HAVE_LIBCRYPTO 390f74e101Schristos #include <openssl/evp.h> 400f74e101Schristos #endif 410f74e101Schristos 42fdccd7e4Schristos #include "netdissect.h" 430f74e101Schristos #include "extract.h" 440f74e101Schristos 45c74ad251Schristos #include "diag-control.h" 46c74ad251Schristos 47c74ad251Schristos #ifdef HAVE_LIBCRYPTO 48c74ad251Schristos #include "strtoaddr.h" 49fdccd7e4Schristos #include "ascii_strcasecmp.h" 50c74ad251Schristos #endif 51fdccd7e4Schristos 52fdccd7e4Schristos #include "ip.h" 53fdccd7e4Schristos #include "ip6.h" 54fdccd7e4Schristos 55b3a00663Schristos /* 56b3a00663Schristos * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 57b3a00663Schristos * All rights reserved. 58b3a00663Schristos * 59b3a00663Schristos * Redistribution and use in source and binary forms, with or without 60b3a00663Schristos * modification, are permitted provided that the following conditions 61b3a00663Schristos * are met: 62b3a00663Schristos * 1. Redistributions of source code must retain the above copyright 63b3a00663Schristos * notice, this list of conditions and the following disclaimer. 64b3a00663Schristos * 2. Redistributions in binary form must reproduce the above copyright 65b3a00663Schristos * notice, this list of conditions and the following disclaimer in the 66b3a00663Schristos * documentation and/or other materials provided with the distribution. 67b3a00663Schristos * 3. Neither the name of the project nor the names of its contributors 68b3a00663Schristos * may be used to endorse or promote products derived from this software 69b3a00663Schristos * without specific prior written permission. 70b3a00663Schristos * 71b3a00663Schristos * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 72b3a00663Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 73b3a00663Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 74b3a00663Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 75b3a00663Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 76b3a00663Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 77b3a00663Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 78b3a00663Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 79b3a00663Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 80b3a00663Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 81b3a00663Schristos * SUCH DAMAGE. 82b3a00663Schristos */ 83b3a00663Schristos 84b3a00663Schristos /* 85b3a00663Schristos * RFC1827/2406 Encapsulated Security Payload. 86b3a00663Schristos */ 87b3a00663Schristos 88b3a00663Schristos struct newesp { 89c74ad251Schristos nd_uint32_t esp_spi; /* ESP */ 90c74ad251Schristos nd_uint32_t esp_seq; /* Sequence number */ 91b3a00663Schristos /*variable size*/ /* (IV and) Payload data */ 92b3a00663Schristos /*variable size*/ /* padding */ 93b3a00663Schristos /*8bit*/ /* pad size */ 94b3a00663Schristos /*8bit*/ /* next header */ 95b3a00663Schristos /*8bit*/ /* next header */ 96b3a00663Schristos /*variable size, 32bit bound*/ /* Authentication data */ 970f74e101Schristos }; 980f74e101Schristos 990f74e101Schristos #ifdef HAVE_LIBCRYPTO 100b3a00663Schristos union inaddr_u { 101c74ad251Schristos nd_ipv4 in4; 102c74ad251Schristos nd_ipv6 in6; 103b3a00663Schristos }; 1040f74e101Schristos struct sa_list { 1050f74e101Schristos struct sa_list *next; 106b3a00663Schristos u_int daddr_version; 107b3a00663Schristos union inaddr_u daddr; 108b3a00663Schristos uint32_t spi; /* if == 0, then IKEv2 */ 1090f74e101Schristos int initiator; 1100f74e101Schristos u_char spii[8]; /* for IKEv2 */ 1110f74e101Schristos u_char spir[8]; 1120f74e101Schristos const EVP_CIPHER *evp; 113c74ad251Schristos u_int ivlen; 1140f74e101Schristos int authlen; 1150f74e101Schristos u_char authsecret[256]; 1160f74e101Schristos int authsecret_len; 1170f74e101Schristos u_char secret[256]; /* is that big enough for all secrets? */ 1180f74e101Schristos int secretlen; 1190f74e101Schristos }; 1200f74e101Schristos 121fdccd7e4Schristos #ifndef HAVE_EVP_CIPHER_CTX_NEW 122fdccd7e4Schristos /* 123fdccd7e4Schristos * Allocate an EVP_CIPHER_CTX. 124fdccd7e4Schristos * Used if we have an older version of OpenSSL that doesn't provide 125fdccd7e4Schristos * routines to allocate and free them. 126fdccd7e4Schristos */ 127fdccd7e4Schristos static EVP_CIPHER_CTX * 128fdccd7e4Schristos EVP_CIPHER_CTX_new(void) 129fdccd7e4Schristos { 130fdccd7e4Schristos EVP_CIPHER_CTX *ctx; 131fdccd7e4Schristos 132fdccd7e4Schristos ctx = malloc(sizeof(*ctx)); 133fdccd7e4Schristos if (ctx == NULL) 134fdccd7e4Schristos return (NULL); 135fdccd7e4Schristos memset(ctx, 0, sizeof(*ctx)); 136fdccd7e4Schristos return (ctx); 137fdccd7e4Schristos } 138fdccd7e4Schristos 139fdccd7e4Schristos static void 140fdccd7e4Schristos EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) 141fdccd7e4Schristos { 142fdccd7e4Schristos EVP_CIPHER_CTX_cleanup(ctx); 143fdccd7e4Schristos free(ctx); 144fdccd7e4Schristos } 145fdccd7e4Schristos #endif 146fdccd7e4Schristos 147c74ad251Schristos #ifdef HAVE_EVP_DECRYPTINIT_EX 14872c96ff3Schristos /* 149c74ad251Schristos * Initialize the cipher by calling EVP_DecryptInit_ex(), because 150c74ad251Schristos * calling EVP_DecryptInit() will reset the cipher context, clearing 15172c96ff3Schristos * the cipher, so calling it twice, with the second call having a 152c74ad251Schristos * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(), 15372c96ff3Schristos * however, won't reset the cipher context, so you can use it to specify 154c74ad251Schristos * the IV in a second call after a first call to EVP_DecryptInit_ex() 15572c96ff3Schristos * to set the cipher and the key. 15672c96ff3Schristos * 15772c96ff3Schristos * XXX - is there some reason why we need to make two calls? 15872c96ff3Schristos */ 15972c96ff3Schristos static int 16072c96ff3Schristos set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 16172c96ff3Schristos const unsigned char *key, 162c74ad251Schristos const unsigned char *iv) 16372c96ff3Schristos { 164c74ad251Schristos return EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv); 16572c96ff3Schristos } 16672c96ff3Schristos #else 16772c96ff3Schristos /* 168c74ad251Schristos * Initialize the cipher by calling EVP_DecryptInit(), because we don't 169c74ad251Schristos * have EVP_DecryptInit_ex(); we rely on it not trashing the context. 17072c96ff3Schristos */ 17172c96ff3Schristos static int 17272c96ff3Schristos set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 17372c96ff3Schristos const unsigned char *key, 174c74ad251Schristos const unsigned char *iv) 17572c96ff3Schristos { 176c74ad251Schristos return EVP_DecryptInit(ctx, cipher, key, iv); 17772c96ff3Schristos } 17872c96ff3Schristos #endif 17972c96ff3Schristos 180c74ad251Schristos static u_char * 181c74ad251Schristos do_decrypt(netdissect_options *ndo, const char *caller, struct sa_list *sa, 182c74ad251Schristos const u_char *iv, const u_char *ct, unsigned int ctlen) 183c74ad251Schristos { 184c74ad251Schristos EVP_CIPHER_CTX *ctx; 185c74ad251Schristos unsigned int block_size; 186c74ad251Schristos unsigned int ptlen; 187c74ad251Schristos u_char *pt; 188c74ad251Schristos int len; 189c74ad251Schristos 190c74ad251Schristos ctx = EVP_CIPHER_CTX_new(); 191c74ad251Schristos if (ctx == NULL) { 1920f74e101Schristos /* 193c74ad251Schristos * Failed to initialize the cipher context. 194c74ad251Schristos * From a look at the OpenSSL code, this appears to 195c74ad251Schristos * mean "couldn't allocate memory for the cipher context"; 196c74ad251Schristos * note that we're not passing any parameters, so there's 197c74ad251Schristos * not much else it can mean. 1980f74e101Schristos */ 199c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 200c74ad251Schristos "%s: can't allocate memory for cipher context", caller); 201c74ad251Schristos return NULL; 202c74ad251Schristos } 203c74ad251Schristos 204c74ad251Schristos if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL) < 0) { 205c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 206c74ad251Schristos (*ndo->ndo_warning)(ndo, "%s: espkey init failed", caller); 207c74ad251Schristos return NULL; 208c74ad251Schristos } 209c74ad251Schristos if (set_cipher_parameters(ctx, NULL, NULL, iv) < 0) { 210c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 211c74ad251Schristos (*ndo->ndo_warning)(ndo, "%s: IV init failed", caller); 212c74ad251Schristos return NULL; 213c74ad251Schristos } 214c74ad251Schristos 215c74ad251Schristos /* 216c74ad251Schristos * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4, 217c74ad251Schristos * if the cipher has a block size of which the ciphertext's size must 218c74ad251Schristos * be a multiple, the payload must be padded to make that happen, so 219c74ad251Schristos * the ciphertext length must be a multiple of the block size. Fail 220c74ad251Schristos * if that's not the case. 221c74ad251Schristos */ 222c74ad251Schristos block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx); 223c74ad251Schristos if ((ctlen % block_size) != 0) { 224c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 225c74ad251Schristos (*ndo->ndo_warning)(ndo, 226c74ad251Schristos "%s: ciphertext size %u is not a multiple of the cipher block size %u", 227c74ad251Schristos caller, ctlen, block_size); 228c74ad251Schristos return NULL; 229c74ad251Schristos } 230c74ad251Schristos 231c74ad251Schristos /* 232c74ad251Schristos * Attempt to allocate a buffer for the decrypted data, because 233c74ad251Schristos * we can't decrypt on top of the input buffer. 234c74ad251Schristos */ 235c74ad251Schristos ptlen = ctlen; 236c74ad251Schristos pt = (u_char *)calloc(1, ptlen); 237c74ad251Schristos if (pt == NULL) { 238c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 239c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 240c74ad251Schristos "%s: can't allocate memory for decryption buffer", caller); 241c74ad251Schristos return NULL; 242c74ad251Schristos } 243c74ad251Schristos 244c74ad251Schristos /* 245c74ad251Schristos * The size of the ciphertext handed to us is a multiple of the 246c74ad251Schristos * cipher block size, so we don't need to worry about padding. 247c74ad251Schristos */ 248c74ad251Schristos if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) { 249c74ad251Schristos free(pt); 250c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 251c74ad251Schristos (*ndo->ndo_warning)(ndo, 252c74ad251Schristos "%s: EVP_CIPHER_CTX_set_padding failed", caller); 253c74ad251Schristos return NULL; 254c74ad251Schristos } 255c74ad251Schristos if (!EVP_DecryptUpdate(ctx, pt, &len, ct, ctlen)) { 256c74ad251Schristos free(pt); 257c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 258c74ad251Schristos (*ndo->ndo_warning)(ndo, "%s: EVP_DecryptUpdate failed", 259c74ad251Schristos caller); 260c74ad251Schristos return NULL; 261c74ad251Schristos } 262c74ad251Schristos EVP_CIPHER_CTX_free(ctx); 263c74ad251Schristos return pt; 264c74ad251Schristos } 265c74ad251Schristos 266c74ad251Schristos /* 267c74ad251Schristos * This will allocate a new buffer containing the decrypted data. 268c74ad251Schristos * It returns 1 on success and 0 on failure. 269c74ad251Schristos * 270c74ad251Schristos * It will push the new buffer and the values of ndo->ndo_packetp and 271c74ad251Schristos * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp 272c74ad251Schristos * and ndo->ndo_snapend to refer to the new buffer. 273c74ad251Schristos * 274c74ad251Schristos * Our caller must pop the buffer off the stack when it's finished 275c74ad251Schristos * dissecting anything in it and before it does any dissection of 276c74ad251Schristos * anything in the old buffer. That will free the new buffer. 277c74ad251Schristos */ 278c74ad251Schristos DIAG_OFF_DEPRECATION 279c74ad251Schristos int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo, 2800f74e101Schristos int initiator, 281c74ad251Schristos const u_char spii[8], 282c74ad251Schristos const u_char spir[8], 283fdccd7e4Schristos const u_char *buf, const u_char *end) 2840f74e101Schristos { 2850f74e101Schristos struct sa_list *sa; 286fdccd7e4Schristos const u_char *iv; 287c74ad251Schristos const u_char *ct; 288c74ad251Schristos unsigned int ctlen; 289c74ad251Schristos u_char *pt; 2900f74e101Schristos 2910f74e101Schristos /* initiator arg is any non-zero value */ 2920f74e101Schristos if(initiator) initiator=1; 2930f74e101Schristos 2940f74e101Schristos /* see if we can find the SA, and if so, decode it */ 2950f74e101Schristos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) { 2960f74e101Schristos if (sa->spi == 0 2970f74e101Schristos && initiator == sa->initiator 2980f74e101Schristos && memcmp(spii, sa->spii, 8) == 0 2990f74e101Schristos && memcmp(spir, sa->spir, 8) == 0) 3000f74e101Schristos break; 3010f74e101Schristos } 3020f74e101Schristos 3030f74e101Schristos if(sa == NULL) return 0; 3040f74e101Schristos if(sa->evp == NULL) return 0; 3050f74e101Schristos 3060f74e101Schristos /* 3070f74e101Schristos * remove authenticator, and see if we still have something to 3080f74e101Schristos * work with 3090f74e101Schristos */ 3100f74e101Schristos end = end - sa->authlen; 3110f74e101Schristos iv = buf; 312c74ad251Schristos ct = iv + sa->ivlen; 313c74ad251Schristos ctlen = end-ct; 3140f74e101Schristos 315c74ad251Schristos if(end <= ct) return 0; 3160f74e101Schristos 317c74ad251Schristos pt = do_decrypt(ndo, __func__, sa, iv, 318c74ad251Schristos ct, ctlen); 319c74ad251Schristos if (pt == NULL) 320fdccd7e4Schristos return 0; 321817e9a7eSchristos 322817e9a7eSchristos /* 323c74ad251Schristos * Switch to the output buffer for dissection, and save it 324c74ad251Schristos * on the buffer stack so it can be freed; our caller must 325c74ad251Schristos * pop it when done. 326817e9a7eSchristos */ 327c74ad251Schristos if (!nd_push_buffer(ndo, pt, pt, ctlen)) { 328c74ad251Schristos free(pt); 329c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 330c74ad251Schristos "%s: can't push buffer on buffer stack", __func__); 33172c96ff3Schristos } 3320f74e101Schristos 3330f74e101Schristos return 1; 3340f74e101Schristos } 335c74ad251Schristos DIAG_ON_DEPRECATION 3360f74e101Schristos 3370f74e101Schristos static void esp_print_addsa(netdissect_options *ndo, 338c74ad251Schristos const struct sa_list *sa, int sa_def) 3390f74e101Schristos { 3400f74e101Schristos /* copy the "sa" */ 3410f74e101Schristos 3420f74e101Schristos struct sa_list *nsa; 3430f74e101Schristos 344c74ad251Schristos /* malloc() return used in a 'struct sa_list': do not free() */ 3450f74e101Schristos nsa = (struct sa_list *)malloc(sizeof(struct sa_list)); 3460f74e101Schristos if (nsa == NULL) 347c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 348c74ad251Schristos "%s: malloc", __func__); 3490f74e101Schristos 3500f74e101Schristos *nsa = *sa; 3510f74e101Schristos 3520f74e101Schristos if (sa_def) 3530f74e101Schristos ndo->ndo_sa_default = nsa; 3540f74e101Schristos 3550f74e101Schristos nsa->next = ndo->ndo_sa_list_head; 3560f74e101Schristos ndo->ndo_sa_list_head = nsa; 3570f74e101Schristos } 3580f74e101Schristos 3590f74e101Schristos 3600f74e101Schristos static u_int hexdigit(netdissect_options *ndo, char hex) 3610f74e101Schristos { 3620f74e101Schristos if (hex >= '0' && hex <= '9') 3630f74e101Schristos return (hex - '0'); 3640f74e101Schristos else if (hex >= 'A' && hex <= 'F') 3650f74e101Schristos return (hex - 'A' + 10); 3660f74e101Schristos else if (hex >= 'a' && hex <= 'f') 3670f74e101Schristos return (hex - 'a' + 10); 3680f74e101Schristos else { 369c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_ESP_SECRET, 370c74ad251Schristos "invalid hex digit %c in espsecret\n", hex); 3710f74e101Schristos } 3720f74e101Schristos } 3730f74e101Schristos 3740f74e101Schristos static u_int hex2byte(netdissect_options *ndo, char *hexstring) 3750f74e101Schristos { 3760f74e101Schristos u_int byte; 3770f74e101Schristos 3780f74e101Schristos byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]); 3790f74e101Schristos return byte; 3800f74e101Schristos } 3810f74e101Schristos 3820f74e101Schristos /* 3830f74e101Schristos * returns size of binary, 0 on failure. 3840f74e101Schristos */ 385c74ad251Schristos static int 386c74ad251Schristos espprint_decode_hex(netdissect_options *ndo, 387c74ad251Schristos u_char *binbuf, unsigned int binbuf_len, char *hex) 3880f74e101Schristos { 3890f74e101Schristos unsigned int len; 3900f74e101Schristos int i; 3910f74e101Schristos 3920f74e101Schristos len = strlen(hex) / 2; 3930f74e101Schristos 3940f74e101Schristos if (len > binbuf_len) { 395c74ad251Schristos (*ndo->ndo_warning)(ndo, "secret is too big: %u\n", len); 3960f74e101Schristos return 0; 3970f74e101Schristos } 3980f74e101Schristos 3990f74e101Schristos i = 0; 4000f74e101Schristos while (hex[0] != '\0' && hex[1]!='\0') { 4010f74e101Schristos binbuf[i] = hex2byte(ndo, hex); 4020f74e101Schristos hex += 2; 4030f74e101Schristos i++; 4040f74e101Schristos } 4050f74e101Schristos 4060f74e101Schristos return i; 4070f74e101Schristos } 4080f74e101Schristos 4090f74e101Schristos /* 4100f74e101Schristos * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret 4110f74e101Schristos */ 4120f74e101Schristos 413c74ad251Schristos DIAG_OFF_DEPRECATION 4140f74e101Schristos static int 4150f74e101Schristos espprint_decode_encalgo(netdissect_options *ndo, 4160f74e101Schristos char *decode, struct sa_list *sa) 4170f74e101Schristos { 4180f74e101Schristos size_t i; 4190f74e101Schristos const EVP_CIPHER *evp; 4200f74e101Schristos int authlen = 0; 4210f74e101Schristos char *colon, *p; 422*26ba0b50Schristos const char *real_decode; 4230f74e101Schristos 4240f74e101Schristos colon = strchr(decode, ':'); 4250f74e101Schristos if (colon == NULL) { 4260f74e101Schristos (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode); 4270f74e101Schristos return 0; 4280f74e101Schristos } 4290f74e101Schristos *colon = '\0'; 4300f74e101Schristos 4310f74e101Schristos if (strlen(decode) > strlen("-hmac96") && 4320f74e101Schristos !strcmp(decode + strlen(decode) - strlen("-hmac96"), 4330f74e101Schristos "-hmac96")) { 4340f74e101Schristos p = strstr(decode, "-hmac96"); 4350f74e101Schristos *p = '\0'; 4360f74e101Schristos authlen = 12; 4370f74e101Schristos } 4380f74e101Schristos if (strlen(decode) > strlen("-cbc") && 4390f74e101Schristos !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) { 4400f74e101Schristos p = strstr(decode, "-cbc"); 4410f74e101Schristos *p = '\0'; 4420f74e101Schristos } 443*26ba0b50Schristos /* 444*26ba0b50Schristos * Not all versions of libcrypto support calls to add aliases 445*26ba0b50Schristos * to ciphers - newer versions of libressl don't - so, instead 446*26ba0b50Schristos * of making "3des" an alias for "des_ede3_cbc", if attempting 447*26ba0b50Schristos * to get the cipher fails and the name is "3des", we try 448*26ba0b50Schristos * "des_ede3_cbc". 449*26ba0b50Schristos */ 450*26ba0b50Schristos real_decode = decode; 451*26ba0b50Schristos if (strcmp(real_decode, "3des") == 0) 452*26ba0b50Schristos real_decode = "des-ede3-cbc"; 453*26ba0b50Schristos evp = EVP_get_cipherbyname(real_decode); 4540f74e101Schristos 4550f74e101Schristos if (!evp) { 456*26ba0b50Schristos if (decode != real_decode) 457*26ba0b50Schristos (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s (%s)\n", real_decode, decode); 458*26ba0b50Schristos else 4590f74e101Schristos (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode); 4600f74e101Schristos sa->evp = NULL; 4610f74e101Schristos sa->authlen = 0; 4620f74e101Schristos sa->ivlen = 0; 4630f74e101Schristos return 0; 4640f74e101Schristos } 4650f74e101Schristos 4660f74e101Schristos sa->evp = evp; 4670f74e101Schristos sa->authlen = authlen; 468c74ad251Schristos /* This returns an int, but it should never be negative */ 4690f74e101Schristos sa->ivlen = EVP_CIPHER_iv_length(evp); 4700f74e101Schristos 4710f74e101Schristos colon++; 4720f74e101Schristos if (colon[0] == '0' && colon[1] == 'x') { 4730f74e101Schristos /* decode some hex! */ 4740f74e101Schristos 4750f74e101Schristos colon += 2; 4760f74e101Schristos sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon); 4770f74e101Schristos if(sa->secretlen == 0) return 0; 4780f74e101Schristos } else { 4790f74e101Schristos i = strlen(colon); 4800f74e101Schristos 4810f74e101Schristos if (i < sizeof(sa->secret)) { 4820f74e101Schristos memcpy(sa->secret, colon, i); 4830f74e101Schristos sa->secretlen = i; 4840f74e101Schristos } else { 4850f74e101Schristos memcpy(sa->secret, colon, sizeof(sa->secret)); 4860f74e101Schristos sa->secretlen = sizeof(sa->secret); 4870f74e101Schristos } 4880f74e101Schristos } 4890f74e101Schristos 4900f74e101Schristos return 1; 4910f74e101Schristos } 492c74ad251Schristos DIAG_ON_DEPRECATION 4930f74e101Schristos 4940f74e101Schristos /* 495817e9a7eSchristos * for the moment, ignore the auth algorithm, just hard code the authenticator 4960f74e101Schristos * length. Need to research how openssl looks up HMAC stuff. 4970f74e101Schristos */ 4980f74e101Schristos static int 4990f74e101Schristos espprint_decode_authalgo(netdissect_options *ndo, 5000f74e101Schristos char *decode, struct sa_list *sa) 5010f74e101Schristos { 5020f74e101Schristos char *colon; 5030f74e101Schristos 5040f74e101Schristos colon = strchr(decode, ':'); 5050f74e101Schristos if (colon == NULL) { 5060f74e101Schristos (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode); 5070f74e101Schristos return 0; 5080f74e101Schristos } 5090f74e101Schristos *colon = '\0'; 5100f74e101Schristos 511c74ad251Schristos if(ascii_strcasecmp(decode,"sha1") == 0 || 512c74ad251Schristos ascii_strcasecmp(decode,"md5") == 0) { 5130f74e101Schristos sa->authlen = 12; 5140f74e101Schristos } 5150f74e101Schristos return 1; 5160f74e101Schristos } 5170f74e101Schristos 5180f74e101Schristos static void esp_print_decode_ikeline(netdissect_options *ndo, char *line, 5190f74e101Schristos const char *file, int lineno) 5200f74e101Schristos { 5210f74e101Schristos /* it's an IKEv2 secret, store it instead */ 5220f74e101Schristos struct sa_list sa1; 5230f74e101Schristos 5240f74e101Schristos char *init; 5250f74e101Schristos char *icookie, *rcookie; 5260f74e101Schristos int ilen, rlen; 5270f74e101Schristos char *authkey; 5280f74e101Schristos char *enckey; 5290f74e101Schristos 5300f74e101Schristos init = strsep(&line, " \t"); 5310f74e101Schristos icookie = strsep(&line, " \t"); 5320f74e101Schristos rcookie = strsep(&line, " \t"); 5330f74e101Schristos authkey = strsep(&line, " \t"); 5340f74e101Schristos enckey = strsep(&line, " \t"); 5350f74e101Schristos 5360f74e101Schristos /* if any fields are missing */ 5370f74e101Schristos if(!init || !icookie || !rcookie || !authkey || !enckey) { 5380f74e101Schristos (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u", 5390f74e101Schristos file, lineno); 5400f74e101Schristos 5410f74e101Schristos return; 5420f74e101Schristos } 5430f74e101Schristos 5440f74e101Schristos ilen = strlen(icookie); 5450f74e101Schristos rlen = strlen(rcookie); 5460f74e101Schristos 5470f74e101Schristos if((init[0]!='I' && init[0]!='R') 5480f74e101Schristos || icookie[0]!='0' || icookie[1]!='x' 5490f74e101Schristos || rcookie[0]!='0' || rcookie[1]!='x' 5500f74e101Schristos || ilen!=18 5510f74e101Schristos || rlen!=18) { 5520f74e101Schristos (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.", 5530f74e101Schristos file, lineno); 5540f74e101Schristos 5550f74e101Schristos (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)", 5560f74e101Schristos init, icookie, ilen, rcookie, rlen); 5570f74e101Schristos 5580f74e101Schristos return; 5590f74e101Schristos } 5600f74e101Schristos 5610f74e101Schristos sa1.spi = 0; 5620f74e101Schristos sa1.initiator = (init[0] == 'I'); 5630f74e101Schristos if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8) 5640f74e101Schristos return; 5650f74e101Schristos 5660f74e101Schristos if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8) 5670f74e101Schristos return; 5680f74e101Schristos 5690f74e101Schristos if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return; 5700f74e101Schristos 5710f74e101Schristos if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return; 5720f74e101Schristos 5730f74e101Schristos esp_print_addsa(ndo, &sa1, FALSE); 5740f74e101Schristos } 5750f74e101Schristos 5760f74e101Schristos /* 5770f74e101Schristos * 5780f74e101Schristos * special form: file /name 5790f74e101Schristos * causes us to go read from this file instead. 5800f74e101Schristos * 5810f74e101Schristos */ 5820f74e101Schristos static void esp_print_decode_onesecret(netdissect_options *ndo, char *line, 5830f74e101Schristos const char *file, int lineno) 5840f74e101Schristos { 5850f74e101Schristos struct sa_list sa1; 5860f74e101Schristos int sa_def; 5870f74e101Schristos 5880f74e101Schristos char *spikey; 5890f74e101Schristos char *decode; 5900f74e101Schristos 5910f74e101Schristos spikey = strsep(&line, " \t"); 5920f74e101Schristos sa_def = 0; 5930f74e101Schristos memset(&sa1, 0, sizeof(struct sa_list)); 5940f74e101Schristos 5950f74e101Schristos /* if there is only one token, then it is an algo:key token */ 5960f74e101Schristos if (line == NULL) { 5970f74e101Schristos decode = spikey; 5980f74e101Schristos spikey = NULL; 599b3a00663Schristos /* sa1.daddr.version = 0; */ 6000f74e101Schristos /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */ 6010f74e101Schristos /* sa1.spi = 0; */ 6020f74e101Schristos sa_def = 1; 6030f74e101Schristos } else 6040f74e101Schristos decode = line; 6050f74e101Schristos 606fdccd7e4Schristos if (spikey && ascii_strcasecmp(spikey, "file") == 0) { 6070f74e101Schristos /* open file and read it */ 6080f74e101Schristos FILE *secretfile; 6090f74e101Schristos char fileline[1024]; 610fdccd7e4Schristos int subfile_lineno=0; 6110f74e101Schristos char *nl; 6120f74e101Schristos char *filename = line; 6130f74e101Schristos 6140f74e101Schristos secretfile = fopen(filename, FOPEN_READ_TXT); 6150f74e101Schristos if (secretfile == NULL) { 616c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_OPEN_FILE, 617c74ad251Schristos "%s: can't open %s: %s\n", 618c74ad251Schristos __func__, filename, strerror(errno)); 6190f74e101Schristos } 6200f74e101Schristos 6210f74e101Schristos while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) { 622fdccd7e4Schristos subfile_lineno++; 6230f74e101Schristos /* remove newline from the line */ 6240f74e101Schristos nl = strchr(fileline, '\n'); 6250f74e101Schristos if (nl) 6260f74e101Schristos *nl = '\0'; 6270f74e101Schristos if (fileline[0] == '#') continue; 6280f74e101Schristos if (fileline[0] == '\0') continue; 6290f74e101Schristos 630fdccd7e4Schristos esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno); 6310f74e101Schristos } 6320f74e101Schristos fclose(secretfile); 6330f74e101Schristos 6340f74e101Schristos return; 6350f74e101Schristos } 6360f74e101Schristos 637fdccd7e4Schristos if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) { 6380f74e101Schristos esp_print_decode_ikeline(ndo, line, file, lineno); 6390f74e101Schristos return; 6400f74e101Schristos } 6410f74e101Schristos 6420f74e101Schristos if (spikey) { 6430f74e101Schristos 6440f74e101Schristos char *spistr, *foo; 645b3a00663Schristos uint32_t spino; 6460f74e101Schristos 6470f74e101Schristos spistr = strsep(&spikey, "@"); 648817e9a7eSchristos if (spistr == NULL) { 649817e9a7eSchristos (*ndo->ndo_warning)(ndo, "print_esp: failed to find the @ token"); 650817e9a7eSchristos return; 651817e9a7eSchristos } 6520f74e101Schristos 6530f74e101Schristos spino = strtoul(spistr, &foo, 0); 6540f74e101Schristos if (spistr == foo || !spikey) { 6550f74e101Schristos (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo); 6560f74e101Schristos return; 6570f74e101Schristos } 6580f74e101Schristos 6590f74e101Schristos sa1.spi = spino; 6600f74e101Schristos 661fdccd7e4Schristos if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) { 662b3a00663Schristos sa1.daddr_version = 6; 663fdccd7e4Schristos } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) { 664b3a00663Schristos sa1.daddr_version = 4; 6650f74e101Schristos } else { 6660f74e101Schristos (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey); 6670f74e101Schristos return; 6680f74e101Schristos } 6690f74e101Schristos } 6700f74e101Schristos 6710f74e101Schristos if (decode) { 6720f74e101Schristos /* skip any blank spaces */ 673c74ad251Schristos while (*decode == ' ' || *decode == '\t' || *decode == '\r' || *decode == '\n') 6740f74e101Schristos decode++; 6750f74e101Schristos 6760f74e101Schristos if(!espprint_decode_encalgo(ndo, decode, &sa1)) { 6770f74e101Schristos return; 6780f74e101Schristos } 6790f74e101Schristos } 6800f74e101Schristos 6810f74e101Schristos esp_print_addsa(ndo, &sa1, sa_def); 6820f74e101Schristos } 6830f74e101Schristos 684c74ad251Schristos DIAG_OFF_DEPRECATION 6850f74e101Schristos static void esp_init(netdissect_options *ndo _U_) 6860f74e101Schristos { 687dc860a36Sspz /* 688dc860a36Sspz * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so 689dc860a36Sspz * we check whether it's undefined or it's less than the 690dc860a36Sspz * value for 1.1.0. 691dc860a36Sspz */ 692dc860a36Sspz #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L 6930f74e101Schristos OpenSSL_add_all_algorithms(); 694dc860a36Sspz #endif 6950f74e101Schristos } 696c74ad251Schristos DIAG_ON_DEPRECATION 6970f74e101Schristos 698c74ad251Schristos void esp_decodesecret_print(netdissect_options *ndo) 6990f74e101Schristos { 7000f74e101Schristos char *line; 7010f74e101Schristos char *p; 7020f74e101Schristos static int initialized = 0; 7030f74e101Schristos 7040f74e101Schristos if (!initialized) { 7050f74e101Schristos esp_init(ndo); 7060f74e101Schristos initialized = 1; 7070f74e101Schristos } 7080f74e101Schristos 7090f74e101Schristos p = ndo->ndo_espsecret; 7100f74e101Schristos 7110f74e101Schristos while (p && p[0] != '\0') { 7120f74e101Schristos /* pick out the first line or first thing until a comma */ 7130f74e101Schristos if ((line = strsep(&p, "\n,")) == NULL) { 7140f74e101Schristos line = p; 7150f74e101Schristos p = NULL; 7160f74e101Schristos } 7170f74e101Schristos 7180f74e101Schristos esp_print_decode_onesecret(ndo, line, "cmdline", 0); 7190f74e101Schristos } 7200f74e101Schristos 7210f74e101Schristos ndo->ndo_espsecret = NULL; 7220f74e101Schristos } 7230f74e101Schristos 7240f74e101Schristos #endif 7250f74e101Schristos 726870189d2Schristos #ifdef HAVE_LIBCRYPTO 727c74ad251Schristos #define USED_IF_LIBCRYPTO 728c74ad251Schristos #else 729c74ad251Schristos #define USED_IF_LIBCRYPTO _U_ 730870189d2Schristos #endif 731c74ad251Schristos 732c74ad251Schristos #ifdef HAVE_LIBCRYPTO 733c74ad251Schristos DIAG_OFF_DEPRECATION 734c74ad251Schristos #endif 735c74ad251Schristos void 7360f74e101Schristos esp_print(netdissect_options *ndo, 737c74ad251Schristos const u_char *bp, u_int length, 738c74ad251Schristos const u_char *bp2 USED_IF_LIBCRYPTO, 739c74ad251Schristos u_int ver USED_IF_LIBCRYPTO, 740c74ad251Schristos int fragmented USED_IF_LIBCRYPTO, 741c74ad251Schristos u_int ttl_hl USED_IF_LIBCRYPTO) 7420f74e101Schristos { 743c74ad251Schristos const struct newesp *esp; 744c74ad251Schristos const u_char *ep; 7450f74e101Schristos #ifdef HAVE_LIBCRYPTO 746fdccd7e4Schristos const struct ip *ip; 7470f74e101Schristos struct sa_list *sa = NULL; 748fdccd7e4Schristos const struct ip6_hdr *ip6 = NULL; 749c74ad251Schristos const u_char *iv; 750c74ad251Schristos u_int ivlen; 751c74ad251Schristos u_int payloadlen; 752c74ad251Schristos const u_char *ct; 753c74ad251Schristos u_char *pt; 754c74ad251Schristos u_int padlen; 755c74ad251Schristos u_int nh; 7560f74e101Schristos #endif 7570f74e101Schristos 758c74ad251Schristos ndo->ndo_protocol = "esp"; 759fdccd7e4Schristos esp = (const struct newesp *)bp; 7600f74e101Schristos 7610f74e101Schristos /* 'ep' points to the end of available data. */ 7620f74e101Schristos ep = ndo->ndo_snapend; 7630f74e101Schristos 764fdccd7e4Schristos if ((const u_char *)(esp + 1) >= ep) { 765c74ad251Schristos nd_print_trunc(ndo); 766c74ad251Schristos return; 7670f74e101Schristos } 768c74ad251Schristos ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp->esp_spi)); 769c74ad251Schristos ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp->esp_seq)); 770c74ad251Schristos ND_PRINT(", length %u", length); 7710f74e101Schristos 772c74ad251Schristos #ifdef HAVE_LIBCRYPTO 773c74ad251Schristos /* initialize SAs */ 7740f74e101Schristos if (ndo->ndo_sa_list_head == NULL) { 7750f74e101Schristos if (!ndo->ndo_espsecret) 776c74ad251Schristos return; 7770f74e101Schristos 778c74ad251Schristos esp_decodesecret_print(ndo); 7790f74e101Schristos } 7800f74e101Schristos 7810f74e101Schristos if (ndo->ndo_sa_list_head == NULL) 782c74ad251Schristos return; 7830f74e101Schristos 784fdccd7e4Schristos ip = (const struct ip *)bp2; 785c74ad251Schristos switch (ver) { 7860f74e101Schristos case 6: 787fdccd7e4Schristos ip6 = (const struct ip6_hdr *)bp2; 7880f74e101Schristos /* we do not attempt to decrypt jumbograms */ 789c74ad251Schristos if (!GET_BE_U_2(ip6->ip6_plen)) 790c74ad251Schristos return; 791c74ad251Schristos /* XXX - check whether it's fragmented? */ 7920f74e101Schristos /* if we can't get nexthdr, we do not need to decrypt it */ 7930f74e101Schristos 7940f74e101Schristos /* see if we can find the SA, and if so, decode it */ 7950f74e101Schristos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) { 796c74ad251Schristos if (sa->spi == GET_BE_U_4(esp->esp_spi) && 797b3a00663Schristos sa->daddr_version == 6 && 798b3a00663Schristos UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst, 799c74ad251Schristos sizeof(nd_ipv6)) == 0) { 8000f74e101Schristos break; 8010f74e101Schristos } 8020f74e101Schristos } 8030f74e101Schristos break; 8040f74e101Schristos case 4: 8050f74e101Schristos /* nexthdr & padding are in the last fragment */ 806c74ad251Schristos if (fragmented) 807c74ad251Schristos return; 8080f74e101Schristos 8090f74e101Schristos /* see if we can find the SA, and if so, decode it */ 8100f74e101Schristos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) { 811c74ad251Schristos if (sa->spi == GET_BE_U_4(esp->esp_spi) && 812b3a00663Schristos sa->daddr_version == 4 && 813b3a00663Schristos UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst, 814c74ad251Schristos sizeof(nd_ipv4)) == 0) { 8150f74e101Schristos break; 8160f74e101Schristos } 8170f74e101Schristos } 8180f74e101Schristos break; 8190f74e101Schristos default: 820c74ad251Schristos return; 8210f74e101Schristos } 8220f74e101Schristos 8230f74e101Schristos /* if we didn't find the specific one, then look for 8240f74e101Schristos * an unspecified one. 8250f74e101Schristos */ 8260f74e101Schristos if (sa == NULL) 8270f74e101Schristos sa = ndo->ndo_sa_default; 8280f74e101Schristos 8290f74e101Schristos /* if not found fail */ 8300f74e101Schristos if (sa == NULL) 831c74ad251Schristos return; 8320f74e101Schristos 83372c96ff3Schristos /* pointer to the IV, if there is one */ 834c74ad251Schristos iv = (const u_char *)(esp + 1) + 0; 83572c96ff3Schristos /* length of the IV, if there is one; 0, if there isn't */ 8360f74e101Schristos ivlen = sa->ivlen; 837c74ad251Schristos 838c74ad251Schristos /* 839c74ad251Schristos * Get a pointer to the ciphertext. 840c74ad251Schristos * 841c74ad251Schristos * p points to the beginning of the payload, i.e. to the 842c74ad251Schristos * initialization vector, so if we skip past the initialization 843c74ad251Schristos * vector, it points to the beginning of the ciphertext. 844c74ad251Schristos */ 845c74ad251Schristos ct = iv + ivlen; 846c74ad251Schristos 847c74ad251Schristos /* 848c74ad251Schristos * Make sure the authentication data/integrity check value length 849c74ad251Schristos * isn't bigger than the total amount of data available after 850c74ad251Schristos * the ESP header and initialization vector is removed and, 851c74ad251Schristos * if not, slice the authentication data/ICV off. 852c74ad251Schristos */ 853c74ad251Schristos if (ep - ct < sa->authlen) { 854c74ad251Schristos nd_print_trunc(ndo); 855c74ad251Schristos return; 856c74ad251Schristos } 8570f74e101Schristos ep = ep - sa->authlen; 8580f74e101Schristos 859c74ad251Schristos /* 860c74ad251Schristos * Calculate the length of the ciphertext. ep points to 861c74ad251Schristos * the beginning of the authentication data/integrity check 862c74ad251Schristos * value, i.e. right past the end of the ciphertext; 863c74ad251Schristos */ 864c74ad251Schristos payloadlen = ep - ct; 8650f74e101Schristos 866c74ad251Schristos if (sa->evp == NULL) 867c74ad251Schristos return; 86872c96ff3Schristos 86972c96ff3Schristos /* 870c74ad251Schristos * If the next header value is past the end of the available 871c74ad251Schristos * data, we won't be able to fetch it once we've decrypted 872c74ad251Schristos * the ciphertext, so there's no point in decrypting the data. 873c74ad251Schristos * 874c74ad251Schristos * Report it as truncation. 87572c96ff3Schristos */ 876c74ad251Schristos if (!ND_TTEST_1(ep - 1)) { 877c74ad251Schristos nd_print_trunc(ndo); 878c74ad251Schristos return; 87972c96ff3Schristos } 88072c96ff3Schristos 881c74ad251Schristos pt = do_decrypt(ndo, __func__, sa, iv, ct, payloadlen); 882c74ad251Schristos if (pt == NULL) 883c74ad251Schristos return; 884c74ad251Schristos 88572c96ff3Schristos /* 886c74ad251Schristos * Switch to the output buffer for dissection, and 887c74ad251Schristos * save it on the buffer stack so it can be freed. 88872c96ff3Schristos */ 889c74ad251Schristos if (!nd_push_buffer(ndo, pt, pt, payloadlen)) { 890c74ad251Schristos free(pt); 891c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 892c74ad251Schristos "%s: can't push buffer on buffer stack", __func__); 893c74ad251Schristos } 8940f74e101Schristos 895c74ad251Schristos /* 896c74ad251Schristos * Sanity check for pad length; if it, plus 2 for the pad 897c74ad251Schristos * length and next header fields, is bigger than the ciphertext 898c74ad251Schristos * length (which is also the plaintext length), it's too big. 899c74ad251Schristos * 900c74ad251Schristos * XXX - the check can fail if the packet is corrupt *or* if 901c74ad251Schristos * it was not decrypted with the correct key, so that the 902c74ad251Schristos * "plaintext" is not what was being sent. 903c74ad251Schristos */ 904c74ad251Schristos padlen = GET_U_1(pt + payloadlen - 2); 905c74ad251Schristos if (padlen + 2 > payloadlen) { 906c74ad251Schristos nd_print_trunc(ndo); 907c74ad251Schristos return; 908c74ad251Schristos } 9090f74e101Schristos 910c74ad251Schristos /* Get the next header */ 911c74ad251Schristos nh = GET_U_1(pt + payloadlen - 1); 9120f74e101Schristos 913c74ad251Schristos ND_PRINT(": "); 9140f74e101Schristos 915c74ad251Schristos /* 916c74ad251Schristos * Don't put padding + padding length(1 byte) + next header(1 byte) 917c74ad251Schristos * in the buffer because they are not part of the plaintext to decode. 918c74ad251Schristos */ 919c74ad251Schristos if (!nd_push_snaplen(ndo, pt, payloadlen - (padlen + 2))) { 920c74ad251Schristos (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 921c74ad251Schristos "%s: can't push snaplen on buffer stack", __func__); 922c74ad251Schristos } 923c74ad251Schristos 924c74ad251Schristos /* Now dissect the plaintext. */ 925c74ad251Schristos ip_demux_print(ndo, pt, payloadlen - (padlen + 2), ver, fragmented, 926c74ad251Schristos ttl_hl, nh, bp2); 927c74ad251Schristos 928c74ad251Schristos /* Pop the buffer, freeing it. */ 929c74ad251Schristos nd_pop_packet_info(ndo); 930c74ad251Schristos /* Pop the nd_push_snaplen */ 931c74ad251Schristos nd_pop_packet_info(ndo); 9320f74e101Schristos #endif 9330f74e101Schristos } 934870189d2Schristos #ifdef HAVE_LIBCRYPTO 935c74ad251Schristos DIAG_ON_DEPRECATION 936870189d2Schristos #endif 937