Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 17 | #define PY_SSIZE_T_CLEAN |
| 18 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 21 | /* Redefined below for Windows debug builds after important #includes */ |
| 22 | #define _PySSL_FIX_ERRNO |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 23 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 24 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 25 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 26 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 27 | do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } _PySSL_FIX_ERRNO; } while (0) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 28 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 29 | PyThreadState *_save = NULL; \ |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 30 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 31 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 32 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 33 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 35 | /* Include symbols from _socket module */ |
| 36 | #include "socketmodule.h" |
| 37 | |
| 38 | static PySocketModule_APIObject PySocketModule; |
| 39 | |
| 40 | #if defined(HAVE_POLL_H) |
| 41 | #include <poll.h> |
| 42 | #elif defined(HAVE_SYS_POLL_H) |
| 43 | #include <sys/poll.h> |
| 44 | #endif |
| 45 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 46 | /* Don't warn about deprecated functions */ |
| 47 | #ifdef __GNUC__ |
| 48 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 49 | #endif |
| 50 | #ifdef __clang__ |
| 51 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 52 | #endif |
| 53 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 54 | /* Include OpenSSL header files */ |
| 55 | #include "openssl/rsa.h" |
| 56 | #include "openssl/crypto.h" |
| 57 | #include "openssl/x509.h" |
| 58 | #include "openssl/x509v3.h" |
| 59 | #include "openssl/pem.h" |
| 60 | #include "openssl/ssl.h" |
| 61 | #include "openssl/err.h" |
| 62 | #include "openssl/rand.h" |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 63 | #include "openssl/bio.h" |
Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 64 | #include "openssl/dh.h" |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 65 | |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 66 | #ifndef HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 67 | # ifdef LIBRESSL_VERSION_NUMBER |
| 68 | # error "LibreSSL is missing X509_VERIFY_PARAM_set1_host(), see https://github.com/libressl-portable/portable/issues/381" |
| 69 | # elif OPENSSL_VERSION_NUMBER > 0x1000200fL |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 70 | # define HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 71 | # else |
| 72 | # error "libssl is too old and does not support X509_VERIFY_PARAM_set1_host()" |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 73 | # endif |
| 74 | #endif |
| 75 | |
Christian Heimes | c087a26 | 2020-05-15 20:55:25 +0200 | [diff] [blame] | 76 | #ifndef OPENSSL_THREADS |
| 77 | # error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL" |
| 78 | #endif |
| 79 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 80 | /* SSL error object */ |
| 81 | static PyObject *PySSLErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 82 | static PyObject *PySSLCertVerificationErrorObject; |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 83 | static PyObject *PySSLZeroReturnErrorObject; |
| 84 | static PyObject *PySSLWantReadErrorObject; |
| 85 | static PyObject *PySSLWantWriteErrorObject; |
| 86 | static PyObject *PySSLSyscallErrorObject; |
| 87 | static PyObject *PySSLEOFErrorObject; |
| 88 | |
| 89 | /* Error mappings */ |
| 90 | static PyObject *err_codes_to_names; |
| 91 | static PyObject *err_names_to_codes; |
| 92 | static PyObject *lib_codes_to_names; |
| 93 | |
| 94 | struct py_ssl_error_code { |
| 95 | const char *mnemonic; |
| 96 | int library, reason; |
| 97 | }; |
| 98 | struct py_ssl_library_code { |
| 99 | const char *library; |
| 100 | int code; |
| 101 | }; |
| 102 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 103 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 104 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 105 | * However, because it uses a different CRT, we need to transfer the |
| 106 | * value of errno from OpenSSL into our debug CRT. |
| 107 | * |
| 108 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 109 | * alternative is to do both debug and release builds of OpenSSL, which |
| 110 | * requires much uglier code to transform their automatically generated |
| 111 | * makefile. This is the lesser of all the evils. |
| 112 | */ |
| 113 | |
| 114 | static void _PySSLFixErrno(void) { |
| 115 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 116 | if (!ucrtbase) { |
| 117 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 118 | * have a catastrophic failure, but this function is not the |
| 119 | * place to raise it. */ |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | typedef int *(__stdcall *errno_func)(void); |
| 124 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 125 | if (ssl_errno) { |
| 126 | errno = *ssl_errno(); |
| 127 | *ssl_errno() = 0; |
| 128 | } else { |
| 129 | errno = ENOTRECOVERABLE; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #undef _PySSL_FIX_ERRNO |
| 134 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 135 | #endif |
| 136 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 137 | /* Include generated data (error codes) */ |
| 138 | #include "_ssl_data.h" |
| 139 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 140 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 141 | # define OPENSSL_VERSION_1_1 1 |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 142 | # define PY_OPENSSL_1_1_API 1 |
| 143 | #endif |
| 144 | |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 145 | /* OpenSSL API compat */ |
| 146 | #ifdef OPENSSL_API_COMPAT |
| 147 | #if OPENSSL_API_COMPAT >= 0x10100000L |
| 148 | |
| 149 | /* OpenSSL API 1.1.0+ does not include version methods */ |
| 150 | #ifndef OPENSSL_NO_TLS1_METHOD |
| 151 | #define OPENSSL_NO_TLS1_METHOD 1 |
| 152 | #endif |
| 153 | #ifndef OPENSSL_NO_TLS1_1_METHOD |
| 154 | #define OPENSSL_NO_TLS1_1_METHOD 1 |
| 155 | #endif |
| 156 | #ifndef OPENSSL_NO_TLS1_2_METHOD |
| 157 | #define OPENSSL_NO_TLS1_2_METHOD 1 |
| 158 | #endif |
| 159 | |
| 160 | #endif /* >= 1.1.0 compcat */ |
| 161 | #endif /* OPENSSL_API_COMPAT */ |
| 162 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 163 | /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ |
| 164 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL |
| 165 | # define PY_OPENSSL_1_1_API 1 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 166 | #endif |
| 167 | |
Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 168 | /* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 169 | * This includes the SSL_set_SSL_CTX() function. |
| 170 | */ |
| 171 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 172 | # define HAVE_SNI 1 |
| 173 | #else |
| 174 | # define HAVE_SNI 0 |
| 175 | #endif |
| 176 | |
Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 177 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 178 | # define HAVE_ALPN 1 |
| 179 | #else |
| 180 | # define HAVE_ALPN 0 |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 181 | #endif |
| 182 | |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 183 | /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped |
| 184 | * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility |
| 185 | * reasons. The check for TLSEXT_TYPE_next_proto_neg works with |
| 186 | * OpenSSL 1.0.1+ and LibreSSL. |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 187 | * OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg. |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 188 | */ |
| 189 | #ifdef OPENSSL_NO_NEXTPROTONEG |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 190 | # define HAVE_NPN 0 |
| 191 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 192 | # define HAVE_NPN 0 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 193 | #elif defined(TLSEXT_TYPE_next_proto_neg) |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 194 | # define HAVE_NPN 1 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 195 | #else |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 196 | # define HAVE_NPN 0 |
| 197 | #endif |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 198 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 199 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 200 | #define HAVE_OPENSSL_KEYLOG 1 |
| 201 | #endif |
| 202 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 203 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 204 | #define INVALID_SOCKET (-1) |
| 205 | #endif |
| 206 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 207 | /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ |
| 208 | #ifndef OPENSSL_VERSION_1_1 |
| 209 | #define HAVE_OPENSSL_CRYPTO_LOCK |
| 210 | #endif |
| 211 | |
| 212 | #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 213 | #define OPENSSL_NO_SSL2 |
| 214 | #endif |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 215 | |
| 216 | #ifndef PY_OPENSSL_1_1_API |
| 217 | /* OpenSSL 1.1 API shims for OpenSSL < 1.1.0 and LibreSSL < 2.7.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 218 | |
| 219 | #define TLS_method SSLv23_method |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 220 | #define TLS_client_method SSLv23_client_method |
| 221 | #define TLS_server_method SSLv23_server_method |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 222 | #define ASN1_STRING_get0_data ASN1_STRING_data |
| 223 | #define X509_get0_notBefore X509_get_notBefore |
| 224 | #define X509_get0_notAfter X509_get_notAfter |
| 225 | #define OpenSSL_version_num SSLeay |
| 226 | #define OpenSSL_version SSLeay_version |
| 227 | #define OPENSSL_VERSION SSLEAY_VERSION |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 228 | |
| 229 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 230 | { |
| 231 | return ne->set; |
| 232 | } |
| 233 | |
| 234 | #ifndef OPENSSL_NO_COMP |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 235 | /* LCOV_EXCL_START */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 236 | static int COMP_get_type(const COMP_METHOD *meth) |
| 237 | { |
| 238 | return meth->type; |
| 239 | } |
Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 240 | /* LCOV_EXCL_STOP */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 241 | #endif |
| 242 | |
| 243 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 244 | { |
| 245 | return ctx->default_passwd_callback; |
| 246 | } |
| 247 | |
| 248 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 249 | { |
| 250 | return ctx->default_passwd_callback_userdata; |
| 251 | } |
| 252 | |
| 253 | static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 254 | { |
| 255 | return x->type; |
| 256 | } |
| 257 | |
| 258 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 259 | { |
| 260 | return x->data.x509; |
| 261 | } |
| 262 | |
| 263 | static int BIO_up_ref(BIO *b) |
| 264 | { |
| 265 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 270 | return store->objs; |
| 271 | } |
| 272 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 273 | static int |
| 274 | SSL_SESSION_has_ticket(const SSL_SESSION *s) |
| 275 | { |
| 276 | return (s->tlsext_ticklen > 0) ? 1 : 0; |
| 277 | } |
| 278 | |
| 279 | static unsigned long |
| 280 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
| 281 | { |
| 282 | return s->tlsext_tick_lifetime_hint; |
| 283 | } |
| 284 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 285 | #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 286 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 287 | /* Default cipher suites */ |
| 288 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 289 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 290 | #endif |
| 291 | |
| 292 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 293 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 294 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 295 | #endif |
| 296 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 297 | /* Python custom selection of sensible cipher suites |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 298 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. |
| 299 | * !aNULL:!eNULL: really no NULL ciphers |
| 300 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. |
| 301 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
| 302 | * !SRP:!PSK: no secure remote password or pre-shared key authentication |
| 303 | */ |
| 304 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" |
| 305 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 306 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 307 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 308 | #else |
| 309 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 310 | #endif |
| 311 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 312 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 313 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 314 | /* these mirror ssl.h */ |
| 315 | PY_SSL_ERROR_NONE, |
| 316 | PY_SSL_ERROR_SSL, |
| 317 | PY_SSL_ERROR_WANT_READ, |
| 318 | PY_SSL_ERROR_WANT_WRITE, |
| 319 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 320 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 321 | PY_SSL_ERROR_ZERO_RETURN, |
| 322 | PY_SSL_ERROR_WANT_CONNECT, |
| 323 | /* start of non ssl.h errorcodes */ |
| 324 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 325 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 326 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 327 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 328 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 329 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 330 | PY_SSL_CLIENT, |
| 331 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 335 | PY_SSL_CERT_NONE, |
| 336 | PY_SSL_CERT_OPTIONAL, |
| 337 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 341 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 342 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 343 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 344 | PY_SSL_VERSION_TLS1, |
| 345 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 346 | PY_SSL_VERSION_TLS1_2, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 347 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 348 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 349 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 350 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 351 | enum py_proto_version { |
| 352 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 353 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 354 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 355 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 356 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 357 | #ifdef TLS1_3_VERSION |
| 358 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 359 | #else |
| 360 | PY_PROTO_TLSv1_3 = 0x304, |
| 361 | #endif |
| 362 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 363 | |
| 364 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 365 | * available version, and the other way around. We have to figure out the |
| 366 | * minimum and maximum available version on our own and hope for the best. |
| 367 | */ |
| 368 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 369 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 370 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 371 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 372 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 373 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 374 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 375 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 376 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 377 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 378 | #else |
| 379 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 380 | #endif |
| 381 | |
| 382 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 383 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 384 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 385 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 386 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 387 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 388 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 389 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 390 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 391 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 392 | #else |
| 393 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 394 | #endif |
| 395 | }; |
| 396 | |
| 397 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 398 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 399 | /* 0 means no, greater than 0 means yes */ |
| 400 | |
| 401 | static unsigned int _ssl_locks_count = 0; |
| 402 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 403 | /* SSL socket object */ |
| 404 | |
| 405 | #define X509_NAME_MAXLEN 256 |
| 406 | |
Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 407 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 408 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 409 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 410 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 411 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 412 | #else |
| 413 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 414 | #endif |
| 415 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 416 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 417 | * older SSL, but let's be safe */ |
| 418 | #define PySSL_CB_MAXLEN 128 |
| 419 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 420 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 421 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 422 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 423 | SSL_CTX *ctx; |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 424 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 425 | unsigned char *npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 426 | int npn_protocols_len; |
| 427 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 428 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 429 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 430 | unsigned int alpn_protocols_len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 431 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 432 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 433 | PyObject *set_sni_cb; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 434 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 435 | int check_hostname; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 436 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 437 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 438 | */ |
| 439 | unsigned int hostflags; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 440 | int protocol; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 441 | #ifdef TLS1_3_VERSION |
| 442 | int post_handshake_auth; |
| 443 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 444 | PyObject *msg_cb; |
| 445 | #ifdef HAVE_OPENSSL_KEYLOG |
| 446 | PyObject *keylog_filename; |
| 447 | BIO *keylog_bio; |
| 448 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 449 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 451 | typedef struct { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 452 | int ssl; /* last seen error from SSL */ |
| 453 | int c; /* last seen error from libc */ |
| 454 | #ifdef MS_WINDOWS |
| 455 | int ws; /* last seen error from winsock */ |
| 456 | #endif |
| 457 | } _PySSLError; |
| 458 | |
| 459 | typedef struct { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 460 | PyObject_HEAD |
| 461 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 462 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 463 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 464 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 465 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 466 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 467 | PyObject *server_hostname; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 468 | _PySSLError err; /* last seen error from various sources */ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 469 | /* Some SSL callbacks don't have error reporting. Callback wrappers |
| 470 | * store exception information on the socket. The handshake, read, write, |
| 471 | * and shutdown methods check for chained exceptions. |
| 472 | */ |
| 473 | PyObject *exc_type; |
| 474 | PyObject *exc_value; |
| 475 | PyObject *exc_tb; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 476 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 478 | typedef struct { |
| 479 | PyObject_HEAD |
| 480 | BIO *bio; |
| 481 | int eof_written; |
| 482 | } PySSLMemoryBIO; |
| 483 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 484 | typedef struct { |
| 485 | PyObject_HEAD |
| 486 | SSL_SESSION *session; |
| 487 | PySSLContext *ctx; |
| 488 | } PySSLSession; |
| 489 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 490 | static PyTypeObject PySSLContext_Type; |
| 491 | static PyTypeObject PySSLSocket_Type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 492 | static PyTypeObject PySSLMemoryBIO_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 493 | static PyTypeObject PySSLSession_Type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 494 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 495 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 496 | { |
| 497 | _PySSLError err = { 0 }; |
| 498 | if (failed) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 499 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 500 | err.ws = WSAGetLastError(); |
| 501 | _PySSL_FIX_ERRNO; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 502 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 503 | err.c = errno; |
| 504 | err.ssl = SSL_get_error(ssl, retcode); |
| 505 | } |
| 506 | return err; |
| 507 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 508 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 509 | /*[clinic input] |
| 510 | module _ssl |
| 511 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" |
| 512 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" |
| 513 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 514 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 515 | [clinic start generated code]*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 516 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 517 | |
| 518 | #include "clinic/_ssl.c.h" |
| 519 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 520 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 521 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 522 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 523 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 524 | #define PySSLSocket_Check(v) Py_IS_TYPE(v, &PySSLSocket_Type) |
| 525 | #define PySSLMemoryBIO_Check(v) Py_IS_TYPE(v, &PySSLMemoryBIO_Type) |
| 526 | #define PySSLSession_Check(v) Py_IS_TYPE(v, &PySSLSession_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 527 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 528 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 529 | SOCKET_IS_NONBLOCKING, |
| 530 | SOCKET_IS_BLOCKING, |
| 531 | SOCKET_HAS_TIMED_OUT, |
| 532 | SOCKET_HAS_BEEN_CLOSED, |
| 533 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 534 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 535 | } timeout_state; |
| 536 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 537 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 538 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 539 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 540 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 541 | /* Get the socket from a PySSLSocket, if it has one */ |
| 542 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 543 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 544 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 545 | /* If sock is NULL, use a timeout of 0 second */ |
| 546 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 547 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 548 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 549 | #include "_ssl/debughelpers.c" |
| 550 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 551 | /* |
| 552 | * SSL errors. |
| 553 | */ |
| 554 | |
| 555 | PyDoc_STRVAR(SSLError_doc, |
| 556 | "An error occurred in the SSL implementation."); |
| 557 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 558 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 559 | "A certificate could not be verified."); |
| 560 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 561 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 562 | "SSL/TLS session closed cleanly."); |
| 563 | |
| 564 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 565 | "Non-blocking SSL socket needs to read more data\n" |
| 566 | "before the requested operation can be completed."); |
| 567 | |
| 568 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 569 | "Non-blocking SSL socket needs to write more data\n" |
| 570 | "before the requested operation can be completed."); |
| 571 | |
| 572 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 573 | "System error when attempting SSL operation."); |
| 574 | |
| 575 | PyDoc_STRVAR(SSLEOFError_doc, |
| 576 | "SSL/TLS connection terminated abruptly."); |
| 577 | |
| 578 | static PyObject * |
| 579 | SSLError_str(PyOSErrorObject *self) |
| 580 | { |
| 581 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 582 | Py_INCREF(self->strerror); |
| 583 | return self->strerror; |
| 584 | } |
| 585 | else |
| 586 | return PyObject_Str(self->args); |
| 587 | } |
| 588 | |
| 589 | static PyType_Slot sslerror_type_slots[] = { |
| 590 | {Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */ |
Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 591 | {Py_tp_doc, (void*)SSLError_doc}, |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 592 | {Py_tp_str, SSLError_str}, |
| 593 | {0, 0}, |
| 594 | }; |
| 595 | |
| 596 | static PyType_Spec sslerror_type_spec = { |
| 597 | "ssl.SSLError", |
| 598 | sizeof(PyOSErrorObject), |
| 599 | 0, |
| 600 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 601 | sslerror_type_slots |
| 602 | }; |
| 603 | |
| 604 | static void |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 605 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 606 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 607 | { |
| 608 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 609 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 610 | PyObject *init_value, *msg, *key; |
| 611 | _Py_IDENTIFIER(reason); |
| 612 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 613 | _Py_IDENTIFIER(verify_message); |
| 614 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 615 | |
| 616 | if (errcode != 0) { |
| 617 | int lib, reason; |
| 618 | |
| 619 | lib = ERR_GET_LIB(errcode); |
| 620 | reason = ERR_GET_REASON(errcode); |
| 621 | key = Py_BuildValue("ii", lib, reason); |
| 622 | if (key == NULL) |
| 623 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 624 | reason_obj = PyDict_GetItemWithError(err_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 625 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 626 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 627 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 628 | } |
| 629 | key = PyLong_FromLong(lib); |
| 630 | if (key == NULL) |
| 631 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 632 | lib_obj = PyDict_GetItemWithError(lib_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 633 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 634 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 635 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 636 | } |
| 637 | if (errstr == NULL) |
| 638 | errstr = ERR_reason_error_string(errcode); |
| 639 | } |
| 640 | if (errstr == NULL) |
| 641 | errstr = "unknown error"; |
| 642 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 643 | /* verify code for cert validation error */ |
| 644 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 645 | const char *verify_str = NULL; |
| 646 | long verify_code; |
| 647 | |
| 648 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 649 | verify_code_obj = PyLong_FromLong(verify_code); |
| 650 | if (verify_code_obj == NULL) { |
| 651 | goto fail; |
| 652 | } |
| 653 | |
| 654 | switch (verify_code) { |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 655 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 656 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 657 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 658 | verify_obj = PyUnicode_FromFormat( |
| 659 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 660 | sslsock->server_hostname |
| 661 | ); |
| 662 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 663 | #endif |
| 664 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 665 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 666 | verify_obj = PyUnicode_FromFormat( |
| 667 | "IP address mismatch, certificate is not valid for '%S'.", |
| 668 | sslsock->server_hostname |
| 669 | ); |
| 670 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 671 | #endif |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 672 | default: |
| 673 | verify_str = X509_verify_cert_error_string(verify_code); |
| 674 | if (verify_str != NULL) { |
| 675 | verify_obj = PyUnicode_FromString(verify_str); |
| 676 | } else { |
| 677 | verify_obj = Py_None; |
| 678 | Py_INCREF(verify_obj); |
| 679 | } |
| 680 | break; |
| 681 | } |
| 682 | if (verify_obj == NULL) { |
| 683 | goto fail; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (verify_obj && reason_obj && lib_obj) |
| 688 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 689 | lib_obj, reason_obj, errstr, verify_obj, |
| 690 | lineno); |
| 691 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 692 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 693 | lib_obj, reason_obj, errstr, lineno); |
| 694 | else if (lib_obj) |
| 695 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 696 | lib_obj, errstr, lineno); |
| 697 | else |
| 698 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 699 | if (msg == NULL) |
| 700 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 701 | |
Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 702 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 703 | if (init_value == NULL) |
| 704 | goto fail; |
| 705 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 706 | err_value = PyObject_CallObject(type, init_value); |
| 707 | Py_DECREF(init_value); |
| 708 | if (err_value == NULL) |
| 709 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 710 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 711 | if (reason_obj == NULL) |
| 712 | reason_obj = Py_None; |
| 713 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 714 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 715 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 716 | if (lib_obj == NULL) |
| 717 | lib_obj = Py_None; |
| 718 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 719 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 720 | |
| 721 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 722 | /* Only set verify code / message for SSLCertVerificationError */ |
| 723 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 724 | verify_code_obj)) |
| 725 | goto fail; |
| 726 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 727 | goto fail; |
| 728 | } |
| 729 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 730 | PyErr_SetObject(type, err_value); |
| 731 | fail: |
| 732 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 733 | Py_XDECREF(verify_code_obj); |
| 734 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 735 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 736 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 737 | static int |
| 738 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 739 | if (sslsock->exc_type == NULL) |
| 740 | return 0; |
| 741 | |
| 742 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 743 | sslsock->exc_type = NULL; |
| 744 | sslsock->exc_value = NULL; |
| 745 | sslsock->exc_tb = NULL; |
| 746 | return -1; |
| 747 | } |
| 748 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 749 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 750 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 751 | { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 752 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 753 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 754 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 755 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 756 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 758 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 759 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 760 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 761 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 762 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 763 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 764 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 765 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 766 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 767 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 768 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 769 | break; |
| 770 | case SSL_ERROR_WANT_READ: |
| 771 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 772 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 773 | p = PY_SSL_ERROR_WANT_READ; |
| 774 | break; |
| 775 | case SSL_ERROR_WANT_WRITE: |
| 776 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 777 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 778 | errstr = "The operation did not complete (write)"; |
| 779 | break; |
| 780 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 781 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 782 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 783 | break; |
| 784 | case SSL_ERROR_WANT_CONNECT: |
| 785 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 786 | errstr = "The operation did not complete (connect)"; |
| 787 | break; |
| 788 | case SSL_ERROR_SYSCALL: |
| 789 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 790 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 791 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 792 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 793 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 794 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 795 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 796 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 797 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 798 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 799 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 800 | if (err.ws) { |
| 801 | return PyErr_SetFromWindowsErr(err.ws); |
| 802 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 803 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 804 | if (err.c) { |
| 805 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 806 | return PyErr_SetFromErrno(PyExc_OSError); |
| 807 | } |
| 808 | Py_INCREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 809 | s->errorhandler(); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 810 | Py_DECREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 811 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 812 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 813 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 814 | type = PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 815 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 816 | } |
| 817 | } else { |
| 818 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | } |
| 822 | case SSL_ERROR_SSL: |
| 823 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 824 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 825 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 826 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 827 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 828 | } |
| 829 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 830 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
| 831 | type = PySSLCertVerificationErrorObject; |
| 832 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 833 | break; |
| 834 | } |
| 835 | default: |
| 836 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 837 | errstr = "Invalid error code"; |
| 838 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 839 | } |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 840 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 841 | ERR_clear_error(); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 842 | PySSL_ChainExceptions(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 843 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 846 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 847 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 849 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 850 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 851 | else |
| 852 | errcode = 0; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 853 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 854 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 855 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 858 | /* |
| 859 | * SSL objects |
| 860 | */ |
| 861 | |
| 862 | static int |
| 863 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 864 | { |
| 865 | int retval = -1; |
| 866 | ASN1_OCTET_STRING *ip; |
| 867 | PyObject *hostname; |
| 868 | size_t len; |
| 869 | |
| 870 | assert(server_hostname); |
| 871 | |
| 872 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 873 | * When name starts with a dot (e.g ".example.com"), it will be |
| 874 | * matched by a certificate valid for any sub-domain of name. |
| 875 | */ |
| 876 | len = strlen(server_hostname); |
| 877 | if (len == 0 || *server_hostname == '.') { |
| 878 | PyErr_SetString( |
| 879 | PyExc_ValueError, |
| 880 | "server_hostname cannot be an empty string or start with a " |
| 881 | "leading dot."); |
| 882 | return retval; |
| 883 | } |
| 884 | |
| 885 | /* inet_pton is not available on all platforms. */ |
| 886 | ip = a2i_IPADDRESS(server_hostname); |
| 887 | if (ip == NULL) { |
| 888 | ERR_clear_error(); |
| 889 | } |
| 890 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 891 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 892 | if (hostname == NULL) { |
| 893 | goto error; |
| 894 | } |
| 895 | self->server_hostname = hostname; |
| 896 | |
| 897 | /* Only send SNI extension for non-IP hostnames */ |
| 898 | if (ip == NULL) { |
| 899 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
| 900 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 901 | } |
| 902 | } |
| 903 | if (self->ctx->check_hostname) { |
| 904 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 905 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 906 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 907 | strlen(server_hostname))) { |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 908 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 909 | goto error; |
| 910 | } |
| 911 | } else { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 912 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip), |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 913 | ASN1_STRING_length(ip))) { |
| 914 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 915 | goto error; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | retval = 0; |
| 920 | error: |
| 921 | if (ip != NULL) { |
| 922 | ASN1_OCTET_STRING_free(ip); |
| 923 | } |
| 924 | return retval; |
| 925 | } |
| 926 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 927 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 928 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 929 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 930 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 931 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 932 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 933 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 934 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 935 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 936 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 938 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 939 | if (self == NULL) |
| 940 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 942 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 943 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 944 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 945 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 946 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 947 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 948 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 949 | self->err = err; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 950 | self->exc_type = NULL; |
| 951 | self->exc_value = NULL; |
| 952 | self->exc_tb = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 953 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 954 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 955 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 957 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 958 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 959 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 960 | if (self->ssl == NULL) { |
| 961 | Py_DECREF(self); |
| 962 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 963 | return NULL; |
| 964 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 965 | SSL_set_app_data(self->ssl, self); |
| 966 | if (sock) { |
| 967 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 968 | } else { |
| 969 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 970 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 971 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 972 | BIO_up_ref(inbio->bio); |
| 973 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 974 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 975 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 976 | SSL_set_mode(self->ssl, |
| 977 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 978 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 979 | #ifdef TLS1_3_VERSION |
| 980 | if (sslctx->post_handshake_auth == 1) { |
| 981 | if (socket_type == PY_SSL_SERVER) { |
| 982 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 983 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 984 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 985 | int mode = SSL_get_verify_mode(self->ssl); |
| 986 | if (mode & SSL_VERIFY_PEER) { |
| 987 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 988 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 989 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 990 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 991 | } |
| 992 | } else { |
| 993 | /* client socket */ |
| 994 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 995 | } |
| 996 | } |
| 997 | #endif |
| 998 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 999 | if (server_hostname != NULL) { |
| 1000 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 1001 | Py_DECREF(self); |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1005 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 1006 | * to non-blocking mode (blocking is the default) |
| 1007 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1008 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1009 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 1010 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 1011 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1013 | PySSL_BEGIN_ALLOW_THREADS |
| 1014 | if (socket_type == PY_SSL_CLIENT) |
| 1015 | SSL_set_connect_state(self->ssl); |
| 1016 | else |
| 1017 | SSL_set_accept_state(self->ssl); |
| 1018 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1020 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1021 | if (sock != NULL) { |
| 1022 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 1023 | if (self->Socket == NULL) { |
| 1024 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1025 | return NULL; |
| 1026 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 1027 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1028 | if (owner && owner != Py_None) { |
| 1029 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 1030 | Py_DECREF(self); |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | } |
| 1034 | if (session && session != Py_None) { |
| 1035 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 1036 | Py_DECREF(self); |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1040 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1043 | /* SSL object methods */ |
| 1044 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1045 | /*[clinic input] |
| 1046 | _ssl._SSLSocket.do_handshake |
| 1047 | [clinic start generated code]*/ |
| 1048 | |
| 1049 | static PyObject * |
| 1050 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 1051 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1052 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1053 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1054 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1055 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1056 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1057 | _PyTime_t timeout, deadline = 0; |
| 1058 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1060 | if (sock) { |
| 1061 | if (((PyObject*)sock) == Py_None) { |
| 1062 | _setSSLError("Underlying socket connection gone", |
| 1063 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | Py_INCREF(sock); |
| 1067 | |
| 1068 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1069 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1070 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1071 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1072 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1073 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1074 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 1075 | has_timeout = (timeout > 0); |
| 1076 | if (has_timeout) |
| 1077 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 1078 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1079 | /* Actually negotiate SSL connection */ |
| 1080 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1081 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1082 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1083 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1084 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1085 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1086 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1087 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1088 | if (PyErr_CheckSignals()) |
| 1089 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1090 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1091 | if (has_timeout) |
| 1092 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 1093 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1094 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1095 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1096 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1097 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | sockstate = SOCKET_OPERATION_OK; |
| 1100 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1101 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1102 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1103 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1104 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1105 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1106 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1107 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1108 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1109 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1110 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1111 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1112 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1113 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1114 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1115 | break; |
| 1116 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1117 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 1118 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1119 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1120 | if (ret < 1) |
| 1121 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1122 | if (PySSL_ChainExceptions(self) < 0) |
| 1123 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1124 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1125 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1126 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1127 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1128 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1131 | static PyObject * |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1132 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 1133 | { |
| 1134 | char buf[X509_NAME_MAXLEN]; |
| 1135 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1136 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1137 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1138 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1139 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1140 | if (buflen < 0) { |
| 1141 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1142 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1143 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1144 | /* initial buffer is too small for oid + terminating null byte */ |
| 1145 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1146 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1147 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1148 | /* allocate len + 1 for terminating NULL byte */ |
| 1149 | namebuf = PyMem_Malloc(buflen + 1); |
| 1150 | if (namebuf == NULL) { |
| 1151 | PyErr_NoMemory(); |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1155 | if (buflen < 0) { |
| 1156 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1157 | goto done; |
| 1158 | } |
| 1159 | } |
| 1160 | if (!buflen && no_name) { |
| 1161 | Py_INCREF(Py_None); |
| 1162 | name_obj = Py_None; |
| 1163 | } |
| 1164 | else { |
| 1165 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1166 | } |
| 1167 | |
| 1168 | done: |
| 1169 | if (buf != namebuf) { |
| 1170 | PyMem_Free(namebuf); |
| 1171 | } |
| 1172 | return name_obj; |
| 1173 | } |
| 1174 | |
| 1175 | static PyObject * |
| 1176 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 1177 | { |
| 1178 | Py_ssize_t buflen; |
| 1179 | unsigned char *valuebuf = NULL; |
| 1180 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1181 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1182 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1183 | if (buflen < 0) { |
| 1184 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1185 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1186 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1187 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1188 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1189 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1193 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1194 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1195 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1196 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1197 | PyObject *rdnt; |
| 1198 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1199 | int entry_count = X509_NAME_entry_count(xname); |
| 1200 | X509_NAME_ENTRY *entry; |
| 1201 | ASN1_OBJECT *name; |
| 1202 | ASN1_STRING *value; |
| 1203 | int index_counter; |
| 1204 | int rdn_level = -1; |
| 1205 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1207 | dn = PyList_New(0); |
| 1208 | if (dn == NULL) |
| 1209 | return NULL; |
| 1210 | /* now create another tuple to hold the top-level RDN */ |
| 1211 | rdn = PyList_New(0); |
| 1212 | if (rdn == NULL) |
| 1213 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1215 | for (index_counter = 0; |
| 1216 | index_counter < entry_count; |
| 1217 | index_counter++) |
| 1218 | { |
| 1219 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1221 | /* check to see if we've gotten to a new RDN */ |
| 1222 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1223 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1224 | /* yes, new RDN */ |
| 1225 | /* add old RDN to DN */ |
| 1226 | rdnt = PyList_AsTuple(rdn); |
| 1227 | Py_DECREF(rdn); |
| 1228 | if (rdnt == NULL) |
| 1229 | goto fail0; |
| 1230 | retcode = PyList_Append(dn, rdnt); |
| 1231 | Py_DECREF(rdnt); |
| 1232 | if (retcode < 0) |
| 1233 | goto fail0; |
| 1234 | /* create new RDN */ |
| 1235 | rdn = PyList_New(0); |
| 1236 | if (rdn == NULL) |
| 1237 | goto fail0; |
| 1238 | } |
| 1239 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1240 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1242 | /* now add this attribute to the current RDN */ |
| 1243 | name = X509_NAME_ENTRY_get_object(entry); |
| 1244 | value = X509_NAME_ENTRY_get_data(entry); |
| 1245 | attr = _create_tuple_for_attribute(name, value); |
| 1246 | /* |
| 1247 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1248 | entry->set, |
| 1249 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1250 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1251 | */ |
| 1252 | if (attr == NULL) |
| 1253 | goto fail1; |
| 1254 | retcode = PyList_Append(rdn, attr); |
| 1255 | Py_DECREF(attr); |
| 1256 | if (retcode < 0) |
| 1257 | goto fail1; |
| 1258 | } |
| 1259 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1260 | if (rdn != NULL) { |
| 1261 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1262 | rdnt = PyList_AsTuple(rdn); |
| 1263 | Py_DECREF(rdn); |
| 1264 | if (rdnt == NULL) |
| 1265 | goto fail0; |
| 1266 | retcode = PyList_Append(dn, rdnt); |
| 1267 | Py_DECREF(rdnt); |
| 1268 | if (retcode < 0) |
| 1269 | goto fail0; |
| 1270 | } |
| 1271 | else { |
| 1272 | Py_DECREF(rdn); |
| 1273 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1274 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1275 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1276 | /* convert list to tuple */ |
| 1277 | rdnt = PyList_AsTuple(dn); |
| 1278 | Py_DECREF(dn); |
| 1279 | if (rdnt == NULL) |
| 1280 | return NULL; |
| 1281 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1282 | |
| 1283 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1284 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1285 | |
| 1286 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1287 | Py_XDECREF(dn); |
| 1288 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | static PyObject * |
| 1292 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1293 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1294 | /* this code follows the procedure outlined in |
| 1295 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1296 | function to extract the STACK_OF(GENERAL_NAME), |
| 1297 | then iterates through the stack to add the |
| 1298 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1299 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1300 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1301 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1302 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1303 | GENERAL_NAMES *names = NULL; |
| 1304 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1305 | BIO *biobuf = NULL; |
| 1306 | char buf[2048]; |
| 1307 | char *vptr; |
| 1308 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1309 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1310 | if (certificate == NULL) |
| 1311 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1313 | /* get a memory buffer */ |
| 1314 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1315 | if (biobuf == NULL) { |
| 1316 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1317 | return NULL; |
| 1318 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1319 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1320 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1321 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1322 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1323 | if (peer_alt_names == Py_None) { |
| 1324 | peer_alt_names = PyList_New(0); |
| 1325 | if (peer_alt_names == NULL) |
| 1326 | goto fail; |
| 1327 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1329 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1330 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1331 | int gntype; |
| 1332 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1333 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1334 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1335 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1336 | switch (gntype) { |
| 1337 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1338 | /* we special-case DirName as a tuple of |
| 1339 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1341 | t = PyTuple_New(2); |
| 1342 | if (t == NULL) { |
| 1343 | goto fail; |
| 1344 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1346 | v = PyUnicode_FromString("DirName"); |
| 1347 | if (v == NULL) { |
| 1348 | Py_DECREF(t); |
| 1349 | goto fail; |
| 1350 | } |
| 1351 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1353 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1354 | if (v == NULL) { |
| 1355 | Py_DECREF(t); |
| 1356 | goto fail; |
| 1357 | } |
| 1358 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1359 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1360 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1361 | case GEN_EMAIL: |
| 1362 | case GEN_DNS: |
| 1363 | case GEN_URI: |
| 1364 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1365 | correctly, CVE-2013-4238 */ |
| 1366 | t = PyTuple_New(2); |
| 1367 | if (t == NULL) |
| 1368 | goto fail; |
| 1369 | switch (gntype) { |
| 1370 | case GEN_EMAIL: |
| 1371 | v = PyUnicode_FromString("email"); |
| 1372 | as = name->d.rfc822Name; |
| 1373 | break; |
| 1374 | case GEN_DNS: |
| 1375 | v = PyUnicode_FromString("DNS"); |
| 1376 | as = name->d.dNSName; |
| 1377 | break; |
| 1378 | case GEN_URI: |
| 1379 | v = PyUnicode_FromString("URI"); |
| 1380 | as = name->d.uniformResourceIdentifier; |
| 1381 | break; |
| 1382 | } |
| 1383 | if (v == NULL) { |
| 1384 | Py_DECREF(t); |
| 1385 | goto fail; |
| 1386 | } |
| 1387 | PyTuple_SET_ITEM(t, 0, v); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1388 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as), |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1389 | ASN1_STRING_length(as)); |
| 1390 | if (v == NULL) { |
| 1391 | Py_DECREF(t); |
| 1392 | goto fail; |
| 1393 | } |
| 1394 | PyTuple_SET_ITEM(t, 1, v); |
| 1395 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1396 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1397 | case GEN_RID: |
| 1398 | t = PyTuple_New(2); |
| 1399 | if (t == NULL) |
| 1400 | goto fail; |
| 1401 | |
| 1402 | v = PyUnicode_FromString("Registered ID"); |
| 1403 | if (v == NULL) { |
| 1404 | Py_DECREF(t); |
| 1405 | goto fail; |
| 1406 | } |
| 1407 | PyTuple_SET_ITEM(t, 0, v); |
| 1408 | |
| 1409 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1410 | if (len < 0) { |
| 1411 | Py_DECREF(t); |
| 1412 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1413 | goto fail; |
| 1414 | } else if (len >= (int)sizeof(buf)) { |
| 1415 | v = PyUnicode_FromString("<INVALID>"); |
| 1416 | } else { |
| 1417 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1418 | } |
| 1419 | if (v == NULL) { |
| 1420 | Py_DECREF(t); |
| 1421 | goto fail; |
| 1422 | } |
| 1423 | PyTuple_SET_ITEM(t, 1, v); |
| 1424 | break; |
| 1425 | |
Christian Heimes | 2b7de66 | 2019-12-07 17:59:36 +0100 | [diff] [blame] | 1426 | case GEN_IPADD: |
| 1427 | /* OpenSSL < 3.0.0 adds a trailing \n to IPv6. 3.0.0 removed |
| 1428 | * the trailing newline. Remove it in all versions |
| 1429 | */ |
| 1430 | t = PyTuple_New(2); |
| 1431 | if (t == NULL) |
| 1432 | goto fail; |
| 1433 | |
| 1434 | v = PyUnicode_FromString("IP Address"); |
| 1435 | if (v == NULL) { |
| 1436 | Py_DECREF(t); |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | PyTuple_SET_ITEM(t, 0, v); |
| 1440 | |
| 1441 | if (name->d.ip->length == 4) { |
| 1442 | unsigned char *p = name->d.ip->data; |
| 1443 | v = PyUnicode_FromFormat( |
| 1444 | "%d.%d.%d.%d", |
| 1445 | p[0], p[1], p[2], p[3] |
| 1446 | ); |
| 1447 | } else if (name->d.ip->length == 16) { |
| 1448 | /* PyUnicode_FromFormat() does not support %X */ |
| 1449 | unsigned char *p = name->d.ip->data; |
| 1450 | len = sprintf( |
| 1451 | buf, |
| 1452 | "%X:%X:%X:%X:%X:%X:%X:%X", |
| 1453 | p[0] << 8 | p[1], |
| 1454 | p[2] << 8 | p[3], |
| 1455 | p[4] << 8 | p[5], |
| 1456 | p[6] << 8 | p[7], |
| 1457 | p[8] << 8 | p[9], |
| 1458 | p[10] << 8 | p[11], |
| 1459 | p[12] << 8 | p[13], |
| 1460 | p[14] << 8 | p[15] |
| 1461 | ); |
| 1462 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1463 | } else { |
| 1464 | v = PyUnicode_FromString("<invalid>"); |
| 1465 | } |
| 1466 | |
| 1467 | if (v == NULL) { |
| 1468 | Py_DECREF(t); |
| 1469 | goto fail; |
| 1470 | } |
| 1471 | PyTuple_SET_ITEM(t, 1, v); |
| 1472 | break; |
| 1473 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1474 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1475 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1476 | switch (gntype) { |
| 1477 | /* check for new general name type */ |
| 1478 | case GEN_OTHERNAME: |
| 1479 | case GEN_X400: |
| 1480 | case GEN_EDIPARTY: |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1481 | case GEN_RID: |
| 1482 | break; |
| 1483 | default: |
| 1484 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1485 | "Unknown general name type %d", |
| 1486 | gntype) == -1) { |
| 1487 | goto fail; |
| 1488 | } |
| 1489 | break; |
| 1490 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1491 | (void) BIO_reset(biobuf); |
| 1492 | GENERAL_NAME_print(biobuf, name); |
| 1493 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1494 | if (len < 0) { |
| 1495 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1496 | goto fail; |
| 1497 | } |
| 1498 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1499 | if (vptr == NULL) { |
| 1500 | PyErr_Format(PyExc_ValueError, |
| 1501 | "Invalid value %.200s", |
| 1502 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1503 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1504 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1505 | t = PyTuple_New(2); |
| 1506 | if (t == NULL) |
| 1507 | goto fail; |
| 1508 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1509 | if (v == NULL) { |
| 1510 | Py_DECREF(t); |
| 1511 | goto fail; |
| 1512 | } |
| 1513 | PyTuple_SET_ITEM(t, 0, v); |
| 1514 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1515 | (len - (vptr - buf + 1))); |
| 1516 | if (v == NULL) { |
| 1517 | Py_DECREF(t); |
| 1518 | goto fail; |
| 1519 | } |
| 1520 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1521 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1522 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1524 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1526 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1527 | Py_DECREF(t); |
| 1528 | goto fail; |
| 1529 | } |
| 1530 | Py_DECREF(t); |
| 1531 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1532 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1533 | } |
| 1534 | BIO_free(biobuf); |
| 1535 | if (peer_alt_names != Py_None) { |
| 1536 | v = PyList_AsTuple(peer_alt_names); |
| 1537 | Py_DECREF(peer_alt_names); |
| 1538 | return v; |
| 1539 | } else { |
| 1540 | return peer_alt_names; |
| 1541 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1542 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1543 | |
| 1544 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1545 | if (biobuf != NULL) |
| 1546 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1548 | if (peer_alt_names != Py_None) { |
| 1549 | Py_XDECREF(peer_alt_names); |
| 1550 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1552 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1556 | _get_aia_uri(X509 *certificate, int nid) { |
| 1557 | PyObject *lst = NULL, *ostr = NULL; |
| 1558 | int i, result; |
| 1559 | AUTHORITY_INFO_ACCESS *info; |
| 1560 | |
| 1561 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1562 | if (info == NULL) |
| 1563 | return Py_None; |
| 1564 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1565 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1566 | return Py_None; |
| 1567 | } |
| 1568 | |
| 1569 | if ((lst = PyList_New(0)) == NULL) { |
| 1570 | goto fail; |
| 1571 | } |
| 1572 | |
| 1573 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1574 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1575 | ASN1_IA5STRING *uri; |
| 1576 | |
| 1577 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1578 | (ad->location->type != GEN_URI)) { |
| 1579 | continue; |
| 1580 | } |
| 1581 | uri = ad->location->d.uniformResourceIdentifier; |
| 1582 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1583 | uri->length); |
| 1584 | if (ostr == NULL) { |
| 1585 | goto fail; |
| 1586 | } |
| 1587 | result = PyList_Append(lst, ostr); |
| 1588 | Py_DECREF(ostr); |
| 1589 | if (result < 0) { |
| 1590 | goto fail; |
| 1591 | } |
| 1592 | } |
| 1593 | AUTHORITY_INFO_ACCESS_free(info); |
| 1594 | |
| 1595 | /* convert to tuple or None */ |
| 1596 | if (PyList_Size(lst) == 0) { |
| 1597 | Py_DECREF(lst); |
| 1598 | return Py_None; |
| 1599 | } else { |
| 1600 | PyObject *tup; |
| 1601 | tup = PyList_AsTuple(lst); |
| 1602 | Py_DECREF(lst); |
| 1603 | return tup; |
| 1604 | } |
| 1605 | |
| 1606 | fail: |
| 1607 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1608 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1609 | return NULL; |
| 1610 | } |
| 1611 | |
| 1612 | static PyObject * |
| 1613 | _get_crl_dp(X509 *certificate) { |
| 1614 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1615 | int i, j; |
| 1616 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1617 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1618 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1619 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1620 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1621 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1622 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1623 | lst = PyList_New(0); |
| 1624 | if (lst == NULL) |
| 1625 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1626 | |
| 1627 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1628 | DIST_POINT *dp; |
| 1629 | STACK_OF(GENERAL_NAME) *gns; |
| 1630 | |
| 1631 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1632 | if (dp->distpoint == NULL) { |
| 1633 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1634 | continue; |
| 1635 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1636 | gns = dp->distpoint->name.fullname; |
| 1637 | |
| 1638 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1639 | GENERAL_NAME *gn; |
| 1640 | ASN1_IA5STRING *uri; |
| 1641 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1642 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1643 | |
| 1644 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1645 | if (gn->type != GEN_URI) { |
| 1646 | continue; |
| 1647 | } |
| 1648 | uri = gn->d.uniformResourceIdentifier; |
| 1649 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1650 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1651 | if (ouri == NULL) |
| 1652 | goto done; |
| 1653 | |
| 1654 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1655 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1656 | if (err < 0) |
| 1657 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1658 | } |
| 1659 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1660 | |
| 1661 | /* Convert to tuple. */ |
| 1662 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1663 | |
| 1664 | done: |
| 1665 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1666 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1667 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1671 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1673 | PyObject *retval = NULL; |
| 1674 | BIO *biobuf = NULL; |
| 1675 | PyObject *peer; |
| 1676 | PyObject *peer_alt_names = NULL; |
| 1677 | PyObject *issuer; |
| 1678 | PyObject *version; |
| 1679 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1680 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1681 | ASN1_INTEGER *serialNumber; |
| 1682 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1683 | int len, result; |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1684 | const ASN1_TIME *notBefore, *notAfter; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1685 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1687 | retval = PyDict_New(); |
| 1688 | if (retval == NULL) |
| 1689 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1690 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1691 | peer = _create_tuple_for_X509_NAME( |
| 1692 | X509_get_subject_name(certificate)); |
| 1693 | if (peer == NULL) |
| 1694 | goto fail0; |
| 1695 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1696 | Py_DECREF(peer); |
| 1697 | goto fail0; |
| 1698 | } |
| 1699 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1700 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1701 | issuer = _create_tuple_for_X509_NAME( |
| 1702 | X509_get_issuer_name(certificate)); |
| 1703 | if (issuer == NULL) |
| 1704 | goto fail0; |
| 1705 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1706 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1707 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1708 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1709 | Py_DECREF(issuer); |
| 1710 | |
| 1711 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1712 | if (version == NULL) |
| 1713 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1714 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1715 | Py_DECREF(version); |
| 1716 | goto fail0; |
| 1717 | } |
| 1718 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1720 | /* get a memory buffer */ |
| 1721 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1722 | if (biobuf == NULL) { |
| 1723 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1724 | goto fail0; |
| 1725 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1726 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1727 | (void) BIO_reset(biobuf); |
| 1728 | serialNumber = X509_get_serialNumber(certificate); |
| 1729 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1730 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1731 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1732 | if (len < 0) { |
| 1733 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1734 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1735 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1736 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1737 | if (sn_obj == NULL) |
| 1738 | goto fail1; |
| 1739 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1740 | Py_DECREF(sn_obj); |
| 1741 | goto fail1; |
| 1742 | } |
| 1743 | Py_DECREF(sn_obj); |
| 1744 | |
| 1745 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1746 | notBefore = X509_get0_notBefore(certificate); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1747 | ASN1_TIME_print(biobuf, notBefore); |
| 1748 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1749 | if (len < 0) { |
| 1750 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1751 | goto fail1; |
| 1752 | } |
| 1753 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1754 | if (pnotBefore == NULL) |
| 1755 | goto fail1; |
| 1756 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1757 | Py_DECREF(pnotBefore); |
| 1758 | goto fail1; |
| 1759 | } |
| 1760 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1762 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1763 | notAfter = X509_get0_notAfter(certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1764 | ASN1_TIME_print(biobuf, notAfter); |
| 1765 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1766 | if (len < 0) { |
| 1767 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1768 | goto fail1; |
| 1769 | } |
| 1770 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1771 | if (pnotAfter == NULL) |
| 1772 | goto fail1; |
| 1773 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1774 | Py_DECREF(pnotAfter); |
| 1775 | goto fail1; |
| 1776 | } |
| 1777 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1778 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1779 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1781 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1782 | if (peer_alt_names == NULL) |
| 1783 | goto fail1; |
| 1784 | else if (peer_alt_names != Py_None) { |
| 1785 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1786 | peer_alt_names) < 0) { |
| 1787 | Py_DECREF(peer_alt_names); |
| 1788 | goto fail1; |
| 1789 | } |
| 1790 | Py_DECREF(peer_alt_names); |
| 1791 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1792 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1793 | /* Authority Information Access: OCSP URIs */ |
| 1794 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1795 | if (obj == NULL) { |
| 1796 | goto fail1; |
| 1797 | } else if (obj != Py_None) { |
| 1798 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1799 | Py_DECREF(obj); |
| 1800 | if (result < 0) { |
| 1801 | goto fail1; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1806 | if (obj == NULL) { |
| 1807 | goto fail1; |
| 1808 | } else if (obj != Py_None) { |
| 1809 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1810 | Py_DECREF(obj); |
| 1811 | if (result < 0) { |
| 1812 | goto fail1; |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | /* CDP (CRL distribution points) */ |
| 1817 | obj = _get_crl_dp(certificate); |
| 1818 | if (obj == NULL) { |
| 1819 | goto fail1; |
| 1820 | } else if (obj != Py_None) { |
| 1821 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1822 | Py_DECREF(obj); |
| 1823 | if (result < 0) { |
| 1824 | goto fail1; |
| 1825 | } |
| 1826 | } |
| 1827 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1828 | BIO_free(biobuf); |
| 1829 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1830 | |
| 1831 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1832 | if (biobuf != NULL) |
| 1833 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1834 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1835 | Py_XDECREF(retval); |
| 1836 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1837 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1838 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1839 | static PyObject * |
| 1840 | _certificate_to_der(X509 *certificate) |
| 1841 | { |
| 1842 | unsigned char *bytes_buf = NULL; |
| 1843 | int len; |
| 1844 | PyObject *retval; |
| 1845 | |
| 1846 | bytes_buf = NULL; |
| 1847 | len = i2d_X509(certificate, &bytes_buf); |
| 1848 | if (len < 0) { |
| 1849 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1850 | return NULL; |
| 1851 | } |
| 1852 | /* this is actually an immutable bytes sequence */ |
| 1853 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1854 | OPENSSL_free(bytes_buf); |
| 1855 | return retval; |
| 1856 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1857 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1858 | /*[clinic input] |
| 1859 | _ssl._test_decode_cert |
| 1860 | path: object(converter="PyUnicode_FSConverter") |
| 1861 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1862 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1863 | [clinic start generated code]*/ |
| 1864 | |
| 1865 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1866 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1867 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1868 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1869 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1870 | X509 *x=NULL; |
| 1871 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1873 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1874 | PyErr_SetString(PySSLErrorObject, |
| 1875 | "Can't malloc memory to read file"); |
| 1876 | goto fail0; |
| 1877 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1878 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1879 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1880 | PyErr_SetString(PySSLErrorObject, |
| 1881 | "Can't open file"); |
| 1882 | goto fail0; |
| 1883 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1884 | |
Alex Gaynor | 40dad95 | 2019-08-15 08:31:28 -0400 | [diff] [blame] | 1885 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1886 | if (x == NULL) { |
| 1887 | PyErr_SetString(PySSLErrorObject, |
| 1888 | "Error decoding PEM-encoded file"); |
| 1889 | goto fail0; |
| 1890 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1892 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1893 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1894 | |
| 1895 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1896 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1897 | if (cert != NULL) BIO_free(cert); |
| 1898 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1902 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1903 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1904 | der as binary_mode: bool = False |
| 1905 | / |
| 1906 | |
| 1907 | Returns the certificate for the peer. |
| 1908 | |
| 1909 | If no certificate was provided, returns None. If a certificate was |
| 1910 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1911 | returns a dict containing information about the peer certificate. |
| 1912 | |
| 1913 | If the optional argument is True, returns a DER-encoded copy of the |
| 1914 | peer certificate, or None if no certificate was provided. This will |
| 1915 | return the certificate even if it wasn't validated. |
| 1916 | [clinic start generated code]*/ |
| 1917 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1918 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1919 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1920 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1921 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1922 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1923 | X509 *peer_cert; |
| 1924 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1925 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1926 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1927 | PyErr_SetString(PyExc_ValueError, |
| 1928 | "handshake not done yet"); |
| 1929 | return NULL; |
| 1930 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1931 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1932 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1933 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1934 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1935 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1936 | /* return cert in DER-encoded format */ |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1937 | result = _certificate_to_der(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1938 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1939 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1940 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1941 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1942 | else |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1943 | result = _decode_certificate(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1944 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1945 | X509_free(peer_cert); |
| 1946 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1949 | static PyObject * |
| 1950 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1951 | { |
| 1952 | const char *cipher_name, *cipher_protocol; |
| 1953 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1954 | if (retval == NULL) |
| 1955 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1956 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1957 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1958 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1959 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1960 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1961 | } else { |
| 1962 | v = PyUnicode_FromString(cipher_name); |
| 1963 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1964 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1965 | PyTuple_SET_ITEM(retval, 0, v); |
| 1966 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1967 | |
| 1968 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1969 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1970 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1971 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1972 | } else { |
| 1973 | v = PyUnicode_FromString(cipher_protocol); |
| 1974 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1975 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1976 | PyTuple_SET_ITEM(retval, 1, v); |
| 1977 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1978 | |
| 1979 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1980 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1981 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1982 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1983 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1984 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1985 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1986 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1987 | Py_DECREF(retval); |
| 1988 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1991 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1992 | static PyObject * |
| 1993 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1994 | { |
| 1995 | const char *cipher_name, *cipher_protocol; |
| 1996 | |
| 1997 | unsigned long cipher_id; |
| 1998 | int alg_bits, strength_bits, len; |
| 1999 | char buf[512] = {0}; |
| 2000 | #if OPENSSL_VERSION_1_1 |
| 2001 | int aead, nid; |
| 2002 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 2003 | #endif |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2004 | |
| 2005 | /* can be NULL */ |
| 2006 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 2007 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 2008 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 2009 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 2010 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 2011 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2012 | if (len > 1 && buf[len-1] == '\n') |
| 2013 | buf[len-1] = '\0'; |
| 2014 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 2015 | |
| 2016 | #if OPENSSL_VERSION_1_1 |
| 2017 | aead = SSL_CIPHER_is_aead(cipher); |
| 2018 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 2019 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2020 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 2021 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2022 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 2023 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2024 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 2025 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2026 | #endif |
| 2027 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 2028 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2029 | "{sksssssssisi" |
| 2030 | #if OPENSSL_VERSION_1_1 |
| 2031 | "sOssssssss" |
| 2032 | #endif |
| 2033 | "}", |
| 2034 | "id", cipher_id, |
| 2035 | "name", cipher_name, |
| 2036 | "protocol", cipher_protocol, |
| 2037 | "description", buf, |
| 2038 | "strength_bits", strength_bits, |
| 2039 | "alg_bits", alg_bits |
| 2040 | #if OPENSSL_VERSION_1_1 |
| 2041 | ,"aead", aead ? Py_True : Py_False, |
| 2042 | "symmetric", skcipher, |
| 2043 | "digest", digest, |
| 2044 | "kea", kx, |
| 2045 | "auth", auth |
| 2046 | #endif |
| 2047 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2048 | } |
| 2049 | #endif |
| 2050 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2051 | /*[clinic input] |
| 2052 | _ssl._SSLSocket.shared_ciphers |
| 2053 | [clinic start generated code]*/ |
| 2054 | |
| 2055 | static PyObject * |
| 2056 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 2057 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2058 | { |
| 2059 | STACK_OF(SSL_CIPHER) *ciphers; |
| 2060 | int i; |
| 2061 | PyObject *res; |
| 2062 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2063 | ciphers = SSL_get_ciphers(self->ssl); |
| 2064 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2065 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2066 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 2067 | if (!res) |
| 2068 | return NULL; |
| 2069 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 2070 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 2071 | if (!tup) { |
| 2072 | Py_DECREF(res); |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | PyList_SET_ITEM(res, i, tup); |
| 2076 | } |
| 2077 | return res; |
| 2078 | } |
| 2079 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2080 | /*[clinic input] |
| 2081 | _ssl._SSLSocket.cipher |
| 2082 | [clinic start generated code]*/ |
| 2083 | |
| 2084 | static PyObject * |
| 2085 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 2086 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2087 | { |
| 2088 | const SSL_CIPHER *current; |
| 2089 | |
| 2090 | if (self->ssl == NULL) |
| 2091 | Py_RETURN_NONE; |
| 2092 | current = SSL_get_current_cipher(self->ssl); |
| 2093 | if (current == NULL) |
| 2094 | Py_RETURN_NONE; |
| 2095 | return cipher_to_tuple(current); |
| 2096 | } |
| 2097 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2098 | /*[clinic input] |
| 2099 | _ssl._SSLSocket.version |
| 2100 | [clinic start generated code]*/ |
| 2101 | |
| 2102 | static PyObject * |
| 2103 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 2104 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2105 | { |
| 2106 | const char *version; |
| 2107 | |
| 2108 | if (self->ssl == NULL) |
| 2109 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 2110 | if (!SSL_is_init_finished(self->ssl)) { |
| 2111 | /* handshake not finished */ |
| 2112 | Py_RETURN_NONE; |
| 2113 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2114 | version = SSL_get_version(self->ssl); |
| 2115 | if (!strcmp(version, "unknown")) |
| 2116 | Py_RETURN_NONE; |
| 2117 | return PyUnicode_FromString(version); |
| 2118 | } |
| 2119 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2120 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2121 | /*[clinic input] |
| 2122 | _ssl._SSLSocket.selected_npn_protocol |
| 2123 | [clinic start generated code]*/ |
| 2124 | |
| 2125 | static PyObject * |
| 2126 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 2127 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 2128 | { |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2129 | const unsigned char *out; |
| 2130 | unsigned int outlen; |
| 2131 | |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2132 | SSL_get0_next_proto_negotiated(self->ssl, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2133 | &out, &outlen); |
| 2134 | |
| 2135 | if (out == NULL) |
| 2136 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2137 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 2138 | } |
| 2139 | #endif |
| 2140 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2141 | #if HAVE_ALPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2142 | /*[clinic input] |
| 2143 | _ssl._SSLSocket.selected_alpn_protocol |
| 2144 | [clinic start generated code]*/ |
| 2145 | |
| 2146 | static PyObject * |
| 2147 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2148 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2149 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2150 | const unsigned char *out; |
| 2151 | unsigned int outlen; |
| 2152 | |
| 2153 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2154 | |
| 2155 | if (out == NULL) |
| 2156 | Py_RETURN_NONE; |
| 2157 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2158 | } |
| 2159 | #endif |
| 2160 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2161 | /*[clinic input] |
| 2162 | _ssl._SSLSocket.compression |
| 2163 | [clinic start generated code]*/ |
| 2164 | |
| 2165 | static PyObject * |
| 2166 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2167 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2168 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2169 | #ifdef OPENSSL_NO_COMP |
| 2170 | Py_RETURN_NONE; |
| 2171 | #else |
| 2172 | const COMP_METHOD *comp_method; |
| 2173 | const char *short_name; |
| 2174 | |
| 2175 | if (self->ssl == NULL) |
| 2176 | Py_RETURN_NONE; |
| 2177 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2178 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2179 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2180 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2181 | if (short_name == NULL) |
| 2182 | Py_RETURN_NONE; |
| 2183 | return PyUnicode_DecodeFSDefault(short_name); |
| 2184 | #endif |
| 2185 | } |
| 2186 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2187 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2188 | Py_INCREF(self->ctx); |
| 2189 | return self->ctx; |
| 2190 | } |
| 2191 | |
| 2192 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2193 | void *closure) { |
| 2194 | |
| 2195 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2196 | #if !HAVE_SNI |
| 2197 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 2198 | "context is not supported by your OpenSSL library"); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2199 | return -1; |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2200 | #else |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2201 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2202 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2203 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2204 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2205 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2206 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2207 | return -1; |
| 2208 | } |
| 2209 | |
| 2210 | return 0; |
| 2211 | } |
| 2212 | |
| 2213 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2214 | "_setter_context(ctx)\n\ |
| 2215 | \ |
| 2216 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2217 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2218 | on the SSLContext to change the certificate information associated with the\n\ |
| 2219 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2220 | |
| 2221 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2222 | static PyObject * |
| 2223 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2224 | { |
| 2225 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2226 | } |
| 2227 | |
| 2228 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2229 | "Whether this is a server-side socket."); |
| 2230 | |
| 2231 | static PyObject * |
| 2232 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2233 | { |
| 2234 | if (self->server_hostname == NULL) |
| 2235 | Py_RETURN_NONE; |
| 2236 | Py_INCREF(self->server_hostname); |
| 2237 | return self->server_hostname; |
| 2238 | } |
| 2239 | |
| 2240 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2241 | "The currently set server hostname (for SNI)."); |
| 2242 | |
| 2243 | static PyObject * |
| 2244 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2245 | { |
| 2246 | PyObject *owner; |
| 2247 | |
| 2248 | if (self->owner == NULL) |
| 2249 | Py_RETURN_NONE; |
| 2250 | |
| 2251 | owner = PyWeakref_GetObject(self->owner); |
| 2252 | Py_INCREF(owner); |
| 2253 | return owner; |
| 2254 | } |
| 2255 | |
| 2256 | static int |
| 2257 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2258 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2259 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2260 | if (self->owner == NULL) |
| 2261 | return -1; |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2266 | "The Python-level owner of this object.\ |
| 2267 | Passed as \"self\" in servername callback."); |
| 2268 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2269 | static int |
| 2270 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2271 | { |
| 2272 | Py_VISIT(self->exc_type); |
| 2273 | Py_VISIT(self->exc_value); |
| 2274 | Py_VISIT(self->exc_tb); |
| 2275 | return 0; |
| 2276 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2277 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2278 | static int |
| 2279 | PySSL_clear(PySSLSocket *self) |
| 2280 | { |
| 2281 | Py_CLEAR(self->exc_type); |
| 2282 | Py_CLEAR(self->exc_value); |
| 2283 | Py_CLEAR(self->exc_tb); |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | static void |
| 2288 | PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2289 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2290 | if (self->ssl) |
| 2291 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2292 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2293 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2294 | Py_XDECREF(self->server_hostname); |
| 2295 | Py_XDECREF(self->owner); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2296 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2299 | /* If the socket has a timeout, do a select()/poll() on the socket. |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2300 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2301 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2302 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2304 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2305 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2306 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2307 | int rc; |
| 2308 | #ifdef HAVE_POLL |
| 2309 | struct pollfd pollfd; |
| 2310 | _PyTime_t ms; |
| 2311 | #else |
| 2312 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2313 | fd_set fds; |
| 2314 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2315 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2316 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2317 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2318 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2319 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2320 | else if (timeout < 0) { |
| 2321 | if (s->sock_timeout > 0) |
| 2322 | return SOCKET_HAS_TIMED_OUT; |
| 2323 | else |
| 2324 | return SOCKET_IS_BLOCKING; |
| 2325 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2327 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2328 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2329 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2331 | /* Prefer poll, if available, since you can poll() any fd |
| 2332 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2333 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2334 | pollfd.fd = s->sock_fd; |
| 2335 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2336 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2337 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2338 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2339 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2340 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2341 | PySSL_BEGIN_ALLOW_THREADS |
| 2342 | rc = poll(&pollfd, 1, (int)ms); |
| 2343 | PySSL_END_ALLOW_THREADS |
| 2344 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2345 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2346 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2347 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2348 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2349 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2350 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2351 | FD_ZERO(&fds); |
| 2352 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2353 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2354 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2355 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2356 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2357 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2358 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2359 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2360 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2361 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2362 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2363 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2364 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2365 | (when we are able to write or when there's something to read) */ |
| 2366 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2369 | /*[clinic input] |
| 2370 | _ssl._SSLSocket.write |
| 2371 | b: Py_buffer |
| 2372 | / |
| 2373 | |
| 2374 | Writes the bytes-like object b into the SSL object. |
| 2375 | |
| 2376 | Returns the number of bytes written. |
| 2377 | [clinic start generated code]*/ |
| 2378 | |
| 2379 | static PyObject * |
| 2380 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2381 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2382 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2383 | int len; |
| 2384 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2385 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2386 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2387 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2388 | _PyTime_t timeout, deadline = 0; |
| 2389 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2391 | if (sock != NULL) { |
| 2392 | if (((PyObject*)sock) == Py_None) { |
| 2393 | _setSSLError("Underlying socket connection gone", |
| 2394 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2400 | if (b->len > INT_MAX) { |
Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2401 | PyErr_Format(PyExc_OverflowError, |
| 2402 | "string longer than %d bytes", INT_MAX); |
| 2403 | goto error; |
| 2404 | } |
| 2405 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2406 | if (sock != NULL) { |
| 2407 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2408 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2409 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2410 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2411 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2412 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2413 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2414 | has_timeout = (timeout > 0); |
| 2415 | if (has_timeout) |
| 2416 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2417 | |
| 2418 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2419 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2420 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2421 | "The write operation timed out"); |
| 2422 | goto error; |
| 2423 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2424 | PyErr_SetString(PySSLErrorObject, |
| 2425 | "Underlying socket has been closed."); |
| 2426 | goto error; |
| 2427 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2428 | PyErr_SetString(PySSLErrorObject, |
| 2429 | "Underlying socket too large for select()."); |
| 2430 | goto error; |
| 2431 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2432 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2433 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2434 | PySSL_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2435 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2436 | err = _PySSL_errno(len <= 0, self->ssl, len); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2437 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2438 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2439 | |
| 2440 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2441 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2442 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2443 | if (has_timeout) |
| 2444 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2445 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2446 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2447 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2448 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2449 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2450 | } else { |
| 2451 | sockstate = SOCKET_OPERATION_OK; |
| 2452 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2453 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2454 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2455 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2456 | "The write operation timed out"); |
| 2457 | goto error; |
| 2458 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2459 | PyErr_SetString(PySSLErrorObject, |
| 2460 | "Underlying socket has been closed."); |
| 2461 | goto error; |
| 2462 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2463 | break; |
| 2464 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2465 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2466 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2467 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2468 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2469 | if (len <= 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2470 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2471 | if (PySSL_ChainExceptions(self) < 0) |
| 2472 | return NULL; |
| 2473 | return PyLong_FromLong(len); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2474 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2475 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2476 | PySSL_ChainExceptions(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2477 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2480 | /*[clinic input] |
| 2481 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2482 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2483 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2484 | [clinic start generated code]*/ |
| 2485 | |
| 2486 | static PyObject * |
| 2487 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2488 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2489 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2490 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2491 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2493 | PySSL_BEGIN_ALLOW_THREADS |
| 2494 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2495 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2496 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2497 | self->err = err; |
| 2498 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2499 | if (count < 0) |
| 2500 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2501 | else |
| 2502 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2505 | /*[clinic input] |
| 2506 | _ssl._SSLSocket.read |
| 2507 | size as len: int |
| 2508 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2509 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2510 | ] |
| 2511 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2512 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2513 | Read up to size bytes from the SSL socket. |
| 2514 | [clinic start generated code]*/ |
| 2515 | |
| 2516 | static PyObject * |
| 2517 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2518 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2519 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2520 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2521 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2522 | char *mem; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2523 | int count; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2524 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2525 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2526 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2527 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2528 | _PyTime_t timeout, deadline = 0; |
| 2529 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2530 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2531 | if (!group_right_1 && len < 0) { |
| 2532 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2536 | if (sock != NULL) { |
| 2537 | if (((PyObject*)sock) == Py_None) { |
| 2538 | _setSSLError("Underlying socket connection gone", |
| 2539 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2545 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2546 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2547 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2548 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2549 | if (len == 0) { |
| 2550 | Py_XDECREF(sock); |
| 2551 | return dest; |
| 2552 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2553 | mem = PyBytes_AS_STRING(dest); |
| 2554 | } |
| 2555 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2556 | mem = buffer->buf; |
| 2557 | if (len <= 0 || len > buffer->len) { |
| 2558 | len = (int) buffer->len; |
| 2559 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2560 | PyErr_SetString(PyExc_OverflowError, |
| 2561 | "maximum length can't fit in a C 'int'"); |
| 2562 | goto error; |
| 2563 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2564 | if (len == 0) { |
| 2565 | count = 0; |
| 2566 | goto done; |
| 2567 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2568 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2571 | if (sock != NULL) { |
| 2572 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2573 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2574 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2575 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2576 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2577 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2578 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2579 | has_timeout = (timeout > 0); |
| 2580 | if (has_timeout) |
| 2581 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2582 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2583 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2584 | PySSL_BEGIN_ALLOW_THREADS |
| 2585 | count = SSL_read(self->ssl, mem, len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2586 | err = _PySSL_errno(count <= 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2587 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2588 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2589 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2590 | if (PyErr_CheckSignals()) |
| 2591 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2592 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2593 | if (has_timeout) |
| 2594 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2595 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2596 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2597 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2598 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2599 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2600 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2601 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2602 | { |
| 2603 | count = 0; |
| 2604 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2605 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2606 | else |
| 2607 | sockstate = SOCKET_OPERATION_OK; |
| 2608 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2609 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2610 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2611 | "The read operation timed out"); |
| 2612 | goto error; |
| 2613 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2614 | break; |
| 2615 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2616 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2617 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2618 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2619 | if (count <= 0) { |
| 2620 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2621 | goto error; |
| 2622 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2623 | if (self->exc_type != NULL) |
| 2624 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2625 | |
| 2626 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2627 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2628 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2629 | _PyBytes_Resize(&dest, count); |
| 2630 | return dest; |
| 2631 | } |
| 2632 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2633 | return PyLong_FromLong(count); |
| 2634 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2635 | |
| 2636 | error: |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2637 | PySSL_ChainExceptions(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2638 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2639 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2640 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2641 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2644 | /*[clinic input] |
| 2645 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2646 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2647 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2648 | [clinic start generated code]*/ |
| 2649 | |
| 2650 | static PyObject * |
| 2651 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2652 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2653 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2654 | _PySSLError err; |
| 2655 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2656 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2657 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2658 | _PyTime_t timeout, deadline = 0; |
| 2659 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2661 | if (sock != NULL) { |
| 2662 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2663 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2664 | _setSSLError("Underlying socket connection gone", |
| 2665 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2666 | return NULL; |
| 2667 | } |
| 2668 | Py_INCREF(sock); |
| 2669 | |
| 2670 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2671 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2672 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2673 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2674 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2675 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2676 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2677 | has_timeout = (timeout > 0); |
| 2678 | if (has_timeout) |
| 2679 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2680 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2681 | while (1) { |
| 2682 | PySSL_BEGIN_ALLOW_THREADS |
| 2683 | /* Disable read-ahead so that unwrap can work correctly. |
| 2684 | * Otherwise OpenSSL might read in too much data, |
| 2685 | * eating clear text data that happens to be |
| 2686 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2687 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2688 | * function is used and the shutdown_seen_zero != 0 |
| 2689 | * condition is met. |
| 2690 | */ |
| 2691 | if (self->shutdown_seen_zero) |
| 2692 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2693 | ret = SSL_shutdown(self->ssl); |
| 2694 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2695 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2696 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2697 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2698 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2699 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2700 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2701 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2702 | /* Don't loop endlessly; instead preserve legacy |
| 2703 | behaviour of trying SSL_shutdown() only twice. |
| 2704 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2705 | if (++zeros > 1) |
| 2706 | break; |
| 2707 | /* Shutdown was sent, now try receiving */ |
| 2708 | self->shutdown_seen_zero = 1; |
| 2709 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2712 | if (has_timeout) |
| 2713 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2714 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2715 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2716 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2717 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2718 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2719 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2720 | else |
| 2721 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2722 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2723 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2724 | if (err.ssl == SSL_ERROR_WANT_READ) |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2725 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2726 | "The read operation timed out"); |
| 2727 | else |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2728 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2729 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2730 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2731 | } |
| 2732 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2733 | PyErr_SetString(PySSLErrorObject, |
| 2734 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2735 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2736 | } |
| 2737 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2738 | /* Retain the SSL error code */ |
| 2739 | break; |
| 2740 | } |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2741 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2742 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2743 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2744 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2745 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2746 | if (self->exc_type != NULL) |
| 2747 | goto error; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2748 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2749 | /* It's already INCREF'ed */ |
| 2750 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2751 | else |
| 2752 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2753 | |
| 2754 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2755 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2756 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2757 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2760 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2761 | _ssl._SSLSocket.get_channel_binding |
| 2762 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2763 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2764 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2765 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2766 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2767 | of the data or None if the data is not available (e.g. before the handshake). |
| 2768 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2769 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2771 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2772 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2773 | const char *cb_type) |
| 2774 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2775 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2776 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2777 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2778 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2779 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2780 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2781 | /* if session is resumed XOR we are the client */ |
| 2782 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2783 | } |
| 2784 | else { |
| 2785 | /* if a new session XOR we are the server */ |
| 2786 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2787 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2788 | } |
| 2789 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2790 | PyErr_Format( |
| 2791 | PyExc_ValueError, |
| 2792 | "'%s' channel binding type not implemented", |
| 2793 | cb_type |
| 2794 | ); |
| 2795 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2799 | if (len == 0) |
| 2800 | Py_RETURN_NONE; |
| 2801 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2802 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2803 | } |
| 2804 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2805 | /*[clinic input] |
| 2806 | _ssl._SSLSocket.verify_client_post_handshake |
| 2807 | |
| 2808 | Initiate TLS 1.3 post-handshake authentication |
| 2809 | [clinic start generated code]*/ |
| 2810 | |
| 2811 | static PyObject * |
| 2812 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2813 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2814 | { |
| 2815 | #ifdef TLS1_3_VERSION |
| 2816 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2817 | if (err == 0) |
| 2818 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2819 | else |
| 2820 | Py_RETURN_NONE; |
| 2821 | #else |
| 2822 | PyErr_SetString(PyExc_NotImplementedError, |
| 2823 | "Post-handshake auth is not supported by your " |
| 2824 | "OpenSSL version."); |
| 2825 | return NULL; |
| 2826 | #endif |
| 2827 | } |
| 2828 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2829 | #ifdef OPENSSL_VERSION_1_1 |
| 2830 | |
| 2831 | static SSL_SESSION* |
| 2832 | _ssl_session_dup(SSL_SESSION *session) { |
| 2833 | SSL_SESSION *newsession = NULL; |
| 2834 | int slen; |
| 2835 | unsigned char *senc = NULL, *p; |
| 2836 | const unsigned char *const_p; |
| 2837 | |
| 2838 | if (session == NULL) { |
| 2839 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2840 | goto error; |
| 2841 | } |
| 2842 | |
| 2843 | /* get length */ |
| 2844 | slen = i2d_SSL_SESSION(session, NULL); |
| 2845 | if (slen == 0 || slen > 0xFF00) { |
| 2846 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2847 | goto error; |
| 2848 | } |
| 2849 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2850 | PyErr_NoMemory(); |
| 2851 | goto error; |
| 2852 | } |
| 2853 | p = senc; |
| 2854 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2855 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2856 | goto error; |
| 2857 | } |
| 2858 | const_p = senc; |
| 2859 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2860 | if (session == NULL) { |
| 2861 | goto error; |
| 2862 | } |
| 2863 | PyMem_Free(senc); |
| 2864 | return newsession; |
| 2865 | error: |
| 2866 | if (senc != NULL) { |
| 2867 | PyMem_Free(senc); |
| 2868 | } |
| 2869 | return NULL; |
| 2870 | } |
| 2871 | #endif |
| 2872 | |
| 2873 | static PyObject * |
| 2874 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2875 | /* get_session can return sessions from a server-side connection, |
| 2876 | * it does not check for handshake done or client socket. */ |
| 2877 | PySSLSession *pysess; |
| 2878 | SSL_SESSION *session; |
| 2879 | |
| 2880 | #ifdef OPENSSL_VERSION_1_1 |
| 2881 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2882 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2883 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2884 | if (session == NULL) { |
| 2885 | Py_RETURN_NONE; |
| 2886 | } |
| 2887 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2888 | return NULL; |
| 2889 | } |
| 2890 | #else |
| 2891 | session = SSL_get1_session(self->ssl); |
| 2892 | if (session == NULL) { |
| 2893 | Py_RETURN_NONE; |
| 2894 | } |
| 2895 | #endif |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2896 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2897 | if (pysess == NULL) { |
| 2898 | SSL_SESSION_free(session); |
| 2899 | return NULL; |
| 2900 | } |
| 2901 | |
| 2902 | assert(self->ctx); |
| 2903 | pysess->ctx = self->ctx; |
| 2904 | Py_INCREF(pysess->ctx); |
| 2905 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2906 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2907 | return (PyObject *)pysess; |
| 2908 | } |
| 2909 | |
| 2910 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2911 | void *closure) |
| 2912 | { |
| 2913 | PySSLSession *pysess; |
| 2914 | #ifdef OPENSSL_VERSION_1_1 |
| 2915 | SSL_SESSION *session; |
| 2916 | #endif |
| 2917 | int result; |
| 2918 | |
| 2919 | if (!PySSLSession_Check(value)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2920 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2921 | return -1; |
| 2922 | } |
| 2923 | pysess = (PySSLSession *)value; |
| 2924 | |
| 2925 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2926 | PyErr_SetString(PyExc_ValueError, |
| 2927 | "Session refers to a different SSLContext."); |
| 2928 | return -1; |
| 2929 | } |
| 2930 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2931 | PyErr_SetString(PyExc_ValueError, |
| 2932 | "Cannot set session for server-side SSLSocket."); |
| 2933 | return -1; |
| 2934 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2935 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2936 | PyErr_SetString(PyExc_ValueError, |
| 2937 | "Cannot set session after handshake."); |
| 2938 | return -1; |
| 2939 | } |
| 2940 | #ifdef OPENSSL_VERSION_1_1 |
| 2941 | /* duplicate session */ |
| 2942 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2943 | return -1; |
| 2944 | } |
| 2945 | result = SSL_set_session(self->ssl, session); |
| 2946 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2947 | SSL_SESSION_free(session); |
| 2948 | #else |
| 2949 | result = SSL_set_session(self->ssl, pysess->session); |
| 2950 | #endif |
| 2951 | if (result == 0) { |
| 2952 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2953 | return -1; |
| 2954 | } |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2959 | "_setter_session(session)\n\ |
| 2960 | \ |
| 2961 | Get / set SSLSession."); |
| 2962 | |
| 2963 | static PyObject * |
| 2964 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2965 | if (SSL_session_reused(self->ssl)) { |
| 2966 | Py_RETURN_TRUE; |
| 2967 | } else { |
| 2968 | Py_RETURN_FALSE; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2973 | "Was the client session reused during handshake?"); |
| 2974 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2975 | static PyGetSetDef ssl_getsetlist[] = { |
| 2976 | {"context", (getter) PySSL_get_context, |
| 2977 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2978 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2979 | PySSL_get_server_side_doc}, |
| 2980 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2981 | PySSL_get_server_hostname_doc}, |
| 2982 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2983 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2984 | {"session", (getter) PySSL_get_session, |
| 2985 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2986 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2987 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2988 | {NULL}, /* sentinel */ |
| 2989 | }; |
| 2990 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2991 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2992 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2993 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2994 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2995 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2996 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2997 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2998 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2999 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 3000 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 3001 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 3002 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 3003 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 3004 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3005 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3006 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3007 | }; |
| 3008 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3009 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3010 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3011 | "_ssl._SSLSocket", /*tp_name*/ |
| 3012 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3013 | 0, /*tp_itemsize*/ |
| 3014 | /* methods */ |
| 3015 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3016 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3017 | 0, /*tp_getattr*/ |
| 3018 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3019 | 0, /*tp_as_async*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3020 | 0, /*tp_repr*/ |
| 3021 | 0, /*tp_as_number*/ |
| 3022 | 0, /*tp_as_sequence*/ |
| 3023 | 0, /*tp_as_mapping*/ |
| 3024 | 0, /*tp_hash*/ |
| 3025 | 0, /*tp_call*/ |
| 3026 | 0, /*tp_str*/ |
| 3027 | 0, /*tp_getattro*/ |
| 3028 | 0, /*tp_setattro*/ |
| 3029 | 0, /*tp_as_buffer*/ |
| 3030 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3031 | 0, /*tp_doc*/ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3032 | (traverseproc) PySSL_traverse, /*tp_traverse*/ |
| 3033 | (inquiry) PySSL_clear, /*tp_clear*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3034 | 0, /*tp_richcompare*/ |
| 3035 | 0, /*tp_weaklistoffset*/ |
| 3036 | 0, /*tp_iter*/ |
| 3037 | 0, /*tp_iternext*/ |
| 3038 | PySSLMethods, /*tp_methods*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3039 | 0, /*tp_members*/ |
| 3040 | ssl_getsetlist, /*tp_getset*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3041 | }; |
| 3042 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3043 | |
| 3044 | /* |
| 3045 | * _SSLContext objects |
| 3046 | */ |
| 3047 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3048 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3049 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3050 | { |
| 3051 | int mode; |
| 3052 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 3053 | |
| 3054 | switch(n) { |
| 3055 | case PY_SSL_CERT_NONE: |
| 3056 | mode = SSL_VERIFY_NONE; |
| 3057 | break; |
| 3058 | case PY_SSL_CERT_OPTIONAL: |
| 3059 | mode = SSL_VERIFY_PEER; |
| 3060 | break; |
| 3061 | case PY_SSL_CERT_REQUIRED: |
| 3062 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3063 | break; |
| 3064 | default: |
| 3065 | PyErr_SetString(PyExc_ValueError, |
| 3066 | "invalid value for verify_mode"); |
| 3067 | return -1; |
| 3068 | } |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3069 | |
| 3070 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3071 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 3072 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3073 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3074 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 3075 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3076 | return 0; |
| 3077 | } |
| 3078 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3079 | /*[clinic input] |
| 3080 | @classmethod |
| 3081 | _ssl._SSLContext.__new__ |
| 3082 | protocol as proto_version: int |
| 3083 | / |
| 3084 | [clinic start generated code]*/ |
| 3085 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3086 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3087 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 3088 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3089 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3090 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3091 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3092 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3093 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3094 | int result; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3095 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3096 | unsigned long libver; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3097 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3099 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3100 | switch(proto_version) { |
| 3101 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 3102 | case PY_SSL_VERSION_SSL3: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3103 | ctx = SSL_CTX_new(SSLv3_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3104 | break; |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3105 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3106 | #if (defined(TLS1_VERSION) && \ |
| 3107 | !defined(OPENSSL_NO_TLS1) && \ |
| 3108 | !defined(OPENSSL_NO_TLS1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3109 | case PY_SSL_VERSION_TLS1: |
| 3110 | ctx = SSL_CTX_new(TLSv1_method()); |
| 3111 | break; |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3112 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3113 | #if (defined(TLS1_1_VERSION) && \ |
| 3114 | !defined(OPENSSL_NO_TLS1_1) && \ |
| 3115 | !defined(OPENSSL_NO_TLS1_1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3116 | case PY_SSL_VERSION_TLS1_1: |
| 3117 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 3118 | break; |
| 3119 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3120 | #if (defined(TLS1_2_VERSION) && \ |
| 3121 | !defined(OPENSSL_NO_TLS1_2) && \ |
| 3122 | !defined(OPENSSL_NO_TLS1_2_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3123 | case PY_SSL_VERSION_TLS1_2: |
| 3124 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 3125 | break; |
| 3126 | #endif |
| 3127 | case PY_SSL_VERSION_TLS: |
| 3128 | /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3129 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3130 | break; |
| 3131 | case PY_SSL_VERSION_TLS_CLIENT: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3132 | ctx = SSL_CTX_new(TLS_client_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3133 | break; |
| 3134 | case PY_SSL_VERSION_TLS_SERVER: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3135 | ctx = SSL_CTX_new(TLS_server_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3136 | break; |
| 3137 | default: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3138 | proto_version = -1; |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3139 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3140 | PySSL_END_ALLOW_THREADS |
| 3141 | |
| 3142 | if (proto_version == -1) { |
| 3143 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3144 | "invalid or unsupported protocol version"); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3145 | return NULL; |
| 3146 | } |
| 3147 | if (ctx == NULL) { |
Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 3148 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3149 | return NULL; |
| 3150 | } |
| 3151 | |
| 3152 | assert(type != NULL && type->tp_alloc != NULL); |
| 3153 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 3154 | if (self == NULL) { |
| 3155 | SSL_CTX_free(ctx); |
| 3156 | return NULL; |
| 3157 | } |
| 3158 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3159 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3160 | self->protocol = proto_version; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3161 | self->msg_cb = NULL; |
| 3162 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3163 | self->keylog_filename = NULL; |
| 3164 | self->keylog_bio = NULL; |
| 3165 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3166 | #if HAVE_NPN |
Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 3167 | self->npn_protocols = NULL; |
| 3168 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3169 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3170 | self->alpn_protocols = NULL; |
| 3171 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3172 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3173 | self->set_sni_cb = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3174 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3175 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3176 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3177 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3178 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3179 | Py_DECREF(self); |
| 3180 | return NULL; |
| 3181 | } |
| 3182 | } else { |
| 3183 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3184 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3185 | Py_DECREF(self); |
| 3186 | return NULL; |
| 3187 | } |
| 3188 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3189 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3190 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3191 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3192 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3193 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3194 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3195 | /* Minimal security flags for server and client side context. |
| 3196 | * Client sockets ignore server-side parameters. */ |
| 3197 | #ifdef SSL_OP_NO_COMPRESSION |
| 3198 | options |= SSL_OP_NO_COMPRESSION; |
| 3199 | #endif |
| 3200 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3201 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3202 | #endif |
| 3203 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3204 | options |= SSL_OP_SINGLE_DH_USE; |
| 3205 | #endif |
| 3206 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3207 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3208 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3209 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3210 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3211 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3212 | * It's far from perfect but gives users a better head start. */ |
| 3213 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3214 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3215 | /* stick to OpenSSL's default settings */ |
| 3216 | result = 1; |
| 3217 | #else |
| 3218 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3219 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3220 | } else { |
| 3221 | /* SSLv2 needs MD5 */ |
| 3222 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3223 | } |
| 3224 | if (result == 0) { |
| 3225 | Py_DECREF(self); |
| 3226 | ERR_clear_error(); |
| 3227 | PyErr_SetString(PySSLErrorObject, |
| 3228 | "No cipher can be selected."); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3232 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 3233 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 3234 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 3235 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 3236 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 3237 | conservative and assume it wasn't fixed until release. We do this check |
| 3238 | at runtime to avoid problems from the dynamic linker. |
| 3239 | See #25672 for more on this. */ |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3240 | libver = OpenSSL_version_num(); |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3241 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 3242 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 3243 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 3244 | } |
| 3245 | #endif |
| 3246 | |
| 3247 | |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3248 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3249 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 3250 | prime256v1 by default. This is Apache mod_ssl's initialization |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3251 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 3252 | */ |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3253 | #if defined(SSL_CTX_set_ecdh_auto) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3254 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 3255 | #else |
| 3256 | { |
| 3257 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 3258 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3259 | EC_KEY_free(key); |
| 3260 | } |
| 3261 | #endif |
| 3262 | #endif |
| 3263 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3264 | #define SID_CTX "Python" |
| 3265 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3266 | sizeof(SID_CTX)); |
| 3267 | #undef SID_CTX |
| 3268 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3269 | params = SSL_CTX_get0_param(self->ctx); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3270 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3271 | /* Improve trust chain building when cross-signed intermediate |
| 3272 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3273 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3274 | #endif |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3275 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3276 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3277 | #ifdef TLS1_3_VERSION |
| 3278 | self->post_handshake_auth = 0; |
| 3279 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3280 | #endif |
| 3281 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3282 | return (PyObject *)self; |
| 3283 | } |
| 3284 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3285 | static int |
| 3286 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3287 | { |
| 3288 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3289 | Py_VISIT(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3290 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3291 | Py_VISIT(self->msg_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
| 3294 | |
| 3295 | static int |
| 3296 | context_clear(PySSLContext *self) |
| 3297 | { |
| 3298 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3299 | Py_CLEAR(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3300 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3301 | Py_CLEAR(self->msg_cb); |
| 3302 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3303 | Py_CLEAR(self->keylog_filename); |
| 3304 | if (self->keylog_bio != NULL) { |
| 3305 | PySSL_BEGIN_ALLOW_THREADS |
| 3306 | BIO_free_all(self->keylog_bio); |
| 3307 | PySSL_END_ALLOW_THREADS |
| 3308 | self->keylog_bio = NULL; |
| 3309 | } |
| 3310 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3311 | return 0; |
| 3312 | } |
| 3313 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3314 | static void |
| 3315 | context_dealloc(PySSLContext *self) |
| 3316 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3317 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3318 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3319 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3320 | SSL_CTX_free(self->ctx); |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3321 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3322 | PyMem_FREE(self->npn_protocols); |
| 3323 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3324 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3325 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3326 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3327 | Py_TYPE(self)->tp_free(self); |
| 3328 | } |
| 3329 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3330 | /*[clinic input] |
| 3331 | _ssl._SSLContext.set_ciphers |
| 3332 | cipherlist: str |
| 3333 | / |
| 3334 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3335 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3336 | static PyObject * |
| 3337 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3338 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3339 | { |
| 3340 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3341 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3342 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3343 | otherwise the error will be reported again when another SSL call |
| 3344 | is done. */ |
| 3345 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3346 | PyErr_SetString(PySSLErrorObject, |
| 3347 | "No cipher can be selected."); |
| 3348 | return NULL; |
| 3349 | } |
| 3350 | Py_RETURN_NONE; |
| 3351 | } |
| 3352 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3353 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 3354 | /*[clinic input] |
| 3355 | _ssl._SSLContext.get_ciphers |
| 3356 | [clinic start generated code]*/ |
| 3357 | |
| 3358 | static PyObject * |
| 3359 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3360 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3361 | { |
| 3362 | SSL *ssl = NULL; |
| 3363 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3364 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3365 | int i=0; |
| 3366 | PyObject *result = NULL, *dct; |
| 3367 | |
| 3368 | ssl = SSL_new(self->ctx); |
| 3369 | if (ssl == NULL) { |
| 3370 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3371 | goto exit; |
| 3372 | } |
| 3373 | sk = SSL_get_ciphers(ssl); |
| 3374 | |
| 3375 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3376 | if (result == NULL) { |
| 3377 | goto exit; |
| 3378 | } |
| 3379 | |
| 3380 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3381 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3382 | dct = cipher_to_dict(cipher); |
| 3383 | if (dct == NULL) { |
| 3384 | Py_CLEAR(result); |
| 3385 | goto exit; |
| 3386 | } |
| 3387 | PyList_SET_ITEM(result, i, dct); |
| 3388 | } |
| 3389 | |
| 3390 | exit: |
| 3391 | if (ssl != NULL) |
| 3392 | SSL_free(ssl); |
| 3393 | return result; |
| 3394 | |
| 3395 | } |
| 3396 | #endif |
| 3397 | |
| 3398 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3399 | #if HAVE_NPN || HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3400 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3401 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3402 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3403 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3404 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3405 | int ret; |
| 3406 | if (client_protocols == NULL) { |
| 3407 | client_protocols = (unsigned char *)""; |
| 3408 | client_protocols_len = 0; |
| 3409 | } |
| 3410 | if (server_protocols == NULL) { |
| 3411 | server_protocols = (unsigned char *)""; |
| 3412 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3413 | } |
| 3414 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3415 | ret = SSL_select_next_proto(out, outlen, |
| 3416 | server_protocols, server_protocols_len, |
| 3417 | client_protocols, client_protocols_len); |
| 3418 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3419 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3420 | |
| 3421 | return SSL_TLSEXT_ERR_OK; |
| 3422 | } |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3423 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3424 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3425 | #if HAVE_NPN |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3426 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 3427 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3428 | _advertiseNPN_cb(SSL *s, |
| 3429 | const unsigned char **data, unsigned int *len, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3430 | void *args) |
| 3431 | { |
| 3432 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3433 | |
| 3434 | if (ssl_ctx->npn_protocols == NULL) { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3435 | *data = (unsigned char *)""; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3436 | *len = 0; |
| 3437 | } else { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3438 | *data = ssl_ctx->npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3439 | *len = ssl_ctx->npn_protocols_len; |
| 3440 | } |
| 3441 | |
| 3442 | return SSL_TLSEXT_ERR_OK; |
| 3443 | } |
| 3444 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3445 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3446 | _selectNPN_cb(SSL *s, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3447 | unsigned char **out, unsigned char *outlen, |
| 3448 | const unsigned char *server, unsigned int server_len, |
| 3449 | void *args) |
| 3450 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3451 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3452 | return do_protocol_selection(0, out, outlen, server, server_len, |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3453 | ctx->npn_protocols, ctx->npn_protocols_len); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3454 | } |
| 3455 | #endif |
| 3456 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3457 | /*[clinic input] |
| 3458 | _ssl._SSLContext._set_npn_protocols |
| 3459 | protos: Py_buffer |
| 3460 | / |
| 3461 | [clinic start generated code]*/ |
| 3462 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3463 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3464 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3465 | Py_buffer *protos) |
| 3466 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3467 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3468 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3469 | PyMem_Free(self->npn_protocols); |
| 3470 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3471 | if (self->npn_protocols == NULL) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3472 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3473 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3474 | self->npn_protocols_len = (int) protos->len; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3475 | |
| 3476 | /* set both server and client callbacks, because the context can |
| 3477 | * be used to create both types of sockets */ |
| 3478 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3479 | _advertiseNPN_cb, |
| 3480 | self); |
| 3481 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3482 | _selectNPN_cb, |
| 3483 | self); |
| 3484 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3485 | Py_RETURN_NONE; |
| 3486 | #else |
| 3487 | PyErr_SetString(PyExc_NotImplementedError, |
| 3488 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3489 | return NULL; |
| 3490 | #endif |
| 3491 | } |
| 3492 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3493 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3494 | static int |
| 3495 | _selectALPN_cb(SSL *s, |
| 3496 | const unsigned char **out, unsigned char *outlen, |
| 3497 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3498 | void *args) |
| 3499 | { |
| 3500 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3501 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3502 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3503 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3504 | } |
| 3505 | #endif |
| 3506 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3507 | /*[clinic input] |
| 3508 | _ssl._SSLContext._set_alpn_protocols |
| 3509 | protos: Py_buffer |
| 3510 | / |
| 3511 | [clinic start generated code]*/ |
| 3512 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3513 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3514 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3515 | Py_buffer *protos) |
| 3516 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3517 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3518 | #if HAVE_ALPN |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3519 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3520 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3521 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3522 | return NULL; |
| 3523 | } |
| 3524 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3525 | PyMem_FREE(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3526 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3527 | if (!self->alpn_protocols) |
| 3528 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3529 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3530 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3531 | |
| 3532 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3533 | return PyErr_NoMemory(); |
| 3534 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3535 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3536 | Py_RETURN_NONE; |
| 3537 | #else |
| 3538 | PyErr_SetString(PyExc_NotImplementedError, |
| 3539 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3540 | return NULL; |
| 3541 | #endif |
| 3542 | } |
| 3543 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3544 | static PyObject * |
| 3545 | get_verify_mode(PySSLContext *self, void *c) |
| 3546 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3547 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3548 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3549 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3550 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3551 | case SSL_VERIFY_NONE: |
| 3552 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3553 | case SSL_VERIFY_PEER: |
| 3554 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3555 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3556 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3557 | } |
| 3558 | PyErr_SetString(PySSLErrorObject, |
| 3559 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3560 | return NULL; |
| 3561 | } |
| 3562 | |
| 3563 | static int |
| 3564 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3565 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3566 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3567 | if (!PyArg_Parse(arg, "i", &n)) |
| 3568 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3569 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3570 | PyErr_SetString(PyExc_ValueError, |
| 3571 | "Cannot set verify_mode to CERT_NONE when " |
| 3572 | "check_hostname is enabled."); |
| 3573 | return -1; |
| 3574 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3575 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3579 | get_verify_flags(PySSLContext *self, void *c) |
| 3580 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3581 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3582 | unsigned long flags; |
| 3583 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3584 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3585 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3586 | return PyLong_FromUnsignedLong(flags); |
| 3587 | } |
| 3588 | |
| 3589 | static int |
| 3590 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3591 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3592 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3593 | unsigned long new_flags, flags, set, clear; |
| 3594 | |
| 3595 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3596 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3597 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3598 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3599 | clear = flags & ~new_flags; |
| 3600 | set = ~flags & new_flags; |
| 3601 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3602 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3603 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3604 | return -1; |
| 3605 | } |
| 3606 | } |
| 3607 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3608 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3609 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3610 | return -1; |
| 3611 | } |
| 3612 | } |
| 3613 | return 0; |
| 3614 | } |
| 3615 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3616 | /* Getter and setter for protocol version */ |
| 3617 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) |
| 3618 | |
| 3619 | |
| 3620 | static int |
| 3621 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3622 | { |
| 3623 | long v; |
| 3624 | int result; |
| 3625 | |
| 3626 | if (!PyArg_Parse(arg, "l", &v)) |
| 3627 | return -1; |
| 3628 | if (v > INT_MAX) { |
| 3629 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3630 | return -1; |
| 3631 | } |
| 3632 | |
| 3633 | switch(self->protocol) { |
| 3634 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3635 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3636 | case PY_SSL_VERSION_TLS: |
| 3637 | break; |
| 3638 | default: |
| 3639 | PyErr_SetString( |
| 3640 | PyExc_ValueError, |
| 3641 | "The context's protocol doesn't support modification of " |
| 3642 | "highest and lowest version." |
| 3643 | ); |
| 3644 | return -1; |
| 3645 | } |
| 3646 | |
| 3647 | if (what == 0) { |
| 3648 | switch(v) { |
| 3649 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3650 | v = 0; |
| 3651 | break; |
| 3652 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3653 | /* Emulate max for set_min_proto_version */ |
| 3654 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3655 | break; |
| 3656 | default: |
| 3657 | break; |
| 3658 | } |
| 3659 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3660 | } |
| 3661 | else { |
| 3662 | switch(v) { |
| 3663 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3664 | v = 0; |
| 3665 | break; |
| 3666 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3667 | /* Emulate max for set_min_proto_version */ |
| 3668 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3669 | break; |
| 3670 | default: |
| 3671 | break; |
| 3672 | } |
| 3673 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3674 | } |
| 3675 | if (result == 0) { |
| 3676 | PyErr_Format(PyExc_ValueError, |
| 3677 | "Unsupported protocol version 0x%x", v); |
| 3678 | return -1; |
| 3679 | } |
| 3680 | return 0; |
| 3681 | } |
| 3682 | |
| 3683 | static PyObject * |
| 3684 | get_minimum_version(PySSLContext *self, void *c) |
| 3685 | { |
| 3686 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3687 | if (v == 0) { |
| 3688 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3689 | } |
| 3690 | return PyLong_FromLong(v); |
| 3691 | } |
| 3692 | |
| 3693 | static int |
| 3694 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3695 | { |
| 3696 | return set_min_max_proto_version(self, arg, 0); |
| 3697 | } |
| 3698 | |
| 3699 | static PyObject * |
| 3700 | get_maximum_version(PySSLContext *self, void *c) |
| 3701 | { |
| 3702 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3703 | if (v == 0) { |
| 3704 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3705 | } |
| 3706 | return PyLong_FromLong(v); |
| 3707 | } |
| 3708 | |
| 3709 | static int |
| 3710 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3711 | { |
| 3712 | return set_min_max_proto_version(self, arg, 1); |
| 3713 | } |
| 3714 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ |
| 3715 | |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3716 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 3717 | static PyObject * |
| 3718 | get_num_tickets(PySSLContext *self, void *c) |
| 3719 | { |
Victor Stinner | 76611c7 | 2019-07-09 13:30:52 +0200 | [diff] [blame] | 3720 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | static int |
| 3724 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3725 | { |
| 3726 | long num; |
| 3727 | if (!PyArg_Parse(arg, "l", &num)) |
| 3728 | return -1; |
| 3729 | if (num < 0) { |
| 3730 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3731 | return -1; |
| 3732 | } |
| 3733 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3734 | PyErr_SetString(PyExc_ValueError, |
| 3735 | "SSLContext is not a server context."); |
| 3736 | return -1; |
| 3737 | } |
| 3738 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3739 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3740 | return -1; |
| 3741 | } |
| 3742 | return 0; |
| 3743 | } |
| 3744 | |
| 3745 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3746 | "Control the number of TLSv1.3 session tickets"); |
| 3747 | #endif /* OpenSSL 1.1.1 */ |
| 3748 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3749 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3750 | get_options(PySSLContext *self, void *c) |
| 3751 | { |
| 3752 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3753 | } |
| 3754 | |
| 3755 | static int |
| 3756 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3757 | { |
| 3758 | long new_opts, opts, set, clear; |
| 3759 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3760 | return -1; |
| 3761 | opts = SSL_CTX_get_options(self->ctx); |
| 3762 | clear = opts & ~new_opts; |
| 3763 | set = ~opts & new_opts; |
| 3764 | if (clear) { |
| 3765 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3766 | SSL_CTX_clear_options(self->ctx, clear); |
| 3767 | #else |
| 3768 | PyErr_SetString(PyExc_ValueError, |
| 3769 | "can't clear options before OpenSSL 0.9.8m"); |
| 3770 | return -1; |
| 3771 | #endif |
| 3772 | } |
| 3773 | if (set) |
| 3774 | SSL_CTX_set_options(self->ctx, set); |
| 3775 | return 0; |
| 3776 | } |
| 3777 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3778 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3779 | get_host_flags(PySSLContext *self, void *c) |
| 3780 | { |
| 3781 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3782 | } |
| 3783 | |
| 3784 | static int |
| 3785 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3786 | { |
| 3787 | X509_VERIFY_PARAM *param; |
| 3788 | unsigned int new_flags = 0; |
| 3789 | |
| 3790 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3791 | return -1; |
| 3792 | |
| 3793 | param = SSL_CTX_get0_param(self->ctx); |
| 3794 | self->hostflags = new_flags; |
| 3795 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3796 | return 0; |
| 3797 | } |
| 3798 | |
| 3799 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3800 | get_check_hostname(PySSLContext *self, void *c) |
| 3801 | { |
| 3802 | return PyBool_FromLong(self->check_hostname); |
| 3803 | } |
| 3804 | |
| 3805 | static int |
| 3806 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3807 | { |
| 3808 | int check_hostname; |
| 3809 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3810 | return -1; |
| 3811 | if (check_hostname && |
| 3812 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3813 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3814 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3815 | return -1; |
| 3816 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3817 | } |
| 3818 | self->check_hostname = check_hostname; |
| 3819 | return 0; |
| 3820 | } |
| 3821 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3822 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3823 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3824 | #if TLS1_3_VERSION |
| 3825 | return PyBool_FromLong(self->post_handshake_auth); |
| 3826 | #else |
| 3827 | Py_RETURN_NONE; |
| 3828 | #endif |
| 3829 | } |
| 3830 | |
| 3831 | #if TLS1_3_VERSION |
| 3832 | static int |
| 3833 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3834 | if (arg == NULL) { |
| 3835 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3836 | return -1; |
| 3837 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3838 | int pha = PyObject_IsTrue(arg); |
| 3839 | |
| 3840 | if (pha == -1) { |
| 3841 | return -1; |
| 3842 | } |
| 3843 | self->post_handshake_auth = pha; |
| 3844 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3845 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3846 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3847 | |
| 3848 | return 0; |
| 3849 | } |
| 3850 | #endif |
| 3851 | |
| 3852 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3853 | get_protocol(PySSLContext *self, void *c) { |
| 3854 | return PyLong_FromLong(self->protocol); |
| 3855 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3856 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3857 | typedef struct { |
| 3858 | PyThreadState *thread_state; |
| 3859 | PyObject *callable; |
| 3860 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3861 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3862 | int error; |
| 3863 | } _PySSLPasswordInfo; |
| 3864 | |
| 3865 | static int |
| 3866 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3867 | const char *bad_type_error) |
| 3868 | { |
| 3869 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3870 | from a unicode, bytes, or byte array object. |
| 3871 | The password field will be dynamically allocated and must be freed |
| 3872 | by the caller */ |
| 3873 | PyObject *password_bytes = NULL; |
| 3874 | const char *data = NULL; |
| 3875 | Py_ssize_t size; |
| 3876 | |
| 3877 | if (PyUnicode_Check(password)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3878 | password_bytes = PyUnicode_AsUTF8String(password); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3879 | if (!password_bytes) { |
| 3880 | goto error; |
| 3881 | } |
| 3882 | data = PyBytes_AS_STRING(password_bytes); |
| 3883 | size = PyBytes_GET_SIZE(password_bytes); |
| 3884 | } else if (PyBytes_Check(password)) { |
| 3885 | data = PyBytes_AS_STRING(password); |
| 3886 | size = PyBytes_GET_SIZE(password); |
| 3887 | } else if (PyByteArray_Check(password)) { |
| 3888 | data = PyByteArray_AS_STRING(password); |
| 3889 | size = PyByteArray_GET_SIZE(password); |
| 3890 | } else { |
| 3891 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3892 | goto error; |
| 3893 | } |
| 3894 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3895 | if (size > (Py_ssize_t)INT_MAX) { |
| 3896 | PyErr_Format(PyExc_ValueError, |
| 3897 | "password cannot be longer than %d bytes", INT_MAX); |
| 3898 | goto error; |
| 3899 | } |
| 3900 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3901 | PyMem_Free(pw_info->password); |
| 3902 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3903 | if (!pw_info->password) { |
| 3904 | PyErr_SetString(PyExc_MemoryError, |
| 3905 | "unable to allocate password buffer"); |
| 3906 | goto error; |
| 3907 | } |
| 3908 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3909 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3910 | |
| 3911 | Py_XDECREF(password_bytes); |
| 3912 | return 1; |
| 3913 | |
| 3914 | error: |
| 3915 | Py_XDECREF(password_bytes); |
| 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | static int |
| 3920 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3921 | { |
| 3922 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3923 | PyObject *fn_ret = NULL; |
| 3924 | |
| 3925 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3926 | |
| 3927 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3928 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3929 | if (!fn_ret) { |
| 3930 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3931 | core python API, so we could use it to add a frame here */ |
| 3932 | goto error; |
| 3933 | } |
| 3934 | |
| 3935 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3936 | "password callback must return a string")) { |
| 3937 | goto error; |
| 3938 | } |
| 3939 | Py_CLEAR(fn_ret); |
| 3940 | } |
| 3941 | |
| 3942 | if (pw_info->size > size) { |
| 3943 | PyErr_Format(PyExc_ValueError, |
| 3944 | "password cannot be longer than %d bytes", size); |
| 3945 | goto error; |
| 3946 | } |
| 3947 | |
| 3948 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3949 | memcpy(buf, pw_info->password, pw_info->size); |
| 3950 | return pw_info->size; |
| 3951 | |
| 3952 | error: |
| 3953 | Py_XDECREF(fn_ret); |
| 3954 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3955 | pw_info->error = 1; |
| 3956 | return -1; |
| 3957 | } |
| 3958 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3959 | /*[clinic input] |
| 3960 | _ssl._SSLContext.load_cert_chain |
| 3961 | certfile: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3962 | keyfile: object = None |
| 3963 | password: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3964 | |
| 3965 | [clinic start generated code]*/ |
| 3966 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3967 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3968 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3969 | PyObject *keyfile, PyObject *password) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3970 | /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3971 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3972 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3973 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3974 | void *orig_passwd_userdata = SSL_CTX_get_default_passwd_cb_userdata(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3975 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3976 | int r; |
| 3977 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3978 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3979 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3980 | if (keyfile == Py_None) |
| 3981 | keyfile = NULL; |
| 3982 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3983 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3984 | PyErr_SetString(PyExc_TypeError, |
| 3985 | "certfile should be a valid filesystem path"); |
| 3986 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3987 | return NULL; |
| 3988 | } |
| 3989 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3990 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3991 | PyErr_SetString(PyExc_TypeError, |
| 3992 | "keyfile should be a valid filesystem path"); |
| 3993 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3994 | goto error; |
| 3995 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3996 | if (password != Py_None) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3997 | if (PyCallable_Check(password)) { |
| 3998 | pw_info.callable = password; |
| 3999 | } else if (!_pwinfo_set(&pw_info, password, |
| 4000 | "password should be a string or callable")) { |
| 4001 | goto error; |
| 4002 | } |
| 4003 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 4004 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 4005 | } |
| 4006 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4007 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 4008 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4009 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4010 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4011 | if (pw_info.error) { |
| 4012 | ERR_clear_error(); |
| 4013 | /* the password callback has already set the error information */ |
| 4014 | } |
| 4015 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 4016 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4017 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4018 | } |
| 4019 | else { |
| 4020 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4021 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4022 | goto error; |
| 4023 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4024 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 4025 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4026 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 4027 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4028 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 4029 | Py_CLEAR(keyfile_bytes); |
| 4030 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4031 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4032 | if (pw_info.error) { |
| 4033 | ERR_clear_error(); |
| 4034 | /* the password callback has already set the error information */ |
| 4035 | } |
| 4036 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 4037 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4038 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4039 | } |
| 4040 | else { |
| 4041 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4042 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4043 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4044 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4045 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4046 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4047 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4048 | if (r != 1) { |
| 4049 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4050 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4051 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4052 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4053 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4054 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4055 | Py_RETURN_NONE; |
| 4056 | |
| 4057 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4058 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4059 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4060 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4061 | Py_XDECREF(keyfile_bytes); |
| 4062 | Py_XDECREF(certfile_bytes); |
| 4063 | return NULL; |
| 4064 | } |
| 4065 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4066 | /* internal helper function, returns -1 on error |
| 4067 | */ |
| 4068 | static int |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 4069 | _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4070 | int filetype) |
| 4071 | { |
| 4072 | BIO *biobuf = NULL; |
| 4073 | X509_STORE *store; |
| 4074 | int retval = 0, err, loaded = 0; |
| 4075 | |
| 4076 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 4077 | |
| 4078 | if (len <= 0) { |
| 4079 | PyErr_SetString(PyExc_ValueError, |
| 4080 | "Empty certificate data"); |
| 4081 | return -1; |
| 4082 | } else if (len > INT_MAX) { |
| 4083 | PyErr_SetString(PyExc_OverflowError, |
| 4084 | "Certificate data is too long."); |
| 4085 | return -1; |
| 4086 | } |
| 4087 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 4088 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4089 | if (biobuf == NULL) { |
| 4090 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 4091 | return -1; |
| 4092 | } |
| 4093 | |
| 4094 | store = SSL_CTX_get_cert_store(self->ctx); |
| 4095 | assert(store != NULL); |
| 4096 | |
| 4097 | while (1) { |
| 4098 | X509 *cert = NULL; |
| 4099 | int r; |
| 4100 | |
| 4101 | if (filetype == SSL_FILETYPE_ASN1) { |
| 4102 | cert = d2i_X509_bio(biobuf, NULL); |
| 4103 | } else { |
| 4104 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4105 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 4106 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 4107 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4108 | } |
| 4109 | if (cert == NULL) { |
| 4110 | break; |
| 4111 | } |
| 4112 | r = X509_STORE_add_cert(store, cert); |
| 4113 | X509_free(cert); |
| 4114 | if (!r) { |
| 4115 | err = ERR_peek_last_error(); |
| 4116 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 4117 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 4118 | /* cert already in hash table, not an error */ |
| 4119 | ERR_clear_error(); |
| 4120 | } else { |
| 4121 | break; |
| 4122 | } |
| 4123 | } |
| 4124 | loaded++; |
| 4125 | } |
| 4126 | |
| 4127 | err = ERR_peek_last_error(); |
| 4128 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 4129 | (loaded > 0) && |
| 4130 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 4131 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 4132 | /* EOF ASN1 file, not an error */ |
| 4133 | ERR_clear_error(); |
| 4134 | retval = 0; |
| 4135 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 4136 | (loaded > 0) && |
| 4137 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 4138 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 4139 | /* EOF PEM file, not an error */ |
| 4140 | ERR_clear_error(); |
| 4141 | retval = 0; |
| 4142 | } else { |
| 4143 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4144 | retval = -1; |
| 4145 | } |
| 4146 | |
| 4147 | BIO_free(biobuf); |
| 4148 | return retval; |
| 4149 | } |
| 4150 | |
| 4151 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4152 | /*[clinic input] |
| 4153 | _ssl._SSLContext.load_verify_locations |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4154 | cafile: object = None |
| 4155 | capath: object = None |
| 4156 | cadata: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4157 | |
| 4158 | [clinic start generated code]*/ |
| 4159 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4160 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4161 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 4162 | PyObject *cafile, |
| 4163 | PyObject *capath, |
| 4164 | PyObject *cadata) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4165 | /*[clinic end generated code: output=454c7e41230ca551 input=42ecfe258233e194]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4166 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4167 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 4168 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4169 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4170 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4171 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4172 | if (cafile == Py_None) |
| 4173 | cafile = NULL; |
| 4174 | if (capath == Py_None) |
| 4175 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4176 | if (cadata == Py_None) |
| 4177 | cadata = NULL; |
| 4178 | |
| 4179 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4180 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4181 | "cafile, capath and cadata cannot be all omitted"); |
| 4182 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4183 | } |
| 4184 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4185 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4186 | PyErr_SetString(PyExc_TypeError, |
| 4187 | "cafile should be a valid filesystem path"); |
| 4188 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4189 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4190 | } |
| 4191 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4192 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4193 | PyErr_SetString(PyExc_TypeError, |
| 4194 | "capath should be a valid filesystem path"); |
| 4195 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4196 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4197 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4198 | |
| 4199 | /* validata cadata type and load cadata */ |
| 4200 | if (cadata) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4201 | if (PyUnicode_Check(cadata)) { |
| 4202 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4203 | if (cadata_ascii == NULL) { |
| 4204 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4205 | goto invalid_cadata; |
| 4206 | } |
| 4207 | goto error; |
| 4208 | } |
| 4209 | r = _add_ca_certs(self, |
| 4210 | PyBytes_AS_STRING(cadata_ascii), |
| 4211 | PyBytes_GET_SIZE(cadata_ascii), |
| 4212 | SSL_FILETYPE_PEM); |
| 4213 | Py_DECREF(cadata_ascii); |
| 4214 | if (r == -1) { |
| 4215 | goto error; |
| 4216 | } |
| 4217 | } |
| 4218 | else if (PyObject_CheckBuffer(cadata)) { |
| 4219 | Py_buffer buf; |
| 4220 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 4221 | goto error; |
| 4222 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4223 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4224 | PyBuffer_Release(&buf); |
| 4225 | PyErr_SetString(PyExc_TypeError, |
| 4226 | "cadata should be a contiguous buffer with " |
| 4227 | "a single dimension"); |
| 4228 | goto error; |
| 4229 | } |
| 4230 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4231 | PyBuffer_Release(&buf); |
| 4232 | if (r == -1) { |
| 4233 | goto error; |
| 4234 | } |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4235 | } |
| 4236 | else { |
| 4237 | invalid_cadata: |
| 4238 | PyErr_SetString(PyExc_TypeError, |
| 4239 | "cadata should be an ASCII string or a " |
| 4240 | "bytes-like object"); |
| 4241 | goto error; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | /* load cafile or capath */ |
| 4246 | if (cafile || capath) { |
| 4247 | if (cafile) |
| 4248 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4249 | if (capath) |
| 4250 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4251 | PySSL_BEGIN_ALLOW_THREADS |
| 4252 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4253 | PySSL_END_ALLOW_THREADS |
| 4254 | if (r != 1) { |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4255 | if (errno != 0) { |
| 4256 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4257 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4258 | } |
| 4259 | else { |
| 4260 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4261 | } |
| 4262 | goto error; |
| 4263 | } |
| 4264 | } |
| 4265 | goto end; |
| 4266 | |
| 4267 | error: |
| 4268 | ok = 0; |
| 4269 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4270 | Py_XDECREF(cafile_bytes); |
| 4271 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4272 | if (ok) { |
| 4273 | Py_RETURN_NONE; |
| 4274 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4275 | return NULL; |
| 4276 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4279 | /*[clinic input] |
| 4280 | _ssl._SSLContext.load_dh_params |
| 4281 | path as filepath: object |
| 4282 | / |
| 4283 | |
| 4284 | [clinic start generated code]*/ |
| 4285 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4286 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4287 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4288 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4289 | { |
| 4290 | FILE *f; |
| 4291 | DH *dh; |
| 4292 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4293 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4294 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4295 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4296 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4297 | errno = 0; |
| 4298 | PySSL_BEGIN_ALLOW_THREADS |
| 4299 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4300 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4301 | PySSL_END_ALLOW_THREADS |
| 4302 | if (dh == NULL) { |
| 4303 | if (errno != 0) { |
| 4304 | ERR_clear_error(); |
| 4305 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4306 | } |
| 4307 | else { |
| 4308 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4309 | } |
| 4310 | return NULL; |
| 4311 | } |
| 4312 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 4313 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4314 | DH_free(dh); |
| 4315 | Py_RETURN_NONE; |
| 4316 | } |
| 4317 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4318 | /*[clinic input] |
| 4319 | _ssl._SSLContext._wrap_socket |
| 4320 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 4321 | server_side: int |
| 4322 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4323 | * |
| 4324 | owner: object = None |
| 4325 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4326 | |
| 4327 | [clinic start generated code]*/ |
| 4328 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4329 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4330 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4331 | int server_side, PyObject *hostname_obj, |
| 4332 | PyObject *owner, PyObject *session) |
| 4333 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4334 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4335 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4336 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4337 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4338 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4339 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4340 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4341 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4342 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4343 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4344 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4345 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4346 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4347 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4348 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4349 | if (hostname != NULL) |
| 4350 | PyMem_Free(hostname); |
| 4351 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4354 | /*[clinic input] |
| 4355 | _ssl._SSLContext._wrap_bio |
| 4356 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4357 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4358 | server_side: int |
| 4359 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4360 | * |
| 4361 | owner: object = None |
| 4362 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4363 | |
| 4364 | [clinic start generated code]*/ |
| 4365 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4366 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4367 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4368 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4369 | PyObject *hostname_obj, PyObject *owner, |
| 4370 | PyObject *session) |
| 4371 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=8cf22f4d586ac56a]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4372 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4373 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4374 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4375 | |
| 4376 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4377 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4378 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4379 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4380 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4384 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4385 | incoming, outgoing); |
| 4386 | |
| 4387 | PyMem_Free(hostname); |
| 4388 | return res; |
| 4389 | } |
| 4390 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4391 | /*[clinic input] |
| 4392 | _ssl._SSLContext.session_stats |
| 4393 | [clinic start generated code]*/ |
| 4394 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4395 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4396 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4397 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4398 | { |
| 4399 | int r; |
| 4400 | PyObject *value, *stats = PyDict_New(); |
| 4401 | if (!stats) |
| 4402 | return NULL; |
| 4403 | |
| 4404 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4405 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4406 | if (value == NULL) \ |
| 4407 | goto error; \ |
| 4408 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4409 | Py_DECREF(value); \ |
| 4410 | if (r < 0) \ |
| 4411 | goto error; |
| 4412 | |
| 4413 | ADD_STATS(number, "number"); |
| 4414 | ADD_STATS(connect, "connect"); |
| 4415 | ADD_STATS(connect_good, "connect_good"); |
| 4416 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4417 | ADD_STATS(accept, "accept"); |
| 4418 | ADD_STATS(accept_good, "accept_good"); |
| 4419 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4420 | ADD_STATS(accept, "accept"); |
| 4421 | ADD_STATS(hits, "hits"); |
| 4422 | ADD_STATS(misses, "misses"); |
| 4423 | ADD_STATS(timeouts, "timeouts"); |
| 4424 | ADD_STATS(cache_full, "cache_full"); |
| 4425 | |
| 4426 | #undef ADD_STATS |
| 4427 | |
| 4428 | return stats; |
| 4429 | |
| 4430 | error: |
| 4431 | Py_DECREF(stats); |
| 4432 | return NULL; |
| 4433 | } |
| 4434 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4435 | /*[clinic input] |
| 4436 | _ssl._SSLContext.set_default_verify_paths |
| 4437 | [clinic start generated code]*/ |
| 4438 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4439 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4440 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4441 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4442 | { |
| 4443 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 4444 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4445 | return NULL; |
| 4446 | } |
| 4447 | Py_RETURN_NONE; |
| 4448 | } |
| 4449 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4450 | #ifndef OPENSSL_NO_ECDH |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4451 | /*[clinic input] |
| 4452 | _ssl._SSLContext.set_ecdh_curve |
| 4453 | name: object |
| 4454 | / |
| 4455 | |
| 4456 | [clinic start generated code]*/ |
| 4457 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4458 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4459 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4460 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4461 | { |
| 4462 | PyObject *name_bytes; |
| 4463 | int nid; |
| 4464 | EC_KEY *key; |
| 4465 | |
| 4466 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4467 | return NULL; |
| 4468 | assert(PyBytes_Check(name_bytes)); |
| 4469 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4470 | Py_DECREF(name_bytes); |
| 4471 | if (nid == 0) { |
| 4472 | PyErr_Format(PyExc_ValueError, |
| 4473 | "unknown elliptic curve name %R", name); |
| 4474 | return NULL; |
| 4475 | } |
| 4476 | key = EC_KEY_new_by_curve_name(nid); |
| 4477 | if (key == NULL) { |
| 4478 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4479 | return NULL; |
| 4480 | } |
| 4481 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4482 | EC_KEY_free(key); |
| 4483 | Py_RETURN_NONE; |
| 4484 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4485 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4486 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4487 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4488 | static int |
| 4489 | _servername_callback(SSL *s, int *al, void *args) |
| 4490 | { |
| 4491 | int ret; |
| 4492 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 4493 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4494 | PyObject *result; |
| 4495 | /* The high-level ssl.SSLSocket object */ |
| 4496 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4497 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4498 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4499 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4500 | if (ssl_ctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4501 | /* remove race condition in this the call back while if removing the |
| 4502 | * callback is in progress */ |
| 4503 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4504 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | ssl = SSL_get_app_data(s); |
| 4508 | assert(PySSLSocket_Check(ssl)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4509 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4510 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4511 | * SSL connection and that has a .context attribute that can be changed to |
| 4512 | * identify the requested hostname. Since the official API is the Python |
| 4513 | * level API we want to pass the callback a Python level object rather than |
| 4514 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4515 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4516 | * will be passed. If both do not exist only then the C-level object is |
| 4517 | * passed. */ |
| 4518 | if (ssl->owner) |
| 4519 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4520 | else if (ssl->Socket) |
| 4521 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4522 | else |
| 4523 | ssl_socket = (PyObject *) ssl; |
| 4524 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4525 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4526 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4527 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4528 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4529 | if (servername == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4530 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4531 | Py_None, ssl_ctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4532 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4533 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4534 | PyObject *servername_bytes; |
| 4535 | PyObject *servername_str; |
| 4536 | |
| 4537 | servername_bytes = PyBytes_FromString(servername); |
| 4538 | if (servername_bytes == NULL) { |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4539 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 4540 | goto error; |
| 4541 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4542 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4543 | * back into a str object, but still as an A-label (bpo-28414) |
| 4544 | */ |
| 4545 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
| 4546 | Py_DECREF(servername_bytes); |
| 4547 | if (servername_str == NULL) { |
| 4548 | PyErr_WriteUnraisable(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4549 | goto error; |
| 4550 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4551 | result = PyObject_CallFunctionObjArgs( |
| 4552 | ssl_ctx->set_sni_cb, ssl_socket, servername_str, |
| 4553 | ssl_ctx, NULL); |
| 4554 | Py_DECREF(servername_str); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4555 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4556 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4557 | |
| 4558 | if (result == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4559 | PyErr_WriteUnraisable(ssl_ctx->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4560 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4561 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4562 | } |
| 4563 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4564 | /* Result may be None, a SSLContext or an integer |
| 4565 | * None and SSLContext are OK, integer or other values are an error. |
| 4566 | */ |
| 4567 | if (result == Py_None) { |
| 4568 | ret = SSL_TLSEXT_ERR_OK; |
| 4569 | } else { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4570 | *al = (int) PyLong_AsLong(result); |
| 4571 | if (PyErr_Occurred()) { |
| 4572 | PyErr_WriteUnraisable(result); |
| 4573 | *al = SSL_AD_INTERNAL_ERROR; |
| 4574 | } |
| 4575 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4576 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4577 | Py_DECREF(result); |
| 4578 | } |
| 4579 | |
| 4580 | PyGILState_Release(gstate); |
| 4581 | return ret; |
| 4582 | |
| 4583 | error: |
| 4584 | Py_DECREF(ssl_socket); |
| 4585 | *al = SSL_AD_INTERNAL_ERROR; |
| 4586 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4587 | PyGILState_Release(gstate); |
| 4588 | return ret; |
| 4589 | } |
Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 4590 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4591 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4592 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4593 | get_sni_callback(PySSLContext *self, void *c) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4594 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4595 | PyObject *cb = self->set_sni_cb; |
| 4596 | if (cb == NULL) { |
| 4597 | Py_RETURN_NONE; |
| 4598 | } |
| 4599 | Py_INCREF(cb); |
| 4600 | return cb; |
| 4601 | } |
| 4602 | |
| 4603 | static int |
| 4604 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4605 | { |
| 4606 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4607 | PyErr_SetString(PyExc_ValueError, |
| 4608 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4609 | return -1; |
| 4610 | } |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4611 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4612 | Py_CLEAR(self->set_sni_cb); |
| 4613 | if (arg == Py_None) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4614 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4615 | } |
| 4616 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4617 | if (!PyCallable_Check(arg)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4618 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4619 | PyErr_SetString(PyExc_TypeError, |
| 4620 | "not a callable object"); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4621 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4622 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4623 | Py_INCREF(arg); |
| 4624 | self->set_sni_cb = arg; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4625 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4626 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4627 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4628 | return 0; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4629 | #else |
| 4630 | PyErr_SetString(PyExc_NotImplementedError, |
| 4631 | "The TLS extension servername callback, " |
| 4632 | "SSL_CTX_set_tlsext_servername_callback, " |
| 4633 | "is not in the current OpenSSL library."); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4634 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4635 | #endif |
| 4636 | } |
| 4637 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4638 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4639 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4640 | \n\ |
| 4641 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4642 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4643 | See RFC 6066 for details of the SNI extension."); |
| 4644 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4645 | /*[clinic input] |
| 4646 | _ssl._SSLContext.cert_store_stats |
| 4647 | |
| 4648 | Returns quantities of loaded X.509 certificates. |
| 4649 | |
| 4650 | X.509 certificates with a CA extension and certificate revocation lists |
| 4651 | inside the context's cert store. |
| 4652 | |
| 4653 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4654 | been used at least once. |
| 4655 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4656 | |
| 4657 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4658 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4659 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4660 | { |
| 4661 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4662 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4663 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4664 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4665 | |
| 4666 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4667 | objs = X509_STORE_get0_objects(store); |
| 4668 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4669 | obj = sk_X509_OBJECT_value(objs, i); |
| 4670 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4671 | case X509_LU_X509: |
| 4672 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4673 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4674 | ca++; |
| 4675 | } |
| 4676 | break; |
| 4677 | case X509_LU_CRL: |
| 4678 | crl++; |
| 4679 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4680 | default: |
| 4681 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4682 | * As far as I can tell they are internal states and never |
| 4683 | * stored in a cert store */ |
| 4684 | break; |
| 4685 | } |
| 4686 | } |
| 4687 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4688 | "x509_ca", ca); |
| 4689 | } |
| 4690 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4691 | /*[clinic input] |
| 4692 | _ssl._SSLContext.get_ca_certs |
| 4693 | binary_form: bool = False |
| 4694 | |
| 4695 | Returns a list of dicts with information of loaded CA certs. |
| 4696 | |
| 4697 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4698 | certificate. |
| 4699 | |
| 4700 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4701 | been used at least once. |
| 4702 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4703 | |
| 4704 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4705 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4706 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4707 | { |
| 4708 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4709 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4710 | PyObject *ci = NULL, *rlist = NULL; |
| 4711 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4712 | |
| 4713 | if ((rlist = PyList_New(0)) == NULL) { |
| 4714 | return NULL; |
| 4715 | } |
| 4716 | |
| 4717 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4718 | objs = X509_STORE_get0_objects(store); |
| 4719 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4720 | X509_OBJECT *obj; |
| 4721 | X509 *cert; |
| 4722 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4723 | obj = sk_X509_OBJECT_value(objs, i); |
| 4724 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4725 | /* not a x509 cert */ |
| 4726 | continue; |
| 4727 | } |
| 4728 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4729 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4730 | if (!X509_check_ca(cert)) { |
| 4731 | continue; |
| 4732 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4733 | if (binary_form) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4734 | ci = _certificate_to_der(cert); |
| 4735 | } else { |
| 4736 | ci = _decode_certificate(cert); |
| 4737 | } |
| 4738 | if (ci == NULL) { |
| 4739 | goto error; |
| 4740 | } |
| 4741 | if (PyList_Append(rlist, ci) == -1) { |
| 4742 | goto error; |
| 4743 | } |
| 4744 | Py_CLEAR(ci); |
| 4745 | } |
| 4746 | return rlist; |
| 4747 | |
| 4748 | error: |
| 4749 | Py_XDECREF(ci); |
| 4750 | Py_XDECREF(rlist); |
| 4751 | return NULL; |
| 4752 | } |
| 4753 | |
| 4754 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4755 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4756 | {"check_hostname", (getter) get_check_hostname, |
| 4757 | (setter) set_check_hostname, NULL}, |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4758 | {"_host_flags", (getter) get_host_flags, |
| 4759 | (setter) set_host_flags, NULL}, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4760 | #if SSL_CTRL_GET_MAX_PROTO_VERSION |
| 4761 | {"minimum_version", (getter) get_minimum_version, |
| 4762 | (setter) set_minimum_version, NULL}, |
| 4763 | {"maximum_version", (getter) get_maximum_version, |
| 4764 | (setter) set_maximum_version, NULL}, |
| 4765 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4766 | #ifdef HAVE_OPENSSL_KEYLOG |
| 4767 | {"keylog_filename", (getter) _PySSLContext_get_keylog_filename, |
| 4768 | (setter) _PySSLContext_set_keylog_filename, NULL}, |
| 4769 | #endif |
| 4770 | {"_msg_callback", (getter) _PySSLContext_get_msg_callback, |
| 4771 | (setter) _PySSLContext_set_msg_callback, NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4772 | {"sni_callback", (getter) get_sni_callback, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4773 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 4774 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 4775 | {"num_tickets", (getter) get_num_tickets, |
| 4776 | (setter) set_num_tickets, PySSLContext_num_tickets_doc}, |
| 4777 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4778 | {"options", (getter) get_options, |
| 4779 | (setter) set_options, NULL}, |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4780 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4781 | #ifdef TLS1_3_VERSION |
| 4782 | (setter) set_post_handshake_auth, |
| 4783 | #else |
| 4784 | NULL, |
| 4785 | #endif |
| 4786 | NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4787 | {"protocol", (getter) get_protocol, |
| 4788 | NULL, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4789 | {"verify_flags", (getter) get_verify_flags, |
| 4790 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4791 | {"verify_mode", (getter) get_verify_mode, |
| 4792 | (setter) set_verify_mode, NULL}, |
| 4793 | {NULL}, /* sentinel */ |
| 4794 | }; |
| 4795 | |
| 4796 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4797 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4798 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4799 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4800 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4801 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4802 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4803 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4804 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4805 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4806 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4807 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4808 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4809 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4810 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4811 | {NULL, NULL} /* sentinel */ |
| 4812 | }; |
| 4813 | |
| 4814 | static PyTypeObject PySSLContext_Type = { |
| 4815 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4816 | "_ssl._SSLContext", /*tp_name*/ |
| 4817 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 4818 | 0, /*tp_itemsize*/ |
| 4819 | (destructor)context_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4820 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4821 | 0, /*tp_getattr*/ |
| 4822 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4823 | 0, /*tp_as_async*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4824 | 0, /*tp_repr*/ |
| 4825 | 0, /*tp_as_number*/ |
| 4826 | 0, /*tp_as_sequence*/ |
| 4827 | 0, /*tp_as_mapping*/ |
| 4828 | 0, /*tp_hash*/ |
| 4829 | 0, /*tp_call*/ |
| 4830 | 0, /*tp_str*/ |
| 4831 | 0, /*tp_getattro*/ |
| 4832 | 0, /*tp_setattro*/ |
| 4833 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4834 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4835 | 0, /*tp_doc*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4836 | (traverseproc) context_traverse, /*tp_traverse*/ |
| 4837 | (inquiry) context_clear, /*tp_clear*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4838 | 0, /*tp_richcompare*/ |
| 4839 | 0, /*tp_weaklistoffset*/ |
| 4840 | 0, /*tp_iter*/ |
| 4841 | 0, /*tp_iternext*/ |
| 4842 | context_methods, /*tp_methods*/ |
| 4843 | 0, /*tp_members*/ |
| 4844 | context_getsetlist, /*tp_getset*/ |
| 4845 | 0, /*tp_base*/ |
| 4846 | 0, /*tp_dict*/ |
| 4847 | 0, /*tp_descr_get*/ |
| 4848 | 0, /*tp_descr_set*/ |
| 4849 | 0, /*tp_dictoffset*/ |
| 4850 | 0, /*tp_init*/ |
| 4851 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4852 | _ssl__SSLContext, /*tp_new*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4853 | }; |
| 4854 | |
| 4855 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4856 | /* |
| 4857 | * MemoryBIO objects |
| 4858 | */ |
| 4859 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4860 | /*[clinic input] |
| 4861 | @classmethod |
| 4862 | _ssl.MemoryBIO.__new__ |
| 4863 | |
| 4864 | [clinic start generated code]*/ |
| 4865 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4866 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4867 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4868 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4869 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4870 | BIO *bio; |
| 4871 | PySSLMemoryBIO *self; |
| 4872 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4873 | bio = BIO_new(BIO_s_mem()); |
| 4874 | if (bio == NULL) { |
| 4875 | PyErr_SetString(PySSLErrorObject, |
| 4876 | "failed to allocate BIO"); |
| 4877 | return NULL; |
| 4878 | } |
| 4879 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4880 | * just that no data is currently available. The SSL routines should retry |
| 4881 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4882 | BIO_set_retry_read(bio); |
| 4883 | BIO_set_mem_eof_return(bio, -1); |
| 4884 | |
| 4885 | assert(type != NULL && type->tp_alloc != NULL); |
| 4886 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4887 | if (self == NULL) { |
| 4888 | BIO_free(bio); |
| 4889 | return NULL; |
| 4890 | } |
| 4891 | self->bio = bio; |
| 4892 | self->eof_written = 0; |
| 4893 | |
| 4894 | return (PyObject *) self; |
| 4895 | } |
| 4896 | |
| 4897 | static void |
| 4898 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4899 | { |
| 4900 | BIO_free(self->bio); |
| 4901 | Py_TYPE(self)->tp_free(self); |
| 4902 | } |
| 4903 | |
| 4904 | static PyObject * |
| 4905 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4906 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4907 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4911 | "The number of bytes pending in the memory BIO."); |
| 4912 | |
| 4913 | static PyObject * |
| 4914 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4915 | { |
| 4916 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4917 | && self->eof_written); |
| 4918 | } |
| 4919 | |
| 4920 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4921 | "Whether the memory BIO is at EOF."); |
| 4922 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4923 | /*[clinic input] |
| 4924 | _ssl.MemoryBIO.read |
| 4925 | size as len: int = -1 |
| 4926 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4927 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4928 | Read up to size bytes from the memory BIO. |
| 4929 | |
| 4930 | If size is not specified, read the entire buffer. |
| 4931 | If the return value is an empty bytes instance, this means either |
| 4932 | EOF or that no data is available. Use the "eof" property to |
| 4933 | distinguish between the two. |
| 4934 | [clinic start generated code]*/ |
| 4935 | |
| 4936 | static PyObject * |
| 4937 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4938 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4939 | { |
| 4940 | int avail, nbytes; |
| 4941 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4942 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4943 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4944 | if ((len < 0) || (len > avail)) |
| 4945 | len = avail; |
| 4946 | |
| 4947 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4948 | if ((result == NULL) || (len == 0)) |
| 4949 | return result; |
| 4950 | |
| 4951 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4952 | if (nbytes < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4953 | Py_DECREF(result); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4954 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4955 | return NULL; |
| 4956 | } |
| 4957 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4958 | /* There should never be any short reads but check anyway. */ |
| 4959 | if (nbytes < len) { |
| 4960 | _PyBytes_Resize(&result, nbytes); |
| 4961 | } |
| 4962 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4963 | return result; |
| 4964 | } |
| 4965 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4966 | /*[clinic input] |
| 4967 | _ssl.MemoryBIO.write |
| 4968 | b: Py_buffer |
| 4969 | / |
| 4970 | |
| 4971 | Writes the bytes b into the memory BIO. |
| 4972 | |
| 4973 | Returns the number of bytes written. |
| 4974 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4975 | |
| 4976 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4977 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4978 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4979 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4980 | int nbytes; |
| 4981 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4982 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4983 | PyErr_Format(PyExc_OverflowError, |
| 4984 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4985 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | if (self->eof_written) { |
| 4989 | PyErr_SetString(PySSLErrorObject, |
| 4990 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4991 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4992 | } |
| 4993 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4994 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4995 | if (nbytes < 0) { |
| 4996 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4997 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4998 | } |
| 4999 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5000 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5003 | /*[clinic input] |
| 5004 | _ssl.MemoryBIO.write_eof |
| 5005 | |
| 5006 | Write an EOF marker to the memory BIO. |
| 5007 | |
| 5008 | When all data has been read, the "eof" property will be True. |
| 5009 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5010 | |
| 5011 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5012 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 5013 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5014 | { |
| 5015 | self->eof_written = 1; |
| 5016 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 5017 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 5018 | BIO_clear_retry_flags(self->bio); |
| 5019 | BIO_set_mem_eof_return(self->bio, 0); |
| 5020 | |
| 5021 | Py_RETURN_NONE; |
| 5022 | } |
| 5023 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5024 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 5025 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 5026 | PySSL_memory_bio_pending_doc}, |
| 5027 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 5028 | PySSL_memory_bio_eof_doc}, |
| 5029 | {NULL}, /* sentinel */ |
| 5030 | }; |
| 5031 | |
| 5032 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5033 | _SSL_MEMORYBIO_READ_METHODDEF |
| 5034 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 5035 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5036 | {NULL, NULL} /* sentinel */ |
| 5037 | }; |
| 5038 | |
| 5039 | static PyTypeObject PySSLMemoryBIO_Type = { |
| 5040 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5041 | "_ssl.MemoryBIO", /*tp_name*/ |
| 5042 | sizeof(PySSLMemoryBIO), /*tp_basicsize*/ |
| 5043 | 0, /*tp_itemsize*/ |
| 5044 | (destructor)memory_bio_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5045 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5046 | 0, /*tp_getattr*/ |
| 5047 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5048 | 0, /*tp_as_async*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5049 | 0, /*tp_repr*/ |
| 5050 | 0, /*tp_as_number*/ |
| 5051 | 0, /*tp_as_sequence*/ |
| 5052 | 0, /*tp_as_mapping*/ |
| 5053 | 0, /*tp_hash*/ |
| 5054 | 0, /*tp_call*/ |
| 5055 | 0, /*tp_str*/ |
| 5056 | 0, /*tp_getattro*/ |
| 5057 | 0, /*tp_setattro*/ |
| 5058 | 0, /*tp_as_buffer*/ |
| 5059 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5060 | 0, /*tp_doc*/ |
| 5061 | 0, /*tp_traverse*/ |
| 5062 | 0, /*tp_clear*/ |
| 5063 | 0, /*tp_richcompare*/ |
| 5064 | 0, /*tp_weaklistoffset*/ |
| 5065 | 0, /*tp_iter*/ |
| 5066 | 0, /*tp_iternext*/ |
| 5067 | memory_bio_methods, /*tp_methods*/ |
| 5068 | 0, /*tp_members*/ |
| 5069 | memory_bio_getsetlist, /*tp_getset*/ |
| 5070 | 0, /*tp_base*/ |
| 5071 | 0, /*tp_dict*/ |
| 5072 | 0, /*tp_descr_get*/ |
| 5073 | 0, /*tp_descr_set*/ |
| 5074 | 0, /*tp_dictoffset*/ |
| 5075 | 0, /*tp_init*/ |
| 5076 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5077 | _ssl_MemoryBIO, /*tp_new*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5078 | }; |
| 5079 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5080 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5081 | /* |
| 5082 | * SSL Session object |
| 5083 | */ |
| 5084 | |
| 5085 | static void |
| 5086 | PySSLSession_dealloc(PySSLSession *self) |
| 5087 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 5088 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5089 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5090 | Py_XDECREF(self->ctx); |
| 5091 | if (self->session != NULL) { |
| 5092 | SSL_SESSION_free(self->session); |
| 5093 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5094 | PyObject_GC_Del(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | static PyObject * |
| 5098 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 5099 | { |
| 5100 | int result; |
| 5101 | |
| 5102 | if (left == NULL || right == NULL) { |
| 5103 | PyErr_BadInternalCall(); |
| 5104 | return NULL; |
| 5105 | } |
| 5106 | |
| 5107 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 5108 | Py_RETURN_NOTIMPLEMENTED; |
| 5109 | } |
| 5110 | |
| 5111 | if (left == right) { |
| 5112 | result = 0; |
| 5113 | } else { |
| 5114 | const unsigned char *left_id, *right_id; |
| 5115 | unsigned int left_len, right_len; |
| 5116 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 5117 | &left_len); |
| 5118 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 5119 | &right_len); |
| 5120 | if (left_len == right_len) { |
| 5121 | result = memcmp(left_id, right_id, left_len); |
| 5122 | } else { |
| 5123 | result = 1; |
| 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | switch (op) { |
| 5128 | case Py_EQ: |
| 5129 | if (result == 0) { |
| 5130 | Py_RETURN_TRUE; |
| 5131 | } else { |
| 5132 | Py_RETURN_FALSE; |
| 5133 | } |
| 5134 | break; |
| 5135 | case Py_NE: |
| 5136 | if (result != 0) { |
| 5137 | Py_RETURN_TRUE; |
| 5138 | } else { |
| 5139 | Py_RETURN_FALSE; |
| 5140 | } |
| 5141 | break; |
| 5142 | case Py_LT: |
| 5143 | case Py_LE: |
| 5144 | case Py_GT: |
| 5145 | case Py_GE: |
| 5146 | Py_RETURN_NOTIMPLEMENTED; |
| 5147 | break; |
| 5148 | default: |
| 5149 | PyErr_BadArgument(); |
| 5150 | return NULL; |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | static int |
| 5155 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 5156 | { |
| 5157 | Py_VISIT(self->ctx); |
| 5158 | return 0; |
| 5159 | } |
| 5160 | |
| 5161 | static int |
| 5162 | PySSLSession_clear(PySSLSession *self) |
| 5163 | { |
| 5164 | Py_CLEAR(self->ctx); |
| 5165 | return 0; |
| 5166 | } |
| 5167 | |
| 5168 | |
| 5169 | static PyObject * |
| 5170 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 5171 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 5172 | } |
| 5173 | |
| 5174 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 5175 | "Session creation time (seconds since epoch)."); |
| 5176 | |
| 5177 | |
| 5178 | static PyObject * |
| 5179 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 5180 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 5181 | } |
| 5182 | |
| 5183 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 5184 | "Session timeout (delta in seconds)."); |
| 5185 | |
| 5186 | |
| 5187 | static PyObject * |
| 5188 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 5189 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 5190 | return PyLong_FromUnsignedLong(hint); |
| 5191 | } |
| 5192 | |
| 5193 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 5194 | "Ticket life time hint."); |
| 5195 | |
| 5196 | |
| 5197 | static PyObject * |
| 5198 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 5199 | const unsigned char *id; |
| 5200 | unsigned int len; |
| 5201 | id = SSL_SESSION_get_id(self->session, &len); |
| 5202 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 5203 | } |
| 5204 | |
| 5205 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 5206 | "Session id"); |
| 5207 | |
| 5208 | |
| 5209 | static PyObject * |
| 5210 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 5211 | if (SSL_SESSION_has_ticket(self->session)) { |
| 5212 | Py_RETURN_TRUE; |
| 5213 | } else { |
| 5214 | Py_RETURN_FALSE; |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5219 | "Does the session contain a ticket?"); |
| 5220 | |
| 5221 | |
| 5222 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5223 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5224 | PySSLSession_get_has_ticket_doc}, |
| 5225 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5226 | PySSLSession_get_session_id_doc}, |
| 5227 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5228 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5229 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5230 | PySSLSession_get_time_doc}, |
| 5231 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5232 | PySSLSession_get_timeout_doc}, |
| 5233 | {NULL}, /* sentinel */ |
| 5234 | }; |
| 5235 | |
| 5236 | static PyTypeObject PySSLSession_Type = { |
| 5237 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5238 | "_ssl.Session", /*tp_name*/ |
| 5239 | sizeof(PySSLSession), /*tp_basicsize*/ |
| 5240 | 0, /*tp_itemsize*/ |
| 5241 | (destructor)PySSLSession_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5242 | 0, /*tp_vectorcall_offset*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5243 | 0, /*tp_getattr*/ |
| 5244 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5245 | 0, /*tp_as_async*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5246 | 0, /*tp_repr*/ |
| 5247 | 0, /*tp_as_number*/ |
| 5248 | 0, /*tp_as_sequence*/ |
| 5249 | 0, /*tp_as_mapping*/ |
| 5250 | 0, /*tp_hash*/ |
| 5251 | 0, /*tp_call*/ |
| 5252 | 0, /*tp_str*/ |
| 5253 | 0, /*tp_getattro*/ |
| 5254 | 0, /*tp_setattro*/ |
| 5255 | 0, /*tp_as_buffer*/ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5256 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5257 | 0, /*tp_doc*/ |
| 5258 | (traverseproc)PySSLSession_traverse, /*tp_traverse*/ |
| 5259 | (inquiry)PySSLSession_clear, /*tp_clear*/ |
| 5260 | PySSLSession_richcompare, /*tp_richcompare*/ |
| 5261 | 0, /*tp_weaklistoffset*/ |
| 5262 | 0, /*tp_iter*/ |
| 5263 | 0, /*tp_iternext*/ |
| 5264 | 0, /*tp_methods*/ |
| 5265 | 0, /*tp_members*/ |
| 5266 | PySSLSession_getsetlist, /*tp_getset*/ |
| 5267 | }; |
| 5268 | |
| 5269 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5270 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5271 | /*[clinic input] |
| 5272 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5273 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5274 | entropy: double |
| 5275 | / |
| 5276 | |
| 5277 | Mix string into the OpenSSL PRNG state. |
| 5278 | |
| 5279 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5280 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5281 | [clinic start generated code]*/ |
| 5282 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5284 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5285 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5286 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5287 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5288 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5289 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5290 | buf = (const char *)view->buf; |
| 5291 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5292 | do { |
| 5293 | written = Py_MIN(len, INT_MAX); |
| 5294 | RAND_add(buf, (int)written, entropy); |
| 5295 | buf += written; |
| 5296 | len -= written; |
| 5297 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5298 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5301 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5302 | PySSL_RAND(int len, int pseudo) |
| 5303 | { |
| 5304 | int ok; |
| 5305 | PyObject *bytes; |
| 5306 | unsigned long err; |
| 5307 | const char *errstr; |
| 5308 | PyObject *v; |
| 5309 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5310 | if (len < 0) { |
| 5311 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5312 | return NULL; |
| 5313 | } |
| 5314 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5315 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5316 | if (bytes == NULL) |
| 5317 | return NULL; |
| 5318 | if (pseudo) { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 5319 | #ifdef PY_OPENSSL_1_1_API |
| 5320 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5321 | #else |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5322 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 5323 | #endif |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5324 | if (ok == 0 || ok == 1) |
| 5325 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5326 | } |
| 5327 | else { |
| 5328 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5329 | if (ok == 1) |
| 5330 | return bytes; |
| 5331 | } |
| 5332 | Py_DECREF(bytes); |
| 5333 | |
| 5334 | err = ERR_get_error(); |
| 5335 | errstr = ERR_reason_error_string(err); |
| 5336 | v = Py_BuildValue("(ks)", err, errstr); |
| 5337 | if (v != NULL) { |
| 5338 | PyErr_SetObject(PySSLErrorObject, v); |
| 5339 | Py_DECREF(v); |
| 5340 | } |
| 5341 | return NULL; |
| 5342 | } |
| 5343 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5344 | /*[clinic input] |
| 5345 | _ssl.RAND_bytes |
| 5346 | n: int |
| 5347 | / |
| 5348 | |
| 5349 | Generate n cryptographically strong pseudo-random bytes. |
| 5350 | [clinic start generated code]*/ |
| 5351 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5352 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5353 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5354 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5355 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5356 | return PySSL_RAND(n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5357 | } |
| 5358 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5359 | /*[clinic input] |
| 5360 | _ssl.RAND_pseudo_bytes |
| 5361 | n: int |
| 5362 | / |
| 5363 | |
| 5364 | Generate n pseudo-random bytes. |
| 5365 | |
| 5366 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5367 | if the bytes generated are cryptographically strong. |
| 5368 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5369 | |
| 5370 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5371 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5372 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5373 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5374 | return PySSL_RAND(n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5375 | } |
| 5376 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5377 | /*[clinic input] |
| 5378 | _ssl.RAND_status |
| 5379 | |
| 5380 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 5381 | |
| 5382 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5383 | using the ssl() function. |
| 5384 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5385 | |
| 5386 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5387 | _ssl_RAND_status_impl(PyObject *module) |
| 5388 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5389 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5390 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5393 | #ifndef OPENSSL_NO_EGD |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5394 | /* LCOV_EXCL_START */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5395 | /*[clinic input] |
| 5396 | _ssl.RAND_egd |
| 5397 | path: object(converter="PyUnicode_FSConverter") |
| 5398 | / |
| 5399 | |
| 5400 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 5401 | |
| 5402 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 5403 | fails or if it does not provide enough data to seed PRNG. |
| 5404 | [clinic start generated code]*/ |
| 5405 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5406 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5407 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 5408 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5409 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5410 | int bytes = RAND_egd(PyBytes_AsString(path)); |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5411 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5412 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5413 | PyErr_SetString(PySSLErrorObject, |
| 5414 | "EGD connection failed or EGD did not return " |
| 5415 | "enough data to seed the PRNG"); |
| 5416 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5417 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5418 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5419 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5420 | /* LCOV_EXCL_STOP */ |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5421 | #endif /* OPENSSL_NO_EGD */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5422 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5423 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5424 | |
| 5425 | /*[clinic input] |
| 5426 | _ssl.get_default_verify_paths |
| 5427 | |
| 5428 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5429 | |
| 5430 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5431 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5432 | |
| 5433 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5434 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5435 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5436 | { |
| 5437 | PyObject *ofile_env = NULL; |
| 5438 | PyObject *ofile = NULL; |
| 5439 | PyObject *odir_env = NULL; |
| 5440 | PyObject *odir = NULL; |
| 5441 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5442 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5443 | const char *tmp = (info); \ |
| 5444 | target = NULL; \ |
| 5445 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5446 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5447 | target = PyBytes_FromString(tmp); } \ |
| 5448 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5449 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5450 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5451 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5452 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5453 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5454 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5455 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5456 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5457 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5458 | |
| 5459 | error: |
| 5460 | Py_XDECREF(ofile_env); |
| 5461 | Py_XDECREF(ofile); |
| 5462 | Py_XDECREF(odir_env); |
| 5463 | Py_XDECREF(odir); |
| 5464 | return NULL; |
| 5465 | } |
| 5466 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5467 | static PyObject* |
| 5468 | asn1obj2py(ASN1_OBJECT *obj) |
| 5469 | { |
| 5470 | int nid; |
| 5471 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5472 | |
| 5473 | nid = OBJ_obj2nid(obj); |
| 5474 | if (nid == NID_undef) { |
| 5475 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5476 | return NULL; |
| 5477 | } |
| 5478 | sn = OBJ_nid2sn(nid); |
| 5479 | ln = OBJ_nid2ln(nid); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5480 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5481 | } |
| 5482 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5483 | /*[clinic input] |
| 5484 | _ssl.txt2obj |
| 5485 | txt: str |
| 5486 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5487 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5488 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5489 | |
| 5490 | By default objects are looked up by OID. With name=True short and |
| 5491 | long name are also matched. |
| 5492 | [clinic start generated code]*/ |
| 5493 | |
| 5494 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5495 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5496 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5497 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5498 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5499 | ASN1_OBJECT *obj; |
| 5500 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5501 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5502 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5503 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5504 | return NULL; |
| 5505 | } |
| 5506 | result = asn1obj2py(obj); |
| 5507 | ASN1_OBJECT_free(obj); |
| 5508 | return result; |
| 5509 | } |
| 5510 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5511 | /*[clinic input] |
| 5512 | _ssl.nid2obj |
| 5513 | nid: int |
| 5514 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5515 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5516 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5517 | [clinic start generated code]*/ |
| 5518 | |
| 5519 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5520 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5521 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5522 | { |
| 5523 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5524 | ASN1_OBJECT *obj; |
| 5525 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5526 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5527 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5528 | return NULL; |
| 5529 | } |
| 5530 | obj = OBJ_nid2obj(nid); |
| 5531 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5532 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5533 | return NULL; |
| 5534 | } |
| 5535 | result = asn1obj2py(obj); |
| 5536 | ASN1_OBJECT_free(obj); |
| 5537 | return result; |
| 5538 | } |
| 5539 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5540 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5541 | |
| 5542 | static PyObject* |
| 5543 | certEncodingType(DWORD encodingType) |
| 5544 | { |
| 5545 | static PyObject *x509_asn = NULL; |
| 5546 | static PyObject *pkcs_7_asn = NULL; |
| 5547 | |
| 5548 | if (x509_asn == NULL) { |
| 5549 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5550 | if (x509_asn == NULL) |
| 5551 | return NULL; |
| 5552 | } |
| 5553 | if (pkcs_7_asn == NULL) { |
| 5554 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5555 | if (pkcs_7_asn == NULL) |
| 5556 | return NULL; |
| 5557 | } |
| 5558 | switch(encodingType) { |
| 5559 | case X509_ASN_ENCODING: |
| 5560 | Py_INCREF(x509_asn); |
| 5561 | return x509_asn; |
| 5562 | case PKCS_7_ASN_ENCODING: |
| 5563 | Py_INCREF(pkcs_7_asn); |
| 5564 | return pkcs_7_asn; |
| 5565 | default: |
| 5566 | return PyLong_FromLong(encodingType); |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | static PyObject* |
| 5571 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5572 | { |
| 5573 | CERT_ENHKEY_USAGE *usage; |
| 5574 | DWORD size, error, i; |
| 5575 | PyObject *retval; |
| 5576 | |
| 5577 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5578 | error = GetLastError(); |
| 5579 | if (error == CRYPT_E_NOT_FOUND) { |
| 5580 | Py_RETURN_TRUE; |
| 5581 | } |
| 5582 | return PyErr_SetFromWindowsErr(error); |
| 5583 | } |
| 5584 | |
| 5585 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5586 | if (usage == NULL) { |
| 5587 | return PyErr_NoMemory(); |
| 5588 | } |
| 5589 | |
| 5590 | /* Now get the actual enhanced usage property */ |
| 5591 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5592 | PyMem_Free(usage); |
| 5593 | error = GetLastError(); |
| 5594 | if (error == CRYPT_E_NOT_FOUND) { |
| 5595 | Py_RETURN_TRUE; |
| 5596 | } |
| 5597 | return PyErr_SetFromWindowsErr(error); |
| 5598 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5599 | retval = PyFrozenSet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5600 | if (retval == NULL) { |
| 5601 | goto error; |
| 5602 | } |
| 5603 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5604 | if (usage->rgpszUsageIdentifier[i]) { |
| 5605 | PyObject *oid; |
| 5606 | int err; |
| 5607 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5608 | if (oid == NULL) { |
| 5609 | Py_CLEAR(retval); |
| 5610 | goto error; |
| 5611 | } |
| 5612 | err = PySet_Add(retval, oid); |
| 5613 | Py_DECREF(oid); |
| 5614 | if (err == -1) { |
| 5615 | Py_CLEAR(retval); |
| 5616 | goto error; |
| 5617 | } |
| 5618 | } |
| 5619 | } |
| 5620 | error: |
| 5621 | PyMem_Free(usage); |
| 5622 | return retval; |
| 5623 | } |
| 5624 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5625 | static HCERTSTORE |
| 5626 | ssl_collect_certificates(const char *store_name) |
| 5627 | { |
| 5628 | /* this function collects the system certificate stores listed in |
| 5629 | * system_stores into a collection certificate store for being |
| 5630 | * enumerated. The store must be readable to be added to the |
| 5631 | * store collection. |
| 5632 | */ |
| 5633 | |
| 5634 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5635 | static DWORD system_stores[] = { |
| 5636 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5637 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5638 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5639 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5640 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5641 | CERT_SYSTEM_STORE_SERVICES, |
| 5642 | CERT_SYSTEM_STORE_USERS}; |
| 5643 | size_t i, storesAdded; |
| 5644 | BOOL result; |
| 5645 | |
| 5646 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5647 | (HCRYPTPROV)NULL, 0, NULL); |
| 5648 | if (!hCollectionStore) { |
| 5649 | return NULL; |
| 5650 | } |
| 5651 | storesAdded = 0; |
| 5652 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5653 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5654 | (HCRYPTPROV)NULL, |
| 5655 | CERT_STORE_READONLY_FLAG | |
| 5656 | system_stores[i], store_name); |
| 5657 | if (hSystemStore) { |
| 5658 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5659 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5660 | if (result) { |
| 5661 | ++storesAdded; |
| 5662 | } |
neonene | ed70129 | 2019-09-09 21:33:43 +0900 | [diff] [blame] | 5663 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5664 | } |
| 5665 | } |
| 5666 | if (storesAdded == 0) { |
| 5667 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5668 | return NULL; |
| 5669 | } |
| 5670 | |
| 5671 | return hCollectionStore; |
| 5672 | } |
| 5673 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5674 | /*[clinic input] |
| 5675 | _ssl.enum_certificates |
| 5676 | store_name: str |
| 5677 | |
| 5678 | Retrieve certificates from Windows' cert store. |
| 5679 | |
| 5680 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5681 | more cert storages, too. The function returns a list of (bytes, |
| 5682 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5683 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5684 | a set of OIDs or the boolean True. |
| 5685 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5686 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5687 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5688 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5689 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5690 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5691 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5692 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5693 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5694 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5695 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5696 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5697 | if (result == NULL) { |
| 5698 | return NULL; |
| 5699 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5700 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5701 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5702 | Py_DECREF(result); |
| 5703 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5704 | } |
| 5705 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5706 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5707 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5708 | pCertCtx->cbCertEncoded); |
| 5709 | if (!cert) { |
| 5710 | Py_CLEAR(result); |
| 5711 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5712 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5713 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5714 | Py_CLEAR(result); |
| 5715 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5716 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5717 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5718 | if (keyusage == Py_True) { |
| 5719 | Py_DECREF(keyusage); |
| 5720 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5721 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5722 | if (keyusage == NULL) { |
| 5723 | Py_CLEAR(result); |
| 5724 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5725 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5726 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5727 | Py_CLEAR(result); |
| 5728 | break; |
| 5729 | } |
| 5730 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5731 | cert = NULL; |
| 5732 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5733 | enc = NULL; |
| 5734 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5735 | keyusage = NULL; |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5736 | if (PySet_Add(result, tup) == -1) { |
| 5737 | Py_CLEAR(result); |
| 5738 | Py_CLEAR(tup); |
| 5739 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5740 | } |
| 5741 | Py_CLEAR(tup); |
| 5742 | } |
| 5743 | if (pCertCtx) { |
| 5744 | /* loop ended with an error, need to clean up context manually */ |
| 5745 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | /* In error cases cert, enc and tup may not be NULL */ |
| 5749 | Py_XDECREF(cert); |
| 5750 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5751 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5752 | Py_XDECREF(tup); |
| 5753 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5754 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5755 | associated with the store, in this case our collection store and the |
| 5756 | associated system stores. */ |
| 5757 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5758 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5759 | Py_XDECREF(result); |
| 5760 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5761 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5762 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5763 | /* convert set to list */ |
| 5764 | if (result == NULL) { |
| 5765 | return NULL; |
| 5766 | } else { |
| 5767 | PyObject *lst = PySequence_List(result); |
| 5768 | Py_DECREF(result); |
| 5769 | return lst; |
| 5770 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5771 | } |
| 5772 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5773 | /*[clinic input] |
| 5774 | _ssl.enum_crls |
| 5775 | store_name: str |
| 5776 | |
| 5777 | Retrieve CRLs from Windows' cert store. |
| 5778 | |
| 5779 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5780 | more cert storages, too. The function returns a list of (bytes, |
| 5781 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5782 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5783 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5784 | |
| 5785 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5786 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5787 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5788 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5789 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5790 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5791 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5792 | PyObject *result = NULL; |
| 5793 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5794 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5795 | if (result == NULL) { |
| 5796 | return NULL; |
| 5797 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5798 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5799 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5800 | Py_DECREF(result); |
| 5801 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5802 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5803 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5804 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5805 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5806 | pCrlCtx->cbCrlEncoded); |
| 5807 | if (!crl) { |
| 5808 | Py_CLEAR(result); |
| 5809 | break; |
| 5810 | } |
| 5811 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5812 | Py_CLEAR(result); |
| 5813 | break; |
| 5814 | } |
| 5815 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5816 | Py_CLEAR(result); |
| 5817 | break; |
| 5818 | } |
| 5819 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5820 | crl = NULL; |
| 5821 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5822 | enc = NULL; |
| 5823 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5824 | if (PySet_Add(result, tup) == -1) { |
| 5825 | Py_CLEAR(result); |
| 5826 | Py_CLEAR(tup); |
| 5827 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5828 | } |
| 5829 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5830 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5831 | if (pCrlCtx) { |
| 5832 | /* loop ended with an error, need to clean up context manually */ |
| 5833 | CertFreeCRLContext(pCrlCtx); |
| 5834 | } |
| 5835 | |
| 5836 | /* In error cases cert, enc and tup may not be NULL */ |
| 5837 | Py_XDECREF(crl); |
| 5838 | Py_XDECREF(enc); |
| 5839 | Py_XDECREF(tup); |
| 5840 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5841 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5842 | associated with the store, in this case our collection store and the |
| 5843 | associated system stores. */ |
| 5844 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5845 | /* This error case might shadow another exception.*/ |
| 5846 | Py_XDECREF(result); |
| 5847 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5848 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5849 | /* convert set to list */ |
| 5850 | if (result == NULL) { |
| 5851 | return NULL; |
| 5852 | } else { |
| 5853 | PyObject *lst = PySequence_List(result); |
| 5854 | Py_DECREF(result); |
| 5855 | return lst; |
| 5856 | } |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5857 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5858 | |
| 5859 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5860 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5861 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5862 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5863 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5864 | _SSL_RAND_ADD_METHODDEF |
| 5865 | _SSL_RAND_BYTES_METHODDEF |
| 5866 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5867 | _SSL_RAND_EGD_METHODDEF |
| 5868 | _SSL_RAND_STATUS_METHODDEF |
| 5869 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5870 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5871 | _SSL_ENUM_CRLS_METHODDEF |
| 5872 | _SSL_TXT2OBJ_METHODDEF |
| 5873 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5874 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5875 | }; |
| 5876 | |
| 5877 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5878 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5879 | |
| 5880 | /* an implementation of OpenSSL threading operations in terms |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5881 | * of the Python C thread library |
| 5882 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5883 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5884 | |
| 5885 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5886 | |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5887 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5888 | /* use new CRYPTO_THREADID API. */ |
| 5889 | static void |
| 5890 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5891 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5892 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5893 | } |
| 5894 | #else |
| 5895 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5896 | static unsigned long |
| 5897 | _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5898 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5899 | } |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5900 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5901 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5902 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5903 | (int mode, int n, const char *file, int line) { |
| 5904 | /* this function is needed to perform locking on shared data |
| 5905 | structures. (Note that OpenSSL uses a number of global data |
| 5906 | structures that will be implicitly shared whenever multiple |
| 5907 | threads use OpenSSL.) Multi-threaded applications will |
| 5908 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5909 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5910 | locking_function() must be able to handle up to |
| 5911 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5912 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5913 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5914 | file and line are the file number of the function setting the |
| 5915 | lock. They can be useful for debugging. |
| 5916 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5917 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5918 | if ((_ssl_locks == NULL) || |
| 5919 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5920 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5921 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5922 | if (mode & CRYPTO_LOCK) { |
| 5923 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5924 | } else { |
| 5925 | PyThread_release_lock(_ssl_locks[n]); |
| 5926 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | static int _setup_ssl_threads(void) { |
| 5930 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5931 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5932 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5933 | if (_ssl_locks == NULL) { |
| 5934 | _ssl_locks_count = CRYPTO_num_locks(); |
Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5935 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5936 | sizeof(PyThread_type_lock)); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5937 | if (_ssl_locks == NULL) { |
| 5938 | PyErr_NoMemory(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5939 | return 0; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5940 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5941 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5942 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5943 | if (_ssl_locks[i] == NULL) { |
| 5944 | unsigned int j; |
| 5945 | for (j = 0; j < i; j++) { |
| 5946 | PyThread_free_lock(_ssl_locks[j]); |
| 5947 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5948 | PyMem_Free(_ssl_locks); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5949 | return 0; |
| 5950 | } |
| 5951 | } |
| 5952 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5953 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5954 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5955 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5956 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5957 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5958 | } |
| 5959 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5962 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5964 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5965 | "Implementation module for SSL socket operations. See the socket module\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5966 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5967 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5968 | |
| 5969 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5970 | PyModuleDef_HEAD_INIT, |
| 5971 | "_ssl", |
| 5972 | module_doc, |
| 5973 | -1, |
| 5974 | PySSL_methods, |
| 5975 | NULL, |
| 5976 | NULL, |
| 5977 | NULL, |
| 5978 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5979 | }; |
| 5980 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5981 | |
| 5982 | static void |
| 5983 | parse_openssl_version(unsigned long libver, |
| 5984 | unsigned int *major, unsigned int *minor, |
| 5985 | unsigned int *fix, unsigned int *patch, |
| 5986 | unsigned int *status) |
| 5987 | { |
| 5988 | *status = libver & 0xF; |
| 5989 | libver >>= 4; |
| 5990 | *patch = libver & 0xFF; |
| 5991 | libver >>= 8; |
| 5992 | *fix = libver & 0xFF; |
| 5993 | libver >>= 8; |
| 5994 | *minor = libver & 0xFF; |
| 5995 | libver >>= 8; |
| 5996 | *major = libver & 0xFF; |
| 5997 | } |
| 5998 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5999 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6000 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6001 | { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6002 | PyObject *m, *d, *r, *bases; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6003 | unsigned long libver; |
| 6004 | unsigned int major, minor, fix, patch, status; |
| 6005 | PySocketModule_APIObject *socket_api; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6006 | struct py_ssl_error_code *errcode; |
| 6007 | struct py_ssl_library_code *libcode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6008 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 6009 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 6010 | return NULL; |
| 6011 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6012 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 6013 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) |
| 6014 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6015 | if (PyType_Ready(&PySSLSession_Type) < 0) |
| 6016 | return NULL; |
| 6017 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6018 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6019 | m = PyModule_Create(&_sslmodule); |
| 6020 | if (m == NULL) |
| 6021 | return NULL; |
| 6022 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6023 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6024 | /* Load _socket module and its C API */ |
| 6025 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 6026 | if (!socket_api) |
| 6027 | return NULL; |
| 6028 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6029 | |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 6030 | #ifndef OPENSSL_VERSION_1_1 |
| 6031 | /* Load all algorithms and initialize cpuid */ |
| 6032 | OPENSSL_add_all_algorithms_noconf(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6033 | /* Init OpenSSL */ |
| 6034 | SSL_load_error_strings(); |
| 6035 | SSL_library_init(); |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 6036 | #endif |
| 6037 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6038 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6039 | /* note that this will start threading if not already started */ |
| 6040 | if (!_setup_ssl_threads()) { |
| 6041 | return NULL; |
| 6042 | } |
Christian Heimes | c087a26 | 2020-05-15 20:55:25 +0200 | [diff] [blame] | 6043 | #elif OPENSSL_VERSION_1_1 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6044 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 6045 | _ssl_locks_count++; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 6046 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6047 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6048 | /* Add symbols to module dict */ |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6049 | sslerror_type_slots[0].pfunc = PyExc_OSError; |
| 6050 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6051 | if (PySSLErrorObject == NULL) |
| 6052 | return NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6053 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6054 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 6055 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); |
| 6056 | if (bases == NULL) |
| 6057 | return NULL; |
| 6058 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( |
| 6059 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, |
| 6060 | bases, NULL); |
| 6061 | Py_DECREF(bases); |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6062 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 6063 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 6064 | PySSLErrorObject, NULL); |
| 6065 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 6066 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 6067 | PySSLErrorObject, NULL); |
| 6068 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 6069 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 6070 | PySSLErrorObject, NULL); |
| 6071 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 6072 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 6073 | PySSLErrorObject, NULL); |
| 6074 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 6075 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 6076 | PySSLErrorObject, NULL); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6077 | if (PySSLCertVerificationErrorObject == NULL |
| 6078 | || PySSLZeroReturnErrorObject == NULL |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6079 | || PySSLWantReadErrorObject == NULL |
| 6080 | || PySSLWantWriteErrorObject == NULL |
| 6081 | || PySSLSyscallErrorObject == NULL |
| 6082 | || PySSLEOFErrorObject == NULL) |
| 6083 | return NULL; |
| 6084 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6085 | || PyDict_SetItemString(d, "SSLCertVerificationError", |
| 6086 | PySSLCertVerificationErrorObject) != 0 |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6087 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 6088 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 6089 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 6090 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 6091 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6092 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 6093 | if (PyDict_SetItemString(d, "_SSLContext", |
| 6094 | (PyObject *)&PySSLContext_Type) != 0) |
| 6095 | return NULL; |
| 6096 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 6097 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6098 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 6099 | if (PyDict_SetItemString(d, "MemoryBIO", |
| 6100 | (PyObject *)&PySSLMemoryBIO_Type) != 0) |
| 6101 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6102 | if (PyDict_SetItemString(d, "SSLSession", |
| 6103 | (PyObject *)&PySSLSession_Type) != 0) |
| 6104 | return NULL; |
| 6105 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 6106 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 6107 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 6108 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6109 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 6110 | PY_SSL_ERROR_ZERO_RETURN); |
| 6111 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 6112 | PY_SSL_ERROR_WANT_READ); |
| 6113 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 6114 | PY_SSL_ERROR_WANT_WRITE); |
| 6115 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 6116 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 6117 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 6118 | PY_SSL_ERROR_SYSCALL); |
| 6119 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 6120 | PY_SSL_ERROR_SSL); |
| 6121 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 6122 | PY_SSL_ERROR_WANT_CONNECT); |
| 6123 | /* non ssl.h errorcodes */ |
| 6124 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 6125 | PY_SSL_ERROR_EOF); |
| 6126 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 6127 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 6128 | /* cert requirements */ |
| 6129 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 6130 | PY_SSL_CERT_NONE); |
| 6131 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 6132 | PY_SSL_CERT_OPTIONAL); |
| 6133 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 6134 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 6135 | /* CRL verification for verification_flags */ |
| 6136 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 6137 | 0); |
| 6138 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 6139 | X509_V_FLAG_CRL_CHECK); |
| 6140 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 6141 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6142 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 6143 | X509_V_FLAG_X509_STRICT); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 6144 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 6145 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 6146 | X509_V_FLAG_TRUSTED_FIRST); |
| 6147 | #endif |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 6148 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6149 | /* Alert Descriptions from ssl.h */ |
| 6150 | /* note RESERVED constants no longer intended for use have been removed */ |
| 6151 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 6152 | |
| 6153 | #define ADD_AD_CONSTANT(s) \ |
| 6154 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 6155 | SSL_AD_##s) |
| 6156 | |
| 6157 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 6158 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 6159 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 6160 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 6161 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 6162 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 6163 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 6164 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 6165 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 6166 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 6167 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 6168 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 6169 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 6170 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 6171 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 6172 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 6173 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 6174 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 6175 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 6176 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 6177 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6178 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 6179 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 6180 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 6181 | #endif |
| 6182 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 6183 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 6184 | #endif |
| 6185 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 6186 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 6187 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6188 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 6189 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 6190 | #endif |
| 6191 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 6192 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 6193 | #endif |
| 6194 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 6195 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 6196 | #endif |
| 6197 | |
| 6198 | #undef ADD_AD_CONSTANT |
| 6199 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6200 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6201 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6202 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 6203 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6204 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6205 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6206 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 6207 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6208 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6209 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6210 | PY_SSL_VERSION_TLS); |
| 6211 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 6212 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 6213 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 6214 | PY_SSL_VERSION_TLS_CLIENT); |
| 6215 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 6216 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6217 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 6218 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6219 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 6220 | PY_SSL_VERSION_TLS1_1); |
| 6221 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 6222 | PY_SSL_VERSION_TLS1_2); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6223 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6224 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 6225 | PyModule_AddIntConstant(m, "OP_ALL", |
| 6226 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6227 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 6228 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 6229 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6230 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 6231 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6232 | #ifdef SSL_OP_NO_TLSv1_3 |
| 6233 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 6234 | #else |
| 6235 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 6236 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 6237 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 6238 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 6239 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6240 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6241 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 6242 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6243 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 6244 | #ifdef SSL_OP_NO_COMPRESSION |
| 6245 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 6246 | SSL_OP_NO_COMPRESSION); |
| 6247 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 6248 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 6249 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 6250 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 6251 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 6252 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 6253 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 6254 | SSL_OP_NO_RENEGOTIATION); |
| 6255 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6256 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 6257 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 6258 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 6259 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 6260 | #endif |
| 6261 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 6262 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 6263 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 6264 | #endif |
| 6265 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 6266 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 6267 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 6268 | #endif |
| 6269 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 6270 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 6271 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 6272 | #endif |
| 6273 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 6274 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 6275 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 6276 | #endif |
| 6277 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 6278 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 6279 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 6280 | #endif |
| 6281 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6282 | /* protocol versions */ |
| 6283 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 6284 | PY_PROTO_MINIMUM_SUPPORTED); |
| 6285 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 6286 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 6287 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 6288 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 6289 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 6290 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 6291 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 6292 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6293 | #define addbool(m, key, value) \ |
| 6294 | do { \ |
| 6295 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 6296 | Py_INCREF(bool_obj); \ |
| 6297 | PyModule_AddObject((m), (key), bool_obj); \ |
| 6298 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6299 | |
| 6300 | #if HAVE_SNI |
| 6301 | addbool(m, "HAS_SNI", 1); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6302 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6303 | addbool(m, "HAS_SNI", 0); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6304 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6305 | |
| 6306 | addbool(m, "HAS_TLS_UNIQUE", 1); |
| 6307 | |
| 6308 | #ifndef OPENSSL_NO_ECDH |
| 6309 | addbool(m, "HAS_ECDH", 1); |
| 6310 | #else |
| 6311 | addbool(m, "HAS_ECDH", 0); |
| 6312 | #endif |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6313 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6314 | #if HAVE_NPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6315 | addbool(m, "HAS_NPN", 1); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6316 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6317 | addbool(m, "HAS_NPN", 0); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6318 | #endif |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6319 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6320 | #if HAVE_ALPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6321 | addbool(m, "HAS_ALPN", 1); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6322 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6323 | addbool(m, "HAS_ALPN", 0); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6324 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6325 | |
| 6326 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 6327 | addbool(m, "HAS_SSLv2", 1); |
| 6328 | #else |
| 6329 | addbool(m, "HAS_SSLv2", 0); |
| 6330 | #endif |
| 6331 | |
| 6332 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 6333 | addbool(m, "HAS_SSLv3", 1); |
| 6334 | #else |
| 6335 | addbool(m, "HAS_SSLv3", 0); |
| 6336 | #endif |
| 6337 | |
| 6338 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 6339 | addbool(m, "HAS_TLSv1", 1); |
| 6340 | #else |
| 6341 | addbool(m, "HAS_TLSv1", 0); |
| 6342 | #endif |
| 6343 | |
| 6344 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 6345 | addbool(m, "HAS_TLSv1_1", 1); |
| 6346 | #else |
| 6347 | addbool(m, "HAS_TLSv1_1", 0); |
| 6348 | #endif |
| 6349 | |
| 6350 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 6351 | addbool(m, "HAS_TLSv1_2", 1); |
| 6352 | #else |
| 6353 | addbool(m, "HAS_TLSv1_2", 0); |
| 6354 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6355 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6356 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6357 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6358 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6359 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6360 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6361 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6362 | /* Mappings for error codes */ |
| 6363 | err_codes_to_names = PyDict_New(); |
| 6364 | err_names_to_codes = PyDict_New(); |
| 6365 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 6366 | return NULL; |
| 6367 | errcode = error_codes; |
| 6368 | while (errcode->mnemonic != NULL) { |
| 6369 | PyObject *mnemo, *key; |
| 6370 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 6371 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 6372 | if (mnemo == NULL || key == NULL) |
| 6373 | return NULL; |
| 6374 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 6375 | return NULL; |
| 6376 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 6377 | return NULL; |
| 6378 | Py_DECREF(key); |
| 6379 | Py_DECREF(mnemo); |
| 6380 | errcode++; |
| 6381 | } |
| 6382 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 6383 | return NULL; |
| 6384 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 6385 | return NULL; |
| 6386 | |
| 6387 | lib_codes_to_names = PyDict_New(); |
| 6388 | if (lib_codes_to_names == NULL) |
| 6389 | return NULL; |
| 6390 | libcode = library_codes; |
| 6391 | while (libcode->library != NULL) { |
| 6392 | PyObject *mnemo, *key; |
| 6393 | key = PyLong_FromLong(libcode->code); |
| 6394 | mnemo = PyUnicode_FromString(libcode->library); |
| 6395 | if (key == NULL || mnemo == NULL) |
| 6396 | return NULL; |
| 6397 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 6398 | return NULL; |
| 6399 | Py_DECREF(key); |
| 6400 | Py_DECREF(mnemo); |
| 6401 | libcode++; |
| 6402 | } |
| 6403 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 6404 | return NULL; |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 6405 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6406 | /* OpenSSL version */ |
| 6407 | /* SSLeay() gives us the version of the library linked against, |
| 6408 | which could be different from the headers version. |
| 6409 | */ |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 6410 | libver = OpenSSL_version_num(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6411 | r = PyLong_FromUnsignedLong(libver); |
| 6412 | if (r == NULL) |
| 6413 | return NULL; |
| 6414 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6415 | return NULL; |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6416 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6417 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6418 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6419 | return NULL; |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 6420 | r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6421 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6422 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6423 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6424 | libver = OPENSSL_VERSION_NUMBER; |
| 6425 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6426 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6427 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6428 | return NULL; |
| 6429 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6430 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6431 | } |