]>
The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
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 int esp_print_decrypt_buffer_by_ikev2(netdissect_options
*ndo
,
98 u_char spii
[8], u_char spir
[8],
99 u_char
*buf
, u_char
*end
)
106 /* initiator arg is any non-zero value */
107 if(initiator
) initiator
=1;
109 /* see if we can find the SA, and if so, decode it */
110 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
112 && initiator
== sa
->initiator
113 && memcmp(spii
, sa
->spii
, 8) == 0
114 && memcmp(spir
, sa
->spir
, 8) == 0)
118 if(sa
== NULL
) return 0;
119 if(sa
->evp
== NULL
) return 0;
122 * remove authenticator, and see if we still have something to
125 end
= end
- sa
->authlen
;
127 buf
= buf
+ sa
->ivlen
;
130 if(end
<= buf
) return 0;
132 memset(&ctx
, 0, sizeof(ctx
));
133 if (EVP_CipherInit(&ctx
, sa
->evp
, sa
->secret
, NULL
, 0) < 0)
134 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
135 EVP_CipherInit(&ctx
, NULL
, NULL
, iv
, 0);
136 EVP_Cipher(&ctx
, buf
, buf
, len
);
137 EVP_CIPHER_CTX_cleanup(&ctx
);
139 ndo
->ndo_packetp
= buf
;
140 ndo
->ndo_snapend
= end
;
146 static void esp_print_addsa(netdissect_options
*ndo
,
147 struct sa_list
*sa
, int sa_def
)
153 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
155 (*ndo
->ndo_error
)(ndo
, "ran out of memory to allocate sa structure");
160 ndo
->ndo_sa_default
= nsa
;
162 nsa
->next
= ndo
->ndo_sa_list_head
;
163 ndo
->ndo_sa_list_head
= nsa
;
167 static u_int
hexdigit(netdissect_options
*ndo
, char hex
)
169 if (hex
>= '0' && hex
<= '9')
171 else if (hex
>= 'A' && hex
<= 'F')
172 return (hex
- 'A' + 10);
173 else if (hex
>= 'a' && hex
<= 'f')
174 return (hex
- 'a' + 10);
176 (*ndo
->ndo_error
)(ndo
, "invalid hex digit %c in espsecret\n", hex
);
181 static u_int
hex2byte(netdissect_options
*ndo
, char *hexstring
)
185 byte
= (hexdigit(ndo
, hexstring
[0]) << 4) + hexdigit(ndo
, hexstring
[1]);
190 * returns size of binary, 0 on failure.
193 int espprint_decode_hex(netdissect_options
*ndo
,
194 u_char
*binbuf
, unsigned int binbuf_len
,
200 len
= strlen(hex
) / 2;
202 if (len
> binbuf_len
) {
203 (*ndo
->ndo_warning
)(ndo
, "secret is too big: %d\n", len
);
208 while (hex
[0] != '\0' && hex
[1]!='\0') {
209 binbuf
[i
] = hex2byte(ndo
, hex
);
218 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
222 espprint_decode_encalgo(netdissect_options
*ndo
,
223 char *decode
, struct sa_list
*sa
)
227 const EVP_CIPHER
*evp
;
231 colon
= strchr(decode
, ':');
233 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
238 len
= colon
- decode
;
239 if (strlen(decode
) > strlen("-hmac96") &&
240 !strcmp(decode
+ strlen(decode
) - strlen("-hmac96"),
242 p
= strstr(decode
, "-hmac96");
246 if (strlen(decode
) > strlen("-cbc") &&
247 !strcmp(decode
+ strlen(decode
) - strlen("-cbc"), "-cbc")) {
248 p
= strstr(decode
, "-cbc");
251 evp
= EVP_get_cipherbyname(decode
);
254 (*ndo
->ndo_warning
)(ndo
, "failed to find cipher algo %s\n", decode
);
262 sa
->authlen
= authlen
;
263 sa
->ivlen
= EVP_CIPHER_iv_length(evp
);
266 if (colon
[0] == '0' && colon
[1] == 'x') {
267 /* decode some hex! */
270 sa
->secretlen
= espprint_decode_hex(ndo
, sa
->secret
, sizeof(sa
->secret
), colon
);
271 if(sa
->secretlen
== 0) return 0;
275 if (i
< sizeof(sa
->secret
)) {
276 memcpy(sa
->secret
, colon
, i
);
279 memcpy(sa
->secret
, colon
, sizeof(sa
->secret
));
280 sa
->secretlen
= sizeof(sa
->secret
);
288 * for the moment, ignore the auth algorith, just hard code the authenticator
289 * length. Need to research how openssl looks up HMAC stuff.
292 espprint_decode_authalgo(netdissect_options
*ndo
,
293 char *decode
, struct sa_list
*sa
)
297 colon
= strchr(decode
, ':');
299 (*ndo
->ndo_warning
)(ndo
, "failed to decode espsecret: %s\n", decode
);
304 if(strcasecmp(colon
,"sha1") == 0 ||
305 strcasecmp(colon
,"md5") == 0) {
311 static void esp_print_decode_ikeline(netdissect_options
*ndo
, char *line
,
312 const char *file
, int lineno
)
314 /* it's an IKEv2 secret, store it instead */
318 char *icookie
, *rcookie
;
323 init
= strsep(&line
, " \t");
324 icookie
= strsep(&line
, " \t");
325 rcookie
= strsep(&line
, " \t");
326 authkey
= strsep(&line
, " \t");
327 enckey
= strsep(&line
, " \t");
329 /* if any fields are missing */
330 if(!init
|| !icookie
|| !rcookie
|| !authkey
|| !enckey
) {
331 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to find all fields for ikev2 at %s:%u",
337 ilen
= strlen(icookie
);
338 rlen
= strlen(rcookie
);
340 if((init
[0]!='I' && init
[0]!='R')
341 || icookie
[0]!='0' || icookie
[1]!='x'
342 || rcookie
[0]!='0' || rcookie
[1]!='x'
345 (*ndo
->ndo_warning
)(ndo
, "print_esp: line %s:%u improperly formatted.",
348 (*ndo
->ndo_warning
)(ndo
, "init=%s icookie=%s(%u) rcookie=%s(%u)",
349 init
, icookie
, ilen
, rcookie
, rlen
);
355 sa1
.initiator
= (init
[0] == 'I');
356 if(espprint_decode_hex(ndo
, sa1
.spii
, sizeof(sa1
.spii
), icookie
+2)!=8)
359 if(espprint_decode_hex(ndo
, sa1
.spir
, sizeof(sa1
.spir
), rcookie
+2)!=8)
362 if(!espprint_decode_encalgo(ndo
, enckey
, &sa1
)) return;
364 if(!espprint_decode_authalgo(ndo
, authkey
, &sa1
)) return;
366 esp_print_addsa(ndo
, &sa1
, FALSE
);
371 * special form: file /name
372 * causes us to go read from this file instead.
375 static void esp_print_decode_onesecret(netdissect_options
*ndo
, char *line
,
376 const char *file
, int lineno
)
384 spikey
= strsep(&line
, " \t");
386 memset(&sa1
, 0, sizeof(struct sa_list
));
388 /* if there is only one token, then it is an algo:key token */
392 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
398 if (spikey
&& strcasecmp(spikey
, "file") == 0) {
399 /* open file and read it */
404 char *filename
= line
;
406 secretfile
= fopen(filename
, FOPEN_READ_TXT
);
407 if (secretfile
== NULL
) {
412 while (fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
414 /* remove newline from the line */
415 nl
= strchr(fileline
, '\n');
418 if (fileline
[0] == '#') continue;
419 if (fileline
[0] == '\0') continue;
421 esp_print_decode_onesecret(ndo
, fileline
, filename
, lineno
);
428 if (spikey
&& strcasecmp(spikey
, "ikev2") == 0) {
429 esp_print_decode_ikeline(ndo
, line
, file
, lineno
);
437 struct sockaddr_in
*sin
;
439 struct sockaddr_in6
*sin6
;
442 spistr
= strsep(&spikey
, "@");
444 spino
= strtoul(spistr
, &foo
, 0);
445 if (spistr
== foo
|| !spikey
) {
446 (*ndo
->ndo_warning
)(ndo
, "print_esp: failed to decode spi# %s\n", foo
);
452 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
454 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
455 if (inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
456 #ifdef HAVE_SOCKADDR_SA_LEN
457 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
459 sin6
->sin6_family
= AF_INET6
;
462 if (inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
463 #ifdef HAVE_SOCKADDR_SA_LEN
464 sin
->sin_len
= sizeof(struct sockaddr_in
);
466 sin
->sin_family
= AF_INET
;
468 (*ndo
->ndo_warning
)(ndo
, "print_esp: can not decode IP# %s\n", spikey
);
474 /* skip any blank spaces */
475 while (isspace((unsigned char)*decode
))
478 if(!espprint_decode_encalgo(ndo
, decode
, &sa1
)) {
483 esp_print_addsa(ndo
, &sa1
, sa_def
);
486 static void esp_init(netdissect_options
*ndo _U_
)
489 OpenSSL_add_all_algorithms();
490 EVP_add_cipher_alias(SN_des_ede3_cbc
, "3des");
493 void esp_print_decodesecret(netdissect_options
*ndo
)
497 static int initialized
= 0;
504 p
= ndo
->ndo_espsecret
;
506 while (p
&& p
[0] != '\0') {
507 /* pick out the first line or first thing until a comma */
508 if ((line
= strsep(&p
, "\n,")) == NULL
) {
513 esp_print_decode_onesecret(ndo
, line
, "cmdline", 0);
516 ndo
->ndo_espsecret
= NULL
;
522 esp_print(netdissect_options
*ndo
,
523 const u_char
*bp
, const int length
, const u_char
*bp2
524 #ifndef HAVE_LIBCRYPTO
529 #ifndef HAVE_LIBCRYPTO
534 #ifndef HAVE_LIBCRYPTO
539 register const struct newesp
*esp
;
540 register const u_char
*ep
;
541 #ifdef HAVE_LIBCRYPTO
543 struct sa_list
*sa
= NULL
;
544 int espsecret_keylen
;
546 struct ip6_hdr
*ip6
= NULL
;
558 esp
= (struct newesp
*)bp
;
560 #ifdef HAVE_LIBCRYPTO
566 /* keep secret out of a register */
567 p
= (u_char
*)&secret
;
570 /* 'ep' points to the end of available data. */
571 ep
= ndo
->ndo_snapend
;
573 if ((u_char
*)(esp
+ 1) >= ep
) {
574 fputs("[|ESP]", stdout
);
577 (*ndo
->ndo_printf
)(ndo
, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
578 (*ndo
->ndo_printf
)(ndo
, ",seq=0x%x)", EXTRACT_32BITS(&esp
->esp_seq
));
579 (*ndo
->ndo_printf
)(ndo
, ", length %u", length
);
581 #ifndef HAVE_LIBCRYPTO
584 /* initiailize SAs */
585 if (ndo
->ndo_sa_list_head
== NULL
) {
586 if (!ndo
->ndo_espsecret
)
589 esp_print_decodesecret(ndo
);
592 if (ndo
->ndo_sa_list_head
== NULL
)
595 ip
= (struct ip
*)bp2
;
599 ip6
= (struct ip6_hdr
*)bp2
;
600 /* we do not attempt to decrypt jumbograms */
601 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
603 /* if we can't get nexthdr, we do not need to decrypt it */
604 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
606 /* see if we can find the SA, and if so, decode it */
607 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
608 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
609 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
610 sin6
->sin6_family
== AF_INET6
&&
611 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
612 sizeof(struct in6_addr
)) == 0) {
619 /* nexthdr & padding are in the last fragment */
620 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
622 len
= EXTRACT_16BITS(&ip
->ip_len
);
624 /* see if we can find the SA, and if so, decode it */
625 for (sa
= ndo
->ndo_sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
626 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
627 if (sa
->spi
== EXTRACT_32BITS(&esp
->esp_spi
) &&
628 sin
->sin_family
== AF_INET
&&
629 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
638 /* if we didn't find the specific one, then look for
639 * an unspecified one.
642 sa
= ndo
->ndo_sa_default
;
644 /* if not found fail */
648 /* if we can't get nexthdr, we do not need to decrypt it */
651 if (ep
- bp2
> len
) {
652 /* FCS included at end of frame (NetBSD 1.6 or later) */
656 ivoff
= (u_char
*)(esp
+ 1) + 0;
659 espsecret_keylen
= sa
->secretlen
;
660 ep
= ep
- sa
->authlen
;
663 memset(&ctx
, 0, sizeof(ctx
));
664 if (EVP_CipherInit(&ctx
, sa
->evp
, secret
, NULL
, 0) < 0)
665 (*ndo
->ndo_warning
)(ndo
, "espkey init failed");
667 blocksz
= EVP_CIPHER_CTX_block_size(&ctx
);
670 EVP_CipherInit(&ctx
, NULL
, NULL
, p
, 0);
671 EVP_Cipher(&ctx
, p
+ ivlen
, p
+ ivlen
, ep
- (p
+ ivlen
));
672 EVP_CIPHER_CTX_cleanup(&ctx
);
673 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
675 advance
= sizeof(struct newesp
);
677 /* sanity check for pad length */
678 if (ep
- bp
< *(ep
- 2))
682 *padlen
= *(ep
- 2) + 2;
687 (ndo
->ndo_printf
)(ndo
, ": ");
697 * c-style: whitesmith