Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +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. |
| 4 | |
| 5 | This module is imported by socket.py. It should *not* be used |
| 6 | directly. |
| 7 | |
| 8 | */ |
| 9 | |
| 10 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 12 | enum py_ssl_error { |
| 13 | /* these mirror ssl.h */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 14 | PY_SSL_ERROR_NONE, |
| 15 | PY_SSL_ERROR_SSL, |
| 16 | PY_SSL_ERROR_WANT_READ, |
| 17 | PY_SSL_ERROR_WANT_WRITE, |
| 18 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 19 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | PY_SSL_ERROR_ZERO_RETURN, |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 21 | PY_SSL_ERROR_WANT_CONNECT, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 22 | /* start of non ssl.h errorcodes */ |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 23 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 24 | PY_SSL_ERROR_INVALID_ERROR_CODE |
| 25 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 27 | enum py_ssl_server_or_client { |
| 28 | PY_SSL_CLIENT, |
| 29 | PY_SSL_SERVER |
| 30 | }; |
| 31 | |
| 32 | enum py_ssl_cert_requirements { |
| 33 | PY_SSL_CERT_NONE, |
| 34 | PY_SSL_CERT_OPTIONAL, |
| 35 | PY_SSL_CERT_REQUIRED |
| 36 | }; |
| 37 | |
| 38 | enum py_ssl_version { |
| 39 | PY_SSL_VERSION_SSL2, |
| 40 | PY_SSL_VERSION_SSL3, |
| 41 | PY_SSL_VERSION_SSL23, |
| 42 | PY_SSL_VERSION_TLS1, |
| 43 | }; |
| 44 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 45 | /* Include symbols from _socket module */ |
| 46 | #include "socketmodule.h" |
| 47 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 48 | #if defined(HAVE_POLL_H) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | #include <poll.h> |
| 50 | #elif defined(HAVE_SYS_POLL_H) |
| 51 | #include <sys/poll.h> |
| 52 | #endif |
| 53 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 54 | /* Include OpenSSL header files */ |
| 55 | #include "openssl/rsa.h" |
| 56 | #include "openssl/crypto.h" |
| 57 | #include "openssl/x509.h" |
| 58 | #include "openssl/pem.h" |
| 59 | #include "openssl/ssl.h" |
| 60 | #include "openssl/err.h" |
| 61 | #include "openssl/rand.h" |
| 62 | |
| 63 | /* SSL error object */ |
| 64 | static PyObject *PySSLErrorObject; |
| 65 | |
| 66 | /* SSL socket object */ |
| 67 | |
| 68 | #define X509_NAME_MAXLEN 256 |
| 69 | |
| 70 | /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
| 71 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 72 | # define HAVE_OPENSSL_RAND 1 |
| 73 | #else |
| 74 | # undef HAVE_OPENSSL_RAND |
| 75 | #endif |
| 76 | |
| 77 | typedef struct { |
| 78 | PyObject_HEAD |
| 79 | PySocketSockObject *Socket; /* Socket on which we're layered */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 80 | SSL_CTX* ctx; |
| 81 | SSL* ssl; |
| 82 | X509* peer_cert; |
| 83 | char server[X509_NAME_MAXLEN]; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 84 | char issuer[X509_NAME_MAXLEN]; |
| 85 | |
| 86 | } PySSLObject; |
| 87 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 88 | static PyTypeObject PySSL_Type; |
| 89 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args); |
| 90 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 91 | static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 92 | int writing); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 93 | static PyObject *PySSL_peercert(PySSLObject *self); |
| 94 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 95 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 96 | #define PySSLObject_Check(v) (Py_Type(v) == &PySSL_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 97 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 98 | typedef enum { |
| 99 | SOCKET_IS_NONBLOCKING, |
| 100 | SOCKET_IS_BLOCKING, |
| 101 | SOCKET_HAS_TIMED_OUT, |
| 102 | SOCKET_HAS_BEEN_CLOSED, |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 103 | SOCKET_TOO_LARGE_FOR_SELECT, |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 104 | SOCKET_OPERATION_OK |
| 105 | } timeout_state; |
| 106 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 107 | /* Wrap error strings with filename and line # */ |
| 108 | #define STRINGIFY1(x) #x |
| 109 | #define STRINGIFY2(x) STRINGIFY1(x) |
| 110 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
| 111 | #define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) |
| 112 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 113 | /* XXX It might be helpful to augment the error message generated |
| 114 | below with the name of the SSL function that generated the error. |
| 115 | I expect it's obvious most of the time. |
| 116 | */ |
| 117 | |
| 118 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 119 | PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 120 | { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 121 | PyObject *v; |
| 122 | char buf[2048]; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 123 | char *errstr; |
| 124 | int err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 125 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 126 | |
| 127 | assert(ret <= 0); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 128 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 129 | if ((obj != NULL) && (obj->ssl != NULL)) { |
| 130 | err = SSL_get_error(obj->ssl, ret); |
| 131 | |
| 132 | switch (err) { |
| 133 | case SSL_ERROR_ZERO_RETURN: |
| 134 | errstr = "TLS/SSL connection has been closed"; |
| 135 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 136 | break; |
| 137 | case SSL_ERROR_WANT_READ: |
| 138 | errstr = "The operation did not complete (read)"; |
| 139 | p = PY_SSL_ERROR_WANT_READ; |
| 140 | break; |
| 141 | case SSL_ERROR_WANT_WRITE: |
| 142 | p = PY_SSL_ERROR_WANT_WRITE; |
| 143 | errstr = "The operation did not complete (write)"; |
| 144 | break; |
| 145 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 146 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
| 147 | errstr = |
| 148 | "The operation did not complete (X509 lookup)"; |
| 149 | break; |
| 150 | case SSL_ERROR_WANT_CONNECT: |
| 151 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 152 | errstr = "The operation did not complete (connect)"; |
| 153 | break; |
| 154 | case SSL_ERROR_SYSCALL: |
| 155 | { |
| 156 | unsigned long e = ERR_get_error(); |
| 157 | if (e == 0) { |
| 158 | if (ret == 0 || !obj->Socket) { |
| 159 | p = PY_SSL_ERROR_EOF; |
| 160 | errstr = |
| 161 | "EOF occurred in violation of protocol"; |
| 162 | } else if (ret == -1) { |
| 163 | /* underlying BIO reported an I/O error */ |
| 164 | return obj->Socket->errorhandler(); |
| 165 | } else { /* possible? */ |
| 166 | p = PY_SSL_ERROR_SYSCALL; |
| 167 | errstr = "Some I/O error occurred"; |
| 168 | } |
| 169 | } else { |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 170 | p = PY_SSL_ERROR_SYSCALL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 171 | /* XXX Protected by global interpreter lock */ |
| 172 | errstr = ERR_error_string(e, NULL); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 173 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 174 | break; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 175 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 176 | case SSL_ERROR_SSL: |
| 177 | { |
| 178 | unsigned long e = ERR_get_error(); |
| 179 | p = PY_SSL_ERROR_SSL; |
| 180 | if (e != 0) |
| 181 | /* XXX Protected by global interpreter lock */ |
| 182 | errstr = ERR_error_string(e, NULL); |
| 183 | else { /* possible? */ |
| 184 | errstr = |
| 185 | "A failure in the SSL library occurred"; |
| 186 | } |
| 187 | break; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 188 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 189 | default: |
| 190 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 191 | errstr = "Invalid error code"; |
| 192 | } |
| 193 | } else { |
| 194 | errstr = ERR_error_string(ERR_peek_last_error(), NULL); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 195 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 196 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
| 197 | v = Py_BuildValue("(is)", p, buf); |
| 198 | if (v != NULL) { |
| 199 | PyErr_SetObject(PySSLErrorObject, v); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 200 | Py_DECREF(v); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 201 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
| 204 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 205 | static PySSLObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 206 | newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file, |
| 207 | enum py_ssl_server_or_client socket_type, |
| 208 | enum py_ssl_cert_requirements certreq, |
| 209 | enum py_ssl_version proto_version, |
| 210 | char *cacerts_file) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 211 | { |
| 212 | PySSLObject *self; |
| 213 | char *errstr = NULL; |
| 214 | int ret; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 215 | int err; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 216 | int sockstate; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 217 | int verification_mode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 218 | |
| 219 | self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 220 | if (self == NULL) |
| 221 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 222 | memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN); |
| 223 | memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 224 | self->peer_cert = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 225 | self->ssl = NULL; |
| 226 | self->ctx = NULL; |
| 227 | self->Socket = NULL; |
| 228 | |
| 229 | if ((key_file && !cert_file) || (!key_file && cert_file)) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 230 | errstr = ERRSTR("Both the key & certificate files " |
| 231 | "must be specified"); |
| 232 | goto fail; |
| 233 | } |
| 234 | |
| 235 | if ((socket_type == PY_SSL_SERVER) && |
| 236 | ((key_file == NULL) || (cert_file == NULL))) { |
| 237 | errstr = ERRSTR("Both the key & certificate files " |
| 238 | "must be specified for server-side operation"); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 239 | goto fail; |
| 240 | } |
| 241 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 242 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 243 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 244 | self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */ |
| 245 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 246 | self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */ |
| 247 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 248 | self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */ |
| 249 | else |
| 250 | self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 251 | Py_END_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 252 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 253 | if (self->ctx == NULL) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 254 | errstr = ERRSTR("Invalid SSL protocol variant specified."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 255 | goto fail; |
| 256 | } |
| 257 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 258 | if (certreq != PY_SSL_CERT_NONE) { |
| 259 | if (cacerts_file == NULL) { |
| 260 | errstr = ERRSTR("No root certificates specified for " |
| 261 | "verification of other-side certificates."); |
| 262 | goto fail; |
| 263 | } else { |
| 264 | Py_BEGIN_ALLOW_THREADS |
| 265 | ret = SSL_CTX_load_verify_locations(self->ctx, |
| 266 | cacerts_file, |
| 267 | NULL); |
| 268 | Py_END_ALLOW_THREADS |
| 269 | if (ret != 1) { |
| 270 | PySSL_SetError(NULL, 0, __FILE__, __LINE__); |
| 271 | goto fail; |
| 272 | } |
| 273 | } |
| 274 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 275 | if (key_file) { |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 276 | Py_BEGIN_ALLOW_THREADS |
| 277 | ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 278 | SSL_FILETYPE_PEM); |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 279 | Py_END_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 280 | if (ret != 1) { |
| 281 | PySSL_SetError(NULL, 0, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | goto fail; |
| 283 | } |
| 284 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 285 | Py_BEGIN_ALLOW_THREADS |
| 286 | ret = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 287 | cert_file); |
| 288 | Py_END_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 289 | if (ret != 1) { |
| 290 | PySSL_SetError(NULL, 0, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 291 | goto fail; |
| 292 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 293 | /* ssl compatibility */ |
| 294 | SSL_CTX_set_options(self->ctx, SSL_OP_ALL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 297 | verification_mode = SSL_VERIFY_NONE; |
| 298 | if (certreq == PY_SSL_CERT_OPTIONAL) |
| 299 | verification_mode = SSL_VERIFY_PEER; |
| 300 | else if (certreq == PY_SSL_CERT_REQUIRED) |
| 301 | verification_mode = (SSL_VERIFY_PEER | |
| 302 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 303 | SSL_CTX_set_verify(self->ctx, verification_mode, |
| 304 | NULL); /* set verify lvl */ |
| 305 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 306 | Py_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 307 | self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 308 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 309 | SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 310 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 311 | /* 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] | 312 | * to non-blocking mode (blocking is the default) |
| 313 | */ |
| 314 | if (Sock->sock_timeout >= 0.0) { |
| 315 | /* Set both the read and write BIO's to non-blocking mode */ |
| 316 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 317 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 318 | } |
| 319 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 320 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 321 | if (socket_type == PY_SSL_CLIENT) |
| 322 | SSL_set_connect_state(self->ssl); |
| 323 | else |
| 324 | SSL_set_accept_state(self->ssl); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 325 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 326 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 327 | /* Actually negotiate SSL connection */ |
| 328 | /* XXX If SSL_connect() returns 0, it's also a failure. */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 329 | sockstate = 0; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 330 | do { |
| 331 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 332 | if (socket_type == PY_SSL_CLIENT) |
| 333 | ret = SSL_connect(self->ssl); |
| 334 | else |
| 335 | ret = SSL_accept(self->ssl); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 336 | err = SSL_get_error(self->ssl, ret); |
| 337 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 338 | if(PyErr_CheckSignals()) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 339 | goto fail; |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 340 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 341 | if (err == SSL_ERROR_WANT_READ) { |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 342 | sockstate = check_socket_and_wait_for_timeout(Sock, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 343 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 344 | sockstate = check_socket_and_wait_for_timeout(Sock, 1); |
| 345 | } else { |
| 346 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 347 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 348 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 349 | PyErr_SetString(PySSLErrorObject, |
| 350 | ERRSTR("The connect operation timed out")); |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 351 | goto fail; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 352 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 353 | PyErr_SetString(PySSLErrorObject, |
| 354 | ERRSTR("Underlying socket has been closed.")); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 355 | goto fail; |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 356 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 357 | PyErr_SetString(PySSLErrorObject, |
| 358 | ERRSTR("Underlying socket too large for select().")); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 359 | goto fail; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 360 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 361 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 362 | } |
| 363 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 364 | if (ret < 1) { |
| 365 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 366 | goto fail; |
| 367 | } |
| 368 | self->ssl->debug = 1; |
| 369 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 370 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 371 | if ((self->peer_cert = SSL_get_peer_certificate(self->ssl))) { |
| 372 | X509_NAME_oneline(X509_get_subject_name(self->peer_cert), |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 373 | self->server, X509_NAME_MAXLEN); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 374 | X509_NAME_oneline(X509_get_issuer_name(self->peer_cert), |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 375 | self->issuer, X509_NAME_MAXLEN); |
| 376 | } |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 377 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 378 | self->Socket = Sock; |
| 379 | Py_INCREF(self->Socket); |
| 380 | return self; |
| 381 | fail: |
| 382 | if (errstr) |
| 383 | PyErr_SetString(PySSLErrorObject, errstr); |
| 384 | Py_DECREF(self); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 388 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 389 | PySSL_sslwrap(PyObject *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 390 | { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 391 | PySocketSockObject *Sock; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 392 | int server_side = 0; |
| 393 | int verification_mode = PY_SSL_CERT_NONE; |
| 394 | int protocol = PY_SSL_VERSION_SSL23; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 395 | char *key_file = NULL; |
| 396 | char *cert_file = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 397 | char *cacerts_file = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 398 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 399 | if (!PyArg_ParseTuple(args, "O!i|zziiz:sslwrap", |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 400 | PySocketModule.Sock_Type, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 401 | &Sock, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 402 | &server_side, |
| 403 | &key_file, &cert_file, |
| 404 | &verification_mode, &protocol, |
| 405 | &cacerts_file)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 408 | /* |
| 409 | fprintf(stderr, |
| 410 | "server_side is %d, keyfile %p, certfile %p, verify_mode %d, " |
| 411 | "protocol %d, certs %p\n", |
| 412 | server_side, key_file, cert_file, verification_mode, |
| 413 | protocol, cacerts_file); |
| 414 | */ |
| 415 | |
| 416 | return (PyObject *) newPySSLObject(Sock, key_file, cert_file, |
| 417 | server_side, verification_mode, |
| 418 | protocol, cacerts_file); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 421 | PyDoc_STRVAR(ssl_doc, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 422 | "sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n" |
| 423 | " cacertsfile]) -> sslobject"); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 424 | |
| 425 | /* SSL object methods */ |
| 426 | |
| 427 | static PyObject * |
| 428 | PySSL_server(PySSLObject *self) |
| 429 | { |
Neal Norwitz | ae2c876 | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 430 | return PyUnicode_FromString(self->server); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static PyObject * |
| 434 | PySSL_issuer(PySSLObject *self) |
| 435 | { |
Neal Norwitz | ae2c876 | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 436 | return PyUnicode_FromString(self->issuer); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 439 | static PyObject * |
| 440 | _create_dict_for_X509_NAME (X509_NAME *xname) |
| 441 | { |
| 442 | PyObject *pd = PyDict_New(); |
| 443 | int index_counter; |
| 444 | |
| 445 | if (pd == NULL) |
| 446 | return NULL; |
| 447 | |
| 448 | for (index_counter = 0; |
| 449 | index_counter < X509_NAME_entry_count(xname); |
| 450 | index_counter++) |
| 451 | { |
| 452 | char namebuf[X509_NAME_MAXLEN]; |
| 453 | int buflen; |
| 454 | PyObject *name_obj; |
| 455 | ASN1_STRING *value; |
| 456 | PyObject *value_obj; |
| 457 | unsigned char *valuebuf = NULL; |
| 458 | |
| 459 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, |
| 460 | index_counter); |
| 461 | |
| 462 | ASN1_OBJECT *name = X509_NAME_ENTRY_get_object(entry); |
| 463 | buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0); |
| 464 | if (buflen < 0) |
| 465 | goto fail0; |
| 466 | name_obj = PyString_FromStringAndSize(namebuf, buflen); |
| 467 | if (name_obj == NULL) |
| 468 | goto fail0; |
| 469 | |
| 470 | value = X509_NAME_ENTRY_get_data(entry); |
| 471 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 472 | if (buflen < 0) { |
| 473 | Py_DECREF(name_obj); |
| 474 | goto fail0; |
| 475 | } |
| 476 | value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
| 477 | buflen, "strict"); |
| 478 | OPENSSL_free(valuebuf); |
| 479 | if (value_obj == NULL) { |
| 480 | Py_DECREF(name_obj); |
| 481 | goto fail0; |
| 482 | } |
| 483 | if (PyDict_SetItem(pd, name_obj, value_obj) < 0) { |
| 484 | Py_DECREF(name_obj); |
| 485 | Py_DECREF(value_obj); |
| 486 | goto fail0; |
| 487 | } |
| 488 | Py_DECREF(name_obj); |
| 489 | Py_DECREF(value_obj); |
| 490 | } |
| 491 | return pd; |
| 492 | |
| 493 | fail0: |
| 494 | Py_XDECREF(pd); |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | static PyObject * |
| 499 | PySSL_peercert(PySSLObject *self) |
| 500 | { |
| 501 | PyObject *retval = NULL; |
| 502 | BIO *biobuf = NULL; |
| 503 | PyObject *peer; |
| 504 | PyObject *issuer; |
| 505 | PyObject *version; |
| 506 | char buf[2048]; |
| 507 | int len; |
| 508 | ASN1_TIME *notBefore, *notAfter; |
| 509 | PyObject *pnotBefore, *pnotAfter; |
| 510 | int verification; |
| 511 | |
| 512 | if (!self->peer_cert) |
| 513 | Py_RETURN_NONE; |
| 514 | |
| 515 | retval = PyDict_New(); |
| 516 | if (retval == NULL) |
| 517 | return NULL; |
| 518 | |
| 519 | verification = SSL_CTX_get_verify_mode(self->ctx); |
| 520 | if ((verification & SSL_VERIFY_PEER) == 0) |
| 521 | return retval; |
| 522 | |
| 523 | peer = _create_dict_for_X509_NAME( |
| 524 | X509_get_subject_name(self->peer_cert)); |
| 525 | if (peer == NULL) |
| 526 | goto fail0; |
| 527 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 528 | Py_DECREF(peer); |
| 529 | goto fail0; |
| 530 | } |
| 531 | Py_DECREF(peer); |
| 532 | |
| 533 | issuer = _create_dict_for_X509_NAME( |
| 534 | X509_get_issuer_name(self->peer_cert)); |
| 535 | if (issuer == NULL) |
| 536 | goto fail0; |
| 537 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| 538 | Py_DECREF(issuer); |
| 539 | goto fail0; |
| 540 | } |
| 541 | Py_DECREF(issuer); |
| 542 | |
| 543 | version = PyInt_FromLong(X509_get_version(self->peer_cert)); |
| 544 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 545 | Py_DECREF(version); |
| 546 | goto fail0; |
| 547 | } |
| 548 | Py_DECREF(version); |
| 549 | |
| 550 | /* get a memory buffer */ |
| 551 | biobuf = BIO_new(BIO_s_mem()); |
| 552 | |
| 553 | notBefore = X509_get_notBefore(self->peer_cert); |
| 554 | ASN1_TIME_print(biobuf, notBefore); |
| 555 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 556 | pnotBefore = PyString_FromStringAndSize(buf, len); |
| 557 | if (pnotBefore == NULL) |
| 558 | goto fail1; |
| 559 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 560 | Py_DECREF(pnotBefore); |
| 561 | goto fail1; |
| 562 | } |
| 563 | Py_DECREF(pnotBefore); |
| 564 | |
| 565 | (void) BIO_reset(biobuf); |
| 566 | notAfter = X509_get_notAfter(self->peer_cert); |
| 567 | ASN1_TIME_print(biobuf, notAfter); |
| 568 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 569 | BIO_free(biobuf); |
| 570 | pnotAfter = PyString_FromStringAndSize(buf, len); |
| 571 | if (pnotAfter == NULL) |
| 572 | goto fail0; |
| 573 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 574 | Py_DECREF(pnotAfter); |
| 575 | goto fail0; |
| 576 | } |
| 577 | Py_DECREF(pnotAfter); |
| 578 | return retval; |
| 579 | |
| 580 | fail1: |
| 581 | if (biobuf != NULL) |
| 582 | BIO_free(biobuf); |
| 583 | fail0: |
| 584 | Py_XDECREF(retval); |
| 585 | return NULL; |
| 586 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 587 | |
| 588 | static void PySSL_dealloc(PySSLObject *self) |
| 589 | { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 590 | if (self->peer_cert) /* Possible not to have one? */ |
| 591 | X509_free (self->peer_cert); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 592 | if (self->ssl) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 593 | SSL_free(self->ssl); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 594 | if (self->ctx) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 595 | SSL_CTX_free(self->ctx); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 596 | Py_XDECREF(self->Socket); |
| 597 | PyObject_Del(self); |
| 598 | } |
| 599 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 600 | /* 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] | 601 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 602 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 603 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 605 | static int |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 606 | check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 607 | { |
| 608 | fd_set fds; |
| 609 | struct timeval tv; |
| 610 | int rc; |
| 611 | |
| 612 | /* 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] | 613 | if (s->sock_timeout < 0.0) |
| 614 | return SOCKET_IS_BLOCKING; |
| 615 | else if (s->sock_timeout == 0.0) |
| 616 | return SOCKET_IS_NONBLOCKING; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 617 | |
| 618 | /* Guard against closed socket */ |
| 619 | if (s->sock_fd < 0) |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 620 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 621 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 622 | /* Prefer poll, if available, since you can poll() any fd |
| 623 | * which can't be done with select(). */ |
| 624 | #ifdef HAVE_POLL |
| 625 | { |
| 626 | struct pollfd pollfd; |
| 627 | int timeout; |
| 628 | |
| 629 | pollfd.fd = s->sock_fd; |
| 630 | pollfd.events = writing ? POLLOUT : POLLIN; |
| 631 | |
| 632 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 633 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 634 | Py_BEGIN_ALLOW_THREADS |
| 635 | rc = poll(&pollfd, 1, timeout); |
| 636 | Py_END_ALLOW_THREADS |
| 637 | |
| 638 | goto normal_return; |
| 639 | } |
| 640 | #endif |
| 641 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 642 | /* Guard against socket too large for select*/ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 643 | #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 644 | if (s->sock_fd >= FD_SETSIZE) |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 645 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 646 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 648 | /* Construct the arguments to select */ |
| 649 | tv.tv_sec = (int)s->sock_timeout; |
| 650 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 651 | FD_ZERO(&fds); |
| 652 | FD_SET(s->sock_fd, &fds); |
| 653 | |
| 654 | /* See if the socket is ready */ |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 655 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 656 | if (writing) |
| 657 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 658 | else |
| 659 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 660 | Py_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 661 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 662 | normal_return: |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 663 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 664 | (when we are able to write or when there's something to read) */ |
| 665 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 668 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
| 669 | { |
| 670 | char *data; |
| 671 | int len; |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 672 | int count; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 673 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 674 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 675 | |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 676 | if (!PyArg_ParseTuple(args, "s#:write", &data, &count)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 677 | return NULL; |
| 678 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 679 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
| 680 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 681 | PyErr_SetString(PySSLErrorObject, |
| 682 | "The write operation timed out"); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 683 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 684 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 685 | PyErr_SetString(PySSLErrorObject, |
| 686 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 687 | return NULL; |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 688 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 689 | PyErr_SetString(PySSLErrorObject, |
| 690 | "Underlying socket too large for select()."); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 691 | return NULL; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 692 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 693 | do { |
| 694 | err = 0; |
| 695 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 696 | len = SSL_write(self->ssl, data, count); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 697 | err = SSL_get_error(self->ssl, len); |
| 698 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 699 | if(PyErr_CheckSignals()) { |
| 700 | return NULL; |
| 701 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 702 | if (err == SSL_ERROR_WANT_READ) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 703 | sockstate = |
| 704 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 705 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 706 | sockstate = |
| 707 | check_socket_and_wait_for_timeout(self->Socket, 1); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 708 | } else { |
| 709 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 710 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 711 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 712 | PyErr_SetString(PySSLErrorObject, |
| 713 | "The write operation timed out"); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 714 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 715 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 716 | PyErr_SetString(PySSLErrorObject, |
| 717 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 718 | return NULL; |
| 719 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 720 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 721 | } |
| 722 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 723 | if (len > 0) |
| 724 | return PyInt_FromLong(len); |
| 725 | else |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 726 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 729 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 730 | "write(s) -> len\n\ |
| 731 | \n\ |
| 732 | 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] | 733 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 734 | |
| 735 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
| 736 | { |
| 737 | PyObject *buf; |
| 738 | int count = 0; |
| 739 | int len = 1024; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 740 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 741 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 742 | |
| 743 | if (!PyArg_ParseTuple(args, "|i:read", &len)) |
| 744 | return NULL; |
| 745 | |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 746 | if (!(buf = PyBytes_FromStringAndSize((char *) 0, len))) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 747 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 748 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 749 | /* first check if there are bytes ready to be read */ |
| 750 | Py_BEGIN_ALLOW_THREADS |
| 751 | count = SSL_pending(self->ssl); |
| 752 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 753 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 754 | if (!count) { |
| 755 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
| 756 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 757 | PyErr_SetString(PySSLErrorObject, |
| 758 | "The read operation timed out"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 759 | Py_DECREF(buf); |
| 760 | return NULL; |
| 761 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 762 | PyErr_SetString(PySSLErrorObject, |
| 763 | "Underlying socket too large for select()."); |
| 764 | Py_DECREF(buf); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 765 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 766 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 767 | if (SSL_get_shutdown(self->ssl) != |
| 768 | SSL_RECEIVED_SHUTDOWN) |
| 769 | { |
| 770 | Py_DECREF(buf); |
| 771 | PyErr_SetString(PySSLErrorObject, |
| 772 | "Socket closed without SSL shutdown handshake"); |
| 773 | return NULL; |
| 774 | } else { |
| 775 | /* should contain a zero-length string */ |
| 776 | _PyString_Resize(&buf, 0); |
| 777 | return buf; |
| 778 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 779 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 780 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 781 | do { |
| 782 | err = 0; |
| 783 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 784 | count = SSL_read(self->ssl, PyBytes_AS_STRING(buf), len); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 785 | err = SSL_get_error(self->ssl, count); |
| 786 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 787 | if(PyErr_CheckSignals()) { |
| 788 | Py_DECREF(buf); |
| 789 | return NULL; |
| 790 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 791 | if (err == SSL_ERROR_WANT_READ) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 792 | sockstate = |
| 793 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 794 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 795 | sockstate = |
| 796 | check_socket_and_wait_for_timeout(self->Socket, 1); |
| 797 | } else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 798 | (SSL_get_shutdown(self->ssl) == |
| 799 | SSL_RECEIVED_SHUTDOWN)) |
| 800 | { |
| 801 | _PyString_Resize(&buf, 0); |
| 802 | return buf; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 803 | } else { |
| 804 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 805 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 806 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 807 | PyErr_SetString(PySSLErrorObject, |
| 808 | "The read operation timed out"); |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 809 | Py_DECREF(buf); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 810 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 811 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 812 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 813 | } |
| 814 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 815 | if (count <= 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 816 | Py_DECREF(buf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 817 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 818 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 819 | if (count != len) |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 820 | if (PyBytes_Resize(buf, count) < 0) { |
| 821 | Py_DECREF(buf); |
| 822 | return NULL; |
| 823 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 824 | return buf; |
| 825 | } |
| 826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 827 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 828 | "read([len]) -> bytes\n\ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 829 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 830 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 831 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 832 | static PyObject *PySSL_SSLshutdown(PySSLObject *self, PyObject *args) |
| 833 | { |
| 834 | int err; |
| 835 | |
| 836 | /* Guard against closed socket */ |
| 837 | if (self->Socket->sock_fd < 0) { |
| 838 | PyErr_SetString(PySSLErrorObject, |
| 839 | "Underlying socket has been closed."); |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | Py_BEGIN_ALLOW_THREADS |
| 844 | err = SSL_shutdown(self->ssl); |
| 845 | if (err == 0) { |
| 846 | /* we need to call it again to finish the shutdown */ |
| 847 | err = SSL_shutdown(self->ssl); |
| 848 | } |
| 849 | Py_END_ALLOW_THREADS |
| 850 | |
| 851 | if (err < 0) |
| 852 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
| 853 | else { |
| 854 | Py_INCREF(self->Socket); |
| 855 | return (PyObject *) (self->Socket); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 860 | "shutdown(s) -> socket\n\ |
| 861 | \n\ |
| 862 | Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 863 | the underlying socket object."); |
| 864 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 865 | static PyMethodDef PySSLMethods[] = { |
| 866 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 867 | PySSL_SSLwrite_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 868 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 869 | PySSL_SSLread_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 870 | {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
| 871 | {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 872 | {"peer_certificate", (PyCFunction)PySSL_peercert, METH_NOARGS}, |
| 873 | {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 874 | PySSL_SSLshutdown_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 875 | {NULL, NULL} |
| 876 | }; |
| 877 | |
| 878 | static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
| 879 | { |
| 880 | return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
| 881 | } |
| 882 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 883 | static PyTypeObject PySSL_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 884 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 885 | "ssl.SSLContext", /*tp_name*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 886 | sizeof(PySSLObject), /*tp_basicsize*/ |
| 887 | 0, /*tp_itemsize*/ |
| 888 | /* methods */ |
| 889 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 890 | 0, /*tp_print*/ |
| 891 | (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
| 892 | 0, /*tp_setattr*/ |
| 893 | 0, /*tp_compare*/ |
| 894 | 0, /*tp_repr*/ |
| 895 | 0, /*tp_as_number*/ |
| 896 | 0, /*tp_as_sequence*/ |
| 897 | 0, /*tp_as_mapping*/ |
| 898 | 0, /*tp_hash*/ |
| 899 | }; |
| 900 | |
| 901 | #ifdef HAVE_OPENSSL_RAND |
| 902 | |
| 903 | /* helper routines for seeding the SSL PRNG */ |
| 904 | static PyObject * |
| 905 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 906 | { |
| 907 | char *buf; |
| 908 | int len; |
| 909 | double entropy; |
| 910 | |
| 911 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 912 | return NULL; |
| 913 | RAND_add(buf, len, entropy); |
| 914 | Py_INCREF(Py_None); |
| 915 | return Py_None; |
| 916 | } |
| 917 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 918 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 919 | "RAND_add(string, entropy)\n\ |
| 920 | \n\ |
| 921 | Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 922 | bound on the entropy contained in string."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 923 | |
| 924 | static PyObject * |
| 925 | PySSL_RAND_status(PyObject *self) |
| 926 | { |
Neal Norwitz | 1ccfa90 | 2007-08-26 02:26:31 +0000 | [diff] [blame] | 927 | return PyBool_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 930 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 931 | "RAND_status() -> 0 or 1\n\ |
| 932 | \n\ |
Neal Norwitz | 1ccfa90 | 2007-08-26 02:26:31 +0000 | [diff] [blame] | 933 | Returns True if the OpenSSL PRNG has been seeded with enough data and\n\ |
| 934 | False if not. It is necessary to seed the PRNG with RAND_add()\n\ |
| 935 | on some platforms before using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 936 | |
| 937 | static PyObject * |
| 938 | PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 939 | { |
| 940 | int bytes; |
| 941 | |
| 942 | if (!PyString_Check(arg)) |
| 943 | return PyErr_Format(PyExc_TypeError, |
| 944 | "RAND_egd() expected string, found %s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 945 | Py_Type(arg)->tp_name); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 946 | bytes = RAND_egd(PyString_AS_STRING(arg)); |
| 947 | if (bytes == -1) { |
| 948 | PyErr_SetString(PySSLErrorObject, |
| 949 | "EGD connection failed or EGD did not return " |
| 950 | "enough data to seed the PRNG"); |
| 951 | return NULL; |
| 952 | } |
| 953 | return PyInt_FromLong(bytes); |
| 954 | } |
| 955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 956 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 957 | "RAND_egd(path) -> bytes\n\ |
| 958 | \n\ |
| 959 | Queries the entropy gather daemon (EGD) on socket path. Returns number\n\ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 960 | of bytes read. Raises ssl.sslerror if connection to EGD fails or\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 961 | if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 962 | |
| 963 | #endif |
| 964 | |
| 965 | /* List of functions exported by this module. */ |
| 966 | |
| 967 | static PyMethodDef PySSL_methods[] = { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 968 | {"sslwrap", PySSL_sslwrap, |
| 969 | METH_VARARGS, ssl_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 970 | #ifdef HAVE_OPENSSL_RAND |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 971 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 972 | PySSL_RAND_add_doc}, |
| 973 | {"RAND_egd", PySSL_RAND_egd, METH_O, |
| 974 | PySSL_RAND_egd_doc}, |
| 975 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 976 | PySSL_RAND_status_doc}, |
| 977 | #endif |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 978 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 982 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 983 | "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] | 984 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 985 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 986 | PyMODINIT_FUNC |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 987 | init_ssl(void) |
| 988 | { |
| 989 | PyObject *m, *d; |
| 990 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 991 | Py_Type(&PySSL_Type) = &PyType_Type; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 992 | |
| 993 | m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 994 | if (m == NULL) |
| 995 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 996 | d = PyModule_GetDict(m); |
| 997 | |
| 998 | /* Load _socket module and its C API */ |
| 999 | if (PySocketModule_ImportModuleAndAPI()) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1000 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Init OpenSSL */ |
| 1003 | SSL_load_error_strings(); |
| 1004 | SSLeay_add_ssl_algorithms(); |
| 1005 | |
| 1006 | /* Add symbols to module dict */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1007 | PySSLErrorObject = PyErr_NewException("ssl.sslerror", |
| 1008 | PySocketModule.error, |
| 1009 | NULL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1010 | if (PySSLErrorObject == NULL) |
| 1011 | return; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1012 | if (PyDict_SetItemString(d, "sslerror", PySSLErrorObject) != 0) |
| 1013 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1014 | if (PyDict_SetItemString(d, "SSLType", |
| 1015 | (PyObject *)&PySSL_Type) != 0) |
| 1016 | return; |
| 1017 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1018 | PY_SSL_ERROR_ZERO_RETURN); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1019 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1020 | PY_SSL_ERROR_WANT_READ); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1021 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1022 | PY_SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1023 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1024 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1025 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1026 | PY_SSL_ERROR_SYSCALL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1027 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1028 | PY_SSL_ERROR_SSL); |
| 1029 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 1030 | PY_SSL_ERROR_WANT_CONNECT); |
| 1031 | /* non ssl.h errorcodes */ |
| 1032 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 1033 | PY_SSL_ERROR_EOF); |
| 1034 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 1035 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1036 | /* cert requirements */ |
| 1037 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 1038 | PY_SSL_CERT_NONE); |
| 1039 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 1040 | PY_SSL_CERT_OPTIONAL); |
| 1041 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 1042 | PY_SSL_CERT_REQUIRED); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1043 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1044 | /* protocol versions */ |
| 1045 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 1046 | PY_SSL_VERSION_SSL2); |
| 1047 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 1048 | PY_SSL_VERSION_SSL3); |
| 1049 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 1050 | PY_SSL_VERSION_SSL23); |
| 1051 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 1052 | PY_SSL_VERSION_TLS1); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1053 | } |