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