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