snifftest.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /* snifftest.c
  2. *
  3. * Copyright (C) 2006-2021 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/types.h>
  26. #include <wolfssl/wolfcrypt/logging.h>
  27. #include <wolfssl/wolfcrypt/error-crypt.h>
  28. #include <wolfssl/version.h>
  29. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  30. #include <wolfssl/wolfcrypt/memory.h>
  31. #endif
  32. #ifdef _WIN32
  33. #define WOLFSSL_SNIFFER
  34. #endif
  35. #ifndef WOLFSSL_SNIFFER
  36. #ifndef NO_MAIN_DRIVER
  37. /* blank build */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. int main(void)
  41. {
  42. printf("do ./configure --enable-sniffer to enable build support\n");
  43. return EXIT_SUCCESS;
  44. }
  45. #endif /* !NO_MAIN_DRIVER */
  46. #else
  47. /* do a full build */
  48. #ifdef _MSC_VER
  49. /* builds on *nix too, for scanf device and port */
  50. #define _CRT_SECURE_NO_WARNINGS
  51. #endif
  52. #include <pcap/pcap.h> /* pcap stuff */
  53. #include <stdio.h> /* printf */
  54. #include <stdlib.h> /* EXIT_SUCCESS */
  55. #include <string.h> /* strcmp */
  56. #include <signal.h> /* signal */
  57. #include <ctype.h> /* isprint */
  58. #include <cyassl/sniffer.h>
  59. #ifndef _WIN32
  60. #include <sys/socket.h> /* AF_INET */
  61. #include <arpa/inet.h>
  62. #include <netinet/in.h>
  63. #endif
  64. typedef unsigned char byte;
  65. enum {
  66. ETHER_IF_FRAME_LEN = 14, /* ethernet interface frame length */
  67. NULL_IF_FRAME_LEN = 4, /* no link interface frame length */
  68. };
  69. /* A TLS record can be 16k and change. The chain is broken up into 2K chunks.
  70. * This covers the TLS record, plus a chunk for TCP/IP headers. */
  71. #ifndef CHAIN_INPUT_CHUNK_SIZE
  72. #define CHAIN_INPUT_CHUNK_SIZE 2048
  73. #elif (CHAIN_INPUT_CHUNK_SIZE < 256)
  74. #undef CHAIN_INPUT_CHUNK_SIZE
  75. #define CHAIN_INPUT_CHUNK_SIZE 256
  76. #elif (CHAIN_INPUT_CHUNK_SIZE > 16384)
  77. #undef CHAIN_INPUT_CHUNK_SIZE
  78. #define CHAIN_INPUT_CHUNK_SIZE 16384
  79. #endif
  80. #define CHAIN_INPUT_COUNT ((16384 / CHAIN_INPUT_CHUNK_SIZE) + 1)
  81. #ifndef STORE_DATA_BLOCK_SZ
  82. #define STORE_DATA_BLOCK_SZ 1024
  83. #endif
  84. #if defined(HAVE_ECC) && !defined(NO_ECC_SECP) && (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  85. #define DEFAULT_SERVER_EPH_KEY_ECC "../../certs/statickeys/ecc-secp256r1.pem"
  86. #else
  87. #define DEFAULT_SERVER_EPH_KEY_ECC ""
  88. #endif
  89. #ifndef NO_DH
  90. #define DEFAULT_SERVER_EPH_KEY_DH "../../certs/statickeys/dh-ffdhe2048.pem"
  91. #else
  92. #define DEFAULT_SERVER_EPH_KEY_DH ""
  93. #endif
  94. #ifdef HAVE_CURVE25519
  95. #define DEFAULT_SERVER_EPH_KEY_X25519 "../../certs/statickeys/x25519.pem"
  96. #else
  97. #define DEFAULT_SERVER_EPH_KEY_X25519 ""
  98. #endif
  99. #ifndef DEFAULT_SERVER_EPH_KEY
  100. #define DEFAULT_SERVER_EPH_KEY \
  101. DEFAULT_SERVER_EPH_KEY_ECC "," \
  102. DEFAULT_SERVER_EPH_KEY_DH "," \
  103. DEFAULT_SERVER_EPH_KEY_X25519
  104. #endif
  105. #define DEFAULT_SERVER_KEY_RSA "../../certs/server-key.pem"
  106. #define DEFAULT_SERVER_KEY_ECC "../../certs/ecc-key.pem"
  107. #ifndef DEFAULT_SERVER_KEY
  108. #ifndef NO_RSA
  109. #define DEFAULT_SERVER_KEY DEFAULT_SERVER_KEY_RSA
  110. #elif defined(HAVE_ECC)
  111. #define DEFAULT_SERVER_KEY DEFAULT_SERVER_KEY_ECC
  112. #endif
  113. #endif
  114. #ifdef WOLFSSL_SNIFFER_WATCH
  115. static const byte rsaHash[] = {
  116. 0x3d, 0x4a, 0x60, 0xfc, 0xbf, 0xe5, 0x4d, 0x3e,
  117. 0x85, 0x62, 0xf2, 0xfc, 0xdb, 0x0d, 0x51, 0xdd,
  118. 0xcd, 0xc2, 0x53, 0x81, 0x1a, 0x67, 0x31, 0xa0,
  119. 0x7f, 0xd2, 0x11, 0x74, 0xbf, 0xea, 0xc9, 0xc5
  120. };
  121. static const byte eccHash[] = {
  122. 0x9e, 0x45, 0xb6, 0xf8, 0xc6, 0x5d, 0x60, 0x90,
  123. 0x40, 0x8f, 0xd2, 0x0e, 0xb1, 0x59, 0xe7, 0xbd,
  124. 0xb0, 0x9b, 0x3c, 0x7a, 0x3a, 0xbe, 0x13, 0x52,
  125. 0x07, 0x4f, 0x1a, 0x64, 0x45, 0xe0, 0x13, 0x34
  126. };
  127. #endif
  128. pcap_t* pcap = NULL;
  129. pcap_if_t* alldevs = NULL;
  130. static void FreeAll(void)
  131. {
  132. if (pcap)
  133. pcap_close(pcap);
  134. if (alldevs)
  135. pcap_freealldevs(alldevs);
  136. #ifndef _WIN32
  137. ssl_FreeSniffer();
  138. #endif
  139. }
  140. #ifdef WOLFSSL_SNIFFER_STATS
  141. static void DumpStats(void)
  142. {
  143. SSLStats sslStats;
  144. ssl_ReadStatistics(&sslStats);
  145. printf("SSL Stats (sslStandardConns):%lu\n",
  146. sslStats.sslStandardConns);
  147. printf("SSL Stats (sslClientAuthConns):%lu\n",
  148. sslStats.sslClientAuthConns);
  149. printf("SSL Stats (sslResumedConns):%lu\n",
  150. sslStats.sslResumedConns);
  151. printf("SSL Stats (sslEphemeralMisses):%lu\n",
  152. sslStats.sslEphemeralMisses);
  153. printf("SSL Stats (sslResumptionInserts):%lu\n",
  154. sslStats.sslResumptionInserts);
  155. printf("SSL Stats (sslResumeMisses):%lu\n",
  156. sslStats.sslResumeMisses);
  157. printf("SSL Stats (sslCiphersUnsupported):%lu\n",
  158. sslStats.sslCiphersUnsupported);
  159. printf("SSL Stats (sslKeysUnmatched):%lu\n",
  160. sslStats.sslKeysUnmatched);
  161. printf("SSL Stats (sslKeyFails):%lu\n",
  162. sslStats.sslKeyFails);
  163. printf("SSL Stats (sslDecodeFails):%lu\n",
  164. sslStats.sslDecodeFails);
  165. printf("SSL Stats (sslAlerts):%lu\n",
  166. sslStats.sslAlerts);
  167. printf("SSL Stats (sslDecryptedBytes):%lu\n",
  168. sslStats.sslDecryptedBytes);
  169. printf("SSL Stats (sslEncryptedBytes):%lu\n",
  170. sslStats.sslEncryptedBytes);
  171. printf("SSL Stats (sslEncryptedPackets):%lu\n",
  172. sslStats.sslEncryptedPackets);
  173. printf("SSL Stats (sslDecryptedPackets):%lu\n",
  174. sslStats.sslDecryptedPackets);
  175. printf("SSL Stats (sslKeyMatches):%lu\n",
  176. sslStats.sslKeyMatches);
  177. printf("SSL Stats (sslEncryptedConns):%lu\n",
  178. sslStats.sslEncryptedConns);
  179. }
  180. #endif /* WOLFSSL_SNIFFER_STATS */
  181. static void sig_handler(const int sig)
  182. {
  183. printf("SIGINT handled = %d.\n", sig);
  184. FreeAll();
  185. #ifdef WOLFSSL_SNIFFER_STATS
  186. DumpStats();
  187. #endif
  188. if (sig)
  189. exit(EXIT_SUCCESS);
  190. }
  191. static void err_sys(const char* msg)
  192. {
  193. fprintf(stderr, "%s\n", msg);
  194. if (msg)
  195. exit(EXIT_FAILURE);
  196. }
  197. #ifdef _WIN32
  198. #define SNPRINTF _snprintf
  199. #else
  200. #define SNPRINTF snprintf
  201. #endif
  202. static char* iptos(const struct in_addr* addr)
  203. {
  204. static char output[32];
  205. byte *p = (byte*)&addr->s_addr;
  206. snprintf(output, sizeof(output), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  207. return output;
  208. }
  209. static const char* ip6tos(const struct in6_addr* addr)
  210. {
  211. static char output[42];
  212. return inet_ntop(AF_INET6, addr, output, 42);
  213. }
  214. #if defined(WOLFSSL_SNIFFER_STORE_DATA_CB) || defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
  215. static inline unsigned int min(unsigned int a, unsigned int b)
  216. {
  217. return a > b ? b : a;
  218. }
  219. #endif
  220. #ifdef WOLFSSL_SNIFFER_WATCH
  221. static int myWatchCb(void* vSniffer,
  222. const unsigned char* certHash, unsigned int certHashSz,
  223. const unsigned char* certChain, unsigned int certChainSz,
  224. void* ctx, char* error)
  225. {
  226. const char* certName = NULL;
  227. (void)certChain;
  228. (void)certChainSz;
  229. (void)ctx;
  230. if (certHashSz == sizeof(rsaHash) &&
  231. XMEMCMP(certHash, rsaHash, certHashSz) == 0) {
  232. certName = DEFAULT_SERVER_KEY_RSA;
  233. }
  234. if (certHashSz == sizeof(eccHash) &&
  235. XMEMCMP(certHash, eccHash, certHashSz) == 0) {
  236. certName = DEFAULT_SERVER_KEY_ECC;
  237. }
  238. if (certName == NULL) {
  239. /* don't return error if key is not loaded */
  240. printf("Warning: No matching key found for cert hash\n");
  241. return 0;
  242. }
  243. return ssl_SetWatchKey_file(vSniffer, certName, FILETYPE_PEM, NULL, error);
  244. }
  245. #endif /* WOLFSSL_SNIFFER_WATCH */
  246. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  247. static int myStoreDataCb(const unsigned char* decryptBuf,
  248. unsigned int decryptBufSz, unsigned int decryptBufOffset, void* ctx)
  249. {
  250. byte** data = (byte**)ctx;
  251. unsigned int qty;
  252. if (data == NULL)
  253. return -1;
  254. if (decryptBufSz < decryptBufOffset)
  255. return -1;
  256. qty = min(decryptBufSz - decryptBufOffset, STORE_DATA_BLOCK_SZ);
  257. if (*data == NULL) {
  258. byte* tmpData;
  259. tmpData = (byte*)XREALLOC(*data, decryptBufSz + 1,
  260. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  261. if (tmpData == NULL) {
  262. XFREE(*data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  263. *data = NULL;
  264. return -1;
  265. }
  266. *data = tmpData;
  267. }
  268. XMEMCPY(*data + decryptBufOffset, decryptBuf + decryptBufOffset, qty);
  269. return qty;
  270. }
  271. #endif /* WOLFSSL_SNIFFER_STORE_DATA_CB */
  272. /* try and load as both static ephemeral and private key */
  273. /* only fail if no key is loaded */
  274. /* Allow comma seperated list of files */
  275. static int load_key(const char* name, const char* server, int port,
  276. const char* keyFiles, const char* passwd, char* err)
  277. {
  278. int ret = -1;
  279. int loadCount = 0;
  280. char *keyFile, *ptr = NULL;
  281. keyFile = XSTRTOK((char*)keyFiles, ",", &ptr);
  282. while (keyFile != NULL) {
  283. #ifdef WOLFSSL_STATIC_EPHEMERAL
  284. #ifdef HAVE_SNI
  285. ret = ssl_SetNamedEphemeralKey(name, server, port, keyFile,
  286. FILETYPE_PEM, passwd, err);
  287. #else
  288. ret = ssl_SetEphemeralKey(server, port, keyFile,
  289. FILETYPE_PEM, passwd, err);
  290. #endif
  291. if (ret == 0)
  292. loadCount++;
  293. #endif
  294. #ifdef HAVE_SNI
  295. ret = ssl_SetNamedPrivateKey(name, server, port, keyFile,
  296. FILETYPE_PEM, passwd, err);
  297. #else
  298. ret = ssl_SetPrivateKey(server, port, keyFile,
  299. FILETYPE_PEM, passwd, err);
  300. #endif
  301. if (ret == 0)
  302. loadCount++;
  303. if (loadCount == 0) {
  304. printf("Failed loading private key %s: ret %d\n", keyFile, ret);
  305. printf("Please run directly from sslSniffer/sslSnifferTest dir\n");
  306. ret = -1;
  307. }
  308. else {
  309. ret = 0;
  310. }
  311. keyFile = XSTRTOK(NULL, ",", &ptr);
  312. }
  313. (void)name;
  314. return ret;
  315. }
  316. static void TrimNewLine(char* str)
  317. {
  318. word32 strSz = 0;
  319. if (str)
  320. strSz = (word32)XSTRLEN(str);
  321. if (strSz > 0 && (str[strSz-1] == '\n' || str[strSz-1] == '\r'))
  322. str[strSz-1] = '\0';
  323. }
  324. static void show_appinfo(void)
  325. {
  326. printf("snifftest %s\n", LIBWOLFSSL_VERSION_STRING);
  327. /* list enabled sniffer features */
  328. printf("sniffer features: "
  329. #ifdef WOLFSSL_SNIFFER_STATS
  330. "stats, "
  331. #endif
  332. #ifdef WOLFSSL_SNIFFER_WATCH
  333. "watch, "
  334. #endif
  335. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  336. "store_data_cb "
  337. #endif
  338. #ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
  339. "chain_input "
  340. #endif
  341. #ifdef WOLFSSL_SNIFFER_KEY_CALLBACK
  342. "key_callback "
  343. #endif
  344. #ifdef DEBUG_SNIFFER
  345. "debug "
  346. #endif
  347. #ifdef WOLFSSL_TLS13
  348. "tls_v13 "
  349. #endif
  350. #ifdef HAVE_SESSION_TICKET
  351. "session_ticket "
  352. #endif
  353. #ifdef WOLFSSL_STATIC_EPHEMERAL
  354. "static_ephemeral "
  355. #endif
  356. #ifdef WOLFSSL_ENCRYPTED_KEYS
  357. "encrypted_keys "
  358. #endif
  359. #ifdef HAVE_SNI
  360. "sni "
  361. #endif
  362. #ifdef HAVE_EXTENDED_MASTER
  363. "extended_master "
  364. #endif
  365. #ifdef HAVE_MAX_FRAGMENT
  366. "max fragment "
  367. #endif
  368. #ifdef WOLFSSL_ASYNC_CRYPT
  369. "async_crypt "
  370. #endif
  371. #ifndef NO_RSA
  372. "rsa "
  373. #endif
  374. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  375. "dh "
  376. #endif
  377. #ifdef HAVE_ECC
  378. "ecc "
  379. #endif
  380. #ifdef HAVE_CURVE448
  381. "x448 "
  382. #endif
  383. #ifdef HAVE_CURVE22519
  384. "x22519 "
  385. #endif
  386. "\n\n"
  387. );
  388. }
  389. static void show_usage(void)
  390. {
  391. printf("usage:\n");
  392. printf("\t./snifftest\n");
  393. printf("\t\tprompts for options\n");
  394. printf("\t./snifftest dump pemKey [server] [port] [password]\n");
  395. }
  396. int main(int argc, char** argv)
  397. {
  398. int ret = 0;
  399. int hadBadPacket = 0;
  400. int inum = 0;
  401. int port = 0;
  402. int saveFile = 0;
  403. int i = 0, defDev = 0;
  404. int frame = ETHER_IF_FRAME_LEN;
  405. char err[PCAP_ERRBUF_SIZE];
  406. char filter[32];
  407. const char *keyFilesSrc = NULL;
  408. char keyFilesBuf[MAX_FILENAME_SZ];
  409. char keyFilesUser[MAX_FILENAME_SZ];
  410. const char *server = NULL;
  411. const char *sniName = NULL;
  412. struct bpf_program fp;
  413. pcap_if_t *d;
  414. pcap_addr_t *a;
  415. int isChain = 0;
  416. int j;
  417. #ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
  418. struct iovec chains[CHAIN_INPUT_COUNT];
  419. unsigned int remainder;
  420. #endif
  421. show_appinfo();
  422. signal(SIGINT, sig_handler);
  423. #ifndef _WIN32
  424. ssl_InitSniffer(); /* dll load on Windows */
  425. #endif
  426. ssl_Trace("./tracefile.txt", err);
  427. ssl_EnableRecovery(1, -1, err);
  428. #ifdef WOLFSSL_SNIFFER_WATCH
  429. ssl_SetWatchKeyCallback(myWatchCb, err);
  430. #endif
  431. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  432. ssl_SetStoreDataCallback(myStoreDataCb);
  433. #endif
  434. if (argc == 1) {
  435. char cmdLineArg[128];
  436. /* normal case, user chooses device and port */
  437. if (pcap_findalldevs(&alldevs, err) == -1)
  438. err_sys("Error in pcap_findalldevs");
  439. for (d = alldevs; d; d=d->next) {
  440. printf("%d. %s", ++i, d->name);
  441. if (strcmp(d->name, "lo0") == 0) {
  442. defDev = i;
  443. }
  444. if (d->description)
  445. printf(" (%s)\n", d->description);
  446. else
  447. printf(" (No description available)\n");
  448. }
  449. if (i == 0)
  450. err_sys("No interfaces found! Make sure pcap or WinPcap is"
  451. " installed correctly and you have sufficient permissions");
  452. printf("Enter the interface number (1-%d) [default: %d]: ", i, defDev);
  453. XMEMSET(cmdLineArg, 0, sizeof(cmdLineArg));
  454. if (XFGETS(cmdLineArg, sizeof(cmdLineArg), stdin))
  455. inum = XATOI(cmdLineArg);
  456. if (inum == 0)
  457. inum = defDev;
  458. else if (inum < 1 || inum > i)
  459. err_sys("Interface number out of range");
  460. /* Jump to the selected adapter */
  461. for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
  462. pcap = pcap_create(d->name, err);
  463. if (pcap == NULL) printf("pcap_create failed %s\n", err);
  464. /* print out addresses for selected interface */
  465. for (a = d->addresses; a; a = a->next) {
  466. if (a->addr->sa_family == AF_INET) {
  467. server =
  468. iptos(&((struct sockaddr_in *)a->addr)->sin_addr);
  469. printf("server = %s\n", server);
  470. }
  471. else if (a->addr->sa_family == AF_INET6) {
  472. server =
  473. ip6tos(&((struct sockaddr_in6 *)a->addr)->sin6_addr);
  474. printf("server = %s\n", server);
  475. }
  476. }
  477. if (server == NULL)
  478. err_sys("Unable to get device IPv4 or IPv6 address");
  479. ret = pcap_set_snaplen(pcap, 65536);
  480. if (ret != 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap));
  481. ret = pcap_set_timeout(pcap, 1000);
  482. if (ret != 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap));
  483. ret = pcap_set_buffer_size(pcap, 1000000);
  484. if (ret != 0)
  485. printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap));
  486. ret = pcap_set_promisc(pcap, 1);
  487. if (ret != 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap));
  488. ret = pcap_activate(pcap);
  489. if (ret != 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap));
  490. printf("Enter the port to scan [default: 11111]: ");
  491. XMEMSET(cmdLineArg, 0, sizeof(cmdLineArg));
  492. if (XFGETS(cmdLineArg, sizeof(cmdLineArg), stdin)) {
  493. port = XATOI(cmdLineArg);
  494. }
  495. if (port <= 0)
  496. port = 11111;
  497. SNPRINTF(filter, sizeof(filter), "tcp and port %d", port);
  498. ret = pcap_compile(pcap, &fp, filter, 0, 0);
  499. if (ret != 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap));
  500. ret = pcap_setfilter(pcap, &fp);
  501. if (ret != 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
  502. /* optionally enter the private key to use */
  503. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(DEFAULT_SERVER_EPH_KEY)
  504. keyFilesSrc = DEFAULT_SERVER_EPH_KEY;
  505. #else
  506. keyFilesSrc = DEFAULT_SERVER_KEY;
  507. #endif
  508. printf("Enter the server key [default: %s]: ", keyFilesSrc);
  509. XMEMSET(keyFilesBuf, 0, sizeof(keyFilesBuf));
  510. XMEMSET(keyFilesUser, 0, sizeof(keyFilesUser));
  511. if (XFGETS(keyFilesUser, sizeof(keyFilesUser), stdin)) {
  512. TrimNewLine(keyFilesUser);
  513. if (XSTRLEN(keyFilesUser) > 0) {
  514. keyFilesSrc = keyFilesUser;
  515. }
  516. }
  517. XSTRNCPY(keyFilesBuf, keyFilesSrc, sizeof(keyFilesBuf));
  518. /* optionally enter a named key (SNI) */
  519. #if !defined(WOLFSSL_SNIFFER_WATCH) && defined(HAVE_SNI)
  520. printf("Enter alternate SNI [default: none]: ");
  521. XMEMSET(cmdLineArg, 0, sizeof(cmdLineArg));
  522. if (XFGETS(cmdLineArg, sizeof(cmdLineArg), stdin)) {
  523. TrimNewLine(cmdLineArg);
  524. if (XSTRLEN(cmdLineArg) > 0) {
  525. sniName = cmdLineArg;
  526. }
  527. }
  528. #endif /* !WOLFSSL_SNIFFER_WATCH && HAVE_SNI */
  529. /* get IPv4 or IPv6 addresses for selected interface */
  530. for (a = d->addresses; a; a = a->next) {
  531. server = NULL;
  532. if (a->addr->sa_family == AF_INET) {
  533. server =
  534. iptos(&((struct sockaddr_in *)a->addr)->sin_addr);
  535. }
  536. else if (a->addr->sa_family == AF_INET6) {
  537. server =
  538. ip6tos(&((struct sockaddr_in6 *)a->addr)->sin6_addr);
  539. }
  540. if (server) {
  541. XSTRNCPY(keyFilesBuf, keyFilesSrc, sizeof(keyFilesBuf));
  542. ret = load_key(sniName, server, port, keyFilesBuf, NULL, err);
  543. if (ret != 0) {
  544. exit(EXIT_FAILURE);
  545. }
  546. }
  547. }
  548. }
  549. else if (argc >= 3) {
  550. saveFile = 1;
  551. pcap = pcap_open_offline(argv[1], err);
  552. if (pcap == NULL) {
  553. printf("pcap_open_offline failed %s\n", err);
  554. ret = -1;
  555. }
  556. else {
  557. const char* passwd = NULL;
  558. /* defaults for server and port */
  559. port = 443;
  560. server = "127.0.0.1";
  561. keyFilesSrc = argv[2];
  562. if (argc >= 4)
  563. server = argv[3];
  564. if (argc >= 5)
  565. port = XATOI(argv[4]);
  566. if (argc >= 6)
  567. passwd = argv[5];
  568. ret = load_key(NULL, server, port, keyFilesSrc, passwd, err);
  569. if (ret != 0) {
  570. exit(EXIT_FAILURE);
  571. }
  572. /* Only let through TCP/IP packets */
  573. ret = pcap_compile(pcap, &fp, "(ip6 or ip) and tcp", 0, 0);
  574. if (ret != 0) {
  575. printf("pcap_compile failed %s\n", pcap_geterr(pcap));
  576. exit(EXIT_FAILURE);
  577. }
  578. ret = pcap_setfilter(pcap, &fp);
  579. if (ret != 0) {
  580. printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
  581. exit(EXIT_FAILURE);
  582. }
  583. }
  584. }
  585. else {
  586. show_usage();
  587. exit(EXIT_FAILURE);
  588. }
  589. if (ret != 0)
  590. err_sys(err);
  591. if (pcap_datalink(pcap) == DLT_NULL)
  592. frame = NULL_IF_FRAME_LEN;
  593. while (1) {
  594. static int packetNumber = 0;
  595. struct pcap_pkthdr header;
  596. const unsigned char* packet = pcap_next(pcap, &header);
  597. SSLInfo sslInfo;
  598. void* chain = NULL;
  599. int chainSz = 0;
  600. packetNumber++;
  601. if (packet) {
  602. byte* data = NULL;
  603. if (header.caplen > 40) { /* min ip(20) + min tcp(20) */
  604. packet += frame;
  605. header.caplen -= frame;
  606. }
  607. else
  608. continue;
  609. #ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
  610. isChain = 1;
  611. j = 0;
  612. remainder = header.caplen;
  613. chainSz = 0;
  614. do {
  615. unsigned int chunkSz = min(remainder, CHAIN_INPUT_CHUNK_SIZE);
  616. chains[chainSz].iov_base = (void*)(packet + j);
  617. chains[chainSz].iov_len = chunkSz;
  618. j += chunkSz;
  619. remainder -= chunkSz;
  620. chainSz++;
  621. } while (j < (int)header.caplen);
  622. chain = (void*)chains;
  623. #else
  624. chain = (void*)packet;
  625. chainSz = header.caplen;
  626. (void)isChain;
  627. #endif
  628. #ifdef WOLFSSL_ASYNC_CRYPT
  629. do {
  630. WOLF_EVENT* events[1]; /* poll for single event */
  631. int eventCount = 0;
  632. /* For async call the original API again with same data,
  633. * or call with different sessions for multiple concurrent
  634. * stream processing */
  635. ret = ssl_DecodePacketAsync(chain, chainSz, isChain, &data, err,
  636. &sslInfo, NULL);
  637. if (ret == WC_PENDING_E) {
  638. if (ssl_PollSniffer(events, 1, WOLF_POLL_FLAG_CHECK_HW,
  639. &eventCount) != 0) {
  640. break;
  641. }
  642. }
  643. } while (ret == WC_PENDING_E);
  644. #elif defined(WOLFSSL_SNIFFER_CHAIN_INPUT) && \
  645. defined(WOLFSSL_SNIFFER_STORE_DATA_CB)
  646. ret = ssl_DecodePacketWithChainSessionInfoStoreData(chain, chainSz,
  647. &data, &sslInfo, err);
  648. #elif defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
  649. (void)sslInfo;
  650. ret = ssl_DecodePacketWithChain(chain, chainSz, &data, err);
  651. #else
  652. #if defined(WOLFSSL_SNIFFER_STORE_DATA_CB)
  653. ret = ssl_DecodePacketWithSessionInfoStoreData(packet,
  654. header.caplen, &data, &sslInfo, err);
  655. #else
  656. ret = ssl_DecodePacketWithSessionInfo(packet, header.caplen, &data,
  657. &sslInfo, err);
  658. #endif
  659. (void)chain;
  660. (void)chainSz;
  661. #endif
  662. if (ret < 0) {
  663. printf("ssl_Decode ret = %d, %s\n", ret, err);
  664. hadBadPacket = 1;
  665. }
  666. if (ret > 0) {
  667. /* Convert non-printable data to periods. */
  668. for (j = 0; j < ret; j++) {
  669. if (isprint(data[j]) || isspace(data[j])) continue;
  670. data[j] = '.';
  671. }
  672. data[ret] = 0;
  673. printf("SSL App Data(%d:%d):%s\n", packetNumber, ret, data);
  674. ssl_FreeZeroDecodeBuffer(&data, ret, err);
  675. }
  676. }
  677. else if (saveFile)
  678. break; /* we're done reading file */
  679. }
  680. FreeAll();
  681. return hadBadPacket ? EXIT_FAILURE : EXIT_SUCCESS;
  682. }
  683. #endif /* full build */