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.17 2000/12/12 09:58:41 itojun Exp $ (LBL)";
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
60 #include "interface.h"
61 #include "addrtoname.h"
64 esp_print(register const u_char *bp, register const u_char *bp2, int *nhdr)
66 register const struct esp *esp;
67 register const u_char *ep;
69 enum { NONE, DESCBC, BLOWFISH, RC5, CAST128, DES3CBC } algo = NONE;
72 struct ip6_hdr *ip6 = NULL;
80 esp = (struct esp *)bp;
81 spi = (u_int32_t)ntohl(esp->esp_spi);
83 /* 'ep' points to the end of available data. */
86 if ((u_char *)(esp + 1) >= ep - sizeof(struct esp)) {
87 fputs("[|ESP]", stdout);
90 printf("ESP(spi=0x%08x", spi);
91 printf(",seq=0x%x", (u_int32_t)ntohl(*(u_int32_t *)(esp + 1)));
94 /* if we don't have decryption key, we can't decrypt this packet. */
98 if (strncmp(espsecret, "des-cbc:", 8) == 0
99 && strlen(espsecret + 8) == 8) {
102 secret = espsecret + 8;
103 } else if (strncmp(espsecret, "blowfish-cbc:", 13) == 0) {
106 secret = espsecret + 13;
107 } else if (strncmp(espsecret, "rc5-cbc:", 8) == 0) {
110 secret = espsecret + 8;
111 } else if (strncmp(espsecret, "cast128-cbc:", 12) == 0) {
114 secret = espsecret + 12;
115 } else if (strncmp(espsecret, "3des-cbc:", 9) == 0
116 && strlen(espsecret + 9) == 24) {
119 secret = espsecret + 9;
120 } else if (strncmp(espsecret, "none:", 5) == 0) {
123 secret = espsecret + 5;
124 } else if (strlen(espsecret) == 8) {
134 ip = (struct ip *)bp2;
138 ip6 = (struct ip6_hdr *)bp2;
140 /* we do not attempt to decrypt jumbograms */
141 if (!ntohs(ip6->ip6_plen))
143 /* if we can't get nexthdr, we do not need to decrypt it */
144 len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
151 len = ntohs(ip->ip_len);
157 /* if we can't get nexthdr, we do not need to decrypt it */
162 ivoff = (u_char *)(esp + 1) + sizeof(u_int32_t);
164 ivoff = (u_char *)(esp + 1);
168 #ifdef HAVE_LIBCRYPTO
171 des_key_schedule schedule;
176 memcpy(iv, ivoff, 4);
177 memcpy(&iv[4], ivoff, 4);
185 memcpy(iv, ivoff, 8);
192 des_set_key((void *)secret, schedule);
195 des_cbc_encrypt((void *)p, (void *)p,
196 (long)(ep - p), schedule, (void *)iv,
198 advance = ivoff - (u_char *)esp + ivlen;
203 #endif /*HAVE_LIBCRYPTO*/
206 #ifdef HAVE_LIBCRYPTO
211 BF_set_key(&schedule, strlen(secret), secret);
214 BF_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
216 advance = ivoff - (u_char *)esp + ivlen;
221 #endif /*HAVE_LIBCRYPTO*/
224 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_RC5_H)
229 RC5_32_set_key(&schedule, strlen(secret), secret,
233 RC5_32_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
235 advance = ivoff - (u_char *)esp + ivlen;
240 #endif /*HAVE_LIBCRYPTO*/
243 #if defined(HAVE_LIBCRYPTO) && defined(HAVE_CAST_H) && !defined(HAVE_BUGGY_CAST128)
248 CAST_set_key(&schedule, strlen(secret), secret);
251 CAST_cbc_encrypt(p, p, (long)(ep - p), &schedule, ivoff,
253 advance = ivoff - (u_char *)esp + ivlen;
258 #endif /*HAVE_LIBCRYPTO*/
261 #if defined(HAVE_LIBCRYPTO)
263 des_key_schedule s1, s2, s3;
267 des_set_key((void *)secret, s1);
268 des_set_key((void *)(secret + 8), s2);
269 des_set_key((void *)(secret + 16), s3);
272 des_ede3_cbc_encrypt((void *)p, (void *)p,
273 (long)(ep - p), s1, s2, s3, (void *)ivoff, DES_DECRYPT);
274 advance = ivoff - (u_char *)esp + ivlen;
279 #endif /*HAVE_LIBCRYPTO*/
284 advance = sizeof(struct esp) + sizeof(u_int32_t);
286 advance = sizeof(struct esp);
290 /* sanity check for pad length */
291 if (ep - bp < *(ep - 2))