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.
30 #include <tcpdump-stdinc.h>
34 /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
35 * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
36 * is the simplest way of handling the dependency.
39 #ifdef HAVE_OPENSSL_EVP_H
40 #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 */
71 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
72 * All rights reserved.
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 * 3. Neither the name of the project nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
86 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
87 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
92 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
93 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
94 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
95 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100 * RFC1827/2406 Encapsulated Security Payload.
104 u_int32_t esp_spi
; /* ESP */
105 u_int32_t esp_seq
; /* Sequence number */
106 /*variable size*/ /* (IV and) Payload data */
107 /*variable size*/ /* padding */
108 /*8bit*/ /* pad size */
109 /*8bit*/ /* next header */
110 /*8bit*/ /* next header */
111 /*variable size, 32bit bound*/ /* Authentication data */
114 #ifdef HAVE_LIBCRYPTO
116 struct sa_list
*next
;
117 struct sockaddr_storage daddr
;
118 u_int32_t spi
; /* if == 0, then IKEv2 */
120 u_char spii
[8]; /* for IKEv2 */
122 const EVP_CIPHER
*evp
;
125 u_char authsecret
[256];
127 u_char secret
[256]; /* is that big enough for all secrets? */
132 * this will adjust ndo_packetp and ndo_snapend to new buffer!
134 USES_APPLE_DEPRECATED_API
135 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
137 u_char spii
[8], u_char spir
[8],
138 u_char
*buf
, u_char
*end
)
145 /* initiator arg is any non-zero value */
146 if(initiator
) initiator
=1;
148 /* see if we can find the SA, and if so, decode it */
149 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
151 && initiator
== sa
->initiator
152 && memcmp(spii
, sa
->spii
, 8) == 0
153 && memcmp(spir
, sa
->spir
, 8) == 0)
157 if(sa
== NULL
) return 0;
158 if(sa
->evp
== NULL
) return 0;
161 * remove authenticator, and see if we still have something to
164 end
= end
- sa
->authlen
;
166 buf
= buf
+ sa
->ivlen
;
169 if(end
<= buf
) return 0;
171 memset(&ctx
, 0, sizeof(ctx
));
172 if (EVP_CipherInit(&ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0)
173 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
174 EVP_CipherInit(&ctx
, NULL
, NULL
, iv
, 0);
175 EVP_Cipher(&ctx
, buf
, buf
, len
);
176 EVP_CIPHER_CTX_cleanup(&ctx
);
178 ndo
->ndo_packetp
= buf
;
179 ndo
->ndo_snapend
= end
;
186 static void esp_print_addsa(netdissect_options
*ndo
,
187 struct sa_list
*sa
, int sa_def
)
193 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
195 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
200 ndo
->ndo_sa_default
= nsa
;
202 nsa
->next
= ndo
->ndo_sa_list_head
;
203 ndo
->ndo_sa_list_head
= nsa
;
207 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
209 if (hex
>= '0' && hex
<= '9')
211 else if (hex
>= 'A' && hex
<= 'F')
212 return (hex
- 'A' + 10);
213 else if (hex
>= 'a' && hex
<= 'f')
214 return (hex
- 'a' + 10);
216 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
221 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
225 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
230 * returns size of binary, 0 on failure.
233 int espprint_decode_hex(netdissect_options
*ndo
,
234 u_char
*binbuf
, unsigned int binbuf_len
,
240 len
= strlen(hex
) / 2;
242 if (len
> binbuf_len
) {
243 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
248 while (hex
[0] != '\0' && hex
[1]!='\0') {
249 binbuf
[i
] = hex2byte(ndo
, hex
);
258 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
261 USES_APPLE_DEPRECATED_API
263 espprint_decode_encalgo(netdissect_options
*ndo
,
264 char *decode
, struct sa_list
*sa
)
267 const EVP_CIPHER
*evp
;
271 colon
= strchr(decode
, ':');
273 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
278 if (strlen(decode
) > strlen("-hmac96") &&
279 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
281 p
= strstr(decode
, "-hmac96");
285 if (strlen(decode
) > strlen("-cbc") &&
286 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
287 p
= strstr(decode
, "-cbc");
290 evp
= EVP_get_cipherbyname(decode
);
293 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
301 sa
->authlen
= authlen
;
302 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
305 if (colon
[0] == '0' && colon
[1] == 'x') {
306 /* decode some hex! */
309 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
310 if(sa
->secretlen
== 0) return 0;
314 if (i
< sizeof(sa
->secret
)) {
315 memcpy(sa
->secret
, colon
, i
);
318 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
319 sa
->secretlen
= sizeof(sa
->secret
);
328 * for the moment, ignore the auth algorith, just hard code the authenticator
329 * length. Need to research how openssl looks up HMAC stuff.
332 espprint_decode_authalgo(netdissect_options
*ndo
,
333 char *decode
, struct sa_list
*sa
)
337 colon
= strchr(decode
, ':');
339 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
344 if(strcasecmp(colon
,"sha1") == 0 ||
345 strcasecmp(colon
,"md5") == 0) {
351 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
352 const char *file
, int lineno
)
354 /* it's an IKEv2 secret, store it instead */
358 char *icookie
, *rcookie
;
363 init
= strsep(&line
, " \t");
364 icookie
= strsep(&line
, " \t");
365 rcookie
= strsep(&line
, " \t");
366 authkey
= strsep(&line
, " \t");
367 enckey
= strsep(&line
, " \t");
369 /* if any fields are missing */
370 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
371 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
377 ilen
= strlen(icookie
);
378 rlen
= strlen(rcookie
);
380 if((init
[0]!='I' && init
[0]!='R')
381 || icookie
[0]!='0' || icookie
[1]!='x'
382 || rcookie
[0]!='0' || rcookie
[1]!='x'
385 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
388 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
389 init
, icookie
, ilen
, rcookie
, rlen
);
395 sa1
.initiator
= (init
[0] == 'I');
396 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
399 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
402 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
404 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
406 esp_print_addsa(ndo
, &sa1
, FALSE
);
411 * special form: file /name
412 * causes us to go read from this file instead.
415 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
416 const char *file
, int lineno
)
424 spikey
= strsep(&line
, " \t");
426 memset(&sa1
, 0, sizeof(struct sa_list
));
428 /* if there is only one token, then it is an algo:key token */
432 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
438 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
439 /* open file and read it */
444 char *filename
= line
;
446 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
447 if (secretfile
== NULL
) {
452 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
454 /* remove newline from the line */
455 nl
= strchr(fileline
, '\n');
458 if (fileline
[0] == '#') continue;
459 if (fileline
[0] == '\0') continue;
461 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
468 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
469 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
477 struct sockaddr_in
*sin
;
479 struct sockaddr_in6
*sin6
;
482 spistr
= strsep(&spikey
, "@");
484 spino
= strtoul(spistr
, &foo
, 0);
485 if (spistr
== foo
|| !spikey
) {
486 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
492 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
494 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
495 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
496 #ifdef HAVE_SOCKADDR_SA_LEN
497 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
499 sin6
->sin6_family
= AF_INET6
;
502 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
503 #ifdef HAVE_SOCKADDR_SA_LEN
504 sin
->sin_len
= sizeof(struct sockaddr_in
);
506 sin
->sin_family
= AF_INET
;
508 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
514 /* skip any blank spaces */
515 while (isspace((unsigned char)*decode
))
518 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
523 esp_print_addsa(ndo
, &sa1
, sa_def
);
526 USES_APPLE_DEPRECATED_API
527 static void esp_init(netdissect_options
*ndo _U_
)
530 OpenSSL_add_all_algorithms();
531 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
535 void esp_print_decodesecret(netdissect_options
*ndo
)
539 static int initialized
= 0;
546 p
= ndo
->ndo_espsecret
;
548 while (p
&& p
[0] != '\0') {
549 /* pick out the first line or first thing until a comma */
550 if ((line
= strsep(&p
, "\n,")) == NULL
) {
555 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
558 ndo
->ndo_espsecret
= NULL
;
563 #ifdef HAVE_LIBCRYPTO
564 USES_APPLE_DEPRECATED_API
567 esp_print(netdissect_options
*ndo
,
568 const u_char
*bp
, const int length
, const u_char
*bp2
569 #ifndef HAVE_LIBCRYPTO
574 #ifndef HAVE_LIBCRYPTO
579 #ifndef HAVE_LIBCRYPTO
584 register const struct newesp
*esp
;
585 register const u_char
*ep
;
586 #ifdef HAVE_LIBCRYPTO
588 struct sa_list
*sa
= NULL
;
590 struct ip6_hdr
*ip6
= NULL
;
601 esp
= (struct newesp
*)bp
;
603 #ifdef HAVE_LIBCRYPTO
609 /* keep secret out of a register */
610 p
= (u_char
*)&secret
;
613 /* 'ep' points to the end of available data. */
614 ep
= ndo
->ndo_snapend
;
616 if ((u_char
*)(esp
+ 1) >= ep
) {
617 fputs("[|ESP]", stdout
);
620 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
621 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
622 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
624 #ifndef HAVE_LIBCRYPTO
627 /* initiailize SAs */
628 if (ndo
->ndo_sa_list_head
== NULL
) {
629 if (!ndo
->ndo_espsecret
)
632 esp_print_decodesecret(ndo
);
635 if (ndo
->ndo_sa_list_head
== NULL
)
638 ip
= (struct ip
*)bp2
;
642 ip6
= (struct ip6_hdr
*)bp2
;
643 /* we do not attempt to decrypt jumbograms */
644 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
646 /* if we can't get nexthdr, we do not need to decrypt it */
647 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
649 /* see if we can find the SA, and if so, decode it */
650 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
651 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
652 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
653 sin6
->sin6_family
== AF_INET6
&&
654 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
655 sizeof(struct in6_addr
)) == 0) {
662 /* nexthdr & padding are in the last fragment */
663 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
665 len
= EXTRACT_16BITS(&ip
->ip_len
);
667 /* see if we can find the SA, and if so, decode it */
668 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
669 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
670 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
671 sin
->sin_family
== AF_INET
&&
672 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
681 /* if we didn't find the specific one, then look for
682 * an unspecified one.
685 sa
= ndo
->ndo_sa_default
;
687 /* if not found fail */
691 /* if we can't get nexthdr, we do not need to decrypt it */
694 if (ep
- bp2
> len
) {
695 /* FCS included at end of frame (NetBSD 1.6 or later) */
699 ivoff
= (u_char
*)(esp
+ 1) + 0;
702 ep
= ep
- sa
->authlen
;
705 memset(&ctx
, 0, sizeof(ctx
));
706 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
707 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
710 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
711 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
712 EVP_CIPHER_CTX_cleanup(&ctx
);
713 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
715 advance
= sizeof(struct newesp
);
717 /* sanity check for pad length */
718 if (ep
- bp
< *(ep
- 2))
722 *padlen
= *(ep
- 2) + 2;
727 (ndo
->ndo_printf
)(ndo
, ": ");
734 #ifdef HAVE_LIBCRYPTO
740 * c-style: whitesmith