141c99275SPeter Avalos /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
241c99275SPeter Avalos
341c99275SPeter Avalos /*
441c99275SPeter Avalos * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
541c99275SPeter Avalos * The Regents of the University of California. All rights reserved.
641c99275SPeter Avalos *
741c99275SPeter Avalos * Redistribution and use in source and binary forms, with or without
841c99275SPeter Avalos * modification, are permitted provided that: (1) source code distributions
941c99275SPeter Avalos * retain the above copyright notice and this paragraph in its entirety, (2)
1041c99275SPeter Avalos * distributions including binary code include the above copyright notice and
1141c99275SPeter Avalos * this paragraph in its entirety in the documentation or other materials
1241c99275SPeter Avalos * provided with the distribution, and (3) all advertising materials mentioning
1341c99275SPeter Avalos * features or use of this software display the following acknowledgement:
1441c99275SPeter Avalos * ``This product includes software developed by the University of California,
1541c99275SPeter Avalos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1641c99275SPeter Avalos * the University nor the names of its contributors may be used to endorse
1741c99275SPeter Avalos * or promote products derived from this software without specific prior
1841c99275SPeter Avalos * written permission.
1941c99275SPeter Avalos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
2041c99275SPeter Avalos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
2141c99275SPeter Avalos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2241c99275SPeter Avalos */
2341c99275SPeter Avalos
24411677aeSAaron LI /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
2541c99275SPeter Avalos
2641c99275SPeter Avalos #ifdef HAVE_CONFIG_H
27*ed775ee7SAntonio Huete Jimenez #include <config.h>
2841c99275SPeter Avalos #endif
2941c99275SPeter Avalos
30*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
31411677aeSAaron LI
3241c99275SPeter Avalos #include <string.h>
3341c99275SPeter Avalos #include <stdlib.h>
3441c99275SPeter Avalos
35411677aeSAaron LI /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
36411677aeSAaron LI * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
37411677aeSAaron LI * is the simplest way of handling the dependency.
38411677aeSAaron LI */
3941c99275SPeter Avalos #ifdef HAVE_LIBCRYPTO
4041c99275SPeter Avalos #ifdef HAVE_OPENSSL_EVP_H
4141c99275SPeter Avalos #include <openssl/evp.h>
42411677aeSAaron LI #else
43411677aeSAaron LI #undef HAVE_LIBCRYPTO
4441c99275SPeter Avalos #endif
4541c99275SPeter Avalos #endif
4641c99275SPeter Avalos
4741c99275SPeter Avalos #include "netdissect.h"
4841c99275SPeter Avalos #include "extract.h"
4941c99275SPeter Avalos
50*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_LIBCRYPTO
51*ed775ee7SAntonio Huete Jimenez #include "strtoaddr.h"
52411677aeSAaron LI #include "ascii_strcasecmp.h"
53*ed775ee7SAntonio Huete Jimenez #endif
54411677aeSAaron LI
55411677aeSAaron LI #include "ip.h"
56411677aeSAaron LI #include "ip6.h"
57411677aeSAaron LI
58411677aeSAaron LI /*
59411677aeSAaron LI * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
60411677aeSAaron LI * All rights reserved.
61411677aeSAaron LI *
62411677aeSAaron LI * Redistribution and use in source and binary forms, with or without
63411677aeSAaron LI * modification, are permitted provided that the following conditions
64411677aeSAaron LI * are met:
65411677aeSAaron LI * 1. Redistributions of source code must retain the above copyright
66411677aeSAaron LI * notice, this list of conditions and the following disclaimer.
67411677aeSAaron LI * 2. Redistributions in binary form must reproduce the above copyright
68411677aeSAaron LI * notice, this list of conditions and the following disclaimer in the
69411677aeSAaron LI * documentation and/or other materials provided with the distribution.
70411677aeSAaron LI * 3. Neither the name of the project nor the names of its contributors
71411677aeSAaron LI * may be used to endorse or promote products derived from this software
72411677aeSAaron LI * without specific prior written permission.
73411677aeSAaron LI *
74411677aeSAaron LI * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
75411677aeSAaron LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76411677aeSAaron LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77411677aeSAaron LI * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
78411677aeSAaron LI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79411677aeSAaron LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80411677aeSAaron LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81411677aeSAaron LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82411677aeSAaron LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83411677aeSAaron LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84411677aeSAaron LI * SUCH DAMAGE.
85411677aeSAaron LI */
86411677aeSAaron LI
87411677aeSAaron LI /*
88411677aeSAaron LI * RFC1827/2406 Encapsulated Security Payload.
89411677aeSAaron LI */
90411677aeSAaron LI
91411677aeSAaron LI struct newesp {
92*ed775ee7SAntonio Huete Jimenez nd_uint32_t esp_spi; /* ESP */
93*ed775ee7SAntonio Huete Jimenez nd_uint32_t esp_seq; /* Sequence number */
94411677aeSAaron LI /*variable size*/ /* (IV and) Payload data */
95411677aeSAaron LI /*variable size*/ /* padding */
96411677aeSAaron LI /*8bit*/ /* pad size */
97411677aeSAaron LI /*8bit*/ /* next header */
98411677aeSAaron LI /*8bit*/ /* next header */
99411677aeSAaron LI /*variable size, 32bit bound*/ /* Authentication data */
10041c99275SPeter Avalos };
10141c99275SPeter Avalos
10241c99275SPeter Avalos #ifdef HAVE_LIBCRYPTO
103411677aeSAaron LI union inaddr_u {
104*ed775ee7SAntonio Huete Jimenez nd_ipv4 in4;
105*ed775ee7SAntonio Huete Jimenez nd_ipv6 in6;
106411677aeSAaron LI };
10741c99275SPeter Avalos struct sa_list {
10841c99275SPeter Avalos struct sa_list *next;
109411677aeSAaron LI u_int daddr_version;
110411677aeSAaron LI union inaddr_u daddr;
111411677aeSAaron LI uint32_t spi; /* if == 0, then IKEv2 */
11227bfbee1SPeter Avalos int initiator;
11327bfbee1SPeter Avalos u_char spii[8]; /* for IKEv2 */
11427bfbee1SPeter Avalos u_char spir[8];
11541c99275SPeter Avalos const EVP_CIPHER *evp;
116*ed775ee7SAntonio Huete Jimenez u_int ivlen;
11741c99275SPeter Avalos int authlen;
11827bfbee1SPeter Avalos u_char authsecret[256];
11927bfbee1SPeter Avalos int authsecret_len;
12041c99275SPeter Avalos u_char secret[256]; /* is that big enough for all secrets? */
12141c99275SPeter Avalos int secretlen;
12241c99275SPeter Avalos };
12341c99275SPeter Avalos
124411677aeSAaron LI #ifndef HAVE_EVP_CIPHER_CTX_NEW
125411677aeSAaron LI /*
126411677aeSAaron LI * Allocate an EVP_CIPHER_CTX.
127411677aeSAaron LI * Used if we have an older version of OpenSSL that doesn't provide
128411677aeSAaron LI * routines to allocate and free them.
129411677aeSAaron LI */
130411677aeSAaron LI static EVP_CIPHER_CTX *
EVP_CIPHER_CTX_new(void)131411677aeSAaron LI EVP_CIPHER_CTX_new(void)
132411677aeSAaron LI {
133411677aeSAaron LI EVP_CIPHER_CTX *ctx;
134411677aeSAaron LI
135411677aeSAaron LI ctx = malloc(sizeof(*ctx));
136411677aeSAaron LI if (ctx == NULL)
137411677aeSAaron LI return (NULL);
138411677aeSAaron LI memset(ctx, 0, sizeof(*ctx));
139411677aeSAaron LI return (ctx);
140411677aeSAaron LI }
141411677aeSAaron LI
142411677aeSAaron LI static void
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX * ctx)143411677aeSAaron LI EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
144411677aeSAaron LI {
145411677aeSAaron LI EVP_CIPHER_CTX_cleanup(ctx);
146411677aeSAaron LI free(ctx);
147411677aeSAaron LI }
148411677aeSAaron LI #endif
149411677aeSAaron LI
150*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_EVP_DECRYPTINIT_EX
151411677aeSAaron LI /*
152*ed775ee7SAntonio Huete Jimenez * Initialize the cipher by calling EVP_DecryptInit_ex(), because
153*ed775ee7SAntonio Huete Jimenez * calling EVP_DecryptInit() will reset the cipher context, clearing
154411677aeSAaron LI * the cipher, so calling it twice, with the second call having a
155*ed775ee7SAntonio Huete Jimenez * null cipher, will clear the already-set cipher. EVP_DecryptInit_ex(),
156411677aeSAaron LI * however, won't reset the cipher context, so you can use it to specify
157*ed775ee7SAntonio Huete Jimenez * the IV in a second call after a first call to EVP_DecryptInit_ex()
158411677aeSAaron LI * to set the cipher and the key.
159411677aeSAaron LI *
160411677aeSAaron LI * XXX - is there some reason why we need to make two calls?
161411677aeSAaron LI */
162411677aeSAaron LI static int
set_cipher_parameters(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)163411677aeSAaron LI set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
164411677aeSAaron LI const unsigned char *key,
165*ed775ee7SAntonio Huete Jimenez const unsigned char *iv)
166411677aeSAaron LI {
167*ed775ee7SAntonio Huete Jimenez return EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
168411677aeSAaron LI }
169411677aeSAaron LI #else
170411677aeSAaron LI /*
171*ed775ee7SAntonio Huete Jimenez * Initialize the cipher by calling EVP_DecryptInit(), because we don't
172*ed775ee7SAntonio Huete Jimenez * have EVP_DecryptInit_ex(); we rely on it not trashing the context.
173411677aeSAaron LI */
174411677aeSAaron LI static int
set_cipher_parameters(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,const unsigned char * key,const unsigned char * iv)175411677aeSAaron LI set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
176411677aeSAaron LI const unsigned char *key,
177*ed775ee7SAntonio Huete Jimenez const unsigned char *iv)
178411677aeSAaron LI {
179*ed775ee7SAntonio Huete Jimenez return EVP_DecryptInit(ctx, cipher, key, iv);
180411677aeSAaron LI }
181411677aeSAaron LI #endif
182411677aeSAaron LI
183*ed775ee7SAntonio Huete Jimenez 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)184*ed775ee7SAntonio Huete Jimenez do_decrypt(netdissect_options *ndo, const char *caller, struct sa_list *sa,
185*ed775ee7SAntonio Huete Jimenez const u_char *iv, const u_char *ct, unsigned int ctlen)
186*ed775ee7SAntonio Huete Jimenez {
187*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX *ctx;
188*ed775ee7SAntonio Huete Jimenez unsigned int block_size;
189*ed775ee7SAntonio Huete Jimenez unsigned int ptlen;
190*ed775ee7SAntonio Huete Jimenez u_char *pt;
191*ed775ee7SAntonio Huete Jimenez int len;
192*ed775ee7SAntonio Huete Jimenez
193*ed775ee7SAntonio Huete Jimenez ctx = EVP_CIPHER_CTX_new();
194*ed775ee7SAntonio Huete Jimenez if (ctx == NULL) {
19527bfbee1SPeter Avalos /*
196*ed775ee7SAntonio Huete Jimenez * Failed to initialize the cipher context.
197*ed775ee7SAntonio Huete Jimenez * From a look at the OpenSSL code, this appears to
198*ed775ee7SAntonio Huete Jimenez * mean "couldn't allocate memory for the cipher context";
199*ed775ee7SAntonio Huete Jimenez * note that we're not passing any parameters, so there's
200*ed775ee7SAntonio Huete Jimenez * not much else it can mean.
201*ed775ee7SAntonio Huete Jimenez */
202*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
203*ed775ee7SAntonio Huete Jimenez "%s: can't allocate memory for cipher context", caller);
204*ed775ee7SAntonio Huete Jimenez return NULL;
205*ed775ee7SAntonio Huete Jimenez }
206*ed775ee7SAntonio Huete Jimenez
207*ed775ee7SAntonio Huete Jimenez if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL) < 0) {
208*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
209*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo, "%s: espkey init failed", caller);
210*ed775ee7SAntonio Huete Jimenez return NULL;
211*ed775ee7SAntonio Huete Jimenez }
212*ed775ee7SAntonio Huete Jimenez if (set_cipher_parameters(ctx, NULL, NULL, iv) < 0) {
213*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
214*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo, "%s: IV init failed", caller);
215*ed775ee7SAntonio Huete Jimenez return NULL;
216*ed775ee7SAntonio Huete Jimenez }
217*ed775ee7SAntonio Huete Jimenez
218*ed775ee7SAntonio Huete Jimenez /*
219*ed775ee7SAntonio Huete Jimenez * At least as I read RFC 5996 section 3.14 and RFC 4303 section 2.4,
220*ed775ee7SAntonio Huete Jimenez * if the cipher has a block size of which the ciphertext's size must
221*ed775ee7SAntonio Huete Jimenez * be a multiple, the payload must be padded to make that happen, so
222*ed775ee7SAntonio Huete Jimenez * the ciphertext length must be a multiple of the block size. Fail
223*ed775ee7SAntonio Huete Jimenez * if that's not the case.
224*ed775ee7SAntonio Huete Jimenez */
225*ed775ee7SAntonio Huete Jimenez block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
226*ed775ee7SAntonio Huete Jimenez if ((ctlen % block_size) != 0) {
227*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
228*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo,
229*ed775ee7SAntonio Huete Jimenez "%s: ciphertext size %u is not a multiple of the cipher block size %u",
230*ed775ee7SAntonio Huete Jimenez caller, ctlen, block_size);
231*ed775ee7SAntonio Huete Jimenez return NULL;
232*ed775ee7SAntonio Huete Jimenez }
233*ed775ee7SAntonio Huete Jimenez
234*ed775ee7SAntonio Huete Jimenez /*
235*ed775ee7SAntonio Huete Jimenez * Attempt to allocate a buffer for the decrypted data, because
236*ed775ee7SAntonio Huete Jimenez * we can't decrypt on top of the input buffer.
237*ed775ee7SAntonio Huete Jimenez */
238*ed775ee7SAntonio Huete Jimenez ptlen = ctlen;
239*ed775ee7SAntonio Huete Jimenez pt = (u_char *)malloc(ptlen);
240*ed775ee7SAntonio Huete Jimenez if (pt == NULL) {
241*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
242*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
243*ed775ee7SAntonio Huete Jimenez "%s: can't allocate memory for decryption buffer", caller);
244*ed775ee7SAntonio Huete Jimenez return NULL;
245*ed775ee7SAntonio Huete Jimenez }
246*ed775ee7SAntonio Huete Jimenez
247*ed775ee7SAntonio Huete Jimenez /*
248*ed775ee7SAntonio Huete Jimenez * The size of the ciphertext handed to us is a multiple of the
249*ed775ee7SAntonio Huete Jimenez * cipher block size, so we don't need to worry about padding.
250*ed775ee7SAntonio Huete Jimenez */
251*ed775ee7SAntonio Huete Jimenez if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) {
252*ed775ee7SAntonio Huete Jimenez free(pt);
253*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
254*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo,
255*ed775ee7SAntonio Huete Jimenez "%s: EVP_CIPHER_CTX_set_padding failed", caller);
256*ed775ee7SAntonio Huete Jimenez return NULL;
257*ed775ee7SAntonio Huete Jimenez }
258*ed775ee7SAntonio Huete Jimenez if (!EVP_DecryptUpdate(ctx, pt, &len, ct, ctlen)) {
259*ed775ee7SAntonio Huete Jimenez free(pt);
260*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
261*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo, "%s: EVP_DecryptUpdate failed",
262*ed775ee7SAntonio Huete Jimenez caller);
263*ed775ee7SAntonio Huete Jimenez return NULL;
264*ed775ee7SAntonio Huete Jimenez }
265*ed775ee7SAntonio Huete Jimenez EVP_CIPHER_CTX_free(ctx);
266*ed775ee7SAntonio Huete Jimenez return pt;
267*ed775ee7SAntonio Huete Jimenez }
268*ed775ee7SAntonio Huete Jimenez
269*ed775ee7SAntonio Huete Jimenez /*
270*ed775ee7SAntonio Huete Jimenez * This will allocate a new buffer containing the decrypted data.
271*ed775ee7SAntonio Huete Jimenez * It returns 1 on success and 0 on failure.
272*ed775ee7SAntonio Huete Jimenez *
273*ed775ee7SAntonio Huete Jimenez * It will push the new buffer and the values of ndo->ndo_packetp and
274*ed775ee7SAntonio Huete Jimenez * ndo->ndo_snapend onto the buffer stack, and change ndo->ndo_packetp
275*ed775ee7SAntonio Huete Jimenez * and ndo->ndo_snapend to refer to the new buffer.
276*ed775ee7SAntonio Huete Jimenez *
277*ed775ee7SAntonio Huete Jimenez * Our caller must pop the buffer off the stack when it's finished
278*ed775ee7SAntonio Huete Jimenez * dissecting anything in it and before it does any dissection of
279*ed775ee7SAntonio Huete Jimenez * anything in the old buffer. That will free the new buffer.
28027bfbee1SPeter Avalos */
281411677aeSAaron LI USES_APPLE_DEPRECATED_API
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)282*ed775ee7SAntonio Huete Jimenez int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo,
28327bfbee1SPeter Avalos int initiator,
284*ed775ee7SAntonio Huete Jimenez const u_char spii[8],
285*ed775ee7SAntonio Huete Jimenez const u_char spir[8],
286411677aeSAaron LI const u_char *buf, const u_char *end)
28727bfbee1SPeter Avalos {
28827bfbee1SPeter Avalos struct sa_list *sa;
289411677aeSAaron LI const u_char *iv;
290*ed775ee7SAntonio Huete Jimenez const u_char *ct;
291*ed775ee7SAntonio Huete Jimenez unsigned int ctlen;
292*ed775ee7SAntonio Huete Jimenez u_char *pt;
29327bfbee1SPeter Avalos
29427bfbee1SPeter Avalos /* initiator arg is any non-zero value */
29527bfbee1SPeter Avalos if(initiator) initiator=1;
29627bfbee1SPeter Avalos
29727bfbee1SPeter Avalos /* see if we can find the SA, and if so, decode it */
29827bfbee1SPeter Avalos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
29927bfbee1SPeter Avalos if (sa->spi == 0
30027bfbee1SPeter Avalos && initiator == sa->initiator
30127bfbee1SPeter Avalos && memcmp(spii, sa->spii, 8) == 0
30227bfbee1SPeter Avalos && memcmp(spir, sa->spir, 8) == 0)
30327bfbee1SPeter Avalos break;
30427bfbee1SPeter Avalos }
30527bfbee1SPeter Avalos
30627bfbee1SPeter Avalos if(sa == NULL) return 0;
30727bfbee1SPeter Avalos if(sa->evp == NULL) return 0;
30827bfbee1SPeter Avalos
30927bfbee1SPeter Avalos /*
31027bfbee1SPeter Avalos * remove authenticator, and see if we still have something to
31127bfbee1SPeter Avalos * work with
31227bfbee1SPeter Avalos */
31327bfbee1SPeter Avalos end = end - sa->authlen;
31427bfbee1SPeter Avalos iv = buf;
315*ed775ee7SAntonio Huete Jimenez ct = iv + sa->ivlen;
316*ed775ee7SAntonio Huete Jimenez ctlen = end-ct;
31727bfbee1SPeter Avalos
318*ed775ee7SAntonio Huete Jimenez if(end <= ct) return 0;
31927bfbee1SPeter Avalos
320*ed775ee7SAntonio Huete Jimenez pt = do_decrypt(ndo, "esp_decrypt_buffer_by_ikev2_print", sa, iv,
321*ed775ee7SAntonio Huete Jimenez ct, ctlen);
322*ed775ee7SAntonio Huete Jimenez if (pt == NULL)
323411677aeSAaron LI return 0;
324411677aeSAaron LI
325411677aeSAaron LI /*
326*ed775ee7SAntonio Huete Jimenez * Switch to the output buffer for dissection, and save it
327*ed775ee7SAntonio Huete Jimenez * on the buffer stack so it can be freed; our caller must
328*ed775ee7SAntonio Huete Jimenez * pop it when done.
329411677aeSAaron LI */
330*ed775ee7SAntonio Huete Jimenez if (!nd_push_buffer(ndo, pt, pt, pt + ctlen)) {
331*ed775ee7SAntonio Huete Jimenez free(pt);
332*ed775ee7SAntonio Huete Jimenez return 0;
333411677aeSAaron LI }
33427bfbee1SPeter Avalos
33527bfbee1SPeter Avalos return 1;
33627bfbee1SPeter Avalos }
337411677aeSAaron LI USES_APPLE_RST
33827bfbee1SPeter Avalos
esp_print_addsa(netdissect_options * ndo,struct sa_list * sa,int sa_def)33941c99275SPeter Avalos static void esp_print_addsa(netdissect_options *ndo,
34041c99275SPeter Avalos struct sa_list *sa, int sa_def)
34141c99275SPeter Avalos {
34241c99275SPeter Avalos /* copy the "sa" */
34341c99275SPeter Avalos
34441c99275SPeter Avalos struct sa_list *nsa;
34541c99275SPeter Avalos
346*ed775ee7SAntonio Huete Jimenez /* malloc() return used in a 'struct sa_list': do not free() */
34741c99275SPeter Avalos nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
34841c99275SPeter Avalos if (nsa == NULL)
349*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
350*ed775ee7SAntonio Huete Jimenez "%s: malloc", __func__);
35141c99275SPeter Avalos
35241c99275SPeter Avalos *nsa = *sa;
35341c99275SPeter Avalos
35441c99275SPeter Avalos if (sa_def)
35541c99275SPeter Avalos ndo->ndo_sa_default = nsa;
35641c99275SPeter Avalos
35741c99275SPeter Avalos nsa->next = ndo->ndo_sa_list_head;
35841c99275SPeter Avalos ndo->ndo_sa_list_head = nsa;
35941c99275SPeter Avalos }
36041c99275SPeter Avalos
36141c99275SPeter Avalos
hexdigit(netdissect_options * ndo,char hex)36241c99275SPeter Avalos static u_int hexdigit(netdissect_options *ndo, char hex)
36341c99275SPeter Avalos {
36441c99275SPeter Avalos if (hex >= '0' && hex <= '9')
36541c99275SPeter Avalos return (hex - '0');
36641c99275SPeter Avalos else if (hex >= 'A' && hex <= 'F')
36741c99275SPeter Avalos return (hex - 'A' + 10);
36841c99275SPeter Avalos else if (hex >= 'a' && hex <= 'f')
36941c99275SPeter Avalos return (hex - 'a' + 10);
37041c99275SPeter Avalos else {
371*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_ESP_SECRET,
372*ed775ee7SAntonio Huete Jimenez "invalid hex digit %c in espsecret\n", hex);
37341c99275SPeter Avalos }
37441c99275SPeter Avalos }
37541c99275SPeter Avalos
hex2byte(netdissect_options * ndo,char * hexstring)37641c99275SPeter Avalos static u_int hex2byte(netdissect_options *ndo, char *hexstring)
37741c99275SPeter Avalos {
37841c99275SPeter Avalos u_int byte;
37941c99275SPeter Avalos
38041c99275SPeter Avalos byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
38141c99275SPeter Avalos return byte;
38241c99275SPeter Avalos }
38341c99275SPeter Avalos
38441c99275SPeter Avalos /*
38527bfbee1SPeter Avalos * returns size of binary, 0 on failure.
38627bfbee1SPeter Avalos */
38727bfbee1SPeter Avalos static
espprint_decode_hex(netdissect_options * ndo,u_char * binbuf,unsigned int binbuf_len,char * hex)38827bfbee1SPeter Avalos int espprint_decode_hex(netdissect_options *ndo,
38927bfbee1SPeter Avalos u_char *binbuf, unsigned int binbuf_len,
39027bfbee1SPeter Avalos char *hex)
39127bfbee1SPeter Avalos {
39227bfbee1SPeter Avalos unsigned int len;
39327bfbee1SPeter Avalos int i;
39427bfbee1SPeter Avalos
39527bfbee1SPeter Avalos len = strlen(hex) / 2;
39627bfbee1SPeter Avalos
39727bfbee1SPeter Avalos if (len > binbuf_len) {
398*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_warning)(ndo, "secret is too big: %u\n", len);
39927bfbee1SPeter Avalos return 0;
40027bfbee1SPeter Avalos }
40127bfbee1SPeter Avalos
40227bfbee1SPeter Avalos i = 0;
40327bfbee1SPeter Avalos while (hex[0] != '\0' && hex[1]!='\0') {
40427bfbee1SPeter Avalos binbuf[i] = hex2byte(ndo, hex);
40527bfbee1SPeter Avalos hex += 2;
40627bfbee1SPeter Avalos i++;
40727bfbee1SPeter Avalos }
40827bfbee1SPeter Avalos
40927bfbee1SPeter Avalos return i;
41027bfbee1SPeter Avalos }
41127bfbee1SPeter Avalos
41227bfbee1SPeter Avalos /*
41341c99275SPeter Avalos * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
41427bfbee1SPeter Avalos */
41527bfbee1SPeter Avalos
416411677aeSAaron LI USES_APPLE_DEPRECATED_API
41727bfbee1SPeter Avalos static int
espprint_decode_encalgo(netdissect_options * ndo,char * decode,struct sa_list * sa)41827bfbee1SPeter Avalos espprint_decode_encalgo(netdissect_options *ndo,
41927bfbee1SPeter Avalos char *decode, struct sa_list *sa)
42027bfbee1SPeter Avalos {
42127bfbee1SPeter Avalos size_t i;
42227bfbee1SPeter Avalos const EVP_CIPHER *evp;
42327bfbee1SPeter Avalos int authlen = 0;
42427bfbee1SPeter Avalos char *colon, *p;
42527bfbee1SPeter Avalos
42627bfbee1SPeter Avalos colon = strchr(decode, ':');
42727bfbee1SPeter Avalos if (colon == NULL) {
42827bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
42927bfbee1SPeter Avalos return 0;
43027bfbee1SPeter Avalos }
43127bfbee1SPeter Avalos *colon = '\0';
43227bfbee1SPeter Avalos
43327bfbee1SPeter Avalos if (strlen(decode) > strlen("-hmac96") &&
43427bfbee1SPeter Avalos !strcmp(decode + strlen(decode) - strlen("-hmac96"),
43527bfbee1SPeter Avalos "-hmac96")) {
43627bfbee1SPeter Avalos p = strstr(decode, "-hmac96");
43727bfbee1SPeter Avalos *p = '\0';
43827bfbee1SPeter Avalos authlen = 12;
43927bfbee1SPeter Avalos }
44027bfbee1SPeter Avalos if (strlen(decode) > strlen("-cbc") &&
44127bfbee1SPeter Avalos !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
44227bfbee1SPeter Avalos p = strstr(decode, "-cbc");
44327bfbee1SPeter Avalos *p = '\0';
44427bfbee1SPeter Avalos }
44527bfbee1SPeter Avalos evp = EVP_get_cipherbyname(decode);
44627bfbee1SPeter Avalos
44727bfbee1SPeter Avalos if (!evp) {
44827bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
44927bfbee1SPeter Avalos sa->evp = NULL;
45027bfbee1SPeter Avalos sa->authlen = 0;
45127bfbee1SPeter Avalos sa->ivlen = 0;
45227bfbee1SPeter Avalos return 0;
45327bfbee1SPeter Avalos }
45427bfbee1SPeter Avalos
45527bfbee1SPeter Avalos sa->evp = evp;
45627bfbee1SPeter Avalos sa->authlen = authlen;
457*ed775ee7SAntonio Huete Jimenez /* This returns an int, but it should never be negative */
45827bfbee1SPeter Avalos sa->ivlen = EVP_CIPHER_iv_length(evp);
45927bfbee1SPeter Avalos
46027bfbee1SPeter Avalos colon++;
46127bfbee1SPeter Avalos if (colon[0] == '0' && colon[1] == 'x') {
46227bfbee1SPeter Avalos /* decode some hex! */
46327bfbee1SPeter Avalos
46427bfbee1SPeter Avalos colon += 2;
46527bfbee1SPeter Avalos sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
46627bfbee1SPeter Avalos if(sa->secretlen == 0) return 0;
46727bfbee1SPeter Avalos } else {
46827bfbee1SPeter Avalos i = strlen(colon);
46927bfbee1SPeter Avalos
47027bfbee1SPeter Avalos if (i < sizeof(sa->secret)) {
47127bfbee1SPeter Avalos memcpy(sa->secret, colon, i);
47227bfbee1SPeter Avalos sa->secretlen = i;
47327bfbee1SPeter Avalos } else {
47427bfbee1SPeter Avalos memcpy(sa->secret, colon, sizeof(sa->secret));
47527bfbee1SPeter Avalos sa->secretlen = sizeof(sa->secret);
47627bfbee1SPeter Avalos }
47727bfbee1SPeter Avalos }
47827bfbee1SPeter Avalos
47927bfbee1SPeter Avalos return 1;
48027bfbee1SPeter Avalos }
481411677aeSAaron LI USES_APPLE_RST
48227bfbee1SPeter Avalos
48327bfbee1SPeter Avalos /*
484411677aeSAaron LI * for the moment, ignore the auth algorithm, just hard code the authenticator
48527bfbee1SPeter Avalos * length. Need to research how openssl looks up HMAC stuff.
48627bfbee1SPeter Avalos */
48727bfbee1SPeter Avalos static int
espprint_decode_authalgo(netdissect_options * ndo,char * decode,struct sa_list * sa)48827bfbee1SPeter Avalos espprint_decode_authalgo(netdissect_options *ndo,
48927bfbee1SPeter Avalos char *decode, struct sa_list *sa)
49027bfbee1SPeter Avalos {
49127bfbee1SPeter Avalos char *colon;
49227bfbee1SPeter Avalos
49327bfbee1SPeter Avalos colon = strchr(decode, ':');
49427bfbee1SPeter Avalos if (colon == NULL) {
49527bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
49627bfbee1SPeter Avalos return 0;
49727bfbee1SPeter Avalos }
49827bfbee1SPeter Avalos *colon = '\0';
49927bfbee1SPeter Avalos
500*ed775ee7SAntonio Huete Jimenez if(ascii_strcasecmp(decode,"sha1") == 0 ||
501*ed775ee7SAntonio Huete Jimenez ascii_strcasecmp(decode,"md5") == 0) {
50227bfbee1SPeter Avalos sa->authlen = 12;
50327bfbee1SPeter Avalos }
50427bfbee1SPeter Avalos return 1;
50527bfbee1SPeter Avalos }
50627bfbee1SPeter Avalos
esp_print_decode_ikeline(netdissect_options * ndo,char * line,const char * file,int lineno)50727bfbee1SPeter Avalos static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
50827bfbee1SPeter Avalos const char *file, int lineno)
50927bfbee1SPeter Avalos {
51027bfbee1SPeter Avalos /* it's an IKEv2 secret, store it instead */
51127bfbee1SPeter Avalos struct sa_list sa1;
51227bfbee1SPeter Avalos
51327bfbee1SPeter Avalos char *init;
51427bfbee1SPeter Avalos char *icookie, *rcookie;
51527bfbee1SPeter Avalos int ilen, rlen;
51627bfbee1SPeter Avalos char *authkey;
51727bfbee1SPeter Avalos char *enckey;
51827bfbee1SPeter Avalos
51927bfbee1SPeter Avalos init = strsep(&line, " \t");
52027bfbee1SPeter Avalos icookie = strsep(&line, " \t");
52127bfbee1SPeter Avalos rcookie = strsep(&line, " \t");
52227bfbee1SPeter Avalos authkey = strsep(&line, " \t");
52327bfbee1SPeter Avalos enckey = strsep(&line, " \t");
52427bfbee1SPeter Avalos
52527bfbee1SPeter Avalos /* if any fields are missing */
52627bfbee1SPeter Avalos if(!init || !icookie || !rcookie || !authkey || !enckey) {
52727bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
52827bfbee1SPeter Avalos file, lineno);
52927bfbee1SPeter Avalos
53027bfbee1SPeter Avalos return;
53127bfbee1SPeter Avalos }
53227bfbee1SPeter Avalos
53327bfbee1SPeter Avalos ilen = strlen(icookie);
53427bfbee1SPeter Avalos rlen = strlen(rcookie);
53527bfbee1SPeter Avalos
53627bfbee1SPeter Avalos if((init[0]!='I' && init[0]!='R')
53727bfbee1SPeter Avalos || icookie[0]!='0' || icookie[1]!='x'
53827bfbee1SPeter Avalos || rcookie[0]!='0' || rcookie[1]!='x'
53927bfbee1SPeter Avalos || ilen!=18
54027bfbee1SPeter Avalos || rlen!=18) {
54127bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
54227bfbee1SPeter Avalos file, lineno);
54327bfbee1SPeter Avalos
54427bfbee1SPeter Avalos (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
54527bfbee1SPeter Avalos init, icookie, ilen, rcookie, rlen);
54627bfbee1SPeter Avalos
54727bfbee1SPeter Avalos return;
54827bfbee1SPeter Avalos }
54927bfbee1SPeter Avalos
55027bfbee1SPeter Avalos sa1.spi = 0;
55127bfbee1SPeter Avalos sa1.initiator = (init[0] == 'I');
55227bfbee1SPeter Avalos if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
55327bfbee1SPeter Avalos return;
55427bfbee1SPeter Avalos
55527bfbee1SPeter Avalos if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
55627bfbee1SPeter Avalos return;
55727bfbee1SPeter Avalos
55827bfbee1SPeter Avalos if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
55927bfbee1SPeter Avalos
56027bfbee1SPeter Avalos if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
56127bfbee1SPeter Avalos
56227bfbee1SPeter Avalos esp_print_addsa(ndo, &sa1, FALSE);
56327bfbee1SPeter Avalos }
56427bfbee1SPeter Avalos
56527bfbee1SPeter Avalos /*
56641c99275SPeter Avalos *
56741c99275SPeter Avalos * special form: file /name
56841c99275SPeter Avalos * causes us to go read from this file instead.
56941c99275SPeter Avalos *
57041c99275SPeter Avalos */
esp_print_decode_onesecret(netdissect_options * ndo,char * line,const char * file,int lineno)57127bfbee1SPeter Avalos static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
57227bfbee1SPeter Avalos const char *file, int lineno)
57341c99275SPeter Avalos {
57441c99275SPeter Avalos struct sa_list sa1;
57541c99275SPeter Avalos int sa_def;
57641c99275SPeter Avalos
57741c99275SPeter Avalos char *spikey;
57841c99275SPeter Avalos char *decode;
57941c99275SPeter Avalos
58041c99275SPeter Avalos spikey = strsep(&line, " \t");
58141c99275SPeter Avalos sa_def = 0;
58241c99275SPeter Avalos memset(&sa1, 0, sizeof(struct sa_list));
58341c99275SPeter Avalos
58441c99275SPeter Avalos /* if there is only one token, then it is an algo:key token */
58541c99275SPeter Avalos if (line == NULL) {
58641c99275SPeter Avalos decode = spikey;
58741c99275SPeter Avalos spikey = NULL;
588411677aeSAaron LI /* sa1.daddr.version = 0; */
58941c99275SPeter Avalos /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
59041c99275SPeter Avalos /* sa1.spi = 0; */
59141c99275SPeter Avalos sa_def = 1;
59241c99275SPeter Avalos } else
59341c99275SPeter Avalos decode = line;
59441c99275SPeter Avalos
595411677aeSAaron LI if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
59641c99275SPeter Avalos /* open file and read it */
59741c99275SPeter Avalos FILE *secretfile;
59841c99275SPeter Avalos char fileline[1024];
599411677aeSAaron LI int subfile_lineno=0;
60041c99275SPeter Avalos char *nl;
60127bfbee1SPeter Avalos char *filename = line;
60241c99275SPeter Avalos
60327bfbee1SPeter Avalos secretfile = fopen(filename, FOPEN_READ_TXT);
60441c99275SPeter Avalos if (secretfile == NULL) {
605*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_OPEN_FILE,
606*ed775ee7SAntonio Huete Jimenez "%s: can't open %s: %s\n",
607*ed775ee7SAntonio Huete Jimenez __func__, filename, strerror(errno));
60841c99275SPeter Avalos }
60941c99275SPeter Avalos
61041c99275SPeter Avalos while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
611411677aeSAaron LI subfile_lineno++;
61241c99275SPeter Avalos /* remove newline from the line */
61341c99275SPeter Avalos nl = strchr(fileline, '\n');
61441c99275SPeter Avalos if (nl)
61541c99275SPeter Avalos *nl = '\0';
61641c99275SPeter Avalos if (fileline[0] == '#') continue;
61741c99275SPeter Avalos if (fileline[0] == '\0') continue;
61841c99275SPeter Avalos
619411677aeSAaron LI esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
62041c99275SPeter Avalos }
62141c99275SPeter Avalos fclose(secretfile);
62241c99275SPeter Avalos
62341c99275SPeter Avalos return;
62441c99275SPeter Avalos }
62541c99275SPeter Avalos
626411677aeSAaron LI if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
62727bfbee1SPeter Avalos esp_print_decode_ikeline(ndo, line, file, lineno);
62827bfbee1SPeter Avalos return;
62927bfbee1SPeter Avalos }
63027bfbee1SPeter Avalos
63141c99275SPeter Avalos if (spikey) {
63227bfbee1SPeter Avalos
63341c99275SPeter Avalos char *spistr, *foo;
634411677aeSAaron LI uint32_t spino;
63541c99275SPeter Avalos
63641c99275SPeter Avalos spistr = strsep(&spikey, "@");
637411677aeSAaron LI if (spistr == NULL) {
638411677aeSAaron LI (*ndo->ndo_warning)(ndo, "print_esp: failed to find the @ token");
639411677aeSAaron LI return;
640411677aeSAaron LI }
64141c99275SPeter Avalos
64241c99275SPeter Avalos spino = strtoul(spistr, &foo, 0);
64341c99275SPeter Avalos if (spistr == foo || !spikey) {
64441c99275SPeter Avalos (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
64541c99275SPeter Avalos return;
64641c99275SPeter Avalos }
64741c99275SPeter Avalos
64841c99275SPeter Avalos sa1.spi = spino;
64941c99275SPeter Avalos
650411677aeSAaron LI if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
651411677aeSAaron LI sa1.daddr_version = 6;
652411677aeSAaron LI } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
653411677aeSAaron LI sa1.daddr_version = 4;
65441c99275SPeter Avalos } else {
65541c99275SPeter Avalos (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
65641c99275SPeter Avalos return;
65741c99275SPeter Avalos }
65841c99275SPeter Avalos }
65941c99275SPeter Avalos
66041c99275SPeter Avalos if (decode) {
66141c99275SPeter Avalos /* skip any blank spaces */
662*ed775ee7SAntonio Huete Jimenez while (*decode == ' ' || *decode == '\t' || *decode == '\r' || *decode == '\n')
66341c99275SPeter Avalos decode++;
66441c99275SPeter Avalos
66527bfbee1SPeter Avalos if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
66641c99275SPeter Avalos return;
66741c99275SPeter Avalos }
66841c99275SPeter Avalos }
66941c99275SPeter Avalos
67041c99275SPeter Avalos esp_print_addsa(ndo, &sa1, sa_def);
67141c99275SPeter Avalos }
67241c99275SPeter Avalos
673411677aeSAaron LI USES_APPLE_DEPRECATED_API
esp_init(netdissect_options * ndo _U_)67441c99275SPeter Avalos static void esp_init(netdissect_options *ndo _U_)
67541c99275SPeter Avalos {
676411677aeSAaron LI /*
677411677aeSAaron LI * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
678411677aeSAaron LI * we check whether it's undefined or it's less than the
679411677aeSAaron LI * value for 1.1.0.
680411677aeSAaron LI */
681411677aeSAaron LI #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
68241c99275SPeter Avalos OpenSSL_add_all_algorithms();
683411677aeSAaron LI #endif
68441c99275SPeter Avalos EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
68541c99275SPeter Avalos }
686411677aeSAaron LI USES_APPLE_RST
68727bfbee1SPeter Avalos
esp_decodesecret_print(netdissect_options * ndo)688*ed775ee7SAntonio Huete Jimenez void esp_decodesecret_print(netdissect_options *ndo)
68927bfbee1SPeter Avalos {
69027bfbee1SPeter Avalos char *line;
69127bfbee1SPeter Avalos char *p;
69227bfbee1SPeter Avalos static int initialized = 0;
69327bfbee1SPeter Avalos
69427bfbee1SPeter Avalos if (!initialized) {
69527bfbee1SPeter Avalos esp_init(ndo);
69627bfbee1SPeter Avalos initialized = 1;
69727bfbee1SPeter Avalos }
69827bfbee1SPeter Avalos
69927bfbee1SPeter Avalos p = ndo->ndo_espsecret;
70027bfbee1SPeter Avalos
70127bfbee1SPeter Avalos while (p && p[0] != '\0') {
70227bfbee1SPeter Avalos /* pick out the first line or first thing until a comma */
70327bfbee1SPeter Avalos if ((line = strsep(&p, "\n,")) == NULL) {
70427bfbee1SPeter Avalos line = p;
70527bfbee1SPeter Avalos p = NULL;
70627bfbee1SPeter Avalos }
70727bfbee1SPeter Avalos
70827bfbee1SPeter Avalos esp_print_decode_onesecret(ndo, line, "cmdline", 0);
70927bfbee1SPeter Avalos }
71027bfbee1SPeter Avalos
71127bfbee1SPeter Avalos ndo->ndo_espsecret = NULL;
71227bfbee1SPeter Avalos }
71327bfbee1SPeter Avalos
71441c99275SPeter Avalos #endif
71541c99275SPeter Avalos
716411677aeSAaron LI #ifdef HAVE_LIBCRYPTO
717*ed775ee7SAntonio Huete Jimenez #define USED_IF_LIBCRYPTO
718*ed775ee7SAntonio Huete Jimenez #else
719*ed775ee7SAntonio Huete Jimenez #define USED_IF_LIBCRYPTO _U_
720*ed775ee7SAntonio Huete Jimenez #endif
721*ed775ee7SAntonio Huete Jimenez
722*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_LIBCRYPTO
723411677aeSAaron LI USES_APPLE_DEPRECATED_API
724411677aeSAaron LI #endif
725*ed775ee7SAntonio Huete Jimenez 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)72641c99275SPeter Avalos esp_print(netdissect_options *ndo,
727*ed775ee7SAntonio Huete Jimenez const u_char *bp, u_int length,
728*ed775ee7SAntonio Huete Jimenez const u_char *bp2 USED_IF_LIBCRYPTO,
729*ed775ee7SAntonio Huete Jimenez u_int ver USED_IF_LIBCRYPTO,
730*ed775ee7SAntonio Huete Jimenez int fragmented USED_IF_LIBCRYPTO,
731*ed775ee7SAntonio Huete Jimenez u_int ttl_hl USED_IF_LIBCRYPTO)
73241c99275SPeter Avalos {
733*ed775ee7SAntonio Huete Jimenez const struct newesp *esp;
734*ed775ee7SAntonio Huete Jimenez const u_char *ep;
73541c99275SPeter Avalos #ifdef HAVE_LIBCRYPTO
736411677aeSAaron LI const struct ip *ip;
73741c99275SPeter Avalos struct sa_list *sa = NULL;
738411677aeSAaron LI const struct ip6_hdr *ip6 = NULL;
739*ed775ee7SAntonio Huete Jimenez const u_char *iv;
740*ed775ee7SAntonio Huete Jimenez u_int ivlen;
741*ed775ee7SAntonio Huete Jimenez u_int payloadlen;
742*ed775ee7SAntonio Huete Jimenez const u_char *ct;
743*ed775ee7SAntonio Huete Jimenez u_char *pt;
744*ed775ee7SAntonio Huete Jimenez u_int padlen;
745*ed775ee7SAntonio Huete Jimenez u_int nh;
74641c99275SPeter Avalos #endif
74741c99275SPeter Avalos
748*ed775ee7SAntonio Huete Jimenez ndo->ndo_protocol = "esp";
749411677aeSAaron LI esp = (const struct newesp *)bp;
75041c99275SPeter Avalos
75141c99275SPeter Avalos /* 'ep' points to the end of available data. */
75241c99275SPeter Avalos ep = ndo->ndo_snapend;
75341c99275SPeter Avalos
754411677aeSAaron LI if ((const u_char *)(esp + 1) >= ep) {
755*ed775ee7SAntonio Huete Jimenez nd_print_trunc(ndo);
756*ed775ee7SAntonio Huete Jimenez return;
75741c99275SPeter Avalos }
758*ed775ee7SAntonio Huete Jimenez ND_PRINT("ESP(spi=0x%08x", GET_BE_U_4(esp->esp_spi));
759*ed775ee7SAntonio Huete Jimenez ND_PRINT(",seq=0x%x)", GET_BE_U_4(esp->esp_seq));
760*ed775ee7SAntonio Huete Jimenez ND_PRINT(", length %u", length);
76141c99275SPeter Avalos
762*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_LIBCRYPTO
76341c99275SPeter Avalos /* initiailize SAs */
76441c99275SPeter Avalos if (ndo->ndo_sa_list_head == NULL) {
76541c99275SPeter Avalos if (!ndo->ndo_espsecret)
766*ed775ee7SAntonio Huete Jimenez return;
76741c99275SPeter Avalos
768*ed775ee7SAntonio Huete Jimenez esp_decodesecret_print(ndo);
76941c99275SPeter Avalos }
77041c99275SPeter Avalos
77141c99275SPeter Avalos if (ndo->ndo_sa_list_head == NULL)
772*ed775ee7SAntonio Huete Jimenez return;
77341c99275SPeter Avalos
774411677aeSAaron LI ip = (const struct ip *)bp2;
775*ed775ee7SAntonio Huete Jimenez switch (ver) {
77641c99275SPeter Avalos case 6:
777411677aeSAaron LI ip6 = (const struct ip6_hdr *)bp2;
77841c99275SPeter Avalos /* we do not attempt to decrypt jumbograms */
779*ed775ee7SAntonio Huete Jimenez if (!GET_BE_U_2(ip6->ip6_plen))
780*ed775ee7SAntonio Huete Jimenez return;
781*ed775ee7SAntonio Huete Jimenez /* XXX - check whether it's fragmented? */
78241c99275SPeter Avalos /* if we can't get nexthdr, we do not need to decrypt it */
78341c99275SPeter Avalos
78441c99275SPeter Avalos /* see if we can find the SA, and if so, decode it */
78541c99275SPeter Avalos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
786*ed775ee7SAntonio Huete Jimenez if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
787411677aeSAaron LI sa->daddr_version == 6 &&
788411677aeSAaron LI UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
789*ed775ee7SAntonio Huete Jimenez sizeof(nd_ipv6)) == 0) {
79041c99275SPeter Avalos break;
79141c99275SPeter Avalos }
79241c99275SPeter Avalos }
79341c99275SPeter Avalos break;
79441c99275SPeter Avalos case 4:
79541c99275SPeter Avalos /* nexthdr & padding are in the last fragment */
796*ed775ee7SAntonio Huete Jimenez if (fragmented)
797*ed775ee7SAntonio Huete Jimenez return;
79841c99275SPeter Avalos
79941c99275SPeter Avalos /* see if we can find the SA, and if so, decode it */
80041c99275SPeter Avalos for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
801*ed775ee7SAntonio Huete Jimenez if (sa->spi == GET_BE_U_4(esp->esp_spi) &&
802411677aeSAaron LI sa->daddr_version == 4 &&
803411677aeSAaron LI UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
804*ed775ee7SAntonio Huete Jimenez sizeof(nd_ipv4)) == 0) {
80541c99275SPeter Avalos break;
80641c99275SPeter Avalos }
80741c99275SPeter Avalos }
80841c99275SPeter Avalos break;
80941c99275SPeter Avalos default:
810*ed775ee7SAntonio Huete Jimenez return;
81141c99275SPeter Avalos }
81241c99275SPeter Avalos
81341c99275SPeter Avalos /* if we didn't find the specific one, then look for
81441c99275SPeter Avalos * an unspecified one.
81541c99275SPeter Avalos */
81641c99275SPeter Avalos if (sa == NULL)
81741c99275SPeter Avalos sa = ndo->ndo_sa_default;
81841c99275SPeter Avalos
81941c99275SPeter Avalos /* if not found fail */
82041c99275SPeter Avalos if (sa == NULL)
821*ed775ee7SAntonio Huete Jimenez return;
82241c99275SPeter Avalos
823411677aeSAaron LI /* pointer to the IV, if there is one */
824*ed775ee7SAntonio Huete Jimenez iv = (const u_char *)(esp + 1) + 0;
825411677aeSAaron LI /* length of the IV, if there is one; 0, if there isn't */
82641c99275SPeter Avalos ivlen = sa->ivlen;
827*ed775ee7SAntonio Huete Jimenez
828*ed775ee7SAntonio Huete Jimenez /*
829*ed775ee7SAntonio Huete Jimenez * Get a pointer to the ciphertext.
830*ed775ee7SAntonio Huete Jimenez *
831*ed775ee7SAntonio Huete Jimenez * p points to the beginning of the payload, i.e. to the
832*ed775ee7SAntonio Huete Jimenez * initialization vector, so if we skip past the initialization
833*ed775ee7SAntonio Huete Jimenez * vector, it points to the beginning of the ciphertext.
834*ed775ee7SAntonio Huete Jimenez */
835*ed775ee7SAntonio Huete Jimenez ct = iv + ivlen;
836*ed775ee7SAntonio Huete Jimenez
837*ed775ee7SAntonio Huete Jimenez /*
838*ed775ee7SAntonio Huete Jimenez * Make sure the authentication data/integrity check value length
839*ed775ee7SAntonio Huete Jimenez * isn't bigger than the total amount of data available after
840*ed775ee7SAntonio Huete Jimenez * the ESP header and initialization vector is removed and,
841*ed775ee7SAntonio Huete Jimenez * if not, slice the authentication data/ICV off.
842*ed775ee7SAntonio Huete Jimenez */
843*ed775ee7SAntonio Huete Jimenez if (ep - ct < sa->authlen) {
844*ed775ee7SAntonio Huete Jimenez nd_print_trunc(ndo);
845*ed775ee7SAntonio Huete Jimenez return;
846*ed775ee7SAntonio Huete Jimenez }
84741c99275SPeter Avalos ep = ep - sa->authlen;
84841c99275SPeter Avalos
849*ed775ee7SAntonio Huete Jimenez /*
850*ed775ee7SAntonio Huete Jimenez * Calculate the length of the ciphertext. ep points to
851*ed775ee7SAntonio Huete Jimenez * the beginning of the authentication data/integrity check
852*ed775ee7SAntonio Huete Jimenez * value, i.e. right past the end of the ciphertext;
853*ed775ee7SAntonio Huete Jimenez */
854*ed775ee7SAntonio Huete Jimenez payloadlen = ep - ct;
85541c99275SPeter Avalos
856*ed775ee7SAntonio Huete Jimenez if (sa->evp == NULL)
857*ed775ee7SAntonio Huete Jimenez return;
858411677aeSAaron LI
859411677aeSAaron LI /*
860*ed775ee7SAntonio Huete Jimenez * If the next header value is past the end of the available
861*ed775ee7SAntonio Huete Jimenez * data, we won't be able to fetch it once we've decrypted
862*ed775ee7SAntonio Huete Jimenez * the ciphertext, so there's no point in decrypting the data.
863*ed775ee7SAntonio Huete Jimenez *
864*ed775ee7SAntonio Huete Jimenez * Report it as truncation.
865411677aeSAaron LI */
866*ed775ee7SAntonio Huete Jimenez if (!ND_TTEST_1(ep - 1)) {
867*ed775ee7SAntonio Huete Jimenez nd_print_trunc(ndo);
868*ed775ee7SAntonio Huete Jimenez return;
869411677aeSAaron LI }
870411677aeSAaron LI
871*ed775ee7SAntonio Huete Jimenez pt = do_decrypt(ndo, "esp_print", sa, iv, ct, payloadlen);
872*ed775ee7SAntonio Huete Jimenez if (pt == NULL)
873*ed775ee7SAntonio Huete Jimenez return;
874*ed775ee7SAntonio Huete Jimenez
875411677aeSAaron LI /*
876*ed775ee7SAntonio Huete Jimenez * Switch to the output buffer for dissection, and
877*ed775ee7SAntonio Huete Jimenez * save it on the buffer stack so it can be freed.
878411677aeSAaron LI */
879*ed775ee7SAntonio Huete Jimenez ep = pt + payloadlen;
880*ed775ee7SAntonio Huete Jimenez if (!nd_push_buffer(ndo, pt, pt, ep)) {
881*ed775ee7SAntonio Huete Jimenez free(pt);
882*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
883*ed775ee7SAntonio Huete Jimenez "%s: can't push buffer on buffer stack", __func__);
884*ed775ee7SAntonio Huete Jimenez }
88541c99275SPeter Avalos
886*ed775ee7SAntonio Huete Jimenez /*
887*ed775ee7SAntonio Huete Jimenez * Sanity check for pad length; if it, plus 2 for the pad
888*ed775ee7SAntonio Huete Jimenez * length and next header fields, is bigger than the ciphertext
889*ed775ee7SAntonio Huete Jimenez * length (which is also the plaintext length), it's too big.
890*ed775ee7SAntonio Huete Jimenez *
891*ed775ee7SAntonio Huete Jimenez * XXX - the check can fail if the packet is corrupt *or* if
892*ed775ee7SAntonio Huete Jimenez * it was not decrypted with the correct key, so that the
893*ed775ee7SAntonio Huete Jimenez * "plaintext" is not what was being sent.
894*ed775ee7SAntonio Huete Jimenez */
895*ed775ee7SAntonio Huete Jimenez padlen = GET_U_1(ep - 2);
896*ed775ee7SAntonio Huete Jimenez if (padlen + 2 > payloadlen) {
897*ed775ee7SAntonio Huete Jimenez nd_print_trunc(ndo);
898*ed775ee7SAntonio Huete Jimenez return;
899*ed775ee7SAntonio Huete Jimenez }
90041c99275SPeter Avalos
901*ed775ee7SAntonio Huete Jimenez /* Get the next header */
902*ed775ee7SAntonio Huete Jimenez nh = GET_U_1(ep - 1);
90341c99275SPeter Avalos
904*ed775ee7SAntonio Huete Jimenez ND_PRINT(": ");
90541c99275SPeter Avalos
906*ed775ee7SAntonio Huete Jimenez /*
907*ed775ee7SAntonio Huete Jimenez * Don't put padding + padding length(1 byte) + next header(1 byte)
908*ed775ee7SAntonio Huete Jimenez * in the buffer because they are not part of the plaintext to decode.
909*ed775ee7SAntonio Huete Jimenez */
910*ed775ee7SAntonio Huete Jimenez nd_push_snapend(ndo, ep - (padlen + 2));
911*ed775ee7SAntonio Huete Jimenez
912*ed775ee7SAntonio Huete Jimenez /* Now dissect the plaintext. */
913*ed775ee7SAntonio Huete Jimenez ip_demux_print(ndo, pt, payloadlen - (padlen + 2), ver, fragmented,
914*ed775ee7SAntonio Huete Jimenez ttl_hl, nh, bp2);
915*ed775ee7SAntonio Huete Jimenez
916*ed775ee7SAntonio Huete Jimenez /* Pop the buffer, freeing it. */
917*ed775ee7SAntonio Huete Jimenez nd_pop_packet_info(ndo);
918*ed775ee7SAntonio Huete Jimenez /* Pop the nd_push_snapend */
919*ed775ee7SAntonio Huete Jimenez nd_pop_packet_info(ndo);
92041c99275SPeter Avalos #endif
92141c99275SPeter Avalos }
922411677aeSAaron LI #ifdef HAVE_LIBCRYPTO
923411677aeSAaron LI USES_APPLE_RST
924411677aeSAaron LI #endif
925