Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 17 | #define PY_SSIZE_T_CLEAN |
| 18 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 21 | #include "pythread.h" |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 22 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 23 | /* Redefined below for Windows debug builds after important #includes */ |
| 24 | #define _PySSL_FIX_ERRNO |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 25 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 26 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 27 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 28 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 29 | do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } _PySSL_FIX_ERRNO; } while (0) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 30 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 31 | PyThreadState *_save = NULL; \ |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 32 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 33 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 34 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 35 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 37 | /* Include symbols from _socket module */ |
| 38 | #include "socketmodule.h" |
| 39 | |
| 40 | static PySocketModule_APIObject PySocketModule; |
| 41 | |
| 42 | #if defined(HAVE_POLL_H) |
| 43 | #include <poll.h> |
| 44 | #elif defined(HAVE_SYS_POLL_H) |
| 45 | #include <sys/poll.h> |
| 46 | #endif |
| 47 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 48 | /* Don't warn about deprecated functions */ |
| 49 | #ifdef __GNUC__ |
| 50 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 51 | #endif |
| 52 | #ifdef __clang__ |
| 53 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 54 | #endif |
| 55 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 56 | /* Include OpenSSL header files */ |
| 57 | #include "openssl/rsa.h" |
| 58 | #include "openssl/crypto.h" |
| 59 | #include "openssl/x509.h" |
| 60 | #include "openssl/x509v3.h" |
| 61 | #include "openssl/pem.h" |
| 62 | #include "openssl/ssl.h" |
| 63 | #include "openssl/err.h" |
| 64 | #include "openssl/rand.h" |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 65 | #include "openssl/bio.h" |
Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 66 | #include "openssl/dh.h" |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 67 | |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 68 | #ifndef HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 69 | # ifdef LIBRESSL_VERSION_NUMBER |
| 70 | # error "LibreSSL is missing X509_VERIFY_PARAM_set1_host(), see https://github.com/libressl-portable/portable/issues/381" |
| 71 | # elif OPENSSL_VERSION_NUMBER > 0x1000200fL |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 72 | # define HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 73 | # else |
| 74 | # error "libssl is too old and does not support X509_VERIFY_PARAM_set1_host()" |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 75 | # endif |
| 76 | #endif |
| 77 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 78 | /* SSL error object */ |
| 79 | static PyObject *PySSLErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 80 | static PyObject *PySSLCertVerificationErrorObject; |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 81 | static PyObject *PySSLZeroReturnErrorObject; |
| 82 | static PyObject *PySSLWantReadErrorObject; |
| 83 | static PyObject *PySSLWantWriteErrorObject; |
| 84 | static PyObject *PySSLSyscallErrorObject; |
| 85 | static PyObject *PySSLEOFErrorObject; |
| 86 | |
| 87 | /* Error mappings */ |
| 88 | static PyObject *err_codes_to_names; |
| 89 | static PyObject *err_names_to_codes; |
| 90 | static PyObject *lib_codes_to_names; |
| 91 | |
| 92 | struct py_ssl_error_code { |
| 93 | const char *mnemonic; |
| 94 | int library, reason; |
| 95 | }; |
| 96 | struct py_ssl_library_code { |
| 97 | const char *library; |
| 98 | int code; |
| 99 | }; |
| 100 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 101 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 102 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 103 | * However, because it uses a different CRT, we need to transfer the |
| 104 | * value of errno from OpenSSL into our debug CRT. |
| 105 | * |
| 106 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 107 | * alternative is to do both debug and release builds of OpenSSL, which |
| 108 | * requires much uglier code to transform their automatically generated |
| 109 | * makefile. This is the lesser of all the evils. |
| 110 | */ |
| 111 | |
| 112 | static void _PySSLFixErrno(void) { |
| 113 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 114 | if (!ucrtbase) { |
| 115 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 116 | * have a catastrophic failure, but this function is not the |
| 117 | * place to raise it. */ |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | typedef int *(__stdcall *errno_func)(void); |
| 122 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 123 | if (ssl_errno) { |
| 124 | errno = *ssl_errno(); |
| 125 | *ssl_errno() = 0; |
| 126 | } else { |
| 127 | errno = ENOTRECOVERABLE; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #undef _PySSL_FIX_ERRNO |
| 132 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 133 | #endif |
| 134 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 135 | /* Include generated data (error codes) */ |
| 136 | #include "_ssl_data.h" |
| 137 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 138 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 139 | # define OPENSSL_VERSION_1_1 1 |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 140 | # define PY_OPENSSL_1_1_API 1 |
| 141 | #endif |
| 142 | |
| 143 | /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ |
| 144 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL |
| 145 | # define PY_OPENSSL_1_1_API 1 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 146 | #endif |
| 147 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 148 | /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 |
| 149 | http://www.openssl.org/news/changelog.html |
| 150 | */ |
| 151 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 152 | # define HAVE_TLSv1_2 1 |
| 153 | #else |
| 154 | # define HAVE_TLSv1_2 0 |
| 155 | #endif |
| 156 | |
Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 157 | /* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 158 | * This includes the SSL_set_SSL_CTX() function. |
| 159 | */ |
| 160 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 161 | # define HAVE_SNI 1 |
| 162 | #else |
| 163 | # define HAVE_SNI 0 |
| 164 | #endif |
| 165 | |
Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 166 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 167 | # define HAVE_ALPN 1 |
| 168 | #else |
| 169 | # define HAVE_ALPN 0 |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 170 | #endif |
| 171 | |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 172 | /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped |
| 173 | * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility |
| 174 | * reasons. The check for TLSEXT_TYPE_next_proto_neg works with |
| 175 | * OpenSSL 1.0.1+ and LibreSSL. |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 176 | * OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg. |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 177 | */ |
| 178 | #ifdef OPENSSL_NO_NEXTPROTONEG |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 179 | # define HAVE_NPN 0 |
| 180 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 181 | # define HAVE_NPN 0 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 182 | #elif defined(TLSEXT_TYPE_next_proto_neg) |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 183 | # define HAVE_NPN 1 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 184 | #else |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 185 | # define HAVE_NPN 0 |
| 186 | #endif |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 187 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 188 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 189 | #define HAVE_OPENSSL_KEYLOG 1 |
| 190 | #endif |
| 191 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 192 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 193 | #define INVALID_SOCKET (-1) |
| 194 | #endif |
| 195 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 196 | /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ |
| 197 | #ifndef OPENSSL_VERSION_1_1 |
| 198 | #define HAVE_OPENSSL_CRYPTO_LOCK |
| 199 | #endif |
| 200 | |
| 201 | #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 202 | #define OPENSSL_NO_SSL2 |
| 203 | #endif |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 204 | |
| 205 | #ifndef PY_OPENSSL_1_1_API |
| 206 | /* 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] | 207 | |
| 208 | #define TLS_method SSLv23_method |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 209 | #define TLS_client_method SSLv23_client_method |
| 210 | #define TLS_server_method SSLv23_server_method |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 211 | |
| 212 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 213 | { |
| 214 | return ne->set; |
| 215 | } |
| 216 | |
| 217 | #ifndef OPENSSL_NO_COMP |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 218 | /* LCOV_EXCL_START */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 219 | static int COMP_get_type(const COMP_METHOD *meth) |
| 220 | { |
| 221 | return meth->type; |
| 222 | } |
Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 223 | /* LCOV_EXCL_STOP */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 224 | #endif |
| 225 | |
| 226 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 227 | { |
| 228 | return ctx->default_passwd_callback; |
| 229 | } |
| 230 | |
| 231 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 232 | { |
| 233 | return ctx->default_passwd_callback_userdata; |
| 234 | } |
| 235 | |
| 236 | static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 237 | { |
| 238 | return x->type; |
| 239 | } |
| 240 | |
| 241 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 242 | { |
| 243 | return x->data.x509; |
| 244 | } |
| 245 | |
| 246 | static int BIO_up_ref(BIO *b) |
| 247 | { |
| 248 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 253 | return store->objs; |
| 254 | } |
| 255 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 256 | static int |
| 257 | SSL_SESSION_has_ticket(const SSL_SESSION *s) |
| 258 | { |
| 259 | return (s->tlsext_ticklen > 0) ? 1 : 0; |
| 260 | } |
| 261 | |
| 262 | static unsigned long |
| 263 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
| 264 | { |
| 265 | return s->tlsext_tick_lifetime_hint; |
| 266 | } |
| 267 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 268 | #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 269 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 270 | /* Default cipher suites */ |
| 271 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 272 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 273 | #endif |
| 274 | |
| 275 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 276 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 277 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 278 | #endif |
| 279 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 280 | /* Python custom selection of sensible cipher suites |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 281 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. |
| 282 | * !aNULL:!eNULL: really no NULL ciphers |
| 283 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. |
| 284 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
| 285 | * !SRP:!PSK: no secure remote password or pre-shared key authentication |
| 286 | */ |
| 287 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" |
| 288 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 289 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 290 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 291 | #else |
| 292 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 293 | #endif |
| 294 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 295 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 296 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 297 | /* these mirror ssl.h */ |
| 298 | PY_SSL_ERROR_NONE, |
| 299 | PY_SSL_ERROR_SSL, |
| 300 | PY_SSL_ERROR_WANT_READ, |
| 301 | PY_SSL_ERROR_WANT_WRITE, |
| 302 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 303 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 304 | PY_SSL_ERROR_ZERO_RETURN, |
| 305 | PY_SSL_ERROR_WANT_CONNECT, |
| 306 | /* start of non ssl.h errorcodes */ |
| 307 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 308 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 309 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 310 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 311 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 312 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 313 | PY_SSL_CLIENT, |
| 314 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 318 | PY_SSL_CERT_NONE, |
| 319 | PY_SSL_CERT_OPTIONAL, |
| 320 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 324 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 325 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 326 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 327 | #if HAVE_TLSv1_2 |
| 328 | PY_SSL_VERSION_TLS1, |
| 329 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 330 | PY_SSL_VERSION_TLS1_2, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 331 | #else |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 332 | PY_SSL_VERSION_TLS1, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 333 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 334 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 335 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 336 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 337 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 338 | enum py_proto_version { |
| 339 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 340 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 341 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 342 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 343 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 344 | #ifdef TLS1_3_VERSION |
| 345 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 346 | #else |
| 347 | PY_PROTO_TLSv1_3 = 0x304, |
| 348 | #endif |
| 349 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 350 | |
| 351 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 352 | * available version, and the other way around. We have to figure out the |
| 353 | * minimum and maximum available version on our own and hope for the best. |
| 354 | */ |
| 355 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 356 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 357 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 358 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 359 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 360 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 361 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 362 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 363 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 364 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 365 | #else |
| 366 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 367 | #endif |
| 368 | |
| 369 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 370 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 371 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 372 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 373 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 374 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 375 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 376 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 377 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 378 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 379 | #else |
| 380 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 381 | #endif |
| 382 | }; |
| 383 | |
| 384 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 385 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 386 | /* 0 means no, greater than 0 means yes */ |
| 387 | |
| 388 | static unsigned int _ssl_locks_count = 0; |
| 389 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 390 | /* SSL socket object */ |
| 391 | |
| 392 | #define X509_NAME_MAXLEN 256 |
| 393 | |
Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 394 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 395 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 396 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 397 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 398 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 399 | #else |
| 400 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 401 | #endif |
| 402 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 403 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 404 | * older SSL, but let's be safe */ |
| 405 | #define PySSL_CB_MAXLEN 128 |
| 406 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 407 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 408 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 409 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 410 | SSL_CTX *ctx; |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 411 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 412 | unsigned char *npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 413 | int npn_protocols_len; |
| 414 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 415 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 416 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 417 | unsigned int alpn_protocols_len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 418 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 419 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 420 | PyObject *set_sni_cb; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 421 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 422 | int check_hostname; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 423 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 424 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 425 | */ |
| 426 | unsigned int hostflags; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 427 | int protocol; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 428 | #ifdef TLS1_3_VERSION |
| 429 | int post_handshake_auth; |
| 430 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 431 | PyObject *msg_cb; |
| 432 | #ifdef HAVE_OPENSSL_KEYLOG |
| 433 | PyObject *keylog_filename; |
| 434 | BIO *keylog_bio; |
| 435 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 436 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 438 | typedef struct { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 439 | int ssl; /* last seen error from SSL */ |
| 440 | int c; /* last seen error from libc */ |
| 441 | #ifdef MS_WINDOWS |
| 442 | int ws; /* last seen error from winsock */ |
| 443 | #endif |
| 444 | } _PySSLError; |
| 445 | |
| 446 | typedef struct { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 447 | PyObject_HEAD |
| 448 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 449 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 450 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 451 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 452 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 453 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 454 | PyObject *server_hostname; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 455 | _PySSLError err; /* last seen error from various sources */ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 456 | /* Some SSL callbacks don't have error reporting. Callback wrappers |
| 457 | * store exception information on the socket. The handshake, read, write, |
| 458 | * and shutdown methods check for chained exceptions. |
| 459 | */ |
| 460 | PyObject *exc_type; |
| 461 | PyObject *exc_value; |
| 462 | PyObject *exc_tb; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 463 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 465 | typedef struct { |
| 466 | PyObject_HEAD |
| 467 | BIO *bio; |
| 468 | int eof_written; |
| 469 | } PySSLMemoryBIO; |
| 470 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 471 | typedef struct { |
| 472 | PyObject_HEAD |
| 473 | SSL_SESSION *session; |
| 474 | PySSLContext *ctx; |
| 475 | } PySSLSession; |
| 476 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 477 | static PyTypeObject PySSLContext_Type; |
| 478 | static PyTypeObject PySSLSocket_Type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 479 | static PyTypeObject PySSLMemoryBIO_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 480 | static PyTypeObject PySSLSession_Type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 481 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 482 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 483 | { |
| 484 | _PySSLError err = { 0 }; |
| 485 | if (failed) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 486 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 487 | err.ws = WSAGetLastError(); |
| 488 | _PySSL_FIX_ERRNO; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 489 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 490 | err.c = errno; |
| 491 | err.ssl = SSL_get_error(ssl, retcode); |
| 492 | } |
| 493 | return err; |
| 494 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 495 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 496 | /*[clinic input] |
| 497 | module _ssl |
| 498 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" |
| 499 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" |
| 500 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 501 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 502 | [clinic start generated code]*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 503 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 504 | |
| 505 | #include "clinic/_ssl.c.h" |
| 506 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 507 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 508 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 509 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 510 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 511 | #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 512 | #define PySSLMemoryBIO_Check(v) (Py_TYPE(v) == &PySSLMemoryBIO_Type) |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 513 | #define PySSLSession_Check(v) (Py_TYPE(v) == &PySSLSession_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 514 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 515 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 516 | SOCKET_IS_NONBLOCKING, |
| 517 | SOCKET_IS_BLOCKING, |
| 518 | SOCKET_HAS_TIMED_OUT, |
| 519 | SOCKET_HAS_BEEN_CLOSED, |
| 520 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 521 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 522 | } timeout_state; |
| 523 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 524 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 525 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 526 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 528 | /* Get the socket from a PySSLSocket, if it has one */ |
| 529 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 530 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 531 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 532 | /* If sock is NULL, use a timeout of 0 second */ |
| 533 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 534 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 535 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 536 | #include "_ssl/debughelpers.c" |
| 537 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 538 | /* |
| 539 | * SSL errors. |
| 540 | */ |
| 541 | |
| 542 | PyDoc_STRVAR(SSLError_doc, |
| 543 | "An error occurred in the SSL implementation."); |
| 544 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 545 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 546 | "A certificate could not be verified."); |
| 547 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 548 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 549 | "SSL/TLS session closed cleanly."); |
| 550 | |
| 551 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 552 | "Non-blocking SSL socket needs to read more data\n" |
| 553 | "before the requested operation can be completed."); |
| 554 | |
| 555 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 556 | "Non-blocking SSL socket needs to write more data\n" |
| 557 | "before the requested operation can be completed."); |
| 558 | |
| 559 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 560 | "System error when attempting SSL operation."); |
| 561 | |
| 562 | PyDoc_STRVAR(SSLEOFError_doc, |
| 563 | "SSL/TLS connection terminated abruptly."); |
| 564 | |
| 565 | static PyObject * |
| 566 | SSLError_str(PyOSErrorObject *self) |
| 567 | { |
| 568 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 569 | Py_INCREF(self->strerror); |
| 570 | return self->strerror; |
| 571 | } |
| 572 | else |
| 573 | return PyObject_Str(self->args); |
| 574 | } |
| 575 | |
| 576 | static PyType_Slot sslerror_type_slots[] = { |
| 577 | {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] | 578 | {Py_tp_doc, (void*)SSLError_doc}, |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 579 | {Py_tp_str, SSLError_str}, |
| 580 | {0, 0}, |
| 581 | }; |
| 582 | |
| 583 | static PyType_Spec sslerror_type_spec = { |
| 584 | "ssl.SSLError", |
| 585 | sizeof(PyOSErrorObject), |
| 586 | 0, |
| 587 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 588 | sslerror_type_slots |
| 589 | }; |
| 590 | |
| 591 | static void |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 592 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 593 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 594 | { |
| 595 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 596 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 597 | PyObject *init_value, *msg, *key; |
| 598 | _Py_IDENTIFIER(reason); |
| 599 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 600 | _Py_IDENTIFIER(verify_message); |
| 601 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 602 | |
| 603 | if (errcode != 0) { |
| 604 | int lib, reason; |
| 605 | |
| 606 | lib = ERR_GET_LIB(errcode); |
| 607 | reason = ERR_GET_REASON(errcode); |
| 608 | key = Py_BuildValue("ii", lib, reason); |
| 609 | if (key == NULL) |
| 610 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 611 | reason_obj = PyDict_GetItemWithError(err_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 612 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 613 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 614 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 615 | } |
| 616 | key = PyLong_FromLong(lib); |
| 617 | if (key == NULL) |
| 618 | goto fail; |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 619 | lib_obj = PyDict_GetItemWithError(lib_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 620 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 621 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 622 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 623 | } |
| 624 | if (errstr == NULL) |
| 625 | errstr = ERR_reason_error_string(errcode); |
| 626 | } |
| 627 | if (errstr == NULL) |
| 628 | errstr = "unknown error"; |
| 629 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 630 | /* verify code for cert validation error */ |
| 631 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 632 | const char *verify_str = NULL; |
| 633 | long verify_code; |
| 634 | |
| 635 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 636 | verify_code_obj = PyLong_FromLong(verify_code); |
| 637 | if (verify_code_obj == NULL) { |
| 638 | goto fail; |
| 639 | } |
| 640 | |
| 641 | switch (verify_code) { |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 642 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 643 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 644 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 645 | verify_obj = PyUnicode_FromFormat( |
| 646 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 647 | sslsock->server_hostname |
| 648 | ); |
| 649 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 650 | #endif |
| 651 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 652 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 653 | verify_obj = PyUnicode_FromFormat( |
| 654 | "IP address mismatch, certificate is not valid for '%S'.", |
| 655 | sslsock->server_hostname |
| 656 | ); |
| 657 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 658 | #endif |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 659 | default: |
| 660 | verify_str = X509_verify_cert_error_string(verify_code); |
| 661 | if (verify_str != NULL) { |
| 662 | verify_obj = PyUnicode_FromString(verify_str); |
| 663 | } else { |
| 664 | verify_obj = Py_None; |
| 665 | Py_INCREF(verify_obj); |
| 666 | } |
| 667 | break; |
| 668 | } |
| 669 | if (verify_obj == NULL) { |
| 670 | goto fail; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (verify_obj && reason_obj && lib_obj) |
| 675 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 676 | lib_obj, reason_obj, errstr, verify_obj, |
| 677 | lineno); |
| 678 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 679 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 680 | lib_obj, reason_obj, errstr, lineno); |
| 681 | else if (lib_obj) |
| 682 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 683 | lib_obj, errstr, lineno); |
| 684 | else |
| 685 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 686 | if (msg == NULL) |
| 687 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 688 | |
Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 689 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 690 | if (init_value == NULL) |
| 691 | goto fail; |
| 692 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 693 | err_value = PyObject_CallObject(type, init_value); |
| 694 | Py_DECREF(init_value); |
| 695 | if (err_value == NULL) |
| 696 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 697 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 698 | if (reason_obj == NULL) |
| 699 | reason_obj = Py_None; |
| 700 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 701 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 702 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 703 | if (lib_obj == NULL) |
| 704 | lib_obj = Py_None; |
| 705 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 706 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 707 | |
| 708 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 709 | /* Only set verify code / message for SSLCertVerificationError */ |
| 710 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 711 | verify_code_obj)) |
| 712 | goto fail; |
| 713 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 714 | goto fail; |
| 715 | } |
| 716 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 717 | PyErr_SetObject(type, err_value); |
| 718 | fail: |
| 719 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 720 | Py_XDECREF(verify_code_obj); |
| 721 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 722 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 723 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 724 | static int |
| 725 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 726 | if (sslsock->exc_type == NULL) |
| 727 | return 0; |
| 728 | |
| 729 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 730 | sslsock->exc_type = NULL; |
| 731 | sslsock->exc_value = NULL; |
| 732 | sslsock->exc_tb = NULL; |
| 733 | return -1; |
| 734 | } |
| 735 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 736 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 737 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 738 | { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 739 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 740 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 741 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 742 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 743 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 745 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 746 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 747 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 748 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 749 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 750 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 751 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 752 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 753 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 754 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 755 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 756 | break; |
| 757 | case SSL_ERROR_WANT_READ: |
| 758 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 759 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 760 | p = PY_SSL_ERROR_WANT_READ; |
| 761 | break; |
| 762 | case SSL_ERROR_WANT_WRITE: |
| 763 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 764 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 765 | errstr = "The operation did not complete (write)"; |
| 766 | break; |
| 767 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 768 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 769 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 770 | break; |
| 771 | case SSL_ERROR_WANT_CONNECT: |
| 772 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 773 | errstr = "The operation did not complete (connect)"; |
| 774 | break; |
| 775 | case SSL_ERROR_SYSCALL: |
| 776 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 777 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 778 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 779 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 780 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 781 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 782 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 783 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 784 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 785 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 786 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 787 | if (err.ws) { |
| 788 | return PyErr_SetFromWindowsErr(err.ws); |
| 789 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 790 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 791 | if (err.c) { |
| 792 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 793 | return PyErr_SetFromErrno(PyExc_OSError); |
| 794 | } |
| 795 | Py_INCREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 796 | s->errorhandler(); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 797 | Py_DECREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 798 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 799 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 800 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 801 | type = PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 802 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 803 | } |
| 804 | } else { |
| 805 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 806 | } |
| 807 | break; |
| 808 | } |
| 809 | case SSL_ERROR_SSL: |
| 810 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 811 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 812 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 813 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 814 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 815 | } |
| 816 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 817 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
| 818 | type = PySSLCertVerificationErrorObject; |
| 819 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | default: |
| 823 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 824 | errstr = "Invalid error code"; |
| 825 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 826 | } |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 827 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 828 | ERR_clear_error(); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 829 | PySSL_ChainExceptions(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 830 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 833 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 834 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 836 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 837 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 838 | else |
| 839 | errcode = 0; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 840 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 841 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 842 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 845 | /* |
| 846 | * SSL objects |
| 847 | */ |
| 848 | |
| 849 | static int |
| 850 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 851 | { |
| 852 | int retval = -1; |
| 853 | ASN1_OCTET_STRING *ip; |
| 854 | PyObject *hostname; |
| 855 | size_t len; |
| 856 | |
| 857 | assert(server_hostname); |
| 858 | |
| 859 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 860 | * When name starts with a dot (e.g ".example.com"), it will be |
| 861 | * matched by a certificate valid for any sub-domain of name. |
| 862 | */ |
| 863 | len = strlen(server_hostname); |
| 864 | if (len == 0 || *server_hostname == '.') { |
| 865 | PyErr_SetString( |
| 866 | PyExc_ValueError, |
| 867 | "server_hostname cannot be an empty string or start with a " |
| 868 | "leading dot."); |
| 869 | return retval; |
| 870 | } |
| 871 | |
| 872 | /* inet_pton is not available on all platforms. */ |
| 873 | ip = a2i_IPADDRESS(server_hostname); |
| 874 | if (ip == NULL) { |
| 875 | ERR_clear_error(); |
| 876 | } |
| 877 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 878 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 879 | if (hostname == NULL) { |
| 880 | goto error; |
| 881 | } |
| 882 | self->server_hostname = hostname; |
| 883 | |
| 884 | /* Only send SNI extension for non-IP hostnames */ |
| 885 | if (ip == NULL) { |
| 886 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
| 887 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 888 | } |
| 889 | } |
| 890 | if (self->ctx->check_hostname) { |
| 891 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 892 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 893 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 894 | strlen(server_hostname))) { |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 895 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 896 | goto error; |
| 897 | } |
| 898 | } else { |
| 899 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip), |
| 900 | ASN1_STRING_length(ip))) { |
| 901 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 902 | goto error; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | retval = 0; |
| 907 | error: |
| 908 | if (ip != NULL) { |
| 909 | ASN1_OCTET_STRING_free(ip); |
| 910 | } |
| 911 | return retval; |
| 912 | } |
| 913 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 914 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 915 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 916 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 917 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 918 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 919 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 921 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 922 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 923 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 925 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 926 | if (self == NULL) |
| 927 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 929 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 930 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 931 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 932 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 933 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 934 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 935 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 936 | self->err = err; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 937 | self->exc_type = NULL; |
| 938 | self->exc_value = NULL; |
| 939 | self->exc_tb = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 940 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 941 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 942 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 944 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 945 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 946 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 947 | if (self->ssl == NULL) { |
| 948 | Py_DECREF(self); |
| 949 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 950 | return NULL; |
| 951 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 952 | SSL_set_app_data(self->ssl, self); |
| 953 | if (sock) { |
| 954 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 955 | } else { |
| 956 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 957 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 958 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 959 | BIO_up_ref(inbio->bio); |
| 960 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 961 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 962 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 963 | SSL_set_mode(self->ssl, |
| 964 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 965 | |
Christian Heimes | f22c4cf | 2019-07-01 09:25:48 +0200 | [diff] [blame] | 966 | #ifdef TLS1_3_VERSION |
| 967 | if (sslctx->post_handshake_auth == 1) { |
| 968 | if (socket_type == PY_SSL_SERVER) { |
| 969 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 970 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 971 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 972 | int mode = SSL_get_verify_mode(self->ssl); |
| 973 | if (mode & SSL_VERIFY_PEER) { |
| 974 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 975 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 976 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 977 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 978 | } |
| 979 | } else { |
| 980 | /* client socket */ |
| 981 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 982 | } |
| 983 | } |
| 984 | #endif |
| 985 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 986 | if (server_hostname != NULL) { |
| 987 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 988 | Py_DECREF(self); |
| 989 | return NULL; |
| 990 | } |
| 991 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 992 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 993 | * to non-blocking mode (blocking is the default) |
| 994 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 995 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 996 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 997 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 998 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1000 | PySSL_BEGIN_ALLOW_THREADS |
| 1001 | if (socket_type == PY_SSL_CLIENT) |
| 1002 | SSL_set_connect_state(self->ssl); |
| 1003 | else |
| 1004 | SSL_set_accept_state(self->ssl); |
| 1005 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 1006 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1007 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1008 | if (sock != NULL) { |
| 1009 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 1010 | if (self->Socket == NULL) { |
| 1011 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1012 | return NULL; |
| 1013 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 1014 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1015 | if (owner && owner != Py_None) { |
| 1016 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 1017 | Py_DECREF(self); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | } |
| 1021 | if (session && session != Py_None) { |
| 1022 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 1023 | Py_DECREF(self); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1027 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1030 | /* SSL object methods */ |
| 1031 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1032 | /*[clinic input] |
| 1033 | _ssl._SSLSocket.do_handshake |
| 1034 | [clinic start generated code]*/ |
| 1035 | |
| 1036 | static PyObject * |
| 1037 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 1038 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1039 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1040 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1041 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1042 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1043 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1044 | _PyTime_t timeout, deadline = 0; |
| 1045 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1047 | if (sock) { |
| 1048 | if (((PyObject*)sock) == Py_None) { |
| 1049 | _setSSLError("Underlying socket connection gone", |
| 1050 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | Py_INCREF(sock); |
| 1054 | |
| 1055 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1056 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1057 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1058 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1059 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1060 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1061 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 1062 | has_timeout = (timeout > 0); |
| 1063 | if (has_timeout) |
| 1064 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 1065 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1066 | /* Actually negotiate SSL connection */ |
| 1067 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1068 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1069 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1070 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1071 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1072 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1073 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1074 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1075 | if (PyErr_CheckSignals()) |
| 1076 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1077 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1078 | if (has_timeout) |
| 1079 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 1080 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1081 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1082 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1083 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1084 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1085 | } else { |
| 1086 | sockstate = SOCKET_OPERATION_OK; |
| 1087 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1088 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1089 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1090 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1091 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1092 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1093 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1094 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1095 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1096 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1097 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1098 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1099 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1100 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1101 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1102 | break; |
| 1103 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1104 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 1105 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1106 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1107 | if (ret < 1) |
| 1108 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1109 | if (PySSL_ChainExceptions(self) < 0) |
| 1110 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1111 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1112 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1113 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 1114 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1115 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1118 | static PyObject * |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1119 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 1120 | { |
| 1121 | char buf[X509_NAME_MAXLEN]; |
| 1122 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1123 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1124 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1125 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1126 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1127 | if (buflen < 0) { |
| 1128 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1129 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1130 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1131 | /* initial buffer is too small for oid + terminating null byte */ |
| 1132 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1133 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1134 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1135 | /* allocate len + 1 for terminating NULL byte */ |
| 1136 | namebuf = PyMem_Malloc(buflen + 1); |
| 1137 | if (namebuf == NULL) { |
| 1138 | PyErr_NoMemory(); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1142 | if (buflen < 0) { |
| 1143 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1144 | goto done; |
| 1145 | } |
| 1146 | } |
| 1147 | if (!buflen && no_name) { |
| 1148 | Py_INCREF(Py_None); |
| 1149 | name_obj = Py_None; |
| 1150 | } |
| 1151 | else { |
| 1152 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1153 | } |
| 1154 | |
| 1155 | done: |
| 1156 | if (buf != namebuf) { |
| 1157 | PyMem_Free(namebuf); |
| 1158 | } |
| 1159 | return name_obj; |
| 1160 | } |
| 1161 | |
| 1162 | static PyObject * |
| 1163 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 1164 | { |
| 1165 | Py_ssize_t buflen; |
| 1166 | unsigned char *valuebuf = NULL; |
| 1167 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1169 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1170 | if (buflen < 0) { |
| 1171 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1172 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1173 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1174 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1175 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1176 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1180 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1181 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1182 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1183 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1184 | PyObject *rdnt; |
| 1185 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1186 | int entry_count = X509_NAME_entry_count(xname); |
| 1187 | X509_NAME_ENTRY *entry; |
| 1188 | ASN1_OBJECT *name; |
| 1189 | ASN1_STRING *value; |
| 1190 | int index_counter; |
| 1191 | int rdn_level = -1; |
| 1192 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1193 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1194 | dn = PyList_New(0); |
| 1195 | if (dn == NULL) |
| 1196 | return NULL; |
| 1197 | /* now create another tuple to hold the top-level RDN */ |
| 1198 | rdn = PyList_New(0); |
| 1199 | if (rdn == NULL) |
| 1200 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1202 | for (index_counter = 0; |
| 1203 | index_counter < entry_count; |
| 1204 | index_counter++) |
| 1205 | { |
| 1206 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1208 | /* check to see if we've gotten to a new RDN */ |
| 1209 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1210 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1211 | /* yes, new RDN */ |
| 1212 | /* add old RDN to DN */ |
| 1213 | rdnt = PyList_AsTuple(rdn); |
| 1214 | Py_DECREF(rdn); |
| 1215 | if (rdnt == NULL) |
| 1216 | goto fail0; |
| 1217 | retcode = PyList_Append(dn, rdnt); |
| 1218 | Py_DECREF(rdnt); |
| 1219 | if (retcode < 0) |
| 1220 | goto fail0; |
| 1221 | /* create new RDN */ |
| 1222 | rdn = PyList_New(0); |
| 1223 | if (rdn == NULL) |
| 1224 | goto fail0; |
| 1225 | } |
| 1226 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1227 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1229 | /* now add this attribute to the current RDN */ |
| 1230 | name = X509_NAME_ENTRY_get_object(entry); |
| 1231 | value = X509_NAME_ENTRY_get_data(entry); |
| 1232 | attr = _create_tuple_for_attribute(name, value); |
| 1233 | /* |
| 1234 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1235 | entry->set, |
| 1236 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1237 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1238 | */ |
| 1239 | if (attr == NULL) |
| 1240 | goto fail1; |
| 1241 | retcode = PyList_Append(rdn, attr); |
| 1242 | Py_DECREF(attr); |
| 1243 | if (retcode < 0) |
| 1244 | goto fail1; |
| 1245 | } |
| 1246 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1247 | if (rdn != NULL) { |
| 1248 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1249 | rdnt = PyList_AsTuple(rdn); |
| 1250 | Py_DECREF(rdn); |
| 1251 | if (rdnt == NULL) |
| 1252 | goto fail0; |
| 1253 | retcode = PyList_Append(dn, rdnt); |
| 1254 | Py_DECREF(rdnt); |
| 1255 | if (retcode < 0) |
| 1256 | goto fail0; |
| 1257 | } |
| 1258 | else { |
| 1259 | Py_DECREF(rdn); |
| 1260 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1261 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1263 | /* convert list to tuple */ |
| 1264 | rdnt = PyList_AsTuple(dn); |
| 1265 | Py_DECREF(dn); |
| 1266 | if (rdnt == NULL) |
| 1267 | return NULL; |
| 1268 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1269 | |
| 1270 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1271 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1272 | |
| 1273 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1274 | Py_XDECREF(dn); |
| 1275 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static PyObject * |
| 1279 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1281 | /* this code follows the procedure outlined in |
| 1282 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1283 | function to extract the STACK_OF(GENERAL_NAME), |
| 1284 | then iterates through the stack to add the |
| 1285 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1286 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1287 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1288 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1289 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1290 | GENERAL_NAMES *names = NULL; |
| 1291 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1292 | BIO *biobuf = NULL; |
| 1293 | char buf[2048]; |
| 1294 | char *vptr; |
| 1295 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1296 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1297 | if (certificate == NULL) |
| 1298 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1299 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1300 | /* get a memory buffer */ |
| 1301 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1302 | if (biobuf == NULL) { |
| 1303 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1304 | return NULL; |
| 1305 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1306 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1307 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1308 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1309 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1310 | if (peer_alt_names == Py_None) { |
| 1311 | peer_alt_names = PyList_New(0); |
| 1312 | if (peer_alt_names == NULL) |
| 1313 | goto fail; |
| 1314 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1315 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1316 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1317 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1318 | int gntype; |
| 1319 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1321 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1322 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1323 | switch (gntype) { |
| 1324 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1325 | /* we special-case DirName as a tuple of |
| 1326 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1327 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1328 | t = PyTuple_New(2); |
| 1329 | if (t == NULL) { |
| 1330 | goto fail; |
| 1331 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1333 | v = PyUnicode_FromString("DirName"); |
| 1334 | if (v == NULL) { |
| 1335 | Py_DECREF(t); |
| 1336 | goto fail; |
| 1337 | } |
| 1338 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1339 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1340 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1341 | if (v == NULL) { |
| 1342 | Py_DECREF(t); |
| 1343 | goto fail; |
| 1344 | } |
| 1345 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1346 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1347 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1348 | case GEN_EMAIL: |
| 1349 | case GEN_DNS: |
| 1350 | case GEN_URI: |
| 1351 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1352 | correctly, CVE-2013-4238 */ |
| 1353 | t = PyTuple_New(2); |
| 1354 | if (t == NULL) |
| 1355 | goto fail; |
| 1356 | switch (gntype) { |
| 1357 | case GEN_EMAIL: |
| 1358 | v = PyUnicode_FromString("email"); |
| 1359 | as = name->d.rfc822Name; |
| 1360 | break; |
| 1361 | case GEN_DNS: |
| 1362 | v = PyUnicode_FromString("DNS"); |
| 1363 | as = name->d.dNSName; |
| 1364 | break; |
| 1365 | case GEN_URI: |
| 1366 | v = PyUnicode_FromString("URI"); |
| 1367 | as = name->d.uniformResourceIdentifier; |
| 1368 | break; |
| 1369 | } |
| 1370 | if (v == NULL) { |
| 1371 | Py_DECREF(t); |
| 1372 | goto fail; |
| 1373 | } |
| 1374 | PyTuple_SET_ITEM(t, 0, v); |
| 1375 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as), |
| 1376 | ASN1_STRING_length(as)); |
| 1377 | if (v == NULL) { |
| 1378 | Py_DECREF(t); |
| 1379 | goto fail; |
| 1380 | } |
| 1381 | PyTuple_SET_ITEM(t, 1, v); |
| 1382 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1383 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1384 | case GEN_RID: |
| 1385 | t = PyTuple_New(2); |
| 1386 | if (t == NULL) |
| 1387 | goto fail; |
| 1388 | |
| 1389 | v = PyUnicode_FromString("Registered ID"); |
| 1390 | if (v == NULL) { |
| 1391 | Py_DECREF(t); |
| 1392 | goto fail; |
| 1393 | } |
| 1394 | PyTuple_SET_ITEM(t, 0, v); |
| 1395 | |
| 1396 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1397 | if (len < 0) { |
| 1398 | Py_DECREF(t); |
| 1399 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1400 | goto fail; |
| 1401 | } else if (len >= (int)sizeof(buf)) { |
| 1402 | v = PyUnicode_FromString("<INVALID>"); |
| 1403 | } else { |
| 1404 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1405 | } |
| 1406 | if (v == NULL) { |
| 1407 | Py_DECREF(t); |
| 1408 | goto fail; |
| 1409 | } |
| 1410 | PyTuple_SET_ITEM(t, 1, v); |
| 1411 | break; |
| 1412 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1413 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1414 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1415 | switch (gntype) { |
| 1416 | /* check for new general name type */ |
| 1417 | case GEN_OTHERNAME: |
| 1418 | case GEN_X400: |
| 1419 | case GEN_EDIPARTY: |
| 1420 | case GEN_IPADD: |
| 1421 | case GEN_RID: |
| 1422 | break; |
| 1423 | default: |
| 1424 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1425 | "Unknown general name type %d", |
| 1426 | gntype) == -1) { |
| 1427 | goto fail; |
| 1428 | } |
| 1429 | break; |
| 1430 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1431 | (void) BIO_reset(biobuf); |
| 1432 | GENERAL_NAME_print(biobuf, name); |
| 1433 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1434 | if (len < 0) { |
| 1435 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1436 | goto fail; |
| 1437 | } |
| 1438 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1439 | if (vptr == NULL) { |
| 1440 | PyErr_Format(PyExc_ValueError, |
| 1441 | "Invalid value %.200s", |
| 1442 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1443 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1444 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1445 | t = PyTuple_New(2); |
| 1446 | if (t == NULL) |
| 1447 | goto fail; |
| 1448 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1449 | if (v == NULL) { |
| 1450 | Py_DECREF(t); |
| 1451 | goto fail; |
| 1452 | } |
| 1453 | PyTuple_SET_ITEM(t, 0, v); |
| 1454 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1455 | (len - (vptr - buf + 1))); |
| 1456 | if (v == NULL) { |
| 1457 | Py_DECREF(t); |
| 1458 | goto fail; |
| 1459 | } |
| 1460 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1461 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1462 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1464 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1465 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1466 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1467 | Py_DECREF(t); |
| 1468 | goto fail; |
| 1469 | } |
| 1470 | Py_DECREF(t); |
| 1471 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1472 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1473 | } |
| 1474 | BIO_free(biobuf); |
| 1475 | if (peer_alt_names != Py_None) { |
| 1476 | v = PyList_AsTuple(peer_alt_names); |
| 1477 | Py_DECREF(peer_alt_names); |
| 1478 | return v; |
| 1479 | } else { |
| 1480 | return peer_alt_names; |
| 1481 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1482 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1483 | |
| 1484 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1485 | if (biobuf != NULL) |
| 1486 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1488 | if (peer_alt_names != Py_None) { |
| 1489 | Py_XDECREF(peer_alt_names); |
| 1490 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1491 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1492 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1496 | _get_aia_uri(X509 *certificate, int nid) { |
| 1497 | PyObject *lst = NULL, *ostr = NULL; |
| 1498 | int i, result; |
| 1499 | AUTHORITY_INFO_ACCESS *info; |
| 1500 | |
| 1501 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1502 | if (info == NULL) |
| 1503 | return Py_None; |
| 1504 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1505 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1506 | return Py_None; |
| 1507 | } |
| 1508 | |
| 1509 | if ((lst = PyList_New(0)) == NULL) { |
| 1510 | goto fail; |
| 1511 | } |
| 1512 | |
| 1513 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1514 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1515 | ASN1_IA5STRING *uri; |
| 1516 | |
| 1517 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1518 | (ad->location->type != GEN_URI)) { |
| 1519 | continue; |
| 1520 | } |
| 1521 | uri = ad->location->d.uniformResourceIdentifier; |
| 1522 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1523 | uri->length); |
| 1524 | if (ostr == NULL) { |
| 1525 | goto fail; |
| 1526 | } |
| 1527 | result = PyList_Append(lst, ostr); |
| 1528 | Py_DECREF(ostr); |
| 1529 | if (result < 0) { |
| 1530 | goto fail; |
| 1531 | } |
| 1532 | } |
| 1533 | AUTHORITY_INFO_ACCESS_free(info); |
| 1534 | |
| 1535 | /* convert to tuple or None */ |
| 1536 | if (PyList_Size(lst) == 0) { |
| 1537 | Py_DECREF(lst); |
| 1538 | return Py_None; |
| 1539 | } else { |
| 1540 | PyObject *tup; |
| 1541 | tup = PyList_AsTuple(lst); |
| 1542 | Py_DECREF(lst); |
| 1543 | return tup; |
| 1544 | } |
| 1545 | |
| 1546 | fail: |
| 1547 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1548 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1549 | return NULL; |
| 1550 | } |
| 1551 | |
| 1552 | static PyObject * |
| 1553 | _get_crl_dp(X509 *certificate) { |
| 1554 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1555 | int i, j; |
| 1556 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1557 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1558 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1559 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1560 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1561 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1562 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1563 | lst = PyList_New(0); |
| 1564 | if (lst == NULL) |
| 1565 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1566 | |
| 1567 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1568 | DIST_POINT *dp; |
| 1569 | STACK_OF(GENERAL_NAME) *gns; |
| 1570 | |
| 1571 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1572 | if (dp->distpoint == NULL) { |
| 1573 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1574 | continue; |
| 1575 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1576 | gns = dp->distpoint->name.fullname; |
| 1577 | |
| 1578 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1579 | GENERAL_NAME *gn; |
| 1580 | ASN1_IA5STRING *uri; |
| 1581 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1582 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1583 | |
| 1584 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1585 | if (gn->type != GEN_URI) { |
| 1586 | continue; |
| 1587 | } |
| 1588 | uri = gn->d.uniformResourceIdentifier; |
| 1589 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1590 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1591 | if (ouri == NULL) |
| 1592 | goto done; |
| 1593 | |
| 1594 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1595 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1596 | if (err < 0) |
| 1597 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1598 | } |
| 1599 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1600 | |
| 1601 | /* Convert to tuple. */ |
| 1602 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1603 | |
| 1604 | done: |
| 1605 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1606 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1607 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1611 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1613 | PyObject *retval = NULL; |
| 1614 | BIO *biobuf = NULL; |
| 1615 | PyObject *peer; |
| 1616 | PyObject *peer_alt_names = NULL; |
| 1617 | PyObject *issuer; |
| 1618 | PyObject *version; |
| 1619 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1620 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1621 | ASN1_INTEGER *serialNumber; |
| 1622 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1623 | int len, result; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1624 | ASN1_TIME *notBefore, *notAfter; |
| 1625 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1627 | retval = PyDict_New(); |
| 1628 | if (retval == NULL) |
| 1629 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1631 | peer = _create_tuple_for_X509_NAME( |
| 1632 | X509_get_subject_name(certificate)); |
| 1633 | if (peer == NULL) |
| 1634 | goto fail0; |
| 1635 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1636 | Py_DECREF(peer); |
| 1637 | goto fail0; |
| 1638 | } |
| 1639 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1640 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1641 | issuer = _create_tuple_for_X509_NAME( |
| 1642 | X509_get_issuer_name(certificate)); |
| 1643 | if (issuer == NULL) |
| 1644 | goto fail0; |
| 1645 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1646 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1647 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1648 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1649 | Py_DECREF(issuer); |
| 1650 | |
| 1651 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1652 | if (version == NULL) |
| 1653 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1654 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1655 | Py_DECREF(version); |
| 1656 | goto fail0; |
| 1657 | } |
| 1658 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1660 | /* get a memory buffer */ |
| 1661 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1662 | if (biobuf == NULL) { |
| 1663 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1664 | goto fail0; |
| 1665 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1667 | (void) BIO_reset(biobuf); |
| 1668 | serialNumber = X509_get_serialNumber(certificate); |
| 1669 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1670 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1671 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1672 | if (len < 0) { |
| 1673 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1674 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1675 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1676 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1677 | if (sn_obj == NULL) |
| 1678 | goto fail1; |
| 1679 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1680 | Py_DECREF(sn_obj); |
| 1681 | goto fail1; |
| 1682 | } |
| 1683 | Py_DECREF(sn_obj); |
| 1684 | |
| 1685 | (void) BIO_reset(biobuf); |
| 1686 | notBefore = X509_get_notBefore(certificate); |
| 1687 | ASN1_TIME_print(biobuf, notBefore); |
| 1688 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1689 | if (len < 0) { |
| 1690 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1691 | goto fail1; |
| 1692 | } |
| 1693 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1694 | if (pnotBefore == NULL) |
| 1695 | goto fail1; |
| 1696 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1697 | Py_DECREF(pnotBefore); |
| 1698 | goto fail1; |
| 1699 | } |
| 1700 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1702 | (void) BIO_reset(biobuf); |
| 1703 | notAfter = X509_get_notAfter(certificate); |
| 1704 | ASN1_TIME_print(biobuf, notAfter); |
| 1705 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1706 | if (len < 0) { |
| 1707 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1708 | goto fail1; |
| 1709 | } |
| 1710 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1711 | if (pnotAfter == NULL) |
| 1712 | goto fail1; |
| 1713 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1714 | Py_DECREF(pnotAfter); |
| 1715 | goto fail1; |
| 1716 | } |
| 1717 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1719 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1721 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1722 | if (peer_alt_names == NULL) |
| 1723 | goto fail1; |
| 1724 | else if (peer_alt_names != Py_None) { |
| 1725 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1726 | peer_alt_names) < 0) { |
| 1727 | Py_DECREF(peer_alt_names); |
| 1728 | goto fail1; |
| 1729 | } |
| 1730 | Py_DECREF(peer_alt_names); |
| 1731 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1732 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1733 | /* Authority Information Access: OCSP URIs */ |
| 1734 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1735 | if (obj == NULL) { |
| 1736 | goto fail1; |
| 1737 | } else if (obj != Py_None) { |
| 1738 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1739 | Py_DECREF(obj); |
| 1740 | if (result < 0) { |
| 1741 | goto fail1; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1746 | if (obj == NULL) { |
| 1747 | goto fail1; |
| 1748 | } else if (obj != Py_None) { |
| 1749 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1750 | Py_DECREF(obj); |
| 1751 | if (result < 0) { |
| 1752 | goto fail1; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | /* CDP (CRL distribution points) */ |
| 1757 | obj = _get_crl_dp(certificate); |
| 1758 | if (obj == NULL) { |
| 1759 | goto fail1; |
| 1760 | } else if (obj != Py_None) { |
| 1761 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1762 | Py_DECREF(obj); |
| 1763 | if (result < 0) { |
| 1764 | goto fail1; |
| 1765 | } |
| 1766 | } |
| 1767 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1768 | BIO_free(biobuf); |
| 1769 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1770 | |
| 1771 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1772 | if (biobuf != NULL) |
| 1773 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1774 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1775 | Py_XDECREF(retval); |
| 1776 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1777 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1778 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1779 | static PyObject * |
| 1780 | _certificate_to_der(X509 *certificate) |
| 1781 | { |
| 1782 | unsigned char *bytes_buf = NULL; |
| 1783 | int len; |
| 1784 | PyObject *retval; |
| 1785 | |
| 1786 | bytes_buf = NULL; |
| 1787 | len = i2d_X509(certificate, &bytes_buf); |
| 1788 | if (len < 0) { |
| 1789 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | /* this is actually an immutable bytes sequence */ |
| 1793 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1794 | OPENSSL_free(bytes_buf); |
| 1795 | return retval; |
| 1796 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1797 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1798 | /*[clinic input] |
| 1799 | _ssl._test_decode_cert |
| 1800 | path: object(converter="PyUnicode_FSConverter") |
| 1801 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1802 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1803 | [clinic start generated code]*/ |
| 1804 | |
| 1805 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1806 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1807 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1808 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1809 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1810 | X509 *x=NULL; |
| 1811 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1813 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1814 | PyErr_SetString(PySSLErrorObject, |
| 1815 | "Can't malloc memory to read file"); |
| 1816 | goto fail0; |
| 1817 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1818 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1819 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1820 | PyErr_SetString(PySSLErrorObject, |
| 1821 | "Can't open file"); |
| 1822 | goto fail0; |
| 1823 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1824 | |
Miss Islington (bot) | f781283 | 2019-08-15 05:52:51 -0700 | [diff] [blame] | 1825 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1826 | if (x == NULL) { |
| 1827 | PyErr_SetString(PySSLErrorObject, |
| 1828 | "Error decoding PEM-encoded file"); |
| 1829 | goto fail0; |
| 1830 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1832 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1833 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1834 | |
| 1835 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1836 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1837 | if (cert != NULL) BIO_free(cert); |
| 1838 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1842 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1843 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1844 | der as binary_mode: bool = False |
| 1845 | / |
| 1846 | |
| 1847 | Returns the certificate for the peer. |
| 1848 | |
| 1849 | If no certificate was provided, returns None. If a certificate was |
| 1850 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1851 | returns a dict containing information about the peer certificate. |
| 1852 | |
| 1853 | If the optional argument is True, returns a DER-encoded copy of the |
| 1854 | peer certificate, or None if no certificate was provided. This will |
| 1855 | return the certificate even if it wasn't validated. |
| 1856 | [clinic start generated code]*/ |
| 1857 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1858 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1859 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1860 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
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 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1863 | X509 *peer_cert; |
| 1864 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1865 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1866 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1867 | PyErr_SetString(PyExc_ValueError, |
| 1868 | "handshake not done yet"); |
| 1869 | return NULL; |
| 1870 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1871 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1872 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1873 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1874 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1875 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1876 | /* return cert in DER-encoded format */ |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1877 | result = _certificate_to_der(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1878 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1879 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1880 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1881 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1882 | else |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1883 | result = _decode_certificate(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1884 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1885 | X509_free(peer_cert); |
| 1886 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1889 | static PyObject * |
| 1890 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1891 | { |
| 1892 | const char *cipher_name, *cipher_protocol; |
| 1893 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1894 | if (retval == NULL) |
| 1895 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1896 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1897 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1898 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1899 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1900 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1901 | } else { |
| 1902 | v = PyUnicode_FromString(cipher_name); |
| 1903 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1904 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1905 | PyTuple_SET_ITEM(retval, 0, v); |
| 1906 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1907 | |
| 1908 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1909 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1910 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1911 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1912 | } else { |
| 1913 | v = PyUnicode_FromString(cipher_protocol); |
| 1914 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1915 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1916 | PyTuple_SET_ITEM(retval, 1, v); |
| 1917 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1918 | |
| 1919 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1920 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1921 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1922 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1923 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1924 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1925 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1926 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1927 | Py_DECREF(retval); |
| 1928 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1931 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1932 | static PyObject * |
| 1933 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1934 | { |
| 1935 | const char *cipher_name, *cipher_protocol; |
| 1936 | |
| 1937 | unsigned long cipher_id; |
| 1938 | int alg_bits, strength_bits, len; |
| 1939 | char buf[512] = {0}; |
| 1940 | #if OPENSSL_VERSION_1_1 |
| 1941 | int aead, nid; |
| 1942 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 1943 | #endif |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1944 | |
| 1945 | /* can be NULL */ |
| 1946 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1947 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1948 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1949 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1950 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 1951 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1952 | if (len > 1 && buf[len-1] == '\n') |
| 1953 | buf[len-1] = '\0'; |
| 1954 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 1955 | |
| 1956 | #if OPENSSL_VERSION_1_1 |
| 1957 | aead = SSL_CIPHER_is_aead(cipher); |
| 1958 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 1959 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1960 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 1961 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1962 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 1963 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1964 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 1965 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1966 | #endif |
| 1967 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1968 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1969 | "{sksssssssisi" |
| 1970 | #if OPENSSL_VERSION_1_1 |
| 1971 | "sOssssssss" |
| 1972 | #endif |
| 1973 | "}", |
| 1974 | "id", cipher_id, |
| 1975 | "name", cipher_name, |
| 1976 | "protocol", cipher_protocol, |
| 1977 | "description", buf, |
| 1978 | "strength_bits", strength_bits, |
| 1979 | "alg_bits", alg_bits |
| 1980 | #if OPENSSL_VERSION_1_1 |
| 1981 | ,"aead", aead ? Py_True : Py_False, |
| 1982 | "symmetric", skcipher, |
| 1983 | "digest", digest, |
| 1984 | "kea", kx, |
| 1985 | "auth", auth |
| 1986 | #endif |
| 1987 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1988 | } |
| 1989 | #endif |
| 1990 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1991 | /*[clinic input] |
| 1992 | _ssl._SSLSocket.shared_ciphers |
| 1993 | [clinic start generated code]*/ |
| 1994 | |
| 1995 | static PyObject * |
| 1996 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 1997 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1998 | { |
| 1999 | STACK_OF(SSL_CIPHER) *ciphers; |
| 2000 | int i; |
| 2001 | PyObject *res; |
| 2002 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2003 | ciphers = SSL_get_ciphers(self->ssl); |
| 2004 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2005 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2006 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 2007 | if (!res) |
| 2008 | return NULL; |
| 2009 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 2010 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 2011 | if (!tup) { |
| 2012 | Py_DECREF(res); |
| 2013 | return NULL; |
| 2014 | } |
| 2015 | PyList_SET_ITEM(res, i, tup); |
| 2016 | } |
| 2017 | return res; |
| 2018 | } |
| 2019 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2020 | /*[clinic input] |
| 2021 | _ssl._SSLSocket.cipher |
| 2022 | [clinic start generated code]*/ |
| 2023 | |
| 2024 | static PyObject * |
| 2025 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 2026 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2027 | { |
| 2028 | const SSL_CIPHER *current; |
| 2029 | |
| 2030 | if (self->ssl == NULL) |
| 2031 | Py_RETURN_NONE; |
| 2032 | current = SSL_get_current_cipher(self->ssl); |
| 2033 | if (current == NULL) |
| 2034 | Py_RETURN_NONE; |
| 2035 | return cipher_to_tuple(current); |
| 2036 | } |
| 2037 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2038 | /*[clinic input] |
| 2039 | _ssl._SSLSocket.version |
| 2040 | [clinic start generated code]*/ |
| 2041 | |
| 2042 | static PyObject * |
| 2043 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 2044 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2045 | { |
| 2046 | const char *version; |
| 2047 | |
| 2048 | if (self->ssl == NULL) |
| 2049 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 2050 | if (!SSL_is_init_finished(self->ssl)) { |
| 2051 | /* handshake not finished */ |
| 2052 | Py_RETURN_NONE; |
| 2053 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2054 | version = SSL_get_version(self->ssl); |
| 2055 | if (!strcmp(version, "unknown")) |
| 2056 | Py_RETURN_NONE; |
| 2057 | return PyUnicode_FromString(version); |
| 2058 | } |
| 2059 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2060 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2061 | /*[clinic input] |
| 2062 | _ssl._SSLSocket.selected_npn_protocol |
| 2063 | [clinic start generated code]*/ |
| 2064 | |
| 2065 | static PyObject * |
| 2066 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 2067 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 2068 | { |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2069 | const unsigned char *out; |
| 2070 | unsigned int outlen; |
| 2071 | |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2072 | SSL_get0_next_proto_negotiated(self->ssl, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2073 | &out, &outlen); |
| 2074 | |
| 2075 | if (out == NULL) |
| 2076 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2077 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 2078 | } |
| 2079 | #endif |
| 2080 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2081 | #if HAVE_ALPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2082 | /*[clinic input] |
| 2083 | _ssl._SSLSocket.selected_alpn_protocol |
| 2084 | [clinic start generated code]*/ |
| 2085 | |
| 2086 | static PyObject * |
| 2087 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2088 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2089 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2090 | const unsigned char *out; |
| 2091 | unsigned int outlen; |
| 2092 | |
| 2093 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2094 | |
| 2095 | if (out == NULL) |
| 2096 | Py_RETURN_NONE; |
| 2097 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2098 | } |
| 2099 | #endif |
| 2100 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2101 | /*[clinic input] |
| 2102 | _ssl._SSLSocket.compression |
| 2103 | [clinic start generated code]*/ |
| 2104 | |
| 2105 | static PyObject * |
| 2106 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2107 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2108 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2109 | #ifdef OPENSSL_NO_COMP |
| 2110 | Py_RETURN_NONE; |
| 2111 | #else |
| 2112 | const COMP_METHOD *comp_method; |
| 2113 | const char *short_name; |
| 2114 | |
| 2115 | if (self->ssl == NULL) |
| 2116 | Py_RETURN_NONE; |
| 2117 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2118 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2119 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2120 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2121 | if (short_name == NULL) |
| 2122 | Py_RETURN_NONE; |
| 2123 | return PyUnicode_DecodeFSDefault(short_name); |
| 2124 | #endif |
| 2125 | } |
| 2126 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2127 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2128 | Py_INCREF(self->ctx); |
| 2129 | return self->ctx; |
| 2130 | } |
| 2131 | |
| 2132 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2133 | void *closure) { |
| 2134 | |
| 2135 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2136 | #if !HAVE_SNI |
| 2137 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 2138 | "context is not supported by your OpenSSL library"); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2139 | return -1; |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2140 | #else |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2141 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2142 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2143 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2144 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2145 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2146 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2147 | return -1; |
| 2148 | } |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2154 | "_setter_context(ctx)\n\ |
| 2155 | \ |
| 2156 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2157 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2158 | on the SSLContext to change the certificate information associated with the\n\ |
| 2159 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2160 | |
| 2161 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2162 | static PyObject * |
| 2163 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2164 | { |
| 2165 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2166 | } |
| 2167 | |
| 2168 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2169 | "Whether this is a server-side socket."); |
| 2170 | |
| 2171 | static PyObject * |
| 2172 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2173 | { |
| 2174 | if (self->server_hostname == NULL) |
| 2175 | Py_RETURN_NONE; |
| 2176 | Py_INCREF(self->server_hostname); |
| 2177 | return self->server_hostname; |
| 2178 | } |
| 2179 | |
| 2180 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2181 | "The currently set server hostname (for SNI)."); |
| 2182 | |
| 2183 | static PyObject * |
| 2184 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2185 | { |
| 2186 | PyObject *owner; |
| 2187 | |
| 2188 | if (self->owner == NULL) |
| 2189 | Py_RETURN_NONE; |
| 2190 | |
| 2191 | owner = PyWeakref_GetObject(self->owner); |
| 2192 | Py_INCREF(owner); |
| 2193 | return owner; |
| 2194 | } |
| 2195 | |
| 2196 | static int |
| 2197 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2198 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2199 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2200 | if (self->owner == NULL) |
| 2201 | return -1; |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2206 | "The Python-level owner of this object.\ |
| 2207 | Passed as \"self\" in servername callback."); |
| 2208 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2209 | static int |
| 2210 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2211 | { |
| 2212 | Py_VISIT(self->exc_type); |
| 2213 | Py_VISIT(self->exc_value); |
| 2214 | Py_VISIT(self->exc_tb); |
| 2215 | return 0; |
| 2216 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2217 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2218 | static int |
| 2219 | PySSL_clear(PySSLSocket *self) |
| 2220 | { |
| 2221 | Py_CLEAR(self->exc_type); |
| 2222 | Py_CLEAR(self->exc_value); |
| 2223 | Py_CLEAR(self->exc_tb); |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | static void |
| 2228 | PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2229 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2230 | if (self->ssl) |
| 2231 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2232 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2233 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2234 | Py_XDECREF(self->server_hostname); |
| 2235 | Py_XDECREF(self->owner); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2236 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2239 | /* 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] | 2240 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2241 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2242 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2243 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2244 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2245 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2246 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2247 | int rc; |
| 2248 | #ifdef HAVE_POLL |
| 2249 | struct pollfd pollfd; |
| 2250 | _PyTime_t ms; |
| 2251 | #else |
| 2252 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2253 | fd_set fds; |
| 2254 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2255 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2256 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2257 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2258 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2259 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2260 | else if (timeout < 0) { |
| 2261 | if (s->sock_timeout > 0) |
| 2262 | return SOCKET_HAS_TIMED_OUT; |
| 2263 | else |
| 2264 | return SOCKET_IS_BLOCKING; |
| 2265 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2267 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2268 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2269 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2271 | /* Prefer poll, if available, since you can poll() any fd |
| 2272 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2273 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2274 | pollfd.fd = s->sock_fd; |
| 2275 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2276 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2277 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2278 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2279 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2280 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2281 | PySSL_BEGIN_ALLOW_THREADS |
| 2282 | rc = poll(&pollfd, 1, (int)ms); |
| 2283 | PySSL_END_ALLOW_THREADS |
| 2284 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2285 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2286 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2287 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2288 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2289 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2290 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2291 | FD_ZERO(&fds); |
| 2292 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2293 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2294 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2295 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2296 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2297 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2298 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2299 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2300 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2301 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2302 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2303 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2304 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2305 | (when we are able to write or when there's something to read) */ |
| 2306 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2309 | /*[clinic input] |
| 2310 | _ssl._SSLSocket.write |
| 2311 | b: Py_buffer |
| 2312 | / |
| 2313 | |
| 2314 | Writes the bytes-like object b into the SSL object. |
| 2315 | |
| 2316 | Returns the number of bytes written. |
| 2317 | [clinic start generated code]*/ |
| 2318 | |
| 2319 | static PyObject * |
| 2320 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2321 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2322 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2323 | int len; |
| 2324 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2325 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2326 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2327 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2328 | _PyTime_t timeout, deadline = 0; |
| 2329 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2331 | if (sock != NULL) { |
| 2332 | if (((PyObject*)sock) == Py_None) { |
| 2333 | _setSSLError("Underlying socket connection gone", |
| 2334 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2335 | return NULL; |
| 2336 | } |
| 2337 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2340 | if (b->len > INT_MAX) { |
Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2341 | PyErr_Format(PyExc_OverflowError, |
| 2342 | "string longer than %d bytes", INT_MAX); |
| 2343 | goto error; |
| 2344 | } |
| 2345 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2346 | if (sock != NULL) { |
| 2347 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2348 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2349 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2350 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2351 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2352 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2353 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2354 | has_timeout = (timeout > 0); |
| 2355 | if (has_timeout) |
| 2356 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2357 | |
| 2358 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2359 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2360 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2361 | "The write operation timed out"); |
| 2362 | goto error; |
| 2363 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2364 | PyErr_SetString(PySSLErrorObject, |
| 2365 | "Underlying socket has been closed."); |
| 2366 | goto error; |
| 2367 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2368 | PyErr_SetString(PySSLErrorObject, |
| 2369 | "Underlying socket too large for select()."); |
| 2370 | goto error; |
| 2371 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2372 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2373 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2374 | PySSL_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2375 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2376 | err = _PySSL_errno(len <= 0, self->ssl, len); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2377 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2378 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2379 | |
| 2380 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2381 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2382 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2383 | if (has_timeout) |
| 2384 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2385 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2386 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2387 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2388 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2389 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | sockstate = SOCKET_OPERATION_OK; |
| 2392 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2393 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2394 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2395 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2396 | "The write operation timed out"); |
| 2397 | goto error; |
| 2398 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2399 | PyErr_SetString(PySSLErrorObject, |
| 2400 | "Underlying socket has been closed."); |
| 2401 | goto error; |
| 2402 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2403 | break; |
| 2404 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2405 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2406 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2408 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2409 | if (len <= 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2410 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2411 | if (PySSL_ChainExceptions(self) < 0) |
| 2412 | return NULL; |
| 2413 | return PyLong_FromLong(len); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2414 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2415 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2416 | PySSL_ChainExceptions(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2417 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2420 | /*[clinic input] |
| 2421 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2422 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2423 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2424 | [clinic start generated code]*/ |
| 2425 | |
| 2426 | static PyObject * |
| 2427 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2428 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2429 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2430 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2431 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2432 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2433 | PySSL_BEGIN_ALLOW_THREADS |
| 2434 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2435 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2436 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2437 | self->err = err; |
| 2438 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2439 | if (count < 0) |
| 2440 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2441 | else |
| 2442 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2445 | /*[clinic input] |
| 2446 | _ssl._SSLSocket.read |
| 2447 | size as len: int |
| 2448 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2449 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2450 | ] |
| 2451 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2452 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2453 | Read up to size bytes from the SSL socket. |
| 2454 | [clinic start generated code]*/ |
| 2455 | |
| 2456 | static PyObject * |
| 2457 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2458 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2459 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2460 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2461 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2462 | char *mem; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2463 | int count; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2464 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2465 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2466 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2467 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2468 | _PyTime_t timeout, deadline = 0; |
| 2469 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2470 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2471 | if (!group_right_1 && len < 0) { |
| 2472 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2473 | return NULL; |
| 2474 | } |
| 2475 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2476 | if (sock != NULL) { |
| 2477 | if (((PyObject*)sock) == Py_None) { |
| 2478 | _setSSLError("Underlying socket connection gone", |
| 2479 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2480 | return NULL; |
| 2481 | } |
| 2482 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2485 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2486 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2487 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2488 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2489 | if (len == 0) { |
| 2490 | Py_XDECREF(sock); |
| 2491 | return dest; |
| 2492 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2493 | mem = PyBytes_AS_STRING(dest); |
| 2494 | } |
| 2495 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2496 | mem = buffer->buf; |
| 2497 | if (len <= 0 || len > buffer->len) { |
| 2498 | len = (int) buffer->len; |
| 2499 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2500 | PyErr_SetString(PyExc_OverflowError, |
| 2501 | "maximum length can't fit in a C 'int'"); |
| 2502 | goto error; |
| 2503 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2504 | if (len == 0) { |
| 2505 | count = 0; |
| 2506 | goto done; |
| 2507 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2508 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2511 | if (sock != NULL) { |
| 2512 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2513 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2514 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2515 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2516 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2517 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2518 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2519 | has_timeout = (timeout > 0); |
| 2520 | if (has_timeout) |
| 2521 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2522 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2523 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2524 | PySSL_BEGIN_ALLOW_THREADS |
| 2525 | count = SSL_read(self->ssl, mem, len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2526 | err = _PySSL_errno(count <= 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2527 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2528 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2529 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2530 | if (PyErr_CheckSignals()) |
| 2531 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2532 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2533 | if (has_timeout) |
| 2534 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2535 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2536 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2537 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2538 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2539 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2540 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2541 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2542 | { |
| 2543 | count = 0; |
| 2544 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2545 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2546 | else |
| 2547 | sockstate = SOCKET_OPERATION_OK; |
| 2548 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2549 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2550 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2551 | "The read operation timed out"); |
| 2552 | goto error; |
| 2553 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2554 | break; |
| 2555 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2556 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2557 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2558 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2559 | if (count <= 0) { |
| 2560 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2561 | goto error; |
| 2562 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2563 | if (self->exc_type != NULL) |
| 2564 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2565 | |
| 2566 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2567 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2568 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2569 | _PyBytes_Resize(&dest, count); |
| 2570 | return dest; |
| 2571 | } |
| 2572 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2573 | return PyLong_FromLong(count); |
| 2574 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2575 | |
| 2576 | error: |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2577 | PySSL_ChainExceptions(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2578 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2579 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2580 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2581 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2584 | /*[clinic input] |
| 2585 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2586 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2587 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2588 | [clinic start generated code]*/ |
| 2589 | |
| 2590 | static PyObject * |
| 2591 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2592 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2593 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2594 | _PySSLError err; |
| 2595 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2596 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2597 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2598 | _PyTime_t timeout, deadline = 0; |
| 2599 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2600 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2601 | if (sock != NULL) { |
| 2602 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2603 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2604 | _setSSLError("Underlying socket connection gone", |
| 2605 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2606 | return NULL; |
| 2607 | } |
| 2608 | Py_INCREF(sock); |
| 2609 | |
| 2610 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2611 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2612 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2613 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2614 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2615 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2616 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2617 | has_timeout = (timeout > 0); |
| 2618 | if (has_timeout) |
| 2619 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2620 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2621 | while (1) { |
| 2622 | PySSL_BEGIN_ALLOW_THREADS |
| 2623 | /* Disable read-ahead so that unwrap can work correctly. |
| 2624 | * Otherwise OpenSSL might read in too much data, |
| 2625 | * eating clear text data that happens to be |
| 2626 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2627 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2628 | * function is used and the shutdown_seen_zero != 0 |
| 2629 | * condition is met. |
| 2630 | */ |
| 2631 | if (self->shutdown_seen_zero) |
| 2632 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2633 | ret = SSL_shutdown(self->ssl); |
| 2634 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2635 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2636 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2637 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2638 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2639 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2640 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2641 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2642 | /* Don't loop endlessly; instead preserve legacy |
| 2643 | behaviour of trying SSL_shutdown() only twice. |
| 2644 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2645 | if (++zeros > 1) |
| 2646 | break; |
| 2647 | /* Shutdown was sent, now try receiving */ |
| 2648 | self->shutdown_seen_zero = 1; |
| 2649 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2652 | if (has_timeout) |
| 2653 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2654 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2655 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2656 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2657 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2658 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2659 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2660 | else |
| 2661 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2662 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2663 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2664 | if (err.ssl == SSL_ERROR_WANT_READ) |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2665 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2666 | "The read operation timed out"); |
| 2667 | else |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2668 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2669 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2670 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2671 | } |
| 2672 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2673 | PyErr_SetString(PySSLErrorObject, |
| 2674 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2675 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2676 | } |
| 2677 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2678 | /* Retain the SSL error code */ |
| 2679 | break; |
| 2680 | } |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2681 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2682 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2683 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2684 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2685 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2686 | if (self->exc_type != NULL) |
| 2687 | goto error; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2688 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2689 | /* It's already INCREF'ed */ |
| 2690 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2691 | else |
| 2692 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2693 | |
| 2694 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2695 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2696 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2697 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2700 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2701 | _ssl._SSLSocket.get_channel_binding |
| 2702 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2703 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2704 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2705 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2706 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2707 | of the data or None if the data is not available (e.g. before the handshake). |
| 2708 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2709 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2710 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2711 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2712 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2713 | const char *cb_type) |
| 2714 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2715 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2716 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2717 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2718 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2719 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2720 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2721 | /* if session is resumed XOR we are the client */ |
| 2722 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2723 | } |
| 2724 | else { |
| 2725 | /* if a new session XOR we are the server */ |
| 2726 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2727 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2728 | } |
| 2729 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2730 | PyErr_Format( |
| 2731 | PyExc_ValueError, |
| 2732 | "'%s' channel binding type not implemented", |
| 2733 | cb_type |
| 2734 | ); |
| 2735 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2739 | if (len == 0) |
| 2740 | Py_RETURN_NONE; |
| 2741 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2742 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2743 | } |
| 2744 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2745 | /*[clinic input] |
| 2746 | _ssl._SSLSocket.verify_client_post_handshake |
| 2747 | |
| 2748 | Initiate TLS 1.3 post-handshake authentication |
| 2749 | [clinic start generated code]*/ |
| 2750 | |
| 2751 | static PyObject * |
| 2752 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2753 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2754 | { |
| 2755 | #ifdef TLS1_3_VERSION |
| 2756 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2757 | if (err == 0) |
| 2758 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2759 | else |
| 2760 | Py_RETURN_NONE; |
| 2761 | #else |
| 2762 | PyErr_SetString(PyExc_NotImplementedError, |
| 2763 | "Post-handshake auth is not supported by your " |
| 2764 | "OpenSSL version."); |
| 2765 | return NULL; |
| 2766 | #endif |
| 2767 | } |
| 2768 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2769 | #ifdef OPENSSL_VERSION_1_1 |
| 2770 | |
| 2771 | static SSL_SESSION* |
| 2772 | _ssl_session_dup(SSL_SESSION *session) { |
| 2773 | SSL_SESSION *newsession = NULL; |
| 2774 | int slen; |
| 2775 | unsigned char *senc = NULL, *p; |
| 2776 | const unsigned char *const_p; |
| 2777 | |
| 2778 | if (session == NULL) { |
| 2779 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2780 | goto error; |
| 2781 | } |
| 2782 | |
| 2783 | /* get length */ |
| 2784 | slen = i2d_SSL_SESSION(session, NULL); |
| 2785 | if (slen == 0 || slen > 0xFF00) { |
| 2786 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2787 | goto error; |
| 2788 | } |
| 2789 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2790 | PyErr_NoMemory(); |
| 2791 | goto error; |
| 2792 | } |
| 2793 | p = senc; |
| 2794 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2795 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2796 | goto error; |
| 2797 | } |
| 2798 | const_p = senc; |
| 2799 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2800 | if (session == NULL) { |
| 2801 | goto error; |
| 2802 | } |
| 2803 | PyMem_Free(senc); |
| 2804 | return newsession; |
| 2805 | error: |
| 2806 | if (senc != NULL) { |
| 2807 | PyMem_Free(senc); |
| 2808 | } |
| 2809 | return NULL; |
| 2810 | } |
| 2811 | #endif |
| 2812 | |
| 2813 | static PyObject * |
| 2814 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2815 | /* get_session can return sessions from a server-side connection, |
| 2816 | * it does not check for handshake done or client socket. */ |
| 2817 | PySSLSession *pysess; |
| 2818 | SSL_SESSION *session; |
| 2819 | |
| 2820 | #ifdef OPENSSL_VERSION_1_1 |
| 2821 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2822 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2823 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2824 | if (session == NULL) { |
| 2825 | Py_RETURN_NONE; |
| 2826 | } |
| 2827 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2828 | return NULL; |
| 2829 | } |
| 2830 | #else |
| 2831 | session = SSL_get1_session(self->ssl); |
| 2832 | if (session == NULL) { |
| 2833 | Py_RETURN_NONE; |
| 2834 | } |
| 2835 | #endif |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2836 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2837 | if (pysess == NULL) { |
| 2838 | SSL_SESSION_free(session); |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
| 2842 | assert(self->ctx); |
| 2843 | pysess->ctx = self->ctx; |
| 2844 | Py_INCREF(pysess->ctx); |
| 2845 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2846 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2847 | return (PyObject *)pysess; |
| 2848 | } |
| 2849 | |
| 2850 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2851 | void *closure) |
| 2852 | { |
| 2853 | PySSLSession *pysess; |
| 2854 | #ifdef OPENSSL_VERSION_1_1 |
| 2855 | SSL_SESSION *session; |
| 2856 | #endif |
| 2857 | int result; |
| 2858 | |
| 2859 | if (!PySSLSession_Check(value)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2860 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2861 | return -1; |
| 2862 | } |
| 2863 | pysess = (PySSLSession *)value; |
| 2864 | |
| 2865 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2866 | PyErr_SetString(PyExc_ValueError, |
| 2867 | "Session refers to a different SSLContext."); |
| 2868 | return -1; |
| 2869 | } |
| 2870 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2871 | PyErr_SetString(PyExc_ValueError, |
| 2872 | "Cannot set session for server-side SSLSocket."); |
| 2873 | return -1; |
| 2874 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2875 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2876 | PyErr_SetString(PyExc_ValueError, |
| 2877 | "Cannot set session after handshake."); |
| 2878 | return -1; |
| 2879 | } |
| 2880 | #ifdef OPENSSL_VERSION_1_1 |
| 2881 | /* duplicate session */ |
| 2882 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2883 | return -1; |
| 2884 | } |
| 2885 | result = SSL_set_session(self->ssl, session); |
| 2886 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2887 | SSL_SESSION_free(session); |
| 2888 | #else |
| 2889 | result = SSL_set_session(self->ssl, pysess->session); |
| 2890 | #endif |
| 2891 | if (result == 0) { |
| 2892 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2893 | return -1; |
| 2894 | } |
| 2895 | return 0; |
| 2896 | } |
| 2897 | |
| 2898 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2899 | "_setter_session(session)\n\ |
| 2900 | \ |
| 2901 | Get / set SSLSession."); |
| 2902 | |
| 2903 | static PyObject * |
| 2904 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2905 | if (SSL_session_reused(self->ssl)) { |
| 2906 | Py_RETURN_TRUE; |
| 2907 | } else { |
| 2908 | Py_RETURN_FALSE; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2913 | "Was the client session reused during handshake?"); |
| 2914 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2915 | static PyGetSetDef ssl_getsetlist[] = { |
| 2916 | {"context", (getter) PySSL_get_context, |
| 2917 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2918 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2919 | PySSL_get_server_side_doc}, |
| 2920 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2921 | PySSL_get_server_hostname_doc}, |
| 2922 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2923 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2924 | {"session", (getter) PySSL_get_session, |
| 2925 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2926 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2927 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2928 | {NULL}, /* sentinel */ |
| 2929 | }; |
| 2930 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2931 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2932 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2933 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2934 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2935 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2936 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2937 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2938 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2939 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2940 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 2941 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 2942 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2943 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2944 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2945 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2946 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2947 | }; |
| 2948 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2949 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2950 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2951 | "_ssl._SSLSocket", /*tp_name*/ |
| 2952 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2953 | 0, /*tp_itemsize*/ |
| 2954 | /* methods */ |
| 2955 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2956 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2957 | 0, /*tp_getattr*/ |
| 2958 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2959 | 0, /*tp_as_async*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2960 | 0, /*tp_repr*/ |
| 2961 | 0, /*tp_as_number*/ |
| 2962 | 0, /*tp_as_sequence*/ |
| 2963 | 0, /*tp_as_mapping*/ |
| 2964 | 0, /*tp_hash*/ |
| 2965 | 0, /*tp_call*/ |
| 2966 | 0, /*tp_str*/ |
| 2967 | 0, /*tp_getattro*/ |
| 2968 | 0, /*tp_setattro*/ |
| 2969 | 0, /*tp_as_buffer*/ |
| 2970 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2971 | 0, /*tp_doc*/ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2972 | (traverseproc) PySSL_traverse, /*tp_traverse*/ |
| 2973 | (inquiry) PySSL_clear, /*tp_clear*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2974 | 0, /*tp_richcompare*/ |
| 2975 | 0, /*tp_weaklistoffset*/ |
| 2976 | 0, /*tp_iter*/ |
| 2977 | 0, /*tp_iternext*/ |
| 2978 | PySSLMethods, /*tp_methods*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2979 | 0, /*tp_members*/ |
| 2980 | ssl_getsetlist, /*tp_getset*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2981 | }; |
| 2982 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* |
| 2985 | * _SSLContext objects |
| 2986 | */ |
| 2987 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2988 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2989 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2990 | { |
| 2991 | int mode; |
| 2992 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 2993 | |
| 2994 | switch(n) { |
| 2995 | case PY_SSL_CERT_NONE: |
| 2996 | mode = SSL_VERIFY_NONE; |
| 2997 | break; |
| 2998 | case PY_SSL_CERT_OPTIONAL: |
| 2999 | mode = SSL_VERIFY_PEER; |
| 3000 | break; |
| 3001 | case PY_SSL_CERT_REQUIRED: |
| 3002 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3003 | break; |
| 3004 | default: |
| 3005 | PyErr_SetString(PyExc_ValueError, |
| 3006 | "invalid value for verify_mode"); |
| 3007 | return -1; |
| 3008 | } |
Christian Heimes | f22c4cf | 2019-07-01 09:25:48 +0200 | [diff] [blame] | 3009 | |
| 3010 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3011 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 3012 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3013 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3014 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 3015 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3016 | return 0; |
| 3017 | } |
| 3018 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3019 | /*[clinic input] |
| 3020 | @classmethod |
| 3021 | _ssl._SSLContext.__new__ |
| 3022 | protocol as proto_version: int |
| 3023 | / |
| 3024 | [clinic start generated code]*/ |
| 3025 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3026 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3027 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 3028 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3029 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3030 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3031 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3032 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3033 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3034 | int result; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3035 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3036 | unsigned long libver; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 3037 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3038 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3039 | PySSL_BEGIN_ALLOW_THREADS |
| 3040 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 3041 | ctx = SSL_CTX_new(TLSv1_method()); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 3042 | #if HAVE_TLSv1_2 |
| 3043 | else if (proto_version == PY_SSL_VERSION_TLS1_1) |
| 3044 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 3045 | else if (proto_version == PY_SSL_VERSION_TLS1_2) |
| 3046 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 3047 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3048 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3049 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 3050 | ctx = SSL_CTX_new(SSLv3_method()); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 3051 | #endif |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3052 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3053 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 3054 | ctx = SSL_CTX_new(SSLv2_method()); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 3055 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3056 | else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3057 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3058 | else if (proto_version == PY_SSL_VERSION_TLS_CLIENT) |
| 3059 | ctx = SSL_CTX_new(TLS_client_method()); |
| 3060 | else if (proto_version == PY_SSL_VERSION_TLS_SERVER) |
| 3061 | ctx = SSL_CTX_new(TLS_server_method()); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3062 | else |
| 3063 | proto_version = -1; |
| 3064 | PySSL_END_ALLOW_THREADS |
| 3065 | |
| 3066 | if (proto_version == -1) { |
| 3067 | PyErr_SetString(PyExc_ValueError, |
| 3068 | "invalid protocol version"); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | if (ctx == NULL) { |
Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 3072 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3073 | return NULL; |
| 3074 | } |
| 3075 | |
| 3076 | assert(type != NULL && type->tp_alloc != NULL); |
| 3077 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 3078 | if (self == NULL) { |
| 3079 | SSL_CTX_free(ctx); |
| 3080 | return NULL; |
| 3081 | } |
| 3082 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3083 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3084 | self->protocol = proto_version; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3085 | self->msg_cb = NULL; |
| 3086 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3087 | self->keylog_filename = NULL; |
| 3088 | self->keylog_bio = NULL; |
| 3089 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3090 | #if HAVE_NPN |
Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 3091 | self->npn_protocols = NULL; |
| 3092 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3093 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3094 | self->alpn_protocols = NULL; |
| 3095 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3096 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3097 | self->set_sni_cb = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3098 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3099 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3100 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3101 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3102 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3103 | Py_DECREF(self); |
| 3104 | return NULL; |
| 3105 | } |
| 3106 | } else { |
| 3107 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3108 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3109 | Py_DECREF(self); |
| 3110 | return NULL; |
| 3111 | } |
| 3112 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3113 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3114 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3115 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3116 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3117 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3118 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3119 | /* Minimal security flags for server and client side context. |
| 3120 | * Client sockets ignore server-side parameters. */ |
| 3121 | #ifdef SSL_OP_NO_COMPRESSION |
| 3122 | options |= SSL_OP_NO_COMPRESSION; |
| 3123 | #endif |
| 3124 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3125 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3126 | #endif |
| 3127 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3128 | options |= SSL_OP_SINGLE_DH_USE; |
| 3129 | #endif |
| 3130 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3131 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3132 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3133 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3134 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3135 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3136 | * It's far from perfect but gives users a better head start. */ |
| 3137 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3138 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3139 | /* stick to OpenSSL's default settings */ |
| 3140 | result = 1; |
| 3141 | #else |
| 3142 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3143 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3144 | } else { |
| 3145 | /* SSLv2 needs MD5 */ |
| 3146 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3147 | } |
| 3148 | if (result == 0) { |
| 3149 | Py_DECREF(self); |
| 3150 | ERR_clear_error(); |
| 3151 | PyErr_SetString(PySSLErrorObject, |
| 3152 | "No cipher can be selected."); |
| 3153 | return NULL; |
| 3154 | } |
| 3155 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3156 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 3157 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 3158 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 3159 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 3160 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 3161 | conservative and assume it wasn't fixed until release. We do this check |
| 3162 | at runtime to avoid problems from the dynamic linker. |
| 3163 | See #25672 for more on this. */ |
| 3164 | libver = SSLeay(); |
| 3165 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 3166 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 3167 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 3168 | } |
| 3169 | #endif |
| 3170 | |
| 3171 | |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3172 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3173 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 3174 | prime256v1 by default. This is Apache mod_ssl's initialization |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3175 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 3176 | */ |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3177 | #if defined(SSL_CTX_set_ecdh_auto) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3178 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 3179 | #else |
| 3180 | { |
| 3181 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 3182 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3183 | EC_KEY_free(key); |
| 3184 | } |
| 3185 | #endif |
| 3186 | #endif |
| 3187 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3188 | #define SID_CTX "Python" |
| 3189 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3190 | sizeof(SID_CTX)); |
| 3191 | #undef SID_CTX |
| 3192 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3193 | params = SSL_CTX_get0_param(self->ctx); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3194 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3195 | /* Improve trust chain building when cross-signed intermediate |
| 3196 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3197 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3198 | #endif |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3199 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3200 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3201 | #ifdef TLS1_3_VERSION |
| 3202 | self->post_handshake_auth = 0; |
| 3203 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3204 | #endif |
| 3205 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3206 | return (PyObject *)self; |
| 3207 | } |
| 3208 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3209 | static int |
| 3210 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3211 | { |
| 3212 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3213 | Py_VISIT(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3214 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3215 | Py_VISIT(self->msg_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3216 | return 0; |
| 3217 | } |
| 3218 | |
| 3219 | static int |
| 3220 | context_clear(PySSLContext *self) |
| 3221 | { |
| 3222 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3223 | Py_CLEAR(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3224 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3225 | Py_CLEAR(self->msg_cb); |
| 3226 | #ifdef HAVE_OPENSSL_KEYLOG |
| 3227 | Py_CLEAR(self->keylog_filename); |
| 3228 | if (self->keylog_bio != NULL) { |
| 3229 | PySSL_BEGIN_ALLOW_THREADS |
| 3230 | BIO_free_all(self->keylog_bio); |
| 3231 | PySSL_END_ALLOW_THREADS |
| 3232 | self->keylog_bio = NULL; |
| 3233 | } |
| 3234 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3238 | static void |
| 3239 | context_dealloc(PySSLContext *self) |
| 3240 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3241 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3242 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3243 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3244 | SSL_CTX_free(self->ctx); |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3245 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3246 | PyMem_FREE(self->npn_protocols); |
| 3247 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3248 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3249 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3250 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3251 | Py_TYPE(self)->tp_free(self); |
| 3252 | } |
| 3253 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3254 | /*[clinic input] |
| 3255 | _ssl._SSLContext.set_ciphers |
| 3256 | cipherlist: str |
| 3257 | / |
| 3258 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3259 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3260 | static PyObject * |
| 3261 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3262 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3263 | { |
| 3264 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3265 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3266 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3267 | otherwise the error will be reported again when another SSL call |
| 3268 | is done. */ |
| 3269 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3270 | PyErr_SetString(PySSLErrorObject, |
| 3271 | "No cipher can be selected."); |
| 3272 | return NULL; |
| 3273 | } |
| 3274 | Py_RETURN_NONE; |
| 3275 | } |
| 3276 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3277 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 3278 | /*[clinic input] |
| 3279 | _ssl._SSLContext.get_ciphers |
| 3280 | [clinic start generated code]*/ |
| 3281 | |
| 3282 | static PyObject * |
| 3283 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3284 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3285 | { |
| 3286 | SSL *ssl = NULL; |
| 3287 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3288 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3289 | int i=0; |
| 3290 | PyObject *result = NULL, *dct; |
| 3291 | |
| 3292 | ssl = SSL_new(self->ctx); |
| 3293 | if (ssl == NULL) { |
| 3294 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3295 | goto exit; |
| 3296 | } |
| 3297 | sk = SSL_get_ciphers(ssl); |
| 3298 | |
| 3299 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3300 | if (result == NULL) { |
| 3301 | goto exit; |
| 3302 | } |
| 3303 | |
| 3304 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3305 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3306 | dct = cipher_to_dict(cipher); |
| 3307 | if (dct == NULL) { |
| 3308 | Py_CLEAR(result); |
| 3309 | goto exit; |
| 3310 | } |
| 3311 | PyList_SET_ITEM(result, i, dct); |
| 3312 | } |
| 3313 | |
| 3314 | exit: |
| 3315 | if (ssl != NULL) |
| 3316 | SSL_free(ssl); |
| 3317 | return result; |
| 3318 | |
| 3319 | } |
| 3320 | #endif |
| 3321 | |
| 3322 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3323 | #if HAVE_NPN || HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3324 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3325 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3326 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3327 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3328 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3329 | int ret; |
| 3330 | if (client_protocols == NULL) { |
| 3331 | client_protocols = (unsigned char *)""; |
| 3332 | client_protocols_len = 0; |
| 3333 | } |
| 3334 | if (server_protocols == NULL) { |
| 3335 | server_protocols = (unsigned char *)""; |
| 3336 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3337 | } |
| 3338 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3339 | ret = SSL_select_next_proto(out, outlen, |
| 3340 | server_protocols, server_protocols_len, |
| 3341 | client_protocols, client_protocols_len); |
| 3342 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3343 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3344 | |
| 3345 | return SSL_TLSEXT_ERR_OK; |
| 3346 | } |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3347 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3348 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3349 | #if HAVE_NPN |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3350 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 3351 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3352 | _advertiseNPN_cb(SSL *s, |
| 3353 | const unsigned char **data, unsigned int *len, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3354 | void *args) |
| 3355 | { |
| 3356 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3357 | |
| 3358 | if (ssl_ctx->npn_protocols == NULL) { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3359 | *data = (unsigned char *)""; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3360 | *len = 0; |
| 3361 | } else { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3362 | *data = ssl_ctx->npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3363 | *len = ssl_ctx->npn_protocols_len; |
| 3364 | } |
| 3365 | |
| 3366 | return SSL_TLSEXT_ERR_OK; |
| 3367 | } |
| 3368 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3369 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3370 | _selectNPN_cb(SSL *s, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3371 | unsigned char **out, unsigned char *outlen, |
| 3372 | const unsigned char *server, unsigned int server_len, |
| 3373 | void *args) |
| 3374 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3375 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3376 | return do_protocol_selection(0, out, outlen, server, server_len, |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3377 | ctx->npn_protocols, ctx->npn_protocols_len); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3378 | } |
| 3379 | #endif |
| 3380 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3381 | /*[clinic input] |
| 3382 | _ssl._SSLContext._set_npn_protocols |
| 3383 | protos: Py_buffer |
| 3384 | / |
| 3385 | [clinic start generated code]*/ |
| 3386 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3387 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3388 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3389 | Py_buffer *protos) |
| 3390 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3391 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3392 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3393 | PyMem_Free(self->npn_protocols); |
| 3394 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3395 | if (self->npn_protocols == NULL) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3396 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3397 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3398 | self->npn_protocols_len = (int) protos->len; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3399 | |
| 3400 | /* set both server and client callbacks, because the context can |
| 3401 | * be used to create both types of sockets */ |
| 3402 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3403 | _advertiseNPN_cb, |
| 3404 | self); |
| 3405 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3406 | _selectNPN_cb, |
| 3407 | self); |
| 3408 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3409 | Py_RETURN_NONE; |
| 3410 | #else |
| 3411 | PyErr_SetString(PyExc_NotImplementedError, |
| 3412 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3413 | return NULL; |
| 3414 | #endif |
| 3415 | } |
| 3416 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3417 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3418 | static int |
| 3419 | _selectALPN_cb(SSL *s, |
| 3420 | const unsigned char **out, unsigned char *outlen, |
| 3421 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3422 | void *args) |
| 3423 | { |
| 3424 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3425 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3426 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3427 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3428 | } |
| 3429 | #endif |
| 3430 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3431 | /*[clinic input] |
| 3432 | _ssl._SSLContext._set_alpn_protocols |
| 3433 | protos: Py_buffer |
| 3434 | / |
| 3435 | [clinic start generated code]*/ |
| 3436 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3437 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3438 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3439 | Py_buffer *protos) |
| 3440 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3441 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3442 | #if HAVE_ALPN |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3443 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3444 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3445 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3446 | return NULL; |
| 3447 | } |
| 3448 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3449 | PyMem_FREE(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3450 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3451 | if (!self->alpn_protocols) |
| 3452 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3453 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3454 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3455 | |
| 3456 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3457 | return PyErr_NoMemory(); |
| 3458 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3459 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3460 | Py_RETURN_NONE; |
| 3461 | #else |
| 3462 | PyErr_SetString(PyExc_NotImplementedError, |
| 3463 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3464 | return NULL; |
| 3465 | #endif |
| 3466 | } |
| 3467 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3468 | static PyObject * |
| 3469 | get_verify_mode(PySSLContext *self, void *c) |
| 3470 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3471 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3472 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3473 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3474 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3475 | case SSL_VERIFY_NONE: |
| 3476 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3477 | case SSL_VERIFY_PEER: |
| 3478 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3479 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3480 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3481 | } |
| 3482 | PyErr_SetString(PySSLErrorObject, |
| 3483 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3484 | return NULL; |
| 3485 | } |
| 3486 | |
| 3487 | static int |
| 3488 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3489 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3490 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3491 | if (!PyArg_Parse(arg, "i", &n)) |
| 3492 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3493 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3494 | PyErr_SetString(PyExc_ValueError, |
| 3495 | "Cannot set verify_mode to CERT_NONE when " |
| 3496 | "check_hostname is enabled."); |
| 3497 | return -1; |
| 3498 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3499 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3503 | get_verify_flags(PySSLContext *self, void *c) |
| 3504 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3505 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3506 | unsigned long flags; |
| 3507 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3508 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3509 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3510 | return PyLong_FromUnsignedLong(flags); |
| 3511 | } |
| 3512 | |
| 3513 | static int |
| 3514 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3515 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3516 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3517 | unsigned long new_flags, flags, set, clear; |
| 3518 | |
| 3519 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3520 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3521 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3522 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3523 | clear = flags & ~new_flags; |
| 3524 | set = ~flags & new_flags; |
| 3525 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3526 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3527 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3528 | return -1; |
| 3529 | } |
| 3530 | } |
| 3531 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3532 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3533 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3534 | return -1; |
| 3535 | } |
| 3536 | } |
| 3537 | return 0; |
| 3538 | } |
| 3539 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3540 | /* Getter and setter for protocol version */ |
| 3541 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) |
| 3542 | |
| 3543 | |
| 3544 | static int |
| 3545 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3546 | { |
| 3547 | long v; |
| 3548 | int result; |
| 3549 | |
| 3550 | if (!PyArg_Parse(arg, "l", &v)) |
| 3551 | return -1; |
| 3552 | if (v > INT_MAX) { |
| 3553 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3554 | return -1; |
| 3555 | } |
| 3556 | |
| 3557 | switch(self->protocol) { |
| 3558 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3559 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3560 | case PY_SSL_VERSION_TLS: |
| 3561 | break; |
| 3562 | default: |
| 3563 | PyErr_SetString( |
| 3564 | PyExc_ValueError, |
| 3565 | "The context's protocol doesn't support modification of " |
| 3566 | "highest and lowest version." |
| 3567 | ); |
| 3568 | return -1; |
| 3569 | } |
| 3570 | |
| 3571 | if (what == 0) { |
| 3572 | switch(v) { |
| 3573 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3574 | v = 0; |
| 3575 | break; |
| 3576 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3577 | /* Emulate max for set_min_proto_version */ |
| 3578 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3579 | break; |
| 3580 | default: |
| 3581 | break; |
| 3582 | } |
| 3583 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3584 | } |
| 3585 | else { |
| 3586 | switch(v) { |
| 3587 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3588 | v = 0; |
| 3589 | break; |
| 3590 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3591 | /* Emulate max for set_min_proto_version */ |
| 3592 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3593 | break; |
| 3594 | default: |
| 3595 | break; |
| 3596 | } |
| 3597 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3598 | } |
| 3599 | if (result == 0) { |
| 3600 | PyErr_Format(PyExc_ValueError, |
| 3601 | "Unsupported protocol version 0x%x", v); |
| 3602 | return -1; |
| 3603 | } |
| 3604 | return 0; |
| 3605 | } |
| 3606 | |
| 3607 | static PyObject * |
| 3608 | get_minimum_version(PySSLContext *self, void *c) |
| 3609 | { |
| 3610 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3611 | if (v == 0) { |
| 3612 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3613 | } |
| 3614 | return PyLong_FromLong(v); |
| 3615 | } |
| 3616 | |
| 3617 | static int |
| 3618 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3619 | { |
| 3620 | return set_min_max_proto_version(self, arg, 0); |
| 3621 | } |
| 3622 | |
| 3623 | static PyObject * |
| 3624 | get_maximum_version(PySSLContext *self, void *c) |
| 3625 | { |
| 3626 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3627 | if (v == 0) { |
| 3628 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3629 | } |
| 3630 | return PyLong_FromLong(v); |
| 3631 | } |
| 3632 | |
| 3633 | static int |
| 3634 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3635 | { |
| 3636 | return set_min_max_proto_version(self, arg, 1); |
| 3637 | } |
| 3638 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ |
| 3639 | |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3640 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 3641 | static PyObject * |
| 3642 | get_num_tickets(PySSLContext *self, void *c) |
| 3643 | { |
Miss Islington (bot) | bbad695 | 2019-07-09 05:42:49 -0700 | [diff] [blame] | 3644 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | static int |
| 3648 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3649 | { |
| 3650 | long num; |
| 3651 | if (!PyArg_Parse(arg, "l", &num)) |
| 3652 | return -1; |
| 3653 | if (num < 0) { |
| 3654 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3655 | return -1; |
| 3656 | } |
| 3657 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3658 | PyErr_SetString(PyExc_ValueError, |
| 3659 | "SSLContext is not a server context."); |
| 3660 | return -1; |
| 3661 | } |
| 3662 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3663 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3664 | return -1; |
| 3665 | } |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
| 3669 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3670 | "Control the number of TLSv1.3 session tickets"); |
| 3671 | #endif /* OpenSSL 1.1.1 */ |
| 3672 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3673 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3674 | get_options(PySSLContext *self, void *c) |
| 3675 | { |
| 3676 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3677 | } |
| 3678 | |
| 3679 | static int |
| 3680 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3681 | { |
| 3682 | long new_opts, opts, set, clear; |
| 3683 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3684 | return -1; |
| 3685 | opts = SSL_CTX_get_options(self->ctx); |
| 3686 | clear = opts & ~new_opts; |
| 3687 | set = ~opts & new_opts; |
| 3688 | if (clear) { |
| 3689 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3690 | SSL_CTX_clear_options(self->ctx, clear); |
| 3691 | #else |
| 3692 | PyErr_SetString(PyExc_ValueError, |
| 3693 | "can't clear options before OpenSSL 0.9.8m"); |
| 3694 | return -1; |
| 3695 | #endif |
| 3696 | } |
| 3697 | if (set) |
| 3698 | SSL_CTX_set_options(self->ctx, set); |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3702 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3703 | get_host_flags(PySSLContext *self, void *c) |
| 3704 | { |
| 3705 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3706 | } |
| 3707 | |
| 3708 | static int |
| 3709 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3710 | { |
| 3711 | X509_VERIFY_PARAM *param; |
| 3712 | unsigned int new_flags = 0; |
| 3713 | |
| 3714 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3715 | return -1; |
| 3716 | |
| 3717 | param = SSL_CTX_get0_param(self->ctx); |
| 3718 | self->hostflags = new_flags; |
| 3719 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
| 3723 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3724 | get_check_hostname(PySSLContext *self, void *c) |
| 3725 | { |
| 3726 | return PyBool_FromLong(self->check_hostname); |
| 3727 | } |
| 3728 | |
| 3729 | static int |
| 3730 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3731 | { |
| 3732 | int check_hostname; |
| 3733 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3734 | return -1; |
| 3735 | if (check_hostname && |
| 3736 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3737 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3738 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3739 | return -1; |
| 3740 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3741 | } |
| 3742 | self->check_hostname = check_hostname; |
| 3743 | return 0; |
| 3744 | } |
| 3745 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3746 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3747 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3748 | #if TLS1_3_VERSION |
| 3749 | return PyBool_FromLong(self->post_handshake_auth); |
| 3750 | #else |
| 3751 | Py_RETURN_NONE; |
| 3752 | #endif |
| 3753 | } |
| 3754 | |
| 3755 | #if TLS1_3_VERSION |
| 3756 | static int |
| 3757 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3758 | if (arg == NULL) { |
| 3759 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3760 | return -1; |
| 3761 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3762 | int pha = PyObject_IsTrue(arg); |
| 3763 | |
| 3764 | if (pha == -1) { |
| 3765 | return -1; |
| 3766 | } |
| 3767 | self->post_handshake_auth = pha; |
| 3768 | |
Christian Heimes | f22c4cf | 2019-07-01 09:25:48 +0200 | [diff] [blame] | 3769 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3770 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3771 | |
| 3772 | return 0; |
| 3773 | } |
| 3774 | #endif |
| 3775 | |
| 3776 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3777 | get_protocol(PySSLContext *self, void *c) { |
| 3778 | return PyLong_FromLong(self->protocol); |
| 3779 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3780 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3781 | typedef struct { |
| 3782 | PyThreadState *thread_state; |
| 3783 | PyObject *callable; |
| 3784 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3785 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3786 | int error; |
| 3787 | } _PySSLPasswordInfo; |
| 3788 | |
| 3789 | static int |
| 3790 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3791 | const char *bad_type_error) |
| 3792 | { |
| 3793 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3794 | from a unicode, bytes, or byte array object. |
| 3795 | The password field will be dynamically allocated and must be freed |
| 3796 | by the caller */ |
| 3797 | PyObject *password_bytes = NULL; |
| 3798 | const char *data = NULL; |
| 3799 | Py_ssize_t size; |
| 3800 | |
| 3801 | if (PyUnicode_Check(password)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3802 | password_bytes = PyUnicode_AsUTF8String(password); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3803 | if (!password_bytes) { |
| 3804 | goto error; |
| 3805 | } |
| 3806 | data = PyBytes_AS_STRING(password_bytes); |
| 3807 | size = PyBytes_GET_SIZE(password_bytes); |
| 3808 | } else if (PyBytes_Check(password)) { |
| 3809 | data = PyBytes_AS_STRING(password); |
| 3810 | size = PyBytes_GET_SIZE(password); |
| 3811 | } else if (PyByteArray_Check(password)) { |
| 3812 | data = PyByteArray_AS_STRING(password); |
| 3813 | size = PyByteArray_GET_SIZE(password); |
| 3814 | } else { |
| 3815 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3816 | goto error; |
| 3817 | } |
| 3818 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3819 | if (size > (Py_ssize_t)INT_MAX) { |
| 3820 | PyErr_Format(PyExc_ValueError, |
| 3821 | "password cannot be longer than %d bytes", INT_MAX); |
| 3822 | goto error; |
| 3823 | } |
| 3824 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3825 | PyMem_Free(pw_info->password); |
| 3826 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3827 | if (!pw_info->password) { |
| 3828 | PyErr_SetString(PyExc_MemoryError, |
| 3829 | "unable to allocate password buffer"); |
| 3830 | goto error; |
| 3831 | } |
| 3832 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3833 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3834 | |
| 3835 | Py_XDECREF(password_bytes); |
| 3836 | return 1; |
| 3837 | |
| 3838 | error: |
| 3839 | Py_XDECREF(password_bytes); |
| 3840 | return 0; |
| 3841 | } |
| 3842 | |
| 3843 | static int |
| 3844 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3845 | { |
| 3846 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3847 | PyObject *fn_ret = NULL; |
| 3848 | |
| 3849 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3850 | |
| 3851 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3852 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3853 | if (!fn_ret) { |
| 3854 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3855 | core python API, so we could use it to add a frame here */ |
| 3856 | goto error; |
| 3857 | } |
| 3858 | |
| 3859 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3860 | "password callback must return a string")) { |
| 3861 | goto error; |
| 3862 | } |
| 3863 | Py_CLEAR(fn_ret); |
| 3864 | } |
| 3865 | |
| 3866 | if (pw_info->size > size) { |
| 3867 | PyErr_Format(PyExc_ValueError, |
| 3868 | "password cannot be longer than %d bytes", size); |
| 3869 | goto error; |
| 3870 | } |
| 3871 | |
| 3872 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3873 | memcpy(buf, pw_info->password, pw_info->size); |
| 3874 | return pw_info->size; |
| 3875 | |
| 3876 | error: |
| 3877 | Py_XDECREF(fn_ret); |
| 3878 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3879 | pw_info->error = 1; |
| 3880 | return -1; |
| 3881 | } |
| 3882 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3883 | /*[clinic input] |
| 3884 | _ssl._SSLContext.load_cert_chain |
| 3885 | certfile: object |
| 3886 | keyfile: object = NULL |
| 3887 | password: object = NULL |
| 3888 | |
| 3889 | [clinic start generated code]*/ |
| 3890 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3891 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3892 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3893 | PyObject *keyfile, PyObject *password) |
| 3894 | /*[clinic end generated code: output=9480bc1c380e2095 input=7cf9ac673cbee6fc]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3895 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3896 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3897 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3898 | 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] | 3899 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3900 | int r; |
| 3901 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3902 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3903 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3904 | if (keyfile == Py_None) |
| 3905 | keyfile = NULL; |
| 3906 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3907 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3908 | PyErr_SetString(PyExc_TypeError, |
| 3909 | "certfile should be a valid filesystem path"); |
| 3910 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3911 | return NULL; |
| 3912 | } |
| 3913 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3914 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3915 | PyErr_SetString(PyExc_TypeError, |
| 3916 | "keyfile should be a valid filesystem path"); |
| 3917 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3918 | goto error; |
| 3919 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3920 | if (password && password != Py_None) { |
| 3921 | if (PyCallable_Check(password)) { |
| 3922 | pw_info.callable = password; |
| 3923 | } else if (!_pwinfo_set(&pw_info, password, |
| 3924 | "password should be a string or callable")) { |
| 3925 | goto error; |
| 3926 | } |
| 3927 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3928 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3929 | } |
| 3930 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3931 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3932 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3933 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3934 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3935 | if (pw_info.error) { |
| 3936 | ERR_clear_error(); |
| 3937 | /* the password callback has already set the error information */ |
| 3938 | } |
| 3939 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3940 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3941 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3942 | } |
| 3943 | else { |
| 3944 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3945 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3946 | goto error; |
| 3947 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3948 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3949 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3950 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 3951 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3952 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 3953 | Py_CLEAR(keyfile_bytes); |
| 3954 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3955 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3956 | if (pw_info.error) { |
| 3957 | ERR_clear_error(); |
| 3958 | /* the password callback has already set the error information */ |
| 3959 | } |
| 3960 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3961 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3962 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3963 | } |
| 3964 | else { |
| 3965 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3966 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3967 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3968 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3969 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3970 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3971 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3972 | if (r != 1) { |
| 3973 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3974 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3975 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3976 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3977 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3978 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3979 | Py_RETURN_NONE; |
| 3980 | |
| 3981 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3982 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3983 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3984 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3985 | Py_XDECREF(keyfile_bytes); |
| 3986 | Py_XDECREF(certfile_bytes); |
| 3987 | return NULL; |
| 3988 | } |
| 3989 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3990 | /* internal helper function, returns -1 on error |
| 3991 | */ |
| 3992 | static int |
| 3993 | _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len, |
| 3994 | int filetype) |
| 3995 | { |
| 3996 | BIO *biobuf = NULL; |
| 3997 | X509_STORE *store; |
| 3998 | int retval = 0, err, loaded = 0; |
| 3999 | |
| 4000 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 4001 | |
| 4002 | if (len <= 0) { |
| 4003 | PyErr_SetString(PyExc_ValueError, |
| 4004 | "Empty certificate data"); |
| 4005 | return -1; |
| 4006 | } else if (len > INT_MAX) { |
| 4007 | PyErr_SetString(PyExc_OverflowError, |
| 4008 | "Certificate data is too long."); |
| 4009 | return -1; |
| 4010 | } |
| 4011 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 4012 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4013 | if (biobuf == NULL) { |
| 4014 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 4015 | return -1; |
| 4016 | } |
| 4017 | |
| 4018 | store = SSL_CTX_get_cert_store(self->ctx); |
| 4019 | assert(store != NULL); |
| 4020 | |
| 4021 | while (1) { |
| 4022 | X509 *cert = NULL; |
| 4023 | int r; |
| 4024 | |
| 4025 | if (filetype == SSL_FILETYPE_ASN1) { |
| 4026 | cert = d2i_X509_bio(biobuf, NULL); |
| 4027 | } else { |
| 4028 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4029 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 4030 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 4031 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4032 | } |
| 4033 | if (cert == NULL) { |
| 4034 | break; |
| 4035 | } |
| 4036 | r = X509_STORE_add_cert(store, cert); |
| 4037 | X509_free(cert); |
| 4038 | if (!r) { |
| 4039 | err = ERR_peek_last_error(); |
| 4040 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 4041 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 4042 | /* cert already in hash table, not an error */ |
| 4043 | ERR_clear_error(); |
| 4044 | } else { |
| 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | loaded++; |
| 4049 | } |
| 4050 | |
| 4051 | err = ERR_peek_last_error(); |
| 4052 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 4053 | (loaded > 0) && |
| 4054 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 4055 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 4056 | /* EOF ASN1 file, not an error */ |
| 4057 | ERR_clear_error(); |
| 4058 | retval = 0; |
| 4059 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 4060 | (loaded > 0) && |
| 4061 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 4062 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 4063 | /* EOF PEM file, not an error */ |
| 4064 | ERR_clear_error(); |
| 4065 | retval = 0; |
| 4066 | } else { |
| 4067 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4068 | retval = -1; |
| 4069 | } |
| 4070 | |
| 4071 | BIO_free(biobuf); |
| 4072 | return retval; |
| 4073 | } |
| 4074 | |
| 4075 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4076 | /*[clinic input] |
| 4077 | _ssl._SSLContext.load_verify_locations |
| 4078 | cafile: object = NULL |
| 4079 | capath: object = NULL |
| 4080 | cadata: object = NULL |
| 4081 | |
| 4082 | [clinic start generated code]*/ |
| 4083 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4084 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4085 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 4086 | PyObject *cafile, |
| 4087 | PyObject *capath, |
| 4088 | PyObject *cadata) |
| 4089 | /*[clinic end generated code: output=454c7e41230ca551 input=997f1fb3a784ef88]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4090 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4091 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 4092 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4093 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4094 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4095 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4096 | if (cafile == Py_None) |
| 4097 | cafile = NULL; |
| 4098 | if (capath == Py_None) |
| 4099 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4100 | if (cadata == Py_None) |
| 4101 | cadata = NULL; |
| 4102 | |
| 4103 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4104 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4105 | "cafile, capath and cadata cannot be all omitted"); |
| 4106 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4107 | } |
| 4108 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4109 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4110 | PyErr_SetString(PyExc_TypeError, |
| 4111 | "cafile should be a valid filesystem path"); |
| 4112 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4113 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4114 | } |
| 4115 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4116 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4117 | PyErr_SetString(PyExc_TypeError, |
| 4118 | "capath should be a valid filesystem path"); |
| 4119 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4120 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4121 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4122 | |
| 4123 | /* validata cadata type and load cadata */ |
| 4124 | if (cadata) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4125 | if (PyUnicode_Check(cadata)) { |
| 4126 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4127 | if (cadata_ascii == NULL) { |
| 4128 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4129 | goto invalid_cadata; |
| 4130 | } |
| 4131 | goto error; |
| 4132 | } |
| 4133 | r = _add_ca_certs(self, |
| 4134 | PyBytes_AS_STRING(cadata_ascii), |
| 4135 | PyBytes_GET_SIZE(cadata_ascii), |
| 4136 | SSL_FILETYPE_PEM); |
| 4137 | Py_DECREF(cadata_ascii); |
| 4138 | if (r == -1) { |
| 4139 | goto error; |
| 4140 | } |
| 4141 | } |
| 4142 | else if (PyObject_CheckBuffer(cadata)) { |
| 4143 | Py_buffer buf; |
| 4144 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 4145 | goto error; |
| 4146 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4147 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4148 | PyBuffer_Release(&buf); |
| 4149 | PyErr_SetString(PyExc_TypeError, |
| 4150 | "cadata should be a contiguous buffer with " |
| 4151 | "a single dimension"); |
| 4152 | goto error; |
| 4153 | } |
| 4154 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4155 | PyBuffer_Release(&buf); |
| 4156 | if (r == -1) { |
| 4157 | goto error; |
| 4158 | } |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4159 | } |
| 4160 | else { |
| 4161 | invalid_cadata: |
| 4162 | PyErr_SetString(PyExc_TypeError, |
| 4163 | "cadata should be an ASCII string or a " |
| 4164 | "bytes-like object"); |
| 4165 | goto error; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4166 | } |
| 4167 | } |
| 4168 | |
| 4169 | /* load cafile or capath */ |
| 4170 | if (cafile || capath) { |
| 4171 | if (cafile) |
| 4172 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4173 | if (capath) |
| 4174 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4175 | PySSL_BEGIN_ALLOW_THREADS |
| 4176 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4177 | PySSL_END_ALLOW_THREADS |
| 4178 | if (r != 1) { |
| 4179 | ok = 0; |
| 4180 | if (errno != 0) { |
| 4181 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4182 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4183 | } |
| 4184 | else { |
| 4185 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4186 | } |
| 4187 | goto error; |
| 4188 | } |
| 4189 | } |
| 4190 | goto end; |
| 4191 | |
| 4192 | error: |
| 4193 | ok = 0; |
| 4194 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4195 | Py_XDECREF(cafile_bytes); |
| 4196 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4197 | if (ok) { |
| 4198 | Py_RETURN_NONE; |
| 4199 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4200 | return NULL; |
| 4201 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4204 | /*[clinic input] |
| 4205 | _ssl._SSLContext.load_dh_params |
| 4206 | path as filepath: object |
| 4207 | / |
| 4208 | |
| 4209 | [clinic start generated code]*/ |
| 4210 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4211 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4212 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4213 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4214 | { |
| 4215 | FILE *f; |
| 4216 | DH *dh; |
| 4217 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4218 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4219 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4220 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4221 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4222 | errno = 0; |
| 4223 | PySSL_BEGIN_ALLOW_THREADS |
| 4224 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4225 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4226 | PySSL_END_ALLOW_THREADS |
| 4227 | if (dh == NULL) { |
| 4228 | if (errno != 0) { |
| 4229 | ERR_clear_error(); |
| 4230 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4231 | } |
| 4232 | else { |
| 4233 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4234 | } |
| 4235 | return NULL; |
| 4236 | } |
| 4237 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 4238 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4239 | DH_free(dh); |
| 4240 | Py_RETURN_NONE; |
| 4241 | } |
| 4242 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4243 | /*[clinic input] |
| 4244 | _ssl._SSLContext._wrap_socket |
| 4245 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 4246 | server_side: int |
| 4247 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4248 | * |
| 4249 | owner: object = None |
| 4250 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4251 | |
| 4252 | [clinic start generated code]*/ |
| 4253 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4254 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4255 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4256 | int server_side, PyObject *hostname_obj, |
| 4257 | PyObject *owner, PyObject *session) |
| 4258 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4259 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4260 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4261 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4262 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4263 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4264 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4265 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4266 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4267 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4268 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4269 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4270 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4271 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4272 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4273 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4274 | if (hostname != NULL) |
| 4275 | PyMem_Free(hostname); |
| 4276 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4279 | /*[clinic input] |
| 4280 | _ssl._SSLContext._wrap_bio |
| 4281 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4282 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4283 | server_side: int |
| 4284 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4285 | * |
| 4286 | owner: object = None |
| 4287 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4288 | |
| 4289 | [clinic start generated code]*/ |
| 4290 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4291 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4292 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4293 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4294 | PyObject *hostname_obj, PyObject *owner, |
| 4295 | PyObject *session) |
| 4296 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=8cf22f4d586ac56a]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4297 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4298 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4299 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4300 | |
| 4301 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4302 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4303 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4304 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4305 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4309 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4310 | incoming, outgoing); |
| 4311 | |
| 4312 | PyMem_Free(hostname); |
| 4313 | return res; |
| 4314 | } |
| 4315 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4316 | /*[clinic input] |
| 4317 | _ssl._SSLContext.session_stats |
| 4318 | [clinic start generated code]*/ |
| 4319 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4320 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4321 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4322 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4323 | { |
| 4324 | int r; |
| 4325 | PyObject *value, *stats = PyDict_New(); |
| 4326 | if (!stats) |
| 4327 | return NULL; |
| 4328 | |
| 4329 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4330 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4331 | if (value == NULL) \ |
| 4332 | goto error; \ |
| 4333 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4334 | Py_DECREF(value); \ |
| 4335 | if (r < 0) \ |
| 4336 | goto error; |
| 4337 | |
| 4338 | ADD_STATS(number, "number"); |
| 4339 | ADD_STATS(connect, "connect"); |
| 4340 | ADD_STATS(connect_good, "connect_good"); |
| 4341 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4342 | ADD_STATS(accept, "accept"); |
| 4343 | ADD_STATS(accept_good, "accept_good"); |
| 4344 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4345 | ADD_STATS(accept, "accept"); |
| 4346 | ADD_STATS(hits, "hits"); |
| 4347 | ADD_STATS(misses, "misses"); |
| 4348 | ADD_STATS(timeouts, "timeouts"); |
| 4349 | ADD_STATS(cache_full, "cache_full"); |
| 4350 | |
| 4351 | #undef ADD_STATS |
| 4352 | |
| 4353 | return stats; |
| 4354 | |
| 4355 | error: |
| 4356 | Py_DECREF(stats); |
| 4357 | return NULL; |
| 4358 | } |
| 4359 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4360 | /*[clinic input] |
| 4361 | _ssl._SSLContext.set_default_verify_paths |
| 4362 | [clinic start generated code]*/ |
| 4363 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4364 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4365 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4366 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4367 | { |
| 4368 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 4369 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4370 | return NULL; |
| 4371 | } |
| 4372 | Py_RETURN_NONE; |
| 4373 | } |
| 4374 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4375 | #ifndef OPENSSL_NO_ECDH |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4376 | /*[clinic input] |
| 4377 | _ssl._SSLContext.set_ecdh_curve |
| 4378 | name: object |
| 4379 | / |
| 4380 | |
| 4381 | [clinic start generated code]*/ |
| 4382 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4383 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4384 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4385 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4386 | { |
| 4387 | PyObject *name_bytes; |
| 4388 | int nid; |
| 4389 | EC_KEY *key; |
| 4390 | |
| 4391 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4392 | return NULL; |
| 4393 | assert(PyBytes_Check(name_bytes)); |
| 4394 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4395 | Py_DECREF(name_bytes); |
| 4396 | if (nid == 0) { |
| 4397 | PyErr_Format(PyExc_ValueError, |
| 4398 | "unknown elliptic curve name %R", name); |
| 4399 | return NULL; |
| 4400 | } |
| 4401 | key = EC_KEY_new_by_curve_name(nid); |
| 4402 | if (key == NULL) { |
| 4403 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4404 | return NULL; |
| 4405 | } |
| 4406 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4407 | EC_KEY_free(key); |
| 4408 | Py_RETURN_NONE; |
| 4409 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4410 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4411 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4412 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4413 | static int |
| 4414 | _servername_callback(SSL *s, int *al, void *args) |
| 4415 | { |
| 4416 | int ret; |
| 4417 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 4418 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4419 | PyObject *result; |
| 4420 | /* The high-level ssl.SSLSocket object */ |
| 4421 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4422 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4423 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4424 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4425 | if (ssl_ctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4426 | /* remove race condition in this the call back while if removing the |
| 4427 | * callback is in progress */ |
| 4428 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4429 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | ssl = SSL_get_app_data(s); |
| 4433 | assert(PySSLSocket_Check(ssl)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4434 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4435 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4436 | * SSL connection and that has a .context attribute that can be changed to |
| 4437 | * identify the requested hostname. Since the official API is the Python |
| 4438 | * level API we want to pass the callback a Python level object rather than |
| 4439 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4440 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4441 | * will be passed. If both do not exist only then the C-level object is |
| 4442 | * passed. */ |
| 4443 | if (ssl->owner) |
| 4444 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4445 | else if (ssl->Socket) |
| 4446 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4447 | else |
| 4448 | ssl_socket = (PyObject *) ssl; |
| 4449 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4450 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4451 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4452 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4453 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4454 | if (servername == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4455 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4456 | Py_None, ssl_ctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4457 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4458 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4459 | PyObject *servername_bytes; |
| 4460 | PyObject *servername_str; |
| 4461 | |
| 4462 | servername_bytes = PyBytes_FromString(servername); |
| 4463 | if (servername_bytes == NULL) { |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4464 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 4465 | goto error; |
| 4466 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4467 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4468 | * back into a str object, but still as an A-label (bpo-28414) |
| 4469 | */ |
| 4470 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
| 4471 | Py_DECREF(servername_bytes); |
| 4472 | if (servername_str == NULL) { |
| 4473 | PyErr_WriteUnraisable(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4474 | goto error; |
| 4475 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4476 | result = PyObject_CallFunctionObjArgs( |
| 4477 | ssl_ctx->set_sni_cb, ssl_socket, servername_str, |
| 4478 | ssl_ctx, NULL); |
| 4479 | Py_DECREF(servername_str); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4480 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4481 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4482 | |
| 4483 | if (result == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4484 | PyErr_WriteUnraisable(ssl_ctx->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4485 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4486 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4487 | } |
| 4488 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4489 | /* Result may be None, a SSLContext or an integer |
| 4490 | * None and SSLContext are OK, integer or other values are an error. |
| 4491 | */ |
| 4492 | if (result == Py_None) { |
| 4493 | ret = SSL_TLSEXT_ERR_OK; |
| 4494 | } else { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4495 | *al = (int) PyLong_AsLong(result); |
| 4496 | if (PyErr_Occurred()) { |
| 4497 | PyErr_WriteUnraisable(result); |
| 4498 | *al = SSL_AD_INTERNAL_ERROR; |
| 4499 | } |
| 4500 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4501 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4502 | Py_DECREF(result); |
| 4503 | } |
| 4504 | |
| 4505 | PyGILState_Release(gstate); |
| 4506 | return ret; |
| 4507 | |
| 4508 | error: |
| 4509 | Py_DECREF(ssl_socket); |
| 4510 | *al = SSL_AD_INTERNAL_ERROR; |
| 4511 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4512 | PyGILState_Release(gstate); |
| 4513 | return ret; |
| 4514 | } |
Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 4515 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4516 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4517 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4518 | get_sni_callback(PySSLContext *self, void *c) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4519 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4520 | PyObject *cb = self->set_sni_cb; |
| 4521 | if (cb == NULL) { |
| 4522 | Py_RETURN_NONE; |
| 4523 | } |
| 4524 | Py_INCREF(cb); |
| 4525 | return cb; |
| 4526 | } |
| 4527 | |
| 4528 | static int |
| 4529 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4530 | { |
| 4531 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4532 | PyErr_SetString(PyExc_ValueError, |
| 4533 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4534 | return -1; |
| 4535 | } |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4536 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4537 | Py_CLEAR(self->set_sni_cb); |
| 4538 | if (arg == Py_None) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4539 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4540 | } |
| 4541 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4542 | if (!PyCallable_Check(arg)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4543 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4544 | PyErr_SetString(PyExc_TypeError, |
| 4545 | "not a callable object"); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4546 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4547 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4548 | Py_INCREF(arg); |
| 4549 | self->set_sni_cb = arg; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4550 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4551 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4552 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4553 | return 0; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4554 | #else |
| 4555 | PyErr_SetString(PyExc_NotImplementedError, |
| 4556 | "The TLS extension servername callback, " |
| 4557 | "SSL_CTX_set_tlsext_servername_callback, " |
| 4558 | "is not in the current OpenSSL library."); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4559 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4560 | #endif |
| 4561 | } |
| 4562 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4563 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4564 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4565 | \n\ |
| 4566 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4567 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4568 | See RFC 6066 for details of the SNI extension."); |
| 4569 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4570 | /*[clinic input] |
| 4571 | _ssl._SSLContext.cert_store_stats |
| 4572 | |
| 4573 | Returns quantities of loaded X.509 certificates. |
| 4574 | |
| 4575 | X.509 certificates with a CA extension and certificate revocation lists |
| 4576 | inside the context's cert store. |
| 4577 | |
| 4578 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4579 | been used at least once. |
| 4580 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4581 | |
| 4582 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4583 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4584 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4585 | { |
| 4586 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4587 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4588 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4589 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4590 | |
| 4591 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4592 | objs = X509_STORE_get0_objects(store); |
| 4593 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4594 | obj = sk_X509_OBJECT_value(objs, i); |
| 4595 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4596 | case X509_LU_X509: |
| 4597 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4598 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4599 | ca++; |
| 4600 | } |
| 4601 | break; |
| 4602 | case X509_LU_CRL: |
| 4603 | crl++; |
| 4604 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4605 | default: |
| 4606 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4607 | * As far as I can tell they are internal states and never |
| 4608 | * stored in a cert store */ |
| 4609 | break; |
| 4610 | } |
| 4611 | } |
| 4612 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4613 | "x509_ca", ca); |
| 4614 | } |
| 4615 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4616 | /*[clinic input] |
| 4617 | _ssl._SSLContext.get_ca_certs |
| 4618 | binary_form: bool = False |
| 4619 | |
| 4620 | Returns a list of dicts with information of loaded CA certs. |
| 4621 | |
| 4622 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4623 | certificate. |
| 4624 | |
| 4625 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4626 | been used at least once. |
| 4627 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4628 | |
| 4629 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4630 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4631 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4632 | { |
| 4633 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4634 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4635 | PyObject *ci = NULL, *rlist = NULL; |
| 4636 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4637 | |
| 4638 | if ((rlist = PyList_New(0)) == NULL) { |
| 4639 | return NULL; |
| 4640 | } |
| 4641 | |
| 4642 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4643 | objs = X509_STORE_get0_objects(store); |
| 4644 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4645 | X509_OBJECT *obj; |
| 4646 | X509 *cert; |
| 4647 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4648 | obj = sk_X509_OBJECT_value(objs, i); |
| 4649 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4650 | /* not a x509 cert */ |
| 4651 | continue; |
| 4652 | } |
| 4653 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4654 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4655 | if (!X509_check_ca(cert)) { |
| 4656 | continue; |
| 4657 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4658 | if (binary_form) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4659 | ci = _certificate_to_der(cert); |
| 4660 | } else { |
| 4661 | ci = _decode_certificate(cert); |
| 4662 | } |
| 4663 | if (ci == NULL) { |
| 4664 | goto error; |
| 4665 | } |
| 4666 | if (PyList_Append(rlist, ci) == -1) { |
| 4667 | goto error; |
| 4668 | } |
| 4669 | Py_CLEAR(ci); |
| 4670 | } |
| 4671 | return rlist; |
| 4672 | |
| 4673 | error: |
| 4674 | Py_XDECREF(ci); |
| 4675 | Py_XDECREF(rlist); |
| 4676 | return NULL; |
| 4677 | } |
| 4678 | |
| 4679 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4680 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4681 | {"check_hostname", (getter) get_check_hostname, |
| 4682 | (setter) set_check_hostname, NULL}, |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4683 | {"_host_flags", (getter) get_host_flags, |
| 4684 | (setter) set_host_flags, NULL}, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4685 | #if SSL_CTRL_GET_MAX_PROTO_VERSION |
| 4686 | {"minimum_version", (getter) get_minimum_version, |
| 4687 | (setter) set_minimum_version, NULL}, |
| 4688 | {"maximum_version", (getter) get_maximum_version, |
| 4689 | (setter) set_maximum_version, NULL}, |
| 4690 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4691 | #ifdef HAVE_OPENSSL_KEYLOG |
| 4692 | {"keylog_filename", (getter) _PySSLContext_get_keylog_filename, |
| 4693 | (setter) _PySSLContext_set_keylog_filename, NULL}, |
| 4694 | #endif |
| 4695 | {"_msg_callback", (getter) _PySSLContext_get_msg_callback, |
| 4696 | (setter) _PySSLContext_set_msg_callback, NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4697 | {"sni_callback", (getter) get_sni_callback, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4698 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 4699 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 4700 | {"num_tickets", (getter) get_num_tickets, |
| 4701 | (setter) set_num_tickets, PySSLContext_num_tickets_doc}, |
| 4702 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4703 | {"options", (getter) get_options, |
| 4704 | (setter) set_options, NULL}, |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4705 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4706 | #ifdef TLS1_3_VERSION |
| 4707 | (setter) set_post_handshake_auth, |
| 4708 | #else |
| 4709 | NULL, |
| 4710 | #endif |
| 4711 | NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4712 | {"protocol", (getter) get_protocol, |
| 4713 | NULL, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4714 | {"verify_flags", (getter) get_verify_flags, |
| 4715 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4716 | {"verify_mode", (getter) get_verify_mode, |
| 4717 | (setter) set_verify_mode, NULL}, |
| 4718 | {NULL}, /* sentinel */ |
| 4719 | }; |
| 4720 | |
| 4721 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4722 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4723 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4724 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4725 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4726 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4727 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4728 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4729 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4730 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4731 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4732 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4733 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4734 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4735 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4736 | {NULL, NULL} /* sentinel */ |
| 4737 | }; |
| 4738 | |
| 4739 | static PyTypeObject PySSLContext_Type = { |
| 4740 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4741 | "_ssl._SSLContext", /*tp_name*/ |
| 4742 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 4743 | 0, /*tp_itemsize*/ |
| 4744 | (destructor)context_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4745 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4746 | 0, /*tp_getattr*/ |
| 4747 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4748 | 0, /*tp_as_async*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4749 | 0, /*tp_repr*/ |
| 4750 | 0, /*tp_as_number*/ |
| 4751 | 0, /*tp_as_sequence*/ |
| 4752 | 0, /*tp_as_mapping*/ |
| 4753 | 0, /*tp_hash*/ |
| 4754 | 0, /*tp_call*/ |
| 4755 | 0, /*tp_str*/ |
| 4756 | 0, /*tp_getattro*/ |
| 4757 | 0, /*tp_setattro*/ |
| 4758 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4759 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4760 | 0, /*tp_doc*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4761 | (traverseproc) context_traverse, /*tp_traverse*/ |
| 4762 | (inquiry) context_clear, /*tp_clear*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4763 | 0, /*tp_richcompare*/ |
| 4764 | 0, /*tp_weaklistoffset*/ |
| 4765 | 0, /*tp_iter*/ |
| 4766 | 0, /*tp_iternext*/ |
| 4767 | context_methods, /*tp_methods*/ |
| 4768 | 0, /*tp_members*/ |
| 4769 | context_getsetlist, /*tp_getset*/ |
| 4770 | 0, /*tp_base*/ |
| 4771 | 0, /*tp_dict*/ |
| 4772 | 0, /*tp_descr_get*/ |
| 4773 | 0, /*tp_descr_set*/ |
| 4774 | 0, /*tp_dictoffset*/ |
| 4775 | 0, /*tp_init*/ |
| 4776 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4777 | _ssl__SSLContext, /*tp_new*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4778 | }; |
| 4779 | |
| 4780 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4781 | /* |
| 4782 | * MemoryBIO objects |
| 4783 | */ |
| 4784 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4785 | /*[clinic input] |
| 4786 | @classmethod |
| 4787 | _ssl.MemoryBIO.__new__ |
| 4788 | |
| 4789 | [clinic start generated code]*/ |
| 4790 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4791 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4792 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4793 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4794 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4795 | BIO *bio; |
| 4796 | PySSLMemoryBIO *self; |
| 4797 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4798 | bio = BIO_new(BIO_s_mem()); |
| 4799 | if (bio == NULL) { |
| 4800 | PyErr_SetString(PySSLErrorObject, |
| 4801 | "failed to allocate BIO"); |
| 4802 | return NULL; |
| 4803 | } |
| 4804 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4805 | * just that no data is currently available. The SSL routines should retry |
| 4806 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4807 | BIO_set_retry_read(bio); |
| 4808 | BIO_set_mem_eof_return(bio, -1); |
| 4809 | |
| 4810 | assert(type != NULL && type->tp_alloc != NULL); |
| 4811 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4812 | if (self == NULL) { |
| 4813 | BIO_free(bio); |
| 4814 | return NULL; |
| 4815 | } |
| 4816 | self->bio = bio; |
| 4817 | self->eof_written = 0; |
| 4818 | |
| 4819 | return (PyObject *) self; |
| 4820 | } |
| 4821 | |
| 4822 | static void |
| 4823 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4824 | { |
| 4825 | BIO_free(self->bio); |
| 4826 | Py_TYPE(self)->tp_free(self); |
| 4827 | } |
| 4828 | |
| 4829 | static PyObject * |
| 4830 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4831 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4832 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4836 | "The number of bytes pending in the memory BIO."); |
| 4837 | |
| 4838 | static PyObject * |
| 4839 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4840 | { |
| 4841 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4842 | && self->eof_written); |
| 4843 | } |
| 4844 | |
| 4845 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4846 | "Whether the memory BIO is at EOF."); |
| 4847 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4848 | /*[clinic input] |
| 4849 | _ssl.MemoryBIO.read |
| 4850 | size as len: int = -1 |
| 4851 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4852 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4853 | Read up to size bytes from the memory BIO. |
| 4854 | |
| 4855 | If size is not specified, read the entire buffer. |
| 4856 | If the return value is an empty bytes instance, this means either |
| 4857 | EOF or that no data is available. Use the "eof" property to |
| 4858 | distinguish between the two. |
| 4859 | [clinic start generated code]*/ |
| 4860 | |
| 4861 | static PyObject * |
| 4862 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4863 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4864 | { |
| 4865 | int avail, nbytes; |
| 4866 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4867 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4868 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4869 | if ((len < 0) || (len > avail)) |
| 4870 | len = avail; |
| 4871 | |
| 4872 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4873 | if ((result == NULL) || (len == 0)) |
| 4874 | return result; |
| 4875 | |
| 4876 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4877 | if (nbytes < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4878 | Py_DECREF(result); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4879 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4880 | return NULL; |
| 4881 | } |
| 4882 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4883 | /* There should never be any short reads but check anyway. */ |
| 4884 | if (nbytes < len) { |
| 4885 | _PyBytes_Resize(&result, nbytes); |
| 4886 | } |
| 4887 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4888 | return result; |
| 4889 | } |
| 4890 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4891 | /*[clinic input] |
| 4892 | _ssl.MemoryBIO.write |
| 4893 | b: Py_buffer |
| 4894 | / |
| 4895 | |
| 4896 | Writes the bytes b into the memory BIO. |
| 4897 | |
| 4898 | Returns the number of bytes written. |
| 4899 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4900 | |
| 4901 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4902 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4903 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4904 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4905 | int nbytes; |
| 4906 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4907 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4908 | PyErr_Format(PyExc_OverflowError, |
| 4909 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4910 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | if (self->eof_written) { |
| 4914 | PyErr_SetString(PySSLErrorObject, |
| 4915 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4916 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4917 | } |
| 4918 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4919 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4920 | if (nbytes < 0) { |
| 4921 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4922 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4925 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4926 | } |
| 4927 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4928 | /*[clinic input] |
| 4929 | _ssl.MemoryBIO.write_eof |
| 4930 | |
| 4931 | Write an EOF marker to the memory BIO. |
| 4932 | |
| 4933 | When all data has been read, the "eof" property will be True. |
| 4934 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4935 | |
| 4936 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4937 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4938 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4939 | { |
| 4940 | self->eof_written = 1; |
| 4941 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4942 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4943 | BIO_clear_retry_flags(self->bio); |
| 4944 | BIO_set_mem_eof_return(self->bio, 0); |
| 4945 | |
| 4946 | Py_RETURN_NONE; |
| 4947 | } |
| 4948 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4949 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4950 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4951 | PySSL_memory_bio_pending_doc}, |
| 4952 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 4953 | PySSL_memory_bio_eof_doc}, |
| 4954 | {NULL}, /* sentinel */ |
| 4955 | }; |
| 4956 | |
| 4957 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4958 | _SSL_MEMORYBIO_READ_METHODDEF |
| 4959 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 4960 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4961 | {NULL, NULL} /* sentinel */ |
| 4962 | }; |
| 4963 | |
| 4964 | static PyTypeObject PySSLMemoryBIO_Type = { |
| 4965 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4966 | "_ssl.MemoryBIO", /*tp_name*/ |
| 4967 | sizeof(PySSLMemoryBIO), /*tp_basicsize*/ |
| 4968 | 0, /*tp_itemsize*/ |
| 4969 | (destructor)memory_bio_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4970 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4971 | 0, /*tp_getattr*/ |
| 4972 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4973 | 0, /*tp_as_async*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4974 | 0, /*tp_repr*/ |
| 4975 | 0, /*tp_as_number*/ |
| 4976 | 0, /*tp_as_sequence*/ |
| 4977 | 0, /*tp_as_mapping*/ |
| 4978 | 0, /*tp_hash*/ |
| 4979 | 0, /*tp_call*/ |
| 4980 | 0, /*tp_str*/ |
| 4981 | 0, /*tp_getattro*/ |
| 4982 | 0, /*tp_setattro*/ |
| 4983 | 0, /*tp_as_buffer*/ |
| 4984 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4985 | 0, /*tp_doc*/ |
| 4986 | 0, /*tp_traverse*/ |
| 4987 | 0, /*tp_clear*/ |
| 4988 | 0, /*tp_richcompare*/ |
| 4989 | 0, /*tp_weaklistoffset*/ |
| 4990 | 0, /*tp_iter*/ |
| 4991 | 0, /*tp_iternext*/ |
| 4992 | memory_bio_methods, /*tp_methods*/ |
| 4993 | 0, /*tp_members*/ |
| 4994 | memory_bio_getsetlist, /*tp_getset*/ |
| 4995 | 0, /*tp_base*/ |
| 4996 | 0, /*tp_dict*/ |
| 4997 | 0, /*tp_descr_get*/ |
| 4998 | 0, /*tp_descr_set*/ |
| 4999 | 0, /*tp_dictoffset*/ |
| 5000 | 0, /*tp_init*/ |
| 5001 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5002 | _ssl_MemoryBIO, /*tp_new*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5003 | }; |
| 5004 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5005 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5006 | /* |
| 5007 | * SSL Session object |
| 5008 | */ |
| 5009 | |
| 5010 | static void |
| 5011 | PySSLSession_dealloc(PySSLSession *self) |
| 5012 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 5013 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5014 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5015 | Py_XDECREF(self->ctx); |
| 5016 | if (self->session != NULL) { |
| 5017 | SSL_SESSION_free(self->session); |
| 5018 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5019 | PyObject_GC_Del(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | static PyObject * |
| 5023 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 5024 | { |
| 5025 | int result; |
| 5026 | |
| 5027 | if (left == NULL || right == NULL) { |
| 5028 | PyErr_BadInternalCall(); |
| 5029 | return NULL; |
| 5030 | } |
| 5031 | |
| 5032 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 5033 | Py_RETURN_NOTIMPLEMENTED; |
| 5034 | } |
| 5035 | |
| 5036 | if (left == right) { |
| 5037 | result = 0; |
| 5038 | } else { |
| 5039 | const unsigned char *left_id, *right_id; |
| 5040 | unsigned int left_len, right_len; |
| 5041 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 5042 | &left_len); |
| 5043 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 5044 | &right_len); |
| 5045 | if (left_len == right_len) { |
| 5046 | result = memcmp(left_id, right_id, left_len); |
| 5047 | } else { |
| 5048 | result = 1; |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | switch (op) { |
| 5053 | case Py_EQ: |
| 5054 | if (result == 0) { |
| 5055 | Py_RETURN_TRUE; |
| 5056 | } else { |
| 5057 | Py_RETURN_FALSE; |
| 5058 | } |
| 5059 | break; |
| 5060 | case Py_NE: |
| 5061 | if (result != 0) { |
| 5062 | Py_RETURN_TRUE; |
| 5063 | } else { |
| 5064 | Py_RETURN_FALSE; |
| 5065 | } |
| 5066 | break; |
| 5067 | case Py_LT: |
| 5068 | case Py_LE: |
| 5069 | case Py_GT: |
| 5070 | case Py_GE: |
| 5071 | Py_RETURN_NOTIMPLEMENTED; |
| 5072 | break; |
| 5073 | default: |
| 5074 | PyErr_BadArgument(); |
| 5075 | return NULL; |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | static int |
| 5080 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 5081 | { |
| 5082 | Py_VISIT(self->ctx); |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | static int |
| 5087 | PySSLSession_clear(PySSLSession *self) |
| 5088 | { |
| 5089 | Py_CLEAR(self->ctx); |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
| 5093 | |
| 5094 | static PyObject * |
| 5095 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 5096 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 5097 | } |
| 5098 | |
| 5099 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 5100 | "Session creation time (seconds since epoch)."); |
| 5101 | |
| 5102 | |
| 5103 | static PyObject * |
| 5104 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 5105 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 5106 | } |
| 5107 | |
| 5108 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 5109 | "Session timeout (delta in seconds)."); |
| 5110 | |
| 5111 | |
| 5112 | static PyObject * |
| 5113 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 5114 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 5115 | return PyLong_FromUnsignedLong(hint); |
| 5116 | } |
| 5117 | |
| 5118 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 5119 | "Ticket life time hint."); |
| 5120 | |
| 5121 | |
| 5122 | static PyObject * |
| 5123 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 5124 | const unsigned char *id; |
| 5125 | unsigned int len; |
| 5126 | id = SSL_SESSION_get_id(self->session, &len); |
| 5127 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 5128 | } |
| 5129 | |
| 5130 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 5131 | "Session id"); |
| 5132 | |
| 5133 | |
| 5134 | static PyObject * |
| 5135 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 5136 | if (SSL_SESSION_has_ticket(self->session)) { |
| 5137 | Py_RETURN_TRUE; |
| 5138 | } else { |
| 5139 | Py_RETURN_FALSE; |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5144 | "Does the session contain a ticket?"); |
| 5145 | |
| 5146 | |
| 5147 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5148 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5149 | PySSLSession_get_has_ticket_doc}, |
| 5150 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5151 | PySSLSession_get_session_id_doc}, |
| 5152 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5153 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5154 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5155 | PySSLSession_get_time_doc}, |
| 5156 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5157 | PySSLSession_get_timeout_doc}, |
| 5158 | {NULL}, /* sentinel */ |
| 5159 | }; |
| 5160 | |
| 5161 | static PyTypeObject PySSLSession_Type = { |
| 5162 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5163 | "_ssl.Session", /*tp_name*/ |
| 5164 | sizeof(PySSLSession), /*tp_basicsize*/ |
| 5165 | 0, /*tp_itemsize*/ |
| 5166 | (destructor)PySSLSession_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5167 | 0, /*tp_vectorcall_offset*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5168 | 0, /*tp_getattr*/ |
| 5169 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5170 | 0, /*tp_as_async*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5171 | 0, /*tp_repr*/ |
| 5172 | 0, /*tp_as_number*/ |
| 5173 | 0, /*tp_as_sequence*/ |
| 5174 | 0, /*tp_as_mapping*/ |
| 5175 | 0, /*tp_hash*/ |
| 5176 | 0, /*tp_call*/ |
| 5177 | 0, /*tp_str*/ |
| 5178 | 0, /*tp_getattro*/ |
| 5179 | 0, /*tp_setattro*/ |
| 5180 | 0, /*tp_as_buffer*/ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5181 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5182 | 0, /*tp_doc*/ |
| 5183 | (traverseproc)PySSLSession_traverse, /*tp_traverse*/ |
| 5184 | (inquiry)PySSLSession_clear, /*tp_clear*/ |
| 5185 | PySSLSession_richcompare, /*tp_richcompare*/ |
| 5186 | 0, /*tp_weaklistoffset*/ |
| 5187 | 0, /*tp_iter*/ |
| 5188 | 0, /*tp_iternext*/ |
| 5189 | 0, /*tp_methods*/ |
| 5190 | 0, /*tp_members*/ |
| 5191 | PySSLSession_getsetlist, /*tp_getset*/ |
| 5192 | }; |
| 5193 | |
| 5194 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5195 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5196 | /*[clinic input] |
| 5197 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5198 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5199 | entropy: double |
| 5200 | / |
| 5201 | |
| 5202 | Mix string into the OpenSSL PRNG state. |
| 5203 | |
| 5204 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5205 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5206 | [clinic start generated code]*/ |
| 5207 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5208 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5209 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5210 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5211 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5212 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5213 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5214 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5215 | buf = (const char *)view->buf; |
| 5216 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5217 | do { |
| 5218 | written = Py_MIN(len, INT_MAX); |
| 5219 | RAND_add(buf, (int)written, entropy); |
| 5220 | buf += written; |
| 5221 | len -= written; |
| 5222 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5223 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5226 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5227 | PySSL_RAND(int len, int pseudo) |
| 5228 | { |
| 5229 | int ok; |
| 5230 | PyObject *bytes; |
| 5231 | unsigned long err; |
| 5232 | const char *errstr; |
| 5233 | PyObject *v; |
| 5234 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5235 | if (len < 0) { |
| 5236 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5237 | return NULL; |
| 5238 | } |
| 5239 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5240 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5241 | if (bytes == NULL) |
| 5242 | return NULL; |
| 5243 | if (pseudo) { |
| 5244 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5245 | if (ok == 0 || ok == 1) |
| 5246 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5247 | } |
| 5248 | else { |
| 5249 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5250 | if (ok == 1) |
| 5251 | return bytes; |
| 5252 | } |
| 5253 | Py_DECREF(bytes); |
| 5254 | |
| 5255 | err = ERR_get_error(); |
| 5256 | errstr = ERR_reason_error_string(err); |
| 5257 | v = Py_BuildValue("(ks)", err, errstr); |
| 5258 | if (v != NULL) { |
| 5259 | PyErr_SetObject(PySSLErrorObject, v); |
| 5260 | Py_DECREF(v); |
| 5261 | } |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5265 | /*[clinic input] |
| 5266 | _ssl.RAND_bytes |
| 5267 | n: int |
| 5268 | / |
| 5269 | |
| 5270 | Generate n cryptographically strong pseudo-random bytes. |
| 5271 | [clinic start generated code]*/ |
| 5272 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5273 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5274 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5275 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5276 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5277 | return PySSL_RAND(n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5278 | } |
| 5279 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5280 | /*[clinic input] |
| 5281 | _ssl.RAND_pseudo_bytes |
| 5282 | n: int |
| 5283 | / |
| 5284 | |
| 5285 | Generate n pseudo-random bytes. |
| 5286 | |
| 5287 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5288 | if the bytes generated are cryptographically strong. |
| 5289 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5290 | |
| 5291 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5292 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5293 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5294 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5295 | return PySSL_RAND(n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5296 | } |
| 5297 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5298 | /*[clinic input] |
| 5299 | _ssl.RAND_status |
| 5300 | |
| 5301 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 5302 | |
| 5303 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5304 | using the ssl() function. |
| 5305 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5306 | |
| 5307 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5308 | _ssl_RAND_status_impl(PyObject *module) |
| 5309 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5310 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5311 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5314 | #ifndef OPENSSL_NO_EGD |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5315 | /* LCOV_EXCL_START */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5316 | /*[clinic input] |
| 5317 | _ssl.RAND_egd |
| 5318 | path: object(converter="PyUnicode_FSConverter") |
| 5319 | / |
| 5320 | |
| 5321 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 5322 | |
| 5323 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 5324 | fails or if it does not provide enough data to seed PRNG. |
| 5325 | [clinic start generated code]*/ |
| 5326 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5327 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5328 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 5329 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5330 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5331 | int bytes = RAND_egd(PyBytes_AsString(path)); |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5332 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5333 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5334 | PyErr_SetString(PySSLErrorObject, |
| 5335 | "EGD connection failed or EGD did not return " |
| 5336 | "enough data to seed the PRNG"); |
| 5337 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5338 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5339 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5340 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5341 | /* LCOV_EXCL_STOP */ |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5342 | #endif /* OPENSSL_NO_EGD */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5343 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5344 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5345 | |
| 5346 | /*[clinic input] |
| 5347 | _ssl.get_default_verify_paths |
| 5348 | |
| 5349 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5350 | |
| 5351 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5352 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5353 | |
| 5354 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5355 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5356 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5357 | { |
| 5358 | PyObject *ofile_env = NULL; |
| 5359 | PyObject *ofile = NULL; |
| 5360 | PyObject *odir_env = NULL; |
| 5361 | PyObject *odir = NULL; |
| 5362 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5363 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5364 | const char *tmp = (info); \ |
| 5365 | target = NULL; \ |
| 5366 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5367 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5368 | target = PyBytes_FromString(tmp); } \ |
| 5369 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5370 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5371 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5372 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5373 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5374 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5375 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5376 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5377 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5378 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5379 | |
| 5380 | error: |
| 5381 | Py_XDECREF(ofile_env); |
| 5382 | Py_XDECREF(ofile); |
| 5383 | Py_XDECREF(odir_env); |
| 5384 | Py_XDECREF(odir); |
| 5385 | return NULL; |
| 5386 | } |
| 5387 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5388 | static PyObject* |
| 5389 | asn1obj2py(ASN1_OBJECT *obj) |
| 5390 | { |
| 5391 | int nid; |
| 5392 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5393 | |
| 5394 | nid = OBJ_obj2nid(obj); |
| 5395 | if (nid == NID_undef) { |
| 5396 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5397 | return NULL; |
| 5398 | } |
| 5399 | sn = OBJ_nid2sn(nid); |
| 5400 | ln = OBJ_nid2ln(nid); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5401 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5402 | } |
| 5403 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5404 | /*[clinic input] |
| 5405 | _ssl.txt2obj |
| 5406 | txt: str |
| 5407 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5408 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5409 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5410 | |
| 5411 | By default objects are looked up by OID. With name=True short and |
| 5412 | long name are also matched. |
| 5413 | [clinic start generated code]*/ |
| 5414 | |
| 5415 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5416 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5417 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5418 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5419 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5420 | ASN1_OBJECT *obj; |
| 5421 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5422 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5423 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5424 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5425 | return NULL; |
| 5426 | } |
| 5427 | result = asn1obj2py(obj); |
| 5428 | ASN1_OBJECT_free(obj); |
| 5429 | return result; |
| 5430 | } |
| 5431 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5432 | /*[clinic input] |
| 5433 | _ssl.nid2obj |
| 5434 | nid: int |
| 5435 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5436 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5437 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5438 | [clinic start generated code]*/ |
| 5439 | |
| 5440 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5441 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5442 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5443 | { |
| 5444 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5445 | ASN1_OBJECT *obj; |
| 5446 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5447 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5448 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5449 | return NULL; |
| 5450 | } |
| 5451 | obj = OBJ_nid2obj(nid); |
| 5452 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5453 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5454 | return NULL; |
| 5455 | } |
| 5456 | result = asn1obj2py(obj); |
| 5457 | ASN1_OBJECT_free(obj); |
| 5458 | return result; |
| 5459 | } |
| 5460 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5461 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5462 | |
| 5463 | static PyObject* |
| 5464 | certEncodingType(DWORD encodingType) |
| 5465 | { |
| 5466 | static PyObject *x509_asn = NULL; |
| 5467 | static PyObject *pkcs_7_asn = NULL; |
| 5468 | |
| 5469 | if (x509_asn == NULL) { |
| 5470 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5471 | if (x509_asn == NULL) |
| 5472 | return NULL; |
| 5473 | } |
| 5474 | if (pkcs_7_asn == NULL) { |
| 5475 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5476 | if (pkcs_7_asn == NULL) |
| 5477 | return NULL; |
| 5478 | } |
| 5479 | switch(encodingType) { |
| 5480 | case X509_ASN_ENCODING: |
| 5481 | Py_INCREF(x509_asn); |
| 5482 | return x509_asn; |
| 5483 | case PKCS_7_ASN_ENCODING: |
| 5484 | Py_INCREF(pkcs_7_asn); |
| 5485 | return pkcs_7_asn; |
| 5486 | default: |
| 5487 | return PyLong_FromLong(encodingType); |
| 5488 | } |
| 5489 | } |
| 5490 | |
| 5491 | static PyObject* |
| 5492 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5493 | { |
| 5494 | CERT_ENHKEY_USAGE *usage; |
| 5495 | DWORD size, error, i; |
| 5496 | PyObject *retval; |
| 5497 | |
| 5498 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5499 | error = GetLastError(); |
| 5500 | if (error == CRYPT_E_NOT_FOUND) { |
| 5501 | Py_RETURN_TRUE; |
| 5502 | } |
| 5503 | return PyErr_SetFromWindowsErr(error); |
| 5504 | } |
| 5505 | |
| 5506 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5507 | if (usage == NULL) { |
| 5508 | return PyErr_NoMemory(); |
| 5509 | } |
| 5510 | |
| 5511 | /* Now get the actual enhanced usage property */ |
| 5512 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5513 | PyMem_Free(usage); |
| 5514 | error = GetLastError(); |
| 5515 | if (error == CRYPT_E_NOT_FOUND) { |
| 5516 | Py_RETURN_TRUE; |
| 5517 | } |
| 5518 | return PyErr_SetFromWindowsErr(error); |
| 5519 | } |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5520 | retval = PyFrozenSet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5521 | if (retval == NULL) { |
| 5522 | goto error; |
| 5523 | } |
| 5524 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5525 | if (usage->rgpszUsageIdentifier[i]) { |
| 5526 | PyObject *oid; |
| 5527 | int err; |
| 5528 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5529 | if (oid == NULL) { |
| 5530 | Py_CLEAR(retval); |
| 5531 | goto error; |
| 5532 | } |
| 5533 | err = PySet_Add(retval, oid); |
| 5534 | Py_DECREF(oid); |
| 5535 | if (err == -1) { |
| 5536 | Py_CLEAR(retval); |
| 5537 | goto error; |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | error: |
| 5542 | PyMem_Free(usage); |
| 5543 | return retval; |
| 5544 | } |
| 5545 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5546 | static HCERTSTORE |
| 5547 | ssl_collect_certificates(const char *store_name) |
| 5548 | { |
| 5549 | /* this function collects the system certificate stores listed in |
| 5550 | * system_stores into a collection certificate store for being |
| 5551 | * enumerated. The store must be readable to be added to the |
| 5552 | * store collection. |
| 5553 | */ |
| 5554 | |
| 5555 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5556 | static DWORD system_stores[] = { |
| 5557 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5558 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5559 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5560 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5561 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5562 | CERT_SYSTEM_STORE_SERVICES, |
| 5563 | CERT_SYSTEM_STORE_USERS}; |
| 5564 | size_t i, storesAdded; |
| 5565 | BOOL result; |
| 5566 | |
| 5567 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5568 | (HCRYPTPROV)NULL, 0, NULL); |
| 5569 | if (!hCollectionStore) { |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | storesAdded = 0; |
| 5573 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5574 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5575 | (HCRYPTPROV)NULL, |
| 5576 | CERT_STORE_READONLY_FLAG | |
| 5577 | system_stores[i], store_name); |
| 5578 | if (hSystemStore) { |
| 5579 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5580 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5581 | if (result) { |
| 5582 | ++storesAdded; |
| 5583 | } |
Steve Dower | 5d695b6 | 2019-09-09 06:48:22 -0700 | [diff] [blame] | 5584 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5585 | } |
| 5586 | } |
| 5587 | if (storesAdded == 0) { |
| 5588 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5589 | return NULL; |
| 5590 | } |
| 5591 | |
| 5592 | return hCollectionStore; |
| 5593 | } |
| 5594 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5595 | /*[clinic input] |
| 5596 | _ssl.enum_certificates |
| 5597 | store_name: str |
| 5598 | |
| 5599 | Retrieve certificates from Windows' cert store. |
| 5600 | |
| 5601 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5602 | more cert storages, too. The function returns a list of (bytes, |
| 5603 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5604 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5605 | a set of OIDs or the boolean True. |
| 5606 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5607 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5609 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5610 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5611 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5612 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5613 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5614 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5615 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5616 | |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5617 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5618 | if (result == NULL) { |
| 5619 | return NULL; |
| 5620 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5621 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5622 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5623 | Py_DECREF(result); |
| 5624 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5625 | } |
| 5626 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5627 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5628 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5629 | pCertCtx->cbCertEncoded); |
| 5630 | if (!cert) { |
| 5631 | Py_CLEAR(result); |
| 5632 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5633 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5634 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5635 | Py_CLEAR(result); |
| 5636 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5637 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5638 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5639 | if (keyusage == Py_True) { |
| 5640 | Py_DECREF(keyusage); |
| 5641 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5642 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5643 | if (keyusage == NULL) { |
| 5644 | Py_CLEAR(result); |
| 5645 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5646 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5647 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5648 | Py_CLEAR(result); |
| 5649 | break; |
| 5650 | } |
| 5651 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5652 | cert = NULL; |
| 5653 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5654 | enc = NULL; |
| 5655 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5656 | keyusage = NULL; |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5657 | if (PySet_Add(result, tup) == -1) { |
| 5658 | Py_CLEAR(result); |
| 5659 | Py_CLEAR(tup); |
| 5660 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5661 | } |
| 5662 | Py_CLEAR(tup); |
| 5663 | } |
| 5664 | if (pCertCtx) { |
| 5665 | /* loop ended with an error, need to clean up context manually */ |
| 5666 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | /* In error cases cert, enc and tup may not be NULL */ |
| 5670 | Py_XDECREF(cert); |
| 5671 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5672 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5673 | Py_XDECREF(tup); |
| 5674 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5675 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5676 | associated with the store, in this case our collection store and the |
| 5677 | associated system stores. */ |
| 5678 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5679 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5680 | Py_XDECREF(result); |
| 5681 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5682 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5683 | |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5684 | /* convert set to list */ |
| 5685 | if (result == NULL) { |
| 5686 | return NULL; |
| 5687 | } else { |
| 5688 | PyObject *lst = PySequence_List(result); |
| 5689 | Py_DECREF(result); |
| 5690 | return lst; |
| 5691 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5692 | } |
| 5693 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5694 | /*[clinic input] |
| 5695 | _ssl.enum_crls |
| 5696 | store_name: str |
| 5697 | |
| 5698 | Retrieve CRLs from Windows' cert store. |
| 5699 | |
| 5700 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5701 | more cert storages, too. The function returns a list of (bytes, |
| 5702 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5703 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5704 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5705 | |
| 5706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5707 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5708 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5709 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5710 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5711 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5712 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5713 | PyObject *result = NULL; |
| 5714 | |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5715 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5716 | if (result == NULL) { |
| 5717 | return NULL; |
| 5718 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5719 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5720 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5721 | Py_DECREF(result); |
| 5722 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5723 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5724 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5725 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5726 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5727 | pCrlCtx->cbCrlEncoded); |
| 5728 | if (!crl) { |
| 5729 | Py_CLEAR(result); |
| 5730 | break; |
| 5731 | } |
| 5732 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5733 | Py_CLEAR(result); |
| 5734 | break; |
| 5735 | } |
| 5736 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5737 | Py_CLEAR(result); |
| 5738 | break; |
| 5739 | } |
| 5740 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5741 | crl = NULL; |
| 5742 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5743 | enc = NULL; |
| 5744 | |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5745 | if (PySet_Add(result, tup) == -1) { |
| 5746 | Py_CLEAR(result); |
| 5747 | Py_CLEAR(tup); |
| 5748 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5749 | } |
| 5750 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5751 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5752 | if (pCrlCtx) { |
| 5753 | /* loop ended with an error, need to clean up context manually */ |
| 5754 | CertFreeCRLContext(pCrlCtx); |
| 5755 | } |
| 5756 | |
| 5757 | /* In error cases cert, enc and tup may not be NULL */ |
| 5758 | Py_XDECREF(crl); |
| 5759 | Py_XDECREF(enc); |
| 5760 | Py_XDECREF(tup); |
| 5761 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5762 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5763 | associated with the store, in this case our collection store and the |
| 5764 | associated system stores. */ |
| 5765 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5766 | /* This error case might shadow another exception.*/ |
| 5767 | Py_XDECREF(result); |
| 5768 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5769 | } |
Steve Dower | fdd17ab | 2019-09-10 02:02:04 -0700 | [diff] [blame^] | 5770 | /* convert set to list */ |
| 5771 | if (result == NULL) { |
| 5772 | return NULL; |
| 5773 | } else { |
| 5774 | PyObject *lst = PySequence_List(result); |
| 5775 | Py_DECREF(result); |
| 5776 | return lst; |
| 5777 | } |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5778 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5779 | |
| 5780 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5781 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5782 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5783 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5784 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5785 | _SSL_RAND_ADD_METHODDEF |
| 5786 | _SSL_RAND_BYTES_METHODDEF |
| 5787 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5788 | _SSL_RAND_EGD_METHODDEF |
| 5789 | _SSL_RAND_STATUS_METHODDEF |
| 5790 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5791 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5792 | _SSL_ENUM_CRLS_METHODDEF |
| 5793 | _SSL_TXT2OBJ_METHODDEF |
| 5794 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5795 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5796 | }; |
| 5797 | |
| 5798 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5799 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5800 | |
| 5801 | /* an implementation of OpenSSL threading operations in terms |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5802 | * of the Python C thread library |
| 5803 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5804 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5805 | |
| 5806 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5807 | |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5808 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5809 | /* use new CRYPTO_THREADID API. */ |
| 5810 | static void |
| 5811 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5812 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5813 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5814 | } |
| 5815 | #else |
| 5816 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5817 | static unsigned long |
| 5818 | _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5819 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5820 | } |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5821 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5822 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5823 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5824 | (int mode, int n, const char *file, int line) { |
| 5825 | /* this function is needed to perform locking on shared data |
| 5826 | structures. (Note that OpenSSL uses a number of global data |
| 5827 | structures that will be implicitly shared whenever multiple |
| 5828 | threads use OpenSSL.) Multi-threaded applications will |
| 5829 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5830 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5831 | locking_function() must be able to handle up to |
| 5832 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5833 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5834 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5835 | file and line are the file number of the function setting the |
| 5836 | lock. They can be useful for debugging. |
| 5837 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5838 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5839 | if ((_ssl_locks == NULL) || |
| 5840 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5841 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5842 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5843 | if (mode & CRYPTO_LOCK) { |
| 5844 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5845 | } else { |
| 5846 | PyThread_release_lock(_ssl_locks[n]); |
| 5847 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | static int _setup_ssl_threads(void) { |
| 5851 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5852 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5853 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5854 | if (_ssl_locks == NULL) { |
| 5855 | _ssl_locks_count = CRYPTO_num_locks(); |
Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5856 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5857 | sizeof(PyThread_type_lock)); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5858 | if (_ssl_locks == NULL) { |
| 5859 | PyErr_NoMemory(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5860 | return 0; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5861 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5862 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5863 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5864 | if (_ssl_locks[i] == NULL) { |
| 5865 | unsigned int j; |
| 5866 | for (j = 0; j < i; j++) { |
| 5867 | PyThread_free_lock(_ssl_locks[j]); |
| 5868 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5869 | PyMem_Free(_ssl_locks); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5870 | return 0; |
| 5871 | } |
| 5872 | } |
| 5873 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5874 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5875 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5876 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5877 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5878 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5879 | } |
| 5880 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5881 | } |
| 5882 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5883 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5885 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5886 | "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] | 5887 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5888 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5889 | |
| 5890 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5891 | PyModuleDef_HEAD_INIT, |
| 5892 | "_ssl", |
| 5893 | module_doc, |
| 5894 | -1, |
| 5895 | PySSL_methods, |
| 5896 | NULL, |
| 5897 | NULL, |
| 5898 | NULL, |
| 5899 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5900 | }; |
| 5901 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5902 | |
| 5903 | static void |
| 5904 | parse_openssl_version(unsigned long libver, |
| 5905 | unsigned int *major, unsigned int *minor, |
| 5906 | unsigned int *fix, unsigned int *patch, |
| 5907 | unsigned int *status) |
| 5908 | { |
| 5909 | *status = libver & 0xF; |
| 5910 | libver >>= 4; |
| 5911 | *patch = libver & 0xFF; |
| 5912 | libver >>= 8; |
| 5913 | *fix = libver & 0xFF; |
| 5914 | libver >>= 8; |
| 5915 | *minor = libver & 0xFF; |
| 5916 | libver >>= 8; |
| 5917 | *major = libver & 0xFF; |
| 5918 | } |
| 5919 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5920 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5921 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5922 | { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5923 | PyObject *m, *d, *r, *bases; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5924 | unsigned long libver; |
| 5925 | unsigned int major, minor, fix, patch, status; |
| 5926 | PySocketModule_APIObject *socket_api; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5927 | struct py_ssl_error_code *errcode; |
| 5928 | struct py_ssl_library_code *libcode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5929 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5930 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 5931 | return NULL; |
| 5932 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5933 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5934 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) |
| 5935 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5936 | if (PyType_Ready(&PySSLSession_Type) < 0) |
| 5937 | return NULL; |
| 5938 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5939 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5940 | m = PyModule_Create(&_sslmodule); |
| 5941 | if (m == NULL) |
| 5942 | return NULL; |
| 5943 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5944 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5945 | /* Load _socket module and its C API */ |
| 5946 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 5947 | if (!socket_api) |
| 5948 | return NULL; |
| 5949 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5950 | |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5951 | #ifndef OPENSSL_VERSION_1_1 |
| 5952 | /* Load all algorithms and initialize cpuid */ |
| 5953 | OPENSSL_add_all_algorithms_noconf(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5954 | /* Init OpenSSL */ |
| 5955 | SSL_load_error_strings(); |
| 5956 | SSL_library_init(); |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5957 | #endif |
| 5958 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5959 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5960 | /* note that this will start threading if not already started */ |
| 5961 | if (!_setup_ssl_threads()) { |
| 5962 | return NULL; |
| 5963 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5964 | #elif OPENSSL_VERSION_1_1 && defined(OPENSSL_THREADS) |
| 5965 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 5966 | _ssl_locks_count++; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5967 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5968 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5969 | /* Add symbols to module dict */ |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5970 | sslerror_type_slots[0].pfunc = PyExc_OSError; |
| 5971 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5972 | if (PySSLErrorObject == NULL) |
| 5973 | return NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5974 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5975 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 5976 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); |
| 5977 | if (bases == NULL) |
| 5978 | return NULL; |
| 5979 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( |
| 5980 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, |
| 5981 | bases, NULL); |
| 5982 | Py_DECREF(bases); |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5983 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 5984 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 5985 | PySSLErrorObject, NULL); |
| 5986 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 5987 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 5988 | PySSLErrorObject, NULL); |
| 5989 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 5990 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 5991 | PySSLErrorObject, NULL); |
| 5992 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 5993 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 5994 | PySSLErrorObject, NULL); |
| 5995 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 5996 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 5997 | PySSLErrorObject, NULL); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5998 | if (PySSLCertVerificationErrorObject == NULL |
| 5999 | || PySSLZeroReturnErrorObject == NULL |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6000 | || PySSLWantReadErrorObject == NULL |
| 6001 | || PySSLWantWriteErrorObject == NULL |
| 6002 | || PySSLSyscallErrorObject == NULL |
| 6003 | || PySSLEOFErrorObject == NULL) |
| 6004 | return NULL; |
| 6005 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 6006 | || PyDict_SetItemString(d, "SSLCertVerificationError", |
| 6007 | PySSLCertVerificationErrorObject) != 0 |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 6008 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 6009 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 6010 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 6011 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 6012 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6013 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 6014 | if (PyDict_SetItemString(d, "_SSLContext", |
| 6015 | (PyObject *)&PySSLContext_Type) != 0) |
| 6016 | return NULL; |
| 6017 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 6018 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6019 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 6020 | if (PyDict_SetItemString(d, "MemoryBIO", |
| 6021 | (PyObject *)&PySSLMemoryBIO_Type) != 0) |
| 6022 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6023 | if (PyDict_SetItemString(d, "SSLSession", |
| 6024 | (PyObject *)&PySSLSession_Type) != 0) |
| 6025 | return NULL; |
| 6026 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 6027 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 6028 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 6029 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6030 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 6031 | PY_SSL_ERROR_ZERO_RETURN); |
| 6032 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 6033 | PY_SSL_ERROR_WANT_READ); |
| 6034 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 6035 | PY_SSL_ERROR_WANT_WRITE); |
| 6036 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 6037 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 6038 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 6039 | PY_SSL_ERROR_SYSCALL); |
| 6040 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 6041 | PY_SSL_ERROR_SSL); |
| 6042 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 6043 | PY_SSL_ERROR_WANT_CONNECT); |
| 6044 | /* non ssl.h errorcodes */ |
| 6045 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 6046 | PY_SSL_ERROR_EOF); |
| 6047 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 6048 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 6049 | /* cert requirements */ |
| 6050 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 6051 | PY_SSL_CERT_NONE); |
| 6052 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 6053 | PY_SSL_CERT_OPTIONAL); |
| 6054 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 6055 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 6056 | /* CRL verification for verification_flags */ |
| 6057 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 6058 | 0); |
| 6059 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 6060 | X509_V_FLAG_CRL_CHECK); |
| 6061 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 6062 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6063 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 6064 | X509_V_FLAG_X509_STRICT); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 6065 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 6066 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 6067 | X509_V_FLAG_TRUSTED_FIRST); |
| 6068 | #endif |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 6069 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6070 | /* Alert Descriptions from ssl.h */ |
| 6071 | /* note RESERVED constants no longer intended for use have been removed */ |
| 6072 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 6073 | |
| 6074 | #define ADD_AD_CONSTANT(s) \ |
| 6075 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 6076 | SSL_AD_##s) |
| 6077 | |
| 6078 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 6079 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 6080 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 6081 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 6082 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 6083 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 6084 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 6085 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 6086 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 6087 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 6088 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 6089 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 6090 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 6091 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 6092 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 6093 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 6094 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 6095 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 6096 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 6097 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 6098 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6099 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 6100 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 6101 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 6102 | #endif |
| 6103 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 6104 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 6105 | #endif |
| 6106 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 6107 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 6108 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 6109 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 6110 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 6111 | #endif |
| 6112 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 6113 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 6114 | #endif |
| 6115 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 6116 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 6117 | #endif |
| 6118 | |
| 6119 | #undef ADD_AD_CONSTANT |
| 6120 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6121 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6122 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6123 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 6124 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 6125 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6126 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6127 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 6128 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 6129 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6130 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 6131 | PY_SSL_VERSION_TLS); |
| 6132 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 6133 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 6134 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 6135 | PY_SSL_VERSION_TLS_CLIENT); |
| 6136 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 6137 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6138 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 6139 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6140 | #if HAVE_TLSv1_2 |
| 6141 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 6142 | PY_SSL_VERSION_TLS1_1); |
| 6143 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 6144 | PY_SSL_VERSION_TLS1_2); |
| 6145 | #endif |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6146 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6147 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 6148 | PyModule_AddIntConstant(m, "OP_ALL", |
| 6149 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6150 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 6151 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 6152 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 6153 | #if HAVE_TLSv1_2 |
| 6154 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 6155 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
| 6156 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6157 | #ifdef SSL_OP_NO_TLSv1_3 |
| 6158 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 6159 | #else |
| 6160 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 6161 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 6162 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 6163 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 6164 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 6165 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6166 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 6167 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 6168 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 6169 | #ifdef SSL_OP_NO_COMPRESSION |
| 6170 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 6171 | SSL_OP_NO_COMPRESSION); |
| 6172 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 6173 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 6174 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 6175 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 6176 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 6177 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 6178 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 6179 | SSL_OP_NO_RENEGOTIATION); |
| 6180 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 6181 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 6182 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 6183 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 6184 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 6185 | #endif |
| 6186 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 6187 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 6188 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 6189 | #endif |
| 6190 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 6191 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 6192 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 6193 | #endif |
| 6194 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 6195 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 6196 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 6197 | #endif |
| 6198 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 6199 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 6200 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 6201 | #endif |
| 6202 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 6203 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 6204 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 6205 | #endif |
| 6206 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6207 | /* protocol versions */ |
| 6208 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 6209 | PY_PROTO_MINIMUM_SUPPORTED); |
| 6210 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 6211 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 6212 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 6213 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 6214 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 6215 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 6216 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 6217 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6218 | #define addbool(m, key, value) \ |
| 6219 | do { \ |
| 6220 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 6221 | Py_INCREF(bool_obj); \ |
| 6222 | PyModule_AddObject((m), (key), bool_obj); \ |
| 6223 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6224 | |
| 6225 | #if HAVE_SNI |
| 6226 | addbool(m, "HAS_SNI", 1); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6227 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6228 | addbool(m, "HAS_SNI", 0); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6229 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6230 | |
| 6231 | addbool(m, "HAS_TLS_UNIQUE", 1); |
| 6232 | |
| 6233 | #ifndef OPENSSL_NO_ECDH |
| 6234 | addbool(m, "HAS_ECDH", 1); |
| 6235 | #else |
| 6236 | addbool(m, "HAS_ECDH", 0); |
| 6237 | #endif |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6238 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6239 | #if HAVE_NPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6240 | addbool(m, "HAS_NPN", 1); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6241 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6242 | addbool(m, "HAS_NPN", 0); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6243 | #endif |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6244 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6245 | #if HAVE_ALPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6246 | addbool(m, "HAS_ALPN", 1); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6247 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6248 | addbool(m, "HAS_ALPN", 0); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6249 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6250 | |
| 6251 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 6252 | addbool(m, "HAS_SSLv2", 1); |
| 6253 | #else |
| 6254 | addbool(m, "HAS_SSLv2", 0); |
| 6255 | #endif |
| 6256 | |
| 6257 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 6258 | addbool(m, "HAS_SSLv3", 1); |
| 6259 | #else |
| 6260 | addbool(m, "HAS_SSLv3", 0); |
| 6261 | #endif |
| 6262 | |
| 6263 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 6264 | addbool(m, "HAS_TLSv1", 1); |
| 6265 | #else |
| 6266 | addbool(m, "HAS_TLSv1", 0); |
| 6267 | #endif |
| 6268 | |
| 6269 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 6270 | addbool(m, "HAS_TLSv1_1", 1); |
| 6271 | #else |
| 6272 | addbool(m, "HAS_TLSv1_1", 0); |
| 6273 | #endif |
| 6274 | |
| 6275 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 6276 | addbool(m, "HAS_TLSv1_2", 1); |
| 6277 | #else |
| 6278 | addbool(m, "HAS_TLSv1_2", 0); |
| 6279 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6280 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6281 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6282 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6283 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6284 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6285 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6286 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6287 | /* Mappings for error codes */ |
| 6288 | err_codes_to_names = PyDict_New(); |
| 6289 | err_names_to_codes = PyDict_New(); |
| 6290 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 6291 | return NULL; |
| 6292 | errcode = error_codes; |
| 6293 | while (errcode->mnemonic != NULL) { |
| 6294 | PyObject *mnemo, *key; |
| 6295 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 6296 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 6297 | if (mnemo == NULL || key == NULL) |
| 6298 | return NULL; |
| 6299 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 6300 | return NULL; |
| 6301 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 6302 | return NULL; |
| 6303 | Py_DECREF(key); |
| 6304 | Py_DECREF(mnemo); |
| 6305 | errcode++; |
| 6306 | } |
| 6307 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 6308 | return NULL; |
| 6309 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 6310 | return NULL; |
| 6311 | |
| 6312 | lib_codes_to_names = PyDict_New(); |
| 6313 | if (lib_codes_to_names == NULL) |
| 6314 | return NULL; |
| 6315 | libcode = library_codes; |
| 6316 | while (libcode->library != NULL) { |
| 6317 | PyObject *mnemo, *key; |
| 6318 | key = PyLong_FromLong(libcode->code); |
| 6319 | mnemo = PyUnicode_FromString(libcode->library); |
| 6320 | if (key == NULL || mnemo == NULL) |
| 6321 | return NULL; |
| 6322 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 6323 | return NULL; |
| 6324 | Py_DECREF(key); |
| 6325 | Py_DECREF(mnemo); |
| 6326 | libcode++; |
| 6327 | } |
| 6328 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 6329 | return NULL; |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 6330 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6331 | /* OpenSSL version */ |
| 6332 | /* SSLeay() gives us the version of the library linked against, |
| 6333 | which could be different from the headers version. |
| 6334 | */ |
| 6335 | libver = SSLeay(); |
| 6336 | r = PyLong_FromUnsignedLong(libver); |
| 6337 | if (r == NULL) |
| 6338 | return NULL; |
| 6339 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6340 | return NULL; |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6341 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6342 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6343 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6344 | return NULL; |
| 6345 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 6346 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6347 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6348 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6349 | libver = OPENSSL_VERSION_NUMBER; |
| 6350 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6351 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6352 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6353 | return NULL; |
| 6354 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6355 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6356 | } |