]>
The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
43bdfa794ce7f25ff830d5b83dd452a68ff456ca
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.54 2004-07-21 21:42:32 guy Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
53 #include "interface.h"
54 #include "netdissect.h"
55 #include "addrtoname.h"
58 #ifndef HAVE_SOCKADDR_STORAGE
60 struct sockaddr_storage
{
62 struct sockaddr_in sin
;
63 struct sockaddr_in6 sin6
;
67 #define sockaddr_storage sockaddr
69 #endif /* HAVE_SOCKADDR_STORAGE */
74 struct sockaddr_storage daddr
;
76 const EVP_CIPHER
*evp
;
79 char secret
[256]; /* is that big enough for all secrets? */
83 static void esp_print_addsa(netdissect_options
*ndo
,
84 struct sa_list
*sa
, int sa_def
)
90 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
92 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
97 ndo
->ndo_sa_default
= nsa
;
99 nsa
->next
= ndo
->ndo_sa_list_head
;
100 ndo
->ndo_sa_list_head
= nsa
;
104 static int hexdigit(netdissect_options
*ndo
, char hex
)
106 if (hex
>= '0' && hex
<= '9')
108 else if (hex
>= 'A' && hex
<= 'F')
109 return (hex
- 'A' + 10);
110 else if (hex
>= 'a' && hex
<= 'f')
111 return (hex
- 'a' + 10);
113 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
118 static int hex2byte(netdissect_options
*ndo
, char *hexstring
)
122 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
127 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
129 * special form: file /name
130 * causes us to go read from this file instead.
133 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
)
141 spikey
= strsep(&line
, " \t");
143 memset(&sa1
, 0, sizeof(struct sa_list
));
145 /* if there is only one token, then it is an algo:key token */
149 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
155 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
156 /* open file and read it */
161 secretfile
= fopen(line
, FOPEN_READ_TXT
);
162 if (secretfile
== NULL
) {
167 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
168 /* remove newline from the line */
169 nl
= strchr(fileline
, '\n');
172 if (fileline
[0] == '#') continue;
173 if (fileline
[0] == '\0') continue;
175 esp_print_decode_onesecret(ndo
, fileline
);
185 struct sockaddr_in
*sin
;
187 struct sockaddr_in6
*sin6
;
190 spistr
= strsep(&spikey
, "@");
192 spino
= strtoul(spistr
, &foo
, 0);
193 if (spistr
== foo
|| !spikey
) {
194 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
200 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
202 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
203 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
204 #ifdef HAVE_SOCKADDR_SA_LEN
205 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
207 sin6
->sin6_family
= AF_INET6
;
210 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
211 #ifdef HAVE_SOCKADDR_SA_LEN
212 sin
->sin_len
= sizeof(struct sockaddr_in
);
214 sin
->sin_family
= AF_INET
;
216 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
223 char espsecret_key
[256];
226 const EVP_CIPHER
*evp
;
229 /* skip any blank spaces */
230 while (isspace((unsigned char)*decode
))
233 colon
= strchr(decode
, ':');
235 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
240 len
= colon
- decode
;
241 if (strlen(decode
) > strlen("-hmac96") &&
242 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
244 p
= strstr(decode
, "-hmac96");
248 if (strlen(decode
) > strlen("-cbc") &&
249 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
250 p
= strstr(decode
, "-cbc");
253 evp
= EVP_get_cipherbyname(decode
);
255 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
263 sa1
.authlen
= authlen
;
264 sa1
.ivlen
= EVP_CIPHER_iv_length(evp
);
267 if (colon
[0] == '0' && colon
[1] == 'x') {
268 /* decode some hex! */
270 len
= strlen(colon
) / 2;
273 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
278 while (colon
[0] != '\0' && colon
[1]!='\0') {
279 espsecret_key
[i
] = hex2byte(ndo
, colon
);
284 memcpy(sa1
.secret
, espsecret_key
, i
);
289 if (i
< sizeof(sa1
.secret
)) {
290 memcpy(sa1
.secret
, colon
, i
);
293 memcpy(sa1
.secret
, colon
, sizeof(sa1
.secret
));
294 sa1
.secretlen
= sizeof(sa1
.secret
);
299 esp_print_addsa(ndo
, &sa1
, sa_def
);
302 static void esp_print_decodesecret(netdissect_options
*ndo
)
307 p
= ndo
->ndo_espsecret
;
309 while (ndo
->ndo_espsecret
&& ndo
->ndo_espsecret
[0] != '\0') {
310 /* pick out the first line or first thing until a comma */
311 if ((line
= strsep(&ndo
->ndo_espsecret
, "\n,")) == NULL
) {
312 line
= ndo
->ndo_espsecret
;
313 ndo
->ndo_espsecret
= NULL
;
316 esp_print_decode_onesecret(ndo
, line
);
320 static void esp_init(netdissect_options
*ndo _U_
)
323 OpenSSL_add_all_algorithms();
324 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
329 esp_print(netdissect_options
*ndo
,
330 const u_char
*bp
, const int length
, const u_char
*bp2
331 #ifndef HAVE_LIBCRYPTO
336 #ifndef HAVE_LIBCRYPTO
341 #ifndef HAVE_LIBCRYPTO
346 register const struct newesp
*esp
;
347 register const u_char
*ep
;
348 #ifdef HAVE_LIBCRYPTO
350 struct sa_list
*sa
= NULL
;
351 int espsecret_keylen
;
353 struct ip6_hdr
*ip6
= NULL
;
363 static int initialized
= 0;
366 esp
= (struct newesp
*)bp
;
368 #ifdef HAVE_LIBCRYPTO
379 /* keep secret out of a register */
380 p
= (u_char
*)&secret
;
383 /* 'ep' points to the end of available data. */
384 ep
= ndo
->ndo_snapend
;
386 if ((u_char
*)(esp
+ 1) >= ep
) {
387 fputs("[|ESP]", stdout
);
390 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
391 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
392 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
394 #ifndef HAVE_LIBCRYPTO
397 /* initiailize SAs */
398 if (ndo
->ndo_sa_list_head
== NULL
) {
399 if (!ndo
->ndo_espsecret
)
402 esp_print_decodesecret(ndo
);
405 if (ndo
->ndo_sa_list_head
== NULL
)
408 ip
= (struct ip
*)bp2
;
412 ip6
= (struct ip6_hdr
*)bp2
;
413 /* we do not attempt to decrypt jumbograms */
414 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
416 /* if we can't get nexthdr, we do not need to decrypt it */
417 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
419 /* see if we can find the SA, and if so, decode it */
420 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
421 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
422 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
423 sin6
->sin6_family
== AF_INET6
&&
424 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
425 sizeof(struct in6_addr
)) == 0) {
432 /* nexthdr & padding are in the last fragment */
433 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
435 len
= EXTRACT_16BITS(&ip
->ip_len
);
437 /* see if we can find the SA, and if so, decode it */
438 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
439 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
440 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
441 sin
->sin_family
== AF_INET
&&
442 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
451 /* if we didn't find the specific one, then look for
452 * an unspecified one.
455 sa
= ndo
->ndo_sa_default
;
457 /* if not found fail */
461 /* if we can't get nexthdr, we do not need to decrypt it */
464 if (ep
- bp2
> len
) {
465 /* FCS included at end of frame (NetBSD 1.6 or later) */
469 ivoff
= (u_char
*)(esp
+ 1) + 0;
472 espsecret_keylen
= sa
->secretlen
;
473 ep
= ep
- sa
->authlen
;
476 memset(&ctx
, 0, sizeof(ctx
));
477 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
478 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
480 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
483 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
484 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
485 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
487 advance
= sizeof(struct newesp
);
489 /* sanity check for pad length */
490 if (ep
- bp
< *(ep
- 2))
494 *padlen
= *(ep
- 2) + 2;
499 (ndo
->ndo_printf
)(ndo
, ": ");
509 * c-style: whitesmith