xref: /aosp_15_r20/external/tcpdump/print-esp.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*	$NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $	*/
2*05b00f60SXin Li 
3*05b00f60SXin Li /*
4*05b00f60SXin Li  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5*05b00f60SXin Li  *	The Regents of the University of California.  All rights reserved.
6*05b00f60SXin Li  *
7*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
8*05b00f60SXin Li  * modification, are permitted provided that: (1) source code distributions
9*05b00f60SXin Li  * retain the above copyright notice and this paragraph in its entirety, (2)
10*05b00f60SXin Li  * distributions including binary code include the above copyright notice and
11*05b00f60SXin Li  * this paragraph in its entirety in the documentation or other materials
12*05b00f60SXin Li  * provided with the distribution, and (3) all advertising materials mentioning
13*05b00f60SXin Li  * features or use of this software display the following acknowledgement:
14*05b00f60SXin Li  * ``This product includes software developed by the University of California,
15*05b00f60SXin Li  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16*05b00f60SXin Li  * the University nor the names of its contributors may be used to endorse
17*05b00f60SXin Li  * or promote products derived from this software without specific prior
18*05b00f60SXin Li  * written permission.
19*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20*05b00f60SXin Li  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21*05b00f60SXin Li  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22*05b00f60SXin Li  */
23*05b00f60SXin Li 
24*05b00f60SXin Li /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
25*05b00f60SXin Li 
26*05b00f60SXin Li #ifdef HAVE_CONFIG_H
27*05b00f60SXin Li #include <config.h>
28*05b00f60SXin Li #endif
29*05b00f60SXin Li 
30*05b00f60SXin Li #include "netdissect-stdinc.h"
31*05b00f60SXin Li 
32*05b00f60SXin Li #include <string.h>
33*05b00f60SXin Li #include <stdlib.h>
34*05b00f60SXin Li 
35*05b00f60SXin Li /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
36*05b00f60SXin Li  * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
37*05b00f60SXin Li  * is the simplest way of handling the dependency.
38*05b00f60SXin Li  */
39*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
40*05b00f60SXin Li #ifdef HAVE_OPENSSL_EVP_H
41*05b00f60SXin Li #include <openssl/evp.h>
42*05b00f60SXin Li #else
43*05b00f60SXin Li #undef HAVE_LIBCRYPTO
44*05b00f60SXin Li #endif
45*05b00f60SXin Li #endif
46*05b00f60SXin Li 
47*05b00f60SXin Li #include "netdissect.h"
48*05b00f60SXin Li #include "extract.h"
49*05b00f60SXin Li 
50*05b00f60SXin Li #include "diag-control.h"
51*05b00f60SXin Li 
52*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
53*05b00f60SXin Li #include "strtoaddr.h"
54*05b00f60SXin Li #include "ascii_strcasecmp.h"
55*05b00f60SXin Li #endif
56*05b00f60SXin Li 
57*05b00f60SXin Li #include "ip.h"
58*05b00f60SXin Li #include "ip6.h"
59*05b00f60SXin Li 
60*05b00f60SXin Li /*
61*05b00f60SXin Li  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
62*05b00f60SXin Li  * All rights reserved.
63*05b00f60SXin Li  *
64*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
65*05b00f60SXin Li  * modification, are permitted provided that the following conditions
66*05b00f60SXin Li  * are met:
67*05b00f60SXin Li  * 1. Redistributions of source code must retain the above copyright
68*05b00f60SXin Li  *    notice, this list of conditions and the following disclaimer.
69*05b00f60SXin Li  * 2. Redistributions in binary form must reproduce the above copyright
70*05b00f60SXin Li  *    notice, this list of conditions and the following disclaimer in the
71*05b00f60SXin Li  *    documentation and/or other materials provided with the distribution.
72*05b00f60SXin Li  * 3. Neither the name of the project nor the names of its contributors
73*05b00f60SXin Li  *    may be used to endorse or promote products derived from this software
74*05b00f60SXin Li  *    without specific prior written permission.
75*05b00f60SXin Li  *
76*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
77*05b00f60SXin Li  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78*05b00f60SXin Li  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79*05b00f60SXin Li  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
80*05b00f60SXin Li  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81*05b00f60SXin Li  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82*05b00f60SXin Li  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83*05b00f60SXin Li  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84*05b00f60SXin Li  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85*05b00f60SXin Li  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86*05b00f60SXin Li  * SUCH DAMAGE.
87*05b00f60SXin Li  */
88*05b00f60SXin Li 
89*05b00f60SXin Li /*
90*05b00f60SXin Li  * RFC1827/2406 Encapsulated Security Payload.
91*05b00f60SXin Li  */
92*05b00f60SXin Li 
93*05b00f60SXin Li struct newesp {
94*05b00f60SXin Li 	nd_uint32_t	esp_spi;	/* ESP */
95*05b00f60SXin Li 	nd_uint32_t	esp_seq;	/* Sequence number */
96*05b00f60SXin Li 	/*variable size*/		/* (IV and) Payload data */
97*05b00f60SXin Li 	/*variable size*/		/* padding */
98*05b00f60SXin Li 	/*8bit*/			/* pad size */
99*05b00f60SXin Li 	/*8bit*/			/* next header */
100*05b00f60SXin Li 	/*8bit*/			/* next header */
101*05b00f60SXin Li 	/*variable size, 32bit bound*/	/* Authentication data */
102*05b00f60SXin Li };
103*05b00f60SXin Li 
104*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
105*05b00f60SXin Li union inaddr_u {
106*05b00f60SXin Li 	nd_ipv4 in4;
107*05b00f60SXin Li 	nd_ipv6 in6;
108*05b00f60SXin Li };
109*05b00f60SXin Li struct sa_list {
110*05b00f60SXin Li 	struct sa_list	*next;
111*05b00f60SXin Li 	u_int		daddr_version;
112*05b00f60SXin Li 	union inaddr_u	daddr;
113*05b00f60SXin Li 	uint32_t	spi;          /* if == 0, then IKEv2 */
114*05b00f60SXin Li 	int             initiator;
115*05b00f60SXin Li 	u_char          spii[8];      /* for IKEv2 */
116*05b00f60SXin Li 	u_char          spir[8];
117*05b00f60SXin Li 	const EVP_CIPHER *evp;
118*05b00f60SXin Li 	u_int		ivlen;
119*05b00f60SXin Li 	int		authlen;
120*05b00f60SXin Li 	u_char          authsecret[256];
121*05b00f60SXin Li 	int             authsecret_len;
122*05b00f60SXin Li 	u_char		secret[256];  /* is that big enough for all secrets? */
123*05b00f60SXin Li 	int		secretlen;
124*05b00f60SXin Li };
125*05b00f60SXin Li 
126*05b00f60SXin Li #ifndef HAVE_EVP_CIPHER_CTX_NEW
127*05b00f60SXin Li /*
128*05b00f60SXin Li  * Allocate an EVP_CIPHER_CTX.
129*05b00f60SXin Li  * Used if we have an older version of OpenSSL that doesn't provide
130*05b00f60SXin Li  * routines to allocate and free them.
131*05b00f60SXin Li  */
132*05b00f60SXin Li static EVP_CIPHER_CTX *
EVP_CIPHER_CTX_new(void)133*05b00f60SXin Li EVP_CIPHER_CTX_new(void)
134*05b00f60SXin Li {
135*05b00f60SXin Li 	EVP_CIPHER_CTX *ctx;
136*05b00f60SXin Li 
137*05b00f60SXin Li 	ctx = malloc(sizeof(*ctx));
138*05b00f60SXin Li 	if (ctx == NULL)
139*05b00f60SXin Li 		return (NULL);
140*05b00f60SXin Li 	memset(ctx, 0, sizeof(*ctx));
141*05b00f60SXin Li 	return (ctx);
142*05b00f60SXin Li }
143*05b00f60SXin Li 
144*05b00f60SXin Li static void
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX * ctx)145*05b00f60SXin Li EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
146*05b00f60SXin Li {
147*05b00f60SXin Li 	EVP_CIPHER_CTX_cleanup(ctx);
148*05b00f60SXin Li 	free(ctx);
149*05b00f60SXin Li }
150*05b00f60SXin Li #endif
151*05b00f60SXin Li 
152*05b00f60SXin Li #ifdef HAVE_EVP_DECRYPTINIT_EX
153*05b00f60SXin Li /*
154*05b00f60SXin Li  * Initialize the cipher by calling EVP_DecryptInit_ex(), because
155*05b00f60SXin Li  * calling EVP_DecryptInit() will reset the cipher context, clearing
156*05b00f60SXin Li  * the cipher, so calling it twice, with the second call having a
157*05b00f60SXin Li  * null cipher, will clear the already-set cipher.  EVP_DecryptInit_ex(),
158*05b00f60SXin Li  * however, won't reset the cipher context, so you can use it to specify
159*05b00f60SXin Li  * the IV in a second call after a first call to EVP_DecryptInit_ex()
160*05b00f60SXin Li  * to set the cipher and the key.
161*05b00f60SXin Li  *
162*05b00f60SXin Li  * XXX - is there some reason why we need to make two calls?
163*05b00f60SXin Li  */
164*05b00f60SXin Li static int
set_cipher_parameters(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)165*05b00f60SXin Li set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
166*05b00f60SXin Li 		      const unsigned char *key,
167*05b00f60SXin Li 		      const unsigned char *iv)
168*05b00f60SXin Li {
169*05b00f60SXin Li 	return EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
170*05b00f60SXin Li }
171*05b00f60SXin Li #else
172*05b00f60SXin Li /*
173*05b00f60SXin Li  * Initialize the cipher by calling EVP_DecryptInit(), because we don't
174*05b00f60SXin Li  * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
175*05b00f60SXin Li  */
176*05b00f60SXin Li static int
set_cipher_parameters(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)177*05b00f60SXin Li set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
178*05b00f60SXin Li 		      const unsigned char *key,
179*05b00f60SXin Li 		      const unsigned char *iv)
180*05b00f60SXin Li {
181*05b00f60SXin Li 	return EVP_DecryptInit(ctx, cipher, key, iv);
182*05b00f60SXin Li }
183*05b00f60SXin Li #endif
184*05b00f60SXin Li 
185*05b00f60SXin Li static u_char *
do_decrypt(netdissect_options * ndo,const char * caller,struct sa_list * sa,const u_char * iv,const u_char * ct,unsigned int ctlen)186*05b00f60SXin Li do_decrypt(netdissect_options *ndo, const char *caller, struct sa_list *sa,
187*05b00f60SXin Li     const u_char *iv, const u_char *ct, unsigned int ctlen)
188*05b00f60SXin Li {
189*05b00f60SXin Li 	EVP_CIPHER_CTX *ctx;
190*05b00f60SXin Li 	unsigned int block_size;
191*05b00f60SXin Li 	unsigned int ptlen;
192*05b00f60SXin Li 	u_char *pt;
193*05b00f60SXin Li 	int len;
194*05b00f60SXin Li 
195*05b00f60SXin Li 	ctx = EVP_CIPHER_CTX_new();
196*05b00f60SXin Li 	if (ctx == NULL) {
197*05b00f60SXin Li 		/*
198*05b00f60SXin Li 		 * Failed to initialize the cipher context.
199*05b00f60SXin Li 		 * From a look at the OpenSSL code, this appears to
200*05b00f60SXin Li 		 * mean "couldn't allocate memory for the cipher context";
201*05b00f60SXin Li 		 * note that we're not passing any parameters, so there's
202*05b00f60SXin Li 		 * not much else it can mean.
203*05b00f60SXin Li 		 */
204*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
205*05b00f60SXin Li 		    "%s: can't allocate memory for cipher context", caller);
206*05b00f60SXin Li 		return NULL;
207*05b00f60SXin Li 	}
208*05b00f60SXin Li 
209*05b00f60SXin Li 	if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL) < 0) {
210*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
211*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "%s: espkey init failed", caller);
212*05b00f60SXin Li 		return NULL;
213*05b00f60SXin Li 	}
214*05b00f60SXin Li 	if (set_cipher_parameters(ctx, NULL, NULL, iv) < 0) {
215*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
216*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "%s: IV init failed", caller);
217*05b00f60SXin Li 		return NULL;
218*05b00f60SXin Li 	}
219*05b00f60SXin Li 
220*05b00f60SXin Li 	/*
221*05b00f60SXin Li 	 * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
222*05b00f60SXin Li 	 * if the cipher has a block size of which the ciphertext's size must
223*05b00f60SXin Li 	 * be a multiple, the payload must be padded to make that happen, so
224*05b00f60SXin Li 	 * the ciphertext length must be a multiple of the block size.  Fail
225*05b00f60SXin Li 	 * if that's not the case.
226*05b00f60SXin Li 	 */
227*05b00f60SXin Li 	block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
228*05b00f60SXin Li 	if ((ctlen % block_size) != 0) {
229*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
230*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo,
231*05b00f60SXin Li 		    "%s: ciphertext size %u is not a multiple of the cipher block size %u",
232*05b00f60SXin Li 		    caller, ctlen, block_size);
233*05b00f60SXin Li 		return NULL;
234*05b00f60SXin Li 	}
235*05b00f60SXin Li 
236*05b00f60SXin Li 	/*
237*05b00f60SXin Li 	 * Attempt to allocate a buffer for the decrypted data, because
238*05b00f60SXin Li 	 * we can't decrypt on top of the input buffer.
239*05b00f60SXin Li 	 */
240*05b00f60SXin Li 	ptlen = ctlen;
241*05b00f60SXin Li 	pt = (u_char *)calloc(1, ptlen);
242*05b00f60SXin Li 	if (pt == NULL) {
243*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
244*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
245*05b00f60SXin Li 		    "%s: can't allocate memory for decryption buffer", caller);
246*05b00f60SXin Li 		return NULL;
247*05b00f60SXin Li 	}
248*05b00f60SXin Li 
249*05b00f60SXin Li 	/*
250*05b00f60SXin Li 	 * The size of the ciphertext handed to us is a multiple of the
251*05b00f60SXin Li 	 * cipher block size, so we don't need to worry about padding.
252*05b00f60SXin Li 	 */
253*05b00f60SXin Li 	if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) {
254*05b00f60SXin Li 		free(pt);
255*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
256*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo,
257*05b00f60SXin Li 		    "%s: EVP_CIPHER_CTX_set_padding failed", caller);
258*05b00f60SXin Li 		return NULL;
259*05b00f60SXin Li 	}
260*05b00f60SXin Li 	if (!EVP_DecryptUpdate(ctx, pt, &len, ct, ctlen)) {
261*05b00f60SXin Li 		free(pt);
262*05b00f60SXin Li 		EVP_CIPHER_CTX_free(ctx);
263*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "%s: EVP_DecryptUpdate failed",
264*05b00f60SXin Li 		    caller);
265*05b00f60SXin Li 		return NULL;
266*05b00f60SXin Li 	}
267*05b00f60SXin Li 	EVP_CIPHER_CTX_free(ctx);
268*05b00f60SXin Li 	return pt;
269*05b00f60SXin Li }
270*05b00f60SXin Li 
271*05b00f60SXin Li /*
272*05b00f60SXin Li  * This will allocate a new buffer containing the decrypted data.
273*05b00f60SXin Li  * It returns 1 on success and 0 on failure.
274*05b00f60SXin Li  *
275*05b00f60SXin Li  * It will push the new buffer and the values of ndo->ndo_packetp and
276*05b00f60SXin Li  * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
277*05b00f60SXin Li  * and ndo->ndo_snapend to refer to the new buffer.
278*05b00f60SXin Li  *
279*05b00f60SXin Li  * Our caller must pop the buffer off the stack when it's finished
280*05b00f60SXin Li  * dissecting anything in it and before it does any dissection of
281*05b00f60SXin Li  * anything in the old buffer.  That will free the new buffer.
282*05b00f60SXin Li  */
283*05b00f60SXin Li DIAG_OFF_DEPRECATION
esp_decrypt_buffer_by_ikev2_print(netdissect_options * ndo,int initiator,const u_char spii[8],const u_char spir[8],const u_char * buf,const u_char * end)284*05b00f60SXin Li int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo,
285*05b00f60SXin Li 				      int initiator,
286*05b00f60SXin Li 				      const u_char spii[8],
287*05b00f60SXin Li 				      const u_char spir[8],
288*05b00f60SXin Li 				      const u_char *buf, const u_char *end)
289*05b00f60SXin Li {
290*05b00f60SXin Li 	struct sa_list *sa;
291*05b00f60SXin Li 	const u_char *iv;
292*05b00f60SXin Li 	const u_char *ct;
293*05b00f60SXin Li 	unsigned int ctlen;
294*05b00f60SXin Li 	u_char *pt;
295*05b00f60SXin Li 
296*05b00f60SXin Li 	/* initiator arg is any non-zero value */
297*05b00f60SXin Li 	if(initiator) initiator=1;
298*05b00f60SXin Li 
299*05b00f60SXin Li 	/* see if we can find the SA, and if so, decode it */
300*05b00f60SXin Li 	for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
301*05b00f60SXin Li 		if (sa->spi == 0
302*05b00f60SXin Li 		    && initiator == sa->initiator
303*05b00f60SXin Li 		    && memcmp(spii, sa->spii, 8) == 0
304*05b00f60SXin Li 		    && memcmp(spir, sa->spir, 8) == 0)
305*05b00f60SXin Li 			break;
306*05b00f60SXin Li 	}
307*05b00f60SXin Li 
308*05b00f60SXin Li 	if(sa == NULL) return 0;
309*05b00f60SXin Li 	if(sa->evp == NULL) return 0;
310*05b00f60SXin Li 
311*05b00f60SXin Li 	/*
312*05b00f60SXin Li 	 * remove authenticator, and see if we still have something to
313*05b00f60SXin Li 	 * work with
314*05b00f60SXin Li 	 */
315*05b00f60SXin Li 	end = end - sa->authlen;
316*05b00f60SXin Li 	iv  = buf;
317*05b00f60SXin Li 	ct = iv + sa->ivlen;
318*05b00f60SXin Li 	ctlen = end-ct;
319*05b00f60SXin Li 
320*05b00f60SXin Li 	if(end <= ct) return 0;
321*05b00f60SXin Li 
322*05b00f60SXin Li 	pt = do_decrypt(ndo, __func__, sa, iv,
323*05b00f60SXin Li 	    ct, ctlen);
324*05b00f60SXin Li 	if (pt == NULL)
325*05b00f60SXin Li 		return 0;
326*05b00f60SXin Li 
327*05b00f60SXin Li 	/*
328*05b00f60SXin Li 	 * Switch to the output buffer for dissection, and save it
329*05b00f60SXin Li 	 * on the buffer stack so it can be freed; our caller must
330*05b00f60SXin Li 	 * pop it when done.
331*05b00f60SXin Li 	 */
332*05b00f60SXin Li 	if (!nd_push_buffer(ndo, pt, pt, ctlen)) {
333*05b00f60SXin Li 		free(pt);
334*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
335*05b00f60SXin Li 			"%s: can't push buffer on buffer stack", __func__);
336*05b00f60SXin Li 	}
337*05b00f60SXin Li 
338*05b00f60SXin Li 	return 1;
339*05b00f60SXin Li }
340*05b00f60SXin Li DIAG_ON_DEPRECATION
341*05b00f60SXin Li 
esp_print_addsa(netdissect_options * ndo,const struct sa_list * sa,int sa_def)342*05b00f60SXin Li static void esp_print_addsa(netdissect_options *ndo,
343*05b00f60SXin Li 			    const struct sa_list *sa, int sa_def)
344*05b00f60SXin Li {
345*05b00f60SXin Li 	/* copy the "sa" */
346*05b00f60SXin Li 
347*05b00f60SXin Li 	struct sa_list *nsa;
348*05b00f60SXin Li 
349*05b00f60SXin Li 	/* malloc() return used in a 'struct sa_list': do not free() */
350*05b00f60SXin Li 	nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
351*05b00f60SXin Li 	if (nsa == NULL)
352*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
353*05b00f60SXin Li 				  "%s: malloc", __func__);
354*05b00f60SXin Li 
355*05b00f60SXin Li 	*nsa = *sa;
356*05b00f60SXin Li 
357*05b00f60SXin Li 	if (sa_def)
358*05b00f60SXin Li 		ndo->ndo_sa_default = nsa;
359*05b00f60SXin Li 
360*05b00f60SXin Li 	nsa->next = ndo->ndo_sa_list_head;
361*05b00f60SXin Li 	ndo->ndo_sa_list_head = nsa;
362*05b00f60SXin Li }
363*05b00f60SXin Li 
364*05b00f60SXin Li 
hexdigit(netdissect_options * ndo,char hex)365*05b00f60SXin Li static u_int hexdigit(netdissect_options *ndo, char hex)
366*05b00f60SXin Li {
367*05b00f60SXin Li 	if (hex >= '0' && hex <= '9')
368*05b00f60SXin Li 		return (hex - '0');
369*05b00f60SXin Li 	else if (hex >= 'A' && hex <= 'F')
370*05b00f60SXin Li 		return (hex - 'A' + 10);
371*05b00f60SXin Li 	else if (hex >= 'a' && hex <= 'f')
372*05b00f60SXin Li 		return (hex - 'a' + 10);
373*05b00f60SXin Li 	else {
374*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_ESP_SECRET,
375*05b00f60SXin Li 				  "invalid hex digit %c in espsecret\n", hex);
376*05b00f60SXin Li 	}
377*05b00f60SXin Li }
378*05b00f60SXin Li 
hex2byte(netdissect_options * ndo,char * hexstring)379*05b00f60SXin Li static u_int hex2byte(netdissect_options *ndo, char *hexstring)
380*05b00f60SXin Li {
381*05b00f60SXin Li 	u_int byte;
382*05b00f60SXin Li 
383*05b00f60SXin Li 	byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
384*05b00f60SXin Li 	return byte;
385*05b00f60SXin Li }
386*05b00f60SXin Li 
387*05b00f60SXin Li /*
388*05b00f60SXin Li  * returns size of binary, 0 on failure.
389*05b00f60SXin Li  */
390*05b00f60SXin Li static int
espprint_decode_hex(netdissect_options * ndo,u_char * binbuf,unsigned int binbuf_len,char * hex)391*05b00f60SXin Li espprint_decode_hex(netdissect_options *ndo,
392*05b00f60SXin Li 		    u_char *binbuf, unsigned int binbuf_len, char *hex)
393*05b00f60SXin Li {
394*05b00f60SXin Li 	unsigned int len;
395*05b00f60SXin Li 	int i;
396*05b00f60SXin Li 
397*05b00f60SXin Li 	len = strlen(hex) / 2;
398*05b00f60SXin Li 
399*05b00f60SXin Li 	if (len > binbuf_len) {
400*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "secret is too big: %u\n", len);
401*05b00f60SXin Li 		return 0;
402*05b00f60SXin Li 	}
403*05b00f60SXin Li 
404*05b00f60SXin Li 	i = 0;
405*05b00f60SXin Li 	while (hex[0] != '\0' && hex[1]!='\0') {
406*05b00f60SXin Li 		binbuf[i] = hex2byte(ndo, hex);
407*05b00f60SXin Li 		hex += 2;
408*05b00f60SXin Li 		i++;
409*05b00f60SXin Li 	}
410*05b00f60SXin Li 
411*05b00f60SXin Li 	return i;
412*05b00f60SXin Li }
413*05b00f60SXin Li 
414*05b00f60SXin Li /*
415*05b00f60SXin Li  * decode the form:    SPINUM@IP <tab> ALGONAME:0xsecret
416*05b00f60SXin Li  */
417*05b00f60SXin Li 
418*05b00f60SXin Li DIAG_OFF_DEPRECATION
419*05b00f60SXin Li static int
espprint_decode_encalgo(netdissect_options * ndo,char * decode,struct sa_list * sa)420*05b00f60SXin Li espprint_decode_encalgo(netdissect_options *ndo,
421*05b00f60SXin Li 			char *decode, struct sa_list *sa)
422*05b00f60SXin Li {
423*05b00f60SXin Li 	size_t i;
424*05b00f60SXin Li 	const EVP_CIPHER *evp;
425*05b00f60SXin Li 	int authlen = 0;
426*05b00f60SXin Li 	char *colon, *p;
427*05b00f60SXin Li 
428*05b00f60SXin Li 	colon = strchr(decode, ':');
429*05b00f60SXin Li 	if (colon == NULL) {
430*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
431*05b00f60SXin Li 		return 0;
432*05b00f60SXin Li 	}
433*05b00f60SXin Li 	*colon = '\0';
434*05b00f60SXin Li 
435*05b00f60SXin Li 	if (strlen(decode) > strlen("-hmac96") &&
436*05b00f60SXin Li 	    !strcmp(decode + strlen(decode) - strlen("-hmac96"),
437*05b00f60SXin Li 		    "-hmac96")) {
438*05b00f60SXin Li 		p = strstr(decode, "-hmac96");
439*05b00f60SXin Li 		*p = '\0';
440*05b00f60SXin Li 		authlen = 12;
441*05b00f60SXin Li 	}
442*05b00f60SXin Li 	if (strlen(decode) > strlen("-cbc") &&
443*05b00f60SXin Li 	    !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
444*05b00f60SXin Li 		p = strstr(decode, "-cbc");
445*05b00f60SXin Li 		*p = '\0';
446*05b00f60SXin Li 	}
447*05b00f60SXin Li 	evp = EVP_get_cipherbyname(decode);
448*05b00f60SXin Li 
449*05b00f60SXin Li 	if (!evp) {
450*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
451*05b00f60SXin Li 		sa->evp = NULL;
452*05b00f60SXin Li 		sa->authlen = 0;
453*05b00f60SXin Li 		sa->ivlen = 0;
454*05b00f60SXin Li 		return 0;
455*05b00f60SXin Li 	}
456*05b00f60SXin Li 
457*05b00f60SXin Li 	sa->evp = evp;
458*05b00f60SXin Li 	sa->authlen = authlen;
459*05b00f60SXin Li 	/* This returns an int, but it should never be negative */
460*05b00f60SXin Li 	sa->ivlen = EVP_CIPHER_iv_length(evp);
461*05b00f60SXin Li 
462*05b00f60SXin Li 	colon++;
463*05b00f60SXin Li 	if (colon[0] == '0' && colon[1] == 'x') {
464*05b00f60SXin Li 		/* decode some hex! */
465*05b00f60SXin Li 
466*05b00f60SXin Li 		colon += 2;
467*05b00f60SXin Li 		sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
468*05b00f60SXin Li 		if(sa->secretlen == 0) return 0;
469*05b00f60SXin Li 	} else {
470*05b00f60SXin Li 		i = strlen(colon);
471*05b00f60SXin Li 
472*05b00f60SXin Li 		if (i < sizeof(sa->secret)) {
473*05b00f60SXin Li 			memcpy(sa->secret, colon, i);
474*05b00f60SXin Li 			sa->secretlen = i;
475*05b00f60SXin Li 		} else {
476*05b00f60SXin Li 			memcpy(sa->secret, colon, sizeof(sa->secret));
477*05b00f60SXin Li 			sa->secretlen = sizeof(sa->secret);
478*05b00f60SXin Li 		}
479*05b00f60SXin Li 	}
480*05b00f60SXin Li 
481*05b00f60SXin Li 	return 1;
482*05b00f60SXin Li }
483*05b00f60SXin Li DIAG_ON_DEPRECATION
484*05b00f60SXin Li 
485*05b00f60SXin Li /*
486*05b00f60SXin Li  * for the moment, ignore the auth algorithm, just hard code the authenticator
487*05b00f60SXin Li  * length. Need to research how openssl looks up HMAC stuff.
488*05b00f60SXin Li  */
489*05b00f60SXin Li static int
espprint_decode_authalgo(netdissect_options * ndo,char * decode,struct sa_list * sa)490*05b00f60SXin Li espprint_decode_authalgo(netdissect_options *ndo,
491*05b00f60SXin Li 			 char *decode, struct sa_list *sa)
492*05b00f60SXin Li {
493*05b00f60SXin Li 	char *colon;
494*05b00f60SXin Li 
495*05b00f60SXin Li 	colon = strchr(decode, ':');
496*05b00f60SXin Li 	if (colon == NULL) {
497*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
498*05b00f60SXin Li 		return 0;
499*05b00f60SXin Li 	}
500*05b00f60SXin Li 	*colon = '\0';
501*05b00f60SXin Li 
502*05b00f60SXin Li 	if(ascii_strcasecmp(decode,"sha1") == 0 ||
503*05b00f60SXin Li 	   ascii_strcasecmp(decode,"md5") == 0) {
504*05b00f60SXin Li 		sa->authlen = 12;
505*05b00f60SXin Li 	}
506*05b00f60SXin Li 	return 1;
507*05b00f60SXin Li }
508*05b00f60SXin Li 
esp_print_decode_ikeline(netdissect_options * ndo,char * line,const char * file,int lineno)509*05b00f60SXin Li static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
510*05b00f60SXin Li 				     const char *file, int lineno)
511*05b00f60SXin Li {
512*05b00f60SXin Li 	/* it's an IKEv2 secret, store it instead */
513*05b00f60SXin Li 	struct sa_list sa1;
514*05b00f60SXin Li 
515*05b00f60SXin Li 	char *init;
516*05b00f60SXin Li 	char *icookie, *rcookie;
517*05b00f60SXin Li 	int   ilen, rlen;
518*05b00f60SXin Li 	char *authkey;
519*05b00f60SXin Li 	char *enckey;
520*05b00f60SXin Li 
521*05b00f60SXin Li 	init = strsep(&line, " \t");
522*05b00f60SXin Li 	icookie = strsep(&line, " \t");
523*05b00f60SXin Li 	rcookie = strsep(&line, " \t");
524*05b00f60SXin Li 	authkey = strsep(&line, " \t");
525*05b00f60SXin Li 	enckey  = strsep(&line, " \t");
526*05b00f60SXin Li 
527*05b00f60SXin Li 	/* if any fields are missing */
528*05b00f60SXin Li 	if(!init || !icookie || !rcookie || !authkey || !enckey) {
529*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
530*05b00f60SXin Li 				    file, lineno);
531*05b00f60SXin Li 
532*05b00f60SXin Li 		return;
533*05b00f60SXin Li 	}
534*05b00f60SXin Li 
535*05b00f60SXin Li 	ilen = strlen(icookie);
536*05b00f60SXin Li 	rlen = strlen(rcookie);
537*05b00f60SXin Li 
538*05b00f60SXin Li 	if((init[0]!='I' && init[0]!='R')
539*05b00f60SXin Li 	   || icookie[0]!='0' || icookie[1]!='x'
540*05b00f60SXin Li 	   || rcookie[0]!='0' || rcookie[1]!='x'
541*05b00f60SXin Li 	   || ilen!=18
542*05b00f60SXin Li 	   || rlen!=18) {
543*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
544*05b00f60SXin Li 				    file, lineno);
545*05b00f60SXin Li 
546*05b00f60SXin Li 		(*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
547*05b00f60SXin Li 				    init, icookie, ilen, rcookie, rlen);
548*05b00f60SXin Li 
549*05b00f60SXin Li 		return;
550*05b00f60SXin Li 	}
551*05b00f60SXin Li 
552*05b00f60SXin Li 	sa1.spi = 0;
553*05b00f60SXin Li 	sa1.initiator = (init[0] == 'I');
554*05b00f60SXin Li 	if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
555*05b00f60SXin Li 		return;
556*05b00f60SXin Li 
557*05b00f60SXin Li 	if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
558*05b00f60SXin Li 		return;
559*05b00f60SXin Li 
560*05b00f60SXin Li 	if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
561*05b00f60SXin Li 
562*05b00f60SXin Li 	if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
563*05b00f60SXin Li 
564*05b00f60SXin Li 	esp_print_addsa(ndo, &sa1, FALSE);
565*05b00f60SXin Li }
566*05b00f60SXin Li 
567*05b00f60SXin Li /*
568*05b00f60SXin Li  *
569*05b00f60SXin Li  * special form: file /name
570*05b00f60SXin Li  * causes us to go read from this file instead.
571*05b00f60SXin Li  *
572*05b00f60SXin Li  */
esp_print_decode_onesecret(netdissect_options * ndo,char * line,const char * file,int lineno)573*05b00f60SXin Li static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
574*05b00f60SXin Li 				       const char *file, int lineno)
575*05b00f60SXin Li {
576*05b00f60SXin Li 	struct sa_list sa1;
577*05b00f60SXin Li 	int sa_def;
578*05b00f60SXin Li 
579*05b00f60SXin Li 	char *spikey;
580*05b00f60SXin Li 	char *decode;
581*05b00f60SXin Li 
582*05b00f60SXin Li 	spikey = strsep(&line, " \t");
583*05b00f60SXin Li 	sa_def = 0;
584*05b00f60SXin Li 	memset(&sa1, 0, sizeof(struct sa_list));
585*05b00f60SXin Li 
586*05b00f60SXin Li 	/* if there is only one token, then it is an algo:key token */
587*05b00f60SXin Li 	if (line == NULL) {
588*05b00f60SXin Li 		decode = spikey;
589*05b00f60SXin Li 		spikey = NULL;
590*05b00f60SXin Li 		/* sa1.daddr.version = 0; */
591*05b00f60SXin Li 		/* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
592*05b00f60SXin Li 		/* sa1.spi = 0; */
593*05b00f60SXin Li 		sa_def    = 1;
594*05b00f60SXin Li 	} else
595*05b00f60SXin Li 		decode = line;
596*05b00f60SXin Li 
597*05b00f60SXin Li 	if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
598*05b00f60SXin Li 		/* open file and read it */
599*05b00f60SXin Li 		FILE *secretfile;
600*05b00f60SXin Li 		char  fileline[1024];
601*05b00f60SXin Li 		int   subfile_lineno=0;
602*05b00f60SXin Li 		char  *nl;
603*05b00f60SXin Li 		char *filename = line;
604*05b00f60SXin Li 
605*05b00f60SXin Li 		secretfile = fopen(filename, FOPEN_READ_TXT);
606*05b00f60SXin Li 		if (secretfile == NULL) {
607*05b00f60SXin Li 			(*ndo->ndo_error)(ndo, S_ERR_ND_OPEN_FILE,
608*05b00f60SXin Li 					  "%s: can't open %s: %s\n",
609*05b00f60SXin Li 					  __func__, filename, strerror(errno));
610*05b00f60SXin Li 		}
611*05b00f60SXin Li 
612*05b00f60SXin Li 		while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
613*05b00f60SXin Li 			subfile_lineno++;
614*05b00f60SXin Li 			/* remove newline from the line */
615*05b00f60SXin Li 			nl = strchr(fileline, '\n');
616*05b00f60SXin Li 			if (nl)
617*05b00f60SXin Li 				*nl = '\0';
618*05b00f60SXin Li 			if (fileline[0] == '#') continue;
619*05b00f60SXin Li 			if (fileline[0] == '\0') continue;
620*05b00f60SXin Li 
621*05b00f60SXin Li 			esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
622*05b00f60SXin Li 		}
623*05b00f60SXin Li 		fclose(secretfile);
624*05b00f60SXin Li 
625*05b00f60SXin Li 		return;
626*05b00f60SXin Li 	}
627*05b00f60SXin Li 
628*05b00f60SXin Li 	if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
629*05b00f60SXin Li 		esp_print_decode_ikeline(ndo, line, file, lineno);
630*05b00f60SXin Li 		return;
631*05b00f60SXin Li 	}
632*05b00f60SXin Li 
633*05b00f60SXin Li 	if (spikey) {
634*05b00f60SXin Li 
635*05b00f60SXin Li 		char *spistr, *foo;
636*05b00f60SXin Li 		uint32_t spino;
637*05b00f60SXin Li 
638*05b00f60SXin Li 		spistr = strsep(&spikey, "@");
639*05b00f60SXin Li 		if (spistr == NULL) {
640*05b00f60SXin Li 			(*ndo->ndo_warning)(ndo, "print_esp: failed to find the @ token");
641*05b00f60SXin Li 			return;
642*05b00f60SXin Li 		}
643*05b00f60SXin Li 
644*05b00f60SXin Li 		spino = strtoul(spistr, &foo, 0);
645*05b00f60SXin Li 		if (spistr == foo || !spikey) {
646*05b00f60SXin Li 			(*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
647*05b00f60SXin Li 			return;
648*05b00f60SXin Li 		}
649*05b00f60SXin Li 
650*05b00f60SXin Li 		sa1.spi = spino;
651*05b00f60SXin Li 
652*05b00f60SXin Li 		if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
653*05b00f60SXin Li 			sa1.daddr_version = 6;
654*05b00f60SXin Li 		} else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
655*05b00f60SXin Li 			sa1.daddr_version = 4;
656*05b00f60SXin Li 		} else {
657*05b00f60SXin Li 			(*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
658*05b00f60SXin Li 			return;
659*05b00f60SXin Li 		}
660*05b00f60SXin Li 	}
661*05b00f60SXin Li 
662*05b00f60SXin Li 	if (decode) {
663*05b00f60SXin Li 		/* skip any blank spaces */
664*05b00f60SXin Li 		while (*decode == ' ' || *decode == '\t' || *decode == '\r' || *decode == '\n')
665*05b00f60SXin Li 			decode++;
666*05b00f60SXin Li 
667*05b00f60SXin Li 		if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
668*05b00f60SXin Li 			return;
669*05b00f60SXin Li 		}
670*05b00f60SXin Li 	}
671*05b00f60SXin Li 
672*05b00f60SXin Li 	esp_print_addsa(ndo, &sa1, sa_def);
673*05b00f60SXin Li }
674*05b00f60SXin Li 
675*05b00f60SXin Li DIAG_OFF_DEPRECATION
esp_init(netdissect_options * ndo _U_)676*05b00f60SXin Li static void esp_init(netdissect_options *ndo _U_)
677*05b00f60SXin Li {
678*05b00f60SXin Li 	/*
679*05b00f60SXin Li 	 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
680*05b00f60SXin Li 	 * we check whether it's undefined or it's less than the
681*05b00f60SXin Li 	 * value for 1.1.0.
682*05b00f60SXin Li 	 */
683*05b00f60SXin Li #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
684*05b00f60SXin Li 	OpenSSL_add_all_algorithms();
685*05b00f60SXin Li #endif
686*05b00f60SXin Li 	EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
687*05b00f60SXin Li }
688*05b00f60SXin Li DIAG_ON_DEPRECATION
689*05b00f60SXin Li 
esp_decodesecret_print(netdissect_options * ndo)690*05b00f60SXin Li void esp_decodesecret_print(netdissect_options *ndo)
691*05b00f60SXin Li {
692*05b00f60SXin Li 	char *line;
693*05b00f60SXin Li 	char *p;
694*05b00f60SXin Li 	static int initialized = 0;
695*05b00f60SXin Li 
696*05b00f60SXin Li 	if (!initialized) {
697*05b00f60SXin Li 		esp_init(ndo);
698*05b00f60SXin Li 		initialized = 1;
699*05b00f60SXin Li 	}
700*05b00f60SXin Li 
701*05b00f60SXin Li 	p = ndo->ndo_espsecret;
702*05b00f60SXin Li 
703*05b00f60SXin Li 	while (p && p[0] != '\0') {
704*05b00f60SXin Li 		/* pick out the first line or first thing until a comma */
705*05b00f60SXin Li 		if ((line = strsep(&p, "\n,")) == NULL) {
706*05b00f60SXin Li 			line = p;
707*05b00f60SXin Li 			p = NULL;
708*05b00f60SXin Li 		}
709*05b00f60SXin Li 
710*05b00f60SXin Li 		esp_print_decode_onesecret(ndo, line, "cmdline", 0);
711*05b00f60SXin Li 	}
712*05b00f60SXin Li 
713*05b00f60SXin Li 	ndo->ndo_espsecret = NULL;
714*05b00f60SXin Li }
715*05b00f60SXin Li 
716*05b00f60SXin Li #endif
717*05b00f60SXin Li 
718*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
719*05b00f60SXin Li #define USED_IF_LIBCRYPTO
720*05b00f60SXin Li #else
721*05b00f60SXin Li #define USED_IF_LIBCRYPTO _U_
722*05b00f60SXin Li #endif
723*05b00f60SXin Li 
724*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
725*05b00f60SXin Li DIAG_OFF_DEPRECATION
726*05b00f60SXin Li #endif
727*05b00f60SXin Li void
esp_print(netdissect_options * ndo,const u_char * bp,u_int length,const u_char * bp2 USED_IF_LIBCRYPTO,u_int ver USED_IF_LIBCRYPTO,int fragmented USED_IF_LIBCRYPTO,u_int ttl_hl USED_IF_LIBCRYPTO)728*05b00f60SXin Li esp_print(netdissect_options *ndo,
729*05b00f60SXin Li 	  const u_char *bp, u_int length,
730*05b00f60SXin Li 	  const u_char *bp2 USED_IF_LIBCRYPTO,
731*05b00f60SXin Li 	  u_int ver USED_IF_LIBCRYPTO,
732*05b00f60SXin Li 	  int fragmented USED_IF_LIBCRYPTO,
733*05b00f60SXin Li 	  u_int ttl_hl USED_IF_LIBCRYPTO)
734*05b00f60SXin Li {
735*05b00f60SXin Li 	const struct newesp *esp;
736*05b00f60SXin Li 	const u_char *ep;
737*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
738*05b00f60SXin Li 	const struct ip *ip;
739*05b00f60SXin Li 	struct sa_list *sa = NULL;
740*05b00f60SXin Li 	const struct ip6_hdr *ip6 = NULL;
741*05b00f60SXin Li 	const u_char *iv;
742*05b00f60SXin Li 	u_int ivlen;
743*05b00f60SXin Li 	u_int payloadlen;
744*05b00f60SXin Li 	const u_char *ct;
745*05b00f60SXin Li 	u_char *pt;
746*05b00f60SXin Li 	u_int padlen;
747*05b00f60SXin Li 	u_int nh;
748*05b00f60SXin Li #endif
749*05b00f60SXin Li 
750*05b00f60SXin Li 	ndo->ndo_protocol = "esp";
751*05b00f60SXin Li 	esp = (const struct newesp *)bp;
752*05b00f60SXin Li 
753*05b00f60SXin Li 	/* 'ep' points to the end of available data. */
754*05b00f60SXin Li 	ep = ndo->ndo_snapend;
755*05b00f60SXin Li 
756*05b00f60SXin Li 	if ((const u_char *)(esp + 1) >= ep) {
757*05b00f60SXin Li 		nd_print_trunc(ndo);
758*05b00f60SXin Li 		return;
759*05b00f60SXin Li 	}
760*05b00f60SXin Li 	ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp->esp_spi));
761*05b00f60SXin Li 	ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp->esp_seq));
762*05b00f60SXin Li 	ND_PRINT(", length %u", length);
763*05b00f60SXin Li 
764*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
765*05b00f60SXin Li 	/* initialize SAs */
766*05b00f60SXin Li 	if (ndo->ndo_sa_list_head == NULL) {
767*05b00f60SXin Li 		if (!ndo->ndo_espsecret)
768*05b00f60SXin Li 			return;
769*05b00f60SXin Li 
770*05b00f60SXin Li 		esp_decodesecret_print(ndo);
771*05b00f60SXin Li 	}
772*05b00f60SXin Li 
773*05b00f60SXin Li 	if (ndo->ndo_sa_list_head == NULL)
774*05b00f60SXin Li 		return;
775*05b00f60SXin Li 
776*05b00f60SXin Li 	ip = (const struct ip *)bp2;
777*05b00f60SXin Li 	switch (ver) {
778*05b00f60SXin Li 	case 6:
779*05b00f60SXin Li 		ip6 = (const struct ip6_hdr *)bp2;
780*05b00f60SXin Li 		/* we do not attempt to decrypt jumbograms */
781*05b00f60SXin Li 		if (!GET_BE_U_2(ip6->ip6_plen))
782*05b00f60SXin Li 			return;
783*05b00f60SXin Li 		/* XXX - check whether it's fragmented? */
784*05b00f60SXin Li 		/* if we can't get nexthdr, we do not need to decrypt it */
785*05b00f60SXin Li 
786*05b00f60SXin Li 		/* see if we can find the SA, and if so, decode it */
787*05b00f60SXin Li 		for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
788*05b00f60SXin Li 			if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
789*05b00f60SXin Li 			    sa->daddr_version == 6 &&
790*05b00f60SXin Li 			    UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
791*05b00f60SXin Li 				   sizeof(nd_ipv6)) == 0) {
792*05b00f60SXin Li 				break;
793*05b00f60SXin Li 			}
794*05b00f60SXin Li 		}
795*05b00f60SXin Li 		break;
796*05b00f60SXin Li 	case 4:
797*05b00f60SXin Li 		/* nexthdr & padding are in the last fragment */
798*05b00f60SXin Li 		if (fragmented)
799*05b00f60SXin Li 			return;
800*05b00f60SXin Li 
801*05b00f60SXin Li 		/* see if we can find the SA, and if so, decode it */
802*05b00f60SXin Li 		for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
803*05b00f60SXin Li 			if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
804*05b00f60SXin Li 			    sa->daddr_version == 4 &&
805*05b00f60SXin Li 			    UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
806*05b00f60SXin Li 				   sizeof(nd_ipv4)) == 0) {
807*05b00f60SXin Li 				break;
808*05b00f60SXin Li 			}
809*05b00f60SXin Li 		}
810*05b00f60SXin Li 		break;
811*05b00f60SXin Li 	default:
812*05b00f60SXin Li 		return;
813*05b00f60SXin Li 	}
814*05b00f60SXin Li 
815*05b00f60SXin Li 	/* if we didn't find the specific one, then look for
816*05b00f60SXin Li 	 * an unspecified one.
817*05b00f60SXin Li 	 */
818*05b00f60SXin Li 	if (sa == NULL)
819*05b00f60SXin Li 		sa = ndo->ndo_sa_default;
820*05b00f60SXin Li 
821*05b00f60SXin Li 	/* if not found fail */
822*05b00f60SXin Li 	if (sa == NULL)
823*05b00f60SXin Li 		return;
824*05b00f60SXin Li 
825*05b00f60SXin Li 	/* pointer to the IV, if there is one */
826*05b00f60SXin Li 	iv = (const u_char *)(esp + 1) + 0;
827*05b00f60SXin Li 	/* length of the IV, if there is one; 0, if there isn't */
828*05b00f60SXin Li 	ivlen = sa->ivlen;
829*05b00f60SXin Li 
830*05b00f60SXin Li 	/*
831*05b00f60SXin Li 	 * Get a pointer to the ciphertext.
832*05b00f60SXin Li 	 *
833*05b00f60SXin Li 	 * p points to the beginning of the payload, i.e. to the
834*05b00f60SXin Li 	 * initialization vector, so if we skip past the initialization
835*05b00f60SXin Li 	 * vector, it points to the beginning of the ciphertext.
836*05b00f60SXin Li 	 */
837*05b00f60SXin Li 	ct = iv + ivlen;
838*05b00f60SXin Li 
839*05b00f60SXin Li 	/*
840*05b00f60SXin Li 	 * Make sure the authentication data/integrity check value length
841*05b00f60SXin Li 	 * isn't bigger than the total amount of data available after
842*05b00f60SXin Li 	 * the ESP header and initialization vector is removed and,
843*05b00f60SXin Li 	 * if not, slice the authentication data/ICV off.
844*05b00f60SXin Li 	 */
845*05b00f60SXin Li 	if (ep - ct < sa->authlen) {
846*05b00f60SXin Li 		nd_print_trunc(ndo);
847*05b00f60SXin Li 		return;
848*05b00f60SXin Li 	}
849*05b00f60SXin Li 	ep = ep - sa->authlen;
850*05b00f60SXin Li 
851*05b00f60SXin Li 	/*
852*05b00f60SXin Li 	 * Calculate the length of the ciphertext.  ep points to
853*05b00f60SXin Li 	 * the beginning of the authentication data/integrity check
854*05b00f60SXin Li 	 * value, i.e. right past the end of the ciphertext;
855*05b00f60SXin Li 	 */
856*05b00f60SXin Li 	payloadlen = ep - ct;
857*05b00f60SXin Li 
858*05b00f60SXin Li 	if (sa->evp == NULL)
859*05b00f60SXin Li 		return;
860*05b00f60SXin Li 
861*05b00f60SXin Li 	/*
862*05b00f60SXin Li 	 * If the next header value is past the end of the available
863*05b00f60SXin Li 	 * data, we won't be able to fetch it once we've decrypted
864*05b00f60SXin Li 	 * the ciphertext, so there's no point in decrypting the data.
865*05b00f60SXin Li 	 *
866*05b00f60SXin Li 	 * Report it as truncation.
867*05b00f60SXin Li 	 */
868*05b00f60SXin Li 	if (!ND_TTEST_1(ep - 1)) {
869*05b00f60SXin Li 		nd_print_trunc(ndo);
870*05b00f60SXin Li 		return;
871*05b00f60SXin Li 	}
872*05b00f60SXin Li 
873*05b00f60SXin Li 	pt = do_decrypt(ndo, __func__, sa, iv, ct, payloadlen);
874*05b00f60SXin Li 	if (pt == NULL)
875*05b00f60SXin Li 		return;
876*05b00f60SXin Li 
877*05b00f60SXin Li 	/*
878*05b00f60SXin Li 	 * Switch to the output buffer for dissection, and
879*05b00f60SXin Li 	 * save it on the buffer stack so it can be freed.
880*05b00f60SXin Li 	 */
881*05b00f60SXin Li 	if (!nd_push_buffer(ndo, pt, pt, payloadlen)) {
882*05b00f60SXin Li 		free(pt);
883*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
884*05b00f60SXin Li 			"%s: can't push buffer on buffer stack", __func__);
885*05b00f60SXin Li 	}
886*05b00f60SXin Li 
887*05b00f60SXin Li 	/*
888*05b00f60SXin Li 	 * Sanity check for pad length; if it, plus 2 for the pad
889*05b00f60SXin Li 	 * length and next header fields, is bigger than the ciphertext
890*05b00f60SXin Li 	 * length (which is also the plaintext length), it's too big.
891*05b00f60SXin Li 	 *
892*05b00f60SXin Li 	 * XXX - the check can fail if the packet is corrupt *or* if
893*05b00f60SXin Li 	 * it was not decrypted with the correct key, so that the
894*05b00f60SXin Li 	 * "plaintext" is not what was being sent.
895*05b00f60SXin Li 	 */
896*05b00f60SXin Li 	padlen = GET_U_1(pt + payloadlen - 2);
897*05b00f60SXin Li 	if (padlen + 2 > payloadlen) {
898*05b00f60SXin Li 		nd_print_trunc(ndo);
899*05b00f60SXin Li 		return;
900*05b00f60SXin Li 	}
901*05b00f60SXin Li 
902*05b00f60SXin Li 	/* Get the next header */
903*05b00f60SXin Li 	nh = GET_U_1(pt + payloadlen - 1);
904*05b00f60SXin Li 
905*05b00f60SXin Li 	ND_PRINT(": ");
906*05b00f60SXin Li 
907*05b00f60SXin Li 	/*
908*05b00f60SXin Li 	 * Don't put padding + padding length(1 byte) + next header(1 byte)
909*05b00f60SXin Li 	 * in the buffer because they are not part of the plaintext to decode.
910*05b00f60SXin Li 	 */
911*05b00f60SXin Li 	if (!nd_push_snaplen(ndo, pt, payloadlen - (padlen + 2))) {
912*05b00f60SXin Li 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
913*05b00f60SXin Li 			"%s: can't push snaplen on buffer stack", __func__);
914*05b00f60SXin Li 	}
915*05b00f60SXin Li 
916*05b00f60SXin Li 	/* Now dissect the plaintext. */
917*05b00f60SXin Li 	ip_demux_print(ndo, pt, payloadlen - (padlen + 2), ver, fragmented,
918*05b00f60SXin Li 		       ttl_hl, nh, bp2);
919*05b00f60SXin Li 
920*05b00f60SXin Li 	/* Pop the buffer, freeing it. */
921*05b00f60SXin Li 	nd_pop_packet_info(ndo);
922*05b00f60SXin Li 	/* Pop the nd_push_snaplen */
923*05b00f60SXin Li 	nd_pop_packet_info(ndo);
924*05b00f60SXin Li #endif
925*05b00f60SXin Li }
926*05b00f60SXin Li #ifdef HAVE_LIBCRYPTO
927*05b00f60SXin Li DIAG_ON_DEPRECATION
928*05b00f60SXin Li #endif
929