| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* sniffer.h | 
 | 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 |  | 
 | 22 |  | 
 | 23 |  | 
 | 24 | #ifndef WOLFSSL_SNIFFER_H | 
 | 25 | #define WOLFSSL_SNIFFER_H | 
 | 26 |  | 
 | 27 | #include <wolfssl/wolfcrypt/settings.h> | 
 | 28 |  | 
 | 29 | #ifdef _WIN32 | 
 | 30 |     #ifdef SSL_SNIFFER_EXPORTS | 
 | 31 |         #define SSL_SNIFFER_API __declspec(dllexport) | 
 | 32 |     #else | 
 | 33 |         #define SSL_SNIFFER_API __declspec(dllimport) | 
 | 34 |     #endif | 
 | 35 | #else | 
 | 36 |     #define SSL_SNIFFER_API | 
 | 37 | #endif /* _WIN32 */ | 
 | 38 |  | 
 | 39 |  | 
 | 40 | #ifdef __cplusplus | 
 | 41 |     extern "C" { | 
 | 42 | #endif | 
 | 43 |  | 
 | 44 | /* @param typeK: (formerly keyType) was shadowing a global declaration in | 
 | 45 |  *                wolfssl/wolfcrypt/asn.h line 175 | 
 | 46 |  */ | 
 | 47 | WOLFSSL_API | 
 | 48 | SSL_SNIFFER_API int ssl_SetPrivateKey(const char* address, int port, | 
 | 49 |                                       const char* keyFile, int typeK, | 
 | 50 |                                       const char* password, char* error); | 
 | 51 |  | 
 | 52 | WOLFSSL_API | 
 | 53 | SSL_SNIFFER_API int ssl_SetPrivateKeyBuffer(const char* address, int port, | 
 | 54 |                                             const char* keyBuf, int keySz,  | 
 | 55 |                                             int typeK, const char* password,  | 
 | 56 |                                             char* error); | 
 | 57 |  | 
 | 58 |  | 
 | 59 | WOLFSSL_API | 
 | 60 | SSL_SNIFFER_API int ssl_SetNamedPrivateKey(const char* name, | 
 | 61 |                                            const char* address, int port, | 
 | 62 |                                            const char* keyFile, int typeK, | 
 | 63 |                                            const char* password, char* error); | 
 | 64 |  | 
 | 65 | WOLFSSL_API | 
 | 66 | SSL_SNIFFER_API int ssl_SetNamedPrivateKeyBuffer(const char* name, | 
 | 67 |                                                  const char* address, int port, | 
 | 68 |                                                  const char* keyBuf, int keySz,  | 
 | 69 |                                                  int typeK, const char* password,  | 
 | 70 |                                                  char* error); | 
 | 71 |  | 
 | 72 | WOLFSSL_API  | 
 | 73 | SSL_SNIFFER_API int ssl_SetEphemeralKey(const char* address, int port,  | 
 | 74 |                                         const char* keyFile, int typeKey,  | 
 | 75 |                                         const char* password, char* error); | 
 | 76 |  | 
 | 77 | WOLFSSL_API  | 
 | 78 | SSL_SNIFFER_API int ssl_SetEphemeralKeyBuffer(const char* address, int port,  | 
 | 79 |                                               const char* keyBuf, int keySz, int typeKey,  | 
 | 80 |                                               const char* password, char* error); | 
 | 81 |  | 
 | 82 |  | 
 | 83 | WOLFSSL_API  | 
 | 84 | SSL_SNIFFER_API int ssl_SetNamedEphemeralKey(const char* name, | 
 | 85 |                                              const char* address, int port, | 
 | 86 |                                              const char* keyFile, int typeKey, | 
 | 87 |                                              const char* password, char* error); | 
 | 88 |  | 
 | 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 |  | 
 | 95 | WOLFSSL_API | 
 | 96 | SSL_SNIFFER_API int ssl_DecodePacket(const unsigned char* packet, int length, | 
 | 97 |                                      unsigned char** data, char* error); | 
 | 98 |  | 
 | 99 | WOLFSSL_API | 
 | 100 | SSL_SNIFFER_API int ssl_FreeDecodeBuffer(unsigned char** data, char* error); | 
 | 101 |  | 
 | 102 | WOLFSSL_API | 
 | 103 | SSL_SNIFFER_API int ssl_FreeZeroDecodeBuffer(unsigned char** data, int sz, | 
 | 104 |                                              char* error); | 
 | 105 |  | 
 | 106 | WOLFSSL_API | 
 | 107 | SSL_SNIFFER_API int ssl_Trace(const char* traceFile, char* error); | 
 | 108 |  | 
 | 109 | WOLFSSL_API | 
 | 110 | SSL_SNIFFER_API int ssl_EnableRecovery(int onOff, int maxMemory, char* error); | 
 | 111 |  | 
 | 112 | WOLFSSL_API | 
 | 113 | SSL_SNIFFER_API int ssl_GetSessionStats(unsigned int* active, | 
 | 114 |                                         unsigned int* total, | 
 | 115 |                                         unsigned int* peak, | 
 | 116 |                                         unsigned int* maxSessions, | 
 | 117 |                                         unsigned int* missedData, | 
 | 118 |                                         unsigned int* reassemblyMemory, | 
 | 119 |                                         char* error); | 
 | 120 |  | 
 | 121 | WOLFSSL_API void ssl_InitSniffer(void); | 
 | 122 |  | 
 | 123 | WOLFSSL_API void ssl_FreeSniffer(void); | 
 | 124 |  | 
 | 125 |  | 
 | 126 | /* ssl_SetPrivateKey typeKs */ | 
 | 127 | enum { | 
 | 128 |     FILETYPE_PEM = 1, | 
 | 129 |     FILETYPE_DER = 2, | 
 | 130 | }; | 
 | 131 |  | 
 | 132 |  | 
 | 133 | /* | 
 | 134 |  * New Sniffer API that provides read-only access to the TLS and cipher | 
 | 135 |  * information associated with the SSL session. | 
 | 136 |  */ | 
 | 137 |  | 
 | 138 | typedef struct SSLInfo | 
 | 139 | { | 
 | 140 |     unsigned char  isValid; | 
 | 141 |             /* indicates if the info in this struct is valid: 0 = no, 1 = yes */ | 
 | 142 |     unsigned char  protocolVersionMajor;    /* SSL Version: major */ | 
 | 143 |     unsigned char  protocolVersionMinor;    /* SSL Version: minor */ | 
 | 144 |     unsigned char  serverCipherSuite0;      /* first byte, normally 0 */ | 
 | 145 |     unsigned char  serverCipherSuite;       /* second byte, actual suite */ | 
 | 146 |     unsigned char  serverCipherSuiteName[256]; | 
 | 147 |             /* cipher name, e.g., "TLS_RSA_..." */ | 
 | 148 |     unsigned char  serverNameIndication[128]; | 
 | 149 |     unsigned int   keySize; | 
 | 150 | } SSLInfo; | 
 | 151 |  | 
 | 152 |  | 
 | 153 | WOLFSSL_API | 
 | 154 | SSL_SNIFFER_API int ssl_DecodePacketWithSessionInfo( | 
 | 155 |                         const unsigned char* packet, int length, | 
 | 156 |                         unsigned char** data, SSLInfo* sslInfo, char* error); | 
 | 157 |  | 
 | 158 | typedef void (*SSLConnCb)(const void* session, SSLInfo* info, void* ctx); | 
 | 159 |  | 
 | 160 | WOLFSSL_API | 
 | 161 | SSL_SNIFFER_API int ssl_SetConnectionCb(SSLConnCb cb); | 
 | 162 |  | 
 | 163 | WOLFSSL_API | 
 | 164 | SSL_SNIFFER_API int ssl_SetConnectionCtx(void* ctx); | 
 | 165 |  | 
 | 166 |  | 
 | 167 | typedef struct SSLStats | 
 | 168 | { | 
 | 169 |     unsigned long int sslStandardConns; | 
 | 170 |     unsigned long int sslClientAuthConns; | 
 | 171 |     unsigned long int sslResumedConns; | 
 | 172 |     unsigned long int sslEphemeralMisses; | 
 | 173 |     unsigned long int sslResumeMisses; | 
 | 174 |     unsigned long int sslCiphersUnsupported; | 
 | 175 |     unsigned long int sslKeysUnmatched; | 
 | 176 |     unsigned long int sslKeyFails; | 
 | 177 |     unsigned long int sslDecodeFails; | 
 | 178 |     unsigned long int sslAlerts; | 
 | 179 |     unsigned long int sslDecryptedBytes; | 
 | 180 |     unsigned long int sslEncryptedBytes; | 
 | 181 |     unsigned long int sslEncryptedPackets; | 
 | 182 |     unsigned long int sslDecryptedPackets; | 
 | 183 |     unsigned long int sslKeyMatches; | 
 | 184 |     unsigned long int sslEncryptedConns; | 
 | 185 |  | 
 | 186 |     unsigned long int sslResumptionValid; | 
 | 187 |     unsigned long int sslResumptionInserts; | 
 | 188 | } SSLStats; | 
 | 189 |  | 
 | 190 |  | 
 | 191 | WOLFSSL_API | 
 | 192 | SSL_SNIFFER_API int ssl_ResetStatistics(void); | 
 | 193 |  | 
 | 194 |  | 
 | 195 | WOLFSSL_API | 
 | 196 | SSL_SNIFFER_API int ssl_ReadStatistics(SSLStats* stats); | 
 | 197 |  | 
 | 198 |  | 
 | 199 | WOLFSSL_API | 
 | 200 | SSL_SNIFFER_API int ssl_ReadResetStatistics(SSLStats* stats); | 
 | 201 |  | 
 | 202 |  | 
 | 203 | typedef int (*SSLWatchCb)(void* vSniffer, | 
 | 204 |                         const unsigned char* certHash, | 
 | 205 |                         unsigned int certHashSz, | 
 | 206 |                         const unsigned char* certChain, | 
 | 207 |                         unsigned int certChainSz, | 
 | 208 |                         void* ctx, char* error); | 
 | 209 |  | 
 | 210 | WOLFSSL_API | 
 | 211 | SSL_SNIFFER_API int ssl_SetWatchKeyCallback(SSLWatchCb cb, char* error); | 
 | 212 |  | 
 | 213 | WOLFSSL_API | 
 | 214 | SSL_SNIFFER_API int ssl_SetWatchKeyCallback_ex(SSLWatchCb cb, int devId, | 
 | 215 |                         char* error); | 
 | 216 |  | 
 | 217 | WOLFSSL_API | 
 | 218 | SSL_SNIFFER_API int ssl_SetWatchKeyCtx(void* ctx, char* error); | 
 | 219 |  | 
 | 220 | WOLFSSL_API | 
 | 221 | SSL_SNIFFER_API int ssl_SetWatchKey_buffer(void* vSniffer, | 
 | 222 |                         const unsigned char* key, unsigned int keySz, | 
 | 223 |                         int keyType, char* error); | 
 | 224 |  | 
 | 225 | WOLFSSL_API | 
 | 226 | SSL_SNIFFER_API int ssl_SetWatchKey_file(void* vSniffer, | 
 | 227 |                         const char* keyFile, int keyType, | 
 | 228 |                         const char* password, char* error); | 
 | 229 |  | 
 | 230 |  | 
 | 231 | typedef int (*SSLStoreDataCb)(const unsigned char* decryptBuf, | 
 | 232 |         unsigned int decryptBufSz, unsigned int decryptBufOffset, void* ctx); | 
 | 233 |  | 
 | 234 | WOLFSSL_API | 
 | 235 | SSL_SNIFFER_API int ssl_SetStoreDataCallback(SSLStoreDataCb cb); | 
 | 236 |  | 
 | 237 | WOLFSSL_API | 
 | 238 | SSL_SNIFFER_API int ssl_DecodePacketWithSessionInfoStoreData( | 
 | 239 |         const unsigned char* packet, int length, void* ctx, | 
 | 240 |         SSLInfo* sslInfo, char* error); | 
 | 241 |  | 
 | 242 |  | 
 | 243 | WOLFSSL_API | 
 | 244 | SSL_SNIFFER_API int ssl_DecodePacketWithChain(void* vChain, | 
 | 245 |         unsigned int chainSz, unsigned char** data, char* error); | 
 | 246 |  | 
 | 247 |  | 
 | 248 | WOLFSSL_API | 
 | 249 | SSL_SNIFFER_API int ssl_DecodePacketWithChainSessionInfoStoreData( | 
 | 250 |         void* vChain, unsigned int chainSz, void* ctx, SSLInfo* sslInfo, | 
 | 251 |         char* error); | 
 | 252 |  | 
 | 253 | #ifdef __cplusplus | 
 | 254 |     }  /* extern "C" */ | 
 | 255 | #endif | 
 | 256 |  | 
 | 257 | #endif /* wolfSSL_SNIFFER_H */ | 
 | 258 |  |