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 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 490 | static PyTypeObject *PySSLContext_Type; |
| 491 | static PyTypeObject *PySSLSocket_Type; |
| 492 | static PyTypeObject *PySSLMemoryBIO_Type; |
| 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 |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 511 | class _ssl._SSLContext "PySSLContext *" "PySSLContext_Type" |
| 512 | class _ssl._SSLSocket "PySSLSocket *" "PySSLSocket_Type" |
| 513 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "PySSLMemoryBIO_Type" |
| 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 | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 516 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=cc4883756da17954]*/ |
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 *); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [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[] = { |
Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 590 | {Py_tp_doc, (void*)SSLError_doc}, |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 591 | {Py_tp_str, SSLError_str}, |
| 592 | {0, 0}, |
| 593 | }; |
| 594 | |
| 595 | static PyType_Spec sslerror_type_spec = { |
| 596 | "ssl.SSLError", |
| 597 | sizeof(PyOSErrorObject), |
| 598 | 0, |
| 599 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 600 | sslerror_type_slots |
| 601 | }; |
| 602 | |
| 603 | static void |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 604 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 605 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 606 | { |
| 607 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 608 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 609 | PyObject *init_value, *msg, *key; |
| 610 | _Py_IDENTIFIER(reason); |
| 611 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 612 | _Py_IDENTIFIER(verify_message); |
| 613 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 614 | |
| 615 | if (errcode != 0) { |
| 616 | int lib, reason; |
| 617 | |
| 618 | lib = ERR_GET_LIB(errcode); |
| 619 | reason = ERR_GET_REASON(errcode); |
| 620 | key = Py_BuildValue("ii", lib, reason); |
| 621 | if (key == NULL) |
| 622 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 623 | reason_obj = PyDict_GetItemWithError(err_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 624 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 625 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 626 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 627 | } |
| 628 | key = PyLong_FromLong(lib); |
| 629 | if (key == NULL) |
| 630 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 631 | lib_obj = PyDict_GetItemWithError(lib_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 632 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 633 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 634 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 635 | } |
| 636 | if (errstr == NULL) |
| 637 | errstr = ERR_reason_error_string(errcode); |
| 638 | } |
| 639 | if (errstr == NULL) |
| 640 | errstr = "unknown error"; |
| 641 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 642 | /* verify code for cert validation error */ |
| 643 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 644 | const char *verify_str = NULL; |
| 645 | long verify_code; |
| 646 | |
| 647 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 648 | verify_code_obj = PyLong_FromLong(verify_code); |
| 649 | if (verify_code_obj == NULL) { |
| 650 | goto fail; |
| 651 | } |
| 652 | |
| 653 | switch (verify_code) { |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 654 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 655 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 656 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 657 | verify_obj = PyUnicode_FromFormat( |
| 658 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 659 | sslsock->server_hostname |
| 660 | ); |
| 661 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 662 | #endif |
| 663 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 664 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 665 | verify_obj = PyUnicode_FromFormat( |
| 666 | "IP address mismatch, certificate is not valid for '%S'.", |
| 667 | sslsock->server_hostname |
| 668 | ); |
| 669 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 670 | #endif |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 671 | default: |
| 672 | verify_str = X509_verify_cert_error_string(verify_code); |
| 673 | if (verify_str != NULL) { |
| 674 | verify_obj = PyUnicode_FromString(verify_str); |
| 675 | } else { |
| 676 | verify_obj = Py_None; |
| 677 | Py_INCREF(verify_obj); |
| 678 | } |
| 679 | break; |
| 680 | } |
| 681 | if (verify_obj == NULL) { |
| 682 | goto fail; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (verify_obj && reason_obj && lib_obj) |
| 687 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 688 | lib_obj, reason_obj, errstr, verify_obj, |
| 689 | lineno); |
| 690 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 691 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 692 | lib_obj, reason_obj, errstr, lineno); |
| 693 | else if (lib_obj) |
| 694 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 695 | lib_obj, errstr, lineno); |
| 696 | else |
| 697 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 698 | if (msg == NULL) |
| 699 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 700 | |
Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 701 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 702 | if (init_value == NULL) |
| 703 | goto fail; |
| 704 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 705 | err_value = PyObject_CallObject(type, init_value); |
| 706 | Py_DECREF(init_value); |
| 707 | if (err_value == NULL) |
| 708 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 709 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 710 | if (reason_obj == NULL) |
| 711 | reason_obj = Py_None; |
| 712 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 713 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 714 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 715 | if (lib_obj == NULL) |
| 716 | lib_obj = Py_None; |
| 717 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 718 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 719 | |
| 720 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 721 | /* Only set verify code / message for SSLCertVerificationError */ |
| 722 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 723 | verify_code_obj)) |
| 724 | goto fail; |
| 725 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 726 | goto fail; |
| 727 | } |
| 728 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 729 | PyErr_SetObject(type, err_value); |
| 730 | fail: |
| 731 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 732 | Py_XDECREF(verify_code_obj); |
| 733 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 734 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 735 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 736 | static int |
| 737 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 738 | if (sslsock->exc_type == NULL) |
| 739 | return 0; |
| 740 | |
| 741 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 742 | sslsock->exc_type = NULL; |
| 743 | sslsock->exc_value = NULL; |
| 744 | sslsock->exc_tb = NULL; |
| 745 | return -1; |
| 746 | } |
| 747 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 748 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 749 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 750 | { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 751 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 752 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 753 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 754 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 755 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 757 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 758 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 759 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 760 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 761 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 762 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 763 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 764 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 765 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 766 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 767 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 768 | break; |
| 769 | case SSL_ERROR_WANT_READ: |
| 770 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 771 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 772 | p = PY_SSL_ERROR_WANT_READ; |
| 773 | break; |
| 774 | case SSL_ERROR_WANT_WRITE: |
| 775 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 776 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 777 | errstr = "The operation did not complete (write)"; |
| 778 | break; |
| 779 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 780 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 781 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 782 | break; |
| 783 | case SSL_ERROR_WANT_CONNECT: |
| 784 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 785 | errstr = "The operation did not complete (connect)"; |
| 786 | break; |
| 787 | case SSL_ERROR_SYSCALL: |
| 788 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 789 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 790 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 791 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 792 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 793 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 794 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 795 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 796 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 797 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 798 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 799 | if (err.ws) { |
| 800 | return PyErr_SetFromWindowsErr(err.ws); |
| 801 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 802 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 803 | if (err.c) { |
| 804 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 805 | return PyErr_SetFromErrno(PyExc_OSError); |
| 806 | } |
Dima Tisnek | 495bd03 | 2020-08-16 02:01:19 +0900 | [diff] [blame] | 807 | else { |
| 808 | p = PY_SSL_ERROR_EOF; |
| 809 | type = PySSLEOFErrorObject; |
| 810 | errstr = "EOF occurred in violation of protocol"; |
| 811 | } |
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__); |
Zackery Spytz | c32f297 | 2020-10-25 12:02:30 -0600 | [diff] [blame] | 901 | goto error; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | if (self->ctx->check_hostname) { |
| 905 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 906 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 907 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 908 | strlen(server_hostname))) { |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 909 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 910 | goto error; |
| 911 | } |
| 912 | } else { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 913 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip), |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 914 | ASN1_STRING_length(ip))) { |
| 915 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 916 | goto error; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | retval = 0; |
| 921 | error: |
| 922 | if (ip != NULL) { |
| 923 | ASN1_OCTET_STRING_free(ip); |
| 924 | } |
| 925 | return retval; |
| 926 | } |
| 927 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 928 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 929 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 930 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 931 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 932 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 933 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 934 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 935 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 936 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 937 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 938 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 939 | self = PyObject_New(PySSLSocket, PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 940 | if (self == NULL) |
| 941 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 943 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 944 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 945 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 946 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 947 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 948 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 949 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 950 | self->err = err; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 951 | self->exc_type = NULL; |
| 952 | self->exc_value = NULL; |
| 953 | self->exc_tb = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 954 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 955 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 956 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 958 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 959 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 960 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 961 | if (self->ssl == NULL) { |
| 962 | Py_DECREF(self); |
| 963 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 964 | return NULL; |
| 965 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 966 | SSL_set_app_data(self->ssl, self); |
| 967 | if (sock) { |
| 968 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 969 | } else { |
| 970 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 971 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 972 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 973 | BIO_up_ref(inbio->bio); |
| 974 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 975 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 976 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 977 | SSL_set_mode(self->ssl, |
| 978 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 979 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 980 | #ifdef TLS1_3_VERSION |
| 981 | if (sslctx->post_handshake_auth == 1) { |
| 982 | if (socket_type == PY_SSL_SERVER) { |
| 983 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 984 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 985 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 986 | int mode = SSL_get_verify_mode(self->ssl); |
| 987 | if (mode & SSL_VERIFY_PEER) { |
| 988 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 989 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 990 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 991 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 992 | } |
| 993 | } else { |
| 994 | /* client socket */ |
| 995 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 996 | } |
| 997 | } |
| 998 | #endif |
| 999 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 1000 | if (server_hostname != NULL) { |
| 1001 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 1002 | Py_DECREF(self); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1006 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 1007 | * to non-blocking mode (blocking is the default) |
| 1008 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1009 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1010 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 1011 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 1012 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1014 | PySSL_BEGIN_ALLOW_THREADS |
| 1015 | if (socket_type == PY_SSL_CLIENT) |
| 1016 | SSL_set_connect_state(self->ssl); |
| 1017 | else |
| 1018 | SSL_set_accept_state(self->ssl); |
| 1019 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1021 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1022 | if (sock != NULL) { |
| 1023 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 1024 | if (self->Socket == NULL) { |
| 1025 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1026 | return NULL; |
| 1027 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 1028 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1029 | if (owner && owner != Py_None) { |
| 1030 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 1031 | Py_DECREF(self); |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | } |
| 1035 | if (session && session != Py_None) { |
| 1036 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 1037 | Py_DECREF(self); |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1041 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1044 | /* SSL object methods */ |
| 1045 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1046 | /*[clinic input] |
| 1047 | _ssl._SSLSocket.do_handshake |
| 1048 | [clinic start generated code]*/ |
| 1049 | |
| 1050 | static PyObject * |
| 1051 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 1052 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1053 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1054 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1055 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1056 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1057 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1058 | _PyTime_t timeout, deadline = 0; |
| 1059 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1061 | if (sock) { |
| 1062 | if (((PyObject*)sock) == Py_None) { |
| 1063 | _setSSLError("Underlying socket connection gone", |
| 1064 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | Py_INCREF(sock); |
| 1068 | |
| 1069 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1070 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1071 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1072 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1073 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1074 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1075 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 1076 | has_timeout = (timeout > 0); |
| 1077 | if (has_timeout) |
| 1078 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 1079 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1080 | /* Actually negotiate SSL connection */ |
| 1081 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1082 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1083 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1084 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1085 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1086 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1087 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1088 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1089 | if (PyErr_CheckSignals()) |
| 1090 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1091 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1092 | if (has_timeout) |
| 1093 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 1094 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1095 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1096 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1097 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1098 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1099 | } else { |
| 1100 | sockstate = SOCKET_OPERATION_OK; |
| 1101 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1102 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1103 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 1104 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1105 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1106 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1107 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1108 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1109 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1110 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1111 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1112 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1113 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1114 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1115 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1116 | break; |
| 1117 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1118 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 1119 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1120 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1121 | if (ret < 1) |
| 1122 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1123 | if (PySSL_ChainExceptions(self) < 0) |
| 1124 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1125 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1126 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1127 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1128 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1129 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1132 | static PyObject * |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1133 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 1134 | { |
| 1135 | char buf[X509_NAME_MAXLEN]; |
| 1136 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1137 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1138 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1139 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1140 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1141 | if (buflen < 0) { |
| 1142 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1143 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1144 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1145 | /* initial buffer is too small for oid + terminating null byte */ |
| 1146 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1147 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1148 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1149 | /* allocate len + 1 for terminating NULL byte */ |
| 1150 | namebuf = PyMem_Malloc(buflen + 1); |
| 1151 | if (namebuf == NULL) { |
| 1152 | PyErr_NoMemory(); |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1156 | if (buflen < 0) { |
| 1157 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1158 | goto done; |
| 1159 | } |
| 1160 | } |
| 1161 | if (!buflen && no_name) { |
| 1162 | Py_INCREF(Py_None); |
| 1163 | name_obj = Py_None; |
| 1164 | } |
| 1165 | else { |
| 1166 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1167 | } |
| 1168 | |
| 1169 | done: |
| 1170 | if (buf != namebuf) { |
| 1171 | PyMem_Free(namebuf); |
| 1172 | } |
| 1173 | return name_obj; |
| 1174 | } |
| 1175 | |
| 1176 | static PyObject * |
| 1177 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 1178 | { |
| 1179 | Py_ssize_t buflen; |
| 1180 | unsigned char *valuebuf = NULL; |
| 1181 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1183 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1184 | if (buflen < 0) { |
| 1185 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1186 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1187 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1188 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1189 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1190 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1194 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1195 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1196 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1197 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1198 | PyObject *rdnt; |
| 1199 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1200 | int entry_count = X509_NAME_entry_count(xname); |
| 1201 | X509_NAME_ENTRY *entry; |
| 1202 | ASN1_OBJECT *name; |
| 1203 | ASN1_STRING *value; |
| 1204 | int index_counter; |
| 1205 | int rdn_level = -1; |
| 1206 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1208 | dn = PyList_New(0); |
| 1209 | if (dn == NULL) |
| 1210 | return NULL; |
| 1211 | /* now create another tuple to hold the top-level RDN */ |
| 1212 | rdn = PyList_New(0); |
| 1213 | if (rdn == NULL) |
| 1214 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1216 | for (index_counter = 0; |
| 1217 | index_counter < entry_count; |
| 1218 | index_counter++) |
| 1219 | { |
| 1220 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1222 | /* check to see if we've gotten to a new RDN */ |
| 1223 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1224 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1225 | /* yes, new RDN */ |
| 1226 | /* add old RDN to DN */ |
| 1227 | rdnt = PyList_AsTuple(rdn); |
| 1228 | Py_DECREF(rdn); |
| 1229 | if (rdnt == NULL) |
| 1230 | goto fail0; |
| 1231 | retcode = PyList_Append(dn, rdnt); |
| 1232 | Py_DECREF(rdnt); |
| 1233 | if (retcode < 0) |
| 1234 | goto fail0; |
| 1235 | /* create new RDN */ |
| 1236 | rdn = PyList_New(0); |
| 1237 | if (rdn == NULL) |
| 1238 | goto fail0; |
| 1239 | } |
| 1240 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1241 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1243 | /* now add this attribute to the current RDN */ |
| 1244 | name = X509_NAME_ENTRY_get_object(entry); |
| 1245 | value = X509_NAME_ENTRY_get_data(entry); |
| 1246 | attr = _create_tuple_for_attribute(name, value); |
| 1247 | /* |
| 1248 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1249 | entry->set, |
| 1250 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1251 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1252 | */ |
| 1253 | if (attr == NULL) |
| 1254 | goto fail1; |
| 1255 | retcode = PyList_Append(rdn, attr); |
| 1256 | Py_DECREF(attr); |
| 1257 | if (retcode < 0) |
| 1258 | goto fail1; |
| 1259 | } |
| 1260 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1261 | if (rdn != NULL) { |
| 1262 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1263 | rdnt = PyList_AsTuple(rdn); |
| 1264 | Py_DECREF(rdn); |
| 1265 | if (rdnt == NULL) |
| 1266 | goto fail0; |
| 1267 | retcode = PyList_Append(dn, rdnt); |
| 1268 | Py_DECREF(rdnt); |
| 1269 | if (retcode < 0) |
| 1270 | goto fail0; |
| 1271 | } |
| 1272 | else { |
| 1273 | Py_DECREF(rdn); |
| 1274 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1275 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1276 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1277 | /* convert list to tuple */ |
| 1278 | rdnt = PyList_AsTuple(dn); |
| 1279 | Py_DECREF(dn); |
| 1280 | if (rdnt == NULL) |
| 1281 | return NULL; |
| 1282 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1283 | |
| 1284 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1285 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1286 | |
| 1287 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1288 | Py_XDECREF(dn); |
| 1289 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | static PyObject * |
| 1293 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1294 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1295 | /* this code follows the procedure outlined in |
| 1296 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1297 | function to extract the STACK_OF(GENERAL_NAME), |
| 1298 | then iterates through the stack to add the |
| 1299 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1300 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1301 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1302 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1303 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1304 | GENERAL_NAMES *names = NULL; |
| 1305 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1306 | BIO *biobuf = NULL; |
| 1307 | char buf[2048]; |
| 1308 | char *vptr; |
| 1309 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1311 | if (certificate == NULL) |
| 1312 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1313 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1314 | /* get a memory buffer */ |
| 1315 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1316 | if (biobuf == NULL) { |
| 1317 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1318 | return NULL; |
| 1319 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1320 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1321 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1322 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1323 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1324 | if (peer_alt_names == Py_None) { |
| 1325 | peer_alt_names = PyList_New(0); |
| 1326 | if (peer_alt_names == NULL) |
| 1327 | goto fail; |
| 1328 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1330 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1331 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1332 | int gntype; |
| 1333 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1335 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1336 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1337 | switch (gntype) { |
| 1338 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1339 | /* we special-case DirName as a tuple of |
| 1340 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1341 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1342 | t = PyTuple_New(2); |
| 1343 | if (t == NULL) { |
| 1344 | goto fail; |
| 1345 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1346 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1347 | v = PyUnicode_FromString("DirName"); |
| 1348 | if (v == NULL) { |
| 1349 | Py_DECREF(t); |
| 1350 | goto fail; |
| 1351 | } |
| 1352 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1353 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1354 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1355 | if (v == NULL) { |
| 1356 | Py_DECREF(t); |
| 1357 | goto fail; |
| 1358 | } |
| 1359 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1360 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1361 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1362 | case GEN_EMAIL: |
| 1363 | case GEN_DNS: |
| 1364 | case GEN_URI: |
| 1365 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1366 | correctly, CVE-2013-4238 */ |
| 1367 | t = PyTuple_New(2); |
| 1368 | if (t == NULL) |
| 1369 | goto fail; |
| 1370 | switch (gntype) { |
| 1371 | case GEN_EMAIL: |
| 1372 | v = PyUnicode_FromString("email"); |
| 1373 | as = name->d.rfc822Name; |
| 1374 | break; |
| 1375 | case GEN_DNS: |
| 1376 | v = PyUnicode_FromString("DNS"); |
| 1377 | as = name->d.dNSName; |
| 1378 | break; |
| 1379 | case GEN_URI: |
| 1380 | v = PyUnicode_FromString("URI"); |
| 1381 | as = name->d.uniformResourceIdentifier; |
| 1382 | break; |
| 1383 | } |
| 1384 | if (v == NULL) { |
| 1385 | Py_DECREF(t); |
| 1386 | goto fail; |
| 1387 | } |
| 1388 | PyTuple_SET_ITEM(t, 0, v); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1389 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as), |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1390 | ASN1_STRING_length(as)); |
| 1391 | if (v == NULL) { |
| 1392 | Py_DECREF(t); |
| 1393 | goto fail; |
| 1394 | } |
| 1395 | PyTuple_SET_ITEM(t, 1, v); |
| 1396 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1397 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1398 | case GEN_RID: |
| 1399 | t = PyTuple_New(2); |
| 1400 | if (t == NULL) |
| 1401 | goto fail; |
| 1402 | |
| 1403 | v = PyUnicode_FromString("Registered ID"); |
| 1404 | if (v == NULL) { |
| 1405 | Py_DECREF(t); |
| 1406 | goto fail; |
| 1407 | } |
| 1408 | PyTuple_SET_ITEM(t, 0, v); |
| 1409 | |
| 1410 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1411 | if (len < 0) { |
| 1412 | Py_DECREF(t); |
| 1413 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1414 | goto fail; |
| 1415 | } else if (len >= (int)sizeof(buf)) { |
| 1416 | v = PyUnicode_FromString("<INVALID>"); |
| 1417 | } else { |
| 1418 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1419 | } |
| 1420 | if (v == NULL) { |
| 1421 | Py_DECREF(t); |
| 1422 | goto fail; |
| 1423 | } |
| 1424 | PyTuple_SET_ITEM(t, 1, v); |
| 1425 | break; |
| 1426 | |
Christian Heimes | 2b7de66 | 2019-12-07 17:59:36 +0100 | [diff] [blame] | 1427 | case GEN_IPADD: |
| 1428 | /* OpenSSL < 3.0.0 adds a trailing \n to IPv6. 3.0.0 removed |
| 1429 | * the trailing newline. Remove it in all versions |
| 1430 | */ |
| 1431 | t = PyTuple_New(2); |
| 1432 | if (t == NULL) |
| 1433 | goto fail; |
| 1434 | |
| 1435 | v = PyUnicode_FromString("IP Address"); |
| 1436 | if (v == NULL) { |
| 1437 | Py_DECREF(t); |
| 1438 | goto fail; |
| 1439 | } |
| 1440 | PyTuple_SET_ITEM(t, 0, v); |
| 1441 | |
| 1442 | if (name->d.ip->length == 4) { |
| 1443 | unsigned char *p = name->d.ip->data; |
| 1444 | v = PyUnicode_FromFormat( |
| 1445 | "%d.%d.%d.%d", |
| 1446 | p[0], p[1], p[2], p[3] |
| 1447 | ); |
| 1448 | } else if (name->d.ip->length == 16) { |
| 1449 | /* PyUnicode_FromFormat() does not support %X */ |
| 1450 | unsigned char *p = name->d.ip->data; |
| 1451 | len = sprintf( |
| 1452 | buf, |
| 1453 | "%X:%X:%X:%X:%X:%X:%X:%X", |
| 1454 | p[0] << 8 | p[1], |
| 1455 | p[2] << 8 | p[3], |
| 1456 | p[4] << 8 | p[5], |
| 1457 | p[6] << 8 | p[7], |
| 1458 | p[8] << 8 | p[9], |
| 1459 | p[10] << 8 | p[11], |
| 1460 | p[12] << 8 | p[13], |
| 1461 | p[14] << 8 | p[15] |
| 1462 | ); |
| 1463 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1464 | } else { |
| 1465 | v = PyUnicode_FromString("<invalid>"); |
| 1466 | } |
| 1467 | |
| 1468 | if (v == NULL) { |
| 1469 | Py_DECREF(t); |
| 1470 | goto fail; |
| 1471 | } |
| 1472 | PyTuple_SET_ITEM(t, 1, v); |
| 1473 | break; |
| 1474 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1475 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1476 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1477 | switch (gntype) { |
| 1478 | /* check for new general name type */ |
| 1479 | case GEN_OTHERNAME: |
| 1480 | case GEN_X400: |
| 1481 | case GEN_EDIPARTY: |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1482 | case GEN_RID: |
| 1483 | break; |
| 1484 | default: |
| 1485 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1486 | "Unknown general name type %d", |
| 1487 | gntype) == -1) { |
| 1488 | goto fail; |
| 1489 | } |
| 1490 | break; |
| 1491 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1492 | (void) BIO_reset(biobuf); |
| 1493 | GENERAL_NAME_print(biobuf, name); |
| 1494 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1495 | if (len < 0) { |
| 1496 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1497 | goto fail; |
| 1498 | } |
| 1499 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1500 | if (vptr == NULL) { |
| 1501 | PyErr_Format(PyExc_ValueError, |
| 1502 | "Invalid value %.200s", |
| 1503 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1504 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1505 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1506 | t = PyTuple_New(2); |
| 1507 | if (t == NULL) |
| 1508 | goto fail; |
| 1509 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1510 | if (v == NULL) { |
| 1511 | Py_DECREF(t); |
| 1512 | goto fail; |
| 1513 | } |
| 1514 | PyTuple_SET_ITEM(t, 0, v); |
| 1515 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1516 | (len - (vptr - buf + 1))); |
| 1517 | if (v == NULL) { |
| 1518 | Py_DECREF(t); |
| 1519 | goto fail; |
| 1520 | } |
| 1521 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1522 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1523 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1525 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1527 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1528 | Py_DECREF(t); |
| 1529 | goto fail; |
| 1530 | } |
| 1531 | Py_DECREF(t); |
| 1532 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1533 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1534 | } |
| 1535 | BIO_free(biobuf); |
| 1536 | if (peer_alt_names != Py_None) { |
| 1537 | v = PyList_AsTuple(peer_alt_names); |
| 1538 | Py_DECREF(peer_alt_names); |
| 1539 | return v; |
| 1540 | } else { |
| 1541 | return peer_alt_names; |
| 1542 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1543 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1544 | |
| 1545 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1546 | if (biobuf != NULL) |
| 1547 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1549 | if (peer_alt_names != Py_None) { |
| 1550 | Py_XDECREF(peer_alt_names); |
| 1551 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1552 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1553 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1557 | _get_aia_uri(X509 *certificate, int nid) { |
| 1558 | PyObject *lst = NULL, *ostr = NULL; |
| 1559 | int i, result; |
| 1560 | AUTHORITY_INFO_ACCESS *info; |
| 1561 | |
| 1562 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1563 | if (info == NULL) |
| 1564 | return Py_None; |
| 1565 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1566 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1567 | return Py_None; |
| 1568 | } |
| 1569 | |
| 1570 | if ((lst = PyList_New(0)) == NULL) { |
| 1571 | goto fail; |
| 1572 | } |
| 1573 | |
| 1574 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1575 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1576 | ASN1_IA5STRING *uri; |
| 1577 | |
| 1578 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1579 | (ad->location->type != GEN_URI)) { |
| 1580 | continue; |
| 1581 | } |
| 1582 | uri = ad->location->d.uniformResourceIdentifier; |
| 1583 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1584 | uri->length); |
| 1585 | if (ostr == NULL) { |
| 1586 | goto fail; |
| 1587 | } |
| 1588 | result = PyList_Append(lst, ostr); |
| 1589 | Py_DECREF(ostr); |
| 1590 | if (result < 0) { |
| 1591 | goto fail; |
| 1592 | } |
| 1593 | } |
| 1594 | AUTHORITY_INFO_ACCESS_free(info); |
| 1595 | |
| 1596 | /* convert to tuple or None */ |
| 1597 | if (PyList_Size(lst) == 0) { |
| 1598 | Py_DECREF(lst); |
| 1599 | return Py_None; |
| 1600 | } else { |
| 1601 | PyObject *tup; |
| 1602 | tup = PyList_AsTuple(lst); |
| 1603 | Py_DECREF(lst); |
| 1604 | return tup; |
| 1605 | } |
| 1606 | |
| 1607 | fail: |
| 1608 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1609 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1610 | return NULL; |
| 1611 | } |
| 1612 | |
| 1613 | static PyObject * |
| 1614 | _get_crl_dp(X509 *certificate) { |
| 1615 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1616 | int i, j; |
| 1617 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1618 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1619 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1620 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1621 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1622 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1623 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1624 | lst = PyList_New(0); |
| 1625 | if (lst == NULL) |
| 1626 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1627 | |
| 1628 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1629 | DIST_POINT *dp; |
| 1630 | STACK_OF(GENERAL_NAME) *gns; |
| 1631 | |
| 1632 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1633 | if (dp->distpoint == NULL) { |
| 1634 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1635 | continue; |
| 1636 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1637 | gns = dp->distpoint->name.fullname; |
| 1638 | |
| 1639 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1640 | GENERAL_NAME *gn; |
| 1641 | ASN1_IA5STRING *uri; |
| 1642 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1643 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1644 | |
| 1645 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1646 | if (gn->type != GEN_URI) { |
| 1647 | continue; |
| 1648 | } |
| 1649 | uri = gn->d.uniformResourceIdentifier; |
| 1650 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1651 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1652 | if (ouri == NULL) |
| 1653 | goto done; |
| 1654 | |
| 1655 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1656 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1657 | if (err < 0) |
| 1658 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1659 | } |
| 1660 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1661 | |
| 1662 | /* Convert to tuple. */ |
| 1663 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1664 | |
| 1665 | done: |
| 1666 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1667 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1668 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1672 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1673 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1674 | PyObject *retval = NULL; |
| 1675 | BIO *biobuf = NULL; |
| 1676 | PyObject *peer; |
| 1677 | PyObject *peer_alt_names = NULL; |
| 1678 | PyObject *issuer; |
| 1679 | PyObject *version; |
| 1680 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1681 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1682 | ASN1_INTEGER *serialNumber; |
| 1683 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1684 | int len, result; |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1685 | const ASN1_TIME *notBefore, *notAfter; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1686 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1687 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1688 | retval = PyDict_New(); |
| 1689 | if (retval == NULL) |
| 1690 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1691 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1692 | peer = _create_tuple_for_X509_NAME( |
| 1693 | X509_get_subject_name(certificate)); |
| 1694 | if (peer == NULL) |
| 1695 | goto fail0; |
| 1696 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1697 | Py_DECREF(peer); |
| 1698 | goto fail0; |
| 1699 | } |
| 1700 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1702 | issuer = _create_tuple_for_X509_NAME( |
| 1703 | X509_get_issuer_name(certificate)); |
| 1704 | if (issuer == NULL) |
| 1705 | goto fail0; |
| 1706 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1707 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1708 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1709 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1710 | Py_DECREF(issuer); |
| 1711 | |
| 1712 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1713 | if (version == NULL) |
| 1714 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1715 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1716 | Py_DECREF(version); |
| 1717 | goto fail0; |
| 1718 | } |
| 1719 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1721 | /* get a memory buffer */ |
| 1722 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1723 | if (biobuf == NULL) { |
| 1724 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1725 | goto fail0; |
| 1726 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1728 | (void) BIO_reset(biobuf); |
| 1729 | serialNumber = X509_get_serialNumber(certificate); |
| 1730 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1731 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1732 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1733 | if (len < 0) { |
| 1734 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1735 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1736 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1737 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1738 | if (sn_obj == NULL) |
| 1739 | goto fail1; |
| 1740 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1741 | Py_DECREF(sn_obj); |
| 1742 | goto fail1; |
| 1743 | } |
| 1744 | Py_DECREF(sn_obj); |
| 1745 | |
| 1746 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1747 | notBefore = X509_get0_notBefore(certificate); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1748 | ASN1_TIME_print(biobuf, notBefore); |
| 1749 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1750 | if (len < 0) { |
| 1751 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1752 | goto fail1; |
| 1753 | } |
| 1754 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1755 | if (pnotBefore == NULL) |
| 1756 | goto fail1; |
| 1757 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1758 | Py_DECREF(pnotBefore); |
| 1759 | goto fail1; |
| 1760 | } |
| 1761 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1763 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1764 | notAfter = X509_get0_notAfter(certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1765 | ASN1_TIME_print(biobuf, notAfter); |
| 1766 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1767 | if (len < 0) { |
| 1768 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1769 | goto fail1; |
| 1770 | } |
| 1771 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1772 | if (pnotAfter == NULL) |
| 1773 | goto fail1; |
| 1774 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1775 | Py_DECREF(pnotAfter); |
| 1776 | goto fail1; |
| 1777 | } |
| 1778 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1779 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1780 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1781 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1782 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1783 | if (peer_alt_names == NULL) |
| 1784 | goto fail1; |
| 1785 | else if (peer_alt_names != Py_None) { |
| 1786 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1787 | peer_alt_names) < 0) { |
| 1788 | Py_DECREF(peer_alt_names); |
| 1789 | goto fail1; |
| 1790 | } |
| 1791 | Py_DECREF(peer_alt_names); |
| 1792 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1793 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1794 | /* Authority Information Access: OCSP URIs */ |
| 1795 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1796 | if (obj == NULL) { |
| 1797 | goto fail1; |
| 1798 | } else if (obj != Py_None) { |
| 1799 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1800 | Py_DECREF(obj); |
| 1801 | if (result < 0) { |
| 1802 | goto fail1; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1807 | if (obj == NULL) { |
| 1808 | goto fail1; |
| 1809 | } else if (obj != Py_None) { |
| 1810 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1811 | Py_DECREF(obj); |
| 1812 | if (result < 0) { |
| 1813 | goto fail1; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | /* CDP (CRL distribution points) */ |
| 1818 | obj = _get_crl_dp(certificate); |
| 1819 | if (obj == NULL) { |
| 1820 | goto fail1; |
| 1821 | } else if (obj != Py_None) { |
| 1822 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1823 | Py_DECREF(obj); |
| 1824 | if (result < 0) { |
| 1825 | goto fail1; |
| 1826 | } |
| 1827 | } |
| 1828 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1829 | BIO_free(biobuf); |
| 1830 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1831 | |
| 1832 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1833 | if (biobuf != NULL) |
| 1834 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1835 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1836 | Py_XDECREF(retval); |
| 1837 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1838 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1839 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1840 | static PyObject * |
| 1841 | _certificate_to_der(X509 *certificate) |
| 1842 | { |
| 1843 | unsigned char *bytes_buf = NULL; |
| 1844 | int len; |
| 1845 | PyObject *retval; |
| 1846 | |
| 1847 | bytes_buf = NULL; |
| 1848 | len = i2d_X509(certificate, &bytes_buf); |
| 1849 | if (len < 0) { |
| 1850 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1851 | return NULL; |
| 1852 | } |
| 1853 | /* this is actually an immutable bytes sequence */ |
| 1854 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1855 | OPENSSL_free(bytes_buf); |
| 1856 | return retval; |
| 1857 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1858 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1859 | /*[clinic input] |
| 1860 | _ssl._test_decode_cert |
| 1861 | path: object(converter="PyUnicode_FSConverter") |
| 1862 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1863 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1864 | [clinic start generated code]*/ |
| 1865 | |
| 1866 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1867 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1868 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1869 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1870 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1871 | X509 *x=NULL; |
| 1872 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1874 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1875 | PyErr_SetString(PySSLErrorObject, |
| 1876 | "Can't malloc memory to read file"); |
| 1877 | goto fail0; |
| 1878 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1879 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1880 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1881 | PyErr_SetString(PySSLErrorObject, |
| 1882 | "Can't open file"); |
| 1883 | goto fail0; |
| 1884 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1885 | |
Alex Gaynor | 40dad95 | 2019-08-15 08:31:28 -0400 | [diff] [blame] | 1886 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1887 | if (x == NULL) { |
| 1888 | PyErr_SetString(PySSLErrorObject, |
| 1889 | "Error decoding PEM-encoded file"); |
| 1890 | goto fail0; |
| 1891 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1893 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1894 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1895 | |
| 1896 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1897 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1898 | if (cert != NULL) BIO_free(cert); |
| 1899 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1903 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1904 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1905 | der as binary_mode: bool = False |
| 1906 | / |
| 1907 | |
| 1908 | Returns the certificate for the peer. |
| 1909 | |
| 1910 | If no certificate was provided, returns None. If a certificate was |
| 1911 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1912 | returns a dict containing information about the peer certificate. |
| 1913 | |
| 1914 | If the optional argument is True, returns a DER-encoded copy of the |
| 1915 | peer certificate, or None if no certificate was provided. This will |
| 1916 | return the certificate even if it wasn't validated. |
| 1917 | [clinic start generated code]*/ |
| 1918 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1919 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1920 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1921 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1922 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1923 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1924 | X509 *peer_cert; |
| 1925 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1926 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1927 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1928 | PyErr_SetString(PyExc_ValueError, |
| 1929 | "handshake not done yet"); |
| 1930 | return NULL; |
| 1931 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1932 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1933 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1934 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1936 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1937 | /* return cert in DER-encoded format */ |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1938 | result = _certificate_to_der(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1939 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1940 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1941 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1942 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1943 | else |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1944 | result = _decode_certificate(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1945 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1946 | X509_free(peer_cert); |
| 1947 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1950 | static PyObject * |
| 1951 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1952 | { |
| 1953 | const char *cipher_name, *cipher_protocol; |
| 1954 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1955 | if (retval == NULL) |
| 1956 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1957 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1958 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1959 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1960 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1961 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1962 | } else { |
| 1963 | v = PyUnicode_FromString(cipher_name); |
| 1964 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1965 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1966 | PyTuple_SET_ITEM(retval, 0, v); |
| 1967 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1968 | |
| 1969 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1970 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1971 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1972 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1973 | } else { |
| 1974 | v = PyUnicode_FromString(cipher_protocol); |
| 1975 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1976 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1977 | PyTuple_SET_ITEM(retval, 1, v); |
| 1978 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1979 | |
| 1980 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1981 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1982 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1983 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1984 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1985 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1986 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1987 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1988 | Py_DECREF(retval); |
| 1989 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1992 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1993 | static PyObject * |
| 1994 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1995 | { |
| 1996 | const char *cipher_name, *cipher_protocol; |
| 1997 | |
| 1998 | unsigned long cipher_id; |
| 1999 | int alg_bits, strength_bits, len; |
| 2000 | char buf[512] = {0}; |
| 2001 | #if OPENSSL_VERSION_1_1 |
| 2002 | int aead, nid; |
| 2003 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 2004 | #endif |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* can be NULL */ |
| 2007 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 2008 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 2009 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 2010 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 2011 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 2012 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2013 | if (len > 1 && buf[len-1] == '\n') |
| 2014 | buf[len-1] = '\0'; |
| 2015 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 2016 | |
| 2017 | #if OPENSSL_VERSION_1_1 |
| 2018 | aead = SSL_CIPHER_is_aead(cipher); |
| 2019 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 2020 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2021 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 2022 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2023 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 2024 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2025 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 2026 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2027 | #endif |
| 2028 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 2029 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2030 | "{sksssssssisi" |
| 2031 | #if OPENSSL_VERSION_1_1 |
| 2032 | "sOssssssss" |
| 2033 | #endif |
| 2034 | "}", |
| 2035 | "id", cipher_id, |
| 2036 | "name", cipher_name, |
| 2037 | "protocol", cipher_protocol, |
| 2038 | "description", buf, |
| 2039 | "strength_bits", strength_bits, |
| 2040 | "alg_bits", alg_bits |
| 2041 | #if OPENSSL_VERSION_1_1 |
| 2042 | ,"aead", aead ? Py_True : Py_False, |
| 2043 | "symmetric", skcipher, |
| 2044 | "digest", digest, |
| 2045 | "kea", kx, |
| 2046 | "auth", auth |
| 2047 | #endif |
| 2048 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2049 | } |
| 2050 | #endif |
| 2051 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2052 | /*[clinic input] |
| 2053 | _ssl._SSLSocket.shared_ciphers |
| 2054 | [clinic start generated code]*/ |
| 2055 | |
| 2056 | static PyObject * |
| 2057 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 2058 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2059 | { |
| 2060 | STACK_OF(SSL_CIPHER) *ciphers; |
| 2061 | int i; |
| 2062 | PyObject *res; |
| 2063 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2064 | ciphers = SSL_get_ciphers(self->ssl); |
| 2065 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2066 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2067 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 2068 | if (!res) |
| 2069 | return NULL; |
| 2070 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 2071 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 2072 | if (!tup) { |
| 2073 | Py_DECREF(res); |
| 2074 | return NULL; |
| 2075 | } |
| 2076 | PyList_SET_ITEM(res, i, tup); |
| 2077 | } |
| 2078 | return res; |
| 2079 | } |
| 2080 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2081 | /*[clinic input] |
| 2082 | _ssl._SSLSocket.cipher |
| 2083 | [clinic start generated code]*/ |
| 2084 | |
| 2085 | static PyObject * |
| 2086 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 2087 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2088 | { |
| 2089 | const SSL_CIPHER *current; |
| 2090 | |
| 2091 | if (self->ssl == NULL) |
| 2092 | Py_RETURN_NONE; |
| 2093 | current = SSL_get_current_cipher(self->ssl); |
| 2094 | if (current == NULL) |
| 2095 | Py_RETURN_NONE; |
| 2096 | return cipher_to_tuple(current); |
| 2097 | } |
| 2098 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2099 | /*[clinic input] |
| 2100 | _ssl._SSLSocket.version |
| 2101 | [clinic start generated code]*/ |
| 2102 | |
| 2103 | static PyObject * |
| 2104 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 2105 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2106 | { |
| 2107 | const char *version; |
| 2108 | |
| 2109 | if (self->ssl == NULL) |
| 2110 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 2111 | if (!SSL_is_init_finished(self->ssl)) { |
| 2112 | /* handshake not finished */ |
| 2113 | Py_RETURN_NONE; |
| 2114 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2115 | version = SSL_get_version(self->ssl); |
| 2116 | if (!strcmp(version, "unknown")) |
| 2117 | Py_RETURN_NONE; |
| 2118 | return PyUnicode_FromString(version); |
| 2119 | } |
| 2120 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2121 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2122 | /*[clinic input] |
| 2123 | _ssl._SSLSocket.selected_npn_protocol |
| 2124 | [clinic start generated code]*/ |
| 2125 | |
| 2126 | static PyObject * |
| 2127 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 2128 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 2129 | { |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2130 | const unsigned char *out; |
| 2131 | unsigned int outlen; |
| 2132 | |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2133 | SSL_get0_next_proto_negotiated(self->ssl, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2134 | &out, &outlen); |
| 2135 | |
| 2136 | if (out == NULL) |
| 2137 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2138 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 2139 | } |
| 2140 | #endif |
| 2141 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2142 | #if HAVE_ALPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2143 | /*[clinic input] |
| 2144 | _ssl._SSLSocket.selected_alpn_protocol |
| 2145 | [clinic start generated code]*/ |
| 2146 | |
| 2147 | static PyObject * |
| 2148 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2149 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2150 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2151 | const unsigned char *out; |
| 2152 | unsigned int outlen; |
| 2153 | |
| 2154 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2155 | |
| 2156 | if (out == NULL) |
| 2157 | Py_RETURN_NONE; |
| 2158 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2159 | } |
| 2160 | #endif |
| 2161 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2162 | /*[clinic input] |
| 2163 | _ssl._SSLSocket.compression |
| 2164 | [clinic start generated code]*/ |
| 2165 | |
| 2166 | static PyObject * |
| 2167 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2168 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2169 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2170 | #ifdef OPENSSL_NO_COMP |
| 2171 | Py_RETURN_NONE; |
| 2172 | #else |
| 2173 | const COMP_METHOD *comp_method; |
| 2174 | const char *short_name; |
| 2175 | |
| 2176 | if (self->ssl == NULL) |
| 2177 | Py_RETURN_NONE; |
| 2178 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2179 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2180 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2181 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2182 | if (short_name == NULL) |
| 2183 | Py_RETURN_NONE; |
| 2184 | return PyUnicode_DecodeFSDefault(short_name); |
| 2185 | #endif |
| 2186 | } |
| 2187 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2188 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2189 | Py_INCREF(self->ctx); |
| 2190 | return self->ctx; |
| 2191 | } |
| 2192 | |
| 2193 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2194 | void *closure) { |
| 2195 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2196 | if (PyObject_TypeCheck(value, PySSLContext_Type)) { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2197 | #if !HAVE_SNI |
| 2198 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 2199 | "context is not supported by your OpenSSL library"); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2200 | return -1; |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2201 | #else |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2202 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2203 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2204 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2205 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2206 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2207 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2208 | return -1; |
| 2209 | } |
| 2210 | |
| 2211 | return 0; |
| 2212 | } |
| 2213 | |
| 2214 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2215 | "_setter_context(ctx)\n\ |
| 2216 | \ |
| 2217 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2218 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2219 | on the SSLContext to change the certificate information associated with the\n\ |
| 2220 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2221 | |
| 2222 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2223 | static PyObject * |
| 2224 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2225 | { |
| 2226 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2227 | } |
| 2228 | |
| 2229 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2230 | "Whether this is a server-side socket."); |
| 2231 | |
| 2232 | static PyObject * |
| 2233 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2234 | { |
| 2235 | if (self->server_hostname == NULL) |
| 2236 | Py_RETURN_NONE; |
| 2237 | Py_INCREF(self->server_hostname); |
| 2238 | return self->server_hostname; |
| 2239 | } |
| 2240 | |
| 2241 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2242 | "The currently set server hostname (for SNI)."); |
| 2243 | |
| 2244 | static PyObject * |
| 2245 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2246 | { |
| 2247 | PyObject *owner; |
| 2248 | |
| 2249 | if (self->owner == NULL) |
| 2250 | Py_RETURN_NONE; |
| 2251 | |
| 2252 | owner = PyWeakref_GetObject(self->owner); |
| 2253 | Py_INCREF(owner); |
| 2254 | return owner; |
| 2255 | } |
| 2256 | |
| 2257 | static int |
| 2258 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2259 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2260 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2261 | if (self->owner == NULL) |
| 2262 | return -1; |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2267 | "The Python-level owner of this object.\ |
| 2268 | Passed as \"self\" in servername callback."); |
| 2269 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2270 | static int |
| 2271 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2272 | { |
| 2273 | Py_VISIT(self->exc_type); |
| 2274 | Py_VISIT(self->exc_value); |
| 2275 | Py_VISIT(self->exc_tb); |
| 2276 | return 0; |
| 2277 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2278 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2279 | static int |
| 2280 | PySSL_clear(PySSLSocket *self) |
| 2281 | { |
| 2282 | Py_CLEAR(self->exc_type); |
| 2283 | Py_CLEAR(self->exc_value); |
| 2284 | Py_CLEAR(self->exc_tb); |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | static void |
| 2289 | PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2290 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2291 | PyTypeObject *tp = Py_TYPE(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2292 | if (self->ssl) |
| 2293 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2294 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2295 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2296 | Py_XDECREF(self->server_hostname); |
| 2297 | Py_XDECREF(self->owner); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2298 | PyObject_Del(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2299 | Py_DECREF(tp); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2302 | /* 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] | 2303 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2304 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2305 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2306 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2307 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2308 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2309 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2310 | int rc; |
| 2311 | #ifdef HAVE_POLL |
| 2312 | struct pollfd pollfd; |
| 2313 | _PyTime_t ms; |
| 2314 | #else |
| 2315 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2316 | fd_set fds; |
| 2317 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2318 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2319 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2320 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2321 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2322 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2323 | else if (timeout < 0) { |
| 2324 | if (s->sock_timeout > 0) |
| 2325 | return SOCKET_HAS_TIMED_OUT; |
| 2326 | else |
| 2327 | return SOCKET_IS_BLOCKING; |
| 2328 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2329 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2330 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2331 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2332 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2334 | /* Prefer poll, if available, since you can poll() any fd |
| 2335 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2336 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2337 | pollfd.fd = s->sock_fd; |
| 2338 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2339 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2340 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2341 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2342 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2343 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2344 | PySSL_BEGIN_ALLOW_THREADS |
| 2345 | rc = poll(&pollfd, 1, (int)ms); |
| 2346 | PySSL_END_ALLOW_THREADS |
| 2347 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2348 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2349 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2350 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2351 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2352 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2353 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2354 | FD_ZERO(&fds); |
| 2355 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2356 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2357 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2358 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2359 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2360 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2361 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2362 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2363 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2364 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2365 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2366 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2367 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2368 | (when we are able to write or when there's something to read) */ |
| 2369 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2372 | /*[clinic input] |
| 2373 | _ssl._SSLSocket.write |
| 2374 | b: Py_buffer |
| 2375 | / |
| 2376 | |
| 2377 | Writes the bytes-like object b into the SSL object. |
| 2378 | |
| 2379 | Returns the number of bytes written. |
| 2380 | [clinic start generated code]*/ |
| 2381 | |
| 2382 | static PyObject * |
| 2383 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2384 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2385 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2386 | int len; |
| 2387 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2388 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2389 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2390 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2391 | _PyTime_t timeout, deadline = 0; |
| 2392 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2393 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2394 | if (sock != NULL) { |
| 2395 | if (((PyObject*)sock) == Py_None) { |
| 2396 | _setSSLError("Underlying socket connection gone", |
| 2397 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2398 | return NULL; |
| 2399 | } |
| 2400 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2403 | if (b->len > INT_MAX) { |
Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2404 | PyErr_Format(PyExc_OverflowError, |
| 2405 | "string longer than %d bytes", INT_MAX); |
| 2406 | goto error; |
| 2407 | } |
| 2408 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2409 | if (sock != NULL) { |
| 2410 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2411 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2412 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2413 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2414 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2415 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2416 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2417 | has_timeout = (timeout > 0); |
| 2418 | if (has_timeout) |
| 2419 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2420 | |
| 2421 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2422 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2423 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2424 | "The write operation timed out"); |
| 2425 | goto error; |
| 2426 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2427 | PyErr_SetString(PySSLErrorObject, |
| 2428 | "Underlying socket has been closed."); |
| 2429 | goto error; |
| 2430 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2431 | PyErr_SetString(PySSLErrorObject, |
| 2432 | "Underlying socket too large for select()."); |
| 2433 | goto error; |
| 2434 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2435 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2436 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2437 | PySSL_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2438 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2439 | err = _PySSL_errno(len <= 0, self->ssl, len); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2440 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2441 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2442 | |
| 2443 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2444 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2445 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2446 | if (has_timeout) |
| 2447 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2448 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2449 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2450 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2451 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2452 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2453 | } else { |
| 2454 | sockstate = SOCKET_OPERATION_OK; |
| 2455 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2456 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2457 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2458 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2459 | "The write operation timed out"); |
| 2460 | goto error; |
| 2461 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2462 | PyErr_SetString(PySSLErrorObject, |
| 2463 | "Underlying socket has been closed."); |
| 2464 | goto error; |
| 2465 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2466 | break; |
| 2467 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2468 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2469 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2470 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2471 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2472 | if (len <= 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2473 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2474 | if (PySSL_ChainExceptions(self) < 0) |
| 2475 | return NULL; |
| 2476 | return PyLong_FromLong(len); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2477 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2478 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2479 | PySSL_ChainExceptions(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2480 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2483 | /*[clinic input] |
| 2484 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2485 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2486 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2487 | [clinic start generated code]*/ |
| 2488 | |
| 2489 | static PyObject * |
| 2490 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2491 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
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 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2494 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2496 | PySSL_BEGIN_ALLOW_THREADS |
| 2497 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2498 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2499 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2500 | self->err = err; |
| 2501 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2502 | if (count < 0) |
| 2503 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2504 | else |
| 2505 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2508 | /*[clinic input] |
| 2509 | _ssl._SSLSocket.read |
| 2510 | size as len: int |
| 2511 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2512 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2513 | ] |
| 2514 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2515 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2516 | Read up to size bytes from the SSL socket. |
| 2517 | [clinic start generated code]*/ |
| 2518 | |
| 2519 | static PyObject * |
| 2520 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2521 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2522 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2523 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2524 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2525 | char *mem; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2526 | int count; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2527 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2528 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2529 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2530 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2531 | _PyTime_t timeout, deadline = 0; |
| 2532 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2533 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2534 | if (!group_right_1 && len < 0) { |
| 2535 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2536 | return NULL; |
| 2537 | } |
| 2538 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2539 | if (sock != NULL) { |
| 2540 | if (((PyObject*)sock) == Py_None) { |
| 2541 | _setSSLError("Underlying socket connection gone", |
| 2542 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2548 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2549 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2550 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2551 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2552 | if (len == 0) { |
| 2553 | Py_XDECREF(sock); |
| 2554 | return dest; |
| 2555 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2556 | mem = PyBytes_AS_STRING(dest); |
| 2557 | } |
| 2558 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2559 | mem = buffer->buf; |
| 2560 | if (len <= 0 || len > buffer->len) { |
| 2561 | len = (int) buffer->len; |
| 2562 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2563 | PyErr_SetString(PyExc_OverflowError, |
| 2564 | "maximum length can't fit in a C 'int'"); |
| 2565 | goto error; |
| 2566 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2567 | if (len == 0) { |
| 2568 | count = 0; |
| 2569 | goto done; |
| 2570 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2571 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2574 | if (sock != NULL) { |
| 2575 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2576 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2577 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2578 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2579 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2580 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2581 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2582 | has_timeout = (timeout > 0); |
| 2583 | if (has_timeout) |
| 2584 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2585 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2586 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2587 | PySSL_BEGIN_ALLOW_THREADS |
| 2588 | count = SSL_read(self->ssl, mem, len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2589 | err = _PySSL_errno(count <= 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2590 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2591 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2592 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2593 | if (PyErr_CheckSignals()) |
| 2594 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2595 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2596 | if (has_timeout) |
| 2597 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2598 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2599 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2600 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2601 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2602 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2603 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2604 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2605 | { |
| 2606 | count = 0; |
| 2607 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2608 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2609 | else |
| 2610 | sockstate = SOCKET_OPERATION_OK; |
| 2611 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2612 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2613 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2614 | "The read operation timed out"); |
| 2615 | goto error; |
| 2616 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2617 | break; |
| 2618 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2619 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2620 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2621 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2622 | if (count <= 0) { |
| 2623 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2624 | goto error; |
| 2625 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2626 | if (self->exc_type != NULL) |
| 2627 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2628 | |
| 2629 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2630 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2631 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2632 | _PyBytes_Resize(&dest, count); |
| 2633 | return dest; |
| 2634 | } |
| 2635 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2636 | return PyLong_FromLong(count); |
| 2637 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2638 | |
| 2639 | error: |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2640 | PySSL_ChainExceptions(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2641 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2642 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2643 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2644 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2647 | /*[clinic input] |
| 2648 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2649 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2650 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2651 | [clinic start generated code]*/ |
| 2652 | |
| 2653 | static PyObject * |
| 2654 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2655 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2656 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2657 | _PySSLError err; |
| 2658 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2659 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2660 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2661 | _PyTime_t timeout, deadline = 0; |
| 2662 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2663 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2664 | if (sock != NULL) { |
| 2665 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2666 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2667 | _setSSLError("Underlying socket connection gone", |
| 2668 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2669 | return NULL; |
| 2670 | } |
| 2671 | Py_INCREF(sock); |
| 2672 | |
| 2673 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2674 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2675 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2676 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2677 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2678 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2679 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2680 | has_timeout = (timeout > 0); |
| 2681 | if (has_timeout) |
| 2682 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2683 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2684 | while (1) { |
| 2685 | PySSL_BEGIN_ALLOW_THREADS |
| 2686 | /* Disable read-ahead so that unwrap can work correctly. |
| 2687 | * Otherwise OpenSSL might read in too much data, |
| 2688 | * eating clear text data that happens to be |
| 2689 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2690 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2691 | * function is used and the shutdown_seen_zero != 0 |
| 2692 | * condition is met. |
| 2693 | */ |
| 2694 | if (self->shutdown_seen_zero) |
| 2695 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2696 | ret = SSL_shutdown(self->ssl); |
| 2697 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2698 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2699 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2700 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2701 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2702 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2703 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2704 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2705 | /* Don't loop endlessly; instead preserve legacy |
| 2706 | behaviour of trying SSL_shutdown() only twice. |
| 2707 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2708 | if (++zeros > 1) |
| 2709 | break; |
| 2710 | /* Shutdown was sent, now try receiving */ |
| 2711 | self->shutdown_seen_zero = 1; |
| 2712 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2715 | if (has_timeout) |
| 2716 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2717 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2718 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2719 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2720 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2721 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2722 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2723 | else |
| 2724 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2725 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2726 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2727 | if (err.ssl == SSL_ERROR_WANT_READ) |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2728 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2729 | "The read operation timed out"); |
| 2730 | else |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2731 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2732 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2733 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2734 | } |
| 2735 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2736 | PyErr_SetString(PySSLErrorObject, |
| 2737 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2738 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2739 | } |
| 2740 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2741 | /* Retain the SSL error code */ |
| 2742 | break; |
| 2743 | } |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2744 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2745 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2746 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2747 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2748 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2749 | if (self->exc_type != NULL) |
| 2750 | goto error; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2751 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2752 | /* It's already INCREF'ed */ |
| 2753 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2754 | else |
| 2755 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2756 | |
| 2757 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2758 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2759 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2760 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2763 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2764 | _ssl._SSLSocket.get_channel_binding |
| 2765 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2766 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2767 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2768 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2769 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2770 | of the data or None if the data is not available (e.g. before the handshake). |
| 2771 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2772 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2773 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2774 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2775 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2776 | const char *cb_type) |
| 2777 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2778 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2779 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2780 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2781 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2782 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2783 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2784 | /* if session is resumed XOR we are the client */ |
| 2785 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2786 | } |
| 2787 | else { |
| 2788 | /* if a new session XOR we are the server */ |
| 2789 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2790 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2791 | } |
| 2792 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2793 | PyErr_Format( |
| 2794 | PyExc_ValueError, |
| 2795 | "'%s' channel binding type not implemented", |
| 2796 | cb_type |
| 2797 | ); |
| 2798 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2802 | if (len == 0) |
| 2803 | Py_RETURN_NONE; |
| 2804 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2805 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2806 | } |
| 2807 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2808 | /*[clinic input] |
| 2809 | _ssl._SSLSocket.verify_client_post_handshake |
| 2810 | |
| 2811 | Initiate TLS 1.3 post-handshake authentication |
| 2812 | [clinic start generated code]*/ |
| 2813 | |
| 2814 | static PyObject * |
| 2815 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2816 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2817 | { |
| 2818 | #ifdef TLS1_3_VERSION |
| 2819 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2820 | if (err == 0) |
| 2821 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2822 | else |
| 2823 | Py_RETURN_NONE; |
| 2824 | #else |
| 2825 | PyErr_SetString(PyExc_NotImplementedError, |
| 2826 | "Post-handshake auth is not supported by your " |
| 2827 | "OpenSSL version."); |
| 2828 | return NULL; |
| 2829 | #endif |
| 2830 | } |
| 2831 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2832 | #ifdef OPENSSL_VERSION_1_1 |
| 2833 | |
| 2834 | static SSL_SESSION* |
| 2835 | _ssl_session_dup(SSL_SESSION *session) { |
| 2836 | SSL_SESSION *newsession = NULL; |
| 2837 | int slen; |
| 2838 | unsigned char *senc = NULL, *p; |
| 2839 | const unsigned char *const_p; |
| 2840 | |
| 2841 | if (session == NULL) { |
| 2842 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2843 | goto error; |
| 2844 | } |
| 2845 | |
| 2846 | /* get length */ |
| 2847 | slen = i2d_SSL_SESSION(session, NULL); |
| 2848 | if (slen == 0 || slen > 0xFF00) { |
| 2849 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2850 | goto error; |
| 2851 | } |
| 2852 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2853 | PyErr_NoMemory(); |
| 2854 | goto error; |
| 2855 | } |
| 2856 | p = senc; |
| 2857 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2858 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2859 | goto error; |
| 2860 | } |
| 2861 | const_p = senc; |
| 2862 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2863 | if (session == NULL) { |
| 2864 | goto error; |
| 2865 | } |
| 2866 | PyMem_Free(senc); |
| 2867 | return newsession; |
| 2868 | error: |
| 2869 | if (senc != NULL) { |
| 2870 | PyMem_Free(senc); |
| 2871 | } |
| 2872 | return NULL; |
| 2873 | } |
| 2874 | #endif |
| 2875 | |
| 2876 | static PyObject * |
| 2877 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2878 | /* get_session can return sessions from a server-side connection, |
| 2879 | * it does not check for handshake done or client socket. */ |
| 2880 | PySSLSession *pysess; |
| 2881 | SSL_SESSION *session; |
| 2882 | |
| 2883 | #ifdef OPENSSL_VERSION_1_1 |
| 2884 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2885 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2886 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2887 | if (session == NULL) { |
| 2888 | Py_RETURN_NONE; |
| 2889 | } |
| 2890 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2891 | return NULL; |
| 2892 | } |
| 2893 | #else |
| 2894 | session = SSL_get1_session(self->ssl); |
| 2895 | if (session == NULL) { |
| 2896 | Py_RETURN_NONE; |
| 2897 | } |
| 2898 | #endif |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2899 | pysess = PyObject_GC_New(PySSLSession, PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2900 | if (pysess == NULL) { |
| 2901 | SSL_SESSION_free(session); |
| 2902 | return NULL; |
| 2903 | } |
| 2904 | |
| 2905 | assert(self->ctx); |
| 2906 | pysess->ctx = self->ctx; |
| 2907 | Py_INCREF(pysess->ctx); |
| 2908 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2909 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2910 | return (PyObject *)pysess; |
| 2911 | } |
| 2912 | |
| 2913 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2914 | void *closure) |
| 2915 | { |
| 2916 | PySSLSession *pysess; |
| 2917 | #ifdef OPENSSL_VERSION_1_1 |
| 2918 | SSL_SESSION *session; |
| 2919 | #endif |
| 2920 | int result; |
| 2921 | |
| 2922 | if (!PySSLSession_Check(value)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2923 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2924 | return -1; |
| 2925 | } |
| 2926 | pysess = (PySSLSession *)value; |
| 2927 | |
| 2928 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2929 | PyErr_SetString(PyExc_ValueError, |
| 2930 | "Session refers to a different SSLContext."); |
| 2931 | return -1; |
| 2932 | } |
| 2933 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2934 | PyErr_SetString(PyExc_ValueError, |
| 2935 | "Cannot set session for server-side SSLSocket."); |
| 2936 | return -1; |
| 2937 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2938 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2939 | PyErr_SetString(PyExc_ValueError, |
| 2940 | "Cannot set session after handshake."); |
| 2941 | return -1; |
| 2942 | } |
| 2943 | #ifdef OPENSSL_VERSION_1_1 |
| 2944 | /* duplicate session */ |
| 2945 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2946 | return -1; |
| 2947 | } |
| 2948 | result = SSL_set_session(self->ssl, session); |
| 2949 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2950 | SSL_SESSION_free(session); |
| 2951 | #else |
| 2952 | result = SSL_set_session(self->ssl, pysess->session); |
| 2953 | #endif |
| 2954 | if (result == 0) { |
| 2955 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2956 | return -1; |
| 2957 | } |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
| 2961 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2962 | "_setter_session(session)\n\ |
| 2963 | \ |
| 2964 | Get / set SSLSession."); |
| 2965 | |
| 2966 | static PyObject * |
| 2967 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2968 | if (SSL_session_reused(self->ssl)) { |
| 2969 | Py_RETURN_TRUE; |
| 2970 | } else { |
| 2971 | Py_RETURN_FALSE; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2976 | "Was the client session reused during handshake?"); |
| 2977 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2978 | static PyGetSetDef ssl_getsetlist[] = { |
| 2979 | {"context", (getter) PySSL_get_context, |
| 2980 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2981 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2982 | PySSL_get_server_side_doc}, |
| 2983 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2984 | PySSL_get_server_hostname_doc}, |
| 2985 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2986 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2987 | {"session", (getter) PySSL_get_session, |
| 2988 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2989 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2990 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2991 | {NULL}, /* sentinel */ |
| 2992 | }; |
| 2993 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2994 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2995 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2996 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2997 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2998 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2999 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 3000 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3001 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 3002 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 3003 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 3004 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 3005 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 3006 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 3007 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3008 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3009 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3010 | }; |
| 3011 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3012 | static PyType_Slot PySSLSocket_slots[] = { |
| 3013 | {Py_tp_methods, PySSLMethods}, |
| 3014 | {Py_tp_getset, ssl_getsetlist}, |
| 3015 | {Py_tp_dealloc, PySSL_dealloc}, |
| 3016 | {Py_tp_traverse, PySSL_traverse}, |
| 3017 | {Py_tp_clear, PySSL_clear}, |
| 3018 | {0, 0}, |
| 3019 | }; |
| 3020 | |
| 3021 | static PyType_Spec PySSLSocket_spec = { |
| 3022 | "_ssl._SSLSocket", |
| 3023 | sizeof(PySSLSocket), |
| 3024 | 0, |
| 3025 | Py_TPFLAGS_DEFAULT, |
| 3026 | PySSLSocket_slots, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3027 | }; |
| 3028 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3029 | |
| 3030 | /* |
| 3031 | * _SSLContext objects |
| 3032 | */ |
| 3033 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3034 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3035 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3036 | { |
| 3037 | int mode; |
| 3038 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 3039 | |
| 3040 | switch(n) { |
| 3041 | case PY_SSL_CERT_NONE: |
| 3042 | mode = SSL_VERIFY_NONE; |
| 3043 | break; |
| 3044 | case PY_SSL_CERT_OPTIONAL: |
| 3045 | mode = SSL_VERIFY_PEER; |
| 3046 | break; |
| 3047 | case PY_SSL_CERT_REQUIRED: |
| 3048 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3049 | break; |
| 3050 | default: |
| 3051 | PyErr_SetString(PyExc_ValueError, |
| 3052 | "invalid value for verify_mode"); |
| 3053 | return -1; |
| 3054 | } |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3055 | |
| 3056 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3057 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 3058 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3059 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3060 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 3061 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3062 | return 0; |
| 3063 | } |
| 3064 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3065 | /*[clinic input] |
| 3066 | @classmethod |
| 3067 | _ssl._SSLContext.__new__ |
| 3068 | protocol as proto_version: int |
| 3069 | / |
| 3070 | [clinic start generated code]*/ |
| 3071 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3072 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3073 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 3074 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3075 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3076 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3077 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3078 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3079 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3080 | int result; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3081 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3082 | unsigned long libver; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3083 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3084 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3085 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3086 | switch(proto_version) { |
| 3087 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 3088 | case PY_SSL_VERSION_SSL3: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3089 | ctx = SSL_CTX_new(SSLv3_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3090 | break; |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3091 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3092 | #if (defined(TLS1_VERSION) && \ |
| 3093 | !defined(OPENSSL_NO_TLS1) && \ |
| 3094 | !defined(OPENSSL_NO_TLS1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3095 | case PY_SSL_VERSION_TLS1: |
| 3096 | ctx = SSL_CTX_new(TLSv1_method()); |
| 3097 | break; |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3098 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3099 | #if (defined(TLS1_1_VERSION) && \ |
| 3100 | !defined(OPENSSL_NO_TLS1_1) && \ |
| 3101 | !defined(OPENSSL_NO_TLS1_1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3102 | case PY_SSL_VERSION_TLS1_1: |
| 3103 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 3104 | break; |
| 3105 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3106 | #if (defined(TLS1_2_VERSION) && \ |
| 3107 | !defined(OPENSSL_NO_TLS1_2) && \ |
| 3108 | !defined(OPENSSL_NO_TLS1_2_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3109 | case PY_SSL_VERSION_TLS1_2: |
| 3110 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 3111 | break; |
| 3112 | #endif |
| 3113 | case PY_SSL_VERSION_TLS: |
| 3114 | /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3115 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3116 | break; |
| 3117 | case PY_SSL_VERSION_TLS_CLIENT: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3118 | ctx = SSL_CTX_new(TLS_client_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3119 | break; |
| 3120 | case PY_SSL_VERSION_TLS_SERVER: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3121 | ctx = SSL_CTX_new(TLS_server_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3122 | break; |
| 3123 | default: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3124 | proto_version = -1; |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3125 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3126 | PySSL_END_ALLOW_THREADS |
| 3127 | |
| 3128 | if (proto_version == -1) { |
| 3129 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3130 | "invalid or unsupported protocol version"); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3131 | return NULL; |
| 3132 | } |
| 3133 | if (ctx == NULL) { |
Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 3134 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3135 | return NULL; |
| 3136 | } |
| 3137 | |
| 3138 | assert(type != NULL && type->tp_alloc != NULL); |
| 3139 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 3140 | if (self == NULL) { |
| 3141 | SSL_CTX_free(ctx); |
| 3142 | return NULL; |
| 3143 | } |
| 3144 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3145 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3146 | self->protocol = proto_version; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3147 | self->msg_cb = NULL; |
| 3148 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3149 | self->keylog_filename = NULL; |
| 3150 | self->keylog_bio = NULL; |
| 3151 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3152 | #if HAVE_NPN |
Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 3153 | self->npn_protocols = NULL; |
| 3154 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3155 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3156 | self->alpn_protocols = NULL; |
| 3157 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3158 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3159 | self->set_sni_cb = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3160 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3161 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3162 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3163 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3164 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3165 | Py_DECREF(self); |
| 3166 | return NULL; |
| 3167 | } |
| 3168 | } else { |
| 3169 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3170 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3171 | Py_DECREF(self); |
| 3172 | return NULL; |
| 3173 | } |
| 3174 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3175 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3176 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3177 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3178 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3179 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3180 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3181 | /* Minimal security flags for server and client side context. |
| 3182 | * Client sockets ignore server-side parameters. */ |
| 3183 | #ifdef SSL_OP_NO_COMPRESSION |
| 3184 | options |= SSL_OP_NO_COMPRESSION; |
| 3185 | #endif |
| 3186 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3187 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3188 | #endif |
| 3189 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3190 | options |= SSL_OP_SINGLE_DH_USE; |
| 3191 | #endif |
| 3192 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3193 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3194 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3195 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3196 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3197 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3198 | * It's far from perfect but gives users a better head start. */ |
| 3199 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3200 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3201 | /* stick to OpenSSL's default settings */ |
| 3202 | result = 1; |
| 3203 | #else |
| 3204 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3205 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3206 | } else { |
| 3207 | /* SSLv2 needs MD5 */ |
| 3208 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3209 | } |
| 3210 | if (result == 0) { |
| 3211 | Py_DECREF(self); |
| 3212 | ERR_clear_error(); |
| 3213 | PyErr_SetString(PySSLErrorObject, |
| 3214 | "No cipher can be selected."); |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3218 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 3219 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 3220 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 3221 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 3222 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 3223 | conservative and assume it wasn't fixed until release. We do this check |
| 3224 | at runtime to avoid problems from the dynamic linker. |
| 3225 | See #25672 for more on this. */ |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3226 | libver = OpenSSL_version_num(); |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3227 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 3228 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 3229 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 3230 | } |
| 3231 | #endif |
| 3232 | |
| 3233 | |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3234 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3235 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 3236 | prime256v1 by default. This is Apache mod_ssl's initialization |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3237 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 3238 | */ |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3239 | #if defined(SSL_CTX_set_ecdh_auto) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3240 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 3241 | #else |
| 3242 | { |
| 3243 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 3244 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3245 | EC_KEY_free(key); |
| 3246 | } |
| 3247 | #endif |
| 3248 | #endif |
| 3249 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3250 | #define SID_CTX "Python" |
| 3251 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3252 | sizeof(SID_CTX)); |
| 3253 | #undef SID_CTX |
| 3254 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3255 | params = SSL_CTX_get0_param(self->ctx); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3256 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3257 | /* Improve trust chain building when cross-signed intermediate |
| 3258 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3259 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3260 | #endif |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3261 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3262 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3263 | #ifdef TLS1_3_VERSION |
| 3264 | self->post_handshake_auth = 0; |
| 3265 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3266 | #endif |
| 3267 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3268 | return (PyObject *)self; |
| 3269 | } |
| 3270 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3271 | static int |
| 3272 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3273 | { |
| 3274 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3275 | Py_VISIT(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3276 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3277 | Py_VISIT(self->msg_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3278 | return 0; |
| 3279 | } |
| 3280 | |
| 3281 | static int |
| 3282 | context_clear(PySSLContext *self) |
| 3283 | { |
| 3284 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3285 | Py_CLEAR(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3286 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3287 | Py_CLEAR(self->msg_cb); |
| 3288 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3289 | Py_CLEAR(self->keylog_filename); |
| 3290 | if (self->keylog_bio != NULL) { |
| 3291 | PySSL_BEGIN_ALLOW_THREADS |
| 3292 | BIO_free_all(self->keylog_bio); |
| 3293 | PySSL_END_ALLOW_THREADS |
| 3294 | self->keylog_bio = NULL; |
| 3295 | } |
| 3296 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3297 | return 0; |
| 3298 | } |
| 3299 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3300 | static void |
| 3301 | context_dealloc(PySSLContext *self) |
| 3302 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3303 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3304 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3305 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3306 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3307 | SSL_CTX_free(self->ctx); |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3308 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3309 | PyMem_FREE(self->npn_protocols); |
| 3310 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3311 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3312 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3313 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3314 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3315 | Py_DECREF(tp); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3318 | /*[clinic input] |
| 3319 | _ssl._SSLContext.set_ciphers |
| 3320 | cipherlist: str |
| 3321 | / |
| 3322 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3323 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3324 | static PyObject * |
| 3325 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3326 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3327 | { |
| 3328 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3329 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3330 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3331 | otherwise the error will be reported again when another SSL call |
| 3332 | is done. */ |
| 3333 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3334 | PyErr_SetString(PySSLErrorObject, |
| 3335 | "No cipher can be selected."); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | Py_RETURN_NONE; |
| 3339 | } |
| 3340 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3341 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 3342 | /*[clinic input] |
| 3343 | _ssl._SSLContext.get_ciphers |
| 3344 | [clinic start generated code]*/ |
| 3345 | |
| 3346 | static PyObject * |
| 3347 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3348 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3349 | { |
| 3350 | SSL *ssl = NULL; |
| 3351 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3352 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3353 | int i=0; |
| 3354 | PyObject *result = NULL, *dct; |
| 3355 | |
| 3356 | ssl = SSL_new(self->ctx); |
| 3357 | if (ssl == NULL) { |
| 3358 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3359 | goto exit; |
| 3360 | } |
| 3361 | sk = SSL_get_ciphers(ssl); |
| 3362 | |
| 3363 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3364 | if (result == NULL) { |
| 3365 | goto exit; |
| 3366 | } |
| 3367 | |
| 3368 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3369 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3370 | dct = cipher_to_dict(cipher); |
| 3371 | if (dct == NULL) { |
| 3372 | Py_CLEAR(result); |
| 3373 | goto exit; |
| 3374 | } |
| 3375 | PyList_SET_ITEM(result, i, dct); |
| 3376 | } |
| 3377 | |
| 3378 | exit: |
| 3379 | if (ssl != NULL) |
| 3380 | SSL_free(ssl); |
| 3381 | return result; |
| 3382 | |
| 3383 | } |
| 3384 | #endif |
| 3385 | |
| 3386 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3387 | #if HAVE_NPN || HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3388 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3389 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3390 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3391 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3392 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3393 | int ret; |
| 3394 | if (client_protocols == NULL) { |
| 3395 | client_protocols = (unsigned char *)""; |
| 3396 | client_protocols_len = 0; |
| 3397 | } |
| 3398 | if (server_protocols == NULL) { |
| 3399 | server_protocols = (unsigned char *)""; |
| 3400 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3401 | } |
| 3402 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3403 | ret = SSL_select_next_proto(out, outlen, |
| 3404 | server_protocols, server_protocols_len, |
| 3405 | client_protocols, client_protocols_len); |
| 3406 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3407 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3408 | |
| 3409 | return SSL_TLSEXT_ERR_OK; |
| 3410 | } |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3411 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3412 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3413 | #if HAVE_NPN |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3414 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 3415 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3416 | _advertiseNPN_cb(SSL *s, |
| 3417 | const unsigned char **data, unsigned int *len, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3418 | void *args) |
| 3419 | { |
| 3420 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3421 | |
| 3422 | if (ssl_ctx->npn_protocols == NULL) { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3423 | *data = (unsigned char *)""; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3424 | *len = 0; |
| 3425 | } else { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3426 | *data = ssl_ctx->npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3427 | *len = ssl_ctx->npn_protocols_len; |
| 3428 | } |
| 3429 | |
| 3430 | return SSL_TLSEXT_ERR_OK; |
| 3431 | } |
| 3432 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3433 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3434 | _selectNPN_cb(SSL *s, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3435 | unsigned char **out, unsigned char *outlen, |
| 3436 | const unsigned char *server, unsigned int server_len, |
| 3437 | void *args) |
| 3438 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3439 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3440 | return do_protocol_selection(0, out, outlen, server, server_len, |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3441 | ctx->npn_protocols, ctx->npn_protocols_len); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3442 | } |
| 3443 | #endif |
| 3444 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3445 | /*[clinic input] |
| 3446 | _ssl._SSLContext._set_npn_protocols |
| 3447 | protos: Py_buffer |
| 3448 | / |
| 3449 | [clinic start generated code]*/ |
| 3450 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3451 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3452 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3453 | Py_buffer *protos) |
| 3454 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3455 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3456 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3457 | PyMem_Free(self->npn_protocols); |
| 3458 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3459 | if (self->npn_protocols == NULL) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3460 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3461 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3462 | self->npn_protocols_len = (int) protos->len; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* set both server and client callbacks, because the context can |
| 3465 | * be used to create both types of sockets */ |
| 3466 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3467 | _advertiseNPN_cb, |
| 3468 | self); |
| 3469 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3470 | _selectNPN_cb, |
| 3471 | self); |
| 3472 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3473 | Py_RETURN_NONE; |
| 3474 | #else |
| 3475 | PyErr_SetString(PyExc_NotImplementedError, |
| 3476 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3477 | return NULL; |
| 3478 | #endif |
| 3479 | } |
| 3480 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3481 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3482 | static int |
| 3483 | _selectALPN_cb(SSL *s, |
| 3484 | const unsigned char **out, unsigned char *outlen, |
| 3485 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3486 | void *args) |
| 3487 | { |
| 3488 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3489 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3490 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3491 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3492 | } |
| 3493 | #endif |
| 3494 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3495 | /*[clinic input] |
| 3496 | _ssl._SSLContext._set_alpn_protocols |
| 3497 | protos: Py_buffer |
| 3498 | / |
| 3499 | [clinic start generated code]*/ |
| 3500 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3501 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3502 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3503 | Py_buffer *protos) |
| 3504 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3505 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3506 | #if HAVE_ALPN |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3507 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3508 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3509 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3510 | return NULL; |
| 3511 | } |
| 3512 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3513 | PyMem_FREE(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3514 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3515 | if (!self->alpn_protocols) |
| 3516 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3517 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3518 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3519 | |
| 3520 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3521 | return PyErr_NoMemory(); |
| 3522 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3523 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3524 | Py_RETURN_NONE; |
| 3525 | #else |
| 3526 | PyErr_SetString(PyExc_NotImplementedError, |
| 3527 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3528 | return NULL; |
| 3529 | #endif |
| 3530 | } |
| 3531 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3532 | static PyObject * |
| 3533 | get_verify_mode(PySSLContext *self, void *c) |
| 3534 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3535 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3536 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3537 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3538 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3539 | case SSL_VERIFY_NONE: |
| 3540 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3541 | case SSL_VERIFY_PEER: |
| 3542 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3543 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3544 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3545 | } |
| 3546 | PyErr_SetString(PySSLErrorObject, |
| 3547 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3548 | return NULL; |
| 3549 | } |
| 3550 | |
| 3551 | static int |
| 3552 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3553 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3554 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3555 | if (!PyArg_Parse(arg, "i", &n)) |
| 3556 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3557 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3558 | PyErr_SetString(PyExc_ValueError, |
| 3559 | "Cannot set verify_mode to CERT_NONE when " |
| 3560 | "check_hostname is enabled."); |
| 3561 | return -1; |
| 3562 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3563 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3567 | get_verify_flags(PySSLContext *self, void *c) |
| 3568 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3569 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3570 | unsigned long flags; |
| 3571 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3572 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3573 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3574 | return PyLong_FromUnsignedLong(flags); |
| 3575 | } |
| 3576 | |
| 3577 | static int |
| 3578 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3579 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3580 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3581 | unsigned long new_flags, flags, set, clear; |
| 3582 | |
| 3583 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3584 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3585 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3586 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3587 | clear = flags & ~new_flags; |
| 3588 | set = ~flags & new_flags; |
| 3589 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3590 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3591 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3592 | return -1; |
| 3593 | } |
| 3594 | } |
| 3595 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3596 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3597 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3598 | return -1; |
| 3599 | } |
| 3600 | } |
| 3601 | return 0; |
| 3602 | } |
| 3603 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3604 | /* Getter and setter for protocol version */ |
| 3605 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) |
| 3606 | |
| 3607 | |
| 3608 | static int |
| 3609 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3610 | { |
| 3611 | long v; |
| 3612 | int result; |
| 3613 | |
| 3614 | if (!PyArg_Parse(arg, "l", &v)) |
| 3615 | return -1; |
| 3616 | if (v > INT_MAX) { |
| 3617 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3618 | return -1; |
| 3619 | } |
| 3620 | |
| 3621 | switch(self->protocol) { |
| 3622 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3623 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3624 | case PY_SSL_VERSION_TLS: |
| 3625 | break; |
| 3626 | default: |
| 3627 | PyErr_SetString( |
| 3628 | PyExc_ValueError, |
| 3629 | "The context's protocol doesn't support modification of " |
| 3630 | "highest and lowest version." |
| 3631 | ); |
| 3632 | return -1; |
| 3633 | } |
| 3634 | |
| 3635 | if (what == 0) { |
| 3636 | switch(v) { |
| 3637 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3638 | v = 0; |
| 3639 | break; |
| 3640 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3641 | /* Emulate max for set_min_proto_version */ |
| 3642 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3643 | break; |
| 3644 | default: |
| 3645 | break; |
| 3646 | } |
| 3647 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3648 | } |
| 3649 | else { |
| 3650 | switch(v) { |
| 3651 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3652 | v = 0; |
| 3653 | break; |
| 3654 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3655 | /* Emulate max for set_min_proto_version */ |
| 3656 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3657 | break; |
| 3658 | default: |
| 3659 | break; |
| 3660 | } |
| 3661 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3662 | } |
| 3663 | if (result == 0) { |
| 3664 | PyErr_Format(PyExc_ValueError, |
| 3665 | "Unsupported protocol version 0x%x", v); |
| 3666 | return -1; |
| 3667 | } |
| 3668 | return 0; |
| 3669 | } |
| 3670 | |
| 3671 | static PyObject * |
| 3672 | get_minimum_version(PySSLContext *self, void *c) |
| 3673 | { |
| 3674 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3675 | if (v == 0) { |
| 3676 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3677 | } |
| 3678 | return PyLong_FromLong(v); |
| 3679 | } |
| 3680 | |
| 3681 | static int |
| 3682 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3683 | { |
| 3684 | return set_min_max_proto_version(self, arg, 0); |
| 3685 | } |
| 3686 | |
| 3687 | static PyObject * |
| 3688 | get_maximum_version(PySSLContext *self, void *c) |
| 3689 | { |
| 3690 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3691 | if (v == 0) { |
| 3692 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3693 | } |
| 3694 | return PyLong_FromLong(v); |
| 3695 | } |
| 3696 | |
| 3697 | static int |
| 3698 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3699 | { |
| 3700 | return set_min_max_proto_version(self, arg, 1); |
| 3701 | } |
| 3702 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ |
| 3703 | |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3704 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 3705 | static PyObject * |
| 3706 | get_num_tickets(PySSLContext *self, void *c) |
| 3707 | { |
Victor Stinner | 76611c7 | 2019-07-09 13:30:52 +0200 | [diff] [blame] | 3708 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | static int |
| 3712 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3713 | { |
| 3714 | long num; |
| 3715 | if (!PyArg_Parse(arg, "l", &num)) |
| 3716 | return -1; |
| 3717 | if (num < 0) { |
| 3718 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3719 | return -1; |
| 3720 | } |
| 3721 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3722 | PyErr_SetString(PyExc_ValueError, |
| 3723 | "SSLContext is not a server context."); |
| 3724 | return -1; |
| 3725 | } |
| 3726 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3727 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3728 | return -1; |
| 3729 | } |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
| 3733 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3734 | "Control the number of TLSv1.3 session tickets"); |
| 3735 | #endif /* OpenSSL 1.1.1 */ |
| 3736 | |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 3737 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 3738 | static PyObject * |
| 3739 | get_security_level(PySSLContext *self, void *c) |
| 3740 | { |
| 3741 | return PyLong_FromLong(SSL_CTX_get_security_level(self->ctx)); |
| 3742 | } |
| 3743 | PyDoc_STRVAR(PySSLContext_security_level_doc, "The current security level"); |
| 3744 | #endif /* OpenSSL 1.1.0 */ |
| 3745 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3746 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3747 | get_options(PySSLContext *self, void *c) |
| 3748 | { |
| 3749 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3750 | } |
| 3751 | |
| 3752 | static int |
| 3753 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3754 | { |
| 3755 | long new_opts, opts, set, clear; |
| 3756 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3757 | return -1; |
| 3758 | opts = SSL_CTX_get_options(self->ctx); |
| 3759 | clear = opts & ~new_opts; |
| 3760 | set = ~opts & new_opts; |
| 3761 | if (clear) { |
| 3762 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3763 | SSL_CTX_clear_options(self->ctx, clear); |
| 3764 | #else |
| 3765 | PyErr_SetString(PyExc_ValueError, |
| 3766 | "can't clear options before OpenSSL 0.9.8m"); |
| 3767 | return -1; |
| 3768 | #endif |
| 3769 | } |
| 3770 | if (set) |
| 3771 | SSL_CTX_set_options(self->ctx, set); |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3775 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3776 | get_host_flags(PySSLContext *self, void *c) |
| 3777 | { |
| 3778 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3779 | } |
| 3780 | |
| 3781 | static int |
| 3782 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3783 | { |
| 3784 | X509_VERIFY_PARAM *param; |
| 3785 | unsigned int new_flags = 0; |
| 3786 | |
| 3787 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3788 | return -1; |
| 3789 | |
| 3790 | param = SSL_CTX_get0_param(self->ctx); |
| 3791 | self->hostflags = new_flags; |
| 3792 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
| 3796 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3797 | get_check_hostname(PySSLContext *self, void *c) |
| 3798 | { |
| 3799 | return PyBool_FromLong(self->check_hostname); |
| 3800 | } |
| 3801 | |
| 3802 | static int |
| 3803 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3804 | { |
| 3805 | int check_hostname; |
| 3806 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3807 | return -1; |
| 3808 | if (check_hostname && |
| 3809 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3810 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3811 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3812 | return -1; |
| 3813 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3814 | } |
| 3815 | self->check_hostname = check_hostname; |
| 3816 | return 0; |
| 3817 | } |
| 3818 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3819 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3820 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3821 | #if TLS1_3_VERSION |
| 3822 | return PyBool_FromLong(self->post_handshake_auth); |
| 3823 | #else |
| 3824 | Py_RETURN_NONE; |
| 3825 | #endif |
| 3826 | } |
| 3827 | |
| 3828 | #if TLS1_3_VERSION |
| 3829 | static int |
| 3830 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3831 | if (arg == NULL) { |
| 3832 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3833 | return -1; |
| 3834 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3835 | int pha = PyObject_IsTrue(arg); |
| 3836 | |
| 3837 | if (pha == -1) { |
| 3838 | return -1; |
| 3839 | } |
| 3840 | self->post_handshake_auth = pha; |
| 3841 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3842 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3843 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3844 | |
| 3845 | return 0; |
| 3846 | } |
| 3847 | #endif |
| 3848 | |
| 3849 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3850 | get_protocol(PySSLContext *self, void *c) { |
| 3851 | return PyLong_FromLong(self->protocol); |
| 3852 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3853 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3854 | typedef struct { |
| 3855 | PyThreadState *thread_state; |
| 3856 | PyObject *callable; |
| 3857 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3858 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3859 | int error; |
| 3860 | } _PySSLPasswordInfo; |
| 3861 | |
| 3862 | static int |
| 3863 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3864 | const char *bad_type_error) |
| 3865 | { |
| 3866 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3867 | from a unicode, bytes, or byte array object. |
| 3868 | The password field will be dynamically allocated and must be freed |
| 3869 | by the caller */ |
| 3870 | PyObject *password_bytes = NULL; |
| 3871 | const char *data = NULL; |
| 3872 | Py_ssize_t size; |
| 3873 | |
| 3874 | if (PyUnicode_Check(password)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3875 | password_bytes = PyUnicode_AsUTF8String(password); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3876 | if (!password_bytes) { |
| 3877 | goto error; |
| 3878 | } |
| 3879 | data = PyBytes_AS_STRING(password_bytes); |
| 3880 | size = PyBytes_GET_SIZE(password_bytes); |
| 3881 | } else if (PyBytes_Check(password)) { |
| 3882 | data = PyBytes_AS_STRING(password); |
| 3883 | size = PyBytes_GET_SIZE(password); |
| 3884 | } else if (PyByteArray_Check(password)) { |
| 3885 | data = PyByteArray_AS_STRING(password); |
| 3886 | size = PyByteArray_GET_SIZE(password); |
| 3887 | } else { |
| 3888 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3889 | goto error; |
| 3890 | } |
| 3891 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3892 | if (size > (Py_ssize_t)INT_MAX) { |
| 3893 | PyErr_Format(PyExc_ValueError, |
| 3894 | "password cannot be longer than %d bytes", INT_MAX); |
| 3895 | goto error; |
| 3896 | } |
| 3897 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3898 | PyMem_Free(pw_info->password); |
| 3899 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3900 | if (!pw_info->password) { |
| 3901 | PyErr_SetString(PyExc_MemoryError, |
| 3902 | "unable to allocate password buffer"); |
| 3903 | goto error; |
| 3904 | } |
| 3905 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3906 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3907 | |
| 3908 | Py_XDECREF(password_bytes); |
| 3909 | return 1; |
| 3910 | |
| 3911 | error: |
| 3912 | Py_XDECREF(password_bytes); |
| 3913 | return 0; |
| 3914 | } |
| 3915 | |
| 3916 | static int |
| 3917 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3918 | { |
| 3919 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3920 | PyObject *fn_ret = NULL; |
| 3921 | |
| 3922 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3923 | |
| 3924 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3925 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3926 | if (!fn_ret) { |
| 3927 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3928 | core python API, so we could use it to add a frame here */ |
| 3929 | goto error; |
| 3930 | } |
| 3931 | |
| 3932 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3933 | "password callback must return a string")) { |
| 3934 | goto error; |
| 3935 | } |
| 3936 | Py_CLEAR(fn_ret); |
| 3937 | } |
| 3938 | |
| 3939 | if (pw_info->size > size) { |
| 3940 | PyErr_Format(PyExc_ValueError, |
| 3941 | "password cannot be longer than %d bytes", size); |
| 3942 | goto error; |
| 3943 | } |
| 3944 | |
| 3945 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3946 | memcpy(buf, pw_info->password, pw_info->size); |
| 3947 | return pw_info->size; |
| 3948 | |
| 3949 | error: |
| 3950 | Py_XDECREF(fn_ret); |
| 3951 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3952 | pw_info->error = 1; |
| 3953 | return -1; |
| 3954 | } |
| 3955 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3956 | /*[clinic input] |
| 3957 | _ssl._SSLContext.load_cert_chain |
| 3958 | certfile: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3959 | keyfile: object = None |
| 3960 | password: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3961 | |
| 3962 | [clinic start generated code]*/ |
| 3963 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3964 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3965 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3966 | PyObject *keyfile, PyObject *password) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3967 | /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3968 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3969 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3970 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3971 | 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] | 3972 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3973 | int r; |
| 3974 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3975 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3976 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3977 | if (keyfile == Py_None) |
| 3978 | keyfile = NULL; |
| 3979 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3980 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3981 | PyErr_SetString(PyExc_TypeError, |
| 3982 | "certfile should be a valid filesystem path"); |
| 3983 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3984 | return NULL; |
| 3985 | } |
| 3986 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3987 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3988 | PyErr_SetString(PyExc_TypeError, |
| 3989 | "keyfile should be a valid filesystem path"); |
| 3990 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3991 | goto error; |
| 3992 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3993 | if (password != Py_None) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3994 | if (PyCallable_Check(password)) { |
| 3995 | pw_info.callable = password; |
| 3996 | } else if (!_pwinfo_set(&pw_info, password, |
| 3997 | "password should be a string or callable")) { |
| 3998 | goto error; |
| 3999 | } |
| 4000 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 4001 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 4002 | } |
| 4003 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4004 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 4005 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4006 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4007 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4008 | if (pw_info.error) { |
| 4009 | ERR_clear_error(); |
| 4010 | /* the password callback has already set the error information */ |
| 4011 | } |
| 4012 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 4013 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4014 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4015 | } |
| 4016 | else { |
| 4017 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4018 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4019 | goto error; |
| 4020 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4021 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 4022 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4023 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 4024 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4025 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 4026 | Py_CLEAR(keyfile_bytes); |
| 4027 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4028 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4029 | if (pw_info.error) { |
| 4030 | ERR_clear_error(); |
| 4031 | /* the password callback has already set the error information */ |
| 4032 | } |
| 4033 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 4034 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4035 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4036 | } |
| 4037 | else { |
| 4038 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4039 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4040 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4041 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4042 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4043 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4044 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4045 | if (r != 1) { |
| 4046 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4047 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4048 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4049 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4050 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4051 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4052 | Py_RETURN_NONE; |
| 4053 | |
| 4054 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4055 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4056 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4057 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4058 | Py_XDECREF(keyfile_bytes); |
| 4059 | Py_XDECREF(certfile_bytes); |
| 4060 | return NULL; |
| 4061 | } |
| 4062 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4063 | /* internal helper function, returns -1 on error |
| 4064 | */ |
| 4065 | static int |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 4066 | _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4067 | int filetype) |
| 4068 | { |
| 4069 | BIO *biobuf = NULL; |
| 4070 | X509_STORE *store; |
| 4071 | int retval = 0, err, loaded = 0; |
| 4072 | |
| 4073 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 4074 | |
| 4075 | if (len <= 0) { |
| 4076 | PyErr_SetString(PyExc_ValueError, |
| 4077 | "Empty certificate data"); |
| 4078 | return -1; |
| 4079 | } else if (len > INT_MAX) { |
| 4080 | PyErr_SetString(PyExc_OverflowError, |
| 4081 | "Certificate data is too long."); |
| 4082 | return -1; |
| 4083 | } |
| 4084 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 4085 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4086 | if (biobuf == NULL) { |
| 4087 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 4088 | return -1; |
| 4089 | } |
| 4090 | |
| 4091 | store = SSL_CTX_get_cert_store(self->ctx); |
| 4092 | assert(store != NULL); |
| 4093 | |
| 4094 | while (1) { |
| 4095 | X509 *cert = NULL; |
| 4096 | int r; |
| 4097 | |
| 4098 | if (filetype == SSL_FILETYPE_ASN1) { |
| 4099 | cert = d2i_X509_bio(biobuf, NULL); |
| 4100 | } else { |
| 4101 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4102 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 4103 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 4104 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4105 | } |
| 4106 | if (cert == NULL) { |
| 4107 | break; |
| 4108 | } |
| 4109 | r = X509_STORE_add_cert(store, cert); |
| 4110 | X509_free(cert); |
| 4111 | if (!r) { |
| 4112 | err = ERR_peek_last_error(); |
| 4113 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 4114 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 4115 | /* cert already in hash table, not an error */ |
| 4116 | ERR_clear_error(); |
| 4117 | } else { |
| 4118 | break; |
| 4119 | } |
| 4120 | } |
| 4121 | loaded++; |
| 4122 | } |
| 4123 | |
| 4124 | err = ERR_peek_last_error(); |
| 4125 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 4126 | (loaded > 0) && |
| 4127 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 4128 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 4129 | /* EOF ASN1 file, not an error */ |
| 4130 | ERR_clear_error(); |
| 4131 | retval = 0; |
| 4132 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 4133 | (loaded > 0) && |
| 4134 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 4135 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 4136 | /* EOF PEM file, not an error */ |
| 4137 | ERR_clear_error(); |
| 4138 | retval = 0; |
| 4139 | } else { |
| 4140 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4141 | retval = -1; |
| 4142 | } |
| 4143 | |
| 4144 | BIO_free(biobuf); |
| 4145 | return retval; |
| 4146 | } |
| 4147 | |
| 4148 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4149 | /*[clinic input] |
| 4150 | _ssl._SSLContext.load_verify_locations |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4151 | cafile: object = None |
| 4152 | capath: object = None |
| 4153 | cadata: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4154 | |
| 4155 | [clinic start generated code]*/ |
| 4156 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4157 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4158 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 4159 | PyObject *cafile, |
| 4160 | PyObject *capath, |
| 4161 | PyObject *cadata) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4162 | /*[clinic end generated code: output=454c7e41230ca551 input=42ecfe258233e194]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4163 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4164 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 4165 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4166 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4167 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4168 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4169 | if (cafile == Py_None) |
| 4170 | cafile = NULL; |
| 4171 | if (capath == Py_None) |
| 4172 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4173 | if (cadata == Py_None) |
| 4174 | cadata = NULL; |
| 4175 | |
| 4176 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4177 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4178 | "cafile, capath and cadata cannot be all omitted"); |
| 4179 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4180 | } |
| 4181 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4182 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4183 | PyErr_SetString(PyExc_TypeError, |
| 4184 | "cafile should be a valid filesystem path"); |
| 4185 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4186 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4187 | } |
| 4188 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4189 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4190 | PyErr_SetString(PyExc_TypeError, |
| 4191 | "capath should be a valid filesystem path"); |
| 4192 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4193 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4194 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4195 | |
| 4196 | /* validata cadata type and load cadata */ |
| 4197 | if (cadata) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4198 | if (PyUnicode_Check(cadata)) { |
| 4199 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4200 | if (cadata_ascii == NULL) { |
| 4201 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4202 | goto invalid_cadata; |
| 4203 | } |
| 4204 | goto error; |
| 4205 | } |
| 4206 | r = _add_ca_certs(self, |
| 4207 | PyBytes_AS_STRING(cadata_ascii), |
| 4208 | PyBytes_GET_SIZE(cadata_ascii), |
| 4209 | SSL_FILETYPE_PEM); |
| 4210 | Py_DECREF(cadata_ascii); |
| 4211 | if (r == -1) { |
| 4212 | goto error; |
| 4213 | } |
| 4214 | } |
| 4215 | else if (PyObject_CheckBuffer(cadata)) { |
| 4216 | Py_buffer buf; |
| 4217 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 4218 | goto error; |
| 4219 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4220 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4221 | PyBuffer_Release(&buf); |
| 4222 | PyErr_SetString(PyExc_TypeError, |
| 4223 | "cadata should be a contiguous buffer with " |
| 4224 | "a single dimension"); |
| 4225 | goto error; |
| 4226 | } |
| 4227 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4228 | PyBuffer_Release(&buf); |
| 4229 | if (r == -1) { |
| 4230 | goto error; |
| 4231 | } |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4232 | } |
| 4233 | else { |
| 4234 | invalid_cadata: |
| 4235 | PyErr_SetString(PyExc_TypeError, |
| 4236 | "cadata should be an ASCII string or a " |
| 4237 | "bytes-like object"); |
| 4238 | goto error; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | /* load cafile or capath */ |
| 4243 | if (cafile || capath) { |
| 4244 | if (cafile) |
| 4245 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4246 | if (capath) |
| 4247 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4248 | PySSL_BEGIN_ALLOW_THREADS |
| 4249 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4250 | PySSL_END_ALLOW_THREADS |
| 4251 | if (r != 1) { |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4252 | if (errno != 0) { |
| 4253 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4254 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4255 | } |
| 4256 | else { |
| 4257 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4258 | } |
| 4259 | goto error; |
| 4260 | } |
| 4261 | } |
| 4262 | goto end; |
| 4263 | |
| 4264 | error: |
| 4265 | ok = 0; |
| 4266 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4267 | Py_XDECREF(cafile_bytes); |
| 4268 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4269 | if (ok) { |
| 4270 | Py_RETURN_NONE; |
| 4271 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4272 | return NULL; |
| 4273 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4276 | /*[clinic input] |
| 4277 | _ssl._SSLContext.load_dh_params |
| 4278 | path as filepath: object |
| 4279 | / |
| 4280 | |
| 4281 | [clinic start generated code]*/ |
| 4282 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4283 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4284 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4285 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4286 | { |
| 4287 | FILE *f; |
| 4288 | DH *dh; |
| 4289 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4290 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4291 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4292 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4293 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4294 | errno = 0; |
| 4295 | PySSL_BEGIN_ALLOW_THREADS |
| 4296 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4297 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4298 | PySSL_END_ALLOW_THREADS |
| 4299 | if (dh == NULL) { |
| 4300 | if (errno != 0) { |
| 4301 | ERR_clear_error(); |
| 4302 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4303 | } |
| 4304 | else { |
| 4305 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4306 | } |
| 4307 | return NULL; |
| 4308 | } |
Zackery Spytz | aebc049 | 2020-07-07 22:21:58 -0600 | [diff] [blame] | 4309 | if (!SSL_CTX_set_tmp_dh(self->ctx, dh)) { |
| 4310 | DH_free(dh); |
| 4311 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4312 | } |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4313 | DH_free(dh); |
| 4314 | Py_RETURN_NONE; |
| 4315 | } |
| 4316 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4317 | /*[clinic input] |
| 4318 | _ssl._SSLContext._wrap_socket |
| 4319 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 4320 | server_side: int |
| 4321 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4322 | * |
| 4323 | owner: object = None |
| 4324 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4325 | |
| 4326 | [clinic start generated code]*/ |
| 4327 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4328 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4329 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4330 | int server_side, PyObject *hostname_obj, |
| 4331 | PyObject *owner, PyObject *session) |
| 4332 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4333 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4334 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4335 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4337 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4338 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4339 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4340 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4341 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4342 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4343 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4344 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4345 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4346 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4347 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4348 | if (hostname != NULL) |
| 4349 | PyMem_Free(hostname); |
| 4350 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4353 | /*[clinic input] |
| 4354 | _ssl._SSLContext._wrap_bio |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4355 | incoming: object(subclass_of="PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4356 | outgoing: object(subclass_of="PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4357 | server_side: int |
| 4358 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4359 | * |
| 4360 | owner: object = None |
| 4361 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4362 | |
| 4363 | [clinic start generated code]*/ |
| 4364 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4365 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4366 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4367 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4368 | PyObject *hostname_obj, PyObject *owner, |
| 4369 | PyObject *session) |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4370 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=63867b8f3e1a1aa3]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4371 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4372 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4373 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4374 | |
| 4375 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4376 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4377 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4378 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4379 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4383 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4384 | incoming, outgoing); |
| 4385 | |
| 4386 | PyMem_Free(hostname); |
| 4387 | return res; |
| 4388 | } |
| 4389 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4390 | /*[clinic input] |
| 4391 | _ssl._SSLContext.session_stats |
| 4392 | [clinic start generated code]*/ |
| 4393 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4394 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4395 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4396 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4397 | { |
| 4398 | int r; |
| 4399 | PyObject *value, *stats = PyDict_New(); |
| 4400 | if (!stats) |
| 4401 | return NULL; |
| 4402 | |
| 4403 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4404 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4405 | if (value == NULL) \ |
| 4406 | goto error; \ |
| 4407 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4408 | Py_DECREF(value); \ |
| 4409 | if (r < 0) \ |
| 4410 | goto error; |
| 4411 | |
| 4412 | ADD_STATS(number, "number"); |
| 4413 | ADD_STATS(connect, "connect"); |
| 4414 | ADD_STATS(connect_good, "connect_good"); |
| 4415 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4416 | ADD_STATS(accept, "accept"); |
| 4417 | ADD_STATS(accept_good, "accept_good"); |
| 4418 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4419 | ADD_STATS(accept, "accept"); |
| 4420 | ADD_STATS(hits, "hits"); |
| 4421 | ADD_STATS(misses, "misses"); |
| 4422 | ADD_STATS(timeouts, "timeouts"); |
| 4423 | ADD_STATS(cache_full, "cache_full"); |
| 4424 | |
| 4425 | #undef ADD_STATS |
| 4426 | |
| 4427 | return stats; |
| 4428 | |
| 4429 | error: |
| 4430 | Py_DECREF(stats); |
| 4431 | return NULL; |
| 4432 | } |
| 4433 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4434 | /*[clinic input] |
| 4435 | _ssl._SSLContext.set_default_verify_paths |
| 4436 | [clinic start generated code]*/ |
| 4437 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4438 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4439 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4440 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4441 | { |
| 4442 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 4443 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4444 | return NULL; |
| 4445 | } |
| 4446 | Py_RETURN_NONE; |
| 4447 | } |
| 4448 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4449 | #ifndef OPENSSL_NO_ECDH |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4450 | /*[clinic input] |
| 4451 | _ssl._SSLContext.set_ecdh_curve |
| 4452 | name: object |
| 4453 | / |
| 4454 | |
| 4455 | [clinic start generated code]*/ |
| 4456 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4457 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4458 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4459 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4460 | { |
| 4461 | PyObject *name_bytes; |
| 4462 | int nid; |
| 4463 | EC_KEY *key; |
| 4464 | |
| 4465 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4466 | return NULL; |
| 4467 | assert(PyBytes_Check(name_bytes)); |
| 4468 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4469 | Py_DECREF(name_bytes); |
| 4470 | if (nid == 0) { |
| 4471 | PyErr_Format(PyExc_ValueError, |
| 4472 | "unknown elliptic curve name %R", name); |
| 4473 | return NULL; |
| 4474 | } |
| 4475 | key = EC_KEY_new_by_curve_name(nid); |
| 4476 | if (key == NULL) { |
| 4477 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4478 | return NULL; |
| 4479 | } |
| 4480 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4481 | EC_KEY_free(key); |
| 4482 | Py_RETURN_NONE; |
| 4483 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4484 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4485 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4486 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4487 | static int |
| 4488 | _servername_callback(SSL *s, int *al, void *args) |
| 4489 | { |
| 4490 | int ret; |
| 4491 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 4492 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4493 | PyObject *result; |
| 4494 | /* The high-level ssl.SSLSocket object */ |
| 4495 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4496 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4497 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4498 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4499 | if (ssl_ctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4500 | /* remove race condition in this the call back while if removing the |
| 4501 | * callback is in progress */ |
| 4502 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4503 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | ssl = SSL_get_app_data(s); |
| 4507 | assert(PySSLSocket_Check(ssl)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4508 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4509 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4510 | * SSL connection and that has a .context attribute that can be changed to |
| 4511 | * identify the requested hostname. Since the official API is the Python |
| 4512 | * level API we want to pass the callback a Python level object rather than |
| 4513 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4514 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4515 | * will be passed. If both do not exist only then the C-level object is |
| 4516 | * passed. */ |
| 4517 | if (ssl->owner) |
| 4518 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4519 | else if (ssl->Socket) |
| 4520 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4521 | else |
| 4522 | ssl_socket = (PyObject *) ssl; |
| 4523 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4524 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4525 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4526 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4527 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4528 | if (servername == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4529 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4530 | Py_None, ssl_ctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4531 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4532 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4533 | PyObject *servername_bytes; |
| 4534 | PyObject *servername_str; |
| 4535 | |
| 4536 | servername_bytes = PyBytes_FromString(servername); |
| 4537 | if (servername_bytes == NULL) { |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4538 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 4539 | goto error; |
| 4540 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4541 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4542 | * back into a str object, but still as an A-label (bpo-28414) |
| 4543 | */ |
| 4544 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4545 | if (servername_str == NULL) { |
| 4546 | PyErr_WriteUnraisable(servername_bytes); |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4547 | Py_DECREF(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4548 | goto error; |
| 4549 | } |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4550 | Py_DECREF(servername_bytes); |
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}, |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 4793 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 4794 | {"security_level", (getter) get_security_level, |
| 4795 | NULL, PySSLContext_security_level_doc}, |
| 4796 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4797 | {NULL}, /* sentinel */ |
| 4798 | }; |
| 4799 | |
| 4800 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4801 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4802 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4803 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4804 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4805 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4806 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4807 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4808 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4809 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4810 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4811 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4812 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4813 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4814 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4815 | {NULL, NULL} /* sentinel */ |
| 4816 | }; |
| 4817 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4818 | static PyType_Slot PySSLContext_slots[] = { |
| 4819 | {Py_tp_methods, context_methods}, |
| 4820 | {Py_tp_getset, context_getsetlist}, |
| 4821 | {Py_tp_new, _ssl__SSLContext}, |
| 4822 | {Py_tp_dealloc, context_dealloc}, |
| 4823 | {Py_tp_traverse, context_traverse}, |
| 4824 | {Py_tp_clear, context_clear}, |
| 4825 | {0, 0}, |
| 4826 | }; |
| 4827 | |
| 4828 | static PyType_Spec PySSLContext_spec = { |
| 4829 | "_ssl._SSLContext", |
| 4830 | sizeof(PySSLContext), |
| 4831 | 0, |
| 4832 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4833 | PySSLContext_slots, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4834 | }; |
| 4835 | |
| 4836 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4837 | /* |
| 4838 | * MemoryBIO objects |
| 4839 | */ |
| 4840 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4841 | /*[clinic input] |
| 4842 | @classmethod |
| 4843 | _ssl.MemoryBIO.__new__ |
| 4844 | |
| 4845 | [clinic start generated code]*/ |
| 4846 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4847 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4848 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4849 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4850 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4851 | BIO *bio; |
| 4852 | PySSLMemoryBIO *self; |
| 4853 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4854 | bio = BIO_new(BIO_s_mem()); |
| 4855 | if (bio == NULL) { |
| 4856 | PyErr_SetString(PySSLErrorObject, |
| 4857 | "failed to allocate BIO"); |
| 4858 | return NULL; |
| 4859 | } |
| 4860 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4861 | * just that no data is currently available. The SSL routines should retry |
| 4862 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4863 | BIO_set_retry_read(bio); |
| 4864 | BIO_set_mem_eof_return(bio, -1); |
| 4865 | |
| 4866 | assert(type != NULL && type->tp_alloc != NULL); |
| 4867 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4868 | if (self == NULL) { |
| 4869 | BIO_free(bio); |
| 4870 | return NULL; |
| 4871 | } |
| 4872 | self->bio = bio; |
| 4873 | self->eof_written = 0; |
| 4874 | |
| 4875 | return (PyObject *) self; |
| 4876 | } |
| 4877 | |
| 4878 | static void |
| 4879 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4880 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4881 | PyTypeObject *tp = Py_TYPE(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4882 | BIO_free(self->bio); |
| 4883 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4884 | Py_DECREF(tp); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | static PyObject * |
| 4888 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4889 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4890 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4894 | "The number of bytes pending in the memory BIO."); |
| 4895 | |
| 4896 | static PyObject * |
| 4897 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4898 | { |
| 4899 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4900 | && self->eof_written); |
| 4901 | } |
| 4902 | |
| 4903 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4904 | "Whether the memory BIO is at EOF."); |
| 4905 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4906 | /*[clinic input] |
| 4907 | _ssl.MemoryBIO.read |
| 4908 | size as len: int = -1 |
| 4909 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4910 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4911 | Read up to size bytes from the memory BIO. |
| 4912 | |
| 4913 | If size is not specified, read the entire buffer. |
| 4914 | If the return value is an empty bytes instance, this means either |
| 4915 | EOF or that no data is available. Use the "eof" property to |
| 4916 | distinguish between the two. |
| 4917 | [clinic start generated code]*/ |
| 4918 | |
| 4919 | static PyObject * |
| 4920 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4921 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4922 | { |
| 4923 | int avail, nbytes; |
| 4924 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4925 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4926 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4927 | if ((len < 0) || (len > avail)) |
| 4928 | len = avail; |
| 4929 | |
| 4930 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4931 | if ((result == NULL) || (len == 0)) |
| 4932 | return result; |
| 4933 | |
| 4934 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4935 | if (nbytes < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4936 | Py_DECREF(result); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4937 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4938 | return NULL; |
| 4939 | } |
| 4940 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4941 | /* There should never be any short reads but check anyway. */ |
| 4942 | if (nbytes < len) { |
| 4943 | _PyBytes_Resize(&result, nbytes); |
| 4944 | } |
| 4945 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4946 | return result; |
| 4947 | } |
| 4948 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4949 | /*[clinic input] |
| 4950 | _ssl.MemoryBIO.write |
| 4951 | b: Py_buffer |
| 4952 | / |
| 4953 | |
| 4954 | Writes the bytes b into the memory BIO. |
| 4955 | |
| 4956 | Returns the number of bytes written. |
| 4957 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4958 | |
| 4959 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4960 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4961 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4962 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4963 | int nbytes; |
| 4964 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4965 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4966 | PyErr_Format(PyExc_OverflowError, |
| 4967 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4968 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | if (self->eof_written) { |
| 4972 | PyErr_SetString(PySSLErrorObject, |
| 4973 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4974 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4975 | } |
| 4976 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4977 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4978 | if (nbytes < 0) { |
| 4979 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4980 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4981 | } |
| 4982 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4983 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4984 | } |
| 4985 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4986 | /*[clinic input] |
| 4987 | _ssl.MemoryBIO.write_eof |
| 4988 | |
| 4989 | Write an EOF marker to the memory BIO. |
| 4990 | |
| 4991 | When all data has been read, the "eof" property will be True. |
| 4992 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4993 | |
| 4994 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4995 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4996 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4997 | { |
| 4998 | self->eof_written = 1; |
| 4999 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 5000 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 5001 | BIO_clear_retry_flags(self->bio); |
| 5002 | BIO_set_mem_eof_return(self->bio, 0); |
| 5003 | |
| 5004 | Py_RETURN_NONE; |
| 5005 | } |
| 5006 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5007 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 5008 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 5009 | PySSL_memory_bio_pending_doc}, |
| 5010 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 5011 | PySSL_memory_bio_eof_doc}, |
| 5012 | {NULL}, /* sentinel */ |
| 5013 | }; |
| 5014 | |
| 5015 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5016 | _SSL_MEMORYBIO_READ_METHODDEF |
| 5017 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 5018 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5019 | {NULL, NULL} /* sentinel */ |
| 5020 | }; |
| 5021 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5022 | static PyType_Slot PySSLMemoryBIO_slots[] = { |
| 5023 | {Py_tp_methods, memory_bio_methods}, |
| 5024 | {Py_tp_getset, memory_bio_getsetlist}, |
| 5025 | {Py_tp_new, _ssl_MemoryBIO}, |
| 5026 | {Py_tp_dealloc, memory_bio_dealloc}, |
| 5027 | {0, 0}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5028 | }; |
| 5029 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5030 | static PyType_Spec PySSLMemoryBIO_spec = { |
| 5031 | "_ssl.MemoryBIO", |
| 5032 | sizeof(PySSLMemoryBIO), |
| 5033 | 0, |
| 5034 | Py_TPFLAGS_DEFAULT, |
| 5035 | PySSLMemoryBIO_slots, |
| 5036 | }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5037 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5038 | /* |
| 5039 | * SSL Session object |
| 5040 | */ |
| 5041 | |
| 5042 | static void |
| 5043 | PySSLSession_dealloc(PySSLSession *self) |
| 5044 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5045 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 5046 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5047 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5048 | Py_XDECREF(self->ctx); |
| 5049 | if (self->session != NULL) { |
| 5050 | SSL_SESSION_free(self->session); |
| 5051 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5052 | PyObject_GC_Del(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5053 | Py_DECREF(tp); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | static PyObject * |
| 5057 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 5058 | { |
| 5059 | int result; |
| 5060 | |
| 5061 | if (left == NULL || right == NULL) { |
| 5062 | PyErr_BadInternalCall(); |
| 5063 | return NULL; |
| 5064 | } |
| 5065 | |
| 5066 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 5067 | Py_RETURN_NOTIMPLEMENTED; |
| 5068 | } |
| 5069 | |
| 5070 | if (left == right) { |
| 5071 | result = 0; |
| 5072 | } else { |
| 5073 | const unsigned char *left_id, *right_id; |
| 5074 | unsigned int left_len, right_len; |
| 5075 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 5076 | &left_len); |
| 5077 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 5078 | &right_len); |
| 5079 | if (left_len == right_len) { |
| 5080 | result = memcmp(left_id, right_id, left_len); |
| 5081 | } else { |
| 5082 | result = 1; |
| 5083 | } |
| 5084 | } |
| 5085 | |
| 5086 | switch (op) { |
| 5087 | case Py_EQ: |
| 5088 | if (result == 0) { |
| 5089 | Py_RETURN_TRUE; |
| 5090 | } else { |
| 5091 | Py_RETURN_FALSE; |
| 5092 | } |
| 5093 | break; |
| 5094 | case Py_NE: |
| 5095 | if (result != 0) { |
| 5096 | Py_RETURN_TRUE; |
| 5097 | } else { |
| 5098 | Py_RETURN_FALSE; |
| 5099 | } |
| 5100 | break; |
| 5101 | case Py_LT: |
| 5102 | case Py_LE: |
| 5103 | case Py_GT: |
| 5104 | case Py_GE: |
| 5105 | Py_RETURN_NOTIMPLEMENTED; |
| 5106 | break; |
| 5107 | default: |
| 5108 | PyErr_BadArgument(); |
| 5109 | return NULL; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | static int |
| 5114 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 5115 | { |
| 5116 | Py_VISIT(self->ctx); |
| 5117 | return 0; |
| 5118 | } |
| 5119 | |
| 5120 | static int |
| 5121 | PySSLSession_clear(PySSLSession *self) |
| 5122 | { |
| 5123 | Py_CLEAR(self->ctx); |
| 5124 | return 0; |
| 5125 | } |
| 5126 | |
| 5127 | |
| 5128 | static PyObject * |
| 5129 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 5130 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 5131 | } |
| 5132 | |
| 5133 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 5134 | "Session creation time (seconds since epoch)."); |
| 5135 | |
| 5136 | |
| 5137 | static PyObject * |
| 5138 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 5139 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 5140 | } |
| 5141 | |
| 5142 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 5143 | "Session timeout (delta in seconds)."); |
| 5144 | |
| 5145 | |
| 5146 | static PyObject * |
| 5147 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 5148 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 5149 | return PyLong_FromUnsignedLong(hint); |
| 5150 | } |
| 5151 | |
| 5152 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 5153 | "Ticket life time hint."); |
| 5154 | |
| 5155 | |
| 5156 | static PyObject * |
| 5157 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 5158 | const unsigned char *id; |
| 5159 | unsigned int len; |
| 5160 | id = SSL_SESSION_get_id(self->session, &len); |
| 5161 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 5162 | } |
| 5163 | |
| 5164 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 5165 | "Session id"); |
| 5166 | |
| 5167 | |
| 5168 | static PyObject * |
| 5169 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 5170 | if (SSL_SESSION_has_ticket(self->session)) { |
| 5171 | Py_RETURN_TRUE; |
| 5172 | } else { |
| 5173 | Py_RETURN_FALSE; |
| 5174 | } |
| 5175 | } |
| 5176 | |
| 5177 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5178 | "Does the session contain a ticket?"); |
| 5179 | |
| 5180 | |
| 5181 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5182 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5183 | PySSLSession_get_has_ticket_doc}, |
| 5184 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5185 | PySSLSession_get_session_id_doc}, |
| 5186 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5187 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5188 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5189 | PySSLSession_get_time_doc}, |
| 5190 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5191 | PySSLSession_get_timeout_doc}, |
| 5192 | {NULL}, /* sentinel */ |
| 5193 | }; |
| 5194 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5195 | static PyType_Slot PySSLSession_slots[] = { |
| 5196 | {Py_tp_getset,PySSLSession_getsetlist}, |
| 5197 | {Py_tp_richcompare, PySSLSession_richcompare}, |
| 5198 | {Py_tp_dealloc, PySSLSession_dealloc}, |
| 5199 | {Py_tp_traverse, PySSLSession_traverse}, |
| 5200 | {Py_tp_clear, PySSLSession_clear}, |
| 5201 | {0, 0}, |
| 5202 | }; |
| 5203 | |
| 5204 | static PyType_Spec PySSLSession_spec = { |
| 5205 | "_ssl.SSLSession", |
| 5206 | sizeof(PySSLSession), |
| 5207 | 0, |
| 5208 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 5209 | PySSLSession_slots, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5210 | }; |
| 5211 | |
| 5212 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5213 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5214 | /*[clinic input] |
| 5215 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5216 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5217 | entropy: double |
| 5218 | / |
| 5219 | |
| 5220 | Mix string into the OpenSSL PRNG state. |
| 5221 | |
| 5222 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5223 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5224 | [clinic start generated code]*/ |
| 5225 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5227 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5228 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5229 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5230 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5231 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5232 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5233 | buf = (const char *)view->buf; |
| 5234 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5235 | do { |
| 5236 | written = Py_MIN(len, INT_MAX); |
| 5237 | RAND_add(buf, (int)written, entropy); |
| 5238 | buf += written; |
| 5239 | len -= written; |
| 5240 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5241 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5244 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5245 | PySSL_RAND(int len, int pseudo) |
| 5246 | { |
| 5247 | int ok; |
| 5248 | PyObject *bytes; |
| 5249 | unsigned long err; |
| 5250 | const char *errstr; |
| 5251 | PyObject *v; |
| 5252 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5253 | if (len < 0) { |
| 5254 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5255 | return NULL; |
| 5256 | } |
| 5257 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5258 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5259 | if (bytes == NULL) |
| 5260 | return NULL; |
| 5261 | if (pseudo) { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 5262 | #ifdef PY_OPENSSL_1_1_API |
| 5263 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5264 | #else |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5265 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 5266 | #endif |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5267 | if (ok == 0 || ok == 1) |
| 5268 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5269 | } |
| 5270 | else { |
| 5271 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5272 | if (ok == 1) |
| 5273 | return bytes; |
| 5274 | } |
| 5275 | Py_DECREF(bytes); |
| 5276 | |
| 5277 | err = ERR_get_error(); |
| 5278 | errstr = ERR_reason_error_string(err); |
| 5279 | v = Py_BuildValue("(ks)", err, errstr); |
| 5280 | if (v != NULL) { |
| 5281 | PyErr_SetObject(PySSLErrorObject, v); |
| 5282 | Py_DECREF(v); |
| 5283 | } |
| 5284 | return NULL; |
| 5285 | } |
| 5286 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5287 | /*[clinic input] |
| 5288 | _ssl.RAND_bytes |
| 5289 | n: int |
| 5290 | / |
| 5291 | |
| 5292 | Generate n cryptographically strong pseudo-random bytes. |
| 5293 | [clinic start generated code]*/ |
| 5294 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5295 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5296 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5297 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5298 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5299 | return PySSL_RAND(n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5300 | } |
| 5301 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5302 | /*[clinic input] |
| 5303 | _ssl.RAND_pseudo_bytes |
| 5304 | n: int |
| 5305 | / |
| 5306 | |
| 5307 | Generate n pseudo-random bytes. |
| 5308 | |
| 5309 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5310 | if the bytes generated are cryptographically strong. |
| 5311 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5312 | |
| 5313 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5314 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5315 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5316 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5317 | return PySSL_RAND(n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5318 | } |
| 5319 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5320 | /*[clinic input] |
| 5321 | _ssl.RAND_status |
| 5322 | |
| 5323 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 5324 | |
| 5325 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5326 | using the ssl() function. |
| 5327 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5328 | |
| 5329 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5330 | _ssl_RAND_status_impl(PyObject *module) |
| 5331 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5332 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5333 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5336 | #ifndef OPENSSL_NO_EGD |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5337 | /* LCOV_EXCL_START */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5338 | /*[clinic input] |
| 5339 | _ssl.RAND_egd |
| 5340 | path: object(converter="PyUnicode_FSConverter") |
| 5341 | / |
| 5342 | |
| 5343 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 5344 | |
| 5345 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 5346 | fails or if it does not provide enough data to seed PRNG. |
| 5347 | [clinic start generated code]*/ |
| 5348 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5349 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5350 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 5351 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5352 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5353 | int bytes = RAND_egd(PyBytes_AsString(path)); |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5354 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5355 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5356 | PyErr_SetString(PySSLErrorObject, |
| 5357 | "EGD connection failed or EGD did not return " |
| 5358 | "enough data to seed the PRNG"); |
| 5359 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5360 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5361 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5362 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5363 | /* LCOV_EXCL_STOP */ |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5364 | #endif /* OPENSSL_NO_EGD */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5365 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5366 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5367 | |
| 5368 | /*[clinic input] |
| 5369 | _ssl.get_default_verify_paths |
| 5370 | |
| 5371 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5372 | |
| 5373 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5374 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5375 | |
| 5376 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5377 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5378 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5379 | { |
| 5380 | PyObject *ofile_env = NULL; |
| 5381 | PyObject *ofile = NULL; |
| 5382 | PyObject *odir_env = NULL; |
| 5383 | PyObject *odir = NULL; |
| 5384 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5385 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5386 | const char *tmp = (info); \ |
| 5387 | target = NULL; \ |
| 5388 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5389 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5390 | target = PyBytes_FromString(tmp); } \ |
| 5391 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5392 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5393 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5394 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5395 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5396 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5397 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5398 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5399 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5400 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5401 | |
| 5402 | error: |
| 5403 | Py_XDECREF(ofile_env); |
| 5404 | Py_XDECREF(ofile); |
| 5405 | Py_XDECREF(odir_env); |
| 5406 | Py_XDECREF(odir); |
| 5407 | return NULL; |
| 5408 | } |
| 5409 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5410 | static PyObject* |
| 5411 | asn1obj2py(ASN1_OBJECT *obj) |
| 5412 | { |
| 5413 | int nid; |
| 5414 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5415 | |
| 5416 | nid = OBJ_obj2nid(obj); |
| 5417 | if (nid == NID_undef) { |
| 5418 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5419 | return NULL; |
| 5420 | } |
| 5421 | sn = OBJ_nid2sn(nid); |
| 5422 | ln = OBJ_nid2ln(nid); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5423 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5426 | /*[clinic input] |
| 5427 | _ssl.txt2obj |
| 5428 | txt: str |
| 5429 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5430 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5431 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5432 | |
| 5433 | By default objects are looked up by OID. With name=True short and |
| 5434 | long name are also matched. |
| 5435 | [clinic start generated code]*/ |
| 5436 | |
| 5437 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5438 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5439 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5440 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5441 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5442 | ASN1_OBJECT *obj; |
| 5443 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5444 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5445 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5446 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5447 | return NULL; |
| 5448 | } |
| 5449 | result = asn1obj2py(obj); |
| 5450 | ASN1_OBJECT_free(obj); |
| 5451 | return result; |
| 5452 | } |
| 5453 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5454 | /*[clinic input] |
| 5455 | _ssl.nid2obj |
| 5456 | nid: int |
| 5457 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5458 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5459 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5460 | [clinic start generated code]*/ |
| 5461 | |
| 5462 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5463 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5464 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5465 | { |
| 5466 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5467 | ASN1_OBJECT *obj; |
| 5468 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5469 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5470 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5471 | return NULL; |
| 5472 | } |
| 5473 | obj = OBJ_nid2obj(nid); |
| 5474 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5475 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5476 | return NULL; |
| 5477 | } |
| 5478 | result = asn1obj2py(obj); |
| 5479 | ASN1_OBJECT_free(obj); |
| 5480 | return result; |
| 5481 | } |
| 5482 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5483 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5484 | |
| 5485 | static PyObject* |
| 5486 | certEncodingType(DWORD encodingType) |
| 5487 | { |
| 5488 | static PyObject *x509_asn = NULL; |
| 5489 | static PyObject *pkcs_7_asn = NULL; |
| 5490 | |
| 5491 | if (x509_asn == NULL) { |
| 5492 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5493 | if (x509_asn == NULL) |
| 5494 | return NULL; |
| 5495 | } |
| 5496 | if (pkcs_7_asn == NULL) { |
| 5497 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5498 | if (pkcs_7_asn == NULL) |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | switch(encodingType) { |
| 5502 | case X509_ASN_ENCODING: |
| 5503 | Py_INCREF(x509_asn); |
| 5504 | return x509_asn; |
| 5505 | case PKCS_7_ASN_ENCODING: |
| 5506 | Py_INCREF(pkcs_7_asn); |
| 5507 | return pkcs_7_asn; |
| 5508 | default: |
| 5509 | return PyLong_FromLong(encodingType); |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | static PyObject* |
| 5514 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5515 | { |
| 5516 | CERT_ENHKEY_USAGE *usage; |
| 5517 | DWORD size, error, i; |
| 5518 | PyObject *retval; |
| 5519 | |
| 5520 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5521 | error = GetLastError(); |
| 5522 | if (error == CRYPT_E_NOT_FOUND) { |
| 5523 | Py_RETURN_TRUE; |
| 5524 | } |
| 5525 | return PyErr_SetFromWindowsErr(error); |
| 5526 | } |
| 5527 | |
| 5528 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5529 | if (usage == NULL) { |
| 5530 | return PyErr_NoMemory(); |
| 5531 | } |
| 5532 | |
| 5533 | /* Now get the actual enhanced usage property */ |
| 5534 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5535 | PyMem_Free(usage); |
| 5536 | error = GetLastError(); |
| 5537 | if (error == CRYPT_E_NOT_FOUND) { |
| 5538 | Py_RETURN_TRUE; |
| 5539 | } |
| 5540 | return PyErr_SetFromWindowsErr(error); |
| 5541 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5542 | retval = PyFrozenSet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5543 | if (retval == NULL) { |
| 5544 | goto error; |
| 5545 | } |
| 5546 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5547 | if (usage->rgpszUsageIdentifier[i]) { |
| 5548 | PyObject *oid; |
| 5549 | int err; |
| 5550 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5551 | if (oid == NULL) { |
| 5552 | Py_CLEAR(retval); |
| 5553 | goto error; |
| 5554 | } |
| 5555 | err = PySet_Add(retval, oid); |
| 5556 | Py_DECREF(oid); |
| 5557 | if (err == -1) { |
| 5558 | Py_CLEAR(retval); |
| 5559 | goto error; |
| 5560 | } |
| 5561 | } |
| 5562 | } |
| 5563 | error: |
| 5564 | PyMem_Free(usage); |
| 5565 | return retval; |
| 5566 | } |
| 5567 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5568 | static HCERTSTORE |
| 5569 | ssl_collect_certificates(const char *store_name) |
| 5570 | { |
| 5571 | /* this function collects the system certificate stores listed in |
| 5572 | * system_stores into a collection certificate store for being |
| 5573 | * enumerated. The store must be readable to be added to the |
| 5574 | * store collection. |
| 5575 | */ |
| 5576 | |
| 5577 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5578 | static DWORD system_stores[] = { |
| 5579 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5580 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5581 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5582 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5583 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5584 | CERT_SYSTEM_STORE_SERVICES, |
| 5585 | CERT_SYSTEM_STORE_USERS}; |
| 5586 | size_t i, storesAdded; |
| 5587 | BOOL result; |
| 5588 | |
| 5589 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5590 | (HCRYPTPROV)NULL, 0, NULL); |
| 5591 | if (!hCollectionStore) { |
| 5592 | return NULL; |
| 5593 | } |
| 5594 | storesAdded = 0; |
| 5595 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5596 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5597 | (HCRYPTPROV)NULL, |
| 5598 | CERT_STORE_READONLY_FLAG | |
| 5599 | system_stores[i], store_name); |
| 5600 | if (hSystemStore) { |
| 5601 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5602 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5603 | if (result) { |
| 5604 | ++storesAdded; |
| 5605 | } |
neonene | ed70129 | 2019-09-09 21:33:43 +0900 | [diff] [blame] | 5606 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5607 | } |
| 5608 | } |
| 5609 | if (storesAdded == 0) { |
| 5610 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5611 | return NULL; |
| 5612 | } |
| 5613 | |
| 5614 | return hCollectionStore; |
| 5615 | } |
| 5616 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5617 | /*[clinic input] |
| 5618 | _ssl.enum_certificates |
| 5619 | store_name: str |
| 5620 | |
| 5621 | Retrieve certificates from Windows' cert store. |
| 5622 | |
| 5623 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5624 | more cert storages, too. The function returns a list of (bytes, |
| 5625 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5626 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5627 | a set of OIDs or the boolean True. |
| 5628 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5629 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5630 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5631 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5632 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5633 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5634 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5635 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5636 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5637 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5638 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5639 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5640 | if (result == NULL) { |
| 5641 | return NULL; |
| 5642 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5643 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5644 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5645 | Py_DECREF(result); |
| 5646 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5647 | } |
| 5648 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5649 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5650 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5651 | pCertCtx->cbCertEncoded); |
| 5652 | if (!cert) { |
| 5653 | Py_CLEAR(result); |
| 5654 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5655 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5656 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5657 | Py_CLEAR(result); |
| 5658 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5659 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5660 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5661 | if (keyusage == Py_True) { |
| 5662 | Py_DECREF(keyusage); |
| 5663 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5664 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5665 | if (keyusage == NULL) { |
| 5666 | Py_CLEAR(result); |
| 5667 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5668 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5669 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5670 | Py_CLEAR(result); |
| 5671 | break; |
| 5672 | } |
| 5673 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5674 | cert = NULL; |
| 5675 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5676 | enc = NULL; |
| 5677 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5678 | keyusage = NULL; |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5679 | if (PySet_Add(result, tup) == -1) { |
| 5680 | Py_CLEAR(result); |
| 5681 | Py_CLEAR(tup); |
| 5682 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5683 | } |
| 5684 | Py_CLEAR(tup); |
| 5685 | } |
| 5686 | if (pCertCtx) { |
| 5687 | /* loop ended with an error, need to clean up context manually */ |
| 5688 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5689 | } |
| 5690 | |
| 5691 | /* In error cases cert, enc and tup may not be NULL */ |
| 5692 | Py_XDECREF(cert); |
| 5693 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5694 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5695 | Py_XDECREF(tup); |
| 5696 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5697 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5698 | associated with the store, in this case our collection store and the |
| 5699 | associated system stores. */ |
| 5700 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5701 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5702 | Py_XDECREF(result); |
| 5703 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5704 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5705 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5706 | /* convert set to list */ |
| 5707 | if (result == NULL) { |
| 5708 | return NULL; |
| 5709 | } else { |
| 5710 | PyObject *lst = PySequence_List(result); |
| 5711 | Py_DECREF(result); |
| 5712 | return lst; |
| 5713 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5714 | } |
| 5715 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5716 | /*[clinic input] |
| 5717 | _ssl.enum_crls |
| 5718 | store_name: str |
| 5719 | |
| 5720 | Retrieve CRLs from Windows' cert store. |
| 5721 | |
| 5722 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5723 | more cert storages, too. The function returns a list of (bytes, |
| 5724 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5725 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5726 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5727 | |
| 5728 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5729 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5730 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5731 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5732 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5733 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5734 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5735 | PyObject *result = NULL; |
| 5736 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5737 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5738 | if (result == NULL) { |
| 5739 | return NULL; |
| 5740 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5741 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5742 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5743 | Py_DECREF(result); |
| 5744 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5745 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5746 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5747 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5748 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5749 | pCrlCtx->cbCrlEncoded); |
| 5750 | if (!crl) { |
| 5751 | Py_CLEAR(result); |
| 5752 | break; |
| 5753 | } |
| 5754 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5755 | Py_CLEAR(result); |
| 5756 | break; |
| 5757 | } |
| 5758 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5759 | Py_CLEAR(result); |
| 5760 | break; |
| 5761 | } |
| 5762 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5763 | crl = NULL; |
| 5764 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5765 | enc = NULL; |
| 5766 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5767 | if (PySet_Add(result, tup) == -1) { |
| 5768 | Py_CLEAR(result); |
| 5769 | Py_CLEAR(tup); |
| 5770 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5771 | } |
| 5772 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5773 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5774 | if (pCrlCtx) { |
| 5775 | /* loop ended with an error, need to clean up context manually */ |
| 5776 | CertFreeCRLContext(pCrlCtx); |
| 5777 | } |
| 5778 | |
| 5779 | /* In error cases cert, enc and tup may not be NULL */ |
| 5780 | Py_XDECREF(crl); |
| 5781 | Py_XDECREF(enc); |
| 5782 | Py_XDECREF(tup); |
| 5783 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5784 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5785 | associated with the store, in this case our collection store and the |
| 5786 | associated system stores. */ |
| 5787 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5788 | /* This error case might shadow another exception.*/ |
| 5789 | Py_XDECREF(result); |
| 5790 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5791 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5792 | /* convert set to list */ |
| 5793 | if (result == NULL) { |
| 5794 | return NULL; |
| 5795 | } else { |
| 5796 | PyObject *lst = PySequence_List(result); |
| 5797 | Py_DECREF(result); |
| 5798 | return lst; |
| 5799 | } |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5800 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5801 | |
| 5802 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5803 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5804 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5805 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5806 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5807 | _SSL_RAND_ADD_METHODDEF |
| 5808 | _SSL_RAND_BYTES_METHODDEF |
| 5809 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5810 | _SSL_RAND_EGD_METHODDEF |
| 5811 | _SSL_RAND_STATUS_METHODDEF |
| 5812 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5813 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5814 | _SSL_ENUM_CRLS_METHODDEF |
| 5815 | _SSL_TXT2OBJ_METHODDEF |
| 5816 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5817 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5818 | }; |
| 5819 | |
| 5820 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5821 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5822 | |
| 5823 | /* an implementation of OpenSSL threading operations in terms |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5824 | * of the Python C thread library |
| 5825 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5826 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5827 | |
| 5828 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5829 | |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5830 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5831 | /* use new CRYPTO_THREADID API. */ |
| 5832 | static void |
| 5833 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5834 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5835 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5836 | } |
| 5837 | #else |
| 5838 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5839 | static unsigned long |
| 5840 | _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5841 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5842 | } |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5843 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5844 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5845 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5846 | (int mode, int n, const char *file, int line) { |
| 5847 | /* this function is needed to perform locking on shared data |
| 5848 | structures. (Note that OpenSSL uses a number of global data |
| 5849 | structures that will be implicitly shared whenever multiple |
| 5850 | threads use OpenSSL.) Multi-threaded applications will |
| 5851 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5852 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5853 | locking_function() must be able to handle up to |
| 5854 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5855 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5856 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5857 | file and line are the file number of the function setting the |
| 5858 | lock. They can be useful for debugging. |
| 5859 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5860 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5861 | if ((_ssl_locks == NULL) || |
| 5862 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5863 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5864 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5865 | if (mode & CRYPTO_LOCK) { |
| 5866 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5867 | } else { |
| 5868 | PyThread_release_lock(_ssl_locks[n]); |
| 5869 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | static int _setup_ssl_threads(void) { |
| 5873 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5874 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5875 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5876 | if (_ssl_locks == NULL) { |
| 5877 | _ssl_locks_count = CRYPTO_num_locks(); |
Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5878 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5879 | sizeof(PyThread_type_lock)); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5880 | if (_ssl_locks == NULL) { |
| 5881 | PyErr_NoMemory(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5882 | return 0; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5883 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5884 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5885 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5886 | if (_ssl_locks[i] == NULL) { |
| 5887 | unsigned int j; |
| 5888 | for (j = 0; j < i; j++) { |
| 5889 | PyThread_free_lock(_ssl_locks[j]); |
| 5890 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5891 | PyMem_Free(_ssl_locks); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5892 | return 0; |
| 5893 | } |
| 5894 | } |
| 5895 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5896 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5897 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5898 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5899 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5900 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5901 | } |
| 5902 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5905 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5906 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5907 | static int |
| 5908 | sslmodule_init_types(PyObject *module) |
| 5909 | { |
| 5910 | PySSLContext_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5911 | module, &PySSLContext_spec, NULL |
| 5912 | ); |
| 5913 | if (PySSLContext_Type == NULL) |
| 5914 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5915 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5916 | PySSLSocket_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5917 | module, &PySSLSocket_spec, NULL |
| 5918 | ); |
| 5919 | if (PySSLSocket_Type == NULL) |
| 5920 | return -1; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5921 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5922 | PySSLMemoryBIO_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5923 | module, &PySSLMemoryBIO_spec, NULL |
| 5924 | ); |
| 5925 | if (PySSLMemoryBIO_Type == NULL) |
| 5926 | return -1; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5927 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5928 | PySSLSession_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5929 | module, &PySSLSession_spec, NULL |
| 5930 | ); |
| 5931 | if (PySSLSession_Type == NULL) |
| 5932 | return -1; |
| 5933 | |
| 5934 | if (PyModule_AddType(module, PySSLContext_Type)) |
| 5935 | return -1; |
| 5936 | if (PyModule_AddType(module, PySSLSocket_Type)) |
| 5937 | return -1; |
| 5938 | if (PyModule_AddType(module, PySSLMemoryBIO_Type)) |
| 5939 | return -1; |
| 5940 | if (PyModule_AddType(module, PySSLSession_Type)) |
| 5941 | return -1; |
| 5942 | |
| 5943 | return 0; |
| 5944 | } |
| 5945 | |
| 5946 | static int |
| 5947 | sslmodule_init_exceptions(PyObject *module) |
| 5948 | { |
| 5949 | PyObject *bases = NULL; |
| 5950 | |
| 5951 | #define add_exception(exc, name, doc, base) \ |
| 5952 | do { \ |
| 5953 | (exc) = PyErr_NewExceptionWithDoc("ssl." name, (doc), (base), NULL); \ |
| 5954 | if ((exc) == NULL) goto error; \ |
| 5955 | if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \ |
| 5956 | } while(0) |
| 5957 | |
Serhiy Storchaka | 686c203 | 2020-11-22 13:25:02 +0200 | [diff] [blame] | 5958 | PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, PyExc_OSError); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5959 | if (PySSLErrorObject == NULL) { |
| 5960 | goto error; |
| 5961 | } |
| 5962 | if (PyModule_AddObjectRef(module, "SSLError", PySSLErrorObject) < 0) { |
| 5963 | goto error; |
| 5964 | } |
| 5965 | |
| 5966 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 5967 | bases = PyTuple_Pack(2, PySSLErrorObject, PyExc_ValueError); |
| 5968 | if (bases == NULL) { |
| 5969 | goto error; |
| 5970 | } |
| 5971 | add_exception( |
| 5972 | PySSLCertVerificationErrorObject, |
| 5973 | "SSLCertVerificationError", |
| 5974 | SSLCertVerificationError_doc, |
| 5975 | bases |
| 5976 | ); |
| 5977 | Py_CLEAR(bases); |
| 5978 | |
| 5979 | add_exception( |
| 5980 | PySSLZeroReturnErrorObject, |
| 5981 | "SSLZeroReturnError", |
| 5982 | SSLZeroReturnError_doc, |
| 5983 | PySSLErrorObject |
| 5984 | ); |
| 5985 | |
| 5986 | add_exception( |
| 5987 | PySSLWantWriteErrorObject, |
| 5988 | "SSLWantWriteError", |
| 5989 | SSLWantWriteError_doc, |
| 5990 | PySSLErrorObject |
| 5991 | ); |
| 5992 | |
| 5993 | add_exception( |
| 5994 | PySSLWantReadErrorObject, |
| 5995 | "SSLWantReadError", |
| 5996 | SSLWantReadError_doc, |
| 5997 | PySSLErrorObject |
| 5998 | ); |
| 5999 | |
| 6000 | add_exception( |
| 6001 | PySSLSyscallErrorObject, |
| 6002 | "SSLSyscallError", |
| 6003 | SSLSyscallError_doc, |
| 6004 | PySSLErrorObject |
| 6005 | ); |
| 6006 | |
| 6007 | add_exception( |
| 6008 | PySSLEOFErrorObject, |
| 6009 | "SSLEOFError", |
| 6010 | SSLEOFError_doc, |
| 6011 | PySSLErrorObject |
| 6012 | ); |
| 6013 | #undef add_exception |
| 6014 | |
| 6015 | return 0; |
| 6016 | error: |
| 6017 | Py_XDECREF(bases); |
| 6018 | return -1; |
| 6019 | } |
| 6020 | |
| 6021 | static int |
| 6022 | sslmodule_init_socketapi(PyObject *module) |
| 6023 | { |
| 6024 | PySocketModule_APIObject *socket_api; |
| 6025 | |
| 6026 | /* Load _socket module and its C API */ |
| 6027 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 6028 | if (socket_api == NULL) |
| 6029 | return -1; |
| 6030 | PySocketModule = *socket_api; |
| 6031 | |
| 6032 | return 0; |
| 6033 | } |
| 6034 | |
| 6035 | static int |
| 6036 | sslmodule_init_errorcodes(PyObject *module) |
| 6037 | { |
| 6038 | struct py_ssl_error_code *errcode; |
| 6039 | struct py_ssl_library_code *libcode; |
| 6040 | |
| 6041 | /* Mappings for error codes */ |
| 6042 | err_codes_to_names = PyDict_New(); |
| 6043 | if (err_codes_to_names == NULL) |
| 6044 | return -1; |
| 6045 | err_names_to_codes = PyDict_New(); |
| 6046 | if (err_names_to_codes == NULL) |
| 6047 | return -1; |
| 6048 | lib_codes_to_names = PyDict_New(); |
| 6049 | if (lib_codes_to_names == NULL) |
| 6050 | return -1; |
| 6051 | |
| 6052 | errcode = error_codes; |
| 6053 | while (errcode->mnemonic != NULL) { |
| 6054 | PyObject *mnemo, *key; |
| 6055 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 6056 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 6057 | if (mnemo == NULL || key == NULL) |
| 6058 | return -1; |
| 6059 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 6060 | return -1; |
| 6061 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 6062 | return -1; |
| 6063 | Py_DECREF(key); |
| 6064 | Py_DECREF(mnemo); |
| 6065 | errcode++; |
| 6066 | } |
| 6067 | |
| 6068 | libcode = library_codes; |
| 6069 | while (libcode->library != NULL) { |
| 6070 | PyObject *mnemo, *key; |
| 6071 | key = PyLong_FromLong(libcode->code); |
| 6072 | mnemo = PyUnicode_FromString(libcode->library); |
| 6073 | if (key == NULL || mnemo == NULL) |
| 6074 | return -1; |
| 6075 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 6076 | return -1; |
| 6077 | Py_DECREF(key); |
| 6078 | Py_DECREF(mnemo); |
| 6079 | libcode++; |
| 6080 | } |
| 6081 | |
| 6082 | if (PyModule_AddObject(module, "err_codes_to_names", err_codes_to_names)) |
| 6083 | return -1; |
| 6084 | if (PyModule_AddObject(module, "err_names_to_codes", err_names_to_codes)) |
| 6085 | return -1; |
| 6086 | if (PyModule_AddObject(module, "lib_codes_to_names", lib_codes_to_names)) |
| 6087 | return -1; |
| 6088 | |
| 6089 | return 0; |
| 6090 | } |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6091 | |
| 6092 | static void |
| 6093 | parse_openssl_version(unsigned long libver, |
| 6094 | unsigned int *major, unsigned int *minor, |
| 6095 | unsigned int *fix, unsigned int *patch, |
| 6096 | unsigned int *status) |
| 6097 | { |
| 6098 | *status = libver & 0xF; |
| 6099 | libver >>= 4; |
| 6100 | *patch = libver & 0xFF; |
| 6101 | libver >>= 8; |
| 6102 | *fix = libver & 0xFF; |
| 6103 | libver >>= 8; |
| 6104 | *minor = libver & 0xFF; |
| 6105 | libver >>= 8; |
| 6106 | *major = libver & 0xFF; |
| 6107 | } |
| 6108 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6109 | static int |
| 6110 | sslmodule_init_versioninfo(PyObject *m) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6111 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6112 | PyObject *r; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6113 | unsigned long libver; |
| 6114 | unsigned int major, minor, fix, patch, status; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6115 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6116 | /* OpenSSL version */ |
| 6117 | /* SSLeay() gives us the version of the library linked against, |
| 6118 | which could be different from the headers version. |
| 6119 | */ |
| 6120 | libver = OpenSSL_version_num(); |
| 6121 | r = PyLong_FromUnsignedLong(libver); |
| 6122 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6123 | return -1; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6124 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6125 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6126 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6127 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6128 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6129 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6130 | r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION)); |
| 6131 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6132 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6133 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6134 | libver = OPENSSL_VERSION_NUMBER; |
| 6135 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6136 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6137 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6138 | return -1; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6139 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6140 | return 0; |
| 6141 | } |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 6142 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6143 | static int |
| 6144 | sslmodule_init_constants(PyObject *m) |
| 6145 | { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 6146 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 6147 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 6148 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6149 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 6150 | PY_SSL_ERROR_ZERO_RETURN); |
| 6151 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 6152 | PY_SSL_ERROR_WANT_READ); |
| 6153 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 6154 | PY_SSL_ERROR_WANT_WRITE); |
| 6155 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 6156 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 6157 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 6158 | PY_SSL_ERROR_SYSCALL); |
| 6159 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 6160 | PY_SSL_ERROR_SSL); |
| 6161 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 6162 | PY_SSL_ERROR_WANT_CONNECT); |
| 6163 | /* non ssl.h errorcodes */ |
| 6164 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 6165 | PY_SSL_ERROR_EOF); |
| 6166 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 6167 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 6168 | /* cert requirements */ |
| 6169 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 6170 | PY_SSL_CERT_NONE); |
| 6171 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 6172 | PY_SSL_CERT_OPTIONAL); |
| 6173 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 6174 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 6175 | /* CRL verification for verification_flags */ |
| 6176 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 6177 | 0); |
| 6178 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 6179 | X509_V_FLAG_CRL_CHECK); |
| 6180 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 6181 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6182 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 6183 | X509_V_FLAG_X509_STRICT); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 6184 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 6185 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 6186 | X509_V_FLAG_TRUSTED_FIRST); |
| 6187 | #endif |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 6188 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6189 | /* Alert Descriptions from ssl.h */ |
| 6190 | /* note RESERVED constants no longer intended for use have been removed */ |
| 6191 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 6192 | |
| 6193 | #define ADD_AD_CONSTANT(s) \ |
| 6194 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 6195 | SSL_AD_##s) |
| 6196 | |
| 6197 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 6198 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 6199 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 6200 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 6201 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 6202 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 6203 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 6204 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 6205 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 6206 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 6207 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 6208 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 6209 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 6210 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 6211 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 6212 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 6213 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 6214 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 6215 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 6216 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 6217 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6218 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 6219 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 6220 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 6221 | #endif |
| 6222 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 6223 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 6224 | #endif |
| 6225 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 6226 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 6227 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6228 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 6229 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 6230 | #endif |
| 6231 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 6232 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 6233 | #endif |
| 6234 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 6235 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 6236 | #endif |
| 6237 | |
| 6238 | #undef ADD_AD_CONSTANT |
| 6239 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6240 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6241 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6242 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 6243 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6244 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6245 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6246 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 6247 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6248 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6249 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6250 | PY_SSL_VERSION_TLS); |
| 6251 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 6252 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 6253 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 6254 | PY_SSL_VERSION_TLS_CLIENT); |
| 6255 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 6256 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6257 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 6258 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6259 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 6260 | PY_SSL_VERSION_TLS1_1); |
| 6261 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 6262 | PY_SSL_VERSION_TLS1_2); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6263 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6264 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 6265 | PyModule_AddIntConstant(m, "OP_ALL", |
| 6266 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6267 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 6268 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 6269 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6270 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 6271 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6272 | #ifdef SSL_OP_NO_TLSv1_3 |
| 6273 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 6274 | #else |
| 6275 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 6276 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 6277 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 6278 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 6279 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6280 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6281 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 6282 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6283 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 6284 | #ifdef SSL_OP_NO_COMPRESSION |
| 6285 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 6286 | SSL_OP_NO_COMPRESSION); |
| 6287 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 6288 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 6289 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 6290 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 6291 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 6292 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 6293 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 6294 | SSL_OP_NO_RENEGOTIATION); |
| 6295 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6296 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 6297 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 6298 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 6299 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 6300 | #endif |
| 6301 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 6302 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 6303 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 6304 | #endif |
| 6305 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 6306 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 6307 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 6308 | #endif |
| 6309 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 6310 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 6311 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 6312 | #endif |
| 6313 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 6314 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 6315 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 6316 | #endif |
| 6317 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 6318 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 6319 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 6320 | #endif |
| 6321 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6322 | /* protocol versions */ |
| 6323 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 6324 | PY_PROTO_MINIMUM_SUPPORTED); |
| 6325 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 6326 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 6327 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 6328 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 6329 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 6330 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 6331 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 6332 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6333 | #define addbool(m, key, value) \ |
| 6334 | do { \ |
| 6335 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 6336 | Py_INCREF(bool_obj); \ |
| 6337 | PyModule_AddObject((m), (key), bool_obj); \ |
| 6338 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6339 | |
| 6340 | #if HAVE_SNI |
| 6341 | addbool(m, "HAS_SNI", 1); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6342 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6343 | addbool(m, "HAS_SNI", 0); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6344 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6345 | |
| 6346 | addbool(m, "HAS_TLS_UNIQUE", 1); |
| 6347 | |
| 6348 | #ifndef OPENSSL_NO_ECDH |
| 6349 | addbool(m, "HAS_ECDH", 1); |
| 6350 | #else |
| 6351 | addbool(m, "HAS_ECDH", 0); |
| 6352 | #endif |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6353 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6354 | #if HAVE_NPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6355 | addbool(m, "HAS_NPN", 1); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6356 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6357 | addbool(m, "HAS_NPN", 0); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6358 | #endif |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6359 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6360 | #if HAVE_ALPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6361 | addbool(m, "HAS_ALPN", 1); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6362 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6363 | addbool(m, "HAS_ALPN", 0); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6364 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6365 | |
| 6366 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 6367 | addbool(m, "HAS_SSLv2", 1); |
| 6368 | #else |
| 6369 | addbool(m, "HAS_SSLv2", 0); |
| 6370 | #endif |
| 6371 | |
| 6372 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 6373 | addbool(m, "HAS_SSLv3", 1); |
| 6374 | #else |
| 6375 | addbool(m, "HAS_SSLv3", 0); |
| 6376 | #endif |
| 6377 | |
| 6378 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 6379 | addbool(m, "HAS_TLSv1", 1); |
| 6380 | #else |
| 6381 | addbool(m, "HAS_TLSv1", 0); |
| 6382 | #endif |
| 6383 | |
| 6384 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 6385 | addbool(m, "HAS_TLSv1_1", 1); |
| 6386 | #else |
| 6387 | addbool(m, "HAS_TLSv1_1", 0); |
| 6388 | #endif |
| 6389 | |
| 6390 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 6391 | addbool(m, "HAS_TLSv1_2", 1); |
| 6392 | #else |
| 6393 | addbool(m, "HAS_TLSv1_2", 0); |
| 6394 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6395 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6396 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6397 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6398 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6399 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6400 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6401 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6402 | return 0; |
| 6403 | } |
| 6404 | |
| 6405 | static int |
| 6406 | sslmodule_legacy(PyObject *module) |
| 6407 | { |
| 6408 | #ifndef OPENSSL_VERSION_1_1 |
| 6409 | /* Load all algorithms and initialize cpuid */ |
| 6410 | OPENSSL_add_all_algorithms_noconf(); |
| 6411 | /* Init OpenSSL */ |
| 6412 | SSL_load_error_strings(); |
| 6413 | SSL_library_init(); |
| 6414 | #endif |
| 6415 | |
| 6416 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
| 6417 | /* note that this will start threading if not already started */ |
| 6418 | if (!_setup_ssl_threads()) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6419 | return NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6420 | } |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6421 | #elif OPENSSL_VERSION_1_1 |
| 6422 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 6423 | _ssl_locks_count++; |
| 6424 | #endif |
| 6425 | return 0; |
| 6426 | } |
| 6427 | |
| 6428 | PyDoc_STRVAR(module_doc, |
| 6429 | "Implementation module for SSL socket operations. See the socket module\n\ |
| 6430 | for documentation."); |
| 6431 | |
| 6432 | |
| 6433 | static struct PyModuleDef _sslmodule = { |
| 6434 | PyModuleDef_HEAD_INIT, |
| 6435 | "_ssl", |
| 6436 | module_doc, |
| 6437 | -1, |
| 6438 | PySSL_methods, |
| 6439 | NULL, |
| 6440 | NULL, |
| 6441 | NULL, |
| 6442 | NULL |
| 6443 | }; |
| 6444 | |
| 6445 | PyMODINIT_FUNC |
| 6446 | PyInit__ssl(void) |
| 6447 | { |
| 6448 | PyObject *m; |
| 6449 | |
| 6450 | m = PyModule_Create(&_sslmodule); |
| 6451 | if (m == NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6452 | return NULL; |
| 6453 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6454 | if (sslmodule_init_types(m) != 0) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6455 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6456 | if (sslmodule_init_exceptions(m) != 0) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6457 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6458 | if (sslmodule_init_socketapi(m) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6459 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6460 | if (sslmodule_init_errorcodes(m) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6461 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6462 | if (sslmodule_init_constants(m) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6463 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6464 | if (sslmodule_init_versioninfo(m) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6465 | return NULL; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6466 | if (sslmodule_legacy(m) != 0) |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6467 | return NULL; |
| 6468 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6469 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6470 | } |