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