]>
The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
1 /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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
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.
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)";
35 #include <tcpdump-stdinc.h>
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
53 #if defined(__MINGW32__) || defined(__WATCOMC__)
54 extern char *strsep(char **stringp
, const char *delim
); /* Missing/strsep.c */
57 #include "interface.h"
58 #include "addrtoname.h"
61 #ifndef HAVE_SOCKADDR_STORAGE
63 struct sockaddr_storage
{
65 struct sockaddr_in sin
;
66 struct sockaddr_in6 sin6
;
70 #define sockaddr_storage sockaddr
72 #endif /* HAVE_SOCKADDR_STORAGE */
76 struct sockaddr_storage daddr
;
78 const EVP_CIPHER
*evp
;
81 char secret
[256]; /* is that big enough for all secrets? */
85 static struct sa_list
*sa_list_head
= NULL
;
86 static struct sa_list
*sa_default
= NULL
;
88 static void esp_print_addsa(struct sa_list
*sa
, int sa_def
)
94 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
96 error("ran out of memory to allocate sa structure");
103 nsa
->next
= sa_list_head
;
108 static int hexdigit(char hex
)
110 if (hex
>= '0' && hex
<= '9')
112 else if (hex
>= 'A' && hex
<= 'F')
113 return (hex
- 'A' + 10);
114 else if (hex
>= 'a' && hex
<= 'f')
115 return (hex
- 'a' + 10);
117 printf("invalid hex digit %c in espsecret\n", hex
);
122 static int hex2byte(char *hexstring
)
126 byte
= (hexdigit(hexstring
[0]) << 4) + hexdigit(hexstring
[1]);
131 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
133 * special form: file /name
134 * causes us to go read from this file instead.
137 static void esp_print_decode_onesecret(char *line
)
145 spikey
= strsep(&line
, " \t");
147 memset(&sa1
, 0, sizeof(struct sa_list
));
149 /* if there is only one token, then it is an algo:key token */
153 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
159 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
160 /* open file and read it */
165 secretfile
= fopen(line
, FOPEN_READ_TXT
);
166 if (secretfile
== NULL
) {
171 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
172 /* remove newline from the line */
173 nl
= strchr(fileline
, '\n');
176 if (fileline
[0] == '#') continue;
177 if (fileline
[0] == '\0') continue;
179 esp_print_decode_onesecret(fileline
);
189 struct sockaddr_in
*sin
;
191 struct sockaddr_in6
*sin6
;
194 spistr
= strsep(&spikey
, "@");
196 spino
= strtoul(spistr
, &foo
, 0);
197 if (spistr
== foo
|| !spikey
) {
198 printf("print_esp: failed to decode spi# %s\n", foo
);
204 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
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
);
211 sin6
->sin6_family
= AF_INET6
;
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
);
218 sin
->sin_family
= AF_INET
;
220 printf("print_esp: can not decode IP# %s\n", spikey
);
227 char espsecret_key
[256];
229 const EVP_CIPHER
*evp
;
233 /* skip any blank spaces */
234 while (isspace((unsigned char)*decode
))
237 colon
= strchr(decode
, ':');
239 printf("failed to decode espsecret: %s\n", decode
);
244 len
= colon
- decode
;
245 if (strlen(decode
) > strlen("-hmac96") &&
246 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
248 p
= strstr(decode
, "-hmac96");
252 if (strlen(decode
) > strlen("-cbc") &&
253 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
254 p
= strstr(decode
, "-cbc");
257 evp
= EVP_get_cipherbyname(decode
);
259 printf("failed to find cipher algo %s\n", decode
);
267 sa1
.authlen
= authlen
;
271 if (colon
[0] == '0' && colon
[1] == 'x') {
272 /* decode some hex! */
274 len
= strlen(colon
) / 2;
277 printf("secret is too big: %d\n", len
);
282 while (colon
[0] != '\0' && colon
[1]!='\0') {
283 espsecret_key
[i
] = hex2byte(colon
);
288 memcpy(sa1
.secret
, espsecret_key
, i
);
293 if (i
< sizeof(sa1
.secret
)) {
294 memcpy(sa1
.secret
, colon
, i
);
297 memcpy(sa1
.secret
, colon
, sizeof(sa1
.secret
));
298 sa1
.secretlen
= sizeof(sa1
.secret
);
303 esp_print_addsa(&sa1
, sa_def
);
306 static void esp_print_decodesecret(void)
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
) {
320 esp_print_decode_onesecret(line
);
324 static void esp_init(void)
327 OpenSSL_add_all_algorithms();
328 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
332 esp_print(const u_char
*bp
, const u_char
*bp2
, int *nhdr
, int *padlen
)
334 register const struct newesp
*esp
;
335 register const u_char
*ep
;
337 struct sa_list
*sa
= NULL
;
338 int espsecret_keylen
;
340 struct ip6_hdr
*ip6
= NULL
;
347 #ifdef HAVE_LIBCRYPTO
351 static int initialized
= 0;
354 esp
= (struct newesp
*)bp
;
358 #ifdef HAVE_LIBCRYPTO
366 /* keep secret out of a register */
367 p
= (u_char
*)&secret
;
370 /* 'ep' points to the end of available data. */
373 if ((u_char
*)(esp
+ 1) >= ep
) {
374 fputs("[|ESP]", stdout
);
377 printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
378 printf(",seq=0x%x", EXTRACT_32BITS(&esp
->esp_seq
));
381 /* initiailize SAs */
382 if (sa_list_head
== NULL
) {
386 esp_print_decodesecret();
389 if (sa_list_head
== NULL
)
392 ip
= (struct ip
*)bp2
;
396 ip6
= (struct ip6_hdr
*)bp2
;
397 /* we do not attempt to decrypt jumbograms */
398 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
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
);
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) {
416 /* nexthdr & padding are in the last fragment */
417 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
419 len
= EXTRACT_16BITS(&ip
->ip_len
);
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
) {
435 /* if we didn't find the specific one, then look for
436 * an unspecified one.
441 /* if not found fail */
445 /* if we can't get nexthdr, we do not need to decrypt it */
448 if (ep
- bp2
> len
) {
449 /* FCS included at end of frame (NetBSD 1.6 or later) */
453 ivoff
= (u_char
*)(esp
+ 1) + 0;
456 espsecret_keylen
= sa
->secretlen
;
459 memset(&ctx
, 0, sizeof(ctx
));
460 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
461 printf("espkey init failed");
463 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
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
;
470 advance
= sizeof(struct newesp
);
472 ep
= ep
- sa
->authlen
;
473 /* sanity check for pad length */
474 if (ep
- bp
< *(ep
- 2))
478 *padlen
= *(ep
- 2) + 2;