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" |
| 21 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 22 | PyThreadState *_save = NULL; \ |
| 23 | if (_ssl_locks_count>0) {_save = PyEval_SaveThread();} |
| 24 | #define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)}; |
| 25 | #define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()}; |
| 26 | #define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \ |
| 27 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 29 | #else /* no WITH_THREAD */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 30 | |
| 31 | #define PySSL_BEGIN_ALLOW_THREADS |
| 32 | #define PySSL_BLOCK_THREADS |
| 33 | #define PySSL_UNBLOCK_THREADS |
| 34 | #define PySSL_END_ALLOW_THREADS |
| 35 | |
| 36 | #endif |
| 37 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 38 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 39 | /* these mirror ssl.h */ |
| 40 | PY_SSL_ERROR_NONE, |
| 41 | PY_SSL_ERROR_SSL, |
| 42 | PY_SSL_ERROR_WANT_READ, |
| 43 | PY_SSL_ERROR_WANT_WRITE, |
| 44 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 45 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 46 | PY_SSL_ERROR_ZERO_RETURN, |
| 47 | PY_SSL_ERROR_WANT_CONNECT, |
| 48 | /* start of non ssl.h errorcodes */ |
| 49 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 50 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 51 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 52 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 54 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 55 | PY_SSL_CLIENT, |
| 56 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 60 | PY_SSL_CERT_NONE, |
| 61 | PY_SSL_CERT_OPTIONAL, |
| 62 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 66 | PY_SSL_VERSION_SSL2, |
| 67 | PY_SSL_VERSION_SSL3, |
| 68 | PY_SSL_VERSION_SSL23, |
| 69 | PY_SSL_VERSION_TLS1 |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 72 | /* Include symbols from _socket module */ |
| 73 | #include "socketmodule.h" |
| 74 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 75 | static PySocketModule_APIObject PySocketModule; |
| 76 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 77 | #if defined(HAVE_POLL_H) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #include <poll.h> |
| 79 | #elif defined(HAVE_SYS_POLL_H) |
| 80 | #include <sys/poll.h> |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 83 | /* Include OpenSSL header files */ |
| 84 | #include "openssl/rsa.h" |
| 85 | #include "openssl/crypto.h" |
| 86 | #include "openssl/x509.h" |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 87 | #include "openssl/x509v3.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 88 | #include "openssl/pem.h" |
| 89 | #include "openssl/ssl.h" |
| 90 | #include "openssl/err.h" |
| 91 | #include "openssl/rand.h" |
| 92 | |
| 93 | /* SSL error object */ |
| 94 | static PyObject *PySSLErrorObject; |
| 95 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 96 | #ifdef WITH_THREAD |
| 97 | |
| 98 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 99 | /* 0 means no, greater than 0 means yes */ |
| 100 | |
| 101 | static unsigned int _ssl_locks_count = 0; |
| 102 | |
| 103 | #endif /* def WITH_THREAD */ |
| 104 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 105 | /* SSL socket object */ |
| 106 | |
| 107 | #define X509_NAME_MAXLEN 256 |
| 108 | |
| 109 | /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
| 110 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 111 | # define HAVE_OPENSSL_RAND 1 |
| 112 | #else |
| 113 | # undef HAVE_OPENSSL_RAND |
| 114 | #endif |
| 115 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 116 | /* SSL_CTX_clear_options() and SSL_clear_options() were first added in OpenSSL 0.9.8m */ |
| 117 | #if OPENSSL_VERSION_NUMBER >= 0x009080dfL |
| 118 | # define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 119 | #else |
| 120 | # undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 121 | #endif |
| 122 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 123 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 124 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 125 | SSL_CTX *ctx; |
| 126 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 128 | typedef struct { |
| 129 | PyObject_HEAD |
| 130 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 131 | SSL *ssl; |
| 132 | X509 *peer_cert; |
| 133 | int shutdown_seen_zero; |
| 134 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 136 | static PyTypeObject PySSLContext_Type; |
| 137 | static PyTypeObject PySSLSocket_Type; |
| 138 | |
| 139 | static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args); |
| 140 | static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 141 | static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 142 | int writing); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 143 | static PyObject *PySSL_peercert(PySSLSocket *self, PyObject *args); |
| 144 | static PyObject *PySSL_cipher(PySSLSocket *self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 146 | #define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type) |
| 147 | #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 149 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 150 | SOCKET_IS_NONBLOCKING, |
| 151 | SOCKET_IS_BLOCKING, |
| 152 | SOCKET_HAS_TIMED_OUT, |
| 153 | SOCKET_HAS_BEEN_CLOSED, |
| 154 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 155 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 156 | } timeout_state; |
| 157 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 158 | /* Wrap error strings with filename and line # */ |
| 159 | #define STRINGIFY1(x) #x |
| 160 | #define STRINGIFY2(x) STRINGIFY1(x) |
| 161 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
| 162 | #define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) |
| 163 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 164 | /* XXX It might be helpful to augment the error message generated |
| 165 | below with the name of the SSL function that generated the error. |
| 166 | I expect it's obvious most of the time. |
| 167 | */ |
| 168 | |
| 169 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 170 | PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 172 | PyObject *v; |
| 173 | char buf[2048]; |
| 174 | char *errstr; |
| 175 | int err; |
| 176 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 178 | assert(ret <= 0); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 180 | if (obj->ssl != NULL) { |
| 181 | err = SSL_get_error(obj->ssl, ret); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 183 | switch (err) { |
| 184 | case SSL_ERROR_ZERO_RETURN: |
| 185 | errstr = "TLS/SSL connection has been closed"; |
| 186 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 187 | break; |
| 188 | case SSL_ERROR_WANT_READ: |
| 189 | errstr = "The operation did not complete (read)"; |
| 190 | p = PY_SSL_ERROR_WANT_READ; |
| 191 | break; |
| 192 | case SSL_ERROR_WANT_WRITE: |
| 193 | p = PY_SSL_ERROR_WANT_WRITE; |
| 194 | errstr = "The operation did not complete (write)"; |
| 195 | break; |
| 196 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 197 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 198 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 199 | break; |
| 200 | case SSL_ERROR_WANT_CONNECT: |
| 201 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 202 | errstr = "The operation did not complete (connect)"; |
| 203 | break; |
| 204 | case SSL_ERROR_SYSCALL: |
| 205 | { |
| 206 | unsigned long e = ERR_get_error(); |
| 207 | if (e == 0) { |
| 208 | PySocketSockObject *s |
| 209 | = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket); |
| 210 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 211 | p = PY_SSL_ERROR_EOF; |
| 212 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 213 | } else if (ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 214 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 215 | ERR_clear_error(); |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 216 | return s->errorhandler(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 217 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 218 | p = PY_SSL_ERROR_SYSCALL; |
| 219 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 220 | } |
| 221 | } else { |
| 222 | p = PY_SSL_ERROR_SYSCALL; |
| 223 | /* XXX Protected by global interpreter lock */ |
| 224 | errstr = ERR_error_string(e, NULL); |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | case SSL_ERROR_SSL: |
| 229 | { |
| 230 | unsigned long e = ERR_get_error(); |
| 231 | p = PY_SSL_ERROR_SSL; |
| 232 | if (e != 0) |
| 233 | /* XXX Protected by global interpreter lock */ |
| 234 | errstr = ERR_error_string(e, NULL); |
| 235 | else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 236 | errstr = "A failure in the SSL library occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 237 | } |
| 238 | break; |
| 239 | } |
| 240 | default: |
| 241 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 242 | errstr = "Invalid error code"; |
| 243 | } |
| 244 | } else { |
| 245 | errstr = ERR_error_string(ERR_peek_last_error(), NULL); |
| 246 | } |
| 247 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 248 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 249 | v = Py_BuildValue("(is)", p, buf); |
| 250 | if (v != NULL) { |
| 251 | PyErr_SetObject(PySSLErrorObject, v); |
| 252 | Py_DECREF(v); |
| 253 | } |
| 254 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 257 | static PyObject * |
| 258 | _setSSLError (char *errstr, int errcode, char *filename, int lineno) { |
| 259 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 260 | char buf[2048]; |
| 261 | PyObject *v; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 263 | if (errstr == NULL) { |
| 264 | errcode = ERR_peek_last_error(); |
| 265 | errstr = ERR_error_string(errcode, NULL); |
| 266 | } |
| 267 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 268 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 269 | v = Py_BuildValue("(is)", errcode, buf); |
| 270 | if (v != NULL) { |
| 271 | PyErr_SetObject(PySSLErrorObject, v); |
| 272 | Py_DECREF(v); |
| 273 | } |
| 274 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 277 | static PySSLSocket * |
| 278 | newPySSLSocket(SSL_CTX *ctx, PySocketSockObject *sock, |
| 279 | enum py_ssl_server_or_client socket_type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 280 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 281 | PySSLSocket *self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 283 | self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 284 | if (self == NULL) |
| 285 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 287 | self->peer_cert = NULL; |
| 288 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 289 | self->Socket = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 291 | /* Make sure the SSL error state is initialized */ |
| 292 | (void) ERR_get_state(); |
| 293 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 295 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 296 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 297 | PySSL_END_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 298 | SSL_set_fd(self->ssl, sock->sock_fd); |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 299 | #ifdef SSL_MODE_AUTO_RETRY |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 300 | SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY); |
Antoine Pitrou | 0ae7b58 | 2010-04-09 20:42:09 +0000 | [diff] [blame] | 301 | #endif |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 303 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 304 | * to non-blocking mode (blocking is the default) |
| 305 | */ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 306 | if (sock->sock_timeout >= 0.0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 307 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 308 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 309 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 311 | PySSL_BEGIN_ALLOW_THREADS |
| 312 | if (socket_type == PY_SSL_CLIENT) |
| 313 | SSL_set_connect_state(self->ssl); |
| 314 | else |
| 315 | SSL_set_accept_state(self->ssl); |
| 316 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 318 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 319 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 322 | /* SSL object methods */ |
| 323 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 324 | static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 325 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 326 | int ret; |
| 327 | int err; |
| 328 | int sockstate, nonblocking; |
| 329 | PySocketSockObject *sock |
| 330 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 332 | if (((PyObject*)sock) == Py_None) { |
| 333 | _setSSLError("Underlying socket connection gone", |
| 334 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 335 | return NULL; |
| 336 | } |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 338 | /* just in case the blocking state of the socket has been changed */ |
| 339 | nonblocking = (sock->sock_timeout >= 0.0); |
| 340 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 341 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 343 | /* Actually negotiate SSL connection */ |
| 344 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
| 345 | sockstate = 0; |
| 346 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 347 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 348 | ret = SSL_do_handshake(self->ssl); |
| 349 | err = SSL_get_error(self->ssl, ret); |
| 350 | PySSL_END_ALLOW_THREADS |
| 351 | if(PyErr_CheckSignals()) { |
| 352 | return NULL; |
| 353 | } |
| 354 | if (err == SSL_ERROR_WANT_READ) { |
| 355 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 356 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 357 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 358 | } else { |
| 359 | sockstate = SOCKET_OPERATION_OK; |
| 360 | } |
| 361 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 362 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 363 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 364 | return NULL; |
| 365 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 366 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 367 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 368 | return NULL; |
| 369 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 370 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 371 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 374 | break; |
| 375 | } |
| 376 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 377 | if (ret < 1) |
| 378 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 379 | self->ssl->debug = 1; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 380 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 381 | if (self->peer_cert) |
| 382 | X509_free (self->peer_cert); |
| 383 | PySSL_BEGIN_ALLOW_THREADS |
| 384 | self->peer_cert = SSL_get_peer_certificate(self->ssl); |
| 385 | PySSL_END_ALLOW_THREADS |
| 386 | |
| 387 | Py_INCREF(Py_None); |
| 388 | return Py_None; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 391 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 392 | _create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 394 | char namebuf[X509_NAME_MAXLEN]; |
| 395 | int buflen; |
| 396 | PyObject *name_obj; |
| 397 | PyObject *value_obj; |
| 398 | PyObject *attr; |
| 399 | unsigned char *valuebuf = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 401 | buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0); |
| 402 | if (buflen < 0) { |
| 403 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 404 | goto fail; |
| 405 | } |
| 406 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 407 | if (name_obj == NULL) |
| 408 | goto fail; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 410 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 411 | if (buflen < 0) { |
| 412 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 413 | Py_DECREF(name_obj); |
| 414 | goto fail; |
| 415 | } |
| 416 | value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 417 | buflen, "strict"); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 418 | OPENSSL_free(valuebuf); |
| 419 | if (value_obj == NULL) { |
| 420 | Py_DECREF(name_obj); |
| 421 | goto fail; |
| 422 | } |
| 423 | attr = PyTuple_New(2); |
| 424 | if (attr == NULL) { |
| 425 | Py_DECREF(name_obj); |
| 426 | Py_DECREF(value_obj); |
| 427 | goto fail; |
| 428 | } |
| 429 | PyTuple_SET_ITEM(attr, 0, name_obj); |
| 430 | PyTuple_SET_ITEM(attr, 1, value_obj); |
| 431 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 432 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 433 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 434 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static PyObject * |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 438 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 439 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 440 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 441 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 442 | PyObject *rdnt; |
| 443 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 444 | int entry_count = X509_NAME_entry_count(xname); |
| 445 | X509_NAME_ENTRY *entry; |
| 446 | ASN1_OBJECT *name; |
| 447 | ASN1_STRING *value; |
| 448 | int index_counter; |
| 449 | int rdn_level = -1; |
| 450 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 452 | dn = PyList_New(0); |
| 453 | if (dn == NULL) |
| 454 | return NULL; |
| 455 | /* now create another tuple to hold the top-level RDN */ |
| 456 | rdn = PyList_New(0); |
| 457 | if (rdn == NULL) |
| 458 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 460 | for (index_counter = 0; |
| 461 | index_counter < entry_count; |
| 462 | index_counter++) |
| 463 | { |
| 464 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 466 | /* check to see if we've gotten to a new RDN */ |
| 467 | if (rdn_level >= 0) { |
| 468 | if (rdn_level != entry->set) { |
| 469 | /* yes, new RDN */ |
| 470 | /* add old RDN to DN */ |
| 471 | rdnt = PyList_AsTuple(rdn); |
| 472 | Py_DECREF(rdn); |
| 473 | if (rdnt == NULL) |
| 474 | goto fail0; |
| 475 | retcode = PyList_Append(dn, rdnt); |
| 476 | Py_DECREF(rdnt); |
| 477 | if (retcode < 0) |
| 478 | goto fail0; |
| 479 | /* create new RDN */ |
| 480 | rdn = PyList_New(0); |
| 481 | if (rdn == NULL) |
| 482 | goto fail0; |
| 483 | } |
| 484 | } |
| 485 | rdn_level = entry->set; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 487 | /* now add this attribute to the current RDN */ |
| 488 | name = X509_NAME_ENTRY_get_object(entry); |
| 489 | value = X509_NAME_ENTRY_get_data(entry); |
| 490 | attr = _create_tuple_for_attribute(name, value); |
| 491 | /* |
| 492 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 493 | entry->set, |
| 494 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 495 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 496 | */ |
| 497 | if (attr == NULL) |
| 498 | goto fail1; |
| 499 | retcode = PyList_Append(rdn, attr); |
| 500 | Py_DECREF(attr); |
| 501 | if (retcode < 0) |
| 502 | goto fail1; |
| 503 | } |
| 504 | /* now, there's typically a dangling RDN */ |
| 505 | if ((rdn != NULL) && (PyList_Size(rdn) > 0)) { |
| 506 | rdnt = PyList_AsTuple(rdn); |
| 507 | Py_DECREF(rdn); |
| 508 | if (rdnt == NULL) |
| 509 | goto fail0; |
| 510 | retcode = PyList_Append(dn, rdnt); |
| 511 | Py_DECREF(rdnt); |
| 512 | if (retcode < 0) |
| 513 | goto fail0; |
| 514 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 516 | /* convert list to tuple */ |
| 517 | rdnt = PyList_AsTuple(dn); |
| 518 | Py_DECREF(dn); |
| 519 | if (rdnt == NULL) |
| 520 | return NULL; |
| 521 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 522 | |
| 523 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 524 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 525 | |
| 526 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 527 | Py_XDECREF(dn); |
| 528 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static PyObject * |
| 532 | _get_peer_alt_names (X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 534 | /* this code follows the procedure outlined in |
| 535 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 536 | function to extract the STACK_OF(GENERAL_NAME), |
| 537 | then iterates through the stack to add the |
| 538 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 540 | int i, j; |
| 541 | PyObject *peer_alt_names = Py_None; |
| 542 | PyObject *v, *t; |
| 543 | X509_EXTENSION *ext = NULL; |
| 544 | GENERAL_NAMES *names = NULL; |
| 545 | GENERAL_NAME *name; |
| 546 | X509V3_EXT_METHOD *method; |
| 547 | BIO *biobuf = NULL; |
| 548 | char buf[2048]; |
| 549 | char *vptr; |
| 550 | int len; |
| 551 | /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */ |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 552 | #if OPENSSL_VERSION_NUMBER >= 0x009060dfL |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 553 | const unsigned char *p; |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 554 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 555 | unsigned char *p; |
Victor Stinner | 7124a41 | 2010-03-02 22:48:17 +0000 | [diff] [blame] | 556 | #endif |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 558 | if (certificate == NULL) |
| 559 | return peer_alt_names; |
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 | /* get a memory buffer */ |
| 562 | biobuf = BIO_new(BIO_s_mem()); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 564 | i = 0; |
| 565 | while ((i = X509_get_ext_by_NID( |
| 566 | certificate, NID_subject_alt_name, i)) >= 0) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 568 | if (peer_alt_names == Py_None) { |
| 569 | peer_alt_names = PyList_New(0); |
| 570 | if (peer_alt_names == NULL) |
| 571 | goto fail; |
| 572 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 574 | /* now decode the altName */ |
| 575 | ext = X509_get_ext(certificate, i); |
| 576 | if(!(method = X509V3_EXT_get(ext))) { |
| 577 | PyErr_SetString |
| 578 | (PySSLErrorObject, |
| 579 | ERRSTR("No method for internalizing subjectAltName!")); |
| 580 | goto fail; |
| 581 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 583 | p = ext->value->data; |
| 584 | if (method->it) |
| 585 | names = (GENERAL_NAMES*) |
| 586 | (ASN1_item_d2i(NULL, |
| 587 | &p, |
| 588 | ext->value->length, |
| 589 | ASN1_ITEM_ptr(method->it))); |
| 590 | else |
| 591 | names = (GENERAL_NAMES*) |
| 592 | (method->d2i(NULL, |
| 593 | &p, |
| 594 | ext->value->length)); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 596 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 598 | /* get a rendering of each name in the set of names */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 599 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 600 | name = sk_GENERAL_NAME_value(names, j); |
| 601 | if (name->type == GEN_DIRNAME) { |
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 | /* we special-case DirName as a tuple of |
| 604 | tuples of attributes */ |
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 | t = PyTuple_New(2); |
| 607 | if (t == NULL) { |
| 608 | goto fail; |
| 609 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 611 | v = PyUnicode_FromString("DirName"); |
| 612 | if (v == NULL) { |
| 613 | Py_DECREF(t); |
| 614 | goto fail; |
| 615 | } |
| 616 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 618 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 619 | if (v == NULL) { |
| 620 | Py_DECREF(t); |
| 621 | goto fail; |
| 622 | } |
| 623 | PyTuple_SET_ITEM(t, 1, v); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 625 | } else { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 626 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 627 | /* for everything else, we use the OpenSSL print form */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 629 | (void) BIO_reset(biobuf); |
| 630 | GENERAL_NAME_print(biobuf, name); |
| 631 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 632 | if (len < 0) { |
| 633 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 634 | goto fail; |
| 635 | } |
| 636 | vptr = strchr(buf, ':'); |
| 637 | if (vptr == NULL) |
| 638 | goto fail; |
| 639 | t = PyTuple_New(2); |
| 640 | if (t == NULL) |
| 641 | goto fail; |
| 642 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 643 | if (v == NULL) { |
| 644 | Py_DECREF(t); |
| 645 | goto fail; |
| 646 | } |
| 647 | PyTuple_SET_ITEM(t, 0, v); |
| 648 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 649 | (len - (vptr - buf + 1))); |
| 650 | if (v == NULL) { |
| 651 | Py_DECREF(t); |
| 652 | goto fail; |
| 653 | } |
| 654 | PyTuple_SET_ITEM(t, 1, v); |
| 655 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 657 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 659 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 660 | Py_DECREF(t); |
| 661 | goto fail; |
| 662 | } |
| 663 | Py_DECREF(t); |
| 664 | } |
| 665 | } |
| 666 | BIO_free(biobuf); |
| 667 | if (peer_alt_names != Py_None) { |
| 668 | v = PyList_AsTuple(peer_alt_names); |
| 669 | Py_DECREF(peer_alt_names); |
| 670 | return v; |
| 671 | } else { |
| 672 | return peer_alt_names; |
| 673 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 674 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 675 | |
| 676 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 677 | if (biobuf != NULL) |
| 678 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 680 | if (peer_alt_names != Py_None) { |
| 681 | Py_XDECREF(peer_alt_names); |
| 682 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 684 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static PyObject * |
| 688 | _decode_certificate (X509 *certificate, int verbose) { |
| 689 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 690 | PyObject *retval = NULL; |
| 691 | BIO *biobuf = NULL; |
| 692 | PyObject *peer; |
| 693 | PyObject *peer_alt_names = NULL; |
| 694 | PyObject *issuer; |
| 695 | PyObject *version; |
| 696 | PyObject *sn_obj; |
| 697 | ASN1_INTEGER *serialNumber; |
| 698 | char buf[2048]; |
| 699 | int len; |
| 700 | ASN1_TIME *notBefore, *notAfter; |
| 701 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 703 | retval = PyDict_New(); |
| 704 | if (retval == NULL) |
| 705 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 707 | peer = _create_tuple_for_X509_NAME( |
| 708 | X509_get_subject_name(certificate)); |
| 709 | if (peer == NULL) |
| 710 | goto fail0; |
| 711 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 712 | Py_DECREF(peer); |
| 713 | goto fail0; |
| 714 | } |
| 715 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 717 | if (verbose) { |
| 718 | issuer = _create_tuple_for_X509_NAME( |
| 719 | X509_get_issuer_name(certificate)); |
| 720 | if (issuer == NULL) |
| 721 | goto fail0; |
| 722 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| 723 | Py_DECREF(issuer); |
| 724 | goto fail0; |
| 725 | } |
| 726 | Py_DECREF(issuer); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 727 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 728 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
| 729 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 730 | Py_DECREF(version); |
| 731 | goto fail0; |
| 732 | } |
| 733 | Py_DECREF(version); |
| 734 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 736 | /* get a memory buffer */ |
| 737 | biobuf = BIO_new(BIO_s_mem()); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 739 | if (verbose) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 741 | (void) BIO_reset(biobuf); |
| 742 | serialNumber = X509_get_serialNumber(certificate); |
| 743 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 744 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 745 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 746 | if (len < 0) { |
| 747 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 748 | goto fail1; |
| 749 | } |
| 750 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 751 | if (sn_obj == NULL) |
| 752 | goto fail1; |
| 753 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 754 | Py_DECREF(sn_obj); |
| 755 | goto fail1; |
| 756 | } |
| 757 | Py_DECREF(sn_obj); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 759 | (void) BIO_reset(biobuf); |
| 760 | notBefore = X509_get_notBefore(certificate); |
| 761 | ASN1_TIME_print(biobuf, notBefore); |
| 762 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 763 | if (len < 0) { |
| 764 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 765 | goto fail1; |
| 766 | } |
| 767 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 768 | if (pnotBefore == NULL) |
| 769 | goto fail1; |
| 770 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 771 | Py_DECREF(pnotBefore); |
| 772 | goto fail1; |
| 773 | } |
| 774 | Py_DECREF(pnotBefore); |
| 775 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 777 | (void) BIO_reset(biobuf); |
| 778 | notAfter = X509_get_notAfter(certificate); |
| 779 | ASN1_TIME_print(biobuf, notAfter); |
| 780 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 781 | if (len < 0) { |
| 782 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 783 | goto fail1; |
| 784 | } |
| 785 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 786 | if (pnotAfter == NULL) |
| 787 | goto fail1; |
| 788 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 789 | Py_DECREF(pnotAfter); |
| 790 | goto fail1; |
| 791 | } |
| 792 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 794 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 796 | peer_alt_names = _get_peer_alt_names(certificate); |
| 797 | if (peer_alt_names == NULL) |
| 798 | goto fail1; |
| 799 | else if (peer_alt_names != Py_None) { |
| 800 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 801 | peer_alt_names) < 0) { |
| 802 | Py_DECREF(peer_alt_names); |
| 803 | goto fail1; |
| 804 | } |
| 805 | Py_DECREF(peer_alt_names); |
| 806 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 808 | BIO_free(biobuf); |
| 809 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 810 | |
| 811 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 812 | if (biobuf != NULL) |
| 813 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 814 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 815 | Py_XDECREF(retval); |
| 816 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 817 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 818 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 819 | |
| 820 | static PyObject * |
| 821 | PySSL_test_decode_certificate (PyObject *mod, PyObject *args) { |
| 822 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 823 | PyObject *retval = NULL; |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 824 | PyObject *filename; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 825 | X509 *x=NULL; |
| 826 | BIO *cert; |
| 827 | int verbose = 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 828 | |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 829 | if (!PyArg_ParseTuple(args, "O&|i:test_decode_certificate", |
| 830 | PyUnicode_FSConverter, &filename, &verbose)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 831 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 833 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 834 | PyErr_SetString(PySSLErrorObject, |
| 835 | "Can't malloc memory to read file"); |
| 836 | goto fail0; |
| 837 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 838 | |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 839 | if (BIO_read_filename(cert, PyBytes_AsString(filename)) <= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 840 | PyErr_SetString(PySSLErrorObject, |
| 841 | "Can't open file"); |
| 842 | goto fail0; |
| 843 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 845 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 846 | if (x == NULL) { |
| 847 | PyErr_SetString(PySSLErrorObject, |
| 848 | "Error decoding PEM-encoded file"); |
| 849 | goto fail0; |
| 850 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 852 | retval = _decode_certificate(x, verbose); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 853 | |
| 854 | fail0: |
Victor Stinner | 3800e1e | 2010-05-16 21:23:48 +0000 | [diff] [blame] | 855 | Py_DECREF(filename); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 856 | if (cert != NULL) BIO_free(cert); |
| 857 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | |
| 861 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 862 | PySSL_peercert(PySSLSocket *self, PyObject *args) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 863 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 864 | PyObject *retval = NULL; |
| 865 | int len; |
| 866 | int verification; |
| 867 | PyObject *binary_mode = Py_None; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 868 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 869 | if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode)) |
| 870 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 872 | if (!self->peer_cert) |
| 873 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 875 | if (PyObject_IsTrue(binary_mode)) { |
| 876 | /* return cert in DER-encoded format */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 878 | unsigned char *bytes_buf = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 880 | bytes_buf = NULL; |
| 881 | len = i2d_X509(self->peer_cert, &bytes_buf); |
| 882 | if (len < 0) { |
| 883 | PySSL_SetError(self, len, __FILE__, __LINE__); |
| 884 | return NULL; |
| 885 | } |
| 886 | /* this is actually an immutable bytes sequence */ |
| 887 | retval = PyBytes_FromStringAndSize |
| 888 | ((const char *) bytes_buf, len); |
| 889 | OPENSSL_free(bytes_buf); |
| 890 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 891 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 892 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 893 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 894 | if ((verification & SSL_VERIFY_PEER) == 0) |
| 895 | return PyDict_New(); |
| 896 | else |
| 897 | return _decode_certificate (self->peer_cert, 0); |
| 898 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | PyDoc_STRVAR(PySSL_peercert_doc, |
| 902 | "peer_certificate([der=False]) -> certificate\n\ |
| 903 | \n\ |
| 904 | Returns the certificate for the peer. If no certificate was provided,\n\ |
| 905 | returns None. If a certificate was provided, but not validated, returns\n\ |
| 906 | an empty dictionary. Otherwise returns a dict containing information\n\ |
| 907 | about the peer certificate.\n\ |
| 908 | \n\ |
| 909 | If the optional argument is True, returns a DER-encoded copy of the\n\ |
| 910 | peer certificate, or None if no certificate was provided. This will\n\ |
| 911 | return the certificate even if it wasn't validated."); |
| 912 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 913 | static PyObject *PySSL_cipher (PySSLSocket *self) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 915 | PyObject *retval, *v; |
| 916 | SSL_CIPHER *current; |
| 917 | char *cipher_name; |
| 918 | char *cipher_protocol; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 920 | if (self->ssl == NULL) |
| 921 | return Py_None; |
| 922 | current = SSL_get_current_cipher(self->ssl); |
| 923 | if (current == NULL) |
| 924 | return Py_None; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 926 | retval = PyTuple_New(3); |
| 927 | if (retval == NULL) |
| 928 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 930 | cipher_name = (char *) SSL_CIPHER_get_name(current); |
| 931 | if (cipher_name == NULL) { |
| 932 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 933 | } else { |
| 934 | v = PyUnicode_FromString(cipher_name); |
| 935 | if (v == NULL) |
| 936 | goto fail0; |
| 937 | PyTuple_SET_ITEM(retval, 0, v); |
| 938 | } |
| 939 | cipher_protocol = SSL_CIPHER_get_version(current); |
| 940 | if (cipher_protocol == NULL) { |
| 941 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 942 | } else { |
| 943 | v = PyUnicode_FromString(cipher_protocol); |
| 944 | if (v == NULL) |
| 945 | goto fail0; |
| 946 | PyTuple_SET_ITEM(retval, 1, v); |
| 947 | } |
| 948 | v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL)); |
| 949 | if (v == NULL) |
| 950 | goto fail0; |
| 951 | PyTuple_SET_ITEM(retval, 2, v); |
| 952 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 953 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 954 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 955 | Py_DECREF(retval); |
| 956 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 959 | static void PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 960 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 961 | if (self->peer_cert) /* Possible not to have one? */ |
| 962 | X509_free (self->peer_cert); |
| 963 | if (self->ssl) |
| 964 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 965 | Py_XDECREF(self->Socket); |
| 966 | PyObject_Del(self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 969 | /* 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] | 970 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 971 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 972 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 973 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 974 | static int |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 975 | check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 976 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 977 | fd_set fds; |
| 978 | struct timeval tv; |
| 979 | int rc; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 981 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 982 | if (s->sock_timeout < 0.0) |
| 983 | return SOCKET_IS_BLOCKING; |
| 984 | else if (s->sock_timeout == 0.0) |
| 985 | return SOCKET_IS_NONBLOCKING; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 987 | /* Guard against closed socket */ |
| 988 | if (s->sock_fd < 0) |
| 989 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 991 | /* Prefer poll, if available, since you can poll() any fd |
| 992 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 993 | #ifdef HAVE_POLL |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 994 | { |
| 995 | struct pollfd pollfd; |
| 996 | int timeout; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 998 | pollfd.fd = s->sock_fd; |
| 999 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1001 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 1002 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 1003 | PySSL_BEGIN_ALLOW_THREADS |
| 1004 | rc = poll(&pollfd, 1, timeout); |
| 1005 | PySSL_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1006 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1007 | goto normal_return; |
| 1008 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1009 | #endif |
| 1010 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1011 | /* Guard against socket too large for select*/ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 1012 | #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1013 | if (s->sock_fd >= FD_SETSIZE) |
| 1014 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 1015 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1017 | /* Construct the arguments to select */ |
| 1018 | tv.tv_sec = (int)s->sock_timeout; |
| 1019 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1020 | FD_ZERO(&fds); |
| 1021 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1023 | /* See if the socket is ready */ |
| 1024 | PySSL_BEGIN_ALLOW_THREADS |
| 1025 | if (writing) |
| 1026 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 1027 | else |
| 1028 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 1029 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1030 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1031 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1032 | normal_return: |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1033 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1034 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 1035 | (when we are able to write or when there's something to read) */ |
| 1036 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1039 | static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1040 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1041 | Py_buffer buf; |
| 1042 | int len; |
| 1043 | int sockstate; |
| 1044 | int err; |
| 1045 | int nonblocking; |
| 1046 | PySocketSockObject *sock |
| 1047 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1049 | if (((PyObject*)sock) == Py_None) { |
| 1050 | _setSSLError("Underlying socket connection gone", |
| 1051 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | |
| 1055 | if (!PyArg_ParseTuple(args, "y*:write", &buf)) |
| 1056 | return NULL; |
| 1057 | |
| 1058 | /* just in case the blocking state of the socket has been changed */ |
| 1059 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1060 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1061 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1062 | |
| 1063 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1064 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1065 | PyErr_SetString(PySSLErrorObject, |
| 1066 | "The write operation timed out"); |
| 1067 | goto error; |
| 1068 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1069 | PyErr_SetString(PySSLErrorObject, |
| 1070 | "Underlying socket has been closed."); |
| 1071 | goto error; |
| 1072 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1073 | PyErr_SetString(PySSLErrorObject, |
| 1074 | "Underlying socket too large for select()."); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | do { |
| 1078 | err = 0; |
| 1079 | PySSL_BEGIN_ALLOW_THREADS |
| 1080 | len = SSL_write(self->ssl, buf.buf, buf.len); |
| 1081 | err = SSL_get_error(self->ssl, len); |
| 1082 | PySSL_END_ALLOW_THREADS |
| 1083 | if (PyErr_CheckSignals()) { |
| 1084 | goto error; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1085 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1086 | if (err == SSL_ERROR_WANT_READ) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1087 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1088 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1089 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1090 | } else { |
| 1091 | sockstate = SOCKET_OPERATION_OK; |
| 1092 | } |
| 1093 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1094 | PyErr_SetString(PySSLErrorObject, |
| 1095 | "The write operation timed out"); |
| 1096 | goto error; |
| 1097 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1098 | PyErr_SetString(PySSLErrorObject, |
| 1099 | "Underlying socket has been closed."); |
| 1100 | goto error; |
| 1101 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1102 | break; |
| 1103 | } |
| 1104 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1106 | PyBuffer_Release(&buf); |
| 1107 | if (len > 0) |
| 1108 | return PyLong_FromLong(len); |
| 1109 | else |
| 1110 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 1111 | |
| 1112 | error: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1113 | PyBuffer_Release(&buf); |
| 1114 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1117 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1118 | "write(s) -> len\n\ |
| 1119 | \n\ |
| 1120 | 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] | 1121 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1123 | static PyObject *PySSL_SSLpending(PySSLSocket *self) |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1124 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1125 | int count = 0; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1127 | PySSL_BEGIN_ALLOW_THREADS |
| 1128 | count = SSL_pending(self->ssl); |
| 1129 | PySSL_END_ALLOW_THREADS |
| 1130 | if (count < 0) |
| 1131 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1132 | else |
| 1133 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | PyDoc_STRVAR(PySSL_SSLpending_doc, |
| 1137 | "pending() -> count\n\ |
| 1138 | \n\ |
| 1139 | Returns the number of already decrypted bytes available for read,\n\ |
| 1140 | pending on the connection.\n"); |
| 1141 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1142 | static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1143 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1144 | PyObject *dest = NULL; |
| 1145 | Py_buffer buf; |
| 1146 | int buf_passed = 0; |
| 1147 | int count = -1; |
| 1148 | char *mem; |
| 1149 | /* XXX this should use Py_ssize_t */ |
| 1150 | int len = 1024; |
| 1151 | int sockstate; |
| 1152 | int err; |
| 1153 | int nonblocking; |
| 1154 | PySocketSockObject *sock |
| 1155 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1156 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1157 | if (((PyObject*)sock) == Py_None) { |
| 1158 | _setSSLError("Underlying socket connection gone", |
| 1159 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | if (!PyArg_ParseTuple(args, "|Oi:read", &dest, &count)) |
| 1164 | return NULL; |
| 1165 | if ((dest == NULL) || (dest == Py_None)) { |
| 1166 | if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len))) |
| 1167 | return NULL; |
| 1168 | mem = PyByteArray_AS_STRING(dest); |
| 1169 | } else if (PyLong_Check(dest)) { |
| 1170 | len = PyLong_AS_LONG(dest); |
| 1171 | if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len))) |
| 1172 | return NULL; |
| 1173 | mem = PyByteArray_AS_STRING(dest); |
| 1174 | } else { |
| 1175 | if (PyObject_GetBuffer(dest, &buf, PyBUF_CONTIG) < 0) |
| 1176 | return NULL; |
| 1177 | mem = buf.buf; |
| 1178 | len = buf.len; |
| 1179 | if ((count > 0) && (count <= len)) |
| 1180 | len = count; |
| 1181 | buf_passed = 1; |
| 1182 | } |
| 1183 | |
| 1184 | /* just in case the blocking state of the socket has been changed */ |
| 1185 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1186 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1187 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1188 | |
| 1189 | /* first check if there are bytes ready to be read */ |
| 1190 | PySSL_BEGIN_ALLOW_THREADS |
| 1191 | count = SSL_pending(self->ssl); |
| 1192 | PySSL_END_ALLOW_THREADS |
| 1193 | |
| 1194 | if (!count) { |
| 1195 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1196 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1197 | PyErr_SetString(PySSLErrorObject, |
| 1198 | "The read operation timed out"); |
| 1199 | goto error; |
| 1200 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1201 | PyErr_SetString(PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1202 | "Underlying socket too large for select()."); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1203 | goto error; |
| 1204 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1205 | count = 0; |
| 1206 | goto done; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 1207 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1208 | } |
| 1209 | do { |
| 1210 | err = 0; |
| 1211 | PySSL_BEGIN_ALLOW_THREADS |
| 1212 | count = SSL_read(self->ssl, mem, len); |
| 1213 | err = SSL_get_error(self->ssl, count); |
| 1214 | PySSL_END_ALLOW_THREADS |
| 1215 | if (PyErr_CheckSignals()) |
| 1216 | goto error; |
| 1217 | if (err == SSL_ERROR_WANT_READ) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1218 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1219 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1220 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1221 | } else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 1222 | (SSL_get_shutdown(self->ssl) == |
| 1223 | SSL_RECEIVED_SHUTDOWN)) |
| 1224 | { |
| 1225 | count = 0; |
| 1226 | goto done; |
| 1227 | } else { |
| 1228 | sockstate = SOCKET_OPERATION_OK; |
| 1229 | } |
| 1230 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1231 | PyErr_SetString(PySSLErrorObject, |
| 1232 | "The read operation timed out"); |
| 1233 | goto error; |
| 1234 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1235 | break; |
| 1236 | } |
| 1237 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 1238 | if (count <= 0) { |
| 1239 | PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1240 | goto error; |
| 1241 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1242 | done: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1243 | if (!buf_passed) { |
| 1244 | PyObject *res = PyBytes_FromStringAndSize(mem, count); |
| 1245 | Py_DECREF(dest); |
| 1246 | return res; |
| 1247 | } else { |
| 1248 | PyBuffer_Release(&buf); |
| 1249 | return PyLong_FromLong(count); |
| 1250 | } |
Benjamin Peterson | 56420b4 | 2009-02-28 19:06:54 +0000 | [diff] [blame] | 1251 | error: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1252 | if (!buf_passed) { |
| 1253 | Py_DECREF(dest); |
| 1254 | } else { |
| 1255 | PyBuffer_Release(&buf); |
| 1256 | } |
| 1257 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1260 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1261 | "read([len]) -> string\n\ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1262 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1263 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1265 | static PyObject *PySSL_SSLshutdown(PySSLSocket *self) |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1266 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1267 | int err, ssl_err, sockstate, nonblocking; |
| 1268 | int zeros = 0; |
| 1269 | PySocketSockObject *sock |
| 1270 | = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1271 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1272 | /* Guard against closed socket */ |
| 1273 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) { |
| 1274 | _setSSLError("Underlying socket connection gone", |
| 1275 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1276 | return NULL; |
| 1277 | } |
| 1278 | |
| 1279 | /* Just in case the blocking state of the socket has been changed */ |
| 1280 | nonblocking = (sock->sock_timeout >= 0.0); |
| 1281 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1282 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1283 | |
| 1284 | while (1) { |
| 1285 | PySSL_BEGIN_ALLOW_THREADS |
| 1286 | /* Disable read-ahead so that unwrap can work correctly. |
| 1287 | * Otherwise OpenSSL might read in too much data, |
| 1288 | * eating clear text data that happens to be |
| 1289 | * transmitted after the SSL shutdown. |
| 1290 | * Should be safe to call repeatedly everytime this |
| 1291 | * function is used and the shutdown_seen_zero != 0 |
| 1292 | * condition is met. |
| 1293 | */ |
| 1294 | if (self->shutdown_seen_zero) |
| 1295 | SSL_set_read_ahead(self->ssl, 0); |
| 1296 | err = SSL_shutdown(self->ssl); |
| 1297 | PySSL_END_ALLOW_THREADS |
| 1298 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
| 1299 | if (err > 0) |
| 1300 | break; |
| 1301 | if (err == 0) { |
| 1302 | /* Don't loop endlessly; instead preserve legacy |
| 1303 | behaviour of trying SSL_shutdown() only twice. |
| 1304 | This looks necessary for OpenSSL < 0.9.8m */ |
| 1305 | if (++zeros > 1) |
| 1306 | break; |
| 1307 | /* Shutdown was sent, now try receiving */ |
| 1308 | self->shutdown_seen_zero = 1; |
| 1309 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1312 | /* Possibly retry shutdown until timeout or failure */ |
| 1313 | ssl_err = SSL_get_error(self->ssl, err); |
| 1314 | if (ssl_err == SSL_ERROR_WANT_READ) |
| 1315 | sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1316 | else if (ssl_err == SSL_ERROR_WANT_WRITE) |
| 1317 | sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1318 | else |
| 1319 | break; |
| 1320 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1321 | if (ssl_err == SSL_ERROR_WANT_READ) |
| 1322 | PyErr_SetString(PySSLErrorObject, |
| 1323 | "The read operation timed out"); |
| 1324 | else |
| 1325 | PyErr_SetString(PySSLErrorObject, |
| 1326 | "The write operation timed out"); |
| 1327 | return NULL; |
| 1328 | } |
| 1329 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1330 | PyErr_SetString(PySSLErrorObject, |
| 1331 | "Underlying socket too large for select()."); |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | else if (sockstate != SOCKET_OPERATION_OK) |
| 1335 | /* Retain the SSL error code */ |
| 1336 | break; |
| 1337 | } |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1339 | if (err < 0) |
| 1340 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
| 1341 | else { |
| 1342 | Py_INCREF(sock); |
| 1343 | return (PyObject *) sock; |
| 1344 | } |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 1348 | "shutdown(s) -> socket\n\ |
| 1349 | \n\ |
| 1350 | Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 1351 | the underlying socket object."); |
| 1352 | |
| 1353 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1354 | static PyMethodDef PySSLMethods[] = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1355 | {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS}, |
| 1356 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
| 1357 | PySSL_SSLwrite_doc}, |
| 1358 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
| 1359 | PySSL_SSLread_doc}, |
| 1360 | {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS, |
| 1361 | PySSL_SSLpending_doc}, |
| 1362 | {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS, |
| 1363 | PySSL_peercert_doc}, |
| 1364 | {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS}, |
| 1365 | {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 1366 | PySSL_SSLshutdown_doc}, |
| 1367 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1368 | }; |
| 1369 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1370 | static PyTypeObject PySSLSocket_Type = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1371 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1372 | "_ssl._SSLSocket", /*tp_name*/ |
| 1373 | sizeof(PySSLSocket), /*tp_basicsize*/ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1374 | 0, /*tp_itemsize*/ |
| 1375 | /* methods */ |
| 1376 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 1377 | 0, /*tp_print*/ |
| 1378 | 0, /*tp_getattr*/ |
| 1379 | 0, /*tp_setattr*/ |
| 1380 | 0, /*tp_reserved*/ |
| 1381 | 0, /*tp_repr*/ |
| 1382 | 0, /*tp_as_number*/ |
| 1383 | 0, /*tp_as_sequence*/ |
| 1384 | 0, /*tp_as_mapping*/ |
| 1385 | 0, /*tp_hash*/ |
| 1386 | 0, /*tp_call*/ |
| 1387 | 0, /*tp_str*/ |
| 1388 | 0, /*tp_getattro*/ |
| 1389 | 0, /*tp_setattro*/ |
| 1390 | 0, /*tp_as_buffer*/ |
| 1391 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 1392 | 0, /*tp_doc*/ |
| 1393 | 0, /*tp_traverse*/ |
| 1394 | 0, /*tp_clear*/ |
| 1395 | 0, /*tp_richcompare*/ |
| 1396 | 0, /*tp_weaklistoffset*/ |
| 1397 | 0, /*tp_iter*/ |
| 1398 | 0, /*tp_iternext*/ |
| 1399 | PySSLMethods, /*tp_methods*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1400 | }; |
| 1401 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* |
| 1404 | * _SSLContext objects |
| 1405 | */ |
| 1406 | |
| 1407 | static PyObject * |
| 1408 | context_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1409 | { |
| 1410 | char *kwlist[] = {"protocol", NULL}; |
| 1411 | PySSLContext *self; |
| 1412 | int proto_version = PY_SSL_VERSION_SSL23; |
| 1413 | SSL_CTX *ctx = NULL; |
| 1414 | |
| 1415 | if (!PyArg_ParseTupleAndKeywords( |
| 1416 | args, kwds, "i:_SSLContext", kwlist, |
| 1417 | &proto_version)) |
| 1418 | return NULL; |
| 1419 | |
| 1420 | PySSL_BEGIN_ALLOW_THREADS |
| 1421 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 1422 | ctx = SSL_CTX_new(TLSv1_method()); |
| 1423 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 1424 | ctx = SSL_CTX_new(SSLv3_method()); |
| 1425 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 1426 | ctx = SSL_CTX_new(SSLv2_method()); |
| 1427 | else if (proto_version == PY_SSL_VERSION_SSL23) |
| 1428 | ctx = SSL_CTX_new(SSLv23_method()); |
| 1429 | else |
| 1430 | proto_version = -1; |
| 1431 | PySSL_END_ALLOW_THREADS |
| 1432 | |
| 1433 | if (proto_version == -1) { |
| 1434 | PyErr_SetString(PyExc_ValueError, |
| 1435 | "invalid protocol version"); |
| 1436 | return NULL; |
| 1437 | } |
| 1438 | if (ctx == NULL) { |
| 1439 | PyErr_SetString(PySSLErrorObject, |
| 1440 | "failed to allocate SSL context"); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
| 1444 | assert(type != NULL && type->tp_alloc != NULL); |
| 1445 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 1446 | if (self == NULL) { |
| 1447 | SSL_CTX_free(ctx); |
| 1448 | return NULL; |
| 1449 | } |
| 1450 | self->ctx = ctx; |
| 1451 | /* Defaults */ |
| 1452 | SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL); |
| 1453 | SSL_CTX_set_options(self->ctx, SSL_OP_ALL); |
| 1454 | |
| 1455 | return (PyObject *)self; |
| 1456 | } |
| 1457 | |
| 1458 | static void |
| 1459 | context_dealloc(PySSLContext *self) |
| 1460 | { |
| 1461 | SSL_CTX_free(self->ctx); |
| 1462 | Py_TYPE(self)->tp_free(self); |
| 1463 | } |
| 1464 | |
| 1465 | static PyObject * |
| 1466 | set_ciphers(PySSLContext *self, PyObject *args) |
| 1467 | { |
| 1468 | int ret; |
| 1469 | const char *cipherlist; |
| 1470 | |
| 1471 | if (!PyArg_ParseTuple(args, "s:set_ciphers", &cipherlist)) |
| 1472 | return NULL; |
| 1473 | ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
| 1474 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 1475 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 1476 | otherwise the error will be reported again when another SSL call |
| 1477 | is done. */ |
| 1478 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1479 | PyErr_SetString(PySSLErrorObject, |
| 1480 | "No cipher can be selected."); |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | Py_RETURN_NONE; |
| 1484 | } |
| 1485 | |
| 1486 | static PyObject * |
| 1487 | get_verify_mode(PySSLContext *self, void *c) |
| 1488 | { |
| 1489 | switch (SSL_CTX_get_verify_mode(self->ctx)) { |
| 1490 | case SSL_VERIFY_NONE: |
| 1491 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 1492 | case SSL_VERIFY_PEER: |
| 1493 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 1494 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 1495 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 1496 | } |
| 1497 | PyErr_SetString(PySSLErrorObject, |
| 1498 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | |
| 1502 | static int |
| 1503 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 1504 | { |
| 1505 | int n, mode; |
| 1506 | if (!PyArg_Parse(arg, "i", &n)) |
| 1507 | return -1; |
| 1508 | if (n == PY_SSL_CERT_NONE) |
| 1509 | mode = SSL_VERIFY_NONE; |
| 1510 | else if (n == PY_SSL_CERT_OPTIONAL) |
| 1511 | mode = SSL_VERIFY_PEER; |
| 1512 | else if (n == PY_SSL_CERT_REQUIRED) |
| 1513 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1514 | else { |
| 1515 | PyErr_SetString(PyExc_ValueError, |
| 1516 | "invalid value for verify_mode"); |
| 1517 | return -1; |
| 1518 | } |
| 1519 | SSL_CTX_set_verify(self->ctx, mode, NULL); |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1524 | get_options(PySSLContext *self, void *c) |
| 1525 | { |
| 1526 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 1527 | } |
| 1528 | |
| 1529 | static int |
| 1530 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 1531 | { |
| 1532 | long new_opts, opts, set, clear; |
| 1533 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 1534 | return -1; |
| 1535 | opts = SSL_CTX_get_options(self->ctx); |
| 1536 | clear = opts & ~new_opts; |
| 1537 | set = ~opts & new_opts; |
| 1538 | if (clear) { |
| 1539 | #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 1540 | SSL_CTX_clear_options(self->ctx, clear); |
| 1541 | #else |
| 1542 | PyErr_SetString(PyExc_ValueError, |
| 1543 | "can't clear options before OpenSSL 0.9.8m"); |
| 1544 | return -1; |
| 1545 | #endif |
| 1546 | } |
| 1547 | if (set) |
| 1548 | SSL_CTX_set_options(self->ctx, set); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | static PyObject * |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1553 | load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1554 | { |
| 1555 | char *kwlist[] = {"certfile", "keyfile", NULL}; |
| 1556 | PyObject *certfile, *keyfile = NULL; |
| 1557 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
| 1558 | int r; |
| 1559 | |
| 1560 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1561 | "O|O:load_cert_chain", kwlist, |
| 1562 | &certfile, &keyfile)) |
| 1563 | return NULL; |
| 1564 | if (keyfile == Py_None) |
| 1565 | keyfile = NULL; |
| 1566 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
| 1567 | PyErr_SetString(PyExc_TypeError, |
| 1568 | "certfile should be a valid filesystem path"); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
| 1572 | PyErr_SetString(PyExc_TypeError, |
| 1573 | "keyfile should be a valid filesystem path"); |
| 1574 | goto error; |
| 1575 | } |
| 1576 | PySSL_BEGIN_ALLOW_THREADS |
| 1577 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 1578 | PyBytes_AS_STRING(certfile_bytes)); |
| 1579 | PySSL_END_ALLOW_THREADS |
| 1580 | if (r != 1) { |
| 1581 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1582 | goto error; |
| 1583 | } |
| 1584 | PySSL_BEGIN_ALLOW_THREADS |
| 1585 | r = SSL_CTX_use_RSAPrivateKey_file(self->ctx, |
| 1586 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 1587 | SSL_FILETYPE_PEM); |
| 1588 | PySSL_END_ALLOW_THREADS |
| 1589 | Py_XDECREF(keyfile_bytes); |
| 1590 | Py_XDECREF(certfile_bytes); |
| 1591 | if (r != 1) { |
| 1592 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | PySSL_BEGIN_ALLOW_THREADS |
| 1596 | r = SSL_CTX_check_private_key(self->ctx); |
| 1597 | PySSL_END_ALLOW_THREADS |
| 1598 | if (r != 1) { |
| 1599 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | Py_RETURN_NONE; |
| 1603 | |
| 1604 | error: |
| 1605 | Py_XDECREF(keyfile_bytes); |
| 1606 | Py_XDECREF(certfile_bytes); |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | |
| 1610 | static PyObject * |
| 1611 | load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1612 | { |
| 1613 | char *kwlist[] = {"cafile", "capath", NULL}; |
| 1614 | PyObject *cafile = NULL, *capath = NULL; |
| 1615 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 1616 | const char *cafile_buf = NULL, *capath_buf = NULL; |
| 1617 | int r; |
| 1618 | |
| 1619 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1620 | "|OO:load_verify_locations", kwlist, |
| 1621 | &cafile, &capath)) |
| 1622 | return NULL; |
| 1623 | if (cafile == Py_None) |
| 1624 | cafile = NULL; |
| 1625 | if (capath == Py_None) |
| 1626 | capath = NULL; |
| 1627 | if (cafile == NULL && capath == NULL) { |
| 1628 | PyErr_SetString(PyExc_TypeError, |
| 1629 | "cafile and capath cannot be both omitted"); |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
| 1633 | PyErr_SetString(PyExc_TypeError, |
| 1634 | "cafile should be a valid filesystem path"); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
| 1638 | Py_DECREF(cafile_bytes); |
| 1639 | PyErr_SetString(PyExc_TypeError, |
| 1640 | "capath should be a valid filesystem path"); |
| 1641 | return NULL; |
| 1642 | } |
| 1643 | if (cafile) |
| 1644 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 1645 | if (capath) |
| 1646 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 1647 | PySSL_BEGIN_ALLOW_THREADS |
| 1648 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 1649 | PySSL_END_ALLOW_THREADS |
| 1650 | Py_XDECREF(cafile_bytes); |
| 1651 | Py_XDECREF(capath_bytes); |
| 1652 | if (r != 1) { |
| 1653 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | Py_RETURN_NONE; |
| 1657 | } |
| 1658 | |
| 1659 | static PyObject * |
| 1660 | context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 1661 | { |
| 1662 | char *kwlist[] = {"sock", "server_side", NULL}; |
| 1663 | PySocketSockObject *sock; |
| 1664 | int server_side = 0; |
| 1665 | |
| 1666 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!i:_wrap_socket", kwlist, |
| 1667 | PySocketModule.Sock_Type, |
| 1668 | &sock, &server_side)) |
| 1669 | return NULL; |
| 1670 | |
| 1671 | return (PyObject *) newPySSLSocket(self->ctx, sock, server_side); |
| 1672 | } |
| 1673 | |
| 1674 | static PyGetSetDef context_getsetlist[] = { |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1675 | {"options", (getter) get_options, |
| 1676 | (setter) set_options, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1677 | {"verify_mode", (getter) get_verify_mode, |
| 1678 | (setter) set_verify_mode, NULL}, |
| 1679 | {NULL}, /* sentinel */ |
| 1680 | }; |
| 1681 | |
| 1682 | static struct PyMethodDef context_methods[] = { |
| 1683 | {"_wrap_socket", (PyCFunction) context_wrap_socket, |
| 1684 | METH_VARARGS | METH_KEYWORDS, NULL}, |
| 1685 | {"set_ciphers", (PyCFunction) set_ciphers, |
| 1686 | METH_VARARGS, NULL}, |
| 1687 | {"load_cert_chain", (PyCFunction) load_cert_chain, |
| 1688 | METH_VARARGS | METH_KEYWORDS, NULL}, |
| 1689 | {"load_verify_locations", (PyCFunction) load_verify_locations, |
| 1690 | METH_VARARGS | METH_KEYWORDS, NULL}, |
| 1691 | {NULL, NULL} /* sentinel */ |
| 1692 | }; |
| 1693 | |
| 1694 | static PyTypeObject PySSLContext_Type = { |
| 1695 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1696 | "_ssl._SSLContext", /*tp_name*/ |
| 1697 | sizeof(PySSLContext), /*tp_basicsize*/ |
| 1698 | 0, /*tp_itemsize*/ |
| 1699 | (destructor)context_dealloc, /*tp_dealloc*/ |
| 1700 | 0, /*tp_print*/ |
| 1701 | 0, /*tp_getattr*/ |
| 1702 | 0, /*tp_setattr*/ |
| 1703 | 0, /*tp_reserved*/ |
| 1704 | 0, /*tp_repr*/ |
| 1705 | 0, /*tp_as_number*/ |
| 1706 | 0, /*tp_as_sequence*/ |
| 1707 | 0, /*tp_as_mapping*/ |
| 1708 | 0, /*tp_hash*/ |
| 1709 | 0, /*tp_call*/ |
| 1710 | 0, /*tp_str*/ |
| 1711 | 0, /*tp_getattro*/ |
| 1712 | 0, /*tp_setattro*/ |
| 1713 | 0, /*tp_as_buffer*/ |
| 1714 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 1715 | 0, /*tp_doc*/ |
| 1716 | 0, /*tp_traverse*/ |
| 1717 | 0, /*tp_clear*/ |
| 1718 | 0, /*tp_richcompare*/ |
| 1719 | 0, /*tp_weaklistoffset*/ |
| 1720 | 0, /*tp_iter*/ |
| 1721 | 0, /*tp_iternext*/ |
| 1722 | context_methods, /*tp_methods*/ |
| 1723 | 0, /*tp_members*/ |
| 1724 | context_getsetlist, /*tp_getset*/ |
| 1725 | 0, /*tp_base*/ |
| 1726 | 0, /*tp_dict*/ |
| 1727 | 0, /*tp_descr_get*/ |
| 1728 | 0, /*tp_descr_set*/ |
| 1729 | 0, /*tp_dictoffset*/ |
| 1730 | 0, /*tp_init*/ |
| 1731 | 0, /*tp_alloc*/ |
| 1732 | context_new, /*tp_new*/ |
| 1733 | }; |
| 1734 | |
| 1735 | |
| 1736 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1737 | #ifdef HAVE_OPENSSL_RAND |
| 1738 | |
| 1739 | /* helper routines for seeding the SSL PRNG */ |
| 1740 | static PyObject * |
| 1741 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 1742 | { |
| 1743 | char *buf; |
| 1744 | int len; |
| 1745 | double entropy; |
| 1746 | |
| 1747 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1748 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1749 | RAND_add(buf, len, entropy); |
| 1750 | Py_INCREF(Py_None); |
| 1751 | return Py_None; |
| 1752 | } |
| 1753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1754 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1755 | "RAND_add(string, entropy)\n\ |
| 1756 | \n\ |
| 1757 | 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] | 1758 | bound on the entropy contained in string. See RFC 1750."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1759 | |
| 1760 | static PyObject * |
| 1761 | PySSL_RAND_status(PyObject *self) |
| 1762 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1763 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1767 | "RAND_status() -> 0 or 1\n\ |
| 1768 | \n\ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1769 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 1770 | It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
| 1771 | using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1772 | |
| 1773 | static PyObject * |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 1774 | PySSL_RAND_egd(PyObject *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1775 | { |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 1776 | PyObject *path; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1777 | int bytes; |
| 1778 | |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 1779 | if (!PyArg_ParseTuple(args, "O&|i:RAND_egd", |
| 1780 | PyUnicode_FSConverter, &path)) |
| 1781 | return NULL; |
| 1782 | |
| 1783 | bytes = RAND_egd(PyBytes_AsString(path)); |
| 1784 | Py_DECREF(path); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1785 | if (bytes == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 1786 | PyErr_SetString(PySSLErrorObject, |
| 1787 | "EGD connection failed or EGD did not return " |
| 1788 | "enough data to seed the PRNG"); |
| 1789 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1790 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1791 | return PyLong_FromLong(bytes); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1794 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1795 | "RAND_egd(path) -> bytes\n\ |
| 1796 | \n\ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1797 | Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\ |
| 1798 | Returns number of bytes read. Raises SSLError if connection to EGD\n\ |
| 1799 | fails or if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1800 | |
| 1801 | #endif |
| 1802 | |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 1803 | |
| 1804 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1805 | /* List of functions exported by this module. */ |
| 1806 | |
| 1807 | static PyMethodDef PySSL_methods[] = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1808 | {"_test_decode_cert", PySSL_test_decode_certificate, |
| 1809 | METH_VARARGS}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1810 | #ifdef HAVE_OPENSSL_RAND |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1811 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
| 1812 | PySSL_RAND_add_doc}, |
Victor Stinner | f9faaad | 2010-05-16 21:36:37 +0000 | [diff] [blame] | 1813 | {"RAND_egd", PySSL_RAND_egd, METH_VARARGS, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1814 | PySSL_RAND_egd_doc}, |
| 1815 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 1816 | PySSL_RAND_status_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1817 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1818 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1819 | }; |
| 1820 | |
| 1821 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1822 | #ifdef WITH_THREAD |
| 1823 | |
| 1824 | /* an implementation of OpenSSL threading operations in terms |
| 1825 | of the Python C thread library */ |
| 1826 | |
| 1827 | static PyThread_type_lock *_ssl_locks = NULL; |
| 1828 | |
| 1829 | static unsigned long _ssl_thread_id_function (void) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1830 | return PyThread_get_thread_ident(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1833 | static void _ssl_thread_locking_function |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1834 | (int mode, int n, const char *file, int line) { |
| 1835 | /* this function is needed to perform locking on shared data |
| 1836 | structures. (Note that OpenSSL uses a number of global data |
| 1837 | structures that will be implicitly shared whenever multiple |
| 1838 | threads use OpenSSL.) Multi-threaded applications will |
| 1839 | crash at random if it is not set. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1840 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1841 | locking_function() must be able to handle up to |
| 1842 | CRYPTO_num_locks() different mutex locks. It sets the n-th |
| 1843 | lock if mode & CRYPTO_LOCK, and releases it otherwise. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1844 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1845 | file and line are the file number of the function setting the |
| 1846 | lock. They can be useful for debugging. |
| 1847 | */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1849 | if ((_ssl_locks == NULL) || |
| 1850 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 1851 | return; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1852 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1853 | if (mode & CRYPTO_LOCK) { |
| 1854 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 1855 | } else { |
| 1856 | PyThread_release_lock(_ssl_locks[n]); |
| 1857 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static int _setup_ssl_threads(void) { |
| 1861 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1862 | unsigned int i; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1864 | if (_ssl_locks == NULL) { |
| 1865 | _ssl_locks_count = CRYPTO_num_locks(); |
| 1866 | _ssl_locks = (PyThread_type_lock *) |
| 1867 | malloc(sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 1868 | if (_ssl_locks == NULL) |
| 1869 | return 0; |
| 1870 | memset(_ssl_locks, 0, |
| 1871 | sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 1872 | for (i = 0; i < _ssl_locks_count; i++) { |
| 1873 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 1874 | if (_ssl_locks[i] == NULL) { |
| 1875 | unsigned int j; |
| 1876 | for (j = 0; j < i; j++) { |
| 1877 | PyThread_free_lock(_ssl_locks[j]); |
| 1878 | } |
| 1879 | free(_ssl_locks); |
| 1880 | return 0; |
| 1881 | } |
| 1882 | } |
| 1883 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
| 1884 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
| 1885 | } |
| 1886 | return 1; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1889 | #endif /* def HAVE_THREAD */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1891 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1892 | "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] | 1893 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1894 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1895 | |
| 1896 | static struct PyModuleDef _sslmodule = { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1897 | PyModuleDef_HEAD_INIT, |
| 1898 | "_ssl", |
| 1899 | module_doc, |
| 1900 | -1, |
| 1901 | PySSL_methods, |
| 1902 | NULL, |
| 1903 | NULL, |
| 1904 | NULL, |
| 1905 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1906 | }; |
| 1907 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1908 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1909 | PyInit__ssl(void) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1910 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1911 | PyObject *m, *d, *r; |
| 1912 | unsigned long libver; |
| 1913 | unsigned int major, minor, fix, patch, status; |
| 1914 | PySocketModule_APIObject *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1916 | if (PyType_Ready(&PySSLContext_Type) < 0) |
| 1917 | return NULL; |
| 1918 | if (PyType_Ready(&PySSLSocket_Type) < 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1919 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1920 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1921 | m = PyModule_Create(&_sslmodule); |
| 1922 | if (m == NULL) |
| 1923 | return NULL; |
| 1924 | d = PyModule_GetDict(m); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1926 | /* Load _socket module and its C API */ |
| 1927 | socket_api = PySocketModule_ImportModuleAndAPI(); |
| 1928 | if (!socket_api) |
| 1929 | return NULL; |
| 1930 | PySocketModule = *socket_api; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1932 | /* Init OpenSSL */ |
| 1933 | SSL_load_error_strings(); |
| 1934 | SSL_library_init(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1935 | #ifdef WITH_THREAD |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1936 | /* note that this will start threading if not already started */ |
| 1937 | if (!_setup_ssl_threads()) { |
| 1938 | return NULL; |
| 1939 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1940 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1941 | OpenSSL_add_all_algorithms(); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1942 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1943 | /* Add symbols to module dict */ |
| 1944 | PySSLErrorObject = PyErr_NewException("ssl.SSLError", |
| 1945 | PySocketModule.error, |
| 1946 | NULL); |
| 1947 | if (PySSLErrorObject == NULL) |
| 1948 | return NULL; |
| 1949 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0) |
| 1950 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1951 | if (PyDict_SetItemString(d, "_SSLContext", |
| 1952 | (PyObject *)&PySSLContext_Type) != 0) |
| 1953 | return NULL; |
| 1954 | if (PyDict_SetItemString(d, "_SSLSocket", |
| 1955 | (PyObject *)&PySSLSocket_Type) != 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1956 | return NULL; |
| 1957 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 1958 | PY_SSL_ERROR_ZERO_RETURN); |
| 1959 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 1960 | PY_SSL_ERROR_WANT_READ); |
| 1961 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 1962 | PY_SSL_ERROR_WANT_WRITE); |
| 1963 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 1964 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 1965 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 1966 | PY_SSL_ERROR_SYSCALL); |
| 1967 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 1968 | PY_SSL_ERROR_SSL); |
| 1969 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 1970 | PY_SSL_ERROR_WANT_CONNECT); |
| 1971 | /* non ssl.h errorcodes */ |
| 1972 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 1973 | PY_SSL_ERROR_EOF); |
| 1974 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 1975 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 1976 | /* cert requirements */ |
| 1977 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 1978 | PY_SSL_CERT_NONE); |
| 1979 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 1980 | PY_SSL_CERT_OPTIONAL); |
| 1981 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 1982 | PY_SSL_CERT_REQUIRED); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1983 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1984 | /* protocol versions */ |
| 1985 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 1986 | PY_SSL_VERSION_SSL2); |
| 1987 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 1988 | PY_SSL_VERSION_SSL3); |
| 1989 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 1990 | PY_SSL_VERSION_SSL23); |
| 1991 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 1992 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1994 | /* protocol options */ |
| 1995 | PyModule_AddIntConstant(m, "OP_ALL", SSL_OP_ALL); |
| 1996 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 1997 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 1998 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
| 1999 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2000 | /* OpenSSL version */ |
| 2001 | /* SSLeay() gives us the version of the library linked against, |
| 2002 | which could be different from the headers version. |
| 2003 | */ |
| 2004 | libver = SSLeay(); |
| 2005 | r = PyLong_FromUnsignedLong(libver); |
| 2006 | if (r == NULL) |
| 2007 | return NULL; |
| 2008 | if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 2009 | return NULL; |
| 2010 | status = libver & 0xF; |
| 2011 | libver >>= 4; |
| 2012 | patch = libver & 0xFF; |
| 2013 | libver >>= 8; |
| 2014 | fix = libver & 0xFF; |
| 2015 | libver >>= 8; |
| 2016 | minor = libver & 0xFF; |
| 2017 | libver >>= 8; |
| 2018 | major = libver & 0xFF; |
| 2019 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 2020 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 2021 | return NULL; |
| 2022 | r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 2023 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 2024 | return NULL; |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2026 | return m; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2027 | } |