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
)
229 const EVP_CIPHER
*evp
;
233 colon
= strchr(decode
, ':');
235 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
240 if (strlen(decode
) > strlen("-hmac96") &&
241 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
243 p
= strstr(decode
, "-hmac96");
247 if (strlen(decode
) > strlen("-cbc") &&
248 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
249 p
= strstr(decode
, "-cbc");
252 evp
= EVP_get_cipherbyname(decode
);
255 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
263 sa
->authlen
= authlen
;
264 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
267 if (colon
[0] == '0' && colon
[1] == 'x') {
268 /* decode some hex! */
271 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
272 if(sa
->secretlen
== 0) return 0;
276 if (i
< sizeof(sa
->secret
)) {
277 memcpy(sa
->secret
, colon
, i
);
280 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
281 sa
->secretlen
= sizeof(sa
->secret
);
290 * for the moment, ignore the auth algorith, just hard code the authenticator
291 * length. Need to research how openssl looks up HMAC stuff.
294 espprint_decode_authalgo(netdissect_options
*ndo
,
295 char *decode
, struct sa_list
*sa
)
299 colon
= strchr(decode
, ':');
301 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
306 if(strcasecmp(colon
,"sha1") == 0 ||
307 strcasecmp(colon
,"md5") == 0) {
313 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
314 const char *file
, int lineno
)
316 /* it's an IKEv2 secret, store it instead */
320 char *icookie
, *rcookie
;
325 init
= strsep(&line
, " \t");
326 icookie
= strsep(&line
, " \t");
327 rcookie
= strsep(&line
, " \t");
328 authkey
= strsep(&line
, " \t");
329 enckey
= strsep(&line
, " \t");
331 /* if any fields are missing */
332 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
333 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
339 ilen
= strlen(icookie
);
340 rlen
= strlen(rcookie
);
342 if((init
[0]!='I' && init
[0]!='R')
343 || icookie
[0]!='0' || icookie
[1]!='x'
344 || rcookie
[0]!='0' || rcookie
[1]!='x'
347 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
350 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
351 init
, icookie
, ilen
, rcookie
, rlen
);
357 sa1
.initiator
= (init
[0] == 'I');
358 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
361 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
364 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
366 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
368 esp_print_addsa(ndo
, &sa1
, FALSE
);
373 * special form: file /name
374 * causes us to go read from this file instead.
377 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
378 const char *file
, int lineno
)
386 spikey
= strsep(&line
, " \t");
388 memset(&sa1
, 0, sizeof(struct sa_list
));
390 /* if there is only one token, then it is an algo:key token */
394 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
400 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
401 /* open file and read it */
406 char *filename
= line
;
408 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
409 if (secretfile
== NULL
) {
414 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
416 /* remove newline from the line */
417 nl
= strchr(fileline
, '\n');
420 if (fileline
[0] == '#') continue;
421 if (fileline
[0] == '\0') continue;
423 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
430 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
431 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
439 struct sockaddr_in
*sin
;
441 struct sockaddr_in6
*sin6
;
444 spistr
= strsep(&spikey
, "@");
446 spino
= strtoul(spistr
, &foo
, 0);
447 if (spistr
== foo
|| !spikey
) {
448 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
454 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
456 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
457 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
458 #ifdef HAVE_SOCKADDR_SA_LEN
459 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
461 sin6
->sin6_family
= AF_INET6
;
464 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
465 #ifdef HAVE_SOCKADDR_SA_LEN
466 sin
->sin_len
= sizeof(struct sockaddr_in
);
468 sin
->sin_family
= AF_INET
;
470 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
476 /* skip any blank spaces */
477 while (isspace((unsigned char)*decode
))
480 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
485 esp_print_addsa(ndo
, &sa1
, sa_def
);
488 USES_APPLE_DEPRECATED_API
489 static void esp_init(netdissect_options
*ndo _U_
)
492 OpenSSL_add_all_algorithms();
493 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
497 void esp_print_decodesecret(netdissect_options
*ndo
)
501 static int initialized
= 0;
508 p
= ndo
->ndo_espsecret
;
510 while (p
&& p
[0] != '\0') {
511 /* pick out the first line or first thing until a comma */
512 if ((line
= strsep(&p
, "\n,")) == NULL
) {
517 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
520 ndo
->ndo_espsecret
= NULL
;
525 #ifdef HAVE_LIBCRYPTO
526 USES_APPLE_DEPRECATED_API
529 esp_print(netdissect_options
*ndo
,
530 const u_char
*bp
, const int length
, const u_char
*bp2
531 #ifndef HAVE_LIBCRYPTO
536 #ifndef HAVE_LIBCRYPTO
541 #ifndef HAVE_LIBCRYPTO
546 register const struct newesp
*esp
;
547 register const u_char
*ep
;
548 #ifdef HAVE_LIBCRYPTO
550 struct sa_list
*sa
= NULL
;
552 struct ip6_hdr
*ip6
= NULL
;
563 esp
= (struct newesp
*)bp
;
565 #ifdef HAVE_LIBCRYPTO
571 /* keep secret out of a register */
572 p
= (u_char
*)&secret
;
575 /* 'ep' points to the end of available data. */
576 ep
= ndo
->ndo_snapend
;
578 if ((u_char
*)(esp
+ 1) >= ep
) {
579 fputs("[|ESP]", stdout
);
582 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
583 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
584 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
586 #ifndef HAVE_LIBCRYPTO
589 /* initiailize SAs */
590 if (ndo
->ndo_sa_list_head
== NULL
) {
591 if (!ndo
->ndo_espsecret
)
594 esp_print_decodesecret(ndo
);
597 if (ndo
->ndo_sa_list_head
== NULL
)
600 ip
= (struct ip
*)bp2
;
604 ip6
= (struct ip6_hdr
*)bp2
;
605 /* we do not attempt to decrypt jumbograms */
606 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
608 /* if we can't get nexthdr, we do not need to decrypt it */
609 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
611 /* see if we can find the SA, and if so, decode it */
612 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
613 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
614 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
615 sin6
->sin6_family
== AF_INET6
&&
616 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
617 sizeof(struct in6_addr
)) == 0) {
624 /* nexthdr & padding are in the last fragment */
625 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
627 len
= EXTRACT_16BITS(&ip
->ip_len
);
629 /* see if we can find the SA, and if so, decode it */
630 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
631 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
632 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
633 sin
->sin_family
== AF_INET
&&
634 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
643 /* if we didn't find the specific one, then look for
644 * an unspecified one.
647 sa
= ndo
->ndo_sa_default
;
649 /* if not found fail */
653 /* if we can't get nexthdr, we do not need to decrypt it */
656 if (ep
- bp2
> len
) {
657 /* FCS included at end of frame (NetBSD 1.6 or later) */
661 ivoff
= (u_char
*)(esp
+ 1) + 0;
664 ep
= ep
- sa
->authlen
;
667 memset(&ctx
, 0, sizeof(ctx
));
668 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
669 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
672 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
673 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
674 EVP_CIPHER_CTX_cleanup(&ctx
);
675 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
677 advance
= sizeof(struct newesp
);
679 /* sanity check for pad length */
680 if (ep
- bp
< *(ep
- 2))
684 *padlen
= *(ep
- 2) + 2;
689 (ndo
->ndo_printf
)(ndo
, ": ");
696 #ifdef HAVE_LIBCRYPTO
702 * c-style: whitesmith