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