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