Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 17 | #define PY_SSIZE_T_CLEAN |
| 18 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 21 | #include "pythread.h" |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 22 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 23 | /* Redefined below for Windows debug builds after important #includes */ |
| 24 | #define _PySSL_FIX_ERRNO |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 25 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 26 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 27 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 28 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 29 | do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } _PySSL_FIX_ERRNO; } while (0) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 30 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 31 | PyThreadState *_save = NULL; \ |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 32 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 33 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 34 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 35 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 37 | /* Include symbols from _socket module */ |
| 38 | #include "socketmodule.h" |
| 39 | |
| 40 | static PySocketModule_APIObject PySocketModule; |
| 41 | |
| 42 | #if defined(HAVE_POLL_H) |
| 43 | #include <poll.h> |
| 44 | #elif defined(HAVE_SYS_POLL_H) |
| 45 | #include <sys/poll.h> |
| 46 | #endif |
| 47 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 48 | /* Don't warn about deprecated functions */ |
| 49 | #ifdef __GNUC__ |
| 50 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 51 | #endif |
| 52 | #ifdef __clang__ |
| 53 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 54 | #endif |
| 55 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 56 | /* Include OpenSSL header files */ |
| 57 | #include "openssl/rsa.h" |
| 58 | #include "openssl/crypto.h" |
| 59 | #include "openssl/x509.h" |
| 60 | #include "openssl/x509v3.h" |
| 61 | #include "openssl/pem.h" |
| 62 | #include "openssl/ssl.h" |
| 63 | #include "openssl/err.h" |
| 64 | #include "openssl/rand.h" |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 65 | #include "openssl/bio.h" |
Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 66 | #include "openssl/dh.h" |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 67 | |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 68 | #ifndef HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 69 | # ifdef LIBRESSL_VERSION_NUMBER |
| 70 | # error "LibreSSL is missing X509_VERIFY_PARAM_set1_host(), see https://github.com/libressl-portable/portable/issues/381" |
| 71 | # elif OPENSSL_VERSION_NUMBER > 0x1000200fL |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 72 | # define HAVE_X509_VERIFY_PARAM_SET1_HOST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 73 | # else |
| 74 | # error "libssl is too old and does not support X509_VERIFY_PARAM_set1_host()" |
Christian Heimes | ff5be6e | 2018-01-20 13:19:21 +0100 | [diff] [blame] | 75 | # endif |
| 76 | #endif |
| 77 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 78 | /* SSL error object */ |
| 79 | static PyObject *PySSLErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 80 | static PyObject *PySSLCertVerificationErrorObject; |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 81 | static PyObject *PySSLZeroReturnErrorObject; |
| 82 | static PyObject *PySSLWantReadErrorObject; |
| 83 | static PyObject *PySSLWantWriteErrorObject; |
| 84 | static PyObject *PySSLSyscallErrorObject; |
| 85 | static PyObject *PySSLEOFErrorObject; |
| 86 | |
| 87 | /* Error mappings */ |
| 88 | static PyObject *err_codes_to_names; |
| 89 | static PyObject *err_names_to_codes; |
| 90 | static PyObject *lib_codes_to_names; |
| 91 | |
| 92 | struct py_ssl_error_code { |
| 93 | const char *mnemonic; |
| 94 | int library, reason; |
| 95 | }; |
| 96 | struct py_ssl_library_code { |
| 97 | const char *library; |
| 98 | int code; |
| 99 | }; |
| 100 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 101 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 102 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 103 | * However, because it uses a different CRT, we need to transfer the |
| 104 | * value of errno from OpenSSL into our debug CRT. |
| 105 | * |
| 106 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 107 | * alternative is to do both debug and release builds of OpenSSL, which |
| 108 | * requires much uglier code to transform their automatically generated |
| 109 | * makefile. This is the lesser of all the evils. |
| 110 | */ |
| 111 | |
| 112 | static void _PySSLFixErrno(void) { |
| 113 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 114 | if (!ucrtbase) { |
| 115 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 116 | * have a catastrophic failure, but this function is not the |
| 117 | * place to raise it. */ |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | typedef int *(__stdcall *errno_func)(void); |
| 122 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 123 | if (ssl_errno) { |
| 124 | errno = *ssl_errno(); |
| 125 | *ssl_errno() = 0; |
| 126 | } else { |
| 127 | errno = ENOTRECOVERABLE; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #undef _PySSL_FIX_ERRNO |
| 132 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 133 | #endif |
| 134 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 135 | /* Include generated data (error codes) */ |
| 136 | #include "_ssl_data.h" |
| 137 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 138 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 139 | # define OPENSSL_VERSION_1_1 1 |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 140 | # define PY_OPENSSL_1_1_API 1 |
| 141 | #endif |
| 142 | |
| 143 | /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ |
| 144 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL |
| 145 | # define PY_OPENSSL_1_1_API 1 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 146 | #endif |
| 147 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 148 | /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 |
| 149 | http://www.openssl.org/news/changelog.html |
| 150 | */ |
| 151 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 152 | # define HAVE_TLSv1_2 1 |
| 153 | #else |
| 154 | # define HAVE_TLSv1_2 0 |
| 155 | #endif |
| 156 | |
Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 157 | /* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 158 | * This includes the SSL_set_SSL_CTX() function. |
| 159 | */ |
| 160 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 161 | # define HAVE_SNI 1 |
| 162 | #else |
| 163 | # define HAVE_SNI 0 |
| 164 | #endif |
| 165 | |
Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 166 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 167 | # define HAVE_ALPN 1 |
| 168 | #else |
| 169 | # define HAVE_ALPN 0 |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 170 | #endif |
| 171 | |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 172 | /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped |
| 173 | * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility |
| 174 | * reasons. The check for TLSEXT_TYPE_next_proto_neg works with |
| 175 | * OpenSSL 1.0.1+ and LibreSSL. |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 176 | * OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg. |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 177 | */ |
| 178 | #ifdef OPENSSL_NO_NEXTPROTONEG |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 179 | # define HAVE_NPN 0 |
| 180 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 181 | # define HAVE_NPN 0 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 182 | #elif defined(TLSEXT_TYPE_next_proto_neg) |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 183 | # define HAVE_NPN 1 |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 184 | #else |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 185 | # define HAVE_NPN 0 |
| 186 | #endif |
Christian Heimes | 6cdb795 | 2018-02-24 22:12:40 +0100 | [diff] [blame] | 187 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 188 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 189 | #define INVALID_SOCKET (-1) |
| 190 | #endif |
| 191 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 192 | /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ |
| 193 | #ifndef OPENSSL_VERSION_1_1 |
| 194 | #define HAVE_OPENSSL_CRYPTO_LOCK |
| 195 | #endif |
| 196 | |
| 197 | #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 198 | #define OPENSSL_NO_SSL2 |
| 199 | #endif |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 200 | |
| 201 | #ifndef PY_OPENSSL_1_1_API |
| 202 | /* OpenSSL 1.1 API shims for OpenSSL < 1.1.0 and LibreSSL < 2.7.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 203 | |
| 204 | #define TLS_method SSLv23_method |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 205 | #define TLS_client_method SSLv23_client_method |
| 206 | #define TLS_server_method SSLv23_server_method |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 207 | |
| 208 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 209 | { |
| 210 | return ne->set; |
| 211 | } |
| 212 | |
| 213 | #ifndef OPENSSL_NO_COMP |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 214 | /* LCOV_EXCL_START */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 215 | static int COMP_get_type(const COMP_METHOD *meth) |
| 216 | { |
| 217 | return meth->type; |
| 218 | } |
Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 219 | /* LCOV_EXCL_STOP */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 220 | #endif |
| 221 | |
| 222 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 223 | { |
| 224 | return ctx->default_passwd_callback; |
| 225 | } |
| 226 | |
| 227 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 228 | { |
| 229 | return ctx->default_passwd_callback_userdata; |
| 230 | } |
| 231 | |
| 232 | static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 233 | { |
| 234 | return x->type; |
| 235 | } |
| 236 | |
| 237 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 238 | { |
| 239 | return x->data.x509; |
| 240 | } |
| 241 | |
| 242 | static int BIO_up_ref(BIO *b) |
| 243 | { |
| 244 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 249 | return store->objs; |
| 250 | } |
| 251 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 252 | static int |
| 253 | SSL_SESSION_has_ticket(const SSL_SESSION *s) |
| 254 | { |
| 255 | return (s->tlsext_ticklen > 0) ? 1 : 0; |
| 256 | } |
| 257 | |
| 258 | static unsigned long |
| 259 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
| 260 | { |
| 261 | return s->tlsext_tick_lifetime_hint; |
| 262 | } |
| 263 | |
Christian Heimes | 4ca0739 | 2018-03-24 15:41:37 +0100 | [diff] [blame] | 264 | #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 265 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 266 | /* Default cipher suites */ |
| 267 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 268 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 269 | #endif |
| 270 | |
| 271 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 272 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 273 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 274 | #endif |
| 275 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 276 | /* Python custom selection of sensible cipher suites |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 277 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. |
| 278 | * !aNULL:!eNULL: really no NULL ciphers |
| 279 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. |
| 280 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
| 281 | * !SRP:!PSK: no secure remote password or pre-shared key authentication |
| 282 | */ |
| 283 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" |
| 284 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 285 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 286 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 287 | #else |
| 288 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 289 | #endif |
| 290 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 291 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 292 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 293 | /* these mirror ssl.h */ |
| 294 | PY_SSL_ERROR_NONE, |
| 295 | PY_SSL_ERROR_SSL, |
| 296 | PY_SSL_ERROR_WANT_READ, |
| 297 | PY_SSL_ERROR_WANT_WRITE, |
| 298 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 299 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 300 | PY_SSL_ERROR_ZERO_RETURN, |
| 301 | PY_SSL_ERROR_WANT_CONNECT, |
| 302 | /* start of non ssl.h errorcodes */ |
| 303 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 304 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 305 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 306 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 307 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 308 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 309 | PY_SSL_CLIENT, |
| 310 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 314 | PY_SSL_CERT_NONE, |
| 315 | PY_SSL_CERT_OPTIONAL, |
| 316 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 320 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 321 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 322 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 323 | #if HAVE_TLSv1_2 |
| 324 | PY_SSL_VERSION_TLS1, |
| 325 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 326 | PY_SSL_VERSION_TLS1_2, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 327 | #else |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 328 | PY_SSL_VERSION_TLS1, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 329 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 330 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 331 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 332 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 333 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 334 | enum py_proto_version { |
| 335 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 336 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 337 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 338 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 339 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 340 | #ifdef TLS1_3_VERSION |
| 341 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 342 | #else |
| 343 | PY_PROTO_TLSv1_3 = 0x304, |
| 344 | #endif |
| 345 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 346 | |
| 347 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 348 | * available version, and the other way around. We have to figure out the |
| 349 | * minimum and maximum available version on our own and hope for the best. |
| 350 | */ |
| 351 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 352 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 353 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 354 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 355 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 356 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 357 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 358 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 359 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 360 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 361 | #else |
| 362 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 363 | #endif |
| 364 | |
| 365 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 366 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 367 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 368 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 369 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 370 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 371 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 372 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 373 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 374 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 375 | #else |
| 376 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 377 | #endif |
| 378 | }; |
| 379 | |
| 380 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 381 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 382 | /* 0 means no, greater than 0 means yes */ |
| 383 | |
| 384 | static unsigned int _ssl_locks_count = 0; |
| 385 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 386 | /* SSL socket object */ |
| 387 | |
| 388 | #define X509_NAME_MAXLEN 256 |
| 389 | |
Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 390 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 391 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 392 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 393 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 394 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 395 | #else |
| 396 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 397 | #endif |
| 398 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 399 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 400 | * older SSL, but let's be safe */ |
| 401 | #define PySSL_CB_MAXLEN 128 |
| 402 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 403 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 404 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 405 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 406 | SSL_CTX *ctx; |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 407 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 408 | unsigned char *npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 409 | int npn_protocols_len; |
| 410 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 411 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 412 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 413 | unsigned int alpn_protocols_len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 414 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 415 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 416 | PyObject *set_sni_cb; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 417 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 418 | int check_hostname; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 419 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 420 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 421 | */ |
| 422 | unsigned int hostflags; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 423 | int protocol; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 424 | #ifdef TLS1_3_VERSION |
| 425 | int post_handshake_auth; |
| 426 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 427 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 429 | typedef struct { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 430 | int ssl; /* last seen error from SSL */ |
| 431 | int c; /* last seen error from libc */ |
| 432 | #ifdef MS_WINDOWS |
| 433 | int ws; /* last seen error from winsock */ |
| 434 | #endif |
| 435 | } _PySSLError; |
| 436 | |
| 437 | typedef struct { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 438 | PyObject_HEAD |
| 439 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 440 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 441 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 442 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 443 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 444 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 445 | PyObject *server_hostname; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 446 | _PySSLError err; /* last seen error from various sources */ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 447 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 449 | typedef struct { |
| 450 | PyObject_HEAD |
| 451 | BIO *bio; |
| 452 | int eof_written; |
| 453 | } PySSLMemoryBIO; |
| 454 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 455 | typedef struct { |
| 456 | PyObject_HEAD |
| 457 | SSL_SESSION *session; |
| 458 | PySSLContext *ctx; |
| 459 | } PySSLSession; |
| 460 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 461 | static PyTypeObject PySSLContext_Type; |
| 462 | static PyTypeObject PySSLSocket_Type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 463 | static PyTypeObject PySSLMemoryBIO_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 464 | static PyTypeObject PySSLSession_Type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 465 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 466 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 467 | { |
| 468 | _PySSLError err = { 0 }; |
| 469 | if (failed) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 470 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 471 | err.ws = WSAGetLastError(); |
| 472 | _PySSL_FIX_ERRNO; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 473 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 474 | err.c = errno; |
| 475 | err.ssl = SSL_get_error(ssl, retcode); |
| 476 | } |
| 477 | return err; |
| 478 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 479 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 480 | /*[clinic input] |
| 481 | module _ssl |
| 482 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" |
| 483 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" |
| 484 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 485 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 486 | [clinic start generated code]*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 487 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 488 | |
| 489 | #include "clinic/_ssl.c.h" |
| 490 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 491 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 492 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 493 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 494 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 495 | #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 496 | #define PySSLMemoryBIO_Check(v) (Py_TYPE(v) == &PySSLMemoryBIO_Type) |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 497 | #define PySSLSession_Check(v) (Py_TYPE(v) == &PySSLSession_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 498 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 499 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 500 | SOCKET_IS_NONBLOCKING, |
| 501 | SOCKET_IS_BLOCKING, |
| 502 | SOCKET_HAS_TIMED_OUT, |
| 503 | SOCKET_HAS_BEEN_CLOSED, |
| 504 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 505 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 506 | } timeout_state; |
| 507 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 508 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 509 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 510 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 512 | /* Get the socket from a PySSLSocket, if it has one */ |
| 513 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 514 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 515 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 516 | /* If sock is NULL, use a timeout of 0 second */ |
| 517 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 518 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 519 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 520 | /* |
| 521 | * SSL errors. |
| 522 | */ |
| 523 | |
| 524 | PyDoc_STRVAR(SSLError_doc, |
| 525 | "An error occurred in the SSL implementation."); |
| 526 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 527 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 528 | "A certificate could not be verified."); |
| 529 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 530 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 531 | "SSL/TLS session closed cleanly."); |
| 532 | |
| 533 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 534 | "Non-blocking SSL socket needs to read more data\n" |
| 535 | "before the requested operation can be completed."); |
| 536 | |
| 537 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 538 | "Non-blocking SSL socket needs to write more data\n" |
| 539 | "before the requested operation can be completed."); |
| 540 | |
| 541 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 542 | "System error when attempting SSL operation."); |
| 543 | |
| 544 | PyDoc_STRVAR(SSLEOFError_doc, |
| 545 | "SSL/TLS connection terminated abruptly."); |
| 546 | |
| 547 | static PyObject * |
| 548 | SSLError_str(PyOSErrorObject *self) |
| 549 | { |
| 550 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 551 | Py_INCREF(self->strerror); |
| 552 | return self->strerror; |
| 553 | } |
| 554 | else |
| 555 | return PyObject_Str(self->args); |
| 556 | } |
| 557 | |
| 558 | static PyType_Slot sslerror_type_slots[] = { |
| 559 | {Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */ |
| 560 | {Py_tp_doc, SSLError_doc}, |
| 561 | {Py_tp_str, SSLError_str}, |
| 562 | {0, 0}, |
| 563 | }; |
| 564 | |
| 565 | static PyType_Spec sslerror_type_spec = { |
| 566 | "ssl.SSLError", |
| 567 | sizeof(PyOSErrorObject), |
| 568 | 0, |
| 569 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 570 | sslerror_type_slots |
| 571 | }; |
| 572 | |
| 573 | static void |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 574 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 575 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 576 | { |
| 577 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 578 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 579 | PyObject *init_value, *msg, *key; |
| 580 | _Py_IDENTIFIER(reason); |
| 581 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 582 | _Py_IDENTIFIER(verify_message); |
| 583 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 584 | |
| 585 | if (errcode != 0) { |
| 586 | int lib, reason; |
| 587 | |
| 588 | lib = ERR_GET_LIB(errcode); |
| 589 | reason = ERR_GET_REASON(errcode); |
| 590 | key = Py_BuildValue("ii", lib, reason); |
| 591 | if (key == NULL) |
| 592 | goto fail; |
| 593 | reason_obj = PyDict_GetItem(err_codes_to_names, key); |
| 594 | Py_DECREF(key); |
| 595 | if (reason_obj == NULL) { |
| 596 | /* XXX if reason < 100, it might reflect a library number (!!) */ |
| 597 | PyErr_Clear(); |
| 598 | } |
| 599 | key = PyLong_FromLong(lib); |
| 600 | if (key == NULL) |
| 601 | goto fail; |
| 602 | lib_obj = PyDict_GetItem(lib_codes_to_names, key); |
| 603 | Py_DECREF(key); |
| 604 | if (lib_obj == NULL) { |
| 605 | PyErr_Clear(); |
| 606 | } |
| 607 | if (errstr == NULL) |
| 608 | errstr = ERR_reason_error_string(errcode); |
| 609 | } |
| 610 | if (errstr == NULL) |
| 611 | errstr = "unknown error"; |
| 612 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 613 | /* verify code for cert validation error */ |
| 614 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 615 | const char *verify_str = NULL; |
| 616 | long verify_code; |
| 617 | |
| 618 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 619 | verify_code_obj = PyLong_FromLong(verify_code); |
| 620 | if (verify_code_obj == NULL) { |
| 621 | goto fail; |
| 622 | } |
| 623 | |
| 624 | switch (verify_code) { |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 625 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 626 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 627 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 628 | verify_obj = PyUnicode_FromFormat( |
| 629 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 630 | sslsock->server_hostname |
| 631 | ); |
| 632 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 633 | #endif |
| 634 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 635 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 636 | verify_obj = PyUnicode_FromFormat( |
| 637 | "IP address mismatch, certificate is not valid for '%S'.", |
| 638 | sslsock->server_hostname |
| 639 | ); |
| 640 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 641 | #endif |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 642 | default: |
| 643 | verify_str = X509_verify_cert_error_string(verify_code); |
| 644 | if (verify_str != NULL) { |
| 645 | verify_obj = PyUnicode_FromString(verify_str); |
| 646 | } else { |
| 647 | verify_obj = Py_None; |
| 648 | Py_INCREF(verify_obj); |
| 649 | } |
| 650 | break; |
| 651 | } |
| 652 | if (verify_obj == NULL) { |
| 653 | goto fail; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (verify_obj && reason_obj && lib_obj) |
| 658 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 659 | lib_obj, reason_obj, errstr, verify_obj, |
| 660 | lineno); |
| 661 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 662 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 663 | lib_obj, reason_obj, errstr, lineno); |
| 664 | else if (lib_obj) |
| 665 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 666 | lib_obj, errstr, lineno); |
| 667 | else |
| 668 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 669 | if (msg == NULL) |
| 670 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 671 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 672 | init_value = Py_BuildValue("iN", ssl_errno, msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 673 | if (init_value == NULL) |
| 674 | goto fail; |
| 675 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 676 | err_value = PyObject_CallObject(type, init_value); |
| 677 | Py_DECREF(init_value); |
| 678 | if (err_value == NULL) |
| 679 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 680 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 681 | if (reason_obj == NULL) |
| 682 | reason_obj = Py_None; |
| 683 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 684 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 685 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 686 | if (lib_obj == NULL) |
| 687 | lib_obj = Py_None; |
| 688 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 689 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 690 | |
| 691 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 692 | /* Only set verify code / message for SSLCertVerificationError */ |
| 693 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 694 | verify_code_obj)) |
| 695 | goto fail; |
| 696 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 697 | goto fail; |
| 698 | } |
| 699 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 700 | PyErr_SetObject(type, err_value); |
| 701 | fail: |
| 702 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 703 | Py_XDECREF(verify_code_obj); |
| 704 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 705 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 706 | |
| 707 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 708 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 709 | { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 710 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 711 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 712 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 713 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 714 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 716 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 717 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 718 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 719 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 720 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 721 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 722 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 723 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 724 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 725 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 726 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 727 | break; |
| 728 | case SSL_ERROR_WANT_READ: |
| 729 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 730 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 731 | p = PY_SSL_ERROR_WANT_READ; |
| 732 | break; |
| 733 | case SSL_ERROR_WANT_WRITE: |
| 734 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 735 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 736 | errstr = "The operation did not complete (write)"; |
| 737 | break; |
| 738 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 739 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 740 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 741 | break; |
| 742 | case SSL_ERROR_WANT_CONNECT: |
| 743 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 744 | errstr = "The operation did not complete (connect)"; |
| 745 | break; |
| 746 | case SSL_ERROR_SYSCALL: |
| 747 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 748 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 749 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 750 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 751 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 752 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 753 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 754 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 755 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 756 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 757 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 758 | if (err.ws) { |
| 759 | return PyErr_SetFromWindowsErr(err.ws); |
| 760 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 761 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 762 | if (err.c) { |
| 763 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 764 | return PyErr_SetFromErrno(PyExc_OSError); |
| 765 | } |
| 766 | Py_INCREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 767 | s->errorhandler(); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 768 | Py_DECREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 769 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 770 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 771 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 772 | type = PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 773 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 774 | } |
| 775 | } else { |
| 776 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 777 | } |
| 778 | break; |
| 779 | } |
| 780 | case SSL_ERROR_SSL: |
| 781 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 782 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 783 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 784 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 785 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 786 | } |
| 787 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 788 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
| 789 | type = PySSLCertVerificationErrorObject; |
| 790 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 791 | break; |
| 792 | } |
| 793 | default: |
| 794 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 795 | errstr = "Invalid error code"; |
| 796 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 797 | } |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 798 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 799 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 800 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 803 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 804 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 806 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 807 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 808 | else |
| 809 | errcode = 0; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 810 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 811 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 812 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 815 | /* |
| 816 | * SSL objects |
| 817 | */ |
| 818 | |
| 819 | static int |
| 820 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 821 | { |
| 822 | int retval = -1; |
| 823 | ASN1_OCTET_STRING *ip; |
| 824 | PyObject *hostname; |
| 825 | size_t len; |
| 826 | |
| 827 | assert(server_hostname); |
| 828 | |
| 829 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 830 | * When name starts with a dot (e.g ".example.com"), it will be |
| 831 | * matched by a certificate valid for any sub-domain of name. |
| 832 | */ |
| 833 | len = strlen(server_hostname); |
| 834 | if (len == 0 || *server_hostname == '.') { |
| 835 | PyErr_SetString( |
| 836 | PyExc_ValueError, |
| 837 | "server_hostname cannot be an empty string or start with a " |
| 838 | "leading dot."); |
| 839 | return retval; |
| 840 | } |
| 841 | |
| 842 | /* inet_pton is not available on all platforms. */ |
| 843 | ip = a2i_IPADDRESS(server_hostname); |
| 844 | if (ip == NULL) { |
| 845 | ERR_clear_error(); |
| 846 | } |
| 847 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 848 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 849 | if (hostname == NULL) { |
| 850 | goto error; |
| 851 | } |
| 852 | self->server_hostname = hostname; |
| 853 | |
| 854 | /* Only send SNI extension for non-IP hostnames */ |
| 855 | if (ip == NULL) { |
| 856 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
| 857 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 858 | } |
| 859 | } |
| 860 | if (self->ctx->check_hostname) { |
| 861 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 862 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 863 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 864 | strlen(server_hostname))) { |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 865 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 866 | goto error; |
| 867 | } |
| 868 | } else { |
| 869 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip), |
| 870 | ASN1_STRING_length(ip))) { |
| 871 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 872 | goto error; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | retval = 0; |
| 877 | error: |
| 878 | if (ip != NULL) { |
| 879 | ASN1_OCTET_STRING_free(ip); |
| 880 | } |
| 881 | return retval; |
| 882 | } |
| 883 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 884 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 885 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 886 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 887 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 888 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 889 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 891 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 892 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 893 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 895 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 896 | if (self == NULL) |
| 897 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 899 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 900 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 901 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 902 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 903 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 904 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 905 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 906 | self->err = err; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 907 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 908 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 909 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 911 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 912 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 913 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 914 | if (self->ssl == NULL) { |
| 915 | Py_DECREF(self); |
| 916 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 917 | return NULL; |
| 918 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 919 | SSL_set_app_data(self->ssl, self); |
| 920 | if (sock) { |
| 921 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 922 | } else { |
| 923 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 924 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 925 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 926 | BIO_up_ref(inbio->bio); |
| 927 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 928 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 929 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 930 | SSL_set_mode(self->ssl, |
| 931 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 932 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 933 | if (server_hostname != NULL) { |
| 934 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 935 | Py_DECREF(self); |
| 936 | return NULL; |
| 937 | } |
| 938 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 939 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 940 | * to non-blocking mode (blocking is the default) |
| 941 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 942 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 943 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 944 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 945 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 947 | PySSL_BEGIN_ALLOW_THREADS |
| 948 | if (socket_type == PY_SSL_CLIENT) |
| 949 | SSL_set_connect_state(self->ssl); |
| 950 | else |
| 951 | SSL_set_accept_state(self->ssl); |
| 952 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 953 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 954 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 955 | if (sock != NULL) { |
| 956 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 957 | if (self->Socket == NULL) { |
| 958 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 959 | return NULL; |
| 960 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 961 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 962 | if (owner && owner != Py_None) { |
| 963 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 964 | Py_DECREF(self); |
| 965 | return NULL; |
| 966 | } |
| 967 | } |
| 968 | if (session && session != Py_None) { |
| 969 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 970 | Py_DECREF(self); |
| 971 | return NULL; |
| 972 | } |
| 973 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 974 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 977 | /* SSL object methods */ |
| 978 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 979 | /*[clinic input] |
| 980 | _ssl._SSLSocket.do_handshake |
| 981 | [clinic start generated code]*/ |
| 982 | |
| 983 | static PyObject * |
| 984 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 985 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 986 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 987 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 988 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 989 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 990 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 991 | _PyTime_t timeout, deadline = 0; |
| 992 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 993 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 994 | if (sock) { |
| 995 | if (((PyObject*)sock) == Py_None) { |
| 996 | _setSSLError("Underlying socket connection gone", |
| 997 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 998 | return NULL; |
| 999 | } |
| 1000 | Py_INCREF(sock); |
| 1001 | |
| 1002 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 1003 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1004 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1005 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1006 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1007 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1008 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 1009 | has_timeout = (timeout > 0); |
| 1010 | if (has_timeout) |
| 1011 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 1012 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1013 | /* Actually negotiate SSL connection */ |
| 1014 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1015 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1016 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1017 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1018 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1019 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1020 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1021 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1022 | if (PyErr_CheckSignals()) |
| 1023 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1024 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1025 | if (has_timeout) |
| 1026 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 1027 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1028 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1029 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1030 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1031 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1032 | } else { |
| 1033 | sockstate = SOCKET_OPERATION_OK; |
| 1034 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1035 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1036 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1038 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1039 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1040 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1041 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1042 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1043 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1044 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1045 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1046 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1047 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1048 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1049 | break; |
| 1050 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 1051 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 1052 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1053 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1054 | if (ret < 1) |
| 1055 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1056 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1057 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1058 | |
| 1059 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1060 | Py_XDECREF(sock); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1061 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1064 | static PyObject * |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1065 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 1066 | { |
| 1067 | char buf[X509_NAME_MAXLEN]; |
| 1068 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1069 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1070 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1071 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1072 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1073 | if (buflen < 0) { |
| 1074 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1075 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1076 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1077 | /* initial buffer is too small for oid + terminating null byte */ |
| 1078 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1079 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1080 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1081 | /* allocate len + 1 for terminating NULL byte */ |
| 1082 | namebuf = PyMem_Malloc(buflen + 1); |
| 1083 | if (namebuf == NULL) { |
| 1084 | PyErr_NoMemory(); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1088 | if (buflen < 0) { |
| 1089 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1090 | goto done; |
| 1091 | } |
| 1092 | } |
| 1093 | if (!buflen && no_name) { |
| 1094 | Py_INCREF(Py_None); |
| 1095 | name_obj = Py_None; |
| 1096 | } |
| 1097 | else { |
| 1098 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1099 | } |
| 1100 | |
| 1101 | done: |
| 1102 | if (buf != namebuf) { |
| 1103 | PyMem_Free(namebuf); |
| 1104 | } |
| 1105 | return name_obj; |
| 1106 | } |
| 1107 | |
| 1108 | static PyObject * |
| 1109 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 1110 | { |
| 1111 | Py_ssize_t buflen; |
| 1112 | unsigned char *valuebuf = NULL; |
| 1113 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1114 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1115 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1116 | if (buflen < 0) { |
| 1117 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1118 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1119 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1120 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1121 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1122 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1126 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1127 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1128 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1129 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1130 | PyObject *rdnt; |
| 1131 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1132 | int entry_count = X509_NAME_entry_count(xname); |
| 1133 | X509_NAME_ENTRY *entry; |
| 1134 | ASN1_OBJECT *name; |
| 1135 | ASN1_STRING *value; |
| 1136 | int index_counter; |
| 1137 | int rdn_level = -1; |
| 1138 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1140 | dn = PyList_New(0); |
| 1141 | if (dn == NULL) |
| 1142 | return NULL; |
| 1143 | /* now create another tuple to hold the top-level RDN */ |
| 1144 | rdn = PyList_New(0); |
| 1145 | if (rdn == NULL) |
| 1146 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1147 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1148 | for (index_counter = 0; |
| 1149 | index_counter < entry_count; |
| 1150 | index_counter++) |
| 1151 | { |
| 1152 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1153 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1154 | /* check to see if we've gotten to a new RDN */ |
| 1155 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1156 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1157 | /* yes, new RDN */ |
| 1158 | /* add old RDN to DN */ |
| 1159 | rdnt = PyList_AsTuple(rdn); |
| 1160 | Py_DECREF(rdn); |
| 1161 | if (rdnt == NULL) |
| 1162 | goto fail0; |
| 1163 | retcode = PyList_Append(dn, rdnt); |
| 1164 | Py_DECREF(rdnt); |
| 1165 | if (retcode < 0) |
| 1166 | goto fail0; |
| 1167 | /* create new RDN */ |
| 1168 | rdn = PyList_New(0); |
| 1169 | if (rdn == NULL) |
| 1170 | goto fail0; |
| 1171 | } |
| 1172 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1173 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1175 | /* now add this attribute to the current RDN */ |
| 1176 | name = X509_NAME_ENTRY_get_object(entry); |
| 1177 | value = X509_NAME_ENTRY_get_data(entry); |
| 1178 | attr = _create_tuple_for_attribute(name, value); |
| 1179 | /* |
| 1180 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1181 | entry->set, |
| 1182 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1183 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1184 | */ |
| 1185 | if (attr == NULL) |
| 1186 | goto fail1; |
| 1187 | retcode = PyList_Append(rdn, attr); |
| 1188 | Py_DECREF(attr); |
| 1189 | if (retcode < 0) |
| 1190 | goto fail1; |
| 1191 | } |
| 1192 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1193 | if (rdn != NULL) { |
| 1194 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1195 | rdnt = PyList_AsTuple(rdn); |
| 1196 | Py_DECREF(rdn); |
| 1197 | if (rdnt == NULL) |
| 1198 | goto fail0; |
| 1199 | retcode = PyList_Append(dn, rdnt); |
| 1200 | Py_DECREF(rdnt); |
| 1201 | if (retcode < 0) |
| 1202 | goto fail0; |
| 1203 | } |
| 1204 | else { |
| 1205 | Py_DECREF(rdn); |
| 1206 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1207 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1209 | /* convert list to tuple */ |
| 1210 | rdnt = PyList_AsTuple(dn); |
| 1211 | Py_DECREF(dn); |
| 1212 | if (rdnt == NULL) |
| 1213 | return NULL; |
| 1214 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1215 | |
| 1216 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1217 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1218 | |
| 1219 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1220 | Py_XDECREF(dn); |
| 1221 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static PyObject * |
| 1225 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1227 | /* this code follows the procedure outlined in |
| 1228 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1229 | function to extract the STACK_OF(GENERAL_NAME), |
| 1230 | then iterates through the stack to add the |
| 1231 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1232 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1233 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1234 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1235 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1236 | GENERAL_NAMES *names = NULL; |
| 1237 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1238 | BIO *biobuf = NULL; |
| 1239 | char buf[2048]; |
| 1240 | char *vptr; |
| 1241 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1243 | if (certificate == NULL) |
| 1244 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1245 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1246 | /* get a memory buffer */ |
| 1247 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1248 | if (biobuf == NULL) { |
| 1249 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1250 | return NULL; |
| 1251 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1252 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1253 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1254 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1255 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1256 | if (peer_alt_names == Py_None) { |
| 1257 | peer_alt_names = PyList_New(0); |
| 1258 | if (peer_alt_names == NULL) |
| 1259 | goto fail; |
| 1260 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1262 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1263 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1264 | int gntype; |
| 1265 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1266 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1267 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1268 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1269 | switch (gntype) { |
| 1270 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1271 | /* we special-case DirName as a tuple of |
| 1272 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1274 | t = PyTuple_New(2); |
| 1275 | if (t == NULL) { |
| 1276 | goto fail; |
| 1277 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1278 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1279 | v = PyUnicode_FromString("DirName"); |
| 1280 | if (v == NULL) { |
| 1281 | Py_DECREF(t); |
| 1282 | goto fail; |
| 1283 | } |
| 1284 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1285 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1286 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1287 | if (v == NULL) { |
| 1288 | Py_DECREF(t); |
| 1289 | goto fail; |
| 1290 | } |
| 1291 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1292 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1293 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1294 | case GEN_EMAIL: |
| 1295 | case GEN_DNS: |
| 1296 | case GEN_URI: |
| 1297 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1298 | correctly, CVE-2013-4238 */ |
| 1299 | t = PyTuple_New(2); |
| 1300 | if (t == NULL) |
| 1301 | goto fail; |
| 1302 | switch (gntype) { |
| 1303 | case GEN_EMAIL: |
| 1304 | v = PyUnicode_FromString("email"); |
| 1305 | as = name->d.rfc822Name; |
| 1306 | break; |
| 1307 | case GEN_DNS: |
| 1308 | v = PyUnicode_FromString("DNS"); |
| 1309 | as = name->d.dNSName; |
| 1310 | break; |
| 1311 | case GEN_URI: |
| 1312 | v = PyUnicode_FromString("URI"); |
| 1313 | as = name->d.uniformResourceIdentifier; |
| 1314 | break; |
| 1315 | } |
| 1316 | if (v == NULL) { |
| 1317 | Py_DECREF(t); |
| 1318 | goto fail; |
| 1319 | } |
| 1320 | PyTuple_SET_ITEM(t, 0, v); |
| 1321 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as), |
| 1322 | ASN1_STRING_length(as)); |
| 1323 | if (v == NULL) { |
| 1324 | Py_DECREF(t); |
| 1325 | goto fail; |
| 1326 | } |
| 1327 | PyTuple_SET_ITEM(t, 1, v); |
| 1328 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1329 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1330 | case GEN_RID: |
| 1331 | t = PyTuple_New(2); |
| 1332 | if (t == NULL) |
| 1333 | goto fail; |
| 1334 | |
| 1335 | v = PyUnicode_FromString("Registered ID"); |
| 1336 | if (v == NULL) { |
| 1337 | Py_DECREF(t); |
| 1338 | goto fail; |
| 1339 | } |
| 1340 | PyTuple_SET_ITEM(t, 0, v); |
| 1341 | |
| 1342 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1343 | if (len < 0) { |
| 1344 | Py_DECREF(t); |
| 1345 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1346 | goto fail; |
| 1347 | } else if (len >= (int)sizeof(buf)) { |
| 1348 | v = PyUnicode_FromString("<INVALID>"); |
| 1349 | } else { |
| 1350 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1351 | } |
| 1352 | if (v == NULL) { |
| 1353 | Py_DECREF(t); |
| 1354 | goto fail; |
| 1355 | } |
| 1356 | PyTuple_SET_ITEM(t, 1, v); |
| 1357 | break; |
| 1358 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1359 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1360 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1361 | switch (gntype) { |
| 1362 | /* check for new general name type */ |
| 1363 | case GEN_OTHERNAME: |
| 1364 | case GEN_X400: |
| 1365 | case GEN_EDIPARTY: |
| 1366 | case GEN_IPADD: |
| 1367 | case GEN_RID: |
| 1368 | break; |
| 1369 | default: |
| 1370 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1371 | "Unknown general name type %d", |
| 1372 | gntype) == -1) { |
| 1373 | goto fail; |
| 1374 | } |
| 1375 | break; |
| 1376 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1377 | (void) BIO_reset(biobuf); |
| 1378 | GENERAL_NAME_print(biobuf, name); |
| 1379 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1380 | if (len < 0) { |
| 1381 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1382 | goto fail; |
| 1383 | } |
| 1384 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1385 | if (vptr == NULL) { |
| 1386 | PyErr_Format(PyExc_ValueError, |
| 1387 | "Invalid value %.200s", |
| 1388 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1389 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1390 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1391 | t = PyTuple_New(2); |
| 1392 | if (t == NULL) |
| 1393 | goto fail; |
| 1394 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1395 | if (v == NULL) { |
| 1396 | Py_DECREF(t); |
| 1397 | goto fail; |
| 1398 | } |
| 1399 | PyTuple_SET_ITEM(t, 0, v); |
| 1400 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1401 | (len - (vptr - buf + 1))); |
| 1402 | if (v == NULL) { |
| 1403 | Py_DECREF(t); |
| 1404 | goto fail; |
| 1405 | } |
| 1406 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1407 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1408 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1409 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1410 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1412 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1413 | Py_DECREF(t); |
| 1414 | goto fail; |
| 1415 | } |
| 1416 | Py_DECREF(t); |
| 1417 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1418 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1419 | } |
| 1420 | BIO_free(biobuf); |
| 1421 | if (peer_alt_names != Py_None) { |
| 1422 | v = PyList_AsTuple(peer_alt_names); |
| 1423 | Py_DECREF(peer_alt_names); |
| 1424 | return v; |
| 1425 | } else { |
| 1426 | return peer_alt_names; |
| 1427 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1428 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1429 | |
| 1430 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1431 | if (biobuf != NULL) |
| 1432 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1434 | if (peer_alt_names != Py_None) { |
| 1435 | Py_XDECREF(peer_alt_names); |
| 1436 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1438 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1442 | _get_aia_uri(X509 *certificate, int nid) { |
| 1443 | PyObject *lst = NULL, *ostr = NULL; |
| 1444 | int i, result; |
| 1445 | AUTHORITY_INFO_ACCESS *info; |
| 1446 | |
| 1447 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1448 | if (info == NULL) |
| 1449 | return Py_None; |
| 1450 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1451 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1452 | return Py_None; |
| 1453 | } |
| 1454 | |
| 1455 | if ((lst = PyList_New(0)) == NULL) { |
| 1456 | goto fail; |
| 1457 | } |
| 1458 | |
| 1459 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1460 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1461 | ASN1_IA5STRING *uri; |
| 1462 | |
| 1463 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1464 | (ad->location->type != GEN_URI)) { |
| 1465 | continue; |
| 1466 | } |
| 1467 | uri = ad->location->d.uniformResourceIdentifier; |
| 1468 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1469 | uri->length); |
| 1470 | if (ostr == NULL) { |
| 1471 | goto fail; |
| 1472 | } |
| 1473 | result = PyList_Append(lst, ostr); |
| 1474 | Py_DECREF(ostr); |
| 1475 | if (result < 0) { |
| 1476 | goto fail; |
| 1477 | } |
| 1478 | } |
| 1479 | AUTHORITY_INFO_ACCESS_free(info); |
| 1480 | |
| 1481 | /* convert to tuple or None */ |
| 1482 | if (PyList_Size(lst) == 0) { |
| 1483 | Py_DECREF(lst); |
| 1484 | return Py_None; |
| 1485 | } else { |
| 1486 | PyObject *tup; |
| 1487 | tup = PyList_AsTuple(lst); |
| 1488 | Py_DECREF(lst); |
| 1489 | return tup; |
| 1490 | } |
| 1491 | |
| 1492 | fail: |
| 1493 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1494 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | static PyObject * |
| 1499 | _get_crl_dp(X509 *certificate) { |
| 1500 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1501 | int i, j; |
| 1502 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1503 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1504 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1505 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1506 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1507 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1508 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1509 | lst = PyList_New(0); |
| 1510 | if (lst == NULL) |
| 1511 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1512 | |
| 1513 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1514 | DIST_POINT *dp; |
| 1515 | STACK_OF(GENERAL_NAME) *gns; |
| 1516 | |
| 1517 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1518 | if (dp->distpoint == NULL) { |
| 1519 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1520 | continue; |
| 1521 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1522 | gns = dp->distpoint->name.fullname; |
| 1523 | |
| 1524 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1525 | GENERAL_NAME *gn; |
| 1526 | ASN1_IA5STRING *uri; |
| 1527 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1528 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1529 | |
| 1530 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1531 | if (gn->type != GEN_URI) { |
| 1532 | continue; |
| 1533 | } |
| 1534 | uri = gn->d.uniformResourceIdentifier; |
| 1535 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1536 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1537 | if (ouri == NULL) |
| 1538 | goto done; |
| 1539 | |
| 1540 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1541 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1542 | if (err < 0) |
| 1543 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1544 | } |
| 1545 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1546 | |
| 1547 | /* Convert to tuple. */ |
| 1548 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1549 | |
| 1550 | done: |
| 1551 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1552 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1553 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1557 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1559 | PyObject *retval = NULL; |
| 1560 | BIO *biobuf = NULL; |
| 1561 | PyObject *peer; |
| 1562 | PyObject *peer_alt_names = NULL; |
| 1563 | PyObject *issuer; |
| 1564 | PyObject *version; |
| 1565 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1566 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1567 | ASN1_INTEGER *serialNumber; |
| 1568 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1569 | int len, result; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1570 | ASN1_TIME *notBefore, *notAfter; |
| 1571 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1573 | retval = PyDict_New(); |
| 1574 | if (retval == NULL) |
| 1575 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1577 | peer = _create_tuple_for_X509_NAME( |
| 1578 | X509_get_subject_name(certificate)); |
| 1579 | if (peer == NULL) |
| 1580 | goto fail0; |
| 1581 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1582 | Py_DECREF(peer); |
| 1583 | goto fail0; |
| 1584 | } |
| 1585 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1587 | issuer = _create_tuple_for_X509_NAME( |
| 1588 | X509_get_issuer_name(certificate)); |
| 1589 | if (issuer == NULL) |
| 1590 | goto fail0; |
| 1591 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1592 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1593 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1594 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1595 | Py_DECREF(issuer); |
| 1596 | |
| 1597 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1598 | if (version == NULL) |
| 1599 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1600 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1601 | Py_DECREF(version); |
| 1602 | goto fail0; |
| 1603 | } |
| 1604 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1606 | /* get a memory buffer */ |
| 1607 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1608 | if (biobuf == NULL) { |
| 1609 | PyErr_SetString(PySSLErrorObject, "failed to allocate BIO"); |
| 1610 | goto fail0; |
| 1611 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1613 | (void) BIO_reset(biobuf); |
| 1614 | serialNumber = X509_get_serialNumber(certificate); |
| 1615 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1616 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1617 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1618 | if (len < 0) { |
| 1619 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1620 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1621 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1622 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1623 | if (sn_obj == NULL) |
| 1624 | goto fail1; |
| 1625 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1626 | Py_DECREF(sn_obj); |
| 1627 | goto fail1; |
| 1628 | } |
| 1629 | Py_DECREF(sn_obj); |
| 1630 | |
| 1631 | (void) BIO_reset(biobuf); |
| 1632 | notBefore = X509_get_notBefore(certificate); |
| 1633 | ASN1_TIME_print(biobuf, notBefore); |
| 1634 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1635 | if (len < 0) { |
| 1636 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1637 | goto fail1; |
| 1638 | } |
| 1639 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1640 | if (pnotBefore == NULL) |
| 1641 | goto fail1; |
| 1642 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1643 | Py_DECREF(pnotBefore); |
| 1644 | goto fail1; |
| 1645 | } |
| 1646 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1648 | (void) BIO_reset(biobuf); |
| 1649 | notAfter = X509_get_notAfter(certificate); |
| 1650 | ASN1_TIME_print(biobuf, notAfter); |
| 1651 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1652 | if (len < 0) { |
| 1653 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1654 | goto fail1; |
| 1655 | } |
| 1656 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1657 | if (pnotAfter == NULL) |
| 1658 | goto fail1; |
| 1659 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1660 | Py_DECREF(pnotAfter); |
| 1661 | goto fail1; |
| 1662 | } |
| 1663 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1665 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1667 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1668 | if (peer_alt_names == NULL) |
| 1669 | goto fail1; |
| 1670 | else if (peer_alt_names != Py_None) { |
| 1671 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1672 | peer_alt_names) < 0) { |
| 1673 | Py_DECREF(peer_alt_names); |
| 1674 | goto fail1; |
| 1675 | } |
| 1676 | Py_DECREF(peer_alt_names); |
| 1677 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1678 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1679 | /* Authority Information Access: OCSP URIs */ |
| 1680 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1681 | if (obj == NULL) { |
| 1682 | goto fail1; |
| 1683 | } else if (obj != Py_None) { |
| 1684 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1685 | Py_DECREF(obj); |
| 1686 | if (result < 0) { |
| 1687 | goto fail1; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1692 | if (obj == NULL) { |
| 1693 | goto fail1; |
| 1694 | } else if (obj != Py_None) { |
| 1695 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1696 | Py_DECREF(obj); |
| 1697 | if (result < 0) { |
| 1698 | goto fail1; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /* CDP (CRL distribution points) */ |
| 1703 | obj = _get_crl_dp(certificate); |
| 1704 | if (obj == NULL) { |
| 1705 | goto fail1; |
| 1706 | } else if (obj != Py_None) { |
| 1707 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1708 | Py_DECREF(obj); |
| 1709 | if (result < 0) { |
| 1710 | goto fail1; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1714 | BIO_free(biobuf); |
| 1715 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1716 | |
| 1717 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1718 | if (biobuf != NULL) |
| 1719 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1720 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1721 | Py_XDECREF(retval); |
| 1722 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1723 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1724 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1725 | static PyObject * |
| 1726 | _certificate_to_der(X509 *certificate) |
| 1727 | { |
| 1728 | unsigned char *bytes_buf = NULL; |
| 1729 | int len; |
| 1730 | PyObject *retval; |
| 1731 | |
| 1732 | bytes_buf = NULL; |
| 1733 | len = i2d_X509(certificate, &bytes_buf); |
| 1734 | if (len < 0) { |
| 1735 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1736 | return NULL; |
| 1737 | } |
| 1738 | /* this is actually an immutable bytes sequence */ |
| 1739 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1740 | OPENSSL_free(bytes_buf); |
| 1741 | return retval; |
| 1742 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1743 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1744 | /*[clinic input] |
| 1745 | _ssl._test_decode_cert |
| 1746 | path: object(converter="PyUnicode_FSConverter") |
| 1747 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1748 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1749 | [clinic start generated code]*/ |
| 1750 | |
| 1751 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1752 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1753 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1754 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1755 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1756 | X509 *x=NULL; |
| 1757 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1759 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1760 | PyErr_SetString(PySSLErrorObject, |
| 1761 | "Can't malloc memory to read file"); |
| 1762 | goto fail0; |
| 1763 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1764 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1765 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1766 | PyErr_SetString(PySSLErrorObject, |
| 1767 | "Can't open file"); |
| 1768 | goto fail0; |
| 1769 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1770 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1771 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 1772 | if (x == NULL) { |
| 1773 | PyErr_SetString(PySSLErrorObject, |
| 1774 | "Error decoding PEM-encoded file"); |
| 1775 | goto fail0; |
| 1776 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1778 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1779 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1780 | |
| 1781 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1782 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1783 | if (cert != NULL) BIO_free(cert); |
| 1784 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1788 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1789 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1790 | der as binary_mode: bool = False |
| 1791 | / |
| 1792 | |
| 1793 | Returns the certificate for the peer. |
| 1794 | |
| 1795 | If no certificate was provided, returns None. If a certificate was |
| 1796 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1797 | returns a dict containing information about the peer certificate. |
| 1798 | |
| 1799 | If the optional argument is True, returns a DER-encoded copy of the |
| 1800 | peer certificate, or None if no certificate was provided. This will |
| 1801 | return the certificate even if it wasn't validated. |
| 1802 | [clinic start generated code]*/ |
| 1803 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1804 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1805 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1806 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1807 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1808 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1809 | X509 *peer_cert; |
| 1810 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1811 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1812 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1813 | PyErr_SetString(PyExc_ValueError, |
| 1814 | "handshake not done yet"); |
| 1815 | return NULL; |
| 1816 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1817 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1818 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1819 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1820 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1821 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1822 | /* return cert in DER-encoded format */ |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1823 | result = _certificate_to_der(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1824 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1825 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1826 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1827 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1828 | else |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1829 | result = _decode_certificate(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1830 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1831 | X509_free(peer_cert); |
| 1832 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1835 | static PyObject * |
| 1836 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1837 | { |
| 1838 | const char *cipher_name, *cipher_protocol; |
| 1839 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1840 | if (retval == NULL) |
| 1841 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1842 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1843 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1844 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1845 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1846 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1847 | } else { |
| 1848 | v = PyUnicode_FromString(cipher_name); |
| 1849 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1850 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1851 | PyTuple_SET_ITEM(retval, 0, v); |
| 1852 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1853 | |
| 1854 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1855 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1856 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1857 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1858 | } else { |
| 1859 | v = PyUnicode_FromString(cipher_protocol); |
| 1860 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1861 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1862 | PyTuple_SET_ITEM(retval, 1, v); |
| 1863 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1864 | |
| 1865 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1866 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1867 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1868 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1869 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1870 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1871 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1872 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1873 | Py_DECREF(retval); |
| 1874 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1877 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1878 | static PyObject * |
| 1879 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1880 | { |
| 1881 | const char *cipher_name, *cipher_protocol; |
| 1882 | |
| 1883 | unsigned long cipher_id; |
| 1884 | int alg_bits, strength_bits, len; |
| 1885 | char buf[512] = {0}; |
| 1886 | #if OPENSSL_VERSION_1_1 |
| 1887 | int aead, nid; |
| 1888 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 1889 | #endif |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1890 | |
| 1891 | /* can be NULL */ |
| 1892 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1893 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1894 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1895 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1896 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 1897 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1898 | if (len > 1 && buf[len-1] == '\n') |
| 1899 | buf[len-1] = '\0'; |
| 1900 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 1901 | |
| 1902 | #if OPENSSL_VERSION_1_1 |
| 1903 | aead = SSL_CIPHER_is_aead(cipher); |
| 1904 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 1905 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1906 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 1907 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1908 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 1909 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1910 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 1911 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1912 | #endif |
| 1913 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1914 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1915 | "{sksssssssisi" |
| 1916 | #if OPENSSL_VERSION_1_1 |
| 1917 | "sOssssssss" |
| 1918 | #endif |
| 1919 | "}", |
| 1920 | "id", cipher_id, |
| 1921 | "name", cipher_name, |
| 1922 | "protocol", cipher_protocol, |
| 1923 | "description", buf, |
| 1924 | "strength_bits", strength_bits, |
| 1925 | "alg_bits", alg_bits |
| 1926 | #if OPENSSL_VERSION_1_1 |
| 1927 | ,"aead", aead ? Py_True : Py_False, |
| 1928 | "symmetric", skcipher, |
| 1929 | "digest", digest, |
| 1930 | "kea", kx, |
| 1931 | "auth", auth |
| 1932 | #endif |
| 1933 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1934 | } |
| 1935 | #endif |
| 1936 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1937 | /*[clinic input] |
| 1938 | _ssl._SSLSocket.shared_ciphers |
| 1939 | [clinic start generated code]*/ |
| 1940 | |
| 1941 | static PyObject * |
| 1942 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 1943 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1944 | { |
| 1945 | STACK_OF(SSL_CIPHER) *ciphers; |
| 1946 | int i; |
| 1947 | PyObject *res; |
| 1948 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1949 | ciphers = SSL_get_ciphers(self->ssl); |
| 1950 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1951 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1952 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 1953 | if (!res) |
| 1954 | return NULL; |
| 1955 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 1956 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 1957 | if (!tup) { |
| 1958 | Py_DECREF(res); |
| 1959 | return NULL; |
| 1960 | } |
| 1961 | PyList_SET_ITEM(res, i, tup); |
| 1962 | } |
| 1963 | return res; |
| 1964 | } |
| 1965 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1966 | /*[clinic input] |
| 1967 | _ssl._SSLSocket.cipher |
| 1968 | [clinic start generated code]*/ |
| 1969 | |
| 1970 | static PyObject * |
| 1971 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 1972 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1973 | { |
| 1974 | const SSL_CIPHER *current; |
| 1975 | |
| 1976 | if (self->ssl == NULL) |
| 1977 | Py_RETURN_NONE; |
| 1978 | current = SSL_get_current_cipher(self->ssl); |
| 1979 | if (current == NULL) |
| 1980 | Py_RETURN_NONE; |
| 1981 | return cipher_to_tuple(current); |
| 1982 | } |
| 1983 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1984 | /*[clinic input] |
| 1985 | _ssl._SSLSocket.version |
| 1986 | [clinic start generated code]*/ |
| 1987 | |
| 1988 | static PyObject * |
| 1989 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 1990 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1991 | { |
| 1992 | const char *version; |
| 1993 | |
| 1994 | if (self->ssl == NULL) |
| 1995 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 1996 | if (!SSL_is_init_finished(self->ssl)) { |
| 1997 | /* handshake not finished */ |
| 1998 | Py_RETURN_NONE; |
| 1999 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2000 | version = SSL_get_version(self->ssl); |
| 2001 | if (!strcmp(version, "unknown")) |
| 2002 | Py_RETURN_NONE; |
| 2003 | return PyUnicode_FromString(version); |
| 2004 | } |
| 2005 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2006 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2007 | /*[clinic input] |
| 2008 | _ssl._SSLSocket.selected_npn_protocol |
| 2009 | [clinic start generated code]*/ |
| 2010 | |
| 2011 | static PyObject * |
| 2012 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 2013 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 2014 | { |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2015 | const unsigned char *out; |
| 2016 | unsigned int outlen; |
| 2017 | |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2018 | SSL_get0_next_proto_negotiated(self->ssl, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2019 | &out, &outlen); |
| 2020 | |
| 2021 | if (out == NULL) |
| 2022 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2023 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 2024 | } |
| 2025 | #endif |
| 2026 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 2027 | #if HAVE_ALPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2028 | /*[clinic input] |
| 2029 | _ssl._SSLSocket.selected_alpn_protocol |
| 2030 | [clinic start generated code]*/ |
| 2031 | |
| 2032 | static PyObject * |
| 2033 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2034 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2035 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2036 | const unsigned char *out; |
| 2037 | unsigned int outlen; |
| 2038 | |
| 2039 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2040 | |
| 2041 | if (out == NULL) |
| 2042 | Py_RETURN_NONE; |
| 2043 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2044 | } |
| 2045 | #endif |
| 2046 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2047 | /*[clinic input] |
| 2048 | _ssl._SSLSocket.compression |
| 2049 | [clinic start generated code]*/ |
| 2050 | |
| 2051 | static PyObject * |
| 2052 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2053 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2054 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2055 | #ifdef OPENSSL_NO_COMP |
| 2056 | Py_RETURN_NONE; |
| 2057 | #else |
| 2058 | const COMP_METHOD *comp_method; |
| 2059 | const char *short_name; |
| 2060 | |
| 2061 | if (self->ssl == NULL) |
| 2062 | Py_RETURN_NONE; |
| 2063 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2064 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2065 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2066 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2067 | if (short_name == NULL) |
| 2068 | Py_RETURN_NONE; |
| 2069 | return PyUnicode_DecodeFSDefault(short_name); |
| 2070 | #endif |
| 2071 | } |
| 2072 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2073 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2074 | Py_INCREF(self->ctx); |
| 2075 | return self->ctx; |
| 2076 | } |
| 2077 | |
| 2078 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2079 | void *closure) { |
| 2080 | |
| 2081 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2082 | #if !HAVE_SNI |
| 2083 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 2084 | "context is not supported by your OpenSSL library"); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 2085 | return -1; |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2086 | #else |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2087 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2088 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2089 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 2090 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2091 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2092 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2093 | return -1; |
| 2094 | } |
| 2095 | |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2100 | "_setter_context(ctx)\n\ |
| 2101 | \ |
| 2102 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2103 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2104 | on the SSLContext to change the certificate information associated with the\n\ |
| 2105 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2106 | |
| 2107 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2108 | static PyObject * |
| 2109 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2110 | { |
| 2111 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2112 | } |
| 2113 | |
| 2114 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2115 | "Whether this is a server-side socket."); |
| 2116 | |
| 2117 | static PyObject * |
| 2118 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2119 | { |
| 2120 | if (self->server_hostname == NULL) |
| 2121 | Py_RETURN_NONE; |
| 2122 | Py_INCREF(self->server_hostname); |
| 2123 | return self->server_hostname; |
| 2124 | } |
| 2125 | |
| 2126 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2127 | "The currently set server hostname (for SNI)."); |
| 2128 | |
| 2129 | static PyObject * |
| 2130 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2131 | { |
| 2132 | PyObject *owner; |
| 2133 | |
| 2134 | if (self->owner == NULL) |
| 2135 | Py_RETURN_NONE; |
| 2136 | |
| 2137 | owner = PyWeakref_GetObject(self->owner); |
| 2138 | Py_INCREF(owner); |
| 2139 | return owner; |
| 2140 | } |
| 2141 | |
| 2142 | static int |
| 2143 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2144 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2145 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2146 | if (self->owner == NULL) |
| 2147 | return -1; |
| 2148 | return 0; |
| 2149 | } |
| 2150 | |
| 2151 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2152 | "The Python-level owner of this object.\ |
| 2153 | Passed as \"self\" in servername callback."); |
| 2154 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2155 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2156 | static void PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2157 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2158 | if (self->ssl) |
| 2159 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2160 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2161 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2162 | Py_XDECREF(self->server_hostname); |
| 2163 | Py_XDECREF(self->owner); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2164 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2167 | /* 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] | 2168 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2169 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2170 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2171 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2172 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2173 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2174 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2175 | int rc; |
| 2176 | #ifdef HAVE_POLL |
| 2177 | struct pollfd pollfd; |
| 2178 | _PyTime_t ms; |
| 2179 | #else |
| 2180 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2181 | fd_set fds; |
| 2182 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2183 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2185 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2186 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2187 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2188 | else if (timeout < 0) { |
| 2189 | if (s->sock_timeout > 0) |
| 2190 | return SOCKET_HAS_TIMED_OUT; |
| 2191 | else |
| 2192 | return SOCKET_IS_BLOCKING; |
| 2193 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2194 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2195 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2196 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2197 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2198 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2199 | /* Prefer poll, if available, since you can poll() any fd |
| 2200 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2202 | pollfd.fd = s->sock_fd; |
| 2203 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2204 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2205 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2206 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2207 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2208 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2209 | PySSL_BEGIN_ALLOW_THREADS |
| 2210 | rc = poll(&pollfd, 1, (int)ms); |
| 2211 | PySSL_END_ALLOW_THREADS |
| 2212 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2213 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2214 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2215 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2216 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2217 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2218 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2219 | FD_ZERO(&fds); |
| 2220 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2221 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2222 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2223 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2224 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2225 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2226 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2227 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2228 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2229 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2230 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2231 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2232 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2233 | (when we are able to write or when there's something to read) */ |
| 2234 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2237 | /*[clinic input] |
| 2238 | _ssl._SSLSocket.write |
| 2239 | b: Py_buffer |
| 2240 | / |
| 2241 | |
| 2242 | Writes the bytes-like object b into the SSL object. |
| 2243 | |
| 2244 | Returns the number of bytes written. |
| 2245 | [clinic start generated code]*/ |
| 2246 | |
| 2247 | static PyObject * |
| 2248 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2249 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2250 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2251 | int len; |
| 2252 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2253 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2254 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2255 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2256 | _PyTime_t timeout, deadline = 0; |
| 2257 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2258 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2259 | if (sock != NULL) { |
| 2260 | if (((PyObject*)sock) == Py_None) { |
| 2261 | _setSSLError("Underlying socket connection gone", |
| 2262 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2268 | if (b->len > INT_MAX) { |
Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2269 | PyErr_Format(PyExc_OverflowError, |
| 2270 | "string longer than %d bytes", INT_MAX); |
| 2271 | goto error; |
| 2272 | } |
| 2273 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2274 | if (sock != NULL) { |
| 2275 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2276 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2277 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2278 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2279 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2280 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2281 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2282 | has_timeout = (timeout > 0); |
| 2283 | if (has_timeout) |
| 2284 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2285 | |
| 2286 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2287 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2288 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2289 | "The write operation timed out"); |
| 2290 | goto error; |
| 2291 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2292 | PyErr_SetString(PySSLErrorObject, |
| 2293 | "Underlying socket has been closed."); |
| 2294 | goto error; |
| 2295 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2296 | PyErr_SetString(PySSLErrorObject, |
| 2297 | "Underlying socket too large for select()."); |
| 2298 | goto error; |
| 2299 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2300 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2301 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2302 | PySSL_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2303 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2304 | err = _PySSL_errno(len <= 0, self->ssl, len); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2305 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2306 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2307 | |
| 2308 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2309 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2310 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2311 | if (has_timeout) |
| 2312 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2313 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2314 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2315 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2316 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2317 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2318 | } else { |
| 2319 | sockstate = SOCKET_OPERATION_OK; |
| 2320 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2321 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2322 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2323 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2324 | "The write operation timed out"); |
| 2325 | goto error; |
| 2326 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2327 | PyErr_SetString(PySSLErrorObject, |
| 2328 | "Underlying socket has been closed."); |
| 2329 | goto error; |
| 2330 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2331 | break; |
| 2332 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2333 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2334 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2335 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2336 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2337 | if (len > 0) |
| 2338 | return PyLong_FromLong(len); |
| 2339 | else |
| 2340 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2341 | |
| 2342 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2343 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2344 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2347 | /*[clinic input] |
| 2348 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2349 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2350 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2351 | [clinic start generated code]*/ |
| 2352 | |
| 2353 | static PyObject * |
| 2354 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2355 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2356 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2357 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2358 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2360 | PySSL_BEGIN_ALLOW_THREADS |
| 2361 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2362 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2363 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2364 | self->err = err; |
| 2365 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2366 | if (count < 0) |
| 2367 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2368 | else |
| 2369 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2372 | /*[clinic input] |
| 2373 | _ssl._SSLSocket.read |
| 2374 | size as len: int |
| 2375 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2376 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2377 | ] |
| 2378 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2379 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2380 | Read up to size bytes from the SSL socket. |
| 2381 | [clinic start generated code]*/ |
| 2382 | |
| 2383 | static PyObject * |
| 2384 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2385 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2386 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2387 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2388 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2389 | char *mem; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2390 | int count; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2391 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2392 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2393 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2394 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2395 | _PyTime_t timeout, deadline = 0; |
| 2396 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2397 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2398 | if (!group_right_1 && len < 0) { |
| 2399 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2403 | if (sock != NULL) { |
| 2404 | if (((PyObject*)sock) == Py_None) { |
| 2405 | _setSSLError("Underlying socket connection gone", |
| 2406 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2407 | return NULL; |
| 2408 | } |
| 2409 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2412 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2413 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2414 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2415 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2416 | if (len == 0) { |
| 2417 | Py_XDECREF(sock); |
| 2418 | return dest; |
| 2419 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2420 | mem = PyBytes_AS_STRING(dest); |
| 2421 | } |
| 2422 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2423 | mem = buffer->buf; |
| 2424 | if (len <= 0 || len > buffer->len) { |
| 2425 | len = (int) buffer->len; |
| 2426 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2427 | PyErr_SetString(PyExc_OverflowError, |
| 2428 | "maximum length can't fit in a C 'int'"); |
| 2429 | goto error; |
| 2430 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2431 | if (len == 0) { |
| 2432 | count = 0; |
| 2433 | goto done; |
| 2434 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2435 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2438 | if (sock != NULL) { |
| 2439 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2440 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2441 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2442 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2443 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2444 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2445 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2446 | has_timeout = (timeout > 0); |
| 2447 | if (has_timeout) |
| 2448 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2449 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2450 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2451 | PySSL_BEGIN_ALLOW_THREADS |
| 2452 | count = SSL_read(self->ssl, mem, len); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2453 | err = _PySSL_errno(count <= 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2454 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2455 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2456 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2457 | if (PyErr_CheckSignals()) |
| 2458 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2459 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2460 | if (has_timeout) |
| 2461 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2462 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2463 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2464 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2465 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2466 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2467 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2468 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2469 | { |
| 2470 | count = 0; |
| 2471 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2472 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2473 | else |
| 2474 | sockstate = SOCKET_OPERATION_OK; |
| 2475 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2476 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2477 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2478 | "The read operation timed out"); |
| 2479 | goto error; |
| 2480 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2481 | break; |
| 2482 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2483 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2484 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2485 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2486 | if (count <= 0) { |
| 2487 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2488 | goto error; |
| 2489 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2490 | |
| 2491 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2492 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2493 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2494 | _PyBytes_Resize(&dest, count); |
| 2495 | return dest; |
| 2496 | } |
| 2497 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2498 | return PyLong_FromLong(count); |
| 2499 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2500 | |
| 2501 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2502 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2503 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2504 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2505 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2508 | /*[clinic input] |
| 2509 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2510 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2511 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2512 | [clinic start generated code]*/ |
| 2513 | |
| 2514 | static PyObject * |
| 2515 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2516 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2517 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2518 | _PySSLError err; |
| 2519 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2520 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2521 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2522 | _PyTime_t timeout, deadline = 0; |
| 2523 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2525 | if (sock != NULL) { |
| 2526 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2527 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2528 | _setSSLError("Underlying socket connection gone", |
| 2529 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2530 | return NULL; |
| 2531 | } |
| 2532 | Py_INCREF(sock); |
| 2533 | |
| 2534 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2535 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2536 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2537 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2538 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2539 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2540 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2541 | has_timeout = (timeout > 0); |
| 2542 | if (has_timeout) |
| 2543 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2544 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2545 | while (1) { |
| 2546 | PySSL_BEGIN_ALLOW_THREADS |
| 2547 | /* Disable read-ahead so that unwrap can work correctly. |
| 2548 | * Otherwise OpenSSL might read in too much data, |
| 2549 | * eating clear text data that happens to be |
| 2550 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2551 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2552 | * function is used and the shutdown_seen_zero != 0 |
| 2553 | * condition is met. |
| 2554 | */ |
| 2555 | if (self->shutdown_seen_zero) |
| 2556 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2557 | ret = SSL_shutdown(self->ssl); |
| 2558 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2559 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2560 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2561 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2562 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2563 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2564 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2565 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2566 | /* Don't loop endlessly; instead preserve legacy |
| 2567 | behaviour of trying SSL_shutdown() only twice. |
| 2568 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2569 | if (++zeros > 1) |
| 2570 | break; |
| 2571 | /* Shutdown was sent, now try receiving */ |
| 2572 | self->shutdown_seen_zero = 1; |
| 2573 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2576 | if (has_timeout) |
| 2577 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2578 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2579 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2580 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2581 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2582 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2583 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2584 | else |
| 2585 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2586 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2587 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2588 | if (err.ssl == SSL_ERROR_WANT_READ) |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2589 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2590 | "The read operation timed out"); |
| 2591 | else |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2592 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2593 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2594 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2595 | } |
| 2596 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2597 | PyErr_SetString(PySSLErrorObject, |
| 2598 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2599 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2600 | } |
| 2601 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2602 | /* Retain the SSL error code */ |
| 2603 | break; |
| 2604 | } |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 2605 | |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2606 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2607 | Py_XDECREF(sock); |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2608 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2609 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2610 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2611 | /* It's already INCREF'ed */ |
| 2612 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2613 | else |
| 2614 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2615 | |
| 2616 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2617 | Py_XDECREF(sock); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2618 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2621 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2622 | _ssl._SSLSocket.get_channel_binding |
| 2623 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2624 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2625 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2626 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2627 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2628 | of the data or None if the data is not available (e.g. before the handshake). |
| 2629 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2630 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2632 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2633 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2634 | const char *cb_type) |
| 2635 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2636 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2637 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2638 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2639 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2640 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2641 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2642 | /* if session is resumed XOR we are the client */ |
| 2643 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2644 | } |
| 2645 | else { |
| 2646 | /* if a new session XOR we are the server */ |
| 2647 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2648 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2649 | } |
| 2650 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2651 | PyErr_Format( |
| 2652 | PyExc_ValueError, |
| 2653 | "'%s' channel binding type not implemented", |
| 2654 | cb_type |
| 2655 | ); |
| 2656 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2660 | if (len == 0) |
| 2661 | Py_RETURN_NONE; |
| 2662 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2663 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2664 | } |
| 2665 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2666 | /*[clinic input] |
| 2667 | _ssl._SSLSocket.verify_client_post_handshake |
| 2668 | |
| 2669 | Initiate TLS 1.3 post-handshake authentication |
| 2670 | [clinic start generated code]*/ |
| 2671 | |
| 2672 | static PyObject * |
| 2673 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2674 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2675 | { |
| 2676 | #ifdef TLS1_3_VERSION |
| 2677 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2678 | if (err == 0) |
| 2679 | return _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2680 | else |
| 2681 | Py_RETURN_NONE; |
| 2682 | #else |
| 2683 | PyErr_SetString(PyExc_NotImplementedError, |
| 2684 | "Post-handshake auth is not supported by your " |
| 2685 | "OpenSSL version."); |
| 2686 | return NULL; |
| 2687 | #endif |
| 2688 | } |
| 2689 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2690 | #ifdef OPENSSL_VERSION_1_1 |
| 2691 | |
| 2692 | static SSL_SESSION* |
| 2693 | _ssl_session_dup(SSL_SESSION *session) { |
| 2694 | SSL_SESSION *newsession = NULL; |
| 2695 | int slen; |
| 2696 | unsigned char *senc = NULL, *p; |
| 2697 | const unsigned char *const_p; |
| 2698 | |
| 2699 | if (session == NULL) { |
| 2700 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2701 | goto error; |
| 2702 | } |
| 2703 | |
| 2704 | /* get length */ |
| 2705 | slen = i2d_SSL_SESSION(session, NULL); |
| 2706 | if (slen == 0 || slen > 0xFF00) { |
| 2707 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2708 | goto error; |
| 2709 | } |
| 2710 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2711 | PyErr_NoMemory(); |
| 2712 | goto error; |
| 2713 | } |
| 2714 | p = senc; |
| 2715 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2716 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2717 | goto error; |
| 2718 | } |
| 2719 | const_p = senc; |
| 2720 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2721 | if (session == NULL) { |
| 2722 | goto error; |
| 2723 | } |
| 2724 | PyMem_Free(senc); |
| 2725 | return newsession; |
| 2726 | error: |
| 2727 | if (senc != NULL) { |
| 2728 | PyMem_Free(senc); |
| 2729 | } |
| 2730 | return NULL; |
| 2731 | } |
| 2732 | #endif |
| 2733 | |
| 2734 | static PyObject * |
| 2735 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2736 | /* get_session can return sessions from a server-side connection, |
| 2737 | * it does not check for handshake done or client socket. */ |
| 2738 | PySSLSession *pysess; |
| 2739 | SSL_SESSION *session; |
| 2740 | |
| 2741 | #ifdef OPENSSL_VERSION_1_1 |
| 2742 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2743 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2744 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2745 | if (session == NULL) { |
| 2746 | Py_RETURN_NONE; |
| 2747 | } |
| 2748 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2749 | return NULL; |
| 2750 | } |
| 2751 | #else |
| 2752 | session = SSL_get1_session(self->ssl); |
| 2753 | if (session == NULL) { |
| 2754 | Py_RETURN_NONE; |
| 2755 | } |
| 2756 | #endif |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2757 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2758 | if (pysess == NULL) { |
| 2759 | SSL_SESSION_free(session); |
| 2760 | return NULL; |
| 2761 | } |
| 2762 | |
| 2763 | assert(self->ctx); |
| 2764 | pysess->ctx = self->ctx; |
| 2765 | Py_INCREF(pysess->ctx); |
| 2766 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2767 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2768 | return (PyObject *)pysess; |
| 2769 | } |
| 2770 | |
| 2771 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2772 | void *closure) |
| 2773 | { |
| 2774 | PySSLSession *pysess; |
| 2775 | #ifdef OPENSSL_VERSION_1_1 |
| 2776 | SSL_SESSION *session; |
| 2777 | #endif |
| 2778 | int result; |
| 2779 | |
| 2780 | if (!PySSLSession_Check(value)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2781 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2782 | return -1; |
| 2783 | } |
| 2784 | pysess = (PySSLSession *)value; |
| 2785 | |
| 2786 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2787 | PyErr_SetString(PyExc_ValueError, |
| 2788 | "Session refers to a different SSLContext."); |
| 2789 | return -1; |
| 2790 | } |
| 2791 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2792 | PyErr_SetString(PyExc_ValueError, |
| 2793 | "Cannot set session for server-side SSLSocket."); |
| 2794 | return -1; |
| 2795 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2796 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2797 | PyErr_SetString(PyExc_ValueError, |
| 2798 | "Cannot set session after handshake."); |
| 2799 | return -1; |
| 2800 | } |
| 2801 | #ifdef OPENSSL_VERSION_1_1 |
| 2802 | /* duplicate session */ |
| 2803 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2804 | return -1; |
| 2805 | } |
| 2806 | result = SSL_set_session(self->ssl, session); |
| 2807 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2808 | SSL_SESSION_free(session); |
| 2809 | #else |
| 2810 | result = SSL_set_session(self->ssl, pysess->session); |
| 2811 | #endif |
| 2812 | if (result == 0) { |
| 2813 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2814 | return -1; |
| 2815 | } |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2820 | "_setter_session(session)\n\ |
| 2821 | \ |
| 2822 | Get / set SSLSession."); |
| 2823 | |
| 2824 | static PyObject * |
| 2825 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2826 | if (SSL_session_reused(self->ssl)) { |
| 2827 | Py_RETURN_TRUE; |
| 2828 | } else { |
| 2829 | Py_RETURN_FALSE; |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2834 | "Was the client session reused during handshake?"); |
| 2835 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2836 | static PyGetSetDef ssl_getsetlist[] = { |
| 2837 | {"context", (getter) PySSL_get_context, |
| 2838 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2839 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2840 | PySSL_get_server_side_doc}, |
| 2841 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2842 | PySSL_get_server_hostname_doc}, |
| 2843 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2844 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2845 | {"session", (getter) PySSL_get_session, |
| 2846 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2847 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2848 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2849 | {NULL}, /* sentinel */ |
| 2850 | }; |
| 2851 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2852 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2853 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2854 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2855 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2856 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2857 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2858 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2859 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2860 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2861 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 2862 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 2863 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2864 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2865 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2866 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2867 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2868 | }; |
| 2869 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2870 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2871 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2872 | "_ssl._SSLSocket", /*tp_name*/ |
| 2873 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2874 | 0, /*tp_itemsize*/ |
| 2875 | /* methods */ |
| 2876 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 2877 | 0, /*tp_print*/ |
| 2878 | 0, /*tp_getattr*/ |
| 2879 | 0, /*tp_setattr*/ |
| 2880 | 0, /*tp_reserved*/ |
| 2881 | 0, /*tp_repr*/ |
| 2882 | 0, /*tp_as_number*/ |
| 2883 | 0, /*tp_as_sequence*/ |
| 2884 | 0, /*tp_as_mapping*/ |
| 2885 | 0, /*tp_hash*/ |
| 2886 | 0, /*tp_call*/ |
| 2887 | 0, /*tp_str*/ |
| 2888 | 0, /*tp_getattro*/ |
| 2889 | 0, /*tp_setattro*/ |
| 2890 | 0, /*tp_as_buffer*/ |
| 2891 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2892 | 0, /*tp_doc*/ |
| 2893 | 0, /*tp_traverse*/ |
| 2894 | 0, /*tp_clear*/ |
| 2895 | 0, /*tp_richcompare*/ |
| 2896 | 0, /*tp_weaklistoffset*/ |
| 2897 | 0, /*tp_iter*/ |
| 2898 | 0, /*tp_iternext*/ |
| 2899 | PySSLMethods, /*tp_methods*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2900 | 0, /*tp_members*/ |
| 2901 | ssl_getsetlist, /*tp_getset*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2902 | }; |
| 2903 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2904 | |
| 2905 | /* |
| 2906 | * _SSLContext objects |
| 2907 | */ |
| 2908 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2909 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2910 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2911 | { |
| 2912 | int mode; |
| 2913 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 2914 | |
| 2915 | switch(n) { |
| 2916 | case PY_SSL_CERT_NONE: |
| 2917 | mode = SSL_VERIFY_NONE; |
| 2918 | break; |
| 2919 | case PY_SSL_CERT_OPTIONAL: |
| 2920 | mode = SSL_VERIFY_PEER; |
| 2921 | break; |
| 2922 | case PY_SSL_CERT_REQUIRED: |
| 2923 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2924 | break; |
| 2925 | default: |
| 2926 | PyErr_SetString(PyExc_ValueError, |
| 2927 | "invalid value for verify_mode"); |
| 2928 | return -1; |
| 2929 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2930 | #ifdef TLS1_3_VERSION |
| 2931 | if (self->post_handshake_auth) |
| 2932 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 2933 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2934 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2935 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 2936 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2937 | return 0; |
| 2938 | } |
| 2939 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2940 | /*[clinic input] |
| 2941 | @classmethod |
| 2942 | _ssl._SSLContext.__new__ |
| 2943 | protocol as proto_version: int |
| 2944 | / |
| 2945 | [clinic start generated code]*/ |
| 2946 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2947 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2948 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 2949 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2950 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2951 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2952 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2953 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2954 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2955 | int result; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2956 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 2957 | unsigned long libver; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2958 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2959 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2960 | PySSL_BEGIN_ALLOW_THREADS |
| 2961 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 2962 | ctx = SSL_CTX_new(TLSv1_method()); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 2963 | #if HAVE_TLSv1_2 |
| 2964 | else if (proto_version == PY_SSL_VERSION_TLS1_1) |
| 2965 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 2966 | else if (proto_version == PY_SSL_VERSION_TLS1_2) |
| 2967 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 2968 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2969 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2970 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 2971 | ctx = SSL_CTX_new(SSLv3_method()); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2972 | #endif |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2973 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2974 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 2975 | ctx = SSL_CTX_new(SSLv2_method()); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2976 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2977 | else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2978 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2979 | else if (proto_version == PY_SSL_VERSION_TLS_CLIENT) |
| 2980 | ctx = SSL_CTX_new(TLS_client_method()); |
| 2981 | else if (proto_version == PY_SSL_VERSION_TLS_SERVER) |
| 2982 | ctx = SSL_CTX_new(TLS_server_method()); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2983 | else |
| 2984 | proto_version = -1; |
| 2985 | PySSL_END_ALLOW_THREADS |
| 2986 | |
| 2987 | if (proto_version == -1) { |
| 2988 | PyErr_SetString(PyExc_ValueError, |
| 2989 | "invalid protocol version"); |
| 2990 | return NULL; |
| 2991 | } |
| 2992 | if (ctx == NULL) { |
Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 2993 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | } |
| 2996 | |
| 2997 | assert(type != NULL && type->tp_alloc != NULL); |
| 2998 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 2999 | if (self == NULL) { |
| 3000 | SSL_CTX_free(ctx); |
| 3001 | return NULL; |
| 3002 | } |
| 3003 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3004 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3005 | self->protocol = proto_version; |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3006 | #if HAVE_NPN |
Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 3007 | self->npn_protocols = NULL; |
| 3008 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3009 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3010 | self->alpn_protocols = NULL; |
| 3011 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3012 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3013 | self->set_sni_cb = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3014 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3015 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3016 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3017 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3018 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3019 | Py_DECREF(self); |
| 3020 | return NULL; |
| 3021 | } |
| 3022 | } else { |
| 3023 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3024 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3025 | Py_DECREF(self); |
| 3026 | return NULL; |
| 3027 | } |
| 3028 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3029 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3030 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3031 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3032 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3033 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3034 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3035 | /* Minimal security flags for server and client side context. |
| 3036 | * Client sockets ignore server-side parameters. */ |
| 3037 | #ifdef SSL_OP_NO_COMPRESSION |
| 3038 | options |= SSL_OP_NO_COMPRESSION; |
| 3039 | #endif |
| 3040 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3041 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3042 | #endif |
| 3043 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3044 | options |= SSL_OP_SINGLE_DH_USE; |
| 3045 | #endif |
| 3046 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3047 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3048 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3049 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3050 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3051 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3052 | * It's far from perfect but gives users a better head start. */ |
| 3053 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3054 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3055 | /* stick to OpenSSL's default settings */ |
| 3056 | result = 1; |
| 3057 | #else |
| 3058 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3059 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3060 | } else { |
| 3061 | /* SSLv2 needs MD5 */ |
| 3062 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3063 | } |
| 3064 | if (result == 0) { |
| 3065 | Py_DECREF(self); |
| 3066 | ERR_clear_error(); |
| 3067 | PyErr_SetString(PySSLErrorObject, |
| 3068 | "No cipher can be selected."); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3072 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 3073 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 3074 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 3075 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 3076 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 3077 | conservative and assume it wasn't fixed until release. We do this check |
| 3078 | at runtime to avoid problems from the dynamic linker. |
| 3079 | See #25672 for more on this. */ |
| 3080 | libver = SSLeay(); |
| 3081 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 3082 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 3083 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 3084 | } |
| 3085 | #endif |
| 3086 | |
| 3087 | |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3088 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3089 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 3090 | prime256v1 by default. This is Apache mod_ssl's initialization |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3091 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 3092 | */ |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 3093 | #if defined(SSL_CTX_set_ecdh_auto) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3094 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 3095 | #else |
| 3096 | { |
| 3097 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 3098 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3099 | EC_KEY_free(key); |
| 3100 | } |
| 3101 | #endif |
| 3102 | #endif |
| 3103 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3104 | #define SID_CTX "Python" |
| 3105 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3106 | sizeof(SID_CTX)); |
| 3107 | #undef SID_CTX |
| 3108 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3109 | params = SSL_CTX_get0_param(self->ctx); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3110 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3111 | /* Improve trust chain building when cross-signed intermediate |
| 3112 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3113 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3114 | #endif |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3115 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3116 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3117 | #ifdef TLS1_3_VERSION |
| 3118 | self->post_handshake_auth = 0; |
| 3119 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3120 | #endif |
| 3121 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3122 | return (PyObject *)self; |
| 3123 | } |
| 3124 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3125 | static int |
| 3126 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3127 | { |
| 3128 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3129 | Py_VISIT(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3130 | #endif |
| 3131 | return 0; |
| 3132 | } |
| 3133 | |
| 3134 | static int |
| 3135 | context_clear(PySSLContext *self) |
| 3136 | { |
| 3137 | #ifndef OPENSSL_NO_TLSEXT |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3138 | Py_CLEAR(self->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3139 | #endif |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3143 | static void |
| 3144 | context_dealloc(PySSLContext *self) |
| 3145 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3146 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3147 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3148 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3149 | SSL_CTX_free(self->ctx); |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3150 | #if HAVE_NPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3151 | PyMem_FREE(self->npn_protocols); |
| 3152 | #endif |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3153 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3154 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3155 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3156 | Py_TYPE(self)->tp_free(self); |
| 3157 | } |
| 3158 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3159 | /*[clinic input] |
| 3160 | _ssl._SSLContext.set_ciphers |
| 3161 | cipherlist: str |
| 3162 | / |
| 3163 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3164 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3165 | static PyObject * |
| 3166 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3167 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3168 | { |
| 3169 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3170 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3171 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3172 | otherwise the error will be reported again when another SSL call |
| 3173 | is done. */ |
| 3174 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3175 | PyErr_SetString(PySSLErrorObject, |
| 3176 | "No cipher can be selected."); |
| 3177 | return NULL; |
| 3178 | } |
| 3179 | Py_RETURN_NONE; |
| 3180 | } |
| 3181 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3182 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 3183 | /*[clinic input] |
| 3184 | _ssl._SSLContext.get_ciphers |
| 3185 | [clinic start generated code]*/ |
| 3186 | |
| 3187 | static PyObject * |
| 3188 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3189 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3190 | { |
| 3191 | SSL *ssl = NULL; |
| 3192 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3193 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3194 | int i=0; |
| 3195 | PyObject *result = NULL, *dct; |
| 3196 | |
| 3197 | ssl = SSL_new(self->ctx); |
| 3198 | if (ssl == NULL) { |
| 3199 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3200 | goto exit; |
| 3201 | } |
| 3202 | sk = SSL_get_ciphers(ssl); |
| 3203 | |
| 3204 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3205 | if (result == NULL) { |
| 3206 | goto exit; |
| 3207 | } |
| 3208 | |
| 3209 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3210 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3211 | dct = cipher_to_dict(cipher); |
| 3212 | if (dct == NULL) { |
| 3213 | Py_CLEAR(result); |
| 3214 | goto exit; |
| 3215 | } |
| 3216 | PyList_SET_ITEM(result, i, dct); |
| 3217 | } |
| 3218 | |
| 3219 | exit: |
| 3220 | if (ssl != NULL) |
| 3221 | SSL_free(ssl); |
| 3222 | return result; |
| 3223 | |
| 3224 | } |
| 3225 | #endif |
| 3226 | |
| 3227 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3228 | #if HAVE_NPN || HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3229 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3230 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3231 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3232 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3233 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3234 | int ret; |
| 3235 | if (client_protocols == NULL) { |
| 3236 | client_protocols = (unsigned char *)""; |
| 3237 | client_protocols_len = 0; |
| 3238 | } |
| 3239 | if (server_protocols == NULL) { |
| 3240 | server_protocols = (unsigned char *)""; |
| 3241 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3242 | } |
| 3243 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3244 | ret = SSL_select_next_proto(out, outlen, |
| 3245 | server_protocols, server_protocols_len, |
| 3246 | client_protocols, client_protocols_len); |
| 3247 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3248 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3249 | |
| 3250 | return SSL_TLSEXT_ERR_OK; |
| 3251 | } |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3252 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3253 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3254 | #if HAVE_NPN |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3255 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 3256 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3257 | _advertiseNPN_cb(SSL *s, |
| 3258 | const unsigned char **data, unsigned int *len, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3259 | void *args) |
| 3260 | { |
| 3261 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3262 | |
| 3263 | if (ssl_ctx->npn_protocols == NULL) { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3264 | *data = (unsigned char *)""; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3265 | *len = 0; |
| 3266 | } else { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3267 | *data = ssl_ctx->npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3268 | *len = ssl_ctx->npn_protocols_len; |
| 3269 | } |
| 3270 | |
| 3271 | return SSL_TLSEXT_ERR_OK; |
| 3272 | } |
| 3273 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3274 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3275 | _selectNPN_cb(SSL *s, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3276 | unsigned char **out, unsigned char *outlen, |
| 3277 | const unsigned char *server, unsigned int server_len, |
| 3278 | void *args) |
| 3279 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3280 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3281 | return do_protocol_selection(0, out, outlen, server, server_len, |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3282 | ctx->npn_protocols, ctx->npn_protocols_len); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3283 | } |
| 3284 | #endif |
| 3285 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3286 | /*[clinic input] |
| 3287 | _ssl._SSLContext._set_npn_protocols |
| 3288 | protos: Py_buffer |
| 3289 | / |
| 3290 | [clinic start generated code]*/ |
| 3291 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3292 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3293 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3294 | Py_buffer *protos) |
| 3295 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3296 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3297 | #if HAVE_NPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3298 | PyMem_Free(self->npn_protocols); |
| 3299 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3300 | if (self->npn_protocols == NULL) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3301 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3302 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3303 | self->npn_protocols_len = (int) protos->len; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3304 | |
| 3305 | /* set both server and client callbacks, because the context can |
| 3306 | * be used to create both types of sockets */ |
| 3307 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3308 | _advertiseNPN_cb, |
| 3309 | self); |
| 3310 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3311 | _selectNPN_cb, |
| 3312 | self); |
| 3313 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3314 | Py_RETURN_NONE; |
| 3315 | #else |
| 3316 | PyErr_SetString(PyExc_NotImplementedError, |
| 3317 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3318 | return NULL; |
| 3319 | #endif |
| 3320 | } |
| 3321 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3322 | #if HAVE_ALPN |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3323 | static int |
| 3324 | _selectALPN_cb(SSL *s, |
| 3325 | const unsigned char **out, unsigned char *outlen, |
| 3326 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3327 | void *args) |
| 3328 | { |
| 3329 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3330 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3331 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3332 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3333 | } |
| 3334 | #endif |
| 3335 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3336 | /*[clinic input] |
| 3337 | _ssl._SSLContext._set_alpn_protocols |
| 3338 | protos: Py_buffer |
| 3339 | / |
| 3340 | [clinic start generated code]*/ |
| 3341 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3342 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3343 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3344 | Py_buffer *protos) |
| 3345 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3346 | { |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 3347 | #if HAVE_ALPN |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3348 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3349 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame^] | 3350 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3351 | return NULL; |
| 3352 | } |
| 3353 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3354 | PyMem_FREE(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3355 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3356 | if (!self->alpn_protocols) |
| 3357 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3358 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3359 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3360 | |
| 3361 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3362 | return PyErr_NoMemory(); |
| 3363 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3364 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3365 | Py_RETURN_NONE; |
| 3366 | #else |
| 3367 | PyErr_SetString(PyExc_NotImplementedError, |
| 3368 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3369 | return NULL; |
| 3370 | #endif |
| 3371 | } |
| 3372 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3373 | static PyObject * |
| 3374 | get_verify_mode(PySSLContext *self, void *c) |
| 3375 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3376 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3377 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3378 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3379 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3380 | case SSL_VERIFY_NONE: |
| 3381 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3382 | case SSL_VERIFY_PEER: |
| 3383 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3384 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3385 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3386 | } |
| 3387 | PyErr_SetString(PySSLErrorObject, |
| 3388 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3389 | return NULL; |
| 3390 | } |
| 3391 | |
| 3392 | static int |
| 3393 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3394 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3395 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3396 | if (!PyArg_Parse(arg, "i", &n)) |
| 3397 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3398 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3399 | PyErr_SetString(PyExc_ValueError, |
| 3400 | "Cannot set verify_mode to CERT_NONE when " |
| 3401 | "check_hostname is enabled."); |
| 3402 | return -1; |
| 3403 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3404 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3408 | get_verify_flags(PySSLContext *self, void *c) |
| 3409 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3410 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3411 | unsigned long flags; |
| 3412 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3413 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3414 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3415 | return PyLong_FromUnsignedLong(flags); |
| 3416 | } |
| 3417 | |
| 3418 | static int |
| 3419 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3420 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3421 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3422 | unsigned long new_flags, flags, set, clear; |
| 3423 | |
| 3424 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3425 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3426 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3427 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3428 | clear = flags & ~new_flags; |
| 3429 | set = ~flags & new_flags; |
| 3430 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3431 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3432 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3433 | return -1; |
| 3434 | } |
| 3435 | } |
| 3436 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3437 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3438 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3439 | return -1; |
| 3440 | } |
| 3441 | } |
| 3442 | return 0; |
| 3443 | } |
| 3444 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3445 | /* Getter and setter for protocol version */ |
| 3446 | #if defined(SSL_CTRL_GET_MAX_PROTO_VERSION) |
| 3447 | |
| 3448 | |
| 3449 | static int |
| 3450 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3451 | { |
| 3452 | long v; |
| 3453 | int result; |
| 3454 | |
| 3455 | if (!PyArg_Parse(arg, "l", &v)) |
| 3456 | return -1; |
| 3457 | if (v > INT_MAX) { |
| 3458 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3459 | return -1; |
| 3460 | } |
| 3461 | |
| 3462 | switch(self->protocol) { |
| 3463 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3464 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3465 | case PY_SSL_VERSION_TLS: |
| 3466 | break; |
| 3467 | default: |
| 3468 | PyErr_SetString( |
| 3469 | PyExc_ValueError, |
| 3470 | "The context's protocol doesn't support modification of " |
| 3471 | "highest and lowest version." |
| 3472 | ); |
| 3473 | return -1; |
| 3474 | } |
| 3475 | |
| 3476 | if (what == 0) { |
| 3477 | switch(v) { |
| 3478 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3479 | v = 0; |
| 3480 | break; |
| 3481 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3482 | /* Emulate max for set_min_proto_version */ |
| 3483 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3484 | break; |
| 3485 | default: |
| 3486 | break; |
| 3487 | } |
| 3488 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3489 | } |
| 3490 | else { |
| 3491 | switch(v) { |
| 3492 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3493 | v = 0; |
| 3494 | break; |
| 3495 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3496 | /* Emulate max for set_min_proto_version */ |
| 3497 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3498 | break; |
| 3499 | default: |
| 3500 | break; |
| 3501 | } |
| 3502 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3503 | } |
| 3504 | if (result == 0) { |
| 3505 | PyErr_Format(PyExc_ValueError, |
| 3506 | "Unsupported protocol version 0x%x", v); |
| 3507 | return -1; |
| 3508 | } |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
| 3512 | static PyObject * |
| 3513 | get_minimum_version(PySSLContext *self, void *c) |
| 3514 | { |
| 3515 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3516 | if (v == 0) { |
| 3517 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3518 | } |
| 3519 | return PyLong_FromLong(v); |
| 3520 | } |
| 3521 | |
| 3522 | static int |
| 3523 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3524 | { |
| 3525 | return set_min_max_proto_version(self, arg, 0); |
| 3526 | } |
| 3527 | |
| 3528 | static PyObject * |
| 3529 | get_maximum_version(PySSLContext *self, void *c) |
| 3530 | { |
| 3531 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3532 | if (v == 0) { |
| 3533 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3534 | } |
| 3535 | return PyLong_FromLong(v); |
| 3536 | } |
| 3537 | |
| 3538 | static int |
| 3539 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3540 | { |
| 3541 | return set_min_max_proto_version(self, arg, 1); |
| 3542 | } |
| 3543 | #endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */ |
| 3544 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3545 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3546 | get_options(PySSLContext *self, void *c) |
| 3547 | { |
| 3548 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3549 | } |
| 3550 | |
| 3551 | static int |
| 3552 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3553 | { |
| 3554 | long new_opts, opts, set, clear; |
| 3555 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3556 | return -1; |
| 3557 | opts = SSL_CTX_get_options(self->ctx); |
| 3558 | clear = opts & ~new_opts; |
| 3559 | set = ~opts & new_opts; |
| 3560 | if (clear) { |
| 3561 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3562 | SSL_CTX_clear_options(self->ctx, clear); |
| 3563 | #else |
| 3564 | PyErr_SetString(PyExc_ValueError, |
| 3565 | "can't clear options before OpenSSL 0.9.8m"); |
| 3566 | return -1; |
| 3567 | #endif |
| 3568 | } |
| 3569 | if (set) |
| 3570 | SSL_CTX_set_options(self->ctx, set); |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3574 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3575 | get_host_flags(PySSLContext *self, void *c) |
| 3576 | { |
| 3577 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3578 | } |
| 3579 | |
| 3580 | static int |
| 3581 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3582 | { |
| 3583 | X509_VERIFY_PARAM *param; |
| 3584 | unsigned int new_flags = 0; |
| 3585 | |
| 3586 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3587 | return -1; |
| 3588 | |
| 3589 | param = SSL_CTX_get0_param(self->ctx); |
| 3590 | self->hostflags = new_flags; |
| 3591 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3592 | return 0; |
| 3593 | } |
| 3594 | |
| 3595 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3596 | get_check_hostname(PySSLContext *self, void *c) |
| 3597 | { |
| 3598 | return PyBool_FromLong(self->check_hostname); |
| 3599 | } |
| 3600 | |
| 3601 | static int |
| 3602 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3603 | { |
| 3604 | int check_hostname; |
| 3605 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3606 | return -1; |
| 3607 | if (check_hostname && |
| 3608 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3609 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3610 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3611 | return -1; |
| 3612 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3613 | } |
| 3614 | self->check_hostname = check_hostname; |
| 3615 | return 0; |
| 3616 | } |
| 3617 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3618 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3619 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3620 | #if TLS1_3_VERSION |
| 3621 | return PyBool_FromLong(self->post_handshake_auth); |
| 3622 | #else |
| 3623 | Py_RETURN_NONE; |
| 3624 | #endif |
| 3625 | } |
| 3626 | |
| 3627 | #if TLS1_3_VERSION |
| 3628 | static int |
| 3629 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
| 3630 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 3631 | int mode = SSL_CTX_get_verify_mode(self->ctx); |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3632 | if (arg == NULL) { |
| 3633 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3634 | return -1; |
| 3635 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3636 | int pha = PyObject_IsTrue(arg); |
| 3637 | |
| 3638 | if (pha == -1) { |
| 3639 | return -1; |
| 3640 | } |
| 3641 | self->post_handshake_auth = pha; |
| 3642 | |
| 3643 | /* client-side socket setting, ignored by server-side */ |
| 3644 | SSL_CTX_set_post_handshake_auth(self->ctx, pha); |
| 3645 | |
| 3646 | /* server-side socket setting, ignored by client-side */ |
| 3647 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 3648 | if (pha) { |
| 3649 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 3650 | } else { |
| 3651 | mode ^= SSL_VERIFY_POST_HANDSHAKE; |
| 3652 | } |
| 3653 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
| 3654 | |
| 3655 | return 0; |
| 3656 | } |
| 3657 | #endif |
| 3658 | |
| 3659 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3660 | get_protocol(PySSLContext *self, void *c) { |
| 3661 | return PyLong_FromLong(self->protocol); |
| 3662 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3663 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3664 | typedef struct { |
| 3665 | PyThreadState *thread_state; |
| 3666 | PyObject *callable; |
| 3667 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3668 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3669 | int error; |
| 3670 | } _PySSLPasswordInfo; |
| 3671 | |
| 3672 | static int |
| 3673 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3674 | const char *bad_type_error) |
| 3675 | { |
| 3676 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3677 | from a unicode, bytes, or byte array object. |
| 3678 | The password field will be dynamically allocated and must be freed |
| 3679 | by the caller */ |
| 3680 | PyObject *password_bytes = NULL; |
| 3681 | const char *data = NULL; |
| 3682 | Py_ssize_t size; |
| 3683 | |
| 3684 | if (PyUnicode_Check(password)) { |
| 3685 | password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL); |
| 3686 | if (!password_bytes) { |
| 3687 | goto error; |
| 3688 | } |
| 3689 | data = PyBytes_AS_STRING(password_bytes); |
| 3690 | size = PyBytes_GET_SIZE(password_bytes); |
| 3691 | } else if (PyBytes_Check(password)) { |
| 3692 | data = PyBytes_AS_STRING(password); |
| 3693 | size = PyBytes_GET_SIZE(password); |
| 3694 | } else if (PyByteArray_Check(password)) { |
| 3695 | data = PyByteArray_AS_STRING(password); |
| 3696 | size = PyByteArray_GET_SIZE(password); |
| 3697 | } else { |
| 3698 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3699 | goto error; |
| 3700 | } |
| 3701 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3702 | if (size > (Py_ssize_t)INT_MAX) { |
| 3703 | PyErr_Format(PyExc_ValueError, |
| 3704 | "password cannot be longer than %d bytes", INT_MAX); |
| 3705 | goto error; |
| 3706 | } |
| 3707 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3708 | PyMem_Free(pw_info->password); |
| 3709 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3710 | if (!pw_info->password) { |
| 3711 | PyErr_SetString(PyExc_MemoryError, |
| 3712 | "unable to allocate password buffer"); |
| 3713 | goto error; |
| 3714 | } |
| 3715 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3716 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3717 | |
| 3718 | Py_XDECREF(password_bytes); |
| 3719 | return 1; |
| 3720 | |
| 3721 | error: |
| 3722 | Py_XDECREF(password_bytes); |
| 3723 | return 0; |
| 3724 | } |
| 3725 | |
| 3726 | static int |
| 3727 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3728 | { |
| 3729 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3730 | PyObject *fn_ret = NULL; |
| 3731 | |
| 3732 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3733 | |
| 3734 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3735 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3736 | if (!fn_ret) { |
| 3737 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3738 | core python API, so we could use it to add a frame here */ |
| 3739 | goto error; |
| 3740 | } |
| 3741 | |
| 3742 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3743 | "password callback must return a string")) { |
| 3744 | goto error; |
| 3745 | } |
| 3746 | Py_CLEAR(fn_ret); |
| 3747 | } |
| 3748 | |
| 3749 | if (pw_info->size > size) { |
| 3750 | PyErr_Format(PyExc_ValueError, |
| 3751 | "password cannot be longer than %d bytes", size); |
| 3752 | goto error; |
| 3753 | } |
| 3754 | |
| 3755 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3756 | memcpy(buf, pw_info->password, pw_info->size); |
| 3757 | return pw_info->size; |
| 3758 | |
| 3759 | error: |
| 3760 | Py_XDECREF(fn_ret); |
| 3761 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3762 | pw_info->error = 1; |
| 3763 | return -1; |
| 3764 | } |
| 3765 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3766 | /*[clinic input] |
| 3767 | _ssl._SSLContext.load_cert_chain |
| 3768 | certfile: object |
| 3769 | keyfile: object = NULL |
| 3770 | password: object = NULL |
| 3771 | |
| 3772 | [clinic start generated code]*/ |
| 3773 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3774 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3775 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3776 | PyObject *keyfile, PyObject *password) |
| 3777 | /*[clinic end generated code: output=9480bc1c380e2095 input=7cf9ac673cbee6fc]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3778 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3779 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3780 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3781 | 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] | 3782 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3783 | int r; |
| 3784 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3785 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3786 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3787 | if (keyfile == Py_None) |
| 3788 | keyfile = NULL; |
| 3789 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
| 3790 | PyErr_SetString(PyExc_TypeError, |
| 3791 | "certfile should be a valid filesystem path"); |
| 3792 | return NULL; |
| 3793 | } |
| 3794 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
| 3795 | PyErr_SetString(PyExc_TypeError, |
| 3796 | "keyfile should be a valid filesystem path"); |
| 3797 | goto error; |
| 3798 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3799 | if (password && password != Py_None) { |
| 3800 | if (PyCallable_Check(password)) { |
| 3801 | pw_info.callable = password; |
| 3802 | } else if (!_pwinfo_set(&pw_info, password, |
| 3803 | "password should be a string or callable")) { |
| 3804 | goto error; |
| 3805 | } |
| 3806 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3807 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3808 | } |
| 3809 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3810 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3811 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3812 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3813 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3814 | if (pw_info.error) { |
| 3815 | ERR_clear_error(); |
| 3816 | /* the password callback has already set the error information */ |
| 3817 | } |
| 3818 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3819 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3820 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3821 | } |
| 3822 | else { |
| 3823 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3824 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3825 | goto error; |
| 3826 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3827 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3828 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3829 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 3830 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3831 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 3832 | Py_CLEAR(keyfile_bytes); |
| 3833 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3834 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3835 | if (pw_info.error) { |
| 3836 | ERR_clear_error(); |
| 3837 | /* the password callback has already set the error information */ |
| 3838 | } |
| 3839 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3840 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3841 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3842 | } |
| 3843 | else { |
| 3844 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3845 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3846 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3847 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3848 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3849 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3850 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3851 | if (r != 1) { |
| 3852 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3853 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3854 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3855 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3856 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3857 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3858 | Py_RETURN_NONE; |
| 3859 | |
| 3860 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3861 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3862 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3863 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3864 | Py_XDECREF(keyfile_bytes); |
| 3865 | Py_XDECREF(certfile_bytes); |
| 3866 | return NULL; |
| 3867 | } |
| 3868 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3869 | /* internal helper function, returns -1 on error |
| 3870 | */ |
| 3871 | static int |
| 3872 | _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len, |
| 3873 | int filetype) |
| 3874 | { |
| 3875 | BIO *biobuf = NULL; |
| 3876 | X509_STORE *store; |
| 3877 | int retval = 0, err, loaded = 0; |
| 3878 | |
| 3879 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 3880 | |
| 3881 | if (len <= 0) { |
| 3882 | PyErr_SetString(PyExc_ValueError, |
| 3883 | "Empty certificate data"); |
| 3884 | return -1; |
| 3885 | } else if (len > INT_MAX) { |
| 3886 | PyErr_SetString(PyExc_OverflowError, |
| 3887 | "Certificate data is too long."); |
| 3888 | return -1; |
| 3889 | } |
| 3890 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 3891 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3892 | if (biobuf == NULL) { |
| 3893 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 3894 | return -1; |
| 3895 | } |
| 3896 | |
| 3897 | store = SSL_CTX_get_cert_store(self->ctx); |
| 3898 | assert(store != NULL); |
| 3899 | |
| 3900 | while (1) { |
| 3901 | X509 *cert = NULL; |
| 3902 | int r; |
| 3903 | |
| 3904 | if (filetype == SSL_FILETYPE_ASN1) { |
| 3905 | cert = d2i_X509_bio(biobuf, NULL); |
| 3906 | } else { |
| 3907 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3908 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 3909 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 3910 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3911 | } |
| 3912 | if (cert == NULL) { |
| 3913 | break; |
| 3914 | } |
| 3915 | r = X509_STORE_add_cert(store, cert); |
| 3916 | X509_free(cert); |
| 3917 | if (!r) { |
| 3918 | err = ERR_peek_last_error(); |
| 3919 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 3920 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 3921 | /* cert already in hash table, not an error */ |
| 3922 | ERR_clear_error(); |
| 3923 | } else { |
| 3924 | break; |
| 3925 | } |
| 3926 | } |
| 3927 | loaded++; |
| 3928 | } |
| 3929 | |
| 3930 | err = ERR_peek_last_error(); |
| 3931 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 3932 | (loaded > 0) && |
| 3933 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 3934 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 3935 | /* EOF ASN1 file, not an error */ |
| 3936 | ERR_clear_error(); |
| 3937 | retval = 0; |
| 3938 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 3939 | (loaded > 0) && |
| 3940 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 3941 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3942 | /* EOF PEM file, not an error */ |
| 3943 | ERR_clear_error(); |
| 3944 | retval = 0; |
| 3945 | } else { |
| 3946 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3947 | retval = -1; |
| 3948 | } |
| 3949 | |
| 3950 | BIO_free(biobuf); |
| 3951 | return retval; |
| 3952 | } |
| 3953 | |
| 3954 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3955 | /*[clinic input] |
| 3956 | _ssl._SSLContext.load_verify_locations |
| 3957 | cafile: object = NULL |
| 3958 | capath: object = NULL |
| 3959 | cadata: object = NULL |
| 3960 | |
| 3961 | [clinic start generated code]*/ |
| 3962 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3963 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3964 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 3965 | PyObject *cafile, |
| 3966 | PyObject *capath, |
| 3967 | PyObject *cadata) |
| 3968 | /*[clinic end generated code: output=454c7e41230ca551 input=997f1fb3a784ef88]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3969 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3970 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 3971 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3972 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3973 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3974 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3975 | if (cafile == Py_None) |
| 3976 | cafile = NULL; |
| 3977 | if (capath == Py_None) |
| 3978 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3979 | if (cadata == Py_None) |
| 3980 | cadata = NULL; |
| 3981 | |
| 3982 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3983 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3984 | "cafile, capath and cadata cannot be all omitted"); |
| 3985 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3986 | } |
| 3987 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
| 3988 | PyErr_SetString(PyExc_TypeError, |
| 3989 | "cafile should be a valid filesystem path"); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3990 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3991 | } |
| 3992 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3993 | PyErr_SetString(PyExc_TypeError, |
| 3994 | "capath should be a valid filesystem path"); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3995 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3996 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3997 | |
| 3998 | /* validata cadata type and load cadata */ |
| 3999 | if (cadata) { |
| 4000 | Py_buffer buf; |
| 4001 | PyObject *cadata_ascii = NULL; |
| 4002 | |
| 4003 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE) == 0) { |
| 4004 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4005 | PyBuffer_Release(&buf); |
| 4006 | PyErr_SetString(PyExc_TypeError, |
| 4007 | "cadata should be a contiguous buffer with " |
| 4008 | "a single dimension"); |
| 4009 | goto error; |
| 4010 | } |
| 4011 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4012 | PyBuffer_Release(&buf); |
| 4013 | if (r == -1) { |
| 4014 | goto error; |
| 4015 | } |
| 4016 | } else { |
| 4017 | PyErr_Clear(); |
| 4018 | cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4019 | if (cadata_ascii == NULL) { |
| 4020 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 4021 | "cadata should be an ASCII string or a " |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4022 | "bytes-like object"); |
| 4023 | goto error; |
| 4024 | } |
| 4025 | r = _add_ca_certs(self, |
| 4026 | PyBytes_AS_STRING(cadata_ascii), |
| 4027 | PyBytes_GET_SIZE(cadata_ascii), |
| 4028 | SSL_FILETYPE_PEM); |
| 4029 | Py_DECREF(cadata_ascii); |
| 4030 | if (r == -1) { |
| 4031 | goto error; |
| 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | /* load cafile or capath */ |
| 4037 | if (cafile || capath) { |
| 4038 | if (cafile) |
| 4039 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4040 | if (capath) |
| 4041 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4042 | PySSL_BEGIN_ALLOW_THREADS |
| 4043 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4044 | PySSL_END_ALLOW_THREADS |
| 4045 | if (r != 1) { |
| 4046 | ok = 0; |
| 4047 | if (errno != 0) { |
| 4048 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4049 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4050 | } |
| 4051 | else { |
| 4052 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4053 | } |
| 4054 | goto error; |
| 4055 | } |
| 4056 | } |
| 4057 | goto end; |
| 4058 | |
| 4059 | error: |
| 4060 | ok = 0; |
| 4061 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4062 | Py_XDECREF(cafile_bytes); |
| 4063 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4064 | if (ok) { |
| 4065 | Py_RETURN_NONE; |
| 4066 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4067 | return NULL; |
| 4068 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4071 | /*[clinic input] |
| 4072 | _ssl._SSLContext.load_dh_params |
| 4073 | path as filepath: object |
| 4074 | / |
| 4075 | |
| 4076 | [clinic start generated code]*/ |
| 4077 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4078 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4079 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4080 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4081 | { |
| 4082 | FILE *f; |
| 4083 | DH *dh; |
| 4084 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4085 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4086 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4087 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4088 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4089 | errno = 0; |
| 4090 | PySSL_BEGIN_ALLOW_THREADS |
| 4091 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4092 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4093 | PySSL_END_ALLOW_THREADS |
| 4094 | if (dh == NULL) { |
| 4095 | if (errno != 0) { |
| 4096 | ERR_clear_error(); |
| 4097 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4098 | } |
| 4099 | else { |
| 4100 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4101 | } |
| 4102 | return NULL; |
| 4103 | } |
| 4104 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 4105 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4106 | DH_free(dh); |
| 4107 | Py_RETURN_NONE; |
| 4108 | } |
| 4109 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4110 | /*[clinic input] |
| 4111 | _ssl._SSLContext._wrap_socket |
| 4112 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 4113 | server_side: int |
| 4114 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4115 | * |
| 4116 | owner: object = None |
| 4117 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4118 | |
| 4119 | [clinic start generated code]*/ |
| 4120 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4121 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4122 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4123 | int server_side, PyObject *hostname_obj, |
| 4124 | PyObject *owner, PyObject *session) |
| 4125 | /*[clinic end generated code: output=f103f238633940b4 input=957d5006183d1894]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4126 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4127 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4128 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4129 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4130 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4131 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4132 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4133 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4134 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4135 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4136 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4137 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4138 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4139 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4140 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4141 | if (hostname != NULL) |
| 4142 | PyMem_Free(hostname); |
| 4143 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4146 | /*[clinic input] |
| 4147 | _ssl._SSLContext._wrap_bio |
| 4148 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4149 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4150 | server_side: int |
| 4151 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4152 | * |
| 4153 | owner: object = None |
| 4154 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4155 | |
| 4156 | [clinic start generated code]*/ |
| 4157 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4158 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4159 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4160 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4161 | PyObject *hostname_obj, PyObject *owner, |
| 4162 | PyObject *session) |
| 4163 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=8cf22f4d586ac56a]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4164 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4165 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4166 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4167 | |
| 4168 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4169 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4170 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4171 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4172 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4176 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4177 | incoming, outgoing); |
| 4178 | |
| 4179 | PyMem_Free(hostname); |
| 4180 | return res; |
| 4181 | } |
| 4182 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4183 | /*[clinic input] |
| 4184 | _ssl._SSLContext.session_stats |
| 4185 | [clinic start generated code]*/ |
| 4186 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4187 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4188 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4189 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4190 | { |
| 4191 | int r; |
| 4192 | PyObject *value, *stats = PyDict_New(); |
| 4193 | if (!stats) |
| 4194 | return NULL; |
| 4195 | |
| 4196 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4197 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4198 | if (value == NULL) \ |
| 4199 | goto error; \ |
| 4200 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4201 | Py_DECREF(value); \ |
| 4202 | if (r < 0) \ |
| 4203 | goto error; |
| 4204 | |
| 4205 | ADD_STATS(number, "number"); |
| 4206 | ADD_STATS(connect, "connect"); |
| 4207 | ADD_STATS(connect_good, "connect_good"); |
| 4208 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4209 | ADD_STATS(accept, "accept"); |
| 4210 | ADD_STATS(accept_good, "accept_good"); |
| 4211 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4212 | ADD_STATS(accept, "accept"); |
| 4213 | ADD_STATS(hits, "hits"); |
| 4214 | ADD_STATS(misses, "misses"); |
| 4215 | ADD_STATS(timeouts, "timeouts"); |
| 4216 | ADD_STATS(cache_full, "cache_full"); |
| 4217 | |
| 4218 | #undef ADD_STATS |
| 4219 | |
| 4220 | return stats; |
| 4221 | |
| 4222 | error: |
| 4223 | Py_DECREF(stats); |
| 4224 | return NULL; |
| 4225 | } |
| 4226 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4227 | /*[clinic input] |
| 4228 | _ssl._SSLContext.set_default_verify_paths |
| 4229 | [clinic start generated code]*/ |
| 4230 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4231 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4232 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4233 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4234 | { |
| 4235 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 4236 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4237 | return NULL; |
| 4238 | } |
| 4239 | Py_RETURN_NONE; |
| 4240 | } |
| 4241 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4242 | #ifndef OPENSSL_NO_ECDH |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4243 | /*[clinic input] |
| 4244 | _ssl._SSLContext.set_ecdh_curve |
| 4245 | name: object |
| 4246 | / |
| 4247 | |
| 4248 | [clinic start generated code]*/ |
| 4249 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4250 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4251 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4252 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4253 | { |
| 4254 | PyObject *name_bytes; |
| 4255 | int nid; |
| 4256 | EC_KEY *key; |
| 4257 | |
| 4258 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4259 | return NULL; |
| 4260 | assert(PyBytes_Check(name_bytes)); |
| 4261 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4262 | Py_DECREF(name_bytes); |
| 4263 | if (nid == 0) { |
| 4264 | PyErr_Format(PyExc_ValueError, |
| 4265 | "unknown elliptic curve name %R", name); |
| 4266 | return NULL; |
| 4267 | } |
| 4268 | key = EC_KEY_new_by_curve_name(nid); |
| 4269 | if (key == NULL) { |
| 4270 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 4271 | return NULL; |
| 4272 | } |
| 4273 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4274 | EC_KEY_free(key); |
| 4275 | Py_RETURN_NONE; |
| 4276 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 4277 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4278 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4279 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4280 | static int |
| 4281 | _servername_callback(SSL *s, int *al, void *args) |
| 4282 | { |
| 4283 | int ret; |
| 4284 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 4285 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4286 | PyObject *result; |
| 4287 | /* The high-level ssl.SSLSocket object */ |
| 4288 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4289 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4290 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4291 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4292 | if (ssl_ctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4293 | /* remove race condition in this the call back while if removing the |
| 4294 | * callback is in progress */ |
| 4295 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4296 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | ssl = SSL_get_app_data(s); |
| 4300 | assert(PySSLSocket_Check(ssl)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4301 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4302 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4303 | * SSL connection and that has a .context attribute that can be changed to |
| 4304 | * identify the requested hostname. Since the official API is the Python |
| 4305 | * level API we want to pass the callback a Python level object rather than |
| 4306 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4307 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4308 | * will be passed. If both do not exist only then the C-level object is |
| 4309 | * passed. */ |
| 4310 | if (ssl->owner) |
| 4311 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4312 | else if (ssl->Socket) |
| 4313 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4314 | else |
| 4315 | ssl_socket = (PyObject *) ssl; |
| 4316 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4317 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4318 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4319 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4320 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4321 | if (servername == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4322 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_sni_cb, ssl_socket, |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4323 | Py_None, ssl_ctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4324 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4325 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4326 | PyObject *servername_bytes; |
| 4327 | PyObject *servername_str; |
| 4328 | |
| 4329 | servername_bytes = PyBytes_FromString(servername); |
| 4330 | if (servername_bytes == NULL) { |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4331 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 4332 | goto error; |
| 4333 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4334 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4335 | * back into a str object, but still as an A-label (bpo-28414) |
| 4336 | */ |
| 4337 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
| 4338 | Py_DECREF(servername_bytes); |
| 4339 | if (servername_str == NULL) { |
| 4340 | PyErr_WriteUnraisable(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4341 | goto error; |
| 4342 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4343 | result = PyObject_CallFunctionObjArgs( |
| 4344 | ssl_ctx->set_sni_cb, ssl_socket, servername_str, |
| 4345 | ssl_ctx, NULL); |
| 4346 | Py_DECREF(servername_str); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4347 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4348 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4349 | |
| 4350 | if (result == NULL) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4351 | PyErr_WriteUnraisable(ssl_ctx->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4352 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4353 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4354 | } |
| 4355 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4356 | /* Result may be None, a SSLContext or an integer |
| 4357 | * None and SSLContext are OK, integer or other values are an error. |
| 4358 | */ |
| 4359 | if (result == Py_None) { |
| 4360 | ret = SSL_TLSEXT_ERR_OK; |
| 4361 | } else { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4362 | *al = (int) PyLong_AsLong(result); |
| 4363 | if (PyErr_Occurred()) { |
| 4364 | PyErr_WriteUnraisable(result); |
| 4365 | *al = SSL_AD_INTERNAL_ERROR; |
| 4366 | } |
| 4367 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4368 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4369 | Py_DECREF(result); |
| 4370 | } |
| 4371 | |
| 4372 | PyGILState_Release(gstate); |
| 4373 | return ret; |
| 4374 | |
| 4375 | error: |
| 4376 | Py_DECREF(ssl_socket); |
| 4377 | *al = SSL_AD_INTERNAL_ERROR; |
| 4378 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4379 | PyGILState_Release(gstate); |
| 4380 | return ret; |
| 4381 | } |
Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 4382 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4383 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4384 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4385 | get_sni_callback(PySSLContext *self, void *c) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4386 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4387 | PyObject *cb = self->set_sni_cb; |
| 4388 | if (cb == NULL) { |
| 4389 | Py_RETURN_NONE; |
| 4390 | } |
| 4391 | Py_INCREF(cb); |
| 4392 | return cb; |
| 4393 | } |
| 4394 | |
| 4395 | static int |
| 4396 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4397 | { |
| 4398 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4399 | PyErr_SetString(PyExc_ValueError, |
| 4400 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4401 | return -1; |
| 4402 | } |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 4403 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4404 | Py_CLEAR(self->set_sni_cb); |
| 4405 | if (arg == Py_None) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4406 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4407 | } |
| 4408 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4409 | if (!PyCallable_Check(arg)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4410 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4411 | PyErr_SetString(PyExc_TypeError, |
| 4412 | "not a callable object"); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4413 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4414 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4415 | Py_INCREF(arg); |
| 4416 | self->set_sni_cb = arg; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4417 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4418 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4419 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4420 | return 0; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4421 | #else |
| 4422 | PyErr_SetString(PyExc_NotImplementedError, |
| 4423 | "The TLS extension servername callback, " |
| 4424 | "SSL_CTX_set_tlsext_servername_callback, " |
| 4425 | "is not in the current OpenSSL library."); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4426 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4427 | #endif |
| 4428 | } |
| 4429 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4430 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4431 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4432 | \n\ |
| 4433 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4434 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4435 | See RFC 6066 for details of the SNI extension."); |
| 4436 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4437 | /*[clinic input] |
| 4438 | _ssl._SSLContext.cert_store_stats |
| 4439 | |
| 4440 | Returns quantities of loaded X.509 certificates. |
| 4441 | |
| 4442 | X.509 certificates with a CA extension and certificate revocation lists |
| 4443 | inside the context's cert store. |
| 4444 | |
| 4445 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4446 | been used at least once. |
| 4447 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4448 | |
| 4449 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4450 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4451 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4452 | { |
| 4453 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4454 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4455 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4456 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4457 | |
| 4458 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4459 | objs = X509_STORE_get0_objects(store); |
| 4460 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4461 | obj = sk_X509_OBJECT_value(objs, i); |
| 4462 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4463 | case X509_LU_X509: |
| 4464 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4465 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4466 | ca++; |
| 4467 | } |
| 4468 | break; |
| 4469 | case X509_LU_CRL: |
| 4470 | crl++; |
| 4471 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4472 | default: |
| 4473 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4474 | * As far as I can tell they are internal states and never |
| 4475 | * stored in a cert store */ |
| 4476 | break; |
| 4477 | } |
| 4478 | } |
| 4479 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4480 | "x509_ca", ca); |
| 4481 | } |
| 4482 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4483 | /*[clinic input] |
| 4484 | _ssl._SSLContext.get_ca_certs |
| 4485 | binary_form: bool = False |
| 4486 | |
| 4487 | Returns a list of dicts with information of loaded CA certs. |
| 4488 | |
| 4489 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4490 | certificate. |
| 4491 | |
| 4492 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4493 | been used at least once. |
| 4494 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4495 | |
| 4496 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4497 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4498 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4499 | { |
| 4500 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4501 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4502 | PyObject *ci = NULL, *rlist = NULL; |
| 4503 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4504 | |
| 4505 | if ((rlist = PyList_New(0)) == NULL) { |
| 4506 | return NULL; |
| 4507 | } |
| 4508 | |
| 4509 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4510 | objs = X509_STORE_get0_objects(store); |
| 4511 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4512 | X509_OBJECT *obj; |
| 4513 | X509 *cert; |
| 4514 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4515 | obj = sk_X509_OBJECT_value(objs, i); |
| 4516 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4517 | /* not a x509 cert */ |
| 4518 | continue; |
| 4519 | } |
| 4520 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4521 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4522 | if (!X509_check_ca(cert)) { |
| 4523 | continue; |
| 4524 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4525 | if (binary_form) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4526 | ci = _certificate_to_der(cert); |
| 4527 | } else { |
| 4528 | ci = _decode_certificate(cert); |
| 4529 | } |
| 4530 | if (ci == NULL) { |
| 4531 | goto error; |
| 4532 | } |
| 4533 | if (PyList_Append(rlist, ci) == -1) { |
| 4534 | goto error; |
| 4535 | } |
| 4536 | Py_CLEAR(ci); |
| 4537 | } |
| 4538 | return rlist; |
| 4539 | |
| 4540 | error: |
| 4541 | Py_XDECREF(ci); |
| 4542 | Py_XDECREF(rlist); |
| 4543 | return NULL; |
| 4544 | } |
| 4545 | |
| 4546 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4547 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4548 | {"check_hostname", (getter) get_check_hostname, |
| 4549 | (setter) set_check_hostname, NULL}, |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4550 | {"_host_flags", (getter) get_host_flags, |
| 4551 | (setter) set_host_flags, NULL}, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4552 | #if SSL_CTRL_GET_MAX_PROTO_VERSION |
| 4553 | {"minimum_version", (getter) get_minimum_version, |
| 4554 | (setter) set_minimum_version, NULL}, |
| 4555 | {"maximum_version", (getter) get_maximum_version, |
| 4556 | (setter) set_maximum_version, NULL}, |
| 4557 | #endif |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4558 | {"sni_callback", (getter) get_sni_callback, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4559 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4560 | {"options", (getter) get_options, |
| 4561 | (setter) set_options, NULL}, |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4562 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4563 | #ifdef TLS1_3_VERSION |
| 4564 | (setter) set_post_handshake_auth, |
| 4565 | #else |
| 4566 | NULL, |
| 4567 | #endif |
| 4568 | NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4569 | {"protocol", (getter) get_protocol, |
| 4570 | NULL, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4571 | {"verify_flags", (getter) get_verify_flags, |
| 4572 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4573 | {"verify_mode", (getter) get_verify_mode, |
| 4574 | (setter) set_verify_mode, NULL}, |
| 4575 | {NULL}, /* sentinel */ |
| 4576 | }; |
| 4577 | |
| 4578 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4579 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4580 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4581 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4582 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4583 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4584 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4585 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4586 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4587 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4588 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4589 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4590 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4591 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4592 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4593 | {NULL, NULL} /* sentinel */ |
| 4594 | }; |
| 4595 | |
| 4596 | static PyTypeObject PySSLContext_Type = { |
| 4597 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4598 | "_ssl._SSLContext", /*tp_name*/ |
| 4599 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 4600 | 0, /*tp_itemsize*/ |
| 4601 | (destructor)context_dealloc, /*tp_dealloc*/ |
| 4602 | 0, /*tp_print*/ |
| 4603 | 0, /*tp_getattr*/ |
| 4604 | 0, /*tp_setattr*/ |
| 4605 | 0, /*tp_reserved*/ |
| 4606 | 0, /*tp_repr*/ |
| 4607 | 0, /*tp_as_number*/ |
| 4608 | 0, /*tp_as_sequence*/ |
| 4609 | 0, /*tp_as_mapping*/ |
| 4610 | 0, /*tp_hash*/ |
| 4611 | 0, /*tp_call*/ |
| 4612 | 0, /*tp_str*/ |
| 4613 | 0, /*tp_getattro*/ |
| 4614 | 0, /*tp_setattro*/ |
| 4615 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4616 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4617 | 0, /*tp_doc*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4618 | (traverseproc) context_traverse, /*tp_traverse*/ |
| 4619 | (inquiry) context_clear, /*tp_clear*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4620 | 0, /*tp_richcompare*/ |
| 4621 | 0, /*tp_weaklistoffset*/ |
| 4622 | 0, /*tp_iter*/ |
| 4623 | 0, /*tp_iternext*/ |
| 4624 | context_methods, /*tp_methods*/ |
| 4625 | 0, /*tp_members*/ |
| 4626 | context_getsetlist, /*tp_getset*/ |
| 4627 | 0, /*tp_base*/ |
| 4628 | 0, /*tp_dict*/ |
| 4629 | 0, /*tp_descr_get*/ |
| 4630 | 0, /*tp_descr_set*/ |
| 4631 | 0, /*tp_dictoffset*/ |
| 4632 | 0, /*tp_init*/ |
| 4633 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4634 | _ssl__SSLContext, /*tp_new*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4635 | }; |
| 4636 | |
| 4637 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4638 | /* |
| 4639 | * MemoryBIO objects |
| 4640 | */ |
| 4641 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4642 | /*[clinic input] |
| 4643 | @classmethod |
| 4644 | _ssl.MemoryBIO.__new__ |
| 4645 | |
| 4646 | [clinic start generated code]*/ |
| 4647 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4648 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4649 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4650 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4651 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4652 | BIO *bio; |
| 4653 | PySSLMemoryBIO *self; |
| 4654 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4655 | bio = BIO_new(BIO_s_mem()); |
| 4656 | if (bio == NULL) { |
| 4657 | PyErr_SetString(PySSLErrorObject, |
| 4658 | "failed to allocate BIO"); |
| 4659 | return NULL; |
| 4660 | } |
| 4661 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4662 | * just that no data is currently available. The SSL routines should retry |
| 4663 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4664 | BIO_set_retry_read(bio); |
| 4665 | BIO_set_mem_eof_return(bio, -1); |
| 4666 | |
| 4667 | assert(type != NULL && type->tp_alloc != NULL); |
| 4668 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4669 | if (self == NULL) { |
| 4670 | BIO_free(bio); |
| 4671 | return NULL; |
| 4672 | } |
| 4673 | self->bio = bio; |
| 4674 | self->eof_written = 0; |
| 4675 | |
| 4676 | return (PyObject *) self; |
| 4677 | } |
| 4678 | |
| 4679 | static void |
| 4680 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4681 | { |
| 4682 | BIO_free(self->bio); |
| 4683 | Py_TYPE(self)->tp_free(self); |
| 4684 | } |
| 4685 | |
| 4686 | static PyObject * |
| 4687 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4688 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4689 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4693 | "The number of bytes pending in the memory BIO."); |
| 4694 | |
| 4695 | static PyObject * |
| 4696 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4697 | { |
| 4698 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4699 | && self->eof_written); |
| 4700 | } |
| 4701 | |
| 4702 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4703 | "Whether the memory BIO is at EOF."); |
| 4704 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4705 | /*[clinic input] |
| 4706 | _ssl.MemoryBIO.read |
| 4707 | size as len: int = -1 |
| 4708 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4709 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4710 | Read up to size bytes from the memory BIO. |
| 4711 | |
| 4712 | If size is not specified, read the entire buffer. |
| 4713 | If the return value is an empty bytes instance, this means either |
| 4714 | EOF or that no data is available. Use the "eof" property to |
| 4715 | distinguish between the two. |
| 4716 | [clinic start generated code]*/ |
| 4717 | |
| 4718 | static PyObject * |
| 4719 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4720 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4721 | { |
| 4722 | int avail, nbytes; |
| 4723 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4724 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4725 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4726 | if ((len < 0) || (len > avail)) |
| 4727 | len = avail; |
| 4728 | |
| 4729 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4730 | if ((result == NULL) || (len == 0)) |
| 4731 | return result; |
| 4732 | |
| 4733 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4734 | if (nbytes < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4735 | Py_DECREF(result); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4736 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4737 | return NULL; |
| 4738 | } |
| 4739 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4740 | /* There should never be any short reads but check anyway. */ |
| 4741 | if (nbytes < len) { |
| 4742 | _PyBytes_Resize(&result, nbytes); |
| 4743 | } |
| 4744 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4745 | return result; |
| 4746 | } |
| 4747 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4748 | /*[clinic input] |
| 4749 | _ssl.MemoryBIO.write |
| 4750 | b: Py_buffer |
| 4751 | / |
| 4752 | |
| 4753 | Writes the bytes b into the memory BIO. |
| 4754 | |
| 4755 | Returns the number of bytes written. |
| 4756 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4757 | |
| 4758 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4759 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4760 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4761 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4762 | int nbytes; |
| 4763 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4764 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4765 | PyErr_Format(PyExc_OverflowError, |
| 4766 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4767 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | if (self->eof_written) { |
| 4771 | PyErr_SetString(PySSLErrorObject, |
| 4772 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4773 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4776 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4777 | if (nbytes < 0) { |
| 4778 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4779 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4780 | } |
| 4781 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4782 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4783 | } |
| 4784 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4785 | /*[clinic input] |
| 4786 | _ssl.MemoryBIO.write_eof |
| 4787 | |
| 4788 | Write an EOF marker to the memory BIO. |
| 4789 | |
| 4790 | When all data has been read, the "eof" property will be True. |
| 4791 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4792 | |
| 4793 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4794 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4795 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4796 | { |
| 4797 | self->eof_written = 1; |
| 4798 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4799 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4800 | BIO_clear_retry_flags(self->bio); |
| 4801 | BIO_set_mem_eof_return(self->bio, 0); |
| 4802 | |
| 4803 | Py_RETURN_NONE; |
| 4804 | } |
| 4805 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4806 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4807 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4808 | PySSL_memory_bio_pending_doc}, |
| 4809 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 4810 | PySSL_memory_bio_eof_doc}, |
| 4811 | {NULL}, /* sentinel */ |
| 4812 | }; |
| 4813 | |
| 4814 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4815 | _SSL_MEMORYBIO_READ_METHODDEF |
| 4816 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 4817 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4818 | {NULL, NULL} /* sentinel */ |
| 4819 | }; |
| 4820 | |
| 4821 | static PyTypeObject PySSLMemoryBIO_Type = { |
| 4822 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4823 | "_ssl.MemoryBIO", /*tp_name*/ |
| 4824 | sizeof(PySSLMemoryBIO), /*tp_basicsize*/ |
| 4825 | 0, /*tp_itemsize*/ |
| 4826 | (destructor)memory_bio_dealloc, /*tp_dealloc*/ |
| 4827 | 0, /*tp_print*/ |
| 4828 | 0, /*tp_getattr*/ |
| 4829 | 0, /*tp_setattr*/ |
| 4830 | 0, /*tp_reserved*/ |
| 4831 | 0, /*tp_repr*/ |
| 4832 | 0, /*tp_as_number*/ |
| 4833 | 0, /*tp_as_sequence*/ |
| 4834 | 0, /*tp_as_mapping*/ |
| 4835 | 0, /*tp_hash*/ |
| 4836 | 0, /*tp_call*/ |
| 4837 | 0, /*tp_str*/ |
| 4838 | 0, /*tp_getattro*/ |
| 4839 | 0, /*tp_setattro*/ |
| 4840 | 0, /*tp_as_buffer*/ |
| 4841 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4842 | 0, /*tp_doc*/ |
| 4843 | 0, /*tp_traverse*/ |
| 4844 | 0, /*tp_clear*/ |
| 4845 | 0, /*tp_richcompare*/ |
| 4846 | 0, /*tp_weaklistoffset*/ |
| 4847 | 0, /*tp_iter*/ |
| 4848 | 0, /*tp_iternext*/ |
| 4849 | memory_bio_methods, /*tp_methods*/ |
| 4850 | 0, /*tp_members*/ |
| 4851 | memory_bio_getsetlist, /*tp_getset*/ |
| 4852 | 0, /*tp_base*/ |
| 4853 | 0, /*tp_dict*/ |
| 4854 | 0, /*tp_descr_get*/ |
| 4855 | 0, /*tp_descr_set*/ |
| 4856 | 0, /*tp_dictoffset*/ |
| 4857 | 0, /*tp_init*/ |
| 4858 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4859 | _ssl_MemoryBIO, /*tp_new*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4860 | }; |
| 4861 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4862 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4863 | /* |
| 4864 | * SSL Session object |
| 4865 | */ |
| 4866 | |
| 4867 | static void |
| 4868 | PySSLSession_dealloc(PySSLSession *self) |
| 4869 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 4870 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4871 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4872 | Py_XDECREF(self->ctx); |
| 4873 | if (self->session != NULL) { |
| 4874 | SSL_SESSION_free(self->session); |
| 4875 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4876 | PyObject_GC_Del(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static PyObject * |
| 4880 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 4881 | { |
| 4882 | int result; |
| 4883 | |
| 4884 | if (left == NULL || right == NULL) { |
| 4885 | PyErr_BadInternalCall(); |
| 4886 | return NULL; |
| 4887 | } |
| 4888 | |
| 4889 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 4890 | Py_RETURN_NOTIMPLEMENTED; |
| 4891 | } |
| 4892 | |
| 4893 | if (left == right) { |
| 4894 | result = 0; |
| 4895 | } else { |
| 4896 | const unsigned char *left_id, *right_id; |
| 4897 | unsigned int left_len, right_len; |
| 4898 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 4899 | &left_len); |
| 4900 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 4901 | &right_len); |
| 4902 | if (left_len == right_len) { |
| 4903 | result = memcmp(left_id, right_id, left_len); |
| 4904 | } else { |
| 4905 | result = 1; |
| 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | switch (op) { |
| 4910 | case Py_EQ: |
| 4911 | if (result == 0) { |
| 4912 | Py_RETURN_TRUE; |
| 4913 | } else { |
| 4914 | Py_RETURN_FALSE; |
| 4915 | } |
| 4916 | break; |
| 4917 | case Py_NE: |
| 4918 | if (result != 0) { |
| 4919 | Py_RETURN_TRUE; |
| 4920 | } else { |
| 4921 | Py_RETURN_FALSE; |
| 4922 | } |
| 4923 | break; |
| 4924 | case Py_LT: |
| 4925 | case Py_LE: |
| 4926 | case Py_GT: |
| 4927 | case Py_GE: |
| 4928 | Py_RETURN_NOTIMPLEMENTED; |
| 4929 | break; |
| 4930 | default: |
| 4931 | PyErr_BadArgument(); |
| 4932 | return NULL; |
| 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | static int |
| 4937 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 4938 | { |
| 4939 | Py_VISIT(self->ctx); |
| 4940 | return 0; |
| 4941 | } |
| 4942 | |
| 4943 | static int |
| 4944 | PySSLSession_clear(PySSLSession *self) |
| 4945 | { |
| 4946 | Py_CLEAR(self->ctx); |
| 4947 | return 0; |
| 4948 | } |
| 4949 | |
| 4950 | |
| 4951 | static PyObject * |
| 4952 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 4953 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 4954 | } |
| 4955 | |
| 4956 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 4957 | "Session creation time (seconds since epoch)."); |
| 4958 | |
| 4959 | |
| 4960 | static PyObject * |
| 4961 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 4962 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 4963 | } |
| 4964 | |
| 4965 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 4966 | "Session timeout (delta in seconds)."); |
| 4967 | |
| 4968 | |
| 4969 | static PyObject * |
| 4970 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 4971 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 4972 | return PyLong_FromUnsignedLong(hint); |
| 4973 | } |
| 4974 | |
| 4975 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 4976 | "Ticket life time hint."); |
| 4977 | |
| 4978 | |
| 4979 | static PyObject * |
| 4980 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 4981 | const unsigned char *id; |
| 4982 | unsigned int len; |
| 4983 | id = SSL_SESSION_get_id(self->session, &len); |
| 4984 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 4985 | } |
| 4986 | |
| 4987 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 4988 | "Session id"); |
| 4989 | |
| 4990 | |
| 4991 | static PyObject * |
| 4992 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 4993 | if (SSL_SESSION_has_ticket(self->session)) { |
| 4994 | Py_RETURN_TRUE; |
| 4995 | } else { |
| 4996 | Py_RETURN_FALSE; |
| 4997 | } |
| 4998 | } |
| 4999 | |
| 5000 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5001 | "Does the session contain a ticket?"); |
| 5002 | |
| 5003 | |
| 5004 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5005 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5006 | PySSLSession_get_has_ticket_doc}, |
| 5007 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5008 | PySSLSession_get_session_id_doc}, |
| 5009 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5010 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5011 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5012 | PySSLSession_get_time_doc}, |
| 5013 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5014 | PySSLSession_get_timeout_doc}, |
| 5015 | {NULL}, /* sentinel */ |
| 5016 | }; |
| 5017 | |
| 5018 | static PyTypeObject PySSLSession_Type = { |
| 5019 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5020 | "_ssl.Session", /*tp_name*/ |
| 5021 | sizeof(PySSLSession), /*tp_basicsize*/ |
| 5022 | 0, /*tp_itemsize*/ |
| 5023 | (destructor)PySSLSession_dealloc, /*tp_dealloc*/ |
| 5024 | 0, /*tp_print*/ |
| 5025 | 0, /*tp_getattr*/ |
| 5026 | 0, /*tp_setattr*/ |
| 5027 | 0, /*tp_reserved*/ |
| 5028 | 0, /*tp_repr*/ |
| 5029 | 0, /*tp_as_number*/ |
| 5030 | 0, /*tp_as_sequence*/ |
| 5031 | 0, /*tp_as_mapping*/ |
| 5032 | 0, /*tp_hash*/ |
| 5033 | 0, /*tp_call*/ |
| 5034 | 0, /*tp_str*/ |
| 5035 | 0, /*tp_getattro*/ |
| 5036 | 0, /*tp_setattro*/ |
| 5037 | 0, /*tp_as_buffer*/ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5038 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5039 | 0, /*tp_doc*/ |
| 5040 | (traverseproc)PySSLSession_traverse, /*tp_traverse*/ |
| 5041 | (inquiry)PySSLSession_clear, /*tp_clear*/ |
| 5042 | PySSLSession_richcompare, /*tp_richcompare*/ |
| 5043 | 0, /*tp_weaklistoffset*/ |
| 5044 | 0, /*tp_iter*/ |
| 5045 | 0, /*tp_iternext*/ |
| 5046 | 0, /*tp_methods*/ |
| 5047 | 0, /*tp_members*/ |
| 5048 | PySSLSession_getsetlist, /*tp_getset*/ |
| 5049 | }; |
| 5050 | |
| 5051 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5052 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5053 | /*[clinic input] |
| 5054 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5055 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5056 | entropy: double |
| 5057 | / |
| 5058 | |
| 5059 | Mix string into the OpenSSL PRNG state. |
| 5060 | |
| 5061 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5062 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5063 | [clinic start generated code]*/ |
| 5064 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5065 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5066 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5067 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5068 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5069 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5070 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5071 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5072 | buf = (const char *)view->buf; |
| 5073 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5074 | do { |
| 5075 | written = Py_MIN(len, INT_MAX); |
| 5076 | RAND_add(buf, (int)written, entropy); |
| 5077 | buf += written; |
| 5078 | len -= written; |
| 5079 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5080 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5083 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5084 | PySSL_RAND(int len, int pseudo) |
| 5085 | { |
| 5086 | int ok; |
| 5087 | PyObject *bytes; |
| 5088 | unsigned long err; |
| 5089 | const char *errstr; |
| 5090 | PyObject *v; |
| 5091 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5092 | if (len < 0) { |
| 5093 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5094 | return NULL; |
| 5095 | } |
| 5096 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5097 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5098 | if (bytes == NULL) |
| 5099 | return NULL; |
| 5100 | if (pseudo) { |
| 5101 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5102 | if (ok == 0 || ok == 1) |
| 5103 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5104 | } |
| 5105 | else { |
| 5106 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5107 | if (ok == 1) |
| 5108 | return bytes; |
| 5109 | } |
| 5110 | Py_DECREF(bytes); |
| 5111 | |
| 5112 | err = ERR_get_error(); |
| 5113 | errstr = ERR_reason_error_string(err); |
| 5114 | v = Py_BuildValue("(ks)", err, errstr); |
| 5115 | if (v != NULL) { |
| 5116 | PyErr_SetObject(PySSLErrorObject, v); |
| 5117 | Py_DECREF(v); |
| 5118 | } |
| 5119 | return NULL; |
| 5120 | } |
| 5121 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5122 | /*[clinic input] |
| 5123 | _ssl.RAND_bytes |
| 5124 | n: int |
| 5125 | / |
| 5126 | |
| 5127 | Generate n cryptographically strong pseudo-random bytes. |
| 5128 | [clinic start generated code]*/ |
| 5129 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5130 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5131 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5132 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5133 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5134 | return PySSL_RAND(n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5135 | } |
| 5136 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5137 | /*[clinic input] |
| 5138 | _ssl.RAND_pseudo_bytes |
| 5139 | n: int |
| 5140 | / |
| 5141 | |
| 5142 | Generate n pseudo-random bytes. |
| 5143 | |
| 5144 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5145 | if the bytes generated are cryptographically strong. |
| 5146 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5147 | |
| 5148 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5149 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5150 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5151 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5152 | return PySSL_RAND(n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5153 | } |
| 5154 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5155 | /*[clinic input] |
| 5156 | _ssl.RAND_status |
| 5157 | |
| 5158 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 5159 | |
| 5160 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5161 | using the ssl() function. |
| 5162 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5163 | |
| 5164 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5165 | _ssl_RAND_status_impl(PyObject *module) |
| 5166 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5167 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5168 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5171 | #ifndef OPENSSL_NO_EGD |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5172 | /* LCOV_EXCL_START */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5173 | /*[clinic input] |
| 5174 | _ssl.RAND_egd |
| 5175 | path: object(converter="PyUnicode_FSConverter") |
| 5176 | / |
| 5177 | |
| 5178 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 5179 | |
| 5180 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 5181 | fails or if it does not provide enough data to seed PRNG. |
| 5182 | [clinic start generated code]*/ |
| 5183 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5185 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 5186 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5187 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5188 | int bytes = RAND_egd(PyBytes_AsString(path)); |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 5189 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5190 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 5191 | PyErr_SetString(PySSLErrorObject, |
| 5192 | "EGD connection failed or EGD did not return " |
| 5193 | "enough data to seed the PRNG"); |
| 5194 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5195 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5196 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5197 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 5198 | /* LCOV_EXCL_STOP */ |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 5199 | #endif /* OPENSSL_NO_EGD */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5200 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5201 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5202 | |
| 5203 | /*[clinic input] |
| 5204 | _ssl.get_default_verify_paths |
| 5205 | |
| 5206 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5207 | |
| 5208 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5209 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5210 | |
| 5211 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5212 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5213 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5214 | { |
| 5215 | PyObject *ofile_env = NULL; |
| 5216 | PyObject *ofile = NULL; |
| 5217 | PyObject *odir_env = NULL; |
| 5218 | PyObject *odir = NULL; |
| 5219 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5220 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5221 | const char *tmp = (info); \ |
| 5222 | target = NULL; \ |
| 5223 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5224 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5225 | target = PyBytes_FromString(tmp); } \ |
| 5226 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5227 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5228 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5229 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5230 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5231 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5232 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5233 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5234 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5235 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5236 | |
| 5237 | error: |
| 5238 | Py_XDECREF(ofile_env); |
| 5239 | Py_XDECREF(ofile); |
| 5240 | Py_XDECREF(odir_env); |
| 5241 | Py_XDECREF(odir); |
| 5242 | return NULL; |
| 5243 | } |
| 5244 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5245 | static PyObject* |
| 5246 | asn1obj2py(ASN1_OBJECT *obj) |
| 5247 | { |
| 5248 | int nid; |
| 5249 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5250 | |
| 5251 | nid = OBJ_obj2nid(obj); |
| 5252 | if (nid == NID_undef) { |
| 5253 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5254 | return NULL; |
| 5255 | } |
| 5256 | sn = OBJ_nid2sn(nid); |
| 5257 | ln = OBJ_nid2ln(nid); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 5258 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5259 | } |
| 5260 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5261 | /*[clinic input] |
| 5262 | _ssl.txt2obj |
| 5263 | txt: str |
| 5264 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5265 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5266 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5267 | |
| 5268 | By default objects are looked up by OID. With name=True short and |
| 5269 | long name are also matched. |
| 5270 | [clinic start generated code]*/ |
| 5271 | |
| 5272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5273 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5274 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5275 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5276 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5277 | ASN1_OBJECT *obj; |
| 5278 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5279 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5280 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5281 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5282 | return NULL; |
| 5283 | } |
| 5284 | result = asn1obj2py(obj); |
| 5285 | ASN1_OBJECT_free(obj); |
| 5286 | return result; |
| 5287 | } |
| 5288 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5289 | /*[clinic input] |
| 5290 | _ssl.nid2obj |
| 5291 | nid: int |
| 5292 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5293 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5294 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5295 | [clinic start generated code]*/ |
| 5296 | |
| 5297 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5298 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5299 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5300 | { |
| 5301 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5302 | ASN1_OBJECT *obj; |
| 5303 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5304 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5305 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5306 | return NULL; |
| 5307 | } |
| 5308 | obj = OBJ_nid2obj(nid); |
| 5309 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5310 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5311 | return NULL; |
| 5312 | } |
| 5313 | result = asn1obj2py(obj); |
| 5314 | ASN1_OBJECT_free(obj); |
| 5315 | return result; |
| 5316 | } |
| 5317 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5318 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5319 | |
| 5320 | static PyObject* |
| 5321 | certEncodingType(DWORD encodingType) |
| 5322 | { |
| 5323 | static PyObject *x509_asn = NULL; |
| 5324 | static PyObject *pkcs_7_asn = NULL; |
| 5325 | |
| 5326 | if (x509_asn == NULL) { |
| 5327 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5328 | if (x509_asn == NULL) |
| 5329 | return NULL; |
| 5330 | } |
| 5331 | if (pkcs_7_asn == NULL) { |
| 5332 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5333 | if (pkcs_7_asn == NULL) |
| 5334 | return NULL; |
| 5335 | } |
| 5336 | switch(encodingType) { |
| 5337 | case X509_ASN_ENCODING: |
| 5338 | Py_INCREF(x509_asn); |
| 5339 | return x509_asn; |
| 5340 | case PKCS_7_ASN_ENCODING: |
| 5341 | Py_INCREF(pkcs_7_asn); |
| 5342 | return pkcs_7_asn; |
| 5343 | default: |
| 5344 | return PyLong_FromLong(encodingType); |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | static PyObject* |
| 5349 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5350 | { |
| 5351 | CERT_ENHKEY_USAGE *usage; |
| 5352 | DWORD size, error, i; |
| 5353 | PyObject *retval; |
| 5354 | |
| 5355 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5356 | error = GetLastError(); |
| 5357 | if (error == CRYPT_E_NOT_FOUND) { |
| 5358 | Py_RETURN_TRUE; |
| 5359 | } |
| 5360 | return PyErr_SetFromWindowsErr(error); |
| 5361 | } |
| 5362 | |
| 5363 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5364 | if (usage == NULL) { |
| 5365 | return PyErr_NoMemory(); |
| 5366 | } |
| 5367 | |
| 5368 | /* Now get the actual enhanced usage property */ |
| 5369 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5370 | PyMem_Free(usage); |
| 5371 | error = GetLastError(); |
| 5372 | if (error == CRYPT_E_NOT_FOUND) { |
| 5373 | Py_RETURN_TRUE; |
| 5374 | } |
| 5375 | return PyErr_SetFromWindowsErr(error); |
| 5376 | } |
| 5377 | retval = PySet_New(NULL); |
| 5378 | if (retval == NULL) { |
| 5379 | goto error; |
| 5380 | } |
| 5381 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5382 | if (usage->rgpszUsageIdentifier[i]) { |
| 5383 | PyObject *oid; |
| 5384 | int err; |
| 5385 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5386 | if (oid == NULL) { |
| 5387 | Py_CLEAR(retval); |
| 5388 | goto error; |
| 5389 | } |
| 5390 | err = PySet_Add(retval, oid); |
| 5391 | Py_DECREF(oid); |
| 5392 | if (err == -1) { |
| 5393 | Py_CLEAR(retval); |
| 5394 | goto error; |
| 5395 | } |
| 5396 | } |
| 5397 | } |
| 5398 | error: |
| 5399 | PyMem_Free(usage); |
| 5400 | return retval; |
| 5401 | } |
| 5402 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5403 | /*[clinic input] |
| 5404 | _ssl.enum_certificates |
| 5405 | store_name: str |
| 5406 | |
| 5407 | Retrieve certificates from Windows' cert store. |
| 5408 | |
| 5409 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5410 | more cert storages, too. The function returns a list of (bytes, |
| 5411 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5412 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5413 | a set of OIDs or the boolean True. |
| 5414 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5415 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5416 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5417 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5418 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5419 | { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5420 | HCERTSTORE hStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5421 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5422 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5423 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5424 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5425 | result = PyList_New(0); |
| 5426 | if (result == NULL) { |
| 5427 | return NULL; |
| 5428 | } |
Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 5429 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 5430 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5431 | store_name); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5432 | if (hStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5433 | Py_DECREF(result); |
| 5434 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5435 | } |
| 5436 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5437 | while (pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx)) { |
| 5438 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5439 | pCertCtx->cbCertEncoded); |
| 5440 | if (!cert) { |
| 5441 | Py_CLEAR(result); |
| 5442 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5443 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5444 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5445 | Py_CLEAR(result); |
| 5446 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5447 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5448 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5449 | if (keyusage == Py_True) { |
| 5450 | Py_DECREF(keyusage); |
| 5451 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5452 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5453 | if (keyusage == NULL) { |
| 5454 | Py_CLEAR(result); |
| 5455 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5456 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5457 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5458 | Py_CLEAR(result); |
| 5459 | break; |
| 5460 | } |
| 5461 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5462 | cert = NULL; |
| 5463 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5464 | enc = NULL; |
| 5465 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5466 | keyusage = NULL; |
| 5467 | if (PyList_Append(result, tup) < 0) { |
| 5468 | Py_CLEAR(result); |
| 5469 | break; |
| 5470 | } |
| 5471 | Py_CLEAR(tup); |
| 5472 | } |
| 5473 | if (pCertCtx) { |
| 5474 | /* loop ended with an error, need to clean up context manually */ |
| 5475 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /* In error cases cert, enc and tup may not be NULL */ |
| 5479 | Py_XDECREF(cert); |
| 5480 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5481 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5482 | Py_XDECREF(tup); |
| 5483 | |
| 5484 | if (!CertCloseStore(hStore, 0)) { |
| 5485 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5486 | Py_XDECREF(result); |
| 5487 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5488 | } |
| 5489 | return result; |
| 5490 | } |
| 5491 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5492 | /*[clinic input] |
| 5493 | _ssl.enum_crls |
| 5494 | store_name: str |
| 5495 | |
| 5496 | Retrieve CRLs from Windows' cert store. |
| 5497 | |
| 5498 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5499 | more cert storages, too. The function returns a list of (bytes, |
| 5500 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5501 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5502 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5503 | |
| 5504 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5505 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5506 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5507 | { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5508 | HCERTSTORE hStore = NULL; |
| 5509 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5510 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5511 | PyObject *result = NULL; |
| 5512 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5513 | result = PyList_New(0); |
| 5514 | if (result == NULL) { |
| 5515 | return NULL; |
| 5516 | } |
Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 5517 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 5518 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5519 | store_name); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5520 | if (hStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5521 | Py_DECREF(result); |
| 5522 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5523 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5524 | |
| 5525 | while (pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)) { |
| 5526 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5527 | pCrlCtx->cbCrlEncoded); |
| 5528 | if (!crl) { |
| 5529 | Py_CLEAR(result); |
| 5530 | break; |
| 5531 | } |
| 5532 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5533 | Py_CLEAR(result); |
| 5534 | break; |
| 5535 | } |
| 5536 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5537 | Py_CLEAR(result); |
| 5538 | break; |
| 5539 | } |
| 5540 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5541 | crl = NULL; |
| 5542 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5543 | enc = NULL; |
| 5544 | |
| 5545 | if (PyList_Append(result, tup) < 0) { |
| 5546 | Py_CLEAR(result); |
| 5547 | break; |
| 5548 | } |
| 5549 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5550 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5551 | if (pCrlCtx) { |
| 5552 | /* loop ended with an error, need to clean up context manually */ |
| 5553 | CertFreeCRLContext(pCrlCtx); |
| 5554 | } |
| 5555 | |
| 5556 | /* In error cases cert, enc and tup may not be NULL */ |
| 5557 | Py_XDECREF(crl); |
| 5558 | Py_XDECREF(enc); |
| 5559 | Py_XDECREF(tup); |
| 5560 | |
| 5561 | if (!CertCloseStore(hStore, 0)) { |
| 5562 | /* This error case might shadow another exception.*/ |
| 5563 | Py_XDECREF(result); |
| 5564 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5565 | } |
| 5566 | return result; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5567 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5568 | |
| 5569 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5570 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5571 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5572 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5573 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5574 | _SSL_RAND_ADD_METHODDEF |
| 5575 | _SSL_RAND_BYTES_METHODDEF |
| 5576 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5577 | _SSL_RAND_EGD_METHODDEF |
| 5578 | _SSL_RAND_STATUS_METHODDEF |
| 5579 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5580 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5581 | _SSL_ENUM_CRLS_METHODDEF |
| 5582 | _SSL_TXT2OBJ_METHODDEF |
| 5583 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5584 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5585 | }; |
| 5586 | |
| 5587 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5588 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5589 | |
| 5590 | /* an implementation of OpenSSL threading operations in terms |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5591 | * of the Python C thread library |
| 5592 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5593 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5594 | |
| 5595 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5596 | |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5597 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5598 | /* use new CRYPTO_THREADID API. */ |
| 5599 | static void |
| 5600 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5601 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5602 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5603 | } |
| 5604 | #else |
| 5605 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5606 | static unsigned long |
| 5607 | _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5608 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5609 | } |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5610 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5611 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5612 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5613 | (int mode, int n, const char *file, int line) { |
| 5614 | /* this function is needed to perform locking on shared data |
| 5615 | structures. (Note that OpenSSL uses a number of global data |
| 5616 | structures that will be implicitly shared whenever multiple |
| 5617 | threads use OpenSSL.) Multi-threaded applications will |
| 5618 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5619 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5620 | locking_function() must be able to handle up to |
| 5621 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5622 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5623 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5624 | file and line are the file number of the function setting the |
| 5625 | lock. They can be useful for debugging. |
| 5626 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5627 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5628 | if ((_ssl_locks == NULL) || |
| 5629 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5630 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5631 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5632 | if (mode & CRYPTO_LOCK) { |
| 5633 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5634 | } else { |
| 5635 | PyThread_release_lock(_ssl_locks[n]); |
| 5636 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | static int _setup_ssl_threads(void) { |
| 5640 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5641 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5642 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5643 | if (_ssl_locks == NULL) { |
| 5644 | _ssl_locks_count = CRYPTO_num_locks(); |
Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5645 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5646 | sizeof(PyThread_type_lock)); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5647 | if (_ssl_locks == NULL) { |
| 5648 | PyErr_NoMemory(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5649 | return 0; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5650 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5651 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5652 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5653 | if (_ssl_locks[i] == NULL) { |
| 5654 | unsigned int j; |
| 5655 | for (j = 0; j < i; j++) { |
| 5656 | PyThread_free_lock(_ssl_locks[j]); |
| 5657 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5658 | PyMem_Free(_ssl_locks); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5659 | return 0; |
| 5660 | } |
| 5661 | } |
| 5662 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5663 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5664 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5665 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5666 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5667 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5668 | } |
| 5669 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5672 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5674 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5675 | "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] | 5676 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5677 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5678 | |
| 5679 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5680 | PyModuleDef_HEAD_INIT, |
| 5681 | "_ssl", |
| 5682 | module_doc, |
| 5683 | -1, |
| 5684 | PySSL_methods, |
| 5685 | NULL, |
| 5686 | NULL, |
| 5687 | NULL, |
| 5688 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5689 | }; |
| 5690 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5691 | |
| 5692 | static void |
| 5693 | parse_openssl_version(unsigned long libver, |
| 5694 | unsigned int *major, unsigned int *minor, |
| 5695 | unsigned int *fix, unsigned int *patch, |
| 5696 | unsigned int *status) |
| 5697 | { |
| 5698 | *status = libver & 0xF; |
| 5699 | libver >>= 4; |
| 5700 | *patch = libver & 0xFF; |
| 5701 | libver >>= 8; |
| 5702 | *fix = libver & 0xFF; |
| 5703 | libver >>= 8; |
| 5704 | *minor = libver & 0xFF; |
| 5705 | libver >>= 8; |
| 5706 | *major = libver & 0xFF; |
| 5707 | } |
| 5708 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5709 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5710 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5711 | { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5712 | PyObject *m, *d, *r, *bases; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5713 | unsigned long libver; |
| 5714 | unsigned int major, minor, fix, patch, status; |
| 5715 | PySocketModule_APIObject *socket_api; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5716 | struct py_ssl_error_code *errcode; |
| 5717 | struct py_ssl_library_code *libcode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5718 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5719 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 5720 | return NULL; |
| 5721 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5722 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5723 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) |
| 5724 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5725 | if (PyType_Ready(&PySSLSession_Type) < 0) |
| 5726 | return NULL; |
| 5727 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5728 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5729 | m = PyModule_Create(&_sslmodule); |
| 5730 | if (m == NULL) |
| 5731 | return NULL; |
| 5732 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5733 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5734 | /* Load _socket module and its C API */ |
| 5735 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 5736 | if (!socket_api) |
| 5737 | return NULL; |
| 5738 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5739 | |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5740 | #ifndef OPENSSL_VERSION_1_1 |
| 5741 | /* Load all algorithms and initialize cpuid */ |
| 5742 | OPENSSL_add_all_algorithms_noconf(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5743 | /* Init OpenSSL */ |
| 5744 | SSL_load_error_strings(); |
| 5745 | SSL_library_init(); |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5746 | #endif |
| 5747 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5748 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5749 | /* note that this will start threading if not already started */ |
| 5750 | if (!_setup_ssl_threads()) { |
| 5751 | return NULL; |
| 5752 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5753 | #elif OPENSSL_VERSION_1_1 && defined(OPENSSL_THREADS) |
| 5754 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 5755 | _ssl_locks_count++; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5756 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5757 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5758 | /* Add symbols to module dict */ |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5759 | sslerror_type_slots[0].pfunc = PyExc_OSError; |
| 5760 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5761 | if (PySSLErrorObject == NULL) |
| 5762 | return NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5763 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5764 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 5765 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); |
| 5766 | if (bases == NULL) |
| 5767 | return NULL; |
| 5768 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( |
| 5769 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, |
| 5770 | bases, NULL); |
| 5771 | Py_DECREF(bases); |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5772 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 5773 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 5774 | PySSLErrorObject, NULL); |
| 5775 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 5776 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 5777 | PySSLErrorObject, NULL); |
| 5778 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 5779 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 5780 | PySSLErrorObject, NULL); |
| 5781 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 5782 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 5783 | PySSLErrorObject, NULL); |
| 5784 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 5785 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 5786 | PySSLErrorObject, NULL); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5787 | if (PySSLCertVerificationErrorObject == NULL |
| 5788 | || PySSLZeroReturnErrorObject == NULL |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5789 | || PySSLWantReadErrorObject == NULL |
| 5790 | || PySSLWantWriteErrorObject == NULL |
| 5791 | || PySSLSyscallErrorObject == NULL |
| 5792 | || PySSLEOFErrorObject == NULL) |
| 5793 | return NULL; |
| 5794 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5795 | || PyDict_SetItemString(d, "SSLCertVerificationError", |
| 5796 | PySSLCertVerificationErrorObject) != 0 |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5797 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 5798 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 5799 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 5800 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 5801 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5802 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5803 | if (PyDict_SetItemString(d, "_SSLContext", |
| 5804 | (PyObject *)&PySSLContext_Type) != 0) |
| 5805 | return NULL; |
| 5806 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 5807 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5808 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5809 | if (PyDict_SetItemString(d, "MemoryBIO", |
| 5810 | (PyObject *)&PySSLMemoryBIO_Type) != 0) |
| 5811 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5812 | if (PyDict_SetItemString(d, "SSLSession", |
| 5813 | (PyObject *)&PySSLSession_Type) != 0) |
| 5814 | return NULL; |
| 5815 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 5816 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 5817 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 5818 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5819 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 5820 | PY_SSL_ERROR_ZERO_RETURN); |
| 5821 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 5822 | PY_SSL_ERROR_WANT_READ); |
| 5823 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 5824 | PY_SSL_ERROR_WANT_WRITE); |
| 5825 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 5826 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 5827 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 5828 | PY_SSL_ERROR_SYSCALL); |
| 5829 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 5830 | PY_SSL_ERROR_SSL); |
| 5831 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 5832 | PY_SSL_ERROR_WANT_CONNECT); |
| 5833 | /* non ssl.h errorcodes */ |
| 5834 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 5835 | PY_SSL_ERROR_EOF); |
| 5836 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 5837 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 5838 | /* cert requirements */ |
| 5839 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 5840 | PY_SSL_CERT_NONE); |
| 5841 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 5842 | PY_SSL_CERT_OPTIONAL); |
| 5843 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 5844 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 5845 | /* CRL verification for verification_flags */ |
| 5846 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 5847 | 0); |
| 5848 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 5849 | X509_V_FLAG_CRL_CHECK); |
| 5850 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 5851 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5852 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 5853 | X509_V_FLAG_X509_STRICT); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 5854 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 5855 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 5856 | X509_V_FLAG_TRUSTED_FIRST); |
| 5857 | #endif |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 5858 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5859 | /* Alert Descriptions from ssl.h */ |
| 5860 | /* note RESERVED constants no longer intended for use have been removed */ |
| 5861 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 5862 | |
| 5863 | #define ADD_AD_CONSTANT(s) \ |
| 5864 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 5865 | SSL_AD_##s) |
| 5866 | |
| 5867 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 5868 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 5869 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 5870 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 5871 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 5872 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 5873 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 5874 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 5875 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 5876 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 5877 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 5878 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 5879 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 5880 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 5881 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 5882 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 5883 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 5884 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 5885 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 5886 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 5887 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5888 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5889 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 5890 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 5891 | #endif |
| 5892 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 5893 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 5894 | #endif |
| 5895 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 5896 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 5897 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5898 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 5899 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 5900 | #endif |
| 5901 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 5902 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 5903 | #endif |
| 5904 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 5905 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 5906 | #endif |
| 5907 | |
| 5908 | #undef ADD_AD_CONSTANT |
| 5909 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5910 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5911 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5912 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 5913 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5914 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5915 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5916 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 5917 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5918 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5919 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5920 | PY_SSL_VERSION_TLS); |
| 5921 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 5922 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 5923 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 5924 | PY_SSL_VERSION_TLS_CLIENT); |
| 5925 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 5926 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5927 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 5928 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5929 | #if HAVE_TLSv1_2 |
| 5930 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 5931 | PY_SSL_VERSION_TLS1_1); |
| 5932 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 5933 | PY_SSL_VERSION_TLS1_2); |
| 5934 | #endif |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5935 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5936 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 5937 | PyModule_AddIntConstant(m, "OP_ALL", |
| 5938 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5939 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 5940 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 5941 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5942 | #if HAVE_TLSv1_2 |
| 5943 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 5944 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
| 5945 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5946 | #ifdef SSL_OP_NO_TLSv1_3 |
| 5947 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 5948 | #else |
| 5949 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 5950 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 5951 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 5952 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 5953 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5954 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5955 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 5956 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5957 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 5958 | #ifdef SSL_OP_NO_COMPRESSION |
| 5959 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 5960 | SSL_OP_NO_COMPRESSION); |
| 5961 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 5962 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 5963 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 5964 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 5965 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 5966 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5967 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 5968 | SSL_OP_NO_RENEGOTIATION); |
| 5969 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5970 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 5971 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 5972 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 5973 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 5974 | #endif |
| 5975 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 5976 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 5977 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 5978 | #endif |
| 5979 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 5980 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 5981 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 5982 | #endif |
| 5983 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 5984 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 5985 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 5986 | #endif |
| 5987 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 5988 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 5989 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 5990 | #endif |
| 5991 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 5992 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 5993 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 5994 | #endif |
| 5995 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5996 | /* protocol versions */ |
| 5997 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 5998 | PY_PROTO_MINIMUM_SUPPORTED); |
| 5999 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 6000 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 6001 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 6002 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 6003 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 6004 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 6005 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 6006 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6007 | #define addbool(m, key, value) \ |
| 6008 | do { \ |
| 6009 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 6010 | Py_INCREF(bool_obj); \ |
| 6011 | PyModule_AddObject((m), (key), bool_obj); \ |
| 6012 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6013 | |
| 6014 | #if HAVE_SNI |
| 6015 | addbool(m, "HAS_SNI", 1); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6016 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6017 | addbool(m, "HAS_SNI", 0); |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6018 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6019 | |
| 6020 | addbool(m, "HAS_TLS_UNIQUE", 1); |
| 6021 | |
| 6022 | #ifndef OPENSSL_NO_ECDH |
| 6023 | addbool(m, "HAS_ECDH", 1); |
| 6024 | #else |
| 6025 | addbool(m, "HAS_ECDH", 0); |
| 6026 | #endif |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 6027 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6028 | #if HAVE_NPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6029 | addbool(m, "HAS_NPN", 1); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6030 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6031 | addbool(m, "HAS_NPN", 0); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6032 | #endif |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 6033 | |
Christian Heimes | 29eab55 | 2018-02-25 12:31:33 +0100 | [diff] [blame] | 6034 | #if HAVE_ALPN |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6035 | addbool(m, "HAS_ALPN", 1); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6036 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6037 | addbool(m, "HAS_ALPN", 0); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6038 | #endif |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6039 | |
| 6040 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 6041 | addbool(m, "HAS_SSLv2", 1); |
| 6042 | #else |
| 6043 | addbool(m, "HAS_SSLv2", 0); |
| 6044 | #endif |
| 6045 | |
| 6046 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 6047 | addbool(m, "HAS_SSLv3", 1); |
| 6048 | #else |
| 6049 | addbool(m, "HAS_SSLv3", 0); |
| 6050 | #endif |
| 6051 | |
| 6052 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 6053 | addbool(m, "HAS_TLSv1", 1); |
| 6054 | #else |
| 6055 | addbool(m, "HAS_TLSv1", 0); |
| 6056 | #endif |
| 6057 | |
| 6058 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 6059 | addbool(m, "HAS_TLSv1_1", 1); |
| 6060 | #else |
| 6061 | addbool(m, "HAS_TLSv1_1", 0); |
| 6062 | #endif |
| 6063 | |
| 6064 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 6065 | addbool(m, "HAS_TLSv1_2", 1); |
| 6066 | #else |
| 6067 | addbool(m, "HAS_TLSv1_2", 0); |
| 6068 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 6069 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6070 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6071 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6072 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 6073 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6074 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 6075 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 6076 | /* Mappings for error codes */ |
| 6077 | err_codes_to_names = PyDict_New(); |
| 6078 | err_names_to_codes = PyDict_New(); |
| 6079 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 6080 | return NULL; |
| 6081 | errcode = error_codes; |
| 6082 | while (errcode->mnemonic != NULL) { |
| 6083 | PyObject *mnemo, *key; |
| 6084 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 6085 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 6086 | if (mnemo == NULL || key == NULL) |
| 6087 | return NULL; |
| 6088 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 6089 | return NULL; |
| 6090 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 6091 | return NULL; |
| 6092 | Py_DECREF(key); |
| 6093 | Py_DECREF(mnemo); |
| 6094 | errcode++; |
| 6095 | } |
| 6096 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 6097 | return NULL; |
| 6098 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 6099 | return NULL; |
| 6100 | |
| 6101 | lib_codes_to_names = PyDict_New(); |
| 6102 | if (lib_codes_to_names == NULL) |
| 6103 | return NULL; |
| 6104 | libcode = library_codes; |
| 6105 | while (libcode->library != NULL) { |
| 6106 | PyObject *mnemo, *key; |
| 6107 | key = PyLong_FromLong(libcode->code); |
| 6108 | mnemo = PyUnicode_FromString(libcode->library); |
| 6109 | if (key == NULL || mnemo == NULL) |
| 6110 | return NULL; |
| 6111 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 6112 | return NULL; |
| 6113 | Py_DECREF(key); |
| 6114 | Py_DECREF(mnemo); |
| 6115 | libcode++; |
| 6116 | } |
| 6117 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 6118 | return NULL; |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 6119 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6120 | /* OpenSSL version */ |
| 6121 | /* SSLeay() gives us the version of the library linked against, |
| 6122 | which could be different from the headers version. |
| 6123 | */ |
| 6124 | libver = SSLeay(); |
| 6125 | r = PyLong_FromUnsignedLong(libver); |
| 6126 | if (r == NULL) |
| 6127 | return NULL; |
| 6128 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6129 | return NULL; |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6130 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6131 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6132 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6133 | return NULL; |
| 6134 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 6135 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6136 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 6137 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 6138 | libver = OPENSSL_VERSION_NUMBER; |
| 6139 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6140 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6141 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6142 | return NULL; |
| 6143 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 6144 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6145 | } |