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