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