blob: 16edadd05f3dec22cce0dcc89d4e2571334e258c [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
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000248 if(PyErr_CheckSignals()) {
249 goto fail;
250 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000251 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öwisafec8e32003-06-28 07:40:23 +0000257 errstr = "The connect operation timed out";
258 goto fail;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000259 }
260 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000261 if (ret <= 0) {
262 PySSL_SetError(self, ret);
263 goto fail;
264 }
265 self->ssl->debug = 1;
266
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000267 Py_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000268 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öwis09c35f72002-07-28 09:57:45 +0000274 Py_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000275 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000285static PyObject *
286PySocket_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öwis14f8b4c2002-06-13 20:33:02 +0000305PyDoc_STRVAR(ssl_doc,
306"ssl(socket, [keyfile, certfile]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000307
308/* SSL object methods */
309
310static PyObject *
311PySSL_server(PySSLObject *self)
312{
313 return PyString_FromString(self->server);
314}
315
316static PyObject *
317PySSL_issuer(PySSLObject *self)
318{
319 return PyString_FromString(self->issuer);
320}
321
322
323static 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 Rossum99d4abf2003-01-27 22:22:50 +0000335/* 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 */
339static int
340wait_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 Rossum4f707ac2003-01-31 18:13:18 +0000361 Py_BEGIN_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000362 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 Rossum4f707ac2003-01-31 18:13:18 +0000366 Py_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000367
368 /* Return 1 on timeout, 0 otherwise */
369 return rc == 0;
370}
371
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000372static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
373{
374 char *data;
375 int len;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000376 int timedout;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000377 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000378
379 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
380 return NULL;
381
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000382 timedout = wait_for_timeout(self->Socket, 1);
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000383 if (timedout) {
384 PyErr_SetString(PySSLErrorObject, "The write operation timed out");
385 return NULL;
386 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000387 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öwisafec8e32003-06-28 07:40:23 +0000393 if(PyErr_CheckSignals()) {
394 return NULL;
395 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000396 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é Lemburga5d2b4c2002-02-16 18:23:30 +0000406 if (len > 0)
407 return PyInt_FromLong(len);
408 else
409 return PySSL_SetError(self, len);
410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000413"write(s) -> len\n\
414\n\
415Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000417
418static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
419{
420 PyObject *buf;
421 int count = 0;
422 int len = 1024;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000423 int timedout;
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000424 int err;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000425
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 Rossum99d4abf2003-01-27 22:22:50 +0000432 timedout = wait_for_timeout(self->Socket, 0);
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000433 if (timedout) {
434 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
435 return NULL;
436 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000437 do {
438 err = 0;
439 Py_BEGIN_ALLOW_THREADS
440 count = SSL_read(self->ssl, PyString_AsString(buf), len);
441 err = SSL_get_error(self->ssl, count);
442 Py_END_ALLOW_THREADS
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000443 if(PyErr_CheckSignals()) {
444 Py_DECREF(buf);
445 return NULL;
446 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000447 if (err == SSL_ERROR_WANT_READ) {
448 timedout = wait_for_timeout(self->Socket, 0);
449 } else if (err == SSL_ERROR_WANT_WRITE) {
450 timedout = wait_for_timeout(self->Socket, 1);
451 }
452 if (timedout) {
453 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
Martin v. Löwisafec8e32003-06-28 07:40:23 +0000454 Py_DECREF(buf);
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000455 return NULL;
456 }
457 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000458 if (count <= 0) {
459 Py_DECREF(buf);
460 return PySSL_SetError(self, count);
461 }
Tim Peters5de98422002-04-27 18:44:32 +0000462 if (count != len)
463 _PyString_Resize(&buf, count);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000464 return buf;
465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(PySSL_SSLread_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000468"read([len]) -> string\n\
469\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000471
472static PyMethodDef PySSLMethods[] = {
473 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
474 PySSL_SSLwrite_doc},
475 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
476 PySSL_SSLread_doc},
477 {"server", (PyCFunction)PySSL_server, METH_NOARGS},
478 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
479 {NULL, NULL}
480};
481
482static PyObject *PySSL_getattr(PySSLObject *self, char *name)
483{
484 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
485}
486
Jeremy Hylton938ace62002-07-17 16:30:39 +0000487static PyTypeObject PySSL_Type = {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000488 PyObject_HEAD_INIT(NULL)
489 0, /*ob_size*/
490 "socket.SSL", /*tp_name*/
491 sizeof(PySSLObject), /*tp_basicsize*/
492 0, /*tp_itemsize*/
493 /* methods */
494 (destructor)PySSL_dealloc, /*tp_dealloc*/
495 0, /*tp_print*/
496 (getattrfunc)PySSL_getattr, /*tp_getattr*/
497 0, /*tp_setattr*/
498 0, /*tp_compare*/
499 0, /*tp_repr*/
500 0, /*tp_as_number*/
501 0, /*tp_as_sequence*/
502 0, /*tp_as_mapping*/
503 0, /*tp_hash*/
504};
505
506#ifdef HAVE_OPENSSL_RAND
507
508/* helper routines for seeding the SSL PRNG */
509static PyObject *
510PySSL_RAND_add(PyObject *self, PyObject *args)
511{
512 char *buf;
513 int len;
514 double entropy;
515
516 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
517 return NULL;
518 RAND_add(buf, len, entropy);
519 Py_INCREF(Py_None);
520 return Py_None;
521}
522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000524"RAND_add(string, entropy)\n\
525\n\
526Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000527bound on the entropy contained in string.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000528
529static PyObject *
530PySSL_RAND_status(PyObject *self)
531{
532 return PyInt_FromLong(RAND_status());
533}
534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000536"RAND_status() -> 0 or 1\n\
537\n\
538Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
539It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000540using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000541
542static PyObject *
543PySSL_RAND_egd(PyObject *self, PyObject *arg)
544{
545 int bytes;
546
547 if (!PyString_Check(arg))
548 return PyErr_Format(PyExc_TypeError,
549 "RAND_egd() expected string, found %s",
550 arg->ob_type->tp_name);
551 bytes = RAND_egd(PyString_AS_STRING(arg));
552 if (bytes == -1) {
553 PyErr_SetString(PySSLErrorObject,
554 "EGD connection failed or EGD did not return "
555 "enough data to seed the PRNG");
556 return NULL;
557 }
558 return PyInt_FromLong(bytes);
559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000562"RAND_egd(path) -> bytes\n\
563\n\
564Queries the entropy gather daemon (EGD) on socket path. Returns number\n\
565of bytes read. Raises socket.sslerror if connection to EGD fails or\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000566if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000567
568#endif
569
570/* List of functions exported by this module. */
571
572static PyMethodDef PySSL_methods[] = {
573 {"ssl", PySocket_ssl,
574 METH_VARARGS, ssl_doc},
575#ifdef HAVE_OPENSSL_RAND
576 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
577 PySSL_RAND_add_doc},
578 {"RAND_egd", PySSL_RAND_egd, METH_O,
579 PySSL_RAND_egd_doc},
580 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
581 PySSL_RAND_status_doc},
582#endif
583 {NULL, NULL} /* Sentinel */
584};
585
586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000588"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000590
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000591PyMODINIT_FUNC
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000592init_ssl(void)
593{
594 PyObject *m, *d;
595
596 PySSL_Type.ob_type = &PyType_Type;
597
598 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
599 d = PyModule_GetDict(m);
600
601 /* Load _socket module and its C API */
602 if (PySocketModule_ImportModuleAndAPI())
603 return;
604
605 /* Init OpenSSL */
606 SSL_load_error_strings();
607 SSLeay_add_ssl_algorithms();
608
609 /* Add symbols to module dict */
610 PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
611 if (PySSLErrorObject == NULL)
612 return;
613 PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
614 if (PyDict_SetItemString(d, "SSLType",
615 (PyObject *)&PySSL_Type) != 0)
616 return;
617 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000618 PY_SSL_ERROR_ZERO_RETURN);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000619 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000620 PY_SSL_ERROR_WANT_READ);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000621 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000622 PY_SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000623 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000624 PY_SSL_ERROR_WANT_X509_LOOKUP);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000625 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000626 PY_SSL_ERROR_SYSCALL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000627 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000628 PY_SSL_ERROR_SSL);
629 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
630 PY_SSL_ERROR_WANT_CONNECT);
631 /* non ssl.h errorcodes */
632 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
633 PY_SSL_ERROR_EOF);
634 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
635 PY_SSL_ERROR_INVALID_ERROR_CODE);
636
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000637}