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