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" |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 66 | |
| 67 | /* SSL error object */ |
| 68 | static PyObject *PySSLErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 69 | static PyObject *PySSLCertVerificationErrorObject; |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 70 | static PyObject *PySSLZeroReturnErrorObject; |
| 71 | static PyObject *PySSLWantReadErrorObject; |
| 72 | static PyObject *PySSLWantWriteErrorObject; |
| 73 | static PyObject *PySSLSyscallErrorObject; |
| 74 | static PyObject *PySSLEOFErrorObject; |
| 75 | |
| 76 | /* Error mappings */ |
| 77 | static PyObject *err_codes_to_names; |
| 78 | static PyObject *err_names_to_codes; |
| 79 | static PyObject *lib_codes_to_names; |
| 80 | |
| 81 | struct py_ssl_error_code { |
| 82 | const char *mnemonic; |
| 83 | int library, reason; |
| 84 | }; |
| 85 | struct py_ssl_library_code { |
| 86 | const char *library; |
| 87 | int code; |
| 88 | }; |
| 89 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 90 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 91 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 92 | * However, because it uses a different CRT, we need to transfer the |
| 93 | * value of errno from OpenSSL into our debug CRT. |
| 94 | * |
| 95 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 96 | * alternative is to do both debug and release builds of OpenSSL, which |
| 97 | * requires much uglier code to transform their automatically generated |
| 98 | * makefile. This is the lesser of all the evils. |
| 99 | */ |
| 100 | |
| 101 | static void _PySSLFixErrno(void) { |
| 102 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 103 | if (!ucrtbase) { |
| 104 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 105 | * have a catastrophic failure, but this function is not the |
| 106 | * place to raise it. */ |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | typedef int *(__stdcall *errno_func)(void); |
| 111 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 112 | if (ssl_errno) { |
| 113 | errno = *ssl_errno(); |
| 114 | *ssl_errno() = 0; |
| 115 | } else { |
| 116 | errno = ENOTRECOVERABLE; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #undef _PySSL_FIX_ERRNO |
| 121 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 122 | #endif |
| 123 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 124 | /* Include generated data (error codes) */ |
| 125 | #include "_ssl_data.h" |
| 126 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 127 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 128 | # define OPENSSL_VERSION_1_1 1 |
| 129 | #endif |
| 130 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 131 | /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 |
| 132 | http://www.openssl.org/news/changelog.html |
| 133 | */ |
| 134 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 135 | # define HAVE_TLSv1_2 1 |
| 136 | #else |
| 137 | # define HAVE_TLSv1_2 0 |
| 138 | #endif |
| 139 | |
Christian Heimes | 470fba1 | 2013-11-28 15:12:15 +0100 | [diff] [blame] | 140 | /* 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] | 141 | * This includes the SSL_set_SSL_CTX() function. |
| 142 | */ |
| 143 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 144 | # define HAVE_SNI 1 |
| 145 | #else |
| 146 | # define HAVE_SNI 0 |
| 147 | #endif |
| 148 | |
Benjamin Peterson | d330822 | 2015-09-27 00:09:02 -0700 | [diff] [blame] | 149 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 150 | # define HAVE_ALPN |
| 151 | #endif |
| 152 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 153 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 154 | #define INVALID_SOCKET (-1) |
| 155 | #endif |
| 156 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 157 | #ifdef OPENSSL_VERSION_1_1 |
| 158 | /* OpenSSL 1.1.0+ */ |
| 159 | #ifndef OPENSSL_NO_SSL2 |
| 160 | #define OPENSSL_NO_SSL2 |
| 161 | #endif |
| 162 | #else /* OpenSSL < 1.1.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 163 | #define HAVE_OPENSSL_CRYPTO_LOCK |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 164 | |
| 165 | #define TLS_method SSLv23_method |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 166 | #define TLS_client_method SSLv23_client_method |
| 167 | #define TLS_server_method SSLv23_server_method |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 168 | |
| 169 | static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 170 | { |
| 171 | return ne->set; |
| 172 | } |
| 173 | |
| 174 | #ifndef OPENSSL_NO_COMP |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 175 | /* LCOV_EXCL_START */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 176 | static int COMP_get_type(const COMP_METHOD *meth) |
| 177 | { |
| 178 | return meth->type; |
| 179 | } |
Christian Heimes | 1a63b9f | 2016-09-24 12:07:21 +0200 | [diff] [blame] | 180 | /* LCOV_EXCL_STOP */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 184 | { |
| 185 | return ctx->default_passwd_callback; |
| 186 | } |
| 187 | |
| 188 | static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 189 | { |
| 190 | return ctx->default_passwd_callback_userdata; |
| 191 | } |
| 192 | |
| 193 | static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 194 | { |
| 195 | return x->type; |
| 196 | } |
| 197 | |
| 198 | static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 199 | { |
| 200 | return x->data.x509; |
| 201 | } |
| 202 | |
| 203 | static int BIO_up_ref(BIO *b) |
| 204 | { |
| 205 | CRYPTO_add(&b->references, 1, CRYPTO_LOCK_BIO); |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 210 | return store->objs; |
| 211 | } |
| 212 | |
| 213 | static X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store) |
| 214 | { |
| 215 | return store->param; |
| 216 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 217 | |
| 218 | static int |
| 219 | SSL_SESSION_has_ticket(const SSL_SESSION *s) |
| 220 | { |
| 221 | return (s->tlsext_ticklen > 0) ? 1 : 0; |
| 222 | } |
| 223 | |
| 224 | static unsigned long |
| 225 | SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
| 226 | { |
| 227 | return s->tlsext_tick_lifetime_hint; |
| 228 | } |
| 229 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 230 | #endif /* OpenSSL < 1.1.0 or LibreSSL */ |
| 231 | |
| 232 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 233 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 234 | /* these mirror ssl.h */ |
| 235 | PY_SSL_ERROR_NONE, |
| 236 | PY_SSL_ERROR_SSL, |
| 237 | PY_SSL_ERROR_WANT_READ, |
| 238 | PY_SSL_ERROR_WANT_WRITE, |
| 239 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 240 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 241 | PY_SSL_ERROR_ZERO_RETURN, |
| 242 | PY_SSL_ERROR_WANT_CONNECT, |
| 243 | /* start of non ssl.h errorcodes */ |
| 244 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 245 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 246 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 247 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 248 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 249 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 250 | PY_SSL_CLIENT, |
| 251 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 255 | PY_SSL_CERT_NONE, |
| 256 | PY_SSL_CERT_OPTIONAL, |
| 257 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 261 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 262 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 263 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 264 | #if HAVE_TLSv1_2 |
| 265 | PY_SSL_VERSION_TLS1, |
| 266 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 267 | PY_SSL_VERSION_TLS1_2, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 268 | #else |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 269 | PY_SSL_VERSION_TLS1, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 270 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 271 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 272 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 273 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 274 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 275 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 276 | /* 0 means no, greater than 0 means yes */ |
| 277 | |
| 278 | static unsigned int _ssl_locks_count = 0; |
| 279 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 280 | /* SSL socket object */ |
| 281 | |
| 282 | #define X509_NAME_MAXLEN 256 |
| 283 | |
Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 284 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 285 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 286 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 287 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 288 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 289 | #else |
| 290 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 291 | #endif |
| 292 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 293 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 294 | * older SSL, but let's be safe */ |
| 295 | #define PySSL_CB_MAXLEN 128 |
| 296 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 297 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 298 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 299 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 300 | SSL_CTX *ctx; |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 301 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 302 | unsigned char *npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 303 | int npn_protocols_len; |
| 304 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 305 | #ifdef HAVE_ALPN |
| 306 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 307 | unsigned int alpn_protocols_len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 308 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 309 | #ifndef OPENSSL_NO_TLSEXT |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 310 | PyObject *set_hostname; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 311 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 312 | int check_hostname; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 313 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 315 | typedef struct { |
| 316 | PyObject_HEAD |
| 317 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 318 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 319 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 320 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 321 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 322 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 323 | PyObject *server_hostname; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 324 | int ssl_errno; /* last seen error from SSL */ |
| 325 | int c_errno; /* last seen error from libc */ |
| 326 | #ifdef MS_WINDOWS |
| 327 | int ws_errno; /* last seen error from winsock */ |
| 328 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 329 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 331 | typedef struct { |
| 332 | PyObject_HEAD |
| 333 | BIO *bio; |
| 334 | int eof_written; |
| 335 | } PySSLMemoryBIO; |
| 336 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 337 | typedef struct { |
| 338 | PyObject_HEAD |
| 339 | SSL_SESSION *session; |
| 340 | PySSLContext *ctx; |
| 341 | } PySSLSession; |
| 342 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 343 | static PyTypeObject PySSLContext_Type; |
| 344 | static PyTypeObject PySSLSocket_Type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 345 | static PyTypeObject PySSLMemoryBIO_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 346 | static PyTypeObject PySSLSession_Type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 347 | |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 348 | #ifdef MS_WINDOWS |
| 349 | #define _PySSL_UPDATE_ERRNO_IF(cond, sock, retcode) if (cond) { \ |
| 350 | (sock)->ws_errno = WSAGetLastError(); \ |
| 351 | _PySSL_FIX_ERRNO; \ |
| 352 | (sock)->c_errno = errno; \ |
| 353 | (sock)->ssl_errno = SSL_get_error((sock->ssl), (retcode)); \ |
| 354 | } else { sock->ws_errno = 0; sock->c_errno = 0; sock->ssl_errno = 0; } |
| 355 | #else |
| 356 | #define _PySSL_UPDATE_ERRNO_IF(cond, sock, retcode) if (cond) { \ |
| 357 | (sock)->c_errno = errno; \ |
| 358 | (sock)->ssl_errno = SSL_get_error((sock->ssl), (retcode)); \ |
| 359 | } else { (sock)->c_errno = 0; (sock)->ssl_errno = 0; } |
| 360 | #endif |
| 361 | #define _PySSL_UPDATE_ERRNO(sock, retcode) _PySSL_UPDATE_ERRNO_IF(1, (sock), (retcode)) |
| 362 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 363 | /*[clinic input] |
| 364 | module _ssl |
| 365 | class _ssl._SSLContext "PySSLContext *" "&PySSLContext_Type" |
| 366 | class _ssl._SSLSocket "PySSLSocket *" "&PySSLSocket_Type" |
| 367 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "&PySSLMemoryBIO_Type" |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 368 | class _ssl.SSLSession "PySSLSession *" "&PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 369 | [clinic start generated code]*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 370 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdc67fafeeaa8109]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 371 | |
| 372 | #include "clinic/_ssl.c.h" |
| 373 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 374 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 375 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 376 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 377 | #define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type) |
| 378 | #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 379 | #define PySSLMemoryBIO_Check(v) (Py_TYPE(v) == &PySSLMemoryBIO_Type) |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 380 | #define PySSLSession_Check(v) (Py_TYPE(v) == &PySSLSession_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 381 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 382 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 383 | SOCKET_IS_NONBLOCKING, |
| 384 | SOCKET_IS_BLOCKING, |
| 385 | SOCKET_HAS_TIMED_OUT, |
| 386 | SOCKET_HAS_BEEN_CLOSED, |
| 387 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 388 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 389 | } timeout_state; |
| 390 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 391 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 392 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 393 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 395 | /* Get the socket from a PySSLSocket, if it has one */ |
| 396 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 397 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 398 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 399 | /* If sock is NULL, use a timeout of 0 second */ |
| 400 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 401 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 402 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 403 | /* |
| 404 | * SSL errors. |
| 405 | */ |
| 406 | |
| 407 | PyDoc_STRVAR(SSLError_doc, |
| 408 | "An error occurred in the SSL implementation."); |
| 409 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 410 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 411 | "A certificate could not be verified."); |
| 412 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 413 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 414 | "SSL/TLS session closed cleanly."); |
| 415 | |
| 416 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 417 | "Non-blocking SSL socket needs to read more data\n" |
| 418 | "before the requested operation can be completed."); |
| 419 | |
| 420 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 421 | "Non-blocking SSL socket needs to write more data\n" |
| 422 | "before the requested operation can be completed."); |
| 423 | |
| 424 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 425 | "System error when attempting SSL operation."); |
| 426 | |
| 427 | PyDoc_STRVAR(SSLEOFError_doc, |
| 428 | "SSL/TLS connection terminated abruptly."); |
| 429 | |
| 430 | static PyObject * |
| 431 | SSLError_str(PyOSErrorObject *self) |
| 432 | { |
| 433 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 434 | Py_INCREF(self->strerror); |
| 435 | return self->strerror; |
| 436 | } |
| 437 | else |
| 438 | return PyObject_Str(self->args); |
| 439 | } |
| 440 | |
| 441 | static PyType_Slot sslerror_type_slots[] = { |
| 442 | {Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */ |
| 443 | {Py_tp_doc, SSLError_doc}, |
| 444 | {Py_tp_str, SSLError_str}, |
| 445 | {0, 0}, |
| 446 | }; |
| 447 | |
| 448 | static PyType_Spec sslerror_type_spec = { |
| 449 | "ssl.SSLError", |
| 450 | sizeof(PyOSErrorObject), |
| 451 | 0, |
| 452 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 453 | sslerror_type_slots |
| 454 | }; |
| 455 | |
| 456 | static void |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 457 | fill_and_set_sslerror(PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
| 458 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 459 | { |
| 460 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 461 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 462 | PyObject *init_value, *msg, *key; |
| 463 | _Py_IDENTIFIER(reason); |
| 464 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 465 | _Py_IDENTIFIER(verify_message); |
| 466 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 467 | |
| 468 | if (errcode != 0) { |
| 469 | int lib, reason; |
| 470 | |
| 471 | lib = ERR_GET_LIB(errcode); |
| 472 | reason = ERR_GET_REASON(errcode); |
| 473 | key = Py_BuildValue("ii", lib, reason); |
| 474 | if (key == NULL) |
| 475 | goto fail; |
| 476 | reason_obj = PyDict_GetItem(err_codes_to_names, key); |
| 477 | Py_DECREF(key); |
| 478 | if (reason_obj == NULL) { |
| 479 | /* XXX if reason < 100, it might reflect a library number (!!) */ |
| 480 | PyErr_Clear(); |
| 481 | } |
| 482 | key = PyLong_FromLong(lib); |
| 483 | if (key == NULL) |
| 484 | goto fail; |
| 485 | lib_obj = PyDict_GetItem(lib_codes_to_names, key); |
| 486 | Py_DECREF(key); |
| 487 | if (lib_obj == NULL) { |
| 488 | PyErr_Clear(); |
| 489 | } |
| 490 | if (errstr == NULL) |
| 491 | errstr = ERR_reason_error_string(errcode); |
| 492 | } |
| 493 | if (errstr == NULL) |
| 494 | errstr = "unknown error"; |
| 495 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 496 | /* verify code for cert validation error */ |
| 497 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 498 | const char *verify_str = NULL; |
| 499 | long verify_code; |
| 500 | |
| 501 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 502 | verify_code_obj = PyLong_FromLong(verify_code); |
| 503 | if (verify_code_obj == NULL) { |
| 504 | goto fail; |
| 505 | } |
| 506 | |
| 507 | switch (verify_code) { |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 508 | #ifdef X509_V_ERR_HOSTNAME_MISMATCH |
| 509 | /* OpenSSL >= 1.0.2, LibreSSL >= 2.5.3 */ |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 510 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 511 | verify_obj = PyUnicode_FromFormat( |
| 512 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 513 | sslsock->server_hostname |
| 514 | ); |
| 515 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 516 | #endif |
| 517 | #ifdef X509_V_ERR_IP_ADDRESS_MISMATCH |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 518 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 519 | verify_obj = PyUnicode_FromFormat( |
| 520 | "IP address mismatch, certificate is not valid for '%S'.", |
| 521 | sslsock->server_hostname |
| 522 | ); |
| 523 | break; |
Christian Heimes | 0915360 | 2017-09-08 14:47:58 -0700 | [diff] [blame] | 524 | #endif |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 525 | default: |
| 526 | verify_str = X509_verify_cert_error_string(verify_code); |
| 527 | if (verify_str != NULL) { |
| 528 | verify_obj = PyUnicode_FromString(verify_str); |
| 529 | } else { |
| 530 | verify_obj = Py_None; |
| 531 | Py_INCREF(verify_obj); |
| 532 | } |
| 533 | break; |
| 534 | } |
| 535 | if (verify_obj == NULL) { |
| 536 | goto fail; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (verify_obj && reason_obj && lib_obj) |
| 541 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 542 | lib_obj, reason_obj, errstr, verify_obj, |
| 543 | lineno); |
| 544 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 545 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 546 | lib_obj, reason_obj, errstr, lineno); |
| 547 | else if (lib_obj) |
| 548 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 549 | lib_obj, errstr, lineno); |
| 550 | else |
| 551 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 552 | if (msg == NULL) |
| 553 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 554 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 555 | init_value = Py_BuildValue("iN", ssl_errno, msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 556 | if (init_value == NULL) |
| 557 | goto fail; |
| 558 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 559 | err_value = PyObject_CallObject(type, init_value); |
| 560 | Py_DECREF(init_value); |
| 561 | if (err_value == NULL) |
| 562 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 563 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 564 | if (reason_obj == NULL) |
| 565 | reason_obj = Py_None; |
| 566 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 567 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 568 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 569 | if (lib_obj == NULL) |
| 570 | lib_obj = Py_None; |
| 571 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 572 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 573 | |
| 574 | if ((sslsock != NULL) && (type == PySSLCertVerificationErrorObject)) { |
| 575 | /* Only set verify code / message for SSLCertVerificationError */ |
| 576 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 577 | verify_code_obj)) |
| 578 | goto fail; |
| 579 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 580 | goto fail; |
| 581 | } |
| 582 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 583 | PyErr_SetObject(type, err_value); |
| 584 | fail: |
| 585 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 586 | Py_XDECREF(verify_code_obj); |
| 587 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 588 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 589 | |
| 590 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 591 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 592 | { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 593 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 594 | char *errstr = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 595 | int err; |
| 596 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 597 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 599 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 600 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 601 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 602 | if (sslsock->ssl != NULL) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 603 | err = sslsock->ssl_errno; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 605 | switch (err) { |
| 606 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 607 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 608 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 609 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 610 | break; |
| 611 | case SSL_ERROR_WANT_READ: |
| 612 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 613 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 614 | p = PY_SSL_ERROR_WANT_READ; |
| 615 | break; |
| 616 | case SSL_ERROR_WANT_WRITE: |
| 617 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 618 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 619 | errstr = "The operation did not complete (write)"; |
| 620 | break; |
| 621 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 622 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 623 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 624 | break; |
| 625 | case SSL_ERROR_WANT_CONNECT: |
| 626 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 627 | errstr = "The operation did not complete (connect)"; |
| 628 | break; |
| 629 | case SSL_ERROR_SYSCALL: |
| 630 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 631 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 632 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 633 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 634 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 635 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 636 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 637 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 638 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 639 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 640 | #ifdef MS_WINDOWS |
| 641 | if (sslsock->ws_errno) |
| 642 | return PyErr_SetFromWindowsErr(sslsock->ws_errno); |
| 643 | #endif |
| 644 | if (sslsock->c_errno) { |
| 645 | errno = sslsock->c_errno; |
| 646 | return PyErr_SetFromErrno(PyExc_OSError); |
| 647 | } |
| 648 | Py_INCREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 649 | s->errorhandler(); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 650 | Py_DECREF(s); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 651 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 652 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 653 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 654 | type = PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 655 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 656 | } |
| 657 | } else { |
| 658 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 659 | } |
| 660 | break; |
| 661 | } |
| 662 | case SSL_ERROR_SSL: |
| 663 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 664 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 665 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 666 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 667 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 668 | } |
| 669 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 670 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
| 671 | type = PySSLCertVerificationErrorObject; |
| 672 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 673 | break; |
| 674 | } |
| 675 | default: |
| 676 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 677 | errstr = "Invalid error code"; |
| 678 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 679 | } |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 680 | fill_and_set_sslerror(sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 681 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 682 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 685 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 686 | _setSSLError (const char *errstr, int errcode, const char *filename, int lineno) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 688 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 689 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 690 | else |
| 691 | errcode = 0; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 692 | fill_and_set_sslerror(NULL, PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 693 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 694 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 697 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 698 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 699 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 700 | char *server_hostname, |
| 701 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 702 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 703 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 704 | SSL_CTX *ctx = sslctx->ctx; |
Antoine Pitrou | 19fef69 | 2013-05-25 13:23:03 +0200 | [diff] [blame] | 705 | long mode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 707 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 708 | if (self == NULL) |
| 709 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 711 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 712 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 713 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 714 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 715 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 716 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 717 | self->server_hostname = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 718 | if (server_hostname != NULL) { |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 719 | PyObject *hostname = PyUnicode_Decode(server_hostname, strlen(server_hostname), |
| 720 | "idna", "strict"); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 721 | if (hostname == NULL) { |
| 722 | Py_DECREF(self); |
| 723 | return NULL; |
| 724 | } |
| 725 | self->server_hostname = hostname; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 726 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 727 | self->ssl_errno = 0; |
| 728 | self->c_errno = 0; |
| 729 | #ifdef MS_WINDOWS |
| 730 | self->ws_errno = 0; |
| 731 | #endif |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 732 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 733 | /* Make sure the SSL error state is initialized */ |
| 734 | (void) ERR_get_state(); |
| 735 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 737 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 738 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 739 | PySSL_END_ALLOW_THREADS |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 740 | SSL_set_app_data(self->ssl, self); |
| 741 | if (sock) { |
| 742 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 743 | } else { |
| 744 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 745 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 746 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 747 | BIO_up_ref(inbio->bio); |
| 748 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 749 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 750 | } |
Antoine Pitrou | 19fef69 | 2013-05-25 13:23:03 +0200 | [diff] [blame] | 751 | mode = SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER; |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 752 | #ifdef SSL_MODE_AUTO_RETRY |
Antoine Pitrou | 19fef69 | 2013-05-25 13:23:03 +0200 | [diff] [blame] | 753 | mode |= SSL_MODE_AUTO_RETRY; |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 754 | #endif |
Antoine Pitrou | 19fef69 | 2013-05-25 13:23:03 +0200 | [diff] [blame] | 755 | SSL_set_mode(self->ssl, mode); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 757 | #if HAVE_SNI |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 758 | if (server_hostname != NULL) |
| 759 | SSL_set_tlsext_host_name(self->ssl, server_hostname); |
| 760 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 761 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 762 | * to non-blocking mode (blocking is the default) |
| 763 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 764 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 765 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 766 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 767 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 769 | PySSL_BEGIN_ALLOW_THREADS |
| 770 | if (socket_type == PY_SSL_CLIENT) |
| 771 | SSL_set_connect_state(self->ssl); |
| 772 | else |
| 773 | SSL_set_accept_state(self->ssl); |
| 774 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 775 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 776 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 777 | if (sock != NULL) { |
| 778 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 779 | if (self->Socket == NULL) { |
| 780 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 781 | return NULL; |
| 782 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 783 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 784 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 787 | /* SSL object methods */ |
| 788 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 789 | /*[clinic input] |
| 790 | _ssl._SSLSocket.do_handshake |
| 791 | [clinic start generated code]*/ |
| 792 | |
| 793 | static PyObject * |
| 794 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 795 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 796 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 797 | int ret; |
| 798 | int err; |
| 799 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 800 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 801 | _PyTime_t timeout, deadline = 0; |
| 802 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 803 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 804 | if (sock) { |
| 805 | if (((PyObject*)sock) == Py_None) { |
| 806 | _setSSLError("Underlying socket connection gone", |
| 807 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 808 | return NULL; |
| 809 | } |
| 810 | Py_INCREF(sock); |
| 811 | |
| 812 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 813 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 814 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 815 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 816 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 817 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 818 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 819 | has_timeout = (timeout > 0); |
| 820 | if (has_timeout) |
| 821 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 822 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 823 | /* Actually negotiate SSL connection */ |
| 824 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 825 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 826 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 827 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 828 | _PySSL_UPDATE_ERRNO_IF(ret < 1, self, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 829 | PySSL_END_ALLOW_THREADS |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 830 | err = self->ssl_errno; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 831 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 832 | if (PyErr_CheckSignals()) |
| 833 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 834 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 835 | if (has_timeout) |
| 836 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 837 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 838 | if (err == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 839 | sockstate = PySSL_select(sock, 0, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 840 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 841 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 842 | } else { |
| 843 | sockstate = SOCKET_OPERATION_OK; |
| 844 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 845 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 846 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 847 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 848 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 849 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 850 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 851 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 852 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 853 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 854 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 855 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 856 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 857 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 858 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 859 | break; |
| 860 | } |
| 861 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 862 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 863 | if (ret < 1) |
| 864 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 865 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 866 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 867 | |
| 868 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 869 | Py_XDECREF(sock); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 870 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 873 | static PyObject * |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 874 | _asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 875 | { |
| 876 | char buf[X509_NAME_MAXLEN]; |
| 877 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 878 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 879 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 880 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 881 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 882 | if (buflen < 0) { |
| 883 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 884 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 885 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 886 | /* initial buffer is too small for oid + terminating null byte */ |
| 887 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 888 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 889 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 890 | /* allocate len + 1 for terminating NULL byte */ |
| 891 | namebuf = PyMem_Malloc(buflen + 1); |
| 892 | if (namebuf == NULL) { |
| 893 | PyErr_NoMemory(); |
| 894 | return NULL; |
| 895 | } |
| 896 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 897 | if (buflen < 0) { |
| 898 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 899 | goto done; |
| 900 | } |
| 901 | } |
| 902 | if (!buflen && no_name) { |
| 903 | Py_INCREF(Py_None); |
| 904 | name_obj = Py_None; |
| 905 | } |
| 906 | else { |
| 907 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 908 | } |
| 909 | |
| 910 | done: |
| 911 | if (buf != namebuf) { |
| 912 | PyMem_Free(namebuf); |
| 913 | } |
| 914 | return name_obj; |
| 915 | } |
| 916 | |
| 917 | static PyObject * |
| 918 | _create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 919 | { |
| 920 | Py_ssize_t buflen; |
| 921 | unsigned char *valuebuf = NULL; |
| 922 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 924 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 925 | if (buflen < 0) { |
| 926 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 927 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 928 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 929 | attr = Py_BuildValue("Ns#", _asn1obj2py(name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 930 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 931 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 935 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 936 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 937 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 938 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 939 | PyObject *rdnt; |
| 940 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 941 | int entry_count = X509_NAME_entry_count(xname); |
| 942 | X509_NAME_ENTRY *entry; |
| 943 | ASN1_OBJECT *name; |
| 944 | ASN1_STRING *value; |
| 945 | int index_counter; |
| 946 | int rdn_level = -1; |
| 947 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 949 | dn = PyList_New(0); |
| 950 | if (dn == NULL) |
| 951 | return NULL; |
| 952 | /* now create another tuple to hold the top-level RDN */ |
| 953 | rdn = PyList_New(0); |
| 954 | if (rdn == NULL) |
| 955 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 957 | for (index_counter = 0; |
| 958 | index_counter < entry_count; |
| 959 | index_counter++) |
| 960 | { |
| 961 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 963 | /* check to see if we've gotten to a new RDN */ |
| 964 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 965 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 966 | /* yes, new RDN */ |
| 967 | /* add old RDN to DN */ |
| 968 | rdnt = PyList_AsTuple(rdn); |
| 969 | Py_DECREF(rdn); |
| 970 | if (rdnt == NULL) |
| 971 | goto fail0; |
| 972 | retcode = PyList_Append(dn, rdnt); |
| 973 | Py_DECREF(rdnt); |
| 974 | if (retcode < 0) |
| 975 | goto fail0; |
| 976 | /* create new RDN */ |
| 977 | rdn = PyList_New(0); |
| 978 | if (rdn == NULL) |
| 979 | goto fail0; |
| 980 | } |
| 981 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 982 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 984 | /* now add this attribute to the current RDN */ |
| 985 | name = X509_NAME_ENTRY_get_object(entry); |
| 986 | value = X509_NAME_ENTRY_get_data(entry); |
| 987 | attr = _create_tuple_for_attribute(name, value); |
| 988 | /* |
| 989 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 990 | entry->set, |
| 991 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 992 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 993 | */ |
| 994 | if (attr == NULL) |
| 995 | goto fail1; |
| 996 | retcode = PyList_Append(rdn, attr); |
| 997 | Py_DECREF(attr); |
| 998 | if (retcode < 0) |
| 999 | goto fail1; |
| 1000 | } |
| 1001 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1002 | if (rdn != NULL) { |
| 1003 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1004 | rdnt = PyList_AsTuple(rdn); |
| 1005 | Py_DECREF(rdn); |
| 1006 | if (rdnt == NULL) |
| 1007 | goto fail0; |
| 1008 | retcode = PyList_Append(dn, rdnt); |
| 1009 | Py_DECREF(rdnt); |
| 1010 | if (retcode < 0) |
| 1011 | goto fail0; |
| 1012 | } |
| 1013 | else { |
| 1014 | Py_DECREF(rdn); |
| 1015 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1016 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1018 | /* convert list to tuple */ |
| 1019 | rdnt = PyList_AsTuple(dn); |
| 1020 | Py_DECREF(dn); |
| 1021 | if (rdnt == NULL) |
| 1022 | return NULL; |
| 1023 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1024 | |
| 1025 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1026 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1027 | |
| 1028 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1029 | Py_XDECREF(dn); |
| 1030 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static PyObject * |
| 1034 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1035 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1036 | /* this code follows the procedure outlined in |
| 1037 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1038 | function to extract the STACK_OF(GENERAL_NAME), |
| 1039 | then iterates through the stack to add the |
| 1040 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1041 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1042 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1043 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1044 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1045 | GENERAL_NAMES *names = NULL; |
| 1046 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1047 | BIO *biobuf = NULL; |
| 1048 | char buf[2048]; |
| 1049 | char *vptr; |
| 1050 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1052 | if (certificate == NULL) |
| 1053 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1055 | /* get a memory buffer */ |
| 1056 | biobuf = BIO_new(BIO_s_mem()); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1057 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1058 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1059 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1060 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1061 | if (peer_alt_names == Py_None) { |
| 1062 | peer_alt_names = PyList_New(0); |
| 1063 | if (peer_alt_names == NULL) |
| 1064 | goto fail; |
| 1065 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1067 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1068 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1069 | int gntype; |
| 1070 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1072 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1073 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1074 | switch (gntype) { |
| 1075 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1076 | /* we special-case DirName as a tuple of |
| 1077 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1079 | t = PyTuple_New(2); |
| 1080 | if (t == NULL) { |
| 1081 | goto fail; |
| 1082 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1084 | v = PyUnicode_FromString("DirName"); |
| 1085 | if (v == NULL) { |
| 1086 | Py_DECREF(t); |
| 1087 | goto fail; |
| 1088 | } |
| 1089 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1091 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 1092 | if (v == NULL) { |
| 1093 | Py_DECREF(t); |
| 1094 | goto fail; |
| 1095 | } |
| 1096 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1097 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1098 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1099 | case GEN_EMAIL: |
| 1100 | case GEN_DNS: |
| 1101 | case GEN_URI: |
| 1102 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1103 | correctly, CVE-2013-4238 */ |
| 1104 | t = PyTuple_New(2); |
| 1105 | if (t == NULL) |
| 1106 | goto fail; |
| 1107 | switch (gntype) { |
| 1108 | case GEN_EMAIL: |
| 1109 | v = PyUnicode_FromString("email"); |
| 1110 | as = name->d.rfc822Name; |
| 1111 | break; |
| 1112 | case GEN_DNS: |
| 1113 | v = PyUnicode_FromString("DNS"); |
| 1114 | as = name->d.dNSName; |
| 1115 | break; |
| 1116 | case GEN_URI: |
| 1117 | v = PyUnicode_FromString("URI"); |
| 1118 | as = name->d.uniformResourceIdentifier; |
| 1119 | break; |
| 1120 | } |
| 1121 | if (v == NULL) { |
| 1122 | Py_DECREF(t); |
| 1123 | goto fail; |
| 1124 | } |
| 1125 | PyTuple_SET_ITEM(t, 0, v); |
| 1126 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as), |
| 1127 | ASN1_STRING_length(as)); |
| 1128 | if (v == NULL) { |
| 1129 | Py_DECREF(t); |
| 1130 | goto fail; |
| 1131 | } |
| 1132 | PyTuple_SET_ITEM(t, 1, v); |
| 1133 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1134 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1135 | case GEN_RID: |
| 1136 | t = PyTuple_New(2); |
| 1137 | if (t == NULL) |
| 1138 | goto fail; |
| 1139 | |
| 1140 | v = PyUnicode_FromString("Registered ID"); |
| 1141 | if (v == NULL) { |
| 1142 | Py_DECREF(t); |
| 1143 | goto fail; |
| 1144 | } |
| 1145 | PyTuple_SET_ITEM(t, 0, v); |
| 1146 | |
| 1147 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1148 | if (len < 0) { |
| 1149 | Py_DECREF(t); |
| 1150 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1151 | goto fail; |
| 1152 | } else if (len >= (int)sizeof(buf)) { |
| 1153 | v = PyUnicode_FromString("<INVALID>"); |
| 1154 | } else { |
| 1155 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1156 | } |
| 1157 | if (v == NULL) { |
| 1158 | Py_DECREF(t); |
| 1159 | goto fail; |
| 1160 | } |
| 1161 | PyTuple_SET_ITEM(t, 1, v); |
| 1162 | break; |
| 1163 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1164 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1165 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1166 | switch (gntype) { |
| 1167 | /* check for new general name type */ |
| 1168 | case GEN_OTHERNAME: |
| 1169 | case GEN_X400: |
| 1170 | case GEN_EDIPARTY: |
| 1171 | case GEN_IPADD: |
| 1172 | case GEN_RID: |
| 1173 | break; |
| 1174 | default: |
| 1175 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1176 | "Unknown general name type %d", |
| 1177 | gntype) == -1) { |
| 1178 | goto fail; |
| 1179 | } |
| 1180 | break; |
| 1181 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1182 | (void) BIO_reset(biobuf); |
| 1183 | GENERAL_NAME_print(biobuf, name); |
| 1184 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1185 | if (len < 0) { |
| 1186 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1187 | goto fail; |
| 1188 | } |
| 1189 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1190 | if (vptr == NULL) { |
| 1191 | PyErr_Format(PyExc_ValueError, |
| 1192 | "Invalid value %.200s", |
| 1193 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1194 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1195 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1196 | t = PyTuple_New(2); |
| 1197 | if (t == NULL) |
| 1198 | goto fail; |
| 1199 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1200 | if (v == NULL) { |
| 1201 | Py_DECREF(t); |
| 1202 | goto fail; |
| 1203 | } |
| 1204 | PyTuple_SET_ITEM(t, 0, v); |
| 1205 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1206 | (len - (vptr - buf + 1))); |
| 1207 | if (v == NULL) { |
| 1208 | Py_DECREF(t); |
| 1209 | goto fail; |
| 1210 | } |
| 1211 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1212 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1213 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1215 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1217 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1218 | Py_DECREF(t); |
| 1219 | goto fail; |
| 1220 | } |
| 1221 | Py_DECREF(t); |
| 1222 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1223 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1224 | } |
| 1225 | BIO_free(biobuf); |
| 1226 | if (peer_alt_names != Py_None) { |
| 1227 | v = PyList_AsTuple(peer_alt_names); |
| 1228 | Py_DECREF(peer_alt_names); |
| 1229 | return v; |
| 1230 | } else { |
| 1231 | return peer_alt_names; |
| 1232 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1233 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1234 | |
| 1235 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1236 | if (biobuf != NULL) |
| 1237 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1239 | if (peer_alt_names != Py_None) { |
| 1240 | Py_XDECREF(peer_alt_names); |
| 1241 | } |
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 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1247 | _get_aia_uri(X509 *certificate, int nid) { |
| 1248 | PyObject *lst = NULL, *ostr = NULL; |
| 1249 | int i, result; |
| 1250 | AUTHORITY_INFO_ACCESS *info; |
| 1251 | |
| 1252 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1253 | if (info == NULL) |
| 1254 | return Py_None; |
| 1255 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1256 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1257 | return Py_None; |
| 1258 | } |
| 1259 | |
| 1260 | if ((lst = PyList_New(0)) == NULL) { |
| 1261 | goto fail; |
| 1262 | } |
| 1263 | |
| 1264 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1265 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1266 | ASN1_IA5STRING *uri; |
| 1267 | |
| 1268 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1269 | (ad->location->type != GEN_URI)) { |
| 1270 | continue; |
| 1271 | } |
| 1272 | uri = ad->location->d.uniformResourceIdentifier; |
| 1273 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1274 | uri->length); |
| 1275 | if (ostr == NULL) { |
| 1276 | goto fail; |
| 1277 | } |
| 1278 | result = PyList_Append(lst, ostr); |
| 1279 | Py_DECREF(ostr); |
| 1280 | if (result < 0) { |
| 1281 | goto fail; |
| 1282 | } |
| 1283 | } |
| 1284 | AUTHORITY_INFO_ACCESS_free(info); |
| 1285 | |
| 1286 | /* convert to tuple or None */ |
| 1287 | if (PyList_Size(lst) == 0) { |
| 1288 | Py_DECREF(lst); |
| 1289 | return Py_None; |
| 1290 | } else { |
| 1291 | PyObject *tup; |
| 1292 | tup = PyList_AsTuple(lst); |
| 1293 | Py_DECREF(lst); |
| 1294 | return tup; |
| 1295 | } |
| 1296 | |
| 1297 | fail: |
| 1298 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1299 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1300 | return NULL; |
| 1301 | } |
| 1302 | |
| 1303 | static PyObject * |
| 1304 | _get_crl_dp(X509 *certificate) { |
| 1305 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1306 | int i, j; |
| 1307 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1308 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1309 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1310 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1311 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1312 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1313 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1314 | lst = PyList_New(0); |
| 1315 | if (lst == NULL) |
| 1316 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1317 | |
| 1318 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1319 | DIST_POINT *dp; |
| 1320 | STACK_OF(GENERAL_NAME) *gns; |
| 1321 | |
| 1322 | dp = sk_DIST_POINT_value(dps, i); |
| 1323 | gns = dp->distpoint->name.fullname; |
| 1324 | |
| 1325 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1326 | GENERAL_NAME *gn; |
| 1327 | ASN1_IA5STRING *uri; |
| 1328 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1329 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1330 | |
| 1331 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1332 | if (gn->type != GEN_URI) { |
| 1333 | continue; |
| 1334 | } |
| 1335 | uri = gn->d.uniformResourceIdentifier; |
| 1336 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1337 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1338 | if (ouri == NULL) |
| 1339 | goto done; |
| 1340 | |
| 1341 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1342 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1343 | if (err < 0) |
| 1344 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1345 | } |
| 1346 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1347 | |
| 1348 | /* Convert to tuple. */ |
| 1349 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1350 | |
| 1351 | done: |
| 1352 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1353 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1354 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1358 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1360 | PyObject *retval = NULL; |
| 1361 | BIO *biobuf = NULL; |
| 1362 | PyObject *peer; |
| 1363 | PyObject *peer_alt_names = NULL; |
| 1364 | PyObject *issuer; |
| 1365 | PyObject *version; |
| 1366 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1367 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1368 | ASN1_INTEGER *serialNumber; |
| 1369 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1370 | int len, result; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1371 | ASN1_TIME *notBefore, *notAfter; |
| 1372 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1373 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1374 | retval = PyDict_New(); |
| 1375 | if (retval == NULL) |
| 1376 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1377 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1378 | peer = _create_tuple_for_X509_NAME( |
| 1379 | X509_get_subject_name(certificate)); |
| 1380 | if (peer == NULL) |
| 1381 | goto fail0; |
| 1382 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1383 | Py_DECREF(peer); |
| 1384 | goto fail0; |
| 1385 | } |
| 1386 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1388 | issuer = _create_tuple_for_X509_NAME( |
| 1389 | X509_get_issuer_name(certificate)); |
| 1390 | if (issuer == NULL) |
| 1391 | goto fail0; |
| 1392 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1393 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1394 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1395 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1396 | Py_DECREF(issuer); |
| 1397 | |
| 1398 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1399 | if (version == NULL) |
| 1400 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1401 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1402 | Py_DECREF(version); |
| 1403 | goto fail0; |
| 1404 | } |
| 1405 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1406 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1407 | /* get a memory buffer */ |
| 1408 | biobuf = BIO_new(BIO_s_mem()); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1409 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1410 | (void) BIO_reset(biobuf); |
| 1411 | serialNumber = X509_get_serialNumber(certificate); |
| 1412 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1413 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1414 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1415 | if (len < 0) { |
| 1416 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1417 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1418 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1419 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1420 | if (sn_obj == NULL) |
| 1421 | goto fail1; |
| 1422 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1423 | Py_DECREF(sn_obj); |
| 1424 | goto fail1; |
| 1425 | } |
| 1426 | Py_DECREF(sn_obj); |
| 1427 | |
| 1428 | (void) BIO_reset(biobuf); |
| 1429 | notBefore = X509_get_notBefore(certificate); |
| 1430 | ASN1_TIME_print(biobuf, notBefore); |
| 1431 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1432 | if (len < 0) { |
| 1433 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1434 | goto fail1; |
| 1435 | } |
| 1436 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1437 | if (pnotBefore == NULL) |
| 1438 | goto fail1; |
| 1439 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1440 | Py_DECREF(pnotBefore); |
| 1441 | goto fail1; |
| 1442 | } |
| 1443 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1445 | (void) BIO_reset(biobuf); |
| 1446 | notAfter = X509_get_notAfter(certificate); |
| 1447 | ASN1_TIME_print(biobuf, notAfter); |
| 1448 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1449 | if (len < 0) { |
| 1450 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1451 | goto fail1; |
| 1452 | } |
| 1453 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1454 | if (pnotAfter == NULL) |
| 1455 | goto fail1; |
| 1456 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1457 | Py_DECREF(pnotAfter); |
| 1458 | goto fail1; |
| 1459 | } |
| 1460 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1462 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1464 | peer_alt_names = _get_peer_alt_names(certificate); |
| 1465 | if (peer_alt_names == NULL) |
| 1466 | goto fail1; |
| 1467 | else if (peer_alt_names != Py_None) { |
| 1468 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1469 | peer_alt_names) < 0) { |
| 1470 | Py_DECREF(peer_alt_names); |
| 1471 | goto fail1; |
| 1472 | } |
| 1473 | Py_DECREF(peer_alt_names); |
| 1474 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1475 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1476 | /* Authority Information Access: OCSP URIs */ |
| 1477 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1478 | if (obj == NULL) { |
| 1479 | goto fail1; |
| 1480 | } else if (obj != Py_None) { |
| 1481 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1482 | Py_DECREF(obj); |
| 1483 | if (result < 0) { |
| 1484 | goto fail1; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1489 | if (obj == NULL) { |
| 1490 | goto fail1; |
| 1491 | } else if (obj != Py_None) { |
| 1492 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1493 | Py_DECREF(obj); |
| 1494 | if (result < 0) { |
| 1495 | goto fail1; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* CDP (CRL distribution points) */ |
| 1500 | obj = _get_crl_dp(certificate); |
| 1501 | if (obj == NULL) { |
| 1502 | goto fail1; |
| 1503 | } else if (obj != Py_None) { |
| 1504 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1505 | Py_DECREF(obj); |
| 1506 | if (result < 0) { |
| 1507 | goto fail1; |
| 1508 | } |
| 1509 | } |
| 1510 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1511 | BIO_free(biobuf); |
| 1512 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1513 | |
| 1514 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1515 | if (biobuf != NULL) |
| 1516 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1517 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1518 | Py_XDECREF(retval); |
| 1519 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1520 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1521 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1522 | static PyObject * |
| 1523 | _certificate_to_der(X509 *certificate) |
| 1524 | { |
| 1525 | unsigned char *bytes_buf = NULL; |
| 1526 | int len; |
| 1527 | PyObject *retval; |
| 1528 | |
| 1529 | bytes_buf = NULL; |
| 1530 | len = i2d_X509(certificate, &bytes_buf); |
| 1531 | if (len < 0) { |
| 1532 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | /* this is actually an immutable bytes sequence */ |
| 1536 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1537 | OPENSSL_free(bytes_buf); |
| 1538 | return retval; |
| 1539 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1540 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1541 | /*[clinic input] |
| 1542 | _ssl._test_decode_cert |
| 1543 | path: object(converter="PyUnicode_FSConverter") |
| 1544 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1545 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1546 | [clinic start generated code]*/ |
| 1547 | |
| 1548 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1549 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1550 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1551 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1552 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1553 | X509 *x=NULL; |
| 1554 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1556 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1557 | PyErr_SetString(PySSLErrorObject, |
| 1558 | "Can't malloc memory to read file"); |
| 1559 | goto fail0; |
| 1560 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1561 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1562 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1563 | PyErr_SetString(PySSLErrorObject, |
| 1564 | "Can't open file"); |
| 1565 | goto fail0; |
| 1566 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1568 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 1569 | if (x == NULL) { |
| 1570 | PyErr_SetString(PySSLErrorObject, |
| 1571 | "Error decoding PEM-encoded file"); |
| 1572 | goto fail0; |
| 1573 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1575 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1576 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1577 | |
| 1578 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1579 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1580 | if (cert != NULL) BIO_free(cert); |
| 1581 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1585 | /*[clinic input] |
| 1586 | _ssl._SSLSocket.peer_certificate |
| 1587 | der as binary_mode: bool = False |
| 1588 | / |
| 1589 | |
| 1590 | Returns the certificate for the peer. |
| 1591 | |
| 1592 | If no certificate was provided, returns None. If a certificate was |
| 1593 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1594 | returns a dict containing information about the peer certificate. |
| 1595 | |
| 1596 | If the optional argument is True, returns a DER-encoded copy of the |
| 1597 | peer certificate, or None if no certificate was provided. This will |
| 1598 | return the certificate even if it wasn't validated. |
| 1599 | [clinic start generated code]*/ |
| 1600 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1601 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1602 | _ssl__SSLSocket_peer_certificate_impl(PySSLSocket *self, int binary_mode) |
| 1603 | /*[clinic end generated code: output=f0dc3e4d1d818a1d input=8281bd1d193db843]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1604 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1605 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1606 | X509 *peer_cert; |
| 1607 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1608 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1609 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1610 | PyErr_SetString(PyExc_ValueError, |
| 1611 | "handshake not done yet"); |
| 1612 | return NULL; |
| 1613 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1614 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1615 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1616 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1617 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1618 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1619 | /* return cert in DER-encoded format */ |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1620 | result = _certificate_to_der(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1621 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1622 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1623 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1624 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1625 | else |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1626 | result = _decode_certificate(peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1627 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1628 | X509_free(peer_cert); |
| 1629 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1632 | static PyObject * |
| 1633 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1634 | { |
| 1635 | const char *cipher_name, *cipher_protocol; |
| 1636 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1637 | if (retval == NULL) |
| 1638 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1639 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1640 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1641 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1642 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1643 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1644 | } else { |
| 1645 | v = PyUnicode_FromString(cipher_name); |
| 1646 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1647 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1648 | PyTuple_SET_ITEM(retval, 0, v); |
| 1649 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1650 | |
| 1651 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1652 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1653 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1654 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1655 | } else { |
| 1656 | v = PyUnicode_FromString(cipher_protocol); |
| 1657 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1658 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1659 | PyTuple_SET_ITEM(retval, 1, v); |
| 1660 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1661 | |
| 1662 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1663 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1664 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1665 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1666 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1667 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1668 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1669 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1670 | Py_DECREF(retval); |
| 1671 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1674 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 1675 | static PyObject * |
| 1676 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1677 | { |
| 1678 | const char *cipher_name, *cipher_protocol; |
| 1679 | |
| 1680 | unsigned long cipher_id; |
| 1681 | int alg_bits, strength_bits, len; |
| 1682 | char buf[512] = {0}; |
| 1683 | #if OPENSSL_VERSION_1_1 |
| 1684 | int aead, nid; |
| 1685 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
| 1686 | #endif |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1687 | |
| 1688 | /* can be NULL */ |
| 1689 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1690 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1691 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1692 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1693 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 1694 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1695 | if (len > 1 && buf[len-1] == '\n') |
| 1696 | buf[len-1] = '\0'; |
| 1697 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 1698 | |
| 1699 | #if OPENSSL_VERSION_1_1 |
| 1700 | aead = SSL_CIPHER_is_aead(cipher); |
| 1701 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 1702 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1703 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 1704 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1705 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 1706 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1707 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 1708 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1709 | #endif |
| 1710 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1711 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1712 | "{sksssssssisi" |
| 1713 | #if OPENSSL_VERSION_1_1 |
| 1714 | "sOssssssss" |
| 1715 | #endif |
| 1716 | "}", |
| 1717 | "id", cipher_id, |
| 1718 | "name", cipher_name, |
| 1719 | "protocol", cipher_protocol, |
| 1720 | "description", buf, |
| 1721 | "strength_bits", strength_bits, |
| 1722 | "alg_bits", alg_bits |
| 1723 | #if OPENSSL_VERSION_1_1 |
| 1724 | ,"aead", aead ? Py_True : Py_False, |
| 1725 | "symmetric", skcipher, |
| 1726 | "digest", digest, |
| 1727 | "kea", kx, |
| 1728 | "auth", auth |
| 1729 | #endif |
| 1730 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1731 | } |
| 1732 | #endif |
| 1733 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1734 | /*[clinic input] |
| 1735 | _ssl._SSLSocket.shared_ciphers |
| 1736 | [clinic start generated code]*/ |
| 1737 | |
| 1738 | static PyObject * |
| 1739 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 1740 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1741 | { |
| 1742 | STACK_OF(SSL_CIPHER) *ciphers; |
| 1743 | int i; |
| 1744 | PyObject *res; |
| 1745 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1746 | ciphers = SSL_get_ciphers(self->ssl); |
| 1747 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1748 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1749 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 1750 | if (!res) |
| 1751 | return NULL; |
| 1752 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 1753 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 1754 | if (!tup) { |
| 1755 | Py_DECREF(res); |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | PyList_SET_ITEM(res, i, tup); |
| 1759 | } |
| 1760 | return res; |
| 1761 | } |
| 1762 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1763 | /*[clinic input] |
| 1764 | _ssl._SSLSocket.cipher |
| 1765 | [clinic start generated code]*/ |
| 1766 | |
| 1767 | static PyObject * |
| 1768 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 1769 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1770 | { |
| 1771 | const SSL_CIPHER *current; |
| 1772 | |
| 1773 | if (self->ssl == NULL) |
| 1774 | Py_RETURN_NONE; |
| 1775 | current = SSL_get_current_cipher(self->ssl); |
| 1776 | if (current == NULL) |
| 1777 | Py_RETURN_NONE; |
| 1778 | return cipher_to_tuple(current); |
| 1779 | } |
| 1780 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1781 | /*[clinic input] |
| 1782 | _ssl._SSLSocket.version |
| 1783 | [clinic start generated code]*/ |
| 1784 | |
| 1785 | static PyObject * |
| 1786 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 1787 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1788 | { |
| 1789 | const char *version; |
| 1790 | |
| 1791 | if (self->ssl == NULL) |
| 1792 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 1793 | if (!SSL_is_init_finished(self->ssl)) { |
| 1794 | /* handshake not finished */ |
| 1795 | Py_RETURN_NONE; |
| 1796 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1797 | version = SSL_get_version(self->ssl); |
| 1798 | if (!strcmp(version, "unknown")) |
| 1799 | Py_RETURN_NONE; |
| 1800 | return PyUnicode_FromString(version); |
| 1801 | } |
| 1802 | |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 1803 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1804 | /*[clinic input] |
| 1805 | _ssl._SSLSocket.selected_npn_protocol |
| 1806 | [clinic start generated code]*/ |
| 1807 | |
| 1808 | static PyObject * |
| 1809 | _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self) |
| 1810 | /*[clinic end generated code: output=b91d494cd207ecf6 input=c28fde139204b826]*/ |
| 1811 | { |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1812 | const unsigned char *out; |
| 1813 | unsigned int outlen; |
| 1814 | |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 1815 | SSL_get0_next_proto_negotiated(self->ssl, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1816 | &out, &outlen); |
| 1817 | |
| 1818 | if (out == NULL) |
| 1819 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 1820 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
| 1821 | } |
| 1822 | #endif |
| 1823 | |
| 1824 | #ifdef HAVE_ALPN |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1825 | /*[clinic input] |
| 1826 | _ssl._SSLSocket.selected_alpn_protocol |
| 1827 | [clinic start generated code]*/ |
| 1828 | |
| 1829 | static PyObject * |
| 1830 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 1831 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 1832 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 1833 | const unsigned char *out; |
| 1834 | unsigned int outlen; |
| 1835 | |
| 1836 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 1837 | |
| 1838 | if (out == NULL) |
| 1839 | Py_RETURN_NONE; |
| 1840 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1841 | } |
| 1842 | #endif |
| 1843 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1844 | /*[clinic input] |
| 1845 | _ssl._SSLSocket.compression |
| 1846 | [clinic start generated code]*/ |
| 1847 | |
| 1848 | static PyObject * |
| 1849 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 1850 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 1851 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1852 | #ifdef OPENSSL_NO_COMP |
| 1853 | Py_RETURN_NONE; |
| 1854 | #else |
| 1855 | const COMP_METHOD *comp_method; |
| 1856 | const char *short_name; |
| 1857 | |
| 1858 | if (self->ssl == NULL) |
| 1859 | Py_RETURN_NONE; |
| 1860 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1861 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1862 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 1863 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1864 | if (short_name == NULL) |
| 1865 | Py_RETURN_NONE; |
| 1866 | return PyUnicode_DecodeFSDefault(short_name); |
| 1867 | #endif |
| 1868 | } |
| 1869 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1870 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 1871 | Py_INCREF(self->ctx); |
| 1872 | return self->ctx; |
| 1873 | } |
| 1874 | |
| 1875 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 1876 | void *closure) { |
| 1877 | |
| 1878 | if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 1879 | #if !HAVE_SNI |
| 1880 | PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 1881 | "context is not supported by your OpenSSL library"); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 1882 | return -1; |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 1883 | #else |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1884 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1885 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1886 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 1887 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1888 | } else { |
| 1889 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 1897 | "_setter_context(ctx)\n\ |
| 1898 | \ |
| 1899 | This changes the context associated with the SSLSocket. This is typically\n\ |
| 1900 | used from within a callback function set by the set_servername_callback\n\ |
| 1901 | on the SSLContext to change the certificate information associated with the\n\ |
| 1902 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 1903 | |
| 1904 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1905 | static PyObject * |
| 1906 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 1907 | { |
| 1908 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 1909 | } |
| 1910 | |
| 1911 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 1912 | "Whether this is a server-side socket."); |
| 1913 | |
| 1914 | static PyObject * |
| 1915 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 1916 | { |
| 1917 | if (self->server_hostname == NULL) |
| 1918 | Py_RETURN_NONE; |
| 1919 | Py_INCREF(self->server_hostname); |
| 1920 | return self->server_hostname; |
| 1921 | } |
| 1922 | |
| 1923 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 1924 | "The currently set server hostname (for SNI)."); |
| 1925 | |
| 1926 | static PyObject * |
| 1927 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 1928 | { |
| 1929 | PyObject *owner; |
| 1930 | |
| 1931 | if (self->owner == NULL) |
| 1932 | Py_RETURN_NONE; |
| 1933 | |
| 1934 | owner = PyWeakref_GetObject(self->owner); |
| 1935 | Py_INCREF(owner); |
| 1936 | return owner; |
| 1937 | } |
| 1938 | |
| 1939 | static int |
| 1940 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 1941 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1942 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1943 | if (self->owner == NULL) |
| 1944 | return -1; |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 1949 | "The Python-level owner of this object.\ |
| 1950 | Passed as \"self\" in servername callback."); |
| 1951 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1952 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1953 | static void PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1954 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1955 | if (self->ssl) |
| 1956 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1957 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1958 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 1959 | Py_XDECREF(self->server_hostname); |
| 1960 | Py_XDECREF(self->owner); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1961 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1964 | /* 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] | 1965 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1966 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1967 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1968 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1969 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1970 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1971 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1972 | int rc; |
| 1973 | #ifdef HAVE_POLL |
| 1974 | struct pollfd pollfd; |
| 1975 | _PyTime_t ms; |
| 1976 | #else |
| 1977 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1978 | fd_set fds; |
| 1979 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1980 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1982 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1983 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1984 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 1985 | else if (timeout < 0) { |
| 1986 | if (s->sock_timeout > 0) |
| 1987 | return SOCKET_HAS_TIMED_OUT; |
| 1988 | else |
| 1989 | return SOCKET_IS_BLOCKING; |
| 1990 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1992 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 1993 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1994 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1996 | /* Prefer poll, if available, since you can poll() any fd |
| 1997 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1998 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 1999 | pollfd.fd = s->sock_fd; |
| 2000 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2001 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2002 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2003 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2004 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2005 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2006 | PySSL_BEGIN_ALLOW_THREADS |
| 2007 | rc = poll(&pollfd, 1, (int)ms); |
| 2008 | PySSL_END_ALLOW_THREADS |
| 2009 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2010 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2011 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2012 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2013 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2014 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2015 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2016 | FD_ZERO(&fds); |
| 2017 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2018 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2019 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2020 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2021 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2022 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2023 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2024 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2025 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2026 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2027 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2028 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2029 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2030 | (when we are able to write or when there's something to read) */ |
| 2031 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2034 | /*[clinic input] |
| 2035 | _ssl._SSLSocket.write |
| 2036 | b: Py_buffer |
| 2037 | / |
| 2038 | |
| 2039 | Writes the bytes-like object b into the SSL object. |
| 2040 | |
| 2041 | Returns the number of bytes written. |
| 2042 | [clinic start generated code]*/ |
| 2043 | |
| 2044 | static PyObject * |
| 2045 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2046 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2047 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2048 | int len; |
| 2049 | int sockstate; |
| 2050 | int err; |
| 2051 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2052 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2053 | _PyTime_t timeout, deadline = 0; |
| 2054 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2055 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2056 | if (sock != NULL) { |
| 2057 | if (((PyObject*)sock) == Py_None) { |
| 2058 | _setSSLError("Underlying socket connection gone", |
| 2059 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2060 | return NULL; |
| 2061 | } |
| 2062 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2065 | if (b->len > INT_MAX) { |
Victor Stinner | 6efa965 | 2013-06-25 00:42:31 +0200 | [diff] [blame] | 2066 | PyErr_Format(PyExc_OverflowError, |
| 2067 | "string longer than %d bytes", INT_MAX); |
| 2068 | goto error; |
| 2069 | } |
| 2070 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2071 | if (sock != NULL) { |
| 2072 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2073 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2074 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2075 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2076 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2077 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2078 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2079 | has_timeout = (timeout > 0); |
| 2080 | if (has_timeout) |
| 2081 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2082 | |
| 2083 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2084 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2085 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2086 | "The write operation timed out"); |
| 2087 | goto error; |
| 2088 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2089 | PyErr_SetString(PySSLErrorObject, |
| 2090 | "Underlying socket has been closed."); |
| 2091 | goto error; |
| 2092 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2093 | PyErr_SetString(PySSLErrorObject, |
| 2094 | "Underlying socket too large for select()."); |
| 2095 | goto error; |
| 2096 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2097 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2098 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2099 | PySSL_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2100 | len = SSL_write(self->ssl, b->buf, (int)b->len); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2101 | _PySSL_UPDATE_ERRNO_IF(len <= 0, self, len); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2102 | PySSL_END_ALLOW_THREADS |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2103 | err = self->ssl_errno; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2104 | |
| 2105 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2106 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2107 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2108 | if (has_timeout) |
| 2109 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2110 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2111 | if (err == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2112 | sockstate = PySSL_select(sock, 0, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2113 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2114 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2115 | } else { |
| 2116 | sockstate = SOCKET_OPERATION_OK; |
| 2117 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2118 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2119 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2120 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2121 | "The write operation timed out"); |
| 2122 | goto error; |
| 2123 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 2124 | PyErr_SetString(PySSLErrorObject, |
| 2125 | "Underlying socket has been closed."); |
| 2126 | goto error; |
| 2127 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2128 | break; |
| 2129 | } |
| 2130 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2131 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2132 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2133 | if (len > 0) |
| 2134 | return PyLong_FromLong(len); |
| 2135 | else |
| 2136 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2137 | |
| 2138 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2139 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2140 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2143 | /*[clinic input] |
| 2144 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2145 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2146 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2147 | [clinic start generated code]*/ |
| 2148 | |
| 2149 | static PyObject * |
| 2150 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2151 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2152 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2153 | int count = 0; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2155 | PySSL_BEGIN_ALLOW_THREADS |
| 2156 | count = SSL_pending(self->ssl); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2157 | _PySSL_UPDATE_ERRNO_IF(count < 0, self, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2158 | PySSL_END_ALLOW_THREADS |
| 2159 | if (count < 0) |
| 2160 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2161 | else |
| 2162 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2165 | /*[clinic input] |
| 2166 | _ssl._SSLSocket.read |
| 2167 | size as len: int |
| 2168 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2169 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2170 | ] |
| 2171 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2172 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2173 | Read up to size bytes from the SSL socket. |
| 2174 | [clinic start generated code]*/ |
| 2175 | |
| 2176 | static PyObject * |
| 2177 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2178 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2179 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2180 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2181 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2182 | char *mem; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2183 | int count; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2184 | int sockstate; |
| 2185 | int err; |
| 2186 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2187 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2188 | _PyTime_t timeout, deadline = 0; |
| 2189 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2190 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2191 | if (!group_right_1 && len < 0) { |
| 2192 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2196 | if (sock != NULL) { |
| 2197 | if (((PyObject*)sock) == Py_None) { |
| 2198 | _setSSLError("Underlying socket connection gone", |
| 2199 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2205 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2206 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2207 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2208 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2209 | if (len == 0) { |
| 2210 | Py_XDECREF(sock); |
| 2211 | return dest; |
| 2212 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2213 | mem = PyBytes_AS_STRING(dest); |
| 2214 | } |
| 2215 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2216 | mem = buffer->buf; |
| 2217 | if (len <= 0 || len > buffer->len) { |
| 2218 | len = (int) buffer->len; |
| 2219 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2220 | PyErr_SetString(PyExc_OverflowError, |
| 2221 | "maximum length can't fit in a C 'int'"); |
| 2222 | goto error; |
| 2223 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2224 | if (len == 0) { |
| 2225 | count = 0; |
| 2226 | goto done; |
| 2227 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2228 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2231 | if (sock != NULL) { |
| 2232 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2233 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2234 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2235 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2236 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2237 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2238 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2239 | has_timeout = (timeout > 0); |
| 2240 | if (has_timeout) |
| 2241 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2242 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2243 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2244 | PySSL_BEGIN_ALLOW_THREADS |
| 2245 | count = SSL_read(self->ssl, mem, len); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2246 | _PySSL_UPDATE_ERRNO_IF(count <= 0, self, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2247 | PySSL_END_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2248 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2249 | if (PyErr_CheckSignals()) |
| 2250 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2251 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2252 | if (has_timeout) |
| 2253 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2254 | |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2255 | err = self->ssl_errno; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2256 | if (err == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2257 | sockstate = PySSL_select(sock, 0, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2258 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2259 | sockstate = PySSL_select(sock, 1, timeout); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2260 | } else if (err == SSL_ERROR_ZERO_RETURN && |
| 2261 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2262 | { |
| 2263 | count = 0; |
| 2264 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2265 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2266 | else |
| 2267 | sockstate = SOCKET_OPERATION_OK; |
| 2268 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2269 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2270 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2271 | "The read operation timed out"); |
| 2272 | goto error; |
| 2273 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2274 | break; |
| 2275 | } |
| 2276 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2277 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2278 | if (count <= 0) { |
| 2279 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2280 | goto error; |
| 2281 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2282 | |
| 2283 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2284 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2285 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2286 | _PyBytes_Resize(&dest, count); |
| 2287 | return dest; |
| 2288 | } |
| 2289 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2290 | return PyLong_FromLong(count); |
| 2291 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2292 | |
| 2293 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2294 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2295 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2296 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2297 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2300 | /*[clinic input] |
| 2301 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2302 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2303 | Does the SSL shutdown handshake with the remote end. |
| 2304 | |
| 2305 | Returns the underlying socket object. |
| 2306 | [clinic start generated code]*/ |
| 2307 | |
| 2308 | static PyObject * |
| 2309 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
| 2310 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=ede2cc1a2ddf0ee4]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2311 | { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2312 | int err, sockstate, nonblocking; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2313 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2314 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2315 | _PyTime_t timeout, deadline = 0; |
| 2316 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2318 | if (sock != NULL) { |
| 2319 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2320 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2321 | _setSSLError("Underlying socket connection gone", |
| 2322 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2323 | return NULL; |
| 2324 | } |
| 2325 | Py_INCREF(sock); |
| 2326 | |
| 2327 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2328 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2329 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2330 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2331 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2332 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2333 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2334 | has_timeout = (timeout > 0); |
| 2335 | if (has_timeout) |
| 2336 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2337 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2338 | while (1) { |
| 2339 | PySSL_BEGIN_ALLOW_THREADS |
| 2340 | /* Disable read-ahead so that unwrap can work correctly. |
| 2341 | * Otherwise OpenSSL might read in too much data, |
| 2342 | * eating clear text data that happens to be |
| 2343 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2344 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2345 | * function is used and the shutdown_seen_zero != 0 |
| 2346 | * condition is met. |
| 2347 | */ |
| 2348 | if (self->shutdown_seen_zero) |
| 2349 | SSL_set_read_ahead(self->ssl, 0); |
| 2350 | err = SSL_shutdown(self->ssl); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2351 | _PySSL_UPDATE_ERRNO_IF(err < 0, self, err); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2352 | PySSL_END_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2353 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2354 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
| 2355 | if (err > 0) |
| 2356 | break; |
| 2357 | if (err == 0) { |
| 2358 | /* Don't loop endlessly; instead preserve legacy |
| 2359 | behaviour of trying SSL_shutdown() only twice. |
| 2360 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2361 | if (++zeros > 1) |
| 2362 | break; |
| 2363 | /* Shutdown was sent, now try receiving */ |
| 2364 | self->shutdown_seen_zero = 1; |
| 2365 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2368 | if (has_timeout) |
| 2369 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2370 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2371 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2372 | _PySSL_UPDATE_ERRNO(self, err); |
| 2373 | if (self->ssl_errno == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2374 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2375 | else if (self->ssl_errno == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2376 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2377 | else |
| 2378 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2379 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2380 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame^] | 2381 | if (self->ssl_errno == SSL_ERROR_WANT_READ) |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2382 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2383 | "The read operation timed out"); |
| 2384 | else |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 2385 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2386 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2387 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2388 | } |
| 2389 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2390 | PyErr_SetString(PySSLErrorObject, |
| 2391 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2392 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2393 | } |
| 2394 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2395 | /* Retain the SSL error code */ |
| 2396 | break; |
| 2397 | } |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2399 | if (err < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2400 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2401 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2402 | } |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2403 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2404 | /* It's already INCREF'ed */ |
| 2405 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2406 | else |
| 2407 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2408 | |
| 2409 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2410 | Py_XDECREF(sock); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2411 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2414 | /*[clinic input] |
| 2415 | _ssl._SSLSocket.tls_unique_cb |
| 2416 | |
| 2417 | Returns the 'tls-unique' channel binding data, as defined by RFC 5929. |
| 2418 | |
| 2419 | If the TLS handshake is not yet complete, None is returned. |
| 2420 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2422 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2423 | _ssl__SSLSocket_tls_unique_cb_impl(PySSLSocket *self) |
| 2424 | /*[clinic end generated code: output=f3a832d603f586af input=439525c7b3d8d34d]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2425 | { |
| 2426 | PyObject *retval = NULL; |
| 2427 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2428 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2429 | |
| 2430 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2431 | /* if session is resumed XOR we are the client */ |
| 2432 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2433 | } |
| 2434 | else { |
| 2435 | /* if a new session XOR we are the server */ |
| 2436 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2437 | } |
| 2438 | |
| 2439 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2440 | if (len == 0) |
| 2441 | Py_RETURN_NONE; |
| 2442 | |
| 2443 | retval = PyBytes_FromStringAndSize(buf, len); |
| 2444 | |
| 2445 | return retval; |
| 2446 | } |
| 2447 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2448 | #ifdef OPENSSL_VERSION_1_1 |
| 2449 | |
| 2450 | static SSL_SESSION* |
| 2451 | _ssl_session_dup(SSL_SESSION *session) { |
| 2452 | SSL_SESSION *newsession = NULL; |
| 2453 | int slen; |
| 2454 | unsigned char *senc = NULL, *p; |
| 2455 | const unsigned char *const_p; |
| 2456 | |
| 2457 | if (session == NULL) { |
| 2458 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2459 | goto error; |
| 2460 | } |
| 2461 | |
| 2462 | /* get length */ |
| 2463 | slen = i2d_SSL_SESSION(session, NULL); |
| 2464 | if (slen == 0 || slen > 0xFF00) { |
| 2465 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2466 | goto error; |
| 2467 | } |
| 2468 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2469 | PyErr_NoMemory(); |
| 2470 | goto error; |
| 2471 | } |
| 2472 | p = senc; |
| 2473 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2474 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2475 | goto error; |
| 2476 | } |
| 2477 | const_p = senc; |
| 2478 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2479 | if (session == NULL) { |
| 2480 | goto error; |
| 2481 | } |
| 2482 | PyMem_Free(senc); |
| 2483 | return newsession; |
| 2484 | error: |
| 2485 | if (senc != NULL) { |
| 2486 | PyMem_Free(senc); |
| 2487 | } |
| 2488 | return NULL; |
| 2489 | } |
| 2490 | #endif |
| 2491 | |
| 2492 | static PyObject * |
| 2493 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2494 | /* get_session can return sessions from a server-side connection, |
| 2495 | * it does not check for handshake done or client socket. */ |
| 2496 | PySSLSession *pysess; |
| 2497 | SSL_SESSION *session; |
| 2498 | |
| 2499 | #ifdef OPENSSL_VERSION_1_1 |
| 2500 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2501 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2502 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2503 | if (session == NULL) { |
| 2504 | Py_RETURN_NONE; |
| 2505 | } |
| 2506 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2507 | return NULL; |
| 2508 | } |
| 2509 | #else |
| 2510 | session = SSL_get1_session(self->ssl); |
| 2511 | if (session == NULL) { |
| 2512 | Py_RETURN_NONE; |
| 2513 | } |
| 2514 | #endif |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2515 | pysess = PyObject_GC_New(PySSLSession, &PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2516 | if (pysess == NULL) { |
| 2517 | SSL_SESSION_free(session); |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | |
| 2521 | assert(self->ctx); |
| 2522 | pysess->ctx = self->ctx; |
| 2523 | Py_INCREF(pysess->ctx); |
| 2524 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2525 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2526 | return (PyObject *)pysess; |
| 2527 | } |
| 2528 | |
| 2529 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2530 | void *closure) |
| 2531 | { |
| 2532 | PySSLSession *pysess; |
| 2533 | #ifdef OPENSSL_VERSION_1_1 |
| 2534 | SSL_SESSION *session; |
| 2535 | #endif |
| 2536 | int result; |
| 2537 | |
| 2538 | if (!PySSLSession_Check(value)) { |
| 2539 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
| 2540 | return -1; |
| 2541 | } |
| 2542 | pysess = (PySSLSession *)value; |
| 2543 | |
| 2544 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2545 | PyErr_SetString(PyExc_ValueError, |
| 2546 | "Session refers to a different SSLContext."); |
| 2547 | return -1; |
| 2548 | } |
| 2549 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2550 | PyErr_SetString(PyExc_ValueError, |
| 2551 | "Cannot set session for server-side SSLSocket."); |
| 2552 | return -1; |
| 2553 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2554 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2555 | PyErr_SetString(PyExc_ValueError, |
| 2556 | "Cannot set session after handshake."); |
| 2557 | return -1; |
| 2558 | } |
| 2559 | #ifdef OPENSSL_VERSION_1_1 |
| 2560 | /* duplicate session */ |
| 2561 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2562 | return -1; |
| 2563 | } |
| 2564 | result = SSL_set_session(self->ssl, session); |
| 2565 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2566 | SSL_SESSION_free(session); |
| 2567 | #else |
| 2568 | result = SSL_set_session(self->ssl, pysess->session); |
| 2569 | #endif |
| 2570 | if (result == 0) { |
| 2571 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2572 | return -1; |
| 2573 | } |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
| 2577 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2578 | "_setter_session(session)\n\ |
| 2579 | \ |
| 2580 | Get / set SSLSession."); |
| 2581 | |
| 2582 | static PyObject * |
| 2583 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2584 | if (SSL_session_reused(self->ssl)) { |
| 2585 | Py_RETURN_TRUE; |
| 2586 | } else { |
| 2587 | Py_RETURN_FALSE; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2592 | "Was the client session reused during handshake?"); |
| 2593 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2594 | static PyGetSetDef ssl_getsetlist[] = { |
| 2595 | {"context", (getter) PySSL_get_context, |
| 2596 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2597 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2598 | PySSL_get_server_side_doc}, |
| 2599 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2600 | PySSL_get_server_hostname_doc}, |
| 2601 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2602 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2603 | {"session", (getter) PySSL_get_session, |
| 2604 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2605 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2606 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2607 | {NULL}, /* sentinel */ |
| 2608 | }; |
| 2609 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2610 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2611 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2612 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2613 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2614 | _SSL__SSLSOCKET_PENDING_METHODDEF |
| 2615 | _SSL__SSLSOCKET_PEER_CERTIFICATE_METHODDEF |
| 2616 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2617 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2618 | _SSL__SSLSOCKET_VERSION_METHODDEF |
| 2619 | _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF |
| 2620 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2621 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2622 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
| 2623 | _SSL__SSLSOCKET_TLS_UNIQUE_CB_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2624 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2625 | }; |
| 2626 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2627 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2628 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2629 | "_ssl._SSLSocket", /*tp_name*/ |
| 2630 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2631 | 0, /*tp_itemsize*/ |
| 2632 | /* methods */ |
| 2633 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 2634 | 0, /*tp_print*/ |
| 2635 | 0, /*tp_getattr*/ |
| 2636 | 0, /*tp_setattr*/ |
| 2637 | 0, /*tp_reserved*/ |
| 2638 | 0, /*tp_repr*/ |
| 2639 | 0, /*tp_as_number*/ |
| 2640 | 0, /*tp_as_sequence*/ |
| 2641 | 0, /*tp_as_mapping*/ |
| 2642 | 0, /*tp_hash*/ |
| 2643 | 0, /*tp_call*/ |
| 2644 | 0, /*tp_str*/ |
| 2645 | 0, /*tp_getattro*/ |
| 2646 | 0, /*tp_setattro*/ |
| 2647 | 0, /*tp_as_buffer*/ |
| 2648 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2649 | 0, /*tp_doc*/ |
| 2650 | 0, /*tp_traverse*/ |
| 2651 | 0, /*tp_clear*/ |
| 2652 | 0, /*tp_richcompare*/ |
| 2653 | 0, /*tp_weaklistoffset*/ |
| 2654 | 0, /*tp_iter*/ |
| 2655 | 0, /*tp_iternext*/ |
| 2656 | PySSLMethods, /*tp_methods*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2657 | 0, /*tp_members*/ |
| 2658 | ssl_getsetlist, /*tp_getset*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2659 | }; |
| 2660 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2661 | |
| 2662 | /* |
| 2663 | * _SSLContext objects |
| 2664 | */ |
| 2665 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2666 | static int |
| 2667 | _set_verify_mode(SSL_CTX *ctx, enum py_ssl_cert_requirements n) |
| 2668 | { |
| 2669 | int mode; |
| 2670 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 2671 | |
| 2672 | switch(n) { |
| 2673 | case PY_SSL_CERT_NONE: |
| 2674 | mode = SSL_VERIFY_NONE; |
| 2675 | break; |
| 2676 | case PY_SSL_CERT_OPTIONAL: |
| 2677 | mode = SSL_VERIFY_PEER; |
| 2678 | break; |
| 2679 | case PY_SSL_CERT_REQUIRED: |
| 2680 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2681 | break; |
| 2682 | default: |
| 2683 | PyErr_SetString(PyExc_ValueError, |
| 2684 | "invalid value for verify_mode"); |
| 2685 | return -1; |
| 2686 | } |
| 2687 | /* keep current verify cb */ |
| 2688 | verify_cb = SSL_CTX_get_verify_callback(ctx); |
| 2689 | SSL_CTX_set_verify(ctx, mode, verify_cb); |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2693 | /*[clinic input] |
| 2694 | @classmethod |
| 2695 | _ssl._SSLContext.__new__ |
| 2696 | protocol as proto_version: int |
| 2697 | / |
| 2698 | [clinic start generated code]*/ |
| 2699 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2700 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2701 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 2702 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2703 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2704 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2705 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2706 | SSL_CTX *ctx = NULL; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2707 | int result; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2708 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 2709 | unsigned long libver; |
Berker Peksag | dfcb041 | 2016-04-14 16:48:48 +0300 | [diff] [blame] | 2710 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2711 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2712 | PySSL_BEGIN_ALLOW_THREADS |
| 2713 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 2714 | ctx = SSL_CTX_new(TLSv1_method()); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 2715 | #if HAVE_TLSv1_2 |
| 2716 | else if (proto_version == PY_SSL_VERSION_TLS1_1) |
| 2717 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 2718 | else if (proto_version == PY_SSL_VERSION_TLS1_2) |
| 2719 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 2720 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2721 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2722 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 2723 | ctx = SSL_CTX_new(SSLv3_method()); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2724 | #endif |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2725 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2726 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 2727 | ctx = SSL_CTX_new(SSLv2_method()); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2728 | #endif |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2729 | else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2730 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2731 | else if (proto_version == PY_SSL_VERSION_TLS_CLIENT) |
| 2732 | ctx = SSL_CTX_new(TLS_client_method()); |
| 2733 | else if (proto_version == PY_SSL_VERSION_TLS_SERVER) |
| 2734 | ctx = SSL_CTX_new(TLS_server_method()); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2735 | else |
| 2736 | proto_version = -1; |
| 2737 | PySSL_END_ALLOW_THREADS |
| 2738 | |
| 2739 | if (proto_version == -1) { |
| 2740 | PyErr_SetString(PyExc_ValueError, |
| 2741 | "invalid protocol version"); |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | if (ctx == NULL) { |
Christian Heimes | 17c9ac9 | 2017-09-07 14:14:00 -0700 | [diff] [blame] | 2745 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2746 | return NULL; |
| 2747 | } |
| 2748 | |
| 2749 | assert(type != NULL && type->tp_alloc != NULL); |
| 2750 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 2751 | if (self == NULL) { |
| 2752 | SSL_CTX_free(ctx); |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | self->ctx = ctx; |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 2756 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Christian Heimes | 5cb31c9 | 2012-09-20 12:42:54 +0200 | [diff] [blame] | 2757 | self->npn_protocols = NULL; |
| 2758 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2759 | #ifdef HAVE_ALPN |
| 2760 | self->alpn_protocols = NULL; |
| 2761 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2762 | #ifndef OPENSSL_NO_TLSEXT |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 2763 | self->set_hostname = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2764 | #endif |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 2765 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2766 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 2767 | self->check_hostname = 1; |
| 2768 | if (_set_verify_mode(self->ctx, PY_SSL_CERT_REQUIRED) == -1) { |
| 2769 | Py_DECREF(self); |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | } else { |
| 2773 | self->check_hostname = 0; |
| 2774 | if (_set_verify_mode(self->ctx, PY_SSL_CERT_NONE) == -1) { |
| 2775 | Py_DECREF(self); |
| 2776 | return NULL; |
| 2777 | } |
| 2778 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2779 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2780 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 2781 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 2782 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 2783 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 2784 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2785 | /* Minimal security flags for server and client side context. |
| 2786 | * Client sockets ignore server-side parameters. */ |
| 2787 | #ifdef SSL_OP_NO_COMPRESSION |
| 2788 | options |= SSL_OP_NO_COMPRESSION; |
| 2789 | #endif |
| 2790 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 2791 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 2792 | #endif |
| 2793 | #ifdef SSL_OP_SINGLE_DH_USE |
| 2794 | options |= SSL_OP_SINGLE_DH_USE; |
| 2795 | #endif |
| 2796 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 2797 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 2798 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2799 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2800 | |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2801 | /* A bare minimum cipher list without completly broken cipher suites. |
| 2802 | * It's far from perfect but gives users a better head start. */ |
| 2803 | if (proto_version != PY_SSL_VERSION_SSL2) { |
| 2804 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL:!MD5"); |
| 2805 | } else { |
| 2806 | /* SSLv2 needs MD5 */ |
| 2807 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 2808 | } |
| 2809 | if (result == 0) { |
| 2810 | Py_DECREF(self); |
| 2811 | ERR_clear_error(); |
| 2812 | PyErr_SetString(PySSLErrorObject, |
| 2813 | "No cipher can be selected."); |
| 2814 | return NULL; |
| 2815 | } |
| 2816 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 2817 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 2818 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
| 2819 | usage for no cost at all. However, don't do this for OpenSSL versions |
| 2820 | between 1.0.1 and 1.0.1h or 1.0.0 and 1.0.0m, which are affected by CVE |
| 2821 | 2014-0198. I can't find exactly which beta fixed this CVE, so be |
| 2822 | conservative and assume it wasn't fixed until release. We do this check |
| 2823 | at runtime to avoid problems from the dynamic linker. |
| 2824 | See #25672 for more on this. */ |
| 2825 | libver = SSLeay(); |
| 2826 | if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) && |
| 2827 | !(libver >= 0x10000000UL && libver < 0x100000dfUL)) { |
| 2828 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
| 2829 | } |
| 2830 | #endif |
| 2831 | |
| 2832 | |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 2833 | #if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 2834 | /* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use |
| 2835 | prime256v1 by default. This is Apache mod_ssl's initialization |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2836 | policy, so we should be safe. OpenSSL 1.1 has it enabled by default. |
| 2837 | */ |
Donald Stufft | 8ae264c | 2017-03-02 11:45:29 -0500 | [diff] [blame] | 2838 | #if defined(SSL_CTX_set_ecdh_auto) |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 2839 | SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 2840 | #else |
| 2841 | { |
| 2842 | EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 2843 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 2844 | EC_KEY_free(key); |
| 2845 | } |
| 2846 | #endif |
| 2847 | #endif |
| 2848 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 2849 | #define SID_CTX "Python" |
| 2850 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 2851 | sizeof(SID_CTX)); |
| 2852 | #undef SID_CTX |
| 2853 | |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 2854 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 2855 | { |
| 2856 | /* Improve trust chain building when cross-signed intermediate |
| 2857 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 2858 | X509_STORE *store = SSL_CTX_get_cert_store(self->ctx); |
| 2859 | X509_STORE_set_flags(store, X509_V_FLAG_TRUSTED_FIRST); |
| 2860 | } |
| 2861 | #endif |
| 2862 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2863 | return (PyObject *)self; |
| 2864 | } |
| 2865 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2866 | static int |
| 2867 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 2868 | { |
| 2869 | #ifndef OPENSSL_NO_TLSEXT |
| 2870 | Py_VISIT(self->set_hostname); |
| 2871 | #endif |
| 2872 | return 0; |
| 2873 | } |
| 2874 | |
| 2875 | static int |
| 2876 | context_clear(PySSLContext *self) |
| 2877 | { |
| 2878 | #ifndef OPENSSL_NO_TLSEXT |
| 2879 | Py_CLEAR(self->set_hostname); |
| 2880 | #endif |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2884 | static void |
| 2885 | context_dealloc(PySSLContext *self) |
| 2886 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 2887 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 2888 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2889 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2890 | SSL_CTX_free(self->ctx); |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 2891 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2892 | PyMem_FREE(self->npn_protocols); |
| 2893 | #endif |
| 2894 | #ifdef HAVE_ALPN |
| 2895 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2896 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2897 | Py_TYPE(self)->tp_free(self); |
| 2898 | } |
| 2899 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2900 | /*[clinic input] |
| 2901 | _ssl._SSLContext.set_ciphers |
| 2902 | cipherlist: str |
| 2903 | / |
| 2904 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2905 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2906 | static PyObject * |
| 2907 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 2908 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 2909 | { |
| 2910 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2911 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 2912 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 2913 | otherwise the error will be reported again when another SSL call |
| 2914 | is done. */ |
| 2915 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2916 | PyErr_SetString(PySSLErrorObject, |
| 2917 | "No cipher can be selected."); |
| 2918 | return NULL; |
| 2919 | } |
| 2920 | Py_RETURN_NONE; |
| 2921 | } |
| 2922 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2923 | #if OPENSSL_VERSION_NUMBER >= 0x10002000UL |
| 2924 | /*[clinic input] |
| 2925 | _ssl._SSLContext.get_ciphers |
| 2926 | [clinic start generated code]*/ |
| 2927 | |
| 2928 | static PyObject * |
| 2929 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 2930 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 2931 | { |
| 2932 | SSL *ssl = NULL; |
| 2933 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2934 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 2935 | int i=0; |
| 2936 | PyObject *result = NULL, *dct; |
| 2937 | |
| 2938 | ssl = SSL_new(self->ctx); |
| 2939 | if (ssl == NULL) { |
| 2940 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2941 | goto exit; |
| 2942 | } |
| 2943 | sk = SSL_get_ciphers(ssl); |
| 2944 | |
| 2945 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 2946 | if (result == NULL) { |
| 2947 | goto exit; |
| 2948 | } |
| 2949 | |
| 2950 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 2951 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 2952 | dct = cipher_to_dict(cipher); |
| 2953 | if (dct == NULL) { |
| 2954 | Py_CLEAR(result); |
| 2955 | goto exit; |
| 2956 | } |
| 2957 | PyList_SET_ITEM(result, i, dct); |
| 2958 | } |
| 2959 | |
| 2960 | exit: |
| 2961 | if (ssl != NULL) |
| 2962 | SSL_free(ssl); |
| 2963 | return result; |
| 2964 | |
| 2965 | } |
| 2966 | #endif |
| 2967 | |
| 2968 | |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 2969 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) || defined(HAVE_ALPN) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2970 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 2971 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 2972 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 2973 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2974 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 2975 | int ret; |
| 2976 | if (client_protocols == NULL) { |
| 2977 | client_protocols = (unsigned char *)""; |
| 2978 | client_protocols_len = 0; |
| 2979 | } |
| 2980 | if (server_protocols == NULL) { |
| 2981 | server_protocols = (unsigned char *)""; |
| 2982 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2983 | } |
| 2984 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 2985 | ret = SSL_select_next_proto(out, outlen, |
| 2986 | server_protocols, server_protocols_len, |
| 2987 | client_protocols, client_protocols_len); |
| 2988 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 2989 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2990 | |
| 2991 | return SSL_TLSEXT_ERR_OK; |
| 2992 | } |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 2993 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2994 | |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 2995 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2996 | /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ |
| 2997 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 2998 | _advertiseNPN_cb(SSL *s, |
| 2999 | const unsigned char **data, unsigned int *len, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3000 | void *args) |
| 3001 | { |
| 3002 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3003 | |
| 3004 | if (ssl_ctx->npn_protocols == NULL) { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3005 | *data = (unsigned char *)""; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3006 | *len = 0; |
| 3007 | } else { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3008 | *data = ssl_ctx->npn_protocols; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3009 | *len = ssl_ctx->npn_protocols_len; |
| 3010 | } |
| 3011 | |
| 3012 | return SSL_TLSEXT_ERR_OK; |
| 3013 | } |
| 3014 | /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */ |
| 3015 | static int |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 3016 | _selectNPN_cb(SSL *s, |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3017 | unsigned char **out, unsigned char *outlen, |
| 3018 | const unsigned char *server, unsigned int server_len, |
| 3019 | void *args) |
| 3020 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3021 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3022 | return do_protocol_selection(0, out, outlen, server, server_len, |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3023 | ctx->npn_protocols, ctx->npn_protocols_len); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3024 | } |
| 3025 | #endif |
| 3026 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3027 | /*[clinic input] |
| 3028 | _ssl._SSLContext._set_npn_protocols |
| 3029 | protos: Py_buffer |
| 3030 | / |
| 3031 | [clinic start generated code]*/ |
| 3032 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3033 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3034 | _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self, |
| 3035 | Py_buffer *protos) |
| 3036 | /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3037 | { |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 3038 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3039 | PyMem_Free(self->npn_protocols); |
| 3040 | self->npn_protocols = PyMem_Malloc(protos->len); |
| 3041 | if (self->npn_protocols == NULL) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3042 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3043 | memcpy(self->npn_protocols, protos->buf, protos->len); |
| 3044 | self->npn_protocols_len = (int) protos->len; |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3045 | |
| 3046 | /* set both server and client callbacks, because the context can |
| 3047 | * be used to create both types of sockets */ |
| 3048 | SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 3049 | _advertiseNPN_cb, |
| 3050 | self); |
| 3051 | SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 3052 | _selectNPN_cb, |
| 3053 | self); |
| 3054 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 3055 | Py_RETURN_NONE; |
| 3056 | #else |
| 3057 | PyErr_SetString(PyExc_NotImplementedError, |
| 3058 | "The NPN extension requires OpenSSL 1.0.1 or later."); |
| 3059 | return NULL; |
| 3060 | #endif |
| 3061 | } |
| 3062 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3063 | #ifdef HAVE_ALPN |
| 3064 | static int |
| 3065 | _selectALPN_cb(SSL *s, |
| 3066 | const unsigned char **out, unsigned char *outlen, |
| 3067 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3068 | void *args) |
| 3069 | { |
| 3070 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3071 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3072 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3073 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3074 | } |
| 3075 | #endif |
| 3076 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3077 | /*[clinic input] |
| 3078 | _ssl._SSLContext._set_alpn_protocols |
| 3079 | protos: Py_buffer |
| 3080 | / |
| 3081 | [clinic start generated code]*/ |
| 3082 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3083 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3084 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3085 | Py_buffer *protos) |
| 3086 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3087 | { |
| 3088 | #ifdef HAVE_ALPN |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3089 | if (protos->len > UINT_MAX) { |
| 3090 | PyErr_Format(PyExc_OverflowError, |
| 3091 | "protocols longer than %d bytes", UINT_MAX); |
| 3092 | return NULL; |
| 3093 | } |
| 3094 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3095 | PyMem_FREE(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3096 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3097 | if (!self->alpn_protocols) |
| 3098 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3099 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3100 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3101 | |
| 3102 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3103 | return PyErr_NoMemory(); |
| 3104 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3105 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3106 | Py_RETURN_NONE; |
| 3107 | #else |
| 3108 | PyErr_SetString(PyExc_NotImplementedError, |
| 3109 | "The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 3110 | return NULL; |
| 3111 | #endif |
| 3112 | } |
| 3113 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3114 | static PyObject * |
| 3115 | get_verify_mode(PySSLContext *self, void *c) |
| 3116 | { |
| 3117 | switch (SSL_CTX_get_verify_mode(self->ctx)) { |
| 3118 | case SSL_VERIFY_NONE: |
| 3119 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3120 | case SSL_VERIFY_PEER: |
| 3121 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3122 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3123 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3124 | } |
| 3125 | PyErr_SetString(PySSLErrorObject, |
| 3126 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3127 | return NULL; |
| 3128 | } |
| 3129 | |
| 3130 | static int |
| 3131 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3132 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3133 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3134 | if (!PyArg_Parse(arg, "i", &n)) |
| 3135 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3136 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3137 | PyErr_SetString(PyExc_ValueError, |
| 3138 | "Cannot set verify_mode to CERT_NONE when " |
| 3139 | "check_hostname is enabled."); |
| 3140 | return -1; |
| 3141 | } |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3142 | return _set_verify_mode(self->ctx, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3146 | get_verify_flags(PySSLContext *self, void *c) |
| 3147 | { |
| 3148 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3149 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3150 | unsigned long flags; |
| 3151 | |
| 3152 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3153 | param = X509_STORE_get0_param(store); |
| 3154 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3155 | return PyLong_FromUnsignedLong(flags); |
| 3156 | } |
| 3157 | |
| 3158 | static int |
| 3159 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3160 | { |
| 3161 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3162 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3163 | unsigned long new_flags, flags, set, clear; |
| 3164 | |
| 3165 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3166 | return -1; |
| 3167 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3168 | param = X509_STORE_get0_param(store); |
| 3169 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3170 | clear = flags & ~new_flags; |
| 3171 | set = ~flags & new_flags; |
| 3172 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3173 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3174 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3175 | return -1; |
| 3176 | } |
| 3177 | } |
| 3178 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3179 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3180 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3181 | return -1; |
| 3182 | } |
| 3183 | } |
| 3184 | return 0; |
| 3185 | } |
| 3186 | |
| 3187 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3188 | get_options(PySSLContext *self, void *c) |
| 3189 | { |
| 3190 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3191 | } |
| 3192 | |
| 3193 | static int |
| 3194 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3195 | { |
| 3196 | long new_opts, opts, set, clear; |
| 3197 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3198 | return -1; |
| 3199 | opts = SSL_CTX_get_options(self->ctx); |
| 3200 | clear = opts & ~new_opts; |
| 3201 | set = ~opts & new_opts; |
| 3202 | if (clear) { |
| 3203 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 3204 | SSL_CTX_clear_options(self->ctx, clear); |
| 3205 | #else |
| 3206 | PyErr_SetString(PyExc_ValueError, |
| 3207 | "can't clear options before OpenSSL 0.9.8m"); |
| 3208 | return -1; |
| 3209 | #endif |
| 3210 | } |
| 3211 | if (set) |
| 3212 | SSL_CTX_set_options(self->ctx, set); |
| 3213 | return 0; |
| 3214 | } |
| 3215 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3216 | static PyObject * |
| 3217 | get_check_hostname(PySSLContext *self, void *c) |
| 3218 | { |
| 3219 | return PyBool_FromLong(self->check_hostname); |
| 3220 | } |
| 3221 | |
| 3222 | static int |
| 3223 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3224 | { |
| 3225 | int check_hostname; |
| 3226 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3227 | return -1; |
| 3228 | if (check_hostname && |
| 3229 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
| 3230 | PyErr_SetString(PyExc_ValueError, |
| 3231 | "check_hostname needs a SSL context with either " |
| 3232 | "CERT_OPTIONAL or CERT_REQUIRED"); |
| 3233 | return -1; |
| 3234 | } |
| 3235 | self->check_hostname = check_hostname; |
| 3236 | return 0; |
| 3237 | } |
| 3238 | |
| 3239 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3240 | typedef struct { |
| 3241 | PyThreadState *thread_state; |
| 3242 | PyObject *callable; |
| 3243 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3244 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3245 | int error; |
| 3246 | } _PySSLPasswordInfo; |
| 3247 | |
| 3248 | static int |
| 3249 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3250 | const char *bad_type_error) |
| 3251 | { |
| 3252 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3253 | from a unicode, bytes, or byte array object. |
| 3254 | The password field will be dynamically allocated and must be freed |
| 3255 | by the caller */ |
| 3256 | PyObject *password_bytes = NULL; |
| 3257 | const char *data = NULL; |
| 3258 | Py_ssize_t size; |
| 3259 | |
| 3260 | if (PyUnicode_Check(password)) { |
| 3261 | password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL); |
| 3262 | if (!password_bytes) { |
| 3263 | goto error; |
| 3264 | } |
| 3265 | data = PyBytes_AS_STRING(password_bytes); |
| 3266 | size = PyBytes_GET_SIZE(password_bytes); |
| 3267 | } else if (PyBytes_Check(password)) { |
| 3268 | data = PyBytes_AS_STRING(password); |
| 3269 | size = PyBytes_GET_SIZE(password); |
| 3270 | } else if (PyByteArray_Check(password)) { |
| 3271 | data = PyByteArray_AS_STRING(password); |
| 3272 | size = PyByteArray_GET_SIZE(password); |
| 3273 | } else { |
| 3274 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3275 | goto error; |
| 3276 | } |
| 3277 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3278 | if (size > (Py_ssize_t)INT_MAX) { |
| 3279 | PyErr_Format(PyExc_ValueError, |
| 3280 | "password cannot be longer than %d bytes", INT_MAX); |
| 3281 | goto error; |
| 3282 | } |
| 3283 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3284 | PyMem_Free(pw_info->password); |
| 3285 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3286 | if (!pw_info->password) { |
| 3287 | PyErr_SetString(PyExc_MemoryError, |
| 3288 | "unable to allocate password buffer"); |
| 3289 | goto error; |
| 3290 | } |
| 3291 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3292 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3293 | |
| 3294 | Py_XDECREF(password_bytes); |
| 3295 | return 1; |
| 3296 | |
| 3297 | error: |
| 3298 | Py_XDECREF(password_bytes); |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
| 3302 | static int |
| 3303 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3304 | { |
| 3305 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3306 | PyObject *fn_ret = NULL; |
| 3307 | |
| 3308 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3309 | |
| 3310 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3311 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3312 | if (!fn_ret) { |
| 3313 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3314 | core python API, so we could use it to add a frame here */ |
| 3315 | goto error; |
| 3316 | } |
| 3317 | |
| 3318 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3319 | "password callback must return a string")) { |
| 3320 | goto error; |
| 3321 | } |
| 3322 | Py_CLEAR(fn_ret); |
| 3323 | } |
| 3324 | |
| 3325 | if (pw_info->size > size) { |
| 3326 | PyErr_Format(PyExc_ValueError, |
| 3327 | "password cannot be longer than %d bytes", size); |
| 3328 | goto error; |
| 3329 | } |
| 3330 | |
| 3331 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3332 | memcpy(buf, pw_info->password, pw_info->size); |
| 3333 | return pw_info->size; |
| 3334 | |
| 3335 | error: |
| 3336 | Py_XDECREF(fn_ret); |
| 3337 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3338 | pw_info->error = 1; |
| 3339 | return -1; |
| 3340 | } |
| 3341 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3342 | /*[clinic input] |
| 3343 | _ssl._SSLContext.load_cert_chain |
| 3344 | certfile: object |
| 3345 | keyfile: object = NULL |
| 3346 | password: object = NULL |
| 3347 | |
| 3348 | [clinic start generated code]*/ |
| 3349 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3350 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3351 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3352 | PyObject *keyfile, PyObject *password) |
| 3353 | /*[clinic end generated code: output=9480bc1c380e2095 input=7cf9ac673cbee6fc]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3354 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3355 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3356 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3357 | 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] | 3358 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3359 | int r; |
| 3360 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3361 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3362 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3363 | if (keyfile == Py_None) |
| 3364 | keyfile = NULL; |
| 3365 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
| 3366 | PyErr_SetString(PyExc_TypeError, |
| 3367 | "certfile should be a valid filesystem path"); |
| 3368 | return NULL; |
| 3369 | } |
| 3370 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
| 3371 | PyErr_SetString(PyExc_TypeError, |
| 3372 | "keyfile should be a valid filesystem path"); |
| 3373 | goto error; |
| 3374 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3375 | if (password && password != Py_None) { |
| 3376 | if (PyCallable_Check(password)) { |
| 3377 | pw_info.callable = password; |
| 3378 | } else if (!_pwinfo_set(&pw_info, password, |
| 3379 | "password should be a string or callable")) { |
| 3380 | goto error; |
| 3381 | } |
| 3382 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3383 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3384 | } |
| 3385 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3386 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3387 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3388 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3389 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3390 | if (pw_info.error) { |
| 3391 | ERR_clear_error(); |
| 3392 | /* the password callback has already set the error information */ |
| 3393 | } |
| 3394 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3395 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3396 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3397 | } |
| 3398 | else { |
| 3399 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3400 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3401 | goto error; |
| 3402 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3403 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3404 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3405 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 3406 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3407 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 3408 | Py_CLEAR(keyfile_bytes); |
| 3409 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3410 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3411 | if (pw_info.error) { |
| 3412 | ERR_clear_error(); |
| 3413 | /* the password callback has already set the error information */ |
| 3414 | } |
| 3415 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3416 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3417 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3418 | } |
| 3419 | else { |
| 3420 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3421 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3422 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3423 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3424 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3425 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3426 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3427 | if (r != 1) { |
| 3428 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3429 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3430 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3431 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3432 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3433 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3434 | Py_RETURN_NONE; |
| 3435 | |
| 3436 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3437 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3438 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3439 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3440 | Py_XDECREF(keyfile_bytes); |
| 3441 | Py_XDECREF(certfile_bytes); |
| 3442 | return NULL; |
| 3443 | } |
| 3444 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3445 | /* internal helper function, returns -1 on error |
| 3446 | */ |
| 3447 | static int |
| 3448 | _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len, |
| 3449 | int filetype) |
| 3450 | { |
| 3451 | BIO *biobuf = NULL; |
| 3452 | X509_STORE *store; |
| 3453 | int retval = 0, err, loaded = 0; |
| 3454 | |
| 3455 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 3456 | |
| 3457 | if (len <= 0) { |
| 3458 | PyErr_SetString(PyExc_ValueError, |
| 3459 | "Empty certificate data"); |
| 3460 | return -1; |
| 3461 | } else if (len > INT_MAX) { |
| 3462 | PyErr_SetString(PyExc_OverflowError, |
| 3463 | "Certificate data is too long."); |
| 3464 | return -1; |
| 3465 | } |
| 3466 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 3467 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3468 | if (biobuf == NULL) { |
| 3469 | _setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 3470 | return -1; |
| 3471 | } |
| 3472 | |
| 3473 | store = SSL_CTX_get_cert_store(self->ctx); |
| 3474 | assert(store != NULL); |
| 3475 | |
| 3476 | while (1) { |
| 3477 | X509 *cert = NULL; |
| 3478 | int r; |
| 3479 | |
| 3480 | if (filetype == SSL_FILETYPE_ASN1) { |
| 3481 | cert = d2i_X509_bio(biobuf, NULL); |
| 3482 | } else { |
| 3483 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3484 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 3485 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 3486 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3487 | } |
| 3488 | if (cert == NULL) { |
| 3489 | break; |
| 3490 | } |
| 3491 | r = X509_STORE_add_cert(store, cert); |
| 3492 | X509_free(cert); |
| 3493 | if (!r) { |
| 3494 | err = ERR_peek_last_error(); |
| 3495 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 3496 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 3497 | /* cert already in hash table, not an error */ |
| 3498 | ERR_clear_error(); |
| 3499 | } else { |
| 3500 | break; |
| 3501 | } |
| 3502 | } |
| 3503 | loaded++; |
| 3504 | } |
| 3505 | |
| 3506 | err = ERR_peek_last_error(); |
| 3507 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 3508 | (loaded > 0) && |
| 3509 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 3510 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 3511 | /* EOF ASN1 file, not an error */ |
| 3512 | ERR_clear_error(); |
| 3513 | retval = 0; |
| 3514 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 3515 | (loaded > 0) && |
| 3516 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 3517 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3518 | /* EOF PEM file, not an error */ |
| 3519 | ERR_clear_error(); |
| 3520 | retval = 0; |
| 3521 | } else { |
| 3522 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3523 | retval = -1; |
| 3524 | } |
| 3525 | |
| 3526 | BIO_free(biobuf); |
| 3527 | return retval; |
| 3528 | } |
| 3529 | |
| 3530 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3531 | /*[clinic input] |
| 3532 | _ssl._SSLContext.load_verify_locations |
| 3533 | cafile: object = NULL |
| 3534 | capath: object = NULL |
| 3535 | cadata: object = NULL |
| 3536 | |
| 3537 | [clinic start generated code]*/ |
| 3538 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3539 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3540 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 3541 | PyObject *cafile, |
| 3542 | PyObject *capath, |
| 3543 | PyObject *cadata) |
| 3544 | /*[clinic end generated code: output=454c7e41230ca551 input=997f1fb3a784ef88]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3545 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3546 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 3547 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3548 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3549 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3550 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3551 | if (cafile == Py_None) |
| 3552 | cafile = NULL; |
| 3553 | if (capath == Py_None) |
| 3554 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3555 | if (cadata == Py_None) |
| 3556 | cadata = NULL; |
| 3557 | |
| 3558 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3559 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3560 | "cafile, capath and cadata cannot be all omitted"); |
| 3561 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3562 | } |
| 3563 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
| 3564 | PyErr_SetString(PyExc_TypeError, |
| 3565 | "cafile should be a valid filesystem path"); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3566 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3567 | } |
| 3568 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3569 | PyErr_SetString(PyExc_TypeError, |
| 3570 | "capath should be a valid filesystem path"); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3571 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3572 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3573 | |
| 3574 | /* validata cadata type and load cadata */ |
| 3575 | if (cadata) { |
| 3576 | Py_buffer buf; |
| 3577 | PyObject *cadata_ascii = NULL; |
| 3578 | |
| 3579 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE) == 0) { |
| 3580 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 3581 | PyBuffer_Release(&buf); |
| 3582 | PyErr_SetString(PyExc_TypeError, |
| 3583 | "cadata should be a contiguous buffer with " |
| 3584 | "a single dimension"); |
| 3585 | goto error; |
| 3586 | } |
| 3587 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 3588 | PyBuffer_Release(&buf); |
| 3589 | if (r == -1) { |
| 3590 | goto error; |
| 3591 | } |
| 3592 | } else { |
| 3593 | PyErr_Clear(); |
| 3594 | cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 3595 | if (cadata_ascii == NULL) { |
| 3596 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 3597 | "cadata should be an ASCII string or a " |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3598 | "bytes-like object"); |
| 3599 | goto error; |
| 3600 | } |
| 3601 | r = _add_ca_certs(self, |
| 3602 | PyBytes_AS_STRING(cadata_ascii), |
| 3603 | PyBytes_GET_SIZE(cadata_ascii), |
| 3604 | SSL_FILETYPE_PEM); |
| 3605 | Py_DECREF(cadata_ascii); |
| 3606 | if (r == -1) { |
| 3607 | goto error; |
| 3608 | } |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | /* load cafile or capath */ |
| 3613 | if (cafile || capath) { |
| 3614 | if (cafile) |
| 3615 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 3616 | if (capath) |
| 3617 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 3618 | PySSL_BEGIN_ALLOW_THREADS |
| 3619 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 3620 | PySSL_END_ALLOW_THREADS |
| 3621 | if (r != 1) { |
| 3622 | ok = 0; |
| 3623 | if (errno != 0) { |
| 3624 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3625 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3626 | } |
| 3627 | else { |
| 3628 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3629 | } |
| 3630 | goto error; |
| 3631 | } |
| 3632 | } |
| 3633 | goto end; |
| 3634 | |
| 3635 | error: |
| 3636 | ok = 0; |
| 3637 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3638 | Py_XDECREF(cafile_bytes); |
| 3639 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3640 | if (ok) { |
| 3641 | Py_RETURN_NONE; |
| 3642 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3643 | return NULL; |
| 3644 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3647 | /*[clinic input] |
| 3648 | _ssl._SSLContext.load_dh_params |
| 3649 | path as filepath: object |
| 3650 | / |
| 3651 | |
| 3652 | [clinic start generated code]*/ |
| 3653 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3654 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3655 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 3656 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3657 | { |
| 3658 | FILE *f; |
| 3659 | DH *dh; |
| 3660 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3661 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 3662 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3663 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 3664 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3665 | errno = 0; |
| 3666 | PySSL_BEGIN_ALLOW_THREADS |
| 3667 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 3668 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3669 | PySSL_END_ALLOW_THREADS |
| 3670 | if (dh == NULL) { |
| 3671 | if (errno != 0) { |
| 3672 | ERR_clear_error(); |
| 3673 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 3674 | } |
| 3675 | else { |
| 3676 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3677 | } |
| 3678 | return NULL; |
| 3679 | } |
| 3680 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 3681 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3682 | DH_free(dh); |
| 3683 | Py_RETURN_NONE; |
| 3684 | } |
| 3685 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3686 | /*[clinic input] |
| 3687 | _ssl._SSLContext._wrap_socket |
| 3688 | sock: object(subclass_of="PySocketModule.Sock_Type") |
| 3689 | server_side: int |
| 3690 | server_hostname as hostname_obj: object = None |
| 3691 | |
| 3692 | [clinic start generated code]*/ |
| 3693 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3694 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3695 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
| 3696 | int server_side, PyObject *hostname_obj) |
| 3697 | /*[clinic end generated code: output=6973e4b60995e933 input=83859b9156ddfc63]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3698 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3699 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3700 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3701 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3702 | /* server_hostname is either None (or absent), or to be encoded |
| 3703 | using the idna encoding. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3704 | if (hostname_obj != Py_None) { |
| 3705 | if (!PyArg_Parse(hostname_obj, "et", "idna", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3706 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3707 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3708 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3709 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 3710 | server_side, hostname, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3711 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3712 | if (hostname != NULL) |
| 3713 | PyMem_Free(hostname); |
| 3714 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3717 | /*[clinic input] |
| 3718 | _ssl._SSLContext._wrap_bio |
| 3719 | incoming: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 3720 | outgoing: object(subclass_of="&PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 3721 | server_side: int |
| 3722 | server_hostname as hostname_obj: object = None |
| 3723 | |
| 3724 | [clinic start generated code]*/ |
| 3725 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 3726 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3727 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 3728 | PySSLMemoryBIO *outgoing, int server_side, |
| 3729 | PyObject *hostname_obj) |
| 3730 | /*[clinic end generated code: output=4fe4ba75ad95940d input=17725ecdac0bf220]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3731 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3732 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3733 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3734 | |
| 3735 | /* server_hostname is either None (or absent), or to be encoded |
| 3736 | using the idna encoding. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3737 | if (hostname_obj != Py_None) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3738 | if (!PyArg_Parse(hostname_obj, "et", "idna", &hostname)) |
| 3739 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
| 3743 | incoming, outgoing); |
| 3744 | |
| 3745 | PyMem_Free(hostname); |
| 3746 | return res; |
| 3747 | } |
| 3748 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3749 | /*[clinic input] |
| 3750 | _ssl._SSLContext.session_stats |
| 3751 | [clinic start generated code]*/ |
| 3752 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3753 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3754 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 3755 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 3756 | { |
| 3757 | int r; |
| 3758 | PyObject *value, *stats = PyDict_New(); |
| 3759 | if (!stats) |
| 3760 | return NULL; |
| 3761 | |
| 3762 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 3763 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 3764 | if (value == NULL) \ |
| 3765 | goto error; \ |
| 3766 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 3767 | Py_DECREF(value); \ |
| 3768 | if (r < 0) \ |
| 3769 | goto error; |
| 3770 | |
| 3771 | ADD_STATS(number, "number"); |
| 3772 | ADD_STATS(connect, "connect"); |
| 3773 | ADD_STATS(connect_good, "connect_good"); |
| 3774 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 3775 | ADD_STATS(accept, "accept"); |
| 3776 | ADD_STATS(accept_good, "accept_good"); |
| 3777 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 3778 | ADD_STATS(accept, "accept"); |
| 3779 | ADD_STATS(hits, "hits"); |
| 3780 | ADD_STATS(misses, "misses"); |
| 3781 | ADD_STATS(timeouts, "timeouts"); |
| 3782 | ADD_STATS(cache_full, "cache_full"); |
| 3783 | |
| 3784 | #undef ADD_STATS |
| 3785 | |
| 3786 | return stats; |
| 3787 | |
| 3788 | error: |
| 3789 | Py_DECREF(stats); |
| 3790 | return NULL; |
| 3791 | } |
| 3792 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3793 | /*[clinic input] |
| 3794 | _ssl._SSLContext.set_default_verify_paths |
| 3795 | [clinic start generated code]*/ |
| 3796 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 3797 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3798 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 3799 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 3800 | { |
| 3801 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 3802 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3803 | return NULL; |
| 3804 | } |
| 3805 | Py_RETURN_NONE; |
| 3806 | } |
| 3807 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 3808 | #ifndef OPENSSL_NO_ECDH |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3809 | /*[clinic input] |
| 3810 | _ssl._SSLContext.set_ecdh_curve |
| 3811 | name: object |
| 3812 | / |
| 3813 | |
| 3814 | [clinic start generated code]*/ |
| 3815 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 3816 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3817 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 3818 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 3819 | { |
| 3820 | PyObject *name_bytes; |
| 3821 | int nid; |
| 3822 | EC_KEY *key; |
| 3823 | |
| 3824 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 3825 | return NULL; |
| 3826 | assert(PyBytes_Check(name_bytes)); |
| 3827 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 3828 | Py_DECREF(name_bytes); |
| 3829 | if (nid == 0) { |
| 3830 | PyErr_Format(PyExc_ValueError, |
| 3831 | "unknown elliptic curve name %R", name); |
| 3832 | return NULL; |
| 3833 | } |
| 3834 | key = EC_KEY_new_by_curve_name(nid); |
| 3835 | if (key == NULL) { |
| 3836 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3837 | return NULL; |
| 3838 | } |
| 3839 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3840 | EC_KEY_free(key); |
| 3841 | Py_RETURN_NONE; |
| 3842 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 3843 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 3844 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 3845 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3846 | static int |
| 3847 | _servername_callback(SSL *s, int *al, void *args) |
| 3848 | { |
| 3849 | int ret; |
| 3850 | PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3851 | PySSLSocket *ssl; |
| 3852 | PyObject *servername_o; |
| 3853 | PyObject *servername_idna; |
| 3854 | PyObject *result; |
| 3855 | /* The high-level ssl.SSLSocket object */ |
| 3856 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3857 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 3858 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3859 | |
| 3860 | if (ssl_ctx->set_hostname == NULL) { |
| 3861 | /* remove race condition in this the call back while if removing the |
| 3862 | * callback is in progress */ |
| 3863 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 3864 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | ssl = SSL_get_app_data(s); |
| 3868 | assert(PySSLSocket_Check(ssl)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3869 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 3870 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3871 | * SSL connection and that has a .context attribute that can be changed to |
| 3872 | * identify the requested hostname. Since the official API is the Python |
| 3873 | * level API we want to pass the callback a Python level object rather than |
| 3874 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 3875 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 3876 | * will be passed. If both do not exist only then the C-level object is |
| 3877 | * passed. */ |
| 3878 | if (ssl->owner) |
| 3879 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 3880 | else if (ssl->Socket) |
| 3881 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 3882 | else |
| 3883 | ssl_socket = (PyObject *) ssl; |
| 3884 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3885 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 3886 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3887 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 3888 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 3889 | if (servername == NULL) { |
| 3890 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket, |
| 3891 | Py_None, ssl_ctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3892 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 3893 | else { |
| 3894 | servername_o = PyBytes_FromString(servername); |
| 3895 | if (servername_o == NULL) { |
| 3896 | PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 3897 | goto error; |
| 3898 | } |
| 3899 | servername_idna = PyUnicode_FromEncodedObject(servername_o, "idna", NULL); |
| 3900 | if (servername_idna == NULL) { |
| 3901 | PyErr_WriteUnraisable(servername_o); |
| 3902 | Py_DECREF(servername_o); |
| 3903 | goto error; |
| 3904 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3905 | Py_DECREF(servername_o); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 3906 | result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket, |
| 3907 | servername_idna, ssl_ctx, NULL); |
| 3908 | Py_DECREF(servername_idna); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3909 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3910 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3911 | |
| 3912 | if (result == NULL) { |
| 3913 | PyErr_WriteUnraisable(ssl_ctx->set_hostname); |
| 3914 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 3915 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3916 | } |
| 3917 | else { |
| 3918 | if (result != Py_None) { |
| 3919 | *al = (int) PyLong_AsLong(result); |
| 3920 | if (PyErr_Occurred()) { |
| 3921 | PyErr_WriteUnraisable(result); |
| 3922 | *al = SSL_AD_INTERNAL_ERROR; |
| 3923 | } |
| 3924 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3925 | } |
| 3926 | else { |
| 3927 | ret = SSL_TLSEXT_ERR_OK; |
| 3928 | } |
| 3929 | Py_DECREF(result); |
| 3930 | } |
| 3931 | |
| 3932 | PyGILState_Release(gstate); |
| 3933 | return ret; |
| 3934 | |
| 3935 | error: |
| 3936 | Py_DECREF(ssl_socket); |
| 3937 | *al = SSL_AD_INTERNAL_ERROR; |
| 3938 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3939 | PyGILState_Release(gstate); |
| 3940 | return ret; |
| 3941 | } |
Antoine Pitrou | a596338 | 2013-03-30 16:39:00 +0100 | [diff] [blame] | 3942 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3943 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3944 | /*[clinic input] |
| 3945 | _ssl._SSLContext.set_servername_callback |
| 3946 | method as cb: object |
| 3947 | / |
| 3948 | |
| 3949 | Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension. |
| 3950 | |
| 3951 | If the argument is None then the callback is disabled. The method is called |
| 3952 | with the SSLSocket, the server name as a string, and the SSLContext object. |
| 3953 | See RFC 6066 for details of the SNI extension. |
| 3954 | [clinic start generated code]*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3955 | |
| 3956 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3957 | _ssl__SSLContext_set_servername_callback(PySSLContext *self, PyObject *cb) |
| 3958 | /*[clinic end generated code: output=3439a1b2d5d3b7ea input=a2a83620197d602b]*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3959 | { |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 3960 | #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3961 | Py_CLEAR(self->set_hostname); |
| 3962 | if (cb == Py_None) { |
| 3963 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 3964 | } |
| 3965 | else { |
| 3966 | if (!PyCallable_Check(cb)) { |
| 3967 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 3968 | PyErr_SetString(PyExc_TypeError, |
| 3969 | "not a callable object"); |
| 3970 | return NULL; |
| 3971 | } |
| 3972 | Py_INCREF(cb); |
| 3973 | self->set_hostname = cb; |
| 3974 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 3975 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 3976 | } |
| 3977 | Py_RETURN_NONE; |
| 3978 | #else |
| 3979 | PyErr_SetString(PyExc_NotImplementedError, |
| 3980 | "The TLS extension servername callback, " |
| 3981 | "SSL_CTX_set_tlsext_servername_callback, " |
| 3982 | "is not in the current OpenSSL library."); |
Antoine Pitrou | 41f8c4f | 2013-03-30 16:36:54 +0100 | [diff] [blame] | 3983 | return NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3984 | #endif |
| 3985 | } |
| 3986 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3987 | /*[clinic input] |
| 3988 | _ssl._SSLContext.cert_store_stats |
| 3989 | |
| 3990 | Returns quantities of loaded X.509 certificates. |
| 3991 | |
| 3992 | X.509 certificates with a CA extension and certificate revocation lists |
| 3993 | inside the context's cert store. |
| 3994 | |
| 3995 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 3996 | been used at least once. |
| 3997 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 3998 | |
| 3999 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4000 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4001 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4002 | { |
| 4003 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4004 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4005 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4006 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4007 | |
| 4008 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4009 | objs = X509_STORE_get0_objects(store); |
| 4010 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4011 | obj = sk_X509_OBJECT_value(objs, i); |
| 4012 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4013 | case X509_LU_X509: |
| 4014 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4015 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4016 | ca++; |
| 4017 | } |
| 4018 | break; |
| 4019 | case X509_LU_CRL: |
| 4020 | crl++; |
| 4021 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4022 | default: |
| 4023 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4024 | * As far as I can tell they are internal states and never |
| 4025 | * stored in a cert store */ |
| 4026 | break; |
| 4027 | } |
| 4028 | } |
| 4029 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4030 | "x509_ca", ca); |
| 4031 | } |
| 4032 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4033 | /*[clinic input] |
| 4034 | _ssl._SSLContext.get_ca_certs |
| 4035 | binary_form: bool = False |
| 4036 | |
| 4037 | Returns a list of dicts with information of loaded CA certs. |
| 4038 | |
| 4039 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4040 | certificate. |
| 4041 | |
| 4042 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4043 | been used at least once. |
| 4044 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4045 | |
| 4046 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4047 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4048 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4049 | { |
| 4050 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4051 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4052 | PyObject *ci = NULL, *rlist = NULL; |
| 4053 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4054 | |
| 4055 | if ((rlist = PyList_New(0)) == NULL) { |
| 4056 | return NULL; |
| 4057 | } |
| 4058 | |
| 4059 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4060 | objs = X509_STORE_get0_objects(store); |
| 4061 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4062 | X509_OBJECT *obj; |
| 4063 | X509 *cert; |
| 4064 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4065 | obj = sk_X509_OBJECT_value(objs, i); |
| 4066 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4067 | /* not a x509 cert */ |
| 4068 | continue; |
| 4069 | } |
| 4070 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4071 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4072 | if (!X509_check_ca(cert)) { |
| 4073 | continue; |
| 4074 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4075 | if (binary_form) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4076 | ci = _certificate_to_der(cert); |
| 4077 | } else { |
| 4078 | ci = _decode_certificate(cert); |
| 4079 | } |
| 4080 | if (ci == NULL) { |
| 4081 | goto error; |
| 4082 | } |
| 4083 | if (PyList_Append(rlist, ci) == -1) { |
| 4084 | goto error; |
| 4085 | } |
| 4086 | Py_CLEAR(ci); |
| 4087 | } |
| 4088 | return rlist; |
| 4089 | |
| 4090 | error: |
| 4091 | Py_XDECREF(ci); |
| 4092 | Py_XDECREF(rlist); |
| 4093 | return NULL; |
| 4094 | } |
| 4095 | |
| 4096 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4097 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4098 | {"check_hostname", (getter) get_check_hostname, |
| 4099 | (setter) set_check_hostname, NULL}, |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4100 | {"options", (getter) get_options, |
| 4101 | (setter) set_options, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4102 | {"verify_flags", (getter) get_verify_flags, |
| 4103 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4104 | {"verify_mode", (getter) get_verify_mode, |
| 4105 | (setter) set_verify_mode, NULL}, |
| 4106 | {NULL}, /* sentinel */ |
| 4107 | }; |
| 4108 | |
| 4109 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4110 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4111 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4112 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4113 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
| 4114 | _SSL__SSLCONTEXT__SET_NPN_PROTOCOLS_METHODDEF |
| 4115 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4116 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4117 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4118 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4119 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4120 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
| 4121 | _SSL__SSLCONTEXT_SET_SERVERNAME_CALLBACK_METHODDEF |
| 4122 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4123 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4124 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4125 | {NULL, NULL} /* sentinel */ |
| 4126 | }; |
| 4127 | |
| 4128 | static PyTypeObject PySSLContext_Type = { |
| 4129 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4130 | "_ssl._SSLContext", /*tp_name*/ |
| 4131 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 4132 | 0, /*tp_itemsize*/ |
| 4133 | (destructor)context_dealloc, /*tp_dealloc*/ |
| 4134 | 0, /*tp_print*/ |
| 4135 | 0, /*tp_getattr*/ |
| 4136 | 0, /*tp_setattr*/ |
| 4137 | 0, /*tp_reserved*/ |
| 4138 | 0, /*tp_repr*/ |
| 4139 | 0, /*tp_as_number*/ |
| 4140 | 0, /*tp_as_sequence*/ |
| 4141 | 0, /*tp_as_mapping*/ |
| 4142 | 0, /*tp_hash*/ |
| 4143 | 0, /*tp_call*/ |
| 4144 | 0, /*tp_str*/ |
| 4145 | 0, /*tp_getattro*/ |
| 4146 | 0, /*tp_setattro*/ |
| 4147 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4148 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4149 | 0, /*tp_doc*/ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4150 | (traverseproc) context_traverse, /*tp_traverse*/ |
| 4151 | (inquiry) context_clear, /*tp_clear*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4152 | 0, /*tp_richcompare*/ |
| 4153 | 0, /*tp_weaklistoffset*/ |
| 4154 | 0, /*tp_iter*/ |
| 4155 | 0, /*tp_iternext*/ |
| 4156 | context_methods, /*tp_methods*/ |
| 4157 | 0, /*tp_members*/ |
| 4158 | context_getsetlist, /*tp_getset*/ |
| 4159 | 0, /*tp_base*/ |
| 4160 | 0, /*tp_dict*/ |
| 4161 | 0, /*tp_descr_get*/ |
| 4162 | 0, /*tp_descr_set*/ |
| 4163 | 0, /*tp_dictoffset*/ |
| 4164 | 0, /*tp_init*/ |
| 4165 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4166 | _ssl__SSLContext, /*tp_new*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4167 | }; |
| 4168 | |
| 4169 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4170 | /* |
| 4171 | * MemoryBIO objects |
| 4172 | */ |
| 4173 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4174 | /*[clinic input] |
| 4175 | @classmethod |
| 4176 | _ssl.MemoryBIO.__new__ |
| 4177 | |
| 4178 | [clinic start generated code]*/ |
| 4179 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4180 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4181 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4182 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4183 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4184 | BIO *bio; |
| 4185 | PySSLMemoryBIO *self; |
| 4186 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4187 | bio = BIO_new(BIO_s_mem()); |
| 4188 | if (bio == NULL) { |
| 4189 | PyErr_SetString(PySSLErrorObject, |
| 4190 | "failed to allocate BIO"); |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4194 | * just that no data is currently available. The SSL routines should retry |
| 4195 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4196 | BIO_set_retry_read(bio); |
| 4197 | BIO_set_mem_eof_return(bio, -1); |
| 4198 | |
| 4199 | assert(type != NULL && type->tp_alloc != NULL); |
| 4200 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4201 | if (self == NULL) { |
| 4202 | BIO_free(bio); |
| 4203 | return NULL; |
| 4204 | } |
| 4205 | self->bio = bio; |
| 4206 | self->eof_written = 0; |
| 4207 | |
| 4208 | return (PyObject *) self; |
| 4209 | } |
| 4210 | |
| 4211 | static void |
| 4212 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4213 | { |
| 4214 | BIO_free(self->bio); |
| 4215 | Py_TYPE(self)->tp_free(self); |
| 4216 | } |
| 4217 | |
| 4218 | static PyObject * |
| 4219 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4220 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4221 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4225 | "The number of bytes pending in the memory BIO."); |
| 4226 | |
| 4227 | static PyObject * |
| 4228 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4229 | { |
| 4230 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4231 | && self->eof_written); |
| 4232 | } |
| 4233 | |
| 4234 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4235 | "Whether the memory BIO is at EOF."); |
| 4236 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4237 | /*[clinic input] |
| 4238 | _ssl.MemoryBIO.read |
| 4239 | size as len: int = -1 |
| 4240 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4241 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4242 | Read up to size bytes from the memory BIO. |
| 4243 | |
| 4244 | If size is not specified, read the entire buffer. |
| 4245 | If the return value is an empty bytes instance, this means either |
| 4246 | EOF or that no data is available. Use the "eof" property to |
| 4247 | distinguish between the two. |
| 4248 | [clinic start generated code]*/ |
| 4249 | |
| 4250 | static PyObject * |
| 4251 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4252 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4253 | { |
| 4254 | int avail, nbytes; |
| 4255 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4256 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4257 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4258 | if ((len < 0) || (len > avail)) |
| 4259 | len = avail; |
| 4260 | |
| 4261 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4262 | if ((result == NULL) || (len == 0)) |
| 4263 | return result; |
| 4264 | |
| 4265 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
| 4266 | /* There should never be any short reads but check anyway. */ |
| 4267 | if ((nbytes < len) && (_PyBytes_Resize(&result, len) < 0)) { |
| 4268 | Py_DECREF(result); |
| 4269 | return NULL; |
| 4270 | } |
| 4271 | |
| 4272 | return result; |
| 4273 | } |
| 4274 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4275 | /*[clinic input] |
| 4276 | _ssl.MemoryBIO.write |
| 4277 | b: Py_buffer |
| 4278 | / |
| 4279 | |
| 4280 | Writes the bytes b into the memory BIO. |
| 4281 | |
| 4282 | Returns the number of bytes written. |
| 4283 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4284 | |
| 4285 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4286 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4287 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4288 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4289 | int nbytes; |
| 4290 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4291 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4292 | PyErr_Format(PyExc_OverflowError, |
| 4293 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4294 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | if (self->eof_written) { |
| 4298 | PyErr_SetString(PySSLErrorObject, |
| 4299 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4300 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4301 | } |
| 4302 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4303 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4304 | if (nbytes < 0) { |
| 4305 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4306 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4309 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4310 | } |
| 4311 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4312 | /*[clinic input] |
| 4313 | _ssl.MemoryBIO.write_eof |
| 4314 | |
| 4315 | Write an EOF marker to the memory BIO. |
| 4316 | |
| 4317 | When all data has been read, the "eof" property will be True. |
| 4318 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4319 | |
| 4320 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4321 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4322 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4323 | { |
| 4324 | self->eof_written = 1; |
| 4325 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4326 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4327 | BIO_clear_retry_flags(self->bio); |
| 4328 | BIO_set_mem_eof_return(self->bio, 0); |
| 4329 | |
| 4330 | Py_RETURN_NONE; |
| 4331 | } |
| 4332 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4333 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4334 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4335 | PySSL_memory_bio_pending_doc}, |
| 4336 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 4337 | PySSL_memory_bio_eof_doc}, |
| 4338 | {NULL}, /* sentinel */ |
| 4339 | }; |
| 4340 | |
| 4341 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4342 | _SSL_MEMORYBIO_READ_METHODDEF |
| 4343 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 4344 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4345 | {NULL, NULL} /* sentinel */ |
| 4346 | }; |
| 4347 | |
| 4348 | static PyTypeObject PySSLMemoryBIO_Type = { |
| 4349 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4350 | "_ssl.MemoryBIO", /*tp_name*/ |
| 4351 | sizeof(PySSLMemoryBIO), /*tp_basicsize*/ |
| 4352 | 0, /*tp_itemsize*/ |
| 4353 | (destructor)memory_bio_dealloc, /*tp_dealloc*/ |
| 4354 | 0, /*tp_print*/ |
| 4355 | 0, /*tp_getattr*/ |
| 4356 | 0, /*tp_setattr*/ |
| 4357 | 0, /*tp_reserved*/ |
| 4358 | 0, /*tp_repr*/ |
| 4359 | 0, /*tp_as_number*/ |
| 4360 | 0, /*tp_as_sequence*/ |
| 4361 | 0, /*tp_as_mapping*/ |
| 4362 | 0, /*tp_hash*/ |
| 4363 | 0, /*tp_call*/ |
| 4364 | 0, /*tp_str*/ |
| 4365 | 0, /*tp_getattro*/ |
| 4366 | 0, /*tp_setattro*/ |
| 4367 | 0, /*tp_as_buffer*/ |
| 4368 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4369 | 0, /*tp_doc*/ |
| 4370 | 0, /*tp_traverse*/ |
| 4371 | 0, /*tp_clear*/ |
| 4372 | 0, /*tp_richcompare*/ |
| 4373 | 0, /*tp_weaklistoffset*/ |
| 4374 | 0, /*tp_iter*/ |
| 4375 | 0, /*tp_iternext*/ |
| 4376 | memory_bio_methods, /*tp_methods*/ |
| 4377 | 0, /*tp_members*/ |
| 4378 | memory_bio_getsetlist, /*tp_getset*/ |
| 4379 | 0, /*tp_base*/ |
| 4380 | 0, /*tp_dict*/ |
| 4381 | 0, /*tp_descr_get*/ |
| 4382 | 0, /*tp_descr_set*/ |
| 4383 | 0, /*tp_dictoffset*/ |
| 4384 | 0, /*tp_init*/ |
| 4385 | 0, /*tp_alloc*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4386 | _ssl_MemoryBIO, /*tp_new*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4387 | }; |
| 4388 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4389 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4390 | /* |
| 4391 | * SSL Session object |
| 4392 | */ |
| 4393 | |
| 4394 | static void |
| 4395 | PySSLSession_dealloc(PySSLSession *self) |
| 4396 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 4397 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4398 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4399 | Py_XDECREF(self->ctx); |
| 4400 | if (self->session != NULL) { |
| 4401 | SSL_SESSION_free(self->session); |
| 4402 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4403 | PyObject_GC_Del(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | static PyObject * |
| 4407 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 4408 | { |
| 4409 | int result; |
| 4410 | |
| 4411 | if (left == NULL || right == NULL) { |
| 4412 | PyErr_BadInternalCall(); |
| 4413 | return NULL; |
| 4414 | } |
| 4415 | |
| 4416 | if (!PySSLSession_Check(left) || !PySSLSession_Check(right)) { |
| 4417 | Py_RETURN_NOTIMPLEMENTED; |
| 4418 | } |
| 4419 | |
| 4420 | if (left == right) { |
| 4421 | result = 0; |
| 4422 | } else { |
| 4423 | const unsigned char *left_id, *right_id; |
| 4424 | unsigned int left_len, right_len; |
| 4425 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 4426 | &left_len); |
| 4427 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 4428 | &right_len); |
| 4429 | if (left_len == right_len) { |
| 4430 | result = memcmp(left_id, right_id, left_len); |
| 4431 | } else { |
| 4432 | result = 1; |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | switch (op) { |
| 4437 | case Py_EQ: |
| 4438 | if (result == 0) { |
| 4439 | Py_RETURN_TRUE; |
| 4440 | } else { |
| 4441 | Py_RETURN_FALSE; |
| 4442 | } |
| 4443 | break; |
| 4444 | case Py_NE: |
| 4445 | if (result != 0) { |
| 4446 | Py_RETURN_TRUE; |
| 4447 | } else { |
| 4448 | Py_RETURN_FALSE; |
| 4449 | } |
| 4450 | break; |
| 4451 | case Py_LT: |
| 4452 | case Py_LE: |
| 4453 | case Py_GT: |
| 4454 | case Py_GE: |
| 4455 | Py_RETURN_NOTIMPLEMENTED; |
| 4456 | break; |
| 4457 | default: |
| 4458 | PyErr_BadArgument(); |
| 4459 | return NULL; |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | static int |
| 4464 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 4465 | { |
| 4466 | Py_VISIT(self->ctx); |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
| 4470 | static int |
| 4471 | PySSLSession_clear(PySSLSession *self) |
| 4472 | { |
| 4473 | Py_CLEAR(self->ctx); |
| 4474 | return 0; |
| 4475 | } |
| 4476 | |
| 4477 | |
| 4478 | static PyObject * |
| 4479 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 4480 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 4481 | } |
| 4482 | |
| 4483 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 4484 | "Session creation time (seconds since epoch)."); |
| 4485 | |
| 4486 | |
| 4487 | static PyObject * |
| 4488 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 4489 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 4490 | } |
| 4491 | |
| 4492 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 4493 | "Session timeout (delta in seconds)."); |
| 4494 | |
| 4495 | |
| 4496 | static PyObject * |
| 4497 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 4498 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 4499 | return PyLong_FromUnsignedLong(hint); |
| 4500 | } |
| 4501 | |
| 4502 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 4503 | "Ticket life time hint."); |
| 4504 | |
| 4505 | |
| 4506 | static PyObject * |
| 4507 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 4508 | const unsigned char *id; |
| 4509 | unsigned int len; |
| 4510 | id = SSL_SESSION_get_id(self->session, &len); |
| 4511 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 4512 | } |
| 4513 | |
| 4514 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 4515 | "Session id"); |
| 4516 | |
| 4517 | |
| 4518 | static PyObject * |
| 4519 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 4520 | if (SSL_SESSION_has_ticket(self->session)) { |
| 4521 | Py_RETURN_TRUE; |
| 4522 | } else { |
| 4523 | Py_RETURN_FALSE; |
| 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 4528 | "Does the session contain a ticket?"); |
| 4529 | |
| 4530 | |
| 4531 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 4532 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 4533 | PySSLSession_get_has_ticket_doc}, |
| 4534 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 4535 | PySSLSession_get_session_id_doc}, |
| 4536 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 4537 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 4538 | {"time", (getter) PySSLSession_get_time, NULL, |
| 4539 | PySSLSession_get_time_doc}, |
| 4540 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 4541 | PySSLSession_get_timeout_doc}, |
| 4542 | {NULL}, /* sentinel */ |
| 4543 | }; |
| 4544 | |
| 4545 | static PyTypeObject PySSLSession_Type = { |
| 4546 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4547 | "_ssl.Session", /*tp_name*/ |
| 4548 | sizeof(PySSLSession), /*tp_basicsize*/ |
| 4549 | 0, /*tp_itemsize*/ |
| 4550 | (destructor)PySSLSession_dealloc, /*tp_dealloc*/ |
| 4551 | 0, /*tp_print*/ |
| 4552 | 0, /*tp_getattr*/ |
| 4553 | 0, /*tp_setattr*/ |
| 4554 | 0, /*tp_reserved*/ |
| 4555 | 0, /*tp_repr*/ |
| 4556 | 0, /*tp_as_number*/ |
| 4557 | 0, /*tp_as_sequence*/ |
| 4558 | 0, /*tp_as_mapping*/ |
| 4559 | 0, /*tp_hash*/ |
| 4560 | 0, /*tp_call*/ |
| 4561 | 0, /*tp_str*/ |
| 4562 | 0, /*tp_getattro*/ |
| 4563 | 0, /*tp_setattro*/ |
| 4564 | 0, /*tp_as_buffer*/ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4565 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4566 | 0, /*tp_doc*/ |
| 4567 | (traverseproc)PySSLSession_traverse, /*tp_traverse*/ |
| 4568 | (inquiry)PySSLSession_clear, /*tp_clear*/ |
| 4569 | PySSLSession_richcompare, /*tp_richcompare*/ |
| 4570 | 0, /*tp_weaklistoffset*/ |
| 4571 | 0, /*tp_iter*/ |
| 4572 | 0, /*tp_iternext*/ |
| 4573 | 0, /*tp_methods*/ |
| 4574 | 0, /*tp_members*/ |
| 4575 | PySSLSession_getsetlist, /*tp_getset*/ |
| 4576 | }; |
| 4577 | |
| 4578 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4579 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4580 | /*[clinic input] |
| 4581 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 4582 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4583 | entropy: double |
| 4584 | / |
| 4585 | |
| 4586 | Mix string into the OpenSSL PRNG state. |
| 4587 | |
| 4588 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 4589 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4590 | [clinic start generated code]*/ |
| 4591 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4592 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4593 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 4594 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4595 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 4596 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 4597 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4598 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4599 | buf = (const char *)view->buf; |
| 4600 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 4601 | do { |
| 4602 | written = Py_MIN(len, INT_MAX); |
| 4603 | RAND_add(buf, (int)written, entropy); |
| 4604 | buf += written; |
| 4605 | len -= written; |
| 4606 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4607 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4610 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4611 | PySSL_RAND(int len, int pseudo) |
| 4612 | { |
| 4613 | int ok; |
| 4614 | PyObject *bytes; |
| 4615 | unsigned long err; |
| 4616 | const char *errstr; |
| 4617 | PyObject *v; |
| 4618 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 4619 | if (len < 0) { |
| 4620 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4624 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 4625 | if (bytes == NULL) |
| 4626 | return NULL; |
| 4627 | if (pseudo) { |
| 4628 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 4629 | if (ok == 0 || ok == 1) |
| 4630 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 4631 | } |
| 4632 | else { |
| 4633 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 4634 | if (ok == 1) |
| 4635 | return bytes; |
| 4636 | } |
| 4637 | Py_DECREF(bytes); |
| 4638 | |
| 4639 | err = ERR_get_error(); |
| 4640 | errstr = ERR_reason_error_string(err); |
| 4641 | v = Py_BuildValue("(ks)", err, errstr); |
| 4642 | if (v != NULL) { |
| 4643 | PyErr_SetObject(PySSLErrorObject, v); |
| 4644 | Py_DECREF(v); |
| 4645 | } |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4649 | /*[clinic input] |
| 4650 | _ssl.RAND_bytes |
| 4651 | n: int |
| 4652 | / |
| 4653 | |
| 4654 | Generate n cryptographically strong pseudo-random bytes. |
| 4655 | [clinic start generated code]*/ |
| 4656 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4658 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 4659 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4660 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4661 | return PySSL_RAND(n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4662 | } |
| 4663 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4664 | /*[clinic input] |
| 4665 | _ssl.RAND_pseudo_bytes |
| 4666 | n: int |
| 4667 | / |
| 4668 | |
| 4669 | Generate n pseudo-random bytes. |
| 4670 | |
| 4671 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 4672 | if the bytes generated are cryptographically strong. |
| 4673 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4674 | |
| 4675 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4676 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 4677 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4678 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4679 | return PySSL_RAND(n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4680 | } |
| 4681 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4682 | /*[clinic input] |
| 4683 | _ssl.RAND_status |
| 4684 | |
| 4685 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 4686 | |
| 4687 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 4688 | using the ssl() function. |
| 4689 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4690 | |
| 4691 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4692 | _ssl_RAND_status_impl(PyObject *module) |
| 4693 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4694 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4695 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 4698 | #ifndef OPENSSL_NO_EGD |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4699 | /* LCOV_EXCL_START */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4700 | /*[clinic input] |
| 4701 | _ssl.RAND_egd |
| 4702 | path: object(converter="PyUnicode_FSConverter") |
| 4703 | / |
| 4704 | |
| 4705 | Queries the entropy gather daemon (EGD) on the socket named by 'path'. |
| 4706 | |
| 4707 | Returns number of bytes read. Raises SSLError if connection to EGD |
| 4708 | fails or if it does not provide enough data to seed PRNG. |
| 4709 | [clinic start generated code]*/ |
| 4710 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4711 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4712 | _ssl_RAND_egd_impl(PyObject *module, PyObject *path) |
| 4713 | /*[clinic end generated code: output=02a67c7c367f52fa input=1aeb7eb948312195]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4714 | { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4715 | int bytes = RAND_egd(PyBytes_AsString(path)); |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 4716 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4717 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 4718 | PyErr_SetString(PySSLErrorObject, |
| 4719 | "EGD connection failed or EGD did not return " |
| 4720 | "enough data to seed the PRNG"); |
| 4721 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4722 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4723 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4724 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4725 | /* LCOV_EXCL_STOP */ |
Benjamin Peterson | b8a2f51 | 2016-07-06 23:55:15 -0700 | [diff] [blame] | 4726 | #endif /* OPENSSL_NO_EGD */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4727 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4728 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4729 | |
| 4730 | /*[clinic input] |
| 4731 | _ssl.get_default_verify_paths |
| 4732 | |
| 4733 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 4734 | |
| 4735 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 4736 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4737 | |
| 4738 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4739 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 4740 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4741 | { |
| 4742 | PyObject *ofile_env = NULL; |
| 4743 | PyObject *ofile = NULL; |
| 4744 | PyObject *odir_env = NULL; |
| 4745 | PyObject *odir = NULL; |
| 4746 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 4747 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4748 | const char *tmp = (info); \ |
| 4749 | target = NULL; \ |
| 4750 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 4751 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 4752 | target = PyBytes_FromString(tmp); } \ |
| 4753 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 4754 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4755 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 4756 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 4757 | CONVERT(X509_get_default_cert_file(), ofile); |
| 4758 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 4759 | CONVERT(X509_get_default_cert_dir(), odir); |
| 4760 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4761 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 4762 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4763 | |
| 4764 | error: |
| 4765 | Py_XDECREF(ofile_env); |
| 4766 | Py_XDECREF(ofile); |
| 4767 | Py_XDECREF(odir_env); |
| 4768 | Py_XDECREF(odir); |
| 4769 | return NULL; |
| 4770 | } |
| 4771 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4772 | static PyObject* |
| 4773 | asn1obj2py(ASN1_OBJECT *obj) |
| 4774 | { |
| 4775 | int nid; |
| 4776 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4777 | |
| 4778 | nid = OBJ_obj2nid(obj); |
| 4779 | if (nid == NID_undef) { |
| 4780 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 4781 | return NULL; |
| 4782 | } |
| 4783 | sn = OBJ_nid2sn(nid); |
| 4784 | ln = OBJ_nid2ln(nid); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 4785 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4788 | /*[clinic input] |
| 4789 | _ssl.txt2obj |
| 4790 | txt: str |
| 4791 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4792 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4793 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 4794 | |
| 4795 | By default objects are looked up by OID. With name=True short and |
| 4796 | long name are also matched. |
| 4797 | [clinic start generated code]*/ |
| 4798 | |
| 4799 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4800 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 4801 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4802 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4803 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4804 | ASN1_OBJECT *obj; |
| 4805 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4806 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 4807 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 4808 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4809 | return NULL; |
| 4810 | } |
| 4811 | result = asn1obj2py(obj); |
| 4812 | ASN1_OBJECT_free(obj); |
| 4813 | return result; |
| 4814 | } |
| 4815 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4816 | /*[clinic input] |
| 4817 | _ssl.nid2obj |
| 4818 | nid: int |
| 4819 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4820 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4821 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 4822 | [clinic start generated code]*/ |
| 4823 | |
| 4824 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4825 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 4826 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4827 | { |
| 4828 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4829 | ASN1_OBJECT *obj; |
| 4830 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4831 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 4832 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4833 | return NULL; |
| 4834 | } |
| 4835 | obj = OBJ_nid2obj(nid); |
| 4836 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 4837 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 4838 | return NULL; |
| 4839 | } |
| 4840 | result = asn1obj2py(obj); |
| 4841 | ASN1_OBJECT_free(obj); |
| 4842 | return result; |
| 4843 | } |
| 4844 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4845 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4846 | |
| 4847 | static PyObject* |
| 4848 | certEncodingType(DWORD encodingType) |
| 4849 | { |
| 4850 | static PyObject *x509_asn = NULL; |
| 4851 | static PyObject *pkcs_7_asn = NULL; |
| 4852 | |
| 4853 | if (x509_asn == NULL) { |
| 4854 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 4855 | if (x509_asn == NULL) |
| 4856 | return NULL; |
| 4857 | } |
| 4858 | if (pkcs_7_asn == NULL) { |
| 4859 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 4860 | if (pkcs_7_asn == NULL) |
| 4861 | return NULL; |
| 4862 | } |
| 4863 | switch(encodingType) { |
| 4864 | case X509_ASN_ENCODING: |
| 4865 | Py_INCREF(x509_asn); |
| 4866 | return x509_asn; |
| 4867 | case PKCS_7_ASN_ENCODING: |
| 4868 | Py_INCREF(pkcs_7_asn); |
| 4869 | return pkcs_7_asn; |
| 4870 | default: |
| 4871 | return PyLong_FromLong(encodingType); |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | static PyObject* |
| 4876 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 4877 | { |
| 4878 | CERT_ENHKEY_USAGE *usage; |
| 4879 | DWORD size, error, i; |
| 4880 | PyObject *retval; |
| 4881 | |
| 4882 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 4883 | error = GetLastError(); |
| 4884 | if (error == CRYPT_E_NOT_FOUND) { |
| 4885 | Py_RETURN_TRUE; |
| 4886 | } |
| 4887 | return PyErr_SetFromWindowsErr(error); |
| 4888 | } |
| 4889 | |
| 4890 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 4891 | if (usage == NULL) { |
| 4892 | return PyErr_NoMemory(); |
| 4893 | } |
| 4894 | |
| 4895 | /* Now get the actual enhanced usage property */ |
| 4896 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 4897 | PyMem_Free(usage); |
| 4898 | error = GetLastError(); |
| 4899 | if (error == CRYPT_E_NOT_FOUND) { |
| 4900 | Py_RETURN_TRUE; |
| 4901 | } |
| 4902 | return PyErr_SetFromWindowsErr(error); |
| 4903 | } |
| 4904 | retval = PySet_New(NULL); |
| 4905 | if (retval == NULL) { |
| 4906 | goto error; |
| 4907 | } |
| 4908 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 4909 | if (usage->rgpszUsageIdentifier[i]) { |
| 4910 | PyObject *oid; |
| 4911 | int err; |
| 4912 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 4913 | if (oid == NULL) { |
| 4914 | Py_CLEAR(retval); |
| 4915 | goto error; |
| 4916 | } |
| 4917 | err = PySet_Add(retval, oid); |
| 4918 | Py_DECREF(oid); |
| 4919 | if (err == -1) { |
| 4920 | Py_CLEAR(retval); |
| 4921 | goto error; |
| 4922 | } |
| 4923 | } |
| 4924 | } |
| 4925 | error: |
| 4926 | PyMem_Free(usage); |
| 4927 | return retval; |
| 4928 | } |
| 4929 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4930 | /*[clinic input] |
| 4931 | _ssl.enum_certificates |
| 4932 | store_name: str |
| 4933 | |
| 4934 | Retrieve certificates from Windows' cert store. |
| 4935 | |
| 4936 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 4937 | more cert storages, too. The function returns a list of (bytes, |
| 4938 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 4939 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 4940 | a set of OIDs or the boolean True. |
| 4941 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 4942 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4943 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4944 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 4945 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4946 | { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4947 | HCERTSTORE hStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4948 | PCCERT_CONTEXT pCertCtx = NULL; |
| 4949 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4950 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4951 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4952 | result = PyList_New(0); |
| 4953 | if (result == NULL) { |
| 4954 | return NULL; |
| 4955 | } |
Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 4956 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 4957 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 4958 | store_name); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4959 | if (hStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4960 | Py_DECREF(result); |
| 4961 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 4962 | } |
| 4963 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4964 | while (pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx)) { |
| 4965 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 4966 | pCertCtx->cbCertEncoded); |
| 4967 | if (!cert) { |
| 4968 | Py_CLEAR(result); |
| 4969 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4970 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4971 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 4972 | Py_CLEAR(result); |
| 4973 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4974 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4975 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 4976 | if (keyusage == Py_True) { |
| 4977 | Py_DECREF(keyusage); |
| 4978 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4979 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4980 | if (keyusage == NULL) { |
| 4981 | Py_CLEAR(result); |
| 4982 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 4983 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 4984 | if ((tup = PyTuple_New(3)) == NULL) { |
| 4985 | Py_CLEAR(result); |
| 4986 | break; |
| 4987 | } |
| 4988 | PyTuple_SET_ITEM(tup, 0, cert); |
| 4989 | cert = NULL; |
| 4990 | PyTuple_SET_ITEM(tup, 1, enc); |
| 4991 | enc = NULL; |
| 4992 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 4993 | keyusage = NULL; |
| 4994 | if (PyList_Append(result, tup) < 0) { |
| 4995 | Py_CLEAR(result); |
| 4996 | break; |
| 4997 | } |
| 4998 | Py_CLEAR(tup); |
| 4999 | } |
| 5000 | if (pCertCtx) { |
| 5001 | /* loop ended with an error, need to clean up context manually */ |
| 5002 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | /* In error cases cert, enc and tup may not be NULL */ |
| 5006 | Py_XDECREF(cert); |
| 5007 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5008 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5009 | Py_XDECREF(tup); |
| 5010 | |
| 5011 | if (!CertCloseStore(hStore, 0)) { |
| 5012 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5013 | Py_XDECREF(result); |
| 5014 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5015 | } |
| 5016 | return result; |
| 5017 | } |
| 5018 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5019 | /*[clinic input] |
| 5020 | _ssl.enum_crls |
| 5021 | store_name: str |
| 5022 | |
| 5023 | Retrieve CRLs from Windows' cert store. |
| 5024 | |
| 5025 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5026 | more cert storages, too. The function returns a list of (bytes, |
| 5027 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5028 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5029 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5030 | |
| 5031 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5032 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5033 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5034 | { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5035 | HCERTSTORE hStore = NULL; |
| 5036 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5037 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5038 | PyObject *result = NULL; |
| 5039 | |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5040 | result = PyList_New(0); |
| 5041 | if (result == NULL) { |
| 5042 | return NULL; |
| 5043 | } |
Benjamin Peterson | 9491272 | 2016-02-17 22:13:19 -0800 | [diff] [blame] | 5044 | hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 5045 | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5046 | store_name); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5047 | if (hStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5048 | Py_DECREF(result); |
| 5049 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5050 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5051 | |
| 5052 | while (pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)) { |
| 5053 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5054 | pCrlCtx->cbCrlEncoded); |
| 5055 | if (!crl) { |
| 5056 | Py_CLEAR(result); |
| 5057 | break; |
| 5058 | } |
| 5059 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5060 | Py_CLEAR(result); |
| 5061 | break; |
| 5062 | } |
| 5063 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5064 | Py_CLEAR(result); |
| 5065 | break; |
| 5066 | } |
| 5067 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5068 | crl = NULL; |
| 5069 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5070 | enc = NULL; |
| 5071 | |
| 5072 | if (PyList_Append(result, tup) < 0) { |
| 5073 | Py_CLEAR(result); |
| 5074 | break; |
| 5075 | } |
| 5076 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5077 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5078 | if (pCrlCtx) { |
| 5079 | /* loop ended with an error, need to clean up context manually */ |
| 5080 | CertFreeCRLContext(pCrlCtx); |
| 5081 | } |
| 5082 | |
| 5083 | /* In error cases cert, enc and tup may not be NULL */ |
| 5084 | Py_XDECREF(crl); |
| 5085 | Py_XDECREF(enc); |
| 5086 | Py_XDECREF(tup); |
| 5087 | |
| 5088 | if (!CertCloseStore(hStore, 0)) { |
| 5089 | /* This error case might shadow another exception.*/ |
| 5090 | Py_XDECREF(result); |
| 5091 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5092 | } |
| 5093 | return result; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5094 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5095 | |
| 5096 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5097 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5098 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5099 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5100 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5101 | _SSL_RAND_ADD_METHODDEF |
| 5102 | _SSL_RAND_BYTES_METHODDEF |
| 5103 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
| 5104 | _SSL_RAND_EGD_METHODDEF |
| 5105 | _SSL_RAND_STATUS_METHODDEF |
| 5106 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5107 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5108 | _SSL_ENUM_CRLS_METHODDEF |
| 5109 | _SSL_TXT2OBJ_METHODDEF |
| 5110 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5111 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5112 | }; |
| 5113 | |
| 5114 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5115 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5116 | |
| 5117 | /* an implementation of OpenSSL threading operations in terms |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5118 | * of the Python C thread library |
| 5119 | * Only used up to 1.0.2. OpenSSL 1.1.0+ has its own locking code. |
| 5120 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5121 | |
| 5122 | static PyThread_type_lock *_ssl_locks = NULL; |
| 5123 | |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5124 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5125 | /* use new CRYPTO_THREADID API. */ |
| 5126 | static void |
| 5127 | _ssl_threadid_callback(CRYPTO_THREADID *id) |
| 5128 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 5129 | CRYPTO_THREADID_set_numeric(id, PyThread_get_thread_ident()); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5130 | } |
| 5131 | #else |
| 5132 | /* deprecated CRYPTO_set_id_callback() API. */ |
| 5133 | static unsigned long |
| 5134 | _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5135 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5136 | } |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5137 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5138 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5139 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5140 | (int mode, int n, const char *file, int line) { |
| 5141 | /* this function is needed to perform locking on shared data |
| 5142 | structures. (Note that OpenSSL uses a number of global data |
| 5143 | structures that will be implicitly shared whenever multiple |
| 5144 | threads use OpenSSL.) Multi-threaded applications will |
| 5145 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5146 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5147 | locking_function() must be able to handle up to |
| 5148 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 5149 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5150 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5151 | file and line are the file number of the function setting the |
| 5152 | lock. They can be useful for debugging. |
| 5153 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5154 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5155 | if ((_ssl_locks == NULL) || |
| 5156 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 5157 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5158 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5159 | if (mode & CRYPTO_LOCK) { |
| 5160 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 5161 | } else { |
| 5162 | PyThread_release_lock(_ssl_locks[n]); |
| 5163 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | static int _setup_ssl_threads(void) { |
| 5167 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5168 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5169 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5170 | if (_ssl_locks == NULL) { |
| 5171 | _ssl_locks_count = CRYPTO_num_locks(); |
Christian Heimes | f6365e3 | 2016-09-13 20:48:13 +0200 | [diff] [blame] | 5172 | _ssl_locks = PyMem_Calloc(_ssl_locks_count, |
| 5173 | sizeof(PyThread_type_lock)); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5174 | if (_ssl_locks == NULL) { |
| 5175 | PyErr_NoMemory(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5176 | return 0; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5177 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5178 | for (i = 0; i < _ssl_locks_count; i++) { |
| 5179 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 5180 | if (_ssl_locks[i] == NULL) { |
| 5181 | unsigned int j; |
| 5182 | for (j = 0; j < i; j++) { |
| 5183 | PyThread_free_lock(_ssl_locks[j]); |
| 5184 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 5185 | PyMem_Free(_ssl_locks); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5186 | return 0; |
| 5187 | } |
| 5188 | } |
| 5189 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5190 | #if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 5191 | CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 5192 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5193 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
Christian Heimes | 4d98ca9 | 2013-08-19 17:36:29 +0200 | [diff] [blame] | 5194 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5195 | } |
| 5196 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 5199 | #endif /* HAVE_OPENSSL_CRYPTO_LOCK for OpenSSL < 1.1.0 */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5201 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5202 | "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] | 5203 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5204 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5205 | |
| 5206 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5207 | PyModuleDef_HEAD_INIT, |
| 5208 | "_ssl", |
| 5209 | module_doc, |
| 5210 | -1, |
| 5211 | PySSL_methods, |
| 5212 | NULL, |
| 5213 | NULL, |
| 5214 | NULL, |
| 5215 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5216 | }; |
| 5217 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5218 | |
| 5219 | static void |
| 5220 | parse_openssl_version(unsigned long libver, |
| 5221 | unsigned int *major, unsigned int *minor, |
| 5222 | unsigned int *fix, unsigned int *patch, |
| 5223 | unsigned int *status) |
| 5224 | { |
| 5225 | *status = libver & 0xF; |
| 5226 | libver >>= 4; |
| 5227 | *patch = libver & 0xFF; |
| 5228 | libver >>= 8; |
| 5229 | *fix = libver & 0xFF; |
| 5230 | libver >>= 8; |
| 5231 | *minor = libver & 0xFF; |
| 5232 | libver >>= 8; |
| 5233 | *major = libver & 0xFF; |
| 5234 | } |
| 5235 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5236 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5237 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5238 | { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5239 | PyObject *m, *d, *r, *bases; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5240 | unsigned long libver; |
| 5241 | unsigned int major, minor, fix, patch, status; |
| 5242 | PySocketModule_APIObject *socket_api; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5243 | struct py_ssl_error_code *errcode; |
| 5244 | struct py_ssl_library_code *libcode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5246 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 5247 | return NULL; |
| 5248 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5249 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5250 | if (PyType_Ready(&PySSLMemoryBIO_Type) < 0) |
| 5251 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5252 | if (PyType_Ready(&PySSLSession_Type) < 0) |
| 5253 | return NULL; |
| 5254 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5255 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5256 | m = PyModule_Create(&_sslmodule); |
| 5257 | if (m == NULL) |
| 5258 | return NULL; |
| 5259 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5261 | /* Load _socket module and its C API */ |
| 5262 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 5263 | if (!socket_api) |
| 5264 | return NULL; |
| 5265 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5266 | |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5267 | #ifndef OPENSSL_VERSION_1_1 |
| 5268 | /* Load all algorithms and initialize cpuid */ |
| 5269 | OPENSSL_add_all_algorithms_noconf(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5270 | /* Init OpenSSL */ |
| 5271 | SSL_load_error_strings(); |
| 5272 | SSL_library_init(); |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5273 | #endif |
| 5274 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5275 | #ifdef HAVE_OPENSSL_CRYPTO_LOCK |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5276 | /* note that this will start threading if not already started */ |
| 5277 | if (!_setup_ssl_threads()) { |
| 5278 | return NULL; |
| 5279 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5280 | #elif OPENSSL_VERSION_1_1 && defined(OPENSSL_THREADS) |
| 5281 | /* OpenSSL 1.1.0 builtin thread support is enabled */ |
| 5282 | _ssl_locks_count++; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 5283 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5284 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5285 | /* Add symbols to module dict */ |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5286 | sslerror_type_slots[0].pfunc = PyExc_OSError; |
| 5287 | PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5288 | if (PySSLErrorObject == NULL) |
| 5289 | return NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5290 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5291 | /* ssl.CertificateError used to be a subclass of ValueError */ |
| 5292 | bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError); |
| 5293 | if (bases == NULL) |
| 5294 | return NULL; |
| 5295 | PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc( |
| 5296 | "ssl.SSLCertVerificationError", SSLCertVerificationError_doc, |
| 5297 | bases, NULL); |
| 5298 | Py_DECREF(bases); |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5299 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 5300 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 5301 | PySSLErrorObject, NULL); |
| 5302 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 5303 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 5304 | PySSLErrorObject, NULL); |
| 5305 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 5306 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 5307 | PySSLErrorObject, NULL); |
| 5308 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 5309 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 5310 | PySSLErrorObject, NULL); |
| 5311 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 5312 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 5313 | PySSLErrorObject, NULL); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5314 | if (PySSLCertVerificationErrorObject == NULL |
| 5315 | || PySSLZeroReturnErrorObject == NULL |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5316 | || PySSLWantReadErrorObject == NULL |
| 5317 | || PySSLWantWriteErrorObject == NULL |
| 5318 | || PySSLSyscallErrorObject == NULL |
| 5319 | || PySSLEOFErrorObject == NULL) |
| 5320 | return NULL; |
| 5321 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 5322 | || PyDict_SetItemString(d, "SSLCertVerificationError", |
| 5323 | PySSLCertVerificationErrorObject) != 0 |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 5324 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 5325 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 5326 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 5327 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 5328 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5329 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 5330 | if (PyDict_SetItemString(d, "_SSLContext", |
| 5331 | (PyObject *)&PySSLContext_Type) != 0) |
| 5332 | return NULL; |
| 5333 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 5334 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5335 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 5336 | if (PyDict_SetItemString(d, "MemoryBIO", |
| 5337 | (PyObject *)&PySSLMemoryBIO_Type) != 0) |
| 5338 | return NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5339 | if (PyDict_SetItemString(d, "SSLSession", |
| 5340 | (PyObject *)&PySSLSession_Type) != 0) |
| 5341 | return NULL; |
| 5342 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5343 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 5344 | PY_SSL_ERROR_ZERO_RETURN); |
| 5345 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 5346 | PY_SSL_ERROR_WANT_READ); |
| 5347 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 5348 | PY_SSL_ERROR_WANT_WRITE); |
| 5349 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 5350 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 5351 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 5352 | PY_SSL_ERROR_SYSCALL); |
| 5353 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 5354 | PY_SSL_ERROR_SSL); |
| 5355 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 5356 | PY_SSL_ERROR_WANT_CONNECT); |
| 5357 | /* non ssl.h errorcodes */ |
| 5358 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 5359 | PY_SSL_ERROR_EOF); |
| 5360 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 5361 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 5362 | /* cert requirements */ |
| 5363 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 5364 | PY_SSL_CERT_NONE); |
| 5365 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 5366 | PY_SSL_CERT_OPTIONAL); |
| 5367 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 5368 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 5369 | /* CRL verification for verification_flags */ |
| 5370 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 5371 | 0); |
| 5372 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 5373 | X509_V_FLAG_CRL_CHECK); |
| 5374 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 5375 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5376 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 5377 | X509_V_FLAG_X509_STRICT); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 5378 | #ifdef X509_V_FLAG_TRUSTED_FIRST |
| 5379 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 5380 | X509_V_FLAG_TRUSTED_FIRST); |
| 5381 | #endif |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 5382 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5383 | /* Alert Descriptions from ssl.h */ |
| 5384 | /* note RESERVED constants no longer intended for use have been removed */ |
| 5385 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 5386 | |
| 5387 | #define ADD_AD_CONSTANT(s) \ |
| 5388 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 5389 | SSL_AD_##s) |
| 5390 | |
| 5391 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 5392 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 5393 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 5394 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 5395 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 5396 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 5397 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 5398 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 5399 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 5400 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 5401 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 5402 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 5403 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 5404 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 5405 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 5406 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 5407 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 5408 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 5409 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 5410 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 5411 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5412 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5413 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 5414 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 5415 | #endif |
| 5416 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 5417 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 5418 | #endif |
| 5419 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 5420 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 5421 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5422 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 5423 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 5424 | #endif |
| 5425 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 5426 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 5427 | #endif |
| 5428 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 5429 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 5430 | #endif |
| 5431 | |
| 5432 | #undef ADD_AD_CONSTANT |
| 5433 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5434 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5435 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5436 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 5437 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5438 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5439 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5440 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 5441 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5442 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5443 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5444 | PY_SSL_VERSION_TLS); |
| 5445 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 5446 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 5447 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 5448 | PY_SSL_VERSION_TLS_CLIENT); |
| 5449 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 5450 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5451 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 5452 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5453 | #if HAVE_TLSv1_2 |
| 5454 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 5455 | PY_SSL_VERSION_TLS1_1); |
| 5456 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 5457 | PY_SSL_VERSION_TLS1_2); |
| 5458 | #endif |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5459 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5460 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 5461 | PyModule_AddIntConstant(m, "OP_ALL", |
| 5462 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5463 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 5464 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 5465 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5466 | #if HAVE_TLSv1_2 |
| 5467 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 5468 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
| 5469 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5470 | #ifdef SSL_OP_NO_TLSv1_3 |
| 5471 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 5472 | #else |
| 5473 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 5474 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 5475 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 5476 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 5477 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5478 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5479 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 5480 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5481 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 5482 | #ifdef SSL_OP_NO_COMPRESSION |
| 5483 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 5484 | SSL_OP_NO_COMPRESSION); |
| 5485 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5486 | |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5487 | #if HAVE_SNI |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 5488 | r = Py_True; |
| 5489 | #else |
| 5490 | r = Py_False; |
| 5491 | #endif |
| 5492 | Py_INCREF(r); |
| 5493 | PyModule_AddObject(m, "HAS_SNI", r); |
| 5494 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 5495 | r = Py_True; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 5496 | Py_INCREF(r); |
| 5497 | PyModule_AddObject(m, "HAS_TLS_UNIQUE", r); |
| 5498 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 5499 | #ifdef OPENSSL_NO_ECDH |
| 5500 | r = Py_False; |
| 5501 | #else |
| 5502 | r = Py_True; |
| 5503 | #endif |
| 5504 | Py_INCREF(r); |
| 5505 | PyModule_AddObject(m, "HAS_ECDH", r); |
| 5506 | |
Melvyn Sopacua | b2d096b | 2017-09-04 23:35:15 +0200 | [diff] [blame] | 5507 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 5508 | r = Py_True; |
| 5509 | #else |
| 5510 | r = Py_False; |
| 5511 | #endif |
| 5512 | Py_INCREF(r); |
| 5513 | PyModule_AddObject(m, "HAS_NPN", r); |
| 5514 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 5515 | #ifdef HAVE_ALPN |
| 5516 | r = Py_True; |
| 5517 | #else |
| 5518 | r = Py_False; |
| 5519 | #endif |
| 5520 | Py_INCREF(r); |
| 5521 | PyModule_AddObject(m, "HAS_ALPN", r); |
| 5522 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5523 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 5524 | r = Py_True; |
| 5525 | #else |
| 5526 | r = Py_False; |
| 5527 | #endif |
| 5528 | Py_INCREF(r); |
| 5529 | PyModule_AddObject(m, "HAS_TLSv1_3", r); |
| 5530 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 5531 | /* Mappings for error codes */ |
| 5532 | err_codes_to_names = PyDict_New(); |
| 5533 | err_names_to_codes = PyDict_New(); |
| 5534 | if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 5535 | return NULL; |
| 5536 | errcode = error_codes; |
| 5537 | while (errcode->mnemonic != NULL) { |
| 5538 | PyObject *mnemo, *key; |
| 5539 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 5540 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 5541 | if (mnemo == NULL || key == NULL) |
| 5542 | return NULL; |
| 5543 | if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 5544 | return NULL; |
| 5545 | if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 5546 | return NULL; |
| 5547 | Py_DECREF(key); |
| 5548 | Py_DECREF(mnemo); |
| 5549 | errcode++; |
| 5550 | } |
| 5551 | if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 5552 | return NULL; |
| 5553 | if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 5554 | return NULL; |
| 5555 | |
| 5556 | lib_codes_to_names = PyDict_New(); |
| 5557 | if (lib_codes_to_names == NULL) |
| 5558 | return NULL; |
| 5559 | libcode = library_codes; |
| 5560 | while (libcode->library != NULL) { |
| 5561 | PyObject *mnemo, *key; |
| 5562 | key = PyLong_FromLong(libcode->code); |
| 5563 | mnemo = PyUnicode_FromString(libcode->library); |
| 5564 | if (key == NULL || mnemo == NULL) |
| 5565 | return NULL; |
| 5566 | if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 5567 | return NULL; |
| 5568 | Py_DECREF(key); |
| 5569 | Py_DECREF(mnemo); |
| 5570 | libcode++; |
| 5571 | } |
| 5572 | if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 5573 | return NULL; |
Victor Stinner | 4569cd5 | 2013-06-23 14:58:43 +0200 | [diff] [blame] | 5574 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5575 | /* OpenSSL version */ |
| 5576 | /* SSLeay() gives us the version of the library linked against, |
| 5577 | which could be different from the headers version. |
| 5578 | */ |
| 5579 | libver = SSLeay(); |
| 5580 | r = PyLong_FromUnsignedLong(libver); |
| 5581 | if (r == NULL) |
| 5582 | return NULL; |
| 5583 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 5584 | return NULL; |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5585 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5586 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 5587 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 5588 | return NULL; |
| 5589 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 5590 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 5591 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5592 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 5593 | libver = OPENSSL_VERSION_NUMBER; |
| 5594 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 5595 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 5596 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 5597 | return NULL; |
| 5598 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5599 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5600 | } |