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