blob: 8fe04283ab5037aae646d7f8eb4b422858e2e569 [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);
Neal Norwitz529baf22003-02-02 17:08:33 +000067static int wait_for_timeout(PySocketSockObject *s, int writing);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000068
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
76static PyObject *
77PySSL_SetError(PySSLObject *obj, int ret)
78{
79 PyObject *v, *n, *s;
80 char *errstr;
81 int err;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000082 enum py_ssl_error p;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000083
84 assert(ret <= 0);
85
86 err = SSL_get_error(obj->ssl, ret);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000087
88 switch (err) {
89 case SSL_ERROR_ZERO_RETURN:
90 errstr = "TLS/SSL connection has been closed";
Jeremy Hylton4e547302002-07-02 18:25:00 +000091 p = PY_SSL_ERROR_ZERO_RETURN;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000092 break;
93 case SSL_ERROR_WANT_READ:
94 errstr = "The operation did not complete (read)";
Jeremy Hylton4e547302002-07-02 18:25:00 +000095 p = PY_SSL_ERROR_WANT_READ;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000096 break;
97 case SSL_ERROR_WANT_WRITE:
Jeremy Hylton4e547302002-07-02 18:25:00 +000098 p = PY_SSL_ERROR_WANT_WRITE;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000099 errstr = "The operation did not complete (write)";
100 break;
101 case SSL_ERROR_WANT_X509_LOOKUP:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000102 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000103 errstr = "The operation did not complete (X509 lookup)";
104 break;
105 case SSL_ERROR_WANT_CONNECT:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000106 p = PY_SSL_ERROR_WANT_CONNECT;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000107 errstr = "The operation did not complete (connect)";
108 break;
109 case SSL_ERROR_SYSCALL:
110 {
111 unsigned long e = ERR_get_error();
Jeremy Hylton4e547302002-07-02 18:25:00 +0000112 if (e == 0) {
113 if (ret == 0) {
114 p = PY_SSL_ERROR_EOF;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000115 errstr = "EOF occurred in violation of protocol";
Jeremy Hylton4e547302002-07-02 18:25:00 +0000116 } else if (ret == -1) {
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000117 /* the underlying BIO reported an I/O error */
118 return obj->Socket->errorhandler();
Jeremy Hylton4e547302002-07-02 18:25:00 +0000119 } else { /* possible? */
120 p = PY_SSL_ERROR_SYSCALL;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000121 errstr = "Some I/O error occurred";
122 }
123 } else {
Jeremy Hylton4e547302002-07-02 18:25:00 +0000124 p = PY_SSL_ERROR_SYSCALL;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000125 /* 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 Hylton4e547302002-07-02 18:25:00 +0000133 p = PY_SSL_ERROR_SSL;
134 if (e != 0)
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000135 /* XXX Protected by global interpreter lock */
136 errstr = ERR_error_string(e, NULL);
Jeremy Hylton4e547302002-07-02 18:25:00 +0000137 else { /* possible? */
138 errstr = "A failure in the SSL library occurred";
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000139 }
140 break;
141 }
142 default:
Jeremy Hylton4e547302002-07-02 18:25:00 +0000143 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000144 errstr = "Invalid error code";
145 }
146 n = PyInt_FromLong((long) p);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000147 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000155 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000166static PySSLObject *
167newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
168{
169 PySSLObject *self;
170 char *errstr = NULL;
171 int ret;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000172 int err;
173 int timedout;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000174
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öwis09c35f72002-07-28 09:57:45 +0000192 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000193 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000194 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000195 if (self->ctx == NULL) {
196 errstr = "SSL_CTX_new error";
197 goto fail;
198 }
199
200 if (key_file) {
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000201 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000206 errstr = "SSL_CTX_use_PrivateKey_file error";
207 goto fail;
208 }
209
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000210 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000215 errstr = "SSL_CTX_use_certificate_chain_file error";
216 goto fail;
217 }
218 }
219
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000220 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000221 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öwis09c35f72002-07-28 09:57:45 +0000224 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000225 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000226
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öwis09c35f72002-07-28 09:57:45 +0000236 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000237 SSL_set_connect_state(self->ssl);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000238 Py_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000239
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000240 /* Actually negotiate SSL connection */
241 /* XXX If SSL_connect() returns 0, it's also a failure. */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000242 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
248 if (err == SSL_ERROR_WANT_READ) {
249 timedout = wait_for_timeout(Sock, 0);
250 } else if (err == SSL_ERROR_WANT_WRITE) {
251 timedout = wait_for_timeout(Sock, 1);
252 }
253 if (timedout) {
254 PyErr_SetString(PySSLErrorObject, "The connect operation timed out");
255 return NULL;
256 }
257 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000258 if (ret <= 0) {
259 PySSL_SetError(self, ret);
260 goto fail;
261 }
262 self->ssl->debug = 1;
263
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000264 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000265 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
266 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
267 self->server, X509_NAME_MAXLEN);
268 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
269 self->issuer, X509_NAME_MAXLEN);
270 }
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000271 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272 self->Socket = Sock;
273 Py_INCREF(self->Socket);
274 return self;
275 fail:
276 if (errstr)
277 PyErr_SetString(PySSLErrorObject, errstr);
278 Py_DECREF(self);
279 return NULL;
280}
281
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282static PyObject *
283PySocket_ssl(PyObject *self, PyObject *args)
284{
285 PySSLObject *rv;
286 PySocketSockObject *Sock;
287 char *key_file = NULL;
288 char *cert_file = NULL;
289
290 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
291 PySocketModule.Sock_Type,
292 (PyObject*)&Sock,
293 &key_file, &cert_file))
294 return NULL;
295
296 rv = newPySSLObject(Sock, key_file, cert_file);
297 if (rv == NULL)
298 return NULL;
299 return (PyObject *)rv;
300}
301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000302PyDoc_STRVAR(ssl_doc,
303"ssl(socket, [keyfile, certfile]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000304
305/* SSL object methods */
306
307static PyObject *
308PySSL_server(PySSLObject *self)
309{
310 return PyString_FromString(self->server);
311}
312
313static PyObject *
314PySSL_issuer(PySSLObject *self)
315{
316 return PyString_FromString(self->issuer);
317}
318
319
320static void PySSL_dealloc(PySSLObject *self)
321{
322 if (self->server_cert) /* Possible not to have one? */
323 X509_free (self->server_cert);
324 if (self->ssl)
325 SSL_free(self->ssl);
326 if (self->ctx)
327 SSL_CTX_free(self->ctx);
328 Py_XDECREF(self->Socket);
329 PyObject_Del(self);
330}
331
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000332/* If the socket has a timeout, do a select() on the socket.
333 The argument writing indicates the direction.
334 Return non-zero if the socket timed out, zero otherwise.
335 */
336static int
337wait_for_timeout(PySocketSockObject *s, int writing)
338{
339 fd_set fds;
340 struct timeval tv;
341 int rc;
342
343 /* Nothing to do unless we're in timeout mode (not non-blocking) */
344 if (s->sock_timeout <= 0.0)
345 return 0;
346
347 /* Guard against closed socket */
348 if (s->sock_fd < 0)
349 return 0;
350
351 /* Construct the arguments to select */
352 tv.tv_sec = (int)s->sock_timeout;
353 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
354 FD_ZERO(&fds);
355 FD_SET(s->sock_fd, &fds);
356
357 /* See if the socket is ready */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000358 Py_BEGIN_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000359 if (writing)
360 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
361 else
362 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000363 Py_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000364
365 /* Return 1 on timeout, 0 otherwise */
366 return rc == 0;
367}
368
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000369static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
370{
371 char *data;
372 int len;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000373 int timedout;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000374 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000375
376 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
377 return NULL;
378
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000379 timedout = wait_for_timeout(self->Socket, 1);
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000380 if (timedout) {
381 PyErr_SetString(PySSLErrorObject, "The write operation timed out");
382 return NULL;
383 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000384 do {
385 err = 0;
386 Py_BEGIN_ALLOW_THREADS
387 len = SSL_write(self->ssl, data, len);
388 err = SSL_get_error(self->ssl, len);
389 Py_END_ALLOW_THREADS
390 if (err == SSL_ERROR_WANT_READ) {
391 timedout = wait_for_timeout(self->Socket, 0);
392 } else if (err == SSL_ERROR_WANT_WRITE) {
393 timedout = wait_for_timeout(self->Socket, 1);
394 }
395 if (timedout) {
396 PyErr_SetString(PySSLErrorObject, "The write operation timed out");
397 return NULL;
398 }
399 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000400 if (len > 0)
401 return PyInt_FromLong(len);
402 else
403 return PySSL_SetError(self, len);
404}
405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000407"write(s) -> len\n\
408\n\
409Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000410of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000411
412static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
413{
414 PyObject *buf;
415 int count = 0;
416 int len = 1024;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000417 int timedout;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000418 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000419
420 if (!PyArg_ParseTuple(args, "|i:read", &len))
421 return NULL;
422
423 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
424 return NULL;
425
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000426 timedout = wait_for_timeout(self->Socket, 0);
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000427 if (timedout) {
428 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
429 return NULL;
430 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000431 do {
432 err = 0;
433 Py_BEGIN_ALLOW_THREADS
434 count = SSL_read(self->ssl, PyString_AsString(buf), len);
435 err = SSL_get_error(self->ssl, count);
436 Py_END_ALLOW_THREADS
437 if (err == SSL_ERROR_WANT_READ) {
438 timedout = wait_for_timeout(self->Socket, 0);
439 } else if (err == SSL_ERROR_WANT_WRITE) {
440 timedout = wait_for_timeout(self->Socket, 1);
441 }
442 if (timedout) {
443 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
444 return NULL;
445 }
446 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000447 if (count <= 0) {
448 Py_DECREF(buf);
449 return PySSL_SetError(self, count);
450 }
Tim Peters5de98422002-04-27 18:44:32 +0000451 if (count != len)
452 _PyString_Resize(&buf, count);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000453 return buf;
454}
455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(PySSL_SSLread_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000457"read([len]) -> string\n\
458\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000459Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000460
461static PyMethodDef PySSLMethods[] = {
462 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
463 PySSL_SSLwrite_doc},
464 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
465 PySSL_SSLread_doc},
466 {"server", (PyCFunction)PySSL_server, METH_NOARGS},
467 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
468 {NULL, NULL}
469};
470
471static PyObject *PySSL_getattr(PySSLObject *self, char *name)
472{
473 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
474}
475
Jeremy Hylton938ace62002-07-17 16:30:39 +0000476static PyTypeObject PySSL_Type = {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000477 PyObject_HEAD_INIT(NULL)
478 0, /*ob_size*/
479 "socket.SSL", /*tp_name*/
480 sizeof(PySSLObject), /*tp_basicsize*/
481 0, /*tp_itemsize*/
482 /* methods */
483 (destructor)PySSL_dealloc, /*tp_dealloc*/
484 0, /*tp_print*/
485 (getattrfunc)PySSL_getattr, /*tp_getattr*/
486 0, /*tp_setattr*/
487 0, /*tp_compare*/
488 0, /*tp_repr*/
489 0, /*tp_as_number*/
490 0, /*tp_as_sequence*/
491 0, /*tp_as_mapping*/
492 0, /*tp_hash*/
493};
494
495#ifdef HAVE_OPENSSL_RAND
496
497/* helper routines for seeding the SSL PRNG */
498static PyObject *
499PySSL_RAND_add(PyObject *self, PyObject *args)
500{
501 char *buf;
502 int len;
503 double entropy;
504
505 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
506 return NULL;
507 RAND_add(buf, len, entropy);
508 Py_INCREF(Py_None);
509 return Py_None;
510}
511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000512PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000513"RAND_add(string, entropy)\n\
514\n\
515Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000516bound on the entropy contained in string.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000517
518static PyObject *
519PySSL_RAND_status(PyObject *self)
520{
521 return PyInt_FromLong(RAND_status());
522}
523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000525"RAND_status() -> 0 or 1\n\
526\n\
527Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
528It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000530
531static PyObject *
532PySSL_RAND_egd(PyObject *self, PyObject *arg)
533{
534 int bytes;
535
536 if (!PyString_Check(arg))
537 return PyErr_Format(PyExc_TypeError,
538 "RAND_egd() expected string, found %s",
539 arg->ob_type->tp_name);
540 bytes = RAND_egd(PyString_AS_STRING(arg));
541 if (bytes == -1) {
542 PyErr_SetString(PySSLErrorObject,
543 "EGD connection failed or EGD did not return "
544 "enough data to seed the PRNG");
545 return NULL;
546 }
547 return PyInt_FromLong(bytes);
548}
549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000550PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000551"RAND_egd(path) -> bytes\n\
552\n\
553Queries the entropy gather daemon (EGD) on socket path. Returns number\n\
554of bytes read. Raises socket.sslerror if connection to EGD fails or\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000555if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000556
557#endif
558
559/* List of functions exported by this module. */
560
561static PyMethodDef PySSL_methods[] = {
562 {"ssl", PySocket_ssl,
563 METH_VARARGS, ssl_doc},
564#ifdef HAVE_OPENSSL_RAND
565 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
566 PySSL_RAND_add_doc},
567 {"RAND_egd", PySSL_RAND_egd, METH_O,
568 PySSL_RAND_egd_doc},
569 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
570 PySSL_RAND_status_doc},
571#endif
572 {NULL, NULL} /* Sentinel */
573};
574
575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000576PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000577"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000579
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000580PyMODINIT_FUNC
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000581init_ssl(void)
582{
583 PyObject *m, *d;
584
585 PySSL_Type.ob_type = &PyType_Type;
586
587 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
588 d = PyModule_GetDict(m);
589
590 /* Load _socket module and its C API */
591 if (PySocketModule_ImportModuleAndAPI())
592 return;
593
594 /* Init OpenSSL */
595 SSL_load_error_strings();
596 SSLeay_add_ssl_algorithms();
597
598 /* Add symbols to module dict */
599 PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
600 if (PySSLErrorObject == NULL)
601 return;
602 PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
603 if (PyDict_SetItemString(d, "SSLType",
604 (PyObject *)&PySSL_Type) != 0)
605 return;
606 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000607 PY_SSL_ERROR_ZERO_RETURN);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000608 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000609 PY_SSL_ERROR_WANT_READ);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000610 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000611 PY_SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000612 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000613 PY_SSL_ERROR_WANT_X509_LOOKUP);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000614 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000615 PY_SSL_ERROR_SYSCALL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000616 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000617 PY_SSL_ERROR_SSL);
618 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
619 PY_SSL_ERROR_WANT_CONNECT);
620 /* non ssl.h errorcodes */
621 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
622 PY_SSL_ERROR_EOF);
623 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
624 PY_SSL_ERROR_INVALID_ERROR_CODE);
625
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000626}