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>
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
;
79 u_int32_t spi
; /* if == 0, then IKEv2 */
81 u_char spii
[8]; /* for IKEv2 */
83 const EVP_CIPHER
*evp
;
86 u_char authsecret
[256];
88 u_char secret
[256]; /* is that big enough for all secrets? */
93 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
94 * All rights reserved.
96 * Redistribution and use in source and binary forms, with or without
97 * modification, are permitted provided that the following conditions
99 * 1. Redistributions of source code must retain the above copyright
100 * notice, this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright
102 * notice, this list of conditions and the following disclaimer in the
103 * documentation and/or other materials provided with the distribution.
104 * 3. Neither the name of the project nor the names of its contributors
105 * may be used to endorse or promote products derived from this software
106 * without specific prior written permission.
108 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
109 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
110 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
111 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
112 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
113 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
114 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
116 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
117 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
122 * RFC1827/2406 Encapsulated Security Payload.
126 u_int32_t esp_spi
; /* ESP */
127 u_int32_t esp_seq
; /* Sequence number */
128 /*variable size*/ /* (IV and) Payload data */
129 /*variable size*/ /* padding */
130 /*8bit*/ /* pad size */
131 /*8bit*/ /* next header */
132 /*8bit*/ /* next header */
133 /*variable size, 32bit bound*/ /* Authentication data */
137 * this will adjust ndo_packetp and ndo_snapend to new buffer!
139 USES_APPLE_DEPRECATED_API
140 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
142 u_char spii
[8], u_char spir
[8],
143 u_char
*buf
, u_char
*end
)
150 /* initiator arg is any non-zero value */
151 if(initiator
) initiator
=1;
153 /* see if we can find the SA, and if so, decode it */
154 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
156 && initiator
== sa
->initiator
157 && memcmp(spii
, sa
->spii
, 8) == 0
158 && memcmp(spir
, sa
->spir
, 8) == 0)
162 if(sa
== NULL
) return 0;
163 if(sa
->evp
== NULL
) return 0;
166 * remove authenticator, and see if we still have something to
169 end
= end
- sa
->authlen
;
171 buf
= buf
+ sa
->ivlen
;
174 if(end
<= buf
) return 0;
176 memset(&ctx
, 0, sizeof(ctx
));
177 if (EVP_CipherInit(&ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0)
178 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
179 EVP_CipherInit(&ctx
, NULL
, NULL
, iv
, 0);
180 EVP_Cipher(&ctx
, buf
, buf
, len
);
181 EVP_CIPHER_CTX_cleanup(&ctx
);
183 ndo
->ndo_packetp
= buf
;
184 ndo
->ndo_snapend
= end
;
191 static void esp_print_addsa(netdissect_options
*ndo
,
192 struct sa_list
*sa
, int sa_def
)
198 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
200 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
205 ndo
->ndo_sa_default
= nsa
;
207 nsa
->next
= ndo
->ndo_sa_list_head
;
208 ndo
->ndo_sa_list_head
= nsa
;
212 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
214 if (hex
>= '0' && hex
<= '9')
216 else if (hex
>= 'A' && hex
<= 'F')
217 return (hex
- 'A' + 10);
218 else if (hex
>= 'a' && hex
<= 'f')
219 return (hex
- 'a' + 10);
221 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
226 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
230 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
235 * returns size of binary, 0 on failure.
238 int espprint_decode_hex(netdissect_options
*ndo
,
239 u_char
*binbuf
, unsigned int binbuf_len
,
245 len
= strlen(hex
) / 2;
247 if (len
> binbuf_len
) {
248 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
253 while (hex
[0] != '\0' && hex
[1]!='\0') {
254 binbuf
[i
] = hex2byte(ndo
, hex
);
263 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
266 USES_APPLE_DEPRECATED_API
268 espprint_decode_encalgo(netdissect_options
*ndo
,
269 char *decode
, struct sa_list
*sa
)
272 const EVP_CIPHER
*evp
;
276 colon
= strchr(decode
, ':');
278 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
283 if (strlen(decode
) > strlen("-hmac96") &&
284 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
286 p
= strstr(decode
, "-hmac96");
290 if (strlen(decode
) > strlen("-cbc") &&
291 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
292 p
= strstr(decode
, "-cbc");
295 evp
= EVP_get_cipherbyname(decode
);
298 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
306 sa
->authlen
= authlen
;
307 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
310 if (colon
[0] == '0' && colon
[1] == 'x') {
311 /* decode some hex! */
314 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
315 if(sa
->secretlen
== 0) return 0;
319 if (i
< sizeof(sa
->secret
)) {
320 memcpy(sa
->secret
, colon
, i
);
323 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
324 sa
->secretlen
= sizeof(sa
->secret
);
333 * for the moment, ignore the auth algorith, just hard code the authenticator
334 * length. Need to research how openssl looks up HMAC stuff.
337 espprint_decode_authalgo(netdissect_options
*ndo
,
338 char *decode
, struct sa_list
*sa
)
342 colon
= strchr(decode
, ':');
344 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
349 if(strcasecmp(colon
,"sha1") == 0 ||
350 strcasecmp(colon
,"md5") == 0) {
356 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
357 const char *file
, int lineno
)
359 /* it's an IKEv2 secret, store it instead */
363 char *icookie
, *rcookie
;
368 init
= strsep(&line
, " \t");
369 icookie
= strsep(&line
, " \t");
370 rcookie
= strsep(&line
, " \t");
371 authkey
= strsep(&line
, " \t");
372 enckey
= strsep(&line
, " \t");
374 /* if any fields are missing */
375 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
376 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
382 ilen
= strlen(icookie
);
383 rlen
= strlen(rcookie
);
385 if((init
[0]!='I' && init
[0]!='R')
386 || icookie
[0]!='0' || icookie
[1]!='x'
387 || rcookie
[0]!='0' || rcookie
[1]!='x'
390 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
393 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
394 init
, icookie
, ilen
, rcookie
, rlen
);
400 sa1
.initiator
= (init
[0] == 'I');
401 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
404 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
407 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
409 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
411 esp_print_addsa(ndo
, &sa1
, FALSE
);
416 * special form: file /name
417 * causes us to go read from this file instead.
420 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
421 const char *file
, int lineno
)
429 spikey
= strsep(&line
, " \t");
431 memset(&sa1
, 0, sizeof(struct sa_list
));
433 /* if there is only one token, then it is an algo:key token */
437 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
443 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
444 /* open file and read it */
449 char *filename
= line
;
451 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
452 if (secretfile
== NULL
) {
457 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
459 /* remove newline from the line */
460 nl
= strchr(fileline
, '\n');
463 if (fileline
[0] == '#') continue;
464 if (fileline
[0] == '\0') continue;
466 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
473 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
474 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
482 struct sockaddr_in
*sin
;
484 struct sockaddr_in6
*sin6
;
487 spistr
= strsep(&spikey
, "@");
489 spino
= strtoul(spistr
, &foo
, 0);
490 if (spistr
== foo
|| !spikey
) {
491 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
497 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
499 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
500 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
501 #ifdef HAVE_SOCKADDR_SA_LEN
502 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
504 sin6
->sin6_family
= AF_INET6
;
507 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
508 #ifdef HAVE_SOCKADDR_SA_LEN
509 sin
->sin_len
= sizeof(struct sockaddr_in
);
511 sin
->sin_family
= AF_INET
;
513 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
519 /* skip any blank spaces */
520 while (isspace((unsigned char)*decode
))
523 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
528 esp_print_addsa(ndo
, &sa1
, sa_def
);
531 USES_APPLE_DEPRECATED_API
532 static void esp_init(netdissect_options
*ndo _U_
)
535 OpenSSL_add_all_algorithms();
536 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
540 void esp_print_decodesecret(netdissect_options
*ndo
)
544 static int initialized
= 0;
551 p
= ndo
->ndo_espsecret
;
553 while (p
&& p
[0] != '\0') {
554 /* pick out the first line or first thing until a comma */
555 if ((line
= strsep(&p
, "\n,")) == NULL
) {
560 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
563 ndo
->ndo_espsecret
= NULL
;
568 #ifdef HAVE_LIBCRYPTO
569 USES_APPLE_DEPRECATED_API
572 esp_print(netdissect_options
*ndo
,
573 const u_char
*bp
, const int length
, const u_char
*bp2
574 #ifndef HAVE_LIBCRYPTO
579 #ifndef HAVE_LIBCRYPTO
584 #ifndef HAVE_LIBCRYPTO
589 register const struct newesp
*esp
;
590 register const u_char
*ep
;
591 #ifdef HAVE_LIBCRYPTO
593 struct sa_list
*sa
= NULL
;
595 struct ip6_hdr
*ip6
= NULL
;
606 esp
= (struct newesp
*)bp
;
608 #ifdef HAVE_LIBCRYPTO
614 /* keep secret out of a register */
615 p
= (u_char
*)&secret
;
618 /* 'ep' points to the end of available data. */
619 ep
= ndo
->ndo_snapend
;
621 if ((u_char
*)(esp
+ 1) >= ep
) {
622 fputs("[|ESP]", stdout
);
625 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
626 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
627 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
629 #ifndef HAVE_LIBCRYPTO
632 /* initiailize SAs */
633 if (ndo
->ndo_sa_list_head
== NULL
) {
634 if (!ndo
->ndo_espsecret
)
637 esp_print_decodesecret(ndo
);
640 if (ndo
->ndo_sa_list_head
== NULL
)
643 ip
= (struct ip
*)bp2
;
647 ip6
= (struct ip6_hdr
*)bp2
;
648 /* we do not attempt to decrypt jumbograms */
649 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
651 /* if we can't get nexthdr, we do not need to decrypt it */
652 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
654 /* see if we can find the SA, and if so, decode it */
655 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
656 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
657 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
658 sin6
->sin6_family
== AF_INET6
&&
659 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
660 sizeof(struct in6_addr
)) == 0) {
667 /* nexthdr & padding are in the last fragment */
668 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
670 len
= EXTRACT_16BITS(&ip
->ip_len
);
672 /* see if we can find the SA, and if so, decode it */
673 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
674 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
675 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
676 sin
->sin_family
== AF_INET
&&
677 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
686 /* if we didn't find the specific one, then look for
687 * an unspecified one.
690 sa
= ndo
->ndo_sa_default
;
692 /* if not found fail */
696 /* if we can't get nexthdr, we do not need to decrypt it */
699 if (ep
- bp2
> len
) {
700 /* FCS included at end of frame (NetBSD 1.6 or later) */
704 ivoff
= (u_char
*)(esp
+ 1) + 0;
707 ep
= ep
- sa
->authlen
;
710 memset(&ctx
, 0, sizeof(ctx
));
711 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
712 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
715 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
716 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
717 EVP_CIPHER_CTX_cleanup(&ctx
);
718 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
720 advance
= sizeof(struct newesp
);
722 /* sanity check for pad length */
723 if (ep
- bp
< *(ep
- 2))
727 *padlen
= *(ep
- 2) + 2;
732 (ndo
->ndo_printf
)(ndo
, ": ");
739 #ifdef HAVE_LIBCRYPTO
745 * c-style: whitesmith