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