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