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