openssl_dl.inl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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) || defined(OPENSSL_API_3_0)) \
  85. && !defined(NO_SSL_DL)
  86. #define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr)
  87. #define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr)
  88. #define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr)
  89. #define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr)
  90. #define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr)
  91. #define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr)
  92. #define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr)
  93. #define SSL_new (*(SSL * (*)(SSL_CTX *)) ssl_sw[7].ptr)
  94. #define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *)) ssl_sw[8].ptr)
  95. #define TLS_server_method (*(SSL_METHOD * (*)(void)) ssl_sw[9].ptr)
  96. #define OPENSSL_init_ssl \
  97. (*(int (*)(uint64_t opts, \
  98. const OPENSSL_INIT_SETTINGS *settings))ssl_sw[10] \
  99. .ptr)
  100. #define SSL_CTX_use_PrivateKey_file \
  101. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr)
  102. #define SSL_CTX_use_certificate_file \
  103. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr)
  104. #define SSL_CTX_set_default_passwd_cb \
  105. (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr)
  106. #define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr)
  107. #define SSL_CTX_use_certificate_chain_file \
  108. (*(int (*)(SSL_CTX *, const char *))ssl_sw[15].ptr)
  109. #define TLS_client_method (*(SSL_METHOD * (*)(void)) ssl_sw[16].ptr)
  110. #define SSL_pending (*(int (*)(SSL *))ssl_sw[17].ptr)
  111. #define SSL_CTX_set_verify \
  112. (*(void (*)(SSL_CTX *, \
  113. int, \
  114. int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[18] \
  115. .ptr)
  116. #define SSL_shutdown (*(int (*)(SSL *))ssl_sw[19].ptr)
  117. #define SSL_CTX_load_verify_locations \
  118. (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[20].ptr)
  119. #define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[21].ptr)
  120. #define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[22].ptr)
  121. #define SSL_get_peer_certificate (*(X509 * (*)(SSL *)) ssl_sw[23].ptr)
  122. #define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[24].ptr)
  123. #define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *)) ssl_sw[25].ptr)
  124. #define SSL_CIPHER_get_name \
  125. (*(const char *(*)(const SSL_CIPHER *))ssl_sw[26].ptr)
  126. #define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[27].ptr)
  127. #define SSL_CTX_set_session_id_context \
  128. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[28].ptr)
  129. #define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[29].ptr)
  130. #define SSL_CTX_set_cipher_list \
  131. (*(int (*)(SSL_CTX *, const char *))ssl_sw[30].ptr)
  132. #define SSL_CTX_set_options \
  133. (*(unsigned long (*)(SSL_CTX *, unsigned long))ssl_sw[31].ptr)
  134. #define SSL_CTX_set_info_callback \
  135. (*(void (*)(SSL_CTX * ctx, void (*callback)(const SSL *, int, int))) \
  136. ssl_sw[32] \
  137. .ptr)
  138. #define SSL_get_ex_data (*(char *(*)(const SSL *, int))ssl_sw[33].ptr)
  139. #define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr)
  140. #define SSL_CTX_callback_ctrl \
  141. (*(long (*)(SSL_CTX *, int, void (*)(void)))ssl_sw[35].ptr)
  142. #define SSL_get_servername \
  143. (*(const char *(*)(const SSL *, int type))ssl_sw[36].ptr)
  144. #define SSL_set_SSL_CTX (*(SSL_CTX * (*)(SSL *, SSL_CTX *)) ssl_sw[37].ptr)
  145. #define SSL_ctrl (*(long (*)(SSL *, int, long, void *))ssl_sw[38].ptr)
  146. #define SSL_CTX_set_alpn_protos \
  147. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned))ssl_sw[39].ptr)
  148. typedef int (*tSSL_alpn_select_cb)(SSL *ssl,
  149. const unsigned char **out,
  150. unsigned char *outlen,
  151. const unsigned char *in,
  152. unsigned int inlen,
  153. void *arg);
  154. #define SSL_CTX_set_alpn_select_cb \
  155. (*(void (*)(SSL_CTX *, tSSL_alpn_select_cb, void *))ssl_sw[40].ptr)
  156. typedef int (*tSSL_next_protos_advertised_cb)(SSL *ssl,
  157. const unsigned char **out,
  158. unsigned int *outlen,
  159. void *arg);
  160. #define SSL_CTX_set_next_protos_advertised_cb \
  161. (*(void (*)(SSL_CTX *, tSSL_next_protos_advertised_cb, void *))ssl_sw[41] \
  162. .ptr)
  163. #define SSL_CTX_set_timeout (*(long (*)(SSL_CTX *, long))ssl_sw[42].ptr)
  164. #define SSL_CTX_clear_options(ctx, op) \
  165. SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL)
  166. #define SSL_CTX_set_ecdh_auto(ctx, onoff) \
  167. SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL)
  168. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_CB 53
  169. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG 54
  170. #define SSL_CTRL_SET_TLSEXT_HOSTNAME 55
  171. #define SSL_CTX_set_tlsext_servername_callback(ctx, cb) \
  172. SSL_CTX_callback_ctrl(ctx, \
  173. SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, \
  174. (void (*)(void))cb)
  175. #define SSL_set_tlsext_host_name(ctx, arg) \
  176. SSL_ctrl(ctx, SSL_CTRL_SET_TLSEXT_HOSTNAME, 0, (void *)arg)
  177. #define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
  178. #define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
  179. #define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg))
  180. #define SSL_get_app_data(s) (SSL_get_ex_data(s, 0))
  181. #define SSL_CTX_sess_set_cache_size(ctx, size) SSL_CTX_ctrl(ctx, 42, size, NULL)
  182. #define SSL_CTX_set_session_cache_mode(ctx, mode) \
  183. SSL_CTX_ctrl(ctx, 44, mode, NULL)
  184. #define ERR_get_error (*(unsigned long (*)(void))crypto_sw[0].ptr)
  185. #define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[1].ptr)
  186. #define CONF_modules_unload (*(void (*)(int))crypto_sw[2].ptr)
  187. #define X509_free (*(void (*)(X509 *))crypto_sw[3].ptr)
  188. #define X509_get_subject_name (*(X509_NAME * (*)(X509 *)) crypto_sw[4].ptr)
  189. #define X509_get_issuer_name (*(X509_NAME * (*)(X509 *)) crypto_sw[5].ptr)
  190. #define X509_NAME_oneline \
  191. (*(char *(*)(X509_NAME *, char *, int))crypto_sw[6].ptr)
  192. #define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *)) crypto_sw[7].ptr)
  193. #define EVP_get_digestbyname \
  194. (*(const EVP_MD *(*)(const char *))crypto_sw[8].ptr)
  195. #define EVP_Digest \
  196. (*(int (*)( \
  197. const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \
  198. crypto_sw[9] \
  199. .ptr)
  200. #define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[10].ptr)
  201. #define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[11].ptr)
  202. #define ASN1_INTEGER_to_BN \
  203. (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn)) crypto_sw[12].ptr)
  204. #define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[13].ptr)
  205. #define CRYPTO_free (*(void (*)(void *addr))crypto_sw[14].ptr)
  206. #define ERR_clear_error (*(void (*)(void))crypto_sw[15].ptr)
  207. #define OPENSSL_free(a) CRYPTO_free(a)
  208. #define OPENSSL_REMOVE_THREAD_STATE()
  209. /* init_ssl_ctx() function updates this array.
  210. * It loads SSL library dynamically and changes NULLs to the actual addresses
  211. * of respective functions. The macros above (like SSL_connect()) are really
  212. * just calling these functions indirectly via the pointer. */
  213. static struct ssl_func ssl_sw[] = {
  214. {"SSL_free", TLS_Mandatory, NULL},
  215. {"SSL_accept", TLS_Mandatory, NULL},
  216. {"SSL_connect", TLS_Mandatory, NULL},
  217. {"SSL_read", TLS_Mandatory, NULL},
  218. {"SSL_write", TLS_Mandatory, NULL},
  219. {"SSL_get_error", TLS_Mandatory, NULL},
  220. {"SSL_set_fd", TLS_Mandatory, NULL},
  221. {"SSL_new", TLS_Mandatory, NULL},
  222. {"SSL_CTX_new", TLS_Mandatory, NULL},
  223. {"TLS_server_method", TLS_Mandatory, NULL},
  224. {"OPENSSL_init_ssl", TLS_Mandatory, NULL},
  225. {"SSL_CTX_use_PrivateKey_file", TLS_Mandatory, NULL},
  226. {"SSL_CTX_use_certificate_file", TLS_Mandatory, NULL},
  227. {"SSL_CTX_set_default_passwd_cb", TLS_Mandatory, NULL},
  228. {"SSL_CTX_free", TLS_Mandatory, NULL},
  229. {"SSL_CTX_use_certificate_chain_file", TLS_Mandatory, NULL},
  230. {"TLS_client_method", TLS_Mandatory, NULL},
  231. {"SSL_pending", TLS_Mandatory, NULL},
  232. {"SSL_CTX_set_verify", TLS_Mandatory, NULL},
  233. {"SSL_shutdown", TLS_Mandatory, NULL},
  234. {"SSL_CTX_load_verify_locations", TLS_Mandatory, NULL},
  235. {"SSL_CTX_set_default_verify_paths", TLS_Mandatory, NULL},
  236. {"SSL_CTX_set_verify_depth", TLS_Mandatory, NULL},
  237. #if defined(OPENSSL_API_3_0)
  238. {"SSL_get1_peer_certificate", TLS_Mandatory, NULL},
  239. #else
  240. {"SSL_get_peer_certificate", TLS_Mandatory, NULL},
  241. #endif
  242. {"SSL_get_version", TLS_Mandatory, NULL},
  243. {"SSL_get_current_cipher", TLS_Mandatory, NULL},
  244. {"SSL_CIPHER_get_name", TLS_Mandatory, NULL},
  245. {"SSL_CTX_check_private_key", TLS_Mandatory, NULL},
  246. {"SSL_CTX_set_session_id_context", TLS_Mandatory, NULL},
  247. {"SSL_CTX_ctrl", TLS_Mandatory, NULL},
  248. {"SSL_CTX_set_cipher_list", TLS_Mandatory, NULL},
  249. {"SSL_CTX_set_options", TLS_Mandatory, NULL},
  250. {"SSL_CTX_set_info_callback", TLS_Mandatory, NULL},
  251. {"SSL_get_ex_data", TLS_Mandatory, NULL},
  252. {"SSL_set_ex_data", TLS_Mandatory, NULL},
  253. {"SSL_CTX_callback_ctrl", TLS_Mandatory, NULL},
  254. {"SSL_get_servername", TLS_Mandatory, NULL},
  255. {"SSL_set_SSL_CTX", TLS_Mandatory, NULL},
  256. {"SSL_ctrl", TLS_Mandatory, NULL},
  257. {"SSL_CTX_set_alpn_protos", TLS_ALPN, NULL},
  258. {"SSL_CTX_set_alpn_select_cb", TLS_ALPN, NULL},
  259. {"SSL_CTX_set_next_protos_advertised_cb", TLS_ALPN, NULL},
  260. {"SSL_CTX_set_timeout", TLS_Mandatory, NULL},
  261. {NULL, TLS_END_OF_LIST, NULL}};
  262. /* Similar array as ssl_sw. These functions could be located in different
  263. * lib. */
  264. static struct ssl_func crypto_sw[] = {
  265. {"ERR_get_error", TLS_Mandatory, NULL},
  266. {"ERR_error_string", TLS_Mandatory, NULL},
  267. {"CONF_modules_unload", TLS_Mandatory, NULL},
  268. {"X509_free", TLS_Mandatory, NULL},
  269. {"X509_get_subject_name", TLS_Mandatory, NULL},
  270. {"X509_get_issuer_name", TLS_Mandatory, NULL},
  271. {"X509_NAME_oneline", TLS_Mandatory, NULL},
  272. {"X509_get_serialNumber", TLS_Mandatory, NULL},
  273. {"EVP_get_digestbyname", TLS_Mandatory, NULL},
  274. {"EVP_Digest", TLS_Mandatory, NULL},
  275. {"i2d_X509", TLS_Mandatory, NULL},
  276. {"BN_bn2hex", TLS_Mandatory, NULL},
  277. {"ASN1_INTEGER_to_BN", TLS_Mandatory, NULL},
  278. {"BN_free", TLS_Mandatory, NULL},
  279. {"CRYPTO_free", TLS_Mandatory, NULL},
  280. {"ERR_clear_error", TLS_Mandatory, NULL},
  281. {NULL, TLS_END_OF_LIST, NULL}};
  282. #endif
  283. #if defined(OPENSSL_API_1_0)
  284. #define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr)
  285. #define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr)
  286. #define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr)
  287. #define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr)
  288. #define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr)
  289. #define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr)
  290. #define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr)
  291. #define SSL_new (*(SSL * (*)(SSL_CTX *)) ssl_sw[7].ptr)
  292. #define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *)) ssl_sw[8].ptr)
  293. #define SSLv23_server_method (*(SSL_METHOD * (*)(void)) ssl_sw[9].ptr)
  294. #define SSL_library_init (*(int (*)(void))ssl_sw[10].ptr)
  295. #define SSL_CTX_use_PrivateKey_file \
  296. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr)
  297. #define SSL_CTX_use_certificate_file \
  298. (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr)
  299. #define SSL_CTX_set_default_passwd_cb \
  300. (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr)
  301. #define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr)
  302. #define SSL_load_error_strings (*(void (*)(void))ssl_sw[15].ptr)
  303. #define SSL_CTX_use_certificate_chain_file \
  304. (*(int (*)(SSL_CTX *, const char *))ssl_sw[16].ptr)
  305. #define SSLv23_client_method (*(SSL_METHOD * (*)(void)) ssl_sw[17].ptr)
  306. #define SSL_pending (*(int (*)(SSL *))ssl_sw[18].ptr)
  307. #define SSL_CTX_set_verify \
  308. (*(void (*)(SSL_CTX *, \
  309. int, \
  310. int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[19] \
  311. .ptr)
  312. #define SSL_shutdown (*(int (*)(SSL *))ssl_sw[20].ptr)
  313. #define SSL_CTX_load_verify_locations \
  314. (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[21].ptr)
  315. #define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[22].ptr)
  316. #define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[23].ptr)
  317. #define SSL_get_peer_certificate (*(X509 * (*)(SSL *)) ssl_sw[24].ptr)
  318. #define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[25].ptr)
  319. #define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *)) ssl_sw[26].ptr)
  320. #define SSL_CIPHER_get_name \
  321. (*(const char *(*)(const SSL_CIPHER *))ssl_sw[27].ptr)
  322. #define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[28].ptr)
  323. #define SSL_CTX_set_session_id_context \
  324. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[29].ptr)
  325. #define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[30].ptr)
  326. #define SSL_CTX_set_cipher_list \
  327. (*(int (*)(SSL_CTX *, const char *))ssl_sw[31].ptr)
  328. #define SSL_CTX_set_info_callback \
  329. (*(void (*)(SSL_CTX *, void (*callback)(const SSL *, int, int)))ssl_sw[32] \
  330. .ptr)
  331. #define SSL_get_ex_data (*(char *(*)(const SSL *, int))ssl_sw[33].ptr)
  332. #define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr)
  333. #define SSL_CTX_callback_ctrl \
  334. (*(long (*)(SSL_CTX *, int, void (*)(void)))ssl_sw[35].ptr)
  335. #define SSL_get_servername \
  336. (*(const char *(*)(const SSL *, int type))ssl_sw[36].ptr)
  337. #define SSL_set_SSL_CTX (*(SSL_CTX * (*)(SSL *, SSL_CTX *)) ssl_sw[37].ptr)
  338. #define SSL_ctrl (*(long (*)(SSL *, int, long, void *))ssl_sw[38].ptr)
  339. #define SSL_CTX_set_alpn_protos \
  340. (*(int (*)(SSL_CTX *, const unsigned char *, unsigned))ssl_sw[39].ptr)
  341. typedef int (*tSSL_alpn_select_cb)(SSL *ssl,
  342. const unsigned char **out,
  343. unsigned char *outlen,
  344. const unsigned char *in,
  345. unsigned int inlen,
  346. void *arg);
  347. #define SSL_CTX_set_alpn_select_cb \
  348. (*(void (*)(SSL_CTX *, tSSL_alpn_select_cb, void *))ssl_sw[40].ptr)
  349. typedef int (*tSSL_next_protos_advertised_cb)(SSL *ssl,
  350. const unsigned char **out,
  351. unsigned int *outlen,
  352. void *arg);
  353. #define SSL_CTX_set_next_protos_advertised_cb \
  354. (*(void (*)(SSL_CTX *, tSSL_next_protos_advertised_cb, void *))ssl_sw[41] \
  355. .ptr)
  356. #define SSL_CTX_set_timeout (*(long (*)(SSL_CTX *, long))ssl_sw[42].ptr)
  357. #define SSL_CTX_set_options(ctx, op) \
  358. SSL_CTX_ctrl((ctx), SSL_CTRL_OPTIONS, (op), NULL)
  359. #define SSL_CTX_clear_options(ctx, op) \
  360. SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL)
  361. #define SSL_CTX_set_ecdh_auto(ctx, onoff) \
  362. SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL)
  363. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_CB 53
  364. #define SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG 54
  365. #define SSL_CTRL_SET_TLSEXT_HOSTNAME 55
  366. #define SSL_CTX_set_tlsext_servername_callback(ctx, cb) \
  367. SSL_CTX_callback_ctrl(ctx, \
  368. SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, \
  369. (void (*)(void))cb)
  370. #define SSL_set_tlsext_host_name(ctx, arg) \
  371. SSL_ctrl(ctx, SSL_CTRL_SET_TLSEXT_HOSTNAME, 0, (void *)arg)
  372. #define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
  373. #define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
  374. #define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg))
  375. #define SSL_get_app_data(s) (SSL_get_ex_data(s, 0))
  376. #define SSL_CTX_sess_set_cache_size(ctx, size) SSL_CTX_ctrl(ctx, 42, size, NULL)
  377. #define SSL_CTX_set_session_cache_mode(ctx, mode) \
  378. SSL_CTX_ctrl(ctx, 44, mode, NULL)
  379. #define CRYPTO_num_locks (*(int (*)(void))crypto_sw[0].ptr)
  380. #define CRYPTO_set_locking_callback \
  381. (*(void (*)(void (*)(int, int, const char *, int)))crypto_sw[1].ptr)
  382. #define CRYPTO_set_id_callback \
  383. (*(void (*)(unsigned long (*)(void)))crypto_sw[2].ptr)
  384. #define ERR_get_error (*(unsigned long (*)(void))crypto_sw[3].ptr)
  385. #define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[4].ptr)
  386. #define ERR_remove_state (*(void (*)(unsigned long))crypto_sw[5].ptr)
  387. #define ERR_free_strings (*(void (*)(void))crypto_sw[6].ptr)
  388. #define ENGINE_cleanup (*(void (*)(void))crypto_sw[7].ptr)
  389. #define CONF_modules_unload (*(void (*)(int))crypto_sw[8].ptr)
  390. #define CRYPTO_cleanup_all_ex_data (*(void (*)(void))crypto_sw[9].ptr)
  391. #define EVP_cleanup (*(void (*)(void))crypto_sw[10].ptr)
  392. #define X509_free (*(void (*)(X509 *))crypto_sw[11].ptr)
  393. #define X509_get_subject_name (*(X509_NAME * (*)(X509 *)) crypto_sw[12].ptr)
  394. #define X509_get_issuer_name (*(X509_NAME * (*)(X509 *)) crypto_sw[13].ptr)
  395. #define X509_NAME_oneline \
  396. (*(char *(*)(X509_NAME *, char *, int))crypto_sw[14].ptr)
  397. #define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *)) crypto_sw[15].ptr)
  398. #define i2c_ASN1_INTEGER \
  399. (*(int (*)(ASN1_INTEGER *, unsigned char **))crypto_sw[16].ptr)
  400. #define EVP_get_digestbyname \
  401. (*(const EVP_MD *(*)(const char *))crypto_sw[17].ptr)
  402. #define EVP_Digest \
  403. (*(int (*)( \
  404. const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \
  405. crypto_sw[18] \
  406. .ptr)
  407. #define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[19].ptr)
  408. #define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[20].ptr)
  409. #define ASN1_INTEGER_to_BN \
  410. (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn)) crypto_sw[21].ptr)
  411. #define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[22].ptr)
  412. #define CRYPTO_free (*(void (*)(void *addr))crypto_sw[23].ptr)
  413. #define ERR_clear_error (*(void (*)(void))crypto_sw[24].ptr)
  414. #define OPENSSL_free(a) CRYPTO_free(a)
  415. /* use here ERR_remove_state,
  416. * while on some platforms function is not included into library due to
  417. * deprication */
  418. #define OPENSSL_REMOVE_THREAD_STATE() ERR_remove_state(0)
  419. /* init_ssl_ctx() function updates this array.
  420. * It loads SSL library dynamically and changes NULLs to the actual addresses
  421. * of respective functions. The macros above (like SSL_connect()) are really
  422. * just calling these functions indirectly via the pointer. */
  423. static struct ssl_func ssl_sw[] = {
  424. {"SSL_free", TLS_Mandatory, NULL},
  425. {"SSL_accept", TLS_Mandatory, NULL},
  426. {"SSL_connect", TLS_Mandatory, NULL},
  427. {"SSL_read", TLS_Mandatory, NULL},
  428. {"SSL_write", TLS_Mandatory, NULL},
  429. {"SSL_get_error", TLS_Mandatory, NULL},
  430. {"SSL_set_fd", TLS_Mandatory, NULL},
  431. {"SSL_new", TLS_Mandatory, NULL},
  432. {"SSL_CTX_new", TLS_Mandatory, NULL},
  433. {"SSLv23_server_method", TLS_Mandatory, NULL},
  434. {"SSL_library_init", TLS_Mandatory, NULL},
  435. {"SSL_CTX_use_PrivateKey_file", TLS_Mandatory, NULL},
  436. {"SSL_CTX_use_certificate_file", TLS_Mandatory, NULL},
  437. {"SSL_CTX_set_default_passwd_cb", TLS_Mandatory, NULL},
  438. {"SSL_CTX_free", TLS_Mandatory, NULL},
  439. {"SSL_load_error_strings", TLS_Mandatory, NULL},
  440. {"SSL_CTX_use_certificate_chain_file", TLS_Mandatory, NULL},
  441. {"SSLv23_client_method", TLS_Mandatory, NULL},
  442. {"SSL_pending", TLS_Mandatory, NULL},
  443. {"SSL_CTX_set_verify", TLS_Mandatory, NULL},
  444. {"SSL_shutdown", TLS_Mandatory, NULL},
  445. {"SSL_CTX_load_verify_locations", TLS_Mandatory, NULL},
  446. {"SSL_CTX_set_default_verify_paths", TLS_Mandatory, NULL},
  447. {"SSL_CTX_set_verify_depth", TLS_Mandatory, NULL},
  448. {"SSL_get_peer_certificate", TLS_Mandatory, NULL},
  449. {"SSL_get_version", TLS_Mandatory, NULL},
  450. {"SSL_get_current_cipher", TLS_Mandatory, NULL},
  451. {"SSL_CIPHER_get_name", TLS_Mandatory, NULL},
  452. {"SSL_CTX_check_private_key", TLS_Mandatory, NULL},
  453. {"SSL_CTX_set_session_id_context", TLS_Mandatory, NULL},
  454. {"SSL_CTX_ctrl", TLS_Mandatory, NULL},
  455. {"SSL_CTX_set_cipher_list", TLS_Mandatory, NULL},
  456. {"SSL_CTX_set_info_callback", TLS_Mandatory, NULL},
  457. {"SSL_get_ex_data", TLS_Mandatory, NULL},
  458. {"SSL_set_ex_data", TLS_Mandatory, NULL},
  459. {"SSL_CTX_callback_ctrl", TLS_Mandatory, NULL},
  460. {"SSL_get_servername", TLS_Mandatory, NULL},
  461. {"SSL_set_SSL_CTX", TLS_Mandatory, NULL},
  462. {"SSL_ctrl", TLS_Mandatory, NULL},
  463. {"SSL_CTX_set_alpn_protos", TLS_ALPN, NULL},
  464. {"SSL_CTX_set_alpn_select_cb", TLS_ALPN, NULL},
  465. {"SSL_CTX_set_next_protos_advertised_cb", TLS_ALPN, NULL},
  466. {"SSL_CTX_set_timeout", TLS_Mandatory, NULL},
  467. {NULL, TLS_END_OF_LIST, NULL}};
  468. /* Similar array as ssl_sw. These functions could be located in different
  469. * lib. */
  470. static struct ssl_func crypto_sw[] = {
  471. {"CRYPTO_num_locks", TLS_Mandatory, NULL},
  472. {"CRYPTO_set_locking_callback", TLS_Mandatory, NULL},
  473. {"CRYPTO_set_id_callback", TLS_Mandatory, NULL},
  474. {"ERR_get_error", TLS_Mandatory, NULL},
  475. {"ERR_error_string", TLS_Mandatory, NULL},
  476. {"ERR_remove_state", TLS_Mandatory, NULL},
  477. {"ERR_free_strings", TLS_Mandatory, NULL},
  478. {"ENGINE_cleanup", TLS_Mandatory, NULL},
  479. {"CONF_modules_unload", TLS_Mandatory, NULL},
  480. {"CRYPTO_cleanup_all_ex_data", TLS_Mandatory, NULL},
  481. {"EVP_cleanup", TLS_Mandatory, NULL},
  482. {"X509_free", TLS_Mandatory, NULL},
  483. {"X509_get_subject_name", TLS_Mandatory, NULL},
  484. {"X509_get_issuer_name", TLS_Mandatory, NULL},
  485. {"X509_NAME_oneline", TLS_Mandatory, NULL},
  486. {"X509_get_serialNumber", TLS_Mandatory, NULL},
  487. {"i2c_ASN1_INTEGER", TLS_Mandatory, NULL},
  488. {"EVP_get_digestbyname", TLS_Mandatory, NULL},
  489. {"EVP_Digest", TLS_Mandatory, NULL},
  490. {"i2d_X509", TLS_Mandatory, NULL},
  491. {"BN_bn2hex", TLS_Mandatory, NULL},
  492. {"ASN1_INTEGER_to_BN", TLS_Mandatory, NULL},
  493. {"BN_free", TLS_Mandatory, NULL},
  494. {"CRYPTO_free", TLS_Mandatory, NULL},
  495. {"ERR_clear_error", TLS_Mandatory, NULL},
  496. {NULL, TLS_END_OF_LIST, NULL}};
  497. #endif /* OPENSSL_API_1_0 */