2002-02-16 14:23:30 -04:00
|
|
|
/* SSL socket module
|
|
|
|
|
|
|
|
SSL support based on patches by Brian E Gallew and Laszlo Kovacs.
|
|
|
|
|
|
|
|
This module is imported by socket.py. It should *not* be used
|
|
|
|
directly.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Python.h"
|
2002-04-20 04:47:40 -03:00
|
|
|
enum py_ssl_error {
|
|
|
|
/* these mirror ssl.h */
|
|
|
|
PY_SSL_ERROR_NONE,
|
|
|
|
PY_SSL_ERROR_SSL,
|
|
|
|
PY_SSL_ERROR_WANT_READ,
|
|
|
|
PY_SSL_ERROR_WANT_WRITE,
|
|
|
|
PY_SSL_ERROR_WANT_X509_LOOKUP,
|
|
|
|
PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
|
|
|
|
PY_SSL_ERROR_ZERO_RETURN,
|
|
|
|
PY_SSL_ERROR_WANT_CONNECT,
|
|
|
|
/* start of non ssl.h errorcodes */
|
|
|
|
PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */
|
|
|
|
PY_SSL_ERROR_INVALID_ERROR_CODE
|
|
|
|
};
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
/* Include symbols from _socket module */
|
|
|
|
#include "socketmodule.h"
|
|
|
|
|
2006-07-10 23:04:09 -03:00
|
|
|
#if defined(HAVE_POLL_H)
|
|
|
|
#include <poll.h>
|
|
|
|
#elif defined(HAVE_SYS_POLL_H)
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#endif
|
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
/* Include OpenSSL header files */
|
|
|
|
#include "openssl/rsa.h"
|
|
|
|
#include "openssl/crypto.h"
|
|
|
|
#include "openssl/x509.h"
|
|
|
|
#include "openssl/pem.h"
|
|
|
|
#include "openssl/ssl.h"
|
|
|
|
#include "openssl/err.h"
|
|
|
|
#include "openssl/rand.h"
|
|
|
|
|
|
|
|
/* SSL error object */
|
|
|
|
static PyObject *PySSLErrorObject;
|
|
|
|
|
|
|
|
/* SSL socket object */
|
|
|
|
|
|
|
|
#define X509_NAME_MAXLEN 256
|
|
|
|
|
|
|
|
/* RAND_* APIs got added to OpenSSL in 0.9.5 */
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
|
|
|
|
# define HAVE_OPENSSL_RAND 1
|
|
|
|
#else
|
|
|
|
# undef HAVE_OPENSSL_RAND
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PySocketSockObject *Socket; /* Socket on which we're layered */
|
|
|
|
SSL_CTX* ctx;
|
|
|
|
SSL* ssl;
|
|
|
|
X509* server_cert;
|
|
|
|
char server[X509_NAME_MAXLEN];
|
|
|
|
char issuer[X509_NAME_MAXLEN];
|
|
|
|
|
|
|
|
} PySSLObject;
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject PySSL_Type;
|
|
|
|
static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
|
|
|
|
static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
|
2004-07-10 18:15:17 -03:00
|
|
|
static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
|
|
|
|
int writing);
|
2002-02-16 14:23:30 -04:00
|
|
|
|
2007-07-21 03:55:02 -03:00
|
|
|
#define PySSLObject_Check(v) (Py_Type(v) == &PySSL_Type)
|
2002-02-16 14:23:30 -04:00
|
|
|
|
2004-07-10 18:15:17 -03:00
|
|
|
typedef enum {
|
|
|
|
SOCKET_IS_NONBLOCKING,
|
|
|
|
SOCKET_IS_BLOCKING,
|
|
|
|
SOCKET_HAS_TIMED_OUT,
|
|
|
|
SOCKET_HAS_BEEN_CLOSED,
|
2006-02-12 20:35:21 -04:00
|
|
|
SOCKET_TOO_LARGE_FOR_SELECT,
|
2004-07-10 18:15:17 -03:00
|
|
|
SOCKET_OPERATION_OK
|
|
|
|
} timeout_state;
|
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
/* XXX It might be helpful to augment the error message generated
|
|
|
|
below with the name of the SSL function that generated the error.
|
|
|
|
I expect it's obvious most of the time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySSL_SetError(PySSLObject *obj, int ret)
|
|
|
|
{
|
|
|
|
PyObject *v, *n, *s;
|
|
|
|
char *errstr;
|
|
|
|
int err;
|
2002-04-20 04:47:40 -03:00
|
|
|
enum py_ssl_error p;
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
assert(ret <= 0);
|
|
|
|
|
|
|
|
err = SSL_get_error(obj->ssl, ret);
|
|
|
|
|
2002-04-20 04:47:40 -03:00
|
|
|
switch (err) {
|
2002-02-16 14:23:30 -04:00
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
errstr = "TLS/SSL connection has been closed";
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_ZERO_RETURN;
|
2002-02-16 14:23:30 -04:00
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
errstr = "The operation did not complete (read)";
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_WANT_READ;
|
2002-02-16 14:23:30 -04:00
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_WANT_WRITE;
|
2002-02-16 14:23:30 -04:00
|
|
|
errstr = "The operation did not complete (write)";
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_WANT_X509_LOOKUP;
|
2002-02-16 14:23:30 -04:00
|
|
|
errstr = "The operation did not complete (X509 lookup)";
|
|
|
|
break;
|
2002-04-20 04:47:40 -03:00
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_WANT_CONNECT;
|
2002-04-20 04:47:40 -03:00
|
|
|
errstr = "The operation did not complete (connect)";
|
|
|
|
break;
|
2002-02-16 14:23:30 -04:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
{
|
|
|
|
unsigned long e = ERR_get_error();
|
2002-07-02 15:25:00 -03:00
|
|
|
if (e == 0) {
|
2003-06-30 00:25:20 -03:00
|
|
|
if (ret == 0 || !obj->Socket) {
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_EOF;
|
2002-04-20 04:47:40 -03:00
|
|
|
errstr = "EOF occurred in violation of protocol";
|
2002-07-02 15:25:00 -03:00
|
|
|
} else if (ret == -1) {
|
2002-04-20 04:47:40 -03:00
|
|
|
/* the underlying BIO reported an I/O error */
|
|
|
|
return obj->Socket->errorhandler();
|
2002-07-02 15:25:00 -03:00
|
|
|
} else { /* possible? */
|
|
|
|
p = PY_SSL_ERROR_SYSCALL;
|
2002-04-20 04:47:40 -03:00
|
|
|
errstr = "Some I/O error occurred";
|
|
|
|
}
|
2002-02-16 14:23:30 -04:00
|
|
|
} else {
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_SYSCALL;
|
2002-04-20 04:47:40 -03:00
|
|
|
/* XXX Protected by global interpreter lock */
|
|
|
|
errstr = ERR_error_string(e, NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
{
|
|
|
|
unsigned long e = ERR_get_error();
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_SSL;
|
|
|
|
if (e != 0)
|
2002-02-16 14:23:30 -04:00
|
|
|
/* XXX Protected by global interpreter lock */
|
|
|
|
errstr = ERR_error_string(e, NULL);
|
2002-07-02 15:25:00 -03:00
|
|
|
else { /* possible? */
|
|
|
|
errstr = "A failure in the SSL library occurred";
|
2002-02-16 14:23:30 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2002-07-02 15:25:00 -03:00
|
|
|
p = PY_SSL_ERROR_INVALID_ERROR_CODE;
|
2002-02-16 14:23:30 -04:00
|
|
|
errstr = "Invalid error code";
|
|
|
|
}
|
2002-04-20 04:47:40 -03:00
|
|
|
n = PyInt_FromLong((long) p);
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
v = PyTuple_New(2);
|
|
|
|
if (v == NULL) {
|
|
|
|
Py_DECREF(n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
s = PyString_FromString(errstr);
|
|
|
|
if (s == NULL) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(n);
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(v, 0, n);
|
|
|
|
PyTuple_SET_ITEM(v, 1, s);
|
|
|
|
PyErr_SetObject(PySSLErrorObject, v);
|
2004-08-04 11:59:00 -03:00
|
|
|
Py_DECREF(v);
|
2002-02-16 14:23:30 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PySSLObject *
|
|
|
|
newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
|
|
|
|
{
|
|
|
|
PySSLObject *self;
|
|
|
|
char *errstr = NULL;
|
|
|
|
int ret;
|
2003-01-31 14:13:18 -04:00
|
|
|
int err;
|
2004-07-10 18:15:17 -03:00
|
|
|
int sockstate;
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
|
2006-05-11 04:51:59 -03:00
|
|
|
if (self == NULL)
|
2006-05-10 03:57:58 -03:00
|
|
|
return NULL;
|
2002-02-16 14:23:30 -04:00
|
|
|
memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN);
|
|
|
|
memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
|
|
|
|
self->server_cert = NULL;
|
|
|
|
self->ssl = NULL;
|
|
|
|
self->ctx = NULL;
|
|
|
|
self->Socket = NULL;
|
|
|
|
|
|
|
|
if ((key_file && !cert_file) || (!key_file && cert_file)) {
|
|
|
|
errstr = "Both the key & certificate files must be specified";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
if (self->ctx == NULL) {
|
|
|
|
errstr = "SSL_CTX_new error";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_file) {
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
|
|
|
|
SSL_FILETYPE_PEM);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (ret < 1) {
|
2002-02-16 14:23:30 -04:00
|
|
|
errstr = "SSL_CTX_use_PrivateKey_file error";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
ret = SSL_CTX_use_certificate_chain_file(self->ctx,
|
|
|
|
cert_file);
|
|
|
|
Py_END_ALLOW_THREADS
|
2004-07-10 18:36:55 -03:00
|
|
|
SSL_CTX_set_options(self->ctx, SSL_OP_ALL); /* ssl compatibility */
|
2002-07-28 06:57:45 -03:00
|
|
|
if (ret < 1) {
|
2002-02-16 14:23:30 -04:00
|
|
|
errstr = "SSL_CTX_use_certificate_chain_file error";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
SSL_CTX_set_verify(self->ctx,
|
|
|
|
SSL_VERIFY_NONE, NULL); /* set verify lvl */
|
|
|
|
self->ssl = SSL_new(self->ctx); /* New ssl struct */
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
|
2003-01-31 14:13:18 -04:00
|
|
|
|
2003-10-20 11:01:56 -03:00
|
|
|
/* If the socket is in non-blocking mode or timeout mode, set the BIO
|
2003-01-31 14:13:18 -04:00
|
|
|
* to non-blocking mode (blocking is the default)
|
|
|
|
*/
|
|
|
|
if (Sock->sock_timeout >= 0.0) {
|
|
|
|
/* Set both the read and write BIO's to non-blocking mode */
|
|
|
|
BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
|
|
|
|
BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
|
|
|
|
}
|
|
|
|
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
SSL_set_connect_state(self->ssl);
|
2003-01-31 14:13:18 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-07-28 06:57:45 -03:00
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
/* Actually negotiate SSL connection */
|
|
|
|
/* XXX If SSL_connect() returns 0, it's also a failure. */
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = 0;
|
2003-01-31 14:13:18 -04:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
ret = SSL_connect(self->ssl);
|
|
|
|
err = SSL_get_error(self->ssl, ret);
|
|
|
|
Py_END_ALLOW_THREADS
|
2003-06-28 04:40:23 -03:00
|
|
|
if(PyErr_CheckSignals()) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2003-01-31 14:13:18 -04:00
|
|
|
if (err == SSL_ERROR_WANT_READ) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(Sock, 0);
|
2003-01-31 14:13:18 -04:00
|
|
|
} else if (err == SSL_ERROR_WANT_WRITE) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(Sock, 1);
|
|
|
|
} else {
|
|
|
|
sockstate = SOCKET_OPERATION_OK;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
2006-02-07 02:59:20 -04:00
|
|
|
if (sockstate == SOCKET_HAS_TIMED_OUT) {
|
2004-07-10 18:15:17 -03:00
|
|
|
PyErr_SetString(PySSLErrorObject, "The connect operation timed out");
|
2003-06-28 04:40:23 -03:00
|
|
|
goto fail;
|
2004-07-10 18:15:17 -03:00
|
|
|
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
|
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
|
|
|
|
goto fail;
|
2006-02-12 20:35:21 -04:00
|
|
|
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
|
2006-02-07 03:04:46 -04:00
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
|
|
|
|
goto fail;
|
2004-07-10 18:15:17 -03:00
|
|
|
} else if (sockstate == SOCKET_IS_NONBLOCKING) {
|
|
|
|
break;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
|
|
|
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
|
2002-02-16 14:23:30 -04:00
|
|
|
if (ret <= 0) {
|
|
|
|
PySSL_SetError(self, ret);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
self->ssl->debug = 1;
|
|
|
|
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
|
|
|
|
X509_NAME_oneline(X509_get_subject_name(self->server_cert),
|
|
|
|
self->server, X509_NAME_MAXLEN);
|
|
|
|
X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
|
|
|
|
self->issuer, X509_NAME_MAXLEN);
|
|
|
|
}
|
2002-07-28 06:57:45 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
self->Socket = Sock;
|
|
|
|
Py_INCREF(self->Socket);
|
|
|
|
return self;
|
|
|
|
fail:
|
|
|
|
if (errstr)
|
|
|
|
PyErr_SetString(PySSLErrorObject, errstr);
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySocket_ssl(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PySSLObject *rv;
|
|
|
|
PySocketSockObject *Sock;
|
|
|
|
char *key_file = NULL;
|
|
|
|
char *cert_file = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O!|zz:ssl",
|
|
|
|
PySocketModule.Sock_Type,
|
2006-10-19 08:00:37 -03:00
|
|
|
&Sock,
|
2002-02-16 14:23:30 -04:00
|
|
|
&key_file, &cert_file))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rv = newPySSLObject(Sock, key_file, cert_file);
|
|
|
|
if (rv == NULL)
|
|
|
|
return NULL;
|
|
|
|
return (PyObject *)rv;
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(ssl_doc,
|
|
|
|
"ssl(socket, [keyfile, certfile]) -> sslobject");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
/* SSL object methods */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySSL_server(PySSLObject *self)
|
|
|
|
{
|
|
|
|
return PyString_FromString(self->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySSL_issuer(PySSLObject *self)
|
|
|
|
{
|
|
|
|
return PyString_FromString(self->issuer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PySSL_dealloc(PySSLObject *self)
|
|
|
|
{
|
|
|
|
if (self->server_cert) /* Possible not to have one? */
|
|
|
|
X509_free (self->server_cert);
|
|
|
|
if (self->ssl)
|
|
|
|
SSL_free(self->ssl);
|
|
|
|
if (self->ctx)
|
|
|
|
SSL_CTX_free(self->ctx);
|
|
|
|
Py_XDECREF(self->Socket);
|
|
|
|
PyObject_Del(self);
|
|
|
|
}
|
|
|
|
|
2006-07-10 23:04:09 -03:00
|
|
|
/* If the socket has a timeout, do a select()/poll() on the socket.
|
2003-01-27 18:22:50 -04:00
|
|
|
The argument writing indicates the direction.
|
2004-07-10 18:15:17 -03:00
|
|
|
Returns one of the possibilities in the timeout_state enum (above).
|
2003-01-27 18:22:50 -04:00
|
|
|
*/
|
2004-07-10 18:15:17 -03:00
|
|
|
|
2003-01-27 18:22:50 -04:00
|
|
|
static int
|
2004-07-10 18:15:17 -03:00
|
|
|
check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
|
2003-01-27 18:22:50 -04:00
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Nothing to do unless we're in timeout mode (not non-blocking) */
|
2004-07-10 18:15:17 -03:00
|
|
|
if (s->sock_timeout < 0.0)
|
|
|
|
return SOCKET_IS_BLOCKING;
|
|
|
|
else if (s->sock_timeout == 0.0)
|
|
|
|
return SOCKET_IS_NONBLOCKING;
|
2003-01-27 18:22:50 -04:00
|
|
|
|
|
|
|
/* Guard against closed socket */
|
|
|
|
if (s->sock_fd < 0)
|
2004-07-10 18:15:17 -03:00
|
|
|
return SOCKET_HAS_BEEN_CLOSED;
|
2003-01-27 18:22:50 -04:00
|
|
|
|
2006-07-10 23:04:09 -03:00
|
|
|
/* Prefer poll, if available, since you can poll() any fd
|
|
|
|
* which can't be done with select(). */
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
{
|
|
|
|
struct pollfd pollfd;
|
|
|
|
int timeout;
|
|
|
|
|
|
|
|
pollfd.fd = s->sock_fd;
|
|
|
|
pollfd.events = writing ? POLLOUT : POLLIN;
|
|
|
|
|
|
|
|
/* s->sock_timeout is in seconds, timeout in ms */
|
|
|
|
timeout = (int)(s->sock_timeout * 1000 + 0.5);
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
rc = poll(&pollfd, 1, timeout);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
goto normal_return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-07 03:04:46 -04:00
|
|
|
/* Guard against socket too large for select*/
|
2006-02-11 05:27:05 -04:00
|
|
|
#ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
|
2006-02-07 03:04:46 -04:00
|
|
|
if (s->sock_fd >= FD_SETSIZE)
|
2006-02-12 20:35:21 -04:00
|
|
|
return SOCKET_TOO_LARGE_FOR_SELECT;
|
2006-02-11 05:27:05 -04:00
|
|
|
#endif
|
2006-02-07 03:04:46 -04:00
|
|
|
|
2003-01-27 18:22:50 -04:00
|
|
|
/* Construct the arguments to select */
|
|
|
|
tv.tv_sec = (int)s->sock_timeout;
|
|
|
|
tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(s->sock_fd, &fds);
|
|
|
|
|
|
|
|
/* See if the socket is ready */
|
2003-01-31 14:13:18 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2003-01-27 18:22:50 -04:00
|
|
|
if (writing)
|
|
|
|
rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
|
|
|
|
else
|
|
|
|
rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
|
2003-01-31 14:13:18 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
2003-01-27 18:22:50 -04:00
|
|
|
|
2006-07-10 23:04:09 -03:00
|
|
|
normal_return:
|
2004-07-10 18:15:17 -03:00
|
|
|
/* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
|
|
|
|
(when we are able to write or when there's something to read) */
|
|
|
|
return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
|
2003-01-27 18:22:50 -04:00
|
|
|
}
|
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
int len;
|
2003-10-27 10:24:37 -04:00
|
|
|
int count;
|
2004-07-10 18:15:17 -03:00
|
|
|
int sockstate;
|
2003-01-31 14:13:18 -04:00
|
|
|
int err;
|
2002-02-16 14:23:30 -04:00
|
|
|
|
2003-10-27 10:24:37 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s#:write", &data, &count))
|
2002-02-16 14:23:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
|
|
|
|
if (sockstate == SOCKET_HAS_TIMED_OUT) {
|
2003-01-27 18:22:50 -04:00
|
|
|
PyErr_SetString(PySSLErrorObject, "The write operation timed out");
|
|
|
|
return NULL;
|
2004-07-10 18:15:17 -03:00
|
|
|
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
|
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
|
|
|
|
return NULL;
|
2006-02-12 20:35:21 -04:00
|
|
|
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
|
2006-02-07 03:04:46 -04:00
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
|
|
|
|
return NULL;
|
2003-01-27 18:22:50 -04:00
|
|
|
}
|
2003-01-31 14:13:18 -04:00
|
|
|
do {
|
|
|
|
err = 0;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2003-10-27 10:24:37 -04:00
|
|
|
len = SSL_write(self->ssl, data, count);
|
2003-01-31 14:13:18 -04:00
|
|
|
err = SSL_get_error(self->ssl, len);
|
|
|
|
Py_END_ALLOW_THREADS
|
2003-06-28 04:40:23 -03:00
|
|
|
if(PyErr_CheckSignals()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-31 14:13:18 -04:00
|
|
|
if (err == SSL_ERROR_WANT_READ) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
|
2003-01-31 14:13:18 -04:00
|
|
|
} else if (err == SSL_ERROR_WANT_WRITE) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
|
|
|
|
} else {
|
|
|
|
sockstate = SOCKET_OPERATION_OK;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
2006-02-07 02:59:20 -04:00
|
|
|
if (sockstate == SOCKET_HAS_TIMED_OUT) {
|
2003-01-31 14:13:18 -04:00
|
|
|
PyErr_SetString(PySSLErrorObject, "The write operation timed out");
|
|
|
|
return NULL;
|
2004-07-10 18:15:17 -03:00
|
|
|
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
|
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
|
|
|
|
return NULL;
|
|
|
|
} else if (sockstate == SOCKET_IS_NONBLOCKING) {
|
|
|
|
break;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
|
|
|
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
|
2002-02-16 14:23:30 -04:00
|
|
|
if (len > 0)
|
|
|
|
return PyInt_FromLong(len);
|
|
|
|
else
|
|
|
|
return PySSL_SetError(self, len);
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(PySSL_SSLwrite_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"write(s) -> len\n\
|
|
|
|
\n\
|
|
|
|
Writes the string s into the SSL object. Returns the number\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
of bytes written.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *buf;
|
|
|
|
int count = 0;
|
|
|
|
int len = 1024;
|
2004-07-10 18:15:17 -03:00
|
|
|
int sockstate;
|
2003-01-31 14:13:18 -04:00
|
|
|
int err;
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:read", &len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
|
|
|
|
return NULL;
|
2006-03-31 14:01:16 -04:00
|
|
|
|
|
|
|
/* first check if there are bytes ready to be read */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
count = SSL_pending(self->ssl);
|
|
|
|
Py_END_ALLOW_THREADS
|
2002-02-16 14:23:30 -04:00
|
|
|
|
2006-03-31 14:01:16 -04:00
|
|
|
if (!count) {
|
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
|
|
|
|
if (sockstate == SOCKET_HAS_TIMED_OUT) {
|
|
|
|
PyErr_SetString(PySSLErrorObject, "The read operation timed out");
|
|
|
|
Py_DECREF(buf);
|
|
|
|
return NULL;
|
|
|
|
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
|
|
|
|
PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-27 18:22:50 -04:00
|
|
|
}
|
2003-01-31 14:13:18 -04:00
|
|
|
do {
|
|
|
|
err = 0;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
count = SSL_read(self->ssl, PyString_AsString(buf), len);
|
|
|
|
err = SSL_get_error(self->ssl, count);
|
|
|
|
Py_END_ALLOW_THREADS
|
2003-06-28 04:40:23 -03:00
|
|
|
if(PyErr_CheckSignals()) {
|
|
|
|
Py_DECREF(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-31 14:13:18 -04:00
|
|
|
if (err == SSL_ERROR_WANT_READ) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
|
2003-01-31 14:13:18 -04:00
|
|
|
} else if (err == SSL_ERROR_WANT_WRITE) {
|
2004-07-10 18:15:17 -03:00
|
|
|
sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
|
|
|
|
} else {
|
|
|
|
sockstate = SOCKET_OPERATION_OK;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
2006-02-07 02:59:20 -04:00
|
|
|
if (sockstate == SOCKET_HAS_TIMED_OUT) {
|
2003-01-31 14:13:18 -04:00
|
|
|
PyErr_SetString(PySSLErrorObject, "The read operation timed out");
|
2003-06-28 04:40:23 -03:00
|
|
|
Py_DECREF(buf);
|
2003-01-31 14:13:18 -04:00
|
|
|
return NULL;
|
2004-07-10 18:15:17 -03:00
|
|
|
} else if (sockstate == SOCKET_IS_NONBLOCKING) {
|
|
|
|
break;
|
2003-01-31 14:13:18 -04:00
|
|
|
}
|
|
|
|
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
|
2002-02-16 14:23:30 -04:00
|
|
|
if (count <= 0) {
|
|
|
|
Py_DECREF(buf);
|
|
|
|
return PySSL_SetError(self, count);
|
|
|
|
}
|
2002-04-27 15:44:32 -03:00
|
|
|
if (count != len)
|
|
|
|
_PyString_Resize(&buf, count);
|
2002-02-16 14:23:30 -04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(PySSL_SSLread_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"read([len]) -> string\n\
|
|
|
|
\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
Read up to len bytes from the SSL socket.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
static PyMethodDef PySSLMethods[] = {
|
|
|
|
{"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
|
|
|
|
PySSL_SSLwrite_doc},
|
|
|
|
{"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
|
|
|
|
PySSL_SSLread_doc},
|
|
|
|
{"server", (PyCFunction)PySSL_server, METH_NOARGS},
|
|
|
|
{"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *PySSL_getattr(PySSLObject *self, char *name)
|
|
|
|
{
|
|
|
|
return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
|
|
|
|
}
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject PySSL_Type = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2002-02-16 14:23:30 -04:00
|
|
|
"socket.SSL", /*tp_name*/
|
|
|
|
sizeof(PySSLObject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)PySSL_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)PySSL_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL_RAND
|
|
|
|
|
|
|
|
/* helper routines for seeding the SSL PRNG */
|
|
|
|
static PyObject *
|
|
|
|
PySSL_RAND_add(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int len;
|
|
|
|
double entropy;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
|
|
|
|
return NULL;
|
|
|
|
RAND_add(buf, len, entropy);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(PySSL_RAND_add_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"RAND_add(string, entropy)\n\
|
|
|
|
\n\
|
|
|
|
Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
bound on the entropy contained in string.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySSL_RAND_status(PyObject *self)
|
|
|
|
{
|
|
|
|
return PyInt_FromLong(RAND_status());
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(PySSL_RAND_status_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"RAND_status() -> 0 or 1\n\
|
|
|
|
\n\
|
|
|
|
Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
|
|
|
|
It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
using the ssl() function.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySSL_RAND_egd(PyObject *self, PyObject *arg)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
if (!PyString_Check(arg))
|
|
|
|
return PyErr_Format(PyExc_TypeError,
|
|
|
|
"RAND_egd() expected string, found %s",
|
2007-07-21 03:55:02 -03:00
|
|
|
Py_Type(arg)->tp_name);
|
2002-02-16 14:23:30 -04:00
|
|
|
bytes = RAND_egd(PyString_AS_STRING(arg));
|
|
|
|
if (bytes == -1) {
|
|
|
|
PyErr_SetString(PySSLErrorObject,
|
|
|
|
"EGD connection failed or EGD did not return "
|
|
|
|
"enough data to seed the PRNG");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyInt_FromLong(bytes);
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(PySSL_RAND_egd_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"RAND_egd(path) -> bytes\n\
|
|
|
|
\n\
|
|
|
|
Queries the entropy gather daemon (EGD) on socket path. Returns number\n\
|
|
|
|
of bytes read. Raises socket.sslerror if connection to EGD fails or\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
if it does provide enough data to seed PRNG.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* List of functions exported by this module. */
|
|
|
|
|
|
|
|
static PyMethodDef PySSL_methods[] = {
|
|
|
|
{"ssl", PySocket_ssl,
|
|
|
|
METH_VARARGS, ssl_doc},
|
|
|
|
#ifdef HAVE_OPENSSL_RAND
|
|
|
|
{"RAND_add", PySSL_RAND_add, METH_VARARGS,
|
|
|
|
PySSL_RAND_add_doc},
|
|
|
|
{"RAND_egd", PySSL_RAND_egd, METH_O,
|
|
|
|
PySSL_RAND_egd_doc},
|
|
|
|
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
|
|
|
|
PySSL_RAND_status_doc},
|
|
|
|
#endif
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(module_doc,
|
2002-02-16 14:23:30 -04:00
|
|
|
"Implementation module for SSL socket operations. See the socket module\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
for documentation.");
|
2002-02-16 14:23:30 -04:00
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2002-02-16 14:23:30 -04:00
|
|
|
init_ssl(void)
|
|
|
|
{
|
|
|
|
PyObject *m, *d;
|
|
|
|
|
2007-07-21 03:55:02 -03:00
|
|
|
Py_Type(&PySSL_Type) = &PyType_Type;
|
2002-02-16 14:23:30 -04:00
|
|
|
|
|
|
|
m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2002-02-16 14:23:30 -04:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
|
|
|
|
/* Load _socket module and its C API */
|
|
|
|
if (PySocketModule_ImportModuleAndAPI())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Init OpenSSL */
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSLeay_add_ssl_algorithms();
|
|
|
|
|
|
|
|
/* Add symbols to module dict */
|
2004-03-23 19:16:54 -04:00
|
|
|
PySSLErrorObject = PyErr_NewException("socket.sslerror",
|
|
|
|
PySocketModule.error,
|
|
|
|
NULL);
|
2002-02-16 14:23:30 -04:00
|
|
|
if (PySSLErrorObject == NULL)
|
|
|
|
return;
|
|
|
|
PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
|
|
|
|
if (PyDict_SetItemString(d, "SSLType",
|
|
|
|
(PyObject *)&PySSL_Type) != 0)
|
|
|
|
return;
|
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_ZERO_RETURN);
|
2002-02-16 14:23:30 -04:00
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_WANT_READ);
|
2002-02-16 14:23:30 -04:00
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_WANT_WRITE);
|
2002-02-16 14:23:30 -04:00
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_WANT_X509_LOOKUP);
|
2002-02-16 14:23:30 -04:00
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_SYSCALL);
|
2002-02-16 14:23:30 -04:00
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
|
2002-04-20 04:47:40 -03:00
|
|
|
PY_SSL_ERROR_SSL);
|
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
|
|
|
|
PY_SSL_ERROR_WANT_CONNECT);
|
|
|
|
/* non ssl.h errorcodes */
|
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
|
|
|
|
PY_SSL_ERROR_EOF);
|
|
|
|
PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
|
|
|
|
PY_SSL_ERROR_INVALID_ERROR_CODE);
|
|
|
|
|
2002-02-16 14:23:30 -04:00
|
|
|
}
|