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