openssl_dl.inl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* Copyright (c) 2013-2021 the Civetweb developers
  2. * Copyright (c) 2004-2013 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. typedef struct ssl_st SSL;
  23. typedef struct ssl_method_st SSL_METHOD;
  24. typedef struct ssl_ctx_st SSL_CTX;
  25. typedef struct x509_store_ctx_st X509_STORE_CTX;
  26. typedef struct x509_name X509_NAME;
  27. typedef struct asn1_integer ASN1_INTEGER;
  28. typedef struct bignum BIGNUM;
  29. typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS;
  30. typedef struct evp_md EVP_MD;
  31. typedef struct x509 X509;
  32. #define SSL_CTRL_OPTIONS (32)
  33. #define SSL_CTRL_CLEAR_OPTIONS (77)
  34. #define SSL_CTRL_SET_ECDH_AUTO (94)
  35. #define OPENSSL_INIT_NO_LOAD_SSL_STRINGS 0x00100000L
  36. #define OPENSSL_INIT_LOAD_SSL_STRINGS 0x00200000L
  37. #define OPENSSL_INIT_LOAD_CRYPTO_STRINGS 0x00000002L
  38. #define SSL_VERIFY_NONE (0)
  39. #define SSL_VERIFY_PEER (1)
  40. #define SSL_VERIFY_FAIL_IF_NO_PEER_CERT (2)
  41. #define SSL_VERIFY_CLIENT_ONCE (4)
  42. #define SSL_OP_ALL (0x80000BFFul)
  43. #define SSL_OP_NO_SSLv2 (0x01000000ul)
  44. #define SSL_OP_NO_SSLv3 (0x02000000ul)
  45. #define SSL_OP_NO_TLSv1 (0x04000000ul)
  46. #define SSL_OP_NO_TLSv1_2 (0x08000000ul)
  47. #define SSL_OP_NO_TLSv1_1 (0x10000000ul)
  48. #define SSL_OP_NO_TLSv1_3 (0x20000000ul)
  49. #define SSL_OP_SINGLE_DH_USE (0x00100000ul)
  50. #define SSL_OP_CIPHER_SERVER_PREFERENCE (0x00400000ul)
  51. #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (0x00010000ul)
  52. #define SSL_OP_NO_COMPRESSION (0x00020000ul)
  53. #define SSL_OP_NO_RENEGOTIATION (0x40000000ul)
  54. #define SSL_CB_HANDSHAKE_START (0x10)
  55. #define SSL_CB_HANDSHAKE_DONE (0x20)
  56. #define SSL_ERROR_NONE (0)
  57. #define SSL_ERROR_SSL (1)
  58. #define SSL_ERROR_WANT_READ (2)
  59. #define SSL_ERROR_WANT_WRITE (3)
  60. #define SSL_ERROR_WANT_X509_LOOKUP (4)
  61. #define SSL_ERROR_SYSCALL (5) /* see errno */
  62. #define SSL_ERROR_ZERO_RETURN (6)
  63. #define SSL_ERROR_WANT_CONNECT (7)
  64. #define SSL_ERROR_WANT_ACCEPT (8)
  65. #define TLSEXT_TYPE_server_name (0)
  66. #define TLSEXT_NAMETYPE_host_name (0)
  67. #define SSL_TLSEXT_ERR_OK (0)
  68. #define SSL_TLSEXT_ERR_ALERT_WARNING (1)
  69. #define SSL_TLSEXT_ERR_ALERT_FATAL (2)
  70. #define SSL_TLSEXT_ERR_NOACK (3)
  71. #define SSL_SESS_CACHE_BOTH (3)
  72. enum ssl_func_category {
  73. TLS_Mandatory, /* required for HTTPS */
  74. TLS_ALPN, /* required for Application Layer Protocol Negotiation */
  75. TLS_END_OF_LIST
  76. };
  77. /* Check if all TLS functions/features are available */
  78. static int tls_feature_missing[TLS_END_OF_LIST] = {0};
  79. struct ssl_func {
  80. const char *name; /* SSL function name */
  81. enum ssl_func_category required; /* Mandatory or optional */
  82. void (*ptr)(void); /* Function pointer */
  83. };
  84. #if defined(OPENSSL_API_1_1)
  85. #define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr)
  86. #define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr)
  87. #define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr)
  88. #define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr)
  89. #define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr)
  90. #define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr)
  91. #define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr)
  92. #define SSL_new (*(SSL * (*)(SSL_CTX *)) ssl_sw[7].ptr)
  93. #define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *)) ssl_sw[8].ptr)
  94. #define TLS_server_method (*(SSL_METHOD * (*)(void)) ssl_sw[9].ptr)
  95. #define OPENSSL_init_ssl \
  96. (*(int (*)(uint64_t opts, \
  97. const OPENSSL_INIT_SETTINGS *settings))ssl_sw[10] \
  98. .ptr)
  99. #define SSL_CTX_use_PrivateKey_file \
  100. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr)
  101. #define SSL_CTX_use_certificate_file \
  102. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr)
  103. #define SSL_CTX_set_default_passwd_cb \
  104. (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr)
  105. #define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr)
  106. #define SSL_CTX_use_certificate_chain_file \
  107. (*(int (*)(SSL_CTX *, const char *))ssl_sw[15].ptr)
  108. #define TLS_client_method (*(SSL_METHOD * (*)(void)) ssl_sw[16].ptr)
  109. #define SSL_pending (*(int (*)(SSL *))ssl_sw[17].ptr)
  110. #define SSL_CTX_set_verify \
  111. (*(void (*)(SSL_CTX *, \
  112. int, \
  113. int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[18] \
  114. .ptr)
  115. #define SSL_shutdown (*(int (*)(SSL *))ssl_sw[19].ptr)
  116. #define SSL_CTX_load_verify_locations \
  117. (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[20].ptr)
  118. #define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[21].ptr)
  119. #define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[22].ptr)
  120. #define SSL_get_peer_certificate (*(X509 * (*)(SSL *)) ssl_sw[23].ptr)
  121. #define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[24].ptr)
  122. #define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *)) ssl_sw[25].ptr)
  123. #define SSL_CIPHER_get_name \
  124. (*(const char *(*)(const SSL_CIPHER *))ssl_sw[26].ptr)
  125. #define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[27].ptr)
  126. #define SSL_CTX_set_session_id_context \
  127. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[28].ptr)
  128. #define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[29].ptr)
  129. #define SSL_CTX_set_cipher_list \
  130. (*(int (*)(SSL_CTX *, const char *))ssl_sw[30].ptr)
  131. #define SSL_CTX_set_options \
  132. (*(unsigned long (*)(SSL_CTX *, unsigned long))ssl_sw[31].ptr)
  133. #define SSL_CTX_set_info_callback \
  134. (*(void (*)(SSL_CTX * ctx, void (*callback)(const SSL *, int, int))) \
  135. ssl_sw[32] \
  136. .ptr)
  137. #define SSL_get_ex_data (*(char *(*)(const SSL *, int))ssl_sw[33].ptr)
  138. #define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr)
  139. #define SSL_CTX_callback_ctrl \
  140. (*(long (*)(SSL_CTX *, int, void (*)(void)))ssl_sw[35].ptr)
  141. #define SSL_get_servername \
  142. (*(const char *(*)(const SSL *, int type))ssl_sw[36].ptr)
  143. #define SSL_set_SSL_CTX (*(SSL_CTX * (*)(SSL *, SSL_CTX *)) ssl_sw[37].ptr)
  144. #define SSL_ctrl (*(long (*)(SSL *, int, long, void *))ssl_sw[38].ptr)
  145. #define SSL_CTX_set_alpn_protos \
  146. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned))ssl_sw[39].ptr)
  147. typedef int (*tSSL_alpn_select_cb)(SSL *ssl,
  148. const unsigned char **out,
  149. unsigned char *outlen,
  150. const unsigned char *in,
  151. unsigned int inlen,
  152. void *arg);
  153. #define SSL_CTX_set_alpn_select_cb \
  154. (*(void (*)(SSL_CTX *, tSSL_alpn_select_cb, void *))ssl_sw[40].ptr)
  155. typedef int (*tSSL_next_protos_advertised_cb)(SSL *ssl,
  156. const unsigned char **out,
  157. unsigned int *outlen,
  158. void *arg);
  159. #define SSL_CTX_set_next_protos_advertised_cb \
  160. (*(void (*)(SSL_CTX *, tSSL_next_protos_advertised_cb, void *))ssl_sw[41] \
  161. .ptr)
  162. #define SSL_CTX_set_timeout (*(long (*)(SSL_CTX *, long))ssl_sw[42].ptr)
  163. #define SSL_CTX_clear_options(ctx, op) \
  164. SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL)
  165. #define SSL_CTX_set_ecdh_auto(ctx, onoff) \
  166. SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL)
  167. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_CB 53
  168. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG 54
  169. #define SSL_CTRL_SET_TLSEXT_HOSTNAME 55
  170. #define SSL_CTX_set_tlsext_servername_callback(ctx, cb) \
  171. SSL_CTX_callback_ctrl(ctx, \
  172. SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, \
  173. (void (*)(void))cb)
  174. #define SSL_set_tlsext_host_name(ctx, arg) \
  175. SSL_ctrl(ctx, SSL_CTRL_SET_TLSEXT_HOSTNAME, 0, (void *)arg)
  176. #define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
  177. #define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
  178. #define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg))
  179. #define SSL_get_app_data(s) (SSL_get_ex_data(s, 0))
  180. #define SSL_CTX_sess_set_cache_size(ctx, size) SSL_CTX_ctrl(ctx, 42, size, NULL)
  181. #define SSL_CTX_set_session_cache_mode(ctx, mode) \
  182. SSL_CTX_ctrl(ctx, 44, mode, NULL)
  183. #define ERR_get_error (*(unsigned long (*)(void))crypto_sw[0].ptr)
  184. #define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[1].ptr)
  185. #define CONF_modules_unload (*(void (*)(int))crypto_sw[2].ptr)
  186. #define X509_free (*(void (*)(X509 *))crypto_sw[3].ptr)
  187. #define X509_get_subject_name (*(X509_NAME * (*)(X509 *)) crypto_sw[4].ptr)
  188. #define X509_get_issuer_name (*(X509_NAME * (*)(X509 *)) crypto_sw[5].ptr)
  189. #define X509_NAME_oneline \
  190. (*(char *(*)(X509_NAME *, char *, int))crypto_sw[6].ptr)
  191. #define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *)) crypto_sw[7].ptr)
  192. #define EVP_get_digestbyname \
  193. (*(const EVP_MD *(*)(const char *))crypto_sw[8].ptr)
  194. #define EVP_Digest \
  195. (*(int (*)( \
  196. const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \
  197. crypto_sw[9] \
  198. .ptr)
  199. #define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[10].ptr)
  200. #define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[11].ptr)
  201. #define ASN1_INTEGER_to_BN \
  202. (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn)) crypto_sw[12].ptr)
  203. #define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[13].ptr)
  204. #define CRYPTO_free (*(void (*)(void *addr))crypto_sw[14].ptr)
  205. #define ERR_clear_error (*(void (*)(void))crypto_sw[15].ptr)
  206. #define OPENSSL_free(a) CRYPTO_free(a)
  207. #define OPENSSL_REMOVE_THREAD_STATE()
  208. /* init_ssl_ctx() function updates this array.
  209. * It loads SSL library dynamically and changes NULLs to the actual addresses
  210. * of respective functions. The macros above (like SSL_connect()) are really
  211. * just calling these functions indirectly via the pointer. */
  212. static struct ssl_func ssl_sw[] = {
  213. {"SSL_free", TLS_Mandatory, NULL},
  214. {"SSL_accept", TLS_Mandatory, NULL},
  215. {"SSL_connect", TLS_Mandatory, NULL},
  216. {"SSL_read", TLS_Mandatory, NULL},
  217. {"SSL_write", TLS_Mandatory, NULL},
  218. {"SSL_get_error", TLS_Mandatory, NULL},
  219. {"SSL_set_fd", TLS_Mandatory, NULL},
  220. {"SSL_new", TLS_Mandatory, NULL},
  221. {"SSL_CTX_new", TLS_Mandatory, NULL},
  222. {"TLS_server_method", TLS_Mandatory, NULL},
  223. {"OPENSSL_init_ssl", TLS_Mandatory, NULL},
  224. {"SSL_CTX_use_PrivateKey_file", TLS_Mandatory, NULL},
  225. {"SSL_CTX_use_certificate_file", TLS_Mandatory, NULL},
  226. {"SSL_CTX_set_default_passwd_cb", TLS_Mandatory, NULL},
  227. {"SSL_CTX_free", TLS_Mandatory, NULL},
  228. {"SSL_CTX_use_certificate_chain_file", TLS_Mandatory, NULL},
  229. {"TLS_client_method", TLS_Mandatory, NULL},
  230. {"SSL_pending", TLS_Mandatory, NULL},
  231. {"SSL_CTX_set_verify", TLS_Mandatory, NULL},
  232. {"SSL_shutdown", TLS_Mandatory, NULL},
  233. {"SSL_CTX_load_verify_locations", TLS_Mandatory, NULL},
  234. {"SSL_CTX_set_default_verify_paths", TLS_Mandatory, NULL},
  235. {"SSL_CTX_set_verify_depth", TLS_Mandatory, NULL},
  236. {"SSL_get_peer_certificate", TLS_Mandatory, NULL},
  237. {"SSL_get_version", TLS_Mandatory, NULL},
  238. {"SSL_get_current_cipher", TLS_Mandatory, NULL},
  239. {"SSL_CIPHER_get_name", TLS_Mandatory, NULL},
  240. {"SSL_CTX_check_private_key", TLS_Mandatory, NULL},
  241. {"SSL_CTX_set_session_id_context", TLS_Mandatory, NULL},
  242. {"SSL_CTX_ctrl", TLS_Mandatory, NULL},
  243. {"SSL_CTX_set_cipher_list", TLS_Mandatory, NULL},
  244. {"SSL_CTX_set_options", TLS_Mandatory, NULL},
  245. {"SSL_CTX_set_info_callback", TLS_Mandatory, NULL},
  246. {"SSL_get_ex_data", TLS_Mandatory, NULL},
  247. {"SSL_set_ex_data", TLS_Mandatory, NULL},
  248. {"SSL_CTX_callback_ctrl", TLS_Mandatory, NULL},
  249. {"SSL_get_servername", TLS_Mandatory, NULL},
  250. {"SSL_set_SSL_CTX", TLS_Mandatory, NULL},
  251. {"SSL_ctrl", TLS_Mandatory, NULL},
  252. {"SSL_CTX_set_alpn_protos", TLS_ALPN, NULL},
  253. {"SSL_CTX_set_alpn_select_cb", TLS_ALPN, NULL},
  254. {"SSL_CTX_set_next_protos_advertised_cb", TLS_ALPN, NULL},
  255. {"SSL_CTX_set_timeout", TLS_Mandatory, NULL},
  256. {NULL, TLS_END_OF_LIST, NULL}};
  257. /* Similar array as ssl_sw. These functions could be located in different
  258. * lib. */
  259. static struct ssl_func crypto_sw[] = {
  260. {"ERR_get_error", TLS_Mandatory, NULL},
  261. {"ERR_error_string", TLS_Mandatory, NULL},
  262. {"CONF_modules_unload", TLS_Mandatory, NULL},
  263. {"X509_free", TLS_Mandatory, NULL},
  264. {"X509_get_subject_name", TLS_Mandatory, NULL},
  265. {"X509_get_issuer_name", TLS_Mandatory, NULL},
  266. {"X509_NAME_oneline", TLS_Mandatory, NULL},
  267. {"X509_get_serialNumber", TLS_Mandatory, NULL},
  268. {"EVP_get_digestbyname", TLS_Mandatory, NULL},
  269. {"EVP_Digest", TLS_Mandatory, NULL},
  270. {"i2d_X509", TLS_Mandatory, NULL},
  271. {"BN_bn2hex", TLS_Mandatory, NULL},
  272. {"ASN1_INTEGER_to_BN", TLS_Mandatory, NULL},
  273. {"BN_free", TLS_Mandatory, NULL},
  274. {"CRYPTO_free", TLS_Mandatory, NULL},
  275. {"ERR_clear_error", TLS_Mandatory, NULL},
  276. {NULL, TLS_END_OF_LIST, NULL}};
  277. #endif
  278. #if defined(OPENSSL_API_1_0)
  279. #define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr)
  280. #define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr)
  281. #define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr)
  282. #define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr)
  283. #define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr)
  284. #define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr)
  285. #define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr)
  286. #define SSL_new (*(SSL * (*)(SSL_CTX *)) ssl_sw[7].ptr)
  287. #define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *)) ssl_sw[8].ptr)
  288. #define SSLv23_server_method (*(SSL_METHOD * (*)(void)) ssl_sw[9].ptr)
  289. #define SSL_library_init (*(int (*)(void))ssl_sw[10].ptr)
  290. #define SSL_CTX_use_PrivateKey_file \
  291. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr)
  292. #define SSL_CTX_use_certificate_file \
  293. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr)
  294. #define SSL_CTX_set_default_passwd_cb \
  295. (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr)
  296. #define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr)
  297. #define SSL_load_error_strings (*(void (*)(void))ssl_sw[15].ptr)
  298. #define SSL_CTX_use_certificate_chain_file \
  299. (*(int (*)(SSL_CTX *, const char *))ssl_sw[16].ptr)
  300. #define SSLv23_client_method (*(SSL_METHOD * (*)(void)) ssl_sw[17].ptr)
  301. #define SSL_pending (*(int (*)(SSL *))ssl_sw[18].ptr)
  302. #define SSL_CTX_set_verify \
  303. (*(void (*)(SSL_CTX *, \
  304. int, \
  305. int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[19] \
  306. .ptr)
  307. #define SSL_shutdown (*(int (*)(SSL *))ssl_sw[20].ptr)
  308. #define SSL_CTX_load_verify_locations \
  309. (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[21].ptr)
  310. #define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[22].ptr)
  311. #define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[23].ptr)
  312. #define SSL_get_peer_certificate (*(X509 * (*)(SSL *)) ssl_sw[24].ptr)
  313. #define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[25].ptr)
  314. #define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *)) ssl_sw[26].ptr)
  315. #define SSL_CIPHER_get_name \
  316. (*(const char *(*)(const SSL_CIPHER *))ssl_sw[27].ptr)
  317. #define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[28].ptr)
  318. #define SSL_CTX_set_session_id_context \
  319. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[29].ptr)
  320. #define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[30].ptr)
  321. #define SSL_CTX_set_cipher_list \
  322. (*(int (*)(SSL_CTX *, const char *))ssl_sw[31].ptr)
  323. #define SSL_CTX_set_info_callback \
  324. (*(void (*)(SSL_CTX *, void (*callback)(const SSL *, int, int)))ssl_sw[32] \
  325. .ptr)
  326. #define SSL_get_ex_data (*(char *(*)(const SSL *, int))ssl_sw[33].ptr)
  327. #define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr)
  328. #define SSL_CTX_callback_ctrl \
  329. (*(long (*)(SSL_CTX *, int, void (*)(void)))ssl_sw[35].ptr)
  330. #define SSL_get_servername \
  331. (*(const char *(*)(const SSL *, int type))ssl_sw[36].ptr)
  332. #define SSL_set_SSL_CTX (*(SSL_CTX * (*)(SSL *, SSL_CTX *)) ssl_sw[37].ptr)
  333. #define SSL_ctrl (*(long (*)(SSL *, int, long, void *))ssl_sw[38].ptr)
  334. #define SSL_CTX_set_alpn_protos \
  335. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned))ssl_sw[39].ptr)
  336. typedef int (*tSSL_alpn_select_cb)(SSL *ssl,
  337. const unsigned char **out,
  338. unsigned char *outlen,
  339. const unsigned char *in,
  340. unsigned int inlen,
  341. void *arg);
  342. #define SSL_CTX_set_alpn_select_cb \
  343. (*(void (*)(SSL_CTX *, tSSL_alpn_select_cb, void *))ssl_sw[40].ptr)
  344. typedef int (*tSSL_next_protos_advertised_cb)(SSL *ssl,
  345. const unsigned char **out,
  346. unsigned int *outlen,
  347. void *arg);
  348. #define SSL_CTX_set_next_protos_advertised_cb \
  349. (*(void (*)(SSL_CTX *, tSSL_next_protos_advertised_cb, void *))ssl_sw[41] \
  350. .ptr)
  351. #define SSL_CTX_set_timeout (*(long (*)(SSL_CTX *, long))ssl_sw[42].ptr)
  352. #define SSL_CTX_set_options(ctx, op) \
  353. SSL_CTX_ctrl((ctx), SSL_CTRL_OPTIONS, (op), NULL)
  354. #define SSL_CTX_clear_options(ctx, op) \
  355. SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL)
  356. #define SSL_CTX_set_ecdh_auto(ctx, onoff) \
  357. SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL)
  358. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_CB 53
  359. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG 54
  360. #define SSL_CTRL_SET_TLSEXT_HOSTNAME 55
  361. #define SSL_CTX_set_tlsext_servername_callback(ctx, cb) \
  362. SSL_CTX_callback_ctrl(ctx, \
  363. SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, \
  364. (void (*)(void))cb)
  365. #define SSL_set_tlsext_host_name(ctx, arg) \
  366. SSL_ctrl(ctx, SSL_CTRL_SET_TLSEXT_HOSTNAME, 0, (void *)arg)
  367. #define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
  368. #define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
  369. #define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg))
  370. #define SSL_get_app_data(s) (SSL_get_ex_data(s, 0))
  371. #define SSL_CTX_sess_set_cache_size(ctx, size) SSL_CTX_ctrl(ctx, 42, size, NULL)
  372. #define SSL_CTX_set_session_cache_mode(ctx, mode) \
  373. SSL_CTX_ctrl(ctx, 44, mode, NULL)
  374. #define CRYPTO_num_locks (*(int (*)(void))crypto_sw[0].ptr)
  375. #define CRYPTO_set_locking_callback \
  376. (*(void (*)(void (*)(int, int, const char *, int)))crypto_sw[1].ptr)
  377. #define CRYPTO_set_id_callback \
  378. (*(void (*)(unsigned long (*)(void)))crypto_sw[2].ptr)
  379. #define ERR_get_error (*(unsigned long (*)(void))crypto_sw[3].ptr)
  380. #define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[4].ptr)
  381. #define ERR_remove_state (*(void (*)(unsigned long))crypto_sw[5].ptr)
  382. #define ERR_free_strings (*(void (*)(void))crypto_sw[6].ptr)
  383. #define ENGINE_cleanup (*(void (*)(void))crypto_sw[7].ptr)
  384. #define CONF_modules_unload (*(void (*)(int))crypto_sw[8].ptr)
  385. #define CRYPTO_cleanup_all_ex_data (*(void (*)(void))crypto_sw[9].ptr)
  386. #define EVP_cleanup (*(void (*)(void))crypto_sw[10].ptr)
  387. #define X509_free (*(void (*)(X509 *))crypto_sw[11].ptr)
  388. #define X509_get_subject_name (*(X509_NAME * (*)(X509 *)) crypto_sw[12].ptr)
  389. #define X509_get_issuer_name (*(X509_NAME * (*)(X509 *)) crypto_sw[13].ptr)
  390. #define X509_NAME_oneline \
  391. (*(char *(*)(X509_NAME *, char *, int))crypto_sw[14].ptr)
  392. #define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *)) crypto_sw[15].ptr)
  393. #define i2c_ASN1_INTEGER \
  394. (*(int (*)(ASN1_INTEGER *, unsigned char **))crypto_sw[16].ptr)
  395. #define EVP_get_digestbyname \
  396. (*(const EVP_MD *(*)(const char *))crypto_sw[17].ptr)
  397. #define EVP_Digest \
  398. (*(int (*)( \
  399. const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \
  400. crypto_sw[18] \
  401. .ptr)
  402. #define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[19].ptr)
  403. #define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[20].ptr)
  404. #define ASN1_INTEGER_to_BN \
  405. (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn)) crypto_sw[21].ptr)
  406. #define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[22].ptr)
  407. #define CRYPTO_free (*(void (*)(void *addr))crypto_sw[23].ptr)
  408. #define ERR_clear_error (*(void (*)(void))crypto_sw[24].ptr)
  409. #define OPENSSL_free(a) CRYPTO_free(a)
  410. /* use here ERR_remove_state,
  411. * while on some platforms function is not included into library due to
  412. * deprication */
  413. #define OPENSSL_REMOVE_THREAD_STATE() ERR_remove_state(0)
  414. /* init_ssl_ctx() function updates this array.
  415. * It loads SSL library dynamically and changes NULLs to the actual addresses
  416. * of respective functions. The macros above (like SSL_connect()) are really
  417. * just calling these functions indirectly via the pointer. */
  418. static struct ssl_func ssl_sw[] = {
  419. {"SSL_free", TLS_Mandatory, NULL},
  420. {"SSL_accept", TLS_Mandatory, NULL},
  421. {"SSL_connect", TLS_Mandatory, NULL},
  422. {"SSL_read", TLS_Mandatory, NULL},
  423. {"SSL_write", TLS_Mandatory, NULL},
  424. {"SSL_get_error", TLS_Mandatory, NULL},
  425. {"SSL_set_fd", TLS_Mandatory, NULL},
  426. {"SSL_new", TLS_Mandatory, NULL},
  427. {"SSL_CTX_new", TLS_Mandatory, NULL},
  428. {"SSLv23_server_method", TLS_Mandatory, NULL},
  429. {"SSL_library_init", TLS_Mandatory, NULL},
  430. {"SSL_CTX_use_PrivateKey_file", TLS_Mandatory, NULL},
  431. {"SSL_CTX_use_certificate_file", TLS_Mandatory, NULL},
  432. {"SSL_CTX_set_default_passwd_cb", TLS_Mandatory, NULL},
  433. {"SSL_CTX_free", TLS_Mandatory, NULL},
  434. {"SSL_load_error_strings", TLS_Mandatory, NULL},
  435. {"SSL_CTX_use_certificate_chain_file", TLS_Mandatory, NULL},
  436. {"SSLv23_client_method", TLS_Mandatory, NULL},
  437. {"SSL_pending", TLS_Mandatory, NULL},
  438. {"SSL_CTX_set_verify", TLS_Mandatory, NULL},
  439. {"SSL_shutdown", TLS_Mandatory, NULL},
  440. {"SSL_CTX_load_verify_locations", TLS_Mandatory, NULL},
  441. {"SSL_CTX_set_default_verify_paths", TLS_Mandatory, NULL},
  442. {"SSL_CTX_set_verify_depth", TLS_Mandatory, NULL},
  443. {"SSL_get_peer_certificate", TLS_Mandatory, NULL},
  444. {"SSL_get_version", TLS_Mandatory, NULL},
  445. {"SSL_get_current_cipher", TLS_Mandatory, NULL},
  446. {"SSL_CIPHER_get_name", TLS_Mandatory, NULL},
  447. {"SSL_CTX_check_private_key", TLS_Mandatory, NULL},
  448. {"SSL_CTX_set_session_id_context", TLS_Mandatory, NULL},
  449. {"SSL_CTX_ctrl", TLS_Mandatory, NULL},
  450. {"SSL_CTX_set_cipher_list", TLS_Mandatory, NULL},
  451. {"SSL_CTX_set_info_callback", TLS_Mandatory, NULL},
  452. {"SSL_get_ex_data", TLS_Mandatory, NULL},
  453. {"SSL_set_ex_data", TLS_Mandatory, NULL},
  454. {"SSL_CTX_callback_ctrl", TLS_Mandatory, NULL},
  455. {"SSL_get_servername", TLS_Mandatory, NULL},
  456. {"SSL_set_SSL_CTX", TLS_Mandatory, NULL},
  457. {"SSL_ctrl", TLS_Mandatory, NULL},
  458. {"SSL_CTX_set_alpn_protos", TLS_ALPN, NULL},
  459. {"SSL_CTX_set_alpn_select_cb", TLS_ALPN, NULL},
  460. {"SSL_CTX_set_next_protos_advertised_cb", TLS_ALPN, NULL},
  461. {"SSL_CTX_set_timeout", TLS_Mandatory, NULL},
  462. {NULL, TLS_END_OF_LIST, NULL}};
  463. /* Similar array as ssl_sw. These functions could be located in different
  464. * lib. */
  465. static struct ssl_func crypto_sw[] = {
  466. {"CRYPTO_num_locks", TLS_Mandatory, NULL},
  467. {"CRYPTO_set_locking_callback", TLS_Mandatory, NULL},
  468. {"CRYPTO_set_id_callback", TLS_Mandatory, NULL},
  469. {"ERR_get_error", TLS_Mandatory, NULL},
  470. {"ERR_error_string", TLS_Mandatory, NULL},
  471. {"ERR_remove_state", TLS_Mandatory, NULL},
  472. {"ERR_free_strings", TLS_Mandatory, NULL},
  473. {"ENGINE_cleanup", TLS_Mandatory, NULL},
  474. {"CONF_modules_unload", TLS_Mandatory, NULL},
  475. {"CRYPTO_cleanup_all_ex_data", TLS_Mandatory, NULL},
  476. {"EVP_cleanup", TLS_Mandatory, NULL},
  477. {"X509_free", TLS_Mandatory, NULL},
  478. {"X509_get_subject_name", TLS_Mandatory, NULL},
  479. {"X509_get_issuer_name", TLS_Mandatory, NULL},
  480. {"X509_NAME_oneline", TLS_Mandatory, NULL},
  481. {"X509_get_serialNumber", TLS_Mandatory, NULL},
  482. {"i2c_ASN1_INTEGER", TLS_Mandatory, NULL},
  483. {"EVP_get_digestbyname", TLS_Mandatory, NULL},
  484. {"EVP_Digest", TLS_Mandatory, NULL},
  485. {"i2d_X509", TLS_Mandatory, NULL},
  486. {"BN_bn2hex", TLS_Mandatory, NULL},
  487. {"ASN1_INTEGER_to_BN", TLS_Mandatory, NULL},
  488. {"BN_free", TLS_Mandatory, NULL},
  489. {"CRYPTO_free", TLS_Mandatory, NULL},
  490. {"ERR_clear_error", TLS_Mandatory, NULL},
  491. {NULL, TLS_END_OF_LIST, NULL}};
  492. #endif /* OPENSSL_API_1_0 */