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