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.36 2003-03-19 05:36:22 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__)
60 extern char *strsep (char **stringp
, const char *delim
); /* Missing/strsep.c */
64 #include "interface.h"
65 #include "addrtoname.h"
77 struct esp_algorithm
{
82 int replaysize
; /* number of bytes, in excess of 4,
86 struct esp_algorithm esp_xforms
[]={
87 {"none", NONE
, 0, 0, 0},
88 {"des-cbc", DESCBC
, 8, 0, 0},
89 {"des-cbc-hmac96", DESCBC
, 8, 12, 0},
90 {"blowfish-cbc", BLOWFISH
,8, 0, 0},
91 {"blowfish-cbc-hmac96", BLOWFISH
,8, 12, 0},
92 {"rc5-cbc", RC5
, 8, 0, 0},
93 {"rc5-cbc-hmac96", RC5
, 8, 12, 0},
94 {"cast128-cbc", CAST128
, 8, 0, 0},
95 {"cast128-cbc-hmac96", CAST128
, 8, 12, 0},
96 {"3des-cbc-hmac96", DES3CBC
, 8, 12, 0},
100 struct esp_algorithm
*null_xf
=
101 &esp_xforms
[sizeof(esp_xforms
)/sizeof(esp_xforms
[0]) - 1];
103 #ifndef HAVE_SOCKADDR_STORAGE
105 struct sockaddr_storage
{
107 struct sockaddr_in sin
;
108 struct sockaddr_in6 sin6
;
112 #define sockaddr_storage sockaddr
114 #endif /* HAVE_SOCKADDR_STORAGE */
117 struct sa_list
*next
;
118 struct sockaddr_storage daddr
;
120 struct esp_algorithm
*xform
;
121 char secret
[256]; /* is that big enough for all secrets? */
125 static struct sa_list
*sa_list_head
=NULL
;
126 static struct sa_list
*sa_default
=NULL
;
128 static void esp_print_addsa(struct sa_list
*sa
, int sa_def
)
134 nsa
= (struct sa_list
*)malloc(sizeof(struct sa_list
));
136 fprintf(stderr
, "%s: ran out of memory (%d) to allocate sa structure\n",
137 program_name
, sizeof(struct sa_list
));
147 nsa
->next
= sa_list_head
;
152 static int hexdigit(char hex
)
154 if(hex
>= '0' && hex
<= '9') {
156 } else if(hex
>= 'A' && hex
<= 'F') {
157 return (hex
- 'A' + 10);
158 } else if(hex
>= 'a' && hex
<= 'f') {
159 return (hex
- 'a' + 10);
161 printf("invalid hex digit %c in espsecret\n", hex
);
166 static int hex2byte(char *hexstring
)
170 byte
= (hexdigit(hexstring
[0]) << 4) +
171 hexdigit(hexstring
[1]);
176 * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
178 * special form: file /name
179 * causes us to go read from this file instead.
183 static void esp_print_decode_onesecret(char *line
)
185 struct esp_algorithm
*xf
;
192 spikey
= strsep(&line
, " \t");
194 memset(&sa1
, 0, sizeof(struct sa_list
));
196 /* if there is only one token, then it is an algo:key token */
200 /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
207 if(spikey
&& strcasecmp(spikey
, "file")==0) {
208 /* open file and read it */
213 secretfile
= fopen(line
, FOPEN_READ_TXT
);
214 if(secretfile
== NULL
) {
219 while(fgets(fileline
, sizeof(fileline
)-1, secretfile
) != NULL
) {
221 /* remove newline from the line */
222 nl
= strchr(fileline
, '\n');
226 if(fileline
[0]=='#') continue;
227 if(fileline
[0]=='\0') continue;
229 esp_print_decode_onesecret(fileline
);
239 struct sockaddr_in
*sin
;
241 struct sockaddr_in6
*sin6
;
244 spistr
= strsep(&spikey
, "@");
246 spino
= strtoul(spistr
, &foo
, 0);
247 if(spistr
== foo
|| !spikey
) {
248 printf("print_esp: failed to decode spi# %s\n", foo
);
254 sin
= (struct sockaddr_in
*)&sa1
.daddr
;
256 sin6
= (struct sockaddr_in6
*)&sa1
.daddr
;
257 if(inet_pton(AF_INET6
, spikey
, &sin6
->sin6_addr
) == 1) {
258 #ifdef HAVE_SOCKADDR_SA_LEN
259 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
261 sin6
->sin6_family
= AF_INET6
;
264 if(inet_pton(AF_INET
, spikey
, &sin
->sin_addr
) == 1) {
265 #ifdef HAVE_SOCKADDR_SA_LEN
266 sin
->sin_len
= sizeof(struct sockaddr_in
);
268 sin
->sin_family
= AF_INET
;
270 printf("print_esp: can not decode IP# %s\n", spikey
);
277 char espsecret_key
[256];
280 /* skip any blank spaces */
281 while(isspace(*decode
)) decode
++;
283 colon
= strchr(decode
, ':');
285 printf("failed to decode espsecret: %s\n", decode
);
289 len
= colon
- decode
;
291 while(xf
->name
&& strncasecmp(decode
, xf
->name
, len
)!=0) {
294 if(xf
->name
== NULL
) {
295 printf("failed to find cipher algo %s\n", decode
);
296 /* set to NULL transform */
302 if(colon
[0]=='0' && colon
[1]=='x') {
303 /* decode some hex! */
305 len
= strlen(colon
) / 2;
308 printf("secret is too big: %d\n", len
);
313 while(colon
[0] != '\0' && colon
[1]!='\0') {
314 espsecret_key
[i
]=hex2byte(colon
);
318 memcpy(sa1
.secret
, espsecret_key
, i
);
322 if(i
< sizeof(sa1
.secret
)) {
323 memcpy(sa1
.secret
, espsecret_key
, i
);
326 memcpy(sa1
.secret
, espsecret_key
, sizeof(sa1
.secret
));
327 sa1
.secretlen
= sizeof(sa1
.secret
);
333 esp_print_addsa(&sa1
, sa_def
);
337 static void esp_print_decodesecret(void)
342 if(espsecret
== NULL
) {
347 if(sa_list_head
!= NULL
) {
353 while(espsecret
&& espsecret
[0]!='\0') {
354 /* pick out the first line or first thing until a comma */
355 if((line
= strsep(&espsecret
, "\n,"))==NULL
) {
360 esp_print_decode_onesecret(line
);
365 esp_print(register const u_char
*bp
, register const u_char
*bp2
,
366 int *nhdr
, int *padlen
)
368 register const struct newesp
*esp
;
369 register const u_char
*ep
;
371 struct sa_list
*sa
= NULL
;
372 int espsecret_keylen
;
374 struct ip6_hdr
*ip6
= NULL
;
381 #ifdef HAVE_LIBCRYPTO
385 esp
= (struct newesp
*)bp
;
390 /* keep secret out of a register */
391 p
= (u_char
*)&secret
;
394 /* 'ep' points to the end of available data. */
397 if ((u_char
*)(esp
+ 1) >= ep
) {
398 fputs("[|ESP]", stdout
);
401 printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp
->esp_spi
));
402 printf(",seq=0x%x", EXTRACT_32BITS(&esp
->esp_seq
));
405 /* if we don't have decryption key, we can't decrypt this packet. */
406 if(sa_list_head
== NULL
) {
410 esp_print_decodesecret();
413 if(sa_list_head
== NULL
) {
417 ip
= (struct ip
*)bp2
;
421 ip6
= (struct ip6_hdr
*)bp2
;
422 /* we do not attempt to decrypt jumbograms */
423 if (!EXTRACT_16BITS(&ip6
->ip6_plen
))
425 /* if we can't get nexthdr, we do not need to decrypt it */
426 len
= sizeof(struct ip6_hdr
) + EXTRACT_16BITS(&ip6
->ip6_plen
);
428 /* see if we can find the SA, and if so, decode it */
429 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
430 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&sa
->daddr
;
431 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
432 sin6
->sin6_family
== AF_INET6
&&
433 memcmp(&sin6
->sin6_addr
, &ip6
->ip6_dst
,
434 sizeof(struct in6_addr
)) == 0) {
441 /* nexthdr & padding are in the last fragment */
442 if (EXTRACT_16BITS(&ip
->ip_off
) & IP_MF
)
444 len
= EXTRACT_16BITS(&ip
->ip_len
);
446 /* see if we can find the SA, and if so, decode it */
447 for (sa
= sa_list_head
; sa
!= NULL
; sa
= sa
->next
) {
448 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&sa
->daddr
;
449 if (sa
->spi
== ntohl(esp
->esp_spi
) &&
450 sin
->sin_family
== AF_INET
&&
451 sin
->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
) {
460 /* if we didn't find the specific one, then look for
461 * an unspecified one.
467 /* if not found fail */
471 /* if we can't get nexthdr, we do not need to decrypt it */
474 if (ep
- bp2
> len
) {
475 /* FCS included at end of frame (NetBSD 1.6 or later) */
479 ivoff
= (u_char
*)(esp
+ 1) + sa
->xform
->replaysize
;
480 ivlen
= sa
->xform
->ivlen
;
482 espsecret_keylen
= sa
->secretlen
;
484 switch (sa
->xform
->algo
) {
486 #ifdef HAVE_LIBCRYPTO
489 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
490 DES_key_schedule schedule
;
492 des_key_schedule schedule
;
497 memcpy(iv
, ivoff
, 4);
498 memcpy(&iv
[4], ivoff
, 4);
506 memcpy(iv
, ivoff
, 8);
513 if (espsecret_keylen
!= 8)
516 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
517 DES_set_key_unchecked((const_DES_cblock
*)secret
, &schedule
);
519 DES_cbc_encrypt((const unsigned char *)p
, p
,
520 (long)(ep
- p
), &schedule
, (DES_cblock
*)iv
,
523 #elif OPENSSL_VERSION_NUMBER >= 0x00907000L
524 DES_set_key_unchecked((DES_cblock
*)secret
, schedule
);
526 DES_cbc_encrypt((const unsigned char *)p
, p
,
527 (long)(ep
- p
), schedule
, (DES_cblock
*)iv
,
531 des_set_key((void *)secret
, schedule
);
533 des_cbc_encrypt((void *)p
, (void *)p
,
534 (long)(ep
- p
), schedule
, (void *)iv
,
537 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
542 #endif /*HAVE_LIBCRYPTO*/
545 #ifdef HAVE_LIBCRYPTO
549 if (espsecret_keylen
< 5 || espsecret_keylen
> 56)
551 BF_set_key(&schedule
, espsecret_keylen
, secret
);
554 BF_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
556 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
561 #endif /*HAVE_LIBCRYPTO*/
564 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
568 if (espsecret_keylen
< 5 || espsecret_keylen
> 255)
570 RC5_32_set_key(&schedule
, espsecret_keylen
, secret
,
574 RC5_32_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
576 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
581 #endif /*HAVE_LIBCRYPTO*/
584 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
588 if (espsecret_keylen
< 5 || espsecret_keylen
> 16)
590 CAST_set_key(&schedule
, espsecret_keylen
, secret
);
593 CAST_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
595 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
600 #endif /*HAVE_LIBCRYPTO*/
603 #if defined(HAVE_LIBCRYPTO)
605 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
606 DES_key_schedule s1
, s2
, s3
;
608 if (espsecret_keylen
!= 24)
610 DES_set_odd_parity((DES_cblock
*)secret
);
611 DES_set_odd_parity((DES_cblock
*)(secret
+ 8));
612 DES_set_odd_parity((DES_cblock
*)(secret
+ 16));
613 #if OPENSSL_VERSION_NUMBER >= 0x00908000L
614 if(DES_set_key_checked((const_DES_cblock
*)secret
, &s1
) != 0) {
615 printf("failed to schedule key 1\n");
617 if(DES_set_key_checked((const_DES_cblock
*)(secret
+ 8), &s2
)!=0) {
618 printf("failed to schedule key 2\n");
620 if(DES_set_key_checked((const_DES_cblock
*)(secret
+ 16), &s3
)!=0) {
621 printf("failed to schedule key 3\n");
624 if(DES_set_key_checked((DES_cblock
*)secret
, s1
) != 0) {
625 printf("failed to schedule key 1\n");
627 if(DES_set_key_checked((DES_cblock
*)(secret
+ 8), s2
)!=0) {
628 printf("failed to schedule key 2\n");
630 if(DES_set_key_checked((DES_cblock
*)(secret
+ 16), s3
)!=0) {
631 printf("failed to schedule key 3\n");
636 DES_ede3_cbc_encrypt((const unsigned char *)p
, p
,
639 (DES_cblock
*)ivoff
, DES_DECRYPT
);
641 des_key_schedule s1
, s2
, s3
;
643 if (espsecret_keylen
!= 24)
646 des_set_odd_parity((void *)secret
);
647 des_set_odd_parity((void *)(secret
+ 8));
648 des_set_odd_parity((void *)(secret
+ 16));
649 if(des_set_key((void *)secret
, s1
) != 0) {
650 printf("failed to schedule key 1\n");
652 if(des_set_key((void *)(secret
+ 8), s2
)!=0) {
653 printf("failed to schedule key 2\n");
655 if(des_set_key((void *)(secret
+ 16), s3
)!=0) {
656 printf("failed to schedule key 3\n");
660 des_ede3_cbc_encrypt((void *)p
, (void *)p
,
663 (void *)ivoff
, DES_DECRYPT
);
665 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
670 #endif /*HAVE_LIBCRYPTO*/
674 advance
= sizeof(struct newesp
) + sa
->xform
->replaysize
;
678 ep
= ep
- sa
->xform
->authlen
;
679 /* sanity check for pad length */
680 if (ep
- bp
< *(ep
- 2))
684 *padlen
= *(ep
- 2) + 2;