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