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
[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.39 2003-05-02 08:43:28 guy Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
40 #include <openssl/des.h>
41 #include <openssl/blowfish.h>
42 #ifdef HAVE_OPENSSL_RC5_H
43 #include <openssl/rc5.h>
45 #ifdef HAVE_OPENSSL_CAST_H
46 #include <openssl/cast.h>
58 #if defined(__MINGW32__) || defined(__WATCOMC__)
59 extern char *strsep (char **stringp
, const char *delim
); /* Missing/strsep.c */
63 #include "interface.h"
64 #include "addrtoname.h"
76 struct esp_algorithm
{
81 int replaysize
; /* number of bytes, in excess of 4,
85 struct esp_algorithm esp_xforms
[]={
86 {"none", NONE
, 0, 0, 0},
87 {"des-cbc", DESCBC
, 8, 0, 0},
88 {"des-cbc-hmac96", DESCBC
, 8, 12, 0},
89 {"blowfish-cbc", BLOWFISH
,8, 0, 0},
90 {"blowfish-cbc-hmac96", BLOWFISH
,8, 12, 0},
91 {"rc5-cbc", RC5
, 8, 0, 0},
92 {"rc5-cbc-hmac96", RC5
, 8, 12, 0},
93 {"cast128-cbc", CAST128
, 8, 0, 0},
94 {"cast128-cbc-hmac96", CAST128
, 8, 12, 0},
95 {"3des-cbc-hmac96", DES3CBC
, 8, 12, 0},
99 struct esp_algorithm
*null_xf
=
100 &esp_xforms
[sizeof(esp_xforms
)/sizeof(esp_xforms
[0]) - 1];
102 #ifndef HAVE_SOCKADDR_STORAGE
104 struct sockaddr_storage
{
106 struct sockaddr_in sin
;
107 struct sockaddr_in6 sin6
;
111 #define sockaddr_storage sockaddr
113 #endif /* HAVE_SOCKADDR_STORAGE */
116 struct sa_list
*next
;
117 struct sockaddr_storage daddr
;
119 struct esp_algorithm
*xform
;
120 char secret
[256]; /* is that big enough for all secrets? */
124 static struct sa_list
*sa_list_head
=NULL
;
125 static struct sa_list
*sa_default
=NULL
;
127 static void esp_print_addsa(struct sa_list
*sa
, int sa_def
)
133 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
135 error("ran out of memory to allocate sa structure");
143 nsa
->next
= sa_list_head
;
148 static int hexdigit(char hex
)
150 if(hex
>= '0' && hex
<= '9') {
152 } else if(hex
>= 'A' && hex
<= 'F') {
153 return (hex
- 'A' + 10);
154 } else if(hex
>= 'a' && hex
<= 'f') {
155 return (hex
- 'a' + 10);
157 printf("invalid hex digit %c in espsecret\n", hex
);
162 static int hex2byte(char *hexstring
)
166 byte
= (hexdigit(hexstring
[0]) << 4) +
167 hexdigit(hexstring
[1]);
172 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
174 * special form: file /name
175 * causes us to go read from this file instead.
179 static void esp_print_decode_onesecret(char *line
)
181 struct esp_algorithm
*xf
;
188 spikey
= strsep(&line
, " \t");
190 memset(&sa1
, 0, sizeof(struct sa_list
));
192 /* if there is only one token, then it is an algo:key token */
196 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
203 if(spikey
&& strcasecmp(spikey
, "file")==0) {
204 /* open file and read it */
209 secretfile
= fopen(line
, FOPEN_READ_TXT
);
210 if(secretfile
== NULL
) {
215 while(fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
217 /* remove newline from the line */
218 nl
= strchr(fileline
, '\n');
222 if(fileline
[0]=='#') continue;
223 if(fileline
[0]=='\0') continue;
225 esp_print_decode_onesecret(fileline
);
235 struct sockaddr_in
*sin
;
237 struct sockaddr_in6
*sin6
;
240 spistr
= strsep(&spikey
, "@");
242 spino
= strtoul(spistr
, &foo
, 0);
243 if(spistr
== foo
|| !spikey
) {
244 printf("print_esp: failed to decode spi# %s\n", foo
);
250 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
252 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
253 if(inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
254 #ifdef HAVE_SOCKADDR_SA_LEN
255 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
257 sin6
->sin6_family
= AF_INET6
;
260 if(inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
261 #ifdef HAVE_SOCKADDR_SA_LEN
262 sin
->sin_len
= sizeof(struct sockaddr_in
);
264 sin
->sin_family
= AF_INET
;
266 printf("print_esp: can not decode IP# %s\n", spikey
);
273 char espsecret_key
[256];
276 /* skip any blank spaces */
277 while(isspace((unsigned char)*decode
)) decode
++;
279 colon
= strchr(decode
, ':');
281 printf("failed to decode espsecret: %s\n", decode
);
285 len
= colon
- decode
;
287 while(xf
->name
&& strncasecmp(decode
, xf
->name
, len
)!=0) {
290 if(xf
->name
== NULL
) {
291 printf("failed to find cipher algo %s\n", decode
);
292 /* set to NULL transform */
298 if(colon
[0]=='0' && colon
[1]=='x') {
299 /* decode some hex! */
301 len
= strlen(colon
) / 2;
304 printf("secret is too big: %d\n", len
);
309 while(colon
[0] != '\0' && colon
[1]!='\0') {
310 espsecret_key
[i
]=hex2byte(colon
);
314 memcpy(sa1
.secret
, espsecret_key
, i
);
318 if(i
< sizeof(sa1
.secret
)) {
319 memcpy(sa1
.secret
, espsecret_key
, i
);
322 memcpy(sa1
.secret
, espsecret_key
, sizeof(sa1
.secret
));
323 sa1
.secretlen
= sizeof(sa1
.secret
);
329 esp_print_addsa(&sa1
, sa_def
);
333 static void esp_print_decodesecret(void)
338 if(espsecret
== NULL
) {
343 if(sa_list_head
!= NULL
) {
349 while(espsecret
&& espsecret
[0]!='\0') {
350 /* pick out the first line or first thing until a comma */
351 if((line
= strsep(&espsecret
, "\n,"))==NULL
) {
356 esp_print_decode_onesecret(line
);
361 esp_print(register const u_char
*bp
, register const u_char
*bp2
,
362 int *nhdr
, int *padlen
)
364 register const struct newesp
*esp
;
365 register const u_char
*ep
;
367 struct sa_list
*sa
= NULL
;
368 int espsecret_keylen
;
370 struct ip6_hdr
*ip6
= NULL
;
377 #ifdef HAVE_LIBCRYPTO
381 esp
= (struct newesp
*)bp
;
386 /* keep secret out of a register */
387 p
= (u_char
*)&secret
;
390 /* 'ep' points to the end of available data. */
393 if ((u_char
*)(esp
+ 1) >= ep
) {
394 fputs("[|ESP]", stdout
);
397 printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
398 printf(",seq=0x%x", EXTRACT_32BITS(&esp
->esp_seq
));
401 /* if we don't have decryption key, we can't decrypt this packet. */
402 if(sa_list_head
== NULL
) {
406 esp_print_decodesecret();
409 if(sa_list_head
== NULL
) {
413 ip
= (struct ip
*)bp2
;
417 ip6
= (struct ip6_hdr
*)bp2
;
418 /* we do not attempt to decrypt jumbograms */
419 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
421 /* if we can't get nexthdr, we do not need to decrypt it */
422 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
424 /* see if we can find the SA, and if so, decode it */
425 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
426 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
427 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
428 sin6
->sin6_family
== AF_INET6
&&
429 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
430 sizeof(struct in6_addr
)) == 0) {
437 /* nexthdr & padding are in the last fragment */
438 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
440 len
= EXTRACT_16BITS(&ip
->ip_len
);
442 /* see if we can find the SA, and if so, decode it */
443 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
444 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
445 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
446 sin
->sin_family
== AF_INET
&&
447 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
456 /* if we didn't find the specific one, then look for
457 * an unspecified one.
463 /* if not found fail */
467 /* if we can't get nexthdr, we do not need to decrypt it */
470 if (ep
- bp2
> len
) {
471 /* FCS included at end of frame (NetBSD 1.6 or later) */
475 ivoff
= (u_char
*)(esp
+ 1) + sa
->xform
->replaysize
;
476 ivlen
= sa
->xform
->ivlen
;
478 espsecret_keylen
= sa
->secretlen
;
480 switch (sa
->xform
->algo
) {
482 #ifdef HAVE_LIBCRYPTO
485 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
486 DES_key_schedule schedule
;
488 des_key_schedule schedule
;
493 memcpy(iv
, ivoff
, 4);
494 memcpy(&iv
[4], ivoff
, 4);
502 memcpy(iv
, ivoff
, 8);
509 if (espsecret_keylen
!= 8)
512 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
513 DES_set_key_unchecked((const_DES_cblock
*)secret
, &schedule
);
515 DES_cbc_encrypt((const unsigned char *)p
, p
,
516 (long)(ep
- p
), &schedule
, (DES_cblock
*)iv
,
519 #elif OPENSSL_VERSION_NUMBER >= 0x00907000L
520 DES_set_key_unchecked((DES_cblock
*)secret
, schedule
);
522 DES_cbc_encrypt((const unsigned char *)p
, p
,
523 (long)(ep
- p
), schedule
, (DES_cblock
*)iv
,
527 des_set_key((void *)secret
, schedule
);
529 des_cbc_encrypt((void *)p
, (void *)p
,
530 (long)(ep
- p
), schedule
, (void *)iv
,
533 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
538 #endif /*HAVE_LIBCRYPTO*/
541 #ifdef HAVE_LIBCRYPTO
545 if (espsecret_keylen
< 5 || espsecret_keylen
> 56)
547 BF_set_key(&schedule
, espsecret_keylen
, secret
);
550 BF_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
552 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
557 #endif /*HAVE_LIBCRYPTO*/
560 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
564 if (espsecret_keylen
< 5 || espsecret_keylen
> 255)
566 RC5_32_set_key(&schedule
, espsecret_keylen
, secret
,
570 RC5_32_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
572 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
577 #endif /*HAVE_LIBCRYPTO*/
580 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
584 if (espsecret_keylen
< 5 || espsecret_keylen
> 16)
586 CAST_set_key(&schedule
, espsecret_keylen
, secret
);
589 CAST_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
591 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
596 #endif /*HAVE_LIBCRYPTO*/
599 #if defined(HAVE_LIBCRYPTO)
601 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
602 DES_key_schedule s1
, s2
, s3
;
604 if (espsecret_keylen
!= 24)
606 DES_set_odd_parity((DES_cblock
*)secret
);
607 DES_set_odd_parity((DES_cblock
*)(secret
+ 8));
608 DES_set_odd_parity((DES_cblock
*)(secret
+ 16));
609 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
610 if(DES_set_key_checked((const_DES_cblock
*)secret
, &s1
) != 0) {
611 printf("failed to schedule key 1\n");
613 if(DES_set_key_checked((const_DES_cblock
*)(secret
+ 8), &s2
)!=0) {
614 printf("failed to schedule key 2\n");
616 if(DES_set_key_checked((const_DES_cblock
*)(secret
+ 16), &s3
)!=0) {
617 printf("failed to schedule key 3\n");
620 if(DES_set_key_checked((DES_cblock
*)secret
, s1
) != 0) {
621 printf("failed to schedule key 1\n");
623 if(DES_set_key_checked((DES_cblock
*)(secret
+ 8), s2
)!=0) {
624 printf("failed to schedule key 2\n");
626 if(DES_set_key_checked((DES_cblock
*)(secret
+ 16), s3
)!=0) {
627 printf("failed to schedule key 3\n");
632 DES_ede3_cbc_encrypt((const unsigned char *)p
, p
,
635 (DES_cblock
*)ivoff
, DES_DECRYPT
);
637 des_key_schedule s1
, s2
, s3
;
639 if (espsecret_keylen
!= 24)
642 des_set_odd_parity((void *)secret
);
643 des_set_odd_parity((void *)(secret
+ 8));
644 des_set_odd_parity((void *)(secret
+ 16));
645 if(des_set_key((void *)secret
, s1
) != 0) {
646 printf("failed to schedule key 1\n");
648 if(des_set_key((void *)(secret
+ 8), s2
)!=0) {
649 printf("failed to schedule key 2\n");
651 if(des_set_key((void *)(secret
+ 16), s3
)!=0) {
652 printf("failed to schedule key 3\n");
656 des_ede3_cbc_encrypt((void *)p
, (void *)p
,
659 (void *)ivoff
, DES_DECRYPT
);
661 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
666 #endif /*HAVE_LIBCRYPTO*/
670 advance
= sizeof(struct newesp
) + sa
->xform
->replaysize
;
674 ep
= ep
- sa
->xform
->authlen
;
675 /* sanity check for pad length */
676 if (ep
- bp
< *(ep
- 2))
680 *padlen
= *(ep
- 2) + 2;