sniffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* sniffer.h
  2. *
  3. * Copyright (C) 2006-2023 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. #ifndef WOLFSSL_SNIFFER_H
  22. #define WOLFSSL_SNIFFER_H
  23. #include <wolfssl/wolfcrypt/settings.h>
  24. #include <wolfssl/wolfcrypt/asn_public.h>
  25. #ifdef HAVE_WOLF_EVENT
  26. #include <wolfssl/wolfcrypt/wolfevent.h>
  27. #endif
  28. #ifdef _WIN32
  29. #ifdef SSL_SNIFFER_EXPORTS
  30. #define SSL_SNIFFER_API __declspec(dllexport)
  31. #else
  32. #define SSL_SNIFFER_API __declspec(dllimport)
  33. #endif
  34. #else
  35. #define SSL_SNIFFER_API
  36. #endif /* _WIN32 */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. typedef struct IpAddrInfo {
  41. int version;
  42. union {
  43. word32 ip4;
  44. byte ip6[16];
  45. };
  46. } IpAddrInfo;
  47. typedef struct SnifferStreamInfo {
  48. IpAddrInfo src; /* server address in network byte order */
  49. IpAddrInfo dst; /* client address in network byte order */
  50. word16 dstPort; /* server port */
  51. word16 srcPort; /* client port */
  52. } SnifferStreamInfo;
  53. /* @param typeK: (formerly keyType) was shadowing a global declaration in
  54. * wolfssl/wolfcrypt/asn.h line 175
  55. */
  56. WOLFSSL_API
  57. SSL_SNIFFER_API int ssl_SetPrivateKey(const char* address, int port,
  58. const char* keyFile, int typeK,
  59. const char* password, char* error);
  60. WOLFSSL_API
  61. SSL_SNIFFER_API int ssl_SetPrivateKeyBuffer(const char* address, int port,
  62. const char* keyBuf, int keySz,
  63. int typeK, const char* password,
  64. char* error);
  65. WOLFSSL_API
  66. SSL_SNIFFER_API int ssl_SetNamedPrivateKey(const char* name,
  67. const char* address, int port,
  68. const char* keyFile, int typeK,
  69. const char* password, char* error);
  70. WOLFSSL_API
  71. SSL_SNIFFER_API int ssl_SetNamedPrivateKeyBuffer(const char* name,
  72. const char* address, int port,
  73. const char* keyBuf, int keySz,
  74. int typeK, const char* password,
  75. char* error);
  76. WOLFSSL_API
  77. SSL_SNIFFER_API int ssl_SetEphemeralKey(const char* address, int port,
  78. const char* keyFile, int typeKey,
  79. const char* password, char* error);
  80. WOLFSSL_API
  81. SSL_SNIFFER_API int ssl_SetEphemeralKeyBuffer(const char* address, int port,
  82. const char* keyBuf, int keySz, int typeKey,
  83. const char* password, char* error);
  84. WOLFSSL_API
  85. SSL_SNIFFER_API int ssl_SetNamedEphemeralKey(const char* name,
  86. const char* address, int port,
  87. const char* keyFile, int typeKey,
  88. const char* password, char* error);
  89. WOLFSSL_API
  90. SSL_SNIFFER_API int ssl_SetNamedEphemeralKeyBuffer(const char* name,
  91. const char* address, int port,
  92. const char* keyBuf, int keySz, int typeKey,
  93. const char* password, char* error);
  94. WOLFSSL_API
  95. SSL_SNIFFER_API int ssl_DecodePacket(const unsigned char* packet, int length,
  96. unsigned char** data, char* error);
  97. WOLFSSL_API
  98. SSL_SNIFFER_API int ssl_FreeDecodeBuffer(unsigned char** data, char* error);
  99. WOLFSSL_API
  100. SSL_SNIFFER_API int ssl_FreeZeroDecodeBuffer(unsigned char** data, int sz,
  101. char* error);
  102. WOLFSSL_API
  103. SSL_SNIFFER_API int ssl_Trace(const char* traceFile, char* error);
  104. WOLFSSL_API
  105. SSL_SNIFFER_API int ssl_EnableRecovery(int onOff, int maxMemory, char* error);
  106. WOLFSSL_API
  107. SSL_SNIFFER_API int ssl_GetSessionStats(unsigned int* active,
  108. unsigned int* total,
  109. unsigned int* peak,
  110. unsigned int* maxSessions,
  111. unsigned int* missedData,
  112. unsigned int* reassemblyMemory,
  113. char* error);
  114. WOLFSSL_API
  115. SSL_SNIFFER_API void ssl_InitSniffer(void);
  116. WOLFSSL_API
  117. SSL_SNIFFER_API void ssl_InitSniffer_ex(int devId);
  118. WOLFSSL_API
  119. SSL_SNIFFER_API void ssl_InitSniffer_ex2(int threadNum);
  120. WOLFSSL_API
  121. SSL_SNIFFER_API void ssl_FreeSniffer(void);
  122. /* ssl_SetPrivateKey typeKs */
  123. enum {
  124. FILETYPE_PEM = 1,
  125. FILETYPE_DER = 2,
  126. };
  127. /*
  128. * New Sniffer API that provides read-only access to the TLS and cipher
  129. * information associated with the SSL session.
  130. */
  131. typedef struct SSLInfo
  132. {
  133. unsigned char isValid;
  134. /* indicates if the info in this struct is valid: 0 = no, 1 = yes */
  135. unsigned char protocolVersionMajor; /* SSL Version: major */
  136. unsigned char protocolVersionMinor; /* SSL Version: minor */
  137. unsigned char serverCipherSuite0; /* first byte, normally 0 */
  138. unsigned char serverCipherSuite; /* second byte, actual suite */
  139. unsigned char serverCipherSuiteName[256];
  140. /* cipher name, e.g., "TLS_RSA_..." */
  141. unsigned char serverNameIndication[128];
  142. unsigned int keySize;
  143. } SSLInfo;
  144. WOLFSSL_API
  145. SSL_SNIFFER_API int ssl_DecodePacketWithSessionInfo(
  146. const unsigned char* packet, int length,
  147. unsigned char** data, SSLInfo* sslInfo, char* error);
  148. typedef void (*SSLConnCb)(const void* session, SSLInfo* info, void* ctx);
  149. WOLFSSL_API
  150. SSL_SNIFFER_API int ssl_SetConnectionCb(SSLConnCb cb);
  151. WOLFSSL_API
  152. SSL_SNIFFER_API int ssl_SetConnectionCtx(void* ctx);
  153. typedef struct SSLStats
  154. {
  155. unsigned long int sslStandardConns; /* server_hello count not including resumed sessions */
  156. unsigned long int sslClientAuthConns; /* client's who have presented certificates (mutual authentication) */
  157. unsigned long int sslResumedConns; /* resumed connections */
  158. unsigned long int sslEphemeralMisses; /* TLS v1.2 and older PFS / ephemeral connections missed (not able to decrypt) */
  159. unsigned long int sslResumeMisses; /* Resumption sessions not found */
  160. unsigned long int sslCiphersUnsupported; /* No cipher suite match found when compared to supported */
  161. unsigned long int sslKeysUnmatched; /* Key callback failures (not found). Applies to WOLFSSL_SNIFFER_WATCH only */
  162. unsigned long int sslKeyFails; /* Failures loading or using keys */
  163. unsigned long int sslDecodeFails; /* Dropped packets (not application_data or match protocol version) */
  164. unsigned long int sslAlerts; /* Number of decoded alert messages */
  165. unsigned long int sslDecryptedBytes; /* Number of decrypted bytes */
  166. unsigned long int sslEncryptedBytes; /* Number of encrypted bytes */
  167. unsigned long int sslEncryptedPackets; /* Number of encrypted packets */
  168. unsigned long int sslDecryptedPackets; /* Number of decrypted packets */
  169. unsigned long int sslKeyMatches; /* Key callback successes (failures tracked in sslKeysUnmatched). Applies to WOLFSSL_SNIFFER_WATCH only. */
  170. unsigned long int sslEncryptedConns; /* Number of created sniffer sessions */
  171. unsigned long int sslResumptionInserts; /* Number of sessions reused with resumption */
  172. } SSLStats;
  173. WOLFSSL_API
  174. SSL_SNIFFER_API int ssl_ResetStatistics(void);
  175. WOLFSSL_API
  176. SSL_SNIFFER_API int ssl_ReadStatistics(SSLStats* stats);
  177. WOLFSSL_API
  178. SSL_SNIFFER_API int ssl_ReadResetStatistics(SSLStats* stats);
  179. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_TLS13)
  180. /* macro indicating support for key callback */
  181. #undef WOLFSSL_SNIFFER_KEY_CALLBACK
  182. #define WOLFSSL_SNIFFER_KEY_CALLBACK
  183. typedef int (*SSLKeyCb)(void* vSniffer, int namedGroup,
  184. const unsigned char* srvPub, unsigned int srvPubSz,
  185. const unsigned char* cliPub, unsigned int cliPubSz,
  186. DerBuffer* privKey, void* cbCtx, char* error);
  187. WOLFSSL_API
  188. SSL_SNIFFER_API int ssl_SetKeyCallback(SSLKeyCb cb, void* cbCtx);
  189. #endif
  190. #ifdef WOLFSSL_SNIFFER_WATCH
  191. typedef int (*SSLWatchCb)(void* vSniffer,
  192. const unsigned char* certHash,
  193. unsigned int certHashSz,
  194. const unsigned char* certChain,
  195. unsigned int certChainSz,
  196. void* ctx, char* error);
  197. WOLFSSL_API
  198. SSL_SNIFFER_API int ssl_SetWatchKeyCallback(SSLWatchCb cb, char* error);
  199. WOLFSSL_API
  200. SSL_SNIFFER_API int ssl_SetWatchKeyCallback_ex(SSLWatchCb cb, int devId,
  201. char* error);
  202. WOLFSSL_API
  203. SSL_SNIFFER_API int ssl_SetWatchKeyCtx(void* ctx, char* error);
  204. WOLFSSL_API
  205. SSL_SNIFFER_API int ssl_SetWatchKey_buffer(void* vSniffer,
  206. const unsigned char* key, unsigned int keySz,
  207. int keyType, char* error);
  208. WOLFSSL_API
  209. SSL_SNIFFER_API int ssl_SetWatchKey_file(void* vSniffer,
  210. const char* keyFile, int keyType,
  211. const char* password, char* error);
  212. #endif
  213. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  214. typedef int (*SSLStoreDataCb)(const unsigned char* decryptBuf,
  215. unsigned int decryptBufSz, unsigned int decryptBufOffset, void* ctx);
  216. WOLFSSL_API
  217. SSL_SNIFFER_API int ssl_SetStoreDataCallback(SSLStoreDataCb cb);
  218. #endif
  219. #ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
  220. WOLFSSL_API
  221. SSL_SNIFFER_API int ssl_DecodePacketWithSessionInfoStoreData(
  222. const unsigned char* packet, int length, void* ctx,
  223. SSLInfo* sslInfo, char* error);
  224. #endif
  225. #ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
  226. WOLFSSL_API
  227. SSL_SNIFFER_API int ssl_DecodePacketWithChain(void* vChain,
  228. unsigned int chainSz, unsigned char** data, char* error);
  229. #endif
  230. #if defined(WOLFSSL_SNIFFER_CHAIN_INPUT) && \
  231. defined(WOLFSSL_SNIFFER_STORE_DATA_CB)
  232. WOLFSSL_API
  233. SSL_SNIFFER_API int ssl_DecodePacketWithChainSessionInfoStoreData(
  234. void* vChain, unsigned int chainSz, void* ctx, SSLInfo* sslInfo,
  235. char* error);
  236. #endif
  237. WOLFSSL_API
  238. SSL_SNIFFER_API int ssl_DecodePacket_GetStream(SnifferStreamInfo* info,
  239. const byte* packet, int length, char* error);
  240. #ifdef WOLFSSL_ASYNC_CRYPT
  241. WOLFSSL_API
  242. SSL_SNIFFER_API int ssl_DecodePacketAsync(void* packet, unsigned int packetSz,
  243. int isChain, unsigned char** data, char* error, SSLInfo* sslInfo,
  244. void* userCtx);
  245. WOLFSSL_API
  246. SSL_SNIFFER_API int ssl_PollSniffer(WOLF_EVENT** events, int maxEvents,
  247. WOLF_EVENT_FLAG flags, int* eventCount);
  248. #endif /* WOLFSSL_ASYNC_CRYPT */
  249. #ifdef WOLFSSL_SNIFFER_KEYLOGFILE
  250. typedef enum {
  251. SNIFFER_SECRET_TLS12_MASTER_SECRET,
  252. #if defined(WOLFSSL_TLS13)
  253. SNIFFER_SECRET_CLIENT_EARLY_TRAFFIC_SECRET,
  254. SNIFFER_SECRET_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
  255. SNIFFER_SECRET_SERVER_HANDSHAKE_TRAFFIC_SECRET,
  256. SNIFFER_SECRET_CLIENT_TRAFFIC_SECRET,
  257. SNIFFER_SECRET_SERVER_TRAFFIC_SECRET,
  258. #endif /* WOLFSSL_TLS13 */
  259. SNIFFER_SECRET_NUM_SECRET_TYPES
  260. } SnifferSecretType;
  261. WOLFSSL_API
  262. SSL_SNIFFER_API int ssl_CreateKeyLogSnifferServer(const char* address,
  263. int port,
  264. char* error);
  265. WOLFSSL_API
  266. SSL_SNIFFER_API int ssl_LoadSecretsFromKeyLogFile(const char* keylogfile,
  267. char* error);
  268. typedef int (*SSLSnifferSecretCb)(unsigned char* client_random,
  269. int type,
  270. unsigned char* output_secret);
  271. #endif /* WOLFSSL_SNIFFER_KEYLOGFILE */
  272. #ifdef __cplusplus
  273. } /* extern "C" */
  274. #endif
  275. #endif /* wolfSSL_SNIFFER_H */