| 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 |  | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 21 | #include "pythread.h" | 
| Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 22 |  | 
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 23 | /* Redefined below for Windows debug builds after important #includes */ | 
|  | 24 | #define _PySSL_FIX_ERRNO | 
| Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 25 |  | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 26 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ | 
|  | 27 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) | 
|  | 28 | #define PySSL_END_ALLOW_THREADS_S(save) \ | 
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 29 | 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] | 30 | #define PySSL_BEGIN_ALLOW_THREADS { \ | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 31 | PyThreadState *_save = NULL;  \ | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 32 | PySSL_BEGIN_ALLOW_THREADS_S(_save); | 
|  | 33 | #define PySSL_BLOCK_THREADS     PySSL_END_ALLOW_THREADS_S(_save); | 
|  | 34 | #define PySSL_UNBLOCK_THREADS   PySSL_BEGIN_ALLOW_THREADS_S(_save); | 
|  | 35 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 36 |  | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 37 | /* Include symbols from _socket module */ | 
|  | 38 | #include "socketmodule.h" | 
|  | 39 |  | 
|  | 40 | static PySocketModule_APIObject PySocketModule; | 
|  | 41 |  | 
|  | 42 | #if defined(HAVE_POLL_H) | 
|  | 43 | #include <poll.h> | 
|  | 44 | #elif defined(HAVE_SYS_POLL_H) | 
|  | 45 | #include <sys/poll.h> | 
|  | 46 | #endif | 
|  | 47 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 48 | /* Don't warn about deprecated functions */ | 
|  | 49 | #ifdef __GNUC__ | 
|  | 50 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | 
|  | 51 | #endif | 
|  | 52 | #ifdef __clang__ | 
|  | 53 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | 
|  | 54 | #endif | 
|  | 55 |  | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 56 | /* Include OpenSSL header files */ | 
|  | 57 | #include "openssl/rsa.h" | 
|  | 58 | #include "openssl/crypto.h" | 
|  | 59 | #include "openssl/x509.h" | 
|  | 60 | #include "openssl/x509v3.h" | 
|  | 61 | #include "openssl/pem.h" | 
|  | 62 | #include "openssl/ssl.h" | 
|  | 63 | #include "openssl/err.h" | 
|  | 64 | #include "openssl/rand.h" | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 65 | #include "openssl/bio.h" | 
| Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 66 | #include "openssl/dh.h" | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 67 |  | 
| Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 68 | #ifndef HAVE_X509_VERIFY_PARAM_SET1_HOST | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 69 | #  ifdef LIBRESSL_VERSION_NUMBER | 
|  | 70 | #    error "LibreSSL is missing X509_VERIFY_PARAM_set1_host(), see https://github.com/libressl-portable/portable/issues/381" | 
|  | 71 | #  elif OPENSSL_VERSION_NUMBER > 0x1000200fL | 
| Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 72 | #    define HAVE_X509_VERIFY_PARAM_SET1_HOST | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 73 | #  else | 
|  | 74 | #    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] | 75 | #  endif | 
|  | 76 | #endif | 
|  | 77 |  | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 78 | /* SSL error object */ | 
|  | 79 | static PyObject *PySSLErrorObject; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 80 | static PyObject *PySSLCertVerificationErrorObject; | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 81 | static PyObject *PySSLZeroReturnErrorObject; | 
|  | 82 | static PyObject *PySSLWantReadErrorObject; | 
|  | 83 | static PyObject *PySSLWantWriteErrorObject; | 
|  | 84 | static PyObject *PySSLSyscallErrorObject; | 
|  | 85 | static PyObject *PySSLEOFErrorObject; | 
|  | 86 |  | 
|  | 87 | /* Error mappings */ | 
|  | 88 | static PyObject *err_codes_to_names; | 
|  | 89 | static PyObject *err_names_to_codes; | 
|  | 90 | static PyObject *lib_codes_to_names; | 
|  | 91 |  | 
|  | 92 | struct py_ssl_error_code { | 
|  | 93 | const char *mnemonic; | 
|  | 94 | int library, reason; | 
|  | 95 | }; | 
|  | 96 | struct py_ssl_library_code { | 
|  | 97 | const char *library; | 
|  | 98 | int code; | 
|  | 99 | }; | 
|  | 100 |  | 
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 101 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) | 
|  | 102 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. | 
|  | 103 | * However, because it uses a different CRT, we need to transfer the | 
|  | 104 | * value of errno from OpenSSL into our debug CRT. | 
|  | 105 | * | 
|  | 106 | * Don't be fooled - this is horribly ugly code. The only reasonable | 
|  | 107 | * alternative is to do both debug and release builds of OpenSSL, which | 
|  | 108 | * requires much uglier code to transform their automatically generated | 
|  | 109 | * makefile. This is the lesser of all the evils. | 
|  | 110 | */ | 
|  | 111 |  | 
|  | 112 | static void _PySSLFixErrno(void) { | 
|  | 113 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); | 
|  | 114 | if (!ucrtbase) { | 
|  | 115 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely | 
|  | 116 | * have a catastrophic failure, but this function is not the | 
|  | 117 | * place to raise it. */ | 
|  | 118 | return; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | typedef int *(__stdcall *errno_func)(void); | 
|  | 122 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); | 
|  | 123 | if (ssl_errno) { | 
|  | 124 | errno = *ssl_errno(); | 
|  | 125 | *ssl_errno() = 0; | 
|  | 126 | } else { | 
|  | 127 | errno = ENOTRECOVERABLE; | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | #undef _PySSL_FIX_ERRNO | 
|  | 132 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() | 
|  | 133 | #endif | 
|  | 134 |  | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 135 | /* Include generated data (error codes) */ | 
|  | 136 | #include "_ssl_data.h" | 
|  | 137 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 138 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) | 
|  | 139 | #  define OPENSSL_VERSION_1_1 1 | 
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 140 | #  define PY_OPENSSL_1_1_API 1 | 
|  | 141 | #endif | 
|  | 142 |  | 
|  | 143 | /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ | 
|  | 144 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL | 
|  | 145 | #  define PY_OPENSSL_1_1_API 1 | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 146 | #endif | 
|  | 147 |  | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 148 | /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 | 
|  | 149 | http://www.openssl.org/news/changelog.html | 
|  | 150 | */ | 
|  | 151 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | 
|  | 152 | # define HAVE_TLSv1_2 1 | 
|  | 153 | #else | 
|  | 154 | # define HAVE_TLSv1_2 0 | 
|  | 155 | #endif | 
|  | 156 |  | 
| Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 157 | /* 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] | 158 | * This includes the SSL_set_SSL_CTX() function. | 
|  | 159 | */ | 
|  | 160 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | 
|  | 161 | # define HAVE_SNI 1 | 
|  | 162 | #else | 
|  | 163 | # define HAVE_SNI 0 | 
|  | 164 | #endif | 
|  | 165 |  | 
| Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 166 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 167 | # define HAVE_ALPN 1 | 
|  | 168 | #else | 
|  | 169 | # define HAVE_ALPN 0 | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 170 | #endif | 
|  | 171 |  | 
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 172 | /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped | 
|  | 173 | * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility | 
|  | 174 | * reasons. The check for TLSEXT_TYPE_next_proto_neg works with | 
|  | 175 | * OpenSSL 1.0.1+ and LibreSSL. | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 176 | * 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] | 177 | */ | 
|  | 178 | #ifdef OPENSSL_NO_NEXTPROTONEG | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 179 | # define HAVE_NPN 0 | 
|  | 180 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) | 
|  | 181 | # define HAVE_NPN 0 | 
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 182 | #elif defined(TLSEXT_TYPE_next_proto_neg) | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 183 | # define HAVE_NPN 1 | 
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 184 | #else | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 185 | # define HAVE_NPN 0 | 
|  | 186 | #endif | 
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 187 |  | 
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 188 | #ifndef INVALID_SOCKET /* MS defines this */ | 
|  | 189 | #define INVALID_SOCKET (-1) | 
|  | 190 | #endif | 
|  | 191 |  | 
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 192 | /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ | 
|  | 193 | #ifndef OPENSSL_VERSION_1_1 | 
|  | 194 | #define HAVE_OPENSSL_CRYPTO_LOCK | 
|  | 195 | #endif | 
|  | 196 |  | 
|  | 197 | #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 198 | #define OPENSSL_NO_SSL2 | 
|  | 199 | #endif | 
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 200 |  | 
|  | 201 | #ifndef PY_OPENSSL_1_1_API | 
|  | 202 | /* 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] | 203 |  | 
|  | 204 | #define TLS_method SSLv23_method | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 205 | #define TLS_client_method SSLv23_client_method | 
|  | 206 | #define TLS_server_method SSLv23_server_method | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 207 |  | 
|  | 208 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) | 
|  | 209 | { | 
|  | 210 | return ne->set; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | #ifndef OPENSSL_NO_COMP | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 214 | /* LCOV_EXCL_START */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 215 | static int COMP_get_type(const COMP_METHOD *meth) | 
|  | 216 | { | 
|  | 217 | return meth->type; | 
|  | 218 | } | 
| Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 219 | /* LCOV_EXCL_STOP */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 220 | #endif | 
|  | 221 |  | 
|  | 222 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) | 
|  | 223 | { | 
|  | 224 | return ctx->default_passwd_callback; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) | 
|  | 228 | { | 
|  | 229 | return ctx->default_passwd_callback_userdata; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | static int X509_OBJECT_get_type(X509_OBJECT *x) | 
|  | 233 | { | 
|  | 234 | return x->type; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) | 
|  | 238 | { | 
|  | 239 | return x->data.x509; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | static int BIO_up_ref(BIO *b) | 
|  | 243 | { | 
|  | 244 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); | 
|  | 245 | return 1; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { | 
|  | 249 | return store->objs; | 
|  | 250 | } | 
|  | 251 |  | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 252 | static int | 
|  | 253 | SSL_SESSION_has_ticket(const SSL_SESSION *s) | 
|  | 254 | { | 
|  | 255 | return (s->tlsext_ticklen > 0) ? 1 : 0; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | static unsigned long | 
|  | 259 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) | 
|  | 260 | { | 
|  | 261 | return s->tlsext_tick_lifetime_hint; | 
|  | 262 | } | 
|  | 263 |  | 
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 264 | #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 265 |  | 
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 266 | /* Default cipher suites */ | 
|  | 267 | #ifndef PY_SSL_DEFAULT_CIPHERS | 
|  | 268 | #define PY_SSL_DEFAULT_CIPHERS 1 | 
|  | 269 | #endif | 
|  | 270 |  | 
|  | 271 | #if PY_SSL_DEFAULT_CIPHERS == 0 | 
|  | 272 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING | 
|  | 273 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" | 
|  | 274 | #endif | 
|  | 275 | #elif PY_SSL_DEFAULT_CIPHERS == 1 | 
| Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame^] | 276 | /* Python custom selection of sensible cipher suites | 
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 277 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. | 
|  | 278 | * !aNULL:!eNULL: really no NULL ciphers | 
|  | 279 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. | 
|  | 280 | * !aDSS: no authentication with discrete logarithm DSA algorithm | 
|  | 281 | * !SRP:!PSK: no secure remote password or pre-shared key authentication | 
|  | 282 | */ | 
|  | 283 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" | 
|  | 284 | #elif PY_SSL_DEFAULT_CIPHERS == 2 | 
|  | 285 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ | 
|  | 286 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST | 
|  | 287 | #else | 
|  | 288 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" | 
|  | 289 | #endif | 
|  | 290 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 291 |  | 
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 292 | enum py_ssl_error { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 293 | /* these mirror ssl.h */ | 
|  | 294 | PY_SSL_ERROR_NONE, | 
|  | 295 | PY_SSL_ERROR_SSL, | 
|  | 296 | PY_SSL_ERROR_WANT_READ, | 
|  | 297 | PY_SSL_ERROR_WANT_WRITE, | 
|  | 298 | PY_SSL_ERROR_WANT_X509_LOOKUP, | 
|  | 299 | PY_SSL_ERROR_SYSCALL,     /* look at error stack/return value/errno */ | 
|  | 300 | PY_SSL_ERROR_ZERO_RETURN, | 
|  | 301 | PY_SSL_ERROR_WANT_CONNECT, | 
|  | 302 | /* start of non ssl.h errorcodes */ | 
|  | 303 | PY_SSL_ERROR_EOF,         /* special case of SSL_ERROR_SYSCALL */ | 
|  | 304 | PY_SSL_ERROR_NO_SOCKET,   /* socket has been GC'd */ | 
|  | 305 | PY_SSL_ERROR_INVALID_ERROR_CODE | 
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 306 | }; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 307 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 308 | enum py_ssl_server_or_client { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 309 | PY_SSL_CLIENT, | 
|  | 310 | PY_SSL_SERVER | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 311 | }; | 
|  | 312 |  | 
|  | 313 | enum py_ssl_cert_requirements { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 314 | PY_SSL_CERT_NONE, | 
|  | 315 | PY_SSL_CERT_OPTIONAL, | 
|  | 316 | PY_SSL_CERT_REQUIRED | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 317 | }; | 
|  | 318 |  | 
|  | 319 | enum py_ssl_version { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 320 | PY_SSL_VERSION_SSL2, | 
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 321 | PY_SSL_VERSION_SSL3=1, | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 322 | PY_SSL_VERSION_TLS, /* SSLv23 */ | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 323 | #if HAVE_TLSv1_2 | 
|  | 324 | PY_SSL_VERSION_TLS1, | 
|  | 325 | PY_SSL_VERSION_TLS1_1, | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 326 | PY_SSL_VERSION_TLS1_2, | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 327 | #else | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 328 | PY_SSL_VERSION_TLS1, | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 329 | #endif | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 330 | PY_SSL_VERSION_TLS_CLIENT=0x10, | 
|  | 331 | PY_SSL_VERSION_TLS_SERVER, | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 332 | }; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 333 |  | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 334 | enum py_proto_version { | 
|  | 335 | PY_PROTO_MINIMUM_SUPPORTED = -2, | 
|  | 336 | PY_PROTO_SSLv3 = SSL3_VERSION, | 
|  | 337 | PY_PROTO_TLSv1 = TLS1_VERSION, | 
|  | 338 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, | 
|  | 339 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, | 
|  | 340 | #ifdef TLS1_3_VERSION | 
|  | 341 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, | 
|  | 342 | #else | 
|  | 343 | PY_PROTO_TLSv1_3 = 0x304, | 
|  | 344 | #endif | 
|  | 345 | PY_PROTO_MAXIMUM_SUPPORTED = -1, | 
|  | 346 |  | 
|  | 347 | /* OpenSSL has no dedicated API to set the minimum version to the maximum | 
|  | 348 | * available version, and the other way around. We have to figure out the | 
|  | 349 | * minimum and maximum available version on our own and hope for the best. | 
|  | 350 | */ | 
|  | 351 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) | 
|  | 352 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, | 
|  | 353 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) | 
|  | 354 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, | 
|  | 355 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) | 
|  | 356 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, | 
|  | 357 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) | 
|  | 358 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, | 
|  | 359 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) | 
|  | 360 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, | 
|  | 361 | #else | 
|  | 362 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" | 
|  | 363 | #endif | 
|  | 364 |  | 
|  | 365 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) | 
|  | 366 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, | 
|  | 367 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) | 
|  | 368 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, | 
|  | 369 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) | 
|  | 370 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, | 
|  | 371 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) | 
|  | 372 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, | 
|  | 373 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) | 
|  | 374 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, | 
|  | 375 | #else | 
|  | 376 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" | 
|  | 377 | #endif | 
|  | 378 | }; | 
|  | 379 |  | 
|  | 380 |  | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 381 | /* serves as a flag to see whether we've initialized the SSL thread support. */ | 
|  | 382 | /* 0 means no, greater than 0 means yes */ | 
|  | 383 |  | 
|  | 384 | static unsigned int _ssl_locks_count = 0; | 
|  | 385 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 386 | /* SSL socket object */ | 
|  | 387 |  | 
|  | 388 | #define X509_NAME_MAXLEN 256 | 
|  | 389 |  | 
| Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 390 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in | 
|  | 391 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the | 
|  | 392 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ | 
|  | 393 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 394 | # define HAVE_SSL_CTX_CLEAR_OPTIONS | 
|  | 395 | #else | 
|  | 396 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS | 
|  | 397 | #endif | 
|  | 398 |  | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 399 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for | 
|  | 400 | * older SSL, but let's be safe */ | 
|  | 401 | #define PySSL_CB_MAXLEN 128 | 
|  | 402 |  | 
| Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 403 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 404 | typedef struct { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 405 | PyObject_HEAD | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 406 | SSL_CTX *ctx; | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 407 | #if HAVE_NPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 408 | unsigned char *npn_protocols; | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 409 | int npn_protocols_len; | 
|  | 410 | #endif | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 411 | #if HAVE_ALPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 412 | unsigned char *alpn_protocols; | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 413 | unsigned int alpn_protocols_len; | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 414 | #endif | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 415 | #ifndef OPENSSL_NO_TLSEXT | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 416 | PyObject *set_sni_cb; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 417 | #endif | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 418 | int check_hostname; | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 419 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. | 
|  | 420 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. | 
|  | 421 | */ | 
|  | 422 | unsigned int hostflags; | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 423 | int protocol; | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 424 | #ifdef TLS1_3_VERSION | 
|  | 425 | int post_handshake_auth; | 
|  | 426 | #endif | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 427 | } PySSLContext; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 428 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 429 | typedef struct { | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 430 | int ssl; /* last seen error from SSL */ | 
|  | 431 | int c; /* last seen error from libc */ | 
|  | 432 | #ifdef MS_WINDOWS | 
|  | 433 | int ws; /* last seen error from winsock */ | 
|  | 434 | #endif | 
|  | 435 | } _PySSLError; | 
|  | 436 |  | 
|  | 437 | typedef struct { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 438 | PyObject_HEAD | 
|  | 439 | PyObject *Socket; /* weakref to socket on which we're layered */ | 
|  | 440 | SSL *ssl; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 441 | PySSLContext *ctx; /* weakref to SSL context */ | 
| Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 442 | char shutdown_seen_zero; | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 443 | enum py_ssl_server_or_client socket_type; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 444 | PyObject *owner; /* Python level "owner" passed to servername callback */ | 
|  | 445 | PyObject *server_hostname; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 446 | _PySSLError err; /* last seen error from various sources */ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 447 | } PySSLSocket; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 448 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 449 | typedef struct { | 
|  | 450 | PyObject_HEAD | 
|  | 451 | BIO *bio; | 
|  | 452 | int eof_written; | 
|  | 453 | } PySSLMemoryBIO; | 
|  | 454 |  | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 455 | typedef struct { | 
|  | 456 | PyObject_HEAD | 
|  | 457 | SSL_SESSION *session; | 
|  | 458 | PySSLContext *ctx; | 
|  | 459 | } PySSLSession; | 
|  | 460 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 461 | static PyTypeObject PySSLContext_Type; | 
|  | 462 | static PyTypeObject PySSLSocket_Type; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 463 | static PyTypeObject PySSLMemoryBIO_Type; | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 464 | static PyTypeObject PySSLSession_Type; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 465 |  | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 466 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) | 
|  | 467 | { | 
|  | 468 | _PySSLError err = { 0 }; | 
|  | 469 | if (failed) { | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 470 | #ifdef MS_WINDOWS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 471 | err.ws = WSAGetLastError(); | 
|  | 472 | _PySSL_FIX_ERRNO; | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 473 | #endif | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 474 | err.c = errno; | 
|  | 475 | err.ssl = SSL_get_error(ssl, retcode); | 
|  | 476 | } | 
|  | 477 | return err; | 
|  | 478 | } | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 479 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 480 | /*[clinic input] | 
|  | 481 | module _ssl | 
|  | 482 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" | 
|  | 483 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" | 
|  | 484 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 485 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 486 | [clinic start generated code]*/ | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 487 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 488 |  | 
|  | 489 | #include "clinic/_ssl.c.h" | 
|  | 490 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 491 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 492 |  | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 493 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); | 
|  | 494 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 495 | #define PySSLSocket_Check(v)    (Py_TYPE(v) == &PySSLSocket_Type) | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 496 | #define PySSLMemoryBIO_Check(v)    (Py_TYPE(v) == &PySSLMemoryBIO_Type) | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 497 | #define PySSLSession_Check(v)   (Py_TYPE(v) == &PySSLSession_Type) | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 498 |  | 
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 499 | typedef enum { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 500 | SOCKET_IS_NONBLOCKING, | 
|  | 501 | SOCKET_IS_BLOCKING, | 
|  | 502 | SOCKET_HAS_TIMED_OUT, | 
|  | 503 | SOCKET_HAS_BEEN_CLOSED, | 
|  | 504 | SOCKET_TOO_LARGE_FOR_SELECT, | 
|  | 505 | SOCKET_OPERATION_OK | 
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 506 | } timeout_state; | 
|  | 507 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 508 | /* Wrap error strings with filename and line # */ | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 509 | #define ERRSTR1(x,y,z) (x ":" y ": " z) | 
| Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 510 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 511 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 512 | /* Get the socket from a PySSLSocket, if it has one */ | 
|  | 513 | #define GET_SOCKET(obj) ((obj)->Socket ? \ | 
|  | 514 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 515 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 516 | /* If sock is NULL, use a timeout of 0 second */ | 
|  | 517 | #define GET_SOCKET_TIMEOUT(sock) \ | 
|  | 518 | ((sock != NULL) ? (sock)->sock_timeout : 0) | 
|  | 519 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 520 | /* | 
|  | 521 | * SSL errors. | 
|  | 522 | */ | 
|  | 523 |  | 
|  | 524 | PyDoc_STRVAR(SSLError_doc, | 
|  | 525 | "An error occurred in the SSL implementation."); | 
|  | 526 |  | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 527 | PyDoc_STRVAR(SSLCertVerificationError_doc, | 
|  | 528 | "A certificate could not be verified."); | 
|  | 529 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 530 | PyDoc_STRVAR(SSLZeroReturnError_doc, | 
|  | 531 | "SSL/TLS session closed cleanly."); | 
|  | 532 |  | 
|  | 533 | PyDoc_STRVAR(SSLWantReadError_doc, | 
|  | 534 | "Non-blocking SSL socket needs to read more data\n" | 
|  | 535 | "before the requested operation can be completed."); | 
|  | 536 |  | 
|  | 537 | PyDoc_STRVAR(SSLWantWriteError_doc, | 
|  | 538 | "Non-blocking SSL socket needs to write more data\n" | 
|  | 539 | "before the requested operation can be completed."); | 
|  | 540 |  | 
|  | 541 | PyDoc_STRVAR(SSLSyscallError_doc, | 
|  | 542 | "System error when attempting SSL operation."); | 
|  | 543 |  | 
|  | 544 | PyDoc_STRVAR(SSLEOFError_doc, | 
|  | 545 | "SSL/TLS connection terminated abruptly."); | 
|  | 546 |  | 
|  | 547 | static PyObject * | 
|  | 548 | SSLError_str(PyOSErrorObject *self) | 
|  | 549 | { | 
|  | 550 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { | 
|  | 551 | Py_INCREF(self->strerror); | 
|  | 552 | return self->strerror; | 
|  | 553 | } | 
|  | 554 | else | 
|  | 555 | return PyObject_Str(self->args); | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | static PyType_Slot sslerror_type_slots[] = { | 
|  | 559 | {Py_tp_base, NULL},  /* Filled out in module init as it's not a constant */ | 
|  | 560 | {Py_tp_doc, SSLError_doc}, | 
|  | 561 | {Py_tp_str, SSLError_str}, | 
|  | 562 | {0, 0}, | 
|  | 563 | }; | 
|  | 564 |  | 
|  | 565 | static PyType_Spec sslerror_type_spec = { | 
|  | 566 | "ssl.SSLError", | 
|  | 567 | sizeof(PyOSErrorObject), | 
|  | 568 | 0, | 
|  | 569 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | 
|  | 570 | sslerror_type_slots | 
|  | 571 | }; | 
|  | 572 |  | 
|  | 573 | static void | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 574 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, | 
|  | 575 | const char *errstr, int lineno, unsigned long errcode) | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 576 | { | 
|  | 577 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 578 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 579 | PyObject *init_value, *msg, *key; | 
|  | 580 | _Py_IDENTIFIER(reason); | 
|  | 581 | _Py_IDENTIFIER(library); | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 582 | _Py_IDENTIFIER(verify_message); | 
|  | 583 | _Py_IDENTIFIER(verify_code); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 584 |  | 
|  | 585 | if (errcode != 0) { | 
|  | 586 | int lib, reason; | 
|  | 587 |  | 
|  | 588 | lib = ERR_GET_LIB(errcode); | 
|  | 589 | reason = ERR_GET_REASON(errcode); | 
|  | 590 | key = Py_BuildValue("ii", lib, reason); | 
|  | 591 | if (key == NULL) | 
|  | 592 | goto fail; | 
|  | 593 | reason_obj = PyDict_GetItem(err_codes_to_names, key); | 
|  | 594 | Py_DECREF(key); | 
|  | 595 | if (reason_obj == NULL) { | 
|  | 596 | /* XXX if reason < 100, it might reflect a library number (!!) */ | 
|  | 597 | PyErr_Clear(); | 
|  | 598 | } | 
|  | 599 | key = PyLong_FromLong(lib); | 
|  | 600 | if (key == NULL) | 
|  | 601 | goto fail; | 
|  | 602 | lib_obj = PyDict_GetItem(lib_codes_to_names, key); | 
|  | 603 | Py_DECREF(key); | 
|  | 604 | if (lib_obj == NULL) { | 
|  | 605 | PyErr_Clear(); | 
|  | 606 | } | 
|  | 607 | if (errstr == NULL) | 
|  | 608 | errstr = ERR_reason_error_string(errcode); | 
|  | 609 | } | 
|  | 610 | if (errstr == NULL) | 
|  | 611 | errstr = "unknown error"; | 
|  | 612 |  | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 613 | /* verify code for cert validation error */ | 
|  | 614 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { | 
|  | 615 | const char *verify_str = NULL; | 
|  | 616 | long verify_code; | 
|  | 617 |  | 
|  | 618 | verify_code = SSL_get_verify_result(sslsock->ssl); | 
|  | 619 | verify_code_obj = PyLong_FromLong(verify_code); | 
|  | 620 | if (verify_code_obj == NULL) { | 
|  | 621 | goto fail; | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | switch (verify_code) { | 
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 625 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH | 
|  | 626 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 627 | case X509_V_ERR_HOSTNAME_MISMATCH: | 
|  | 628 | verify_obj = PyUnicode_FromFormat( | 
|  | 629 | "Hostname mismatch, certificate is not valid for '%S'.", | 
|  | 630 | sslsock->server_hostname | 
|  | 631 | ); | 
|  | 632 | break; | 
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 633 | #endif | 
|  | 634 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 635 | case X509_V_ERR_IP_ADDRESS_MISMATCH: | 
|  | 636 | verify_obj = PyUnicode_FromFormat( | 
|  | 637 | "IP address mismatch, certificate is not valid for '%S'.", | 
|  | 638 | sslsock->server_hostname | 
|  | 639 | ); | 
|  | 640 | break; | 
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 641 | #endif | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 642 | default: | 
|  | 643 | verify_str = X509_verify_cert_error_string(verify_code); | 
|  | 644 | if (verify_str != NULL) { | 
|  | 645 | verify_obj = PyUnicode_FromString(verify_str); | 
|  | 646 | } else { | 
|  | 647 | verify_obj = Py_None; | 
|  | 648 | Py_INCREF(verify_obj); | 
|  | 649 | } | 
|  | 650 | break; | 
|  | 651 | } | 
|  | 652 | if (verify_obj == NULL) { | 
|  | 653 | goto fail; | 
|  | 654 | } | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | if (verify_obj && reason_obj && lib_obj) | 
|  | 658 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", | 
|  | 659 | lib_obj, reason_obj, errstr, verify_obj, | 
|  | 660 | lineno); | 
|  | 661 | else if (reason_obj && lib_obj) | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 662 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", | 
|  | 663 | lib_obj, reason_obj, errstr, lineno); | 
|  | 664 | else if (lib_obj) | 
|  | 665 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", | 
|  | 666 | lib_obj, errstr, lineno); | 
|  | 667 | else | 
|  | 668 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 669 | if (msg == NULL) | 
|  | 670 | goto fail; | 
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 671 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 672 | init_value = Py_BuildValue("iN", ssl_errno, msg); | 
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 673 | if (init_value == NULL) | 
|  | 674 | goto fail; | 
|  | 675 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 676 | err_value = PyObject_CallObject(type, init_value); | 
|  | 677 | Py_DECREF(init_value); | 
|  | 678 | if (err_value == NULL) | 
|  | 679 | goto fail; | 
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 680 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 681 | if (reason_obj == NULL) | 
|  | 682 | reason_obj = Py_None; | 
|  | 683 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) | 
|  | 684 | goto fail; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 685 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 686 | if (lib_obj == NULL) | 
|  | 687 | lib_obj = Py_None; | 
|  | 688 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) | 
|  | 689 | goto fail; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 690 |  | 
|  | 691 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { | 
|  | 692 | /* Only set verify code / message for SSLCertVerificationError */ | 
|  | 693 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, | 
|  | 694 | verify_code_obj)) | 
|  | 695 | goto fail; | 
|  | 696 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) | 
|  | 697 | goto fail; | 
|  | 698 | } | 
|  | 699 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 700 | PyErr_SetObject(type, err_value); | 
|  | 701 | fail: | 
|  | 702 | Py_XDECREF(err_value); | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 703 | Py_XDECREF(verify_code_obj); | 
|  | 704 | Py_XDECREF(verify_obj); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 705 | } | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 706 |  | 
|  | 707 | static PyObject * | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 708 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 709 | { | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 710 | PyObject *type = PySSLErrorObject; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 711 | char *errstr = NULL; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 712 | _PySSLError err; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 713 | enum py_ssl_error p = PY_SSL_ERROR_NONE; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 714 | unsigned long e = 0; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 715 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 716 | assert(ret <= 0); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 717 | e = ERR_peek_last_error(); | 
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 718 |  | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 719 | if (sslsock->ssl != NULL) { | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 720 | err = sslsock->err; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 721 |  | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 722 | switch (err.ssl) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 723 | case SSL_ERROR_ZERO_RETURN: | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 724 | errstr = "TLS/SSL connection has been closed (EOF)"; | 
|  | 725 | type = PySSLZeroReturnErrorObject; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 726 | p = PY_SSL_ERROR_ZERO_RETURN; | 
|  | 727 | break; | 
|  | 728 | case SSL_ERROR_WANT_READ: | 
|  | 729 | errstr = "The operation did not complete (read)"; | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 730 | type = PySSLWantReadErrorObject; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 731 | p = PY_SSL_ERROR_WANT_READ; | 
|  | 732 | break; | 
|  | 733 | case SSL_ERROR_WANT_WRITE: | 
|  | 734 | p = PY_SSL_ERROR_WANT_WRITE; | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 735 | type = PySSLWantWriteErrorObject; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 736 | errstr = "The operation did not complete (write)"; | 
|  | 737 | break; | 
|  | 738 | case SSL_ERROR_WANT_X509_LOOKUP: | 
|  | 739 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 740 | errstr = "The operation did not complete (X509 lookup)"; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 741 | break; | 
|  | 742 | case SSL_ERROR_WANT_CONNECT: | 
|  | 743 | p = PY_SSL_ERROR_WANT_CONNECT; | 
|  | 744 | errstr = "The operation did not complete (connect)"; | 
|  | 745 | break; | 
|  | 746 | case SSL_ERROR_SYSCALL: | 
|  | 747 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 748 | if (e == 0) { | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 749 | PySocketSockObject *s = GET_SOCKET(sslsock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 750 | if (ret == 0 || (((PyObject *)s) == Py_None)) { | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 751 | p = PY_SSL_ERROR_EOF; | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 752 | type = PySSLEOFErrorObject; | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 753 | errstr = "EOF occurred in violation of protocol"; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 754 | } else if (s && ret == -1) { | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 755 | /* underlying BIO reported an I/O error */ | 
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 756 | ERR_clear_error(); | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 757 | #ifdef MS_WINDOWS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 758 | if (err.ws) { | 
|  | 759 | return PyErr_SetFromWindowsErr(err.ws); | 
|  | 760 | } | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 761 | #endif | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 762 | if (err.c) { | 
|  | 763 | errno = err.c; | 
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 764 | return PyErr_SetFromErrno(PyExc_OSError); | 
|  | 765 | } | 
|  | 766 | Py_INCREF(s); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 767 | s->errorhandler(); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 768 | Py_DECREF(s); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 769 | return NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 770 | } else { /* possible? */ | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 771 | p = PY_SSL_ERROR_SYSCALL; | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 772 | type = PySSLSyscallErrorObject; | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 773 | errstr = "Some I/O error occurred"; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 774 | } | 
|  | 775 | } else { | 
|  | 776 | p = PY_SSL_ERROR_SYSCALL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 777 | } | 
|  | 778 | break; | 
|  | 779 | } | 
|  | 780 | case SSL_ERROR_SSL: | 
|  | 781 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 782 | p = PY_SSL_ERROR_SSL; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 783 | if (e == 0) { | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 784 | /* possible? */ | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 785 | errstr = "A failure in the SSL library occurred"; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 786 | } | 
|  | 787 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && | 
|  | 788 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { | 
|  | 789 | type = PySSLCertVerificationErrorObject; | 
|  | 790 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 791 | break; | 
|  | 792 | } | 
|  | 793 | default: | 
|  | 794 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; | 
|  | 795 | errstr = "Invalid error code"; | 
|  | 796 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 797 | } | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 798 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); | 
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 799 | ERR_clear_error(); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 800 | return NULL; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 801 | } | 
|  | 802 |  | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 803 | static PyObject * | 
| Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 804 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 805 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 806 | if (errstr == NULL) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 807 | errcode = ERR_peek_last_error(); | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 808 | else | 
|  | 809 | errcode = 0; | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 810 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); | 
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 811 | ERR_clear_error(); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 812 | return NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 813 | } | 
|  | 814 |  | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 815 | /* | 
|  | 816 | * SSL objects | 
|  | 817 | */ | 
|  | 818 |  | 
|  | 819 | static int | 
|  | 820 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) | 
|  | 821 | { | 
|  | 822 | int retval = -1; | 
|  | 823 | ASN1_OCTET_STRING *ip; | 
|  | 824 | PyObject *hostname; | 
|  | 825 | size_t len; | 
|  | 826 |  | 
|  | 827 | assert(server_hostname); | 
|  | 828 |  | 
|  | 829 | /* Disable OpenSSL's special mode with leading dot in hostname: | 
|  | 830 | * When name starts with a dot (e.g ".example.com"), it will be | 
|  | 831 | * matched by a certificate valid for any sub-domain of name. | 
|  | 832 | */ | 
|  | 833 | len = strlen(server_hostname); | 
|  | 834 | if (len == 0 || *server_hostname == '.') { | 
|  | 835 | PyErr_SetString( | 
|  | 836 | PyExc_ValueError, | 
|  | 837 | "server_hostname cannot be an empty string or start with a " | 
|  | 838 | "leading dot."); | 
|  | 839 | return retval; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | /* inet_pton is not available on all platforms. */ | 
|  | 843 | ip = a2i_IPADDRESS(server_hostname); | 
|  | 844 | if (ip == NULL) { | 
|  | 845 | ERR_clear_error(); | 
|  | 846 | } | 
|  | 847 |  | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 848 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 849 | if (hostname == NULL) { | 
|  | 850 | goto error; | 
|  | 851 | } | 
|  | 852 | self->server_hostname = hostname; | 
|  | 853 |  | 
|  | 854 | /* Only send SNI extension for non-IP hostnames */ | 
|  | 855 | if (ip == NULL) { | 
|  | 856 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { | 
|  | 857 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 858 | } | 
|  | 859 | } | 
|  | 860 | if (self->ctx->check_hostname) { | 
|  | 861 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); | 
|  | 862 | if (ip == NULL) { | 
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 863 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, | 
|  | 864 | strlen(server_hostname))) { | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 865 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 866 | goto error; | 
|  | 867 | } | 
|  | 868 | } else { | 
|  | 869 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip), | 
|  | 870 | ASN1_STRING_length(ip))) { | 
|  | 871 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 872 | goto error; | 
|  | 873 | } | 
|  | 874 | } | 
|  | 875 | } | 
|  | 876 | retval = 0; | 
|  | 877 | error: | 
|  | 878 | if (ip != NULL) { | 
|  | 879 | ASN1_OCTET_STRING_free(ip); | 
|  | 880 | } | 
|  | 881 | return retval; | 
|  | 882 | } | 
|  | 883 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 884 | static PySSLSocket * | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 885 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 886 | enum py_ssl_server_or_client socket_type, | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 887 | char *server_hostname, | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 888 | PyObject *owner, PyObject *session, | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 889 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 890 | { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 891 | PySSLSocket *self; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 892 | SSL_CTX *ctx = sslctx->ctx; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 893 | _PySSLError err = { 0 }; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 894 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 895 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 896 | if (self == NULL) | 
|  | 897 | return NULL; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 898 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 899 | self->ssl = NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 900 | self->Socket = NULL; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 901 | self->ctx = sslctx; | 
| Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 902 | Py_INCREF(sslctx); | 
| Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 903 | self->shutdown_seen_zero = 0; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 904 | self->owner = NULL; | 
| Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 905 | self->server_hostname = NULL; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 906 | self->err = err; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 907 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 908 | /* Make sure the SSL error state is initialized */ | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 909 | ERR_clear_error(); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 910 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 911 | PySSL_BEGIN_ALLOW_THREADS | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 912 | self->ssl = SSL_new(ctx); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 913 | PySSL_END_ALLOW_THREADS | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 914 | SSL_set_app_data(self->ssl, self); | 
|  | 915 | if (sock) { | 
|  | 916 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); | 
|  | 917 | } else { | 
|  | 918 | /* BIOs are reference counted and SSL_set_bio borrows our reference. | 
|  | 919 | * To prevent a double free in memory_bio_dealloc() we need to take an | 
|  | 920 | * extra reference here. */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 921 | BIO_up_ref(inbio->bio); | 
|  | 922 | BIO_up_ref(outbio->bio); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 923 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); | 
|  | 924 | } | 
| Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 925 | SSL_set_mode(self->ssl, | 
|  | 926 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); | 
| Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 927 |  | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 928 | if (server_hostname != NULL) { | 
|  | 929 | if (_ssl_configure_hostname(self, server_hostname) < 0) { | 
|  | 930 | Py_DECREF(self); | 
|  | 931 | return NULL; | 
|  | 932 | } | 
|  | 933 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 934 | /* If the socket is in non-blocking mode or timeout mode, set the BIO | 
|  | 935 | * to non-blocking mode (blocking is the default) | 
|  | 936 | */ | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 937 | if (sock && sock->sock_timeout >= 0) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 938 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); | 
|  | 939 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); | 
|  | 940 | } | 
| Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 941 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 942 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 943 | if (socket_type == PY_SSL_CLIENT) | 
|  | 944 | SSL_set_connect_state(self->ssl); | 
|  | 945 | else | 
|  | 946 | SSL_set_accept_state(self->ssl); | 
|  | 947 | PySSL_END_ALLOW_THREADS | 
| Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 948 |  | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 949 | self->socket_type = socket_type; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 950 | if (sock != NULL) { | 
|  | 951 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); | 
|  | 952 | if (self->Socket == NULL) { | 
|  | 953 | Py_DECREF(self); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 954 | return NULL; | 
|  | 955 | } | 
| Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 956 | } | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 957 | if (owner && owner != Py_None) { | 
|  | 958 | if (PySSL_set_owner(self, owner, NULL) == -1) { | 
|  | 959 | Py_DECREF(self); | 
|  | 960 | return NULL; | 
|  | 961 | } | 
|  | 962 | } | 
|  | 963 | if (session && session != Py_None) { | 
|  | 964 | if (PySSL_set_session(self, session, NULL) == -1) { | 
|  | 965 | Py_DECREF(self); | 
|  | 966 | return NULL; | 
|  | 967 | } | 
|  | 968 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 969 | return self; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 970 | } | 
|  | 971 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 972 | /* SSL object methods */ | 
|  | 973 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 974 | /*[clinic input] | 
|  | 975 | _ssl._SSLSocket.do_handshake | 
|  | 976 | [clinic start generated code]*/ | 
|  | 977 |  | 
|  | 978 | static PyObject * | 
|  | 979 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) | 
|  | 980 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 981 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 982 | int ret; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 983 | _PySSLError err; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 984 | int sockstate, nonblocking; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 985 | PySocketSockObject *sock = GET_SOCKET(self); | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 986 | _PyTime_t timeout, deadline = 0; | 
|  | 987 | int has_timeout; | 
| Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 988 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 989 | if (sock) { | 
|  | 990 | if (((PyObject*)sock) == Py_None) { | 
|  | 991 | _setSSLError("Underlying socket connection gone", | 
|  | 992 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); | 
|  | 993 | return NULL; | 
|  | 994 | } | 
|  | 995 | Py_INCREF(sock); | 
|  | 996 |  | 
|  | 997 | /* just in case the blocking state of the socket has been changed */ | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 998 | nonblocking = (sock->sock_timeout >= 0); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 999 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); | 
|  | 1000 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1001 | } | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1002 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1003 | timeout = GET_SOCKET_TIMEOUT(sock); | 
|  | 1004 | has_timeout = (timeout > 0); | 
|  | 1005 | if (has_timeout) | 
|  | 1006 | deadline = _PyTime_GetMonotonicClock() + timeout; | 
|  | 1007 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1008 | /* Actually negotiate SSL connection */ | 
|  | 1009 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1010 | do { | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1011 | PySSL_BEGIN_ALLOW_THREADS | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1012 | ret = SSL_do_handshake(self->ssl); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1013 | err = _PySSL_errno(ret < 1, self->ssl, ret); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1014 | PySSL_END_ALLOW_THREADS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1015 | self->err = err; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1016 |  | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1017 | if (PyErr_CheckSignals()) | 
|  | 1018 | goto error; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1019 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1020 | if (has_timeout) | 
|  | 1021 | timeout = deadline - _PyTime_GetMonotonicClock(); | 
|  | 1022 |  | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1023 | if (err.ssl == SSL_ERROR_WANT_READ) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1024 | sockstate = PySSL_select(sock, 0, timeout); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1025 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1026 | sockstate = PySSL_select(sock, 1, timeout); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1027 | } else { | 
|  | 1028 | sockstate = SOCKET_OPERATION_OK; | 
|  | 1029 | } | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1030 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1031 | if (sockstate == SOCKET_HAS_TIMED_OUT) { | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1032 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1033 | ERRSTR("The handshake operation timed out")); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1034 | goto error; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1035 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { | 
|  | 1036 | PyErr_SetString(PySSLErrorObject, | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1037 | ERRSTR("Underlying socket has been closed.")); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1038 | goto error; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1039 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { | 
|  | 1040 | PyErr_SetString(PySSLErrorObject, | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1041 | ERRSTR("Underlying socket too large for select().")); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1042 | goto error; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1043 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { | 
|  | 1044 | break; | 
|  | 1045 | } | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1046 | } while (err.ssl == SSL_ERROR_WANT_READ || | 
|  | 1047 | err.ssl == SSL_ERROR_WANT_WRITE); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1048 | Py_XDECREF(sock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1049 | if (ret < 1) | 
|  | 1050 | return PySSL_SetError(self, ret, __FILE__, __LINE__); | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1051 |  | 
| Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1052 | Py_RETURN_NONE; | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1053 |  | 
|  | 1054 | error: | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1055 | Py_XDECREF(sock); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1056 | return NULL; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1057 | } | 
|  | 1058 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1059 | static PyObject * | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1060 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) | 
|  | 1061 | { | 
|  | 1062 | char buf[X509_NAME_MAXLEN]; | 
|  | 1063 | char *namebuf = buf; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1064 | int buflen; | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1065 | PyObject *name_obj = NULL; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1066 |  | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1067 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1068 | if (buflen < 0) { | 
|  | 1069 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1070 | return NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1071 | } | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1072 | /* initial buffer is too small for oid + terminating null byte */ | 
|  | 1073 | if (buflen > X509_NAME_MAXLEN - 1) { | 
|  | 1074 | /* make OBJ_obj2txt() calculate the required buflen */ | 
|  | 1075 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); | 
|  | 1076 | /* allocate len + 1 for terminating NULL byte */ | 
|  | 1077 | namebuf = PyMem_Malloc(buflen + 1); | 
|  | 1078 | if (namebuf == NULL) { | 
|  | 1079 | PyErr_NoMemory(); | 
|  | 1080 | return NULL; | 
|  | 1081 | } | 
|  | 1082 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); | 
|  | 1083 | if (buflen < 0) { | 
|  | 1084 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1085 | goto done; | 
|  | 1086 | } | 
|  | 1087 | } | 
|  | 1088 | if (!buflen && no_name) { | 
|  | 1089 | Py_INCREF(Py_None); | 
|  | 1090 | name_obj = Py_None; | 
|  | 1091 | } | 
|  | 1092 | else { | 
|  | 1093 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); | 
|  | 1094 | } | 
|  | 1095 |  | 
|  | 1096 | done: | 
|  | 1097 | if (buf != namebuf) { | 
|  | 1098 | PyMem_Free(namebuf); | 
|  | 1099 | } | 
|  | 1100 | return name_obj; | 
|  | 1101 | } | 
|  | 1102 |  | 
|  | 1103 | static PyObject * | 
|  | 1104 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) | 
|  | 1105 | { | 
|  | 1106 | Py_ssize_t buflen; | 
|  | 1107 | unsigned char *valuebuf = NULL; | 
|  | 1108 | PyObject *attr; | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1109 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1110 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); | 
|  | 1111 | if (buflen < 0) { | 
|  | 1112 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1113 | return NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1114 | } | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1115 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1116 | OPENSSL_free(valuebuf); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1117 | return attr; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1118 | } | 
|  | 1119 |  | 
|  | 1120 | static PyObject * | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1121 | _create_tuple_for_X509_NAME (X509_NAME *xname) | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1122 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1123 | PyObject *dn = NULL;    /* tuple which represents the "distinguished name" */ | 
|  | 1124 | PyObject *rdn = NULL;   /* tuple to hold a "relative distinguished name" */ | 
|  | 1125 | PyObject *rdnt; | 
|  | 1126 | PyObject *attr = NULL;   /* tuple to hold an attribute */ | 
|  | 1127 | int entry_count = X509_NAME_entry_count(xname); | 
|  | 1128 | X509_NAME_ENTRY *entry; | 
|  | 1129 | ASN1_OBJECT *name; | 
|  | 1130 | ASN1_STRING *value; | 
|  | 1131 | int index_counter; | 
|  | 1132 | int rdn_level = -1; | 
|  | 1133 | int retcode; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1134 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1135 | dn = PyList_New(0); | 
|  | 1136 | if (dn == NULL) | 
|  | 1137 | return NULL; | 
|  | 1138 | /* now create another tuple to hold the top-level RDN */ | 
|  | 1139 | rdn = PyList_New(0); | 
|  | 1140 | if (rdn == NULL) | 
|  | 1141 | goto fail0; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1142 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1143 | for (index_counter = 0; | 
|  | 1144 | index_counter < entry_count; | 
|  | 1145 | index_counter++) | 
|  | 1146 | { | 
|  | 1147 | entry = X509_NAME_get_entry(xname, index_counter); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1148 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1149 | /* check to see if we've gotten to a new RDN */ | 
|  | 1150 | if (rdn_level >= 0) { | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1151 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1152 | /* yes, new RDN */ | 
|  | 1153 | /* add old RDN to DN */ | 
|  | 1154 | rdnt = PyList_AsTuple(rdn); | 
|  | 1155 | Py_DECREF(rdn); | 
|  | 1156 | if (rdnt == NULL) | 
|  | 1157 | goto fail0; | 
|  | 1158 | retcode = PyList_Append(dn, rdnt); | 
|  | 1159 | Py_DECREF(rdnt); | 
|  | 1160 | if (retcode < 0) | 
|  | 1161 | goto fail0; | 
|  | 1162 | /* create new RDN */ | 
|  | 1163 | rdn = PyList_New(0); | 
|  | 1164 | if (rdn == NULL) | 
|  | 1165 | goto fail0; | 
|  | 1166 | } | 
|  | 1167 | } | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1168 | rdn_level = X509_NAME_ENTRY_set(entry); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1169 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1170 | /* now add this attribute to the current RDN */ | 
|  | 1171 | name = X509_NAME_ENTRY_get_object(entry); | 
|  | 1172 | value = X509_NAME_ENTRY_get_data(entry); | 
|  | 1173 | attr = _create_tuple_for_attribute(name, value); | 
|  | 1174 | /* | 
|  | 1175 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", | 
|  | 1176 | entry->set, | 
|  | 1177 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), | 
|  | 1178 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); | 
|  | 1179 | */ | 
|  | 1180 | if (attr == NULL) | 
|  | 1181 | goto fail1; | 
|  | 1182 | retcode = PyList_Append(rdn, attr); | 
|  | 1183 | Py_DECREF(attr); | 
|  | 1184 | if (retcode < 0) | 
|  | 1185 | goto fail1; | 
|  | 1186 | } | 
|  | 1187 | /* now, there's typically a dangling RDN */ | 
| Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1188 | if (rdn != NULL) { | 
|  | 1189 | if (PyList_GET_SIZE(rdn) > 0) { | 
|  | 1190 | rdnt = PyList_AsTuple(rdn); | 
|  | 1191 | Py_DECREF(rdn); | 
|  | 1192 | if (rdnt == NULL) | 
|  | 1193 | goto fail0; | 
|  | 1194 | retcode = PyList_Append(dn, rdnt); | 
|  | 1195 | Py_DECREF(rdnt); | 
|  | 1196 | if (retcode < 0) | 
|  | 1197 | goto fail0; | 
|  | 1198 | } | 
|  | 1199 | else { | 
|  | 1200 | Py_DECREF(rdn); | 
|  | 1201 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1202 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1203 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1204 | /* convert list to tuple */ | 
|  | 1205 | rdnt = PyList_AsTuple(dn); | 
|  | 1206 | Py_DECREF(dn); | 
|  | 1207 | if (rdnt == NULL) | 
|  | 1208 | return NULL; | 
|  | 1209 | return rdnt; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1210 |  | 
|  | 1211 | fail1: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1212 | Py_XDECREF(rdn); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1213 |  | 
|  | 1214 | fail0: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1215 | Py_XDECREF(dn); | 
|  | 1216 | return NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1217 | } | 
|  | 1218 |  | 
|  | 1219 | static PyObject * | 
|  | 1220 | _get_peer_alt_names (X509 *certificate) { | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1221 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1222 | /* this code follows the procedure outlined in | 
|  | 1223 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() | 
|  | 1224 | function to extract the STACK_OF(GENERAL_NAME), | 
|  | 1225 | then iterates through the stack to add the | 
|  | 1226 | names. */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1227 |  | 
| Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1228 | int j; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1229 | PyObject *peer_alt_names = Py_None; | 
| Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1230 | PyObject *v = NULL, *t; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1231 | GENERAL_NAMES *names = NULL; | 
|  | 1232 | GENERAL_NAME *name; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1233 | BIO *biobuf = NULL; | 
|  | 1234 | char buf[2048]; | 
|  | 1235 | char *vptr; | 
|  | 1236 | int len; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1237 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1238 | if (certificate == NULL) | 
|  | 1239 | return peer_alt_names; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1240 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1241 | /* get a memory buffer */ | 
|  | 1242 | biobuf = BIO_new(BIO_s_mem()); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1243 |  | 
| Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1244 | names = (GENERAL_NAMES *)X509_get_ext_d2i( | 
|  | 1245 | certificate, NID_subject_alt_name, NULL, NULL); | 
|  | 1246 | if (names != NULL) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1247 | if (peer_alt_names == Py_None) { | 
|  | 1248 | peer_alt_names = PyList_New(0); | 
|  | 1249 | if (peer_alt_names == NULL) | 
|  | 1250 | goto fail; | 
|  | 1251 | } | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1252 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1253 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1254 | /* get a rendering of each name in the set of names */ | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1255 | int gntype; | 
|  | 1256 | ASN1_STRING *as = NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1257 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1258 | name = sk_GENERAL_NAME_value(names, j); | 
| Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1259 | gntype = name->type; | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1260 | switch (gntype) { | 
|  | 1261 | case GEN_DIRNAME: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1262 | /* we special-case DirName as a tuple of | 
|  | 1263 | tuples of attributes */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1264 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1265 | t = PyTuple_New(2); | 
|  | 1266 | if (t == NULL) { | 
|  | 1267 | goto fail; | 
|  | 1268 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1269 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1270 | v = PyUnicode_FromString("DirName"); | 
|  | 1271 | if (v == NULL) { | 
|  | 1272 | Py_DECREF(t); | 
|  | 1273 | goto fail; | 
|  | 1274 | } | 
|  | 1275 | PyTuple_SET_ITEM(t, 0, v); | 
| 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 | v = _create_tuple_for_X509_NAME (name->d.dirn); | 
|  | 1278 | if (v == NULL) { | 
|  | 1279 | Py_DECREF(t); | 
|  | 1280 | goto fail; | 
|  | 1281 | } | 
|  | 1282 | PyTuple_SET_ITEM(t, 1, v); | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1283 | break; | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1284 |  | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1285 | case GEN_EMAIL: | 
|  | 1286 | case GEN_DNS: | 
|  | 1287 | case GEN_URI: | 
|  | 1288 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string | 
|  | 1289 | correctly, CVE-2013-4238 */ | 
|  | 1290 | t = PyTuple_New(2); | 
|  | 1291 | if (t == NULL) | 
|  | 1292 | goto fail; | 
|  | 1293 | switch (gntype) { | 
|  | 1294 | case GEN_EMAIL: | 
|  | 1295 | v = PyUnicode_FromString("email"); | 
|  | 1296 | as = name->d.rfc822Name; | 
|  | 1297 | break; | 
|  | 1298 | case GEN_DNS: | 
|  | 1299 | v = PyUnicode_FromString("DNS"); | 
|  | 1300 | as = name->d.dNSName; | 
|  | 1301 | break; | 
|  | 1302 | case GEN_URI: | 
|  | 1303 | v = PyUnicode_FromString("URI"); | 
|  | 1304 | as = name->d.uniformResourceIdentifier; | 
|  | 1305 | break; | 
|  | 1306 | } | 
|  | 1307 | if (v == NULL) { | 
|  | 1308 | Py_DECREF(t); | 
|  | 1309 | goto fail; | 
|  | 1310 | } | 
|  | 1311 | PyTuple_SET_ITEM(t, 0, v); | 
|  | 1312 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as), | 
|  | 1313 | ASN1_STRING_length(as)); | 
|  | 1314 | if (v == NULL) { | 
|  | 1315 | Py_DECREF(t); | 
|  | 1316 | goto fail; | 
|  | 1317 | } | 
|  | 1318 | PyTuple_SET_ITEM(t, 1, v); | 
|  | 1319 | break; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1320 |  | 
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1321 | case GEN_RID: | 
|  | 1322 | t = PyTuple_New(2); | 
|  | 1323 | if (t == NULL) | 
|  | 1324 | goto fail; | 
|  | 1325 |  | 
|  | 1326 | v = PyUnicode_FromString("Registered ID"); | 
|  | 1327 | if (v == NULL) { | 
|  | 1328 | Py_DECREF(t); | 
|  | 1329 | goto fail; | 
|  | 1330 | } | 
|  | 1331 | PyTuple_SET_ITEM(t, 0, v); | 
|  | 1332 |  | 
|  | 1333 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); | 
|  | 1334 | if (len < 0) { | 
|  | 1335 | Py_DECREF(t); | 
|  | 1336 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1337 | goto fail; | 
|  | 1338 | } else if (len >= (int)sizeof(buf)) { | 
|  | 1339 | v = PyUnicode_FromString("<INVALID>"); | 
|  | 1340 | } else { | 
|  | 1341 | v = PyUnicode_FromStringAndSize(buf, len); | 
|  | 1342 | } | 
|  | 1343 | if (v == NULL) { | 
|  | 1344 | Py_DECREF(t); | 
|  | 1345 | goto fail; | 
|  | 1346 | } | 
|  | 1347 | PyTuple_SET_ITEM(t, 1, v); | 
|  | 1348 | break; | 
|  | 1349 |  | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1350 | default: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1351 | /* for everything else, we use the OpenSSL print form */ | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1352 | switch (gntype) { | 
|  | 1353 | /* check for new general name type */ | 
|  | 1354 | case GEN_OTHERNAME: | 
|  | 1355 | case GEN_X400: | 
|  | 1356 | case GEN_EDIPARTY: | 
|  | 1357 | case GEN_IPADD: | 
|  | 1358 | case GEN_RID: | 
|  | 1359 | break; | 
|  | 1360 | default: | 
|  | 1361 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, | 
|  | 1362 | "Unknown general name type %d", | 
|  | 1363 | gntype) == -1) { | 
|  | 1364 | goto fail; | 
|  | 1365 | } | 
|  | 1366 | break; | 
|  | 1367 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1368 | (void) BIO_reset(biobuf); | 
|  | 1369 | GENERAL_NAME_print(biobuf, name); | 
|  | 1370 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); | 
|  | 1371 | if (len < 0) { | 
|  | 1372 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1373 | goto fail; | 
|  | 1374 | } | 
|  | 1375 | vptr = strchr(buf, ':'); | 
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1376 | if (vptr == NULL) { | 
|  | 1377 | PyErr_Format(PyExc_ValueError, | 
|  | 1378 | "Invalid value %.200s", | 
|  | 1379 | buf); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1380 | goto fail; | 
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1381 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1382 | t = PyTuple_New(2); | 
|  | 1383 | if (t == NULL) | 
|  | 1384 | goto fail; | 
|  | 1385 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); | 
|  | 1386 | if (v == NULL) { | 
|  | 1387 | Py_DECREF(t); | 
|  | 1388 | goto fail; | 
|  | 1389 | } | 
|  | 1390 | PyTuple_SET_ITEM(t, 0, v); | 
|  | 1391 | v = PyUnicode_FromStringAndSize((vptr + 1), | 
|  | 1392 | (len - (vptr - buf + 1))); | 
|  | 1393 | if (v == NULL) { | 
|  | 1394 | Py_DECREF(t); | 
|  | 1395 | goto fail; | 
|  | 1396 | } | 
|  | 1397 | PyTuple_SET_ITEM(t, 1, v); | 
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1398 | break; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1399 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1400 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1401 | /* and add that rendering to the list */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1402 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1403 | if (PyList_Append(peer_alt_names, t) < 0) { | 
|  | 1404 | Py_DECREF(t); | 
|  | 1405 | goto fail; | 
|  | 1406 | } | 
|  | 1407 | Py_DECREF(t); | 
|  | 1408 | } | 
| Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1409 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1410 | } | 
|  | 1411 | BIO_free(biobuf); | 
|  | 1412 | if (peer_alt_names != Py_None) { | 
|  | 1413 | v = PyList_AsTuple(peer_alt_names); | 
|  | 1414 | Py_DECREF(peer_alt_names); | 
|  | 1415 | return v; | 
|  | 1416 | } else { | 
|  | 1417 | return peer_alt_names; | 
|  | 1418 | } | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1419 |  | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1420 |  | 
|  | 1421 | fail: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1422 | if (biobuf != NULL) | 
|  | 1423 | BIO_free(biobuf); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1424 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1425 | if (peer_alt_names != Py_None) { | 
|  | 1426 | Py_XDECREF(peer_alt_names); | 
|  | 1427 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1428 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1429 | return NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1430 | } | 
|  | 1431 |  | 
|  | 1432 | static PyObject * | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1433 | _get_aia_uri(X509 *certificate, int nid) { | 
|  | 1434 | PyObject *lst = NULL, *ostr = NULL; | 
|  | 1435 | int i, result; | 
|  | 1436 | AUTHORITY_INFO_ACCESS *info; | 
|  | 1437 |  | 
|  | 1438 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); | 
| Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1439 | if (info == NULL) | 
|  | 1440 | return Py_None; | 
|  | 1441 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { | 
|  | 1442 | AUTHORITY_INFO_ACCESS_free(info); | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1443 | return Py_None; | 
|  | 1444 | } | 
|  | 1445 |  | 
|  | 1446 | if ((lst = PyList_New(0)) == NULL) { | 
|  | 1447 | goto fail; | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { | 
|  | 1451 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); | 
|  | 1452 | ASN1_IA5STRING *uri; | 
|  | 1453 |  | 
|  | 1454 | if ((OBJ_obj2nid(ad->method) != nid) || | 
|  | 1455 | (ad->location->type != GEN_URI)) { | 
|  | 1456 | continue; | 
|  | 1457 | } | 
|  | 1458 | uri = ad->location->d.uniformResourceIdentifier; | 
|  | 1459 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, | 
|  | 1460 | uri->length); | 
|  | 1461 | if (ostr == NULL) { | 
|  | 1462 | goto fail; | 
|  | 1463 | } | 
|  | 1464 | result = PyList_Append(lst, ostr); | 
|  | 1465 | Py_DECREF(ostr); | 
|  | 1466 | if (result < 0) { | 
|  | 1467 | goto fail; | 
|  | 1468 | } | 
|  | 1469 | } | 
|  | 1470 | AUTHORITY_INFO_ACCESS_free(info); | 
|  | 1471 |  | 
|  | 1472 | /* convert to tuple or None */ | 
|  | 1473 | if (PyList_Size(lst) == 0) { | 
|  | 1474 | Py_DECREF(lst); | 
|  | 1475 | return Py_None; | 
|  | 1476 | } else { | 
|  | 1477 | PyObject *tup; | 
|  | 1478 | tup = PyList_AsTuple(lst); | 
|  | 1479 | Py_DECREF(lst); | 
|  | 1480 | return tup; | 
|  | 1481 | } | 
|  | 1482 |  | 
|  | 1483 | fail: | 
|  | 1484 | AUTHORITY_INFO_ACCESS_free(info); | 
| Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1485 | Py_XDECREF(lst); | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1486 | return NULL; | 
|  | 1487 | } | 
|  | 1488 |  | 
|  | 1489 | static PyObject * | 
|  | 1490 | _get_crl_dp(X509 *certificate) { | 
|  | 1491 | STACK_OF(DIST_POINT) *dps; | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1492 | int i, j; | 
|  | 1493 | PyObject *lst, *res = NULL; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1494 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1495 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); | 
| Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1496 |  | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1497 | if (dps == NULL) | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1498 | return Py_None; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1499 |  | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1500 | lst = PyList_New(0); | 
|  | 1501 | if (lst == NULL) | 
|  | 1502 | goto done; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1503 |  | 
|  | 1504 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { | 
|  | 1505 | DIST_POINT *dp; | 
|  | 1506 | STACK_OF(GENERAL_NAME) *gns; | 
|  | 1507 |  | 
|  | 1508 | dp = sk_DIST_POINT_value(dps, i); | 
|  | 1509 | gns = dp->distpoint->name.fullname; | 
|  | 1510 |  | 
|  | 1511 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { | 
|  | 1512 | GENERAL_NAME *gn; | 
|  | 1513 | ASN1_IA5STRING *uri; | 
|  | 1514 | PyObject *ouri; | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1515 | int err; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1516 |  | 
|  | 1517 | gn = sk_GENERAL_NAME_value(gns, j); | 
|  | 1518 | if (gn->type != GEN_URI) { | 
|  | 1519 | continue; | 
|  | 1520 | } | 
|  | 1521 | uri = gn->d.uniformResourceIdentifier; | 
|  | 1522 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, | 
|  | 1523 | uri->length); | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1524 | if (ouri == NULL) | 
|  | 1525 | goto done; | 
|  | 1526 |  | 
|  | 1527 | err = PyList_Append(lst, ouri); | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1528 | Py_DECREF(ouri); | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1529 | if (err < 0) | 
|  | 1530 | goto done; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1531 | } | 
|  | 1532 | } | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1533 |  | 
|  | 1534 | /* Convert to tuple. */ | 
|  | 1535 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; | 
|  | 1536 |  | 
|  | 1537 | done: | 
|  | 1538 | Py_XDECREF(lst); | 
| Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1539 | CRL_DIST_POINTS_free(dps); | 
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1540 | return res; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1541 | } | 
|  | 1542 |  | 
|  | 1543 | static PyObject * | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1544 | _decode_certificate(X509 *certificate) { | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1545 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1546 | PyObject *retval = NULL; | 
|  | 1547 | BIO *biobuf = NULL; | 
|  | 1548 | PyObject *peer; | 
|  | 1549 | PyObject *peer_alt_names = NULL; | 
|  | 1550 | PyObject *issuer; | 
|  | 1551 | PyObject *version; | 
|  | 1552 | PyObject *sn_obj; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1553 | PyObject *obj; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1554 | ASN1_INTEGER *serialNumber; | 
|  | 1555 | char buf[2048]; | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1556 | int len, result; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1557 | ASN1_TIME *notBefore, *notAfter; | 
|  | 1558 | PyObject *pnotBefore, *pnotAfter; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1559 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1560 | retval = PyDict_New(); | 
|  | 1561 | if (retval == NULL) | 
|  | 1562 | return NULL; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1563 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1564 | peer = _create_tuple_for_X509_NAME( | 
|  | 1565 | X509_get_subject_name(certificate)); | 
|  | 1566 | if (peer == NULL) | 
|  | 1567 | goto fail0; | 
|  | 1568 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { | 
|  | 1569 | Py_DECREF(peer); | 
|  | 1570 | goto fail0; | 
|  | 1571 | } | 
|  | 1572 | Py_DECREF(peer); | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1573 |  | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1574 | issuer = _create_tuple_for_X509_NAME( | 
|  | 1575 | X509_get_issuer_name(certificate)); | 
|  | 1576 | if (issuer == NULL) | 
|  | 1577 | goto fail0; | 
|  | 1578 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1579 | Py_DECREF(issuer); | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1580 | goto fail0; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1581 | } | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1582 | Py_DECREF(issuer); | 
|  | 1583 |  | 
|  | 1584 | version = PyLong_FromLong(X509_get_version(certificate) + 1); | 
| Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1585 | if (version == NULL) | 
|  | 1586 | goto fail0; | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1587 | if (PyDict_SetItemString(retval, "version", version) < 0) { | 
|  | 1588 | Py_DECREF(version); | 
|  | 1589 | goto fail0; | 
|  | 1590 | } | 
|  | 1591 | Py_DECREF(version); | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1592 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1593 | /* get a memory buffer */ | 
|  | 1594 | biobuf = BIO_new(BIO_s_mem()); | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1595 |  | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1596 | (void) BIO_reset(biobuf); | 
|  | 1597 | serialNumber = X509_get_serialNumber(certificate); | 
|  | 1598 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ | 
|  | 1599 | i2a_ASN1_INTEGER(biobuf, serialNumber); | 
|  | 1600 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); | 
|  | 1601 | if (len < 0) { | 
|  | 1602 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1603 | goto fail1; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1604 | } | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1605 | sn_obj = PyUnicode_FromStringAndSize(buf, len); | 
|  | 1606 | if (sn_obj == NULL) | 
|  | 1607 | goto fail1; | 
|  | 1608 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { | 
|  | 1609 | Py_DECREF(sn_obj); | 
|  | 1610 | goto fail1; | 
|  | 1611 | } | 
|  | 1612 | Py_DECREF(sn_obj); | 
|  | 1613 |  | 
|  | 1614 | (void) BIO_reset(biobuf); | 
|  | 1615 | notBefore = X509_get_notBefore(certificate); | 
|  | 1616 | ASN1_TIME_print(biobuf, notBefore); | 
|  | 1617 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); | 
|  | 1618 | if (len < 0) { | 
|  | 1619 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1620 | goto fail1; | 
|  | 1621 | } | 
|  | 1622 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); | 
|  | 1623 | if (pnotBefore == NULL) | 
|  | 1624 | goto fail1; | 
|  | 1625 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { | 
|  | 1626 | Py_DECREF(pnotBefore); | 
|  | 1627 | goto fail1; | 
|  | 1628 | } | 
|  | 1629 | Py_DECREF(pnotBefore); | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1630 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1631 | (void) BIO_reset(biobuf); | 
|  | 1632 | notAfter = X509_get_notAfter(certificate); | 
|  | 1633 | ASN1_TIME_print(biobuf, notAfter); | 
|  | 1634 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); | 
|  | 1635 | if (len < 0) { | 
|  | 1636 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1637 | goto fail1; | 
|  | 1638 | } | 
|  | 1639 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); | 
|  | 1640 | if (pnotAfter == NULL) | 
|  | 1641 | goto fail1; | 
|  | 1642 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { | 
|  | 1643 | Py_DECREF(pnotAfter); | 
|  | 1644 | goto fail1; | 
|  | 1645 | } | 
|  | 1646 | Py_DECREF(pnotAfter); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1647 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1648 | /* Now look for subjectAltName */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1649 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1650 | peer_alt_names = _get_peer_alt_names(certificate); | 
|  | 1651 | if (peer_alt_names == NULL) | 
|  | 1652 | goto fail1; | 
|  | 1653 | else if (peer_alt_names != Py_None) { | 
|  | 1654 | if (PyDict_SetItemString(retval, "subjectAltName", | 
|  | 1655 | peer_alt_names) < 0) { | 
|  | 1656 | Py_DECREF(peer_alt_names); | 
|  | 1657 | goto fail1; | 
|  | 1658 | } | 
|  | 1659 | Py_DECREF(peer_alt_names); | 
|  | 1660 | } | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1661 |  | 
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1662 | /* Authority Information Access: OCSP URIs */ | 
|  | 1663 | obj = _get_aia_uri(certificate, NID_ad_OCSP); | 
|  | 1664 | if (obj == NULL) { | 
|  | 1665 | goto fail1; | 
|  | 1666 | } else if (obj != Py_None) { | 
|  | 1667 | result = PyDict_SetItemString(retval, "OCSP", obj); | 
|  | 1668 | Py_DECREF(obj); | 
|  | 1669 | if (result < 0) { | 
|  | 1670 | goto fail1; | 
|  | 1671 | } | 
|  | 1672 | } | 
|  | 1673 |  | 
|  | 1674 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); | 
|  | 1675 | if (obj == NULL) { | 
|  | 1676 | goto fail1; | 
|  | 1677 | } else if (obj != Py_None) { | 
|  | 1678 | result = PyDict_SetItemString(retval, "caIssuers", obj); | 
|  | 1679 | Py_DECREF(obj); | 
|  | 1680 | if (result < 0) { | 
|  | 1681 | goto fail1; | 
|  | 1682 | } | 
|  | 1683 | } | 
|  | 1684 |  | 
|  | 1685 | /* CDP (CRL distribution points) */ | 
|  | 1686 | obj = _get_crl_dp(certificate); | 
|  | 1687 | if (obj == NULL) { | 
|  | 1688 | goto fail1; | 
|  | 1689 | } else if (obj != Py_None) { | 
|  | 1690 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); | 
|  | 1691 | Py_DECREF(obj); | 
|  | 1692 | if (result < 0) { | 
|  | 1693 | goto fail1; | 
|  | 1694 | } | 
|  | 1695 | } | 
|  | 1696 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1697 | BIO_free(biobuf); | 
|  | 1698 | return retval; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1699 |  | 
|  | 1700 | fail1: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1701 | if (biobuf != NULL) | 
|  | 1702 | BIO_free(biobuf); | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1703 | fail0: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1704 | Py_XDECREF(retval); | 
|  | 1705 | return NULL; | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1706 | } | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1707 |  | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1708 | static PyObject * | 
|  | 1709 | _certificate_to_der(X509 *certificate) | 
|  | 1710 | { | 
|  | 1711 | unsigned char *bytes_buf = NULL; | 
|  | 1712 | int len; | 
|  | 1713 | PyObject *retval; | 
|  | 1714 |  | 
|  | 1715 | bytes_buf = NULL; | 
|  | 1716 | len = i2d_X509(certificate, &bytes_buf); | 
|  | 1717 | if (len < 0) { | 
|  | 1718 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 1719 | return NULL; | 
|  | 1720 | } | 
|  | 1721 | /* this is actually an immutable bytes sequence */ | 
|  | 1722 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); | 
|  | 1723 | OPENSSL_free(bytes_buf); | 
|  | 1724 | return retval; | 
|  | 1725 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1726 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1727 | /*[clinic input] | 
|  | 1728 | _ssl._test_decode_cert | 
|  | 1729 | path: object(converter="PyUnicode_FSConverter") | 
|  | 1730 | / | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1731 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1732 | [clinic start generated code]*/ | 
|  | 1733 |  | 
|  | 1734 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1735 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) | 
|  | 1736 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1737 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1738 | PyObject *retval = NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1739 | X509 *x=NULL; | 
|  | 1740 | BIO *cert; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1741 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1742 | if ((cert=BIO_new(BIO_s_file())) == NULL) { | 
|  | 1743 | PyErr_SetString(PySSLErrorObject, | 
|  | 1744 | "Can't malloc memory to read file"); | 
|  | 1745 | goto fail0; | 
|  | 1746 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1747 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1748 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1749 | PyErr_SetString(PySSLErrorObject, | 
|  | 1750 | "Can't open file"); | 
|  | 1751 | goto fail0; | 
|  | 1752 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1753 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1754 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); | 
|  | 1755 | if (x == NULL) { | 
|  | 1756 | PyErr_SetString(PySSLErrorObject, | 
|  | 1757 | "Error decoding PEM-encoded file"); | 
|  | 1758 | goto fail0; | 
|  | 1759 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1760 |  | 
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1761 | retval = _decode_certificate(x); | 
| Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1762 | X509_free(x); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1763 |  | 
|  | 1764 | fail0: | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1765 | Py_DECREF(path); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1766 | if (cert != NULL) BIO_free(cert); | 
|  | 1767 | return retval; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1768 | } | 
|  | 1769 |  | 
|  | 1770 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1771 | /*[clinic input] | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1772 | _ssl._SSLSocket.getpeercert | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1773 | der as binary_mode: bool = False | 
|  | 1774 | / | 
|  | 1775 |  | 
|  | 1776 | Returns the certificate for the peer. | 
|  | 1777 |  | 
|  | 1778 | If no certificate was provided, returns None.  If a certificate was | 
|  | 1779 | provided, but not validated, returns an empty dictionary.  Otherwise | 
|  | 1780 | returns a dict containing information about the peer certificate. | 
|  | 1781 |  | 
|  | 1782 | If the optional argument is True, returns a DER-encoded copy of the | 
|  | 1783 | peer certificate, or None if no certificate was provided.  This will | 
|  | 1784 | return the certificate even if it wasn't validated. | 
|  | 1785 | [clinic start generated code]*/ | 
|  | 1786 |  | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1787 | static PyObject * | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1788 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) | 
|  | 1789 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1790 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1791 | int verification; | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1792 | X509 *peer_cert; | 
|  | 1793 | PyObject *result; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1794 |  | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1795 | if (!SSL_is_init_finished(self->ssl)) { | 
| Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1796 | PyErr_SetString(PyExc_ValueError, | 
|  | 1797 | "handshake not done yet"); | 
|  | 1798 | return NULL; | 
|  | 1799 | } | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1800 | peer_cert = SSL_get_peer_certificate(self->ssl); | 
|  | 1801 | if (peer_cert == NULL) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1802 | Py_RETURN_NONE; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1803 |  | 
| Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1804 | if (binary_mode) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1805 | /* return cert in DER-encoded format */ | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1806 | result = _certificate_to_der(peer_cert); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1807 | } else { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1808 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1809 | if ((verification & SSL_VERIFY_PEER) == 0) | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1810 | result = PyDict_New(); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1811 | else | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1812 | result = _decode_certificate(peer_cert); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1813 | } | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1814 | X509_free(peer_cert); | 
|  | 1815 | return result; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1816 | } | 
|  | 1817 |  | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1818 | static PyObject * | 
|  | 1819 | cipher_to_tuple(const SSL_CIPHER *cipher) | 
|  | 1820 | { | 
|  | 1821 | const char *cipher_name, *cipher_protocol; | 
|  | 1822 | PyObject *v, *retval = PyTuple_New(3); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1823 | if (retval == NULL) | 
|  | 1824 | return NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1825 |  | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1826 | cipher_name = SSL_CIPHER_get_name(cipher); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1827 | if (cipher_name == NULL) { | 
| Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1828 | Py_INCREF(Py_None); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1829 | PyTuple_SET_ITEM(retval, 0, Py_None); | 
|  | 1830 | } else { | 
|  | 1831 | v = PyUnicode_FromString(cipher_name); | 
|  | 1832 | if (v == NULL) | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1833 | goto fail; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1834 | PyTuple_SET_ITEM(retval, 0, v); | 
|  | 1835 | } | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1836 |  | 
|  | 1837 | cipher_protocol = SSL_CIPHER_get_version(cipher); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1838 | if (cipher_protocol == NULL) { | 
| Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1839 | Py_INCREF(Py_None); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1840 | PyTuple_SET_ITEM(retval, 1, Py_None); | 
|  | 1841 | } else { | 
|  | 1842 | v = PyUnicode_FromString(cipher_protocol); | 
|  | 1843 | if (v == NULL) | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1844 | goto fail; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1845 | PyTuple_SET_ITEM(retval, 1, v); | 
|  | 1846 | } | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1847 |  | 
|  | 1848 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1849 | if (v == NULL) | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1850 | goto fail; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1851 | PyTuple_SET_ITEM(retval, 2, v); | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1852 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1853 | return retval; | 
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1854 |  | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1855 | fail: | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1856 | Py_DECREF(retval); | 
|  | 1857 | return NULL; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1858 | } | 
|  | 1859 |  | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1860 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL | 
|  | 1861 | static PyObject * | 
|  | 1862 | cipher_to_dict(const SSL_CIPHER *cipher) | 
|  | 1863 | { | 
|  | 1864 | const char *cipher_name, *cipher_protocol; | 
|  | 1865 |  | 
|  | 1866 | unsigned long cipher_id; | 
|  | 1867 | int alg_bits, strength_bits, len; | 
|  | 1868 | char buf[512] = {0}; | 
|  | 1869 | #if OPENSSL_VERSION_1_1 | 
|  | 1870 | int aead, nid; | 
|  | 1871 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; | 
|  | 1872 | #endif | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1873 |  | 
|  | 1874 | /* can be NULL */ | 
|  | 1875 | cipher_name = SSL_CIPHER_get_name(cipher); | 
|  | 1876 | cipher_protocol = SSL_CIPHER_get_version(cipher); | 
|  | 1877 | cipher_id = SSL_CIPHER_get_id(cipher); | 
|  | 1878 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1879 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ | 
|  | 1880 | len = (int)strlen(buf); | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1881 | if (len > 1 && buf[len-1] == '\n') | 
|  | 1882 | buf[len-1] = '\0'; | 
|  | 1883 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); | 
|  | 1884 |  | 
|  | 1885 | #if OPENSSL_VERSION_1_1 | 
|  | 1886 | aead = SSL_CIPHER_is_aead(cipher); | 
|  | 1887 | nid = SSL_CIPHER_get_cipher_nid(cipher); | 
|  | 1888 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; | 
|  | 1889 | nid = SSL_CIPHER_get_digest_nid(cipher); | 
|  | 1890 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; | 
|  | 1891 | nid = SSL_CIPHER_get_kx_nid(cipher); | 
|  | 1892 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; | 
|  | 1893 | nid = SSL_CIPHER_get_auth_nid(cipher); | 
|  | 1894 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; | 
|  | 1895 | #endif | 
|  | 1896 |  | 
| Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1897 | return Py_BuildValue( | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1898 | "{sksssssssisi" | 
|  | 1899 | #if OPENSSL_VERSION_1_1 | 
|  | 1900 | "sOssssssss" | 
|  | 1901 | #endif | 
|  | 1902 | "}", | 
|  | 1903 | "id", cipher_id, | 
|  | 1904 | "name", cipher_name, | 
|  | 1905 | "protocol", cipher_protocol, | 
|  | 1906 | "description", buf, | 
|  | 1907 | "strength_bits", strength_bits, | 
|  | 1908 | "alg_bits", alg_bits | 
|  | 1909 | #if OPENSSL_VERSION_1_1 | 
|  | 1910 | ,"aead", aead ? Py_True : Py_False, | 
|  | 1911 | "symmetric", skcipher, | 
|  | 1912 | "digest", digest, | 
|  | 1913 | "kea", kx, | 
|  | 1914 | "auth", auth | 
|  | 1915 | #endif | 
|  | 1916 | ); | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1917 | } | 
|  | 1918 | #endif | 
|  | 1919 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1920 | /*[clinic input] | 
|  | 1921 | _ssl._SSLSocket.shared_ciphers | 
|  | 1922 | [clinic start generated code]*/ | 
|  | 1923 |  | 
|  | 1924 | static PyObject * | 
|  | 1925 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) | 
|  | 1926 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1927 | { | 
|  | 1928 | STACK_OF(SSL_CIPHER) *ciphers; | 
|  | 1929 | int i; | 
|  | 1930 | PyObject *res; | 
|  | 1931 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1932 | ciphers = SSL_get_ciphers(self->ssl); | 
|  | 1933 | if (!ciphers) | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1934 | Py_RETURN_NONE; | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1935 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); | 
|  | 1936 | if (!res) | 
|  | 1937 | return NULL; | 
|  | 1938 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { | 
|  | 1939 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); | 
|  | 1940 | if (!tup) { | 
|  | 1941 | Py_DECREF(res); | 
|  | 1942 | return NULL; | 
|  | 1943 | } | 
|  | 1944 | PyList_SET_ITEM(res, i, tup); | 
|  | 1945 | } | 
|  | 1946 | return res; | 
|  | 1947 | } | 
|  | 1948 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1949 | /*[clinic input] | 
|  | 1950 | _ssl._SSLSocket.cipher | 
|  | 1951 | [clinic start generated code]*/ | 
|  | 1952 |  | 
|  | 1953 | static PyObject * | 
|  | 1954 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) | 
|  | 1955 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ | 
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1956 | { | 
|  | 1957 | const SSL_CIPHER *current; | 
|  | 1958 |  | 
|  | 1959 | if (self->ssl == NULL) | 
|  | 1960 | Py_RETURN_NONE; | 
|  | 1961 | current = SSL_get_current_cipher(self->ssl); | 
|  | 1962 | if (current == NULL) | 
|  | 1963 | Py_RETURN_NONE; | 
|  | 1964 | return cipher_to_tuple(current); | 
|  | 1965 | } | 
|  | 1966 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1967 | /*[clinic input] | 
|  | 1968 | _ssl._SSLSocket.version | 
|  | 1969 | [clinic start generated code]*/ | 
|  | 1970 |  | 
|  | 1971 | static PyObject * | 
|  | 1972 | _ssl__SSLSocket_version_impl(PySSLSocket *self) | 
|  | 1973 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ | 
| Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1974 | { | 
|  | 1975 | const char *version; | 
|  | 1976 |  | 
|  | 1977 | if (self->ssl == NULL) | 
|  | 1978 | Py_RETURN_NONE; | 
| Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 1979 | if (!SSL_is_init_finished(self->ssl)) { | 
|  | 1980 | /* handshake not finished */ | 
|  | 1981 | Py_RETURN_NONE; | 
|  | 1982 | } | 
| Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1983 | version = SSL_get_version(self->ssl); | 
|  | 1984 | if (!strcmp(version, "unknown")) | 
|  | 1985 | Py_RETURN_NONE; | 
|  | 1986 | return PyUnicode_FromString(version); | 
|  | 1987 | } | 
|  | 1988 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 1989 | #if HAVE_NPN | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1990 | /*[clinic input] | 
|  | 1991 | _ssl._SSLSocket.selected_npn_protocol | 
|  | 1992 | [clinic start generated code]*/ | 
|  | 1993 |  | 
|  | 1994 | static PyObject * | 
|  | 1995 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) | 
|  | 1996 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ | 
|  | 1997 | { | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1998 | const unsigned char *out; | 
|  | 1999 | unsigned int outlen; | 
|  | 2000 |  | 
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2001 | SSL_get0_next_proto_negotiated(self->ssl, | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2002 | &out, &outlen); | 
|  | 2003 |  | 
|  | 2004 | if (out == NULL) | 
|  | 2005 | Py_RETURN_NONE; | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2006 | return PyUnicode_FromStringAndSize((char *)out, outlen); | 
|  | 2007 | } | 
|  | 2008 | #endif | 
|  | 2009 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2010 | #if HAVE_ALPN | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2011 | /*[clinic input] | 
|  | 2012 | _ssl._SSLSocket.selected_alpn_protocol | 
|  | 2013 | [clinic start generated code]*/ | 
|  | 2014 |  | 
|  | 2015 | static PyObject * | 
|  | 2016 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) | 
|  | 2017 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ | 
|  | 2018 | { | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2019 | const unsigned char *out; | 
|  | 2020 | unsigned int outlen; | 
|  | 2021 |  | 
|  | 2022 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); | 
|  | 2023 |  | 
|  | 2024 | if (out == NULL) | 
|  | 2025 | Py_RETURN_NONE; | 
|  | 2026 | return PyUnicode_FromStringAndSize((char *)out, outlen); | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2027 | } | 
|  | 2028 | #endif | 
|  | 2029 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2030 | /*[clinic input] | 
|  | 2031 | _ssl._SSLSocket.compression | 
|  | 2032 | [clinic start generated code]*/ | 
|  | 2033 |  | 
|  | 2034 | static PyObject * | 
|  | 2035 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) | 
|  | 2036 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ | 
|  | 2037 | { | 
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2038 | #ifdef OPENSSL_NO_COMP | 
|  | 2039 | Py_RETURN_NONE; | 
|  | 2040 | #else | 
|  | 2041 | const COMP_METHOD *comp_method; | 
|  | 2042 | const char *short_name; | 
|  | 2043 |  | 
|  | 2044 | if (self->ssl == NULL) | 
|  | 2045 | Py_RETURN_NONE; | 
|  | 2046 | comp_method = SSL_get_current_compression(self->ssl); | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2047 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) | 
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2048 | Py_RETURN_NONE; | 
| Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2049 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); | 
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2050 | if (short_name == NULL) | 
|  | 2051 | Py_RETURN_NONE; | 
|  | 2052 | return PyUnicode_DecodeFSDefault(short_name); | 
|  | 2053 | #endif | 
|  | 2054 | } | 
|  | 2055 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2056 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { | 
|  | 2057 | Py_INCREF(self->ctx); | 
|  | 2058 | return self->ctx; | 
|  | 2059 | } | 
|  | 2060 |  | 
|  | 2061 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, | 
|  | 2062 | void *closure) { | 
|  | 2063 |  | 
|  | 2064 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2065 | #if !HAVE_SNI | 
|  | 2066 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " | 
|  | 2067 | "context is not supported by your OpenSSL library"); | 
| Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2068 | return -1; | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2069 | #else | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2070 | Py_INCREF(value); | 
| Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2071 | Py_SETREF(self->ctx, (PySSLContext *)value); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2072 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2073 | #endif | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2074 | } else { | 
| Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2075 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2076 | return -1; | 
|  | 2077 | } | 
|  | 2078 |  | 
|  | 2079 | return 0; | 
|  | 2080 | } | 
|  | 2081 |  | 
|  | 2082 | PyDoc_STRVAR(PySSL_set_context_doc, | 
|  | 2083 | "_setter_context(ctx)\n\ | 
|  | 2084 | \ | 
|  | 2085 | This changes the context associated with the SSLSocket. This is typically\n\ | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2086 | used from within a callback function set by the sni_callback\n\ | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2087 | on the SSLContext to change the certificate information associated with the\n\ | 
|  | 2088 | SSLSocket before the cryptographic exchange handshake messages\n"); | 
|  | 2089 |  | 
|  | 2090 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2091 | static PyObject * | 
|  | 2092 | PySSL_get_server_side(PySSLSocket *self, void *c) | 
|  | 2093 | { | 
|  | 2094 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); | 
|  | 2095 | } | 
|  | 2096 |  | 
|  | 2097 | PyDoc_STRVAR(PySSL_get_server_side_doc, | 
|  | 2098 | "Whether this is a server-side socket."); | 
|  | 2099 |  | 
|  | 2100 | static PyObject * | 
|  | 2101 | PySSL_get_server_hostname(PySSLSocket *self, void *c) | 
|  | 2102 | { | 
|  | 2103 | if (self->server_hostname == NULL) | 
|  | 2104 | Py_RETURN_NONE; | 
|  | 2105 | Py_INCREF(self->server_hostname); | 
|  | 2106 | return self->server_hostname; | 
|  | 2107 | } | 
|  | 2108 |  | 
|  | 2109 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, | 
|  | 2110 | "The currently set server hostname (for SNI)."); | 
|  | 2111 |  | 
|  | 2112 | static PyObject * | 
|  | 2113 | PySSL_get_owner(PySSLSocket *self, void *c) | 
|  | 2114 | { | 
|  | 2115 | PyObject *owner; | 
|  | 2116 |  | 
|  | 2117 | if (self->owner == NULL) | 
|  | 2118 | Py_RETURN_NONE; | 
|  | 2119 |  | 
|  | 2120 | owner = PyWeakref_GetObject(self->owner); | 
|  | 2121 | Py_INCREF(owner); | 
|  | 2122 | return owner; | 
|  | 2123 | } | 
|  | 2124 |  | 
|  | 2125 | static int | 
|  | 2126 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) | 
|  | 2127 | { | 
| Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2128 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2129 | if (self->owner == NULL) | 
|  | 2130 | return -1; | 
|  | 2131 | return 0; | 
|  | 2132 | } | 
|  | 2133 |  | 
|  | 2134 | PyDoc_STRVAR(PySSL_get_owner_doc, | 
|  | 2135 | "The Python-level owner of this object.\ | 
|  | 2136 | Passed as \"self\" in servername callback."); | 
|  | 2137 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2138 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2139 | static void PySSL_dealloc(PySSLSocket *self) | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2140 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2141 | if (self->ssl) | 
|  | 2142 | SSL_free(self->ssl); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2143 | Py_XDECREF(self->Socket); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2144 | Py_XDECREF(self->ctx); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2145 | Py_XDECREF(self->server_hostname); | 
|  | 2146 | Py_XDECREF(self->owner); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2147 | PyObject_Del(self); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2148 | } | 
|  | 2149 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2150 | /* 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] | 2151 | The argument writing indicates the direction. | 
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2152 | Returns one of the possibilities in the timeout_state enum (above). | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2153 | */ | 
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2154 |  | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2155 | static int | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2156 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2157 | { | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2158 | int rc; | 
|  | 2159 | #ifdef HAVE_POLL | 
|  | 2160 | struct pollfd pollfd; | 
|  | 2161 | _PyTime_t ms; | 
|  | 2162 | #else | 
|  | 2163 | int nfds; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2164 | fd_set fds; | 
|  | 2165 | struct timeval tv; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2166 | #endif | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2167 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2168 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2169 | if ((s == NULL) || (timeout == 0)) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2170 | return SOCKET_IS_NONBLOCKING; | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2171 | else if (timeout < 0) { | 
|  | 2172 | if (s->sock_timeout > 0) | 
|  | 2173 | return SOCKET_HAS_TIMED_OUT; | 
|  | 2174 | else | 
|  | 2175 | return SOCKET_IS_BLOCKING; | 
|  | 2176 | } | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2177 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2178 | /* Guard against closed socket */ | 
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2179 | if (s->sock_fd == INVALID_SOCKET) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2180 | return SOCKET_HAS_BEEN_CLOSED; | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2181 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2182 | /* Prefer poll, if available, since you can poll() any fd | 
|  | 2183 | * which can't be done with select(). */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2184 | #ifdef HAVE_POLL | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2185 | pollfd.fd = s->sock_fd; | 
|  | 2186 | pollfd.events = writing ? POLLOUT : POLLIN; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2187 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2188 | /* timeout is in seconds, poll() uses milliseconds */ | 
|  | 2189 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2190 | assert(ms <= INT_MAX); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2191 |  | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2192 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 2193 | rc = poll(&pollfd, 1, (int)ms); | 
|  | 2194 | PySSL_END_ALLOW_THREADS | 
|  | 2195 | #else | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2196 | /* Guard against socket too large for select*/ | 
| Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2197 | if (!_PyIsSelectable_fd(s->sock_fd)) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2198 | return SOCKET_TOO_LARGE_FOR_SELECT; | 
| Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2199 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2200 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2201 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2202 | FD_ZERO(&fds); | 
|  | 2203 | FD_SET(s->sock_fd, &fds); | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2204 |  | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2205 | /* Wait until the socket becomes ready */ | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2206 | PySSL_BEGIN_ALLOW_THREADS | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2207 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2208 | if (writing) | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2209 | rc = select(nfds, NULL, &fds, NULL, &tv); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2210 | else | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2211 | rc = select(nfds, &fds, NULL, NULL, &tv); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2212 | PySSL_END_ALLOW_THREADS | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2213 | #endif | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2214 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2215 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise | 
|  | 2216 | (when we are able to write or when there's something to read) */ | 
|  | 2217 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; | 
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2218 | } | 
|  | 2219 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2220 | /*[clinic input] | 
|  | 2221 | _ssl._SSLSocket.write | 
|  | 2222 | b: Py_buffer | 
|  | 2223 | / | 
|  | 2224 |  | 
|  | 2225 | Writes the bytes-like object b into the SSL object. | 
|  | 2226 |  | 
|  | 2227 | Returns the number of bytes written. | 
|  | 2228 | [clinic start generated code]*/ | 
|  | 2229 |  | 
|  | 2230 | static PyObject * | 
|  | 2231 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) | 
|  | 2232 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2233 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2234 | int len; | 
|  | 2235 | int sockstate; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2236 | _PySSLError err; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2237 | int nonblocking; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2238 | PySocketSockObject *sock = GET_SOCKET(self); | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2239 | _PyTime_t timeout, deadline = 0; | 
|  | 2240 | int has_timeout; | 
| Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2241 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2242 | if (sock != NULL) { | 
|  | 2243 | if (((PyObject*)sock) == Py_None) { | 
|  | 2244 | _setSSLError("Underlying socket connection gone", | 
|  | 2245 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); | 
|  | 2246 | return NULL; | 
|  | 2247 | } | 
|  | 2248 | Py_INCREF(sock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2249 | } | 
|  | 2250 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2251 | if (b->len > INT_MAX) { | 
| Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2252 | PyErr_Format(PyExc_OverflowError, | 
|  | 2253 | "string longer than %d bytes", INT_MAX); | 
|  | 2254 | goto error; | 
|  | 2255 | } | 
|  | 2256 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2257 | if (sock != NULL) { | 
|  | 2258 | /* just in case the blocking state of the socket has been changed */ | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2259 | nonblocking = (sock->sock_timeout >= 0); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2260 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); | 
|  | 2261 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); | 
|  | 2262 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2263 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2264 | timeout = GET_SOCKET_TIMEOUT(sock); | 
|  | 2265 | has_timeout = (timeout > 0); | 
|  | 2266 | if (has_timeout) | 
|  | 2267 | deadline = _PyTime_GetMonotonicClock() + timeout; | 
|  | 2268 |  | 
|  | 2269 | sockstate = PySSL_select(sock, 1, timeout); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2270 | if (sockstate == SOCKET_HAS_TIMED_OUT) { | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2271 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2272 | "The write operation timed out"); | 
|  | 2273 | goto error; | 
|  | 2274 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { | 
|  | 2275 | PyErr_SetString(PySSLErrorObject, | 
|  | 2276 | "Underlying socket has been closed."); | 
|  | 2277 | goto error; | 
|  | 2278 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { | 
|  | 2279 | PyErr_SetString(PySSLErrorObject, | 
|  | 2280 | "Underlying socket too large for select()."); | 
|  | 2281 | goto error; | 
|  | 2282 | } | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2283 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2284 | do { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2285 | PySSL_BEGIN_ALLOW_THREADS | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2286 | len = SSL_write(self->ssl, b->buf, (int)b->len); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2287 | err = _PySSL_errno(len <= 0, self->ssl, len); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2288 | PySSL_END_ALLOW_THREADS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2289 | self->err = err; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2290 |  | 
|  | 2291 | if (PyErr_CheckSignals()) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2292 | goto error; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2293 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2294 | if (has_timeout) | 
|  | 2295 | timeout = deadline - _PyTime_GetMonotonicClock(); | 
|  | 2296 |  | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2297 | if (err.ssl == SSL_ERROR_WANT_READ) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2298 | sockstate = PySSL_select(sock, 0, timeout); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2299 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2300 | sockstate = PySSL_select(sock, 1, timeout); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2301 | } else { | 
|  | 2302 | sockstate = SOCKET_OPERATION_OK; | 
|  | 2303 | } | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2304 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2305 | if (sockstate == SOCKET_HAS_TIMED_OUT) { | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2306 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2307 | "The write operation timed out"); | 
|  | 2308 | goto error; | 
|  | 2309 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { | 
|  | 2310 | PyErr_SetString(PySSLErrorObject, | 
|  | 2311 | "Underlying socket has been closed."); | 
|  | 2312 | goto error; | 
|  | 2313 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { | 
|  | 2314 | break; | 
|  | 2315 | } | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2316 | } while (err.ssl == SSL_ERROR_WANT_READ || | 
|  | 2317 | err.ssl == SSL_ERROR_WANT_WRITE); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2318 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2319 | Py_XDECREF(sock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2320 | if (len > 0) | 
|  | 2321 | return PyLong_FromLong(len); | 
|  | 2322 | else | 
|  | 2323 | return PySSL_SetError(self, len, __FILE__, __LINE__); | 
| Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2324 |  | 
|  | 2325 | error: | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2326 | Py_XDECREF(sock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2327 | return NULL; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2328 | } | 
|  | 2329 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2330 | /*[clinic input] | 
|  | 2331 | _ssl._SSLSocket.pending | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2332 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2333 | Returns the number of already decrypted bytes available for read, pending on the connection. | 
|  | 2334 | [clinic start generated code]*/ | 
|  | 2335 |  | 
|  | 2336 | static PyObject * | 
|  | 2337 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) | 
|  | 2338 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2339 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2340 | int count = 0; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2341 | _PySSLError err; | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2342 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2343 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 2344 | count = SSL_pending(self->ssl); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2345 | err = _PySSL_errno(count < 0, self->ssl, count); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2346 | PySSL_END_ALLOW_THREADS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2347 | self->err = err; | 
|  | 2348 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2349 | if (count < 0) | 
|  | 2350 | return PySSL_SetError(self, count, __FILE__, __LINE__); | 
|  | 2351 | else | 
|  | 2352 | return PyLong_FromLong(count); | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2353 | } | 
|  | 2354 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2355 | /*[clinic input] | 
|  | 2356 | _ssl._SSLSocket.read | 
|  | 2357 | size as len: int | 
|  | 2358 | [ | 
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2359 | buffer: Py_buffer(accept={rwbuffer}) | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2360 | ] | 
|  | 2361 | / | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2362 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2363 | Read up to size bytes from the SSL socket. | 
|  | 2364 | [clinic start generated code]*/ | 
|  | 2365 |  | 
|  | 2366 | static PyObject * | 
|  | 2367 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, | 
|  | 2368 | Py_buffer *buffer) | 
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2369 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2370 | { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2371 | PyObject *dest = NULL; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2372 | char *mem; | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2373 | int count; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2374 | int sockstate; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2375 | _PySSLError err; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2376 | int nonblocking; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2377 | PySocketSockObject *sock = GET_SOCKET(self); | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2378 | _PyTime_t timeout, deadline = 0; | 
|  | 2379 | int has_timeout; | 
| Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2380 |  | 
| Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2381 | if (!group_right_1 && len < 0) { | 
|  | 2382 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); | 
|  | 2383 | return NULL; | 
|  | 2384 | } | 
|  | 2385 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2386 | if (sock != NULL) { | 
|  | 2387 | if (((PyObject*)sock) == Py_None) { | 
|  | 2388 | _setSSLError("Underlying socket connection gone", | 
|  | 2389 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); | 
|  | 2390 | return NULL; | 
|  | 2391 | } | 
|  | 2392 | Py_INCREF(sock); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2393 | } | 
|  | 2394 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2395 | if (!group_right_1) { | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2396 | dest = PyBytes_FromStringAndSize(NULL, len); | 
|  | 2397 | if (dest == NULL) | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2398 | goto error; | 
| Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2399 | if (len == 0) { | 
|  | 2400 | Py_XDECREF(sock); | 
|  | 2401 | return dest; | 
|  | 2402 | } | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2403 | mem = PyBytes_AS_STRING(dest); | 
|  | 2404 | } | 
|  | 2405 | else { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2406 | mem = buffer->buf; | 
|  | 2407 | if (len <= 0 || len > buffer->len) { | 
|  | 2408 | len = (int) buffer->len; | 
|  | 2409 | if (buffer->len != len) { | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2410 | PyErr_SetString(PyExc_OverflowError, | 
|  | 2411 | "maximum length can't fit in a C 'int'"); | 
|  | 2412 | goto error; | 
|  | 2413 | } | 
| Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2414 | if (len == 0) { | 
|  | 2415 | count = 0; | 
|  | 2416 | goto done; | 
|  | 2417 | } | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2418 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2419 | } | 
|  | 2420 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2421 | if (sock != NULL) { | 
|  | 2422 | /* just in case the blocking state of the socket has been changed */ | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2423 | nonblocking = (sock->sock_timeout >= 0); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2424 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); | 
|  | 2425 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); | 
|  | 2426 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2427 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2428 | timeout = GET_SOCKET_TIMEOUT(sock); | 
|  | 2429 | has_timeout = (timeout > 0); | 
|  | 2430 | if (has_timeout) | 
|  | 2431 | deadline = _PyTime_GetMonotonicClock() + timeout; | 
|  | 2432 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2433 | do { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2434 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 2435 | count = SSL_read(self->ssl, mem, len); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2436 | err = _PySSL_errno(count <= 0, self->ssl, count); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2437 | PySSL_END_ALLOW_THREADS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2438 | self->err = err; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2439 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2440 | if (PyErr_CheckSignals()) | 
|  | 2441 | goto error; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2442 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2443 | if (has_timeout) | 
|  | 2444 | timeout = deadline - _PyTime_GetMonotonicClock(); | 
|  | 2445 |  | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2446 | if (err.ssl == SSL_ERROR_WANT_READ) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2447 | sockstate = PySSL_select(sock, 0, timeout); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2448 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2449 | sockstate = PySSL_select(sock, 1, timeout); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2450 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2451 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2452 | { | 
|  | 2453 | count = 0; | 
|  | 2454 | goto done; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2455 | } | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2456 | else | 
|  | 2457 | sockstate = SOCKET_OPERATION_OK; | 
|  | 2458 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2459 | if (sockstate == SOCKET_HAS_TIMED_OUT) { | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2460 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2461 | "The read operation timed out"); | 
|  | 2462 | goto error; | 
|  | 2463 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { | 
|  | 2464 | break; | 
|  | 2465 | } | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2466 | } while (err.ssl == SSL_ERROR_WANT_READ || | 
|  | 2467 | err.ssl == SSL_ERROR_WANT_WRITE); | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2468 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2469 | if (count <= 0) { | 
|  | 2470 | PySSL_SetError(self, count, __FILE__, __LINE__); | 
|  | 2471 | goto error; | 
|  | 2472 | } | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2473 |  | 
|  | 2474 | done: | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2475 | Py_XDECREF(sock); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2476 | if (!group_right_1) { | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2477 | _PyBytes_Resize(&dest, count); | 
|  | 2478 | return dest; | 
|  | 2479 | } | 
|  | 2480 | else { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2481 | return PyLong_FromLong(count); | 
|  | 2482 | } | 
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2483 |  | 
|  | 2484 | error: | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2485 | Py_XDECREF(sock); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2486 | if (!group_right_1) | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2487 | Py_XDECREF(dest); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2488 | return NULL; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2489 | } | 
|  | 2490 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2491 | /*[clinic input] | 
|  | 2492 | _ssl._SSLSocket.shutdown | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2493 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2494 | Does the SSL shutdown handshake with the remote end. | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2495 | [clinic start generated code]*/ | 
|  | 2496 |  | 
|  | 2497 | static PyObject * | 
|  | 2498 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2499 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2500 | { | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2501 | _PySSLError err; | 
|  | 2502 | int sockstate, nonblocking, ret; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2503 | int zeros = 0; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2504 | PySocketSockObject *sock = GET_SOCKET(self); | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2505 | _PyTime_t timeout, deadline = 0; | 
|  | 2506 | int has_timeout; | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2507 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2508 | if (sock != NULL) { | 
|  | 2509 | /* Guard against closed socket */ | 
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2510 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2511 | _setSSLError("Underlying socket connection gone", | 
|  | 2512 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); | 
|  | 2513 | return NULL; | 
|  | 2514 | } | 
|  | 2515 | Py_INCREF(sock); | 
|  | 2516 |  | 
|  | 2517 | /* Just in case the blocking state of the socket has been changed */ | 
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2518 | nonblocking = (sock->sock_timeout >= 0); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2519 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); | 
|  | 2520 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2521 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2522 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2523 | timeout = GET_SOCKET_TIMEOUT(sock); | 
|  | 2524 | has_timeout = (timeout > 0); | 
|  | 2525 | if (has_timeout) | 
|  | 2526 | deadline = _PyTime_GetMonotonicClock() + timeout; | 
|  | 2527 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2528 | while (1) { | 
|  | 2529 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 2530 | /* Disable read-ahead so that unwrap can work correctly. | 
|  | 2531 | * Otherwise OpenSSL might read in too much data, | 
|  | 2532 | * eating clear text data that happens to be | 
|  | 2533 | * transmitted after the SSL shutdown. | 
| Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2534 | * Should be safe to call repeatedly every time this | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2535 | * function is used and the shutdown_seen_zero != 0 | 
|  | 2536 | * condition is met. | 
|  | 2537 | */ | 
|  | 2538 | if (self->shutdown_seen_zero) | 
|  | 2539 | SSL_set_read_ahead(self->ssl, 0); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2540 | ret = SSL_shutdown(self->ssl); | 
|  | 2541 | err = _PySSL_errno(ret < 0, self->ssl, ret); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2542 | PySSL_END_ALLOW_THREADS | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2543 | self->err = err; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2544 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2545 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2546 | if (ret > 0) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2547 | break; | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2548 | if (ret == 0) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2549 | /* Don't loop endlessly; instead preserve legacy | 
|  | 2550 | behaviour of trying SSL_shutdown() only twice. | 
|  | 2551 | This looks necessary for OpenSSL < 0.9.8m */ | 
|  | 2552 | if (++zeros > 1) | 
|  | 2553 | break; | 
|  | 2554 | /* Shutdown was sent, now try receiving */ | 
|  | 2555 | self->shutdown_seen_zero = 1; | 
|  | 2556 | continue; | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2557 | } | 
|  | 2558 |  | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2559 | if (has_timeout) | 
|  | 2560 | timeout = deadline - _PyTime_GetMonotonicClock(); | 
|  | 2561 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2562 | /* Possibly retry shutdown until timeout or failure */ | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2563 | if (err.ssl == SSL_ERROR_WANT_READ) | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2564 | sockstate = PySSL_select(sock, 0, timeout); | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2565 | else if (err.ssl == SSL_ERROR_WANT_WRITE) | 
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2566 | sockstate = PySSL_select(sock, 1, timeout); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2567 | else | 
|  | 2568 | break; | 
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2569 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2570 | if (sockstate == SOCKET_HAS_TIMED_OUT) { | 
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2571 | if (err.ssl == SSL_ERROR_WANT_READ) | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2572 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2573 | "The read operation timed out"); | 
|  | 2574 | else | 
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2575 | PyErr_SetString(PySocketModule.timeout_error, | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2576 | "The write operation timed out"); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2577 | goto error; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2578 | } | 
|  | 2579 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { | 
|  | 2580 | PyErr_SetString(PySSLErrorObject, | 
|  | 2581 | "Underlying socket too large for select()."); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2582 | goto error; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2583 | } | 
|  | 2584 | else if (sockstate != SOCKET_OPERATION_OK) | 
|  | 2585 | /* Retain the SSL error code */ | 
|  | 2586 | break; | 
|  | 2587 | } | 
| Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 2588 |  | 
| Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2589 | if (ret < 0) { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2590 | Py_XDECREF(sock); | 
| Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2591 | return PySSL_SetError(self, ret, __FILE__, __LINE__); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2592 | } | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2593 | if (sock) | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2594 | /* It's already INCREF'ed */ | 
|  | 2595 | return (PyObject *) sock; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2596 | else | 
|  | 2597 | Py_RETURN_NONE; | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2598 |  | 
|  | 2599 | error: | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2600 | Py_XDECREF(sock); | 
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2601 | return NULL; | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2602 | } | 
|  | 2603 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2604 | /*[clinic input] | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2605 | _ssl._SSLSocket.get_channel_binding | 
|  | 2606 | cb_type: str = "tls-unique" | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2607 |  | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2608 | Get channel binding data for current connection. | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2609 |  | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2610 | Raise ValueError if the requested `cb_type` is not supported.  Return bytes | 
|  | 2611 | of the data or None if the data is not available (e.g. before the handshake). | 
|  | 2612 | Only 'tls-unique' channel binding data from RFC 5929 is supported. | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2613 | [clinic start generated code]*/ | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2614 |  | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2615 | static PyObject * | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2616 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, | 
|  | 2617 | const char *cb_type) | 
|  | 2618 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2619 | { | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2620 | char buf[PySSL_CB_MAXLEN]; | 
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2621 | size_t len; | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2622 |  | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2623 | if (strcmp(cb_type, "tls-unique") == 0) { | 
|  | 2624 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { | 
|  | 2625 | /* if session is resumed XOR we are the client */ | 
|  | 2626 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); | 
|  | 2627 | } | 
|  | 2628 | else { | 
|  | 2629 | /* if a new session XOR we are the server */ | 
|  | 2630 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); | 
|  | 2631 | } | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2632 | } | 
|  | 2633 | else { | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2634 | PyErr_Format( | 
|  | 2635 | PyExc_ValueError, | 
|  | 2636 | "'%s' channel binding type not implemented", | 
|  | 2637 | cb_type | 
|  | 2638 | ); | 
|  | 2639 | return NULL; | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2640 | } | 
|  | 2641 |  | 
|  | 2642 | /* It cannot be negative in current OpenSSL version as of July 2011 */ | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2643 | if (len == 0) | 
|  | 2644 | Py_RETURN_NONE; | 
|  | 2645 |  | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2646 | return PyBytes_FromStringAndSize(buf, len); | 
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2647 | } | 
|  | 2648 |  | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2649 | /*[clinic input] | 
|  | 2650 | _ssl._SSLSocket.verify_client_post_handshake | 
|  | 2651 |  | 
|  | 2652 | Initiate TLS 1.3 post-handshake authentication | 
|  | 2653 | [clinic start generated code]*/ | 
|  | 2654 |  | 
|  | 2655 | static PyObject * | 
|  | 2656 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) | 
|  | 2657 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ | 
|  | 2658 | { | 
|  | 2659 | #ifdef TLS1_3_VERSION | 
|  | 2660 | int err = SSL_verify_client_post_handshake(self->ssl); | 
|  | 2661 | if (err == 0) | 
|  | 2662 | return _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 2663 | else | 
|  | 2664 | Py_RETURN_NONE; | 
|  | 2665 | #else | 
|  | 2666 | PyErr_SetString(PyExc_NotImplementedError, | 
|  | 2667 | "Post-handshake auth is not supported by your " | 
|  | 2668 | "OpenSSL version."); | 
|  | 2669 | return NULL; | 
|  | 2670 | #endif | 
|  | 2671 | } | 
|  | 2672 |  | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2673 | #ifdef OPENSSL_VERSION_1_1 | 
|  | 2674 |  | 
|  | 2675 | static SSL_SESSION* | 
|  | 2676 | _ssl_session_dup(SSL_SESSION *session) { | 
|  | 2677 | SSL_SESSION *newsession = NULL; | 
|  | 2678 | int slen; | 
|  | 2679 | unsigned char *senc = NULL, *p; | 
|  | 2680 | const unsigned char *const_p; | 
|  | 2681 |  | 
|  | 2682 | if (session == NULL) { | 
|  | 2683 | PyErr_SetString(PyExc_ValueError, "Invalid session"); | 
|  | 2684 | goto error; | 
|  | 2685 | } | 
|  | 2686 |  | 
|  | 2687 | /* get length */ | 
|  | 2688 | slen = i2d_SSL_SESSION(session, NULL); | 
|  | 2689 | if (slen == 0 || slen > 0xFF00) { | 
|  | 2690 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); | 
|  | 2691 | goto error; | 
|  | 2692 | } | 
|  | 2693 | if ((senc = PyMem_Malloc(slen)) == NULL) { | 
|  | 2694 | PyErr_NoMemory(); | 
|  | 2695 | goto error; | 
|  | 2696 | } | 
|  | 2697 | p = senc; | 
|  | 2698 | if (!i2d_SSL_SESSION(session, &p)) { | 
|  | 2699 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); | 
|  | 2700 | goto error; | 
|  | 2701 | } | 
|  | 2702 | const_p = senc; | 
|  | 2703 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); | 
|  | 2704 | if (session == NULL) { | 
|  | 2705 | goto error; | 
|  | 2706 | } | 
|  | 2707 | PyMem_Free(senc); | 
|  | 2708 | return newsession; | 
|  | 2709 | error: | 
|  | 2710 | if (senc != NULL) { | 
|  | 2711 | PyMem_Free(senc); | 
|  | 2712 | } | 
|  | 2713 | return NULL; | 
|  | 2714 | } | 
|  | 2715 | #endif | 
|  | 2716 |  | 
|  | 2717 | static PyObject * | 
|  | 2718 | PySSL_get_session(PySSLSocket *self, void *closure) { | 
|  | 2719 | /* get_session can return sessions from a server-side connection, | 
|  | 2720 | * it does not check for handshake done or client socket. */ | 
|  | 2721 | PySSLSession *pysess; | 
|  | 2722 | SSL_SESSION *session; | 
|  | 2723 |  | 
|  | 2724 | #ifdef OPENSSL_VERSION_1_1 | 
|  | 2725 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, | 
|  | 2726 | * https://github.com/openssl/openssl/issues/1550 */ | 
|  | 2727 | session = SSL_get0_session(self->ssl);  /* borrowed reference */ | 
|  | 2728 | if (session == NULL) { | 
|  | 2729 | Py_RETURN_NONE; | 
|  | 2730 | } | 
|  | 2731 | if ((session = _ssl_session_dup(session)) == NULL) { | 
|  | 2732 | return NULL; | 
|  | 2733 | } | 
|  | 2734 | #else | 
|  | 2735 | session = SSL_get1_session(self->ssl); | 
|  | 2736 | if (session == NULL) { | 
|  | 2737 | Py_RETURN_NONE; | 
|  | 2738 | } | 
|  | 2739 | #endif | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2740 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2741 | if (pysess == NULL) { | 
|  | 2742 | SSL_SESSION_free(session); | 
|  | 2743 | return NULL; | 
|  | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | assert(self->ctx); | 
|  | 2747 | pysess->ctx = self->ctx; | 
|  | 2748 | Py_INCREF(pysess->ctx); | 
|  | 2749 | pysess->session = session; | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2750 | PyObject_GC_Track(pysess); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2751 | return (PyObject *)pysess; | 
|  | 2752 | } | 
|  | 2753 |  | 
|  | 2754 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, | 
|  | 2755 | void *closure) | 
|  | 2756 | { | 
|  | 2757 | PySSLSession *pysess; | 
|  | 2758 | #ifdef OPENSSL_VERSION_1_1 | 
|  | 2759 | SSL_SESSION *session; | 
|  | 2760 | #endif | 
|  | 2761 | int result; | 
|  | 2762 |  | 
|  | 2763 | if (!PySSLSession_Check(value)) { | 
| Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2764 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2765 | return -1; | 
|  | 2766 | } | 
|  | 2767 | pysess = (PySSLSession *)value; | 
|  | 2768 |  | 
|  | 2769 | if (self->ctx->ctx != pysess->ctx->ctx) { | 
|  | 2770 | PyErr_SetString(PyExc_ValueError, | 
|  | 2771 | "Session refers to a different SSLContext."); | 
|  | 2772 | return -1; | 
|  | 2773 | } | 
|  | 2774 | if (self->socket_type != PY_SSL_CLIENT) { | 
|  | 2775 | PyErr_SetString(PyExc_ValueError, | 
|  | 2776 | "Cannot set session for server-side SSLSocket."); | 
|  | 2777 | return -1; | 
|  | 2778 | } | 
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2779 | if (SSL_is_init_finished(self->ssl)) { | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2780 | PyErr_SetString(PyExc_ValueError, | 
|  | 2781 | "Cannot set session after handshake."); | 
|  | 2782 | return -1; | 
|  | 2783 | } | 
|  | 2784 | #ifdef OPENSSL_VERSION_1_1 | 
|  | 2785 | /* duplicate session */ | 
|  | 2786 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { | 
|  | 2787 | return -1; | 
|  | 2788 | } | 
|  | 2789 | result = SSL_set_session(self->ssl, session); | 
|  | 2790 | /* free duplicate, SSL_set_session() bumps ref count */ | 
|  | 2791 | SSL_SESSION_free(session); | 
|  | 2792 | #else | 
|  | 2793 | result = SSL_set_session(self->ssl, pysess->session); | 
|  | 2794 | #endif | 
|  | 2795 | if (result == 0) { | 
|  | 2796 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 2797 | return -1; | 
|  | 2798 | } | 
|  | 2799 | return 0; | 
|  | 2800 | } | 
|  | 2801 |  | 
|  | 2802 | PyDoc_STRVAR(PySSL_set_session_doc, | 
|  | 2803 | "_setter_session(session)\n\ | 
|  | 2804 | \ | 
|  | 2805 | Get / set SSLSession."); | 
|  | 2806 |  | 
|  | 2807 | static PyObject * | 
|  | 2808 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { | 
|  | 2809 | if (SSL_session_reused(self->ssl)) { | 
|  | 2810 | Py_RETURN_TRUE; | 
|  | 2811 | } else { | 
|  | 2812 | Py_RETURN_FALSE; | 
|  | 2813 | } | 
|  | 2814 | } | 
|  | 2815 |  | 
|  | 2816 | PyDoc_STRVAR(PySSL_get_session_reused_doc, | 
|  | 2817 | "Was the client session reused during handshake?"); | 
|  | 2818 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2819 | static PyGetSetDef ssl_getsetlist[] = { | 
|  | 2820 | {"context", (getter) PySSL_get_context, | 
|  | 2821 | (setter) PySSL_set_context, PySSL_set_context_doc}, | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2822 | {"server_side", (getter) PySSL_get_server_side, NULL, | 
|  | 2823 | PySSL_get_server_side_doc}, | 
|  | 2824 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, | 
|  | 2825 | PySSL_get_server_hostname_doc}, | 
|  | 2826 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, | 
|  | 2827 | PySSL_get_owner_doc}, | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2828 | {"session", (getter) PySSL_get_session, | 
|  | 2829 | (setter) PySSL_set_session, PySSL_set_session_doc}, | 
|  | 2830 | {"session_reused", (getter) PySSL_get_session_reused, NULL, | 
|  | 2831 | PySSL_get_session_reused_doc}, | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2832 | {NULL},            /* sentinel */ | 
|  | 2833 | }; | 
|  | 2834 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2835 | static PyMethodDef PySSLMethods[] = { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2836 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF | 
|  | 2837 | _SSL__SSLSOCKET_WRITE_METHODDEF | 
|  | 2838 | _SSL__SSLSOCKET_READ_METHODDEF | 
|  | 2839 | _SSL__SSLSOCKET_PENDING_METHODDEF | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2840 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF | 
|  | 2841 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2842 | _SSL__SSLSOCKET_CIPHER_METHODDEF | 
|  | 2843 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF | 
|  | 2844 | _SSL__SSLSOCKET_VERSION_METHODDEF | 
|  | 2845 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF | 
|  | 2846 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF | 
|  | 2847 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF | 
|  | 2848 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2849 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2850 | {NULL, NULL} | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2851 | }; | 
|  | 2852 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2853 | static PyTypeObject PySSLSocket_Type = { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2854 | PyVarObject_HEAD_INIT(NULL, 0) | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2855 | "_ssl._SSLSocket",                  /*tp_name*/ | 
|  | 2856 | sizeof(PySSLSocket),                /*tp_basicsize*/ | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2857 | 0,                                  /*tp_itemsize*/ | 
|  | 2858 | /* methods */ | 
|  | 2859 | (destructor)PySSL_dealloc,          /*tp_dealloc*/ | 
|  | 2860 | 0,                                  /*tp_print*/ | 
|  | 2861 | 0,                                  /*tp_getattr*/ | 
|  | 2862 | 0,                                  /*tp_setattr*/ | 
|  | 2863 | 0,                                  /*tp_reserved*/ | 
|  | 2864 | 0,                                  /*tp_repr*/ | 
|  | 2865 | 0,                                  /*tp_as_number*/ | 
|  | 2866 | 0,                                  /*tp_as_sequence*/ | 
|  | 2867 | 0,                                  /*tp_as_mapping*/ | 
|  | 2868 | 0,                                  /*tp_hash*/ | 
|  | 2869 | 0,                                  /*tp_call*/ | 
|  | 2870 | 0,                                  /*tp_str*/ | 
|  | 2871 | 0,                                  /*tp_getattro*/ | 
|  | 2872 | 0,                                  /*tp_setattro*/ | 
|  | 2873 | 0,                                  /*tp_as_buffer*/ | 
|  | 2874 | Py_TPFLAGS_DEFAULT,                 /*tp_flags*/ | 
|  | 2875 | 0,                                  /*tp_doc*/ | 
|  | 2876 | 0,                                  /*tp_traverse*/ | 
|  | 2877 | 0,                                  /*tp_clear*/ | 
|  | 2878 | 0,                                  /*tp_richcompare*/ | 
|  | 2879 | 0,                                  /*tp_weaklistoffset*/ | 
|  | 2880 | 0,                                  /*tp_iter*/ | 
|  | 2881 | 0,                                  /*tp_iternext*/ | 
|  | 2882 | PySSLMethods,                       /*tp_methods*/ | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2883 | 0,                                  /*tp_members*/ | 
|  | 2884 | ssl_getsetlist,                     /*tp_getset*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2885 | }; | 
|  | 2886 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2887 |  | 
|  | 2888 | /* | 
|  | 2889 | * _SSLContext objects | 
|  | 2890 | */ | 
|  | 2891 |  | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2892 | static int | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2893 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2894 | { | 
|  | 2895 | int mode; | 
|  | 2896 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; | 
|  | 2897 |  | 
|  | 2898 | switch(n) { | 
|  | 2899 | case PY_SSL_CERT_NONE: | 
|  | 2900 | mode = SSL_VERIFY_NONE; | 
|  | 2901 | break; | 
|  | 2902 | case PY_SSL_CERT_OPTIONAL: | 
|  | 2903 | mode = SSL_VERIFY_PEER; | 
|  | 2904 | break; | 
|  | 2905 | case PY_SSL_CERT_REQUIRED: | 
|  | 2906 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; | 
|  | 2907 | break; | 
|  | 2908 | default: | 
|  | 2909 | PyErr_SetString(PyExc_ValueError, | 
|  | 2910 | "invalid value for verify_mode"); | 
|  | 2911 | return -1; | 
|  | 2912 | } | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2913 | #ifdef TLS1_3_VERSION | 
|  | 2914 | if (self->post_handshake_auth) | 
|  | 2915 | mode |= SSL_VERIFY_POST_HANDSHAKE; | 
|  | 2916 | #endif | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2917 | /* keep current verify cb */ | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2918 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); | 
|  | 2919 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2920 | return 0; | 
|  | 2921 | } | 
|  | 2922 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2923 | /*[clinic input] | 
|  | 2924 | @classmethod | 
|  | 2925 | _ssl._SSLContext.__new__ | 
|  | 2926 | protocol as proto_version: int | 
|  | 2927 | / | 
|  | 2928 | [clinic start generated code]*/ | 
|  | 2929 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2930 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2931 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) | 
|  | 2932 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2933 | { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2934 | PySSLContext *self; | 
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2935 | long options; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2936 | SSL_CTX *ctx = NULL; | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2937 | X509_VERIFY_PARAM *params; | 
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2938 | int result; | 
| Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2939 | #if defined(SSL_MODE_RELEASE_BUFFERS) | 
| Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 2940 | unsigned long libver; | 
| Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2941 | #endif | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2942 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2943 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 2944 | if (proto_version == PY_SSL_VERSION_TLS1) | 
|  | 2945 | ctx = SSL_CTX_new(TLSv1_method()); | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 2946 | #if HAVE_TLSv1_2 | 
|  | 2947 | else if (proto_version == PY_SSL_VERSION_TLS1_1) | 
|  | 2948 | ctx = SSL_CTX_new(TLSv1_1_method()); | 
|  | 2949 | else if (proto_version == PY_SSL_VERSION_TLS1_2) | 
|  | 2950 | ctx = SSL_CTX_new(TLSv1_2_method()); | 
|  | 2951 | #endif | 
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2952 | #ifndef OPENSSL_NO_SSL3 | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2953 | else if (proto_version == PY_SSL_VERSION_SSL3) | 
|  | 2954 | ctx = SSL_CTX_new(SSLv3_method()); | 
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2955 | #endif | 
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2956 | #ifndef OPENSSL_NO_SSL2 | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2957 | else if (proto_version == PY_SSL_VERSION_SSL2) | 
|  | 2958 | ctx = SSL_CTX_new(SSLv2_method()); | 
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2959 | #endif | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2960 | else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2961 | ctx = SSL_CTX_new(TLS_method()); | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2962 | else if (proto_version == PY_SSL_VERSION_TLS_CLIENT) | 
|  | 2963 | ctx = SSL_CTX_new(TLS_client_method()); | 
|  | 2964 | else if (proto_version == PY_SSL_VERSION_TLS_SERVER) | 
|  | 2965 | ctx = SSL_CTX_new(TLS_server_method()); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2966 | else | 
|  | 2967 | proto_version = -1; | 
|  | 2968 | PySSL_END_ALLOW_THREADS | 
|  | 2969 |  | 
|  | 2970 | if (proto_version == -1) { | 
|  | 2971 | PyErr_SetString(PyExc_ValueError, | 
|  | 2972 | "invalid protocol version"); | 
|  | 2973 | return NULL; | 
|  | 2974 | } | 
|  | 2975 | if (ctx == NULL) { | 
| Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 2976 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2977 | return NULL; | 
|  | 2978 | } | 
|  | 2979 |  | 
|  | 2980 | assert(type != NULL && type->tp_alloc != NULL); | 
|  | 2981 | self = (PySSLContext *) type->tp_alloc(type, 0); | 
|  | 2982 | if (self == NULL) { | 
|  | 2983 | SSL_CTX_free(ctx); | 
|  | 2984 | return NULL; | 
|  | 2985 | } | 
|  | 2986 | self->ctx = ctx; | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2987 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2988 | self->protocol = proto_version; | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2989 | #if HAVE_NPN | 
| Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 2990 | self->npn_protocols = NULL; | 
|  | 2991 | #endif | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2992 | #if HAVE_ALPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2993 | self->alpn_protocols = NULL; | 
|  | 2994 | #endif | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2995 | #ifndef OPENSSL_NO_TLSEXT | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2996 | self->set_sni_cb = NULL; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2997 | #endif | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 2998 | /* Don't check host name by default */ | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2999 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { | 
|  | 3000 | self->check_hostname = 1; | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3001 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3002 | Py_DECREF(self); | 
|  | 3003 | return NULL; | 
|  | 3004 | } | 
|  | 3005 | } else { | 
|  | 3006 | self->check_hostname = 0; | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3007 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3008 | Py_DECREF(self); | 
|  | 3009 | return NULL; | 
|  | 3010 | } | 
|  | 3011 | } | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3012 | /* Defaults */ | 
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3013 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; | 
|  | 3014 | if (proto_version != PY_SSL_VERSION_SSL2) | 
|  | 3015 | options |= SSL_OP_NO_SSLv2; | 
| Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3016 | if (proto_version != PY_SSL_VERSION_SSL3) | 
|  | 3017 | options |= SSL_OP_NO_SSLv3; | 
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3018 | /* Minimal security flags for server and client side context. | 
|  | 3019 | * Client sockets ignore server-side parameters. */ | 
|  | 3020 | #ifdef SSL_OP_NO_COMPRESSION | 
|  | 3021 | options |= SSL_OP_NO_COMPRESSION; | 
|  | 3022 | #endif | 
|  | 3023 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE | 
|  | 3024 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; | 
|  | 3025 | #endif | 
|  | 3026 | #ifdef SSL_OP_SINGLE_DH_USE | 
|  | 3027 | options |= SSL_OP_SINGLE_DH_USE; | 
|  | 3028 | #endif | 
|  | 3029 | #ifdef SSL_OP_SINGLE_ECDH_USE | 
|  | 3030 | options |= SSL_OP_SINGLE_ECDH_USE; | 
|  | 3031 | #endif | 
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3032 | SSL_CTX_set_options(self->ctx, options); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3033 |  | 
| Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3034 | /* A bare minimum cipher list without completely broken cipher suites. | 
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3035 | * It's far from perfect but gives users a better head start. */ | 
|  | 3036 | if (proto_version != PY_SSL_VERSION_SSL2) { | 
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3037 | #if PY_SSL_DEFAULT_CIPHERS == 2 | 
|  | 3038 | /* stick to OpenSSL's default settings */ | 
|  | 3039 | result = 1; | 
|  | 3040 | #else | 
|  | 3041 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); | 
|  | 3042 | #endif | 
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3043 | } else { | 
|  | 3044 | /* SSLv2 needs MD5 */ | 
|  | 3045 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); | 
|  | 3046 | } | 
|  | 3047 | if (result == 0) { | 
|  | 3048 | Py_DECREF(self); | 
|  | 3049 | ERR_clear_error(); | 
|  | 3050 | PyErr_SetString(PySSLErrorObject, | 
|  | 3051 | "No cipher can be selected."); | 
|  | 3052 | return NULL; | 
|  | 3053 | } | 
|  | 3054 |  | 
| Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3055 | #if defined(SSL_MODE_RELEASE_BUFFERS) | 
|  | 3056 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory | 
|  | 3057 | usage for no cost at all. However, don't do this for OpenSSL versions | 
|  | 3058 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE | 
|  | 3059 | 2014-0198. I can't find exactly which beta fixed this CVE, so be | 
|  | 3060 | conservative and assume it wasn't fixed until release. We do this check | 
|  | 3061 | at runtime to avoid problems from the dynamic linker. | 
|  | 3062 | See #25672 for more on this. */ | 
|  | 3063 | libver = SSLeay(); | 
|  | 3064 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && | 
|  | 3065 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { | 
|  | 3066 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); | 
|  | 3067 | } | 
|  | 3068 | #endif | 
|  | 3069 |  | 
|  | 3070 |  | 
| Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3071 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) | 
| Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3072 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use | 
|  | 3073 | prime256v1 by default.  This is Apache mod_ssl's initialization | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3074 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. | 
|  | 3075 | */ | 
| Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3076 | #if defined(SSL_CTX_set_ecdh_auto) | 
| Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3077 | SSL_CTX_set_ecdh_auto(self->ctx, 1); | 
|  | 3078 | #else | 
|  | 3079 | { | 
|  | 3080 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); | 
|  | 3081 | SSL_CTX_set_tmp_ecdh(self->ctx, key); | 
|  | 3082 | EC_KEY_free(key); | 
|  | 3083 | } | 
|  | 3084 | #endif | 
|  | 3085 | #endif | 
|  | 3086 |  | 
| Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3087 | #define SID_CTX "Python" | 
|  | 3088 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, | 
|  | 3089 | sizeof(SID_CTX)); | 
|  | 3090 | #undef SID_CTX | 
|  | 3091 |  | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3092 | params = SSL_CTX_get0_param(self->ctx); | 
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3093 | #ifdef X509_V_FLAG_TRUSTED_FIRST | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3094 | /* Improve trust chain building when cross-signed intermediate | 
|  | 3095 | certificates are present. See https://bugs.python.org/issue23476. */ | 
|  | 3096 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); | 
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3097 | #endif | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3098 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); | 
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3099 |  | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3100 | #ifdef TLS1_3_VERSION | 
|  | 3101 | self->post_handshake_auth = 0; | 
|  | 3102 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); | 
|  | 3103 | #endif | 
|  | 3104 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3105 | return (PyObject *)self; | 
|  | 3106 | } | 
|  | 3107 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3108 | static int | 
|  | 3109 | context_traverse(PySSLContext *self, visitproc visit, void *arg) | 
|  | 3110 | { | 
|  | 3111 | #ifndef OPENSSL_NO_TLSEXT | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3112 | Py_VISIT(self->set_sni_cb); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3113 | #endif | 
|  | 3114 | return 0; | 
|  | 3115 | } | 
|  | 3116 |  | 
|  | 3117 | static int | 
|  | 3118 | context_clear(PySSLContext *self) | 
|  | 3119 | { | 
|  | 3120 | #ifndef OPENSSL_NO_TLSEXT | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3121 | Py_CLEAR(self->set_sni_cb); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3122 | #endif | 
|  | 3123 | return 0; | 
|  | 3124 | } | 
|  | 3125 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3126 | static void | 
|  | 3127 | context_dealloc(PySSLContext *self) | 
|  | 3128 | { | 
| INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3129 | /* bpo-31095: UnTrack is needed before calling any callbacks */ | 
|  | 3130 | PyObject_GC_UnTrack(self); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3131 | context_clear(self); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3132 | SSL_CTX_free(self->ctx); | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3133 | #if HAVE_NPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3134 | PyMem_FREE(self->npn_protocols); | 
|  | 3135 | #endif | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3136 | #if HAVE_ALPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3137 | PyMem_FREE(self->alpn_protocols); | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3138 | #endif | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3139 | Py_TYPE(self)->tp_free(self); | 
|  | 3140 | } | 
|  | 3141 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3142 | /*[clinic input] | 
|  | 3143 | _ssl._SSLContext.set_ciphers | 
|  | 3144 | cipherlist: str | 
|  | 3145 | / | 
|  | 3146 | [clinic start generated code]*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3147 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3148 | static PyObject * | 
|  | 3149 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) | 
|  | 3150 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ | 
|  | 3151 | { | 
|  | 3152 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3153 | if (ret == 0) { | 
| Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3154 | /* Clearing the error queue is necessary on some OpenSSL versions, | 
|  | 3155 | otherwise the error will be reported again when another SSL call | 
|  | 3156 | is done. */ | 
|  | 3157 | ERR_clear_error(); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3158 | PyErr_SetString(PySSLErrorObject, | 
|  | 3159 | "No cipher can be selected."); | 
|  | 3160 | return NULL; | 
|  | 3161 | } | 
|  | 3162 | Py_RETURN_NONE; | 
|  | 3163 | } | 
|  | 3164 |  | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3165 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL | 
|  | 3166 | /*[clinic input] | 
|  | 3167 | _ssl._SSLContext.get_ciphers | 
|  | 3168 | [clinic start generated code]*/ | 
|  | 3169 |  | 
|  | 3170 | static PyObject * | 
|  | 3171 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) | 
|  | 3172 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ | 
|  | 3173 | { | 
|  | 3174 | SSL *ssl = NULL; | 
|  | 3175 | STACK_OF(SSL_CIPHER) *sk = NULL; | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3176 | const SSL_CIPHER *cipher; | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3177 | int i=0; | 
|  | 3178 | PyObject *result = NULL, *dct; | 
|  | 3179 |  | 
|  | 3180 | ssl = SSL_new(self->ctx); | 
|  | 3181 | if (ssl == NULL) { | 
|  | 3182 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3183 | goto exit; | 
|  | 3184 | } | 
|  | 3185 | sk = SSL_get_ciphers(ssl); | 
|  | 3186 |  | 
|  | 3187 | result = PyList_New(sk_SSL_CIPHER_num(sk)); | 
|  | 3188 | if (result == NULL) { | 
|  | 3189 | goto exit; | 
|  | 3190 | } | 
|  | 3191 |  | 
|  | 3192 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | 
|  | 3193 | cipher = sk_SSL_CIPHER_value(sk, i); | 
|  | 3194 | dct = cipher_to_dict(cipher); | 
|  | 3195 | if (dct == NULL) { | 
|  | 3196 | Py_CLEAR(result); | 
|  | 3197 | goto exit; | 
|  | 3198 | } | 
|  | 3199 | PyList_SET_ITEM(result, i, dct); | 
|  | 3200 | } | 
|  | 3201 |  | 
|  | 3202 | exit: | 
|  | 3203 | if (ssl != NULL) | 
|  | 3204 | SSL_free(ssl); | 
|  | 3205 | return result; | 
|  | 3206 |  | 
|  | 3207 | } | 
|  | 3208 | #endif | 
|  | 3209 |  | 
|  | 3210 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3211 | #if HAVE_NPN || HAVE_ALPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3212 | static int | 
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3213 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, | 
|  | 3214 | const unsigned char *server_protocols, unsigned int server_protocols_len, | 
|  | 3215 | const unsigned char *client_protocols, unsigned int client_protocols_len) | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3216 | { | 
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3217 | int ret; | 
|  | 3218 | if (client_protocols == NULL) { | 
|  | 3219 | client_protocols = (unsigned char *)""; | 
|  | 3220 | client_protocols_len = 0; | 
|  | 3221 | } | 
|  | 3222 | if (server_protocols == NULL) { | 
|  | 3223 | server_protocols = (unsigned char *)""; | 
|  | 3224 | server_protocols_len = 0; | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3225 | } | 
|  | 3226 |  | 
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3227 | ret = SSL_select_next_proto(out, outlen, | 
|  | 3228 | server_protocols, server_protocols_len, | 
|  | 3229 | client_protocols, client_protocols_len); | 
|  | 3230 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) | 
|  | 3231 | return SSL_TLSEXT_ERR_NOACK; | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3232 |  | 
|  | 3233 | return SSL_TLSEXT_ERR_OK; | 
|  | 3234 | } | 
| Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3235 | #endif | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3236 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3237 | #if HAVE_NPN | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3238 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ | 
|  | 3239 | static int | 
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3240 | _advertiseNPN_cb(SSL *s, | 
|  | 3241 | const unsigned char **data, unsigned int *len, | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3242 | void *args) | 
|  | 3243 | { | 
|  | 3244 | PySSLContext *ssl_ctx = (PySSLContext *) args; | 
|  | 3245 |  | 
|  | 3246 | if (ssl_ctx->npn_protocols == NULL) { | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3247 | *data = (unsigned char *)""; | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3248 | *len = 0; | 
|  | 3249 | } else { | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3250 | *data = ssl_ctx->npn_protocols; | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3251 | *len = ssl_ctx->npn_protocols_len; | 
|  | 3252 | } | 
|  | 3253 |  | 
|  | 3254 | return SSL_TLSEXT_ERR_OK; | 
|  | 3255 | } | 
|  | 3256 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ | 
|  | 3257 | static int | 
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3258 | _selectNPN_cb(SSL *s, | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3259 | unsigned char **out, unsigned char *outlen, | 
|  | 3260 | const unsigned char *server, unsigned int server_len, | 
|  | 3261 | void *args) | 
|  | 3262 | { | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3263 | PySSLContext *ctx = (PySSLContext *)args; | 
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3264 | return do_protocol_selection(0, out, outlen, server, server_len, | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3265 | ctx->npn_protocols, ctx->npn_protocols_len); | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3266 | } | 
|  | 3267 | #endif | 
|  | 3268 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3269 | /*[clinic input] | 
|  | 3270 | _ssl._SSLContext._set_npn_protocols | 
|  | 3271 | protos: Py_buffer | 
|  | 3272 | / | 
|  | 3273 | [clinic start generated code]*/ | 
|  | 3274 |  | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3275 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3276 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, | 
|  | 3277 | Py_buffer *protos) | 
|  | 3278 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3279 | { | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3280 | #if HAVE_NPN | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3281 | PyMem_Free(self->npn_protocols); | 
|  | 3282 | self->npn_protocols = PyMem_Malloc(protos->len); | 
|  | 3283 | if (self->npn_protocols == NULL) | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3284 | return PyErr_NoMemory(); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3285 | memcpy(self->npn_protocols, protos->buf, protos->len); | 
|  | 3286 | self->npn_protocols_len = (int) protos->len; | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3287 |  | 
|  | 3288 | /* set both server and client callbacks, because the context can | 
|  | 3289 | * be used to create both types of sockets */ | 
|  | 3290 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, | 
|  | 3291 | _advertiseNPN_cb, | 
|  | 3292 | self); | 
|  | 3293 | SSL_CTX_set_next_proto_select_cb(self->ctx, | 
|  | 3294 | _selectNPN_cb, | 
|  | 3295 | self); | 
|  | 3296 |  | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3297 | Py_RETURN_NONE; | 
|  | 3298 | #else | 
|  | 3299 | PyErr_SetString(PyExc_NotImplementedError, | 
|  | 3300 | "The NPN extension requires OpenSSL 1.0.1 or later."); | 
|  | 3301 | return NULL; | 
|  | 3302 | #endif | 
|  | 3303 | } | 
|  | 3304 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3305 | #if HAVE_ALPN | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3306 | static int | 
|  | 3307 | _selectALPN_cb(SSL *s, | 
|  | 3308 | const unsigned char **out, unsigned char *outlen, | 
|  | 3309 | const unsigned char *client_protocols, unsigned int client_protocols_len, | 
|  | 3310 | void *args) | 
|  | 3311 | { | 
|  | 3312 | PySSLContext *ctx = (PySSLContext *)args; | 
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3313 | return do_protocol_selection(1, (unsigned char **)out, outlen, | 
|  | 3314 | ctx->alpn_protocols, ctx->alpn_protocols_len, | 
|  | 3315 | client_protocols, client_protocols_len); | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3316 | } | 
|  | 3317 | #endif | 
|  | 3318 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3319 | /*[clinic input] | 
|  | 3320 | _ssl._SSLContext._set_alpn_protocols | 
|  | 3321 | protos: Py_buffer | 
|  | 3322 | / | 
|  | 3323 | [clinic start generated code]*/ | 
|  | 3324 |  | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3325 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3326 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, | 
|  | 3327 | Py_buffer *protos) | 
|  | 3328 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3329 | { | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3330 | #if HAVE_ALPN | 
| Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3331 | if ((size_t)protos->len > UINT_MAX) { | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3332 | PyErr_Format(PyExc_OverflowError, | 
|  | 3333 | "protocols longer than %d bytes", UINT_MAX); | 
|  | 3334 | return NULL; | 
|  | 3335 | } | 
|  | 3336 |  | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3337 | PyMem_FREE(self->alpn_protocols); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3338 | self->alpn_protocols = PyMem_Malloc(protos->len); | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3339 | if (!self->alpn_protocols) | 
|  | 3340 | return PyErr_NoMemory(); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3341 | memcpy(self->alpn_protocols, protos->buf, protos->len); | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3342 | self->alpn_protocols_len = (unsigned int)protos->len; | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3343 |  | 
|  | 3344 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) | 
|  | 3345 | return PyErr_NoMemory(); | 
|  | 3346 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); | 
|  | 3347 |  | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3348 | Py_RETURN_NONE; | 
|  | 3349 | #else | 
|  | 3350 | PyErr_SetString(PyExc_NotImplementedError, | 
|  | 3351 | "The ALPN extension requires OpenSSL 1.0.2 or later."); | 
|  | 3352 | return NULL; | 
|  | 3353 | #endif | 
|  | 3354 | } | 
|  | 3355 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3356 | static PyObject * | 
|  | 3357 | get_verify_mode(PySSLContext *self, void *c) | 
|  | 3358 | { | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3359 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ | 
|  | 3360 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | | 
|  | 3361 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); | 
|  | 3362 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3363 | case SSL_VERIFY_NONE: | 
|  | 3364 | return PyLong_FromLong(PY_SSL_CERT_NONE); | 
|  | 3365 | case SSL_VERIFY_PEER: | 
|  | 3366 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); | 
|  | 3367 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: | 
|  | 3368 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); | 
|  | 3369 | } | 
|  | 3370 | PyErr_SetString(PySSLErrorObject, | 
|  | 3371 | "invalid return value from SSL_CTX_get_verify_mode"); | 
|  | 3372 | return NULL; | 
|  | 3373 | } | 
|  | 3374 |  | 
|  | 3375 | static int | 
|  | 3376 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3377 | { | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3378 | int n; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3379 | if (!PyArg_Parse(arg, "i", &n)) | 
|  | 3380 | return -1; | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3381 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3382 | PyErr_SetString(PyExc_ValueError, | 
|  | 3383 | "Cannot set verify_mode to CERT_NONE when " | 
|  | 3384 | "check_hostname is enabled."); | 
|  | 3385 | return -1; | 
|  | 3386 | } | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3387 | return _set_verify_mode(self, n); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3388 | } | 
|  | 3389 |  | 
|  | 3390 | static PyObject * | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3391 | get_verify_flags(PySSLContext *self, void *c) | 
|  | 3392 | { | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3393 | X509_VERIFY_PARAM *param; | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3394 | unsigned long flags; | 
|  | 3395 |  | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3396 | param = SSL_CTX_get0_param(self->ctx); | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3397 | flags = X509_VERIFY_PARAM_get_flags(param); | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3398 | return PyLong_FromUnsignedLong(flags); | 
|  | 3399 | } | 
|  | 3400 |  | 
|  | 3401 | static int | 
|  | 3402 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3403 | { | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3404 | X509_VERIFY_PARAM *param; | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3405 | unsigned long new_flags, flags, set, clear; | 
|  | 3406 |  | 
|  | 3407 | if (!PyArg_Parse(arg, "k", &new_flags)) | 
|  | 3408 | return -1; | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3409 | param = SSL_CTX_get0_param(self->ctx); | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3410 | flags = X509_VERIFY_PARAM_get_flags(param); | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3411 | clear = flags & ~new_flags; | 
|  | 3412 | set = ~flags & new_flags; | 
|  | 3413 | if (clear) { | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3414 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3415 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3416 | return -1; | 
|  | 3417 | } | 
|  | 3418 | } | 
|  | 3419 | if (set) { | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3420 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3421 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3422 | return -1; | 
|  | 3423 | } | 
|  | 3424 | } | 
|  | 3425 | return 0; | 
|  | 3426 | } | 
|  | 3427 |  | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3428 | /* Getter and setter for protocol version */ | 
|  | 3429 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) | 
|  | 3430 |  | 
|  | 3431 |  | 
|  | 3432 | static int | 
|  | 3433 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) | 
|  | 3434 | { | 
|  | 3435 | long v; | 
|  | 3436 | int result; | 
|  | 3437 |  | 
|  | 3438 | if (!PyArg_Parse(arg, "l", &v)) | 
|  | 3439 | return -1; | 
|  | 3440 | if (v > INT_MAX) { | 
|  | 3441 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); | 
|  | 3442 | return -1; | 
|  | 3443 | } | 
|  | 3444 |  | 
|  | 3445 | switch(self->protocol) { | 
|  | 3446 | case PY_SSL_VERSION_TLS_CLIENT:  /* fall through */ | 
|  | 3447 | case PY_SSL_VERSION_TLS_SERVER:  /* fall through */ | 
|  | 3448 | case PY_SSL_VERSION_TLS: | 
|  | 3449 | break; | 
|  | 3450 | default: | 
|  | 3451 | PyErr_SetString( | 
|  | 3452 | PyExc_ValueError, | 
|  | 3453 | "The context's protocol doesn't support modification of " | 
|  | 3454 | "highest and lowest version." | 
|  | 3455 | ); | 
|  | 3456 | return -1; | 
|  | 3457 | } | 
|  | 3458 |  | 
|  | 3459 | if (what == 0) { | 
|  | 3460 | switch(v) { | 
|  | 3461 | case PY_PROTO_MINIMUM_SUPPORTED: | 
|  | 3462 | v = 0; | 
|  | 3463 | break; | 
|  | 3464 | case PY_PROTO_MAXIMUM_SUPPORTED: | 
|  | 3465 | /* Emulate max for set_min_proto_version */ | 
|  | 3466 | v = PY_PROTO_MAXIMUM_AVAILABLE; | 
|  | 3467 | break; | 
|  | 3468 | default: | 
|  | 3469 | break; | 
|  | 3470 | } | 
|  | 3471 | result = SSL_CTX_set_min_proto_version(self->ctx, v); | 
|  | 3472 | } | 
|  | 3473 | else { | 
|  | 3474 | switch(v) { | 
|  | 3475 | case PY_PROTO_MAXIMUM_SUPPORTED: | 
|  | 3476 | v = 0; | 
|  | 3477 | break; | 
|  | 3478 | case PY_PROTO_MINIMUM_SUPPORTED: | 
|  | 3479 | /* Emulate max for set_min_proto_version */ | 
|  | 3480 | v = PY_PROTO_MINIMUM_AVAILABLE; | 
|  | 3481 | break; | 
|  | 3482 | default: | 
|  | 3483 | break; | 
|  | 3484 | } | 
|  | 3485 | result = SSL_CTX_set_max_proto_version(self->ctx, v); | 
|  | 3486 | } | 
|  | 3487 | if (result == 0) { | 
|  | 3488 | PyErr_Format(PyExc_ValueError, | 
|  | 3489 | "Unsupported protocol version 0x%x", v); | 
|  | 3490 | return -1; | 
|  | 3491 | } | 
|  | 3492 | return 0; | 
|  | 3493 | } | 
|  | 3494 |  | 
|  | 3495 | static PyObject * | 
|  | 3496 | get_minimum_version(PySSLContext *self, void *c) | 
|  | 3497 | { | 
|  | 3498 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); | 
|  | 3499 | if (v == 0) { | 
|  | 3500 | v = PY_PROTO_MINIMUM_SUPPORTED; | 
|  | 3501 | } | 
|  | 3502 | return PyLong_FromLong(v); | 
|  | 3503 | } | 
|  | 3504 |  | 
|  | 3505 | static int | 
|  | 3506 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3507 | { | 
|  | 3508 | return set_min_max_proto_version(self, arg, 0); | 
|  | 3509 | } | 
|  | 3510 |  | 
|  | 3511 | static PyObject * | 
|  | 3512 | get_maximum_version(PySSLContext *self, void *c) | 
|  | 3513 | { | 
|  | 3514 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); | 
|  | 3515 | if (v == 0) { | 
|  | 3516 | v = PY_PROTO_MAXIMUM_SUPPORTED; | 
|  | 3517 | } | 
|  | 3518 | return PyLong_FromLong(v); | 
|  | 3519 | } | 
|  | 3520 |  | 
|  | 3521 | static int | 
|  | 3522 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3523 | { | 
|  | 3524 | return set_min_max_proto_version(self, arg, 1); | 
|  | 3525 | } | 
|  | 3526 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ | 
|  | 3527 |  | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3528 | static PyObject * | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3529 | get_options(PySSLContext *self, void *c) | 
|  | 3530 | { | 
|  | 3531 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); | 
|  | 3532 | } | 
|  | 3533 |  | 
|  | 3534 | static int | 
|  | 3535 | set_options(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3536 | { | 
|  | 3537 | long new_opts, opts, set, clear; | 
|  | 3538 | if (!PyArg_Parse(arg, "l", &new_opts)) | 
|  | 3539 | return -1; | 
|  | 3540 | opts = SSL_CTX_get_options(self->ctx); | 
|  | 3541 | clear = opts & ~new_opts; | 
|  | 3542 | set = ~opts & new_opts; | 
|  | 3543 | if (clear) { | 
|  | 3544 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS | 
|  | 3545 | SSL_CTX_clear_options(self->ctx, clear); | 
|  | 3546 | #else | 
|  | 3547 | PyErr_SetString(PyExc_ValueError, | 
|  | 3548 | "can't clear options before OpenSSL 0.9.8m"); | 
|  | 3549 | return -1; | 
|  | 3550 | #endif | 
|  | 3551 | } | 
|  | 3552 | if (set) | 
|  | 3553 | SSL_CTX_set_options(self->ctx, set); | 
|  | 3554 | return 0; | 
|  | 3555 | } | 
|  | 3556 |  | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3557 | static PyObject * | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3558 | get_host_flags(PySSLContext *self, void *c) | 
|  | 3559 | { | 
|  | 3560 | return PyLong_FromUnsignedLong(self->hostflags); | 
|  | 3561 | } | 
|  | 3562 |  | 
|  | 3563 | static int | 
|  | 3564 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3565 | { | 
|  | 3566 | X509_VERIFY_PARAM *param; | 
|  | 3567 | unsigned int new_flags = 0; | 
|  | 3568 |  | 
|  | 3569 | if (!PyArg_Parse(arg, "I", &new_flags)) | 
|  | 3570 | return -1; | 
|  | 3571 |  | 
|  | 3572 | param = SSL_CTX_get0_param(self->ctx); | 
|  | 3573 | self->hostflags = new_flags; | 
|  | 3574 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); | 
|  | 3575 | return 0; | 
|  | 3576 | } | 
|  | 3577 |  | 
|  | 3578 | static PyObject * | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3579 | get_check_hostname(PySSLContext *self, void *c) | 
|  | 3580 | { | 
|  | 3581 | return PyBool_FromLong(self->check_hostname); | 
|  | 3582 | } | 
|  | 3583 |  | 
|  | 3584 | static int | 
|  | 3585 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) | 
|  | 3586 | { | 
|  | 3587 | int check_hostname; | 
|  | 3588 | if (!PyArg_Parse(arg, "p", &check_hostname)) | 
|  | 3589 | return -1; | 
|  | 3590 | if (check_hostname && | 
|  | 3591 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { | 
| Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3592 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3593 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { | 
| Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3594 | return -1; | 
|  | 3595 | } | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3596 | } | 
|  | 3597 | self->check_hostname = check_hostname; | 
|  | 3598 | return 0; | 
|  | 3599 | } | 
|  | 3600 |  | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3601 | static PyObject * | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3602 | get_post_handshake_auth(PySSLContext *self, void *c) { | 
|  | 3603 | #if TLS1_3_VERSION | 
|  | 3604 | return PyBool_FromLong(self->post_handshake_auth); | 
|  | 3605 | #else | 
|  | 3606 | Py_RETURN_NONE; | 
|  | 3607 | #endif | 
|  | 3608 | } | 
|  | 3609 |  | 
|  | 3610 | #if TLS1_3_VERSION | 
|  | 3611 | static int | 
|  | 3612 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { | 
|  | 3613 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; | 
|  | 3614 | int mode = SSL_CTX_get_verify_mode(self->ctx); | 
|  | 3615 | int pha = PyObject_IsTrue(arg); | 
|  | 3616 |  | 
|  | 3617 | if (pha == -1) { | 
|  | 3618 | return -1; | 
|  | 3619 | } | 
|  | 3620 | self->post_handshake_auth = pha; | 
|  | 3621 |  | 
|  | 3622 | /* client-side socket setting, ignored by server-side */ | 
|  | 3623 | SSL_CTX_set_post_handshake_auth(self->ctx, pha); | 
|  | 3624 |  | 
|  | 3625 | /* server-side socket setting, ignored by client-side */ | 
|  | 3626 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); | 
|  | 3627 | if (pha) { | 
|  | 3628 | mode |= SSL_VERIFY_POST_HANDSHAKE; | 
|  | 3629 | } else { | 
|  | 3630 | mode ^= SSL_VERIFY_POST_HANDSHAKE; | 
|  | 3631 | } | 
|  | 3632 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); | 
|  | 3633 |  | 
|  | 3634 | return 0; | 
|  | 3635 | } | 
|  | 3636 | #endif | 
|  | 3637 |  | 
|  | 3638 | static PyObject * | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3639 | get_protocol(PySSLContext *self, void *c) { | 
|  | 3640 | return PyLong_FromLong(self->protocol); | 
|  | 3641 | } | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3642 |  | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3643 | typedef struct { | 
|  | 3644 | PyThreadState *thread_state; | 
|  | 3645 | PyObject *callable; | 
|  | 3646 | char *password; | 
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3647 | int size; | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3648 | int error; | 
|  | 3649 | } _PySSLPasswordInfo; | 
|  | 3650 |  | 
|  | 3651 | static int | 
|  | 3652 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, | 
|  | 3653 | const char *bad_type_error) | 
|  | 3654 | { | 
|  | 3655 | /* Set the password and size fields of a _PySSLPasswordInfo struct | 
|  | 3656 | from a unicode, bytes, or byte array object. | 
|  | 3657 | The password field will be dynamically allocated and must be freed | 
|  | 3658 | by the caller */ | 
|  | 3659 | PyObject *password_bytes = NULL; | 
|  | 3660 | const char *data = NULL; | 
|  | 3661 | Py_ssize_t size; | 
|  | 3662 |  | 
|  | 3663 | if (PyUnicode_Check(password)) { | 
|  | 3664 | password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL); | 
|  | 3665 | if (!password_bytes) { | 
|  | 3666 | goto error; | 
|  | 3667 | } | 
|  | 3668 | data = PyBytes_AS_STRING(password_bytes); | 
|  | 3669 | size = PyBytes_GET_SIZE(password_bytes); | 
|  | 3670 | } else if (PyBytes_Check(password)) { | 
|  | 3671 | data = PyBytes_AS_STRING(password); | 
|  | 3672 | size = PyBytes_GET_SIZE(password); | 
|  | 3673 | } else if (PyByteArray_Check(password)) { | 
|  | 3674 | data = PyByteArray_AS_STRING(password); | 
|  | 3675 | size = PyByteArray_GET_SIZE(password); | 
|  | 3676 | } else { | 
|  | 3677 | PyErr_SetString(PyExc_TypeError, bad_type_error); | 
|  | 3678 | goto error; | 
|  | 3679 | } | 
|  | 3680 |  | 
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3681 | if (size > (Py_ssize_t)INT_MAX) { | 
|  | 3682 | PyErr_Format(PyExc_ValueError, | 
|  | 3683 | "password cannot be longer than %d bytes", INT_MAX); | 
|  | 3684 | goto error; | 
|  | 3685 | } | 
|  | 3686 |  | 
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3687 | PyMem_Free(pw_info->password); | 
|  | 3688 | pw_info->password = PyMem_Malloc(size); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3689 | if (!pw_info->password) { | 
|  | 3690 | PyErr_SetString(PyExc_MemoryError, | 
|  | 3691 | "unable to allocate password buffer"); | 
|  | 3692 | goto error; | 
|  | 3693 | } | 
|  | 3694 | memcpy(pw_info->password, data, size); | 
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3695 | pw_info->size = (int)size; | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3696 |  | 
|  | 3697 | Py_XDECREF(password_bytes); | 
|  | 3698 | return 1; | 
|  | 3699 |  | 
|  | 3700 | error: | 
|  | 3701 | Py_XDECREF(password_bytes); | 
|  | 3702 | return 0; | 
|  | 3703 | } | 
|  | 3704 |  | 
|  | 3705 | static int | 
|  | 3706 | _password_callback(char *buf, int size, int rwflag, void *userdata) | 
|  | 3707 | { | 
|  | 3708 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; | 
|  | 3709 | PyObject *fn_ret = NULL; | 
|  | 3710 |  | 
|  | 3711 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); | 
|  | 3712 |  | 
|  | 3713 | if (pw_info->callable) { | 
| Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3714 | fn_ret = _PyObject_CallNoArg(pw_info->callable); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3715 | if (!fn_ret) { | 
|  | 3716 | /* TODO: It would be nice to move _ctypes_add_traceback() into the | 
|  | 3717 | core python API, so we could use it to add a frame here */ | 
|  | 3718 | goto error; | 
|  | 3719 | } | 
|  | 3720 |  | 
|  | 3721 | if (!_pwinfo_set(pw_info, fn_ret, | 
|  | 3722 | "password callback must return a string")) { | 
|  | 3723 | goto error; | 
|  | 3724 | } | 
|  | 3725 | Py_CLEAR(fn_ret); | 
|  | 3726 | } | 
|  | 3727 |  | 
|  | 3728 | if (pw_info->size > size) { | 
|  | 3729 | PyErr_Format(PyExc_ValueError, | 
|  | 3730 | "password cannot be longer than %d bytes", size); | 
|  | 3731 | goto error; | 
|  | 3732 | } | 
|  | 3733 |  | 
|  | 3734 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | 
|  | 3735 | memcpy(buf, pw_info->password, pw_info->size); | 
|  | 3736 | return pw_info->size; | 
|  | 3737 |  | 
|  | 3738 | error: | 
|  | 3739 | Py_XDECREF(fn_ret); | 
|  | 3740 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | 
|  | 3741 | pw_info->error = 1; | 
|  | 3742 | return -1; | 
|  | 3743 | } | 
|  | 3744 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3745 | /*[clinic input] | 
|  | 3746 | _ssl._SSLContext.load_cert_chain | 
|  | 3747 | certfile: object | 
|  | 3748 | keyfile: object = NULL | 
|  | 3749 | password: object = NULL | 
|  | 3750 |  | 
|  | 3751 | [clinic start generated code]*/ | 
|  | 3752 |  | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3753 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3754 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | 
|  | 3755 | PyObject *keyfile, PyObject *password) | 
|  | 3756 | /*[clinic end generated code: output=9480bc1c380e2095 input=7cf9ac673cbee6fc]*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3757 | { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3758 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3759 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); | 
|  | 3760 | 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] | 3761 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3762 | int r; | 
|  | 3763 |  | 
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3764 | errno = 0; | 
| Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3765 | ERR_clear_error(); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3766 | if (keyfile == Py_None) | 
|  | 3767 | keyfile = NULL; | 
|  | 3768 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { | 
|  | 3769 | PyErr_SetString(PyExc_TypeError, | 
|  | 3770 | "certfile should be a valid filesystem path"); | 
|  | 3771 | return NULL; | 
|  | 3772 | } | 
|  | 3773 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { | 
|  | 3774 | PyErr_SetString(PyExc_TypeError, | 
|  | 3775 | "keyfile should be a valid filesystem path"); | 
|  | 3776 | goto error; | 
|  | 3777 | } | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3778 | if (password && password != Py_None) { | 
|  | 3779 | if (PyCallable_Check(password)) { | 
|  | 3780 | pw_info.callable = password; | 
|  | 3781 | } else if (!_pwinfo_set(&pw_info, password, | 
|  | 3782 | "password should be a string or callable")) { | 
|  | 3783 | goto error; | 
|  | 3784 | } | 
|  | 3785 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); | 
|  | 3786 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); | 
|  | 3787 | } | 
|  | 3788 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3789 | r = SSL_CTX_use_certificate_chain_file(self->ctx, | 
|  | 3790 | PyBytes_AS_STRING(certfile_bytes)); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3791 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3792 | if (r != 1) { | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3793 | if (pw_info.error) { | 
|  | 3794 | ERR_clear_error(); | 
|  | 3795 | /* the password callback has already set the error information */ | 
|  | 3796 | } | 
|  | 3797 | else if (errno != 0) { | 
| Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3798 | ERR_clear_error(); | 
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3799 | PyErr_SetFromErrno(PyExc_OSError); | 
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3800 | } | 
|  | 3801 | else { | 
|  | 3802 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3803 | } | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3804 | goto error; | 
|  | 3805 | } | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3806 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | 
| Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3807 | r = SSL_CTX_use_PrivateKey_file(self->ctx, | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3808 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), | 
|  | 3809 | SSL_FILETYPE_PEM); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3810 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | 
|  | 3811 | Py_CLEAR(keyfile_bytes); | 
|  | 3812 | Py_CLEAR(certfile_bytes); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3813 | if (r != 1) { | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3814 | if (pw_info.error) { | 
|  | 3815 | ERR_clear_error(); | 
|  | 3816 | /* the password callback has already set the error information */ | 
|  | 3817 | } | 
|  | 3818 | else if (errno != 0) { | 
| Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3819 | ERR_clear_error(); | 
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3820 | PyErr_SetFromErrno(PyExc_OSError); | 
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3821 | } | 
|  | 3822 | else { | 
|  | 3823 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3824 | } | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3825 | goto error; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3826 | } | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3827 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3828 | r = SSL_CTX_check_private_key(self->ctx); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3829 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3830 | if (r != 1) { | 
|  | 3831 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3832 | goto error; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3833 | } | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3834 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); | 
|  | 3835 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); | 
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3836 | PyMem_Free(pw_info.password); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3837 | Py_RETURN_NONE; | 
|  | 3838 |  | 
|  | 3839 | error: | 
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3840 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); | 
|  | 3841 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); | 
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3842 | PyMem_Free(pw_info.password); | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3843 | Py_XDECREF(keyfile_bytes); | 
|  | 3844 | Py_XDECREF(certfile_bytes); | 
|  | 3845 | return NULL; | 
|  | 3846 | } | 
|  | 3847 |  | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3848 | /* internal helper function, returns -1 on error | 
|  | 3849 | */ | 
|  | 3850 | static int | 
|  | 3851 | _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len, | 
|  | 3852 | int filetype) | 
|  | 3853 | { | 
|  | 3854 | BIO *biobuf = NULL; | 
|  | 3855 | X509_STORE *store; | 
|  | 3856 | int retval = 0, err, loaded = 0; | 
|  | 3857 |  | 
|  | 3858 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); | 
|  | 3859 |  | 
|  | 3860 | if (len <= 0) { | 
|  | 3861 | PyErr_SetString(PyExc_ValueError, | 
|  | 3862 | "Empty certificate data"); | 
|  | 3863 | return -1; | 
|  | 3864 | } else if (len > INT_MAX) { | 
|  | 3865 | PyErr_SetString(PyExc_OverflowError, | 
|  | 3866 | "Certificate data is too long."); | 
|  | 3867 | return -1; | 
|  | 3868 | } | 
|  | 3869 |  | 
| Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 3870 | biobuf = BIO_new_mem_buf(data, (int)len); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3871 | if (biobuf == NULL) { | 
|  | 3872 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); | 
|  | 3873 | return -1; | 
|  | 3874 | } | 
|  | 3875 |  | 
|  | 3876 | store = SSL_CTX_get_cert_store(self->ctx); | 
|  | 3877 | assert(store != NULL); | 
|  | 3878 |  | 
|  | 3879 | while (1) { | 
|  | 3880 | X509 *cert = NULL; | 
|  | 3881 | int r; | 
|  | 3882 |  | 
|  | 3883 | if (filetype == SSL_FILETYPE_ASN1) { | 
|  | 3884 | cert = d2i_X509_bio(biobuf, NULL); | 
|  | 3885 | } else { | 
|  | 3886 | cert = PEM_read_bio_X509(biobuf, NULL, | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3887 | SSL_CTX_get_default_passwd_cb(self->ctx), | 
|  | 3888 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) | 
|  | 3889 | ); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3890 | } | 
|  | 3891 | if (cert == NULL) { | 
|  | 3892 | break; | 
|  | 3893 | } | 
|  | 3894 | r = X509_STORE_add_cert(store, cert); | 
|  | 3895 | X509_free(cert); | 
|  | 3896 | if (!r) { | 
|  | 3897 | err = ERR_peek_last_error(); | 
|  | 3898 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && | 
|  | 3899 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { | 
|  | 3900 | /* cert already in hash table, not an error */ | 
|  | 3901 | ERR_clear_error(); | 
|  | 3902 | } else { | 
|  | 3903 | break; | 
|  | 3904 | } | 
|  | 3905 | } | 
|  | 3906 | loaded++; | 
|  | 3907 | } | 
|  | 3908 |  | 
|  | 3909 | err = ERR_peek_last_error(); | 
|  | 3910 | if ((filetype == SSL_FILETYPE_ASN1) && | 
|  | 3911 | (loaded > 0) && | 
|  | 3912 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && | 
|  | 3913 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { | 
|  | 3914 | /* EOF ASN1 file, not an error */ | 
|  | 3915 | ERR_clear_error(); | 
|  | 3916 | retval = 0; | 
|  | 3917 | } else if ((filetype == SSL_FILETYPE_PEM) && | 
|  | 3918 | (loaded > 0) && | 
|  | 3919 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && | 
|  | 3920 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { | 
|  | 3921 | /* EOF PEM file, not an error */ | 
|  | 3922 | ERR_clear_error(); | 
|  | 3923 | retval = 0; | 
|  | 3924 | } else { | 
|  | 3925 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 3926 | retval = -1; | 
|  | 3927 | } | 
|  | 3928 |  | 
|  | 3929 | BIO_free(biobuf); | 
|  | 3930 | return retval; | 
|  | 3931 | } | 
|  | 3932 |  | 
|  | 3933 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3934 | /*[clinic input] | 
|  | 3935 | _ssl._SSLContext.load_verify_locations | 
|  | 3936 | cafile: object = NULL | 
|  | 3937 | capath: object = NULL | 
|  | 3938 | cadata: object = NULL | 
|  | 3939 |  | 
|  | 3940 | [clinic start generated code]*/ | 
|  | 3941 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3942 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3943 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, | 
|  | 3944 | PyObject *cafile, | 
|  | 3945 | PyObject *capath, | 
|  | 3946 | PyObject *cadata) | 
|  | 3947 | /*[clinic end generated code: output=454c7e41230ca551 input=997f1fb3a784ef88]*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3948 | { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3949 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; | 
|  | 3950 | const char *cafile_buf = NULL, *capath_buf = NULL; | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3951 | int r = 0, ok = 1; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3952 |  | 
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3953 | errno = 0; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3954 | if (cafile == Py_None) | 
|  | 3955 | cafile = NULL; | 
|  | 3956 | if (capath == Py_None) | 
|  | 3957 | capath = NULL; | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3958 | if (cadata == Py_None) | 
|  | 3959 | cadata = NULL; | 
|  | 3960 |  | 
|  | 3961 | if (cafile == NULL && capath == NULL && cadata == NULL) { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3962 | PyErr_SetString(PyExc_TypeError, | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3963 | "cafile, capath and cadata cannot be all omitted"); | 
|  | 3964 | goto error; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3965 | } | 
|  | 3966 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { | 
|  | 3967 | PyErr_SetString(PyExc_TypeError, | 
|  | 3968 | "cafile should be a valid filesystem path"); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3969 | goto error; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3970 | } | 
|  | 3971 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3972 | PyErr_SetString(PyExc_TypeError, | 
|  | 3973 | "capath should be a valid filesystem path"); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3974 | goto error; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3975 | } | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3976 |  | 
|  | 3977 | /* validata cadata type and load cadata */ | 
|  | 3978 | if (cadata) { | 
|  | 3979 | Py_buffer buf; | 
|  | 3980 | PyObject *cadata_ascii = NULL; | 
|  | 3981 |  | 
|  | 3982 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE) == 0) { | 
|  | 3983 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { | 
|  | 3984 | PyBuffer_Release(&buf); | 
|  | 3985 | PyErr_SetString(PyExc_TypeError, | 
|  | 3986 | "cadata should be a contiguous buffer with " | 
|  | 3987 | "a single dimension"); | 
|  | 3988 | goto error; | 
|  | 3989 | } | 
|  | 3990 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); | 
|  | 3991 | PyBuffer_Release(&buf); | 
|  | 3992 | if (r == -1) { | 
|  | 3993 | goto error; | 
|  | 3994 | } | 
|  | 3995 | } else { | 
|  | 3996 | PyErr_Clear(); | 
|  | 3997 | cadata_ascii = PyUnicode_AsASCIIString(cadata); | 
|  | 3998 | if (cadata_ascii == NULL) { | 
|  | 3999 | PyErr_SetString(PyExc_TypeError, | 
| Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 4000 | "cadata should be an ASCII string or a " | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4001 | "bytes-like object"); | 
|  | 4002 | goto error; | 
|  | 4003 | } | 
|  | 4004 | r = _add_ca_certs(self, | 
|  | 4005 | PyBytes_AS_STRING(cadata_ascii), | 
|  | 4006 | PyBytes_GET_SIZE(cadata_ascii), | 
|  | 4007 | SSL_FILETYPE_PEM); | 
|  | 4008 | Py_DECREF(cadata_ascii); | 
|  | 4009 | if (r == -1) { | 
|  | 4010 | goto error; | 
|  | 4011 | } | 
|  | 4012 | } | 
|  | 4013 | } | 
|  | 4014 |  | 
|  | 4015 | /* load cafile or capath */ | 
|  | 4016 | if (cafile || capath) { | 
|  | 4017 | if (cafile) | 
|  | 4018 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); | 
|  | 4019 | if (capath) | 
|  | 4020 | capath_buf = PyBytes_AS_STRING(capath_bytes); | 
|  | 4021 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 4022 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); | 
|  | 4023 | PySSL_END_ALLOW_THREADS | 
|  | 4024 | if (r != 1) { | 
|  | 4025 | ok = 0; | 
|  | 4026 | if (errno != 0) { | 
|  | 4027 | ERR_clear_error(); | 
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4028 | PyErr_SetFromErrno(PyExc_OSError); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4029 | } | 
|  | 4030 | else { | 
|  | 4031 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 4032 | } | 
|  | 4033 | goto error; | 
|  | 4034 | } | 
|  | 4035 | } | 
|  | 4036 | goto end; | 
|  | 4037 |  | 
|  | 4038 | error: | 
|  | 4039 | ok = 0; | 
|  | 4040 | end: | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4041 | Py_XDECREF(cafile_bytes); | 
|  | 4042 | Py_XDECREF(capath_bytes); | 
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4043 | if (ok) { | 
|  | 4044 | Py_RETURN_NONE; | 
|  | 4045 | } else { | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4046 | return NULL; | 
|  | 4047 | } | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4048 | } | 
|  | 4049 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4050 | /*[clinic input] | 
|  | 4051 | _ssl._SSLContext.load_dh_params | 
|  | 4052 | path as filepath: object | 
|  | 4053 | / | 
|  | 4054 |  | 
|  | 4055 | [clinic start generated code]*/ | 
|  | 4056 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4057 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4058 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) | 
|  | 4059 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4060 | { | 
|  | 4061 | FILE *f; | 
|  | 4062 | DH *dh; | 
|  | 4063 |  | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4064 | f = _Py_fopen_obj(filepath, "rb"); | 
| Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4065 | if (f == NULL) | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4066 | return NULL; | 
| Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4067 |  | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4068 | errno = 0; | 
|  | 4069 | PySSL_BEGIN_ALLOW_THREADS | 
|  | 4070 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); | 
| Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4071 | fclose(f); | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4072 | PySSL_END_ALLOW_THREADS | 
|  | 4073 | if (dh == NULL) { | 
|  | 4074 | if (errno != 0) { | 
|  | 4075 | ERR_clear_error(); | 
|  | 4076 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); | 
|  | 4077 | } | 
|  | 4078 | else { | 
|  | 4079 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 4080 | } | 
|  | 4081 | return NULL; | 
|  | 4082 | } | 
|  | 4083 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) | 
|  | 4084 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 4085 | DH_free(dh); | 
|  | 4086 | Py_RETURN_NONE; | 
|  | 4087 | } | 
|  | 4088 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4089 | /*[clinic input] | 
|  | 4090 | _ssl._SSLContext._wrap_socket | 
|  | 4091 | sock: object(subclass_of="PySocketModule.Sock_Type") | 
|  | 4092 | server_side: int | 
|  | 4093 | server_hostname as hostname_obj: object = None | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4094 | * | 
|  | 4095 | owner: object = None | 
|  | 4096 | session: object = None | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4097 |  | 
|  | 4098 | [clinic start generated code]*/ | 
|  | 4099 |  | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4100 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4101 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4102 | int server_side, PyObject *hostname_obj, | 
|  | 4103 | PyObject *owner, PyObject *session) | 
|  | 4104 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4105 | { | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4106 | char *hostname = NULL; | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4107 | PyObject *res; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4108 |  | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4109 | /* server_hostname is either None (or absent), or to be encoded | 
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4110 | as IDN A-label (ASCII str) without NULL bytes. */ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4111 | if (hostname_obj != Py_None) { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4112 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4113 | return NULL; | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4114 | } | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4115 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4116 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, | 
|  | 4117 | server_side, hostname, | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4118 | owner, session, | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4119 | NULL, NULL); | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4120 | if (hostname != NULL) | 
|  | 4121 | PyMem_Free(hostname); | 
|  | 4122 | return res; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4123 | } | 
|  | 4124 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4125 | /*[clinic input] | 
|  | 4126 | _ssl._SSLContext._wrap_bio | 
|  | 4127 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") | 
|  | 4128 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") | 
|  | 4129 | server_side: int | 
|  | 4130 | server_hostname as hostname_obj: object = None | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4131 | * | 
|  | 4132 | owner: object = None | 
|  | 4133 | session: object = None | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4134 |  | 
|  | 4135 | [clinic start generated code]*/ | 
|  | 4136 |  | 
| Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4137 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4138 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, | 
|  | 4139 | PySSLMemoryBIO *outgoing, int server_side, | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4140 | PyObject *hostname_obj, PyObject *owner, | 
|  | 4141 | PyObject *session) | 
|  | 4142 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=8cf22f4d586ac56a]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4143 | { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4144 | char *hostname = NULL; | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4145 | PyObject *res; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4146 |  | 
|  | 4147 | /* server_hostname is either None (or absent), or to be encoded | 
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4148 | as IDN A-label (ASCII str) without NULL bytes. */ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4149 | if (hostname_obj != Py_None) { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4150 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4151 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4152 | } | 
|  | 4153 |  | 
|  | 4154 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, | 
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4155 | owner, session, | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4156 | incoming, outgoing); | 
|  | 4157 |  | 
|  | 4158 | PyMem_Free(hostname); | 
|  | 4159 | return res; | 
|  | 4160 | } | 
|  | 4161 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4162 | /*[clinic input] | 
|  | 4163 | _ssl._SSLContext.session_stats | 
|  | 4164 | [clinic start generated code]*/ | 
|  | 4165 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4166 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4167 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) | 
|  | 4168 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ | 
| Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4169 | { | 
|  | 4170 | int r; | 
|  | 4171 | PyObject *value, *stats = PyDict_New(); | 
|  | 4172 | if (!stats) | 
|  | 4173 | return NULL; | 
|  | 4174 |  | 
|  | 4175 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ | 
|  | 4176 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ | 
|  | 4177 | if (value == NULL) \ | 
|  | 4178 | goto error; \ | 
|  | 4179 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ | 
|  | 4180 | Py_DECREF(value); \ | 
|  | 4181 | if (r < 0) \ | 
|  | 4182 | goto error; | 
|  | 4183 |  | 
|  | 4184 | ADD_STATS(number, "number"); | 
|  | 4185 | ADD_STATS(connect, "connect"); | 
|  | 4186 | ADD_STATS(connect_good, "connect_good"); | 
|  | 4187 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); | 
|  | 4188 | ADD_STATS(accept, "accept"); | 
|  | 4189 | ADD_STATS(accept_good, "accept_good"); | 
|  | 4190 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); | 
|  | 4191 | ADD_STATS(accept, "accept"); | 
|  | 4192 | ADD_STATS(hits, "hits"); | 
|  | 4193 | ADD_STATS(misses, "misses"); | 
|  | 4194 | ADD_STATS(timeouts, "timeouts"); | 
|  | 4195 | ADD_STATS(cache_full, "cache_full"); | 
|  | 4196 |  | 
|  | 4197 | #undef ADD_STATS | 
|  | 4198 |  | 
|  | 4199 | return stats; | 
|  | 4200 |  | 
|  | 4201 | error: | 
|  | 4202 | Py_DECREF(stats); | 
|  | 4203 | return NULL; | 
|  | 4204 | } | 
|  | 4205 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4206 | /*[clinic input] | 
|  | 4207 | _ssl._SSLContext.set_default_verify_paths | 
|  | 4208 | [clinic start generated code]*/ | 
|  | 4209 |  | 
| Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4210 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4211 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) | 
|  | 4212 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ | 
| Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4213 | { | 
|  | 4214 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { | 
|  | 4215 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 4216 | return NULL; | 
|  | 4217 | } | 
|  | 4218 | Py_RETURN_NONE; | 
|  | 4219 | } | 
|  | 4220 |  | 
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4221 | #ifndef OPENSSL_NO_ECDH | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4222 | /*[clinic input] | 
|  | 4223 | _ssl._SSLContext.set_ecdh_curve | 
|  | 4224 | name: object | 
|  | 4225 | / | 
|  | 4226 |  | 
|  | 4227 | [clinic start generated code]*/ | 
|  | 4228 |  | 
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4229 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4230 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) | 
|  | 4231 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ | 
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4232 | { | 
|  | 4233 | PyObject *name_bytes; | 
|  | 4234 | int nid; | 
|  | 4235 | EC_KEY *key; | 
|  | 4236 |  | 
|  | 4237 | if (!PyUnicode_FSConverter(name, &name_bytes)) | 
|  | 4238 | return NULL; | 
|  | 4239 | assert(PyBytes_Check(name_bytes)); | 
|  | 4240 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); | 
|  | 4241 | Py_DECREF(name_bytes); | 
|  | 4242 | if (nid == 0) { | 
|  | 4243 | PyErr_Format(PyExc_ValueError, | 
|  | 4244 | "unknown elliptic curve name %R", name); | 
|  | 4245 | return NULL; | 
|  | 4246 | } | 
|  | 4247 | key = EC_KEY_new_by_curve_name(nid); | 
|  | 4248 | if (key == NULL) { | 
|  | 4249 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
|  | 4250 | return NULL; | 
|  | 4251 | } | 
|  | 4252 | SSL_CTX_set_tmp_ecdh(self->ctx, key); | 
|  | 4253 | EC_KEY_free(key); | 
|  | 4254 | Py_RETURN_NONE; | 
|  | 4255 | } | 
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4256 | #endif | 
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4257 |  | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4258 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4259 | static int | 
|  | 4260 | _servername_callback(SSL *s, int *al, void *args) | 
|  | 4261 | { | 
|  | 4262 | int ret; | 
|  | 4263 | PySSLContext *ssl_ctx = (PySSLContext *) args; | 
|  | 4264 | PySSLSocket *ssl; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4265 | PyObject *result; | 
|  | 4266 | /* The high-level ssl.SSLSocket object */ | 
|  | 4267 | PyObject *ssl_socket; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4268 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); | 
| Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4269 | PyGILState_STATE gstate = PyGILState_Ensure(); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4270 |  | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4271 | if (ssl_ctx->set_sni_cb == NULL) { | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4272 | /* remove race condition in this the call back while if removing the | 
|  | 4273 | * callback is in progress */ | 
|  | 4274 | PyGILState_Release(gstate); | 
| Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4275 | return SSL_TLSEXT_ERR_OK; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4276 | } | 
|  | 4277 |  | 
|  | 4278 | ssl = SSL_get_app_data(s); | 
|  | 4279 | assert(PySSLSocket_Check(ssl)); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4280 |  | 
| Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4281 | /* The servername callback expects an argument that represents the current | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4282 | * SSL connection and that has a .context attribute that can be changed to | 
|  | 4283 | * identify the requested hostname. Since the official API is the Python | 
|  | 4284 | * level API we want to pass the callback a Python level object rather than | 
|  | 4285 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an | 
|  | 4286 | * SSLObject) that will be passed. Otherwise if there's a socket then that | 
|  | 4287 | * will be passed. If both do not exist only then the C-level object is | 
|  | 4288 | * passed. */ | 
|  | 4289 | if (ssl->owner) | 
|  | 4290 | ssl_socket = PyWeakref_GetObject(ssl->owner); | 
|  | 4291 | else if (ssl->Socket) | 
|  | 4292 | ssl_socket = PyWeakref_GetObject(ssl->Socket); | 
|  | 4293 | else | 
|  | 4294 | ssl_socket = (PyObject *) ssl; | 
|  | 4295 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4296 | Py_INCREF(ssl_socket); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4297 | if (ssl_socket == Py_None) | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4298 | goto error; | 
| Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4299 |  | 
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4300 | if (servername == NULL) { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4301 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, | 
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4302 | Py_None, ssl_ctx, NULL); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4303 | } | 
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4304 | else { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4305 | PyObject *servername_bytes; | 
|  | 4306 | PyObject *servername_str; | 
|  | 4307 |  | 
|  | 4308 | servername_bytes = PyBytes_FromString(servername); | 
|  | 4309 | if (servername_bytes == NULL) { | 
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4310 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); | 
|  | 4311 | goto error; | 
|  | 4312 | } | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4313 | /* server_hostname was encoded to an A-label by our caller; put it | 
|  | 4314 | * back into a str object, but still as an A-label (bpo-28414) | 
|  | 4315 | */ | 
|  | 4316 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); | 
|  | 4317 | Py_DECREF(servername_bytes); | 
|  | 4318 | if (servername_str == NULL) { | 
|  | 4319 | PyErr_WriteUnraisable(servername_bytes); | 
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4320 | goto error; | 
|  | 4321 | } | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4322 | result = PyObject_CallFunctionObjArgs( | 
|  | 4323 | ssl_ctx->set_sni_cb, ssl_socket, servername_str, | 
|  | 4324 | ssl_ctx, NULL); | 
|  | 4325 | Py_DECREF(servername_str); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4326 | } | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4327 | Py_DECREF(ssl_socket); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4328 |  | 
|  | 4329 | if (result == NULL) { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4330 | PyErr_WriteUnraisable(ssl_ctx->set_sni_cb); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4331 | *al = SSL_AD_HANDSHAKE_FAILURE; | 
|  | 4332 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | 
|  | 4333 | } | 
|  | 4334 | else { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4335 | /* Result may be None, a SSLContext or an integer | 
|  | 4336 | * None and SSLContext are OK, integer or other values are an error. | 
|  | 4337 | */ | 
|  | 4338 | if (result == Py_None) { | 
|  | 4339 | ret = SSL_TLSEXT_ERR_OK; | 
|  | 4340 | } else { | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4341 | *al = (int) PyLong_AsLong(result); | 
|  | 4342 | if (PyErr_Occurred()) { | 
|  | 4343 | PyErr_WriteUnraisable(result); | 
|  | 4344 | *al = SSL_AD_INTERNAL_ERROR; | 
|  | 4345 | } | 
|  | 4346 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | 
|  | 4347 | } | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4348 | Py_DECREF(result); | 
|  | 4349 | } | 
|  | 4350 |  | 
|  | 4351 | PyGILState_Release(gstate); | 
|  | 4352 | return ret; | 
|  | 4353 |  | 
|  | 4354 | error: | 
|  | 4355 | Py_DECREF(ssl_socket); | 
|  | 4356 | *al = SSL_AD_INTERNAL_ERROR; | 
|  | 4357 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | 
|  | 4358 | PyGILState_Release(gstate); | 
|  | 4359 | return ret; | 
|  | 4360 | } | 
| Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 4361 | #endif | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4362 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4363 | static PyObject * | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4364 | get_sni_callback(PySSLContext *self, void *c) | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4365 | { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4366 | PyObject *cb = self->set_sni_cb; | 
|  | 4367 | if (cb == NULL) { | 
|  | 4368 | Py_RETURN_NONE; | 
|  | 4369 | } | 
|  | 4370 | Py_INCREF(cb); | 
|  | 4371 | return cb; | 
|  | 4372 | } | 
|  | 4373 |  | 
|  | 4374 | static int | 
|  | 4375 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) | 
|  | 4376 | { | 
|  | 4377 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { | 
|  | 4378 | PyErr_SetString(PyExc_ValueError, | 
|  | 4379 | "sni_callback cannot be set on TLS_CLIENT context"); | 
|  | 4380 | return -1; | 
|  | 4381 | } | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4382 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4383 | Py_CLEAR(self->set_sni_cb); | 
|  | 4384 | if (arg == Py_None) { | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4385 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); | 
|  | 4386 | } | 
|  | 4387 | else { | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4388 | if (!PyCallable_Check(arg)) { | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4389 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); | 
|  | 4390 | PyErr_SetString(PyExc_TypeError, | 
|  | 4391 | "not a callable object"); | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4392 | return -1; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4393 | } | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4394 | Py_INCREF(arg); | 
|  | 4395 | self->set_sni_cb = arg; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4396 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); | 
|  | 4397 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); | 
|  | 4398 | } | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4399 | return 0; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4400 | #else | 
|  | 4401 | PyErr_SetString(PyExc_NotImplementedError, | 
|  | 4402 | "The TLS extension servername callback, " | 
|  | 4403 | "SSL_CTX_set_tlsext_servername_callback, " | 
|  | 4404 | "is not in the current OpenSSL library."); | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4405 | return -1; | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4406 | #endif | 
|  | 4407 | } | 
|  | 4408 |  | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4409 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, | 
|  | 4410 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ | 
|  | 4411 | \n\ | 
|  | 4412 | If the argument is None then the callback is disabled. The method is called\n\ | 
|  | 4413 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ | 
|  | 4414 | See RFC 6066 for details of the SNI extension."); | 
|  | 4415 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4416 | /*[clinic input] | 
|  | 4417 | _ssl._SSLContext.cert_store_stats | 
|  | 4418 |  | 
|  | 4419 | Returns quantities of loaded X.509 certificates. | 
|  | 4420 |  | 
|  | 4421 | X.509 certificates with a CA extension and certificate revocation lists | 
|  | 4422 | inside the context's cert store. | 
|  | 4423 |  | 
|  | 4424 | NOTE: Certificates in a capath directory aren't loaded unless they have | 
|  | 4425 | been used at least once. | 
|  | 4426 | [clinic start generated code]*/ | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4427 |  | 
|  | 4428 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4429 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) | 
|  | 4430 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4431 | { | 
|  | 4432 | X509_STORE *store; | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4433 | STACK_OF(X509_OBJECT) *objs; | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4434 | X509_OBJECT *obj; | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4435 | int x509 = 0, crl = 0, ca = 0, i; | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4436 |  | 
|  | 4437 | store = SSL_CTX_get_cert_store(self->ctx); | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4438 | objs = X509_STORE_get0_objects(store); | 
|  | 4439 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { | 
|  | 4440 | obj = sk_X509_OBJECT_value(objs, i); | 
|  | 4441 | switch (X509_OBJECT_get_type(obj)) { | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4442 | case X509_LU_X509: | 
|  | 4443 | x509++; | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4444 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4445 | ca++; | 
|  | 4446 | } | 
|  | 4447 | break; | 
|  | 4448 | case X509_LU_CRL: | 
|  | 4449 | crl++; | 
|  | 4450 | break; | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4451 | default: | 
|  | 4452 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. | 
|  | 4453 | * As far as I can tell they are internal states and never | 
|  | 4454 | * stored in a cert store */ | 
|  | 4455 | break; | 
|  | 4456 | } | 
|  | 4457 | } | 
|  | 4458 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, | 
|  | 4459 | "x509_ca", ca); | 
|  | 4460 | } | 
|  | 4461 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4462 | /*[clinic input] | 
|  | 4463 | _ssl._SSLContext.get_ca_certs | 
|  | 4464 | binary_form: bool = False | 
|  | 4465 |  | 
|  | 4466 | Returns a list of dicts with information of loaded CA certs. | 
|  | 4467 |  | 
|  | 4468 | If the optional argument is True, returns a DER-encoded copy of the CA | 
|  | 4469 | certificate. | 
|  | 4470 |  | 
|  | 4471 | NOTE: Certificates in a capath directory aren't loaded unless they have | 
|  | 4472 | been used at least once. | 
|  | 4473 | [clinic start generated code]*/ | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4474 |  | 
|  | 4475 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4476 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) | 
|  | 4477 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4478 | { | 
|  | 4479 | X509_STORE *store; | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4480 | STACK_OF(X509_OBJECT) *objs; | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4481 | PyObject *ci = NULL, *rlist = NULL; | 
|  | 4482 | int i; | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4483 |  | 
|  | 4484 | if ((rlist = PyList_New(0)) == NULL) { | 
|  | 4485 | return NULL; | 
|  | 4486 | } | 
|  | 4487 |  | 
|  | 4488 | store = SSL_CTX_get_cert_store(self->ctx); | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4489 | objs = X509_STORE_get0_objects(store); | 
|  | 4490 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4491 | X509_OBJECT *obj; | 
|  | 4492 | X509 *cert; | 
|  | 4493 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4494 | obj = sk_X509_OBJECT_value(objs, i); | 
|  | 4495 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4496 | /* not a x509 cert */ | 
|  | 4497 | continue; | 
|  | 4498 | } | 
|  | 4499 | /* CA for any purpose */ | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4500 | cert = X509_OBJECT_get0_X509(obj); | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4501 | if (!X509_check_ca(cert)) { | 
|  | 4502 | continue; | 
|  | 4503 | } | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4504 | if (binary_form) { | 
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4505 | ci = _certificate_to_der(cert); | 
|  | 4506 | } else { | 
|  | 4507 | ci = _decode_certificate(cert); | 
|  | 4508 | } | 
|  | 4509 | if (ci == NULL) { | 
|  | 4510 | goto error; | 
|  | 4511 | } | 
|  | 4512 | if (PyList_Append(rlist, ci) == -1) { | 
|  | 4513 | goto error; | 
|  | 4514 | } | 
|  | 4515 | Py_CLEAR(ci); | 
|  | 4516 | } | 
|  | 4517 | return rlist; | 
|  | 4518 |  | 
|  | 4519 | error: | 
|  | 4520 | Py_XDECREF(ci); | 
|  | 4521 | Py_XDECREF(rlist); | 
|  | 4522 | return NULL; | 
|  | 4523 | } | 
|  | 4524 |  | 
|  | 4525 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4526 | static PyGetSetDef context_getsetlist[] = { | 
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4527 | {"check_hostname", (getter) get_check_hostname, | 
|  | 4528 | (setter) set_check_hostname, NULL}, | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4529 | {"_host_flags", (getter) get_host_flags, | 
|  | 4530 | (setter) set_host_flags, NULL}, | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4531 | #if SSL_CTRL_GET_MAX_PROTO_VERSION | 
|  | 4532 | {"minimum_version", (getter) get_minimum_version, | 
|  | 4533 | (setter) set_minimum_version, NULL}, | 
|  | 4534 | {"maximum_version", (getter) get_maximum_version, | 
|  | 4535 | (setter) set_maximum_version, NULL}, | 
|  | 4536 | #endif | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4537 | {"sni_callback", (getter) get_sni_callback, | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4538 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4539 | {"options", (getter) get_options, | 
|  | 4540 | (setter) set_options, NULL}, | 
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4541 | {"post_handshake_auth", (getter) get_post_handshake_auth, | 
|  | 4542 | #ifdef TLS1_3_VERSION | 
|  | 4543 | (setter) set_post_handshake_auth, | 
|  | 4544 | #else | 
|  | 4545 | NULL, | 
|  | 4546 | #endif | 
|  | 4547 | NULL}, | 
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4548 | {"protocol", (getter) get_protocol, | 
|  | 4549 | NULL, NULL}, | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4550 | {"verify_flags", (getter) get_verify_flags, | 
|  | 4551 | (setter) set_verify_flags, NULL}, | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4552 | {"verify_mode", (getter) get_verify_mode, | 
|  | 4553 | (setter) set_verify_mode, NULL}, | 
|  | 4554 | {NULL},            /* sentinel */ | 
|  | 4555 | }; | 
|  | 4556 |  | 
|  | 4557 | static struct PyMethodDef context_methods[] = { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4558 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF | 
|  | 4559 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF | 
|  | 4560 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF | 
|  | 4561 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF | 
|  | 4562 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF | 
|  | 4563 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF | 
|  | 4564 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF | 
|  | 4565 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF | 
|  | 4566 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF | 
|  | 4567 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF | 
|  | 4568 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4569 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF | 
|  | 4570 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF | 
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4571 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4572 | {NULL, NULL}        /* sentinel */ | 
|  | 4573 | }; | 
|  | 4574 |  | 
|  | 4575 | static PyTypeObject PySSLContext_Type = { | 
|  | 4576 | PyVarObject_HEAD_INIT(NULL, 0) | 
|  | 4577 | "_ssl._SSLContext",                        /*tp_name*/ | 
|  | 4578 | sizeof(PySSLContext),                      /*tp_basicsize*/ | 
|  | 4579 | 0,                                         /*tp_itemsize*/ | 
|  | 4580 | (destructor)context_dealloc,               /*tp_dealloc*/ | 
|  | 4581 | 0,                                         /*tp_print*/ | 
|  | 4582 | 0,                                         /*tp_getattr*/ | 
|  | 4583 | 0,                                         /*tp_setattr*/ | 
|  | 4584 | 0,                                         /*tp_reserved*/ | 
|  | 4585 | 0,                                         /*tp_repr*/ | 
|  | 4586 | 0,                                         /*tp_as_number*/ | 
|  | 4587 | 0,                                         /*tp_as_sequence*/ | 
|  | 4588 | 0,                                         /*tp_as_mapping*/ | 
|  | 4589 | 0,                                         /*tp_hash*/ | 
|  | 4590 | 0,                                         /*tp_call*/ | 
|  | 4591 | 0,                                         /*tp_str*/ | 
|  | 4592 | 0,                                         /*tp_getattro*/ | 
|  | 4593 | 0,                                         /*tp_setattro*/ | 
|  | 4594 | 0,                                         /*tp_as_buffer*/ | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4595 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4596 | 0,                                         /*tp_doc*/ | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4597 | (traverseproc) context_traverse,           /*tp_traverse*/ | 
|  | 4598 | (inquiry) context_clear,                   /*tp_clear*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4599 | 0,                                         /*tp_richcompare*/ | 
|  | 4600 | 0,                                         /*tp_weaklistoffset*/ | 
|  | 4601 | 0,                                         /*tp_iter*/ | 
|  | 4602 | 0,                                         /*tp_iternext*/ | 
|  | 4603 | context_methods,                           /*tp_methods*/ | 
|  | 4604 | 0,                                         /*tp_members*/ | 
|  | 4605 | context_getsetlist,                        /*tp_getset*/ | 
|  | 4606 | 0,                                         /*tp_base*/ | 
|  | 4607 | 0,                                         /*tp_dict*/ | 
|  | 4608 | 0,                                         /*tp_descr_get*/ | 
|  | 4609 | 0,                                         /*tp_descr_set*/ | 
|  | 4610 | 0,                                         /*tp_dictoffset*/ | 
|  | 4611 | 0,                                         /*tp_init*/ | 
|  | 4612 | 0,                                         /*tp_alloc*/ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4613 | _ssl__SSLContext,                          /*tp_new*/ | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4614 | }; | 
|  | 4615 |  | 
|  | 4616 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4617 | /* | 
|  | 4618 | * MemoryBIO objects | 
|  | 4619 | */ | 
|  | 4620 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4621 | /*[clinic input] | 
|  | 4622 | @classmethod | 
|  | 4623 | _ssl.MemoryBIO.__new__ | 
|  | 4624 |  | 
|  | 4625 | [clinic start generated code]*/ | 
|  | 4626 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4627 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4628 | _ssl_MemoryBIO_impl(PyTypeObject *type) | 
|  | 4629 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4630 | { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4631 | BIO *bio; | 
|  | 4632 | PySSLMemoryBIO *self; | 
|  | 4633 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4634 | bio = BIO_new(BIO_s_mem()); | 
|  | 4635 | if (bio == NULL) { | 
|  | 4636 | PyErr_SetString(PySSLErrorObject, | 
|  | 4637 | "failed to allocate BIO"); | 
|  | 4638 | return NULL; | 
|  | 4639 | } | 
|  | 4640 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, | 
|  | 4641 | * just that no data is currently available. The SSL routines should retry | 
|  | 4642 | * the read, which we can achieve by calling BIO_set_retry_read(). */ | 
|  | 4643 | BIO_set_retry_read(bio); | 
|  | 4644 | BIO_set_mem_eof_return(bio, -1); | 
|  | 4645 |  | 
|  | 4646 | assert(type != NULL && type->tp_alloc != NULL); | 
|  | 4647 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); | 
|  | 4648 | if (self == NULL) { | 
|  | 4649 | BIO_free(bio); | 
|  | 4650 | return NULL; | 
|  | 4651 | } | 
|  | 4652 | self->bio = bio; | 
|  | 4653 | self->eof_written = 0; | 
|  | 4654 |  | 
|  | 4655 | return (PyObject *) self; | 
|  | 4656 | } | 
|  | 4657 |  | 
|  | 4658 | static void | 
|  | 4659 | memory_bio_dealloc(PySSLMemoryBIO *self) | 
|  | 4660 | { | 
|  | 4661 | BIO_free(self->bio); | 
|  | 4662 | Py_TYPE(self)->tp_free(self); | 
|  | 4663 | } | 
|  | 4664 |  | 
|  | 4665 | static PyObject * | 
|  | 4666 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) | 
|  | 4667 | { | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4668 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4669 | } | 
|  | 4670 |  | 
|  | 4671 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, | 
|  | 4672 | "The number of bytes pending in the memory BIO."); | 
|  | 4673 |  | 
|  | 4674 | static PyObject * | 
|  | 4675 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) | 
|  | 4676 | { | 
|  | 4677 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) | 
|  | 4678 | && self->eof_written); | 
|  | 4679 | } | 
|  | 4680 |  | 
|  | 4681 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, | 
|  | 4682 | "Whether the memory BIO is at EOF."); | 
|  | 4683 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4684 | /*[clinic input] | 
|  | 4685 | _ssl.MemoryBIO.read | 
|  | 4686 | size as len: int = -1 | 
|  | 4687 | / | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4688 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4689 | Read up to size bytes from the memory BIO. | 
|  | 4690 |  | 
|  | 4691 | If size is not specified, read the entire buffer. | 
|  | 4692 | If the return value is an empty bytes instance, this means either | 
|  | 4693 | EOF or that no data is available. Use the "eof" property to | 
|  | 4694 | distinguish between the two. | 
|  | 4695 | [clinic start generated code]*/ | 
|  | 4696 |  | 
|  | 4697 | static PyObject * | 
|  | 4698 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) | 
|  | 4699 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ | 
|  | 4700 | { | 
|  | 4701 | int avail, nbytes; | 
|  | 4702 | PyObject *result; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4703 |  | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4704 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4705 | if ((len < 0) || (len > avail)) | 
|  | 4706 | len = avail; | 
|  | 4707 |  | 
|  | 4708 | result = PyBytes_FromStringAndSize(NULL, len); | 
|  | 4709 | if ((result == NULL) || (len == 0)) | 
|  | 4710 | return result; | 
|  | 4711 |  | 
|  | 4712 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); | 
|  | 4713 | /* There should never be any short reads but check anyway. */ | 
|  | 4714 | if ((nbytes < len) && (_PyBytes_Resize(&result, len) < 0)) { | 
|  | 4715 | Py_DECREF(result); | 
|  | 4716 | return NULL; | 
|  | 4717 | } | 
|  | 4718 |  | 
|  | 4719 | return result; | 
|  | 4720 | } | 
|  | 4721 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4722 | /*[clinic input] | 
|  | 4723 | _ssl.MemoryBIO.write | 
|  | 4724 | b: Py_buffer | 
|  | 4725 | / | 
|  | 4726 |  | 
|  | 4727 | Writes the bytes b into the memory BIO. | 
|  | 4728 |  | 
|  | 4729 | Returns the number of bytes written. | 
|  | 4730 | [clinic start generated code]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4731 |  | 
|  | 4732 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4733 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) | 
|  | 4734 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4735 | { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4736 | int nbytes; | 
|  | 4737 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4738 | if (b->len > INT_MAX) { | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4739 | PyErr_Format(PyExc_OverflowError, | 
|  | 4740 | "string longer than %d bytes", INT_MAX); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4741 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4742 | } | 
|  | 4743 |  | 
|  | 4744 | if (self->eof_written) { | 
|  | 4745 | PyErr_SetString(PySSLErrorObject, | 
|  | 4746 | "cannot write() after write_eof()"); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4747 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4748 | } | 
|  | 4749 |  | 
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4750 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4751 | if (nbytes < 0) { | 
|  | 4752 | _setSSLError(NULL, 0, __FILE__, __LINE__); | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4753 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4754 | } | 
|  | 4755 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4756 | return PyLong_FromLong(nbytes); | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4757 | } | 
|  | 4758 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4759 | /*[clinic input] | 
|  | 4760 | _ssl.MemoryBIO.write_eof | 
|  | 4761 |  | 
|  | 4762 | Write an EOF marker to the memory BIO. | 
|  | 4763 |  | 
|  | 4764 | When all data has been read, the "eof" property will be True. | 
|  | 4765 | [clinic start generated code]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4766 |  | 
|  | 4767 | static PyObject * | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4768 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) | 
|  | 4769 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4770 | { | 
|  | 4771 | self->eof_written = 1; | 
|  | 4772 | /* After an EOF is written, a zero return from read() should be a real EOF | 
|  | 4773 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ | 
|  | 4774 | BIO_clear_retry_flags(self->bio); | 
|  | 4775 | BIO_set_mem_eof_return(self->bio, 0); | 
|  | 4776 |  | 
|  | 4777 | Py_RETURN_NONE; | 
|  | 4778 | } | 
|  | 4779 |  | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4780 | static PyGetSetDef memory_bio_getsetlist[] = { | 
|  | 4781 | {"pending", (getter) memory_bio_get_pending, NULL, | 
|  | 4782 | PySSL_memory_bio_pending_doc}, | 
|  | 4783 | {"eof", (getter) memory_bio_get_eof, NULL, | 
|  | 4784 | PySSL_memory_bio_eof_doc}, | 
|  | 4785 | {NULL},            /* sentinel */ | 
|  | 4786 | }; | 
|  | 4787 |  | 
|  | 4788 | static struct PyMethodDef memory_bio_methods[] = { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4789 | _SSL_MEMORYBIO_READ_METHODDEF | 
|  | 4790 | _SSL_MEMORYBIO_WRITE_METHODDEF | 
|  | 4791 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4792 | {NULL, NULL}        /* sentinel */ | 
|  | 4793 | }; | 
|  | 4794 |  | 
|  | 4795 | static PyTypeObject PySSLMemoryBIO_Type = { | 
|  | 4796 | PyVarObject_HEAD_INIT(NULL, 0) | 
|  | 4797 | "_ssl.MemoryBIO",                         /*tp_name*/ | 
|  | 4798 | sizeof(PySSLMemoryBIO),                    /*tp_basicsize*/ | 
|  | 4799 | 0,                                         /*tp_itemsize*/ | 
|  | 4800 | (destructor)memory_bio_dealloc,            /*tp_dealloc*/ | 
|  | 4801 | 0,                                         /*tp_print*/ | 
|  | 4802 | 0,                                         /*tp_getattr*/ | 
|  | 4803 | 0,                                         /*tp_setattr*/ | 
|  | 4804 | 0,                                         /*tp_reserved*/ | 
|  | 4805 | 0,                                         /*tp_repr*/ | 
|  | 4806 | 0,                                         /*tp_as_number*/ | 
|  | 4807 | 0,                                         /*tp_as_sequence*/ | 
|  | 4808 | 0,                                         /*tp_as_mapping*/ | 
|  | 4809 | 0,                                         /*tp_hash*/ | 
|  | 4810 | 0,                                         /*tp_call*/ | 
|  | 4811 | 0,                                         /*tp_str*/ | 
|  | 4812 | 0,                                         /*tp_getattro*/ | 
|  | 4813 | 0,                                         /*tp_setattro*/ | 
|  | 4814 | 0,                                         /*tp_as_buffer*/ | 
|  | 4815 | Py_TPFLAGS_DEFAULT,                        /*tp_flags*/ | 
|  | 4816 | 0,                                         /*tp_doc*/ | 
|  | 4817 | 0,                                         /*tp_traverse*/ | 
|  | 4818 | 0,                                         /*tp_clear*/ | 
|  | 4819 | 0,                                         /*tp_richcompare*/ | 
|  | 4820 | 0,                                         /*tp_weaklistoffset*/ | 
|  | 4821 | 0,                                         /*tp_iter*/ | 
|  | 4822 | 0,                                         /*tp_iternext*/ | 
|  | 4823 | memory_bio_methods,                        /*tp_methods*/ | 
|  | 4824 | 0,                                         /*tp_members*/ | 
|  | 4825 | memory_bio_getsetlist,                     /*tp_getset*/ | 
|  | 4826 | 0,                                         /*tp_base*/ | 
|  | 4827 | 0,                                         /*tp_dict*/ | 
|  | 4828 | 0,                                         /*tp_descr_get*/ | 
|  | 4829 | 0,                                         /*tp_descr_set*/ | 
|  | 4830 | 0,                                         /*tp_dictoffset*/ | 
|  | 4831 | 0,                                         /*tp_init*/ | 
|  | 4832 | 0,                                         /*tp_alloc*/ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4833 | _ssl_MemoryBIO,                            /*tp_new*/ | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4834 | }; | 
|  | 4835 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4836 |  | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4837 | /* | 
|  | 4838 | * SSL Session object | 
|  | 4839 | */ | 
|  | 4840 |  | 
|  | 4841 | static void | 
|  | 4842 | PySSLSession_dealloc(PySSLSession *self) | 
|  | 4843 | { | 
| INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 4844 | /* bpo-31095: UnTrack is needed before calling any callbacks */ | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4845 | PyObject_GC_UnTrack(self); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4846 | Py_XDECREF(self->ctx); | 
|  | 4847 | if (self->session != NULL) { | 
|  | 4848 | SSL_SESSION_free(self->session); | 
|  | 4849 | } | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4850 | PyObject_GC_Del(self); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4851 | } | 
|  | 4852 |  | 
|  | 4853 | static PyObject * | 
|  | 4854 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) | 
|  | 4855 | { | 
|  | 4856 | int result; | 
|  | 4857 |  | 
|  | 4858 | if (left == NULL || right == NULL) { | 
|  | 4859 | PyErr_BadInternalCall(); | 
|  | 4860 | return NULL; | 
|  | 4861 | } | 
|  | 4862 |  | 
|  | 4863 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { | 
|  | 4864 | Py_RETURN_NOTIMPLEMENTED; | 
|  | 4865 | } | 
|  | 4866 |  | 
|  | 4867 | if (left == right) { | 
|  | 4868 | result = 0; | 
|  | 4869 | } else { | 
|  | 4870 | const unsigned char *left_id, *right_id; | 
|  | 4871 | unsigned int left_len, right_len; | 
|  | 4872 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, | 
|  | 4873 | &left_len); | 
|  | 4874 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, | 
|  | 4875 | &right_len); | 
|  | 4876 | if (left_len == right_len) { | 
|  | 4877 | result = memcmp(left_id, right_id, left_len); | 
|  | 4878 | } else { | 
|  | 4879 | result = 1; | 
|  | 4880 | } | 
|  | 4881 | } | 
|  | 4882 |  | 
|  | 4883 | switch (op) { | 
|  | 4884 | case Py_EQ: | 
|  | 4885 | if (result == 0) { | 
|  | 4886 | Py_RETURN_TRUE; | 
|  | 4887 | } else { | 
|  | 4888 | Py_RETURN_FALSE; | 
|  | 4889 | } | 
|  | 4890 | break; | 
|  | 4891 | case Py_NE: | 
|  | 4892 | if (result != 0) { | 
|  | 4893 | Py_RETURN_TRUE; | 
|  | 4894 | } else { | 
|  | 4895 | Py_RETURN_FALSE; | 
|  | 4896 | } | 
|  | 4897 | break; | 
|  | 4898 | case Py_LT: | 
|  | 4899 | case Py_LE: | 
|  | 4900 | case Py_GT: | 
|  | 4901 | case Py_GE: | 
|  | 4902 | Py_RETURN_NOTIMPLEMENTED; | 
|  | 4903 | break; | 
|  | 4904 | default: | 
|  | 4905 | PyErr_BadArgument(); | 
|  | 4906 | return NULL; | 
|  | 4907 | } | 
|  | 4908 | } | 
|  | 4909 |  | 
|  | 4910 | static int | 
|  | 4911 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) | 
|  | 4912 | { | 
|  | 4913 | Py_VISIT(self->ctx); | 
|  | 4914 | return 0; | 
|  | 4915 | } | 
|  | 4916 |  | 
|  | 4917 | static int | 
|  | 4918 | PySSLSession_clear(PySSLSession *self) | 
|  | 4919 | { | 
|  | 4920 | Py_CLEAR(self->ctx); | 
|  | 4921 | return 0; | 
|  | 4922 | } | 
|  | 4923 |  | 
|  | 4924 |  | 
|  | 4925 | static PyObject * | 
|  | 4926 | PySSLSession_get_time(PySSLSession *self, void *closure) { | 
|  | 4927 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); | 
|  | 4928 | } | 
|  | 4929 |  | 
|  | 4930 | PyDoc_STRVAR(PySSLSession_get_time_doc, | 
|  | 4931 | "Session creation time (seconds since epoch)."); | 
|  | 4932 |  | 
|  | 4933 |  | 
|  | 4934 | static PyObject * | 
|  | 4935 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { | 
|  | 4936 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); | 
|  | 4937 | } | 
|  | 4938 |  | 
|  | 4939 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, | 
|  | 4940 | "Session timeout (delta in seconds)."); | 
|  | 4941 |  | 
|  | 4942 |  | 
|  | 4943 | static PyObject * | 
|  | 4944 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { | 
|  | 4945 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); | 
|  | 4946 | return PyLong_FromUnsignedLong(hint); | 
|  | 4947 | } | 
|  | 4948 |  | 
|  | 4949 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, | 
|  | 4950 | "Ticket life time hint."); | 
|  | 4951 |  | 
|  | 4952 |  | 
|  | 4953 | static PyObject * | 
|  | 4954 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { | 
|  | 4955 | const unsigned char *id; | 
|  | 4956 | unsigned int len; | 
|  | 4957 | id = SSL_SESSION_get_id(self->session, &len); | 
|  | 4958 | return PyBytes_FromStringAndSize((const char *)id, len); | 
|  | 4959 | } | 
|  | 4960 |  | 
|  | 4961 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, | 
|  | 4962 | "Session id"); | 
|  | 4963 |  | 
|  | 4964 |  | 
|  | 4965 | static PyObject * | 
|  | 4966 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { | 
|  | 4967 | if (SSL_SESSION_has_ticket(self->session)) { | 
|  | 4968 | Py_RETURN_TRUE; | 
|  | 4969 | } else { | 
|  | 4970 | Py_RETURN_FALSE; | 
|  | 4971 | } | 
|  | 4972 | } | 
|  | 4973 |  | 
|  | 4974 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, | 
|  | 4975 | "Does the session contain a ticket?"); | 
|  | 4976 |  | 
|  | 4977 |  | 
|  | 4978 | static PyGetSetDef PySSLSession_getsetlist[] = { | 
|  | 4979 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, | 
|  | 4980 | PySSLSession_get_has_ticket_doc}, | 
|  | 4981 | {"id",   (getter) PySSLSession_get_session_id, NULL, | 
|  | 4982 | PySSLSession_get_session_id_doc}, | 
|  | 4983 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, | 
|  | 4984 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, | 
|  | 4985 | {"time", (getter) PySSLSession_get_time, NULL, | 
|  | 4986 | PySSLSession_get_time_doc}, | 
|  | 4987 | {"timeout", (getter) PySSLSession_get_timeout, NULL, | 
|  | 4988 | PySSLSession_get_timeout_doc}, | 
|  | 4989 | {NULL},            /* sentinel */ | 
|  | 4990 | }; | 
|  | 4991 |  | 
|  | 4992 | static PyTypeObject PySSLSession_Type = { | 
|  | 4993 | PyVarObject_HEAD_INIT(NULL, 0) | 
|  | 4994 | "_ssl.Session",                            /*tp_name*/ | 
|  | 4995 | sizeof(PySSLSession),                      /*tp_basicsize*/ | 
|  | 4996 | 0,                                         /*tp_itemsize*/ | 
|  | 4997 | (destructor)PySSLSession_dealloc,          /*tp_dealloc*/ | 
|  | 4998 | 0,                                         /*tp_print*/ | 
|  | 4999 | 0,                                         /*tp_getattr*/ | 
|  | 5000 | 0,                                         /*tp_setattr*/ | 
|  | 5001 | 0,                                         /*tp_reserved*/ | 
|  | 5002 | 0,                                         /*tp_repr*/ | 
|  | 5003 | 0,                                         /*tp_as_number*/ | 
|  | 5004 | 0,                                         /*tp_as_sequence*/ | 
|  | 5005 | 0,                                         /*tp_as_mapping*/ | 
|  | 5006 | 0,                                         /*tp_hash*/ | 
|  | 5007 | 0,                                         /*tp_call*/ | 
|  | 5008 | 0,                                         /*tp_str*/ | 
|  | 5009 | 0,                                         /*tp_getattro*/ | 
|  | 5010 | 0,                                         /*tp_setattro*/ | 
|  | 5011 | 0,                                         /*tp_as_buffer*/ | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5012 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,   /*tp_flags*/ | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5013 | 0,                                         /*tp_doc*/ | 
|  | 5014 | (traverseproc)PySSLSession_traverse,       /*tp_traverse*/ | 
|  | 5015 | (inquiry)PySSLSession_clear,               /*tp_clear*/ | 
|  | 5016 | PySSLSession_richcompare,                  /*tp_richcompare*/ | 
|  | 5017 | 0,                                         /*tp_weaklistoffset*/ | 
|  | 5018 | 0,                                         /*tp_iter*/ | 
|  | 5019 | 0,                                         /*tp_iternext*/ | 
|  | 5020 | 0,                                         /*tp_methods*/ | 
|  | 5021 | 0,                                         /*tp_members*/ | 
|  | 5022 | PySSLSession_getsetlist,                   /*tp_getset*/ | 
|  | 5023 | }; | 
|  | 5024 |  | 
|  | 5025 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5026 | /* helper routines for seeding the SSL PRNG */ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5027 | /*[clinic input] | 
|  | 5028 | _ssl.RAND_add | 
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5029 | string as view: Py_buffer(accept={str, buffer}) | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5030 | entropy: double | 
|  | 5031 | / | 
|  | 5032 |  | 
|  | 5033 | Mix string into the OpenSSL PRNG state. | 
|  | 5034 |  | 
|  | 5035 | entropy (a float) is a lower bound on the entropy contained in | 
| Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5036 | string.  See RFC 4086. | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5037 | [clinic start generated code]*/ | 
|  | 5038 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5039 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5040 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) | 
| Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5041 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5042 | { | 
| Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5043 | const char *buf; | 
| Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5044 | Py_ssize_t len, written; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5045 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5046 | buf = (const char *)view->buf; | 
|  | 5047 | len = view->len; | 
| Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5048 | do { | 
|  | 5049 | written = Py_MIN(len, INT_MAX); | 
|  | 5050 | RAND_add(buf, (int)written, entropy); | 
|  | 5051 | buf += written; | 
|  | 5052 | len -= written; | 
|  | 5053 | } while (len); | 
| Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5054 | Py_RETURN_NONE; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5055 | } | 
|  | 5056 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5057 | static PyObject * | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5058 | PySSL_RAND(int len, int pseudo) | 
|  | 5059 | { | 
|  | 5060 | int ok; | 
|  | 5061 | PyObject *bytes; | 
|  | 5062 | unsigned long err; | 
|  | 5063 | const char *errstr; | 
|  | 5064 | PyObject *v; | 
|  | 5065 |  | 
| Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5066 | if (len < 0) { | 
|  | 5067 | PyErr_SetString(PyExc_ValueError, "num must be positive"); | 
|  | 5068 | return NULL; | 
|  | 5069 | } | 
|  | 5070 |  | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5071 | bytes = PyBytes_FromStringAndSize(NULL, len); | 
|  | 5072 | if (bytes == NULL) | 
|  | 5073 | return NULL; | 
|  | 5074 | if (pseudo) { | 
|  | 5075 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); | 
|  | 5076 | if (ok == 0 || ok == 1) | 
|  | 5077 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); | 
|  | 5078 | } | 
|  | 5079 | else { | 
|  | 5080 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); | 
|  | 5081 | if (ok == 1) | 
|  | 5082 | return bytes; | 
|  | 5083 | } | 
|  | 5084 | Py_DECREF(bytes); | 
|  | 5085 |  | 
|  | 5086 | err = ERR_get_error(); | 
|  | 5087 | errstr = ERR_reason_error_string(err); | 
|  | 5088 | v = Py_BuildValue("(ks)", err, errstr); | 
|  | 5089 | if (v != NULL) { | 
|  | 5090 | PyErr_SetObject(PySSLErrorObject, v); | 
|  | 5091 | Py_DECREF(v); | 
|  | 5092 | } | 
|  | 5093 | return NULL; | 
|  | 5094 | } | 
|  | 5095 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5096 | /*[clinic input] | 
|  | 5097 | _ssl.RAND_bytes | 
|  | 5098 | n: int | 
|  | 5099 | / | 
|  | 5100 |  | 
|  | 5101 | Generate n cryptographically strong pseudo-random bytes. | 
|  | 5102 | [clinic start generated code]*/ | 
|  | 5103 |  | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5104 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5105 | _ssl_RAND_bytes_impl(PyObject *module, int n) | 
|  | 5106 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5107 | { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5108 | return PySSL_RAND(n, 0); | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5109 | } | 
|  | 5110 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5111 | /*[clinic input] | 
|  | 5112 | _ssl.RAND_pseudo_bytes | 
|  | 5113 | n: int | 
|  | 5114 | / | 
|  | 5115 |  | 
|  | 5116 | Generate n pseudo-random bytes. | 
|  | 5117 |  | 
|  | 5118 | Return a pair (bytes, is_cryptographic).  is_cryptographic is True | 
|  | 5119 | if the bytes generated are cryptographically strong. | 
|  | 5120 | [clinic start generated code]*/ | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5121 |  | 
|  | 5122 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5123 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) | 
|  | 5124 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5125 | { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5126 | return PySSL_RAND(n, 1); | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5127 | } | 
|  | 5128 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5129 | /*[clinic input] | 
|  | 5130 | _ssl.RAND_status | 
|  | 5131 |  | 
|  | 5132 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. | 
|  | 5133 |  | 
|  | 5134 | It is necessary to seed the PRNG with RAND_add() on some platforms before | 
|  | 5135 | using the ssl() function. | 
|  | 5136 | [clinic start generated code]*/ | 
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5137 |  | 
|  | 5138 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5139 | _ssl_RAND_status_impl(PyObject *module) | 
|  | 5140 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5141 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5142 | return PyLong_FromLong(RAND_status()); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5143 | } | 
|  | 5144 |  | 
| Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5145 | #ifndef OPENSSL_NO_EGD | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5146 | /* LCOV_EXCL_START */ | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5147 | /*[clinic input] | 
|  | 5148 | _ssl.RAND_egd | 
|  | 5149 | path: object(converter="PyUnicode_FSConverter") | 
|  | 5150 | / | 
|  | 5151 |  | 
|  | 5152 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. | 
|  | 5153 |  | 
|  | 5154 | Returns number of bytes read.  Raises SSLError if connection to EGD | 
|  | 5155 | fails or if it does not provide enough data to seed PRNG. | 
|  | 5156 | [clinic start generated code]*/ | 
|  | 5157 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5158 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5159 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) | 
|  | 5160 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5161 | { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5162 | int bytes = RAND_egd(PyBytes_AsString(path)); | 
| Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5163 | Py_DECREF(path); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5164 | if (bytes == -1) { | 
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5165 | PyErr_SetString(PySSLErrorObject, | 
|  | 5166 | "EGD connection failed or EGD did not return " | 
|  | 5167 | "enough data to seed the PRNG"); | 
|  | 5168 | return NULL; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5169 | } | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5170 | return PyLong_FromLong(bytes); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5171 | } | 
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5172 | /* LCOV_EXCL_STOP */ | 
| Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5173 | #endif /* OPENSSL_NO_EGD */ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5174 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5175 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5176 |  | 
|  | 5177 | /*[clinic input] | 
|  | 5178 | _ssl.get_default_verify_paths | 
|  | 5179 |  | 
|  | 5180 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. | 
|  | 5181 |  | 
|  | 5182 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. | 
|  | 5183 | [clinic start generated code]*/ | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5184 |  | 
|  | 5185 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5186 | _ssl_get_default_verify_paths_impl(PyObject *module) | 
|  | 5187 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5188 | { | 
|  | 5189 | PyObject *ofile_env = NULL; | 
|  | 5190 | PyObject *ofile = NULL; | 
|  | 5191 | PyObject *odir_env = NULL; | 
|  | 5192 | PyObject *odir = NULL; | 
|  | 5193 |  | 
| Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5194 | #define CONVERT(info, target) { \ | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5195 | const char *tmp = (info); \ | 
|  | 5196 | target = NULL; \ | 
|  | 5197 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ | 
|  | 5198 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ | 
|  | 5199 | target = PyBytes_FromString(tmp); } \ | 
|  | 5200 | if (!target) goto error; \ | 
| Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5201 | } | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5202 |  | 
| Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5203 | CONVERT(X509_get_default_cert_file_env(), ofile_env); | 
|  | 5204 | CONVERT(X509_get_default_cert_file(), ofile); | 
|  | 5205 | CONVERT(X509_get_default_cert_dir_env(), odir_env); | 
|  | 5206 | CONVERT(X509_get_default_cert_dir(), odir); | 
|  | 5207 | #undef CONVERT | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5208 |  | 
| Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5209 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); | 
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5210 |  | 
|  | 5211 | error: | 
|  | 5212 | Py_XDECREF(ofile_env); | 
|  | 5213 | Py_XDECREF(ofile); | 
|  | 5214 | Py_XDECREF(odir_env); | 
|  | 5215 | Py_XDECREF(odir); | 
|  | 5216 | return NULL; | 
|  | 5217 | } | 
|  | 5218 |  | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5219 | static PyObject* | 
|  | 5220 | asn1obj2py(ASN1_OBJECT *obj) | 
|  | 5221 | { | 
|  | 5222 | int nid; | 
|  | 5223 | const char *ln, *sn; | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5224 |  | 
|  | 5225 | nid = OBJ_obj2nid(obj); | 
|  | 5226 | if (nid == NID_undef) { | 
|  | 5227 | PyErr_Format(PyExc_ValueError, "Unknown object"); | 
|  | 5228 | return NULL; | 
|  | 5229 | } | 
|  | 5230 | sn = OBJ_nid2sn(nid); | 
|  | 5231 | ln = OBJ_nid2ln(nid); | 
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5232 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5233 | } | 
|  | 5234 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5235 | /*[clinic input] | 
|  | 5236 | _ssl.txt2obj | 
|  | 5237 | txt: str | 
|  | 5238 | name: bool = False | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5239 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5240 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. | 
|  | 5241 |  | 
|  | 5242 | By default objects are looked up by OID. With name=True short and | 
|  | 5243 | long name are also matched. | 
|  | 5244 | [clinic start generated code]*/ | 
|  | 5245 |  | 
|  | 5246 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5247 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) | 
|  | 5248 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5249 | { | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5250 | PyObject *result = NULL; | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5251 | ASN1_OBJECT *obj; | 
|  | 5252 |  | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5253 | obj = OBJ_txt2obj(txt, name ? 0 : 1); | 
|  | 5254 | if (obj == NULL) { | 
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5255 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5256 | return NULL; | 
|  | 5257 | } | 
|  | 5258 | result = asn1obj2py(obj); | 
|  | 5259 | ASN1_OBJECT_free(obj); | 
|  | 5260 | return result; | 
|  | 5261 | } | 
|  | 5262 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5263 | /*[clinic input] | 
|  | 5264 | _ssl.nid2obj | 
|  | 5265 | nid: int | 
|  | 5266 | / | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5267 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5268 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. | 
|  | 5269 | [clinic start generated code]*/ | 
|  | 5270 |  | 
|  | 5271 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5272 | _ssl_nid2obj_impl(PyObject *module, int nid) | 
|  | 5273 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5274 | { | 
|  | 5275 | PyObject *result = NULL; | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5276 | ASN1_OBJECT *obj; | 
|  | 5277 |  | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5278 | if (nid < NID_undef) { | 
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5279 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5280 | return NULL; | 
|  | 5281 | } | 
|  | 5282 | obj = OBJ_nid2obj(nid); | 
|  | 5283 | if (obj == NULL) { | 
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5284 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); | 
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5285 | return NULL; | 
|  | 5286 | } | 
|  | 5287 | result = asn1obj2py(obj); | 
|  | 5288 | ASN1_OBJECT_free(obj); | 
|  | 5289 | return result; | 
|  | 5290 | } | 
|  | 5291 |  | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5292 | #ifdef _MSC_VER | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5293 |  | 
|  | 5294 | static PyObject* | 
|  | 5295 | certEncodingType(DWORD encodingType) | 
|  | 5296 | { | 
|  | 5297 | static PyObject *x509_asn = NULL; | 
|  | 5298 | static PyObject *pkcs_7_asn = NULL; | 
|  | 5299 |  | 
|  | 5300 | if (x509_asn == NULL) { | 
|  | 5301 | x509_asn = PyUnicode_InternFromString("x509_asn"); | 
|  | 5302 | if (x509_asn == NULL) | 
|  | 5303 | return NULL; | 
|  | 5304 | } | 
|  | 5305 | if (pkcs_7_asn == NULL) { | 
|  | 5306 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); | 
|  | 5307 | if (pkcs_7_asn == NULL) | 
|  | 5308 | return NULL; | 
|  | 5309 | } | 
|  | 5310 | switch(encodingType) { | 
|  | 5311 | case X509_ASN_ENCODING: | 
|  | 5312 | Py_INCREF(x509_asn); | 
|  | 5313 | return x509_asn; | 
|  | 5314 | case PKCS_7_ASN_ENCODING: | 
|  | 5315 | Py_INCREF(pkcs_7_asn); | 
|  | 5316 | return pkcs_7_asn; | 
|  | 5317 | default: | 
|  | 5318 | return PyLong_FromLong(encodingType); | 
|  | 5319 | } | 
|  | 5320 | } | 
|  | 5321 |  | 
|  | 5322 | static PyObject* | 
|  | 5323 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) | 
|  | 5324 | { | 
|  | 5325 | CERT_ENHKEY_USAGE *usage; | 
|  | 5326 | DWORD size, error, i; | 
|  | 5327 | PyObject *retval; | 
|  | 5328 |  | 
|  | 5329 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { | 
|  | 5330 | error = GetLastError(); | 
|  | 5331 | if (error == CRYPT_E_NOT_FOUND) { | 
|  | 5332 | Py_RETURN_TRUE; | 
|  | 5333 | } | 
|  | 5334 | return PyErr_SetFromWindowsErr(error); | 
|  | 5335 | } | 
|  | 5336 |  | 
|  | 5337 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); | 
|  | 5338 | if (usage == NULL) { | 
|  | 5339 | return PyErr_NoMemory(); | 
|  | 5340 | } | 
|  | 5341 |  | 
|  | 5342 | /* Now get the actual enhanced usage property */ | 
|  | 5343 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { | 
|  | 5344 | PyMem_Free(usage); | 
|  | 5345 | error = GetLastError(); | 
|  | 5346 | if (error == CRYPT_E_NOT_FOUND) { | 
|  | 5347 | Py_RETURN_TRUE; | 
|  | 5348 | } | 
|  | 5349 | return PyErr_SetFromWindowsErr(error); | 
|  | 5350 | } | 
|  | 5351 | retval = PySet_New(NULL); | 
|  | 5352 | if (retval == NULL) { | 
|  | 5353 | goto error; | 
|  | 5354 | } | 
|  | 5355 | for (i = 0; i < usage->cUsageIdentifier; ++i) { | 
|  | 5356 | if (usage->rgpszUsageIdentifier[i]) { | 
|  | 5357 | PyObject *oid; | 
|  | 5358 | int err; | 
|  | 5359 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); | 
|  | 5360 | if (oid == NULL) { | 
|  | 5361 | Py_CLEAR(retval); | 
|  | 5362 | goto error; | 
|  | 5363 | } | 
|  | 5364 | err = PySet_Add(retval, oid); | 
|  | 5365 | Py_DECREF(oid); | 
|  | 5366 | if (err == -1) { | 
|  | 5367 | Py_CLEAR(retval); | 
|  | 5368 | goto error; | 
|  | 5369 | } | 
|  | 5370 | } | 
|  | 5371 | } | 
|  | 5372 | error: | 
|  | 5373 | PyMem_Free(usage); | 
|  | 5374 | return retval; | 
|  | 5375 | } | 
|  | 5376 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5377 | /*[clinic input] | 
|  | 5378 | _ssl.enum_certificates | 
|  | 5379 | store_name: str | 
|  | 5380 |  | 
|  | 5381 | Retrieve certificates from Windows' cert store. | 
|  | 5382 |  | 
|  | 5383 | store_name may be one of 'CA', 'ROOT' or 'MY'.  The system may provide | 
|  | 5384 | more cert storages, too.  The function returns a list of (bytes, | 
|  | 5385 | encoding_type, trust) tuples.  The encoding_type flag can be interpreted | 
|  | 5386 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either | 
|  | 5387 | a set of OIDs or the boolean True. | 
|  | 5388 | [clinic start generated code]*/ | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5389 |  | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5390 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5391 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) | 
|  | 5392 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5393 | { | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5394 | HCERTSTORE hStore = NULL; | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5395 | PCCERT_CONTEXT pCertCtx = NULL; | 
|  | 5396 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5397 | PyObject *result = NULL; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5398 |  | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5399 | result = PyList_New(0); | 
|  | 5400 | if (result == NULL) { | 
|  | 5401 | return NULL; | 
|  | 5402 | } | 
| Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 5403 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, | 
|  | 5404 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, | 
|  | 5405 | store_name); | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5406 | if (hStore == NULL) { | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5407 | Py_DECREF(result); | 
|  | 5408 | return PyErr_SetFromWindowsErr(GetLastError()); | 
|  | 5409 | } | 
|  | 5410 |  | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5411 | while (pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx)) { | 
|  | 5412 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, | 
|  | 5413 | pCertCtx->cbCertEncoded); | 
|  | 5414 | if (!cert) { | 
|  | 5415 | Py_CLEAR(result); | 
|  | 5416 | break; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5417 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5418 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { | 
|  | 5419 | Py_CLEAR(result); | 
|  | 5420 | break; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5421 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5422 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); | 
|  | 5423 | if (keyusage == Py_True) { | 
|  | 5424 | Py_DECREF(keyusage); | 
|  | 5425 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5426 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5427 | if (keyusage == NULL) { | 
|  | 5428 | Py_CLEAR(result); | 
|  | 5429 | break; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5430 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5431 | if ((tup = PyTuple_New(3)) == NULL) { | 
|  | 5432 | Py_CLEAR(result); | 
|  | 5433 | break; | 
|  | 5434 | } | 
|  | 5435 | PyTuple_SET_ITEM(tup, 0, cert); | 
|  | 5436 | cert = NULL; | 
|  | 5437 | PyTuple_SET_ITEM(tup, 1, enc); | 
|  | 5438 | enc = NULL; | 
|  | 5439 | PyTuple_SET_ITEM(tup, 2, keyusage); | 
|  | 5440 | keyusage = NULL; | 
|  | 5441 | if (PyList_Append(result, tup) < 0) { | 
|  | 5442 | Py_CLEAR(result); | 
|  | 5443 | break; | 
|  | 5444 | } | 
|  | 5445 | Py_CLEAR(tup); | 
|  | 5446 | } | 
|  | 5447 | if (pCertCtx) { | 
|  | 5448 | /* loop ended with an error, need to clean up context manually */ | 
|  | 5449 | CertFreeCertificateContext(pCertCtx); | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5450 | } | 
|  | 5451 |  | 
|  | 5452 | /* In error cases cert, enc and tup may not be NULL */ | 
|  | 5453 | Py_XDECREF(cert); | 
|  | 5454 | Py_XDECREF(enc); | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5455 | Py_XDECREF(keyusage); | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5456 | Py_XDECREF(tup); | 
|  | 5457 |  | 
|  | 5458 | if (!CertCloseStore(hStore, 0)) { | 
|  | 5459 | /* This error case might shadow another exception.*/ | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5460 | Py_XDECREF(result); | 
|  | 5461 | return PyErr_SetFromWindowsErr(GetLastError()); | 
|  | 5462 | } | 
|  | 5463 | return result; | 
|  | 5464 | } | 
|  | 5465 |  | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5466 | /*[clinic input] | 
|  | 5467 | _ssl.enum_crls | 
|  | 5468 | store_name: str | 
|  | 5469 |  | 
|  | 5470 | Retrieve CRLs from Windows' cert store. | 
|  | 5471 |  | 
|  | 5472 | store_name may be one of 'CA', 'ROOT' or 'MY'.  The system may provide | 
|  | 5473 | more cert storages, too.  The function returns a list of (bytes, | 
|  | 5474 | encoding_type) tuples.  The encoding_type flag can be interpreted with | 
|  | 5475 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. | 
|  | 5476 | [clinic start generated code]*/ | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5477 |  | 
|  | 5478 | static PyObject * | 
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5479 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) | 
|  | 5480 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5481 | { | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5482 | HCERTSTORE hStore = NULL; | 
|  | 5483 | PCCRL_CONTEXT pCrlCtx = NULL; | 
|  | 5484 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; | 
|  | 5485 | PyObject *result = NULL; | 
|  | 5486 |  | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5487 | result = PyList_New(0); | 
|  | 5488 | if (result == NULL) { | 
|  | 5489 | return NULL; | 
|  | 5490 | } | 
| Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 5491 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, | 
|  | 5492 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, | 
|  | 5493 | store_name); | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5494 | if (hStore == NULL) { | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5495 | Py_DECREF(result); | 
|  | 5496 | return PyErr_SetFromWindowsErr(GetLastError()); | 
|  | 5497 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5498 |  | 
|  | 5499 | while (pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)) { | 
|  | 5500 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, | 
|  | 5501 | pCrlCtx->cbCrlEncoded); | 
|  | 5502 | if (!crl) { | 
|  | 5503 | Py_CLEAR(result); | 
|  | 5504 | break; | 
|  | 5505 | } | 
|  | 5506 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { | 
|  | 5507 | Py_CLEAR(result); | 
|  | 5508 | break; | 
|  | 5509 | } | 
|  | 5510 | if ((tup = PyTuple_New(2)) == NULL) { | 
|  | 5511 | Py_CLEAR(result); | 
|  | 5512 | break; | 
|  | 5513 | } | 
|  | 5514 | PyTuple_SET_ITEM(tup, 0, crl); | 
|  | 5515 | crl = NULL; | 
|  | 5516 | PyTuple_SET_ITEM(tup, 1, enc); | 
|  | 5517 | enc = NULL; | 
|  | 5518 |  | 
|  | 5519 | if (PyList_Append(result, tup) < 0) { | 
|  | 5520 | Py_CLEAR(result); | 
|  | 5521 | break; | 
|  | 5522 | } | 
|  | 5523 | Py_CLEAR(tup); | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5524 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5525 | if (pCrlCtx) { | 
|  | 5526 | /* loop ended with an error, need to clean up context manually */ | 
|  | 5527 | CertFreeCRLContext(pCrlCtx); | 
|  | 5528 | } | 
|  | 5529 |  | 
|  | 5530 | /* In error cases cert, enc and tup may not be NULL */ | 
|  | 5531 | Py_XDECREF(crl); | 
|  | 5532 | Py_XDECREF(enc); | 
|  | 5533 | Py_XDECREF(tup); | 
|  | 5534 |  | 
|  | 5535 | if (!CertCloseStore(hStore, 0)) { | 
|  | 5536 | /* This error case might shadow another exception.*/ | 
|  | 5537 | Py_XDECREF(result); | 
|  | 5538 | return PyErr_SetFromWindowsErr(GetLastError()); | 
|  | 5539 | } | 
|  | 5540 | return result; | 
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5541 | } | 
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5542 |  | 
|  | 5543 | #endif /* _MSC_VER */ | 
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5544 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5545 | /* List of functions exported by this module. */ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5546 | static PyMethodDef PySSL_methods[] = { | 
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5547 | _SSL__TEST_DECODE_CERT_METHODDEF | 
|  | 5548 | _SSL_RAND_ADD_METHODDEF | 
|  | 5549 | _SSL_RAND_BYTES_METHODDEF | 
|  | 5550 | _SSL_RAND_PSEUDO_BYTES_METHODDEF | 
|  | 5551 | _SSL_RAND_EGD_METHODDEF | 
|  | 5552 | _SSL_RAND_STATUS_METHODDEF | 
|  | 5553 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF | 
|  | 5554 | _SSL_ENUM_CERTIFICATES_METHODDEF | 
|  | 5555 | _SSL_ENUM_CRLS_METHODDEF | 
|  | 5556 | _SSL_TXT2OBJ_METHODDEF | 
|  | 5557 | _SSL_NID2OBJ_METHODDEF | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5558 | {NULL,                  NULL}            /* Sentinel */ | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5559 | }; | 
|  | 5560 |  | 
|  | 5561 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5562 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5563 |  | 
|  | 5564 | /* an implementation of OpenSSL threading operations in terms | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5565 | * of the Python C thread library | 
|  | 5566 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. | 
|  | 5567 | */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5568 |  | 
|  | 5569 | static PyThread_type_lock *_ssl_locks = NULL; | 
|  | 5570 |  | 
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5571 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 | 
|  | 5572 | /* use new CRYPTO_THREADID API. */ | 
|  | 5573 | static void | 
|  | 5574 | _ssl_threadid_callback(CRYPTO_THREADID *id) | 
|  | 5575 | { | 
| Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5576 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); | 
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5577 | } | 
|  | 5578 | #else | 
|  | 5579 | /* deprecated CRYPTO_set_id_callback() API. */ | 
|  | 5580 | static unsigned long | 
|  | 5581 | _ssl_thread_id_function (void) { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5582 | return PyThread_get_thread_ident(); | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5583 | } | 
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5584 | #endif | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5585 |  | 
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5586 | static void _ssl_thread_locking_function | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5587 | (int mode, int n, const char *file, int line) { | 
|  | 5588 | /* this function is needed to perform locking on shared data | 
|  | 5589 | structures. (Note that OpenSSL uses a number of global data | 
|  | 5590 | structures that will be implicitly shared whenever multiple | 
|  | 5591 | threads use OpenSSL.) Multi-threaded applications will | 
|  | 5592 | crash at random if it is not set. | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5593 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5594 | locking_function() must be able to handle up to | 
|  | 5595 | CRYPTO_num_locks() different mutex locks. It sets the n-th | 
|  | 5596 | lock if mode & CRYPTO_LOCK, and releases it otherwise. | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5597 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5598 | file and line are the file number of the function setting the | 
|  | 5599 | lock. They can be useful for debugging. | 
|  | 5600 | */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5601 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5602 | if ((_ssl_locks == NULL) || | 
|  | 5603 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) | 
|  | 5604 | return; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5605 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5606 | if (mode & CRYPTO_LOCK) { | 
|  | 5607 | PyThread_acquire_lock(_ssl_locks[n], 1); | 
|  | 5608 | } else { | 
|  | 5609 | PyThread_release_lock(_ssl_locks[n]); | 
|  | 5610 | } | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5611 | } | 
|  | 5612 |  | 
|  | 5613 | static int _setup_ssl_threads(void) { | 
|  | 5614 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5615 | unsigned int i; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5616 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5617 | if (_ssl_locks == NULL) { | 
|  | 5618 | _ssl_locks_count = CRYPTO_num_locks(); | 
| Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5619 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, | 
|  | 5620 | sizeof(PyThread_type_lock)); | 
| Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5621 | if (_ssl_locks == NULL) { | 
|  | 5622 | PyErr_NoMemory(); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5623 | return 0; | 
| Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5624 | } | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5625 | for (i = 0;  i < _ssl_locks_count;  i++) { | 
|  | 5626 | _ssl_locks[i] = PyThread_allocate_lock(); | 
|  | 5627 | if (_ssl_locks[i] == NULL) { | 
|  | 5628 | unsigned int j; | 
|  | 5629 | for (j = 0;  j < i;  j++) { | 
|  | 5630 | PyThread_free_lock(_ssl_locks[j]); | 
|  | 5631 | } | 
| Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5632 | PyMem_Free(_ssl_locks); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5633 | return 0; | 
|  | 5634 | } | 
|  | 5635 | } | 
|  | 5636 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); | 
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5637 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 | 
|  | 5638 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); | 
|  | 5639 | #else | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5640 | CRYPTO_set_id_callback(_ssl_thread_id_function); | 
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5641 | #endif | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5642 | } | 
|  | 5643 | return 1; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5644 | } | 
|  | 5645 |  | 
| Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5646 | #endif  /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5647 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5648 | PyDoc_STRVAR(module_doc, | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5649 | "Implementation module for SSL socket operations.  See the socket module\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | for documentation."); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5651 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5652 |  | 
|  | 5653 | static struct PyModuleDef _sslmodule = { | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5654 | PyModuleDef_HEAD_INIT, | 
|  | 5655 | "_ssl", | 
|  | 5656 | module_doc, | 
|  | 5657 | -1, | 
|  | 5658 | PySSL_methods, | 
|  | 5659 | NULL, | 
|  | 5660 | NULL, | 
|  | 5661 | NULL, | 
|  | 5662 | NULL | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5663 | }; | 
|  | 5664 |  | 
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5665 |  | 
|  | 5666 | static void | 
|  | 5667 | parse_openssl_version(unsigned long libver, | 
|  | 5668 | unsigned int *major, unsigned int *minor, | 
|  | 5669 | unsigned int *fix, unsigned int *patch, | 
|  | 5670 | unsigned int *status) | 
|  | 5671 | { | 
|  | 5672 | *status = libver & 0xF; | 
|  | 5673 | libver >>= 4; | 
|  | 5674 | *patch = libver & 0xFF; | 
|  | 5675 | libver >>= 8; | 
|  | 5676 | *fix = libver & 0xFF; | 
|  | 5677 | libver >>= 8; | 
|  | 5678 | *minor = libver & 0xFF; | 
|  | 5679 | libver >>= 8; | 
|  | 5680 | *major = libver & 0xFF; | 
|  | 5681 | } | 
|  | 5682 |  | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5683 | PyMODINIT_FUNC | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5684 | PyInit__ssl(void) | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5685 | { | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5686 | PyObject *m, *d, *r, *bases; | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5687 | unsigned long libver; | 
|  | 5688 | unsigned int major, minor, fix, patch, status; | 
|  | 5689 | PySocketModule_APIObject *socket_api; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5690 | struct py_ssl_error_code *errcode; | 
|  | 5691 | struct py_ssl_library_code *libcode; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5692 |  | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5693 | if (PyType_Ready(&PySSLContext_Type) < 0) | 
|  | 5694 | return NULL; | 
|  | 5695 | if (PyType_Ready(&PySSLSocket_Type) < 0) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5696 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5697 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) | 
|  | 5698 | return NULL; | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5699 | if (PyType_Ready(&PySSLSession_Type) < 0) | 
|  | 5700 | return NULL; | 
|  | 5701 |  | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5702 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5703 | m = PyModule_Create(&_sslmodule); | 
|  | 5704 | if (m == NULL) | 
|  | 5705 | return NULL; | 
|  | 5706 | d = PyModule_GetDict(m); | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5707 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5708 | /* Load _socket module and its C API */ | 
|  | 5709 | socket_api = PySocketModule_ImportModuleAndAPI(); | 
|  | 5710 | if (!socket_api) | 
|  | 5711 | return NULL; | 
|  | 5712 | PySocketModule = *socket_api; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5713 |  | 
| Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5714 | #ifndef OPENSSL_VERSION_1_1 | 
|  | 5715 | /* Load all algorithms and initialize cpuid */ | 
|  | 5716 | OPENSSL_add_all_algorithms_noconf(); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5717 | /* Init OpenSSL */ | 
|  | 5718 | SSL_load_error_strings(); | 
|  | 5719 | SSL_library_init(); | 
| Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5720 | #endif | 
|  | 5721 |  | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5722 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5723 | /* note that this will start threading if not already started */ | 
|  | 5724 | if (!_setup_ssl_threads()) { | 
|  | 5725 | return NULL; | 
|  | 5726 | } | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5727 | #elif OPENSSL_VERSION_1_1 && defined(OPENSSL_THREADS) | 
|  | 5728 | /* OpenSSL 1.1.0 builtin thread support is enabled */ | 
|  | 5729 | _ssl_locks_count++; | 
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5730 | #endif | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5731 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5732 | /* Add symbols to module dict */ | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5733 | sslerror_type_slots[0].pfunc = PyExc_OSError; | 
|  | 5734 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5735 | if (PySSLErrorObject == NULL) | 
|  | 5736 | return NULL; | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5737 |  | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5738 | /* ssl.CertificateError used to be a subclass of ValueError */ | 
|  | 5739 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); | 
|  | 5740 | if (bases == NULL) | 
|  | 5741 | return NULL; | 
|  | 5742 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5743 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, | 
|  | 5744 | bases, NULL); | 
|  | 5745 | Py_DECREF(bases); | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5746 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5747 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, | 
|  | 5748 | PySSLErrorObject, NULL); | 
|  | 5749 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5750 | "ssl.SSLWantReadError", SSLWantReadError_doc, | 
|  | 5751 | PySSLErrorObject, NULL); | 
|  | 5752 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5753 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, | 
|  | 5754 | PySSLErrorObject, NULL); | 
|  | 5755 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5756 | "ssl.SSLSyscallError", SSLSyscallError_doc, | 
|  | 5757 | PySSLErrorObject, NULL); | 
|  | 5758 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( | 
|  | 5759 | "ssl.SSLEOFError", SSLEOFError_doc, | 
|  | 5760 | PySSLErrorObject, NULL); | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5761 | if (PySSLCertVerificationErrorObject == NULL | 
|  | 5762 | || PySSLZeroReturnErrorObject == NULL | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5763 | || PySSLWantReadErrorObject == NULL | 
|  | 5764 | || PySSLWantWriteErrorObject == NULL | 
|  | 5765 | || PySSLSyscallErrorObject == NULL | 
|  | 5766 | || PySSLEOFErrorObject == NULL) | 
|  | 5767 | return NULL; | 
|  | 5768 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 | 
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5769 | || PyDict_SetItemString(d, "SSLCertVerificationError", | 
|  | 5770 | PySSLCertVerificationErrorObject) != 0 | 
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5771 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 | 
|  | 5772 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 | 
|  | 5773 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 | 
|  | 5774 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 | 
|  | 5775 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5776 | return NULL; | 
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5777 | if (PyDict_SetItemString(d, "_SSLContext", | 
|  | 5778 | (PyObject *)&PySSLContext_Type) != 0) | 
|  | 5779 | return NULL; | 
|  | 5780 | if (PyDict_SetItemString(d, "_SSLSocket", | 
|  | 5781 | (PyObject *)&PySSLSocket_Type) != 0) | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5782 | return NULL; | 
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5783 | if (PyDict_SetItemString(d, "MemoryBIO", | 
|  | 5784 | (PyObject *)&PySSLMemoryBIO_Type) != 0) | 
|  | 5785 | return NULL; | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5786 | if (PyDict_SetItemString(d, "SSLSession", | 
|  | 5787 | (PyObject *)&PySSLSession_Type) != 0) | 
|  | 5788 | return NULL; | 
|  | 5789 |  | 
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 5790 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", | 
|  | 5791 | PY_SSL_DEFAULT_CIPHER_STRING); | 
|  | 5792 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5793 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", | 
|  | 5794 | PY_SSL_ERROR_ZERO_RETURN); | 
|  | 5795 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", | 
|  | 5796 | PY_SSL_ERROR_WANT_READ); | 
|  | 5797 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", | 
|  | 5798 | PY_SSL_ERROR_WANT_WRITE); | 
|  | 5799 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", | 
|  | 5800 | PY_SSL_ERROR_WANT_X509_LOOKUP); | 
|  | 5801 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", | 
|  | 5802 | PY_SSL_ERROR_SYSCALL); | 
|  | 5803 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", | 
|  | 5804 | PY_SSL_ERROR_SSL); | 
|  | 5805 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", | 
|  | 5806 | PY_SSL_ERROR_WANT_CONNECT); | 
|  | 5807 | /* non ssl.h errorcodes */ | 
|  | 5808 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", | 
|  | 5809 | PY_SSL_ERROR_EOF); | 
|  | 5810 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", | 
|  | 5811 | PY_SSL_ERROR_INVALID_ERROR_CODE); | 
|  | 5812 | /* cert requirements */ | 
|  | 5813 | PyModule_AddIntConstant(m, "CERT_NONE", | 
|  | 5814 | PY_SSL_CERT_NONE); | 
|  | 5815 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", | 
|  | 5816 | PY_SSL_CERT_OPTIONAL); | 
|  | 5817 | PyModule_AddIntConstant(m, "CERT_REQUIRED", | 
|  | 5818 | PY_SSL_CERT_REQUIRED); | 
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 5819 | /* CRL verification for verification_flags */ | 
|  | 5820 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", | 
|  | 5821 | 0); | 
|  | 5822 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", | 
|  | 5823 | X509_V_FLAG_CRL_CHECK); | 
|  | 5824 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", | 
|  | 5825 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); | 
|  | 5826 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", | 
|  | 5827 | X509_V_FLAG_X509_STRICT); | 
| Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 5828 | #ifdef X509_V_FLAG_TRUSTED_FIRST | 
|  | 5829 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", | 
|  | 5830 | X509_V_FLAG_TRUSTED_FIRST); | 
|  | 5831 | #endif | 
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 5832 |  | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5833 | /* Alert Descriptions from ssl.h */ | 
|  | 5834 | /* note RESERVED constants no longer intended for use have been removed */ | 
|  | 5835 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ | 
|  | 5836 |  | 
|  | 5837 | #define ADD_AD_CONSTANT(s) \ | 
|  | 5838 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ | 
|  | 5839 | SSL_AD_##s) | 
|  | 5840 |  | 
|  | 5841 | ADD_AD_CONSTANT(CLOSE_NOTIFY); | 
|  | 5842 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); | 
|  | 5843 | ADD_AD_CONSTANT(BAD_RECORD_MAC); | 
|  | 5844 | ADD_AD_CONSTANT(RECORD_OVERFLOW); | 
|  | 5845 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); | 
|  | 5846 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); | 
|  | 5847 | ADD_AD_CONSTANT(BAD_CERTIFICATE); | 
|  | 5848 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); | 
|  | 5849 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); | 
|  | 5850 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); | 
|  | 5851 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); | 
|  | 5852 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); | 
|  | 5853 | ADD_AD_CONSTANT(UNKNOWN_CA); | 
|  | 5854 | ADD_AD_CONSTANT(ACCESS_DENIED); | 
|  | 5855 | ADD_AD_CONSTANT(DECODE_ERROR); | 
|  | 5856 | ADD_AD_CONSTANT(DECRYPT_ERROR); | 
|  | 5857 | ADD_AD_CONSTANT(PROTOCOL_VERSION); | 
|  | 5858 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); | 
|  | 5859 | ADD_AD_CONSTANT(INTERNAL_ERROR); | 
|  | 5860 | ADD_AD_CONSTANT(USER_CANCELLED); | 
|  | 5861 | ADD_AD_CONSTANT(NO_RENEGOTIATION); | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5862 | /* Not all constants are in old OpenSSL versions */ | 
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5863 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION | 
|  | 5864 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); | 
|  | 5865 | #endif | 
|  | 5866 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE | 
|  | 5867 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); | 
|  | 5868 | #endif | 
|  | 5869 | #ifdef SSL_AD_UNRECOGNIZED_NAME | 
|  | 5870 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); | 
|  | 5871 | #endif | 
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5872 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE | 
|  | 5873 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); | 
|  | 5874 | #endif | 
|  | 5875 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE | 
|  | 5876 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); | 
|  | 5877 | #endif | 
|  | 5878 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY | 
|  | 5879 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); | 
|  | 5880 | #endif | 
|  | 5881 |  | 
|  | 5882 | #undef ADD_AD_CONSTANT | 
|  | 5883 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5884 | /* protocol versions */ | 
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5885 | #ifndef OPENSSL_NO_SSL2 | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5886 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", | 
|  | 5887 | PY_SSL_VERSION_SSL2); | 
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5888 | #endif | 
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5889 | #ifndef OPENSSL_NO_SSL3 | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5890 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", | 
|  | 5891 | PY_SSL_VERSION_SSL3); | 
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5892 | #endif | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5893 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", | 
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5894 | PY_SSL_VERSION_TLS); | 
|  | 5895 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", | 
|  | 5896 | PY_SSL_VERSION_TLS); | 
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 5897 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", | 
|  | 5898 | PY_SSL_VERSION_TLS_CLIENT); | 
|  | 5899 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", | 
|  | 5900 | PY_SSL_VERSION_TLS_SERVER); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5901 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", | 
|  | 5902 | PY_SSL_VERSION_TLS1); | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5903 | #if HAVE_TLSv1_2 | 
|  | 5904 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", | 
|  | 5905 | PY_SSL_VERSION_TLS1_1); | 
|  | 5906 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", | 
|  | 5907 | PY_SSL_VERSION_TLS1_2); | 
|  | 5908 | #endif | 
| Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5909 |  | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5910 | /* protocol options */ | 
| Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 5911 | PyModule_AddIntConstant(m, "OP_ALL", | 
|  | 5912 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5913 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); | 
|  | 5914 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); | 
|  | 5915 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); | 
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5916 | #if HAVE_TLSv1_2 | 
|  | 5917 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); | 
|  | 5918 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); | 
|  | 5919 | #endif | 
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5920 | #ifdef SSL_OP_NO_TLSv1_3 | 
|  | 5921 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); | 
|  | 5922 | #else | 
|  | 5923 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); | 
|  | 5924 | #endif | 
| Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 5925 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", | 
|  | 5926 | SSL_OP_CIPHER_SERVER_PREFERENCE); | 
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 5927 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); | 
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5928 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); | 
| Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5929 | #ifdef SSL_OP_SINGLE_ECDH_USE | 
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 5930 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); | 
| Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5931 | #endif | 
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 5932 | #ifdef SSL_OP_NO_COMPRESSION | 
|  | 5933 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", | 
|  | 5934 | SSL_OP_NO_COMPRESSION); | 
|  | 5935 | #endif | 
| Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 5936 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT | 
|  | 5937 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", | 
|  | 5938 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); | 
|  | 5939 | #endif | 
| Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 5940 | #ifdef SSL_OP_NO_RENEGOTIATION | 
|  | 5941 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", | 
|  | 5942 | SSL_OP_NO_RENEGOTIATION); | 
|  | 5943 | #endif | 
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5944 |  | 
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 5945 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT | 
|  | 5946 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", | 
|  | 5947 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); | 
|  | 5948 | #endif | 
|  | 5949 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT | 
|  | 5950 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", | 
|  | 5951 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); | 
|  | 5952 | #endif | 
|  | 5953 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS | 
|  | 5954 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", | 
|  | 5955 | X509_CHECK_FLAG_NO_WILDCARDS); | 
|  | 5956 | #endif | 
|  | 5957 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS | 
|  | 5958 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", | 
|  | 5959 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); | 
|  | 5960 | #endif | 
|  | 5961 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS | 
|  | 5962 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", | 
|  | 5963 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); | 
|  | 5964 | #endif | 
|  | 5965 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS | 
|  | 5966 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", | 
|  | 5967 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); | 
|  | 5968 | #endif | 
|  | 5969 |  | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5970 | /* protocol versions */ | 
|  | 5971 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", | 
|  | 5972 | PY_PROTO_MINIMUM_SUPPORTED); | 
|  | 5973 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", | 
|  | 5974 | PY_PROTO_MAXIMUM_SUPPORTED); | 
|  | 5975 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); | 
|  | 5976 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); | 
|  | 5977 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); | 
|  | 5978 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); | 
|  | 5979 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); | 
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 5980 |  | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5981 | #define addbool(m, v, b) \ | 
|  | 5982 | Py_INCREF((b) ? Py_True : Py_False); \ | 
|  | 5983 | PyModule_AddObject((m), (v), (b) ? Py_True : Py_False); | 
|  | 5984 |  | 
|  | 5985 | #if HAVE_SNI | 
|  | 5986 | addbool(m, "HAS_SNI", 1); | 
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 5987 | #else | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5988 | addbool(m, "HAS_SNI", 0); | 
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 5989 | #endif | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5990 |  | 
|  | 5991 | addbool(m, "HAS_TLS_UNIQUE", 1); | 
|  | 5992 |  | 
|  | 5993 | #ifndef OPENSSL_NO_ECDH | 
|  | 5994 | addbool(m, "HAS_ECDH", 1); | 
|  | 5995 | #else | 
|  | 5996 | addbool(m, "HAS_ECDH", 0); | 
|  | 5997 | #endif | 
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 5998 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 5999 | #if HAVE_NPN | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6000 | addbool(m, "HAS_NPN", 1); | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6001 | #else | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6002 | addbool(m, "HAS_NPN", 0); | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6003 | #endif | 
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6004 |  | 
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6005 | #if HAVE_ALPN | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6006 | addbool(m, "HAS_ALPN", 1); | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6007 | #else | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6008 | addbool(m, "HAS_ALPN", 0); | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6009 | #endif | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6010 |  | 
|  | 6011 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) | 
|  | 6012 | addbool(m, "HAS_SSLv2", 1); | 
|  | 6013 | #else | 
|  | 6014 | addbool(m, "HAS_SSLv2", 0); | 
|  | 6015 | #endif | 
|  | 6016 |  | 
|  | 6017 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) | 
|  | 6018 | addbool(m, "HAS_SSLv3", 1); | 
|  | 6019 | #else | 
|  | 6020 | addbool(m, "HAS_SSLv3", 0); | 
|  | 6021 | #endif | 
|  | 6022 |  | 
|  | 6023 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) | 
|  | 6024 | addbool(m, "HAS_TLSv1", 1); | 
|  | 6025 | #else | 
|  | 6026 | addbool(m, "HAS_TLSv1", 0); | 
|  | 6027 | #endif | 
|  | 6028 |  | 
|  | 6029 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) | 
|  | 6030 | addbool(m, "HAS_TLSv1_1", 1); | 
|  | 6031 | #else | 
|  | 6032 | addbool(m, "HAS_TLSv1_1", 0); | 
|  | 6033 | #endif | 
|  | 6034 |  | 
|  | 6035 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) | 
|  | 6036 | addbool(m, "HAS_TLSv1_2", 1); | 
|  | 6037 | #else | 
|  | 6038 | addbool(m, "HAS_TLSv1_2", 0); | 
|  | 6039 | #endif | 
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6040 |  | 
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6041 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6042 | addbool(m, "HAS_TLSv1_3", 1); | 
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6043 | #else | 
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6044 | addbool(m, "HAS_TLSv1_3", 0); | 
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6045 | #endif | 
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6046 |  | 
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6047 | /* Mappings for error codes */ | 
|  | 6048 | err_codes_to_names = PyDict_New(); | 
|  | 6049 | err_names_to_codes = PyDict_New(); | 
|  | 6050 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) | 
|  | 6051 | return NULL; | 
|  | 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 NULL; | 
|  | 6059 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) | 
|  | 6060 | return NULL; | 
|  | 6061 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) | 
|  | 6062 | return NULL; | 
|  | 6063 | Py_DECREF(key); | 
|  | 6064 | Py_DECREF(mnemo); | 
|  | 6065 | errcode++; | 
|  | 6066 | } | 
|  | 6067 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) | 
|  | 6068 | return NULL; | 
|  | 6069 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) | 
|  | 6070 | return NULL; | 
|  | 6071 |  | 
|  | 6072 | lib_codes_to_names = PyDict_New(); | 
|  | 6073 | if (lib_codes_to_names == NULL) | 
|  | 6074 | return NULL; | 
|  | 6075 | libcode = library_codes; | 
|  | 6076 | while (libcode->library != NULL) { | 
|  | 6077 | PyObject *mnemo, *key; | 
|  | 6078 | key = PyLong_FromLong(libcode->code); | 
|  | 6079 | mnemo = PyUnicode_FromString(libcode->library); | 
|  | 6080 | if (key == NULL || mnemo == NULL) | 
|  | 6081 | return NULL; | 
|  | 6082 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) | 
|  | 6083 | return NULL; | 
|  | 6084 | Py_DECREF(key); | 
|  | 6085 | Py_DECREF(mnemo); | 
|  | 6086 | libcode++; | 
|  | 6087 | } | 
|  | 6088 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) | 
|  | 6089 | return NULL; | 
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 6090 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6091 | /* OpenSSL version */ | 
|  | 6092 | /* SSLeay() gives us the version of the library linked against, | 
|  | 6093 | which could be different from the headers version. | 
|  | 6094 | */ | 
|  | 6095 | libver = SSLeay(); | 
|  | 6096 | r = PyLong_FromUnsignedLong(libver); | 
|  | 6097 | if (r == NULL) | 
|  | 6098 | return NULL; | 
|  | 6099 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) | 
|  | 6100 | return NULL; | 
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6101 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6102 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); | 
|  | 6103 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) | 
|  | 6104 | return NULL; | 
|  | 6105 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); | 
|  | 6106 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) | 
|  | 6107 | return NULL; | 
| Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6108 |  | 
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6109 | libver = OPENSSL_VERSION_NUMBER; | 
|  | 6110 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); | 
|  | 6111 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); | 
|  | 6112 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) | 
|  | 6113 | return NULL; | 
|  | 6114 |  | 
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6115 | return m; | 
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6116 | } |