]> The Tcpdump Group git mirrors - tcpdump/blob - print-esp.c
Get rid of unneeded includes of <netinet/in_systm.h> and <netinet/ip.h>.
[tcpdump] / print-esp.c
1 /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
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
18 * written permission.
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.
22 */
23
24 #ifndef lint
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.24 2002-04-07 02:16:03 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <stdlib.h>
39
40 #include <netinet/in.h>
41
42 #ifdef HAVE_LIBCRYPTO
43 #include <openssl/des.h>
44 #include <openssl/blowfish.h>
45 #ifdef HAVE_OPENSSL_RC5_H
46 #include <openssl/rc5.h>
47 #endif
48 #ifdef HAVE_OPENSSL_CAST_H
49 #include <openssl/cast.h>
50 #endif
51 #endif
52
53 #include <stdio.h>
54
55 #include "ip.h"
56 #include "esp.h"
57 #ifdef INET6
58 #include "ip6.h"
59 #endif
60
61 #define AVOID_CHURN 1
62 #include "interface.h"
63 #include "addrtoname.h"
64
65 static struct esp_algorithm *espsecret_xform=NULL; /* cache of decoded alg. */
66 static char *espsecret_key=NULL;
67
68
69 enum cipher { NONE,
70 DESCBC,
71 BLOWFISH,
72 RC5,
73 CAST128,
74 DES3CBC};
75
76
77
78 struct esp_algorithm {
79 char *name;
80 enum cipher algo;
81 int ivlen;
82 int authlen;
83 int replaysize;
84 };
85
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},
97 };
98
99 static int hexdigit(char hex)
100 {
101 if(hex >= '0' && hex <= '9') {
102 return (hex - '0');
103 } else if(hex >= 'A' && hex <= 'F') {
104 return (hex - 'A' + 10);
105 } else if(hex >= 'a' && hex <= 'f') {
106 return (hex - 'a' + 10);
107 } else {
108 printf("invalid hex digit %c in espsecret\n", hex);
109 return 0;
110 }
111 }
112
113 static int hex2byte(char *hexstring)
114 {
115 int byte;
116
117 byte = (hexdigit(hexstring[0]) << 4) +
118 hexdigit(hexstring[1]);
119 return byte;
120 }
121
122
123 static void esp_print_decodesecret(void)
124 {
125 char *colon;
126 int len, i;
127 struct esp_algorithm *xf;
128
129 if(espsecret == NULL) {
130 /* set to NONE transform */
131 espsecret_xform = esp_xforms;
132 return;
133 }
134
135 if(espsecret_key != NULL) {
136 return;
137 }
138
139 colon = strchr(espsecret, ':');
140 if(colon == NULL) {
141 printf("failed to decode espsecret: %s\n",
142 espsecret);
143 /* set to NONE transform */
144 espsecret_xform = esp_xforms;
145 }
146
147 len = colon - espsecret;
148 xf = esp_xforms;
149 while(xf->name && strncasecmp(espsecret, xf->name, len)!=0) {
150 xf++;
151 }
152 if(xf->name == NULL) {
153 printf("failed to find cipher algo %s\n",
154 espsecret);
155 espsecret_xform = esp_xforms;
156 return;
157 }
158 espsecret_xform = xf;
159
160 colon++;
161 if(colon[0]=='0' && colon[1]=='x') {
162 /* decode some hex! */
163 colon+=2;
164 len = strlen(colon) / 2;
165 espsecret_key = (char *)malloc(len);
166 if(espsecret_key == NULL) {
167 fprintf(stderr, "%s: ran out of memory (%d) to allocate secret key\n",
168 program_name, len);
169 exit(2);
170 }
171 i = 0;
172 while(colon[0] != '\0' && colon[1]!='\0') {
173 espsecret_key[i]=hex2byte(colon);
174 colon+=2;
175 i++;
176 }
177 } else {
178 espsecret_key = colon;
179 }
180 }
181
182 int
183 esp_print(register const u_char *bp, register const u_char *bp2,
184 int *nhdr, int *padlen)
185 {
186 register const struct esp *esp;
187 register const u_char *ep;
188 u_int32_t spi;
189 struct ip *ip = NULL;
190 #ifdef INET6
191 struct ip6_hdr *ip6 = NULL;
192 #endif
193 int advance;
194 int len;
195 char *secret;
196 int ivlen = 0;
197 u_char *ivoff;
198 #ifdef HAVE_LIBCRYPTO
199 u_char *p;
200 #endif
201
202 esp = (struct esp *)bp;
203 spi = (u_int32_t)ntohl(esp->esp_spi);
204 secret = NULL;
205
206 #if 0
207 /* keep secret out of a register */
208 p = (u_char *)&secret;
209 #endif
210
211 /* 'ep' points to the end of available data. */
212 ep = snapend;
213
214 if ((u_char *)(esp + 1) >= ep - sizeof(struct esp)) {
215 fputs("[|ESP]", stdout);
216 goto fail;
217 }
218 printf("ESP(spi=0x%08x", spi);
219 printf(",seq=0x%x", (u_int32_t)ntohl(*(u_int32_t *)(esp + 1)));
220 printf(")");
221
222 /* if we don't have decryption key, we can't decrypt this packet. */
223 if (!espsecret)
224 goto fail;
225
226 if(!espsecret_xform) {
227 esp_print_decodesecret();
228 }
229 if(espsecret_xform->algo == NONE) {
230 goto fail;
231 }
232
233 ip = (struct ip *)bp2;
234 switch (IP_V(ip)) {
235 #ifdef INET6
236 case 6:
237 ip6 = (struct ip6_hdr *)bp2;
238 ip = NULL;
239 /* we do not attempt to decrypt jumbograms */
240 if (!ntohs(ip6->ip6_plen))
241 goto fail;
242 /* if we can't get nexthdr, we do not need to decrypt it */
243 len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
244 break;
245 #endif /*INET6*/
246 case 4:
247 /* nexthdr & padding are in the last fragment */
248 if (ntohs(ip->ip_off) & IP_MF)
249 goto fail;
250 #ifdef INET6
251 ip6 = NULL;
252 #endif
253 len = ntohs(ip->ip_len);
254 break;
255 default:
256 goto fail;
257 }
258
259 /* if we can't get nexthdr, we do not need to decrypt it */
260 if (ep - bp2 < len)
261 goto fail;
262
263 ivoff = (u_char *)(esp + 1) + espsecret_xform->replaysize;
264 ivlen = espsecret_xform->ivlen;
265 secret = espsecret_key;
266
267 switch (espsecret_xform->algo) {
268 case DESCBC:
269 #ifdef HAVE_LIBCRYPTO
270 {
271 u_char iv[8];
272 des_key_schedule schedule;
273
274 switch (ivlen) {
275 case 4:
276 memcpy(iv, ivoff, 4);
277 memcpy(&iv[4], ivoff, 4);
278 p = &iv[4];
279 *p++ ^= 0xff;
280 *p++ ^= 0xff;
281 *p++ ^= 0xff;
282 *p++ ^= 0xff;
283 break;
284 case 8:
285 memcpy(iv, ivoff, 8);
286 break;
287 default:
288 goto fail;
289 }
290
291 des_check_key = 0;
292 des_set_key((void *)secret, schedule);
293
294 p = ivoff + ivlen;
295 des_cbc_encrypt((void *)p, (void *)p,
296 (long)(ep - p), schedule, (void *)iv,
297 DES_DECRYPT);
298 advance = ivoff - (u_char *)esp + ivlen;
299 break;
300 }
301 #else
302 goto fail;
303 #endif /*HAVE_LIBCRYPTO*/
304
305 case BLOWFISH:
306 #ifdef HAVE_LIBCRYPTO
307 {
308 BF_KEY schedule;
309
310 BF_set_key(&schedule, strlen(secret), secret);
311
312 p = ivoff + ivlen;
313 BF_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
314 BF_DECRYPT);
315 advance = ivoff - (u_char *)esp + ivlen;
316 break;
317 }
318 #else
319 goto fail;
320 #endif /*HAVE_LIBCRYPTO*/
321
322 case RC5:
323 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
324 {
325 RC5_32_KEY schedule;
326
327 RC5_32_set_key(&schedule, strlen(secret), secret,
328 RC5_16_ROUNDS);
329
330 p = ivoff + ivlen;
331 RC5_32_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
332 RC5_DECRYPT);
333 advance = ivoff - (u_char *)esp + ivlen;
334 break;
335 }
336 #else
337 goto fail;
338 #endif /*HAVE_LIBCRYPTO*/
339
340 case CAST128:
341 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
342 {
343 CAST_KEY schedule;
344
345 CAST_set_key(&schedule, strlen(secret), secret);
346
347 p = ivoff + ivlen;
348 CAST_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
349 CAST_DECRYPT);
350 advance = ivoff - (u_char *)esp + ivlen;
351 break;
352 }
353 #else
354 goto fail;
355 #endif /*HAVE_LIBCRYPTO*/
356
357 case DES3CBC:
358 #if defined(HAVE_LIBCRYPTO)
359 {
360 des_key_schedule s1, s2, s3;
361
362 des_check_key = 1;
363 des_set_odd_parity((void *)secret);
364 des_set_odd_parity((void *)(secret + 8));
365 des_set_odd_parity((void *)(secret + 16));
366 if(des_set_key((void *)secret, s1) != 0) {
367 printf("failed to schedule key 1\n");
368 }
369 if(des_set_key((void *)(secret + 8), s2)!=0) {
370 printf("failed to schedule key 2\n");
371 }
372 if(des_set_key((void *)(secret + 16), s3)!=0) {
373 printf("failed to schedule key 3\n");
374 }
375
376 p = ivoff + ivlen;
377 des_ede3_cbc_encrypt((void *)p, (void *)p,
378 (long)(ep - p),
379 s1, s2, s3,
380 (void *)ivoff, DES_DECRYPT);
381 advance = ivoff - (u_char *)esp + ivlen;
382 break;
383 }
384 #else
385 goto fail;
386 #endif /*HAVE_LIBCRYPTO*/
387
388 case NONE:
389 default:
390 advance = sizeof(struct esp) + espsecret_xform->replaysize;
391 break;
392 }
393
394 ep = ep - espsecret_xform->authlen;
395 /* sanity check for pad length */
396 if (ep - bp < *(ep - 2))
397 goto fail;
398
399 if (padlen)
400 *padlen = *(ep - 2) + 2;
401
402 if (nhdr)
403 *nhdr = *(ep - 1);
404
405 printf(": ");
406 return advance;
407
408 fail:
409 if (nhdr)
410 *nhdr = -1;
411 return 65536;
412 }