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 * |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 440 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 441 | { |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 442 | PyObject *pt = NULL; |
| 443 | PyObject *entry_tuple = NULL; |
| 444 | int entry_count = X509_NAME_entry_count(xname); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 445 | int index_counter; |
| 446 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 447 | pt = PyTuple_New(entry_count); |
| 448 | if (pt == NULL) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 449 | return NULL; |
| 450 | |
| 451 | for (index_counter = 0; |
| 452 | index_counter < X509_NAME_entry_count(xname); |
| 453 | index_counter++) |
| 454 | { |
| 455 | char namebuf[X509_NAME_MAXLEN]; |
| 456 | int buflen; |
| 457 | PyObject *name_obj; |
| 458 | ASN1_STRING *value; |
| 459 | PyObject *value_obj; |
| 460 | unsigned char *valuebuf = NULL; |
| 461 | |
| 462 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, |
| 463 | index_counter); |
| 464 | |
| 465 | ASN1_OBJECT *name = X509_NAME_ENTRY_get_object(entry); |
| 466 | buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0); |
| 467 | if (buflen < 0) |
| 468 | goto fail0; |
| 469 | name_obj = PyString_FromStringAndSize(namebuf, buflen); |
| 470 | if (name_obj == NULL) |
| 471 | goto fail0; |
| 472 | |
| 473 | value = X509_NAME_ENTRY_get_data(entry); |
| 474 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 475 | if (buflen < 0) { |
| 476 | Py_DECREF(name_obj); |
| 477 | goto fail0; |
| 478 | } |
| 479 | value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
| 480 | buflen, "strict"); |
| 481 | OPENSSL_free(valuebuf); |
| 482 | if (value_obj == NULL) { |
| 483 | Py_DECREF(name_obj); |
| 484 | goto fail0; |
| 485 | } |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 486 | entry_tuple = PyTuple_New(2); |
| 487 | if (entry_tuple == NULL) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 488 | Py_DECREF(name_obj); |
| 489 | Py_DECREF(value_obj); |
| 490 | goto fail0; |
| 491 | } |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 492 | PyTuple_SET_ITEM(entry_tuple, 0, name_obj); |
| 493 | PyTuple_SET_ITEM(entry_tuple, 1, value_obj); |
| 494 | PyTuple_SET_ITEM(pt, index_counter, entry_tuple); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 495 | } |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 496 | return pt; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 497 | |
| 498 | fail0: |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 499 | Py_XDECREF(pt); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | static PyObject * |
| 504 | PySSL_peercert(PySSLObject *self) |
| 505 | { |
| 506 | PyObject *retval = NULL; |
| 507 | BIO *biobuf = NULL; |
| 508 | PyObject *peer; |
| 509 | PyObject *issuer; |
| 510 | PyObject *version; |
| 511 | char buf[2048]; |
| 512 | int len; |
| 513 | ASN1_TIME *notBefore, *notAfter; |
| 514 | PyObject *pnotBefore, *pnotAfter; |
| 515 | int verification; |
| 516 | |
| 517 | if (!self->peer_cert) |
| 518 | Py_RETURN_NONE; |
| 519 | |
| 520 | retval = PyDict_New(); |
| 521 | if (retval == NULL) |
| 522 | return NULL; |
| 523 | |
| 524 | verification = SSL_CTX_get_verify_mode(self->ctx); |
| 525 | if ((verification & SSL_VERIFY_PEER) == 0) |
| 526 | return retval; |
| 527 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 528 | peer = _create_tuple_for_X509_NAME( |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 529 | X509_get_subject_name(self->peer_cert)); |
| 530 | if (peer == NULL) |
| 531 | goto fail0; |
| 532 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 533 | Py_DECREF(peer); |
| 534 | goto fail0; |
| 535 | } |
| 536 | Py_DECREF(peer); |
| 537 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 538 | issuer = _create_tuple_for_X509_NAME( |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 539 | X509_get_issuer_name(self->peer_cert)); |
| 540 | if (issuer == NULL) |
| 541 | goto fail0; |
| 542 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| 543 | Py_DECREF(issuer); |
| 544 | goto fail0; |
| 545 | } |
| 546 | Py_DECREF(issuer); |
| 547 | |
| 548 | version = PyInt_FromLong(X509_get_version(self->peer_cert)); |
| 549 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 550 | Py_DECREF(version); |
| 551 | goto fail0; |
| 552 | } |
| 553 | Py_DECREF(version); |
| 554 | |
| 555 | /* get a memory buffer */ |
| 556 | biobuf = BIO_new(BIO_s_mem()); |
| 557 | |
| 558 | notBefore = X509_get_notBefore(self->peer_cert); |
| 559 | ASN1_TIME_print(biobuf, notBefore); |
| 560 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 561 | pnotBefore = PyString_FromStringAndSize(buf, len); |
| 562 | if (pnotBefore == NULL) |
| 563 | goto fail1; |
| 564 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 565 | Py_DECREF(pnotBefore); |
| 566 | goto fail1; |
| 567 | } |
| 568 | Py_DECREF(pnotBefore); |
| 569 | |
| 570 | (void) BIO_reset(biobuf); |
| 571 | notAfter = X509_get_notAfter(self->peer_cert); |
| 572 | ASN1_TIME_print(biobuf, notAfter); |
| 573 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 574 | BIO_free(biobuf); |
| 575 | pnotAfter = PyString_FromStringAndSize(buf, len); |
| 576 | if (pnotAfter == NULL) |
| 577 | goto fail0; |
| 578 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 579 | Py_DECREF(pnotAfter); |
| 580 | goto fail0; |
| 581 | } |
| 582 | Py_DECREF(pnotAfter); |
| 583 | return retval; |
| 584 | |
| 585 | fail1: |
| 586 | if (biobuf != NULL) |
| 587 | BIO_free(biobuf); |
| 588 | fail0: |
| 589 | Py_XDECREF(retval); |
| 590 | return NULL; |
| 591 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 592 | |
| 593 | static void PySSL_dealloc(PySSLObject *self) |
| 594 | { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 595 | if (self->peer_cert) /* Possible not to have one? */ |
| 596 | X509_free (self->peer_cert); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 597 | if (self->ssl) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 598 | SSL_free(self->ssl); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 599 | if (self->ctx) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 600 | SSL_CTX_free(self->ctx); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 601 | Py_XDECREF(self->Socket); |
| 602 | PyObject_Del(self); |
| 603 | } |
| 604 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 605 | /* 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] | 606 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 607 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 608 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 609 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 610 | static int |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 611 | check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 612 | { |
| 613 | fd_set fds; |
| 614 | struct timeval tv; |
| 615 | int rc; |
| 616 | |
| 617 | /* 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] | 618 | if (s->sock_timeout < 0.0) |
| 619 | return SOCKET_IS_BLOCKING; |
| 620 | else if (s->sock_timeout == 0.0) |
| 621 | return SOCKET_IS_NONBLOCKING; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 622 | |
| 623 | /* Guard against closed socket */ |
| 624 | if (s->sock_fd < 0) |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 625 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 626 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 627 | /* Prefer poll, if available, since you can poll() any fd |
| 628 | * which can't be done with select(). */ |
| 629 | #ifdef HAVE_POLL |
| 630 | { |
| 631 | struct pollfd pollfd; |
| 632 | int timeout; |
| 633 | |
| 634 | pollfd.fd = s->sock_fd; |
| 635 | pollfd.events = writing ? POLLOUT : POLLIN; |
| 636 | |
| 637 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 638 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 639 | Py_BEGIN_ALLOW_THREADS |
| 640 | rc = poll(&pollfd, 1, timeout); |
| 641 | Py_END_ALLOW_THREADS |
| 642 | |
| 643 | goto normal_return; |
| 644 | } |
| 645 | #endif |
| 646 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 647 | /* Guard against socket too large for select*/ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 648 | #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 649 | if (s->sock_fd >= FD_SETSIZE) |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 650 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 651 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 653 | /* Construct the arguments to select */ |
| 654 | tv.tv_sec = (int)s->sock_timeout; |
| 655 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 656 | FD_ZERO(&fds); |
| 657 | FD_SET(s->sock_fd, &fds); |
| 658 | |
| 659 | /* See if the socket is ready */ |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 660 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 661 | if (writing) |
| 662 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 663 | else |
| 664 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 665 | Py_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 666 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 667 | normal_return: |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 668 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 669 | (when we are able to write or when there's something to read) */ |
| 670 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 673 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
| 674 | { |
| 675 | char *data; |
| 676 | int len; |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 677 | int count; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 678 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 679 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 680 | |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 681 | if (!PyArg_ParseTuple(args, "s#:write", &data, &count)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 682 | return NULL; |
| 683 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 684 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
| 685 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 686 | PyErr_SetString(PySSLErrorObject, |
| 687 | "The write operation timed out"); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 688 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 689 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 690 | PyErr_SetString(PySSLErrorObject, |
| 691 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 692 | return NULL; |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 693 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 694 | PyErr_SetString(PySSLErrorObject, |
| 695 | "Underlying socket too large for select()."); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 696 | return NULL; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 697 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 698 | do { |
| 699 | err = 0; |
| 700 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 701 | len = SSL_write(self->ssl, data, count); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 702 | err = SSL_get_error(self->ssl, len); |
| 703 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 704 | if(PyErr_CheckSignals()) { |
| 705 | return NULL; |
| 706 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 707 | if (err == SSL_ERROR_WANT_READ) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 708 | sockstate = |
| 709 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 710 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 711 | sockstate = |
| 712 | check_socket_and_wait_for_timeout(self->Socket, 1); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 713 | } else { |
| 714 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 715 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 716 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 717 | PyErr_SetString(PySSLErrorObject, |
| 718 | "The write operation timed out"); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 719 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 720 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 721 | PyErr_SetString(PySSLErrorObject, |
| 722 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 723 | return NULL; |
| 724 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 725 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 726 | } |
| 727 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 728 | if (len > 0) |
| 729 | return PyInt_FromLong(len); |
| 730 | else |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 731 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 734 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 735 | "write(s) -> len\n\ |
| 736 | \n\ |
| 737 | 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] | 738 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 739 | |
| 740 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
| 741 | { |
| 742 | PyObject *buf; |
| 743 | int count = 0; |
| 744 | int len = 1024; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 745 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 746 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 747 | |
| 748 | if (!PyArg_ParseTuple(args, "|i:read", &len)) |
| 749 | return NULL; |
| 750 | |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 751 | if (!(buf = PyBytes_FromStringAndSize((char *) 0, len))) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 752 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 753 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 754 | /* first check if there are bytes ready to be read */ |
| 755 | Py_BEGIN_ALLOW_THREADS |
| 756 | count = SSL_pending(self->ssl); |
| 757 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 758 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 759 | if (!count) { |
| 760 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
| 761 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 762 | PyErr_SetString(PySSLErrorObject, |
| 763 | "The read operation timed out"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 764 | Py_DECREF(buf); |
| 765 | return NULL; |
| 766 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 767 | PyErr_SetString(PySSLErrorObject, |
| 768 | "Underlying socket too large for select()."); |
| 769 | Py_DECREF(buf); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 770 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 771 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 772 | if (SSL_get_shutdown(self->ssl) != |
| 773 | SSL_RECEIVED_SHUTDOWN) |
| 774 | { |
| 775 | Py_DECREF(buf); |
| 776 | PyErr_SetString(PySSLErrorObject, |
| 777 | "Socket closed without SSL shutdown handshake"); |
| 778 | return NULL; |
| 779 | } else { |
| 780 | /* should contain a zero-length string */ |
| 781 | _PyString_Resize(&buf, 0); |
| 782 | return buf; |
| 783 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 784 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 785 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 786 | do { |
| 787 | err = 0; |
| 788 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 789 | count = SSL_read(self->ssl, PyBytes_AS_STRING(buf), len); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 790 | err = SSL_get_error(self->ssl, count); |
| 791 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 792 | if(PyErr_CheckSignals()) { |
| 793 | Py_DECREF(buf); |
| 794 | return NULL; |
| 795 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 796 | if (err == SSL_ERROR_WANT_READ) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 797 | sockstate = |
| 798 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 799 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 800 | sockstate = |
| 801 | check_socket_and_wait_for_timeout(self->Socket, 1); |
| 802 | } else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 803 | (SSL_get_shutdown(self->ssl) == |
| 804 | SSL_RECEIVED_SHUTDOWN)) |
| 805 | { |
| 806 | _PyString_Resize(&buf, 0); |
| 807 | return buf; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 808 | } else { |
| 809 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 810 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 811 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 812 | PyErr_SetString(PySSLErrorObject, |
| 813 | "The read operation timed out"); |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 814 | Py_DECREF(buf); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 815 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 816 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 817 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 818 | } |
| 819 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 820 | if (count <= 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 821 | Py_DECREF(buf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 822 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 823 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 824 | if (count != len) |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 825 | if (PyBytes_Resize(buf, count) < 0) { |
| 826 | Py_DECREF(buf); |
| 827 | return NULL; |
| 828 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 829 | return buf; |
| 830 | } |
| 831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 832 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Jeremy Hylton | 6252083 | 2007-08-04 02:58:42 +0000 | [diff] [blame] | 833 | "read([len]) -> bytes\n\ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 834 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 835 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 836 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 837 | static PyObject *PySSL_SSLshutdown(PySSLObject *self, PyObject *args) |
| 838 | { |
| 839 | int err; |
| 840 | |
| 841 | /* Guard against closed socket */ |
| 842 | if (self->Socket->sock_fd < 0) { |
| 843 | PyErr_SetString(PySSLErrorObject, |
| 844 | "Underlying socket has been closed."); |
| 845 | return NULL; |
| 846 | } |
| 847 | |
| 848 | Py_BEGIN_ALLOW_THREADS |
| 849 | err = SSL_shutdown(self->ssl); |
| 850 | if (err == 0) { |
| 851 | /* we need to call it again to finish the shutdown */ |
| 852 | err = SSL_shutdown(self->ssl); |
| 853 | } |
| 854 | Py_END_ALLOW_THREADS |
| 855 | |
| 856 | if (err < 0) |
| 857 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
| 858 | else { |
| 859 | Py_INCREF(self->Socket); |
| 860 | return (PyObject *) (self->Socket); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 865 | "shutdown(s) -> socket\n\ |
| 866 | \n\ |
| 867 | Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 868 | the underlying socket object."); |
| 869 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 870 | static PyMethodDef PySSLMethods[] = { |
| 871 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 872 | PySSL_SSLwrite_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 873 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 874 | PySSL_SSLread_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 875 | {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
| 876 | {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 877 | {"peer_certificate", (PyCFunction)PySSL_peercert, METH_NOARGS}, |
| 878 | {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 879 | PySSL_SSLshutdown_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 880 | {NULL, NULL} |
| 881 | }; |
| 882 | |
| 883 | static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
| 884 | { |
| 885 | return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
| 886 | } |
| 887 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 888 | static PyTypeObject PySSL_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 889 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 890 | "ssl.SSLContext", /*tp_name*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 891 | sizeof(PySSLObject), /*tp_basicsize*/ |
| 892 | 0, /*tp_itemsize*/ |
| 893 | /* methods */ |
| 894 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 895 | 0, /*tp_print*/ |
| 896 | (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
| 897 | 0, /*tp_setattr*/ |
| 898 | 0, /*tp_compare*/ |
| 899 | 0, /*tp_repr*/ |
| 900 | 0, /*tp_as_number*/ |
| 901 | 0, /*tp_as_sequence*/ |
| 902 | 0, /*tp_as_mapping*/ |
| 903 | 0, /*tp_hash*/ |
| 904 | }; |
| 905 | |
| 906 | #ifdef HAVE_OPENSSL_RAND |
| 907 | |
| 908 | /* helper routines for seeding the SSL PRNG */ |
| 909 | static PyObject * |
| 910 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 911 | { |
| 912 | char *buf; |
| 913 | int len; |
| 914 | double entropy; |
| 915 | |
| 916 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 917 | return NULL; |
| 918 | RAND_add(buf, len, entropy); |
| 919 | Py_INCREF(Py_None); |
| 920 | return Py_None; |
| 921 | } |
| 922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 923 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 924 | "RAND_add(string, entropy)\n\ |
| 925 | \n\ |
| 926 | 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] | 927 | bound on the entropy contained in string."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 928 | |
| 929 | static PyObject * |
| 930 | PySSL_RAND_status(PyObject *self) |
| 931 | { |
Neal Norwitz | 1ccfa90 | 2007-08-26 02:26:31 +0000 | [diff] [blame] | 932 | return PyBool_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 935 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 936 | "RAND_status() -> 0 or 1\n\ |
| 937 | \n\ |
Neal Norwitz | 1ccfa90 | 2007-08-26 02:26:31 +0000 | [diff] [blame] | 938 | Returns True if the OpenSSL PRNG has been seeded with enough data and\n\ |
| 939 | False if not. It is necessary to seed the PRNG with RAND_add()\n\ |
| 940 | on some platforms before using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 941 | |
| 942 | static PyObject * |
| 943 | PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 944 | { |
| 945 | int bytes; |
| 946 | |
| 947 | if (!PyString_Check(arg)) |
| 948 | return PyErr_Format(PyExc_TypeError, |
| 949 | "RAND_egd() expected string, found %s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 950 | Py_Type(arg)->tp_name); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 951 | bytes = RAND_egd(PyString_AS_STRING(arg)); |
| 952 | if (bytes == -1) { |
| 953 | PyErr_SetString(PySSLErrorObject, |
| 954 | "EGD connection failed or EGD did not return " |
| 955 | "enough data to seed the PRNG"); |
| 956 | return NULL; |
| 957 | } |
| 958 | return PyInt_FromLong(bytes); |
| 959 | } |
| 960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 961 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 962 | "RAND_egd(path) -> bytes\n\ |
| 963 | \n\ |
| 964 | Queries the entropy gather daemon (EGD) on socket path. Returns number\n\ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 965 | 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] | 966 | if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 967 | |
| 968 | #endif |
| 969 | |
| 970 | /* List of functions exported by this module. */ |
| 971 | |
| 972 | static PyMethodDef PySSL_methods[] = { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 973 | {"sslwrap", PySSL_sslwrap, |
| 974 | METH_VARARGS, ssl_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 975 | #ifdef HAVE_OPENSSL_RAND |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 976 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 977 | PySSL_RAND_add_doc}, |
| 978 | {"RAND_egd", PySSL_RAND_egd, METH_O, |
| 979 | PySSL_RAND_egd_doc}, |
| 980 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 981 | PySSL_RAND_status_doc}, |
| 982 | #endif |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 983 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 984 | }; |
| 985 | |
| 986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 987 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 988 | "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] | 989 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 990 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 991 | PyMODINIT_FUNC |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 992 | init_ssl(void) |
| 993 | { |
| 994 | PyObject *m, *d; |
| 995 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 996 | Py_Type(&PySSL_Type) = &PyType_Type; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 997 | |
| 998 | m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 999 | if (m == NULL) |
| 1000 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1001 | d = PyModule_GetDict(m); |
| 1002 | |
| 1003 | /* Load _socket module and its C API */ |
| 1004 | if (PySocketModule_ImportModuleAndAPI()) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1005 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* Init OpenSSL */ |
| 1008 | SSL_load_error_strings(); |
| 1009 | SSLeay_add_ssl_algorithms(); |
| 1010 | |
| 1011 | /* Add symbols to module dict */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1012 | PySSLErrorObject = PyErr_NewException("ssl.sslerror", |
| 1013 | PySocketModule.error, |
| 1014 | NULL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1015 | if (PySSLErrorObject == NULL) |
| 1016 | return; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1017 | if (PyDict_SetItemString(d, "sslerror", PySSLErrorObject) != 0) |
| 1018 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1019 | if (PyDict_SetItemString(d, "SSLType", |
| 1020 | (PyObject *)&PySSL_Type) != 0) |
| 1021 | return; |
| 1022 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1023 | PY_SSL_ERROR_ZERO_RETURN); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1024 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1025 | PY_SSL_ERROR_WANT_READ); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1026 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1027 | PY_SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1028 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1029 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1030 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1031 | PY_SSL_ERROR_SYSCALL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1032 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1033 | PY_SSL_ERROR_SSL); |
| 1034 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 1035 | PY_SSL_ERROR_WANT_CONNECT); |
| 1036 | /* non ssl.h errorcodes */ |
| 1037 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 1038 | PY_SSL_ERROR_EOF); |
| 1039 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 1040 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1041 | /* cert requirements */ |
| 1042 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 1043 | PY_SSL_CERT_NONE); |
| 1044 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 1045 | PY_SSL_CERT_OPTIONAL); |
| 1046 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 1047 | PY_SSL_CERT_REQUIRED); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1048 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1049 | /* protocol versions */ |
| 1050 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 1051 | PY_SSL_VERSION_SSL2); |
| 1052 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 1053 | PY_SSL_VERSION_SSL3); |
| 1054 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 1055 | PY_SSL_VERSION_SSL23); |
| 1056 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 1057 | PY_SSL_VERSION_TLS1); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1058 | } |