blob: d12b97daf10421d9bb3d71a6fcc946d3d0a5f840 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
2
3/*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
Elliott Hughese2e3bd12017-05-15 10:59:29 -070024/* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
25
The Android Open Source Project2949f582009-03-03 19:30:46 -080026#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
Elliott Hughese2e3bd12017-05-15 10:59:29 -070030#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080031
Elliott Hughes892a68b2015-10-19 14:43:53 -070032#include <string.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080033#include <stdlib.h>
34
JP Abgrall53f17a92014-02-12 14:02:41 -080035/* Any code in this file that depends on HAVE_LIBCRYPTO depends on
36 * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
37 * is the simplest way of handling the dependency.
38 */
The Android Open Source Project2949f582009-03-03 19:30:46 -080039#ifdef HAVE_LIBCRYPTO
40#ifdef HAVE_OPENSSL_EVP_H
41#include <openssl/evp.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080042#else
43#undef HAVE_LIBCRYPTO
The Android Open Source Project2949f582009-03-03 19:30:46 -080044#endif
45#endif
46
Elliott Hughese2e3bd12017-05-15 10:59:29 -070047#include "netdissect.h"
48#include "strtoaddr.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080049#include "extract.h"
50
Elliott Hughese2e3bd12017-05-15 10:59:29 -070051#include "ascii_strcasecmp.h"
52
53#include "ip.h"
54#include "ip6.h"
55
Elliott Hughes892a68b2015-10-19 14:43:53 -070056/*
57 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
58 * All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 * 3. Neither the name of the project nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
71 *
72 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82 * SUCH DAMAGE.
83 */
84
85/*
86 * RFC1827/2406 Encapsulated Security Payload.
87 */
88
89struct newesp {
90 uint32_t esp_spi; /* ESP */
91 uint32_t esp_seq; /* Sequence number */
92 /*variable size*/ /* (IV and) Payload data */
93 /*variable size*/ /* padding */
94 /*8bit*/ /* pad size */
95 /*8bit*/ /* next header */
96 /*8bit*/ /* next header */
97 /*variable size, 32bit bound*/ /* Authentication data */
The Android Open Source Project2949f582009-03-03 19:30:46 -080098};
The Android Open Source Project2949f582009-03-03 19:30:46 -080099
100#ifdef HAVE_LIBCRYPTO
Elliott Hughes892a68b2015-10-19 14:43:53 -0700101union inaddr_u {
102 struct in_addr in4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700103 struct in6_addr in6;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700104};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800105struct sa_list {
106 struct sa_list *next;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700107 u_int daddr_version;
108 union inaddr_u daddr;
109 uint32_t spi; /* if == 0, then IKEv2 */
JP Abgrall53f17a92014-02-12 14:02:41 -0800110 int initiator;
111 u_char spii[8]; /* for IKEv2 */
112 u_char spir[8];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800113 const EVP_CIPHER *evp;
114 int ivlen;
115 int authlen;
JP Abgrall53f17a92014-02-12 14:02:41 -0800116 u_char authsecret[256];
117 int authsecret_len;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800118 u_char secret[256]; /* is that big enough for all secrets? */
119 int secretlen;
120};
121
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700122#ifndef HAVE_EVP_CIPHER_CTX_NEW
123/*
124 * Allocate an EVP_CIPHER_CTX.
125 * Used if we have an older version of OpenSSL that doesn't provide
126 * routines to allocate and free them.
127 */
128static EVP_CIPHER_CTX *
129EVP_CIPHER_CTX_new(void)
130{
131 EVP_CIPHER_CTX *ctx;
132
133 ctx = malloc(sizeof(*ctx));
134 if (ctx == NULL)
135 return (NULL);
136 memset(ctx, 0, sizeof(*ctx));
137 return (ctx);
138}
139
140static void
141EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
142{
143 EVP_CIPHER_CTX_cleanup(ctx);
144 free(ctx);
145}
146#endif
147
Elliott Hughescec480a2017-12-19 16:54:57 -0800148#ifdef HAVE_EVP_CIPHERINIT_EX
149/*
150 * Initialize the cipher by calling EVP_CipherInit_ex(), because
151 * calling EVP_CipherInit() will reset the cipher context, clearing
152 * the cipher, so calling it twice, with the second call having a
153 * null cipher, will clear the already-set cipher. EVP_CipherInit_ex(),
154 * however, won't reset the cipher context, so you can use it to specify
155 * the IV oin a second call after a first call to EVP_CipherInit_ex()
156 * to set the cipher and the key.
157 *
158 * XXX - is there some reason why we need to make two calls?
159 */
160static int
161set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
162 const unsigned char *key,
163 const unsigned char *iv, int enc)
164{
165 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
166}
167#else
168/*
169 * Initialize the cipher by calling EVP_CipherInit(), because we don't
170 * have EVP_CipherInit_ex(); we rely on it not trashing the context.
171 */
172static int
173set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
174 const unsigned char *key,
175 const unsigned char *iv, int enc)
176{
177 return EVP_CipherInit(ctx, cipher, key, iv, enc);
178}
179#endif
180
JP Abgrall53f17a92014-02-12 14:02:41 -0800181/*
182 * this will adjust ndo_packetp and ndo_snapend to new buffer!
183 */
184USES_APPLE_DEPRECATED_API
185int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
186 int initiator,
187 u_char spii[8], u_char spir[8],
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700188 const u_char *buf, const u_char *end)
JP Abgrall53f17a92014-02-12 14:02:41 -0800189{
190 struct sa_list *sa;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700191 const u_char *iv;
Elliott Hughescec480a2017-12-19 16:54:57 -0800192 unsigned int len;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700193 EVP_CIPHER_CTX *ctx;
Elliott Hughescec480a2017-12-19 16:54:57 -0800194 unsigned int block_size, output_buffer_size;
195 u_char *output_buffer;
JP Abgrall53f17a92014-02-12 14:02:41 -0800196
197 /* initiator arg is any non-zero value */
198 if(initiator) initiator=1;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700199
JP Abgrall53f17a92014-02-12 14:02:41 -0800200 /* see if we can find the SA, and if so, decode it */
201 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
202 if (sa->spi == 0
203 && initiator == sa->initiator
204 && memcmp(spii, sa->spii, 8) == 0
205 && memcmp(spir, sa->spir, 8) == 0)
206 break;
207 }
208
209 if(sa == NULL) return 0;
210 if(sa->evp == NULL) return 0;
211
212 /*
213 * remove authenticator, and see if we still have something to
214 * work with
215 */
216 end = end - sa->authlen;
217 iv = buf;
218 buf = buf + sa->ivlen;
219 len = end-buf;
220
221 if(end <= buf) return 0;
222
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700223 ctx = EVP_CIPHER_CTX_new();
224 if (ctx == NULL)
225 return 0;
Elliott Hughescec480a2017-12-19 16:54:57 -0800226 if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL, 0) < 0)
JP Abgrall53f17a92014-02-12 14:02:41 -0800227 (*ndo->ndo_warning)(ndo, "espkey init failed");
Elliott Hughescec480a2017-12-19 16:54:57 -0800228 set_cipher_parameters(ctx, NULL, NULL, iv, 0);
229 /*
230 * Allocate a buffer for the decrypted data.
231 * The output buffer must be separate from the input buffer, and
232 * its size must be a multiple of the cipher block size.
233 */
234 block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
235 output_buffer_size = len + (block_size - len % block_size);
236 output_buffer = (u_char *)malloc(output_buffer_size);
237 if (output_buffer == NULL) {
238 (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
239 EVP_CIPHER_CTX_free(ctx);
240 return 0;
241 }
242 EVP_Cipher(ctx, output_buffer, buf, len);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700243 EVP_CIPHER_CTX_free(ctx);
JP Abgrall53f17a92014-02-12 14:02:41 -0800244
Elliott Hughescec480a2017-12-19 16:54:57 -0800245 /*
246 * XXX - of course this is wrong, because buf is a const buffer,
247 * but changing this would require a more complicated fix.
248 */
249 memcpy(buf, output_buffer, len);
250 free(output_buffer);
251
JP Abgrall53f17a92014-02-12 14:02:41 -0800252 ndo->ndo_packetp = buf;
253 ndo->ndo_snapend = end;
254
255 return 1;
JP Abgrall53f17a92014-02-12 14:02:41 -0800256}
257USES_APPLE_RST
258
The Android Open Source Project2949f582009-03-03 19:30:46 -0800259static void esp_print_addsa(netdissect_options *ndo,
260 struct sa_list *sa, int sa_def)
261{
262 /* copy the "sa" */
263
264 struct sa_list *nsa;
265
266 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
267 if (nsa == NULL)
268 (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
269
270 *nsa = *sa;
271
272 if (sa_def)
273 ndo->ndo_sa_default = nsa;
274
275 nsa->next = ndo->ndo_sa_list_head;
276 ndo->ndo_sa_list_head = nsa;
277}
278
279
280static u_int hexdigit(netdissect_options *ndo, char hex)
281{
282 if (hex >= '0' && hex <= '9')
283 return (hex - '0');
284 else if (hex >= 'A' && hex <= 'F')
285 return (hex - 'A' + 10);
286 else if (hex >= 'a' && hex <= 'f')
287 return (hex - 'a' + 10);
288 else {
289 (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
290 return 0;
291 }
292}
293
294static u_int hex2byte(netdissect_options *ndo, char *hexstring)
295{
296 u_int byte;
297
298 byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
299 return byte;
300}
301
302/*
JP Abgrall53f17a92014-02-12 14:02:41 -0800303 * returns size of binary, 0 on failure.
304 */
305static
306int espprint_decode_hex(netdissect_options *ndo,
307 u_char *binbuf, unsigned int binbuf_len,
308 char *hex)
309{
310 unsigned int len;
311 int i;
312
313 len = strlen(hex) / 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700314
JP Abgrall53f17a92014-02-12 14:02:41 -0800315 if (len > binbuf_len) {
316 (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
317 return 0;
318 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700319
JP Abgrall53f17a92014-02-12 14:02:41 -0800320 i = 0;
321 while (hex[0] != '\0' && hex[1]!='\0') {
322 binbuf[i] = hex2byte(ndo, hex);
323 hex += 2;
324 i++;
325 }
326
327 return i;
328}
329
330/*
The Android Open Source Project2949f582009-03-03 19:30:46 -0800331 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
JP Abgrall53f17a92014-02-12 14:02:41 -0800332 */
333
334USES_APPLE_DEPRECATED_API
335static int
336espprint_decode_encalgo(netdissect_options *ndo,
337 char *decode, struct sa_list *sa)
338{
339 size_t i;
340 const EVP_CIPHER *evp;
341 int authlen = 0;
342 char *colon, *p;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700343
JP Abgrall53f17a92014-02-12 14:02:41 -0800344 colon = strchr(decode, ':');
345 if (colon == NULL) {
346 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
347 return 0;
348 }
349 *colon = '\0';
Elliott Hughes892a68b2015-10-19 14:43:53 -0700350
JP Abgrall53f17a92014-02-12 14:02:41 -0800351 if (strlen(decode) > strlen("-hmac96") &&
352 !strcmp(decode + strlen(decode) - strlen("-hmac96"),
353 "-hmac96")) {
354 p = strstr(decode, "-hmac96");
355 *p = '\0';
356 authlen = 12;
357 }
358 if (strlen(decode) > strlen("-cbc") &&
359 !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
360 p = strstr(decode, "-cbc");
361 *p = '\0';
362 }
363 evp = EVP_get_cipherbyname(decode);
364
365 if (!evp) {
366 (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
367 sa->evp = NULL;
368 sa->authlen = 0;
369 sa->ivlen = 0;
370 return 0;
371 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700372
JP Abgrall53f17a92014-02-12 14:02:41 -0800373 sa->evp = evp;
374 sa->authlen = authlen;
375 sa->ivlen = EVP_CIPHER_iv_length(evp);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700376
JP Abgrall53f17a92014-02-12 14:02:41 -0800377 colon++;
378 if (colon[0] == '0' && colon[1] == 'x') {
379 /* decode some hex! */
380
381 colon += 2;
382 sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
383 if(sa->secretlen == 0) return 0;
384 } else {
385 i = strlen(colon);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700386
JP Abgrall53f17a92014-02-12 14:02:41 -0800387 if (i < sizeof(sa->secret)) {
388 memcpy(sa->secret, colon, i);
389 sa->secretlen = i;
390 } else {
391 memcpy(sa->secret, colon, sizeof(sa->secret));
392 sa->secretlen = sizeof(sa->secret);
393 }
394 }
395
396 return 1;
397}
398USES_APPLE_RST
399
400/*
401 * for the moment, ignore the auth algorith, just hard code the authenticator
402 * length. Need to research how openssl looks up HMAC stuff.
403 */
404static int
405espprint_decode_authalgo(netdissect_options *ndo,
406 char *decode, struct sa_list *sa)
407{
408 char *colon;
409
410 colon = strchr(decode, ':');
411 if (colon == NULL) {
412 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
413 return 0;
414 }
415 *colon = '\0';
Elliott Hughes892a68b2015-10-19 14:43:53 -0700416
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700417 if(ascii_strcasecmp(colon,"sha1") == 0 ||
418 ascii_strcasecmp(colon,"md5") == 0) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800419 sa->authlen = 12;
420 }
421 return 1;
422}
423
424static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
425 const char *file, int lineno)
426{
427 /* it's an IKEv2 secret, store it instead */
428 struct sa_list sa1;
429
430 char *init;
431 char *icookie, *rcookie;
432 int ilen, rlen;
433 char *authkey;
434 char *enckey;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700435
JP Abgrall53f17a92014-02-12 14:02:41 -0800436 init = strsep(&line, " \t");
437 icookie = strsep(&line, " \t");
438 rcookie = strsep(&line, " \t");
439 authkey = strsep(&line, " \t");
440 enckey = strsep(&line, " \t");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700441
JP Abgrall53f17a92014-02-12 14:02:41 -0800442 /* if any fields are missing */
443 if(!init || !icookie || !rcookie || !authkey || !enckey) {
444 (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
445 file, lineno);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700446
JP Abgrall53f17a92014-02-12 14:02:41 -0800447 return;
448 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700449
JP Abgrall53f17a92014-02-12 14:02:41 -0800450 ilen = strlen(icookie);
451 rlen = strlen(rcookie);
452
453 if((init[0]!='I' && init[0]!='R')
454 || icookie[0]!='0' || icookie[1]!='x'
455 || rcookie[0]!='0' || rcookie[1]!='x'
456 || ilen!=18
457 || rlen!=18) {
458 (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
459 file, lineno);
460
461 (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
462 init, icookie, ilen, rcookie, rlen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700463
JP Abgrall53f17a92014-02-12 14:02:41 -0800464 return;
465 }
466
467 sa1.spi = 0;
468 sa1.initiator = (init[0] == 'I');
469 if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
470 return;
471
472 if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
473 return;
474
475 if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
476
477 if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700478
JP Abgrall53f17a92014-02-12 14:02:41 -0800479 esp_print_addsa(ndo, &sa1, FALSE);
480}
481
482/*
The Android Open Source Project2949f582009-03-03 19:30:46 -0800483 *
484 * special form: file /name
485 * causes us to go read from this file instead.
486 *
487 */
JP Abgrall53f17a92014-02-12 14:02:41 -0800488static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
489 const char *file, int lineno)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800490{
491 struct sa_list sa1;
492 int sa_def;
493
494 char *spikey;
495 char *decode;
496
497 spikey = strsep(&line, " \t");
498 sa_def = 0;
499 memset(&sa1, 0, sizeof(struct sa_list));
500
501 /* if there is only one token, then it is an algo:key token */
502 if (line == NULL) {
503 decode = spikey;
504 spikey = NULL;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700505 /* sa1.daddr.version = 0; */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800506 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
507 /* sa1.spi = 0; */
508 sa_def = 1;
509 } else
510 decode = line;
511
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700512 if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800513 /* open file and read it */
514 FILE *secretfile;
515 char fileline[1024];
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700516 int subfile_lineno=0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800517 char *nl;
JP Abgrall53f17a92014-02-12 14:02:41 -0800518 char *filename = line;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800519
JP Abgrall53f17a92014-02-12 14:02:41 -0800520 secretfile = fopen(filename, FOPEN_READ_TXT);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800521 if (secretfile == NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700522 (*ndo->ndo_error)(ndo, "print_esp: can't open %s: %s\n",
523 filename, strerror(errno));
524 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800525 }
526
527 while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700528 subfile_lineno++;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800529 /* remove newline from the line */
530 nl = strchr(fileline, '\n');
531 if (nl)
532 *nl = '\0';
533 if (fileline[0] == '#') continue;
534 if (fileline[0] == '\0') continue;
535
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700536 esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800537 }
538 fclose(secretfile);
539
540 return;
541 }
542
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700543 if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800544 esp_print_decode_ikeline(ndo, line, file, lineno);
545 return;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700546 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800547
The Android Open Source Project2949f582009-03-03 19:30:46 -0800548 if (spikey) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700549
The Android Open Source Project2949f582009-03-03 19:30:46 -0800550 char *spistr, *foo;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700551 uint32_t spino;
552
The Android Open Source Project2949f582009-03-03 19:30:46 -0800553 spistr = strsep(&spikey, "@");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700554
The Android Open Source Project2949f582009-03-03 19:30:46 -0800555 spino = strtoul(spistr, &foo, 0);
556 if (spistr == foo || !spikey) {
557 (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
558 return;
559 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700560
The Android Open Source Project2949f582009-03-03 19:30:46 -0800561 sa1.spi = spino;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700562
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700563 if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700564 sa1.daddr_version = 6;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700565 } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
566 sa1.daddr_version = 4;
567 } else {
568 (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
569 return;
570 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800571 }
572
573 if (decode) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800574 /* skip any blank spaces */
575 while (isspace((unsigned char)*decode))
576 decode++;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700577
JP Abgrall53f17a92014-02-12 14:02:41 -0800578 if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800579 return;
580 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800581 }
582
583 esp_print_addsa(ndo, &sa1, sa_def);
584}
585
JP Abgrall53f17a92014-02-12 14:02:41 -0800586USES_APPLE_DEPRECATED_API
The Android Open Source Project2949f582009-03-03 19:30:46 -0800587static void esp_init(netdissect_options *ndo _U_)
588{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700589 /*
590 * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
591 * we check whether it's undefined or it's less than the
592 * value for 1.1.0.
593 */
594#if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
The Android Open Source Project2949f582009-03-03 19:30:46 -0800595 OpenSSL_add_all_algorithms();
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700596#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800597 EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
598}
JP Abgrall53f17a92014-02-12 14:02:41 -0800599USES_APPLE_RST
600
601void esp_print_decodesecret(netdissect_options *ndo)
602{
603 char *line;
604 char *p;
605 static int initialized = 0;
606
607 if (!initialized) {
608 esp_init(ndo);
609 initialized = 1;
610 }
611
612 p = ndo->ndo_espsecret;
613
614 while (p && p[0] != '\0') {
615 /* pick out the first line or first thing until a comma */
616 if ((line = strsep(&p, "\n,")) == NULL) {
617 line = p;
618 p = NULL;
619 }
620
621 esp_print_decode_onesecret(ndo, line, "cmdline", 0);
622 }
623
624 ndo->ndo_espsecret = NULL;
625}
626
The Android Open Source Project2949f582009-03-03 19:30:46 -0800627#endif
628
JP Abgrall53f17a92014-02-12 14:02:41 -0800629#ifdef HAVE_LIBCRYPTO
630USES_APPLE_DEPRECATED_API
631#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800632int
633esp_print(netdissect_options *ndo,
634 const u_char *bp, const int length, const u_char *bp2
635#ifndef HAVE_LIBCRYPTO
636 _U_
637#endif
638 ,
639 int *nhdr
640#ifndef HAVE_LIBCRYPTO
641 _U_
642#endif
643 ,
644 int *padlen
645#ifndef HAVE_LIBCRYPTO
646 _U_
647#endif
648 )
649{
650 register const struct newesp *esp;
651 register const u_char *ep;
652#ifdef HAVE_LIBCRYPTO
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700653 const struct ip *ip;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800654 struct sa_list *sa = NULL;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700655 const struct ip6_hdr *ip6 = NULL;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800656 int advance;
657 int len;
658 u_char *secret;
659 int ivlen = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700660 const u_char *ivoff;
661 const u_char *p;
662 EVP_CIPHER_CTX *ctx;
Elliott Hughescec480a2017-12-19 16:54:57 -0800663 unsigned int block_size, output_buffer_size;
664 u_char *output_buffer;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800665#endif
666
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700667 esp = (const struct newesp *)bp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800668
669#ifdef HAVE_LIBCRYPTO
670 secret = NULL;
671 advance = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800672#endif
673
674#if 0
675 /* keep secret out of a register */
676 p = (u_char *)&secret;
677#endif
678
679 /* 'ep' points to the end of available data. */
680 ep = ndo->ndo_snapend;
681
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700682 if ((const u_char *)(esp + 1) >= ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700683 ND_PRINT((ndo, "[|ESP]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800684 goto fail;
685 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700686 ND_PRINT((ndo, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi)));
687 ND_PRINT((ndo, ",seq=0x%x)", EXTRACT_32BITS(&esp->esp_seq)));
688 ND_PRINT((ndo, ", length %u", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800689
690#ifndef HAVE_LIBCRYPTO
691 goto fail;
692#else
693 /* initiailize SAs */
694 if (ndo->ndo_sa_list_head == NULL) {
695 if (!ndo->ndo_espsecret)
696 goto fail;
697
698 esp_print_decodesecret(ndo);
699 }
700
701 if (ndo->ndo_sa_list_head == NULL)
702 goto fail;
703
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700704 ip = (const struct ip *)bp2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800705 switch (IP_V(ip)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800706 case 6:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700707 ip6 = (const struct ip6_hdr *)bp2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800708 /* we do not attempt to decrypt jumbograms */
709 if (!EXTRACT_16BITS(&ip6->ip6_plen))
710 goto fail;
711 /* if we can't get nexthdr, we do not need to decrypt it */
712 len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
713
714 /* see if we can find the SA, and if so, decode it */
715 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800716 if (sa->spi == EXTRACT_32BITS(&esp->esp_spi) &&
Elliott Hughes892a68b2015-10-19 14:43:53 -0700717 sa->daddr_version == 6 &&
718 UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
The Android Open Source Project2949f582009-03-03 19:30:46 -0800719 sizeof(struct in6_addr)) == 0) {
720 break;
721 }
722 }
723 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800724 case 4:
725 /* nexthdr & padding are in the last fragment */
726 if (EXTRACT_16BITS(&ip->ip_off) & IP_MF)
727 goto fail;
728 len = EXTRACT_16BITS(&ip->ip_len);
729
730 /* see if we can find the SA, and if so, decode it */
731 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800732 if (sa->spi == EXTRACT_32BITS(&esp->esp_spi) &&
Elliott Hughes892a68b2015-10-19 14:43:53 -0700733 sa->daddr_version == 4 &&
734 UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
JP Abgrall53f17a92014-02-12 14:02:41 -0800735 sizeof(struct in_addr)) == 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800736 break;
737 }
738 }
739 break;
740 default:
741 goto fail;
742 }
743
744 /* if we didn't find the specific one, then look for
745 * an unspecified one.
746 */
747 if (sa == NULL)
748 sa = ndo->ndo_sa_default;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700749
The Android Open Source Project2949f582009-03-03 19:30:46 -0800750 /* if not found fail */
751 if (sa == NULL)
752 goto fail;
753
754 /* if we can't get nexthdr, we do not need to decrypt it */
755 if (ep - bp2 < len)
756 goto fail;
757 if (ep - bp2 > len) {
758 /* FCS included at end of frame (NetBSD 1.6 or later) */
759 ep = bp2 + len;
760 }
761
Elliott Hughescec480a2017-12-19 16:54:57 -0800762 /* pointer to the IV, if there is one */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700763 ivoff = (const u_char *)(esp + 1) + 0;
Elliott Hughescec480a2017-12-19 16:54:57 -0800764 /* length of the IV, if there is one; 0, if there isn't */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800765 ivlen = sa->ivlen;
766 secret = sa->secret;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800767 ep = ep - sa->authlen;
768
769 if (sa->evp) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700770 ctx = EVP_CIPHER_CTX_new();
771 if (ctx != NULL) {
Elliott Hughescec480a2017-12-19 16:54:57 -0800772 if (set_cipher_parameters(ctx, sa->evp, secret, NULL, 0) < 0)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700773 (*ndo->ndo_warning)(ndo, "espkey init failed");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800774
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700775 p = ivoff;
Elliott Hughescec480a2017-12-19 16:54:57 -0800776 set_cipher_parameters(ctx, NULL, NULL, p, 0);
777 len = ep - (p + ivlen);
778
779 /*
780 * Allocate a buffer for the decrypted data.
781 * The output buffer must be separate from the
782 * input buffer, and its size must be a multiple
783 * of the cipher block size.
784 */
785 block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
786 output_buffer_size = len + (block_size - len % block_size);
787 output_buffer = (u_char *)malloc(output_buffer_size);
788 if (output_buffer == NULL) {
789 (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
790 EVP_CIPHER_CTX_free(ctx);
791 return -1;
792 }
793
794 EVP_Cipher(ctx, output_buffer, p + ivlen, len);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700795 EVP_CIPHER_CTX_free(ctx);
Elliott Hughescec480a2017-12-19 16:54:57 -0800796 /*
797 * XXX - of course this is wrong, because buf is a
798 * const buffer, but changing this would require a
799 * more complicated fix.
800 */
801 memcpy(p + ivlen, output_buffer, len);
802 free(output_buffer);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700803 advance = ivoff - (const u_char *)esp + ivlen;
804 } else
805 advance = sizeof(struct newesp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800806 } else
807 advance = sizeof(struct newesp);
808
809 /* sanity check for pad length */
810 if (ep - bp < *(ep - 2))
811 goto fail;
812
813 if (padlen)
814 *padlen = *(ep - 2) + 2;
815
816 if (nhdr)
817 *nhdr = *(ep - 1);
818
Elliott Hughes892a68b2015-10-19 14:43:53 -0700819 ND_PRINT((ndo, ": "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800820 return advance;
821#endif
822
823fail:
824 return -1;
825}
JP Abgrall53f17a92014-02-12 14:02:41 -0800826#ifdef HAVE_LIBCRYPTO
827USES_APPLE_RST
828#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800829
830/*
831 * Local Variables:
832 * c-style: whitesmith
833 * c-basic-offset: 8
834 * End:
835 */