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.58 2007-12-07 00:03:07 mcr Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
39 /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
40 * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
41 * is the simplest way of handling the dependency.
44 #ifdef HAVE_OPENSSL_EVP_H
45 #include <openssl/evp.h>
59 #include "netdissect.h"
60 #include "addrtoname.h"
63 #ifndef HAVE_SOCKADDR_STORAGE
65 struct sockaddr_storage
{
67 struct sockaddr_in sin
;
68 struct sockaddr_in6 sin6
;
72 #define sockaddr_storage sockaddr
74 #endif /* HAVE_SOCKADDR_STORAGE */
79 struct sockaddr_storage daddr
;
80 u_int32_t spi
; /* if == 0, then IKEv2 */
82 u_char spii
[8]; /* for IKEv2 */
84 const EVP_CIPHER
*evp
;
87 u_char authsecret
[256];
89 u_char secret
[256]; /* is that big enough for all secrets? */
94 * this will adjust ndo_packetp and ndo_snapend to new buffer!
96 USES_APPLE_DEPRECATED_API
97 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
99 u_char spii
[8], u_char spir
[8],
100 u_char
*buf
, u_char
*end
)
107 /* initiator arg is any non-zero value */
108 if(initiator
) initiator
=1;
110 /* see if we can find the SA, and if so, decode it */
111 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
113 && initiator
== sa
->initiator
114 && memcmp(spii
, sa
->spii
, 8) == 0
115 && memcmp(spir
, sa
->spir
, 8) == 0)
119 if(sa
== NULL
) return 0;
120 if(sa
->evp
== NULL
) return 0;
123 * remove authenticator, and see if we still have something to
126 end
= end
- sa
->authlen
;
128 buf
= buf
+ sa
->ivlen
;
131 if(end
<= buf
) return 0;
133 memset(&ctx
, 0, sizeof(ctx
));
134 if (EVP_CipherInit(&ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0)
135 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
136 EVP_CipherInit(&ctx
, NULL
, NULL
, iv
, 0);
137 EVP_Cipher(&ctx
, buf
, buf
, len
);
138 EVP_CIPHER_CTX_cleanup(&ctx
);
140 ndo
->ndo_packetp
= buf
;
141 ndo
->ndo_snapend
= end
;
148 static void esp_print_addsa(netdissect_options
*ndo
,
149 struct sa_list
*sa
, int sa_def
)
155 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
157 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
162 ndo
->ndo_sa_default
= nsa
;
164 nsa
->next
= ndo
->ndo_sa_list_head
;
165 ndo
->ndo_sa_list_head
= nsa
;
169 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
171 if (hex
>= '0' && hex
<= '9')
173 else if (hex
>= 'A' && hex
<= 'F')
174 return (hex
- 'A' + 10);
175 else if (hex
>= 'a' && hex
<= 'f')
176 return (hex
- 'a' + 10);
178 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
183 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
187 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
192 * returns size of binary, 0 on failure.
195 int espprint_decode_hex(netdissect_options
*ndo
,
196 u_char
*binbuf
, unsigned int binbuf_len
,
202 len
= strlen(hex
) / 2;
204 if (len
> binbuf_len
) {
205 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
210 while (hex
[0] != '\0' && hex
[1]!='\0') {
211 binbuf
[i
] = hex2byte(ndo
, hex
);
220 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
223 USES_APPLE_DEPRECATED_API
225 espprint_decode_encalgo(netdissect_options
*ndo
,
226 char *decode
, struct sa_list
*sa
)
230 const EVP_CIPHER
*evp
;
234 colon
= strchr(decode
, ':');
236 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
241 len
= colon
- decode
;
242 if (strlen(decode
) > strlen("-hmac96") &&
243 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
245 p
= strstr(decode
, "-hmac96");
249 if (strlen(decode
) > strlen("-cbc") &&
250 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
251 p
= strstr(decode
, "-cbc");
254 evp
= EVP_get_cipherbyname(decode
);
257 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
265 sa
->authlen
= authlen
;
266 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
269 if (colon
[0] == '0' && colon
[1] == 'x') {
270 /* decode some hex! */
273 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
274 if(sa
->secretlen
== 0) return 0;
278 if (i
< sizeof(sa
->secret
)) {
279 memcpy(sa
->secret
, colon
, i
);
282 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
283 sa
->secretlen
= sizeof(sa
->secret
);
292 * for the moment, ignore the auth algorith, just hard code the authenticator
293 * length. Need to research how openssl looks up HMAC stuff.
296 espprint_decode_authalgo(netdissect_options
*ndo
,
297 char *decode
, struct sa_list
*sa
)
301 colon
= strchr(decode
, ':');
303 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
308 if(strcasecmp(colon
,"sha1") == 0 ||
309 strcasecmp(colon
,"md5") == 0) {
315 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
316 const char *file
, int lineno
)
318 /* it's an IKEv2 secret, store it instead */
322 char *icookie
, *rcookie
;
327 init
= strsep(&line
, " \t");
328 icookie
= strsep(&line
, " \t");
329 rcookie
= strsep(&line
, " \t");
330 authkey
= strsep(&line
, " \t");
331 enckey
= strsep(&line
, " \t");
333 /* if any fields are missing */
334 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
335 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
341 ilen
= strlen(icookie
);
342 rlen
= strlen(rcookie
);
344 if((init
[0]!='I' && init
[0]!='R')
345 || icookie
[0]!='0' || icookie
[1]!='x'
346 || rcookie
[0]!='0' || rcookie
[1]!='x'
349 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
352 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
353 init
, icookie
, ilen
, rcookie
, rlen
);
359 sa1
.initiator
= (init
[0] == 'I');
360 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
363 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
366 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
368 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
370 esp_print_addsa(ndo
, &sa1
, FALSE
);
375 * special form: file /name
376 * causes us to go read from this file instead.
379 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
380 const char *file
, int lineno
)
388 spikey
= strsep(&line
, " \t");
390 memset(&sa1
, 0, sizeof(struct sa_list
));
392 /* if there is only one token, then it is an algo:key token */
396 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
402 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
403 /* open file and read it */
408 char *filename
= line
;
410 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
411 if (secretfile
== NULL
) {
416 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
418 /* remove newline from the line */
419 nl
= strchr(fileline
, '\n');
422 if (fileline
[0] == '#') continue;
423 if (fileline
[0] == '\0') continue;
425 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
432 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
433 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
441 struct sockaddr_in
*sin
;
443 struct sockaddr_in6
*sin6
;
446 spistr
= strsep(&spikey
, "@");
448 spino
= strtoul(spistr
, &foo
, 0);
449 if (spistr
== foo
|| !spikey
) {
450 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
456 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
458 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
459 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
460 #ifdef HAVE_SOCKADDR_SA_LEN
461 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
463 sin6
->sin6_family
= AF_INET6
;
466 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
467 #ifdef HAVE_SOCKADDR_SA_LEN
468 sin
->sin_len
= sizeof(struct sockaddr_in
);
470 sin
->sin_family
= AF_INET
;
472 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
478 /* skip any blank spaces */
479 while (isspace((unsigned char)*decode
))
482 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
487 esp_print_addsa(ndo
, &sa1
, sa_def
);
490 USES_APPLE_DEPRECATED_API
491 static void esp_init(netdissect_options
*ndo _U_
)
494 OpenSSL_add_all_algorithms();
495 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
499 void esp_print_decodesecret(netdissect_options
*ndo
)
503 static int initialized
= 0;
510 p
= ndo
->ndo_espsecret
;
512 while (p
&& p
[0] != '\0') {
513 /* pick out the first line or first thing until a comma */
514 if ((line
= strsep(&p
, "\n,")) == NULL
) {
519 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
522 ndo
->ndo_espsecret
= NULL
;
527 #ifdef HAVE_LIBCRYPTO
528 USES_APPLE_DEPRECATED_API
531 esp_print(netdissect_options
*ndo
,
532 const u_char
*bp
, const int length
, const u_char
*bp2
533 #ifndef HAVE_LIBCRYPTO
538 #ifndef HAVE_LIBCRYPTO
543 #ifndef HAVE_LIBCRYPTO
548 register const struct newesp
*esp
;
549 register const u_char
*ep
;
550 #ifdef HAVE_LIBCRYPTO
552 struct sa_list
*sa
= NULL
;
553 int espsecret_keylen
;
555 struct ip6_hdr
*ip6
= NULL
;
567 esp
= (struct newesp
*)bp
;
569 #ifdef HAVE_LIBCRYPTO
575 /* keep secret out of a register */
576 p
= (u_char
*)&secret
;
579 /* 'ep' points to the end of available data. */
580 ep
= ndo
->ndo_snapend
;
582 if ((u_char
*)(esp
+ 1) >= ep
) {
583 fputs("[|ESP]", stdout
);
586 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
587 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
588 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
590 #ifndef HAVE_LIBCRYPTO
593 /* initiailize SAs */
594 if (ndo
->ndo_sa_list_head
== NULL
) {
595 if (!ndo
->ndo_espsecret
)
598 esp_print_decodesecret(ndo
);
601 if (ndo
->ndo_sa_list_head
== NULL
)
604 ip
= (struct ip
*)bp2
;
608 ip6
= (struct ip6_hdr
*)bp2
;
609 /* we do not attempt to decrypt jumbograms */
610 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
612 /* if we can't get nexthdr, we do not need to decrypt it */
613 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
615 /* see if we can find the SA, and if so, decode it */
616 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
617 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
618 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
619 sin6
->sin6_family
== AF_INET6
&&
620 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
621 sizeof(struct in6_addr
)) == 0) {
628 /* nexthdr & padding are in the last fragment */
629 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
631 len
= EXTRACT_16BITS(&ip
->ip_len
);
633 /* see if we can find the SA, and if so, decode it */
634 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
635 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
636 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
637 sin
->sin_family
== AF_INET
&&
638 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
647 /* if we didn't find the specific one, then look for
648 * an unspecified one.
651 sa
= ndo
->ndo_sa_default
;
653 /* if not found fail */
657 /* if we can't get nexthdr, we do not need to decrypt it */
660 if (ep
- bp2
> len
) {
661 /* FCS included at end of frame (NetBSD 1.6 or later) */
665 ivoff
= (u_char
*)(esp
+ 1) + 0;
668 espsecret_keylen
= sa
->secretlen
;
669 ep
= ep
- sa
->authlen
;
672 memset(&ctx
, 0, sizeof(ctx
));
673 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
674 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
676 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
679 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
680 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
681 EVP_CIPHER_CTX_cleanup(&ctx
);
682 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
684 advance
= sizeof(struct newesp
);
686 /* sanity check for pad length */
687 if (ep
- bp
< *(ep
- 2))
691 *padlen
= *(ep
- 2) + 2;
696 (ndo
->ndo_printf
)(ndo
, ": ");
703 #ifdef HAVE_LIBCRYPTO
709 * c-style: whitesmith