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