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