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