]>
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.50 2004-04-05 00:15:51 mcr 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 "netdissect.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 void esp_print_addsa(netdissect_options
*ndo
,
87 struct sa_list
*sa
, int sa_def
)
93 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
95 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
100 ndo
->ndo_sa_default
= nsa
;
102 nsa
->next
= ndo
->ndo_sa_list_head
;
103 ndo
->ndo_sa_list_head
= nsa
;
107 static int hexdigit(netdissect_options
*ndo
, char hex
)
109 if (hex
>= '0' && hex
<= '9')
111 else if (hex
>= 'A' && hex
<= 'F')
112 return (hex
- 'A' + 10);
113 else if (hex
>= 'a' && hex
<= 'f')
114 return (hex
- 'a' + 10);
116 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
121 static int hex2byte(netdissect_options
*ndo
, char *hexstring
)
125 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
130 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
132 * special form: file /name
133 * causes us to go read from this file instead.
136 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
)
144 spikey
= strsep(&line
, " \t");
146 memset(&sa1
, 0, sizeof(struct sa_list
));
148 /* if there is only one token, then it is an algo:key token */
152 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
158 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
159 /* open file and read it */
164 secretfile
= fopen(line
, FOPEN_READ_TXT
);
165 if (secretfile
== NULL
) {
170 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
171 /* remove newline from the line */
172 nl
= strchr(fileline
, '\n');
175 if (fileline
[0] == '#') continue;
176 if (fileline
[0] == '\0') continue;
178 esp_print_decode_onesecret(ndo
, fileline
);
188 struct sockaddr_in
*sin
;
190 struct sockaddr_in6
*sin6
;
193 spistr
= strsep(&spikey
, "@");
195 spino
= strtoul(spistr
, &foo
, 0);
196 if (spistr
== foo
|| !spikey
) {
197 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
203 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
205 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
206 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
207 #ifdef HAVE_SOCKADDR_SA_LEN
208 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
210 sin6
->sin6_family
= AF_INET6
;
213 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
214 #ifdef HAVE_SOCKADDR_SA_LEN
215 sin
->sin_len
= sizeof(struct sockaddr_in
);
217 sin
->sin_family
= AF_INET
;
219 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
226 char espsecret_key
[256];
229 const EVP_CIPHER
*evp
;
232 /* skip any blank spaces */
233 while (isspace((unsigned char)*decode
))
236 colon
= strchr(decode
, ':');
238 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
243 len
= colon
- decode
;
244 if (strlen(decode
) > strlen("-hmac96") &&
245 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
247 p
= strstr(decode
, "-hmac96");
251 if (strlen(decode
) > strlen("-cbc") &&
252 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
253 p
= strstr(decode
, "-cbc");
256 evp
= EVP_get_cipherbyname(decode
);
258 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
266 sa1
.authlen
= authlen
;
267 sa1
.ivlen
= EVP_CIPHER_iv_length(evp
);
270 if (colon
[0] == '0' && colon
[1] == 'x') {
271 /* decode some hex! */
273 len
= strlen(colon
) / 2;
276 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
281 while (colon
[0] != '\0' && colon
[1]!='\0') {
282 espsecret_key
[i
] = hex2byte(ndo
, colon
);
287 memcpy(sa1
.secret
, espsecret_key
, i
);
292 if (i
< sizeof(sa1
.secret
)) {
293 memcpy(sa1
.secret
, colon
, i
);
296 memcpy(sa1
.secret
, colon
, sizeof(sa1
.secret
));
297 sa1
.secretlen
= sizeof(sa1
.secret
);
302 esp_print_addsa(ndo
, &sa1
, sa_def
);
305 static void esp_print_decodesecret(netdissect_options
*ndo
)
310 p
= ndo
->ndo_espsecret
;
312 while (ndo
->ndo_espsecret
&& ndo
->ndo_espsecret
[0] != '\0') {
313 /* pick out the first line or first thing until a comma */
314 if ((line
= strsep(&ndo
->ndo_espsecret
, "\n,")) == NULL
) {
315 line
= ndo
->ndo_espsecret
;
316 ndo
->ndo_espsecret
= NULL
;
319 esp_print_decode_onesecret(ndo
, line
);
323 static void esp_init(netdissect_options
*ndo _U_
)
326 OpenSSL_add_all_algorithms();
327 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
332 esp_print(netdissect_options
*ndo
,
333 const u_char
*bp
, const u_char
*bp2
334 #ifndef HAVE_LIBCRYPTO
339 #ifndef HAVE_LIBCRYPTO
344 #ifndef HAVE_LIBCRYPTO
349 register const struct newesp
*esp
;
350 register const u_char
*ep
;
351 #ifdef HAVE_LIBCRYPTO
353 struct sa_list
*sa
= NULL
;
354 int espsecret_keylen
;
356 struct ip6_hdr
*ip6
= NULL
;
366 static int initialized
= 0;
369 esp
= (struct newesp
*)bp
;
371 #ifdef HAVE_LIBCRYPTO
382 /* keep secret out of a register */
383 p
= (u_char
*)&secret
;
386 /* 'ep' points to the end of available data. */
387 ep
= ndo
->ndo_snapend
;
389 if ((u_char
*)(esp
+ 1) >= ep
) {
390 fputs("[|ESP]", stdout
);
393 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
394 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
396 #ifndef HAVE_LIBCRYPTO
399 /* initiailize SAs */
400 if (ndo
->ndo_sa_list_head
== NULL
) {
401 if (!ndo
->ndo_espsecret
)
404 esp_print_decodesecret(ndo
);
407 if (ndo
->ndo_sa_list_head
== NULL
)
410 ip
= (struct ip
*)bp2
;
414 ip6
= (struct ip6_hdr
*)bp2
;
415 /* we do not attempt to decrypt jumbograms */
416 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
418 /* if we can't get nexthdr, we do not need to decrypt it */
419 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
421 /* see if we can find the SA, and if so, decode it */
422 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
423 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
424 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
425 sin6
->sin6_family
== AF_INET6
&&
426 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
427 sizeof(struct in6_addr
)) == 0) {
434 /* nexthdr & padding are in the last fragment */
435 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
437 len
= EXTRACT_16BITS(&ip
->ip_len
);
439 /* see if we can find the SA, and if so, decode it */
440 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
441 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
442 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
443 sin
->sin_family
== AF_INET
&&
444 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
453 /* if we didn't find the specific one, then look for
454 * an unspecified one.
457 sa
= ndo
->ndo_sa_default
;
459 /* if not found fail */
463 /* if we can't get nexthdr, we do not need to decrypt it */
466 if (ep
- bp2
> len
) {
467 /* FCS included at end of frame (NetBSD 1.6 or later) */
471 ivoff
= (u_char
*)(esp
+ 1) + 0;
474 espsecret_keylen
= sa
->secretlen
;
477 memset(&ctx
, 0, sizeof(ctx
));
478 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
479 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
481 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
484 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
485 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
486 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
488 advance
= sizeof(struct newesp
);
490 ep
= ep
- sa
->authlen
;
491 /* sanity check for pad length */
492 if (ep
- bp
< *(ep
- 2))
496 *padlen
= *(ep
- 2) + 2;
501 (ndo
->ndo_printf
)(ndo
, ": ");
511 * c-style: whitesmith