Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1 | /* SSL socket module |
| 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" |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 11 | enum py_ssl_error { |
| 12 | /* these mirror ssl.h */ |
| 13 | PY_SSL_ERROR_NONE, |
| 14 | PY_SSL_ERROR_SSL, |
| 15 | PY_SSL_ERROR_WANT_READ, |
| 16 | PY_SSL_ERROR_WANT_WRITE, |
| 17 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 18 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 19 | PY_SSL_ERROR_ZERO_RETURN, |
| 20 | PY_SSL_ERROR_WANT_CONNECT, |
| 21 | /* start of non ssl.h errorcodes */ |
| 22 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 23 | PY_SSL_ERROR_INVALID_ERROR_CODE |
| 24 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 25 | |
| 26 | /* Include symbols from _socket module */ |
| 27 | #include "socketmodule.h" |
| 28 | |
| 29 | /* Include OpenSSL header files */ |
| 30 | #include "openssl/rsa.h" |
| 31 | #include "openssl/crypto.h" |
| 32 | #include "openssl/x509.h" |
| 33 | #include "openssl/pem.h" |
| 34 | #include "openssl/ssl.h" |
| 35 | #include "openssl/err.h" |
| 36 | #include "openssl/rand.h" |
| 37 | |
| 38 | /* SSL error object */ |
| 39 | static PyObject *PySSLErrorObject; |
| 40 | |
| 41 | /* SSL socket object */ |
| 42 | |
| 43 | #define X509_NAME_MAXLEN 256 |
| 44 | |
| 45 | /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
| 46 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 47 | # define HAVE_OPENSSL_RAND 1 |
| 48 | #else |
| 49 | # undef HAVE_OPENSSL_RAND |
| 50 | #endif |
| 51 | |
| 52 | typedef struct { |
| 53 | PyObject_HEAD |
| 54 | PySocketSockObject *Socket; /* Socket on which we're layered */ |
| 55 | SSL_CTX* ctx; |
| 56 | SSL* ssl; |
| 57 | X509* server_cert; |
| 58 | BIO* sbio; |
| 59 | char server[X509_NAME_MAXLEN]; |
| 60 | char issuer[X509_NAME_MAXLEN]; |
| 61 | |
| 62 | } PySSLObject; |
| 63 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 64 | static PyTypeObject PySSL_Type; |
| 65 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args); |
| 66 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args); |
Neal Norwitz | 529baf2 | 2003-02-02 17:08:33 +0000 | [diff] [blame] | 67 | static int wait_for_timeout(PySocketSockObject *s, int writing); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 68 | |
| 69 | #define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type) |
| 70 | |
| 71 | /* XXX It might be helpful to augment the error message generated |
| 72 | below with the name of the SSL function that generated the error. |
| 73 | I expect it's obvious most of the time. |
| 74 | */ |
| 75 | |
| 76 | static PyObject * |
| 77 | PySSL_SetError(PySSLObject *obj, int ret) |
| 78 | { |
| 79 | PyObject *v, *n, *s; |
| 80 | char *errstr; |
| 81 | int err; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 82 | enum py_ssl_error p; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 83 | |
| 84 | assert(ret <= 0); |
| 85 | |
| 86 | err = SSL_get_error(obj->ssl, ret); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 87 | |
| 88 | switch (err) { |
| 89 | case SSL_ERROR_ZERO_RETURN: |
| 90 | errstr = "TLS/SSL connection has been closed"; |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 91 | p = PY_SSL_ERROR_ZERO_RETURN; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 92 | break; |
| 93 | case SSL_ERROR_WANT_READ: |
| 94 | errstr = "The operation did not complete (read)"; |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 95 | p = PY_SSL_ERROR_WANT_READ; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 96 | break; |
| 97 | case SSL_ERROR_WANT_WRITE: |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 98 | p = PY_SSL_ERROR_WANT_WRITE; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 99 | errstr = "The operation did not complete (write)"; |
| 100 | break; |
| 101 | case SSL_ERROR_WANT_X509_LOOKUP: |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 102 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 103 | errstr = "The operation did not complete (X509 lookup)"; |
| 104 | break; |
| 105 | case SSL_ERROR_WANT_CONNECT: |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 106 | p = PY_SSL_ERROR_WANT_CONNECT; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 107 | errstr = "The operation did not complete (connect)"; |
| 108 | break; |
| 109 | case SSL_ERROR_SYSCALL: |
| 110 | { |
| 111 | unsigned long e = ERR_get_error(); |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 112 | if (e == 0) { |
Neal Norwitz | a9002f8 | 2003-06-30 03:25:20 +0000 | [diff] [blame] | 113 | if (ret == 0 || !obj->Socket) { |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 114 | p = PY_SSL_ERROR_EOF; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 115 | errstr = "EOF occurred in violation of protocol"; |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 116 | } else if (ret == -1) { |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 117 | /* the underlying BIO reported an I/O error */ |
| 118 | return obj->Socket->errorhandler(); |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 119 | } else { /* possible? */ |
| 120 | p = PY_SSL_ERROR_SYSCALL; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 121 | errstr = "Some I/O error occurred"; |
| 122 | } |
| 123 | } else { |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 124 | p = PY_SSL_ERROR_SYSCALL; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 125 | /* XXX Protected by global interpreter lock */ |
| 126 | errstr = ERR_error_string(e, NULL); |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | case SSL_ERROR_SSL: |
| 131 | { |
| 132 | unsigned long e = ERR_get_error(); |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 133 | p = PY_SSL_ERROR_SSL; |
| 134 | if (e != 0) |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 135 | /* XXX Protected by global interpreter lock */ |
| 136 | errstr = ERR_error_string(e, NULL); |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 137 | else { /* possible? */ |
| 138 | errstr = "A failure in the SSL library occurred"; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | } |
| 142 | default: |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 143 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 144 | errstr = "Invalid error code"; |
| 145 | } |
| 146 | n = PyInt_FromLong((long) p); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 147 | if (n == NULL) |
| 148 | return NULL; |
| 149 | v = PyTuple_New(2); |
| 150 | if (v == NULL) { |
| 151 | Py_DECREF(n); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 155 | s = PyString_FromString(errstr); |
| 156 | if (s == NULL) { |
| 157 | Py_DECREF(v); |
| 158 | Py_DECREF(n); |
| 159 | } |
| 160 | PyTuple_SET_ITEM(v, 0, n); |
| 161 | PyTuple_SET_ITEM(v, 1, s); |
| 162 | PyErr_SetObject(PySSLErrorObject, v); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 166 | static PySSLObject * |
| 167 | newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file) |
| 168 | { |
| 169 | PySSLObject *self; |
| 170 | char *errstr = NULL; |
| 171 | int ret; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 172 | int err; |
| 173 | int timedout; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 174 | |
| 175 | self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */ |
| 176 | if (self == NULL){ |
| 177 | errstr = "newPySSLObject error"; |
| 178 | goto fail; |
| 179 | } |
| 180 | memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN); |
| 181 | memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN); |
| 182 | self->server_cert = NULL; |
| 183 | self->ssl = NULL; |
| 184 | self->ctx = NULL; |
| 185 | self->Socket = NULL; |
| 186 | |
| 187 | if ((key_file && !cert_file) || (!key_file && cert_file)) { |
| 188 | errstr = "Both the key & certificate files must be specified"; |
| 189 | goto fail; |
| 190 | } |
| 191 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 192 | Py_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 193 | self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 194 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 195 | if (self->ctx == NULL) { |
| 196 | errstr = "SSL_CTX_new error"; |
| 197 | goto fail; |
| 198 | } |
| 199 | |
| 200 | if (key_file) { |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 201 | Py_BEGIN_ALLOW_THREADS |
| 202 | ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
| 203 | SSL_FILETYPE_PEM); |
| 204 | Py_END_ALLOW_THREADS |
| 205 | if (ret < 1) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 206 | errstr = "SSL_CTX_use_PrivateKey_file error"; |
| 207 | goto fail; |
| 208 | } |
| 209 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 210 | Py_BEGIN_ALLOW_THREADS |
| 211 | ret = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 212 | cert_file); |
| 213 | Py_END_ALLOW_THREADS |
| 214 | if (ret < 1) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 215 | errstr = "SSL_CTX_use_certificate_chain_file error"; |
| 216 | goto fail; |
| 217 | } |
| 218 | } |
| 219 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 220 | Py_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 221 | SSL_CTX_set_verify(self->ctx, |
| 222 | SSL_VERIFY_NONE, NULL); /* set verify lvl */ |
| 223 | self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 224 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 225 | 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] | 226 | |
| 227 | /* If the socket is is non-blocking mode or timeout mode, set the BIO |
| 228 | * to non-blocking mode (blocking is the default) |
| 229 | */ |
| 230 | if (Sock->sock_timeout >= 0.0) { |
| 231 | /* Set both the read and write BIO's to non-blocking mode */ |
| 232 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 233 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 234 | } |
| 235 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 236 | Py_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 237 | SSL_set_connect_state(self->ssl); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 238 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 239 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 240 | /* Actually negotiate SSL connection */ |
| 241 | /* XXX If SSL_connect() returns 0, it's also a failure. */ |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 242 | timedout = 0; |
| 243 | do { |
| 244 | Py_BEGIN_ALLOW_THREADS |
| 245 | ret = SSL_connect(self->ssl); |
| 246 | err = SSL_get_error(self->ssl, ret); |
| 247 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 248 | if(PyErr_CheckSignals()) { |
| 249 | goto fail; |
| 250 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 251 | if (err == SSL_ERROR_WANT_READ) { |
| 252 | timedout = wait_for_timeout(Sock, 0); |
| 253 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 254 | timedout = wait_for_timeout(Sock, 1); |
| 255 | } |
| 256 | if (timedout) { |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 257 | errstr = "The connect operation timed out"; |
| 258 | goto fail; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 259 | } |
| 260 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 261 | if (ret <= 0) { |
| 262 | PySSL_SetError(self, ret); |
| 263 | goto fail; |
| 264 | } |
| 265 | self->ssl->debug = 1; |
| 266 | |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 267 | Py_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) { |
| 269 | X509_NAME_oneline(X509_get_subject_name(self->server_cert), |
| 270 | self->server, X509_NAME_MAXLEN); |
| 271 | X509_NAME_oneline(X509_get_issuer_name(self->server_cert), |
| 272 | self->issuer, X509_NAME_MAXLEN); |
| 273 | } |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 274 | Py_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 275 | self->Socket = Sock; |
| 276 | Py_INCREF(self->Socket); |
| 277 | return self; |
| 278 | fail: |
| 279 | if (errstr) |
| 280 | PyErr_SetString(PySSLErrorObject, errstr); |
| 281 | Py_DECREF(self); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 285 | static PyObject * |
| 286 | PySocket_ssl(PyObject *self, PyObject *args) |
| 287 | { |
| 288 | PySSLObject *rv; |
| 289 | PySocketSockObject *Sock; |
| 290 | char *key_file = NULL; |
| 291 | char *cert_file = NULL; |
| 292 | |
| 293 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
| 294 | PySocketModule.Sock_Type, |
| 295 | (PyObject*)&Sock, |
| 296 | &key_file, &cert_file)) |
| 297 | return NULL; |
| 298 | |
| 299 | rv = newPySSLObject(Sock, key_file, cert_file); |
| 300 | if (rv == NULL) |
| 301 | return NULL; |
| 302 | return (PyObject *)rv; |
| 303 | } |
| 304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 305 | PyDoc_STRVAR(ssl_doc, |
| 306 | "ssl(socket, [keyfile, certfile]) -> sslobject"); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 307 | |
| 308 | /* SSL object methods */ |
| 309 | |
| 310 | static PyObject * |
| 311 | PySSL_server(PySSLObject *self) |
| 312 | { |
| 313 | return PyString_FromString(self->server); |
| 314 | } |
| 315 | |
| 316 | static PyObject * |
| 317 | PySSL_issuer(PySSLObject *self) |
| 318 | { |
| 319 | return PyString_FromString(self->issuer); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | static void PySSL_dealloc(PySSLObject *self) |
| 324 | { |
| 325 | if (self->server_cert) /* Possible not to have one? */ |
| 326 | X509_free (self->server_cert); |
| 327 | if (self->ssl) |
| 328 | SSL_free(self->ssl); |
| 329 | if (self->ctx) |
| 330 | SSL_CTX_free(self->ctx); |
| 331 | Py_XDECREF(self->Socket); |
| 332 | PyObject_Del(self); |
| 333 | } |
| 334 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 335 | /* If the socket has a timeout, do a select() on the socket. |
| 336 | The argument writing indicates the direction. |
| 337 | Return non-zero if the socket timed out, zero otherwise. |
| 338 | */ |
| 339 | static int |
| 340 | wait_for_timeout(PySocketSockObject *s, int writing) |
| 341 | { |
| 342 | fd_set fds; |
| 343 | struct timeval tv; |
| 344 | int rc; |
| 345 | |
| 346 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 347 | if (s->sock_timeout <= 0.0) |
| 348 | return 0; |
| 349 | |
| 350 | /* Guard against closed socket */ |
| 351 | if (s->sock_fd < 0) |
| 352 | return 0; |
| 353 | |
| 354 | /* Construct the arguments to select */ |
| 355 | tv.tv_sec = (int)s->sock_timeout; |
| 356 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 357 | FD_ZERO(&fds); |
| 358 | FD_SET(s->sock_fd, &fds); |
| 359 | |
| 360 | /* See if the socket is ready */ |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 361 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 362 | if (writing) |
| 363 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 364 | else |
| 365 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 366 | Py_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 367 | |
| 368 | /* Return 1 on timeout, 0 otherwise */ |
| 369 | return rc == 0; |
| 370 | } |
| 371 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 372 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
| 373 | { |
| 374 | char *data; |
| 375 | int len; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 376 | int timedout; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 377 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 378 | |
| 379 | if (!PyArg_ParseTuple(args, "s#:write", &data, &len)) |
| 380 | return NULL; |
| 381 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 382 | timedout = wait_for_timeout(self->Socket, 1); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 383 | if (timedout) { |
| 384 | PyErr_SetString(PySSLErrorObject, "The write operation timed out"); |
| 385 | return NULL; |
| 386 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 387 | do { |
| 388 | err = 0; |
| 389 | Py_BEGIN_ALLOW_THREADS |
| 390 | len = SSL_write(self->ssl, data, len); |
| 391 | err = SSL_get_error(self->ssl, len); |
| 392 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 393 | if(PyErr_CheckSignals()) { |
| 394 | return NULL; |
| 395 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 396 | if (err == SSL_ERROR_WANT_READ) { |
| 397 | timedout = wait_for_timeout(self->Socket, 0); |
| 398 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 399 | timedout = wait_for_timeout(self->Socket, 1); |
| 400 | } |
| 401 | if (timedout) { |
| 402 | PyErr_SetString(PySSLErrorObject, "The write operation timed out"); |
| 403 | return NULL; |
| 404 | } |
| 405 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 406 | if (len > 0) |
| 407 | return PyInt_FromLong(len); |
| 408 | else |
| 409 | return PySSL_SetError(self, len); |
| 410 | } |
| 411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 412 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 413 | "write(s) -> len\n\ |
| 414 | \n\ |
| 415 | 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] | 416 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 417 | |
| 418 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
| 419 | { |
| 420 | PyObject *buf; |
| 421 | int count = 0; |
| 422 | int len = 1024; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 423 | int timedout; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 424 | int err; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 425 | |
| 426 | if (!PyArg_ParseTuple(args, "|i:read", &len)) |
| 427 | return NULL; |
| 428 | |
| 429 | if (!(buf = PyString_FromStringAndSize((char *) 0, len))) |
| 430 | return NULL; |
| 431 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 432 | timedout = wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 433 | if (timedout) { |
| 434 | PyErr_SetString(PySSLErrorObject, "The read operation timed out"); |
Neal Norwitz | a9002f8 | 2003-06-30 03:25:20 +0000 | [diff] [blame] | 435 | Py_DECREF(buf); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 438 | do { |
| 439 | err = 0; |
| 440 | Py_BEGIN_ALLOW_THREADS |
| 441 | count = SSL_read(self->ssl, PyString_AsString(buf), len); |
| 442 | err = SSL_get_error(self->ssl, count); |
| 443 | Py_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 444 | if(PyErr_CheckSignals()) { |
| 445 | Py_DECREF(buf); |
| 446 | return NULL; |
| 447 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 448 | if (err == SSL_ERROR_WANT_READ) { |
| 449 | timedout = wait_for_timeout(self->Socket, 0); |
| 450 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 451 | timedout = wait_for_timeout(self->Socket, 1); |
| 452 | } |
| 453 | if (timedout) { |
| 454 | PyErr_SetString(PySSLErrorObject, "The read operation timed out"); |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 455 | Py_DECREF(buf); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 459 | if (count <= 0) { |
| 460 | Py_DECREF(buf); |
| 461 | return PySSL_SetError(self, count); |
| 462 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 463 | if (count != len) |
| 464 | _PyString_Resize(&buf, count); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 465 | return buf; |
| 466 | } |
| 467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 468 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 469 | "read([len]) -> string\n\ |
| 470 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 471 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 472 | |
| 473 | static PyMethodDef PySSLMethods[] = { |
| 474 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
| 475 | PySSL_SSLwrite_doc}, |
| 476 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
| 477 | PySSL_SSLread_doc}, |
| 478 | {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
| 479 | {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
| 480 | {NULL, NULL} |
| 481 | }; |
| 482 | |
| 483 | static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
| 484 | { |
| 485 | return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
| 486 | } |
| 487 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 488 | static PyTypeObject PySSL_Type = { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 489 | PyObject_HEAD_INIT(NULL) |
| 490 | 0, /*ob_size*/ |
| 491 | "socket.SSL", /*tp_name*/ |
| 492 | sizeof(PySSLObject), /*tp_basicsize*/ |
| 493 | 0, /*tp_itemsize*/ |
| 494 | /* methods */ |
| 495 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 496 | 0, /*tp_print*/ |
| 497 | (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
| 498 | 0, /*tp_setattr*/ |
| 499 | 0, /*tp_compare*/ |
| 500 | 0, /*tp_repr*/ |
| 501 | 0, /*tp_as_number*/ |
| 502 | 0, /*tp_as_sequence*/ |
| 503 | 0, /*tp_as_mapping*/ |
| 504 | 0, /*tp_hash*/ |
| 505 | }; |
| 506 | |
| 507 | #ifdef HAVE_OPENSSL_RAND |
| 508 | |
| 509 | /* helper routines for seeding the SSL PRNG */ |
| 510 | static PyObject * |
| 511 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 512 | { |
| 513 | char *buf; |
| 514 | int len; |
| 515 | double entropy; |
| 516 | |
| 517 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 518 | return NULL; |
| 519 | RAND_add(buf, len, entropy); |
| 520 | Py_INCREF(Py_None); |
| 521 | return Py_None; |
| 522 | } |
| 523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 524 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 525 | "RAND_add(string, entropy)\n\ |
| 526 | \n\ |
| 527 | 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] | 528 | bound on the entropy contained in string."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 529 | |
| 530 | static PyObject * |
| 531 | PySSL_RAND_status(PyObject *self) |
| 532 | { |
| 533 | return PyInt_FromLong(RAND_status()); |
| 534 | } |
| 535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 536 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 537 | "RAND_status() -> 0 or 1\n\ |
| 538 | \n\ |
| 539 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 540 | 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] | 541 | using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 542 | |
| 543 | static PyObject * |
| 544 | PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 545 | { |
| 546 | int bytes; |
| 547 | |
| 548 | if (!PyString_Check(arg)) |
| 549 | return PyErr_Format(PyExc_TypeError, |
| 550 | "RAND_egd() expected string, found %s", |
| 551 | arg->ob_type->tp_name); |
| 552 | bytes = RAND_egd(PyString_AS_STRING(arg)); |
| 553 | if (bytes == -1) { |
| 554 | PyErr_SetString(PySSLErrorObject, |
| 555 | "EGD connection failed or EGD did not return " |
| 556 | "enough data to seed the PRNG"); |
| 557 | return NULL; |
| 558 | } |
| 559 | return PyInt_FromLong(bytes); |
| 560 | } |
| 561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 562 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 563 | "RAND_egd(path) -> bytes\n\ |
| 564 | \n\ |
| 565 | Queries the entropy gather daemon (EGD) on socket path. Returns number\n\ |
| 566 | 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] | 567 | if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 568 | |
| 569 | #endif |
| 570 | |
| 571 | /* List of functions exported by this module. */ |
| 572 | |
| 573 | static PyMethodDef PySSL_methods[] = { |
| 574 | {"ssl", PySocket_ssl, |
| 575 | METH_VARARGS, ssl_doc}, |
| 576 | #ifdef HAVE_OPENSSL_RAND |
| 577 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
| 578 | PySSL_RAND_add_doc}, |
| 579 | {"RAND_egd", PySSL_RAND_egd, METH_O, |
| 580 | PySSL_RAND_egd_doc}, |
| 581 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 582 | PySSL_RAND_status_doc}, |
| 583 | #endif |
| 584 | {NULL, NULL} /* Sentinel */ |
| 585 | }; |
| 586 | |
| 587 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 588 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 589 | "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] | 590 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 591 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 592 | PyMODINIT_FUNC |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 593 | init_ssl(void) |
| 594 | { |
| 595 | PyObject *m, *d; |
| 596 | |
| 597 | PySSL_Type.ob_type = &PyType_Type; |
| 598 | |
| 599 | m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
| 600 | d = PyModule_GetDict(m); |
| 601 | |
| 602 | /* Load _socket module and its C API */ |
| 603 | if (PySocketModule_ImportModuleAndAPI()) |
| 604 | return; |
| 605 | |
| 606 | /* Init OpenSSL */ |
| 607 | SSL_load_error_strings(); |
| 608 | SSLeay_add_ssl_algorithms(); |
| 609 | |
| 610 | /* Add symbols to module dict */ |
| 611 | PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL); |
| 612 | if (PySSLErrorObject == NULL) |
| 613 | return; |
| 614 | PyDict_SetItemString(d, "sslerror", PySSLErrorObject); |
| 615 | if (PyDict_SetItemString(d, "SSLType", |
| 616 | (PyObject *)&PySSL_Type) != 0) |
| 617 | return; |
| 618 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 619 | PY_SSL_ERROR_ZERO_RETURN); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 620 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 621 | PY_SSL_ERROR_WANT_READ); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 622 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 623 | PY_SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 624 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 625 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 626 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 627 | PY_SSL_ERROR_SYSCALL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 628 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 629 | PY_SSL_ERROR_SSL); |
| 630 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 631 | PY_SSL_ERROR_WANT_CONNECT); |
| 632 | /* non ssl.h errorcodes */ |
| 633 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 634 | PY_SSL_ERROR_EOF); |
| 635 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 636 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 637 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 638 | } |