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.26 2002-07-27 19:30:36 guy Exp $ (LBL)";
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
43 #include <openssl/des.h>
44 #include <openssl/blowfish.h>
45 #ifdef HAVE_OPENSSL_RC5_H
46 #include <openssl/rc5.h>
48 #ifdef HAVE_OPENSSL_CAST_H
49 #include <openssl/cast.h>
62 #include "interface.h"
63 #include "addrtoname.h"
65 static struct esp_algorithm
*espsecret_xform
=NULL
; /* cache of decoded alg. */
66 static char *espsecret_key
=NULL
;
78 struct esp_algorithm
{
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, 4},
90 {"blowfish-cbc", BLOWFISH
,8, 0, 0},
91 {"blowfish-cbc-hmac96", BLOWFISH
,8, 12, 4},
92 {"rc5-cbc", RC5
, 8, 0, 0},
93 {"rc5-cbc-hmac96", RC5
, 8, 12, 4},
94 {"cast128-cbc", CAST128
, 8, 0, 0},
95 {"cast128-cbc-hmac96", CAST128
, 8, 12, 4},
96 {"3des-cbc-hmac96", DES3CBC
, 8, 12, 4},
100 static int hexdigit(char hex
)
102 if(hex
>= '0' && hex
<= '9') {
104 } else if(hex
>= 'A' && hex
<= 'F') {
105 return (hex
- 'A' + 10);
106 } else if(hex
>= 'a' && hex
<= 'f') {
107 return (hex
- 'a' + 10);
109 printf("invalid hex digit %c in espsecret\n", hex
);
114 static int hex2byte(char *hexstring
)
118 byte
= (hexdigit(hexstring
[0]) << 4) +
119 hexdigit(hexstring
[1]);
124 static void esp_print_decodesecret(void)
128 struct esp_algorithm
*xf
;
129 struct esp_algorithm
*null_xf
=
130 &esp_xforms
[sizeof(esp_xforms
)/sizeof(esp_xforms
[0]) - 1];
132 if(espsecret
== NULL
) {
133 /* set to NULL transform */
134 espsecret_xform
= null_xf
;
138 if(espsecret_key
!= NULL
) {
142 colon
= strchr(espsecret
, ':');
144 printf("failed to decode espsecret: %s\n",
146 /* set to NULL transform */
147 espsecret_xform
= null_xf
;
151 len
= colon
- espsecret
;
153 while(xf
->name
&& strncasecmp(espsecret
, xf
->name
, len
)!=0) {
156 if(xf
->name
== NULL
) {
157 printf("failed to find cipher algo %s\n",
159 /* set to NULL transform */
160 espsecret_xform
= null_xf
;
163 espsecret_xform
= xf
;
166 if(colon
[0]=='0' && colon
[1]=='x') {
167 /* decode some hex! */
169 len
= strlen(colon
) / 2;
170 espsecret_key
= (char *)malloc(len
);
171 if(espsecret_key
== NULL
) {
172 fprintf(stderr
, "%s: ran out of memory (%d) to allocate secret key\n",
177 while(colon
[0] != '\0' && colon
[1]!='\0') {
178 espsecret_key
[i
]=hex2byte(colon
);
183 espsecret_key
= colon
;
188 esp_print(register const u_char
*bp
, register const u_char
*bp2
,
189 int *nhdr
, int *padlen
)
191 register const struct newesp
*esp
;
192 register const u_char
*ep
;
193 struct ip
*ip
= NULL
;
195 struct ip6_hdr
*ip6
= NULL
;
202 #ifdef HAVE_LIBCRYPTO
206 esp
= (struct newesp
*)bp
;
210 /* keep secret out of a register */
211 p
= (u_char
*)&secret
;
214 /* 'ep' points to the end of available data. */
217 if ((u_char
*)(esp
+ 1) >= ep
) {
218 fputs("[|ESP]", stdout
);
221 printf("ESP(spi=0x%08x", (u_int32_t
)ntohl(esp
->esp_spi
));
222 printf(",seq=0x%x", (u_int32_t
)ntohl(esp
->esp_seq
));
225 /* if we don't have decryption key, we can't decrypt this packet. */
229 if(!espsecret_xform
) {
230 esp_print_decodesecret();
232 if(espsecret_xform
->name
== NULL
) {
236 ip
= (struct ip
*)bp2
;
240 ip6
= (struct ip6_hdr
*)bp2
;
242 /* we do not attempt to decrypt jumbograms */
243 if (!ntohs(ip6
->ip6_plen
))
245 /* if we can't get nexthdr, we do not need to decrypt it */
246 len
= sizeof(struct ip6_hdr
) + ntohs(ip6
->ip6_plen
);
250 /* nexthdr & padding are in the last fragment */
251 if (ntohs(ip
->ip_off
) & IP_MF
)
256 len
= ntohs(ip
->ip_len
);
262 /* if we can't get nexthdr, we do not need to decrypt it */
266 ivoff
= (u_char
*)(esp
+ 1) + espsecret_xform
->replaysize
;
267 ivlen
= espsecret_xform
->ivlen
;
268 secret
= espsecret_key
;
270 switch (espsecret_xform
->algo
) {
272 #ifdef HAVE_LIBCRYPTO
275 des_key_schedule schedule
;
279 memcpy(iv
, ivoff
, 4);
280 memcpy(&iv
[4], ivoff
, 4);
288 memcpy(iv
, ivoff
, 8);
295 des_set_key((void *)secret
, schedule
);
298 des_cbc_encrypt((void *)p
, (void *)p
,
299 (long)(ep
- p
), schedule
, (void *)iv
,
301 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
306 #endif /*HAVE_LIBCRYPTO*/
309 #ifdef HAVE_LIBCRYPTO
313 BF_set_key(&schedule
, strlen(secret
), secret
);
316 BF_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
318 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
323 #endif /*HAVE_LIBCRYPTO*/
326 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
330 RC5_32_set_key(&schedule
, strlen(secret
), secret
,
334 RC5_32_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
336 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
341 #endif /*HAVE_LIBCRYPTO*/
344 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
348 CAST_set_key(&schedule
, strlen(secret
), secret
);
351 CAST_cbc_encrypt(p
, p
, (long)(ep
- p
), &schedule
, ivoff
,
353 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
358 #endif /*HAVE_LIBCRYPTO*/
361 #if defined(HAVE_LIBCRYPTO)
363 des_key_schedule s1
, s2
, s3
;
366 des_set_odd_parity((void *)secret
);
367 des_set_odd_parity((void *)(secret
+ 8));
368 des_set_odd_parity((void *)(secret
+ 16));
369 if(des_set_key((void *)secret
, s1
) != 0) {
370 printf("failed to schedule key 1\n");
372 if(des_set_key((void *)(secret
+ 8), s2
)!=0) {
373 printf("failed to schedule key 2\n");
375 if(des_set_key((void *)(secret
+ 16), s3
)!=0) {
376 printf("failed to schedule key 3\n");
380 des_ede3_cbc_encrypt((void *)p
, (void *)p
,
383 (void *)ivoff
, DES_DECRYPT
);
384 advance
= ivoff
- (u_char
*)esp
+ ivlen
;
389 #endif /*HAVE_LIBCRYPTO*/
393 advance
= sizeof(struct newesp
) + espsecret_xform
->replaysize
;
397 ep
= ep
- espsecret_xform
->authlen
;
398 /* sanity check for pad length */
399 if (ep
- bp
< *(ep
- 2))
403 *padlen
= *(ep
- 2) + 2;