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