| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
| Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 17 | #define PY_SSIZE_T_CLEAN |
| 18 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 19 | #include "Python.h" |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 21 | /* Redefined below for Windows debug builds after important #includes */ |
| 22 | #define _PySSL_FIX_ERRNO |
| Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 23 | |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 24 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 25 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 26 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 27 | do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } _PySSL_FIX_ERRNO; } while (0) |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 28 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 29 | PyThreadState *_save = NULL; \ |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 30 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 31 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 32 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 33 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 34 | |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 35 | /* Include symbols from _socket module */ |
| 36 | #include "socketmodule.h" |
| 37 | |
| 38 | static PySocketModule_APIObject PySocketModule; |
| 39 | |
| 40 | #if defined(HAVE_POLL_H) |
| 41 | #include <poll.h> |
| 42 | #elif defined(HAVE_SYS_POLL_H) |
| 43 | #include <sys/poll.h> |
| 44 | #endif |
| 45 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 46 | /* Don't warn about deprecated functions */ |
| 47 | #ifdef __GNUC__ |
| 48 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 49 | #endif |
| 50 | #ifdef __clang__ |
| 51 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 52 | #endif |
| 53 | |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 54 | /* Include OpenSSL header files */ |
| 55 | #include "openssl/rsa.h" |
| 56 | #include "openssl/crypto.h" |
| 57 | #include "openssl/x509.h" |
| 58 | #include "openssl/x509v3.h" |
| 59 | #include "openssl/pem.h" |
| 60 | #include "openssl/ssl.h" |
| 61 | #include "openssl/err.h" |
| 62 | #include "openssl/rand.h" |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 63 | #include "openssl/bio.h" |
| Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 64 | #include "openssl/dh.h" |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 65 | |
| Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 66 | #ifndef HAVE_X509_VERIFY_PARAM_SET1_HOST |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 67 | # ifdef LIBRESSL_VERSION_NUMBER |
| 68 | # error "LibreSSL is missing X509_VERIFY_PARAM_set1_host(), see https://github.com/libressl-portable/portable/issues/381" |
| 69 | # elif OPENSSL_VERSION_NUMBER > 0x1000200fL |
| Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 70 | # define HAVE_X509_VERIFY_PARAM_SET1_HOST |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 71 | # else |
| 72 | # error "libssl is too old and does not support X509_VERIFY_PARAM_set1_host()" |
| Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 73 | # endif |
| 74 | #endif |
| 75 | |
| Christian Heimes | c087a26 | 2020-05-15 20:55:25 +0200 | [diff] [blame^] | 76 | #ifndef OPENSSL_THREADS |
| 77 | # error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL" |
| 78 | #endif |
| 79 | |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 80 | /* SSL error object */ |
| 81 | static PyObject *PySSLErrorObject; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 82 | static PyObject *PySSLCertVerificationErrorObject; |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 83 | static PyObject *PySSLZeroReturnErrorObject; |
| 84 | static PyObject *PySSLWantReadErrorObject; |
| 85 | static PyObject *PySSLWantWriteErrorObject; |
| 86 | static PyObject *PySSLSyscallErrorObject; |
| 87 | static PyObject *PySSLEOFErrorObject; |
| 88 | |
| 89 | /* Error mappings */ |
| 90 | static PyObject *err_codes_to_names; |
| 91 | static PyObject *err_names_to_codes; |
| 92 | static PyObject *lib_codes_to_names; |
| 93 | |
| 94 | struct py_ssl_error_code { |
| 95 | const char *mnemonic; |
| 96 | int library, reason; |
| 97 | }; |
| 98 | struct py_ssl_library_code { |
| 99 | const char *library; |
| 100 | int code; |
| 101 | }; |
| 102 | |
| Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 103 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 104 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 105 | * However, because it uses a different CRT, we need to transfer the |
| 106 | * value of errno from OpenSSL into our debug CRT. |
| 107 | * |
| 108 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 109 | * alternative is to do both debug and release builds of OpenSSL, which |
| 110 | * requires much uglier code to transform their automatically generated |
| 111 | * makefile. This is the lesser of all the evils. |
| 112 | */ |
| 113 | |
| 114 | static void _PySSLFixErrno(void) { |
| 115 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 116 | if (!ucrtbase) { |
| 117 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 118 | * have a catastrophic failure, but this function is not the |
| 119 | * place to raise it. */ |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | typedef int *(__stdcall *errno_func)(void); |
| 124 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 125 | if (ssl_errno) { |
| 126 | errno = *ssl_errno(); |
| 127 | *ssl_errno() = 0; |
| 128 | } else { |
| 129 | errno = ENOTRECOVERABLE; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #undef _PySSL_FIX_ERRNO |
| 134 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 135 | #endif |
| 136 | |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 137 | /* Include generated data (error codes) */ |
| 138 | #include "_ssl_data.h" |
| 139 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 140 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 141 | # define OPENSSL_VERSION_1_1 1 |
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 142 | # define PY_OPENSSL_1_1_API 1 |
| 143 | #endif |
| 144 | |
| 145 | /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ |
| 146 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL |
| 147 | # define PY_OPENSSL_1_1_API 1 |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 148 | #endif |
| 149 | |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 150 | /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 |
| 151 | http://www.openssl.org/news/changelog.html |
| 152 | */ |
| 153 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 154 | # define HAVE_TLSv1_2 1 |
| 155 | #else |
| 156 | # define HAVE_TLSv1_2 0 |
| 157 | #endif |
| 158 | |
| Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 159 | /* 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] | 160 | * This includes the SSL_set_SSL_CTX() function. |
| 161 | */ |
| 162 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 163 | # define HAVE_SNI 1 |
| 164 | #else |
| 165 | # define HAVE_SNI 0 |
| 166 | #endif |
| 167 | |
| Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 168 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 169 | # define HAVE_ALPN 1 |
| 170 | #else |
| 171 | # define HAVE_ALPN 0 |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 172 | #endif |
| 173 | |
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 174 | /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped |
| 175 | * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility |
| 176 | * reasons. The check for TLSEXT_TYPE_next_proto_neg works with |
| 177 | * OpenSSL 1.0.1+ and LibreSSL. |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 178 | * 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] | 179 | */ |
| 180 | #ifdef OPENSSL_NO_NEXTPROTONEG |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 181 | # define HAVE_NPN 0 |
| 182 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 183 | # define HAVE_NPN 0 |
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 184 | #elif defined(TLSEXT_TYPE_next_proto_neg) |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 185 | # define HAVE_NPN 1 |
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 186 | #else |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 187 | # define HAVE_NPN 0 |
| 188 | #endif |
| Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 189 | |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 190 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 191 | #define HAVE_OPENSSL_KEYLOG 1 |
| 192 | #endif |
| 193 | |
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 194 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 195 | #define INVALID_SOCKET (-1) |
| 196 | #endif |
| 197 | |
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 198 | /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ |
| 199 | #ifndef OPENSSL_VERSION_1_1 |
| 200 | #define HAVE_OPENSSL_CRYPTO_LOCK |
| 201 | #endif |
| 202 | |
| 203 | #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 204 | #define OPENSSL_NO_SSL2 |
| 205 | #endif |
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 206 | |
| 207 | #ifndef PY_OPENSSL_1_1_API |
| 208 | /* 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] | 209 | |
| 210 | #define TLS_method SSLv23_method |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 211 | #define TLS_client_method SSLv23_client_method |
| 212 | #define TLS_server_method SSLv23_server_method |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 213 | |
| 214 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 215 | { |
| 216 | return ne->set; |
| 217 | } |
| 218 | |
| 219 | #ifndef OPENSSL_NO_COMP |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 220 | /* LCOV_EXCL_START */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 221 | static int COMP_get_type(const COMP_METHOD *meth) |
| 222 | { |
| 223 | return meth->type; |
| 224 | } |
| Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 225 | /* LCOV_EXCL_STOP */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 226 | #endif |
| 227 | |
| 228 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 229 | { |
| 230 | return ctx->default_passwd_callback; |
| 231 | } |
| 232 | |
| 233 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 234 | { |
| 235 | return ctx->default_passwd_callback_userdata; |
| 236 | } |
| 237 | |
| 238 | static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 239 | { |
| 240 | return x->type; |
| 241 | } |
| 242 | |
| 243 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 244 | { |
| 245 | return x->data.x509; |
| 246 | } |
| 247 | |
| 248 | static int BIO_up_ref(BIO *b) |
| 249 | { |
| 250 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 255 | return store->objs; |
| 256 | } |
| 257 | |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 258 | static int |
| 259 | SSL_SESSION_has_ticket(const SSL_SESSION *s) |
| 260 | { |
| 261 | return (s->tlsext_ticklen > 0) ? 1 : 0; |
| 262 | } |
| 263 | |
| 264 | static unsigned long |
| 265 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
| 266 | { |
| 267 | return s->tlsext_tick_lifetime_hint; |
| 268 | } |
| 269 | |
| Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 270 | #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 271 | |
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 272 | /* Default cipher suites */ |
| 273 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 274 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 275 | #endif |
| 276 | |
| 277 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 278 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 279 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 280 | #endif |
| 281 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
| Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 282 | /* Python custom selection of sensible cipher suites |
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 283 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. |
| 284 | * !aNULL:!eNULL: really no NULL ciphers |
| 285 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. |
| 286 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
| 287 | * !SRP:!PSK: no secure remote password or pre-shared key authentication |
| 288 | */ |
| 289 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" |
| 290 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 291 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 292 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 293 | #else |
| 294 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 295 | #endif |
| 296 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 297 | |
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 298 | enum py_ssl_error { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 299 | /* these mirror ssl.h */ |
| 300 | PY_SSL_ERROR_NONE, |
| 301 | PY_SSL_ERROR_SSL, |
| 302 | PY_SSL_ERROR_WANT_READ, |
| 303 | PY_SSL_ERROR_WANT_WRITE, |
| 304 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 305 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 306 | PY_SSL_ERROR_ZERO_RETURN, |
| 307 | PY_SSL_ERROR_WANT_CONNECT, |
| 308 | /* start of non ssl.h errorcodes */ |
| 309 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 310 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 311 | PY_SSL_ERROR_INVALID_ERROR_CODE |
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 312 | }; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 313 | |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 314 | enum py_ssl_server_or_client { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 315 | PY_SSL_CLIENT, |
| 316 | PY_SSL_SERVER |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | enum py_ssl_cert_requirements { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 320 | PY_SSL_CERT_NONE, |
| 321 | PY_SSL_CERT_OPTIONAL, |
| 322 | PY_SSL_CERT_REQUIRED |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | enum py_ssl_version { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 326 | PY_SSL_VERSION_SSL2, |
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 327 | PY_SSL_VERSION_SSL3=1, |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 328 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 329 | #if HAVE_TLSv1_2 |
| 330 | PY_SSL_VERSION_TLS1, |
| 331 | PY_SSL_VERSION_TLS1_1, |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 332 | PY_SSL_VERSION_TLS1_2, |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 333 | #else |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 334 | PY_SSL_VERSION_TLS1, |
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 335 | #endif |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 336 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 337 | PY_SSL_VERSION_TLS_SERVER, |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 338 | }; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 339 | |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 340 | enum py_proto_version { |
| 341 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 342 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 343 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 344 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 345 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 346 | #ifdef TLS1_3_VERSION |
| 347 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 348 | #else |
| 349 | PY_PROTO_TLSv1_3 = 0x304, |
| 350 | #endif |
| 351 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 352 | |
| 353 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 354 | * available version, and the other way around. We have to figure out the |
| 355 | * minimum and maximum available version on our own and hope for the best. |
| 356 | */ |
| 357 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 358 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 359 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 360 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 361 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 362 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 363 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 364 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 365 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 366 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 367 | #else |
| 368 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 369 | #endif |
| 370 | |
| 371 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 372 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 373 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 374 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 375 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 376 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 377 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 378 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 379 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 380 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 381 | #else |
| 382 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 383 | #endif |
| 384 | }; |
| 385 | |
| 386 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 387 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 388 | /* 0 means no, greater than 0 means yes */ |
| 389 | |
| 390 | static unsigned int _ssl_locks_count = 0; |
| 391 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 392 | /* SSL socket object */ |
| 393 | |
| 394 | #define X509_NAME_MAXLEN 256 |
| 395 | |
| Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 396 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 397 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 398 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 399 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 400 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 401 | #else |
| 402 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 403 | #endif |
| 404 | |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 405 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 406 | * older SSL, but let's be safe */ |
| 407 | #define PySSL_CB_MAXLEN 128 |
| 408 | |
| Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 409 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 410 | typedef struct { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 411 | PyObject_HEAD |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 412 | SSL_CTX *ctx; |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 413 | #if HAVE_NPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 414 | unsigned char *npn_protocols; |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 415 | int npn_protocols_len; |
| 416 | #endif |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 417 | #if HAVE_ALPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 418 | unsigned char *alpn_protocols; |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 419 | unsigned int alpn_protocols_len; |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 420 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 421 | #ifndef OPENSSL_NO_TLSEXT |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 422 | PyObject *set_sni_cb; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 423 | #endif |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 424 | int check_hostname; |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 425 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 426 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 427 | */ |
| 428 | unsigned int hostflags; |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 429 | int protocol; |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 430 | #ifdef TLS1_3_VERSION |
| 431 | int post_handshake_auth; |
| 432 | #endif |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 433 | PyObject *msg_cb; |
| 434 | #ifdef HAVE_OPENSSL_KEYLOG |
| 435 | PyObject *keylog_filename; |
| 436 | BIO *keylog_bio; |
| 437 | #endif |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 438 | } PySSLContext; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 439 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 440 | typedef struct { |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 441 | int ssl; /* last seen error from SSL */ |
| 442 | int c; /* last seen error from libc */ |
| 443 | #ifdef MS_WINDOWS |
| 444 | int ws; /* last seen error from winsock */ |
| 445 | #endif |
| 446 | } _PySSLError; |
| 447 | |
| 448 | typedef struct { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 449 | PyObject_HEAD |
| 450 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 451 | SSL *ssl; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 452 | PySSLContext *ctx; /* weakref to SSL context */ |
| Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 453 | char shutdown_seen_zero; |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 454 | enum py_ssl_server_or_client socket_type; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 455 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 456 | PyObject *server_hostname; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 457 | _PySSLError err; /* last seen error from various sources */ |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 458 | /* Some SSL callbacks don't have error reporting. Callback wrappers |
| 459 | * store exception information on the socket. The handshake, read, write, |
| 460 | * and shutdown methods check for chained exceptions. |
| 461 | */ |
| 462 | PyObject *exc_type; |
| 463 | PyObject *exc_value; |
| 464 | PyObject *exc_tb; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 465 | } PySSLSocket; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 466 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 467 | typedef struct { |
| 468 | PyObject_HEAD |
| 469 | BIO *bio; |
| 470 | int eof_written; |
| 471 | } PySSLMemoryBIO; |
| 472 | |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 473 | typedef struct { |
| 474 | PyObject_HEAD |
| 475 | SSL_SESSION *session; |
| 476 | PySSLContext *ctx; |
| 477 | } PySSLSession; |
| 478 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 479 | static PyTypeObject PySSLContext_Type; |
| 480 | static PyTypeObject PySSLSocket_Type; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 481 | static PyTypeObject PySSLMemoryBIO_Type; |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 482 | static PyTypeObject PySSLSession_Type; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 483 | |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 484 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 485 | { |
| 486 | _PySSLError err = { 0 }; |
| 487 | if (failed) { |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 488 | #ifdef MS_WINDOWS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 489 | err.ws = WSAGetLastError(); |
| 490 | _PySSL_FIX_ERRNO; |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 491 | #endif |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 492 | err.c = errno; |
| 493 | err.ssl = SSL_get_error(ssl, retcode); |
| 494 | } |
| 495 | return err; |
| 496 | } |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 497 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 498 | /*[clinic input] |
| 499 | module _ssl |
| 500 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" |
| 501 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" |
| 502 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 503 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 504 | [clinic start generated code]*/ |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 505 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 506 | |
| 507 | #include "clinic/_ssl.c.h" |
| 508 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 509 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 510 | |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 511 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 512 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
| Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 513 | #define PySSLSocket_Check(v) Py_IS_TYPE(v, &PySSLSocket_Type) |
| 514 | #define PySSLMemoryBIO_Check(v) Py_IS_TYPE(v, &PySSLMemoryBIO_Type) |
| 515 | #define PySSLSession_Check(v) Py_IS_TYPE(v, &PySSLSession_Type) |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 516 | |
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 517 | typedef enum { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 518 | SOCKET_IS_NONBLOCKING, |
| 519 | SOCKET_IS_BLOCKING, |
| 520 | SOCKET_HAS_TIMED_OUT, |
| 521 | SOCKET_HAS_BEEN_CLOSED, |
| 522 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 523 | SOCKET_OPERATION_OK |
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 524 | } timeout_state; |
| 525 | |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 526 | /* Wrap error strings with filename and line # */ |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 527 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
| Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 528 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 529 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 530 | /* Get the socket from a PySSLSocket, if it has one */ |
| 531 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 532 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 533 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 534 | /* If sock is NULL, use a timeout of 0 second */ |
| 535 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 536 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 537 | |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 538 | #include "_ssl/debughelpers.c" |
| 539 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 540 | /* |
| 541 | * SSL errors. |
| 542 | */ |
| 543 | |
| 544 | PyDoc_STRVAR(SSLError_doc, |
| 545 | "An error occurred in the SSL implementation."); |
| 546 | |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 547 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 548 | "A certificate could not be verified."); |
| 549 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 550 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 551 | "SSL/TLS session closed cleanly."); |
| 552 | |
| 553 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 554 | "Non-blocking SSL socket needs to read more data\n" |
| 555 | "before the requested operation can be completed."); |
| 556 | |
| 557 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 558 | "Non-blocking SSL socket needs to write more data\n" |
| 559 | "before the requested operation can be completed."); |
| 560 | |
| 561 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 562 | "System error when attempting SSL operation."); |
| 563 | |
| 564 | PyDoc_STRVAR(SSLEOFError_doc, |
| 565 | "SSL/TLS connection terminated abruptly."); |
| 566 | |
| 567 | static PyObject * |
| 568 | SSLError_str(PyOSErrorObject *self) |
| 569 | { |
| 570 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 571 | Py_INCREF(self->strerror); |
| 572 | return self->strerror; |
| 573 | } |
| 574 | else |
| 575 | return PyObject_Str(self->args); |
| 576 | } |
| 577 | |
| 578 | static PyType_Slot sslerror_type_slots[] = { |
| 579 | {Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */ |
| Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 580 | {Py_tp_doc, (void*)SSLError_doc}, |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 581 | {Py_tp_str, SSLError_str}, |
| 582 | {0, 0}, |
| 583 | }; |
| 584 | |
| 585 | static PyType_Spec sslerror_type_spec = { |
| 586 | "ssl.SSLError", |
| 587 | sizeof(PyOSErrorObject), |
| 588 | 0, |
| 589 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 590 | sslerror_type_slots |
| 591 | }; |
| 592 | |
| 593 | static void |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 594 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 595 | const char *errstr, int lineno, unsigned long errcode) |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 596 | { |
| 597 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 598 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 599 | PyObject *init_value, *msg, *key; |
| 600 | _Py_IDENTIFIER(reason); |
| 601 | _Py_IDENTIFIER(library); |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 602 | _Py_IDENTIFIER(verify_message); |
| 603 | _Py_IDENTIFIER(verify_code); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 604 | |
| 605 | if (errcode != 0) { |
| 606 | int lib, reason; |
| 607 | |
| 608 | lib = ERR_GET_LIB(errcode); |
| 609 | reason = ERR_GET_REASON(errcode); |
| 610 | key = Py_BuildValue("ii", lib, reason); |
| 611 | if (key == NULL) |
| 612 | goto fail; |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 613 | reason_obj = PyDict_GetItemWithError(err_codes_to_names, key); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 614 | Py_DECREF(key); |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 615 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 616 | goto fail; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 617 | } |
| 618 | key = PyLong_FromLong(lib); |
| 619 | if (key == NULL) |
| 620 | goto fail; |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 621 | lib_obj = PyDict_GetItemWithError(lib_codes_to_names, key); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 622 | Py_DECREF(key); |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 623 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 624 | goto fail; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 625 | } |
| 626 | if (errstr == NULL) |
| 627 | errstr = ERR_reason_error_string(errcode); |
| 628 | } |
| 629 | if (errstr == NULL) |
| 630 | errstr = "unknown error"; |
| 631 | |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 632 | /* verify code for cert validation error */ |
| 633 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 634 | const char *verify_str = NULL; |
| 635 | long verify_code; |
| 636 | |
| 637 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 638 | verify_code_obj = PyLong_FromLong(verify_code); |
| 639 | if (verify_code_obj == NULL) { |
| 640 | goto fail; |
| 641 | } |
| 642 | |
| 643 | switch (verify_code) { |
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 644 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 645 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 646 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 647 | verify_obj = PyUnicode_FromFormat( |
| 648 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 649 | sslsock->server_hostname |
| 650 | ); |
| 651 | break; |
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 652 | #endif |
| 653 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 654 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 655 | verify_obj = PyUnicode_FromFormat( |
| 656 | "IP address mismatch, certificate is not valid for '%S'.", |
| 657 | sslsock->server_hostname |
| 658 | ); |
| 659 | break; |
| Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 660 | #endif |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 661 | default: |
| 662 | verify_str = X509_verify_cert_error_string(verify_code); |
| 663 | if (verify_str != NULL) { |
| 664 | verify_obj = PyUnicode_FromString(verify_str); |
| 665 | } else { |
| 666 | verify_obj = Py_None; |
| 667 | Py_INCREF(verify_obj); |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | if (verify_obj == NULL) { |
| 672 | goto fail; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | if (verify_obj && reason_obj && lib_obj) |
| 677 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 678 | lib_obj, reason_obj, errstr, verify_obj, |
| 679 | lineno); |
| 680 | else if (reason_obj && lib_obj) |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 681 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 682 | lib_obj, reason_obj, errstr, lineno); |
| 683 | else if (lib_obj) |
| 684 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 685 | lib_obj, errstr, lineno); |
| 686 | else |
| 687 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 688 | if (msg == NULL) |
| 689 | goto fail; |
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 690 | |
| Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 691 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 692 | if (init_value == NULL) |
| 693 | goto fail; |
| 694 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 695 | err_value = PyObject_CallObject(type, init_value); |
| 696 | Py_DECREF(init_value); |
| 697 | if (err_value == NULL) |
| 698 | goto fail; |
| Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 699 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 700 | if (reason_obj == NULL) |
| 701 | reason_obj = Py_None; |
| 702 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 703 | goto fail; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 704 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 705 | if (lib_obj == NULL) |
| 706 | lib_obj = Py_None; |
| 707 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 708 | goto fail; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 709 | |
| 710 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 711 | /* Only set verify code / message for SSLCertVerificationError */ |
| 712 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 713 | verify_code_obj)) |
| 714 | goto fail; |
| 715 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 716 | goto fail; |
| 717 | } |
| 718 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 719 | PyErr_SetObject(type, err_value); |
| 720 | fail: |
| 721 | Py_XDECREF(err_value); |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 722 | Py_XDECREF(verify_code_obj); |
| 723 | Py_XDECREF(verify_obj); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 724 | } |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 725 | |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 726 | static int |
| 727 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 728 | if (sslsock->exc_type == NULL) |
| 729 | return 0; |
| 730 | |
| 731 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 732 | sslsock->exc_type = NULL; |
| 733 | sslsock->exc_value = NULL; |
| 734 | sslsock->exc_tb = NULL; |
| 735 | return -1; |
| 736 | } |
| 737 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 738 | static PyObject * |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 739 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 740 | { |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 741 | PyObject *type = PySSLErrorObject; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 742 | char *errstr = NULL; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 743 | _PySSLError err; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 744 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 745 | unsigned long e = 0; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 746 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 747 | assert(ret <= 0); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 748 | e = ERR_peek_last_error(); |
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 749 | |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 750 | if (sslsock->ssl != NULL) { |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 751 | err = sslsock->err; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 752 | |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 753 | switch (err.ssl) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 754 | case SSL_ERROR_ZERO_RETURN: |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 755 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 756 | type = PySSLZeroReturnErrorObject; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 757 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 758 | break; |
| 759 | case SSL_ERROR_WANT_READ: |
| 760 | errstr = "The operation did not complete (read)"; |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 761 | type = PySSLWantReadErrorObject; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 762 | p = PY_SSL_ERROR_WANT_READ; |
| 763 | break; |
| 764 | case SSL_ERROR_WANT_WRITE: |
| 765 | p = PY_SSL_ERROR_WANT_WRITE; |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 766 | type = PySSLWantWriteErrorObject; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 767 | errstr = "The operation did not complete (write)"; |
| 768 | break; |
| 769 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 770 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 771 | errstr = "The operation did not complete (X509 lookup)"; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 772 | break; |
| 773 | case SSL_ERROR_WANT_CONNECT: |
| 774 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 775 | errstr = "The operation did not complete (connect)"; |
| 776 | break; |
| 777 | case SSL_ERROR_SYSCALL: |
| 778 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 779 | if (e == 0) { |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 780 | PySocketSockObject *s = GET_SOCKET(sslsock); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 781 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 782 | p = PY_SSL_ERROR_EOF; |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 783 | type = PySSLEOFErrorObject; |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 784 | errstr = "EOF occurred in violation of protocol"; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 785 | } else if (s && ret == -1) { |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 786 | /* underlying BIO reported an I/O error */ |
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 787 | ERR_clear_error(); |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 788 | #ifdef MS_WINDOWS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 789 | if (err.ws) { |
| 790 | return PyErr_SetFromWindowsErr(err.ws); |
| 791 | } |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 792 | #endif |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 793 | if (err.c) { |
| 794 | errno = err.c; |
| Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 795 | return PyErr_SetFromErrno(PyExc_OSError); |
| 796 | } |
| 797 | Py_INCREF(s); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 798 | s->errorhandler(); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 799 | Py_DECREF(s); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 800 | return NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 801 | } else { /* possible? */ |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 802 | p = PY_SSL_ERROR_SYSCALL; |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 803 | type = PySSLSyscallErrorObject; |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 804 | errstr = "Some I/O error occurred"; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 805 | } |
| 806 | } else { |
| 807 | p = PY_SSL_ERROR_SYSCALL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 808 | } |
| 809 | break; |
| 810 | } |
| 811 | case SSL_ERROR_SSL: |
| 812 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 813 | p = PY_SSL_ERROR_SSL; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 814 | if (e == 0) { |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 815 | /* possible? */ |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 816 | errstr = "A failure in the SSL library occurred"; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 817 | } |
| 818 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 819 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
| 820 | type = PySSLCertVerificationErrorObject; |
| 821 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | default: |
| 825 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 826 | errstr = "Invalid error code"; |
| 827 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 828 | } |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 829 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); |
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 830 | ERR_clear_error(); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 831 | PySSL_ChainExceptions(sslsock); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 832 | return NULL; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 835 | static PyObject * |
| Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 836 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 837 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 838 | if (errstr == NULL) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 839 | errcode = ERR_peek_last_error(); |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 840 | else |
| 841 | errcode = 0; |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 842 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); |
| Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 843 | ERR_clear_error(); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 844 | return NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 847 | /* |
| 848 | * SSL objects |
| 849 | */ |
| 850 | |
| 851 | static int |
| 852 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 853 | { |
| 854 | int retval = -1; |
| 855 | ASN1_OCTET_STRING *ip; |
| 856 | PyObject *hostname; |
| 857 | size_t len; |
| 858 | |
| 859 | assert(server_hostname); |
| 860 | |
| 861 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 862 | * When name starts with a dot (e.g ".example.com"), it will be |
| 863 | * matched by a certificate valid for any sub-domain of name. |
| 864 | */ |
| 865 | len = strlen(server_hostname); |
| 866 | if (len == 0 || *server_hostname == '.') { |
| 867 | PyErr_SetString( |
| 868 | PyExc_ValueError, |
| 869 | "server_hostname cannot be an empty string or start with a " |
| 870 | "leading dot."); |
| 871 | return retval; |
| 872 | } |
| 873 | |
| 874 | /* inet_pton is not available on all platforms. */ |
| 875 | ip = a2i_IPADDRESS(server_hostname); |
| 876 | if (ip == NULL) { |
| 877 | ERR_clear_error(); |
| 878 | } |
| 879 | |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 880 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 881 | if (hostname == NULL) { |
| 882 | goto error; |
| 883 | } |
| 884 | self->server_hostname = hostname; |
| 885 | |
| 886 | /* Only send SNI extension for non-IP hostnames */ |
| 887 | if (ip == NULL) { |
| 888 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
| 889 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 890 | } |
| 891 | } |
| 892 | if (self->ctx->check_hostname) { |
| 893 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 894 | if (ip == NULL) { |
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 895 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 896 | strlen(server_hostname))) { |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 897 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 898 | goto error; |
| 899 | } |
| 900 | } else { |
| 901 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip), |
| 902 | ASN1_STRING_length(ip))) { |
| 903 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 904 | goto error; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | retval = 0; |
| 909 | error: |
| 910 | if (ip != NULL) { |
| 911 | ASN1_OCTET_STRING_free(ip); |
| 912 | } |
| 913 | return retval; |
| 914 | } |
| 915 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 916 | static PySSLSocket * |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 917 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 918 | enum py_ssl_server_or_client socket_type, |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 919 | char *server_hostname, |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 920 | PyObject *owner, PyObject *session, |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 921 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 922 | { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 923 | PySSLSocket *self; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 924 | SSL_CTX *ctx = sslctx->ctx; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 925 | _PySSLError err = { 0 }; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 926 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 927 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 928 | if (self == NULL) |
| 929 | return NULL; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 930 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 931 | self->ssl = NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 932 | self->Socket = NULL; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 933 | self->ctx = sslctx; |
| Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 934 | Py_INCREF(sslctx); |
| Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 935 | self->shutdown_seen_zero = 0; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 936 | self->owner = NULL; |
| Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 937 | self->server_hostname = NULL; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 938 | self->err = err; |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 939 | self->exc_type = NULL; |
| 940 | self->exc_value = NULL; |
| 941 | self->exc_tb = NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 942 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 943 | /* Make sure the SSL error state is initialized */ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 944 | ERR_clear_error(); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 945 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 946 | PySSL_BEGIN_ALLOW_THREADS |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 947 | self->ssl = SSL_new(ctx); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 948 | PySSL_END_ALLOW_THREADS |
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 949 | if (self->ssl == NULL) { |
| 950 | Py_DECREF(self); |
| 951 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 952 | return NULL; |
| 953 | } |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 954 | SSL_set_app_data(self->ssl, self); |
| 955 | if (sock) { |
| 956 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 957 | } else { |
| 958 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 959 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 960 | * extra reference here. */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 961 | BIO_up_ref(inbio->bio); |
| 962 | BIO_up_ref(outbio->bio); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 963 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 964 | } |
| Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 965 | SSL_set_mode(self->ssl, |
| 966 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
| Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 967 | |
| Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 968 | #ifdef TLS1_3_VERSION |
| 969 | if (sslctx->post_handshake_auth == 1) { |
| 970 | if (socket_type == PY_SSL_SERVER) { |
| 971 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 972 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 973 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 974 | int mode = SSL_get_verify_mode(self->ssl); |
| 975 | if (mode & SSL_VERIFY_PEER) { |
| 976 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 977 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 978 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 979 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 980 | } |
| 981 | } else { |
| 982 | /* client socket */ |
| 983 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 984 | } |
| 985 | } |
| 986 | #endif |
| 987 | |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 988 | if (server_hostname != NULL) { |
| 989 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 990 | Py_DECREF(self); |
| 991 | return NULL; |
| 992 | } |
| 993 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 994 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 995 | * to non-blocking mode (blocking is the default) |
| 996 | */ |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 997 | if (sock && sock->sock_timeout >= 0) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 998 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 999 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 1000 | } |
| Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1001 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1002 | PySSL_BEGIN_ALLOW_THREADS |
| 1003 | if (socket_type == PY_SSL_CLIENT) |
| 1004 | SSL_set_connect_state(self->ssl); |
| 1005 | else |
| 1006 | SSL_set_accept_state(self->ssl); |
| 1007 | PySSL_END_ALLOW_THREADS |
| Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 1008 | |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1009 | self->socket_type = socket_type; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1010 | if (sock != NULL) { |
| 1011 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 1012 | if (self->Socket == NULL) { |
| 1013 | Py_DECREF(self); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1014 | return NULL; |
| 1015 | } |
| Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 1016 | } |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1017 | if (owner && owner != Py_None) { |
| 1018 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 1019 | Py_DECREF(self); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | } |
| 1023 | if (session && session != Py_None) { |
| 1024 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 1025 | Py_DECREF(self); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1029 | return self; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1032 | /* SSL object methods */ |
| 1033 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1034 | /*[clinic input] |
| 1035 | _ssl._SSLSocket.do_handshake |
| 1036 | [clinic start generated code]*/ |
| 1037 | |
| 1038 | static PyObject * |
| 1039 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 1040 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1041 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1042 | int ret; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1043 | _PySSLError err; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1044 | int sockstate, nonblocking; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1045 | PySocketSockObject *sock = GET_SOCKET(self); |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1046 | _PyTime_t timeout, deadline = 0; |
| 1047 | int has_timeout; |
| Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1048 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1049 | if (sock) { |
| 1050 | if (((PyObject*)sock) == Py_None) { |
| 1051 | _setSSLError("Underlying socket connection gone", |
| 1052 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | Py_INCREF(sock); |
| 1056 | |
| 1057 | /* just in case the blocking state of the socket has been changed */ |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1058 | nonblocking = (sock->sock_timeout >= 0); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1059 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1060 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1061 | } |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1062 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1063 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 1064 | has_timeout = (timeout > 0); |
| 1065 | if (has_timeout) |
| 1066 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 1067 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1068 | /* Actually negotiate SSL connection */ |
| 1069 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1070 | do { |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1071 | PySSL_BEGIN_ALLOW_THREADS |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1072 | ret = SSL_do_handshake(self->ssl); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1073 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1074 | PySSL_END_ALLOW_THREADS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1075 | self->err = err; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1076 | |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1077 | if (PyErr_CheckSignals()) |
| 1078 | goto error; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1079 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1080 | if (has_timeout) |
| 1081 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 1082 | |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1083 | if (err.ssl == SSL_ERROR_WANT_READ) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1084 | sockstate = PySSL_select(sock, 0, timeout); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1085 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1086 | sockstate = PySSL_select(sock, 1, timeout); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1087 | } else { |
| 1088 | sockstate = SOCKET_OPERATION_OK; |
| 1089 | } |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1090 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1091 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1092 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1093 | ERRSTR("The handshake operation timed out")); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1094 | goto error; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1095 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1096 | PyErr_SetString(PySSLErrorObject, |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1097 | ERRSTR("Underlying socket has been closed.")); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1098 | goto error; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1099 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1100 | PyErr_SetString(PySSLErrorObject, |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1101 | ERRSTR("Underlying socket too large for select().")); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1102 | goto error; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1103 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1104 | break; |
| 1105 | } |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1106 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 1107 | err.ssl == SSL_ERROR_WANT_WRITE); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1108 | Py_XDECREF(sock); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1109 | if (ret < 1) |
| 1110 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1111 | if (PySSL_ChainExceptions(self) < 0) |
| 1112 | return NULL; |
| Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1113 | Py_RETURN_NONE; |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1114 | error: |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1115 | Py_XDECREF(sock); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1116 | PySSL_ChainExceptions(self); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1117 | return NULL; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1120 | static PyObject * |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1121 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 1122 | { |
| 1123 | char buf[X509_NAME_MAXLEN]; |
| 1124 | char *namebuf = buf; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1125 | int buflen; |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1126 | PyObject *name_obj = NULL; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1127 | |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1128 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1129 | if (buflen < 0) { |
| 1130 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1131 | return NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1132 | } |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1133 | /* initial buffer is too small for oid + terminating null byte */ |
| 1134 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1135 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1136 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1137 | /* allocate len + 1 for terminating NULL byte */ |
| 1138 | namebuf = PyMem_Malloc(buflen + 1); |
| 1139 | if (namebuf == NULL) { |
| 1140 | PyErr_NoMemory(); |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1144 | if (buflen < 0) { |
| 1145 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1146 | goto done; |
| 1147 | } |
| 1148 | } |
| 1149 | if (!buflen && no_name) { |
| 1150 | Py_INCREF(Py_None); |
| 1151 | name_obj = Py_None; |
| 1152 | } |
| 1153 | else { |
| 1154 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1155 | } |
| 1156 | |
| 1157 | done: |
| 1158 | if (buf != namebuf) { |
| 1159 | PyMem_Free(namebuf); |
| 1160 | } |
| 1161 | return name_obj; |
| 1162 | } |
| 1163 | |
| 1164 | static PyObject * |
| 1165 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 1166 | { |
| 1167 | Py_ssize_t buflen; |
| 1168 | unsigned char *valuebuf = NULL; |
| 1169 | PyObject *attr; |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1170 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1171 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1172 | if (buflen < 0) { |
| 1173 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1174 | return NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1175 | } |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1176 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1177 | OPENSSL_free(valuebuf); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1178 | return attr; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | static PyObject * |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1182 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1183 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1184 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1185 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1186 | PyObject *rdnt; |
| 1187 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1188 | int entry_count = X509_NAME_entry_count(xname); |
| 1189 | X509_NAME_ENTRY *entry; |
| 1190 | ASN1_OBJECT *name; |
| 1191 | ASN1_STRING *value; |
| 1192 | int index_counter; |
| 1193 | int rdn_level = -1; |
| 1194 | int retcode; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1195 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1196 | dn = PyList_New(0); |
| 1197 | if (dn == NULL) |
| 1198 | return NULL; |
| 1199 | /* now create another tuple to hold the top-level RDN */ |
| 1200 | rdn = PyList_New(0); |
| 1201 | if (rdn == NULL) |
| 1202 | goto fail0; |
| 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 | for (index_counter = 0; |
| 1205 | index_counter < entry_count; |
| 1206 | index_counter++) |
| 1207 | { |
| 1208 | entry = X509_NAME_get_entry(xname, index_counter); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1209 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1210 | /* check to see if we've gotten to a new RDN */ |
| 1211 | if (rdn_level >= 0) { |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1212 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1213 | /* yes, new RDN */ |
| 1214 | /* add old RDN to DN */ |
| 1215 | rdnt = PyList_AsTuple(rdn); |
| 1216 | Py_DECREF(rdn); |
| 1217 | if (rdnt == NULL) |
| 1218 | goto fail0; |
| 1219 | retcode = PyList_Append(dn, rdnt); |
| 1220 | Py_DECREF(rdnt); |
| 1221 | if (retcode < 0) |
| 1222 | goto fail0; |
| 1223 | /* create new RDN */ |
| 1224 | rdn = PyList_New(0); |
| 1225 | if (rdn == NULL) |
| 1226 | goto fail0; |
| 1227 | } |
| 1228 | } |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1229 | rdn_level = X509_NAME_ENTRY_set(entry); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1230 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1231 | /* now add this attribute to the current RDN */ |
| 1232 | name = X509_NAME_ENTRY_get_object(entry); |
| 1233 | value = X509_NAME_ENTRY_get_data(entry); |
| 1234 | attr = _create_tuple_for_attribute(name, value); |
| 1235 | /* |
| 1236 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1237 | entry->set, |
| 1238 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1239 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1240 | */ |
| 1241 | if (attr == NULL) |
| 1242 | goto fail1; |
| 1243 | retcode = PyList_Append(rdn, attr); |
| 1244 | Py_DECREF(attr); |
| 1245 | if (retcode < 0) |
| 1246 | goto fail1; |
| 1247 | } |
| 1248 | /* now, there's typically a dangling RDN */ |
| Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1249 | if (rdn != NULL) { |
| 1250 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1251 | rdnt = PyList_AsTuple(rdn); |
| 1252 | Py_DECREF(rdn); |
| 1253 | if (rdnt == NULL) |
| 1254 | goto fail0; |
| 1255 | retcode = PyList_Append(dn, rdnt); |
| 1256 | Py_DECREF(rdnt); |
| 1257 | if (retcode < 0) |
| 1258 | goto fail0; |
| 1259 | } |
| 1260 | else { |
| 1261 | Py_DECREF(rdn); |
| 1262 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1263 | } |
| 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 | /* convert list to tuple */ |
| 1266 | rdnt = PyList_AsTuple(dn); |
| 1267 | Py_DECREF(dn); |
| 1268 | if (rdnt == NULL) |
| 1269 | return NULL; |
| 1270 | return rdnt; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1271 | |
| 1272 | fail1: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1273 | Py_XDECREF(rdn); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1274 | |
| 1275 | fail0: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1276 | Py_XDECREF(dn); |
| 1277 | return NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static PyObject * |
| 1281 | _get_peer_alt_names (X509 *certificate) { |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1282 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1283 | /* this code follows the procedure outlined in |
| 1284 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1285 | function to extract the STACK_OF(GENERAL_NAME), |
| 1286 | then iterates through the stack to add the |
| 1287 | names. */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1288 | |
| Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1289 | int j; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1290 | PyObject *peer_alt_names = Py_None; |
| Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1291 | PyObject *v = NULL, *t; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1292 | GENERAL_NAMES *names = NULL; |
| 1293 | GENERAL_NAME *name; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1294 | BIO *biobuf = NULL; |
| 1295 | char buf[2048]; |
| 1296 | char *vptr; |
| 1297 | int len; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1298 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1299 | if (certificate == NULL) |
| 1300 | return peer_alt_names; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1301 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1302 | /* get a memory buffer */ |
| 1303 | biobuf = BIO_new(BIO_s_mem()); |
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1304 | if (biobuf == NULL) { |
| 1305 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1306 | return NULL; |
| 1307 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1308 | |
| Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1309 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1310 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1311 | if (names != NULL) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1312 | if (peer_alt_names == Py_None) { |
| 1313 | peer_alt_names = PyList_New(0); |
| 1314 | if (peer_alt_names == NULL) |
| 1315 | goto fail; |
| 1316 | } |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1317 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1318 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1319 | /* get a rendering of each name in the set of names */ |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1320 | int gntype; |
| 1321 | ASN1_STRING *as = NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1322 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1323 | name = sk_GENERAL_NAME_value(names, j); |
| Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1324 | gntype = name->type; |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1325 | switch (gntype) { |
| 1326 | case GEN_DIRNAME: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1327 | /* we special-case DirName as a tuple of |
| 1328 | tuples of attributes */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1329 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1330 | t = PyTuple_New(2); |
| 1331 | if (t == NULL) { |
| 1332 | goto fail; |
| 1333 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1334 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1335 | v = PyUnicode_FromString("DirName"); |
| 1336 | if (v == NULL) { |
| 1337 | Py_DECREF(t); |
| 1338 | goto fail; |
| 1339 | } |
| 1340 | PyTuple_SET_ITEM(t, 0, v); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1341 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1342 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1343 | if (v == NULL) { |
| 1344 | Py_DECREF(t); |
| 1345 | goto fail; |
| 1346 | } |
| 1347 | PyTuple_SET_ITEM(t, 1, v); |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1348 | break; |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1349 | |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1350 | case GEN_EMAIL: |
| 1351 | case GEN_DNS: |
| 1352 | case GEN_URI: |
| 1353 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1354 | correctly, CVE-2013-4238 */ |
| 1355 | t = PyTuple_New(2); |
| 1356 | if (t == NULL) |
| 1357 | goto fail; |
| 1358 | switch (gntype) { |
| 1359 | case GEN_EMAIL: |
| 1360 | v = PyUnicode_FromString("email"); |
| 1361 | as = name->d.rfc822Name; |
| 1362 | break; |
| 1363 | case GEN_DNS: |
| 1364 | v = PyUnicode_FromString("DNS"); |
| 1365 | as = name->d.dNSName; |
| 1366 | break; |
| 1367 | case GEN_URI: |
| 1368 | v = PyUnicode_FromString("URI"); |
| 1369 | as = name->d.uniformResourceIdentifier; |
| 1370 | break; |
| 1371 | } |
| 1372 | if (v == NULL) { |
| 1373 | Py_DECREF(t); |
| 1374 | goto fail; |
| 1375 | } |
| 1376 | PyTuple_SET_ITEM(t, 0, v); |
| 1377 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as), |
| 1378 | ASN1_STRING_length(as)); |
| 1379 | if (v == NULL) { |
| 1380 | Py_DECREF(t); |
| 1381 | goto fail; |
| 1382 | } |
| 1383 | PyTuple_SET_ITEM(t, 1, v); |
| 1384 | break; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1385 | |
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1386 | case GEN_RID: |
| 1387 | t = PyTuple_New(2); |
| 1388 | if (t == NULL) |
| 1389 | goto fail; |
| 1390 | |
| 1391 | v = PyUnicode_FromString("Registered ID"); |
| 1392 | if (v == NULL) { |
| 1393 | Py_DECREF(t); |
| 1394 | goto fail; |
| 1395 | } |
| 1396 | PyTuple_SET_ITEM(t, 0, v); |
| 1397 | |
| 1398 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1399 | if (len < 0) { |
| 1400 | Py_DECREF(t); |
| 1401 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1402 | goto fail; |
| 1403 | } else if (len >= (int)sizeof(buf)) { |
| 1404 | v = PyUnicode_FromString("<INVALID>"); |
| 1405 | } else { |
| 1406 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1407 | } |
| 1408 | if (v == NULL) { |
| 1409 | Py_DECREF(t); |
| 1410 | goto fail; |
| 1411 | } |
| 1412 | PyTuple_SET_ITEM(t, 1, v); |
| 1413 | break; |
| 1414 | |
| Christian Heimes | 2b7de66 | 2019-12-07 17:59:36 +0100 | [diff] [blame] | 1415 | case GEN_IPADD: |
| 1416 | /* OpenSSL < 3.0.0 adds a trailing \n to IPv6. 3.0.0 removed |
| 1417 | * the trailing newline. Remove it in all versions |
| 1418 | */ |
| 1419 | t = PyTuple_New(2); |
| 1420 | if (t == NULL) |
| 1421 | goto fail; |
| 1422 | |
| 1423 | v = PyUnicode_FromString("IP Address"); |
| 1424 | if (v == NULL) { |
| 1425 | Py_DECREF(t); |
| 1426 | goto fail; |
| 1427 | } |
| 1428 | PyTuple_SET_ITEM(t, 0, v); |
| 1429 | |
| 1430 | if (name->d.ip->length == 4) { |
| 1431 | unsigned char *p = name->d.ip->data; |
| 1432 | v = PyUnicode_FromFormat( |
| 1433 | "%d.%d.%d.%d", |
| 1434 | p[0], p[1], p[2], p[3] |
| 1435 | ); |
| 1436 | } else if (name->d.ip->length == 16) { |
| 1437 | /* PyUnicode_FromFormat() does not support %X */ |
| 1438 | unsigned char *p = name->d.ip->data; |
| 1439 | len = sprintf( |
| 1440 | buf, |
| 1441 | "%X:%X:%X:%X:%X:%X:%X:%X", |
| 1442 | p[0] << 8 | p[1], |
| 1443 | p[2] << 8 | p[3], |
| 1444 | p[4] << 8 | p[5], |
| 1445 | p[6] << 8 | p[7], |
| 1446 | p[8] << 8 | p[9], |
| 1447 | p[10] << 8 | p[11], |
| 1448 | p[12] << 8 | p[13], |
| 1449 | p[14] << 8 | p[15] |
| 1450 | ); |
| 1451 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1452 | } else { |
| 1453 | v = PyUnicode_FromString("<invalid>"); |
| 1454 | } |
| 1455 | |
| 1456 | if (v == NULL) { |
| 1457 | Py_DECREF(t); |
| 1458 | goto fail; |
| 1459 | } |
| 1460 | PyTuple_SET_ITEM(t, 1, v); |
| 1461 | break; |
| 1462 | |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1463 | default: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1464 | /* for everything else, we use the OpenSSL print form */ |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1465 | switch (gntype) { |
| 1466 | /* check for new general name type */ |
| 1467 | case GEN_OTHERNAME: |
| 1468 | case GEN_X400: |
| 1469 | case GEN_EDIPARTY: |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1470 | case GEN_RID: |
| 1471 | break; |
| 1472 | default: |
| 1473 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1474 | "Unknown general name type %d", |
| 1475 | gntype) == -1) { |
| 1476 | goto fail; |
| 1477 | } |
| 1478 | break; |
| 1479 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1480 | (void) BIO_reset(biobuf); |
| 1481 | GENERAL_NAME_print(biobuf, name); |
| 1482 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1483 | if (len < 0) { |
| 1484 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1485 | goto fail; |
| 1486 | } |
| 1487 | vptr = strchr(buf, ':'); |
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1488 | if (vptr == NULL) { |
| 1489 | PyErr_Format(PyExc_ValueError, |
| 1490 | "Invalid value %.200s", |
| 1491 | buf); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1492 | goto fail; |
| Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1493 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1494 | t = PyTuple_New(2); |
| 1495 | if (t == NULL) |
| 1496 | goto fail; |
| 1497 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1498 | if (v == NULL) { |
| 1499 | Py_DECREF(t); |
| 1500 | goto fail; |
| 1501 | } |
| 1502 | PyTuple_SET_ITEM(t, 0, v); |
| 1503 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1504 | (len - (vptr - buf + 1))); |
| 1505 | if (v == NULL) { |
| 1506 | Py_DECREF(t); |
| 1507 | goto fail; |
| 1508 | } |
| 1509 | PyTuple_SET_ITEM(t, 1, v); |
| Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1510 | break; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1511 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1512 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1513 | /* and add that rendering to the list */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1514 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1515 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1516 | Py_DECREF(t); |
| 1517 | goto fail; |
| 1518 | } |
| 1519 | Py_DECREF(t); |
| 1520 | } |
| Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1521 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1522 | } |
| 1523 | BIO_free(biobuf); |
| 1524 | if (peer_alt_names != Py_None) { |
| 1525 | v = PyList_AsTuple(peer_alt_names); |
| 1526 | Py_DECREF(peer_alt_names); |
| 1527 | return v; |
| 1528 | } else { |
| 1529 | return peer_alt_names; |
| 1530 | } |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1531 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1532 | |
| 1533 | fail: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1534 | if (biobuf != NULL) |
| 1535 | BIO_free(biobuf); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1536 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1537 | if (peer_alt_names != Py_None) { |
| 1538 | Py_XDECREF(peer_alt_names); |
| 1539 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1540 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1541 | return NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static PyObject * |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1545 | _get_aia_uri(X509 *certificate, int nid) { |
| 1546 | PyObject *lst = NULL, *ostr = NULL; |
| 1547 | int i, result; |
| 1548 | AUTHORITY_INFO_ACCESS *info; |
| 1549 | |
| 1550 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
| Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1551 | if (info == NULL) |
| 1552 | return Py_None; |
| 1553 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1554 | AUTHORITY_INFO_ACCESS_free(info); |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1555 | return Py_None; |
| 1556 | } |
| 1557 | |
| 1558 | if ((lst = PyList_New(0)) == NULL) { |
| 1559 | goto fail; |
| 1560 | } |
| 1561 | |
| 1562 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1563 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1564 | ASN1_IA5STRING *uri; |
| 1565 | |
| 1566 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1567 | (ad->location->type != GEN_URI)) { |
| 1568 | continue; |
| 1569 | } |
| 1570 | uri = ad->location->d.uniformResourceIdentifier; |
| 1571 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1572 | uri->length); |
| 1573 | if (ostr == NULL) { |
| 1574 | goto fail; |
| 1575 | } |
| 1576 | result = PyList_Append(lst, ostr); |
| 1577 | Py_DECREF(ostr); |
| 1578 | if (result < 0) { |
| 1579 | goto fail; |
| 1580 | } |
| 1581 | } |
| 1582 | AUTHORITY_INFO_ACCESS_free(info); |
| 1583 | |
| 1584 | /* convert to tuple or None */ |
| 1585 | if (PyList_Size(lst) == 0) { |
| 1586 | Py_DECREF(lst); |
| 1587 | return Py_None; |
| 1588 | } else { |
| 1589 | PyObject *tup; |
| 1590 | tup = PyList_AsTuple(lst); |
| 1591 | Py_DECREF(lst); |
| 1592 | return tup; |
| 1593 | } |
| 1594 | |
| 1595 | fail: |
| 1596 | AUTHORITY_INFO_ACCESS_free(info); |
| Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1597 | Py_XDECREF(lst); |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1598 | return NULL; |
| 1599 | } |
| 1600 | |
| 1601 | static PyObject * |
| 1602 | _get_crl_dp(X509 *certificate) { |
| 1603 | STACK_OF(DIST_POINT) *dps; |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1604 | int i, j; |
| 1605 | PyObject *lst, *res = NULL; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1606 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1607 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
| Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1608 | |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1609 | if (dps == NULL) |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1610 | return Py_None; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1611 | |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1612 | lst = PyList_New(0); |
| 1613 | if (lst == NULL) |
| 1614 | goto done; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1615 | |
| 1616 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1617 | DIST_POINT *dp; |
| 1618 | STACK_OF(GENERAL_NAME) *gns; |
| 1619 | |
| 1620 | dp = sk_DIST_POINT_value(dps, i); |
| Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1621 | if (dp->distpoint == NULL) { |
| 1622 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1623 | continue; |
| 1624 | } |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1625 | gns = dp->distpoint->name.fullname; |
| 1626 | |
| 1627 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1628 | GENERAL_NAME *gn; |
| 1629 | ASN1_IA5STRING *uri; |
| 1630 | PyObject *ouri; |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1631 | int err; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1632 | |
| 1633 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1634 | if (gn->type != GEN_URI) { |
| 1635 | continue; |
| 1636 | } |
| 1637 | uri = gn->d.uniformResourceIdentifier; |
| 1638 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1639 | uri->length); |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1640 | if (ouri == NULL) |
| 1641 | goto done; |
| 1642 | |
| 1643 | err = PyList_Append(lst, ouri); |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1644 | Py_DECREF(ouri); |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1645 | if (err < 0) |
| 1646 | goto done; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1647 | } |
| 1648 | } |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1649 | |
| 1650 | /* Convert to tuple. */ |
| 1651 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1652 | |
| 1653 | done: |
| 1654 | Py_XDECREF(lst); |
| Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1655 | CRL_DIST_POINTS_free(dps); |
| Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1656 | return res; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | static PyObject * |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1660 | _decode_certificate(X509 *certificate) { |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1661 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1662 | PyObject *retval = NULL; |
| 1663 | BIO *biobuf = NULL; |
| 1664 | PyObject *peer; |
| 1665 | PyObject *peer_alt_names = NULL; |
| 1666 | PyObject *issuer; |
| 1667 | PyObject *version; |
| 1668 | PyObject *sn_obj; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1669 | PyObject *obj; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1670 | ASN1_INTEGER *serialNumber; |
| 1671 | char buf[2048]; |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1672 | int len, result; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1673 | ASN1_TIME *notBefore, *notAfter; |
| 1674 | PyObject *pnotBefore, *pnotAfter; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1675 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1676 | retval = PyDict_New(); |
| 1677 | if (retval == NULL) |
| 1678 | return NULL; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1679 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1680 | peer = _create_tuple_for_X509_NAME( |
| 1681 | X509_get_subject_name(certificate)); |
| 1682 | if (peer == NULL) |
| 1683 | goto fail0; |
| 1684 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1685 | Py_DECREF(peer); |
| 1686 | goto fail0; |
| 1687 | } |
| 1688 | Py_DECREF(peer); |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1689 | |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1690 | issuer = _create_tuple_for_X509_NAME( |
| 1691 | X509_get_issuer_name(certificate)); |
| 1692 | if (issuer == NULL) |
| 1693 | goto fail0; |
| 1694 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1695 | Py_DECREF(issuer); |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1696 | goto fail0; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1697 | } |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1698 | Py_DECREF(issuer); |
| 1699 | |
| 1700 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
| Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1701 | if (version == NULL) |
| 1702 | goto fail0; |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1703 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1704 | Py_DECREF(version); |
| 1705 | goto fail0; |
| 1706 | } |
| 1707 | Py_DECREF(version); |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1708 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1709 | /* get a memory buffer */ |
| 1710 | biobuf = BIO_new(BIO_s_mem()); |
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1711 | if (biobuf == NULL) { |
| 1712 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1713 | goto fail0; |
| 1714 | } |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1715 | |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1716 | (void) BIO_reset(biobuf); |
| 1717 | serialNumber = X509_get_serialNumber(certificate); |
| 1718 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1719 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1720 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1721 | if (len < 0) { |
| 1722 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1723 | goto fail1; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1724 | } |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1725 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1726 | if (sn_obj == NULL) |
| 1727 | goto fail1; |
| 1728 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1729 | Py_DECREF(sn_obj); |
| 1730 | goto fail1; |
| 1731 | } |
| 1732 | Py_DECREF(sn_obj); |
| 1733 | |
| 1734 | (void) BIO_reset(biobuf); |
| 1735 | notBefore = X509_get_notBefore(certificate); |
| 1736 | ASN1_TIME_print(biobuf, notBefore); |
| 1737 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1738 | if (len < 0) { |
| 1739 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1740 | goto fail1; |
| 1741 | } |
| 1742 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1743 | if (pnotBefore == NULL) |
| 1744 | goto fail1; |
| 1745 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1746 | Py_DECREF(pnotBefore); |
| 1747 | goto fail1; |
| 1748 | } |
| 1749 | Py_DECREF(pnotBefore); |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1750 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1751 | (void) BIO_reset(biobuf); |
| 1752 | notAfter = X509_get_notAfter(certificate); |
| 1753 | ASN1_TIME_print(biobuf, notAfter); |
| 1754 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1755 | if (len < 0) { |
| 1756 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1757 | goto fail1; |
| 1758 | } |
| 1759 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1760 | if (pnotAfter == NULL) |
| 1761 | goto fail1; |
| 1762 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1763 | Py_DECREF(pnotAfter); |
| 1764 | goto fail1; |
| 1765 | } |
| 1766 | Py_DECREF(pnotAfter); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1767 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1768 | /* Now look for subjectAltName */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1769 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1770 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1771 | if (peer_alt_names == NULL) |
| 1772 | goto fail1; |
| 1773 | else if (peer_alt_names != Py_None) { |
| 1774 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1775 | peer_alt_names) < 0) { |
| 1776 | Py_DECREF(peer_alt_names); |
| 1777 | goto fail1; |
| 1778 | } |
| 1779 | Py_DECREF(peer_alt_names); |
| 1780 | } |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1781 | |
| Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1782 | /* Authority Information Access: OCSP URIs */ |
| 1783 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1784 | if (obj == NULL) { |
| 1785 | goto fail1; |
| 1786 | } else if (obj != Py_None) { |
| 1787 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1788 | Py_DECREF(obj); |
| 1789 | if (result < 0) { |
| 1790 | goto fail1; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1795 | if (obj == NULL) { |
| 1796 | goto fail1; |
| 1797 | } else if (obj != Py_None) { |
| 1798 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1799 | Py_DECREF(obj); |
| 1800 | if (result < 0) { |
| 1801 | goto fail1; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | /* CDP (CRL distribution points) */ |
| 1806 | obj = _get_crl_dp(certificate); |
| 1807 | if (obj == NULL) { |
| 1808 | goto fail1; |
| 1809 | } else if (obj != Py_None) { |
| 1810 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1811 | Py_DECREF(obj); |
| 1812 | if (result < 0) { |
| 1813 | goto fail1; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1817 | BIO_free(biobuf); |
| 1818 | return retval; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1819 | |
| 1820 | fail1: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1821 | if (biobuf != NULL) |
| 1822 | BIO_free(biobuf); |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1823 | fail0: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1824 | Py_XDECREF(retval); |
| 1825 | return NULL; |
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1826 | } |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1827 | |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1828 | static PyObject * |
| 1829 | _certificate_to_der(X509 *certificate) |
| 1830 | { |
| 1831 | unsigned char *bytes_buf = NULL; |
| 1832 | int len; |
| 1833 | PyObject *retval; |
| 1834 | |
| 1835 | bytes_buf = NULL; |
| 1836 | len = i2d_X509(certificate, &bytes_buf); |
| 1837 | if (len < 0) { |
| 1838 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1839 | return NULL; |
| 1840 | } |
| 1841 | /* this is actually an immutable bytes sequence */ |
| 1842 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1843 | OPENSSL_free(bytes_buf); |
| 1844 | return retval; |
| 1845 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1846 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1847 | /*[clinic input] |
| 1848 | _ssl._test_decode_cert |
| 1849 | path: object(converter="PyUnicode_FSConverter") |
| 1850 | / |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1851 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1852 | [clinic start generated code]*/ |
| 1853 | |
| 1854 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1855 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1856 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1857 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1858 | PyObject *retval = NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1859 | X509 *x=NULL; |
| 1860 | BIO *cert; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1861 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1862 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1863 | PyErr_SetString(PySSLErrorObject, |
| 1864 | "Can't malloc memory to read file"); |
| 1865 | goto fail0; |
| 1866 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1867 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1868 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1869 | PyErr_SetString(PySSLErrorObject, |
| 1870 | "Can't open file"); |
| 1871 | goto fail0; |
| 1872 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1873 | |
| Alex Gaynor | 40dad95 | 2019-08-15 08:31:28 -0400 | [diff] [blame] | 1874 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1875 | if (x == NULL) { |
| 1876 | PyErr_SetString(PySSLErrorObject, |
| 1877 | "Error decoding PEM-encoded file"); |
| 1878 | goto fail0; |
| 1879 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1880 | |
| Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1881 | retval = _decode_certificate(x); |
| Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1882 | X509_free(x); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1883 | |
| 1884 | fail0: |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1885 | Py_DECREF(path); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1886 | if (cert != NULL) BIO_free(cert); |
| 1887 | return retval; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1891 | /*[clinic input] |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1892 | _ssl._SSLSocket.getpeercert |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1893 | der as binary_mode: bool = False |
| 1894 | / |
| 1895 | |
| 1896 | Returns the certificate for the peer. |
| 1897 | |
| 1898 | If no certificate was provided, returns None. If a certificate was |
| 1899 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1900 | returns a dict containing information about the peer certificate. |
| 1901 | |
| 1902 | If the optional argument is True, returns a DER-encoded copy of the |
| 1903 | peer certificate, or None if no certificate was provided. This will |
| 1904 | return the certificate even if it wasn't validated. |
| 1905 | [clinic start generated code]*/ |
| 1906 | |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1907 | static PyObject * |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1908 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1909 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1910 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1911 | int verification; |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1912 | X509 *peer_cert; |
| 1913 | PyObject *result; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1914 | |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1915 | if (!SSL_is_init_finished(self->ssl)) { |
| Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1916 | PyErr_SetString(PyExc_ValueError, |
| 1917 | "handshake not done yet"); |
| 1918 | return NULL; |
| 1919 | } |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1920 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1921 | if (peer_cert == NULL) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1922 | Py_RETURN_NONE; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1923 | |
| Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1924 | if (binary_mode) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1925 | /* return cert in DER-encoded format */ |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1926 | result = _certificate_to_der(peer_cert); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1927 | } else { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1928 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1929 | if ((verification & SSL_VERIFY_PEER) == 0) |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1930 | result = PyDict_New(); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1931 | else |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1932 | result = _decode_certificate(peer_cert); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1933 | } |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1934 | X509_free(peer_cert); |
| 1935 | return result; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1938 | static PyObject * |
| 1939 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1940 | { |
| 1941 | const char *cipher_name, *cipher_protocol; |
| 1942 | PyObject *v, *retval = PyTuple_New(3); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1943 | if (retval == NULL) |
| 1944 | return NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1945 | |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1946 | cipher_name = SSL_CIPHER_get_name(cipher); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1947 | if (cipher_name == NULL) { |
| Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1948 | Py_INCREF(Py_None); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1949 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1950 | } else { |
| 1951 | v = PyUnicode_FromString(cipher_name); |
| 1952 | if (v == NULL) |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1953 | goto fail; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1954 | PyTuple_SET_ITEM(retval, 0, v); |
| 1955 | } |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1956 | |
| 1957 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1958 | if (cipher_protocol == NULL) { |
| Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1959 | Py_INCREF(Py_None); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1960 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1961 | } else { |
| 1962 | v = PyUnicode_FromString(cipher_protocol); |
| 1963 | if (v == NULL) |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1964 | goto fail; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1965 | PyTuple_SET_ITEM(retval, 1, v); |
| 1966 | } |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1967 | |
| 1968 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1969 | if (v == NULL) |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1970 | goto fail; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1971 | PyTuple_SET_ITEM(retval, 2, v); |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1972 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1973 | return retval; |
| Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1974 | |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1975 | fail: |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1976 | Py_DECREF(retval); |
| 1977 | return NULL; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1980 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1981 | static PyObject * |
| 1982 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1983 | { |
| 1984 | const char *cipher_name, *cipher_protocol; |
| 1985 | |
| 1986 | unsigned long cipher_id; |
| 1987 | int alg_bits, strength_bits, len; |
| 1988 | char buf[512] = {0}; |
| 1989 | #if OPENSSL_VERSION_1_1 |
| 1990 | int aead, nid; |
| 1991 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 1992 | #endif |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1993 | |
| 1994 | /* can be NULL */ |
| 1995 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1996 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1997 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1998 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1999 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 2000 | len = (int)strlen(buf); |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2001 | if (len > 1 && buf[len-1] == '\n') |
| 2002 | buf[len-1] = '\0'; |
| 2003 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 2004 | |
| 2005 | #if OPENSSL_VERSION_1_1 |
| 2006 | aead = SSL_CIPHER_is_aead(cipher); |
| 2007 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 2008 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2009 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 2010 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2011 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 2012 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2013 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 2014 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 2015 | #endif |
| 2016 | |
| Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 2017 | return Py_BuildValue( |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2018 | "{sksssssssisi" |
| 2019 | #if OPENSSL_VERSION_1_1 |
| 2020 | "sOssssssss" |
| 2021 | #endif |
| 2022 | "}", |
| 2023 | "id", cipher_id, |
| 2024 | "name", cipher_name, |
| 2025 | "protocol", cipher_protocol, |
| 2026 | "description", buf, |
| 2027 | "strength_bits", strength_bits, |
| 2028 | "alg_bits", alg_bits |
| 2029 | #if OPENSSL_VERSION_1_1 |
| 2030 | ,"aead", aead ? Py_True : Py_False, |
| 2031 | "symmetric", skcipher, |
| 2032 | "digest", digest, |
| 2033 | "kea", kx, |
| 2034 | "auth", auth |
| 2035 | #endif |
| 2036 | ); |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2037 | } |
| 2038 | #endif |
| 2039 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2040 | /*[clinic input] |
| 2041 | _ssl._SSLSocket.shared_ciphers |
| 2042 | [clinic start generated code]*/ |
| 2043 | |
| 2044 | static PyObject * |
| 2045 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 2046 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2047 | { |
| 2048 | STACK_OF(SSL_CIPHER) *ciphers; |
| 2049 | int i; |
| 2050 | PyObject *res; |
| 2051 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2052 | ciphers = SSL_get_ciphers(self->ssl); |
| 2053 | if (!ciphers) |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2054 | Py_RETURN_NONE; |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2055 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 2056 | if (!res) |
| 2057 | return NULL; |
| 2058 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 2059 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 2060 | if (!tup) { |
| 2061 | Py_DECREF(res); |
| 2062 | return NULL; |
| 2063 | } |
| 2064 | PyList_SET_ITEM(res, i, tup); |
| 2065 | } |
| 2066 | return res; |
| 2067 | } |
| 2068 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2069 | /*[clinic input] |
| 2070 | _ssl._SSLSocket.cipher |
| 2071 | [clinic start generated code]*/ |
| 2072 | |
| 2073 | static PyObject * |
| 2074 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 2075 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
| Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2076 | { |
| 2077 | const SSL_CIPHER *current; |
| 2078 | |
| 2079 | if (self->ssl == NULL) |
| 2080 | Py_RETURN_NONE; |
| 2081 | current = SSL_get_current_cipher(self->ssl); |
| 2082 | if (current == NULL) |
| 2083 | Py_RETURN_NONE; |
| 2084 | return cipher_to_tuple(current); |
| 2085 | } |
| 2086 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2087 | /*[clinic input] |
| 2088 | _ssl._SSLSocket.version |
| 2089 | [clinic start generated code]*/ |
| 2090 | |
| 2091 | static PyObject * |
| 2092 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 2093 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
| Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2094 | { |
| 2095 | const char *version; |
| 2096 | |
| 2097 | if (self->ssl == NULL) |
| 2098 | Py_RETURN_NONE; |
| Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 2099 | if (!SSL_is_init_finished(self->ssl)) { |
| 2100 | /* handshake not finished */ |
| 2101 | Py_RETURN_NONE; |
| 2102 | } |
| Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2103 | version = SSL_get_version(self->ssl); |
| 2104 | if (!strcmp(version, "unknown")) |
| 2105 | Py_RETURN_NONE; |
| 2106 | return PyUnicode_FromString(version); |
| 2107 | } |
| 2108 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2109 | #if HAVE_NPN |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2110 | /*[clinic input] |
| 2111 | _ssl._SSLSocket.selected_npn_protocol |
| 2112 | [clinic start generated code]*/ |
| 2113 | |
| 2114 | static PyObject * |
| 2115 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 2116 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 2117 | { |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2118 | const unsigned char *out; |
| 2119 | unsigned int outlen; |
| 2120 | |
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2121 | SSL_get0_next_proto_negotiated(self->ssl, |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2122 | &out, &outlen); |
| 2123 | |
| 2124 | if (out == NULL) |
| 2125 | Py_RETURN_NONE; |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2126 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 2127 | } |
| 2128 | #endif |
| 2129 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2130 | #if HAVE_ALPN |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2131 | /*[clinic input] |
| 2132 | _ssl._SSLSocket.selected_alpn_protocol |
| 2133 | [clinic start generated code]*/ |
| 2134 | |
| 2135 | static PyObject * |
| 2136 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2137 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2138 | { |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2139 | const unsigned char *out; |
| 2140 | unsigned int outlen; |
| 2141 | |
| 2142 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2143 | |
| 2144 | if (out == NULL) |
| 2145 | Py_RETURN_NONE; |
| 2146 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2147 | } |
| 2148 | #endif |
| 2149 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2150 | /*[clinic input] |
| 2151 | _ssl._SSLSocket.compression |
| 2152 | [clinic start generated code]*/ |
| 2153 | |
| 2154 | static PyObject * |
| 2155 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2156 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2157 | { |
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2158 | #ifdef OPENSSL_NO_COMP |
| 2159 | Py_RETURN_NONE; |
| 2160 | #else |
| 2161 | const COMP_METHOD *comp_method; |
| 2162 | const char *short_name; |
| 2163 | |
| 2164 | if (self->ssl == NULL) |
| 2165 | Py_RETURN_NONE; |
| 2166 | comp_method = SSL_get_current_compression(self->ssl); |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2167 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2168 | Py_RETURN_NONE; |
| Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2169 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2170 | if (short_name == NULL) |
| 2171 | Py_RETURN_NONE; |
| 2172 | return PyUnicode_DecodeFSDefault(short_name); |
| 2173 | #endif |
| 2174 | } |
| 2175 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2176 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2177 | Py_INCREF(self->ctx); |
| 2178 | return self->ctx; |
| 2179 | } |
| 2180 | |
| 2181 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2182 | void *closure) { |
| 2183 | |
| 2184 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2185 | #if !HAVE_SNI |
| 2186 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 2187 | "context is not supported by your OpenSSL library"); |
| Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2188 | return -1; |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2189 | #else |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2190 | Py_INCREF(value); |
| Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2191 | Py_SETREF(self->ctx, (PySSLContext *)value); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2192 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2193 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2194 | } else { |
| Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2195 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2196 | return -1; |
| 2197 | } |
| 2198 | |
| 2199 | return 0; |
| 2200 | } |
| 2201 | |
| 2202 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2203 | "_setter_context(ctx)\n\ |
| 2204 | \ |
| 2205 | This changes the context associated with the SSLSocket. This is typically\n\ |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2206 | used from within a callback function set by the sni_callback\n\ |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2207 | on the SSLContext to change the certificate information associated with the\n\ |
| 2208 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2209 | |
| 2210 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2211 | static PyObject * |
| 2212 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2213 | { |
| 2214 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2215 | } |
| 2216 | |
| 2217 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2218 | "Whether this is a server-side socket."); |
| 2219 | |
| 2220 | static PyObject * |
| 2221 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2222 | { |
| 2223 | if (self->server_hostname == NULL) |
| 2224 | Py_RETURN_NONE; |
| 2225 | Py_INCREF(self->server_hostname); |
| 2226 | return self->server_hostname; |
| 2227 | } |
| 2228 | |
| 2229 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2230 | "The currently set server hostname (for SNI)."); |
| 2231 | |
| 2232 | static PyObject * |
| 2233 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2234 | { |
| 2235 | PyObject *owner; |
| 2236 | |
| 2237 | if (self->owner == NULL) |
| 2238 | Py_RETURN_NONE; |
| 2239 | |
| 2240 | owner = PyWeakref_GetObject(self->owner); |
| 2241 | Py_INCREF(owner); |
| 2242 | return owner; |
| 2243 | } |
| 2244 | |
| 2245 | static int |
| 2246 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2247 | { |
| Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2248 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2249 | if (self->owner == NULL) |
| 2250 | return -1; |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2255 | "The Python-level owner of this object.\ |
| 2256 | Passed as \"self\" in servername callback."); |
| 2257 | |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2258 | static int |
| 2259 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2260 | { |
| 2261 | Py_VISIT(self->exc_type); |
| 2262 | Py_VISIT(self->exc_value); |
| 2263 | Py_VISIT(self->exc_tb); |
| 2264 | return 0; |
| 2265 | } |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2266 | |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2267 | static int |
| 2268 | PySSL_clear(PySSLSocket *self) |
| 2269 | { |
| 2270 | Py_CLEAR(self->exc_type); |
| 2271 | Py_CLEAR(self->exc_value); |
| 2272 | Py_CLEAR(self->exc_tb); |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | static void |
| 2277 | PySSL_dealloc(PySSLSocket *self) |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2278 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2279 | if (self->ssl) |
| 2280 | SSL_free(self->ssl); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2281 | Py_XDECREF(self->Socket); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2282 | Py_XDECREF(self->ctx); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2283 | Py_XDECREF(self->server_hostname); |
| 2284 | Py_XDECREF(self->owner); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2285 | PyObject_Del(self); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2288 | /* 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] | 2289 | The argument writing indicates the direction. |
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2290 | Returns one of the possibilities in the timeout_state enum (above). |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2291 | */ |
| Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2292 | |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2293 | static int |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2294 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2295 | { |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2296 | int rc; |
| 2297 | #ifdef HAVE_POLL |
| 2298 | struct pollfd pollfd; |
| 2299 | _PyTime_t ms; |
| 2300 | #else |
| 2301 | int nfds; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2302 | fd_set fds; |
| 2303 | struct timeval tv; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2304 | #endif |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2305 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2306 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2307 | if ((s == NULL) || (timeout == 0)) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2308 | return SOCKET_IS_NONBLOCKING; |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2309 | else if (timeout < 0) { |
| 2310 | if (s->sock_timeout > 0) |
| 2311 | return SOCKET_HAS_TIMED_OUT; |
| 2312 | else |
| 2313 | return SOCKET_IS_BLOCKING; |
| 2314 | } |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2315 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2316 | /* Guard against closed socket */ |
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2317 | if (s->sock_fd == INVALID_SOCKET) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2318 | return SOCKET_HAS_BEEN_CLOSED; |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2319 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2320 | /* Prefer poll, if available, since you can poll() any fd |
| 2321 | * which can't be done with select(). */ |
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2322 | #ifdef HAVE_POLL |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2323 | pollfd.fd = s->sock_fd; |
| 2324 | pollfd.events = writing ? POLLOUT : POLLIN; |
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2325 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2326 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2327 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2328 | assert(ms <= INT_MAX); |
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2329 | |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2330 | PySSL_BEGIN_ALLOW_THREADS |
| 2331 | rc = poll(&pollfd, 1, (int)ms); |
| 2332 | PySSL_END_ALLOW_THREADS |
| 2333 | #else |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2334 | /* Guard against socket too large for select*/ |
| Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2335 | if (!_PyIsSelectable_fd(s->sock_fd)) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2336 | return SOCKET_TOO_LARGE_FOR_SELECT; |
| Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2337 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2338 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2339 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2340 | FD_ZERO(&fds); |
| 2341 | FD_SET(s->sock_fd, &fds); |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2342 | |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2343 | /* Wait until the socket becomes ready */ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2344 | PySSL_BEGIN_ALLOW_THREADS |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2345 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2346 | if (writing) |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2347 | rc = select(nfds, NULL, &fds, NULL, &tv); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2348 | else |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2349 | rc = select(nfds, &fds, NULL, NULL, &tv); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2350 | PySSL_END_ALLOW_THREADS |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2351 | #endif |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2352 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2353 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2354 | (when we are able to write or when there's something to read) */ |
| 2355 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
| Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2358 | /*[clinic input] |
| 2359 | _ssl._SSLSocket.write |
| 2360 | b: Py_buffer |
| 2361 | / |
| 2362 | |
| 2363 | Writes the bytes-like object b into the SSL object. |
| 2364 | |
| 2365 | Returns the number of bytes written. |
| 2366 | [clinic start generated code]*/ |
| 2367 | |
| 2368 | static PyObject * |
| 2369 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2370 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2371 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2372 | int len; |
| 2373 | int sockstate; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2374 | _PySSLError err; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2375 | int nonblocking; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2376 | PySocketSockObject *sock = GET_SOCKET(self); |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2377 | _PyTime_t timeout, deadline = 0; |
| 2378 | int has_timeout; |
| Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2379 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2380 | if (sock != NULL) { |
| 2381 | if (((PyObject*)sock) == Py_None) { |
| 2382 | _setSSLError("Underlying socket connection gone", |
| 2383 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2384 | return NULL; |
| 2385 | } |
| 2386 | Py_INCREF(sock); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2389 | if (b->len > INT_MAX) { |
| Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2390 | PyErr_Format(PyExc_OverflowError, |
| 2391 | "string longer than %d bytes", INT_MAX); |
| 2392 | goto error; |
| 2393 | } |
| 2394 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2395 | if (sock != NULL) { |
| 2396 | /* just in case the blocking state of the socket has been changed */ |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2397 | nonblocking = (sock->sock_timeout >= 0); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2398 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2399 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2400 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2401 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2402 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2403 | has_timeout = (timeout > 0); |
| 2404 | if (has_timeout) |
| 2405 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2406 | |
| 2407 | sockstate = PySSL_select(sock, 1, timeout); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2408 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2409 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2410 | "The write operation timed out"); |
| 2411 | goto error; |
| 2412 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2413 | PyErr_SetString(PySSLErrorObject, |
| 2414 | "Underlying socket has been closed."); |
| 2415 | goto error; |
| 2416 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2417 | PyErr_SetString(PySSLErrorObject, |
| 2418 | "Underlying socket too large for select()."); |
| 2419 | goto error; |
| 2420 | } |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2421 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2422 | do { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2423 | PySSL_BEGIN_ALLOW_THREADS |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2424 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2425 | err = _PySSL_errno(len <= 0, self->ssl, len); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2426 | PySSL_END_ALLOW_THREADS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2427 | self->err = err; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2428 | |
| 2429 | if (PyErr_CheckSignals()) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2430 | goto error; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2431 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2432 | if (has_timeout) |
| 2433 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2434 | |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2435 | if (err.ssl == SSL_ERROR_WANT_READ) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2436 | sockstate = PySSL_select(sock, 0, timeout); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2437 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2438 | sockstate = PySSL_select(sock, 1, timeout); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2439 | } else { |
| 2440 | sockstate = SOCKET_OPERATION_OK; |
| 2441 | } |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2442 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2443 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2444 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2445 | "The write operation timed out"); |
| 2446 | goto error; |
| 2447 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2448 | PyErr_SetString(PySSLErrorObject, |
| 2449 | "Underlying socket has been closed."); |
| 2450 | goto error; |
| 2451 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2452 | break; |
| 2453 | } |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2454 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2455 | err.ssl == SSL_ERROR_WANT_WRITE); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2456 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2457 | Py_XDECREF(sock); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2458 | if (len <= 0) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2459 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2460 | if (PySSL_ChainExceptions(self) < 0) |
| 2461 | return NULL; |
| 2462 | return PyLong_FromLong(len); |
| Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2463 | error: |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2464 | Py_XDECREF(sock); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2465 | PySSL_ChainExceptions(self); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2466 | return NULL; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2469 | /*[clinic input] |
| 2470 | _ssl._SSLSocket.pending |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2471 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2472 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2473 | [clinic start generated code]*/ |
| 2474 | |
| 2475 | static PyObject * |
| 2476 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2477 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2478 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2479 | int count = 0; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2480 | _PySSLError err; |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2481 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2482 | PySSL_BEGIN_ALLOW_THREADS |
| 2483 | count = SSL_pending(self->ssl); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2484 | err = _PySSL_errno(count < 0, self->ssl, count); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2485 | PySSL_END_ALLOW_THREADS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2486 | self->err = err; |
| 2487 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2488 | if (count < 0) |
| 2489 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2490 | else |
| 2491 | return PyLong_FromLong(count); |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2494 | /*[clinic input] |
| 2495 | _ssl._SSLSocket.read |
| 2496 | size as len: int |
| 2497 | [ |
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2498 | buffer: Py_buffer(accept={rwbuffer}) |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2499 | ] |
| 2500 | / |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2501 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2502 | Read up to size bytes from the SSL socket. |
| 2503 | [clinic start generated code]*/ |
| 2504 | |
| 2505 | static PyObject * |
| 2506 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2507 | Py_buffer *buffer) |
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2508 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2509 | { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2510 | PyObject *dest = NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2511 | char *mem; |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2512 | int count; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2513 | int sockstate; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2514 | _PySSLError err; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2515 | int nonblocking; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2516 | PySocketSockObject *sock = GET_SOCKET(self); |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2517 | _PyTime_t timeout, deadline = 0; |
| 2518 | int has_timeout; |
| Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2519 | |
| Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2520 | if (!group_right_1 && len < 0) { |
| 2521 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2522 | return NULL; |
| 2523 | } |
| 2524 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2525 | if (sock != NULL) { |
| 2526 | if (((PyObject*)sock) == Py_None) { |
| 2527 | _setSSLError("Underlying socket connection gone", |
| 2528 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2529 | return NULL; |
| 2530 | } |
| 2531 | Py_INCREF(sock); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2534 | if (!group_right_1) { |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2535 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2536 | if (dest == NULL) |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2537 | goto error; |
| Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2538 | if (len == 0) { |
| 2539 | Py_XDECREF(sock); |
| 2540 | return dest; |
| 2541 | } |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2542 | mem = PyBytes_AS_STRING(dest); |
| 2543 | } |
| 2544 | else { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2545 | mem = buffer->buf; |
| 2546 | if (len <= 0 || len > buffer->len) { |
| 2547 | len = (int) buffer->len; |
| 2548 | if (buffer->len != len) { |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2549 | PyErr_SetString(PyExc_OverflowError, |
| 2550 | "maximum length can't fit in a C 'int'"); |
| 2551 | goto error; |
| 2552 | } |
| Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2553 | if (len == 0) { |
| 2554 | count = 0; |
| 2555 | goto done; |
| 2556 | } |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2557 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2560 | if (sock != NULL) { |
| 2561 | /* just in case the blocking state of the socket has been changed */ |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2562 | nonblocking = (sock->sock_timeout >= 0); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2563 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2564 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2565 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2566 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2567 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2568 | has_timeout = (timeout > 0); |
| 2569 | if (has_timeout) |
| 2570 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2571 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2572 | do { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2573 | PySSL_BEGIN_ALLOW_THREADS |
| 2574 | count = SSL_read(self->ssl, mem, len); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2575 | err = _PySSL_errno(count <= 0, self->ssl, count); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2576 | PySSL_END_ALLOW_THREADS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2577 | self->err = err; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2578 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2579 | if (PyErr_CheckSignals()) |
| 2580 | goto error; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2581 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2582 | if (has_timeout) |
| 2583 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2584 | |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2585 | if (err.ssl == SSL_ERROR_WANT_READ) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2586 | sockstate = PySSL_select(sock, 0, timeout); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2587 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2588 | sockstate = PySSL_select(sock, 1, timeout); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2589 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2590 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2591 | { |
| 2592 | count = 0; |
| 2593 | goto done; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2594 | } |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2595 | else |
| 2596 | sockstate = SOCKET_OPERATION_OK; |
| 2597 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2598 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2599 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2600 | "The read operation timed out"); |
| 2601 | goto error; |
| 2602 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2603 | break; |
| 2604 | } |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2605 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2606 | err.ssl == SSL_ERROR_WANT_WRITE); |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2607 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2608 | if (count <= 0) { |
| 2609 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2610 | goto error; |
| 2611 | } |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2612 | if (self->exc_type != NULL) |
| 2613 | goto error; |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2614 | |
| 2615 | done: |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2616 | Py_XDECREF(sock); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2617 | if (!group_right_1) { |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2618 | _PyBytes_Resize(&dest, count); |
| 2619 | return dest; |
| 2620 | } |
| 2621 | else { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2622 | return PyLong_FromLong(count); |
| 2623 | } |
| Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2624 | |
| 2625 | error: |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2626 | PySSL_ChainExceptions(self); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2627 | Py_XDECREF(sock); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2628 | if (!group_right_1) |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2629 | Py_XDECREF(dest); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2630 | return NULL; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2633 | /*[clinic input] |
| 2634 | _ssl._SSLSocket.shutdown |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2635 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2636 | Does the SSL shutdown handshake with the remote end. |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2637 | [clinic start generated code]*/ |
| 2638 | |
| 2639 | static PyObject * |
| 2640 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2641 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2642 | { |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2643 | _PySSLError err; |
| 2644 | int sockstate, nonblocking, ret; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2645 | int zeros = 0; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2646 | PySocketSockObject *sock = GET_SOCKET(self); |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2647 | _PyTime_t timeout, deadline = 0; |
| 2648 | int has_timeout; |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2649 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2650 | if (sock != NULL) { |
| 2651 | /* Guard against closed socket */ |
| Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2652 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2653 | _setSSLError("Underlying socket connection gone", |
| 2654 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2655 | return NULL; |
| 2656 | } |
| 2657 | Py_INCREF(sock); |
| 2658 | |
| 2659 | /* Just in case the blocking state of the socket has been changed */ |
| Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2660 | nonblocking = (sock->sock_timeout >= 0); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2661 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2662 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2663 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2664 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2665 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2666 | has_timeout = (timeout > 0); |
| 2667 | if (has_timeout) |
| 2668 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2669 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2670 | while (1) { |
| 2671 | PySSL_BEGIN_ALLOW_THREADS |
| 2672 | /* Disable read-ahead so that unwrap can work correctly. |
| 2673 | * Otherwise OpenSSL might read in too much data, |
| 2674 | * eating clear text data that happens to be |
| 2675 | * transmitted after the SSL shutdown. |
| Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2676 | * Should be safe to call repeatedly every time this |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2677 | * function is used and the shutdown_seen_zero != 0 |
| 2678 | * condition is met. |
| 2679 | */ |
| 2680 | if (self->shutdown_seen_zero) |
| 2681 | SSL_set_read_ahead(self->ssl, 0); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2682 | ret = SSL_shutdown(self->ssl); |
| 2683 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2684 | PySSL_END_ALLOW_THREADS |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2685 | self->err = err; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2686 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2687 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2688 | if (ret > 0) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2689 | break; |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2690 | if (ret == 0) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2691 | /* Don't loop endlessly; instead preserve legacy |
| 2692 | behaviour of trying SSL_shutdown() only twice. |
| 2693 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2694 | if (++zeros > 1) |
| 2695 | break; |
| 2696 | /* Shutdown was sent, now try receiving */ |
| 2697 | self->shutdown_seen_zero = 1; |
| 2698 | continue; |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2701 | if (has_timeout) |
| 2702 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2703 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2704 | /* Possibly retry shutdown until timeout or failure */ |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2705 | if (err.ssl == SSL_ERROR_WANT_READ) |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2706 | sockstate = PySSL_select(sock, 0, timeout); |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2707 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
| Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2708 | sockstate = PySSL_select(sock, 1, timeout); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2709 | else |
| 2710 | break; |
| Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2711 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2712 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2713 | if (err.ssl == SSL_ERROR_WANT_READ) |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2714 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2715 | "The read operation timed out"); |
| 2716 | else |
| Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2717 | PyErr_SetString(PySocketModule.timeout_error, |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2718 | "The write operation timed out"); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2719 | goto error; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2720 | } |
| 2721 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2722 | PyErr_SetString(PySSLErrorObject, |
| 2723 | "Underlying socket too large for select()."); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2724 | goto error; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2725 | } |
| 2726 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2727 | /* Retain the SSL error code */ |
| 2728 | break; |
| 2729 | } |
| Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2730 | if (ret < 0) { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2731 | Py_XDECREF(sock); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2732 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2733 | return NULL; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2734 | } |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2735 | if (self->exc_type != NULL) |
| 2736 | goto error; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2737 | if (sock) |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2738 | /* It's already INCREF'ed */ |
| 2739 | return (PyObject *) sock; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2740 | else |
| 2741 | Py_RETURN_NONE; |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2742 | |
| 2743 | error: |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2744 | Py_XDECREF(sock); |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2745 | PySSL_ChainExceptions(self); |
| Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2746 | return NULL; |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2749 | /*[clinic input] |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2750 | _ssl._SSLSocket.get_channel_binding |
| 2751 | cb_type: str = "tls-unique" |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2752 | |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2753 | Get channel binding data for current connection. |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2754 | |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2755 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2756 | of the data or None if the data is not available (e.g. before the handshake). |
| 2757 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2758 | [clinic start generated code]*/ |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2759 | |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2760 | static PyObject * |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2761 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2762 | const char *cb_type) |
| 2763 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2764 | { |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2765 | char buf[PySSL_CB_MAXLEN]; |
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2766 | size_t len; |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2767 | |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2768 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2769 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2770 | /* if session is resumed XOR we are the client */ |
| 2771 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2772 | } |
| 2773 | else { |
| 2774 | /* if a new session XOR we are the server */ |
| 2775 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2776 | } |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2777 | } |
| 2778 | else { |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2779 | PyErr_Format( |
| 2780 | PyExc_ValueError, |
| 2781 | "'%s' channel binding type not implemented", |
| 2782 | cb_type |
| 2783 | ); |
| 2784 | return NULL; |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2788 | if (len == 0) |
| 2789 | Py_RETURN_NONE; |
| 2790 | |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2791 | return PyBytes_FromStringAndSize(buf, len); |
| Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2792 | } |
| 2793 | |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2794 | /*[clinic input] |
| 2795 | _ssl._SSLSocket.verify_client_post_handshake |
| 2796 | |
| 2797 | Initiate TLS 1.3 post-handshake authentication |
| 2798 | [clinic start generated code]*/ |
| 2799 | |
| 2800 | static PyObject * |
| 2801 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2802 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2803 | { |
| 2804 | #ifdef TLS1_3_VERSION |
| 2805 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2806 | if (err == 0) |
| 2807 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2808 | else |
| 2809 | Py_RETURN_NONE; |
| 2810 | #else |
| 2811 | PyErr_SetString(PyExc_NotImplementedError, |
| 2812 | "Post-handshake auth is not supported by your " |
| 2813 | "OpenSSL version."); |
| 2814 | return NULL; |
| 2815 | #endif |
| 2816 | } |
| 2817 | |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2818 | #ifdef OPENSSL_VERSION_1_1 |
| 2819 | |
| 2820 | static SSL_SESSION* |
| 2821 | _ssl_session_dup(SSL_SESSION *session) { |
| 2822 | SSL_SESSION *newsession = NULL; |
| 2823 | int slen; |
| 2824 | unsigned char *senc = NULL, *p; |
| 2825 | const unsigned char *const_p; |
| 2826 | |
| 2827 | if (session == NULL) { |
| 2828 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2829 | goto error; |
| 2830 | } |
| 2831 | |
| 2832 | /* get length */ |
| 2833 | slen = i2d_SSL_SESSION(session, NULL); |
| 2834 | if (slen == 0 || slen > 0xFF00) { |
| 2835 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2836 | goto error; |
| 2837 | } |
| 2838 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2839 | PyErr_NoMemory(); |
| 2840 | goto error; |
| 2841 | } |
| 2842 | p = senc; |
| 2843 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2844 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2845 | goto error; |
| 2846 | } |
| 2847 | const_p = senc; |
| 2848 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2849 | if (session == NULL) { |
| 2850 | goto error; |
| 2851 | } |
| 2852 | PyMem_Free(senc); |
| 2853 | return newsession; |
| 2854 | error: |
| 2855 | if (senc != NULL) { |
| 2856 | PyMem_Free(senc); |
| 2857 | } |
| 2858 | return NULL; |
| 2859 | } |
| 2860 | #endif |
| 2861 | |
| 2862 | static PyObject * |
| 2863 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2864 | /* get_session can return sessions from a server-side connection, |
| 2865 | * it does not check for handshake done or client socket. */ |
| 2866 | PySSLSession *pysess; |
| 2867 | SSL_SESSION *session; |
| 2868 | |
| 2869 | #ifdef OPENSSL_VERSION_1_1 |
| 2870 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2871 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2872 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2873 | if (session == NULL) { |
| 2874 | Py_RETURN_NONE; |
| 2875 | } |
| 2876 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2877 | return NULL; |
| 2878 | } |
| 2879 | #else |
| 2880 | session = SSL_get1_session(self->ssl); |
| 2881 | if (session == NULL) { |
| 2882 | Py_RETURN_NONE; |
| 2883 | } |
| 2884 | #endif |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2885 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2886 | if (pysess == NULL) { |
| 2887 | SSL_SESSION_free(session); |
| 2888 | return NULL; |
| 2889 | } |
| 2890 | |
| 2891 | assert(self->ctx); |
| 2892 | pysess->ctx = self->ctx; |
| 2893 | Py_INCREF(pysess->ctx); |
| 2894 | pysess->session = session; |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2895 | PyObject_GC_Track(pysess); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2896 | return (PyObject *)pysess; |
| 2897 | } |
| 2898 | |
| 2899 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2900 | void *closure) |
| 2901 | { |
| 2902 | PySSLSession *pysess; |
| 2903 | #ifdef OPENSSL_VERSION_1_1 |
| 2904 | SSL_SESSION *session; |
| 2905 | #endif |
| 2906 | int result; |
| 2907 | |
| 2908 | if (!PySSLSession_Check(value)) { |
| Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2909 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2910 | return -1; |
| 2911 | } |
| 2912 | pysess = (PySSLSession *)value; |
| 2913 | |
| 2914 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2915 | PyErr_SetString(PyExc_ValueError, |
| 2916 | "Session refers to a different SSLContext."); |
| 2917 | return -1; |
| 2918 | } |
| 2919 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2920 | PyErr_SetString(PyExc_ValueError, |
| 2921 | "Cannot set session for server-side SSLSocket."); |
| 2922 | return -1; |
| 2923 | } |
| Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2924 | if (SSL_is_init_finished(self->ssl)) { |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2925 | PyErr_SetString(PyExc_ValueError, |
| 2926 | "Cannot set session after handshake."); |
| 2927 | return -1; |
| 2928 | } |
| 2929 | #ifdef OPENSSL_VERSION_1_1 |
| 2930 | /* duplicate session */ |
| 2931 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2932 | return -1; |
| 2933 | } |
| 2934 | result = SSL_set_session(self->ssl, session); |
| 2935 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2936 | SSL_SESSION_free(session); |
| 2937 | #else |
| 2938 | result = SSL_set_session(self->ssl, pysess->session); |
| 2939 | #endif |
| 2940 | if (result == 0) { |
| 2941 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2942 | return -1; |
| 2943 | } |
| 2944 | return 0; |
| 2945 | } |
| 2946 | |
| 2947 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2948 | "_setter_session(session)\n\ |
| 2949 | \ |
| 2950 | Get / set SSLSession."); |
| 2951 | |
| 2952 | static PyObject * |
| 2953 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2954 | if (SSL_session_reused(self->ssl)) { |
| 2955 | Py_RETURN_TRUE; |
| 2956 | } else { |
| 2957 | Py_RETURN_FALSE; |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2962 | "Was the client session reused during handshake?"); |
| 2963 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2964 | static PyGetSetDef ssl_getsetlist[] = { |
| 2965 | {"context", (getter) PySSL_get_context, |
| 2966 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2967 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2968 | PySSL_get_server_side_doc}, |
| 2969 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2970 | PySSL_get_server_hostname_doc}, |
| 2971 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2972 | PySSL_get_owner_doc}, |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2973 | {"session", (getter) PySSL_get_session, |
| 2974 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2975 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2976 | PySSL_get_session_reused_doc}, |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2977 | {NULL}, /* sentinel */ |
| 2978 | }; |
| 2979 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2980 | static PyMethodDef PySSLMethods[] = { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2981 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2982 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2983 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2984 | _SSL__SSLSOCKET_PENDING_METHODDEF |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2985 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2986 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2987 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2988 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2989 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 2990 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 2991 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2992 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2993 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2994 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2995 | {NULL, NULL} |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2996 | }; |
| 2997 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2998 | static PyTypeObject PySSLSocket_Type = { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2999 | PyVarObject_HEAD_INIT(NULL, 0) |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3000 | "_ssl._SSLSocket", /*tp_name*/ |
| 3001 | sizeof(PySSLSocket), /*tp_basicsize*/ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3002 | 0, /*tp_itemsize*/ |
| 3003 | /* methods */ |
| 3004 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3005 | 0, /*tp_vectorcall_offset*/ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3006 | 0, /*tp_getattr*/ |
| 3007 | 0, /*tp_setattr*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3008 | 0, /*tp_as_async*/ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3009 | 0, /*tp_repr*/ |
| 3010 | 0, /*tp_as_number*/ |
| 3011 | 0, /*tp_as_sequence*/ |
| 3012 | 0, /*tp_as_mapping*/ |
| 3013 | 0, /*tp_hash*/ |
| 3014 | 0, /*tp_call*/ |
| 3015 | 0, /*tp_str*/ |
| 3016 | 0, /*tp_getattro*/ |
| 3017 | 0, /*tp_setattro*/ |
| 3018 | 0, /*tp_as_buffer*/ |
| 3019 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3020 | 0, /*tp_doc*/ |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3021 | (traverseproc) PySSL_traverse, /*tp_traverse*/ |
| 3022 | (inquiry) PySSL_clear, /*tp_clear*/ |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 3023 | 0, /*tp_richcompare*/ |
| 3024 | 0, /*tp_weaklistoffset*/ |
| 3025 | 0, /*tp_iter*/ |
| 3026 | 0, /*tp_iternext*/ |
| 3027 | PySSLMethods, /*tp_methods*/ |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3028 | 0, /*tp_members*/ |
| 3029 | ssl_getsetlist, /*tp_getset*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3030 | }; |
| 3031 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3032 | |
| 3033 | /* |
| 3034 | * _SSLContext objects |
| 3035 | */ |
| 3036 | |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3037 | static int |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3038 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3039 | { |
| 3040 | int mode; |
| 3041 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 3042 | |
| 3043 | switch(n) { |
| 3044 | case PY_SSL_CERT_NONE: |
| 3045 | mode = SSL_VERIFY_NONE; |
| 3046 | break; |
| 3047 | case PY_SSL_CERT_OPTIONAL: |
| 3048 | mode = SSL_VERIFY_PEER; |
| 3049 | break; |
| 3050 | case PY_SSL_CERT_REQUIRED: |
| 3051 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3052 | break; |
| 3053 | default: |
| 3054 | PyErr_SetString(PyExc_ValueError, |
| 3055 | "invalid value for verify_mode"); |
| 3056 | return -1; |
| 3057 | } |
| Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3058 | |
| 3059 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3060 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 3061 | |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3062 | /* keep current verify cb */ |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3063 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 3064 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3065 | return 0; |
| 3066 | } |
| 3067 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3068 | /*[clinic input] |
| 3069 | @classmethod |
| 3070 | _ssl._SSLContext.__new__ |
| 3071 | protocol as proto_version: int |
| 3072 | / |
| 3073 | [clinic start generated code]*/ |
| 3074 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3075 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3076 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 3077 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3078 | { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3079 | PySSLContext *self; |
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3080 | long options; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3081 | SSL_CTX *ctx = NULL; |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3082 | X509_VERIFY_PARAM *params; |
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3083 | int result; |
| Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3084 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3085 | unsigned long libver; |
| Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3086 | #endif |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3087 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3088 | PySSL_BEGIN_ALLOW_THREADS |
| 3089 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 3090 | ctx = SSL_CTX_new(TLSv1_method()); |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 3091 | #if HAVE_TLSv1_2 |
| 3092 | else if (proto_version == PY_SSL_VERSION_TLS1_1) |
| 3093 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 3094 | else if (proto_version == PY_SSL_VERSION_TLS1_2) |
| 3095 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 3096 | #endif |
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3097 | #ifndef OPENSSL_NO_SSL3 |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3098 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 3099 | ctx = SSL_CTX_new(SSLv3_method()); |
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3100 | #endif |
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3101 | #ifndef OPENSSL_NO_SSL2 |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3102 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 3103 | ctx = SSL_CTX_new(SSLv2_method()); |
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3104 | #endif |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3105 | else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3106 | ctx = SSL_CTX_new(TLS_method()); |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3107 | else if (proto_version == PY_SSL_VERSION_TLS_CLIENT) |
| 3108 | ctx = SSL_CTX_new(TLS_client_method()); |
| 3109 | else if (proto_version == PY_SSL_VERSION_TLS_SERVER) |
| 3110 | ctx = SSL_CTX_new(TLS_server_method()); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3111 | else |
| 3112 | proto_version = -1; |
| 3113 | PySSL_END_ALLOW_THREADS |
| 3114 | |
| 3115 | if (proto_version == -1) { |
| 3116 | PyErr_SetString(PyExc_ValueError, |
| 3117 | "invalid protocol version"); |
| 3118 | return NULL; |
| 3119 | } |
| 3120 | if (ctx == NULL) { |
| Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 3121 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3122 | return NULL; |
| 3123 | } |
| 3124 | |
| 3125 | assert(type != NULL && type->tp_alloc != NULL); |
| 3126 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 3127 | if (self == NULL) { |
| 3128 | SSL_CTX_free(ctx); |
| 3129 | return NULL; |
| 3130 | } |
| 3131 | self->ctx = ctx; |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3132 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3133 | self->protocol = proto_version; |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3134 | self->msg_cb = NULL; |
| 3135 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3136 | self->keylog_filename = NULL; |
| 3137 | self->keylog_bio = NULL; |
| 3138 | #endif |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3139 | #if HAVE_NPN |
| Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 3140 | self->npn_protocols = NULL; |
| 3141 | #endif |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3142 | #if HAVE_ALPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3143 | self->alpn_protocols = NULL; |
| 3144 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3145 | #ifndef OPENSSL_NO_TLSEXT |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3146 | self->set_sni_cb = NULL; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3147 | #endif |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3148 | /* Don't check host name by default */ |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3149 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3150 | self->check_hostname = 1; |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3151 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3152 | Py_DECREF(self); |
| 3153 | return NULL; |
| 3154 | } |
| 3155 | } else { |
| 3156 | self->check_hostname = 0; |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3157 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3158 | Py_DECREF(self); |
| 3159 | return NULL; |
| 3160 | } |
| 3161 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3162 | /* Defaults */ |
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3163 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3164 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3165 | options |= SSL_OP_NO_SSLv2; |
| Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3166 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3167 | options |= SSL_OP_NO_SSLv3; |
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3168 | /* Minimal security flags for server and client side context. |
| 3169 | * Client sockets ignore server-side parameters. */ |
| 3170 | #ifdef SSL_OP_NO_COMPRESSION |
| 3171 | options |= SSL_OP_NO_COMPRESSION; |
| 3172 | #endif |
| 3173 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3174 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3175 | #endif |
| 3176 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3177 | options |= SSL_OP_SINGLE_DH_USE; |
| 3178 | #endif |
| 3179 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3180 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3181 | #endif |
| Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3182 | SSL_CTX_set_options(self->ctx, options); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3183 | |
| Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3184 | /* A bare minimum cipher list without completely broken cipher suites. |
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3185 | * It's far from perfect but gives users a better head start. */ |
| 3186 | if (proto_version != PY_SSL_VERSION_SSL2) { |
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3187 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3188 | /* stick to OpenSSL's default settings */ |
| 3189 | result = 1; |
| 3190 | #else |
| 3191 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3192 | #endif |
| Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3193 | } else { |
| 3194 | /* SSLv2 needs MD5 */ |
| 3195 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3196 | } |
| 3197 | if (result == 0) { |
| 3198 | Py_DECREF(self); |
| 3199 | ERR_clear_error(); |
| 3200 | PyErr_SetString(PySSLErrorObject, |
| 3201 | "No cipher can be selected."); |
| 3202 | return NULL; |
| 3203 | } |
| 3204 | |
| Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3205 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 3206 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 3207 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 3208 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 3209 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 3210 | conservative and assume it wasn't fixed until release. We do this check |
| 3211 | at runtime to avoid problems from the dynamic linker. |
| 3212 | See #25672 for more on this. */ |
| 3213 | libver = SSLeay(); |
| 3214 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 3215 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 3216 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 3217 | } |
| 3218 | #endif |
| 3219 | |
| 3220 | |
| Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3221 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
| Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3222 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 3223 | prime256v1 by default. This is Apache mod_ssl's initialization |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3224 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 3225 | */ |
| Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3226 | #if defined(SSL_CTX_set_ecdh_auto) |
| Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3227 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 3228 | #else |
| 3229 | { |
| 3230 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 3231 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3232 | EC_KEY_free(key); |
| 3233 | } |
| 3234 | #endif |
| 3235 | #endif |
| 3236 | |
| Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3237 | #define SID_CTX "Python" |
| 3238 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3239 | sizeof(SID_CTX)); |
| 3240 | #undef SID_CTX |
| 3241 | |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3242 | params = SSL_CTX_get0_param(self->ctx); |
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3243 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3244 | /* Improve trust chain building when cross-signed intermediate |
| 3245 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3246 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3247 | #endif |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3248 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
| Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3249 | |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3250 | #ifdef TLS1_3_VERSION |
| 3251 | self->post_handshake_auth = 0; |
| 3252 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3253 | #endif |
| 3254 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3255 | return (PyObject *)self; |
| 3256 | } |
| 3257 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3258 | static int |
| 3259 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3260 | { |
| 3261 | #ifndef OPENSSL_NO_TLSEXT |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3262 | Py_VISIT(self->set_sni_cb); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3263 | #endif |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3264 | Py_VISIT(self->msg_cb); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3265 | return 0; |
| 3266 | } |
| 3267 | |
| 3268 | static int |
| 3269 | context_clear(PySSLContext *self) |
| 3270 | { |
| 3271 | #ifndef OPENSSL_NO_TLSEXT |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3272 | Py_CLEAR(self->set_sni_cb); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3273 | #endif |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3274 | Py_CLEAR(self->msg_cb); |
| 3275 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3276 | Py_CLEAR(self->keylog_filename); |
| 3277 | if (self->keylog_bio != NULL) { |
| 3278 | PySSL_BEGIN_ALLOW_THREADS |
| 3279 | BIO_free_all(self->keylog_bio); |
| 3280 | PySSL_END_ALLOW_THREADS |
| 3281 | self->keylog_bio = NULL; |
| 3282 | } |
| 3283 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3284 | return 0; |
| 3285 | } |
| 3286 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3287 | static void |
| 3288 | context_dealloc(PySSLContext *self) |
| 3289 | { |
| INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3290 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3291 | PyObject_GC_UnTrack(self); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3292 | context_clear(self); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3293 | SSL_CTX_free(self->ctx); |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3294 | #if HAVE_NPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3295 | PyMem_FREE(self->npn_protocols); |
| 3296 | #endif |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3297 | #if HAVE_ALPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3298 | PyMem_FREE(self->alpn_protocols); |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3299 | #endif |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3300 | Py_TYPE(self)->tp_free(self); |
| 3301 | } |
| 3302 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3303 | /*[clinic input] |
| 3304 | _ssl._SSLContext.set_ciphers |
| 3305 | cipherlist: str |
| 3306 | / |
| 3307 | [clinic start generated code]*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3308 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3309 | static PyObject * |
| 3310 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3311 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3312 | { |
| 3313 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3314 | if (ret == 0) { |
| Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3315 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3316 | otherwise the error will be reported again when another SSL call |
| 3317 | is done. */ |
| 3318 | ERR_clear_error(); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3319 | PyErr_SetString(PySSLErrorObject, |
| 3320 | "No cipher can be selected."); |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | Py_RETURN_NONE; |
| 3324 | } |
| 3325 | |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3326 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 3327 | /*[clinic input] |
| 3328 | _ssl._SSLContext.get_ciphers |
| 3329 | [clinic start generated code]*/ |
| 3330 | |
| 3331 | static PyObject * |
| 3332 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3333 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3334 | { |
| 3335 | SSL *ssl = NULL; |
| 3336 | STACK_OF(SSL_CIPHER) *sk = NULL; |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3337 | const SSL_CIPHER *cipher; |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3338 | int i=0; |
| 3339 | PyObject *result = NULL, *dct; |
| 3340 | |
| 3341 | ssl = SSL_new(self->ctx); |
| 3342 | if (ssl == NULL) { |
| 3343 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3344 | goto exit; |
| 3345 | } |
| 3346 | sk = SSL_get_ciphers(ssl); |
| 3347 | |
| 3348 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3349 | if (result == NULL) { |
| 3350 | goto exit; |
| 3351 | } |
| 3352 | |
| 3353 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3354 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3355 | dct = cipher_to_dict(cipher); |
| 3356 | if (dct == NULL) { |
| 3357 | Py_CLEAR(result); |
| 3358 | goto exit; |
| 3359 | } |
| 3360 | PyList_SET_ITEM(result, i, dct); |
| 3361 | } |
| 3362 | |
| 3363 | exit: |
| 3364 | if (ssl != NULL) |
| 3365 | SSL_free(ssl); |
| 3366 | return result; |
| 3367 | |
| 3368 | } |
| 3369 | #endif |
| 3370 | |
| 3371 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3372 | #if HAVE_NPN || HAVE_ALPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3373 | static int |
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3374 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3375 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3376 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3377 | { |
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3378 | int ret; |
| 3379 | if (client_protocols == NULL) { |
| 3380 | client_protocols = (unsigned char *)""; |
| 3381 | client_protocols_len = 0; |
| 3382 | } |
| 3383 | if (server_protocols == NULL) { |
| 3384 | server_protocols = (unsigned char *)""; |
| 3385 | server_protocols_len = 0; |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3386 | } |
| 3387 | |
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3388 | ret = SSL_select_next_proto(out, outlen, |
| 3389 | server_protocols, server_protocols_len, |
| 3390 | client_protocols, client_protocols_len); |
| 3391 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3392 | return SSL_TLSEXT_ERR_NOACK; |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3393 | |
| 3394 | return SSL_TLSEXT_ERR_OK; |
| 3395 | } |
| Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3396 | #endif |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3397 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3398 | #if HAVE_NPN |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3399 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 3400 | static int |
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3401 | _advertiseNPN_cb(SSL *s, |
| 3402 | const unsigned char **data, unsigned int *len, |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3403 | void *args) |
| 3404 | { |
| 3405 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3406 | |
| 3407 | if (ssl_ctx->npn_protocols == NULL) { |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3408 | *data = (unsigned char *)""; |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3409 | *len = 0; |
| 3410 | } else { |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3411 | *data = ssl_ctx->npn_protocols; |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3412 | *len = ssl_ctx->npn_protocols_len; |
| 3413 | } |
| 3414 | |
| 3415 | return SSL_TLSEXT_ERR_OK; |
| 3416 | } |
| 3417 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3418 | static int |
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3419 | _selectNPN_cb(SSL *s, |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3420 | unsigned char **out, unsigned char *outlen, |
| 3421 | const unsigned char *server, unsigned int server_len, |
| 3422 | void *args) |
| 3423 | { |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3424 | PySSLContext *ctx = (PySSLContext *)args; |
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3425 | return do_protocol_selection(0, out, outlen, server, server_len, |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3426 | ctx->npn_protocols, ctx->npn_protocols_len); |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3427 | } |
| 3428 | #endif |
| 3429 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3430 | /*[clinic input] |
| 3431 | _ssl._SSLContext._set_npn_protocols |
| 3432 | protos: Py_buffer |
| 3433 | / |
| 3434 | [clinic start generated code]*/ |
| 3435 | |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3436 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3437 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3438 | Py_buffer *protos) |
| 3439 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3440 | { |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3441 | #if HAVE_NPN |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3442 | PyMem_Free(self->npn_protocols); |
| 3443 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3444 | if (self->npn_protocols == NULL) |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3445 | return PyErr_NoMemory(); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3446 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3447 | self->npn_protocols_len = (int) protos->len; |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3448 | |
| 3449 | /* set both server and client callbacks, because the context can |
| 3450 | * be used to create both types of sockets */ |
| 3451 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3452 | _advertiseNPN_cb, |
| 3453 | self); |
| 3454 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3455 | _selectNPN_cb, |
| 3456 | self); |
| 3457 | |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3458 | Py_RETURN_NONE; |
| 3459 | #else |
| 3460 | PyErr_SetString(PyExc_NotImplementedError, |
| 3461 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3462 | return NULL; |
| 3463 | #endif |
| 3464 | } |
| 3465 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3466 | #if HAVE_ALPN |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3467 | static int |
| 3468 | _selectALPN_cb(SSL *s, |
| 3469 | const unsigned char **out, unsigned char *outlen, |
| 3470 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3471 | void *args) |
| 3472 | { |
| 3473 | PySSLContext *ctx = (PySSLContext *)args; |
| Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3474 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3475 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3476 | client_protocols, client_protocols_len); |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3477 | } |
| 3478 | #endif |
| 3479 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3480 | /*[clinic input] |
| 3481 | _ssl._SSLContext._set_alpn_protocols |
| 3482 | protos: Py_buffer |
| 3483 | / |
| 3484 | [clinic start generated code]*/ |
| 3485 | |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3486 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3487 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3488 | Py_buffer *protos) |
| 3489 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3490 | { |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3491 | #if HAVE_ALPN |
| Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3492 | if ((size_t)protos->len > UINT_MAX) { |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3493 | PyErr_Format(PyExc_OverflowError, |
| Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3494 | "protocols longer than %u bytes", UINT_MAX); |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3495 | return NULL; |
| 3496 | } |
| 3497 | |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3498 | PyMem_FREE(self->alpn_protocols); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3499 | self->alpn_protocols = PyMem_Malloc(protos->len); |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3500 | if (!self->alpn_protocols) |
| 3501 | return PyErr_NoMemory(); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3502 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3503 | self->alpn_protocols_len = (unsigned int)protos->len; |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3504 | |
| 3505 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3506 | return PyErr_NoMemory(); |
| 3507 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3508 | |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3509 | Py_RETURN_NONE; |
| 3510 | #else |
| 3511 | PyErr_SetString(PyExc_NotImplementedError, |
| 3512 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3513 | return NULL; |
| 3514 | #endif |
| 3515 | } |
| 3516 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3517 | static PyObject * |
| 3518 | get_verify_mode(PySSLContext *self, void *c) |
| 3519 | { |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3520 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3521 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3522 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3523 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3524 | case SSL_VERIFY_NONE: |
| 3525 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3526 | case SSL_VERIFY_PEER: |
| 3527 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3528 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3529 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3530 | } |
| 3531 | PyErr_SetString(PySSLErrorObject, |
| 3532 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3533 | return NULL; |
| 3534 | } |
| 3535 | |
| 3536 | static int |
| 3537 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3538 | { |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3539 | int n; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3540 | if (!PyArg_Parse(arg, "i", &n)) |
| 3541 | return -1; |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3542 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3543 | PyErr_SetString(PyExc_ValueError, |
| 3544 | "Cannot set verify_mode to CERT_NONE when " |
| 3545 | "check_hostname is enabled."); |
| 3546 | return -1; |
| 3547 | } |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3548 | return _set_verify_mode(self, n); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | static PyObject * |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3552 | get_verify_flags(PySSLContext *self, void *c) |
| 3553 | { |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3554 | X509_VERIFY_PARAM *param; |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3555 | unsigned long flags; |
| 3556 | |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3557 | param = SSL_CTX_get0_param(self->ctx); |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3558 | flags = X509_VERIFY_PARAM_get_flags(param); |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3559 | return PyLong_FromUnsignedLong(flags); |
| 3560 | } |
| 3561 | |
| 3562 | static int |
| 3563 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3564 | { |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3565 | X509_VERIFY_PARAM *param; |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3566 | unsigned long new_flags, flags, set, clear; |
| 3567 | |
| 3568 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3569 | return -1; |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3570 | param = SSL_CTX_get0_param(self->ctx); |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3571 | flags = X509_VERIFY_PARAM_get_flags(param); |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3572 | clear = flags & ~new_flags; |
| 3573 | set = ~flags & new_flags; |
| 3574 | if (clear) { |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3575 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3576 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3577 | return -1; |
| 3578 | } |
| 3579 | } |
| 3580 | if (set) { |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3581 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3582 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3583 | return -1; |
| 3584 | } |
| 3585 | } |
| 3586 | return 0; |
| 3587 | } |
| 3588 | |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3589 | /* Getter and setter for protocol version */ |
| 3590 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) |
| 3591 | |
| 3592 | |
| 3593 | static int |
| 3594 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3595 | { |
| 3596 | long v; |
| 3597 | int result; |
| 3598 | |
| 3599 | if (!PyArg_Parse(arg, "l", &v)) |
| 3600 | return -1; |
| 3601 | if (v > INT_MAX) { |
| 3602 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3603 | return -1; |
| 3604 | } |
| 3605 | |
| 3606 | switch(self->protocol) { |
| 3607 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3608 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3609 | case PY_SSL_VERSION_TLS: |
| 3610 | break; |
| 3611 | default: |
| 3612 | PyErr_SetString( |
| 3613 | PyExc_ValueError, |
| 3614 | "The context's protocol doesn't support modification of " |
| 3615 | "highest and lowest version." |
| 3616 | ); |
| 3617 | return -1; |
| 3618 | } |
| 3619 | |
| 3620 | if (what == 0) { |
| 3621 | switch(v) { |
| 3622 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3623 | v = 0; |
| 3624 | break; |
| 3625 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3626 | /* Emulate max for set_min_proto_version */ |
| 3627 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3628 | break; |
| 3629 | default: |
| 3630 | break; |
| 3631 | } |
| 3632 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3633 | } |
| 3634 | else { |
| 3635 | switch(v) { |
| 3636 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3637 | v = 0; |
| 3638 | break; |
| 3639 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3640 | /* Emulate max for set_min_proto_version */ |
| 3641 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3642 | break; |
| 3643 | default: |
| 3644 | break; |
| 3645 | } |
| 3646 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3647 | } |
| 3648 | if (result == 0) { |
| 3649 | PyErr_Format(PyExc_ValueError, |
| 3650 | "Unsupported protocol version 0x%x", v); |
| 3651 | return -1; |
| 3652 | } |
| 3653 | return 0; |
| 3654 | } |
| 3655 | |
| 3656 | static PyObject * |
| 3657 | get_minimum_version(PySSLContext *self, void *c) |
| 3658 | { |
| 3659 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3660 | if (v == 0) { |
| 3661 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3662 | } |
| 3663 | return PyLong_FromLong(v); |
| 3664 | } |
| 3665 | |
| 3666 | static int |
| 3667 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3668 | { |
| 3669 | return set_min_max_proto_version(self, arg, 0); |
| 3670 | } |
| 3671 | |
| 3672 | static PyObject * |
| 3673 | get_maximum_version(PySSLContext *self, void *c) |
| 3674 | { |
| 3675 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3676 | if (v == 0) { |
| 3677 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3678 | } |
| 3679 | return PyLong_FromLong(v); |
| 3680 | } |
| 3681 | |
| 3682 | static int |
| 3683 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3684 | { |
| 3685 | return set_min_max_proto_version(self, arg, 1); |
| 3686 | } |
| 3687 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ |
| 3688 | |
| Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3689 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 3690 | static PyObject * |
| 3691 | get_num_tickets(PySSLContext *self, void *c) |
| 3692 | { |
| Victor Stinner | 76611c7 | 2019-07-09 13:30:52 +0200 | [diff] [blame] | 3693 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
| Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | static int |
| 3697 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3698 | { |
| 3699 | long num; |
| 3700 | if (!PyArg_Parse(arg, "l", &num)) |
| 3701 | return -1; |
| 3702 | if (num < 0) { |
| 3703 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3704 | return -1; |
| 3705 | } |
| 3706 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3707 | PyErr_SetString(PyExc_ValueError, |
| 3708 | "SSLContext is not a server context."); |
| 3709 | return -1; |
| 3710 | } |
| 3711 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3712 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3713 | return -1; |
| 3714 | } |
| 3715 | return 0; |
| 3716 | } |
| 3717 | |
| 3718 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3719 | "Control the number of TLSv1.3 session tickets"); |
| 3720 | #endif /* OpenSSL 1.1.1 */ |
| 3721 | |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3722 | static PyObject * |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3723 | get_options(PySSLContext *self, void *c) |
| 3724 | { |
| 3725 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3726 | } |
| 3727 | |
| 3728 | static int |
| 3729 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3730 | { |
| 3731 | long new_opts, opts, set, clear; |
| 3732 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3733 | return -1; |
| 3734 | opts = SSL_CTX_get_options(self->ctx); |
| 3735 | clear = opts & ~new_opts; |
| 3736 | set = ~opts & new_opts; |
| 3737 | if (clear) { |
| 3738 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3739 | SSL_CTX_clear_options(self->ctx, clear); |
| 3740 | #else |
| 3741 | PyErr_SetString(PyExc_ValueError, |
| 3742 | "can't clear options before OpenSSL 0.9.8m"); |
| 3743 | return -1; |
| 3744 | #endif |
| 3745 | } |
| 3746 | if (set) |
| 3747 | SSL_CTX_set_options(self->ctx, set); |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3751 | static PyObject * |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3752 | get_host_flags(PySSLContext *self, void *c) |
| 3753 | { |
| 3754 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3755 | } |
| 3756 | |
| 3757 | static int |
| 3758 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3759 | { |
| 3760 | X509_VERIFY_PARAM *param; |
| 3761 | unsigned int new_flags = 0; |
| 3762 | |
| 3763 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3764 | return -1; |
| 3765 | |
| 3766 | param = SSL_CTX_get0_param(self->ctx); |
| 3767 | self->hostflags = new_flags; |
| 3768 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3769 | return 0; |
| 3770 | } |
| 3771 | |
| 3772 | static PyObject * |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3773 | get_check_hostname(PySSLContext *self, void *c) |
| 3774 | { |
| 3775 | return PyBool_FromLong(self->check_hostname); |
| 3776 | } |
| 3777 | |
| 3778 | static int |
| 3779 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3780 | { |
| 3781 | int check_hostname; |
| 3782 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3783 | return -1; |
| 3784 | if (check_hostname && |
| 3785 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
| Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3786 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3787 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
| Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3788 | return -1; |
| 3789 | } |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3790 | } |
| 3791 | self->check_hostname = check_hostname; |
| 3792 | return 0; |
| 3793 | } |
| 3794 | |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3795 | static PyObject * |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3796 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3797 | #if TLS1_3_VERSION |
| 3798 | return PyBool_FromLong(self->post_handshake_auth); |
| 3799 | #else |
| 3800 | Py_RETURN_NONE; |
| 3801 | #endif |
| 3802 | } |
| 3803 | |
| 3804 | #if TLS1_3_VERSION |
| 3805 | static int |
| 3806 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
| Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3807 | if (arg == NULL) { |
| 3808 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3809 | return -1; |
| 3810 | } |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3811 | int pha = PyObject_IsTrue(arg); |
| 3812 | |
| 3813 | if (pha == -1) { |
| 3814 | return -1; |
| 3815 | } |
| 3816 | self->post_handshake_auth = pha; |
| 3817 | |
| Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3818 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3819 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3820 | |
| 3821 | return 0; |
| 3822 | } |
| 3823 | #endif |
| 3824 | |
| 3825 | static PyObject * |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3826 | get_protocol(PySSLContext *self, void *c) { |
| 3827 | return PyLong_FromLong(self->protocol); |
| 3828 | } |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3829 | |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3830 | typedef struct { |
| 3831 | PyThreadState *thread_state; |
| 3832 | PyObject *callable; |
| 3833 | char *password; |
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3834 | int size; |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3835 | int error; |
| 3836 | } _PySSLPasswordInfo; |
| 3837 | |
| 3838 | static int |
| 3839 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3840 | const char *bad_type_error) |
| 3841 | { |
| 3842 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3843 | from a unicode, bytes, or byte array object. |
| 3844 | The password field will be dynamically allocated and must be freed |
| 3845 | by the caller */ |
| 3846 | PyObject *password_bytes = NULL; |
| 3847 | const char *data = NULL; |
| 3848 | Py_ssize_t size; |
| 3849 | |
| 3850 | if (PyUnicode_Check(password)) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3851 | password_bytes = PyUnicode_AsUTF8String(password); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3852 | if (!password_bytes) { |
| 3853 | goto error; |
| 3854 | } |
| 3855 | data = PyBytes_AS_STRING(password_bytes); |
| 3856 | size = PyBytes_GET_SIZE(password_bytes); |
| 3857 | } else if (PyBytes_Check(password)) { |
| 3858 | data = PyBytes_AS_STRING(password); |
| 3859 | size = PyBytes_GET_SIZE(password); |
| 3860 | } else if (PyByteArray_Check(password)) { |
| 3861 | data = PyByteArray_AS_STRING(password); |
| 3862 | size = PyByteArray_GET_SIZE(password); |
| 3863 | } else { |
| 3864 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3865 | goto error; |
| 3866 | } |
| 3867 | |
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3868 | if (size > (Py_ssize_t)INT_MAX) { |
| 3869 | PyErr_Format(PyExc_ValueError, |
| 3870 | "password cannot be longer than %d bytes", INT_MAX); |
| 3871 | goto error; |
| 3872 | } |
| 3873 | |
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3874 | PyMem_Free(pw_info->password); |
| 3875 | pw_info->password = PyMem_Malloc(size); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3876 | if (!pw_info->password) { |
| 3877 | PyErr_SetString(PyExc_MemoryError, |
| 3878 | "unable to allocate password buffer"); |
| 3879 | goto error; |
| 3880 | } |
| 3881 | memcpy(pw_info->password, data, size); |
| Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3882 | pw_info->size = (int)size; |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3883 | |
| 3884 | Py_XDECREF(password_bytes); |
| 3885 | return 1; |
| 3886 | |
| 3887 | error: |
| 3888 | Py_XDECREF(password_bytes); |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
| 3892 | static int |
| 3893 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3894 | { |
| 3895 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3896 | PyObject *fn_ret = NULL; |
| 3897 | |
| 3898 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3899 | |
| 3900 | if (pw_info->callable) { |
| Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3901 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3902 | if (!fn_ret) { |
| 3903 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3904 | core python API, so we could use it to add a frame here */ |
| 3905 | goto error; |
| 3906 | } |
| 3907 | |
| 3908 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3909 | "password callback must return a string")) { |
| 3910 | goto error; |
| 3911 | } |
| 3912 | Py_CLEAR(fn_ret); |
| 3913 | } |
| 3914 | |
| 3915 | if (pw_info->size > size) { |
| 3916 | PyErr_Format(PyExc_ValueError, |
| 3917 | "password cannot be longer than %d bytes", size); |
| 3918 | goto error; |
| 3919 | } |
| 3920 | |
| 3921 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3922 | memcpy(buf, pw_info->password, pw_info->size); |
| 3923 | return pw_info->size; |
| 3924 | |
| 3925 | error: |
| 3926 | Py_XDECREF(fn_ret); |
| 3927 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3928 | pw_info->error = 1; |
| 3929 | return -1; |
| 3930 | } |
| 3931 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3932 | /*[clinic input] |
| 3933 | _ssl._SSLContext.load_cert_chain |
| 3934 | certfile: object |
| Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3935 | keyfile: object = None |
| 3936 | password: object = None |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3937 | |
| 3938 | [clinic start generated code]*/ |
| 3939 | |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3940 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3941 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3942 | PyObject *keyfile, PyObject *password) |
| Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3943 | /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3944 | { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3945 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3946 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3947 | 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] | 3948 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3949 | int r; |
| 3950 | |
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3951 | errno = 0; |
| Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3952 | ERR_clear_error(); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3953 | if (keyfile == Py_None) |
| 3954 | keyfile = NULL; |
| 3955 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3956 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3957 | PyErr_SetString(PyExc_TypeError, |
| 3958 | "certfile should be a valid filesystem path"); |
| 3959 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3960 | return NULL; |
| 3961 | } |
| 3962 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3963 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3964 | PyErr_SetString(PyExc_TypeError, |
| 3965 | "keyfile should be a valid filesystem path"); |
| 3966 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3967 | goto error; |
| 3968 | } |
| Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3969 | if (password != Py_None) { |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3970 | if (PyCallable_Check(password)) { |
| 3971 | pw_info.callable = password; |
| 3972 | } else if (!_pwinfo_set(&pw_info, password, |
| 3973 | "password should be a string or callable")) { |
| 3974 | goto error; |
| 3975 | } |
| 3976 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3977 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3978 | } |
| 3979 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3980 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3981 | PyBytes_AS_STRING(certfile_bytes)); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3982 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3983 | if (r != 1) { |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3984 | if (pw_info.error) { |
| 3985 | ERR_clear_error(); |
| 3986 | /* the password callback has already set the error information */ |
| 3987 | } |
| 3988 | else if (errno != 0) { |
| Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3989 | ERR_clear_error(); |
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3990 | PyErr_SetFromErrno(PyExc_OSError); |
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3991 | } |
| 3992 | else { |
| 3993 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3994 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3995 | goto error; |
| 3996 | } |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3997 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3998 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3999 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 4000 | SSL_FILETYPE_PEM); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4001 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 4002 | Py_CLEAR(keyfile_bytes); |
| 4003 | Py_CLEAR(certfile_bytes); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4004 | if (r != 1) { |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4005 | if (pw_info.error) { |
| 4006 | ERR_clear_error(); |
| 4007 | /* the password callback has already set the error information */ |
| 4008 | } |
| 4009 | else if (errno != 0) { |
| Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 4010 | ERR_clear_error(); |
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4011 | PyErr_SetFromErrno(PyExc_OSError); |
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4012 | } |
| 4013 | else { |
| 4014 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4015 | } |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4016 | goto error; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4017 | } |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4018 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4019 | r = SSL_CTX_check_private_key(self->ctx); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4020 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4021 | if (r != 1) { |
| 4022 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4023 | goto error; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4024 | } |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4025 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4026 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4027 | PyMem_Free(pw_info.password); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4028 | Py_RETURN_NONE; |
| 4029 | |
| 4030 | error: |
| Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 4031 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 4032 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 4033 | PyMem_Free(pw_info.password); |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4034 | Py_XDECREF(keyfile_bytes); |
| 4035 | Py_XDECREF(certfile_bytes); |
| 4036 | return NULL; |
| 4037 | } |
| 4038 | |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4039 | /* internal helper function, returns -1 on error |
| 4040 | */ |
| 4041 | static int |
| Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 4042 | _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len, |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4043 | int filetype) |
| 4044 | { |
| 4045 | BIO *biobuf = NULL; |
| 4046 | X509_STORE *store; |
| 4047 | int retval = 0, err, loaded = 0; |
| 4048 | |
| 4049 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 4050 | |
| 4051 | if (len <= 0) { |
| 4052 | PyErr_SetString(PyExc_ValueError, |
| 4053 | "Empty certificate data"); |
| 4054 | return -1; |
| 4055 | } else if (len > INT_MAX) { |
| 4056 | PyErr_SetString(PyExc_OverflowError, |
| 4057 | "Certificate data is too long."); |
| 4058 | return -1; |
| 4059 | } |
| 4060 | |
| Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 4061 | biobuf = BIO_new_mem_buf(data, (int)len); |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4062 | if (biobuf == NULL) { |
| 4063 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 4064 | return -1; |
| 4065 | } |
| 4066 | |
| 4067 | store = SSL_CTX_get_cert_store(self->ctx); |
| 4068 | assert(store != NULL); |
| 4069 | |
| 4070 | while (1) { |
| 4071 | X509 *cert = NULL; |
| 4072 | int r; |
| 4073 | |
| 4074 | if (filetype == SSL_FILETYPE_ASN1) { |
| 4075 | cert = d2i_X509_bio(biobuf, NULL); |
| 4076 | } else { |
| 4077 | cert = PEM_read_bio_X509(biobuf, NULL, |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4078 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 4079 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 4080 | ); |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4081 | } |
| 4082 | if (cert == NULL) { |
| 4083 | break; |
| 4084 | } |
| 4085 | r = X509_STORE_add_cert(store, cert); |
| 4086 | X509_free(cert); |
| 4087 | if (!r) { |
| 4088 | err = ERR_peek_last_error(); |
| 4089 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 4090 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 4091 | /* cert already in hash table, not an error */ |
| 4092 | ERR_clear_error(); |
| 4093 | } else { |
| 4094 | break; |
| 4095 | } |
| 4096 | } |
| 4097 | loaded++; |
| 4098 | } |
| 4099 | |
| 4100 | err = ERR_peek_last_error(); |
| 4101 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 4102 | (loaded > 0) && |
| 4103 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 4104 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 4105 | /* EOF ASN1 file, not an error */ |
| 4106 | ERR_clear_error(); |
| 4107 | retval = 0; |
| 4108 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 4109 | (loaded > 0) && |
| 4110 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 4111 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 4112 | /* EOF PEM file, not an error */ |
| 4113 | ERR_clear_error(); |
| 4114 | retval = 0; |
| 4115 | } else { |
| 4116 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4117 | retval = -1; |
| 4118 | } |
| 4119 | |
| 4120 | BIO_free(biobuf); |
| 4121 | return retval; |
| 4122 | } |
| 4123 | |
| 4124 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4125 | /*[clinic input] |
| 4126 | _ssl._SSLContext.load_verify_locations |
| Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4127 | cafile: object = None |
| 4128 | capath: object = None |
| 4129 | cadata: object = None |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4130 | |
| 4131 | [clinic start generated code]*/ |
| 4132 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4133 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4134 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 4135 | PyObject *cafile, |
| 4136 | PyObject *capath, |
| 4137 | PyObject *cadata) |
| Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4138 | /*[clinic end generated code: output=454c7e41230ca551 input=42ecfe258233e194]*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4139 | { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4140 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 4141 | const char *cafile_buf = NULL, *capath_buf = NULL; |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4142 | int r = 0, ok = 1; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4143 | |
| Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4144 | errno = 0; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4145 | if (cafile == Py_None) |
| 4146 | cafile = NULL; |
| 4147 | if (capath == Py_None) |
| 4148 | capath = NULL; |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4149 | if (cadata == Py_None) |
| 4150 | cadata = NULL; |
| 4151 | |
| 4152 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4153 | PyErr_SetString(PyExc_TypeError, |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4154 | "cafile, capath and cadata cannot be all omitted"); |
| 4155 | goto error; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4156 | } |
| 4157 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4158 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4159 | PyErr_SetString(PyExc_TypeError, |
| 4160 | "cafile should be a valid filesystem path"); |
| 4161 | } |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4162 | goto error; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4163 | } |
| 4164 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4165 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4166 | PyErr_SetString(PyExc_TypeError, |
| 4167 | "capath should be a valid filesystem path"); |
| 4168 | } |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4169 | goto error; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4170 | } |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4171 | |
| 4172 | /* validata cadata type and load cadata */ |
| 4173 | if (cadata) { |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4174 | if (PyUnicode_Check(cadata)) { |
| 4175 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4176 | if (cadata_ascii == NULL) { |
| 4177 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4178 | goto invalid_cadata; |
| 4179 | } |
| 4180 | goto error; |
| 4181 | } |
| 4182 | r = _add_ca_certs(self, |
| 4183 | PyBytes_AS_STRING(cadata_ascii), |
| 4184 | PyBytes_GET_SIZE(cadata_ascii), |
| 4185 | SSL_FILETYPE_PEM); |
| 4186 | Py_DECREF(cadata_ascii); |
| 4187 | if (r == -1) { |
| 4188 | goto error; |
| 4189 | } |
| 4190 | } |
| 4191 | else if (PyObject_CheckBuffer(cadata)) { |
| 4192 | Py_buffer buf; |
| 4193 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 4194 | goto error; |
| 4195 | } |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4196 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4197 | PyBuffer_Release(&buf); |
| 4198 | PyErr_SetString(PyExc_TypeError, |
| 4199 | "cadata should be a contiguous buffer with " |
| 4200 | "a single dimension"); |
| 4201 | goto error; |
| 4202 | } |
| 4203 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4204 | PyBuffer_Release(&buf); |
| 4205 | if (r == -1) { |
| 4206 | goto error; |
| 4207 | } |
| Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4208 | } |
| 4209 | else { |
| 4210 | invalid_cadata: |
| 4211 | PyErr_SetString(PyExc_TypeError, |
| 4212 | "cadata should be an ASCII string or a " |
| 4213 | "bytes-like object"); |
| 4214 | goto error; |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | /* load cafile or capath */ |
| 4219 | if (cafile || capath) { |
| 4220 | if (cafile) |
| 4221 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4222 | if (capath) |
| 4223 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4224 | PySSL_BEGIN_ALLOW_THREADS |
| 4225 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4226 | PySSL_END_ALLOW_THREADS |
| 4227 | if (r != 1) { |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4228 | if (errno != 0) { |
| 4229 | ERR_clear_error(); |
| Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4230 | PyErr_SetFromErrno(PyExc_OSError); |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4231 | } |
| 4232 | else { |
| 4233 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4234 | } |
| 4235 | goto error; |
| 4236 | } |
| 4237 | } |
| 4238 | goto end; |
| 4239 | |
| 4240 | error: |
| 4241 | ok = 0; |
| 4242 | end: |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4243 | Py_XDECREF(cafile_bytes); |
| 4244 | Py_XDECREF(capath_bytes); |
| Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4245 | if (ok) { |
| 4246 | Py_RETURN_NONE; |
| 4247 | } else { |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4248 | return NULL; |
| 4249 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4252 | /*[clinic input] |
| 4253 | _ssl._SSLContext.load_dh_params |
| 4254 | path as filepath: object |
| 4255 | / |
| 4256 | |
| 4257 | [clinic start generated code]*/ |
| 4258 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4259 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4260 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4261 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4262 | { |
| 4263 | FILE *f; |
| 4264 | DH *dh; |
| 4265 | |
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4266 | f = _Py_fopen_obj(filepath, "rb"); |
| Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4267 | if (f == NULL) |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4268 | return NULL; |
| Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4269 | |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4270 | errno = 0; |
| 4271 | PySSL_BEGIN_ALLOW_THREADS |
| 4272 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
| Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4273 | fclose(f); |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4274 | PySSL_END_ALLOW_THREADS |
| 4275 | if (dh == NULL) { |
| 4276 | if (errno != 0) { |
| 4277 | ERR_clear_error(); |
| 4278 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4279 | } |
| 4280 | else { |
| 4281 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4282 | } |
| 4283 | return NULL; |
| 4284 | } |
| 4285 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 4286 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4287 | DH_free(dh); |
| 4288 | Py_RETURN_NONE; |
| 4289 | } |
| 4290 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4291 | /*[clinic input] |
| 4292 | _ssl._SSLContext._wrap_socket |
| 4293 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 4294 | server_side: int |
| 4295 | server_hostname as hostname_obj: object = None |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4296 | * |
| 4297 | owner: object = None |
| 4298 | session: object = None |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4299 | |
| 4300 | [clinic start generated code]*/ |
| 4301 | |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4302 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4303 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4304 | int server_side, PyObject *hostname_obj, |
| 4305 | PyObject *owner, PyObject *session) |
| 4306 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4307 | { |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4308 | char *hostname = NULL; |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4309 | PyObject *res; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4310 | |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4311 | /* server_hostname is either None (or absent), or to be encoded |
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4312 | as IDN A-label (ASCII str) without NULL bytes. */ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4313 | if (hostname_obj != Py_None) { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4314 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4315 | return NULL; |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4316 | } |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4317 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4318 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4319 | server_side, hostname, |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4320 | owner, session, |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4321 | NULL, NULL); |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4322 | if (hostname != NULL) |
| 4323 | PyMem_Free(hostname); |
| 4324 | return res; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4327 | /*[clinic input] |
| 4328 | _ssl._SSLContext._wrap_bio |
| 4329 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4330 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4331 | server_side: int |
| 4332 | server_hostname as hostname_obj: object = None |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4333 | * |
| 4334 | owner: object = None |
| 4335 | session: object = None |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4336 | |
| 4337 | [clinic start generated code]*/ |
| 4338 | |
| Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4339 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4340 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4341 | PySSLMemoryBIO *outgoing, int server_side, |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4342 | PyObject *hostname_obj, PyObject *owner, |
| 4343 | PyObject *session) |
| 4344 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=8cf22f4d586ac56a]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4345 | { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4346 | char *hostname = NULL; |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4347 | PyObject *res; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4348 | |
| 4349 | /* server_hostname is either None (or absent), or to be encoded |
| Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4350 | as IDN A-label (ASCII str) without NULL bytes. */ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4351 | if (hostname_obj != Py_None) { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4352 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4353 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
| Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4357 | owner, session, |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4358 | incoming, outgoing); |
| 4359 | |
| 4360 | PyMem_Free(hostname); |
| 4361 | return res; |
| 4362 | } |
| 4363 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4364 | /*[clinic input] |
| 4365 | _ssl._SSLContext.session_stats |
| 4366 | [clinic start generated code]*/ |
| 4367 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4368 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4369 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4370 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
| Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4371 | { |
| 4372 | int r; |
| 4373 | PyObject *value, *stats = PyDict_New(); |
| 4374 | if (!stats) |
| 4375 | return NULL; |
| 4376 | |
| 4377 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4378 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4379 | if (value == NULL) \ |
| 4380 | goto error; \ |
| 4381 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4382 | Py_DECREF(value); \ |
| 4383 | if (r < 0) \ |
| 4384 | goto error; |
| 4385 | |
| 4386 | ADD_STATS(number, "number"); |
| 4387 | ADD_STATS(connect, "connect"); |
| 4388 | ADD_STATS(connect_good, "connect_good"); |
| 4389 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4390 | ADD_STATS(accept, "accept"); |
| 4391 | ADD_STATS(accept_good, "accept_good"); |
| 4392 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4393 | ADD_STATS(accept, "accept"); |
| 4394 | ADD_STATS(hits, "hits"); |
| 4395 | ADD_STATS(misses, "misses"); |
| 4396 | ADD_STATS(timeouts, "timeouts"); |
| 4397 | ADD_STATS(cache_full, "cache_full"); |
| 4398 | |
| 4399 | #undef ADD_STATS |
| 4400 | |
| 4401 | return stats; |
| 4402 | |
| 4403 | error: |
| 4404 | Py_DECREF(stats); |
| 4405 | return NULL; |
| 4406 | } |
| 4407 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4408 | /*[clinic input] |
| 4409 | _ssl._SSLContext.set_default_verify_paths |
| 4410 | [clinic start generated code]*/ |
| 4411 | |
| Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4412 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4413 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4414 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
| Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4415 | { |
| 4416 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 4417 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4418 | return NULL; |
| 4419 | } |
| 4420 | Py_RETURN_NONE; |
| 4421 | } |
| 4422 | |
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4423 | #ifndef OPENSSL_NO_ECDH |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4424 | /*[clinic input] |
| 4425 | _ssl._SSLContext.set_ecdh_curve |
| 4426 | name: object |
| 4427 | / |
| 4428 | |
| 4429 | [clinic start generated code]*/ |
| 4430 | |
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4431 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4432 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4433 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4434 | { |
| 4435 | PyObject *name_bytes; |
| 4436 | int nid; |
| 4437 | EC_KEY *key; |
| 4438 | |
| 4439 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4440 | return NULL; |
| 4441 | assert(PyBytes_Check(name_bytes)); |
| 4442 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4443 | Py_DECREF(name_bytes); |
| 4444 | if (nid == 0) { |
| 4445 | PyErr_Format(PyExc_ValueError, |
| 4446 | "unknown elliptic curve name %R", name); |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | key = EC_KEY_new_by_curve_name(nid); |
| 4450 | if (key == NULL) { |
| 4451 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4452 | return NULL; |
| 4453 | } |
| 4454 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4455 | EC_KEY_free(key); |
| 4456 | Py_RETURN_NONE; |
| 4457 | } |
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4458 | #endif |
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4459 | |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4460 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4461 | static int |
| 4462 | _servername_callback(SSL *s, int *al, void *args) |
| 4463 | { |
| 4464 | int ret; |
| 4465 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 4466 | PySSLSocket *ssl; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4467 | PyObject *result; |
| 4468 | /* The high-level ssl.SSLSocket object */ |
| 4469 | PyObject *ssl_socket; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4470 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
| Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4471 | PyGILState_STATE gstate = PyGILState_Ensure(); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4472 | |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4473 | if (ssl_ctx->set_sni_cb == NULL) { |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4474 | /* remove race condition in this the call back while if removing the |
| 4475 | * callback is in progress */ |
| 4476 | PyGILState_Release(gstate); |
| Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4477 | return SSL_TLSEXT_ERR_OK; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | ssl = SSL_get_app_data(s); |
| 4481 | assert(PySSLSocket_Check(ssl)); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4482 | |
| Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4483 | /* The servername callback expects an argument that represents the current |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4484 | * SSL connection and that has a .context attribute that can be changed to |
| 4485 | * identify the requested hostname. Since the official API is the Python |
| 4486 | * level API we want to pass the callback a Python level object rather than |
| 4487 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4488 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4489 | * will be passed. If both do not exist only then the C-level object is |
| 4490 | * passed. */ |
| 4491 | if (ssl->owner) |
| 4492 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4493 | else if (ssl->Socket) |
| 4494 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4495 | else |
| 4496 | ssl_socket = (PyObject *) ssl; |
| 4497 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4498 | Py_INCREF(ssl_socket); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4499 | if (ssl_socket == Py_None) |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4500 | goto error; |
| Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4501 | |
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4502 | if (servername == NULL) { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4503 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, |
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4504 | Py_None, ssl_ctx, NULL); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4505 | } |
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4506 | else { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4507 | PyObject *servername_bytes; |
| 4508 | PyObject *servername_str; |
| 4509 | |
| 4510 | servername_bytes = PyBytes_FromString(servername); |
| 4511 | if (servername_bytes == NULL) { |
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4512 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 4513 | goto error; |
| 4514 | } |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4515 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4516 | * back into a str object, but still as an A-label (bpo-28414) |
| 4517 | */ |
| 4518 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
| 4519 | Py_DECREF(servername_bytes); |
| 4520 | if (servername_str == NULL) { |
| 4521 | PyErr_WriteUnraisable(servername_bytes); |
| Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4522 | goto error; |
| 4523 | } |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4524 | result = PyObject_CallFunctionObjArgs( |
| 4525 | ssl_ctx->set_sni_cb, ssl_socket, servername_str, |
| 4526 | ssl_ctx, NULL); |
| 4527 | Py_DECREF(servername_str); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4528 | } |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4529 | Py_DECREF(ssl_socket); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4530 | |
| 4531 | if (result == NULL) { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4532 | PyErr_WriteUnraisable(ssl_ctx->set_sni_cb); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4533 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4534 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4535 | } |
| 4536 | else { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4537 | /* Result may be None, a SSLContext or an integer |
| 4538 | * None and SSLContext are OK, integer or other values are an error. |
| 4539 | */ |
| 4540 | if (result == Py_None) { |
| 4541 | ret = SSL_TLSEXT_ERR_OK; |
| 4542 | } else { |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4543 | *al = (int) PyLong_AsLong(result); |
| 4544 | if (PyErr_Occurred()) { |
| 4545 | PyErr_WriteUnraisable(result); |
| 4546 | *al = SSL_AD_INTERNAL_ERROR; |
| 4547 | } |
| 4548 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4549 | } |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4550 | Py_DECREF(result); |
| 4551 | } |
| 4552 | |
| 4553 | PyGILState_Release(gstate); |
| 4554 | return ret; |
| 4555 | |
| 4556 | error: |
| 4557 | Py_DECREF(ssl_socket); |
| 4558 | *al = SSL_AD_INTERNAL_ERROR; |
| 4559 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4560 | PyGILState_Release(gstate); |
| 4561 | return ret; |
| 4562 | } |
| Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 4563 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4564 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4565 | static PyObject * |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4566 | get_sni_callback(PySSLContext *self, void *c) |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4567 | { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4568 | PyObject *cb = self->set_sni_cb; |
| 4569 | if (cb == NULL) { |
| 4570 | Py_RETURN_NONE; |
| 4571 | } |
| 4572 | Py_INCREF(cb); |
| 4573 | return cb; |
| 4574 | } |
| 4575 | |
| 4576 | static int |
| 4577 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4578 | { |
| 4579 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4580 | PyErr_SetString(PyExc_ValueError, |
| 4581 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4582 | return -1; |
| 4583 | } |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4584 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4585 | Py_CLEAR(self->set_sni_cb); |
| 4586 | if (arg == Py_None) { |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4587 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4588 | } |
| 4589 | else { |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4590 | if (!PyCallable_Check(arg)) { |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4591 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4592 | PyErr_SetString(PyExc_TypeError, |
| 4593 | "not a callable object"); |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4594 | return -1; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4595 | } |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4596 | Py_INCREF(arg); |
| 4597 | self->set_sni_cb = arg; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4598 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4599 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4600 | } |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4601 | return 0; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4602 | #else |
| 4603 | PyErr_SetString(PyExc_NotImplementedError, |
| 4604 | "The TLS extension servername callback, " |
| 4605 | "SSL_CTX_set_tlsext_servername_callback, " |
| 4606 | "is not in the current OpenSSL library."); |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4607 | return -1; |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4608 | #endif |
| 4609 | } |
| 4610 | |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4611 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4612 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4613 | \n\ |
| 4614 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4615 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4616 | See RFC 6066 for details of the SNI extension."); |
| 4617 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4618 | /*[clinic input] |
| 4619 | _ssl._SSLContext.cert_store_stats |
| 4620 | |
| 4621 | Returns quantities of loaded X.509 certificates. |
| 4622 | |
| 4623 | X.509 certificates with a CA extension and certificate revocation lists |
| 4624 | inside the context's cert store. |
| 4625 | |
| 4626 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4627 | been used at least once. |
| 4628 | [clinic start generated code]*/ |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4629 | |
| 4630 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4631 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4632 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4633 | { |
| 4634 | X509_STORE *store; |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4635 | STACK_OF(X509_OBJECT) *objs; |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4636 | X509_OBJECT *obj; |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4637 | int x509 = 0, crl = 0, ca = 0, i; |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4638 | |
| 4639 | store = SSL_CTX_get_cert_store(self->ctx); |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4640 | objs = X509_STORE_get0_objects(store); |
| 4641 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4642 | obj = sk_X509_OBJECT_value(objs, i); |
| 4643 | switch (X509_OBJECT_get_type(obj)) { |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4644 | case X509_LU_X509: |
| 4645 | x509++; |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4646 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4647 | ca++; |
| 4648 | } |
| 4649 | break; |
| 4650 | case X509_LU_CRL: |
| 4651 | crl++; |
| 4652 | break; |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4653 | default: |
| 4654 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4655 | * As far as I can tell they are internal states and never |
| 4656 | * stored in a cert store */ |
| 4657 | break; |
| 4658 | } |
| 4659 | } |
| 4660 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4661 | "x509_ca", ca); |
| 4662 | } |
| 4663 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4664 | /*[clinic input] |
| 4665 | _ssl._SSLContext.get_ca_certs |
| 4666 | binary_form: bool = False |
| 4667 | |
| 4668 | Returns a list of dicts with information of loaded CA certs. |
| 4669 | |
| 4670 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4671 | certificate. |
| 4672 | |
| 4673 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4674 | been used at least once. |
| 4675 | [clinic start generated code]*/ |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4676 | |
| 4677 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4678 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4679 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4680 | { |
| 4681 | X509_STORE *store; |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4682 | STACK_OF(X509_OBJECT) *objs; |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4683 | PyObject *ci = NULL, *rlist = NULL; |
| 4684 | int i; |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4685 | |
| 4686 | if ((rlist = PyList_New(0)) == NULL) { |
| 4687 | return NULL; |
| 4688 | } |
| 4689 | |
| 4690 | store = SSL_CTX_get_cert_store(self->ctx); |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4691 | objs = X509_STORE_get0_objects(store); |
| 4692 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4693 | X509_OBJECT *obj; |
| 4694 | X509 *cert; |
| 4695 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4696 | obj = sk_X509_OBJECT_value(objs, i); |
| 4697 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4698 | /* not a x509 cert */ |
| 4699 | continue; |
| 4700 | } |
| 4701 | /* CA for any purpose */ |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4702 | cert = X509_OBJECT_get0_X509(obj); |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4703 | if (!X509_check_ca(cert)) { |
| 4704 | continue; |
| 4705 | } |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4706 | if (binary_form) { |
| Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4707 | ci = _certificate_to_der(cert); |
| 4708 | } else { |
| 4709 | ci = _decode_certificate(cert); |
| 4710 | } |
| 4711 | if (ci == NULL) { |
| 4712 | goto error; |
| 4713 | } |
| 4714 | if (PyList_Append(rlist, ci) == -1) { |
| 4715 | goto error; |
| 4716 | } |
| 4717 | Py_CLEAR(ci); |
| 4718 | } |
| 4719 | return rlist; |
| 4720 | |
| 4721 | error: |
| 4722 | Py_XDECREF(ci); |
| 4723 | Py_XDECREF(rlist); |
| 4724 | return NULL; |
| 4725 | } |
| 4726 | |
| 4727 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4728 | static PyGetSetDef context_getsetlist[] = { |
| Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4729 | {"check_hostname", (getter) get_check_hostname, |
| 4730 | (setter) set_check_hostname, NULL}, |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4731 | {"_host_flags", (getter) get_host_flags, |
| 4732 | (setter) set_host_flags, NULL}, |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4733 | #if SSL_CTRL_GET_MAX_PROTO_VERSION |
| 4734 | {"minimum_version", (getter) get_minimum_version, |
| 4735 | (setter) set_minimum_version, NULL}, |
| 4736 | {"maximum_version", (getter) get_maximum_version, |
| 4737 | (setter) set_maximum_version, NULL}, |
| 4738 | #endif |
| Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4739 | #ifdef HAVE_OPENSSL_KEYLOG |
| 4740 | {"keylog_filename", (getter) _PySSLContext_get_keylog_filename, |
| 4741 | (setter) _PySSLContext_set_keylog_filename, NULL}, |
| 4742 | #endif |
| 4743 | {"_msg_callback", (getter) _PySSLContext_get_msg_callback, |
| 4744 | (setter) _PySSLContext_set_msg_callback, NULL}, |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4745 | {"sni_callback", (getter) get_sni_callback, |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4746 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
| Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 4747 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 4748 | {"num_tickets", (getter) get_num_tickets, |
| 4749 | (setter) set_num_tickets, PySSLContext_num_tickets_doc}, |
| 4750 | #endif |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4751 | {"options", (getter) get_options, |
| 4752 | (setter) set_options, NULL}, |
| Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4753 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4754 | #ifdef TLS1_3_VERSION |
| 4755 | (setter) set_post_handshake_auth, |
| 4756 | #else |
| 4757 | NULL, |
| 4758 | #endif |
| 4759 | NULL}, |
| Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4760 | {"protocol", (getter) get_protocol, |
| 4761 | NULL, NULL}, |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4762 | {"verify_flags", (getter) get_verify_flags, |
| 4763 | (setter) set_verify_flags, NULL}, |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4764 | {"verify_mode", (getter) get_verify_mode, |
| 4765 | (setter) set_verify_mode, NULL}, |
| 4766 | {NULL}, /* sentinel */ |
| 4767 | }; |
| 4768 | |
| 4769 | static struct PyMethodDef context_methods[] = { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4770 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4771 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4772 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4773 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4774 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4775 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4776 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4777 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4778 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4779 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4780 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4781 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4782 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
| Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4783 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4784 | {NULL, NULL} /* sentinel */ |
| 4785 | }; |
| 4786 | |
| 4787 | static PyTypeObject PySSLContext_Type = { |
| 4788 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4789 | "_ssl._SSLContext", /*tp_name*/ |
| 4790 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 4791 | 0, /*tp_itemsize*/ |
| 4792 | (destructor)context_dealloc, /*tp_dealloc*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4793 | 0, /*tp_vectorcall_offset*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4794 | 0, /*tp_getattr*/ |
| 4795 | 0, /*tp_setattr*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4796 | 0, /*tp_as_async*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4797 | 0, /*tp_repr*/ |
| 4798 | 0, /*tp_as_number*/ |
| 4799 | 0, /*tp_as_sequence*/ |
| 4800 | 0, /*tp_as_mapping*/ |
| 4801 | 0, /*tp_hash*/ |
| 4802 | 0, /*tp_call*/ |
| 4803 | 0, /*tp_str*/ |
| 4804 | 0, /*tp_getattro*/ |
| 4805 | 0, /*tp_setattro*/ |
| 4806 | 0, /*tp_as_buffer*/ |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4807 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4808 | 0, /*tp_doc*/ |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4809 | (traverseproc) context_traverse, /*tp_traverse*/ |
| 4810 | (inquiry) context_clear, /*tp_clear*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4811 | 0, /*tp_richcompare*/ |
| 4812 | 0, /*tp_weaklistoffset*/ |
| 4813 | 0, /*tp_iter*/ |
| 4814 | 0, /*tp_iternext*/ |
| 4815 | context_methods, /*tp_methods*/ |
| 4816 | 0, /*tp_members*/ |
| 4817 | context_getsetlist, /*tp_getset*/ |
| 4818 | 0, /*tp_base*/ |
| 4819 | 0, /*tp_dict*/ |
| 4820 | 0, /*tp_descr_get*/ |
| 4821 | 0, /*tp_descr_set*/ |
| 4822 | 0, /*tp_dictoffset*/ |
| 4823 | 0, /*tp_init*/ |
| 4824 | 0, /*tp_alloc*/ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4825 | _ssl__SSLContext, /*tp_new*/ |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4826 | }; |
| 4827 | |
| 4828 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4829 | /* |
| 4830 | * MemoryBIO objects |
| 4831 | */ |
| 4832 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4833 | /*[clinic input] |
| 4834 | @classmethod |
| 4835 | _ssl.MemoryBIO.__new__ |
| 4836 | |
| 4837 | [clinic start generated code]*/ |
| 4838 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4839 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4840 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4841 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4842 | { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4843 | BIO *bio; |
| 4844 | PySSLMemoryBIO *self; |
| 4845 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4846 | bio = BIO_new(BIO_s_mem()); |
| 4847 | if (bio == NULL) { |
| 4848 | PyErr_SetString(PySSLErrorObject, |
| 4849 | "failed to allocate BIO"); |
| 4850 | return NULL; |
| 4851 | } |
| 4852 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4853 | * just that no data is currently available. The SSL routines should retry |
| 4854 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4855 | BIO_set_retry_read(bio); |
| 4856 | BIO_set_mem_eof_return(bio, -1); |
| 4857 | |
| 4858 | assert(type != NULL && type->tp_alloc != NULL); |
| 4859 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4860 | if (self == NULL) { |
| 4861 | BIO_free(bio); |
| 4862 | return NULL; |
| 4863 | } |
| 4864 | self->bio = bio; |
| 4865 | self->eof_written = 0; |
| 4866 | |
| 4867 | return (PyObject *) self; |
| 4868 | } |
| 4869 | |
| 4870 | static void |
| 4871 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4872 | { |
| 4873 | BIO_free(self->bio); |
| 4874 | Py_TYPE(self)->tp_free(self); |
| 4875 | } |
| 4876 | |
| 4877 | static PyObject * |
| 4878 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4879 | { |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4880 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4884 | "The number of bytes pending in the memory BIO."); |
| 4885 | |
| 4886 | static PyObject * |
| 4887 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4888 | { |
| 4889 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4890 | && self->eof_written); |
| 4891 | } |
| 4892 | |
| 4893 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4894 | "Whether the memory BIO is at EOF."); |
| 4895 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4896 | /*[clinic input] |
| 4897 | _ssl.MemoryBIO.read |
| 4898 | size as len: int = -1 |
| 4899 | / |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4900 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4901 | Read up to size bytes from the memory BIO. |
| 4902 | |
| 4903 | If size is not specified, read the entire buffer. |
| 4904 | If the return value is an empty bytes instance, this means either |
| 4905 | EOF or that no data is available. Use the "eof" property to |
| 4906 | distinguish between the two. |
| 4907 | [clinic start generated code]*/ |
| 4908 | |
| 4909 | static PyObject * |
| 4910 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4911 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4912 | { |
| 4913 | int avail, nbytes; |
| 4914 | PyObject *result; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4915 | |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4916 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4917 | if ((len < 0) || (len > avail)) |
| 4918 | len = avail; |
| 4919 | |
| 4920 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4921 | if ((result == NULL) || (len == 0)) |
| 4922 | return result; |
| 4923 | |
| 4924 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
| Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4925 | if (nbytes < 0) { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4926 | Py_DECREF(result); |
| Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4927 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4928 | return NULL; |
| 4929 | } |
| 4930 | |
| Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4931 | /* There should never be any short reads but check anyway. */ |
| 4932 | if (nbytes < len) { |
| 4933 | _PyBytes_Resize(&result, nbytes); |
| 4934 | } |
| 4935 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4936 | return result; |
| 4937 | } |
| 4938 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4939 | /*[clinic input] |
| 4940 | _ssl.MemoryBIO.write |
| 4941 | b: Py_buffer |
| 4942 | / |
| 4943 | |
| 4944 | Writes the bytes b into the memory BIO. |
| 4945 | |
| 4946 | Returns the number of bytes written. |
| 4947 | [clinic start generated code]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4948 | |
| 4949 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4950 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4951 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4952 | { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4953 | int nbytes; |
| 4954 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4955 | if (b->len > INT_MAX) { |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4956 | PyErr_Format(PyExc_OverflowError, |
| 4957 | "string longer than %d bytes", INT_MAX); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4958 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | if (self->eof_written) { |
| 4962 | PyErr_SetString(PySSLErrorObject, |
| 4963 | "cannot write() after write_eof()"); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4964 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4965 | } |
| 4966 | |
| Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4967 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4968 | if (nbytes < 0) { |
| 4969 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4970 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4971 | } |
| 4972 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4973 | return PyLong_FromLong(nbytes); |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4976 | /*[clinic input] |
| 4977 | _ssl.MemoryBIO.write_eof |
| 4978 | |
| 4979 | Write an EOF marker to the memory BIO. |
| 4980 | |
| 4981 | When all data has been read, the "eof" property will be True. |
| 4982 | [clinic start generated code]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4983 | |
| 4984 | static PyObject * |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4985 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4986 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4987 | { |
| 4988 | self->eof_written = 1; |
| 4989 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4990 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4991 | BIO_clear_retry_flags(self->bio); |
| 4992 | BIO_set_mem_eof_return(self->bio, 0); |
| 4993 | |
| 4994 | Py_RETURN_NONE; |
| 4995 | } |
| 4996 | |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4997 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4998 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4999 | PySSL_memory_bio_pending_doc}, |
| 5000 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 5001 | PySSL_memory_bio_eof_doc}, |
| 5002 | {NULL}, /* sentinel */ |
| 5003 | }; |
| 5004 | |
| 5005 | static struct PyMethodDef memory_bio_methods[] = { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5006 | _SSL_MEMORYBIO_READ_METHODDEF |
| 5007 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 5008 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5009 | {NULL, NULL} /* sentinel */ |
| 5010 | }; |
| 5011 | |
| 5012 | static PyTypeObject PySSLMemoryBIO_Type = { |
| 5013 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5014 | "_ssl.MemoryBIO", /*tp_name*/ |
| 5015 | sizeof(PySSLMemoryBIO), /*tp_basicsize*/ |
| 5016 | 0, /*tp_itemsize*/ |
| 5017 | (destructor)memory_bio_dealloc, /*tp_dealloc*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5018 | 0, /*tp_vectorcall_offset*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5019 | 0, /*tp_getattr*/ |
| 5020 | 0, /*tp_setattr*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5021 | 0, /*tp_as_async*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5022 | 0, /*tp_repr*/ |
| 5023 | 0, /*tp_as_number*/ |
| 5024 | 0, /*tp_as_sequence*/ |
| 5025 | 0, /*tp_as_mapping*/ |
| 5026 | 0, /*tp_hash*/ |
| 5027 | 0, /*tp_call*/ |
| 5028 | 0, /*tp_str*/ |
| 5029 | 0, /*tp_getattro*/ |
| 5030 | 0, /*tp_setattro*/ |
| 5031 | 0, /*tp_as_buffer*/ |
| 5032 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5033 | 0, /*tp_doc*/ |
| 5034 | 0, /*tp_traverse*/ |
| 5035 | 0, /*tp_clear*/ |
| 5036 | 0, /*tp_richcompare*/ |
| 5037 | 0, /*tp_weaklistoffset*/ |
| 5038 | 0, /*tp_iter*/ |
| 5039 | 0, /*tp_iternext*/ |
| 5040 | memory_bio_methods, /*tp_methods*/ |
| 5041 | 0, /*tp_members*/ |
| 5042 | memory_bio_getsetlist, /*tp_getset*/ |
| 5043 | 0, /*tp_base*/ |
| 5044 | 0, /*tp_dict*/ |
| 5045 | 0, /*tp_descr_get*/ |
| 5046 | 0, /*tp_descr_set*/ |
| 5047 | 0, /*tp_dictoffset*/ |
| 5048 | 0, /*tp_init*/ |
| 5049 | 0, /*tp_alloc*/ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5050 | _ssl_MemoryBIO, /*tp_new*/ |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5051 | }; |
| 5052 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5053 | |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5054 | /* |
| 5055 | * SSL Session object |
| 5056 | */ |
| 5057 | |
| 5058 | static void |
| 5059 | PySSLSession_dealloc(PySSLSession *self) |
| 5060 | { |
| INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 5061 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5062 | PyObject_GC_UnTrack(self); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5063 | Py_XDECREF(self->ctx); |
| 5064 | if (self->session != NULL) { |
| 5065 | SSL_SESSION_free(self->session); |
| 5066 | } |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5067 | PyObject_GC_Del(self); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | static PyObject * |
| 5071 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 5072 | { |
| 5073 | int result; |
| 5074 | |
| 5075 | if (left == NULL || right == NULL) { |
| 5076 | PyErr_BadInternalCall(); |
| 5077 | return NULL; |
| 5078 | } |
| 5079 | |
| 5080 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 5081 | Py_RETURN_NOTIMPLEMENTED; |
| 5082 | } |
| 5083 | |
| 5084 | if (left == right) { |
| 5085 | result = 0; |
| 5086 | } else { |
| 5087 | const unsigned char *left_id, *right_id; |
| 5088 | unsigned int left_len, right_len; |
| 5089 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 5090 | &left_len); |
| 5091 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 5092 | &right_len); |
| 5093 | if (left_len == right_len) { |
| 5094 | result = memcmp(left_id, right_id, left_len); |
| 5095 | } else { |
| 5096 | result = 1; |
| 5097 | } |
| 5098 | } |
| 5099 | |
| 5100 | switch (op) { |
| 5101 | case Py_EQ: |
| 5102 | if (result == 0) { |
| 5103 | Py_RETURN_TRUE; |
| 5104 | } else { |
| 5105 | Py_RETURN_FALSE; |
| 5106 | } |
| 5107 | break; |
| 5108 | case Py_NE: |
| 5109 | if (result != 0) { |
| 5110 | Py_RETURN_TRUE; |
| 5111 | } else { |
| 5112 | Py_RETURN_FALSE; |
| 5113 | } |
| 5114 | break; |
| 5115 | case Py_LT: |
| 5116 | case Py_LE: |
| 5117 | case Py_GT: |
| 5118 | case Py_GE: |
| 5119 | Py_RETURN_NOTIMPLEMENTED; |
| 5120 | break; |
| 5121 | default: |
| 5122 | PyErr_BadArgument(); |
| 5123 | return NULL; |
| 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | static int |
| 5128 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 5129 | { |
| 5130 | Py_VISIT(self->ctx); |
| 5131 | return 0; |
| 5132 | } |
| 5133 | |
| 5134 | static int |
| 5135 | PySSLSession_clear(PySSLSession *self) |
| 5136 | { |
| 5137 | Py_CLEAR(self->ctx); |
| 5138 | return 0; |
| 5139 | } |
| 5140 | |
| 5141 | |
| 5142 | static PyObject * |
| 5143 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 5144 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 5145 | } |
| 5146 | |
| 5147 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 5148 | "Session creation time (seconds since epoch)."); |
| 5149 | |
| 5150 | |
| 5151 | static PyObject * |
| 5152 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 5153 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 5154 | } |
| 5155 | |
| 5156 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 5157 | "Session timeout (delta in seconds)."); |
| 5158 | |
| 5159 | |
| 5160 | static PyObject * |
| 5161 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 5162 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 5163 | return PyLong_FromUnsignedLong(hint); |
| 5164 | } |
| 5165 | |
| 5166 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 5167 | "Ticket life time hint."); |
| 5168 | |
| 5169 | |
| 5170 | static PyObject * |
| 5171 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 5172 | const unsigned char *id; |
| 5173 | unsigned int len; |
| 5174 | id = SSL_SESSION_get_id(self->session, &len); |
| 5175 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 5176 | } |
| 5177 | |
| 5178 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 5179 | "Session id"); |
| 5180 | |
| 5181 | |
| 5182 | static PyObject * |
| 5183 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 5184 | if (SSL_SESSION_has_ticket(self->session)) { |
| 5185 | Py_RETURN_TRUE; |
| 5186 | } else { |
| 5187 | Py_RETURN_FALSE; |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5192 | "Does the session contain a ticket?"); |
| 5193 | |
| 5194 | |
| 5195 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5196 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5197 | PySSLSession_get_has_ticket_doc}, |
| 5198 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5199 | PySSLSession_get_session_id_doc}, |
| 5200 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5201 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5202 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5203 | PySSLSession_get_time_doc}, |
| 5204 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5205 | PySSLSession_get_timeout_doc}, |
| 5206 | {NULL}, /* sentinel */ |
| 5207 | }; |
| 5208 | |
| 5209 | static PyTypeObject PySSLSession_Type = { |
| 5210 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5211 | "_ssl.Session", /*tp_name*/ |
| 5212 | sizeof(PySSLSession), /*tp_basicsize*/ |
| 5213 | 0, /*tp_itemsize*/ |
| 5214 | (destructor)PySSLSession_dealloc, /*tp_dealloc*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5215 | 0, /*tp_vectorcall_offset*/ |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5216 | 0, /*tp_getattr*/ |
| 5217 | 0, /*tp_setattr*/ |
| Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5218 | 0, /*tp_as_async*/ |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5219 | 0, /*tp_repr*/ |
| 5220 | 0, /*tp_as_number*/ |
| 5221 | 0, /*tp_as_sequence*/ |
| 5222 | 0, /*tp_as_mapping*/ |
| 5223 | 0, /*tp_hash*/ |
| 5224 | 0, /*tp_call*/ |
| 5225 | 0, /*tp_str*/ |
| 5226 | 0, /*tp_getattro*/ |
| 5227 | 0, /*tp_setattro*/ |
| 5228 | 0, /*tp_as_buffer*/ |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5229 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5230 | 0, /*tp_doc*/ |
| 5231 | (traverseproc)PySSLSession_traverse, /*tp_traverse*/ |
| 5232 | (inquiry)PySSLSession_clear, /*tp_clear*/ |
| 5233 | PySSLSession_richcompare, /*tp_richcompare*/ |
| 5234 | 0, /*tp_weaklistoffset*/ |
| 5235 | 0, /*tp_iter*/ |
| 5236 | 0, /*tp_iternext*/ |
| 5237 | 0, /*tp_methods*/ |
| 5238 | 0, /*tp_members*/ |
| 5239 | PySSLSession_getsetlist, /*tp_getset*/ |
| 5240 | }; |
| 5241 | |
| 5242 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5243 | /* helper routines for seeding the SSL PRNG */ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5244 | /*[clinic input] |
| 5245 | _ssl.RAND_add |
| Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5246 | string as view: Py_buffer(accept={str, buffer}) |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5247 | entropy: double |
| 5248 | / |
| 5249 | |
| 5250 | Mix string into the OpenSSL PRNG state. |
| 5251 | |
| 5252 | entropy (a float) is a lower bound on the entropy contained in |
| Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5253 | string. See RFC 4086. |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5254 | [clinic start generated code]*/ |
| 5255 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5256 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5257 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
| Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5258 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5259 | { |
| Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5260 | const char *buf; |
| Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5261 | Py_ssize_t len, written; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5262 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5263 | buf = (const char *)view->buf; |
| 5264 | len = view->len; |
| Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5265 | do { |
| 5266 | written = Py_MIN(len, INT_MAX); |
| 5267 | RAND_add(buf, (int)written, entropy); |
| 5268 | buf += written; |
| 5269 | len -= written; |
| 5270 | } while (len); |
| Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5271 | Py_RETURN_NONE; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5274 | static PyObject * |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5275 | PySSL_RAND(int len, int pseudo) |
| 5276 | { |
| 5277 | int ok; |
| 5278 | PyObject *bytes; |
| 5279 | unsigned long err; |
| 5280 | const char *errstr; |
| 5281 | PyObject *v; |
| 5282 | |
| Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5283 | if (len < 0) { |
| 5284 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5285 | return NULL; |
| 5286 | } |
| 5287 | |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5288 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5289 | if (bytes == NULL) |
| 5290 | return NULL; |
| 5291 | if (pseudo) { |
| 5292 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5293 | if (ok == 0 || ok == 1) |
| 5294 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5295 | } |
| 5296 | else { |
| 5297 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5298 | if (ok == 1) |
| 5299 | return bytes; |
| 5300 | } |
| 5301 | Py_DECREF(bytes); |
| 5302 | |
| 5303 | err = ERR_get_error(); |
| 5304 | errstr = ERR_reason_error_string(err); |
| 5305 | v = Py_BuildValue("(ks)", err, errstr); |
| 5306 | if (v != NULL) { |
| 5307 | PyErr_SetObject(PySSLErrorObject, v); |
| 5308 | Py_DECREF(v); |
| 5309 | } |
| 5310 | return NULL; |
| 5311 | } |
| 5312 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5313 | /*[clinic input] |
| 5314 | _ssl.RAND_bytes |
| 5315 | n: int |
| 5316 | / |
| 5317 | |
| 5318 | Generate n cryptographically strong pseudo-random bytes. |
| 5319 | [clinic start generated code]*/ |
| 5320 | |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5321 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5322 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5323 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5324 | { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5325 | return PySSL_RAND(n, 0); |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5328 | /*[clinic input] |
| 5329 | _ssl.RAND_pseudo_bytes |
| 5330 | n: int |
| 5331 | / |
| 5332 | |
| 5333 | Generate n pseudo-random bytes. |
| 5334 | |
| 5335 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5336 | if the bytes generated are cryptographically strong. |
| 5337 | [clinic start generated code]*/ |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5338 | |
| 5339 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5340 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5341 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5342 | { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5343 | return PySSL_RAND(n, 1); |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5346 | /*[clinic input] |
| 5347 | _ssl.RAND_status |
| 5348 | |
| 5349 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 5350 | |
| 5351 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5352 | using the ssl() function. |
| 5353 | [clinic start generated code]*/ |
| Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5354 | |
| 5355 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5356 | _ssl_RAND_status_impl(PyObject *module) |
| 5357 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5358 | { |
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5359 | return PyLong_FromLong(RAND_status()); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
| Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5362 | #ifndef OPENSSL_NO_EGD |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5363 | /* LCOV_EXCL_START */ |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5364 | /*[clinic input] |
| 5365 | _ssl.RAND_egd |
| 5366 | path: object(converter="PyUnicode_FSConverter") |
| 5367 | / |
| 5368 | |
| 5369 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 5370 | |
| 5371 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 5372 | fails or if it does not provide enough data to seed PRNG. |
| 5373 | [clinic start generated code]*/ |
| 5374 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5375 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5376 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 5377 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5378 | { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5379 | int bytes = RAND_egd(PyBytes_AsString(path)); |
| Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5380 | Py_DECREF(path); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5381 | if (bytes == -1) { |
| Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5382 | PyErr_SetString(PySSLErrorObject, |
| 5383 | "EGD connection failed or EGD did not return " |
| 5384 | "enough data to seed the PRNG"); |
| 5385 | return NULL; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5386 | } |
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5387 | return PyLong_FromLong(bytes); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5388 | } |
| Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5389 | /* LCOV_EXCL_STOP */ |
| Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5390 | #endif /* OPENSSL_NO_EGD */ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5391 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5392 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5393 | |
| 5394 | /*[clinic input] |
| 5395 | _ssl.get_default_verify_paths |
| 5396 | |
| 5397 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5398 | |
| 5399 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5400 | [clinic start generated code]*/ |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5401 | |
| 5402 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5403 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5404 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5405 | { |
| 5406 | PyObject *ofile_env = NULL; |
| 5407 | PyObject *ofile = NULL; |
| 5408 | PyObject *odir_env = NULL; |
| 5409 | PyObject *odir = NULL; |
| 5410 | |
| Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5411 | #define CONVERT(info, target) { \ |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5412 | const char *tmp = (info); \ |
| 5413 | target = NULL; \ |
| 5414 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5415 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5416 | target = PyBytes_FromString(tmp); } \ |
| 5417 | if (!target) goto error; \ |
| Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5418 | } |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5419 | |
| Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5420 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5421 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5422 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5423 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5424 | #undef CONVERT |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5425 | |
| Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5426 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
| Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5427 | |
| 5428 | error: |
| 5429 | Py_XDECREF(ofile_env); |
| 5430 | Py_XDECREF(ofile); |
| 5431 | Py_XDECREF(odir_env); |
| 5432 | Py_XDECREF(odir); |
| 5433 | return NULL; |
| 5434 | } |
| 5435 | |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5436 | static PyObject* |
| 5437 | asn1obj2py(ASN1_OBJECT *obj) |
| 5438 | { |
| 5439 | int nid; |
| 5440 | const char *ln, *sn; |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5441 | |
| 5442 | nid = OBJ_obj2nid(obj); |
| 5443 | if (nid == NID_undef) { |
| 5444 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5445 | return NULL; |
| 5446 | } |
| 5447 | sn = OBJ_nid2sn(nid); |
| 5448 | ln = OBJ_nid2ln(nid); |
| Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5449 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5450 | } |
| 5451 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5452 | /*[clinic input] |
| 5453 | _ssl.txt2obj |
| 5454 | txt: str |
| 5455 | name: bool = False |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5456 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5457 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5458 | |
| 5459 | By default objects are looked up by OID. With name=True short and |
| 5460 | long name are also matched. |
| 5461 | [clinic start generated code]*/ |
| 5462 | |
| 5463 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5464 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5465 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5466 | { |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5467 | PyObject *result = NULL; |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5468 | ASN1_OBJECT *obj; |
| 5469 | |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5470 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5471 | if (obj == NULL) { |
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5472 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5473 | return NULL; |
| 5474 | } |
| 5475 | result = asn1obj2py(obj); |
| 5476 | ASN1_OBJECT_free(obj); |
| 5477 | return result; |
| 5478 | } |
| 5479 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5480 | /*[clinic input] |
| 5481 | _ssl.nid2obj |
| 5482 | nid: int |
| 5483 | / |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5484 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5485 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5486 | [clinic start generated code]*/ |
| 5487 | |
| 5488 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5489 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5490 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5491 | { |
| 5492 | PyObject *result = NULL; |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5493 | ASN1_OBJECT *obj; |
| 5494 | |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5495 | if (nid < NID_undef) { |
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5496 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5497 | return NULL; |
| 5498 | } |
| 5499 | obj = OBJ_nid2obj(nid); |
| 5500 | if (obj == NULL) { |
| Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5501 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
| Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5502 | return NULL; |
| 5503 | } |
| 5504 | result = asn1obj2py(obj); |
| 5505 | ASN1_OBJECT_free(obj); |
| 5506 | return result; |
| 5507 | } |
| 5508 | |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5509 | #ifdef _MSC_VER |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5510 | |
| 5511 | static PyObject* |
| 5512 | certEncodingType(DWORD encodingType) |
| 5513 | { |
| 5514 | static PyObject *x509_asn = NULL; |
| 5515 | static PyObject *pkcs_7_asn = NULL; |
| 5516 | |
| 5517 | if (x509_asn == NULL) { |
| 5518 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5519 | if (x509_asn == NULL) |
| 5520 | return NULL; |
| 5521 | } |
| 5522 | if (pkcs_7_asn == NULL) { |
| 5523 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5524 | if (pkcs_7_asn == NULL) |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | switch(encodingType) { |
| 5528 | case X509_ASN_ENCODING: |
| 5529 | Py_INCREF(x509_asn); |
| 5530 | return x509_asn; |
| 5531 | case PKCS_7_ASN_ENCODING: |
| 5532 | Py_INCREF(pkcs_7_asn); |
| 5533 | return pkcs_7_asn; |
| 5534 | default: |
| 5535 | return PyLong_FromLong(encodingType); |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | static PyObject* |
| 5540 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5541 | { |
| 5542 | CERT_ENHKEY_USAGE *usage; |
| 5543 | DWORD size, error, i; |
| 5544 | PyObject *retval; |
| 5545 | |
| 5546 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5547 | error = GetLastError(); |
| 5548 | if (error == CRYPT_E_NOT_FOUND) { |
| 5549 | Py_RETURN_TRUE; |
| 5550 | } |
| 5551 | return PyErr_SetFromWindowsErr(error); |
| 5552 | } |
| 5553 | |
| 5554 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5555 | if (usage == NULL) { |
| 5556 | return PyErr_NoMemory(); |
| 5557 | } |
| 5558 | |
| 5559 | /* Now get the actual enhanced usage property */ |
| 5560 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5561 | PyMem_Free(usage); |
| 5562 | error = GetLastError(); |
| 5563 | if (error == CRYPT_E_NOT_FOUND) { |
| 5564 | Py_RETURN_TRUE; |
| 5565 | } |
| 5566 | return PyErr_SetFromWindowsErr(error); |
| 5567 | } |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5568 | retval = PyFrozenSet_New(NULL); |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5569 | if (retval == NULL) { |
| 5570 | goto error; |
| 5571 | } |
| 5572 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5573 | if (usage->rgpszUsageIdentifier[i]) { |
| 5574 | PyObject *oid; |
| 5575 | int err; |
| 5576 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5577 | if (oid == NULL) { |
| 5578 | Py_CLEAR(retval); |
| 5579 | goto error; |
| 5580 | } |
| 5581 | err = PySet_Add(retval, oid); |
| 5582 | Py_DECREF(oid); |
| 5583 | if (err == -1) { |
| 5584 | Py_CLEAR(retval); |
| 5585 | goto error; |
| 5586 | } |
| 5587 | } |
| 5588 | } |
| 5589 | error: |
| 5590 | PyMem_Free(usage); |
| 5591 | return retval; |
| 5592 | } |
| 5593 | |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5594 | static HCERTSTORE |
| 5595 | ssl_collect_certificates(const char *store_name) |
| 5596 | { |
| 5597 | /* this function collects the system certificate stores listed in |
| 5598 | * system_stores into a collection certificate store for being |
| 5599 | * enumerated. The store must be readable to be added to the |
| 5600 | * store collection. |
| 5601 | */ |
| 5602 | |
| 5603 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5604 | static DWORD system_stores[] = { |
| 5605 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5606 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5607 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5608 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5609 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5610 | CERT_SYSTEM_STORE_SERVICES, |
| 5611 | CERT_SYSTEM_STORE_USERS}; |
| 5612 | size_t i, storesAdded; |
| 5613 | BOOL result; |
| 5614 | |
| 5615 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5616 | (HCRYPTPROV)NULL, 0, NULL); |
| 5617 | if (!hCollectionStore) { |
| 5618 | return NULL; |
| 5619 | } |
| 5620 | storesAdded = 0; |
| 5621 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5622 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5623 | (HCRYPTPROV)NULL, |
| 5624 | CERT_STORE_READONLY_FLAG | |
| 5625 | system_stores[i], store_name); |
| 5626 | if (hSystemStore) { |
| 5627 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5628 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5629 | if (result) { |
| 5630 | ++storesAdded; |
| 5631 | } |
| neonene | ed70129 | 2019-09-09 21:33:43 +0900 | [diff] [blame] | 5632 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | if (storesAdded == 0) { |
| 5636 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5637 | return NULL; |
| 5638 | } |
| 5639 | |
| 5640 | return hCollectionStore; |
| 5641 | } |
| 5642 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5643 | /*[clinic input] |
| 5644 | _ssl.enum_certificates |
| 5645 | store_name: str |
| 5646 | |
| 5647 | Retrieve certificates from Windows' cert store. |
| 5648 | |
| 5649 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5650 | more cert storages, too. The function returns a list of (bytes, |
| 5651 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5652 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5653 | a set of OIDs or the boolean True. |
| 5654 | [clinic start generated code]*/ |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5655 | |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5656 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5657 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5658 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5659 | { |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5660 | HCERTSTORE hCollectionStore = NULL; |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5661 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5662 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5663 | PyObject *result = NULL; |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5664 | |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5665 | result = PySet_New(NULL); |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5666 | if (result == NULL) { |
| 5667 | return NULL; |
| 5668 | } |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5669 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5670 | if (hCollectionStore == NULL) { |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5671 | Py_DECREF(result); |
| 5672 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5673 | } |
| 5674 | |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5675 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5676 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5677 | pCertCtx->cbCertEncoded); |
| 5678 | if (!cert) { |
| 5679 | Py_CLEAR(result); |
| 5680 | break; |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5681 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5682 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5683 | Py_CLEAR(result); |
| 5684 | break; |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5685 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5686 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5687 | if (keyusage == Py_True) { |
| 5688 | Py_DECREF(keyusage); |
| 5689 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5690 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5691 | if (keyusage == NULL) { |
| 5692 | Py_CLEAR(result); |
| 5693 | break; |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5694 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5695 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5696 | Py_CLEAR(result); |
| 5697 | break; |
| 5698 | } |
| 5699 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5700 | cert = NULL; |
| 5701 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5702 | enc = NULL; |
| 5703 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5704 | keyusage = NULL; |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5705 | if (PySet_Add(result, tup) == -1) { |
| 5706 | Py_CLEAR(result); |
| 5707 | Py_CLEAR(tup); |
| 5708 | break; |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5709 | } |
| 5710 | Py_CLEAR(tup); |
| 5711 | } |
| 5712 | if (pCertCtx) { |
| 5713 | /* loop ended with an error, need to clean up context manually */ |
| 5714 | CertFreeCertificateContext(pCertCtx); |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /* In error cases cert, enc and tup may not be NULL */ |
| 5718 | Py_XDECREF(cert); |
| 5719 | Py_XDECREF(enc); |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5720 | Py_XDECREF(keyusage); |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5721 | Py_XDECREF(tup); |
| 5722 | |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5723 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5724 | associated with the store, in this case our collection store and the |
| 5725 | associated system stores. */ |
| 5726 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5727 | /* This error case might shadow another exception.*/ |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5728 | Py_XDECREF(result); |
| 5729 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5730 | } |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5731 | |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5732 | /* convert set to list */ |
| 5733 | if (result == NULL) { |
| 5734 | return NULL; |
| 5735 | } else { |
| 5736 | PyObject *lst = PySequence_List(result); |
| 5737 | Py_DECREF(result); |
| 5738 | return lst; |
| 5739 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5740 | } |
| 5741 | |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5742 | /*[clinic input] |
| 5743 | _ssl.enum_crls |
| 5744 | store_name: str |
| 5745 | |
| 5746 | Retrieve CRLs from Windows' cert store. |
| 5747 | |
| 5748 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5749 | more cert storages, too. The function returns a list of (bytes, |
| 5750 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5751 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5752 | [clinic start generated code]*/ |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5753 | |
| 5754 | static PyObject * |
| Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5755 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5756 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5757 | { |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5758 | HCERTSTORE hCollectionStore = NULL; |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5759 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5760 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5761 | PyObject *result = NULL; |
| 5762 | |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5763 | result = PySet_New(NULL); |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5764 | if (result == NULL) { |
| 5765 | return NULL; |
| 5766 | } |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5767 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5768 | if (hCollectionStore == NULL) { |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5769 | Py_DECREF(result); |
| 5770 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5771 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5772 | |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5773 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5774 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5775 | pCrlCtx->cbCrlEncoded); |
| 5776 | if (!crl) { |
| 5777 | Py_CLEAR(result); |
| 5778 | break; |
| 5779 | } |
| 5780 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5781 | Py_CLEAR(result); |
| 5782 | break; |
| 5783 | } |
| 5784 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5785 | Py_CLEAR(result); |
| 5786 | break; |
| 5787 | } |
| 5788 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5789 | crl = NULL; |
| 5790 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5791 | enc = NULL; |
| 5792 | |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5793 | if (PySet_Add(result, tup) == -1) { |
| 5794 | Py_CLEAR(result); |
| 5795 | Py_CLEAR(tup); |
| 5796 | break; |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5797 | } |
| 5798 | Py_CLEAR(tup); |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5799 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5800 | if (pCrlCtx) { |
| 5801 | /* loop ended with an error, need to clean up context manually */ |
| 5802 | CertFreeCRLContext(pCrlCtx); |
| 5803 | } |
| 5804 | |
| 5805 | /* In error cases cert, enc and tup may not be NULL */ |
| 5806 | Py_XDECREF(crl); |
| 5807 | Py_XDECREF(enc); |
| 5808 | Py_XDECREF(tup); |
| 5809 | |
| kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5810 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5811 | associated with the store, in this case our collection store and the |
| 5812 | associated system stores. */ |
| 5813 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5814 | /* This error case might shadow another exception.*/ |
| 5815 | Py_XDECREF(result); |
| 5816 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5817 | } |
| Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5818 | /* convert set to list */ |
| 5819 | if (result == NULL) { |
| 5820 | return NULL; |
| 5821 | } else { |
| 5822 | PyObject *lst = PySequence_List(result); |
| 5823 | Py_DECREF(result); |
| 5824 | return lst; |
| 5825 | } |
| Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5826 | } |
| Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5827 | |
| 5828 | #endif /* _MSC_VER */ |
| Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5829 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5830 | /* List of functions exported by this module. */ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5831 | static PyMethodDef PySSL_methods[] = { |
| Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5832 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5833 | _SSL_RAND_ADD_METHODDEF |
| 5834 | _SSL_RAND_BYTES_METHODDEF |
| 5835 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5836 | _SSL_RAND_EGD_METHODDEF |
| 5837 | _SSL_RAND_STATUS_METHODDEF |
| 5838 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5839 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5840 | _SSL_ENUM_CRLS_METHODDEF |
| 5841 | _SSL_TXT2OBJ_METHODDEF |
| 5842 | _SSL_NID2OBJ_METHODDEF |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5843 | {NULL, NULL} /* Sentinel */ |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5844 | }; |
| 5845 | |
| 5846 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5847 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5848 | |
| 5849 | /* an implementation of OpenSSL threading operations in terms |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5850 | * of the Python C thread library |
| 5851 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5852 | */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5853 | |
| 5854 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5855 | |
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5856 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5857 | /* use new CRYPTO_THREADID API. */ |
| 5858 | static void |
| 5859 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5860 | { |
| Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5861 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5862 | } |
| 5863 | #else |
| 5864 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5865 | static unsigned long |
| 5866 | _ssl_thread_id_function (void) { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5867 | return PyThread_get_thread_ident(); |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5868 | } |
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5869 | #endif |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5870 | |
| Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5871 | static void _ssl_thread_locking_function |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5872 | (int mode, int n, const char *file, int line) { |
| 5873 | /* this function is needed to perform locking on shared data |
| 5874 | structures. (Note that OpenSSL uses a number of global data |
| 5875 | structures that will be implicitly shared whenever multiple |
| 5876 | threads use OpenSSL.) Multi-threaded applications will |
| 5877 | crash at random if it is not set. |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5878 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5879 | locking_function() must be able to handle up to |
| 5880 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5881 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5882 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5883 | file and line are the file number of the function setting the |
| 5884 | lock. They can be useful for debugging. |
| 5885 | */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5886 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5887 | if ((_ssl_locks == NULL) || |
| 5888 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5889 | return; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5890 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5891 | if (mode & CRYPTO_LOCK) { |
| 5892 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5893 | } else { |
| 5894 | PyThread_release_lock(_ssl_locks[n]); |
| 5895 | } |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | static int _setup_ssl_threads(void) { |
| 5899 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5900 | unsigned int i; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5901 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5902 | if (_ssl_locks == NULL) { |
| 5903 | _ssl_locks_count = CRYPTO_num_locks(); |
| Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5904 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5905 | sizeof(PyThread_type_lock)); |
| Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5906 | if (_ssl_locks == NULL) { |
| 5907 | PyErr_NoMemory(); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5908 | return 0; |
| Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5909 | } |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5910 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5911 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5912 | if (_ssl_locks[i] == NULL) { |
| 5913 | unsigned int j; |
| 5914 | for (j = 0; j < i; j++) { |
| 5915 | PyThread_free_lock(_ssl_locks[j]); |
| 5916 | } |
| Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5917 | PyMem_Free(_ssl_locks); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5918 | return 0; |
| 5919 | } |
| 5920 | } |
| 5921 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5922 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5923 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5924 | #else |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5925 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
| Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5926 | #endif |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5927 | } |
| 5928 | return 1; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5931 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5932 | |
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5933 | PyDoc_STRVAR(module_doc, |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5934 | "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] | 5935 | for documentation."); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5936 | |
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5937 | |
| 5938 | static struct PyModuleDef _sslmodule = { |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5939 | PyModuleDef_HEAD_INIT, |
| 5940 | "_ssl", |
| 5941 | module_doc, |
| 5942 | -1, |
| 5943 | PySSL_methods, |
| 5944 | NULL, |
| 5945 | NULL, |
| 5946 | NULL, |
| 5947 | NULL |
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5948 | }; |
| 5949 | |
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5950 | |
| 5951 | static void |
| 5952 | parse_openssl_version(unsigned long libver, |
| 5953 | unsigned int *major, unsigned int *minor, |
| 5954 | unsigned int *fix, unsigned int *patch, |
| 5955 | unsigned int *status) |
| 5956 | { |
| 5957 | *status = libver & 0xF; |
| 5958 | libver >>= 4; |
| 5959 | *patch = libver & 0xFF; |
| 5960 | libver >>= 8; |
| 5961 | *fix = libver & 0xFF; |
| 5962 | libver >>= 8; |
| 5963 | *minor = libver & 0xFF; |
| 5964 | libver >>= 8; |
| 5965 | *major = libver & 0xFF; |
| 5966 | } |
| 5967 | |
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5968 | PyMODINIT_FUNC |
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5969 | PyInit__ssl(void) |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5970 | { |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5971 | PyObject *m, *d, *r, *bases; |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5972 | unsigned long libver; |
| 5973 | unsigned int major, minor, fix, patch, status; |
| 5974 | PySocketModule_APIObject *socket_api; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5975 | struct py_ssl_error_code *errcode; |
| 5976 | struct py_ssl_library_code *libcode; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5977 | |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5978 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 5979 | return NULL; |
| 5980 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5981 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5982 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) |
| 5983 | return NULL; |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5984 | if (PyType_Ready(&PySSLSession_Type) < 0) |
| 5985 | return NULL; |
| 5986 | |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5987 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5988 | m = PyModule_Create(&_sslmodule); |
| 5989 | if (m == NULL) |
| 5990 | return NULL; |
| 5991 | d = PyModule_GetDict(m); |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5992 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5993 | /* Load _socket module and its C API */ |
| 5994 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 5995 | if (!socket_api) |
| 5996 | return NULL; |
| 5997 | PySocketModule = *socket_api; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5998 | |
| Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5999 | #ifndef OPENSSL_VERSION_1_1 |
| 6000 | /* Load all algorithms and initialize cpuid */ |
| 6001 | OPENSSL_add_all_algorithms_noconf(); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6002 | /* Init OpenSSL */ |
| 6003 | SSL_load_error_strings(); |
| 6004 | SSL_library_init(); |
| Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 6005 | #endif |
| 6006 | |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6007 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6008 | /* note that this will start threading if not already started */ |
| 6009 | if (!_setup_ssl_threads()) { |
| 6010 | return NULL; |
| 6011 | } |
| Christian Heimes | c087a26 | 2020-05-15 20:55:25 +0200 | [diff] [blame^] | 6012 | #elif OPENSSL_VERSION_1_1 |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6013 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 6014 | _ssl_locks_count++; |
| Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 6015 | #endif |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6016 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6017 | /* Add symbols to module dict */ |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6018 | sslerror_type_slots[0].pfunc = PyExc_OSError; |
| 6019 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6020 | if (PySSLErrorObject == NULL) |
| 6021 | return NULL; |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6022 | |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6023 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 6024 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); |
| 6025 | if (bases == NULL) |
| 6026 | return NULL; |
| 6027 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( |
| 6028 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, |
| 6029 | bases, NULL); |
| 6030 | Py_DECREF(bases); |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6031 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 6032 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 6033 | PySSLErrorObject, NULL); |
| 6034 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 6035 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 6036 | PySSLErrorObject, NULL); |
| 6037 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 6038 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 6039 | PySSLErrorObject, NULL); |
| 6040 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 6041 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 6042 | PySSLErrorObject, NULL); |
| 6043 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 6044 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 6045 | PySSLErrorObject, NULL); |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6046 | if (PySSLCertVerificationErrorObject == NULL |
| 6047 | || PySSLZeroReturnErrorObject == NULL |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6048 | || PySSLWantReadErrorObject == NULL |
| 6049 | || PySSLWantWriteErrorObject == NULL |
| 6050 | || PySSLSyscallErrorObject == NULL |
| 6051 | || PySSLEOFErrorObject == NULL) |
| 6052 | return NULL; |
| 6053 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
| Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6054 | || PyDict_SetItemString(d, "SSLCertVerificationError", |
| 6055 | PySSLCertVerificationErrorObject) != 0 |
| Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6056 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 6057 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 6058 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 6059 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 6060 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6061 | return NULL; |
| Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 6062 | if (PyDict_SetItemString(d, "_SSLContext", |
| 6063 | (PyObject *)&PySSLContext_Type) != 0) |
| 6064 | return NULL; |
| 6065 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 6066 | (PyObject *)&PySSLSocket_Type) != 0) |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6067 | return NULL; |
| Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 6068 | if (PyDict_SetItemString(d, "MemoryBIO", |
| 6069 | (PyObject *)&PySSLMemoryBIO_Type) != 0) |
| 6070 | return NULL; |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6071 | if (PyDict_SetItemString(d, "SSLSession", |
| 6072 | (PyObject *)&PySSLSession_Type) != 0) |
| 6073 | return NULL; |
| 6074 | |
| Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 6075 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 6076 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 6077 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6078 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 6079 | PY_SSL_ERROR_ZERO_RETURN); |
| 6080 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 6081 | PY_SSL_ERROR_WANT_READ); |
| 6082 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 6083 | PY_SSL_ERROR_WANT_WRITE); |
| 6084 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 6085 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 6086 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 6087 | PY_SSL_ERROR_SYSCALL); |
| 6088 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 6089 | PY_SSL_ERROR_SSL); |
| 6090 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 6091 | PY_SSL_ERROR_WANT_CONNECT); |
| 6092 | /* non ssl.h errorcodes */ |
| 6093 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 6094 | PY_SSL_ERROR_EOF); |
| 6095 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 6096 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 6097 | /* cert requirements */ |
| 6098 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 6099 | PY_SSL_CERT_NONE); |
| 6100 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 6101 | PY_SSL_CERT_OPTIONAL); |
| 6102 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 6103 | PY_SSL_CERT_REQUIRED); |
| Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 6104 | /* CRL verification for verification_flags */ |
| 6105 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 6106 | 0); |
| 6107 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 6108 | X509_V_FLAG_CRL_CHECK); |
| 6109 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 6110 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6111 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 6112 | X509_V_FLAG_X509_STRICT); |
| Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 6113 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 6114 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 6115 | X509_V_FLAG_TRUSTED_FIRST); |
| 6116 | #endif |
| Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 6117 | |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6118 | /* Alert Descriptions from ssl.h */ |
| 6119 | /* note RESERVED constants no longer intended for use have been removed */ |
| 6120 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 6121 | |
| 6122 | #define ADD_AD_CONSTANT(s) \ |
| 6123 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 6124 | SSL_AD_##s) |
| 6125 | |
| 6126 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 6127 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 6128 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 6129 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 6130 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 6131 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 6132 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 6133 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 6134 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 6135 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 6136 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 6137 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 6138 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 6139 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 6140 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 6141 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 6142 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 6143 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 6144 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 6145 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 6146 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6147 | /* Not all constants are in old OpenSSL versions */ |
| Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 6148 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 6149 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 6150 | #endif |
| 6151 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 6152 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 6153 | #endif |
| 6154 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 6155 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 6156 | #endif |
| Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6157 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 6158 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 6159 | #endif |
| 6160 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 6161 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 6162 | #endif |
| 6163 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 6164 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 6165 | #endif |
| 6166 | |
| 6167 | #undef ADD_AD_CONSTANT |
| 6168 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6169 | /* protocol versions */ |
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6170 | #ifndef OPENSSL_NO_SSL2 |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6171 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 6172 | PY_SSL_VERSION_SSL2); |
| Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6173 | #endif |
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6174 | #ifndef OPENSSL_NO_SSL3 |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6175 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 6176 | PY_SSL_VERSION_SSL3); |
| Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6177 | #endif |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6178 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6179 | PY_SSL_VERSION_TLS); |
| 6180 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 6181 | PY_SSL_VERSION_TLS); |
| Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 6182 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 6183 | PY_SSL_VERSION_TLS_CLIENT); |
| 6184 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 6185 | PY_SSL_VERSION_TLS_SERVER); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6186 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 6187 | PY_SSL_VERSION_TLS1); |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6188 | #if HAVE_TLSv1_2 |
| 6189 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 6190 | PY_SSL_VERSION_TLS1_1); |
| 6191 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 6192 | PY_SSL_VERSION_TLS1_2); |
| 6193 | #endif |
| Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6194 | |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6195 | /* protocol options */ |
| Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 6196 | PyModule_AddIntConstant(m, "OP_ALL", |
| 6197 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6198 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 6199 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 6200 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
| Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6201 | #if HAVE_TLSv1_2 |
| 6202 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 6203 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
| 6204 | #endif |
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6205 | #ifdef SSL_OP_NO_TLSv1_3 |
| 6206 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 6207 | #else |
| 6208 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 6209 | #endif |
| Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 6210 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 6211 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
| Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 6212 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
| Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6213 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
| Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6214 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 6215 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
| Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6216 | #endif |
| Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 6217 | #ifdef SSL_OP_NO_COMPRESSION |
| 6218 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 6219 | SSL_OP_NO_COMPRESSION); |
| 6220 | #endif |
| Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 6221 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 6222 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 6223 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 6224 | #endif |
| Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 6225 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 6226 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 6227 | SSL_OP_NO_RENEGOTIATION); |
| 6228 | #endif |
| Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6229 | |
| Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 6230 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 6231 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 6232 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 6233 | #endif |
| 6234 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 6235 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 6236 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 6237 | #endif |
| 6238 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 6239 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 6240 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 6241 | #endif |
| 6242 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 6243 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 6244 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 6245 | #endif |
| 6246 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 6247 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 6248 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 6249 | #endif |
| 6250 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 6251 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 6252 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 6253 | #endif |
| 6254 | |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6255 | /* protocol versions */ |
| 6256 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 6257 | PY_PROTO_MINIMUM_SUPPORTED); |
| 6258 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 6259 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 6260 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 6261 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 6262 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 6263 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 6264 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 6265 | |
| Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6266 | #define addbool(m, key, value) \ |
| 6267 | do { \ |
| 6268 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 6269 | Py_INCREF(bool_obj); \ |
| 6270 | PyModule_AddObject((m), (key), bool_obj); \ |
| 6271 | } while (0) |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6272 | |
| 6273 | #if HAVE_SNI |
| 6274 | addbool(m, "HAS_SNI", 1); |
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6275 | #else |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6276 | addbool(m, "HAS_SNI", 0); |
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6277 | #endif |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6278 | |
| 6279 | addbool(m, "HAS_TLS_UNIQUE", 1); |
| 6280 | |
| 6281 | #ifndef OPENSSL_NO_ECDH |
| 6282 | addbool(m, "HAS_ECDH", 1); |
| 6283 | #else |
| 6284 | addbool(m, "HAS_ECDH", 0); |
| 6285 | #endif |
| Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6286 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6287 | #if HAVE_NPN |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6288 | addbool(m, "HAS_NPN", 1); |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6289 | #else |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6290 | addbool(m, "HAS_NPN", 0); |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6291 | #endif |
| Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6292 | |
| Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6293 | #if HAVE_ALPN |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6294 | addbool(m, "HAS_ALPN", 1); |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6295 | #else |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6296 | addbool(m, "HAS_ALPN", 0); |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6297 | #endif |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6298 | |
| 6299 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 6300 | addbool(m, "HAS_SSLv2", 1); |
| 6301 | #else |
| 6302 | addbool(m, "HAS_SSLv2", 0); |
| 6303 | #endif |
| 6304 | |
| 6305 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 6306 | addbool(m, "HAS_SSLv3", 1); |
| 6307 | #else |
| 6308 | addbool(m, "HAS_SSLv3", 0); |
| 6309 | #endif |
| 6310 | |
| 6311 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 6312 | addbool(m, "HAS_TLSv1", 1); |
| 6313 | #else |
| 6314 | addbool(m, "HAS_TLSv1", 0); |
| 6315 | #endif |
| 6316 | |
| 6317 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 6318 | addbool(m, "HAS_TLSv1_1", 1); |
| 6319 | #else |
| 6320 | addbool(m, "HAS_TLSv1_1", 0); |
| 6321 | #endif |
| 6322 | |
| 6323 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 6324 | addbool(m, "HAS_TLSv1_2", 1); |
| 6325 | #else |
| 6326 | addbool(m, "HAS_TLSv1_2", 0); |
| 6327 | #endif |
| Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6328 | |
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6329 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6330 | addbool(m, "HAS_TLSv1_3", 1); |
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6331 | #else |
| Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6332 | addbool(m, "HAS_TLSv1_3", 0); |
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6333 | #endif |
| Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6334 | |
| Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6335 | /* Mappings for error codes */ |
| 6336 | err_codes_to_names = PyDict_New(); |
| 6337 | err_names_to_codes = PyDict_New(); |
| 6338 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 6339 | return NULL; |
| 6340 | errcode = error_codes; |
| 6341 | while (errcode->mnemonic != NULL) { |
| 6342 | PyObject *mnemo, *key; |
| 6343 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 6344 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 6345 | if (mnemo == NULL || key == NULL) |
| 6346 | return NULL; |
| 6347 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 6348 | return NULL; |
| 6349 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 6350 | return NULL; |
| 6351 | Py_DECREF(key); |
| 6352 | Py_DECREF(mnemo); |
| 6353 | errcode++; |
| 6354 | } |
| 6355 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 6356 | return NULL; |
| 6357 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 6358 | return NULL; |
| 6359 | |
| 6360 | lib_codes_to_names = PyDict_New(); |
| 6361 | if (lib_codes_to_names == NULL) |
| 6362 | return NULL; |
| 6363 | libcode = library_codes; |
| 6364 | while (libcode->library != NULL) { |
| 6365 | PyObject *mnemo, *key; |
| 6366 | key = PyLong_FromLong(libcode->code); |
| 6367 | mnemo = PyUnicode_FromString(libcode->library); |
| 6368 | if (key == NULL || mnemo == NULL) |
| 6369 | return NULL; |
| 6370 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 6371 | return NULL; |
| 6372 | Py_DECREF(key); |
| 6373 | Py_DECREF(mnemo); |
| 6374 | libcode++; |
| 6375 | } |
| 6376 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 6377 | return NULL; |
| Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 6378 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6379 | /* OpenSSL version */ |
| 6380 | /* SSLeay() gives us the version of the library linked against, |
| 6381 | which could be different from the headers version. |
| 6382 | */ |
| 6383 | libver = SSLeay(); |
| 6384 | r = PyLong_FromUnsignedLong(libver); |
| 6385 | if (r == NULL) |
| 6386 | return NULL; |
| 6387 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6388 | return NULL; |
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6389 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6390 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6391 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6392 | return NULL; |
| 6393 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 6394 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6395 | return NULL; |
| Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6396 | |
| Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6397 | libver = OPENSSL_VERSION_NUMBER; |
| 6398 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6399 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6400 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6401 | return NULL; |
| 6402 | |
| Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6403 | return m; |
| Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6404 | } |