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