print-esp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /* $NetBSD: print-ah.c,v 1.4 1996/05/20 00:41:16 fvdl Exp $ */
  2. /*
  3. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that: (1) source code distributions
  8. * retain the above copyright notice and this paragraph in its entirety, (2)
  9. * distributions including binary code include the above copyright notice and
  10. * this paragraph in its entirety in the documentation or other materials
  11. * provided with the distribution, and (3) all advertising materials mentioning
  12. * features or use of this software display the following acknowledgement:
  13. * ``This product includes software developed by the University of California,
  14. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15. * the University nor the names of its contributors may be used to endorse
  16. * or promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21. */
  22. /* \summary: IPSEC Encapsulating Security Payload (ESP) printer */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include <netdissect-stdinc.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. /* Any code in this file that depends on HAVE_LIBCRYPTO depends on
  30. * HAVE_OPENSSL_EVP_H too. Undefining the former when the latter isn't defined
  31. * is the simplest way of handling the dependency.
  32. */
  33. #ifdef HAVE_LIBCRYPTO
  34. #ifdef HAVE_OPENSSL_EVP_H
  35. #include <openssl/evp.h>
  36. #else
  37. #undef HAVE_LIBCRYPTO
  38. #endif
  39. #endif
  40. #include "netdissect.h"
  41. #include "strtoaddr.h"
  42. #include "extract.h"
  43. #include "ascii_strcasecmp.h"
  44. #include "ip.h"
  45. #include "ip6.h"
  46. /*
  47. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  48. * All rights reserved.
  49. *
  50. * Redistribution and use in source and binary forms, with or without
  51. * modification, are permitted provided that the following conditions
  52. * are met:
  53. * 1. Redistributions of source code must retain the above copyright
  54. * notice, this list of conditions and the following disclaimer.
  55. * 2. Redistributions in binary form must reproduce the above copyright
  56. * notice, this list of conditions and the following disclaimer in the
  57. * documentation and/or other materials provided with the distribution.
  58. * 3. Neither the name of the project nor the names of its contributors
  59. * may be used to endorse or promote products derived from this software
  60. * without specific prior written permission.
  61. *
  62. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  63. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  64. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  66. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  67. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  68. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  69. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  70. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  71. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  72. * SUCH DAMAGE.
  73. */
  74. /*
  75. * RFC1827/2406 Encapsulated Security Payload.
  76. */
  77. struct newesp {
  78. uint32_t esp_spi; /* ESP */
  79. uint32_t esp_seq; /* Sequence number */
  80. /*variable size*/ /* (IV and) Payload data */
  81. /*variable size*/ /* padding */
  82. /*8bit*/ /* pad size */
  83. /*8bit*/ /* next header */
  84. /*8bit*/ /* next header */
  85. /*variable size, 32bit bound*/ /* Authentication data */
  86. };
  87. #ifdef HAVE_LIBCRYPTO
  88. union inaddr_u {
  89. struct in_addr in4;
  90. struct in6_addr in6;
  91. };
  92. struct sa_list {
  93. struct sa_list *next;
  94. u_int daddr_version;
  95. union inaddr_u daddr;
  96. uint32_t spi; /* if == 0, then IKEv2 */
  97. int initiator;
  98. u_char spii[8]; /* for IKEv2 */
  99. u_char spir[8];
  100. const EVP_CIPHER *evp;
  101. int ivlen;
  102. int authlen;
  103. u_char authsecret[256];
  104. int authsecret_len;
  105. u_char secret[256]; /* is that big enough for all secrets? */
  106. int secretlen;
  107. };
  108. #ifndef HAVE_EVP_CIPHER_CTX_NEW
  109. /*
  110. * Allocate an EVP_CIPHER_CTX.
  111. * Used if we have an older version of OpenSSL that doesn't provide
  112. * routines to allocate and free them.
  113. */
  114. static EVP_CIPHER_CTX *
  115. EVP_CIPHER_CTX_new(void)
  116. {
  117. EVP_CIPHER_CTX *ctx;
  118. ctx = malloc(sizeof(*ctx));
  119. if (ctx == NULL)
  120. return (NULL);
  121. memset(ctx, 0, sizeof(*ctx));
  122. return (ctx);
  123. }
  124. static void
  125. EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  126. {
  127. EVP_CIPHER_CTX_cleanup(ctx);
  128. free(ctx);
  129. }
  130. #endif
  131. #ifdef HAVE_EVP_CIPHERINIT_EX
  132. /*
  133. * Initialize the cipher by calling EVP_CipherInit_ex(), because
  134. * calling EVP_CipherInit() will reset the cipher context, clearing
  135. * the cipher, so calling it twice, with the second call having a
  136. * null cipher, will clear the already-set cipher. EVP_CipherInit_ex(),
  137. * however, won't reset the cipher context, so you can use it to specify
  138. * the IV oin a second call after a first call to EVP_CipherInit_ex()
  139. * to set the cipher and the key.
  140. *
  141. * XXX - is there some reason why we need to make two calls?
  142. */
  143. static int
  144. set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  145. const unsigned char *key,
  146. const unsigned char *iv, int enc)
  147. {
  148. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  149. }
  150. #else
  151. /*
  152. * Initialize the cipher by calling EVP_CipherInit(), because we don't
  153. * have EVP_CipherInit_ex(); we rely on it not trashing the context.
  154. */
  155. static int
  156. set_cipher_parameters(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  157. const unsigned char *key,
  158. const unsigned char *iv, int enc)
  159. {
  160. return EVP_CipherInit(ctx, cipher, key, iv, enc);
  161. }
  162. #endif
  163. /*
  164. * this will adjust ndo_packetp and ndo_snapend to new buffer!
  165. */
  166. USES_APPLE_DEPRECATED_API
  167. int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
  168. int initiator,
  169. u_char spii[8], u_char spir[8],
  170. const u_char *buf, const u_char *end)
  171. {
  172. struct sa_list *sa;
  173. const u_char *iv;
  174. unsigned int len;
  175. EVP_CIPHER_CTX *ctx;
  176. unsigned int block_size, output_buffer_size;
  177. u_char *output_buffer;
  178. /* initiator arg is any non-zero value */
  179. if(initiator) initiator=1;
  180. /* see if we can find the SA, and if so, decode it */
  181. for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
  182. if (sa->spi == 0
  183. && initiator == sa->initiator
  184. && memcmp(spii, sa->spii, 8) == 0
  185. && memcmp(spir, sa->spir, 8) == 0)
  186. break;
  187. }
  188. if(sa == NULL) return 0;
  189. if(sa->evp == NULL) return 0;
  190. /*
  191. * remove authenticator, and see if we still have something to
  192. * work with
  193. */
  194. end = end - sa->authlen;
  195. iv = buf;
  196. buf = buf + sa->ivlen;
  197. len = end-buf;
  198. if(end <= buf) return 0;
  199. ctx = EVP_CIPHER_CTX_new();
  200. if (ctx == NULL)
  201. return 0;
  202. if (set_cipher_parameters(ctx, sa->evp, sa->secret, NULL, 0) < 0)
  203. (*ndo->ndo_warning)(ndo, "espkey init failed");
  204. set_cipher_parameters(ctx, NULL, NULL, iv, 0);
  205. /*
  206. * Allocate a buffer for the decrypted data.
  207. * The output buffer must be separate from the input buffer, and
  208. * its size must be a multiple of the cipher block size.
  209. */
  210. block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
  211. output_buffer_size = len + (block_size - len % block_size);
  212. output_buffer = (u_char *)malloc(output_buffer_size);
  213. if (output_buffer == NULL) {
  214. (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
  215. EVP_CIPHER_CTX_free(ctx);
  216. return 0;
  217. }
  218. EVP_Cipher(ctx, output_buffer, buf, len);
  219. EVP_CIPHER_CTX_free(ctx);
  220. /*
  221. * XXX - of course this is wrong, because buf is a const buffer,
  222. * but changing this would require a more complicated fix.
  223. */
  224. memcpy(buf, output_buffer, len);
  225. free(output_buffer);
  226. ndo->ndo_packetp = buf;
  227. ndo->ndo_snapend = end;
  228. return 1;
  229. }
  230. USES_APPLE_RST
  231. static void esp_print_addsa(netdissect_options *ndo,
  232. struct sa_list *sa, int sa_def)
  233. {
  234. /* copy the "sa" */
  235. struct sa_list *nsa;
  236. nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
  237. if (nsa == NULL)
  238. (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
  239. *nsa = *sa;
  240. if (sa_def)
  241. ndo->ndo_sa_default = nsa;
  242. nsa->next = ndo->ndo_sa_list_head;
  243. ndo->ndo_sa_list_head = nsa;
  244. }
  245. static u_int hexdigit(netdissect_options *ndo, char hex)
  246. {
  247. if (hex >= '0' && hex <= '9')
  248. return (hex - '0');
  249. else if (hex >= 'A' && hex <= 'F')
  250. return (hex - 'A' + 10);
  251. else if (hex >= 'a' && hex <= 'f')
  252. return (hex - 'a' + 10);
  253. else {
  254. (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
  255. return 0;
  256. }
  257. }
  258. static u_int hex2byte(netdissect_options *ndo, char *hexstring)
  259. {
  260. u_int byte;
  261. byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
  262. return byte;
  263. }
  264. /*
  265. * returns size of binary, 0 on failure.
  266. */
  267. static
  268. int espprint_decode_hex(netdissect_options *ndo,
  269. u_char *binbuf, unsigned int binbuf_len,
  270. char *hex)
  271. {
  272. unsigned int len;
  273. int i;
  274. len = strlen(hex) / 2;
  275. if (len > binbuf_len) {
  276. (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
  277. return 0;
  278. }
  279. i = 0;
  280. while (hex[0] != '\0' && hex[1]!='\0') {
  281. binbuf[i] = hex2byte(ndo, hex);
  282. hex += 2;
  283. i++;
  284. }
  285. return i;
  286. }
  287. /*
  288. * decode the form: SPINUM@IP <tab> ALGONAME:0xsecret
  289. */
  290. USES_APPLE_DEPRECATED_API
  291. static int
  292. espprint_decode_encalgo(netdissect_options *ndo,
  293. char *decode, struct sa_list *sa)
  294. {
  295. size_t i;
  296. const EVP_CIPHER *evp;
  297. int authlen = 0;
  298. char *colon, *p;
  299. colon = strchr(decode, ':');
  300. if (colon == NULL) {
  301. (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
  302. return 0;
  303. }
  304. *colon = '\0';
  305. if (strlen(decode) > strlen("-hmac96") &&
  306. !strcmp(decode + strlen(decode) - strlen("-hmac96"),
  307. "-hmac96")) {
  308. p = strstr(decode, "-hmac96");
  309. *p = '\0';
  310. authlen = 12;
  311. }
  312. if (strlen(decode) > strlen("-cbc") &&
  313. !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
  314. p = strstr(decode, "-cbc");
  315. *p = '\0';
  316. }
  317. evp = EVP_get_cipherbyname(decode);
  318. if (!evp) {
  319. (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
  320. sa->evp = NULL;
  321. sa->authlen = 0;
  322. sa->ivlen = 0;
  323. return 0;
  324. }
  325. sa->evp = evp;
  326. sa->authlen = authlen;
  327. sa->ivlen = EVP_CIPHER_iv_length(evp);
  328. colon++;
  329. if (colon[0] == '0' && colon[1] == 'x') {
  330. /* decode some hex! */
  331. colon += 2;
  332. sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
  333. if(sa->secretlen == 0) return 0;
  334. } else {
  335. i = strlen(colon);
  336. if (i < sizeof(sa->secret)) {
  337. memcpy(sa->secret, colon, i);
  338. sa->secretlen = i;
  339. } else {
  340. memcpy(sa->secret, colon, sizeof(sa->secret));
  341. sa->secretlen = sizeof(sa->secret);
  342. }
  343. }
  344. return 1;
  345. }
  346. USES_APPLE_RST
  347. /*
  348. * for the moment, ignore the auth algorith, just hard code the authenticator
  349. * length. Need to research how openssl looks up HMAC stuff.
  350. */
  351. static int
  352. espprint_decode_authalgo(netdissect_options *ndo,
  353. char *decode, struct sa_list *sa)
  354. {
  355. char *colon;
  356. colon = strchr(decode, ':');
  357. if (colon == NULL) {
  358. (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
  359. return 0;
  360. }
  361. *colon = '\0';
  362. if(ascii_strcasecmp(colon,"sha1") == 0 ||
  363. ascii_strcasecmp(colon,"md5") == 0) {
  364. sa->authlen = 12;
  365. }
  366. return 1;
  367. }
  368. static void esp_print_decode_ikeline(netdissect_options *ndo, char *line,
  369. const char *file, int lineno)
  370. {
  371. /* it's an IKEv2 secret, store it instead */
  372. struct sa_list sa1;
  373. char *init;
  374. char *icookie, *rcookie;
  375. int ilen, rlen;
  376. char *authkey;
  377. char *enckey;
  378. init = strsep(&line, " \t");
  379. icookie = strsep(&line, " \t");
  380. rcookie = strsep(&line, " \t");
  381. authkey = strsep(&line, " \t");
  382. enckey = strsep(&line, " \t");
  383. /* if any fields are missing */
  384. if(!init || !icookie || !rcookie || !authkey || !enckey) {
  385. (*ndo->ndo_warning)(ndo, "print_esp: failed to find all fields for ikev2 at %s:%u",
  386. file, lineno);
  387. return;
  388. }
  389. ilen = strlen(icookie);
  390. rlen = strlen(rcookie);
  391. if((init[0]!='I' && init[0]!='R')
  392. || icookie[0]!='0' || icookie[1]!='x'
  393. || rcookie[0]!='0' || rcookie[1]!='x'
  394. || ilen!=18
  395. || rlen!=18) {
  396. (*ndo->ndo_warning)(ndo, "print_esp: line %s:%u improperly formatted.",
  397. file, lineno);
  398. (*ndo->ndo_warning)(ndo, "init=%s icookie=%s(%u) rcookie=%s(%u)",
  399. init, icookie, ilen, rcookie, rlen);
  400. return;
  401. }
  402. sa1.spi = 0;
  403. sa1.initiator = (init[0] == 'I');
  404. if(espprint_decode_hex(ndo, sa1.spii, sizeof(sa1.spii), icookie+2)!=8)
  405. return;
  406. if(espprint_decode_hex(ndo, sa1.spir, sizeof(sa1.spir), rcookie+2)!=8)
  407. return;
  408. if(!espprint_decode_encalgo(ndo, enckey, &sa1)) return;
  409. if(!espprint_decode_authalgo(ndo, authkey, &sa1)) return;
  410. esp_print_addsa(ndo, &sa1, FALSE);
  411. }
  412. /*
  413. *
  414. * special form: file /name
  415. * causes us to go read from this file instead.
  416. *
  417. */
  418. static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
  419. const char *file, int lineno)
  420. {
  421. struct sa_list sa1;
  422. int sa_def;
  423. char *spikey;
  424. char *decode;
  425. spikey = strsep(&line, " \t");
  426. sa_def = 0;
  427. memset(&sa1, 0, sizeof(struct sa_list));
  428. /* if there is only one token, then it is an algo:key token */
  429. if (line == NULL) {
  430. decode = spikey;
  431. spikey = NULL;
  432. /* sa1.daddr.version = 0; */
  433. /* memset(&sa1.daddr, 0, sizeof(sa1.daddr)); */
  434. /* sa1.spi = 0; */
  435. sa_def = 1;
  436. } else
  437. decode = line;
  438. if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
  439. /* open file and read it */
  440. FILE *secretfile;
  441. char fileline[1024];
  442. int subfile_lineno=0;
  443. char *nl;
  444. char *filename = line;
  445. secretfile = fopen(filename, FOPEN_READ_TXT);
  446. if (secretfile == NULL) {
  447. (*ndo->ndo_error)(ndo, "print_esp: can't open %s: %s\n",
  448. filename, strerror(errno));
  449. return;
  450. }
  451. while (fgets(fileline, sizeof(fileline)-1, secretfile) != NULL) {
  452. subfile_lineno++;
  453. /* remove newline from the line */
  454. nl = strchr(fileline, '\n');
  455. if (nl)
  456. *nl = '\0';
  457. if (fileline[0] == '#') continue;
  458. if (fileline[0] == '\0') continue;
  459. esp_print_decode_onesecret(ndo, fileline, filename, subfile_lineno);
  460. }
  461. fclose(secretfile);
  462. return;
  463. }
  464. if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
  465. esp_print_decode_ikeline(ndo, line, file, lineno);
  466. return;
  467. }
  468. if (spikey) {
  469. char *spistr, *foo;
  470. uint32_t spino;
  471. spistr = strsep(&spikey, "@");
  472. spino = strtoul(spistr, &foo, 0);
  473. if (spistr == foo || !spikey) {
  474. (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
  475. return;
  476. }
  477. sa1.spi = spino;
  478. if (strtoaddr6(spikey, &sa1.daddr.in6) == 1) {
  479. sa1.daddr_version = 6;
  480. } else if (strtoaddr(spikey, &sa1.daddr.in4) == 1) {
  481. sa1.daddr_version = 4;
  482. } else {
  483. (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
  484. return;
  485. }
  486. }
  487. if (decode) {
  488. /* skip any blank spaces */
  489. while (isspace((unsigned char)*decode))
  490. decode++;
  491. if(!espprint_decode_encalgo(ndo, decode, &sa1)) {
  492. return;
  493. }
  494. }
  495. esp_print_addsa(ndo, &sa1, sa_def);
  496. }
  497. USES_APPLE_DEPRECATED_API
  498. static void esp_init(netdissect_options *ndo _U_)
  499. {
  500. /*
  501. * 0.9.6 doesn't appear to define OPENSSL_API_COMPAT, so
  502. * we check whether it's undefined or it's less than the
  503. * value for 1.1.0.
  504. */
  505. #if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
  506. OpenSSL_add_all_algorithms();
  507. #endif
  508. EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
  509. }
  510. USES_APPLE_RST
  511. void esp_print_decodesecret(netdissect_options *ndo)
  512. {
  513. char *line;
  514. char *p;
  515. static int initialized = 0;
  516. if (!initialized) {
  517. esp_init(ndo);
  518. initialized = 1;
  519. }
  520. p = ndo->ndo_espsecret;
  521. while (p && p[0] != '\0') {
  522. /* pick out the first line or first thing until a comma */
  523. if ((line = strsep(&p, "\n,")) == NULL) {
  524. line = p;
  525. p = NULL;
  526. }
  527. esp_print_decode_onesecret(ndo, line, "cmdline", 0);
  528. }
  529. ndo->ndo_espsecret = NULL;
  530. }
  531. #endif
  532. #ifdef HAVE_LIBCRYPTO
  533. USES_APPLE_DEPRECATED_API
  534. #endif
  535. int
  536. esp_print(netdissect_options *ndo,
  537. const u_char *bp, const int length, const u_char *bp2
  538. #ifndef HAVE_LIBCRYPTO
  539. _U_
  540. #endif
  541. ,
  542. int *nhdr
  543. #ifndef HAVE_LIBCRYPTO
  544. _U_
  545. #endif
  546. ,
  547. int *padlen
  548. #ifndef HAVE_LIBCRYPTO
  549. _U_
  550. #endif
  551. )
  552. {
  553. register const struct newesp *esp;
  554. register const u_char *ep;
  555. #ifdef HAVE_LIBCRYPTO
  556. const struct ip *ip;
  557. struct sa_list *sa = NULL;
  558. const struct ip6_hdr *ip6 = NULL;
  559. int advance;
  560. int len;
  561. u_char *secret;
  562. int ivlen = 0;
  563. const u_char *ivoff;
  564. const u_char *p;
  565. EVP_CIPHER_CTX *ctx;
  566. unsigned int block_size, output_buffer_size;
  567. u_char *output_buffer;
  568. #endif
  569. esp = (const struct newesp *)bp;
  570. #ifdef HAVE_LIBCRYPTO
  571. secret = NULL;
  572. advance = 0;
  573. #endif
  574. #if 0
  575. /* keep secret out of a register */
  576. p = (u_char *)&secret;
  577. #endif
  578. /* 'ep' points to the end of available data. */
  579. ep = ndo->ndo_snapend;
  580. if ((const u_char *)(esp + 1) >= ep) {
  581. ND_PRINT((ndo, "[|ESP]"));
  582. goto fail;
  583. }
  584. ND_PRINT((ndo, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi)));
  585. ND_PRINT((ndo, ",seq=0x%x)", EXTRACT_32BITS(&esp->esp_seq)));
  586. ND_PRINT((ndo, ", length %u", length));
  587. #ifndef HAVE_LIBCRYPTO
  588. goto fail;
  589. #else
  590. /* initiailize SAs */
  591. if (ndo->ndo_sa_list_head == NULL) {
  592. if (!ndo->ndo_espsecret)
  593. goto fail;
  594. esp_print_decodesecret(ndo);
  595. }
  596. if (ndo->ndo_sa_list_head == NULL)
  597. goto fail;
  598. ip = (const struct ip *)bp2;
  599. switch (IP_V(ip)) {
  600. case 6:
  601. ip6 = (const struct ip6_hdr *)bp2;
  602. /* we do not attempt to decrypt jumbograms */
  603. if (!EXTRACT_16BITS(&ip6->ip6_plen))
  604. goto fail;
  605. /* if we can't get nexthdr, we do not need to decrypt it */
  606. len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
  607. /* see if we can find the SA, and if so, decode it */
  608. for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
  609. if (sa->spi == EXTRACT_32BITS(&esp->esp_spi) &&
  610. sa->daddr_version == 6 &&
  611. UNALIGNED_MEMCMP(&sa->daddr.in6, &ip6->ip6_dst,
  612. sizeof(struct in6_addr)) == 0) {
  613. break;
  614. }
  615. }
  616. break;
  617. case 4:
  618. /* nexthdr & padding are in the last fragment */
  619. if (EXTRACT_16BITS(&ip->ip_off) & IP_MF)
  620. goto fail;
  621. len = EXTRACT_16BITS(&ip->ip_len);
  622. /* see if we can find the SA, and if so, decode it */
  623. for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
  624. if (sa->spi == EXTRACT_32BITS(&esp->esp_spi) &&
  625. sa->daddr_version == 4 &&
  626. UNALIGNED_MEMCMP(&sa->daddr.in4, &ip->ip_dst,
  627. sizeof(struct in_addr)) == 0) {
  628. break;
  629. }
  630. }
  631. break;
  632. default:
  633. goto fail;
  634. }
  635. /* if we didn't find the specific one, then look for
  636. * an unspecified one.
  637. */
  638. if (sa == NULL)
  639. sa = ndo->ndo_sa_default;
  640. /* if not found fail */
  641. if (sa == NULL)
  642. goto fail;
  643. /* if we can't get nexthdr, we do not need to decrypt it */
  644. if (ep - bp2 < len)
  645. goto fail;
  646. if (ep - bp2 > len) {
  647. /* FCS included at end of frame (NetBSD 1.6 or later) */
  648. ep = bp2 + len;
  649. }
  650. /* pointer to the IV, if there is one */
  651. ivoff = (const u_char *)(esp + 1) + 0;
  652. /* length of the IV, if there is one; 0, if there isn't */
  653. ivlen = sa->ivlen;
  654. secret = sa->secret;
  655. ep = ep - sa->authlen;
  656. if (sa->evp) {
  657. ctx = EVP_CIPHER_CTX_new();
  658. if (ctx != NULL) {
  659. if (set_cipher_parameters(ctx, sa->evp, secret, NULL, 0) < 0)
  660. (*ndo->ndo_warning)(ndo, "espkey init failed");
  661. p = ivoff;
  662. set_cipher_parameters(ctx, NULL, NULL, p, 0);
  663. len = ep - (p + ivlen);
  664. /*
  665. * Allocate a buffer for the decrypted data.
  666. * The output buffer must be separate from the
  667. * input buffer, and its size must be a multiple
  668. * of the cipher block size.
  669. */
  670. block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
  671. output_buffer_size = len + (block_size - len % block_size);
  672. output_buffer = (u_char *)malloc(output_buffer_size);
  673. if (output_buffer == NULL) {
  674. (*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
  675. EVP_CIPHER_CTX_free(ctx);
  676. return -1;
  677. }
  678. EVP_Cipher(ctx, output_buffer, p + ivlen, len);
  679. EVP_CIPHER_CTX_free(ctx);
  680. /*
  681. * XXX - of course this is wrong, because buf is a
  682. * const buffer, but changing this would require a
  683. * more complicated fix.
  684. */
  685. memcpy(p + ivlen, output_buffer, len);
  686. free(output_buffer);
  687. advance = ivoff - (const u_char *)esp + ivlen;
  688. } else
  689. advance = sizeof(struct newesp);
  690. } else
  691. advance = sizeof(struct newesp);
  692. /* sanity check for pad length */
  693. if (ep - bp < *(ep - 2))
  694. goto fail;
  695. if (padlen)
  696. *padlen = *(ep - 2) + 2;
  697. if (nhdr)
  698. *nhdr = *(ep - 1);
  699. ND_PRINT((ndo, ": "));
  700. return advance;
  701. #endif
  702. fail:
  703. return -1;
  704. }
  705. #ifdef HAVE_LIBCRYPTO
  706. USES_APPLE_RST
  707. #endif
  708. /*
  709. * Local Variables:
  710. * c-style: whitesmith
  711. * c-basic-offset: 8
  712. * End:
  713. */