]>
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
[] _U_
=
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.44.2.5 2004-04-10 08:43:20 guy 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 */
77 struct sockaddr_storage daddr
;
79 const EVP_CIPHER
*evp
;
82 char secret
[256]; /* is that big enough for all secrets? */
86 static struct sa_list
*sa_list_head
= NULL
;
87 static struct sa_list
*sa_default
= NULL
;
89 static void esp_print_addsa(struct sa_list
*sa
, int sa_def
)
95 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
97 error("ran out of memory to allocate sa structure");
104 nsa
->next
= sa_list_head
;
109 static int hexdigit(char hex
)
111 if (hex
>= '0' && hex
<= '9')
113 else if (hex
>= 'A' && hex
<= 'F')
114 return (hex
- 'A' + 10);
115 else if (hex
>= 'a' && hex
<= 'f')
116 return (hex
- 'a' + 10);
118 printf("invalid hex digit %c in espsecret\n", hex
);
123 static int hex2byte(char *hexstring
)
127 byte
= (hexdigit(hexstring
[0]) << 4) + hexdigit(hexstring
[1]);
132 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
134 * special form: file /name
135 * causes us to go read from this file instead.
138 static void esp_print_decode_onesecret(char *line
)
146 spikey
= strsep(&line
, " \t");
148 memset(&sa1
, 0, sizeof(struct sa_list
));
150 /* if there is only one token, then it is an algo:key token */
154 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
160 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
161 /* open file and read it */
166 secretfile
= fopen(line
, FOPEN_READ_TXT
);
167 if (secretfile
== NULL
) {
172 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
173 /* remove newline from the line */
174 nl
= strchr(fileline
, '\n');
177 if (fileline
[0] == '#') continue;
178 if (fileline
[0] == '\0') continue;
180 esp_print_decode_onesecret(fileline
);
190 struct sockaddr_in
*sin
;
192 struct sockaddr_in6
*sin6
;
195 spistr
= strsep(&spikey
, "@");
197 spino
= strtoul(spistr
, &foo
, 0);
198 if (spistr
== foo
|| !spikey
) {
199 printf("print_esp: failed to decode spi# %s\n", foo
);
205 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
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
);
212 sin6
->sin6_family
= AF_INET6
;
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
);
219 sin
->sin_family
= AF_INET
;
221 printf("print_esp: can not decode IP# %s\n", spikey
);
228 char espsecret_key
[256];
231 const EVP_CIPHER
*evp
;
235 /* skip any blank spaces */
236 while (isspace((unsigned char)*decode
))
239 colon
= strchr(decode
, ':');
241 printf("failed to decode espsecret: %s\n", decode
);
246 len
= colon
- decode
;
247 if (strlen(decode
) > strlen("-hmac96") &&
248 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
250 p
= strstr(decode
, "-hmac96");
254 if (strlen(decode
) > strlen("-cbc") &&
255 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
256 p
= strstr(decode
, "-cbc");
259 evp
= EVP_get_cipherbyname(decode
);
261 printf("failed to find cipher algo %s\n", decode
);
269 sa1
.authlen
= authlen
;
273 if (colon
[0] == '0' && colon
[1] == 'x') {
274 /* decode some hex! */
276 len
= strlen(colon
) / 2;
279 printf("secret is too big: %d\n", len
);
284 while (colon
[0] != '\0' && colon
[1]!='\0') {
285 espsecret_key
[i
] = hex2byte(colon
);
290 memcpy(sa1
.secret
, espsecret_key
, i
);
295 if (i
< sizeof(sa1
.secret
)) {
296 memcpy(sa1
.secret
, colon
, i
);
299 memcpy(sa1
.secret
, colon
, sizeof(sa1
.secret
));
300 sa1
.secretlen
= sizeof(sa1
.secret
);
305 esp_print_addsa(&sa1
, sa_def
);
308 static void esp_print_decodesecret(void)
315 while (espsecret
&& espsecret
[0] != '\0') {
316 /* pick out the first line or first thing until a comma */
317 if ((line
= strsep(&espsecret
, "\n,")) == NULL
) {
322 esp_print_decode_onesecret(line
);
326 static void esp_init(void)
329 OpenSSL_add_all_algorithms();
330 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
335 esp_print(const u_char
*bp
, const u_char
*bp2
336 #ifndef HAVE_LIBCRYPTO
341 #ifndef HAVE_LIBCRYPTO
346 #ifndef HAVE_LIBCRYPTO
351 register const struct newesp
*esp
;
352 register const u_char
*ep
;
353 #ifdef HAVE_LIBCRYPTO
355 struct sa_list
*sa
= NULL
;
356 int espsecret_keylen
;
358 struct ip6_hdr
*ip6
= NULL
;
368 static int initialized
= 0;
371 esp
= (struct newesp
*)bp
;
373 #ifdef HAVE_LIBCRYPTO
384 /* keep secret out of a register */
385 p
= (u_char
*)&secret
;
388 /* 'ep' points to the end of available data. */
391 if ((u_char
*)(esp
+ 1) >= ep
) {
392 fputs("[|ESP]", stdout
);
395 printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
396 printf(",seq=0x%x", EXTRACT_32BITS(&esp
->esp_seq
));
399 #ifndef HAVE_LIBCRYPTO
402 /* initiailize SAs */
403 if (sa_list_head
== NULL
) {
407 esp_print_decodesecret();
410 if (sa_list_head
== NULL
)
413 ip
= (struct ip
*)bp2
;
417 ip6
= (struct ip6_hdr
*)bp2
;
418 /* we do not attempt to decrypt jumbograms */
419 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
421 /* if we can't get nexthdr, we do not need to decrypt it */
422 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
424 /* see if we can find the SA, and if so, decode it */
425 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
426 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
427 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
428 sin6
->sin6_family
== AF_INET6
&&
429 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
430 sizeof(struct in6_addr
)) == 0) {
437 /* nexthdr & padding are in the last fragment */
438 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
440 len
= EXTRACT_16BITS(&ip
->ip_len
);
442 /* see if we can find the SA, and if so, decode it */
443 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
444 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
445 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
446 sin
->sin_family
== AF_INET
&&
447 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
456 /* if we didn't find the specific one, then look for
457 * an unspecified one.
462 /* if not found fail */
466 /* if we can't get nexthdr, we do not need to decrypt it */
469 if (ep
- bp2
> len
) {
470 /* FCS included at end of frame (NetBSD 1.6 or later) */
474 ivoff
= (u_char
*)(esp
+ 1) + 0;
477 espsecret_keylen
= sa
->secretlen
;
478 ep
= ep
- sa
->authlen
;
481 memset(&ctx
, 0, sizeof(ctx
));
482 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
483 printf("espkey init failed");
485 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
488 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
489 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
490 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
492 advance
= sizeof(struct newesp
);
494 /* sanity check for pad length */
495 if (ep
- bp
< *(ep
- 2))
499 *padlen
= *(ep
- 2) + 2;