1 /* $NetBSD: print-esp.c,v 1.7 2007/07/24 11:53:43 drochner 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.
24 #include <sys/cdefs.h>
27 static const char rcsid
[] _U_
=
28 "@(#) Header: /tcpdump/master/tcpdump/print-esp.c,v 1.55.2.1 2005/04/21 06:44:57 guy Exp (LBL)";
30 __RCSID("$NetBSD: print-esp.c,v 1.7 2007/07/24 11:53:43 drochner Exp $");
40 #include <tcpdump-stdinc.h>
45 #ifdef HAVE_OPENSSL_EVP_H
46 #include <openssl/evp.h>
58 #include "netdissect.h"
59 #include "addrtoname.h"
62 #ifndef HAVE_SOCKADDR_STORAGE
64 struct sockaddr_storage
{
66 struct sockaddr_in sin
;
67 struct sockaddr_in6 sin6
;
71 #define sockaddr_storage sockaddr
73 #endif /* HAVE_SOCKADDR_STORAGE */
78 struct sockaddr_storage daddr
;
80 const EVP_CIPHER
*evp
;
83 u_char secret
[256]; /* is that big enough for all secrets? */
87 static void esp_print_addsa(netdissect_options
*ndo
,
88 struct sa_list
*sa
, int sa_def
)
94 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
96 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
101 ndo
->ndo_sa_default
= nsa
;
103 nsa
->next
= ndo
->ndo_sa_list_head
;
104 ndo
->ndo_sa_list_head
= nsa
;
108 static u_int
hexdigit(netdissect_options
*ndo
, 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 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
122 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
126 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, 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(netdissect_options
*ndo
, 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(ndo
, 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 (*ndo
->ndo_warning
)(ndo
, "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 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
227 u_char espsecret_key
[256];
230 const EVP_CIPHER
*evp
;
233 /* skip any blank spaces */
234 while (isspace((unsigned char)*decode
))
237 colon
= strchr(decode
, ':');
239 (*ndo
->ndo_warning
)(ndo
, "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 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
267 sa1
.authlen
= authlen
;
268 sa1
.ivlen
= EVP_CIPHER_iv_length(evp
);
271 if (colon
[0] == '0' && colon
[1] == 'x') {
272 /* decode some hex! */
274 len
= strlen(colon
) / 2;
277 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
282 while (colon
[0] != '\0' && colon
[1]!='\0') {
283 espsecret_key
[i
] = hex2byte(ndo
, 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(ndo
, &sa1
, sa_def
);
306 static void esp_print_decodesecret(netdissect_options
*ndo
)
311 p
= ndo
->ndo_espsecret
;
313 while (ndo
->ndo_espsecret
&& ndo
->ndo_espsecret
[0] != '\0') {
314 /* pick out the first line or first thing until a comma */
315 if ((line
= strsep(&ndo
->ndo_espsecret
, "\n,")) == NULL
) {
316 line
= ndo
->ndo_espsecret
;
317 ndo
->ndo_espsecret
= NULL
;
320 esp_print_decode_onesecret(ndo
, line
);
324 static void esp_init(netdissect_options
*ndo _U_
)
327 OpenSSL_add_all_algorithms();
328 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
333 esp_print(netdissect_options
*ndo
,
334 const u_char
*bp
, const int length
, const u_char
*bp2
335 #ifndef HAVE_LIBCRYPTO
340 #ifndef HAVE_LIBCRYPTO
345 #ifndef HAVE_LIBCRYPTO
350 register const struct newesp
*esp
;
351 register const u_char
*ep
;
352 #ifdef HAVE_LIBCRYPTO
354 struct sa_list
*sa
= NULL
;
355 int espsecret_keylen
;
357 struct ip6_hdr
*ip6
= NULL
;
367 static int initialized
= 0;
370 esp
= (struct newesp
*)bp
;
372 #ifdef HAVE_LIBCRYPTO
383 /* keep secret out of a register */
384 p
= (u_char
*)&secret
;
387 /* 'ep' points to the end of available data. */
388 ep
= ndo
->ndo_snapend
;
390 if ((u_char
*)(esp
+ 1) >= ep
) {
391 fputs("[|ESP]", stdout
);
394 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
395 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
396 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
398 #ifndef HAVE_LIBCRYPTO
401 /* initiailize SAs */
402 if (ndo
->ndo_sa_list_head
== NULL
) {
403 if (!ndo
->ndo_espsecret
)
406 esp_print_decodesecret(ndo
);
409 if (ndo
->ndo_sa_list_head
== NULL
)
412 ip
= (struct ip
*)bp2
;
416 ip6
= (struct ip6_hdr
*)bp2
;
417 /* we do not attempt to decrypt jumbograms */
418 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
420 /* if we can't get nexthdr, we do not need to decrypt it */
421 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
423 /* see if we can find the SA, and if so, decode it */
424 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
425 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
426 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
427 sin6
->sin6_family
== AF_INET6
&&
428 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
429 sizeof(struct in6_addr
)) == 0) {
436 /* nexthdr & padding are in the last fragment */
437 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
439 len
= EXTRACT_16BITS(&ip
->ip_len
);
441 /* see if we can find the SA, and if so, decode it */
442 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
443 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
444 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
445 sin
->sin_family
== AF_INET
&&
446 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
455 /* if we didn't find the specific one, then look for
456 * an unspecified one.
459 sa
= ndo
->ndo_sa_default
;
461 /* if not found fail */
465 /* if we can't get nexthdr, we do not need to decrypt it */
468 if (ep
- bp2
> len
) {
469 /* FCS included at end of frame (NetBSD 1.6 or later) */
473 ivoff
= (u_char
*)(esp
+ 1) + 0;
476 espsecret_keylen
= sa
->secretlen
;
477 ep
= ep
- sa
->authlen
;
480 memset(&ctx
, 0, sizeof(ctx
));
481 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
482 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
484 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
487 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
488 EVP_Cipher(&ctx
, p
+ ivlen
, 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;
504 (ndo
->ndo_printf
)(ndo
, ": ");
514 * c-style: whitesmith