blob: fd5e2c629555c4b6c53bcbc64df0e2094356bfbd [file] [log] [blame]
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001/* 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öwis6af3e2d2002-04-20 07:47:40 +000011enum 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é Lemburga5d2b4c2002-02-16 18:23:30 +000025
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 */
39static 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
52typedef 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 Hylton938ace62002-07-17 16:30:39 +000064static PyTypeObject PySSL_Type;
65static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
66static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +000067static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
68 int writing);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000069
70#define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type)
71
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +000072typedef enum {
73 SOCKET_IS_NONBLOCKING,
74 SOCKET_IS_BLOCKING,
75 SOCKET_HAS_TIMED_OUT,
76 SOCKET_HAS_BEEN_CLOSED,
77 SOCKET_OPERATION_OK
78} timeout_state;
79
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000080/* XXX It might be helpful to augment the error message generated
81 below with the name of the SSL function that generated the error.
82 I expect it's obvious most of the time.
83*/
84
85static PyObject *
86PySSL_SetError(PySSLObject *obj, int ret)
87{
88 PyObject *v, *n, *s;
89 char *errstr;
90 int err;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000091 enum py_ssl_error p;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000092
93 assert(ret <= 0);
94
95 err = SSL_get_error(obj->ssl, ret);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000096
97 switch (err) {
98 case SSL_ERROR_ZERO_RETURN:
99 errstr = "TLS/SSL connection has been closed";
Jeremy Hylton4e547302002-07-02 18:25:00 +0000100 p = PY_SSL_ERROR_ZERO_RETURN;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000101 break;
102 case SSL_ERROR_WANT_READ:
103 errstr = "The operation did not complete (read)";
Jeremy Hylton4e547302002-07-02 18:25:00 +0000104 p = PY_SSL_ERROR_WANT_READ;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000105 break;
106 case SSL_ERROR_WANT_WRITE:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000107 p = PY_SSL_ERROR_WANT_WRITE;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000108 errstr = "The operation did not complete (write)";
109 break;
110 case SSL_ERROR_WANT_X509_LOOKUP:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000111 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000112 errstr = "The operation did not complete (X509 lookup)";
113 break;
114 case SSL_ERROR_WANT_CONNECT:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000115 p = PY_SSL_ERROR_WANT_CONNECT;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000116 errstr = "The operation did not complete (connect)";
117 break;
118 case SSL_ERROR_SYSCALL:
119 {
120 unsigned long e = ERR_get_error();
Jeremy Hylton4e547302002-07-02 18:25:00 +0000121 if (e == 0) {
Neal Norwitza9002f82003-06-30 03:25:20 +0000122 if (ret == 0 || !obj->Socket) {
Jeremy Hylton4e547302002-07-02 18:25:00 +0000123 p = PY_SSL_ERROR_EOF;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000124 errstr = "EOF occurred in violation of protocol";
Jeremy Hylton4e547302002-07-02 18:25:00 +0000125 } else if (ret == -1) {
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000126 /* the underlying BIO reported an I/O error */
127 return obj->Socket->errorhandler();
Jeremy Hylton4e547302002-07-02 18:25:00 +0000128 } else { /* possible? */
129 p = PY_SSL_ERROR_SYSCALL;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000130 errstr = "Some I/O error occurred";
131 }
132 } else {
Jeremy Hylton4e547302002-07-02 18:25:00 +0000133 p = PY_SSL_ERROR_SYSCALL;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000134 /* XXX Protected by global interpreter lock */
135 errstr = ERR_error_string(e, NULL);
136 }
137 break;
138 }
139 case SSL_ERROR_SSL:
140 {
141 unsigned long e = ERR_get_error();
Jeremy Hylton4e547302002-07-02 18:25:00 +0000142 p = PY_SSL_ERROR_SSL;
143 if (e != 0)
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000144 /* XXX Protected by global interpreter lock */
145 errstr = ERR_error_string(e, NULL);
Jeremy Hylton4e547302002-07-02 18:25:00 +0000146 else { /* possible? */
147 errstr = "A failure in the SSL library occurred";
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000148 }
149 break;
150 }
151 default:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000152 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000153 errstr = "Invalid error code";
154 }
155 n = PyInt_FromLong((long) p);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000156 if (n == NULL)
157 return NULL;
158 v = PyTuple_New(2);
159 if (v == NULL) {
160 Py_DECREF(n);
161 return NULL;
162 }
163
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000164 s = PyString_FromString(errstr);
165 if (s == NULL) {
166 Py_DECREF(v);
167 Py_DECREF(n);
168 }
169 PyTuple_SET_ITEM(v, 0, n);
170 PyTuple_SET_ITEM(v, 1, s);
171 PyErr_SetObject(PySSLErrorObject, v);
Michael W. Hudson5910d812004-08-04 14:59:00 +0000172 Py_DECREF(v);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000173 return NULL;
174}
175
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000176static PySSLObject *
177newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
178{
179 PySSLObject *self;
180 char *errstr = NULL;
181 int ret;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000182 int err;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000183 int sockstate;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000184
185 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
186 if (self == NULL){
187 errstr = "newPySSLObject error";
188 goto fail;
189 }
190 memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN);
191 memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
192 self->server_cert = NULL;
193 self->ssl = NULL;
194 self->ctx = NULL;
195 self->Socket = NULL;
196
197 if ((key_file && !cert_file) || (!key_file && cert_file)) {
198 errstr = "Both the key & certificate files must be specified";
199 goto fail;
200 }
201
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000202 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000203 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000204 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000205 if (self->ctx == NULL) {
206 errstr = "SSL_CTX_new error";
207 goto fail;
208 }
209
210 if (key_file) {
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000211 Py_BEGIN_ALLOW_THREADS
212 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
213 SSL_FILETYPE_PEM);
214 Py_END_ALLOW_THREADS
215 if (ret < 1) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000216 errstr = "SSL_CTX_use_PrivateKey_file error";
217 goto fail;
218 }
219
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000220 Py_BEGIN_ALLOW_THREADS
221 ret = SSL_CTX_use_certificate_chain_file(self->ctx,
222 cert_file);
223 Py_END_ALLOW_THREADS
Andrew M. Kuchling27d3dda2004-07-10 21:36:55 +0000224 SSL_CTX_set_options(self->ctx, SSL_OP_ALL); /* ssl compatibility */
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000225 if (ret < 1) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000226 errstr = "SSL_CTX_use_certificate_chain_file error";
227 goto fail;
228 }
229 }
230
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000231 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000232 SSL_CTX_set_verify(self->ctx,
233 SSL_VERIFY_NONE, NULL); /* set verify lvl */
234 self->ssl = SSL_new(self->ctx); /* New ssl struct */
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000235 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000236 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000237
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000238 /* If the socket is in non-blocking mode or timeout mode, set the BIO
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000239 * to non-blocking mode (blocking is the default)
240 */
241 if (Sock->sock_timeout >= 0.0) {
242 /* Set both the read and write BIO's to non-blocking mode */
243 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
244 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
245 }
246
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000247 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000248 SSL_set_connect_state(self->ssl);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000249 Py_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000250
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000251 /* Actually negotiate SSL connection */
252 /* XXX If SSL_connect() returns 0, it's also a failure. */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000253 sockstate = 0;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000254 do {
255 Py_BEGIN_ALLOW_THREADS
256 ret = SSL_connect(self->ssl);
257 err = SSL_get_error(self->ssl, ret);
258 Py_END_ALLOW_THREADS
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000259 if(PyErr_CheckSignals()) {
260 goto fail;
261 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000262 if (err == SSL_ERROR_WANT_READ) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000263 sockstate = check_socket_and_wait_for_timeout(Sock, 0);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000264 } else if (err == SSL_ERROR_WANT_WRITE) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000265 sockstate = check_socket_and_wait_for_timeout(Sock, 1);
266 } else {
267 sockstate = SOCKET_OPERATION_OK;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000268 }
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000269 if (sockstate == SOCKET_HAS_TIMED_OUT) {
270 PyErr_SetString(PySSLErrorObject, "The connect operation timed out");
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000271 goto fail;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000272 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
273 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
274 goto fail;
275 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
276 break;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000277 }
278 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000279 if (ret <= 0) {
280 PySSL_SetError(self, ret);
281 goto fail;
282 }
283 self->ssl->debug = 1;
284
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000285 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000286 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
287 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
288 self->server, X509_NAME_MAXLEN);
289 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
290 self->issuer, X509_NAME_MAXLEN);
291 }
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000292 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000293 self->Socket = Sock;
294 Py_INCREF(self->Socket);
295 return self;
296 fail:
297 if (errstr)
298 PyErr_SetString(PySSLErrorObject, errstr);
299 Py_DECREF(self);
300 return NULL;
301}
302
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000303static PyObject *
304PySocket_ssl(PyObject *self, PyObject *args)
305{
306 PySSLObject *rv;
307 PySocketSockObject *Sock;
308 char *key_file = NULL;
309 char *cert_file = NULL;
310
311 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
312 PySocketModule.Sock_Type,
313 (PyObject*)&Sock,
314 &key_file, &cert_file))
315 return NULL;
316
317 rv = newPySSLObject(Sock, key_file, cert_file);
318 if (rv == NULL)
319 return NULL;
320 return (PyObject *)rv;
321}
322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000323PyDoc_STRVAR(ssl_doc,
324"ssl(socket, [keyfile, certfile]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000325
326/* SSL object methods */
327
328static PyObject *
329PySSL_server(PySSLObject *self)
330{
331 return PyString_FromString(self->server);
332}
333
334static PyObject *
335PySSL_issuer(PySSLObject *self)
336{
337 return PyString_FromString(self->issuer);
338}
339
340
341static void PySSL_dealloc(PySSLObject *self)
342{
343 if (self->server_cert) /* Possible not to have one? */
344 X509_free (self->server_cert);
345 if (self->ssl)
346 SSL_free(self->ssl);
347 if (self->ctx)
348 SSL_CTX_free(self->ctx);
349 Py_XDECREF(self->Socket);
350 PyObject_Del(self);
351}
352
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000353/* If the socket has a timeout, do a select() on the socket.
354 The argument writing indicates the direction.
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000355 Returns one of the possibilities in the timeout_state enum (above).
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000356 */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000357
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000358static int
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000359check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000360{
361 fd_set fds;
362 struct timeval tv;
363 int rc;
364
365 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000366 if (s->sock_timeout < 0.0)
367 return SOCKET_IS_BLOCKING;
368 else if (s->sock_timeout == 0.0)
369 return SOCKET_IS_NONBLOCKING;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000370
371 /* Guard against closed socket */
372 if (s->sock_fd < 0)
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000373 return SOCKET_HAS_BEEN_CLOSED;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000374
375 /* Construct the arguments to select */
376 tv.tv_sec = (int)s->sock_timeout;
377 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
378 FD_ZERO(&fds);
379 FD_SET(s->sock_fd, &fds);
380
381 /* See if the socket is ready */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000382 Py_BEGIN_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000383 if (writing)
384 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
385 else
386 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000387 Py_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000388
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000389 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
390 (when we are able to write or when there's something to read) */
391 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000392}
393
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000394static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
395{
396 char *data;
397 int len;
Martin v. Löwis405a7952003-10-27 14:24:37 +0000398 int count;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000399 int sockstate;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000400 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000401
Martin v. Löwis405a7952003-10-27 14:24:37 +0000402 if (!PyArg_ParseTuple(args, "s#:write", &data, &count))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000403 return NULL;
404
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000405 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
406 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000407 PyErr_SetString(PySSLErrorObject, "The write operation timed out");
408 return NULL;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000409 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
410 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
411 return NULL;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000412 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000413 do {
414 err = 0;
415 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis405a7952003-10-27 14:24:37 +0000416 len = SSL_write(self->ssl, data, count);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000417 err = SSL_get_error(self->ssl, len);
418 Py_END_ALLOW_THREADS
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000419 if(PyErr_CheckSignals()) {
420 return NULL;
421 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000422 if (err == SSL_ERROR_WANT_READ) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000423 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000424 } else if (err == SSL_ERROR_WANT_WRITE) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000425 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
426 } else {
427 sockstate = SOCKET_OPERATION_OK;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000428 }
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000429 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000430 PyErr_SetString(PySSLErrorObject, "The write operation timed out");
431 return NULL;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000432 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
433 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed.");
434 return NULL;
435 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
436 break;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000437 }
438 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000439 if (len > 0)
440 return PyInt_FromLong(len);
441 else
442 return PySSL_SetError(self, len);
443}
444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000446"write(s) -> len\n\
447\n\
448Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000450
451static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
452{
453 PyObject *buf;
454 int count = 0;
455 int len = 1024;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000456 int sockstate;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000457 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000458
459 if (!PyArg_ParseTuple(args, "|i:read", &len))
460 return NULL;
461
462 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
463 return NULL;
464
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000465 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
466 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000467 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
Neal Norwitza9002f82003-06-30 03:25:20 +0000468 Py_DECREF(buf);
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000469 return NULL;
470 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000471 do {
472 err = 0;
473 Py_BEGIN_ALLOW_THREADS
474 count = SSL_read(self->ssl, PyString_AsString(buf), len);
475 err = SSL_get_error(self->ssl, count);
476 Py_END_ALLOW_THREADS
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000477 if(PyErr_CheckSignals()) {
478 Py_DECREF(buf);
479 return NULL;
480 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000481 if (err == SSL_ERROR_WANT_READ) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000482 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000483 } else if (err == SSL_ERROR_WANT_WRITE) {
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000484 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
485 } else {
486 sockstate = SOCKET_OPERATION_OK;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000487 }
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000488 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000489 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000490 Py_DECREF(buf);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000491 return NULL;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000492 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
493 break;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000494 }
495 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000496 if (count <= 0) {
497 Py_DECREF(buf);
498 return PySSL_SetError(self, count);
499 }
Tim Peters5de98422002-04-27 18:44:32 +0000500 if (count != len)
501 _PyString_Resize(&buf, count);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000502 return buf;
503}
504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000505PyDoc_STRVAR(PySSL_SSLread_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000506"read([len]) -> string\n\
507\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000508Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000509
510static PyMethodDef PySSLMethods[] = {
511 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
512 PySSL_SSLwrite_doc},
513 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
514 PySSL_SSLread_doc},
515 {"server", (PyCFunction)PySSL_server, METH_NOARGS},
516 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
517 {NULL, NULL}
518};
519
520static PyObject *PySSL_getattr(PySSLObject *self, char *name)
521{
522 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
523}
524
Jeremy Hylton938ace62002-07-17 16:30:39 +0000525static PyTypeObject PySSL_Type = {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000526 PyObject_HEAD_INIT(NULL)
527 0, /*ob_size*/
528 "socket.SSL", /*tp_name*/
529 sizeof(PySSLObject), /*tp_basicsize*/
530 0, /*tp_itemsize*/
531 /* methods */
532 (destructor)PySSL_dealloc, /*tp_dealloc*/
533 0, /*tp_print*/
534 (getattrfunc)PySSL_getattr, /*tp_getattr*/
535 0, /*tp_setattr*/
536 0, /*tp_compare*/
537 0, /*tp_repr*/
538 0, /*tp_as_number*/
539 0, /*tp_as_sequence*/
540 0, /*tp_as_mapping*/
541 0, /*tp_hash*/
542};
543
544#ifdef HAVE_OPENSSL_RAND
545
546/* helper routines for seeding the SSL PRNG */
547static PyObject *
548PySSL_RAND_add(PyObject *self, PyObject *args)
549{
550 char *buf;
551 int len;
552 double entropy;
553
554 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
555 return NULL;
556 RAND_add(buf, len, entropy);
557 Py_INCREF(Py_None);
558 return Py_None;
559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000562"RAND_add(string, entropy)\n\
563\n\
564Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000565bound on the entropy contained in string.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000566
567static PyObject *
568PySSL_RAND_status(PyObject *self)
569{
570 return PyInt_FromLong(RAND_status());
571}
572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000573PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000574"RAND_status() -> 0 or 1\n\
575\n\
576Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
577It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000579
580static PyObject *
581PySSL_RAND_egd(PyObject *self, PyObject *arg)
582{
583 int bytes;
584
585 if (!PyString_Check(arg))
586 return PyErr_Format(PyExc_TypeError,
587 "RAND_egd() expected string, found %s",
588 arg->ob_type->tp_name);
589 bytes = RAND_egd(PyString_AS_STRING(arg));
590 if (bytes == -1) {
591 PyErr_SetString(PySSLErrorObject,
592 "EGD connection failed or EGD did not return "
593 "enough data to seed the PRNG");
594 return NULL;
595 }
596 return PyInt_FromLong(bytes);
597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000600"RAND_egd(path) -> bytes\n\
601\n\
602Queries the entropy gather daemon (EGD) on socket path. Returns number\n\
603of bytes read. Raises socket.sslerror if connection to EGD fails or\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000604if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000605
606#endif
607
608/* List of functions exported by this module. */
609
610static PyMethodDef PySSL_methods[] = {
611 {"ssl", PySocket_ssl,
612 METH_VARARGS, ssl_doc},
613#ifdef HAVE_OPENSSL_RAND
614 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
615 PySSL_RAND_add_doc},
616 {"RAND_egd", PySSL_RAND_egd, METH_O,
617 PySSL_RAND_egd_doc},
618 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
619 PySSL_RAND_status_doc},
620#endif
621 {NULL, NULL} /* Sentinel */
622};
623
624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000625PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000626"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000627for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000628
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000629PyMODINIT_FUNC
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000630init_ssl(void)
631{
632 PyObject *m, *d;
633
634 PySSL_Type.ob_type = &PyType_Type;
635
636 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000637 if (m == NULL)
638 return;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000639 d = PyModule_GetDict(m);
640
641 /* Load _socket module and its C API */
642 if (PySocketModule_ImportModuleAndAPI())
643 return;
644
645 /* Init OpenSSL */
646 SSL_load_error_strings();
647 SSLeay_add_ssl_algorithms();
648
649 /* Add symbols to module dict */
Brett Cannon06c34792004-03-23 23:16:54 +0000650 PySSLErrorObject = PyErr_NewException("socket.sslerror",
651 PySocketModule.error,
652 NULL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000653 if (PySSLErrorObject == NULL)
654 return;
655 PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
656 if (PyDict_SetItemString(d, "SSLType",
657 (PyObject *)&PySSL_Type) != 0)
658 return;
659 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000660 PY_SSL_ERROR_ZERO_RETURN);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000661 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000662 PY_SSL_ERROR_WANT_READ);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000663 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000664 PY_SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000665 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000666 PY_SSL_ERROR_WANT_X509_LOOKUP);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000667 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000668 PY_SSL_ERROR_SYSCALL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000669 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000670 PY_SSL_ERROR_SSL);
671 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
672 PY_SSL_ERROR_WANT_CONNECT);
673 /* non ssl.h errorcodes */
674 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
675 PY_SSL_ERROR_EOF);
676 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
677 PY_SSL_ERROR_INVALID_ERROR_CODE);
678
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000679}