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>
40 #ifdef HAVE_OPENSSL_EVP_H
41 #include <openssl/evp.h>
53 #include "netdissect.h"
54 #include "addrtoname.h"
57 #ifndef HAVE_SOCKADDR_STORAGE
59 struct sockaddr_storage
{
61 struct sockaddr_in sin
;
62 struct sockaddr_in6 sin6
;
66 #define sockaddr_storage sockaddr
68 #endif /* HAVE_SOCKADDR_STORAGE */
73 struct sockaddr_storage daddr
;
74 u_int32_t spi
; /* if == 0, then IKEv2 */
76 u_char spii
[8]; /* for IKEv2 */
78 const EVP_CIPHER
*evp
;
81 u_char authsecret
[256];
83 u_char secret
[256]; /* is that big enough for all secrets? */
88 * this will adjust ndo_packetp and ndo_snapend to new buffer!
90 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
92 u_char spii
[8], u_char spir
[8],
93 u_char
*buf
, u_char
*end
)
100 /* initiator arg is any non-zero value */
101 if(initiator
) initiator
=1;
103 /* see if we can find the SA, and if so, decode it */
104 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
106 && initiator
== sa
->initiator
107 && memcmp(spii
, sa
->spii
, 8) == 0
108 && memcmp(spir
, sa
->spir
, 8) == 0)
112 if(sa
== NULL
) return 0;
113 if(sa
->evp
== NULL
) return 0;
116 * remove authenticator, and see if we still have something to
119 end
= end
- sa
->authlen
;
121 buf
= buf
+ sa
->ivlen
;
124 if(end
<= buf
) return 0;
126 memset(&ctx
, 0, sizeof(ctx
));
127 if (EVP_CipherInit(&ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0)
128 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
129 EVP_CipherInit(&ctx
, NULL
, NULL
, iv
, 0);
130 EVP_Cipher(&ctx
, buf
, buf
, len
);
131 EVP_CIPHER_CTX_cleanup(&ctx
);
133 ndo
->ndo_packetp
= buf
;
134 ndo
->ndo_snapend
= end
;
140 static void esp_print_addsa(netdissect_options
*ndo
,
141 struct sa_list
*sa
, int sa_def
)
147 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
149 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
154 ndo
->ndo_sa_default
= nsa
;
156 nsa
->next
= ndo
->ndo_sa_list_head
;
157 ndo
->ndo_sa_list_head
= nsa
;
161 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
163 if (hex
>= '0' && hex
<= '9')
165 else if (hex
>= 'A' && hex
<= 'F')
166 return (hex
- 'A' + 10);
167 else if (hex
>= 'a' && hex
<= 'f')
168 return (hex
- 'a' + 10);
170 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
175 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
179 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
184 * returns size of binary, 0 on failure.
187 int espprint_decode_hex(netdissect_options
*ndo
,
188 u_char
*binbuf
, unsigned int binbuf_len
,
194 len
= strlen(hex
) / 2;
196 if (len
> binbuf_len
) {
197 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
202 while (hex
[0] != '\0' && hex
[1]!='\0') {
203 binbuf
[i
] = hex2byte(ndo
, hex
);
212 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
216 espprint_decode_encalgo(netdissect_options
*ndo
,
217 char *decode
, struct sa_list
*sa
)
221 const EVP_CIPHER
*evp
;
225 colon
= strchr(decode
, ':');
227 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
232 len
= colon
- decode
;
233 if (strlen(decode
) > strlen("-hmac96") &&
234 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
236 p
= strstr(decode
, "-hmac96");
240 if (strlen(decode
) > strlen("-cbc") &&
241 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
242 p
= strstr(decode
, "-cbc");
245 evp
= EVP_get_cipherbyname(decode
);
248 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
256 sa
->authlen
= authlen
;
257 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
260 if (colon
[0] == '0' && colon
[1] == 'x') {
261 /* decode some hex! */
264 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
265 if(sa
->secretlen
== 0) return 0;
269 if (i
< sizeof(sa
->secret
)) {
270 memcpy(sa
->secret
, colon
, i
);
273 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
274 sa
->secretlen
= sizeof(sa
->secret
);
282 * for the moment, ignore the auth algorith, just hard code the authenticator
283 * length. Need to research how openssl looks up HMAC stuff.
286 espprint_decode_authalgo(netdissect_options
*ndo
,
287 char *decode
, struct sa_list
*sa
)
291 colon
= strchr(decode
, ':');
293 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
298 if(strcasecmp(colon
,"sha1") == 0 ||
299 strcasecmp(colon
,"md5") == 0) {
305 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
306 const char *file
, int lineno
)
308 /* it's an IKEv2 secret, store it instead */
312 char *icookie
, *rcookie
;
317 init
= strsep(&line
, " \t");
318 icookie
= strsep(&line
, " \t");
319 rcookie
= strsep(&line
, " \t");
320 authkey
= strsep(&line
, " \t");
321 enckey
= strsep(&line
, " \t");
323 /* if any fields are missing */
324 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
325 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
331 ilen
= strlen(icookie
);
332 rlen
= strlen(rcookie
);
334 if((init
[0]!='I' && init
[0]!='R')
335 || icookie
[0]!='0' || icookie
[1]!='x'
336 || rcookie
[0]!='0' || rcookie
[1]!='x'
339 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
342 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
343 init
, icookie
, ilen
, rcookie
, rlen
);
349 sa1
.initiator
= (init
[0] == 'I');
350 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
353 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
356 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
358 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
360 esp_print_addsa(ndo
, &sa1
, FALSE
);
365 * special form: file /name
366 * causes us to go read from this file instead.
369 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
370 const char *file
, int lineno
)
378 spikey
= strsep(&line
, " \t");
380 memset(&sa1
, 0, sizeof(struct sa_list
));
382 /* if there is only one token, then it is an algo:key token */
386 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
392 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
393 /* open file and read it */
398 char *filename
= line
;
400 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
401 if (secretfile
== NULL
) {
406 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
408 /* remove newline from the line */
409 nl
= strchr(fileline
, '\n');
412 if (fileline
[0] == '#') continue;
413 if (fileline
[0] == '\0') continue;
415 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
422 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
423 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
431 struct sockaddr_in
*sin
;
433 struct sockaddr_in6
*sin6
;
436 spistr
= strsep(&spikey
, "@");
438 spino
= strtoul(spistr
, &foo
, 0);
439 if (spistr
== foo
|| !spikey
) {
440 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
446 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
448 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
449 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
450 #ifdef HAVE_SOCKADDR_SA_LEN
451 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
453 sin6
->sin6_family
= AF_INET6
;
456 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
457 #ifdef HAVE_SOCKADDR_SA_LEN
458 sin
->sin_len
= sizeof(struct sockaddr_in
);
460 sin
->sin_family
= AF_INET
;
462 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
468 /* skip any blank spaces */
469 while (isspace((unsigned char)*decode
))
472 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
477 esp_print_addsa(ndo
, &sa1
, sa_def
);
480 static void esp_init(netdissect_options
*ndo _U_
)
483 OpenSSL_add_all_algorithms();
484 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
487 void esp_print_decodesecret(netdissect_options
*ndo
)
491 static int initialized
= 0;
498 p
= ndo
->ndo_espsecret
;
500 while (p
&& p
[0] != '\0') {
501 /* pick out the first line or first thing until a comma */
502 if ((line
= strsep(&p
, "\n,")) == NULL
) {
507 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
510 ndo
->ndo_espsecret
= NULL
;
516 esp_print(netdissect_options
*ndo
,
517 const u_char
*bp
, const int length
, const u_char
*bp2
518 #ifndef HAVE_LIBCRYPTO
523 #ifndef HAVE_LIBCRYPTO
528 #ifndef HAVE_LIBCRYPTO
533 register const struct newesp
*esp
;
534 register const u_char
*ep
;
535 #ifdef HAVE_LIBCRYPTO
537 struct sa_list
*sa
= NULL
;
538 int espsecret_keylen
;
540 struct ip6_hdr
*ip6
= NULL
;
552 esp
= (struct newesp
*)bp
;
554 #ifdef HAVE_LIBCRYPTO
560 /* keep secret out of a register */
561 p
= (u_char
*)&secret
;
564 /* 'ep' points to the end of available data. */
565 ep
= ndo
->ndo_snapend
;
567 if ((u_char
*)(esp
+ 1) >= ep
) {
568 fputs("[|ESP]", stdout
);
571 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
572 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
573 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
575 #ifndef HAVE_LIBCRYPTO
578 /* initiailize SAs */
579 if (ndo
->ndo_sa_list_head
== NULL
) {
580 if (!ndo
->ndo_espsecret
)
583 esp_print_decodesecret(ndo
);
586 if (ndo
->ndo_sa_list_head
== NULL
)
589 ip
= (struct ip
*)bp2
;
593 ip6
= (struct ip6_hdr
*)bp2
;
594 /* we do not attempt to decrypt jumbograms */
595 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
597 /* if we can't get nexthdr, we do not need to decrypt it */
598 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
600 /* see if we can find the SA, and if so, decode it */
601 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
602 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
603 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
604 sin6
->sin6_family
== AF_INET6
&&
605 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
606 sizeof(struct in6_addr
)) == 0) {
613 /* nexthdr & padding are in the last fragment */
614 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
616 len
= EXTRACT_16BITS(&ip
->ip_len
);
618 /* see if we can find the SA, and if so, decode it */
619 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
620 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
621 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
622 sin
->sin_family
== AF_INET
&&
623 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
632 /* if we didn't find the specific one, then look for
633 * an unspecified one.
636 sa
= ndo
->ndo_sa_default
;
638 /* if not found fail */
642 /* if we can't get nexthdr, we do not need to decrypt it */
645 if (ep
- bp2
> len
) {
646 /* FCS included at end of frame (NetBSD 1.6 or later) */
650 ivoff
= (u_char
*)(esp
+ 1) + 0;
653 espsecret_keylen
= sa
->secretlen
;
654 ep
= ep
- sa
->authlen
;
657 memset(&ctx
, 0, sizeof(ctx
));
658 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
659 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
661 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
664 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
665 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
666 EVP_CIPHER_CTX_cleanup(&ctx
);
667 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
669 advance
= sizeof(struct newesp
);
671 /* sanity check for pad length */
672 if (ep
- bp
< *(ep
- 2))
676 *padlen
= *(ep
- 2) + 2;
681 (ndo
->ndo_printf
)(ndo
, ": ");
691 * c-style: whitesmith