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