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