Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 19 | #ifdef WITH_THREAD |
| 20 | #include "pythread.h" |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 21 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 22 | do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 23 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
| 24 | do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } } while (0) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 25 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 26 | PyThreadState *_save = NULL; \ |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 27 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 28 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 29 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 30 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 31 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 32 | #else /* no WITH_THREAD */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 33 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 34 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) |
| 35 | #define PySSL_END_ALLOW_THREADS_S(save) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 36 | #define PySSL_BEGIN_ALLOW_THREADS |
| 37 | #define PySSL_BLOCK_THREADS |
| 38 | #define PySSL_UNBLOCK_THREADS |
| 39 | #define PySSL_END_ALLOW_THREADS |
| 40 | |
| 41 | #endif |
| 42 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 43 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 44 | /* these mirror ssl.h */ |
| 45 | PY_SSL_ERROR_NONE, |
| 46 | PY_SSL_ERROR_SSL, |
| 47 | PY_SSL_ERROR_WANT_READ, |
| 48 | PY_SSL_ERROR_WANT_WRITE, |
| 49 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 50 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 51 | PY_SSL_ERROR_ZERO_RETURN, |
| 52 | PY_SSL_ERROR_WANT_CONNECT, |
| 53 | /* start of non ssl.h errorcodes */ |
| 54 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 55 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 56 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 57 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 59 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 60 | PY_SSL_CLIENT, |
| 61 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 65 | PY_SSL_CERT_NONE, |
| 66 | PY_SSL_CERT_OPTIONAL, |
| 67 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | enum py_ssl_version { |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 71 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 72 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 73 | #endif |
| 74 | PY_SSL_VERSION_SSL3=1, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 75 | PY_SSL_VERSION_SSL23, |
| 76 | PY_SSL_VERSION_TLS1 |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 79 | /* Include symbols from _socket module */ |
| 80 | #include "socketmodule.h" |
| 81 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 82 | static PySocketModule_APIObject PySocketModule; |
| 83 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 84 | #if defined(HAVE_POLL_H) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 85 | #include <poll.h> |
| 86 | #elif defined(HAVE_SYS_POLL_H) |
| 87 | #include <sys/poll.h> |
| 88 | #endif |
| 89 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 90 | /* Include OpenSSL header files */ |
| 91 | #include "openssl/rsa.h" |
| 92 | #include "openssl/crypto.h" |
| 93 | #include "openssl/x509.h" |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 94 | #include "openssl/x509v3.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 95 | #include "openssl/pem.h" |
| 96 | #include "openssl/ssl.h" |
| 97 | #include "openssl/err.h" |
| 98 | #include "openssl/rand.h" |
| 99 | |
| 100 | /* SSL error object */ |
| 101 | static PyObject *PySSLErrorObject; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 102 | static PyObject *PySSLZeroReturnErrorObject; |
| 103 | static PyObject *PySSLWantReadErrorObject; |
| 104 | static PyObject *PySSLWantWriteErrorObject; |
| 105 | static PyObject *PySSLSyscallErrorObject; |
| 106 | static PyObject *PySSLEOFErrorObject; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 107 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 108 | #ifdef WITH_THREAD |
| 109 | |
| 110 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 111 | /* 0 means no, greater than 0 means yes */ |
| 112 | |
| 113 | static unsigned int _ssl_locks_count = 0; |
| 114 | |
| 115 | #endif /* def WITH_THREAD */ |
| 116 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 117 | /* SSL socket object */ |
| 118 | |
| 119 | #define X509_NAME_MAXLEN 256 |
| 120 | |
| 121 | /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
| 122 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 123 | # define HAVE_OPENSSL_RAND 1 |
| 124 | #else |
| 125 | # undef HAVE_OPENSSL_RAND |
| 126 | #endif |
| 127 | |
Gregory P. Smith | bd4dacb | 2010-10-13 03:53:21 +0000 | [diff] [blame] | 128 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in |
| 129 | * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the |
| 130 | * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */ |
| 131 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 132 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 133 | #else |
| 134 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 135 | #endif |
| 136 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 137 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 138 | * older SSL, but let's be safe */ |
| 139 | #define PySSL_CB_MAXLEN 128 |
| 140 | |
| 141 | /* SSL_get_finished got added to OpenSSL in 0.9.5 */ |
| 142 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 143 | # define HAVE_OPENSSL_FINISHED 1 |
| 144 | #else |
| 145 | # define HAVE_OPENSSL_FINISHED 0 |
| 146 | #endif |
| 147 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 148 | /* ECDH support got added to OpenSSL in 0.9.8 */ |
| 149 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_ECDH) |
| 150 | # define OPENSSL_NO_ECDH |
| 151 | #endif |
| 152 | |
Antoine Pitrou | c135fa4 | 2012-02-19 21:22:39 +0100 | [diff] [blame^] | 153 | /* compression support got added to OpenSSL in 0.9.8 */ |
| 154 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_COMP) |
| 155 | # define OPENSSL_NO_COMP |
| 156 | #endif |
| 157 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 158 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 159 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 160 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 161 | SSL_CTX *ctx; |
| 162 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 164 | typedef struct { |
| 165 | PyObject_HEAD |
| 166 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 167 | SSL *ssl; |
| 168 | X509 *peer_cert; |
| 169 | int shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 170 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 171 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 173 | static PyTypeObject PySSLContext_Type; |
| 174 | static PyTypeObject PySSLSocket_Type; |
| 175 | |
| 176 | static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args); |
| 177 | static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 178 | static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 179 | int writing); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 180 | static PyObject *PySSL_peercert(PySSLSocket *self, PyObject *args); |
| 181 | static PyObject *PySSL_cipher(PySSLSocket *self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 183 | #define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type) |
| 184 | #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 185 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 186 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 187 | SOCKET_IS_NONBLOCKING, |
| 188 | SOCKET_IS_BLOCKING, |
| 189 | SOCKET_HAS_TIMED_OUT, |
| 190 | SOCKET_HAS_BEEN_CLOSED, |
| 191 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 192 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 193 | } timeout_state; |
| 194 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 195 | /* Wrap error strings with filename and line # */ |
| 196 | #define STRINGIFY1(x) #x |
| 197 | #define STRINGIFY2(x) STRINGIFY1(x) |
| 198 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
| 199 | #define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) |
| 200 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 201 | /* XXX It might be helpful to augment the error message generated |
| 202 | below with the name of the SSL function that generated the error. |
| 203 | I expect it's obvious most of the time. |
| 204 | */ |
| 205 | |
| 206 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 207 | PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 208 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 209 | PyObject *v; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 210 | PyObject *type = PySSLErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 211 | char buf[2048]; |
| 212 | char *errstr; |
| 213 | int err; |
| 214 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 216 | assert(ret <= 0); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 218 | if (obj->ssl != NULL) { |
| 219 | err = SSL_get_error(obj->ssl, ret); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 221 | switch (err) { |
| 222 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 223 | errstr = "TLS/SSL connection has been closed (EOF)"; |
| 224 | type = PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 225 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 226 | break; |
| 227 | case SSL_ERROR_WANT_READ: |
| 228 | errstr = "The operation did not complete (read)"; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 229 | type = PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 230 | p = PY_SSL_ERROR_WANT_READ; |
| 231 | break; |
| 232 | case SSL_ERROR_WANT_WRITE: |
| 233 | p = PY_SSL_ERROR_WANT_WRITE; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 234 | type = PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 235 | errstr = "The operation did not complete (write)"; |
| 236 | break; |
| 237 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 238 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 239 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 240 | break; |
| 241 | case SSL_ERROR_WANT_CONNECT: |
| 242 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 243 | errstr = "The operation did not complete (connect)"; |
| 244 | break; |
| 245 | case SSL_ERROR_SYSCALL: |
| 246 | { |
| 247 | unsigned long e = ERR_get_error(); |
| 248 | if (e == 0) { |
| 249 | PySocketSockObject *s |
| 250 | = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket); |
| 251 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 252 | p = PY_SSL_ERROR_EOF; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 253 | type = PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 254 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 255 | } else if (ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 256 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 257 | Py_INCREF(s); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 258 | ERR_clear_error(); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 259 | v = s->errorhandler(); |
| 260 | Py_DECREF(s); |
| 261 | return v; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 262 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 263 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 264 | type = PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 265 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 266 | } |
| 267 | } else { |
| 268 | p = PY_SSL_ERROR_SYSCALL; |
| 269 | /* XXX Protected by global interpreter lock */ |
| 270 | errstr = ERR_error_string(e, NULL); |
| 271 | } |
| 272 | break; |
| 273 | } |
| 274 | case SSL_ERROR_SSL: |
| 275 | { |
| 276 | unsigned long e = ERR_get_error(); |
| 277 | p = PY_SSL_ERROR_SSL; |
| 278 | if (e != 0) |
| 279 | /* XXX Protected by global interpreter lock */ |
| 280 | errstr = ERR_error_string(e, NULL); |
| 281 | else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 282 | errstr = "A failure in the SSL library occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 283 | } |
| 284 | break; |
| 285 | } |
| 286 | default: |
| 287 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 288 | errstr = "Invalid error code"; |
| 289 | } |
| 290 | } else { |
| 291 | errstr = ERR_error_string(ERR_peek_last_error(), NULL); |
| 292 | } |
| 293 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 294 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 295 | v = Py_BuildValue("(is)", p, buf); |
| 296 | if (v != NULL) { |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 297 | PyErr_SetObject(type, v); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 298 | Py_DECREF(v); |
| 299 | } |
| 300 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 303 | static PyObject * |
| 304 | _setSSLError (char *errstr, int errcode, char *filename, int lineno) { |
| 305 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 306 | char buf[2048]; |
| 307 | PyObject *v; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 309 | if (errstr == NULL) { |
| 310 | errcode = ERR_peek_last_error(); |
| 311 | errstr = ERR_error_string(errcode, NULL); |
| 312 | } |
| 313 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 314 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 315 | v = Py_BuildValue("(is)", errcode, buf); |
| 316 | if (v != NULL) { |
| 317 | PyErr_SetObject(PySSLErrorObject, v); |
| 318 | Py_DECREF(v); |
| 319 | } |
| 320 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 323 | static PySSLSocket * |
| 324 | newPySSLSocket(SSL_CTX *ctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 325 | enum py_ssl_server_or_client socket_type, |
| 326 | char *server_hostname) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 328 | PySSLSocket *self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 330 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 331 | if (self == NULL) |
| 332 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 334 | self->peer_cert = NULL; |
| 335 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 336 | self->Socket = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 338 | /* Make sure the SSL error state is initialized */ |
| 339 | (void) ERR_get_state(); |
| 340 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 342 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 343 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 344 | PySSL_END_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 345 | SSL_set_fd(self->ssl, sock->sock_fd); |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 346 | #ifdef SSL_MODE_AUTO_RETRY |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 347 | SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY); |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 348 | #endif |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 350 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 351 | if (server_hostname != NULL) |
| 352 | SSL_set_tlsext_host_name(self->ssl, server_hostname); |
| 353 | #endif |
| 354 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 355 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 356 | * to non-blocking mode (blocking is the default) |
| 357 | */ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 358 | if (sock->sock_timeout >= 0.0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 359 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 360 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 361 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 362 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 363 | PySSL_BEGIN_ALLOW_THREADS |
| 364 | if (socket_type == PY_SSL_CLIENT) |
| 365 | SSL_set_connect_state(self->ssl); |
| 366 | else |
| 367 | SSL_set_accept_state(self->ssl); |
| 368 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 370 | self->socket_type = socket_type; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 371 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 372 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 375 | /* SSL object methods */ |
| 376 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 377 | static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 378 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 379 | int ret; |
| 380 | int err; |
| 381 | int sockstate, nonblocking; |
| 382 | PySocketSockObject *sock |
| 383 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 384 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 385 | if (((PyObject*)sock) == Py_None) { |
| 386 | _setSSLError("Underlying socket connection gone", |
| 387 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 388 | return NULL; |
| 389 | } |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 390 | Py_INCREF(sock); |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 391 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 392 | /* just in case the blocking state of the socket has been changed */ |
| 393 | nonblocking = (sock->sock_timeout >= 0.0); |
| 394 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 395 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 397 | /* Actually negotiate SSL connection */ |
| 398 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 399 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 400 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 401 | ret = SSL_do_handshake(self->ssl); |
| 402 | err = SSL_get_error(self->ssl, ret); |
| 403 | PySSL_END_ALLOW_THREADS |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 404 | if (PyErr_CheckSignals()) |
| 405 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 406 | if (err == SSL_ERROR_WANT_READ) { |
| 407 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 408 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 409 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 410 | } else { |
| 411 | sockstate = SOCKET_OPERATION_OK; |
| 412 | } |
| 413 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 414 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 415 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 416 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 417 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 418 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 419 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 420 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 421 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 422 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 423 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 424 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 425 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 426 | break; |
| 427 | } |
| 428 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 429 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 430 | if (ret < 1) |
| 431 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 433 | if (self->peer_cert) |
| 434 | X509_free (self->peer_cert); |
| 435 | PySSL_BEGIN_ALLOW_THREADS |
| 436 | self->peer_cert = SSL_get_peer_certificate(self->ssl); |
| 437 | PySSL_END_ALLOW_THREADS |
| 438 | |
| 439 | Py_INCREF(Py_None); |
| 440 | return Py_None; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 441 | |
| 442 | error: |
| 443 | Py_DECREF(sock); |
| 444 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 447 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 448 | _create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 450 | char namebuf[X509_NAME_MAXLEN]; |
| 451 | int buflen; |
| 452 | PyObject *name_obj; |
| 453 | PyObject *value_obj; |
| 454 | PyObject *attr; |
| 455 | unsigned char *valuebuf = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 456 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 457 | buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0); |
| 458 | if (buflen < 0) { |
| 459 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 460 | goto fail; |
| 461 | } |
| 462 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 463 | if (name_obj == NULL) |
| 464 | goto fail; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 466 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 467 | if (buflen < 0) { |
| 468 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 469 | Py_DECREF(name_obj); |
| 470 | goto fail; |
| 471 | } |
| 472 | value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 473 | buflen, "strict"); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 474 | OPENSSL_free(valuebuf); |
| 475 | if (value_obj == NULL) { |
| 476 | Py_DECREF(name_obj); |
| 477 | goto fail; |
| 478 | } |
| 479 | attr = PyTuple_New(2); |
| 480 | if (attr == NULL) { |
| 481 | Py_DECREF(name_obj); |
| 482 | Py_DECREF(value_obj); |
| 483 | goto fail; |
| 484 | } |
| 485 | PyTuple_SET_ITEM(attr, 0, name_obj); |
| 486 | PyTuple_SET_ITEM(attr, 1, value_obj); |
| 487 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 488 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 489 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 490 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 494 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 495 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 496 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 497 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 498 | PyObject *rdnt; |
| 499 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 500 | int entry_count = X509_NAME_entry_count(xname); |
| 501 | X509_NAME_ENTRY *entry; |
| 502 | ASN1_OBJECT *name; |
| 503 | ASN1_STRING *value; |
| 504 | int index_counter; |
| 505 | int rdn_level = -1; |
| 506 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 508 | dn = PyList_New(0); |
| 509 | if (dn == NULL) |
| 510 | return NULL; |
| 511 | /* now create another tuple to hold the top-level RDN */ |
| 512 | rdn = PyList_New(0); |
| 513 | if (rdn == NULL) |
| 514 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 516 | for (index_counter = 0; |
| 517 | index_counter < entry_count; |
| 518 | index_counter++) |
| 519 | { |
| 520 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 522 | /* check to see if we've gotten to a new RDN */ |
| 523 | if (rdn_level >= 0) { |
| 524 | if (rdn_level != entry->set) { |
| 525 | /* yes, new RDN */ |
| 526 | /* add old RDN to DN */ |
| 527 | rdnt = PyList_AsTuple(rdn); |
| 528 | Py_DECREF(rdn); |
| 529 | if (rdnt == NULL) |
| 530 | goto fail0; |
| 531 | retcode = PyList_Append(dn, rdnt); |
| 532 | Py_DECREF(rdnt); |
| 533 | if (retcode < 0) |
| 534 | goto fail0; |
| 535 | /* create new RDN */ |
| 536 | rdn = PyList_New(0); |
| 537 | if (rdn == NULL) |
| 538 | goto fail0; |
| 539 | } |
| 540 | } |
| 541 | rdn_level = entry->set; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 543 | /* now add this attribute to the current RDN */ |
| 544 | name = X509_NAME_ENTRY_get_object(entry); |
| 545 | value = X509_NAME_ENTRY_get_data(entry); |
| 546 | attr = _create_tuple_for_attribute(name, value); |
| 547 | /* |
| 548 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 549 | entry->set, |
| 550 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 551 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 552 | */ |
| 553 | if (attr == NULL) |
| 554 | goto fail1; |
| 555 | retcode = PyList_Append(rdn, attr); |
| 556 | Py_DECREF(attr); |
| 557 | if (retcode < 0) |
| 558 | goto fail1; |
| 559 | } |
| 560 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 561 | if (rdn != NULL) { |
| 562 | if (PyList_GET_SIZE(rdn) > 0) { |
| 563 | rdnt = PyList_AsTuple(rdn); |
| 564 | Py_DECREF(rdn); |
| 565 | if (rdnt == NULL) |
| 566 | goto fail0; |
| 567 | retcode = PyList_Append(dn, rdnt); |
| 568 | Py_DECREF(rdnt); |
| 569 | if (retcode < 0) |
| 570 | goto fail0; |
| 571 | } |
| 572 | else { |
| 573 | Py_DECREF(rdn); |
| 574 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 575 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 577 | /* convert list to tuple */ |
| 578 | rdnt = PyList_AsTuple(dn); |
| 579 | Py_DECREF(dn); |
| 580 | if (rdnt == NULL) |
| 581 | return NULL; |
| 582 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 583 | |
| 584 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 585 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 586 | |
| 587 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 588 | Py_XDECREF(dn); |
| 589 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static PyObject * |
| 593 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 595 | /* this code follows the procedure outlined in |
| 596 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 597 | function to extract the STACK_OF(GENERAL_NAME), |
| 598 | then iterates through the stack to add the |
| 599 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 600 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 601 | int i, j; |
| 602 | PyObject *peer_alt_names = Py_None; |
| 603 | PyObject *v, *t; |
| 604 | X509_EXTENSION *ext = NULL; |
| 605 | GENERAL_NAMES *names = NULL; |
| 606 | GENERAL_NAME *name; |
Benjamin Peterson | eb1410f | 2010-10-13 22:06:39 +0000 | [diff] [blame] | 607 | const X509V3_EXT_METHOD *method; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 608 | BIO *biobuf = NULL; |
| 609 | char buf[2048]; |
| 610 | char *vptr; |
| 611 | int len; |
| 612 | /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */ |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 613 | #if OPENSSL_VERSION_NUMBER >= 0x009060dfL |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 614 | const unsigned char *p; |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 615 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 616 | unsigned char *p; |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 617 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 619 | if (certificate == NULL) |
| 620 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 622 | /* get a memory buffer */ |
| 623 | biobuf = BIO_new(BIO_s_mem()); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | d8c347a | 2011-10-01 19:20:25 +0200 | [diff] [blame] | 625 | i = -1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 626 | while ((i = X509_get_ext_by_NID( |
| 627 | certificate, NID_subject_alt_name, i)) >= 0) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 629 | if (peer_alt_names == Py_None) { |
| 630 | peer_alt_names = PyList_New(0); |
| 631 | if (peer_alt_names == NULL) |
| 632 | goto fail; |
| 633 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 635 | /* now decode the altName */ |
| 636 | ext = X509_get_ext(certificate, i); |
| 637 | if(!(method = X509V3_EXT_get(ext))) { |
| 638 | PyErr_SetString |
| 639 | (PySSLErrorObject, |
| 640 | ERRSTR("No method for internalizing subjectAltName!")); |
| 641 | goto fail; |
| 642 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 644 | p = ext->value->data; |
| 645 | if (method->it) |
| 646 | names = (GENERAL_NAMES*) |
| 647 | (ASN1_item_d2i(NULL, |
| 648 | &p, |
| 649 | ext->value->length, |
| 650 | ASN1_ITEM_ptr(method->it))); |
| 651 | else |
| 652 | names = (GENERAL_NAMES*) |
| 653 | (method->d2i(NULL, |
| 654 | &p, |
| 655 | ext->value->length)); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 657 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 659 | /* get a rendering of each name in the set of names */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 661 | name = sk_GENERAL_NAME_value(names, j); |
| 662 | if (name->type == GEN_DIRNAME) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 664 | /* we special-case DirName as a tuple of |
| 665 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 667 | t = PyTuple_New(2); |
| 668 | if (t == NULL) { |
| 669 | goto fail; |
| 670 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 672 | v = PyUnicode_FromString("DirName"); |
| 673 | if (v == NULL) { |
| 674 | Py_DECREF(t); |
| 675 | goto fail; |
| 676 | } |
| 677 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 679 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 680 | if (v == NULL) { |
| 681 | Py_DECREF(t); |
| 682 | goto fail; |
| 683 | } |
| 684 | PyTuple_SET_ITEM(t, 1, v); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 686 | } else { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 688 | /* for everything else, we use the OpenSSL print form */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 689 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 690 | (void) BIO_reset(biobuf); |
| 691 | GENERAL_NAME_print(biobuf, name); |
| 692 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 693 | if (len < 0) { |
| 694 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 695 | goto fail; |
| 696 | } |
| 697 | vptr = strchr(buf, ':'); |
| 698 | if (vptr == NULL) |
| 699 | goto fail; |
| 700 | t = PyTuple_New(2); |
| 701 | if (t == NULL) |
| 702 | goto fail; |
| 703 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 704 | if (v == NULL) { |
| 705 | Py_DECREF(t); |
| 706 | goto fail; |
| 707 | } |
| 708 | PyTuple_SET_ITEM(t, 0, v); |
| 709 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 710 | (len - (vptr - buf + 1))); |
| 711 | if (v == NULL) { |
| 712 | Py_DECREF(t); |
| 713 | goto fail; |
| 714 | } |
| 715 | PyTuple_SET_ITEM(t, 1, v); |
| 716 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 718 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 720 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 721 | Py_DECREF(t); |
| 722 | goto fail; |
| 723 | } |
| 724 | Py_DECREF(t); |
| 725 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 726 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 727 | } |
| 728 | BIO_free(biobuf); |
| 729 | if (peer_alt_names != Py_None) { |
| 730 | v = PyList_AsTuple(peer_alt_names); |
| 731 | Py_DECREF(peer_alt_names); |
| 732 | return v; |
| 733 | } else { |
| 734 | return peer_alt_names; |
| 735 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 736 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 737 | |
| 738 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 739 | if (biobuf != NULL) |
| 740 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 741 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 742 | if (peer_alt_names != Py_None) { |
| 743 | Py_XDECREF(peer_alt_names); |
| 744 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 746 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static PyObject * |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 750 | _decode_certificate(X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 752 | PyObject *retval = NULL; |
| 753 | BIO *biobuf = NULL; |
| 754 | PyObject *peer; |
| 755 | PyObject *peer_alt_names = NULL; |
| 756 | PyObject *issuer; |
| 757 | PyObject *version; |
| 758 | PyObject *sn_obj; |
| 759 | ASN1_INTEGER *serialNumber; |
| 760 | char buf[2048]; |
| 761 | int len; |
| 762 | ASN1_TIME *notBefore, *notAfter; |
| 763 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 764 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 765 | retval = PyDict_New(); |
| 766 | if (retval == NULL) |
| 767 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 769 | peer = _create_tuple_for_X509_NAME( |
| 770 | X509_get_subject_name(certificate)); |
| 771 | if (peer == NULL) |
| 772 | goto fail0; |
| 773 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 774 | Py_DECREF(peer); |
| 775 | goto fail0; |
| 776 | } |
| 777 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 778 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 779 | issuer = _create_tuple_for_X509_NAME( |
| 780 | X509_get_issuer_name(certificate)); |
| 781 | if (issuer == NULL) |
| 782 | goto fail0; |
| 783 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 784 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 785 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 786 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 787 | Py_DECREF(issuer); |
| 788 | |
| 789 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
| 790 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 791 | Py_DECREF(version); |
| 792 | goto fail0; |
| 793 | } |
| 794 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 796 | /* get a memory buffer */ |
| 797 | biobuf = BIO_new(BIO_s_mem()); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 799 | (void) BIO_reset(biobuf); |
| 800 | serialNumber = X509_get_serialNumber(certificate); |
| 801 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 802 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 803 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 804 | if (len < 0) { |
| 805 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 806 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 807 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 808 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 809 | if (sn_obj == NULL) |
| 810 | goto fail1; |
| 811 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 812 | Py_DECREF(sn_obj); |
| 813 | goto fail1; |
| 814 | } |
| 815 | Py_DECREF(sn_obj); |
| 816 | |
| 817 | (void) BIO_reset(biobuf); |
| 818 | notBefore = X509_get_notBefore(certificate); |
| 819 | ASN1_TIME_print(biobuf, notBefore); |
| 820 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 821 | if (len < 0) { |
| 822 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 823 | goto fail1; |
| 824 | } |
| 825 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 826 | if (pnotBefore == NULL) |
| 827 | goto fail1; |
| 828 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 829 | Py_DECREF(pnotBefore); |
| 830 | goto fail1; |
| 831 | } |
| 832 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 834 | (void) BIO_reset(biobuf); |
| 835 | notAfter = X509_get_notAfter(certificate); |
| 836 | ASN1_TIME_print(biobuf, notAfter); |
| 837 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 838 | if (len < 0) { |
| 839 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 840 | goto fail1; |
| 841 | } |
| 842 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 843 | if (pnotAfter == NULL) |
| 844 | goto fail1; |
| 845 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 846 | Py_DECREF(pnotAfter); |
| 847 | goto fail1; |
| 848 | } |
| 849 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 850 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 851 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 853 | peer_alt_names = _get_peer_alt_names(certificate); |
| 854 | if (peer_alt_names == NULL) |
| 855 | goto fail1; |
| 856 | else if (peer_alt_names != Py_None) { |
| 857 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 858 | peer_alt_names) < 0) { |
| 859 | Py_DECREF(peer_alt_names); |
| 860 | goto fail1; |
| 861 | } |
| 862 | Py_DECREF(peer_alt_names); |
| 863 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 865 | BIO_free(biobuf); |
| 866 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 867 | |
| 868 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 869 | if (biobuf != NULL) |
| 870 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 871 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 872 | Py_XDECREF(retval); |
| 873 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 874 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 875 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 876 | |
| 877 | static PyObject * |
| 878 | PySSL_test_decode_certificate (PyObject *mod, PyObject *args) { |
| 879 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 880 | PyObject *retval = NULL; |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 881 | PyObject *filename; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 882 | X509 *x=NULL; |
| 883 | BIO *cert; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 885 | if (!PyArg_ParseTuple(args, "O&:test_decode_certificate", |
| 886 | PyUnicode_FSConverter, &filename)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 887 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 889 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 890 | PyErr_SetString(PySSLErrorObject, |
| 891 | "Can't malloc memory to read file"); |
| 892 | goto fail0; |
| 893 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 894 | |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 895 | if (BIO_read_filename(cert, PyBytes_AsString(filename)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 896 | PyErr_SetString(PySSLErrorObject, |
| 897 | "Can't open file"); |
| 898 | goto fail0; |
| 899 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 901 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 902 | if (x == NULL) { |
| 903 | PyErr_SetString(PySSLErrorObject, |
| 904 | "Error decoding PEM-encoded file"); |
| 905 | goto fail0; |
| 906 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 908 | retval = _decode_certificate(x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 909 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 910 | |
| 911 | fail0: |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 912 | Py_DECREF(filename); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 913 | if (cert != NULL) BIO_free(cert); |
| 914 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | |
| 918 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 919 | PySSL_peercert(PySSLSocket *self, PyObject *args) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 921 | PyObject *retval = NULL; |
| 922 | int len; |
| 923 | int verification; |
| 924 | PyObject *binary_mode = Py_None; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 926 | if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode)) |
| 927 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 929 | if (!self->peer_cert) |
| 930 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 932 | if (PyObject_IsTrue(binary_mode)) { |
| 933 | /* return cert in DER-encoded format */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 935 | unsigned char *bytes_buf = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 937 | bytes_buf = NULL; |
| 938 | len = i2d_X509(self->peer_cert, &bytes_buf); |
| 939 | if (len < 0) { |
| 940 | PySSL_SetError(self, len, __FILE__, __LINE__); |
| 941 | return NULL; |
| 942 | } |
| 943 | /* this is actually an immutable bytes sequence */ |
| 944 | retval = PyBytes_FromStringAndSize |
| 945 | ((const char *) bytes_buf, len); |
| 946 | OPENSSL_free(bytes_buf); |
| 947 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 949 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 950 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 951 | if ((verification & SSL_VERIFY_PEER) == 0) |
| 952 | return PyDict_New(); |
| 953 | else |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 954 | return _decode_certificate(self->peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 955 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | PyDoc_STRVAR(PySSL_peercert_doc, |
| 959 | "peer_certificate([der=False]) -> certificate\n\ |
| 960 | \n\ |
| 961 | Returns the certificate for the peer. If no certificate was provided,\n\ |
| 962 | returns None. If a certificate was provided, but not validated, returns\n\ |
| 963 | an empty dictionary. Otherwise returns a dict containing information\n\ |
| 964 | about the peer certificate.\n\ |
| 965 | \n\ |
| 966 | If the optional argument is True, returns a DER-encoded copy of the\n\ |
| 967 | peer certificate, or None if no certificate was provided. This will\n\ |
| 968 | return the certificate even if it wasn't validated."); |
| 969 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 970 | static PyObject *PySSL_cipher (PySSLSocket *self) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 972 | PyObject *retval, *v; |
Benjamin Peterson | eb1410f | 2010-10-13 22:06:39 +0000 | [diff] [blame] | 973 | const SSL_CIPHER *current; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 974 | char *cipher_name; |
| 975 | char *cipher_protocol; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 977 | if (self->ssl == NULL) |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 978 | Py_RETURN_NONE; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 979 | current = SSL_get_current_cipher(self->ssl); |
| 980 | if (current == NULL) |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 981 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 983 | retval = PyTuple_New(3); |
| 984 | if (retval == NULL) |
| 985 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 987 | cipher_name = (char *) SSL_CIPHER_get_name(current); |
| 988 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 989 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 990 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 991 | } else { |
| 992 | v = PyUnicode_FromString(cipher_name); |
| 993 | if (v == NULL) |
| 994 | goto fail0; |
| 995 | PyTuple_SET_ITEM(retval, 0, v); |
| 996 | } |
| 997 | cipher_protocol = SSL_CIPHER_get_version(current); |
| 998 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 999 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1000 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1001 | } else { |
| 1002 | v = PyUnicode_FromString(cipher_protocol); |
| 1003 | if (v == NULL) |
| 1004 | goto fail0; |
| 1005 | PyTuple_SET_ITEM(retval, 1, v); |
| 1006 | } |
| 1007 | v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL)); |
| 1008 | if (v == NULL) |
| 1009 | goto fail0; |
| 1010 | PyTuple_SET_ITEM(retval, 2, v); |
| 1011 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1012 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1013 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1014 | Py_DECREF(retval); |
| 1015 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1018 | static PyObject *PySSL_compression(PySSLSocket *self) { |
| 1019 | #ifdef OPENSSL_NO_COMP |
| 1020 | Py_RETURN_NONE; |
| 1021 | #else |
| 1022 | const COMP_METHOD *comp_method; |
| 1023 | const char *short_name; |
| 1024 | |
| 1025 | if (self->ssl == NULL) |
| 1026 | Py_RETURN_NONE; |
| 1027 | comp_method = SSL_get_current_compression(self->ssl); |
| 1028 | if (comp_method == NULL || comp_method->type == NID_undef) |
| 1029 | Py_RETURN_NONE; |
| 1030 | short_name = OBJ_nid2sn(comp_method->type); |
| 1031 | if (short_name == NULL) |
| 1032 | Py_RETURN_NONE; |
| 1033 | return PyUnicode_DecodeFSDefault(short_name); |
| 1034 | #endif |
| 1035 | } |
| 1036 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1037 | static void PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1038 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1039 | if (self->peer_cert) /* Possible not to have one? */ |
| 1040 | X509_free (self->peer_cert); |
| 1041 | if (self->ssl) |
| 1042 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1043 | Py_XDECREF(self->Socket); |
| 1044 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1047 | /* 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] | 1048 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1049 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1050 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1051 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1052 | static int |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1053 | check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1054 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1055 | fd_set fds; |
| 1056 | struct timeval tv; |
| 1057 | int rc; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1059 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 1060 | if (s->sock_timeout < 0.0) |
| 1061 | return SOCKET_IS_BLOCKING; |
| 1062 | else if (s->sock_timeout == 0.0) |
| 1063 | return SOCKET_IS_NONBLOCKING; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1065 | /* Guard against closed socket */ |
| 1066 | if (s->sock_fd < 0) |
| 1067 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1069 | /* Prefer poll, if available, since you can poll() any fd |
| 1070 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1071 | #ifdef HAVE_POLL |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1072 | { |
| 1073 | struct pollfd pollfd; |
| 1074 | int timeout; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1075 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1076 | pollfd.fd = s->sock_fd; |
| 1077 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1079 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 1080 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 1081 | PySSL_BEGIN_ALLOW_THREADS |
| 1082 | rc = poll(&pollfd, 1, timeout); |
| 1083 | PySSL_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1085 | goto normal_return; |
| 1086 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1087 | #endif |
| 1088 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1089 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 1090 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1091 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1093 | /* Construct the arguments to select */ |
| 1094 | tv.tv_sec = (int)s->sock_timeout; |
| 1095 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1096 | FD_ZERO(&fds); |
| 1097 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1099 | /* See if the socket is ready */ |
| 1100 | PySSL_BEGIN_ALLOW_THREADS |
| 1101 | if (writing) |
| 1102 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 1103 | else |
| 1104 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 1105 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1106 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1107 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1108 | normal_return: |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1109 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1110 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 1111 | (when we are able to write or when there's something to read) */ |
| 1112 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1115 | static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1116 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1117 | Py_buffer buf; |
| 1118 | int len; |
| 1119 | int sockstate; |
| 1120 | int err; |
| 1121 | int nonblocking; |
| 1122 | PySocketSockObject *sock |
| 1123 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1125 | if (((PyObject*)sock) == Py_None) { |
| 1126 | _setSSLError("Underlying socket connection gone", |
| 1127 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1128 | return NULL; |
| 1129 | } |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1130 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1132 | if (!PyArg_ParseTuple(args, "y*:write", &buf)) { |
| 1133 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1134 | return NULL; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1135 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* just in case the blocking state of the socket has been changed */ |
| 1138 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1139 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1140 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1141 | |
| 1142 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1143 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1144 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1145 | "The write operation timed out"); |
| 1146 | goto error; |
| 1147 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1148 | PyErr_SetString(PySSLErrorObject, |
| 1149 | "Underlying socket has been closed."); |
| 1150 | goto error; |
| 1151 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1152 | PyErr_SetString(PySSLErrorObject, |
| 1153 | "Underlying socket too large for select()."); |
| 1154 | goto error; |
| 1155 | } |
| 1156 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1157 | PySSL_BEGIN_ALLOW_THREADS |
| 1158 | len = SSL_write(self->ssl, buf.buf, buf.len); |
| 1159 | err = SSL_get_error(self->ssl, len); |
| 1160 | PySSL_END_ALLOW_THREADS |
| 1161 | if (PyErr_CheckSignals()) { |
| 1162 | goto error; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1163 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1164 | if (err == SSL_ERROR_WANT_READ) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1165 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1166 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1167 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1168 | } else { |
| 1169 | sockstate = SOCKET_OPERATION_OK; |
| 1170 | } |
| 1171 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1172 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1173 | "The write operation timed out"); |
| 1174 | goto error; |
| 1175 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1176 | PyErr_SetString(PySSLErrorObject, |
| 1177 | "Underlying socket has been closed."); |
| 1178 | goto error; |
| 1179 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1180 | break; |
| 1181 | } |
| 1182 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1184 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1185 | PyBuffer_Release(&buf); |
| 1186 | if (len > 0) |
| 1187 | return PyLong_FromLong(len); |
| 1188 | else |
| 1189 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 1190 | |
| 1191 | error: |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1192 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1193 | PyBuffer_Release(&buf); |
| 1194 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1197 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1198 | "write(s) -> len\n\ |
| 1199 | \n\ |
| 1200 | 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] | 1201 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1202 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1203 | static PyObject *PySSL_SSLpending(PySSLSocket *self) |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1204 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1205 | int count = 0; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1207 | PySSL_BEGIN_ALLOW_THREADS |
| 1208 | count = SSL_pending(self->ssl); |
| 1209 | PySSL_END_ALLOW_THREADS |
| 1210 | if (count < 0) |
| 1211 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1212 | else |
| 1213 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | PyDoc_STRVAR(PySSL_SSLpending_doc, |
| 1217 | "pending() -> count\n\ |
| 1218 | \n\ |
| 1219 | Returns the number of already decrypted bytes available for read,\n\ |
| 1220 | pending on the connection.\n"); |
| 1221 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1222 | static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1223 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1224 | PyObject *dest = NULL; |
| 1225 | Py_buffer buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1226 | char *mem; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1227 | int len, count; |
| 1228 | int buf_passed = 0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1229 | int sockstate; |
| 1230 | int err; |
| 1231 | int nonblocking; |
| 1232 | PySocketSockObject *sock |
| 1233 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1235 | if (((PyObject*)sock) == Py_None) { |
| 1236 | _setSSLError("Underlying socket connection gone", |
| 1237 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1238 | return NULL; |
| 1239 | } |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1240 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1242 | buf.obj = NULL; |
| 1243 | buf.buf = NULL; |
| 1244 | if (!PyArg_ParseTuple(args, "i|w*:read", &len, &buf)) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1245 | goto error; |
| 1246 | |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1247 | if ((buf.buf == NULL) && (buf.obj == NULL)) { |
| 1248 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 1249 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1250 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1251 | mem = PyBytes_AS_STRING(dest); |
| 1252 | } |
| 1253 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1254 | buf_passed = 1; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1255 | mem = buf.buf; |
| 1256 | if (len <= 0 || len > buf.len) { |
| 1257 | len = (int) buf.len; |
| 1258 | if (buf.len != len) { |
| 1259 | PyErr_SetString(PyExc_OverflowError, |
| 1260 | "maximum length can't fit in a C 'int'"); |
| 1261 | goto error; |
| 1262 | } |
| 1263 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /* just in case the blocking state of the socket has been changed */ |
| 1267 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1268 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1269 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1270 | |
| 1271 | /* first check if there are bytes ready to be read */ |
| 1272 | PySSL_BEGIN_ALLOW_THREADS |
| 1273 | count = SSL_pending(self->ssl); |
| 1274 | PySSL_END_ALLOW_THREADS |
| 1275 | |
| 1276 | if (!count) { |
| 1277 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1278 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1279 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1280 | "The read operation timed out"); |
| 1281 | goto error; |
| 1282 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1283 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1284 | "Underlying socket too large for select()."); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1285 | goto error; |
| 1286 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1287 | count = 0; |
| 1288 | goto done; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1289 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1290 | } |
| 1291 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1292 | PySSL_BEGIN_ALLOW_THREADS |
| 1293 | count = SSL_read(self->ssl, mem, len); |
| 1294 | err = SSL_get_error(self->ssl, count); |
| 1295 | PySSL_END_ALLOW_THREADS |
| 1296 | if (PyErr_CheckSignals()) |
| 1297 | goto error; |
| 1298 | if (err == SSL_ERROR_WANT_READ) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1299 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1300 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1301 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1302 | } else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 1303 | (SSL_get_shutdown(self->ssl) == |
| 1304 | SSL_RECEIVED_SHUTDOWN)) |
| 1305 | { |
| 1306 | count = 0; |
| 1307 | goto done; |
| 1308 | } else { |
| 1309 | sockstate = SOCKET_OPERATION_OK; |
| 1310 | } |
| 1311 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1312 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1313 | "The read operation timed out"); |
| 1314 | goto error; |
| 1315 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1316 | break; |
| 1317 | } |
| 1318 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 1319 | if (count <= 0) { |
| 1320 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1321 | goto error; |
| 1322 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1323 | |
| 1324 | done: |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1325 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1326 | if (!buf_passed) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1327 | _PyBytes_Resize(&dest, count); |
| 1328 | return dest; |
| 1329 | } |
| 1330 | else { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1331 | PyBuffer_Release(&buf); |
| 1332 | return PyLong_FromLong(count); |
| 1333 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1334 | |
| 1335 | error: |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1336 | Py_DECREF(sock); |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1337 | if (!buf_passed) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1338 | Py_XDECREF(dest); |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 1339 | else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1340 | PyBuffer_Release(&buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1341 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1344 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1345 | "read([len]) -> string\n\ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1346 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1347 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1349 | static PyObject *PySSL_SSLshutdown(PySSLSocket *self) |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1350 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1351 | int err, ssl_err, sockstate, nonblocking; |
| 1352 | int zeros = 0; |
| 1353 | PySocketSockObject *sock |
| 1354 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1356 | /* Guard against closed socket */ |
| 1357 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) { |
| 1358 | _setSSLError("Underlying socket connection gone", |
| 1359 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1360 | return NULL; |
| 1361 | } |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1362 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* Just in case the blocking state of the socket has been changed */ |
| 1365 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1366 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1367 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1368 | |
| 1369 | while (1) { |
| 1370 | PySSL_BEGIN_ALLOW_THREADS |
| 1371 | /* Disable read-ahead so that unwrap can work correctly. |
| 1372 | * Otherwise OpenSSL might read in too much data, |
| 1373 | * eating clear text data that happens to be |
| 1374 | * transmitted after the SSL shutdown. |
| 1375 | * Should be safe to call repeatedly everytime this |
| 1376 | * function is used and the shutdown_seen_zero != 0 |
| 1377 | * condition is met. |
| 1378 | */ |
| 1379 | if (self->shutdown_seen_zero) |
| 1380 | SSL_set_read_ahead(self->ssl, 0); |
| 1381 | err = SSL_shutdown(self->ssl); |
| 1382 | PySSL_END_ALLOW_THREADS |
| 1383 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
| 1384 | if (err > 0) |
| 1385 | break; |
| 1386 | if (err == 0) { |
| 1387 | /* Don't loop endlessly; instead preserve legacy |
| 1388 | behaviour of trying SSL_shutdown() only twice. |
| 1389 | This looks necessary for OpenSSL < 0.9.8m */ |
| 1390 | if (++zeros > 1) |
| 1391 | break; |
| 1392 | /* Shutdown was sent, now try receiving */ |
| 1393 | self->shutdown_seen_zero = 1; |
| 1394 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1397 | /* Possibly retry shutdown until timeout or failure */ |
| 1398 | ssl_err = SSL_get_error(self->ssl, err); |
| 1399 | if (ssl_err == SSL_ERROR_WANT_READ) |
| 1400 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1401 | else if (ssl_err == SSL_ERROR_WANT_WRITE) |
| 1402 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1403 | else |
| 1404 | break; |
| 1405 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1406 | if (ssl_err == SSL_ERROR_WANT_READ) |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1407 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1408 | "The read operation timed out"); |
| 1409 | else |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1410 | PyErr_SetString(PySocketModule.timeout_error, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1411 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1412 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1413 | } |
| 1414 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1415 | PyErr_SetString(PySSLErrorObject, |
| 1416 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1417 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1418 | } |
| 1419 | else if (sockstate != SOCKET_OPERATION_OK) |
| 1420 | /* Retain the SSL error code */ |
| 1421 | break; |
| 1422 | } |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1424 | if (err < 0) { |
| 1425 | Py_DECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1426 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1427 | } |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 1428 | else |
| 1429 | /* It's already INCREF'ed */ |
| 1430 | return (PyObject *) sock; |
| 1431 | |
| 1432 | error: |
| 1433 | Py_DECREF(sock); |
| 1434 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 1438 | "shutdown(s) -> socket\n\ |
| 1439 | \n\ |
| 1440 | Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 1441 | the underlying socket object."); |
| 1442 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1443 | #if HAVE_OPENSSL_FINISHED |
| 1444 | static PyObject * |
| 1445 | PySSL_tls_unique_cb(PySSLSocket *self) |
| 1446 | { |
| 1447 | PyObject *retval = NULL; |
| 1448 | char buf[PySSL_CB_MAXLEN]; |
| 1449 | int len; |
| 1450 | |
| 1451 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 1452 | /* if session is resumed XOR we are the client */ |
| 1453 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 1454 | } |
| 1455 | else { |
| 1456 | /* if a new session XOR we are the server */ |
| 1457 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 1458 | } |
| 1459 | |
| 1460 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
| 1461 | assert(len >= 0); |
| 1462 | if (len == 0) |
| 1463 | Py_RETURN_NONE; |
| 1464 | |
| 1465 | retval = PyBytes_FromStringAndSize(buf, len); |
| 1466 | |
| 1467 | return retval; |
| 1468 | } |
| 1469 | |
| 1470 | PyDoc_STRVAR(PySSL_tls_unique_cb_doc, |
| 1471 | "tls_unique_cb() -> bytes\n\ |
| 1472 | \n\ |
| 1473 | Returns the 'tls-unique' channel binding data, as defined by RFC 5929.\n\ |
| 1474 | \n\ |
| 1475 | If the TLS handshake is not yet complete, None is returned"); |
| 1476 | |
| 1477 | #endif /* HAVE_OPENSSL_FINISHED */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1478 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1479 | static PyMethodDef PySSLMethods[] = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1480 | {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS}, |
| 1481 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
| 1482 | PySSL_SSLwrite_doc}, |
| 1483 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
| 1484 | PySSL_SSLread_doc}, |
| 1485 | {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS, |
| 1486 | PySSL_SSLpending_doc}, |
| 1487 | {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS, |
| 1488 | PySSL_peercert_doc}, |
| 1489 | {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS}, |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1490 | {"compression", (PyCFunction)PySSL_compression, METH_NOARGS}, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1491 | {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 1492 | PySSL_SSLshutdown_doc}, |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 1493 | #if HAVE_OPENSSL_FINISHED |
| 1494 | {"tls_unique_cb", (PyCFunction)PySSL_tls_unique_cb, METH_NOARGS, |
| 1495 | PySSL_tls_unique_cb_doc}, |
| 1496 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1497 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1498 | }; |
| 1499 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1500 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1501 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1502 | "_ssl._SSLSocket", /*tp_name*/ |
| 1503 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1504 | 0, /*tp_itemsize*/ |
| 1505 | /* methods */ |
| 1506 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 1507 | 0, /*tp_print*/ |
| 1508 | 0, /*tp_getattr*/ |
| 1509 | 0, /*tp_setattr*/ |
| 1510 | 0, /*tp_reserved*/ |
| 1511 | 0, /*tp_repr*/ |
| 1512 | 0, /*tp_as_number*/ |
| 1513 | 0, /*tp_as_sequence*/ |
| 1514 | 0, /*tp_as_mapping*/ |
| 1515 | 0, /*tp_hash*/ |
| 1516 | 0, /*tp_call*/ |
| 1517 | 0, /*tp_str*/ |
| 1518 | 0, /*tp_getattro*/ |
| 1519 | 0, /*tp_setattro*/ |
| 1520 | 0, /*tp_as_buffer*/ |
| 1521 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 1522 | 0, /*tp_doc*/ |
| 1523 | 0, /*tp_traverse*/ |
| 1524 | 0, /*tp_clear*/ |
| 1525 | 0, /*tp_richcompare*/ |
| 1526 | 0, /*tp_weaklistoffset*/ |
| 1527 | 0, /*tp_iter*/ |
| 1528 | 0, /*tp_iternext*/ |
| 1529 | PySSLMethods, /*tp_methods*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1530 | }; |
| 1531 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* |
| 1534 | * _SSLContext objects |
| 1535 | */ |
| 1536 | |
| 1537 | static PyObject * |
| 1538 | context_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1539 | { |
| 1540 | char *kwlist[] = {"protocol", NULL}; |
| 1541 | PySSLContext *self; |
| 1542 | int proto_version = PY_SSL_VERSION_SSL23; |
| 1543 | SSL_CTX *ctx = NULL; |
| 1544 | |
| 1545 | if (!PyArg_ParseTupleAndKeywords( |
| 1546 | args, kwds, "i:_SSLContext", kwlist, |
| 1547 | &proto_version)) |
| 1548 | return NULL; |
| 1549 | |
| 1550 | PySSL_BEGIN_ALLOW_THREADS |
| 1551 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 1552 | ctx = SSL_CTX_new(TLSv1_method()); |
| 1553 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 1554 | ctx = SSL_CTX_new(SSLv3_method()); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 1555 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1556 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 1557 | ctx = SSL_CTX_new(SSLv2_method()); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 1558 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1559 | else if (proto_version == PY_SSL_VERSION_SSL23) |
| 1560 | ctx = SSL_CTX_new(SSLv23_method()); |
| 1561 | else |
| 1562 | proto_version = -1; |
| 1563 | PySSL_END_ALLOW_THREADS |
| 1564 | |
| 1565 | if (proto_version == -1) { |
| 1566 | PyErr_SetString(PyExc_ValueError, |
| 1567 | "invalid protocol version"); |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | if (ctx == NULL) { |
| 1571 | PyErr_SetString(PySSLErrorObject, |
| 1572 | "failed to allocate SSL context"); |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | |
| 1576 | assert(type != NULL && type->tp_alloc != NULL); |
| 1577 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 1578 | if (self == NULL) { |
| 1579 | SSL_CTX_free(ctx); |
| 1580 | return NULL; |
| 1581 | } |
| 1582 | self->ctx = ctx; |
| 1583 | /* Defaults */ |
| 1584 | SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL); |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 1585 | SSL_CTX_set_options(self->ctx, |
| 1586 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 1588 | #define SID_CTX "Python" |
| 1589 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 1590 | sizeof(SID_CTX)); |
| 1591 | #undef SID_CTX |
| 1592 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1593 | return (PyObject *)self; |
| 1594 | } |
| 1595 | |
| 1596 | static void |
| 1597 | context_dealloc(PySSLContext *self) |
| 1598 | { |
| 1599 | SSL_CTX_free(self->ctx); |
| 1600 | Py_TYPE(self)->tp_free(self); |
| 1601 | } |
| 1602 | |
| 1603 | static PyObject * |
| 1604 | set_ciphers(PySSLContext *self, PyObject *args) |
| 1605 | { |
| 1606 | int ret; |
| 1607 | const char *cipherlist; |
| 1608 | |
| 1609 | if (!PyArg_ParseTuple(args, "s:set_ciphers", &cipherlist)) |
| 1610 | return NULL; |
| 1611 | ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
| 1612 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 1613 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 1614 | otherwise the error will be reported again when another SSL call |
| 1615 | is done. */ |
| 1616 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1617 | PyErr_SetString(PySSLErrorObject, |
| 1618 | "No cipher can be selected."); |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | Py_RETURN_NONE; |
| 1622 | } |
| 1623 | |
| 1624 | static PyObject * |
| 1625 | get_verify_mode(PySSLContext *self, void *c) |
| 1626 | { |
| 1627 | switch (SSL_CTX_get_verify_mode(self->ctx)) { |
| 1628 | case SSL_VERIFY_NONE: |
| 1629 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 1630 | case SSL_VERIFY_PEER: |
| 1631 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 1632 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 1633 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 1634 | } |
| 1635 | PyErr_SetString(PySSLErrorObject, |
| 1636 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 1637 | return NULL; |
| 1638 | } |
| 1639 | |
| 1640 | static int |
| 1641 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 1642 | { |
| 1643 | int n, mode; |
| 1644 | if (!PyArg_Parse(arg, "i", &n)) |
| 1645 | return -1; |
| 1646 | if (n == PY_SSL_CERT_NONE) |
| 1647 | mode = SSL_VERIFY_NONE; |
| 1648 | else if (n == PY_SSL_CERT_OPTIONAL) |
| 1649 | mode = SSL_VERIFY_PEER; |
| 1650 | else if (n == PY_SSL_CERT_REQUIRED) |
| 1651 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1652 | else { |
| 1653 | PyErr_SetString(PyExc_ValueError, |
| 1654 | "invalid value for verify_mode"); |
| 1655 | return -1; |
| 1656 | } |
| 1657 | SSL_CTX_set_verify(self->ctx, mode, NULL); |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1662 | get_options(PySSLContext *self, void *c) |
| 1663 | { |
| 1664 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 1665 | } |
| 1666 | |
| 1667 | static int |
| 1668 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 1669 | { |
| 1670 | long new_opts, opts, set, clear; |
| 1671 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 1672 | return -1; |
| 1673 | opts = SSL_CTX_get_options(self->ctx); |
| 1674 | clear = opts & ~new_opts; |
| 1675 | set = ~opts & new_opts; |
| 1676 | if (clear) { |
| 1677 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 1678 | SSL_CTX_clear_options(self->ctx, clear); |
| 1679 | #else |
| 1680 | PyErr_SetString(PyExc_ValueError, |
| 1681 | "can't clear options before OpenSSL 0.9.8m"); |
| 1682 | return -1; |
| 1683 | #endif |
| 1684 | } |
| 1685 | if (set) |
| 1686 | SSL_CTX_set_options(self->ctx, set); |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1690 | typedef struct { |
| 1691 | PyThreadState *thread_state; |
| 1692 | PyObject *callable; |
| 1693 | char *password; |
| 1694 | Py_ssize_t size; |
| 1695 | int error; |
| 1696 | } _PySSLPasswordInfo; |
| 1697 | |
| 1698 | static int |
| 1699 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 1700 | const char *bad_type_error) |
| 1701 | { |
| 1702 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 1703 | from a unicode, bytes, or byte array object. |
| 1704 | The password field will be dynamically allocated and must be freed |
| 1705 | by the caller */ |
| 1706 | PyObject *password_bytes = NULL; |
| 1707 | const char *data = NULL; |
| 1708 | Py_ssize_t size; |
| 1709 | |
| 1710 | if (PyUnicode_Check(password)) { |
| 1711 | password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL); |
| 1712 | if (!password_bytes) { |
| 1713 | goto error; |
| 1714 | } |
| 1715 | data = PyBytes_AS_STRING(password_bytes); |
| 1716 | size = PyBytes_GET_SIZE(password_bytes); |
| 1717 | } else if (PyBytes_Check(password)) { |
| 1718 | data = PyBytes_AS_STRING(password); |
| 1719 | size = PyBytes_GET_SIZE(password); |
| 1720 | } else if (PyByteArray_Check(password)) { |
| 1721 | data = PyByteArray_AS_STRING(password); |
| 1722 | size = PyByteArray_GET_SIZE(password); |
| 1723 | } else { |
| 1724 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 1725 | goto error; |
| 1726 | } |
| 1727 | |
| 1728 | free(pw_info->password); |
| 1729 | pw_info->password = malloc(size); |
| 1730 | if (!pw_info->password) { |
| 1731 | PyErr_SetString(PyExc_MemoryError, |
| 1732 | "unable to allocate password buffer"); |
| 1733 | goto error; |
| 1734 | } |
| 1735 | memcpy(pw_info->password, data, size); |
| 1736 | pw_info->size = size; |
| 1737 | |
| 1738 | Py_XDECREF(password_bytes); |
| 1739 | return 1; |
| 1740 | |
| 1741 | error: |
| 1742 | Py_XDECREF(password_bytes); |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static int |
| 1747 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 1748 | { |
| 1749 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 1750 | PyObject *fn_ret = NULL; |
| 1751 | |
| 1752 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 1753 | |
| 1754 | if (pw_info->callable) { |
| 1755 | fn_ret = PyObject_CallFunctionObjArgs(pw_info->callable, NULL); |
| 1756 | if (!fn_ret) { |
| 1757 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 1758 | core python API, so we could use it to add a frame here */ |
| 1759 | goto error; |
| 1760 | } |
| 1761 | |
| 1762 | if (!_pwinfo_set(pw_info, fn_ret, |
| 1763 | "password callback must return a string")) { |
| 1764 | goto error; |
| 1765 | } |
| 1766 | Py_CLEAR(fn_ret); |
| 1767 | } |
| 1768 | |
| 1769 | if (pw_info->size > size) { |
| 1770 | PyErr_Format(PyExc_ValueError, |
| 1771 | "password cannot be longer than %d bytes", size); |
| 1772 | goto error; |
| 1773 | } |
| 1774 | |
| 1775 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 1776 | memcpy(buf, pw_info->password, pw_info->size); |
| 1777 | return pw_info->size; |
| 1778 | |
| 1779 | error: |
| 1780 | Py_XDECREF(fn_ret); |
| 1781 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 1782 | pw_info->error = 1; |
| 1783 | return -1; |
| 1784 | } |
| 1785 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1786 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1787 | load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1788 | { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1789 | char *kwlist[] = {"certfile", "keyfile", "password", NULL}; |
| 1790 | PyObject *certfile, *keyfile = NULL, *password = NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1791 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1792 | pem_password_cb *orig_passwd_cb = self->ctx->default_passwd_callback; |
| 1793 | void *orig_passwd_userdata = self->ctx->default_passwd_callback_userdata; |
| 1794 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1795 | int r; |
| 1796 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1797 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 1798 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1799 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1800 | "O|OO:load_cert_chain", kwlist, |
| 1801 | &certfile, &keyfile, &password)) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1802 | return NULL; |
| 1803 | if (keyfile == Py_None) |
| 1804 | keyfile = NULL; |
| 1805 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
| 1806 | PyErr_SetString(PyExc_TypeError, |
| 1807 | "certfile should be a valid filesystem path"); |
| 1808 | return NULL; |
| 1809 | } |
| 1810 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
| 1811 | PyErr_SetString(PyExc_TypeError, |
| 1812 | "keyfile should be a valid filesystem path"); |
| 1813 | goto error; |
| 1814 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1815 | if (password && password != Py_None) { |
| 1816 | if (PyCallable_Check(password)) { |
| 1817 | pw_info.callable = password; |
| 1818 | } else if (!_pwinfo_set(&pw_info, password, |
| 1819 | "password should be a string or callable")) { |
| 1820 | goto error; |
| 1821 | } |
| 1822 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 1823 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 1824 | } |
| 1825 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1826 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 1827 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1828 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1829 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1830 | if (pw_info.error) { |
| 1831 | ERR_clear_error(); |
| 1832 | /* the password callback has already set the error information */ |
| 1833 | } |
| 1834 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 1835 | ERR_clear_error(); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1836 | PyErr_SetFromErrno(PyExc_IOError); |
| 1837 | } |
| 1838 | else { |
| 1839 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1840 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1841 | goto error; |
| 1842 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1843 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 1844 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1845 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 1846 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1847 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 1848 | Py_CLEAR(keyfile_bytes); |
| 1849 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1850 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1851 | if (pw_info.error) { |
| 1852 | ERR_clear_error(); |
| 1853 | /* the password callback has already set the error information */ |
| 1854 | } |
| 1855 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 1856 | ERR_clear_error(); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1857 | PyErr_SetFromErrno(PyExc_IOError); |
| 1858 | } |
| 1859 | else { |
| 1860 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1861 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1862 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1863 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1864 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1865 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1866 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1867 | if (r != 1) { |
| 1868 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1869 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1870 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1871 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 1872 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| 1873 | free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1874 | Py_RETURN_NONE; |
| 1875 | |
| 1876 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 1877 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 1878 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| 1879 | free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1880 | Py_XDECREF(keyfile_bytes); |
| 1881 | Py_XDECREF(certfile_bytes); |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | static PyObject * |
| 1886 | load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1887 | { |
| 1888 | char *kwlist[] = {"cafile", "capath", NULL}; |
| 1889 | PyObject *cafile = NULL, *capath = NULL; |
| 1890 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 1891 | const char *cafile_buf = NULL, *capath_buf = NULL; |
| 1892 | int r; |
| 1893 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1894 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1895 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1896 | "|OO:load_verify_locations", kwlist, |
| 1897 | &cafile, &capath)) |
| 1898 | return NULL; |
| 1899 | if (cafile == Py_None) |
| 1900 | cafile = NULL; |
| 1901 | if (capath == Py_None) |
| 1902 | capath = NULL; |
| 1903 | if (cafile == NULL && capath == NULL) { |
| 1904 | PyErr_SetString(PyExc_TypeError, |
| 1905 | "cafile and capath cannot be both omitted"); |
| 1906 | return NULL; |
| 1907 | } |
| 1908 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
| 1909 | PyErr_SetString(PyExc_TypeError, |
| 1910 | "cafile should be a valid filesystem path"); |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Victor Stinner | 80f75e6 | 2011-01-29 11:31:20 +0000 | [diff] [blame] | 1914 | Py_XDECREF(cafile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1915 | PyErr_SetString(PyExc_TypeError, |
| 1916 | "capath should be a valid filesystem path"); |
| 1917 | return NULL; |
| 1918 | } |
| 1919 | if (cafile) |
| 1920 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 1921 | if (capath) |
| 1922 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 1923 | PySSL_BEGIN_ALLOW_THREADS |
| 1924 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 1925 | PySSL_END_ALLOW_THREADS |
| 1926 | Py_XDECREF(cafile_bytes); |
| 1927 | Py_XDECREF(capath_bytes); |
| 1928 | if (r != 1) { |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1929 | if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 1930 | ERR_clear_error(); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 1931 | PyErr_SetFromErrno(PyExc_IOError); |
| 1932 | } |
| 1933 | else { |
| 1934 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1935 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1936 | return NULL; |
| 1937 | } |
| 1938 | Py_RETURN_NONE; |
| 1939 | } |
| 1940 | |
| 1941 | static PyObject * |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1942 | load_dh_params(PySSLContext *self, PyObject *filepath) |
| 1943 | { |
| 1944 | FILE *f; |
| 1945 | DH *dh; |
| 1946 | |
| 1947 | f = _Py_fopen(filepath, "rb"); |
| 1948 | if (f == NULL) { |
| 1949 | if (!PyErr_Occurred()) |
| 1950 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 1951 | return NULL; |
| 1952 | } |
| 1953 | errno = 0; |
| 1954 | PySSL_BEGIN_ALLOW_THREADS |
| 1955 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
| 1956 | PySSL_END_ALLOW_THREADS |
| 1957 | if (dh == NULL) { |
| 1958 | if (errno != 0) { |
| 1959 | ERR_clear_error(); |
| 1960 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 1961 | } |
| 1962 | else { |
| 1963 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1964 | } |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 1968 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1969 | DH_free(dh); |
| 1970 | Py_RETURN_NONE; |
| 1971 | } |
| 1972 | |
| 1973 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1974 | context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1975 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1976 | char *kwlist[] = {"sock", "server_side", "server_hostname", NULL}; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1977 | PySocketSockObject *sock; |
| 1978 | int server_side = 0; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1979 | char *hostname = NULL; |
| 1980 | PyObject *hostname_obj, *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1982 | /* server_hostname is either None (or absent), or to be encoded |
| 1983 | using the idna encoding. */ |
| 1984 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!i|O!:_wrap_socket", kwlist, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1985 | PySocketModule.Sock_Type, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1986 | &sock, &server_side, |
| 1987 | Py_TYPE(Py_None), &hostname_obj)) { |
| 1988 | PyErr_Clear(); |
| 1989 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!iet:_wrap_socket", kwlist, |
| 1990 | PySocketModule.Sock_Type, |
| 1991 | &sock, &server_side, |
| 1992 | "idna", &hostname)) |
| 1993 | return NULL; |
| 1994 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1995 | PyMem_Free(hostname); |
| 1996 | PyErr_SetString(PyExc_ValueError, "server_hostname is not supported " |
| 1997 | "by your OpenSSL library"); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1998 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1999 | #endif |
| 2000 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 2002 | res = (PyObject *) newPySSLSocket(self->ctx, sock, server_side, |
| 2003 | hostname); |
| 2004 | if (hostname != NULL) |
| 2005 | PyMem_Free(hostname); |
| 2006 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 2009 | static PyObject * |
| 2010 | session_stats(PySSLContext *self, PyObject *unused) |
| 2011 | { |
| 2012 | int r; |
| 2013 | PyObject *value, *stats = PyDict_New(); |
| 2014 | if (!stats) |
| 2015 | return NULL; |
| 2016 | |
| 2017 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 2018 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 2019 | if (value == NULL) \ |
| 2020 | goto error; \ |
| 2021 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 2022 | Py_DECREF(value); \ |
| 2023 | if (r < 0) \ |
| 2024 | goto error; |
| 2025 | |
| 2026 | ADD_STATS(number, "number"); |
| 2027 | ADD_STATS(connect, "connect"); |
| 2028 | ADD_STATS(connect_good, "connect_good"); |
| 2029 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 2030 | ADD_STATS(accept, "accept"); |
| 2031 | ADD_STATS(accept_good, "accept_good"); |
| 2032 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 2033 | ADD_STATS(accept, "accept"); |
| 2034 | ADD_STATS(hits, "hits"); |
| 2035 | ADD_STATS(misses, "misses"); |
| 2036 | ADD_STATS(timeouts, "timeouts"); |
| 2037 | ADD_STATS(cache_full, "cache_full"); |
| 2038 | |
| 2039 | #undef ADD_STATS |
| 2040 | |
| 2041 | return stats; |
| 2042 | |
| 2043 | error: |
| 2044 | Py_DECREF(stats); |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 2048 | static PyObject * |
| 2049 | set_default_verify_paths(PySSLContext *self, PyObject *unused) |
| 2050 | { |
| 2051 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 2052 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | Py_RETURN_NONE; |
| 2056 | } |
| 2057 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 2058 | #ifndef OPENSSL_NO_ECDH |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 2059 | static PyObject * |
| 2060 | set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 2061 | { |
| 2062 | PyObject *name_bytes; |
| 2063 | int nid; |
| 2064 | EC_KEY *key; |
| 2065 | |
| 2066 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 2067 | return NULL; |
| 2068 | assert(PyBytes_Check(name_bytes)); |
| 2069 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 2070 | Py_DECREF(name_bytes); |
| 2071 | if (nid == 0) { |
| 2072 | PyErr_Format(PyExc_ValueError, |
| 2073 | "unknown elliptic curve name %R", name); |
| 2074 | return NULL; |
| 2075 | } |
| 2076 | key = EC_KEY_new_by_curve_name(nid); |
| 2077 | if (key == NULL) { |
| 2078 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2079 | return NULL; |
| 2080 | } |
| 2081 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 2082 | EC_KEY_free(key); |
| 2083 | Py_RETURN_NONE; |
| 2084 | } |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 2085 | #endif |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 2086 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2087 | static PyGetSetDef context_getsetlist[] = { |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 2088 | {"options", (getter) get_options, |
| 2089 | (setter) set_options, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2090 | {"verify_mode", (getter) get_verify_mode, |
| 2091 | (setter) set_verify_mode, NULL}, |
| 2092 | {NULL}, /* sentinel */ |
| 2093 | }; |
| 2094 | |
| 2095 | static struct PyMethodDef context_methods[] = { |
| 2096 | {"_wrap_socket", (PyCFunction) context_wrap_socket, |
| 2097 | METH_VARARGS | METH_KEYWORDS, NULL}, |
| 2098 | {"set_ciphers", (PyCFunction) set_ciphers, |
| 2099 | METH_VARARGS, NULL}, |
| 2100 | {"load_cert_chain", (PyCFunction) load_cert_chain, |
| 2101 | METH_VARARGS | METH_KEYWORDS, NULL}, |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 2102 | {"load_dh_params", (PyCFunction) load_dh_params, |
| 2103 | METH_O, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2104 | {"load_verify_locations", (PyCFunction) load_verify_locations, |
| 2105 | METH_VARARGS | METH_KEYWORDS, NULL}, |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 2106 | {"session_stats", (PyCFunction) session_stats, |
| 2107 | METH_NOARGS, NULL}, |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 2108 | {"set_default_verify_paths", (PyCFunction) set_default_verify_paths, |
| 2109 | METH_NOARGS, NULL}, |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 2110 | #ifndef OPENSSL_NO_ECDH |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 2111 | {"set_ecdh_curve", (PyCFunction) set_ecdh_curve, |
| 2112 | METH_O, NULL}, |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 2113 | #endif |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2114 | {NULL, NULL} /* sentinel */ |
| 2115 | }; |
| 2116 | |
| 2117 | static PyTypeObject PySSLContext_Type = { |
| 2118 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2119 | "_ssl._SSLContext", /*tp_name*/ |
| 2120 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 2121 | 0, /*tp_itemsize*/ |
| 2122 | (destructor)context_dealloc, /*tp_dealloc*/ |
| 2123 | 0, /*tp_print*/ |
| 2124 | 0, /*tp_getattr*/ |
| 2125 | 0, /*tp_setattr*/ |
| 2126 | 0, /*tp_reserved*/ |
| 2127 | 0, /*tp_repr*/ |
| 2128 | 0, /*tp_as_number*/ |
| 2129 | 0, /*tp_as_sequence*/ |
| 2130 | 0, /*tp_as_mapping*/ |
| 2131 | 0, /*tp_hash*/ |
| 2132 | 0, /*tp_call*/ |
| 2133 | 0, /*tp_str*/ |
| 2134 | 0, /*tp_getattro*/ |
| 2135 | 0, /*tp_setattro*/ |
| 2136 | 0, /*tp_as_buffer*/ |
| 2137 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 2138 | 0, /*tp_doc*/ |
| 2139 | 0, /*tp_traverse*/ |
| 2140 | 0, /*tp_clear*/ |
| 2141 | 0, /*tp_richcompare*/ |
| 2142 | 0, /*tp_weaklistoffset*/ |
| 2143 | 0, /*tp_iter*/ |
| 2144 | 0, /*tp_iternext*/ |
| 2145 | context_methods, /*tp_methods*/ |
| 2146 | 0, /*tp_members*/ |
| 2147 | context_getsetlist, /*tp_getset*/ |
| 2148 | 0, /*tp_base*/ |
| 2149 | 0, /*tp_dict*/ |
| 2150 | 0, /*tp_descr_get*/ |
| 2151 | 0, /*tp_descr_set*/ |
| 2152 | 0, /*tp_dictoffset*/ |
| 2153 | 0, /*tp_init*/ |
| 2154 | 0, /*tp_alloc*/ |
| 2155 | context_new, /*tp_new*/ |
| 2156 | }; |
| 2157 | |
| 2158 | |
| 2159 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2160 | #ifdef HAVE_OPENSSL_RAND |
| 2161 | |
| 2162 | /* helper routines for seeding the SSL PRNG */ |
| 2163 | static PyObject * |
| 2164 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 2165 | { |
| 2166 | char *buf; |
| 2167 | int len; |
| 2168 | double entropy; |
| 2169 | |
| 2170 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 2171 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2172 | RAND_add(buf, len, entropy); |
| 2173 | Py_INCREF(Py_None); |
| 2174 | return Py_None; |
| 2175 | } |
| 2176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2177 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2178 | "RAND_add(string, entropy)\n\ |
| 2179 | \n\ |
| 2180 | Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2181 | bound on the entropy contained in string. See RFC 1750."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2182 | |
| 2183 | static PyObject * |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 2184 | PySSL_RAND(int len, int pseudo) |
| 2185 | { |
| 2186 | int ok; |
| 2187 | PyObject *bytes; |
| 2188 | unsigned long err; |
| 2189 | const char *errstr; |
| 2190 | PyObject *v; |
| 2191 | |
| 2192 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 2193 | if (bytes == NULL) |
| 2194 | return NULL; |
| 2195 | if (pseudo) { |
| 2196 | ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 2197 | if (ok == 0 || ok == 1) |
| 2198 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 2199 | } |
| 2200 | else { |
| 2201 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 2202 | if (ok == 1) |
| 2203 | return bytes; |
| 2204 | } |
| 2205 | Py_DECREF(bytes); |
| 2206 | |
| 2207 | err = ERR_get_error(); |
| 2208 | errstr = ERR_reason_error_string(err); |
| 2209 | v = Py_BuildValue("(ks)", err, errstr); |
| 2210 | if (v != NULL) { |
| 2211 | PyErr_SetObject(PySSLErrorObject, v); |
| 2212 | Py_DECREF(v); |
| 2213 | } |
| 2214 | return NULL; |
| 2215 | } |
| 2216 | |
| 2217 | static PyObject * |
| 2218 | PySSL_RAND_bytes(PyObject *self, PyObject *args) |
| 2219 | { |
| 2220 | int len; |
| 2221 | if (!PyArg_ParseTuple(args, "i:RAND_bytes", &len)) |
| 2222 | return NULL; |
| 2223 | return PySSL_RAND(len, 0); |
| 2224 | } |
| 2225 | |
| 2226 | PyDoc_STRVAR(PySSL_RAND_bytes_doc, |
| 2227 | "RAND_bytes(n) -> bytes\n\ |
| 2228 | \n\ |
| 2229 | Generate n cryptographically strong pseudo-random bytes."); |
| 2230 | |
| 2231 | static PyObject * |
| 2232 | PySSL_RAND_pseudo_bytes(PyObject *self, PyObject *args) |
| 2233 | { |
| 2234 | int len; |
| 2235 | if (!PyArg_ParseTuple(args, "i:RAND_pseudo_bytes", &len)) |
| 2236 | return NULL; |
| 2237 | return PySSL_RAND(len, 1); |
| 2238 | } |
| 2239 | |
| 2240 | PyDoc_STRVAR(PySSL_RAND_pseudo_bytes_doc, |
| 2241 | "RAND_pseudo_bytes(n) -> (bytes, is_cryptographic)\n\ |
| 2242 | \n\ |
| 2243 | Generate n pseudo-random bytes. is_cryptographic is True if the bytes\ |
| 2244 | generated are cryptographically strong."); |
| 2245 | |
| 2246 | static PyObject * |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2247 | PySSL_RAND_status(PyObject *self) |
| 2248 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2249 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2252 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2253 | "RAND_status() -> 0 or 1\n\ |
| 2254 | \n\ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2255 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 2256 | It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
| 2257 | using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2258 | |
| 2259 | static PyObject * |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 2260 | PySSL_RAND_egd(PyObject *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2261 | { |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 2262 | PyObject *path; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2263 | int bytes; |
| 2264 | |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 2265 | if (!PyArg_ParseTuple(args, "O&|i:RAND_egd", |
| 2266 | PyUnicode_FSConverter, &path)) |
| 2267 | return NULL; |
| 2268 | |
| 2269 | bytes = RAND_egd(PyBytes_AsString(path)); |
| 2270 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2271 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 2272 | PyErr_SetString(PySSLErrorObject, |
| 2273 | "EGD connection failed or EGD did not return " |
| 2274 | "enough data to seed the PRNG"); |
| 2275 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2276 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2277 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2280 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2281 | "RAND_egd(path) -> bytes\n\ |
| 2282 | \n\ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2283 | Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\ |
| 2284 | Returns number of bytes read. Raises SSLError if connection to EGD\n\ |
| 2285 | fails or if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2286 | |
| 2287 | #endif |
| 2288 | |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2289 | |
| 2290 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2291 | /* List of functions exported by this module. */ |
| 2292 | |
| 2293 | static PyMethodDef PySSL_methods[] = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2294 | {"_test_decode_cert", PySSL_test_decode_certificate, |
| 2295 | METH_VARARGS}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2296 | #ifdef HAVE_OPENSSL_RAND |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2297 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
| 2298 | PySSL_RAND_add_doc}, |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 2299 | {"RAND_bytes", PySSL_RAND_bytes, METH_VARARGS, |
| 2300 | PySSL_RAND_bytes_doc}, |
| 2301 | {"RAND_pseudo_bytes", PySSL_RAND_pseudo_bytes, METH_VARARGS, |
| 2302 | PySSL_RAND_pseudo_bytes_doc}, |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 2303 | {"RAND_egd", PySSL_RAND_egd, METH_VARARGS, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2304 | PySSL_RAND_egd_doc}, |
| 2305 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 2306 | PySSL_RAND_status_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2307 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2308 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2309 | }; |
| 2310 | |
| 2311 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2312 | #ifdef WITH_THREAD |
| 2313 | |
| 2314 | /* an implementation of OpenSSL threading operations in terms |
| 2315 | of the Python C thread library */ |
| 2316 | |
| 2317 | static PyThread_type_lock *_ssl_locks = NULL; |
| 2318 | |
| 2319 | static unsigned long _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2320 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2323 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2324 | (int mode, int n, const char *file, int line) { |
| 2325 | /* this function is needed to perform locking on shared data |
| 2326 | structures. (Note that OpenSSL uses a number of global data |
| 2327 | structures that will be implicitly shared whenever multiple |
| 2328 | threads use OpenSSL.) Multi-threaded applications will |
| 2329 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2331 | locking_function() must be able to handle up to |
| 2332 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 2333 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2335 | file and line are the file number of the function setting the |
| 2336 | lock. They can be useful for debugging. |
| 2337 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2338 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2339 | if ((_ssl_locks == NULL) || |
| 2340 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 2341 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2342 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2343 | if (mode & CRYPTO_LOCK) { |
| 2344 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 2345 | } else { |
| 2346 | PyThread_release_lock(_ssl_locks[n]); |
| 2347 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | static int _setup_ssl_threads(void) { |
| 2351 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2352 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2354 | if (_ssl_locks == NULL) { |
| 2355 | _ssl_locks_count = CRYPTO_num_locks(); |
| 2356 | _ssl_locks = (PyThread_type_lock *) |
| 2357 | malloc(sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 2358 | if (_ssl_locks == NULL) |
| 2359 | return 0; |
| 2360 | memset(_ssl_locks, 0, |
| 2361 | sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 2362 | for (i = 0; i < _ssl_locks_count; i++) { |
| 2363 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 2364 | if (_ssl_locks[i] == NULL) { |
| 2365 | unsigned int j; |
| 2366 | for (j = 0; j < i; j++) { |
| 2367 | PyThread_free_lock(_ssl_locks[j]); |
| 2368 | } |
| 2369 | free(_ssl_locks); |
| 2370 | return 0; |
| 2371 | } |
| 2372 | } |
| 2373 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
| 2374 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
| 2375 | } |
| 2376 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2379 | #endif /* def HAVE_THREAD */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2381 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2382 | "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] | 2383 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2384 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2385 | |
| 2386 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2387 | PyModuleDef_HEAD_INIT, |
| 2388 | "_ssl", |
| 2389 | module_doc, |
| 2390 | -1, |
| 2391 | PySSL_methods, |
| 2392 | NULL, |
| 2393 | NULL, |
| 2394 | NULL, |
| 2395 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2396 | }; |
| 2397 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 2398 | |
| 2399 | static void |
| 2400 | parse_openssl_version(unsigned long libver, |
| 2401 | unsigned int *major, unsigned int *minor, |
| 2402 | unsigned int *fix, unsigned int *patch, |
| 2403 | unsigned int *status) |
| 2404 | { |
| 2405 | *status = libver & 0xF; |
| 2406 | libver >>= 4; |
| 2407 | *patch = libver & 0xFF; |
| 2408 | libver >>= 8; |
| 2409 | *fix = libver & 0xFF; |
| 2410 | libver >>= 8; |
| 2411 | *minor = libver & 0xFF; |
| 2412 | libver >>= 8; |
| 2413 | *major = libver & 0xFF; |
| 2414 | } |
| 2415 | |
Antoine Pitrou | a0e0e23 | 2011-10-22 23:41:52 +0200 | [diff] [blame] | 2416 | PyDoc_STRVAR(SSLError_doc, |
| 2417 | "An error occurred in the SSL implementation."); |
| 2418 | |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 2419 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 2420 | "SSL/TLS session closed cleanly."); |
| 2421 | |
| 2422 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 2423 | "Non-blocking SSL socket needs to read more data\n" |
| 2424 | "before the requested operation can be completed."); |
| 2425 | |
| 2426 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 2427 | "Non-blocking SSL socket needs to write more data\n" |
| 2428 | "before the requested operation can be completed."); |
| 2429 | |
| 2430 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 2431 | "System error when attempting SSL operation."); |
| 2432 | |
| 2433 | PyDoc_STRVAR(SSLEOFError_doc, |
| 2434 | "SSL/TLS connection terminated abruptly."); |
| 2435 | |
Antoine Pitrou | a0e0e23 | 2011-10-22 23:41:52 +0200 | [diff] [blame] | 2436 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 2437 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2438 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2439 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2440 | PyObject *m, *d, *r; |
| 2441 | unsigned long libver; |
| 2442 | unsigned int major, minor, fix, patch, status; |
| 2443 | PySocketModule_APIObject *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2444 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2445 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 2446 | return NULL; |
| 2447 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2448 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2450 | m = PyModule_Create(&_sslmodule); |
| 2451 | if (m == NULL) |
| 2452 | return NULL; |
| 2453 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2455 | /* Load _socket module and its C API */ |
| 2456 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 2457 | if (!socket_api) |
| 2458 | return NULL; |
| 2459 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2461 | /* Init OpenSSL */ |
| 2462 | SSL_load_error_strings(); |
| 2463 | SSL_library_init(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2464 | #ifdef WITH_THREAD |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2465 | /* note that this will start threading if not already started */ |
| 2466 | if (!_setup_ssl_threads()) { |
| 2467 | return NULL; |
| 2468 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 2469 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2470 | OpenSSL_add_all_algorithms(); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2471 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2472 | /* Add symbols to module dict */ |
Antoine Pitrou | a0e0e23 | 2011-10-22 23:41:52 +0200 | [diff] [blame] | 2473 | PySSLErrorObject = PyErr_NewExceptionWithDoc("ssl.SSLError", |
| 2474 | SSLError_doc, |
| 2475 | PyExc_OSError, |
| 2476 | NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2477 | if (PySSLErrorObject == NULL) |
| 2478 | return NULL; |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 2479 | PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 2480 | "ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 2481 | PySSLErrorObject, NULL); |
| 2482 | PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 2483 | "ssl.SSLWantReadError", SSLWantReadError_doc, |
| 2484 | PySSLErrorObject, NULL); |
| 2485 | PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 2486 | "ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 2487 | PySSLErrorObject, NULL); |
| 2488 | PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 2489 | "ssl.SSLSyscallError", SSLSyscallError_doc, |
| 2490 | PySSLErrorObject, NULL); |
| 2491 | PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 2492 | "ssl.SSLEOFError", SSLEOFError_doc, |
| 2493 | PySSLErrorObject, NULL); |
| 2494 | if (PySSLZeroReturnErrorObject == NULL |
| 2495 | || PySSLWantReadErrorObject == NULL |
| 2496 | || PySSLWantWriteErrorObject == NULL |
| 2497 | || PySSLSyscallErrorObject == NULL |
| 2498 | || PySSLEOFErrorObject == NULL) |
| 2499 | return NULL; |
| 2500 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
| 2501 | || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 2502 | || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 2503 | || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 2504 | || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 2505 | || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2506 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2507 | if (PyDict_SetItemString(d, "_SSLContext", |
| 2508 | (PyObject *)&PySSLContext_Type) != 0) |
| 2509 | return NULL; |
| 2510 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 2511 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2512 | return NULL; |
| 2513 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 2514 | PY_SSL_ERROR_ZERO_RETURN); |
| 2515 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 2516 | PY_SSL_ERROR_WANT_READ); |
| 2517 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 2518 | PY_SSL_ERROR_WANT_WRITE); |
| 2519 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 2520 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 2521 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 2522 | PY_SSL_ERROR_SYSCALL); |
| 2523 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 2524 | PY_SSL_ERROR_SSL); |
| 2525 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 2526 | PY_SSL_ERROR_WANT_CONNECT); |
| 2527 | /* non ssl.h errorcodes */ |
| 2528 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 2529 | PY_SSL_ERROR_EOF); |
| 2530 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 2531 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 2532 | /* cert requirements */ |
| 2533 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 2534 | PY_SSL_CERT_NONE); |
| 2535 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 2536 | PY_SSL_CERT_OPTIONAL); |
| 2537 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 2538 | PY_SSL_CERT_REQUIRED); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2540 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2541 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2542 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 2543 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2544 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2545 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 2546 | PY_SSL_VERSION_SSL3); |
| 2547 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 2548 | PY_SSL_VERSION_SSL23); |
| 2549 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 2550 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 2551 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 2552 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 2553 | PyModule_AddIntConstant(m, "OP_ALL", |
| 2554 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 2555 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 2556 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 2557 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 2558 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 2559 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 2560 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 2561 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 2562 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 2563 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2564 | #ifdef SSL_OP_NO_COMPRESSION |
| 2565 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 2566 | SSL_OP_NO_COMPRESSION); |
| 2567 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 2569 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2570 | r = Py_True; |
| 2571 | #else |
| 2572 | r = Py_False; |
| 2573 | #endif |
| 2574 | Py_INCREF(r); |
| 2575 | PyModule_AddObject(m, "HAS_SNI", r); |
| 2576 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2577 | #if HAVE_OPENSSL_FINISHED |
| 2578 | r = Py_True; |
| 2579 | #else |
| 2580 | r = Py_False; |
| 2581 | #endif |
| 2582 | Py_INCREF(r); |
| 2583 | PyModule_AddObject(m, "HAS_TLS_UNIQUE", r); |
| 2584 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 2585 | #ifdef OPENSSL_NO_ECDH |
| 2586 | r = Py_False; |
| 2587 | #else |
| 2588 | r = Py_True; |
| 2589 | #endif |
| 2590 | Py_INCREF(r); |
| 2591 | PyModule_AddObject(m, "HAS_ECDH", r); |
| 2592 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2593 | /* OpenSSL version */ |
| 2594 | /* SSLeay() gives us the version of the library linked against, |
| 2595 | which could be different from the headers version. |
| 2596 | */ |
| 2597 | libver = SSLeay(); |
| 2598 | r = PyLong_FromUnsignedLong(libver); |
| 2599 | if (r == NULL) |
| 2600 | return NULL; |
| 2601 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 2602 | return NULL; |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 2603 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2604 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 2605 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 2606 | return NULL; |
| 2607 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 2608 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 2609 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 2610 | |
Antoine Pitrou | b9ac25d | 2011-07-08 18:47:06 +0200 | [diff] [blame] | 2611 | libver = OPENSSL_VERSION_NUMBER; |
| 2612 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 2613 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 2614 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 2615 | return NULL; |
| 2616 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2617 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2618 | } |