]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
43bdfa794ce7f25ff830d5b83dd452a68ff456ca
[tcpdump] / print-esp.c
1 /* $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
24 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.54 2004-07-21 21:42:32 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34
35 #include <tcpdump-stdinc.h>
36
37 #include <stdlib.h>
38
39 #ifdef HAVE_LIBCRYPTO
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
42 #endif
43 #endif
44
45 #include <stdio.h>
46
47 #include "ip.h"
48 #include "esp.h"
49 #ifdef INET6
50 #include "ip6.h"
51 #endif
52
53 #include "interface.h"
54 #include "netdissect.h"
55 #include "addrtoname.h"
56 #include "extract.h"
57
58 #ifndef HAVE_SOCKADDR_STORAGE
59 #ifdef INET6
60 struct sockaddr_storage {
61 union {
62 struct sockaddr_in sin;
63 struct sockaddr_in6 sin6;
64 } un;
65 };
66 #else
67 #define sockaddr_storage sockaddr
68 #endif
69 #endif /* HAVE_SOCKADDR_STORAGE */
70
71 #ifdef HAVE_LIBCRYPTO
72 struct sa_list {
73 struct sa_list *next;
74 struct sockaddr_storage daddr;
75 u_int32_t spi;
76 const EVP_CIPHER *evp;
77 int ivlen;
78 int authlen;
79 char secret[256]; /* is that big enough for all secrets? */
80 int secretlen;
81 };
82
83 static void esp_print_addsa(netdissect_options *ndo,
84 struct sa_list *sa, int sa_def)
85 {
86 /* copy the "sa" */
87
88 struct sa_list *nsa;
89
90 nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
91 if (nsa == NULL)
92 (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
93
94 *nsa = *sa;
95
96 if (sa_def)
97 ndo->ndo_sa_default = nsa;
98
99 nsa->next = ndo->ndo_sa_list_head;
100 ndo->ndo_sa_list_head = nsa;
101 }
102
103
104 static int hexdigit(netdissect_options *ndo, char hex)
105 {
106 if (hex >= '0' && hex <= '9')
107 return (hex - '0');
108 else if (hex >= 'A' && hex <= 'F')
109 return (hex - 'A' + 10);
110 else if (hex >= 'a' && hex <= 'f')
111 return (hex - 'a' + 10);
112 else {
113 (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
114 return 0;
115 }
116 }
117
118 static int hex2byte(netdissect_options *ndo, char *hexstring)
119 {
120 int byte;
121
122 byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
123 return byte;
124 }
125
126 /*
127 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
128 *
129 * special form: file /name
130 * causes us to go read from this file instead.
131 *
132 */
133 static void esp_print_decode_onesecret(netdissect_options *ndo, char *line)
134 {
135 struct sa_list sa1;
136 int sa_def;
137
138 char *spikey;
139 char *decode;
140
141 spikey = strsep(&line, " \t");
142 sa_def = 0;
143 memset(&sa1, 0, sizeof(struct sa_list));
144
145 /* if there is only one token, then it is an algo:key token */
146 if (line == NULL) {
147 decode = spikey;
148 spikey = NULL;
149 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
150 /* sa1.spi = 0; */
151 sa_def = 1;
152 } else
153 decode = line;
154
155 if (spikey && strcasecmp(spikey, "file") == 0) {
156 /* open file and read it */
157 FILE *secretfile;
158 char fileline[1024];
159 char *nl;
160
161 secretfile = fopen(line, FOPEN_READ_TXT);
162 if (secretfile == NULL) {
163 perror(line);
164 exit(3);
165 }
166
167 while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
168 /* remove newline from the line */
169 nl = strchr(fileline, '\n');
170 if (nl)
171 *nl = '\0';
172 if (fileline[0] == '#') continue;
173 if (fileline[0] == '\0') continue;
174
175 esp_print_decode_onesecret(ndo, fileline);
176 }
177 fclose(secretfile);
178
179 return;
180 }
181
182 if (spikey) {
183 char *spistr, *foo;
184 u_int32_t spino;
185 struct sockaddr_in *sin;
186 #ifdef INET6
187 struct sockaddr_in6 *sin6;
188 #endif
189
190 spistr = strsep(&spikey, "@");
191
192 spino = strtoul(spistr, &foo, 0);
193 if (spistr == foo || !spikey) {
194 (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
195 return;
196 }
197
198 sa1.spi = spino;
199
200 sin = (struct sockaddr_in *)&sa1.daddr;
201 #ifdef INET6
202 sin6 = (struct sockaddr_in6 *)&sa1.daddr;
203 if (inet_pton(AF_INET6, spikey, &sin6->sin6_addr) == 1) {
204 #ifdef HAVE_SOCKADDR_SA_LEN
205 sin6->sin6_len = sizeof(struct sockaddr_in6);
206 #endif
207 sin6->sin6_family = AF_INET6;
208 } else
209 #endif
210 if (inet_pton(AF_INET, spikey, &sin->sin_addr) == 1) {
211 #ifdef HAVE_SOCKADDR_SA_LEN
212 sin->sin_len = sizeof(struct sockaddr_in);
213 #endif
214 sin->sin_family = AF_INET;
215 } else {
216 (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
217 return;
218 }
219 }
220
221 if (decode) {
222 char *colon, *p;
223 char espsecret_key[256];
224 int len;
225 size_t i;
226 const EVP_CIPHER *evp;
227 int authlen = 0;
228
229 /* skip any blank spaces */
230 while (isspace((unsigned char)*decode))
231 decode++;
232
233 colon = strchr(decode, ':');
234 if (colon == NULL) {
235 (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
236 return;
237 }
238 *colon = '\0';
239
240 len = colon - decode;
241 if (strlen(decode) > strlen("-hmac96") &&
242 !strcmp(decode + strlen(decode) - strlen("-hmac96"),
243 "-hmac96")) {
244 p = strstr(decode, "-hmac96");
245 *p = '\0';
246 authlen = 12;
247 }
248 if (strlen(decode) > strlen("-cbc") &&
249 !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
250 p = strstr(decode, "-cbc");
251 *p = '\0';
252 }
253 evp = EVP_get_cipherbyname(decode);
254 if (!evp) {
255 (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
256 sa1.evp = NULL;
257 sa1.authlen = 0;
258 sa1.ivlen = 0;
259 return;
260 }
261
262 sa1.evp = evp;
263 sa1.authlen = authlen;
264 sa1.ivlen = EVP_CIPHER_iv_length(evp);
265
266 colon++;
267 if (colon[0] == '0' && colon[1] == 'x') {
268 /* decode some hex! */
269 colon += 2;
270 len = strlen(colon) / 2;
271
272 if (len > 256) {
273 (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
274 return;
275 }
276
277 i = 0;
278 while (colon[0] != '\0' && colon[1]!='\0') {
279 espsecret_key[i] = hex2byte(ndo, colon);
280 colon += 2;
281 i++;
282 }
283
284 memcpy(sa1.secret, espsecret_key, i);
285 sa1.secretlen = i;
286 } else {
287 i = strlen(colon);
288
289 if (i < sizeof(sa1.secret)) {
290 memcpy(sa1.secret, colon, i);
291 sa1.secretlen = i;
292 } else {
293 memcpy(sa1.secret, colon, sizeof(sa1.secret));
294 sa1.secretlen = sizeof(sa1.secret);
295 }
296 }
297 }
298
299 esp_print_addsa(ndo, &sa1, sa_def);
300 }
301
302 static void esp_print_decodesecret(netdissect_options *ndo)
303 {
304 char *line;
305 char *p;
306
307 p = ndo->ndo_espsecret;
308
309 while (ndo->ndo_espsecret && ndo->ndo_espsecret[0] != '\0') {
310 /* pick out the first line or first thing until a comma */
311 if ((line = strsep(&ndo->ndo_espsecret, "\n,")) == NULL) {
312 line = ndo->ndo_espsecret;
313 ndo->ndo_espsecret = NULL;
314 }
315
316 esp_print_decode_onesecret(ndo, line);
317 }
318 }
319
320 static void esp_init(netdissect_options *ndo _U_)
321 {
322
323 OpenSSL_add_all_algorithms();
324 EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
325 }
326 #endif
327
328 int
329 esp_print(netdissect_options *ndo,
330 const u_char *bp, const int length, const u_char *bp2
331 #ifndef HAVE_LIBCRYPTO
332 _U_
333 #endif
334 ,
335 int *nhdr
336 #ifndef HAVE_LIBCRYPTO
337 _U_
338 #endif
339 ,
340 int *padlen
341 #ifndef HAVE_LIBCRYPTO
342 _U_
343 #endif
344 )
345 {
346 register const struct newesp *esp;
347 register const u_char *ep;
348 #ifdef HAVE_LIBCRYPTO
349 struct ip *ip;
350 struct sa_list *sa = NULL;
351 int espsecret_keylen;
352 #ifdef INET6
353 struct ip6_hdr *ip6 = NULL;
354 #endif
355 int advance;
356 int len;
357 char *secret;
358 int ivlen = 0;
359 u_char *ivoff;
360 u_char *p;
361 EVP_CIPHER_CTX ctx;
362 int blocksz;
363 static int initialized = 0;
364 #endif
365
366 esp = (struct newesp *)bp;
367
368 #ifdef HAVE_LIBCRYPTO
369 secret = NULL;
370 advance = 0;
371
372 if (!initialized) {
373 esp_init(ndo);
374 initialized = 1;
375 }
376 #endif
377
378 #if 0
379 /* keep secret out of a register */
380 p = (u_char *)&secret;
381 #endif
382
383 /* 'ep' points to the end of available data. */
384 ep = ndo->ndo_snapend;
385
386 if ((u_char *)(esp + 1) >= ep) {
387 fputs("[|ESP]", stdout);
388 goto fail;
389 }
390 (*ndo->ndo_printf)(ndo, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi));
391 (*ndo->ndo_printf)(ndo, ",seq=0x%x)", EXTRACT_32BITS(&esp->esp_seq));
392 (*ndo->ndo_printf)(ndo, ", length %u", length);
393
394 #ifndef HAVE_LIBCRYPTO
395 goto fail;
396 #else
397 /* initiailize SAs */
398 if (ndo->ndo_sa_list_head == NULL) {
399 if (!ndo->ndo_espsecret)
400 goto fail;
401
402 esp_print_decodesecret(ndo);
403 }
404
405 if (ndo->ndo_sa_list_head == NULL)
406 goto fail;
407
408 ip = (struct ip *)bp2;
409 switch (IP_V(ip)) {
410 #ifdef INET6
411 case 6:
412 ip6 = (struct ip6_hdr *)bp2;
413 /* we do not attempt to decrypt jumbograms */
414 if (!EXTRACT_16BITS(&ip6->ip6_plen))
415 goto fail;
416 /* if we can't get nexthdr, we do not need to decrypt it */
417 len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
418
419 /* see if we can find the SA, and if so, decode it */
420 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
421 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa->daddr;
422 if (sa->spi == ntohl(esp->esp_spi) &&
423 sin6->sin6_family == AF_INET6 &&
424 memcmp(&sin6->sin6_addr, &ip6->ip6_dst,
425 sizeof(struct in6_addr)) == 0) {
426 break;
427 }
428 }
429 break;
430 #endif /*INET6*/
431 case 4:
432 /* nexthdr & padding are in the last fragment */
433 if (EXTRACT_16BITS(&ip->ip_off) & IP_MF)
434 goto fail;
435 len = EXTRACT_16BITS(&ip->ip_len);
436
437 /* see if we can find the SA, and if so, decode it */
438 for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
439 struct sockaddr_in *sin = (struct sockaddr_in *)&sa->daddr;
440 if (sa->spi == ntohl(esp->esp_spi) &&
441 sin->sin_family == AF_INET &&
442 sin->sin_addr.s_addr == ip->ip_dst.s_addr) {
443 break;
444 }
445 }
446 break;
447 default:
448 goto fail;
449 }
450
451 /* if we didn't find the specific one, then look for
452 * an unspecified one.
453 */
454 if (sa == NULL)
455 sa = ndo->ndo_sa_default;
456
457 /* if not found fail */
458 if (sa == NULL)
459 goto fail;
460
461 /* if we can't get nexthdr, we do not need to decrypt it */
462 if (ep - bp2 < len)
463 goto fail;
464 if (ep - bp2 > len) {
465 /* FCS included at end of frame (NetBSD 1.6 or later) */
466 ep = bp2 + len;
467 }
468
469 ivoff = (u_char *)(esp + 1) + 0;
470 ivlen = sa->ivlen;
471 secret = sa->secret;
472 espsecret_keylen = sa->secretlen;
473 ep = ep - sa->authlen;
474
475 if (sa->evp) {
476 memset(&ctx, 0, sizeof(ctx));
477 if (EVP_CipherInit(&ctx, sa->evp, secret, NULL, 0) < 0)
478 (*ndo->ndo_warning)(ndo, "espkey init failed");
479
480 blocksz = EVP_CIPHER_CTX_block_size(&ctx);
481
482 p = ivoff;
483 EVP_CipherInit(&ctx, NULL, NULL, p, 0);
484 EVP_Cipher(&ctx, p + ivlen, p + ivlen, ep - (p + ivlen));
485 advance = ivoff - (u_char *)esp + ivlen;
486 } else
487 advance = sizeof(struct newesp);
488
489 /* sanity check for pad length */
490 if (ep - bp < *(ep - 2))
491 goto fail;
492
493 if (padlen)
494 *padlen = *(ep - 2) + 2;
495
496 if (nhdr)
497 *nhdr = *(ep - 1);
498
499 (ndo->ndo_printf)(ndo, ": ");
500 return advance;
501 #endif
502
503 fail:
504 return -1;
505 }
506
507 /*
508 * Local Variables:
509 * c-style: whitesmith
510 * c-basic-offset: 8
511 * End:
512 */