ssl.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <openssl/ssl.h>
  7. typedef STACK_OF(SSL_CIPHER) Cryptography_STACK_OF_SSL_CIPHER;
  8. """
  9. TYPES = """
  10. static const long Cryptography_HAS_SSL_ST;
  11. static const long Cryptography_HAS_TLS_ST;
  12. static const long Cryptography_HAS_SSL3_METHOD;
  13. static const long Cryptography_HAS_TLSv1_1;
  14. static const long Cryptography_HAS_TLSv1_2;
  15. static const long Cryptography_HAS_TLSv1_3;
  16. static const long Cryptography_HAS_SECURE_RENEGOTIATION;
  17. static const long Cryptography_HAS_SSL_CTX_CLEAR_OPTIONS;
  18. static const long Cryptography_HAS_DTLS;
  19. static const long Cryptography_HAS_SIGALGS;
  20. static const long Cryptography_HAS_PSK;
  21. static const long Cryptography_HAS_VERIFIED_CHAIN;
  22. static const long Cryptography_HAS_KEYLOG;
  23. static const long Cryptography_HAS_GET_PROTO_VERSION;
  24. static const long Cryptography_HAS_TLSEXT_HOSTNAME;
  25. /* Internally invented symbol to tell us if SSL_MODE_RELEASE_BUFFERS is
  26. * supported
  27. */
  28. static const long Cryptography_HAS_RELEASE_BUFFERS;
  29. /* Internally invented symbol to tell us if SSL_OP_NO_COMPRESSION is
  30. * supported
  31. */
  32. static const long Cryptography_HAS_OP_NO_COMPRESSION;
  33. static const long Cryptography_HAS_SSL_OP_MSIE_SSLV2_RSA_PADDING;
  34. static const long Cryptography_HAS_SSL_SET_SSL_CTX;
  35. static const long Cryptography_HAS_SSL_OP_NO_TICKET;
  36. static const long Cryptography_HAS_ALPN;
  37. static const long Cryptography_HAS_NEXTPROTONEG;
  38. static const long Cryptography_HAS_SET_CERT_CB;
  39. static const long Cryptography_HAS_CUSTOM_EXT;
  40. static const long Cryptography_HAS_SRTP;
  41. static const long SSL_FILETYPE_PEM;
  42. static const long SSL_FILETYPE_ASN1;
  43. static const long SSL_ERROR_NONE;
  44. static const long SSL_ERROR_ZERO_RETURN;
  45. static const long SSL_ERROR_WANT_READ;
  46. static const long SSL_ERROR_WANT_WRITE;
  47. static const long SSL_ERROR_WANT_X509_LOOKUP;
  48. static const long SSL_ERROR_WANT_CONNECT;
  49. static const long SSL_ERROR_SYSCALL;
  50. static const long SSL_ERROR_SSL;
  51. static const long SSL_SENT_SHUTDOWN;
  52. static const long SSL_RECEIVED_SHUTDOWN;
  53. static const long SSL_OP_NO_SSLv2;
  54. static const long SSL_OP_NO_SSLv3;
  55. static const long SSL_OP_NO_TLSv1;
  56. static const long SSL_OP_NO_TLSv1_1;
  57. static const long SSL_OP_NO_TLSv1_2;
  58. static const long SSL_OP_NO_TLSv1_3;
  59. static const long SSL_OP_NO_DTLSv1;
  60. static const long SSL_OP_NO_DTLSv1_2;
  61. static const long SSL_OP_NO_COMPRESSION;
  62. static const long SSL_OP_SINGLE_DH_USE;
  63. static const long SSL_OP_EPHEMERAL_RSA;
  64. static const long SSL_OP_MICROSOFT_SESS_ID_BUG;
  65. static const long SSL_OP_NETSCAPE_CHALLENGE_BUG;
  66. static const long SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
  67. static const long SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG;
  68. static const long SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER;
  69. static const long SSL_OP_MSIE_SSLV2_RSA_PADDING;
  70. static const long SSL_OP_SSLEAY_080_CLIENT_DH_BUG;
  71. static const long SSL_OP_TLS_D5_BUG;
  72. static const long SSL_OP_TLS_BLOCK_PADDING_BUG;
  73. static const long SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
  74. static const long SSL_OP_CIPHER_SERVER_PREFERENCE;
  75. static const long SSL_OP_TLS_ROLLBACK_BUG;
  76. static const long SSL_OP_PKCS1_CHECK_1;
  77. static const long SSL_OP_PKCS1_CHECK_2;
  78. static const long SSL_OP_NETSCAPE_CA_DN_BUG;
  79. static const long SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG;
  80. static const long SSL_OP_NO_QUERY_MTU;
  81. static const long SSL_OP_COOKIE_EXCHANGE;
  82. static const long SSL_OP_NO_TICKET;
  83. static const long SSL_OP_ALL;
  84. static const long SSL_OP_SINGLE_ECDH_USE;
  85. static const long SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  86. static const long SSL_OP_LEGACY_SERVER_CONNECT;
  87. static const long SSL_VERIFY_PEER;
  88. static const long SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  89. static const long SSL_VERIFY_CLIENT_ONCE;
  90. static const long SSL_VERIFY_NONE;
  91. static const long SSL_VERIFY_POST_HANDSHAKE;
  92. static const long SSL_SESS_CACHE_OFF;
  93. static const long SSL_SESS_CACHE_CLIENT;
  94. static const long SSL_SESS_CACHE_SERVER;
  95. static const long SSL_SESS_CACHE_BOTH;
  96. static const long SSL_SESS_CACHE_NO_AUTO_CLEAR;
  97. static const long SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
  98. static const long SSL_SESS_CACHE_NO_INTERNAL_STORE;
  99. static const long SSL_SESS_CACHE_NO_INTERNAL;
  100. static const long SSL_ST_CONNECT;
  101. static const long SSL_ST_ACCEPT;
  102. static const long SSL_ST_MASK;
  103. static const long SSL_ST_INIT;
  104. static const long SSL_ST_BEFORE;
  105. static const long SSL_ST_OK;
  106. static const long SSL_ST_RENEGOTIATE;
  107. static const long SSL_CB_LOOP;
  108. static const long SSL_CB_EXIT;
  109. static const long SSL_CB_READ;
  110. static const long SSL_CB_WRITE;
  111. static const long SSL_CB_ALERT;
  112. static const long SSL_CB_READ_ALERT;
  113. static const long SSL_CB_WRITE_ALERT;
  114. static const long SSL_CB_ACCEPT_LOOP;
  115. static const long SSL_CB_ACCEPT_EXIT;
  116. static const long SSL_CB_CONNECT_LOOP;
  117. static const long SSL_CB_CONNECT_EXIT;
  118. static const long SSL_CB_HANDSHAKE_START;
  119. static const long SSL_CB_HANDSHAKE_DONE;
  120. static const long SSL_MODE_RELEASE_BUFFERS;
  121. static const long SSL_MODE_ENABLE_PARTIAL_WRITE;
  122. static const long SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
  123. static const long SSL_MODE_AUTO_RETRY;
  124. static const long SSL3_RANDOM_SIZE;
  125. static const long TLS_ST_BEFORE;
  126. static const long TLS_ST_OK;
  127. typedef ... SSL_METHOD;
  128. typedef ... SSL_CTX;
  129. typedef ... SSL_SESSION;
  130. typedef ... SSL;
  131. static const long TLSEXT_NAMETYPE_host_name;
  132. static const long TLSEXT_STATUSTYPE_ocsp;
  133. typedef ... SSL_CIPHER;
  134. typedef ... Cryptography_STACK_OF_SSL_CIPHER;
  135. typedef struct {
  136. const char *name;
  137. unsigned long id;
  138. } SRTP_PROTECTION_PROFILE;
  139. """
  140. FUNCTIONS = """
  141. /* SSL */
  142. const char *SSL_state_string_long(const SSL *);
  143. SSL_SESSION *SSL_get1_session(SSL *);
  144. int SSL_set_session(SSL *, SSL_SESSION *);
  145. SSL *SSL_new(SSL_CTX *);
  146. void SSL_free(SSL *);
  147. int SSL_set_fd(SSL *, int);
  148. SSL_CTX *SSL_set_SSL_CTX(SSL *, SSL_CTX *);
  149. void SSL_set_bio(SSL *, BIO *, BIO *);
  150. void SSL_set_connect_state(SSL *);
  151. void SSL_set_accept_state(SSL *);
  152. void SSL_set_shutdown(SSL *, int);
  153. int SSL_get_shutdown(const SSL *);
  154. int SSL_pending(const SSL *);
  155. int SSL_write(SSL *, const void *, int);
  156. int SSL_read(SSL *, void *, int);
  157. int SSL_peek(SSL *, void *, int);
  158. X509 *SSL_get_certificate(const SSL *);
  159. X509 *SSL_get_peer_certificate(const SSL *);
  160. int SSL_get_ex_data_X509_STORE_CTX_idx(void);
  161. /* Added in 1.0.2 */
  162. X509_VERIFY_PARAM *SSL_get0_param(SSL *);
  163. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *);
  164. int SSL_get_sigalgs(SSL *, int, int *, int *, int *, unsigned char *,
  165. unsigned char *);
  166. Cryptography_STACK_OF_X509 *SSL_get_peer_cert_chain(const SSL *);
  167. Cryptography_STACK_OF_X509 *SSL_get0_verified_chain(const SSL *);
  168. Cryptography_STACK_OF_X509_NAME *SSL_get_client_CA_list(const SSL *);
  169. int SSL_get_error(const SSL *, int);
  170. long SSL_get_verify_result(const SSL *ssl);
  171. int SSL_do_handshake(SSL *);
  172. int SSL_shutdown(SSL *);
  173. int SSL_renegotiate(SSL *);
  174. int SSL_renegotiate_pending(SSL *);
  175. const char *SSL_get_cipher_list(const SSL *, int);
  176. /* context */
  177. void SSL_CTX_free(SSL_CTX *);
  178. long SSL_CTX_set_timeout(SSL_CTX *, long);
  179. int SSL_CTX_set_default_verify_paths(SSL_CTX *);
  180. void SSL_CTX_set_verify(SSL_CTX *, int, int (*)(int, X509_STORE_CTX *));
  181. void SSL_CTX_set_verify_depth(SSL_CTX *, int);
  182. int SSL_CTX_get_verify_mode(const SSL_CTX *);
  183. int SSL_CTX_get_verify_depth(const SSL_CTX *);
  184. int SSL_CTX_set_cipher_list(SSL_CTX *, const char *);
  185. int SSL_CTX_load_verify_locations(SSL_CTX *, const char *, const char *);
  186. void SSL_CTX_set_default_passwd_cb(SSL_CTX *, pem_password_cb *);
  187. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void *);
  188. int SSL_CTX_use_certificate(SSL_CTX *, X509 *);
  189. int SSL_CTX_use_certificate_file(SSL_CTX *, const char *, int);
  190. int SSL_CTX_use_certificate_chain_file(SSL_CTX *, const char *);
  191. int SSL_CTX_use_PrivateKey(SSL_CTX *, EVP_PKEY *);
  192. int SSL_CTX_use_PrivateKey_file(SSL_CTX *, const char *, int);
  193. int SSL_CTX_check_private_key(const SSL_CTX *);
  194. void SSL_CTX_set_cert_verify_callback(SSL_CTX *,
  195. int (*)(X509_STORE_CTX *, void *),
  196. void *);
  197. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *,
  198. int (*)(
  199. SSL *,
  200. unsigned char *,
  201. unsigned int *
  202. ));
  203. long SSL_CTX_get_read_ahead(SSL_CTX *);
  204. long SSL_CTX_set_read_ahead(SSL_CTX *, long);
  205. int SSL_CTX_use_psk_identity_hint(SSL_CTX *, const char *);
  206. void SSL_CTX_set_psk_server_callback(SSL_CTX *,
  207. unsigned int (*)(
  208. SSL *,
  209. const char *,
  210. unsigned char *,
  211. unsigned int
  212. ));
  213. void SSL_CTX_set_psk_client_callback(SSL_CTX *,
  214. unsigned int (*)(
  215. SSL *,
  216. const char *,
  217. char *,
  218. unsigned int,
  219. unsigned char *,
  220. unsigned int
  221. ));
  222. int SSL_CTX_set_session_id_context(SSL_CTX *, const unsigned char *,
  223. unsigned int);
  224. void SSL_CTX_set_cert_store(SSL_CTX *, X509_STORE *);
  225. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *);
  226. int SSL_CTX_add_client_CA(SSL_CTX *, X509 *);
  227. void SSL_CTX_set_client_CA_list(SSL_CTX *, Cryptography_STACK_OF_X509_NAME *);
  228. void SSL_CTX_set_info_callback(SSL_CTX *, void (*)(const SSL *, int, int));
  229. void (*SSL_CTX_get_info_callback(SSL_CTX *))(const SSL *, int, int);
  230. void SSL_CTX_set_keylog_callback(SSL_CTX *,
  231. void (*)(const SSL *, const char *));
  232. void (*SSL_CTX_get_keylog_callback(SSL_CTX *))(const SSL *, const char *);
  233. long SSL_CTX_set1_sigalgs_list(SSL_CTX *, const char *);
  234. /* SSL_SESSION */
  235. void SSL_SESSION_free(SSL_SESSION *);
  236. /* Information about actually used cipher */
  237. const char *SSL_CIPHER_get_name(const SSL_CIPHER *);
  238. int SSL_CIPHER_get_bits(const SSL_CIPHER *, int *);
  239. /* the modern signature of this is uint32_t, but older openssl declared it
  240. as unsigned long. To make our compiler flags happy we'll declare it as a
  241. 64-bit wide value, which should always be safe */
  242. uint64_t SSL_CIPHER_get_id(const SSL_CIPHER *);
  243. int SSL_CIPHER_is_aead(const SSL_CIPHER *);
  244. int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *);
  245. int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *);
  246. int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *);
  247. int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *);
  248. size_t SSL_get_finished(const SSL *, void *, size_t);
  249. size_t SSL_get_peer_finished(const SSL *, void *, size_t);
  250. Cryptography_STACK_OF_X509_NAME *SSL_load_client_CA_file(const char *);
  251. const char *SSL_get_servername(const SSL *, const int);
  252. /* Function signature changed to const char * in 1.1.0 */
  253. const char *SSL_CIPHER_get_version(const SSL_CIPHER *);
  254. /* These became macros in 1.1.0 */
  255. int SSL_library_init(void);
  256. void SSL_load_error_strings(void);
  257. /* these CRYPTO_EX_DATA functions became macros in 1.1.0 */
  258. int SSL_get_ex_new_index(long, void *, CRYPTO_EX_new *, CRYPTO_EX_dup *,
  259. CRYPTO_EX_free *);
  260. int SSL_set_ex_data(SSL *, int, void *);
  261. int SSL_CTX_get_ex_new_index(long, void *, CRYPTO_EX_new *, CRYPTO_EX_dup *,
  262. CRYPTO_EX_free *);
  263. int SSL_CTX_set_ex_data(SSL_CTX *, int, void *);
  264. SSL_SESSION *SSL_get_session(const SSL *);
  265. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *, unsigned int *);
  266. long SSL_SESSION_get_time(const SSL_SESSION *);
  267. long SSL_SESSION_get_timeout(const SSL_SESSION *);
  268. int SSL_SESSION_has_ticket(const SSL_SESSION *);
  269. long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *);
  270. unsigned long SSL_set_mode(SSL *, unsigned long);
  271. unsigned long SSL_clear_mode(SSL *, unsigned long);
  272. unsigned long SSL_get_mode(SSL *);
  273. unsigned long SSL_set_options(SSL *, unsigned long);
  274. unsigned long SSL_get_options(SSL *);
  275. int SSL_want_read(const SSL *);
  276. int SSL_want_write(const SSL *);
  277. long SSL_total_renegotiations(SSL *);
  278. long SSL_get_secure_renegotiation_support(SSL *);
  279. long SSL_CTX_set_min_proto_version(SSL_CTX *, int);
  280. long SSL_CTX_set_max_proto_version(SSL_CTX *, int);
  281. long SSL_set_min_proto_version(SSL *, int);
  282. long SSL_set_max_proto_version(SSL *, int);
  283. long SSL_CTX_get_min_proto_version(SSL_CTX *);
  284. long SSL_CTX_get_max_proto_version(SSL_CTX *);
  285. long SSL_get_min_proto_version(SSL *);
  286. long SSL_get_max_proto_version(SSL *);
  287. /* Defined as unsigned long because SSL_OP_ALL is greater than signed 32-bit
  288. and Windows defines long as 32-bit. */
  289. unsigned long SSL_CTX_set_options(SSL_CTX *, unsigned long);
  290. unsigned long SSL_CTX_clear_options(SSL_CTX *, unsigned long);
  291. unsigned long SSL_CTX_get_options(SSL_CTX *);
  292. unsigned long SSL_CTX_set_mode(SSL_CTX *, unsigned long);
  293. unsigned long SSL_CTX_clear_mode(SSL_CTX *, unsigned long);
  294. unsigned long SSL_CTX_get_mode(SSL_CTX *);
  295. unsigned long SSL_CTX_set_session_cache_mode(SSL_CTX *, unsigned long);
  296. unsigned long SSL_CTX_get_session_cache_mode(SSL_CTX *);
  297. unsigned long SSL_CTX_set_tmp_dh(SSL_CTX *, DH *);
  298. unsigned long SSL_CTX_set_tmp_ecdh(SSL_CTX *, EC_KEY *);
  299. unsigned long SSL_CTX_add_extra_chain_cert(SSL_CTX *, X509 *);
  300. /*- These aren't macros these functions are all const X on openssl > 1.0.x -*/
  301. /* methods */
  302. const SSL_METHOD *TLSv1_1_method(void);
  303. const SSL_METHOD *TLSv1_1_server_method(void);
  304. const SSL_METHOD *TLSv1_1_client_method(void);
  305. const SSL_METHOD *TLSv1_2_method(void);
  306. const SSL_METHOD *TLSv1_2_server_method(void);
  307. const SSL_METHOD *TLSv1_2_client_method(void);
  308. const SSL_METHOD *SSLv3_method(void);
  309. const SSL_METHOD *SSLv3_server_method(void);
  310. const SSL_METHOD *SSLv3_client_method(void);
  311. const SSL_METHOD *TLSv1_method(void);
  312. const SSL_METHOD *TLSv1_server_method(void);
  313. const SSL_METHOD *TLSv1_client_method(void);
  314. const SSL_METHOD *DTLSv1_method(void);
  315. const SSL_METHOD *DTLSv1_server_method(void);
  316. const SSL_METHOD *DTLSv1_client_method(void);
  317. /* Added in 1.0.2 */
  318. const SSL_METHOD *DTLS_method(void);
  319. const SSL_METHOD *DTLS_server_method(void);
  320. const SSL_METHOD *DTLS_client_method(void);
  321. const SSL_METHOD *SSLv23_method(void);
  322. const SSL_METHOD *SSLv23_server_method(void);
  323. const SSL_METHOD *SSLv23_client_method(void);
  324. const SSL_METHOD *TLS_method(void);
  325. const SSL_METHOD *TLS_server_method(void);
  326. const SSL_METHOD *TLS_client_method(void);
  327. /*- These aren't macros these arguments are all const X on openssl > 1.0.x -*/
  328. SSL_CTX *SSL_CTX_new(SSL_METHOD *);
  329. long SSL_CTX_get_timeout(const SSL_CTX *);
  330. const SSL_CIPHER *SSL_get_current_cipher(const SSL *);
  331. const char *SSL_get_version(const SSL *);
  332. int SSL_version(const SSL *);
  333. void *SSL_CTX_get_ex_data(const SSL_CTX *, int);
  334. void *SSL_get_ex_data(const SSL *, int);
  335. void SSL_set_tlsext_host_name(SSL *, char *);
  336. void SSL_CTX_set_tlsext_servername_callback(
  337. SSL_CTX *,
  338. int (*)(SSL *, int *, void *));
  339. void SSL_CTX_set_tlsext_servername_arg(
  340. SSL_CTX *, void *);
  341. long SSL_set_tlsext_status_ocsp_resp(SSL *, unsigned char *, int);
  342. long SSL_get_tlsext_status_ocsp_resp(SSL *, const unsigned char **);
  343. long SSL_set_tlsext_status_type(SSL *, long);
  344. long SSL_CTX_set_tlsext_status_cb(SSL_CTX *, int(*)(SSL *, void *));
  345. long SSL_CTX_set_tlsext_status_arg(SSL_CTX *, void *);
  346. int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *, const char *);
  347. int SSL_set_tlsext_use_srtp(SSL *, const char *);
  348. SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *);
  349. long SSL_session_reused(SSL *);
  350. int SSL_select_next_proto(unsigned char **, unsigned char *,
  351. const unsigned char *, unsigned int,
  352. const unsigned char *, unsigned int);
  353. int sk_SSL_CIPHER_num(Cryptography_STACK_OF_SSL_CIPHER *);
  354. const SSL_CIPHER *sk_SSL_CIPHER_value(Cryptography_STACK_OF_SSL_CIPHER *, int);
  355. /* ALPN APIs were introduced in OpenSSL 1.0.2. To continue to support earlier
  356. * versions some special handling of these is necessary.
  357. */
  358. int SSL_CTX_set_alpn_protos(SSL_CTX *, const unsigned char *, unsigned);
  359. int SSL_set_alpn_protos(SSL *, const unsigned char *, unsigned);
  360. void SSL_CTX_set_alpn_select_cb(SSL_CTX *,
  361. int (*) (SSL *,
  362. const unsigned char **,
  363. unsigned char *,
  364. const unsigned char *,
  365. unsigned int,
  366. void *),
  367. void *);
  368. void SSL_get0_alpn_selected(const SSL *, const unsigned char **, unsigned *);
  369. long SSL_get_server_tmp_key(SSL *, EVP_PKEY **);
  370. /* SSL_CTX_set_cert_cb is introduced in OpenSSL 1.0.2. To continue to support
  371. * earlier versions some special handling of these is necessary.
  372. */
  373. void SSL_CTX_set_cert_cb(SSL_CTX *, int (*)(SSL *, void *), void *);
  374. void SSL_set_cert_cb(SSL *, int (*)(SSL *, void *), void *);
  375. int SSL_SESSION_set1_id_context(SSL_SESSION *, const unsigned char *,
  376. unsigned int);
  377. /* Added in 1.1.0 for the great opaquing of structs */
  378. size_t SSL_SESSION_get_master_key(const SSL_SESSION *, unsigned char *,
  379. size_t);
  380. size_t SSL_get_client_random(const SSL *, unsigned char *, size_t);
  381. size_t SSL_get_server_random(const SSL *, unsigned char *, size_t);
  382. int SSL_export_keying_material(SSL *, unsigned char *, size_t, const char *,
  383. size_t, const unsigned char *, size_t, int);
  384. long SSL_CTX_sess_number(SSL_CTX *);
  385. long SSL_CTX_sess_connect(SSL_CTX *);
  386. long SSL_CTX_sess_connect_good(SSL_CTX *);
  387. long SSL_CTX_sess_connect_renegotiate(SSL_CTX *);
  388. long SSL_CTX_sess_accept(SSL_CTX *);
  389. long SSL_CTX_sess_accept_good(SSL_CTX *);
  390. long SSL_CTX_sess_accept_renegotiate(SSL_CTX *);
  391. long SSL_CTX_sess_hits(SSL_CTX *);
  392. long SSL_CTX_sess_cb_hits(SSL_CTX *);
  393. long SSL_CTX_sess_misses(SSL_CTX *);
  394. long SSL_CTX_sess_timeouts(SSL_CTX *);
  395. long SSL_CTX_sess_cache_full(SSL_CTX *);
  396. /* DTLS support */
  397. long Cryptography_DTLSv1_get_timeout(SSL *, time_t *, long *);
  398. long DTLSv1_handle_timeout(SSL *);
  399. long DTLS_set_link_mtu(SSL *, long);
  400. long DTLS_get_link_min_mtu(SSL *);
  401. /* Custom extensions. */
  402. typedef int (*custom_ext_add_cb)(SSL *, unsigned int,
  403. const unsigned char **,
  404. size_t *, int *,
  405. void *);
  406. typedef void (*custom_ext_free_cb)(SSL *, unsigned int,
  407. const unsigned char *,
  408. void *);
  409. typedef int (*custom_ext_parse_cb)(SSL *, unsigned int,
  410. const unsigned char *,
  411. size_t, int *,
  412. void *);
  413. int SSL_CTX_add_client_custom_ext(SSL_CTX *, unsigned int,
  414. custom_ext_add_cb,
  415. custom_ext_free_cb, void *,
  416. custom_ext_parse_cb,
  417. void *);
  418. int SSL_CTX_add_server_custom_ext(SSL_CTX *, unsigned int,
  419. custom_ext_add_cb,
  420. custom_ext_free_cb, void *,
  421. custom_ext_parse_cb,
  422. void *);
  423. int SSL_extension_supported(unsigned int);
  424. int SSL_CTX_set_ciphersuites(SSL_CTX *, const char *);
  425. int SSL_verify_client_post_handshake(SSL *);
  426. void SSL_CTX_set_post_handshake_auth(SSL_CTX *, int);
  427. void SSL_set_post_handshake_auth(SSL *, int);
  428. uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *);
  429. int SSL_write_early_data(SSL *, const void *, size_t, size_t *);
  430. int SSL_read_early_data(SSL *, void *, size_t, size_t *);
  431. int SSL_CTX_set_max_early_data(SSL_CTX *, uint32_t);
  432. """
  433. CUSTOMIZATIONS = """
  434. // This symbol is being preserved because removing it will break users with
  435. // pyOpenSSL < 19.1 and pip < 20.x. We need to leave this in place until those
  436. // users have upgraded. PersistentlyDeprecated2020
  437. static const long Cryptography_HAS_TLSEXT_HOSTNAME = 1;
  438. #if CRYPTOGRAPHY_IS_LIBRESSL
  439. static const long Cryptography_HAS_VERIFIED_CHAIN = 0;
  440. Cryptography_STACK_OF_X509 *(*SSL_get0_verified_chain)(const SSL *) = NULL;
  441. #else
  442. static const long Cryptography_HAS_VERIFIED_CHAIN = 1;
  443. #endif
  444. #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_111
  445. static const long Cryptography_HAS_KEYLOG = 0;
  446. void (*SSL_CTX_set_keylog_callback)(SSL_CTX *,
  447. void (*) (const SSL *, const char *)
  448. ) = NULL;
  449. void (*(*SSL_CTX_get_keylog_callback)(SSL_CTX *))(
  450. const SSL *,
  451. const char *
  452. ) = NULL;
  453. #else
  454. static const long Cryptography_HAS_KEYLOG = 1;
  455. #endif
  456. static const long Cryptography_HAS_SECURE_RENEGOTIATION = 1;
  457. #ifdef OPENSSL_NO_SSL3_METHOD
  458. static const long Cryptography_HAS_SSL3_METHOD = 0;
  459. SSL_METHOD* (*SSLv3_method)(void) = NULL;
  460. SSL_METHOD* (*SSLv3_client_method)(void) = NULL;
  461. SSL_METHOD* (*SSLv3_server_method)(void) = NULL;
  462. #else
  463. static const long Cryptography_HAS_SSL3_METHOD = 1;
  464. #endif
  465. static const long Cryptography_HAS_RELEASE_BUFFERS = 1;
  466. static const long Cryptography_HAS_OP_NO_COMPRESSION = 1;
  467. static const long Cryptography_HAS_TLSv1_1 = 1;
  468. static const long Cryptography_HAS_TLSv1_2 = 1;
  469. static const long Cryptography_HAS_SSL_OP_MSIE_SSLV2_RSA_PADDING = 1;
  470. static const long Cryptography_HAS_SSL_OP_NO_TICKET = 1;
  471. static const long Cryptography_HAS_SSL_SET_SSL_CTX = 1;
  472. static const long Cryptography_HAS_NEXTPROTONEG = 0;
  473. static const long Cryptography_HAS_ALPN = 1;
  474. #if CRYPTOGRAPHY_IS_LIBRESSL
  475. void (*SSL_CTX_set_cert_cb)(SSL_CTX *, int (*)(SSL *, void *), void *) = NULL;
  476. void (*SSL_set_cert_cb)(SSL *, int (*)(SSL *, void *), void *) = NULL;
  477. static const long Cryptography_HAS_SET_CERT_CB = 0;
  478. #else
  479. static const long Cryptography_HAS_SET_CERT_CB = 1;
  480. #endif
  481. static const long Cryptography_HAS_SSL_CTX_CLEAR_OPTIONS = 1;
  482. /* in OpenSSL 1.1.0 the SSL_ST values were renamed to TLS_ST and several were
  483. removed */
  484. #if CRYPTOGRAPHY_IS_LIBRESSL
  485. static const long Cryptography_HAS_SSL_ST = 1;
  486. #else
  487. static const long Cryptography_HAS_SSL_ST = 0;
  488. static const long SSL_ST_BEFORE = 0;
  489. static const long SSL_ST_OK = 0;
  490. static const long SSL_ST_INIT = 0;
  491. static const long SSL_ST_RENEGOTIATE = 0;
  492. #endif
  493. #if !CRYPTOGRAPHY_IS_LIBRESSL
  494. static const long Cryptography_HAS_TLS_ST = 1;
  495. #else
  496. static const long Cryptography_HAS_TLS_ST = 0;
  497. static const long TLS_ST_BEFORE = 0;
  498. static const long TLS_ST_OK = 0;
  499. #endif
  500. #if CRYPTOGRAPHY_IS_LIBRESSL
  501. static const long SSL_OP_NO_DTLSv1 = 0;
  502. static const long SSL_OP_NO_DTLSv1_2 = 0;
  503. long (*DTLS_set_link_mtu)(SSL *, long) = NULL;
  504. long (*DTLS_get_link_min_mtu)(SSL *) = NULL;
  505. #endif
  506. static const long Cryptography_HAS_DTLS = 1;
  507. /* Wrap DTLSv1_get_timeout to avoid cffi to handle a 'struct timeval'. */
  508. long Cryptography_DTLSv1_get_timeout(SSL *ssl, time_t *ptv_sec,
  509. long *ptv_usec) {
  510. struct timeval tv = { 0 };
  511. long r = DTLSv1_get_timeout(ssl, &tv);
  512. if (r == 1) {
  513. if (ptv_sec) {
  514. *ptv_sec = tv.tv_sec;
  515. }
  516. if (ptv_usec) {
  517. *ptv_usec = tv.tv_usec;
  518. }
  519. }
  520. return r;
  521. }
  522. #if CRYPTOGRAPHY_IS_LIBRESSL
  523. static const long Cryptography_HAS_SIGALGS = 0;
  524. const int (*SSL_get_sigalgs)(SSL *, int, int *, int *, int *, unsigned char *,
  525. unsigned char *) = NULL;
  526. const long (*SSL_CTX_set1_sigalgs_list)(SSL_CTX *, const char *) = NULL;
  527. #else
  528. static const long Cryptography_HAS_SIGALGS = 1;
  529. #endif
  530. #if CRYPTOGRAPHY_IS_LIBRESSL || defined(OPENSSL_NO_PSK)
  531. static const long Cryptography_HAS_PSK = 0;
  532. int (*SSL_CTX_use_psk_identity_hint)(SSL_CTX *, const char *) = NULL;
  533. void (*SSL_CTX_set_psk_server_callback)(SSL_CTX *,
  534. unsigned int (*)(
  535. SSL *,
  536. const char *,
  537. unsigned char *,
  538. unsigned int
  539. )) = NULL;
  540. void (*SSL_CTX_set_psk_client_callback)(SSL_CTX *,
  541. unsigned int (*)(
  542. SSL *,
  543. const char *,
  544. char *,
  545. unsigned int,
  546. unsigned char *,
  547. unsigned int
  548. )) = NULL;
  549. #else
  550. static const long Cryptography_HAS_PSK = 1;
  551. #endif
  552. #if !CRYPTOGRAPHY_IS_LIBRESSL
  553. static const long Cryptography_HAS_CUSTOM_EXT = 1;
  554. #else
  555. static const long Cryptography_HAS_CUSTOM_EXT = 0;
  556. typedef int (*custom_ext_add_cb)(SSL *, unsigned int,
  557. const unsigned char **,
  558. size_t *, int *,
  559. void *);
  560. typedef void (*custom_ext_free_cb)(SSL *, unsigned int,
  561. const unsigned char *,
  562. void *);
  563. typedef int (*custom_ext_parse_cb)(SSL *, unsigned int,
  564. const unsigned char *,
  565. size_t, int *,
  566. void *);
  567. int (*SSL_CTX_add_client_custom_ext)(SSL_CTX *, unsigned int,
  568. custom_ext_add_cb,
  569. custom_ext_free_cb, void *,
  570. custom_ext_parse_cb,
  571. void *) = NULL;
  572. int (*SSL_CTX_add_server_custom_ext)(SSL_CTX *, unsigned int,
  573. custom_ext_add_cb,
  574. custom_ext_free_cb, void *,
  575. custom_ext_parse_cb,
  576. void *) = NULL;
  577. int (*SSL_extension_supported)(unsigned int) = NULL;
  578. #endif
  579. #ifndef OPENSSL_NO_SRTP
  580. static const long Cryptography_HAS_SRTP = 1;
  581. #else
  582. static const long Cryptography_HAS_SRTP = 0;
  583. int (*SSL_CTX_set_tlsext_use_srtp)(SSL_CTX *, const char *) = NULL;
  584. int (*SSL_set_tlsext_use_srtp)(SSL *, const char *) = NULL;
  585. SRTP_PROTECTION_PROFILE * (*SSL_get_selected_srtp_profile)(SSL *) = NULL;
  586. #endif
  587. #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_111
  588. static const long Cryptography_HAS_TLSv1_3 = 0;
  589. static const long SSL_OP_NO_TLSv1_3 = 0;
  590. static const long SSL_VERIFY_POST_HANDSHAKE = 0;
  591. int (*SSL_CTX_set_ciphersuites)(SSL_CTX *, const char *) = NULL;
  592. int (*SSL_verify_client_post_handshake)(SSL *) = NULL;
  593. void (*SSL_CTX_set_post_handshake_auth)(SSL_CTX *, int) = NULL;
  594. void (*SSL_set_post_handshake_auth)(SSL *, int) = NULL;
  595. uint32_t (*SSL_SESSION_get_max_early_data)(const SSL_SESSION *) = NULL;
  596. int (*SSL_write_early_data)(SSL *, const void *, size_t, size_t *) = NULL;
  597. int (*SSL_read_early_data)(SSL *, void *, size_t, size_t *) = NULL;
  598. int (*SSL_CTX_set_max_early_data)(SSL_CTX *, uint32_t) = NULL;
  599. #else
  600. static const long Cryptography_HAS_TLSv1_3 = 1;
  601. #endif
  602. #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_111 && !CRYPTOGRAPHY_IS_LIBRESSL
  603. static const long Cryptography_HAS_GET_PROTO_VERSION = 0;
  604. long (*SSL_CTX_get_min_proto_version)(SSL_CTX *) = NULL;
  605. long (*SSL_CTX_get_max_proto_version)(SSL_CTX *) = NULL;
  606. long (*SSL_get_min_proto_version)(SSL *) = NULL;
  607. long (*SSL_get_max_proto_version)(SSL *) = NULL;
  608. #else
  609. static const long Cryptography_HAS_GET_PROTO_VERSION = 1;
  610. #endif
  611. """