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.28 2002-08-01 08:53:05 risso 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>
59 #include "interface.h"
60 #include "addrtoname.h"
62 static struct esp_algorithm
*espsecret_xform
=NULL
; /* cache of decoded alg. */
63 static char *espsecret_key
=NULL
;
75 struct esp_algorithm
{
83 struct esp_algorithm esp_xforms
[]={
84 {"none", NONE
, 0, 0, 0},
85 {"des-cbc", DESCBC
, 8, 0, 0},
86 {"des-cbc-hmac96", DESCBC
, 8, 12, 4},
87 {"blowfish-cbc", BLOWFISH
,8, 0, 0},
88 {"blowfish-cbc-hmac96", BLOWFISH
,8, 12, 4},
89 {"rc5-cbc", RC5
, 8, 0, 0},
90 {"rc5-cbc-hmac96", RC5
, 8, 12, 4},
91 {"cast128-cbc", CAST128
, 8, 0, 0},
92 {"cast128-cbc-hmac96", CAST128
, 8, 12, 4},
93 {"3des-cbc-hmac96", DES3CBC
, 8, 12, 4},
97 static int hexdigit(char hex
)
99 if(hex
>= '0' && hex
<= '9') {
101 } else if(hex
>= 'A' && hex
<= 'F') {
102 return (hex
- 'A' + 10);
103 } else if(hex
>= 'a' && hex
<= 'f') {
104 return (hex
- 'a' + 10);
106 printf("invalid hex digit %c in espsecret\n", hex
);
111 static int hex2byte(char *hexstring
)
115 byte
= (hexdigit(hexstring
[0]) << 4) +
116 hexdigit(hexstring
[1]);
121 static void esp_print_decodesecret(void)
125 struct esp_algorithm
*xf
;
126 struct esp_algorithm
*null_xf
=
127 &esp_xforms
[sizeof(esp_xforms
)/sizeof(esp_xforms
[0]) - 1];
129 if(espsecret
== NULL
) {
130 /* set to NULL transform */
131 espsecret_xform
= null_xf
;
135 if(espsecret_key
!= NULL
) {
139 colon
= strchr(espsecret
, ':');
141 printf("failed to decode espsecret: %s\n",
143 /* set to NULL transform */
144 espsecret_xform
= null_xf
;
148 len
= colon
- espsecret
;
150 while(xf
->name
&& strncasecmp(espsecret
, xf
->name
, len
)!=0) {
153 if(xf
->name
== NULL
) {
154 printf("failed to find cipher algo %s\n",
156 /* set to NULL transform */
157 espsecret_xform
= null_xf
;
160 espsecret_xform
= xf
;
163 if(colon
[0]=='0' && colon
[1]=='x') {
164 /* decode some hex! */
166 len
= strlen(colon
) / 2;
167 espsecret_key
= (char *)malloc(len
);
168 if(espsecret_key
== NULL
) {
169 fprintf(stderr
, "%s: ran out of memory (%d) to allocate secret key\n",
174 while(colon
[0] != '\0' && colon
[1]!='\0') {
175 espsecret_key
[i
]=hex2byte(colon
);
180 espsecret_key
= colon
;
185 esp_print(register const u_char
*bp
, register const u_char
*bp2
,
186 int *nhdr
, int *padlen
)
188 register const struct newesp
*esp
;
189 register const u_char
*ep
;
190 struct ip
*ip
= NULL
;
192 struct ip6_hdr
*ip6
= NULL
;
199 #ifdef HAVE_LIBCRYPTO
203 esp
= (struct newesp
*)bp
;
207 /* keep secret out of a register */
208 p
= (u_char
*)&secret
;
211 /* 'ep' points to the end of available data. */
214 if ((u_char
*)(esp
+ 1) >= ep
) {
215 fputs("[|ESP]", stdout
);
218 printf("ESP(spi=0x%08x", (u_int32_t
)ntohl(esp
->esp_spi
));
219 printf(",seq=0x%x", (u_int32_t
)ntohl(esp
->esp_seq
));
222 /* if we don't have decryption key, we can't decrypt this packet. */
226 if(!espsecret_xform
) {
227 esp_print_decodesecret();
229 if(espsecret_xform
->name
== NULL
) {
233 ip
= (struct ip
*)bp2
;
237 ip6
= (struct ip6_hdr
*)bp2
;
239 /* we do not attempt to decrypt jumbograms */
240 if (!ntohs(ip6
->ip6_plen
))
242 /* if we can't get nexthdr, we do not need to decrypt it */
243 len
= sizeof(struct ip6_hdr
) + ntohs(ip6
->ip6_plen
);
247 /* nexthdr & padding are in the last fragment */
248 if (ntohs(ip
->ip_off
) & IP_MF
)
253 len
= ntohs(ip
->ip_len
);
259 /* if we can't get nexthdr, we do not need to decrypt it */
263 ivoff
= (u_char
*)(esp
+ 1) + espsecret_xform
->replaysize
;
264 ivlen
= espsecret_xform
->ivlen
;
265 secret
= espsecret_key
;
267 switch (espsecret_xform
->algo
) {
269 #ifdef HAVE_LIBCRYPTO
272 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
273 DES_key_schedule schedule
;
275 des_key_schedule schedule
;
280 memcpy(iv
, ivoff
, 4);
281 memcpy(&iv
[4], ivoff
, 4);
289 memcpy(iv
, ivoff
, 8);
296 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
297 DES_set_key_unchecked((DES_cblock
*)secret
, schedule
);
299 DES_cbc_encrypt((const unsigned char *)p
, p
,
300 (long)(ep
- p
), schedule
, (DES_cblock
*)iv
,
304 des_set_key((void *)secret
, schedule
);
306 des_cbc_encrypt((void *)p
, (void *)p
,
307 (long)(ep
- p
), schedule
, (void *)iv
,
310 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
315 #endif /*HAVE_LIBCRYPTO*/
318 #ifdef HAVE_LIBCRYPTO
322 BF_set_key(&schedule
, strlen(secret
), secret
);
325 BF_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
327 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
332 #endif /*HAVE_LIBCRYPTO*/
335 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
339 RC5_32_set_key(&schedule
, strlen(secret
), secret
,
343 RC5_32_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
345 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
350 #endif /*HAVE_LIBCRYPTO*/
353 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
357 CAST_set_key(&schedule
, strlen(secret
), secret
);
360 CAST_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
362 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
367 #endif /*HAVE_LIBCRYPTO*/
370 #if defined(HAVE_LIBCRYPTO)
372 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
373 DES_key_schedule s1
, s2
, s3
;
375 DES_set_odd_parity((DES_cblock
*)secret
);
376 DES_set_odd_parity((DES_cblock
*)(secret
+ 8));
377 DES_set_odd_parity((DES_cblock
*)(secret
+ 16));
378 if(DES_set_key_checked((DES_cblock
*)secret
, s1
) != 0) {
379 printf("failed to schedule key 1\n");
381 if(DES_set_key_checked((DES_cblock
*)(secret
+ 8), s2
)!=0) {
382 printf("failed to schedule key 2\n");
384 if(DES_set_key_checked((DES_cblock
*)(secret
+ 16), s3
)!=0) {
385 printf("failed to schedule key 3\n");
389 DES_ede3_cbc_encrypt((const unsigned char *)p
, p
,
392 (DES_cblock
*)ivoff
, DES_DECRYPT
);
394 des_key_schedule s1
, s2
, s3
;
397 des_set_odd_parity((void *)secret
);
398 des_set_odd_parity((void *)(secret
+ 8));
399 des_set_odd_parity((void *)(secret
+ 16));
400 if(des_set_key((void *)secret
, s1
) != 0) {
401 printf("failed to schedule key 1\n");
403 if(des_set_key((void *)(secret
+ 8), s2
)!=0) {
404 printf("failed to schedule key 2\n");
406 if(des_set_key((void *)(secret
+ 16), s3
)!=0) {
407 printf("failed to schedule key 3\n");
411 des_ede3_cbc_encrypt((void *)p
, (void *)p
,
414 (void *)ivoff
, DES_DECRYPT
);
416 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
421 #endif /*HAVE_LIBCRYPTO*/
425 advance
= sizeof(struct newesp
) + espsecret_xform
->replaysize
;
429 ep
= ep
- espsecret_xform
->authlen
;
430 /* sanity check for pad length */
431 if (ep
- bp
< *(ep
- 2))
435 *padlen
= *(ep
- 2) + 2;