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