blob: 6fa65b294de9bb3fa4a4a99a3bbad15ed97ae02a [file] [log] [blame]
Thomas Woutersed03b412007-08-28 21:37:11 +00001/* SSL socket module
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002
3 SSL support based on patches by Brian E Gallew and Laszlo Kovacs.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00004 Re-worked a bit by Bill Janssen to add server-side support and
Bill Janssen6e027db2007-11-15 22:23:56 +00005 certificate decoding. Chris Stawarz contributed some non-blocking
6 patches.
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00007
Thomas Wouters1b7f8912007-09-19 03:06:30 +00008 This module is imported by ssl.py. It should *not* be used
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00009 directly.
10
Thomas Wouters1b7f8912007-09-19 03:06:30 +000011 XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE?
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +000012
13 XXX integrate several "shutdown modes" as suggested in
14 http://bugs.python.org/issue8108#msg102867 ?
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000015*/
16
17#include "Python.h"
Thomas Woutersed03b412007-08-28 21:37:11 +000018
Thomas Wouters1b7f8912007-09-19 03:06:30 +000019#ifdef WITH_THREAD
20#include "pythread.h"
21#define PySSL_BEGIN_ALLOW_THREADS { \
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000022 PyThreadState *_save = NULL; \
23 if (_ssl_locks_count>0) {_save = PyEval_SaveThread();}
24#define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)};
25#define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()};
26#define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \
27 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +000028
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000029#else /* no WITH_THREAD */
Thomas Wouters1b7f8912007-09-19 03:06:30 +000030
31#define PySSL_BEGIN_ALLOW_THREADS
32#define PySSL_BLOCK_THREADS
33#define PySSL_UNBLOCK_THREADS
34#define PySSL_END_ALLOW_THREADS
35
36#endif
37
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000038enum py_ssl_error {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000039 /* these mirror ssl.h */
40 PY_SSL_ERROR_NONE,
41 PY_SSL_ERROR_SSL,
42 PY_SSL_ERROR_WANT_READ,
43 PY_SSL_ERROR_WANT_WRITE,
44 PY_SSL_ERROR_WANT_X509_LOOKUP,
45 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
46 PY_SSL_ERROR_ZERO_RETURN,
47 PY_SSL_ERROR_WANT_CONNECT,
48 /* start of non ssl.h errorcodes */
49 PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */
50 PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */
51 PY_SSL_ERROR_INVALID_ERROR_CODE
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000052};
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000053
Thomas Woutersed03b412007-08-28 21:37:11 +000054enum py_ssl_server_or_client {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000055 PY_SSL_CLIENT,
56 PY_SSL_SERVER
Thomas Woutersed03b412007-08-28 21:37:11 +000057};
58
59enum py_ssl_cert_requirements {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000060 PY_SSL_CERT_NONE,
61 PY_SSL_CERT_OPTIONAL,
62 PY_SSL_CERT_REQUIRED
Thomas Woutersed03b412007-08-28 21:37:11 +000063};
64
65enum py_ssl_version {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +000066 PY_SSL_VERSION_SSL2,
67 PY_SSL_VERSION_SSL3,
68 PY_SSL_VERSION_SSL23,
69 PY_SSL_VERSION_TLS1
Thomas Woutersed03b412007-08-28 21:37:11 +000070};
71
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000072/* Include symbols from _socket module */
73#include "socketmodule.h"
74
Benjamin Petersonb173f782009-05-05 22:31:58 +000075static PySocketModule_APIObject PySocketModule;
76
Thomas Woutersed03b412007-08-28 21:37:11 +000077#if defined(HAVE_POLL_H)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#include <poll.h>
79#elif defined(HAVE_SYS_POLL_H)
80#include <sys/poll.h>
81#endif
82
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000083/* Include OpenSSL header files */
84#include "openssl/rsa.h"
85#include "openssl/crypto.h"
86#include "openssl/x509.h"
Thomas Wouters1b7f8912007-09-19 03:06:30 +000087#include "openssl/x509v3.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000088#include "openssl/pem.h"
89#include "openssl/ssl.h"
90#include "openssl/err.h"
91#include "openssl/rand.h"
92
93/* SSL error object */
94static PyObject *PySSLErrorObject;
95
Thomas Wouters1b7f8912007-09-19 03:06:30 +000096#ifdef WITH_THREAD
97
98/* serves as a flag to see whether we've initialized the SSL thread support. */
99/* 0 means no, greater than 0 means yes */
100
101static unsigned int _ssl_locks_count = 0;
102
103#endif /* def WITH_THREAD */
104
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000105/* SSL socket object */
106
107#define X509_NAME_MAXLEN 256
108
109/* RAND_* APIs got added to OpenSSL in 0.9.5 */
110#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
111# define HAVE_OPENSSL_RAND 1
112#else
113# undef HAVE_OPENSSL_RAND
114#endif
115
Gregory P. Smithbd4dacb2010-10-13 03:53:21 +0000116/* SSL_CTX_clear_options() and SSL_clear_options() were first added in
117 * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the
118 * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */
119#if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L
Antoine Pitroub5218772010-05-21 09:56:06 +0000120# define HAVE_SSL_CTX_CLEAR_OPTIONS
121#else
122# undef HAVE_SSL_CTX_CLEAR_OPTIONS
123#endif
124
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000125typedef struct {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000126 PyObject_HEAD
Antoine Pitrou152efa22010-05-16 18:19:27 +0000127 SSL_CTX *ctx;
128} PySSLContext;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000129
Antoine Pitrou152efa22010-05-16 18:19:27 +0000130typedef struct {
131 PyObject_HEAD
132 PyObject *Socket; /* weakref to socket on which we're layered */
133 SSL *ssl;
134 X509 *peer_cert;
135 int shutdown_seen_zero;
136} PySSLSocket;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000137
Antoine Pitrou152efa22010-05-16 18:19:27 +0000138static PyTypeObject PySSLContext_Type;
139static PyTypeObject PySSLSocket_Type;
140
141static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args);
142static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args);
Thomas Woutersed03b412007-08-28 21:37:11 +0000143static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000144 int writing);
Antoine Pitrou152efa22010-05-16 18:19:27 +0000145static PyObject *PySSL_peercert(PySSLSocket *self, PyObject *args);
146static PyObject *PySSL_cipher(PySSLSocket *self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000147
Antoine Pitrou152efa22010-05-16 18:19:27 +0000148#define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type)
149#define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000150
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000151typedef enum {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000152 SOCKET_IS_NONBLOCKING,
153 SOCKET_IS_BLOCKING,
154 SOCKET_HAS_TIMED_OUT,
155 SOCKET_HAS_BEEN_CLOSED,
156 SOCKET_TOO_LARGE_FOR_SELECT,
157 SOCKET_OPERATION_OK
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000158} timeout_state;
159
Thomas Woutersed03b412007-08-28 21:37:11 +0000160/* Wrap error strings with filename and line # */
161#define STRINGIFY1(x) #x
162#define STRINGIFY2(x) STRINGIFY1(x)
163#define ERRSTR1(x,y,z) (x ":" y ": " z)
164#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
165
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000166/* XXX It might be helpful to augment the error message generated
167 below with the name of the SSL function that generated the error.
168 I expect it's obvious most of the time.
169*/
170
171static PyObject *
Antoine Pitrou152efa22010-05-16 18:19:27 +0000172PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000173{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000174 PyObject *v;
175 char buf[2048];
176 char *errstr;
177 int err;
178 enum py_ssl_error p = PY_SSL_ERROR_NONE;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000179
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000180 assert(ret <= 0);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000181
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000182 if (obj->ssl != NULL) {
183 err = SSL_get_error(obj->ssl, ret);
Thomas Woutersed03b412007-08-28 21:37:11 +0000184
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000185 switch (err) {
186 case SSL_ERROR_ZERO_RETURN:
187 errstr = "TLS/SSL connection has been closed";
188 p = PY_SSL_ERROR_ZERO_RETURN;
189 break;
190 case SSL_ERROR_WANT_READ:
191 errstr = "The operation did not complete (read)";
192 p = PY_SSL_ERROR_WANT_READ;
193 break;
194 case SSL_ERROR_WANT_WRITE:
195 p = PY_SSL_ERROR_WANT_WRITE;
196 errstr = "The operation did not complete (write)";
197 break;
198 case SSL_ERROR_WANT_X509_LOOKUP:
199 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
Antoine Pitrou525807b2010-05-12 14:05:24 +0000200 errstr = "The operation did not complete (X509 lookup)";
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000201 break;
202 case SSL_ERROR_WANT_CONNECT:
203 p = PY_SSL_ERROR_WANT_CONNECT;
204 errstr = "The operation did not complete (connect)";
205 break;
206 case SSL_ERROR_SYSCALL:
207 {
208 unsigned long e = ERR_get_error();
209 if (e == 0) {
210 PySocketSockObject *s
211 = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
212 if (ret == 0 || (((PyObject *)s) == Py_None)) {
Antoine Pitrou525807b2010-05-12 14:05:24 +0000213 p = PY_SSL_ERROR_EOF;
214 errstr = "EOF occurred in violation of protocol";
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000215 } else if (ret == -1) {
Antoine Pitrou525807b2010-05-12 14:05:24 +0000216 /* underlying BIO reported an I/O error */
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000217 Py_INCREF(s);
Antoine Pitrou9d74b422010-05-16 23:14:22 +0000218 ERR_clear_error();
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000219 v = s->errorhandler();
220 Py_DECREF(s);
221 return v;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000222 } else { /* possible? */
Antoine Pitrou525807b2010-05-12 14:05:24 +0000223 p = PY_SSL_ERROR_SYSCALL;
224 errstr = "Some I/O error occurred";
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000225 }
226 } else {
227 p = PY_SSL_ERROR_SYSCALL;
228 /* XXX Protected by global interpreter lock */
229 errstr = ERR_error_string(e, NULL);
230 }
231 break;
232 }
233 case SSL_ERROR_SSL:
234 {
235 unsigned long e = ERR_get_error();
236 p = PY_SSL_ERROR_SSL;
237 if (e != 0)
238 /* XXX Protected by global interpreter lock */
239 errstr = ERR_error_string(e, NULL);
240 else { /* possible? */
Antoine Pitrou525807b2010-05-12 14:05:24 +0000241 errstr = "A failure in the SSL library occurred";
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000242 }
243 break;
244 }
245 default:
246 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
247 errstr = "Invalid error code";
248 }
249 } else {
250 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
251 }
252 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
Antoine Pitrou9d74b422010-05-16 23:14:22 +0000253 ERR_clear_error();
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000254 v = Py_BuildValue("(is)", p, buf);
255 if (v != NULL) {
256 PyErr_SetObject(PySSLErrorObject, v);
257 Py_DECREF(v);
258 }
259 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000260}
261
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000262static PyObject *
263_setSSLError (char *errstr, int errcode, char *filename, int lineno) {
264
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000265 char buf[2048];
266 PyObject *v;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000267
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000268 if (errstr == NULL) {
269 errcode = ERR_peek_last_error();
270 errstr = ERR_error_string(errcode, NULL);
271 }
272 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
Antoine Pitrou9d74b422010-05-16 23:14:22 +0000273 ERR_clear_error();
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000274 v = Py_BuildValue("(is)", errcode, buf);
275 if (v != NULL) {
276 PyErr_SetObject(PySSLErrorObject, v);
277 Py_DECREF(v);
278 }
279 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000280}
281
Antoine Pitrou152efa22010-05-16 18:19:27 +0000282static PySSLSocket *
283newPySSLSocket(SSL_CTX *ctx, PySocketSockObject *sock,
Antoine Pitroud5323212010-10-22 18:19:07 +0000284 enum py_ssl_server_or_client socket_type,
285 char *server_hostname)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000286{
Antoine Pitrou152efa22010-05-16 18:19:27 +0000287 PySSLSocket *self;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000288
Antoine Pitrou152efa22010-05-16 18:19:27 +0000289 self = PyObject_New(PySSLSocket, &PySSLSocket_Type);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000290 if (self == NULL)
291 return NULL;
Antoine Pitrou152efa22010-05-16 18:19:27 +0000292
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000293 self->peer_cert = NULL;
294 self->ssl = NULL;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000295 self->Socket = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000296
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000297 /* Make sure the SSL error state is initialized */
298 (void) ERR_get_state();
299 ERR_clear_error();
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000300
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000301 PySSL_BEGIN_ALLOW_THREADS
Antoine Pitrou152efa22010-05-16 18:19:27 +0000302 self->ssl = SSL_new(ctx);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000303 PySSL_END_ALLOW_THREADS
Antoine Pitrou152efa22010-05-16 18:19:27 +0000304 SSL_set_fd(self->ssl, sock->sock_fd);
Antoine Pitrou0ae7b582010-04-09 20:42:09 +0000305#ifdef SSL_MODE_AUTO_RETRY
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000306 SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY);
Antoine Pitrou0ae7b582010-04-09 20:42:09 +0000307#endif
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000308
Antoine Pitroud5323212010-10-22 18:19:07 +0000309#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
310 if (server_hostname != NULL)
311 SSL_set_tlsext_host_name(self->ssl, server_hostname);
312#endif
313
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000314 /* If the socket is in non-blocking mode or timeout mode, set the BIO
315 * to non-blocking mode (blocking is the default)
316 */
Antoine Pitrou152efa22010-05-16 18:19:27 +0000317 if (sock->sock_timeout >= 0.0) {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000318 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
319 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
320 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000321
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000322 PySSL_BEGIN_ALLOW_THREADS
323 if (socket_type == PY_SSL_CLIENT)
324 SSL_set_connect_state(self->ssl);
325 else
326 SSL_set_accept_state(self->ssl);
327 PySSL_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000328
Antoine Pitrou152efa22010-05-16 18:19:27 +0000329 self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000330 return self;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000331}
332
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000333/* SSL object methods */
334
Antoine Pitrou152efa22010-05-16 18:19:27 +0000335static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000336{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000337 int ret;
338 int err;
339 int sockstate, nonblocking;
340 PySocketSockObject *sock
341 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Antoine Pitroud3f8ab82010-04-24 21:26:44 +0000342
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000343 if (((PyObject*)sock) == Py_None) {
344 _setSSLError("Underlying socket connection gone",
345 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
346 return NULL;
347 }
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000348 Py_INCREF(sock);
Antoine Pitroud3f8ab82010-04-24 21:26:44 +0000349
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000350 /* just in case the blocking state of the socket has been changed */
351 nonblocking = (sock->sock_timeout >= 0.0);
352 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
353 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000354
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000355 /* Actually negotiate SSL connection */
356 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
357 sockstate = 0;
358 do {
Bill Janssen6e027db2007-11-15 22:23:56 +0000359 PySSL_BEGIN_ALLOW_THREADS
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000360 ret = SSL_do_handshake(self->ssl);
361 err = SSL_get_error(self->ssl, ret);
362 PySSL_END_ALLOW_THREADS
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000363 if (PyErr_CheckSignals())
364 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000365 if (err == SSL_ERROR_WANT_READ) {
366 sockstate = check_socket_and_wait_for_timeout(sock, 0);
367 } else if (err == SSL_ERROR_WANT_WRITE) {
368 sockstate = check_socket_and_wait_for_timeout(sock, 1);
369 } else {
370 sockstate = SOCKET_OPERATION_OK;
371 }
372 if (sockstate == SOCKET_HAS_TIMED_OUT) {
373 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou525807b2010-05-12 14:05:24 +0000374 ERRSTR("The handshake operation timed out"));
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000375 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000376 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
377 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou525807b2010-05-12 14:05:24 +0000378 ERRSTR("Underlying socket has been closed."));
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000379 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000380 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
381 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou525807b2010-05-12 14:05:24 +0000382 ERRSTR("Underlying socket too large for select()."));
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000383 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000384 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
385 break;
386 }
387 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000388 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000389 if (ret < 1)
390 return PySSL_SetError(self, ret, __FILE__, __LINE__);
Bill Janssen6e027db2007-11-15 22:23:56 +0000391
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000392 if (self->peer_cert)
393 X509_free (self->peer_cert);
394 PySSL_BEGIN_ALLOW_THREADS
395 self->peer_cert = SSL_get_peer_certificate(self->ssl);
396 PySSL_END_ALLOW_THREADS
397
398 Py_INCREF(Py_None);
399 return Py_None;
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +0000400
401error:
402 Py_DECREF(sock);
403 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000404}
405
Thomas Woutersed03b412007-08-28 21:37:11 +0000406static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000407_create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000408
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000409 char namebuf[X509_NAME_MAXLEN];
410 int buflen;
411 PyObject *name_obj;
412 PyObject *value_obj;
413 PyObject *attr;
414 unsigned char *valuebuf = NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000415
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000416 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
417 if (buflen < 0) {
418 _setSSLError(NULL, 0, __FILE__, __LINE__);
419 goto fail;
420 }
421 name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
422 if (name_obj == NULL)
423 goto fail;
Guido van Rossumf06628b2007-11-21 20:01:53 +0000424
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000425 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
426 if (buflen < 0) {
427 _setSSLError(NULL, 0, __FILE__, __LINE__);
428 Py_DECREF(name_obj);
429 goto fail;
430 }
431 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
Antoine Pitrou525807b2010-05-12 14:05:24 +0000432 buflen, "strict");
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000433 OPENSSL_free(valuebuf);
434 if (value_obj == NULL) {
435 Py_DECREF(name_obj);
436 goto fail;
437 }
438 attr = PyTuple_New(2);
439 if (attr == NULL) {
440 Py_DECREF(name_obj);
441 Py_DECREF(value_obj);
442 goto fail;
443 }
444 PyTuple_SET_ITEM(attr, 0, name_obj);
445 PyTuple_SET_ITEM(attr, 1, value_obj);
446 return attr;
Thomas Woutersed03b412007-08-28 21:37:11 +0000447
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000448 fail:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000449 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000450}
451
452static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000453_create_tuple_for_X509_NAME (X509_NAME *xname)
Thomas Woutersed03b412007-08-28 21:37:11 +0000454{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000455 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
456 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
457 PyObject *rdnt;
458 PyObject *attr = NULL; /* tuple to hold an attribute */
459 int entry_count = X509_NAME_entry_count(xname);
460 X509_NAME_ENTRY *entry;
461 ASN1_OBJECT *name;
462 ASN1_STRING *value;
463 int index_counter;
464 int rdn_level = -1;
465 int retcode;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000466
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000467 dn = PyList_New(0);
468 if (dn == NULL)
469 return NULL;
470 /* now create another tuple to hold the top-level RDN */
471 rdn = PyList_New(0);
472 if (rdn == NULL)
473 goto fail0;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000474
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000475 for (index_counter = 0;
476 index_counter < entry_count;
477 index_counter++)
478 {
479 entry = X509_NAME_get_entry(xname, index_counter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000480
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000481 /* check to see if we've gotten to a new RDN */
482 if (rdn_level >= 0) {
483 if (rdn_level != entry->set) {
484 /* yes, new RDN */
485 /* add old RDN to DN */
486 rdnt = PyList_AsTuple(rdn);
487 Py_DECREF(rdn);
488 if (rdnt == NULL)
489 goto fail0;
490 retcode = PyList_Append(dn, rdnt);
491 Py_DECREF(rdnt);
492 if (retcode < 0)
493 goto fail0;
494 /* create new RDN */
495 rdn = PyList_New(0);
496 if (rdn == NULL)
497 goto fail0;
498 }
499 }
500 rdn_level = entry->set;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000501
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000502 /* now add this attribute to the current RDN */
503 name = X509_NAME_ENTRY_get_object(entry);
504 value = X509_NAME_ENTRY_get_data(entry);
505 attr = _create_tuple_for_attribute(name, value);
506 /*
507 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
508 entry->set,
509 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
510 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
511 */
512 if (attr == NULL)
513 goto fail1;
514 retcode = PyList_Append(rdn, attr);
515 Py_DECREF(attr);
516 if (retcode < 0)
517 goto fail1;
518 }
519 /* now, there's typically a dangling RDN */
520 if ((rdn != NULL) && (PyList_Size(rdn) > 0)) {
521 rdnt = PyList_AsTuple(rdn);
522 Py_DECREF(rdn);
523 if (rdnt == NULL)
524 goto fail0;
525 retcode = PyList_Append(dn, rdnt);
526 Py_DECREF(rdnt);
527 if (retcode < 0)
528 goto fail0;
529 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000530
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000531 /* convert list to tuple */
532 rdnt = PyList_AsTuple(dn);
533 Py_DECREF(dn);
534 if (rdnt == NULL)
535 return NULL;
536 return rdnt;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000537
538 fail1:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000539 Py_XDECREF(rdn);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000540
541 fail0:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000542 Py_XDECREF(dn);
543 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000544}
545
546static PyObject *
547_get_peer_alt_names (X509 *certificate) {
Guido van Rossumf06628b2007-11-21 20:01:53 +0000548
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000549 /* this code follows the procedure outlined in
550 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
551 function to extract the STACK_OF(GENERAL_NAME),
552 then iterates through the stack to add the
553 names. */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000554
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000555 int i, j;
556 PyObject *peer_alt_names = Py_None;
557 PyObject *v, *t;
558 X509_EXTENSION *ext = NULL;
559 GENERAL_NAMES *names = NULL;
560 GENERAL_NAME *name;
Benjamin Petersoneb1410f2010-10-13 22:06:39 +0000561 const X509V3_EXT_METHOD *method;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000562 BIO *biobuf = NULL;
563 char buf[2048];
564 char *vptr;
565 int len;
566 /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
Victor Stinner7124a412010-03-02 22:48:17 +0000567#if OPENSSL_VERSION_NUMBER >= 0x009060dfL
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000568 const unsigned char *p;
Victor Stinner7124a412010-03-02 22:48:17 +0000569#else
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000570 unsigned char *p;
Victor Stinner7124a412010-03-02 22:48:17 +0000571#endif
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000572
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000573 if (certificate == NULL)
574 return peer_alt_names;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000575
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000576 /* get a memory buffer */
577 biobuf = BIO_new(BIO_s_mem());
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000578
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000579 i = 0;
580 while ((i = X509_get_ext_by_NID(
581 certificate, NID_subject_alt_name, i)) >= 0) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000582
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000583 if (peer_alt_names == Py_None) {
584 peer_alt_names = PyList_New(0);
585 if (peer_alt_names == NULL)
586 goto fail;
587 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000588
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000589 /* now decode the altName */
590 ext = X509_get_ext(certificate, i);
591 if(!(method = X509V3_EXT_get(ext))) {
592 PyErr_SetString
593 (PySSLErrorObject,
594 ERRSTR("No method for internalizing subjectAltName!"));
595 goto fail;
596 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000597
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000598 p = ext->value->data;
599 if (method->it)
600 names = (GENERAL_NAMES*)
601 (ASN1_item_d2i(NULL,
602 &p,
603 ext->value->length,
604 ASN1_ITEM_ptr(method->it)));
605 else
606 names = (GENERAL_NAMES*)
607 (method->d2i(NULL,
608 &p,
609 ext->value->length));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000610
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000611 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000612
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000613 /* get a rendering of each name in the set of names */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000614
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000615 name = sk_GENERAL_NAME_value(names, j);
616 if (name->type == GEN_DIRNAME) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000617
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000618 /* we special-case DirName as a tuple of
619 tuples of attributes */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000620
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000621 t = PyTuple_New(2);
622 if (t == NULL) {
623 goto fail;
624 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000625
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000626 v = PyUnicode_FromString("DirName");
627 if (v == NULL) {
628 Py_DECREF(t);
629 goto fail;
630 }
631 PyTuple_SET_ITEM(t, 0, v);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000632
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000633 v = _create_tuple_for_X509_NAME (name->d.dirn);
634 if (v == NULL) {
635 Py_DECREF(t);
636 goto fail;
637 }
638 PyTuple_SET_ITEM(t, 1, v);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000639
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000640 } else {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000641
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000642 /* for everything else, we use the OpenSSL print form */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000643
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000644 (void) BIO_reset(biobuf);
645 GENERAL_NAME_print(biobuf, name);
646 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
647 if (len < 0) {
648 _setSSLError(NULL, 0, __FILE__, __LINE__);
649 goto fail;
650 }
651 vptr = strchr(buf, ':');
652 if (vptr == NULL)
653 goto fail;
654 t = PyTuple_New(2);
655 if (t == NULL)
656 goto fail;
657 v = PyUnicode_FromStringAndSize(buf, (vptr - buf));
658 if (v == NULL) {
659 Py_DECREF(t);
660 goto fail;
661 }
662 PyTuple_SET_ITEM(t, 0, v);
663 v = PyUnicode_FromStringAndSize((vptr + 1),
664 (len - (vptr - buf + 1)));
665 if (v == NULL) {
666 Py_DECREF(t);
667 goto fail;
668 }
669 PyTuple_SET_ITEM(t, 1, v);
670 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000671
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000672 /* and add that rendering to the list */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000673
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000674 if (PyList_Append(peer_alt_names, t) < 0) {
675 Py_DECREF(t);
676 goto fail;
677 }
678 Py_DECREF(t);
679 }
680 }
681 BIO_free(biobuf);
682 if (peer_alt_names != Py_None) {
683 v = PyList_AsTuple(peer_alt_names);
684 Py_DECREF(peer_alt_names);
685 return v;
686 } else {
687 return peer_alt_names;
688 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000689
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000690
691 fail:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000692 if (biobuf != NULL)
693 BIO_free(biobuf);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000694
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000695 if (peer_alt_names != Py_None) {
696 Py_XDECREF(peer_alt_names);
697 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000698
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000699 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000700}
701
702static PyObject *
703_decode_certificate (X509 *certificate, int verbose) {
704
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000705 PyObject *retval = NULL;
706 BIO *biobuf = NULL;
707 PyObject *peer;
708 PyObject *peer_alt_names = NULL;
709 PyObject *issuer;
710 PyObject *version;
711 PyObject *sn_obj;
712 ASN1_INTEGER *serialNumber;
713 char buf[2048];
714 int len;
715 ASN1_TIME *notBefore, *notAfter;
716 PyObject *pnotBefore, *pnotAfter;
Thomas Woutersed03b412007-08-28 21:37:11 +0000717
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000718 retval = PyDict_New();
719 if (retval == NULL)
720 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000721
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000722 peer = _create_tuple_for_X509_NAME(
723 X509_get_subject_name(certificate));
724 if (peer == NULL)
725 goto fail0;
726 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
727 Py_DECREF(peer);
728 goto fail0;
729 }
730 Py_DECREF(peer);
Thomas Woutersed03b412007-08-28 21:37:11 +0000731
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000732 if (verbose) {
733 issuer = _create_tuple_for_X509_NAME(
734 X509_get_issuer_name(certificate));
735 if (issuer == NULL)
736 goto fail0;
737 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
738 Py_DECREF(issuer);
739 goto fail0;
740 }
741 Py_DECREF(issuer);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000742
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000743 version = PyLong_FromLong(X509_get_version(certificate) + 1);
744 if (PyDict_SetItemString(retval, "version", version) < 0) {
745 Py_DECREF(version);
746 goto fail0;
747 }
748 Py_DECREF(version);
749 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000750
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000751 /* get a memory buffer */
752 biobuf = BIO_new(BIO_s_mem());
Guido van Rossumf06628b2007-11-21 20:01:53 +0000753
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000754 if (verbose) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000755
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000756 (void) BIO_reset(biobuf);
757 serialNumber = X509_get_serialNumber(certificate);
758 /* should not exceed 20 octets, 160 bits, so buf is big enough */
759 i2a_ASN1_INTEGER(biobuf, serialNumber);
760 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
761 if (len < 0) {
762 _setSSLError(NULL, 0, __FILE__, __LINE__);
763 goto fail1;
764 }
765 sn_obj = PyUnicode_FromStringAndSize(buf, len);
766 if (sn_obj == NULL)
767 goto fail1;
768 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
769 Py_DECREF(sn_obj);
770 goto fail1;
771 }
772 Py_DECREF(sn_obj);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000773
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000774 (void) BIO_reset(biobuf);
775 notBefore = X509_get_notBefore(certificate);
776 ASN1_TIME_print(biobuf, notBefore);
777 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
778 if (len < 0) {
779 _setSSLError(NULL, 0, __FILE__, __LINE__);
780 goto fail1;
781 }
782 pnotBefore = PyUnicode_FromStringAndSize(buf, len);
783 if (pnotBefore == NULL)
784 goto fail1;
785 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
786 Py_DECREF(pnotBefore);
787 goto fail1;
788 }
789 Py_DECREF(pnotBefore);
790 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000791
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000792 (void) BIO_reset(biobuf);
793 notAfter = X509_get_notAfter(certificate);
794 ASN1_TIME_print(biobuf, notAfter);
795 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
796 if (len < 0) {
797 _setSSLError(NULL, 0, __FILE__, __LINE__);
798 goto fail1;
799 }
800 pnotAfter = PyUnicode_FromStringAndSize(buf, len);
801 if (pnotAfter == NULL)
802 goto fail1;
803 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
804 Py_DECREF(pnotAfter);
805 goto fail1;
806 }
807 Py_DECREF(pnotAfter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000808
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000809 /* Now look for subjectAltName */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000810
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000811 peer_alt_names = _get_peer_alt_names(certificate);
812 if (peer_alt_names == NULL)
813 goto fail1;
814 else if (peer_alt_names != Py_None) {
815 if (PyDict_SetItemString(retval, "subjectAltName",
816 peer_alt_names) < 0) {
817 Py_DECREF(peer_alt_names);
818 goto fail1;
819 }
820 Py_DECREF(peer_alt_names);
821 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000822
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000823 BIO_free(biobuf);
824 return retval;
Thomas Woutersed03b412007-08-28 21:37:11 +0000825
826 fail1:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000827 if (biobuf != NULL)
828 BIO_free(biobuf);
Thomas Woutersed03b412007-08-28 21:37:11 +0000829 fail0:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000830 Py_XDECREF(retval);
831 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000832}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000833
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000834
835static PyObject *
836PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
837
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000838 PyObject *retval = NULL;
Victor Stinner3800e1e2010-05-16 21:23:48 +0000839 PyObject *filename;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000840 X509 *x=NULL;
841 BIO *cert;
842 int verbose = 1;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000843
Victor Stinner3800e1e2010-05-16 21:23:48 +0000844 if (!PyArg_ParseTuple(args, "O&|i:test_decode_certificate",
845 PyUnicode_FSConverter, &filename, &verbose))
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000846 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000847
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000848 if ((cert=BIO_new(BIO_s_file())) == NULL) {
849 PyErr_SetString(PySSLErrorObject,
850 "Can't malloc memory to read file");
851 goto fail0;
852 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000853
Victor Stinner3800e1e2010-05-16 21:23:48 +0000854 if (BIO_read_filename(cert, PyBytes_AsString(filename)) <= 0) {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000855 PyErr_SetString(PySSLErrorObject,
856 "Can't open file");
857 goto fail0;
858 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000859
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000860 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
861 if (x == NULL) {
862 PyErr_SetString(PySSLErrorObject,
863 "Error decoding PEM-encoded file");
864 goto fail0;
865 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000866
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000867 retval = _decode_certificate(x, verbose);
Mark Dickinsonee55df52010-08-03 18:31:54 +0000868 X509_free(x);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000869
870 fail0:
Victor Stinner3800e1e2010-05-16 21:23:48 +0000871 Py_DECREF(filename);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000872 if (cert != NULL) BIO_free(cert);
873 return retval;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000874}
875
876
877static PyObject *
Antoine Pitrou152efa22010-05-16 18:19:27 +0000878PySSL_peercert(PySSLSocket *self, PyObject *args)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000879{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000880 PyObject *retval = NULL;
881 int len;
882 int verification;
883 PyObject *binary_mode = Py_None;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000884
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000885 if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
886 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000887
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000888 if (!self->peer_cert)
889 Py_RETURN_NONE;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000890
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000891 if (PyObject_IsTrue(binary_mode)) {
892 /* return cert in DER-encoded format */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000893
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000894 unsigned char *bytes_buf = NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000895
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000896 bytes_buf = NULL;
897 len = i2d_X509(self->peer_cert, &bytes_buf);
898 if (len < 0) {
899 PySSL_SetError(self, len, __FILE__, __LINE__);
900 return NULL;
901 }
902 /* this is actually an immutable bytes sequence */
903 retval = PyBytes_FromStringAndSize
904 ((const char *) bytes_buf, len);
905 OPENSSL_free(bytes_buf);
906 return retval;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000907
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000908 } else {
Antoine Pitrou152efa22010-05-16 18:19:27 +0000909 verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl));
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000910 if ((verification & SSL_VERIFY_PEER) == 0)
911 return PyDict_New();
912 else
913 return _decode_certificate (self->peer_cert, 0);
914 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000915}
916
917PyDoc_STRVAR(PySSL_peercert_doc,
918"peer_certificate([der=False]) -> certificate\n\
919\n\
920Returns the certificate for the peer. If no certificate was provided,\n\
921returns None. If a certificate was provided, but not validated, returns\n\
922an empty dictionary. Otherwise returns a dict containing information\n\
923about the peer certificate.\n\
924\n\
925If the optional argument is True, returns a DER-encoded copy of the\n\
926peer certificate, or None if no certificate was provided. This will\n\
927return the certificate even if it wasn't validated.");
928
Antoine Pitrou152efa22010-05-16 18:19:27 +0000929static PyObject *PySSL_cipher (PySSLSocket *self) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000930
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000931 PyObject *retval, *v;
Benjamin Petersoneb1410f2010-10-13 22:06:39 +0000932 const SSL_CIPHER *current;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000933 char *cipher_name;
934 char *cipher_protocol;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000935
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000936 if (self->ssl == NULL)
937 return Py_None;
938 current = SSL_get_current_cipher(self->ssl);
939 if (current == NULL)
940 return Py_None;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000941
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000942 retval = PyTuple_New(3);
943 if (retval == NULL)
944 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000945
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000946 cipher_name = (char *) SSL_CIPHER_get_name(current);
947 if (cipher_name == NULL) {
948 PyTuple_SET_ITEM(retval, 0, Py_None);
949 } else {
950 v = PyUnicode_FromString(cipher_name);
951 if (v == NULL)
952 goto fail0;
953 PyTuple_SET_ITEM(retval, 0, v);
954 }
955 cipher_protocol = SSL_CIPHER_get_version(current);
956 if (cipher_protocol == NULL) {
957 PyTuple_SET_ITEM(retval, 1, Py_None);
958 } else {
959 v = PyUnicode_FromString(cipher_protocol);
960 if (v == NULL)
961 goto fail0;
962 PyTuple_SET_ITEM(retval, 1, v);
963 }
964 v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL));
965 if (v == NULL)
966 goto fail0;
967 PyTuple_SET_ITEM(retval, 2, v);
968 return retval;
Guido van Rossumf06628b2007-11-21 20:01:53 +0000969
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000970 fail0:
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000971 Py_DECREF(retval);
972 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000973}
974
Antoine Pitrou152efa22010-05-16 18:19:27 +0000975static void PySSL_dealloc(PySSLSocket *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000976{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000977 if (self->peer_cert) /* Possible not to have one? */
978 X509_free (self->peer_cert);
979 if (self->ssl)
980 SSL_free(self->ssl);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000981 Py_XDECREF(self->Socket);
982 PyObject_Del(self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000983}
984
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000985/* If the socket has a timeout, do a select()/poll() on the socket.
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000986 The argument writing indicates the direction.
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000987 Returns one of the possibilities in the timeout_state enum (above).
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000988 */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000989
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000990static int
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000991check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000992{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000993 fd_set fds;
994 struct timeval tv;
995 int rc;
Guido van Rossum99d4abf2003-01-27 22:22:50 +0000996
Antoine Pitroucbb82eb2010-05-05 15:57:33 +0000997 /* Nothing to do unless we're in timeout mode (not non-blocking) */
998 if (s->sock_timeout < 0.0)
999 return SOCKET_IS_BLOCKING;
1000 else if (s->sock_timeout == 0.0)
1001 return SOCKET_IS_NONBLOCKING;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001002
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001003 /* Guard against closed socket */
1004 if (s->sock_fd < 0)
1005 return SOCKET_HAS_BEEN_CLOSED;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001006
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001007 /* Prefer poll, if available, since you can poll() any fd
1008 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001009#ifdef HAVE_POLL
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001010 {
1011 struct pollfd pollfd;
1012 int timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001014 pollfd.fd = s->sock_fd;
1015 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001016
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001017 /* s->sock_timeout is in seconds, timeout in ms */
1018 timeout = (int)(s->sock_timeout * 1000 + 0.5);
1019 PySSL_BEGIN_ALLOW_THREADS
1020 rc = poll(&pollfd, 1, timeout);
1021 PySSL_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001022
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001023 goto normal_return;
1024 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001025#endif
1026
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001027 /* Guard against socket too large for select*/
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001028#ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001029 if (s->sock_fd >= FD_SETSIZE)
1030 return SOCKET_TOO_LARGE_FOR_SELECT;
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001031#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +00001032
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001033 /* Construct the arguments to select */
1034 tv.tv_sec = (int)s->sock_timeout;
1035 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1036 FD_ZERO(&fds);
1037 FD_SET(s->sock_fd, &fds);
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001038
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001039 /* See if the socket is ready */
1040 PySSL_BEGIN_ALLOW_THREADS
1041 if (writing)
1042 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
1043 else
1044 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
1045 PySSL_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001046
Bill Janssen6e027db2007-11-15 22:23:56 +00001047#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001048normal_return:
Bill Janssen6e027db2007-11-15 22:23:56 +00001049#endif
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001050 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
1051 (when we are able to write or when there's something to read) */
1052 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001053}
1054
Antoine Pitrou152efa22010-05-16 18:19:27 +00001055static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001056{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001057 Py_buffer buf;
1058 int len;
1059 int sockstate;
1060 int err;
1061 int nonblocking;
1062 PySocketSockObject *sock
1063 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen54cc54c2007-12-14 22:08:56 +00001064
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001065 if (((PyObject*)sock) == Py_None) {
1066 _setSSLError("Underlying socket connection gone",
1067 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1068 return NULL;
1069 }
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001070 Py_INCREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001071
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001072 if (!PyArg_ParseTuple(args, "y*:write", &buf)) {
1073 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001074 return NULL;
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001075 }
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001076
1077 /* just in case the blocking state of the socket has been changed */
1078 nonblocking = (sock->sock_timeout >= 0.0);
1079 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1080 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1081
1082 sockstate = check_socket_and_wait_for_timeout(sock, 1);
1083 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1084 PyErr_SetString(PySSLErrorObject,
1085 "The write operation timed out");
1086 goto error;
1087 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1088 PyErr_SetString(PySSLErrorObject,
1089 "Underlying socket has been closed.");
1090 goto error;
1091 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1092 PyErr_SetString(PySSLErrorObject,
1093 "Underlying socket too large for select().");
1094 goto error;
1095 }
1096 do {
1097 err = 0;
1098 PySSL_BEGIN_ALLOW_THREADS
1099 len = SSL_write(self->ssl, buf.buf, buf.len);
1100 err = SSL_get_error(self->ssl, len);
1101 PySSL_END_ALLOW_THREADS
1102 if (PyErr_CheckSignals()) {
1103 goto error;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001104 }
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001105 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitrou525807b2010-05-12 14:05:24 +00001106 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001107 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitrou525807b2010-05-12 14:05:24 +00001108 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001109 } else {
1110 sockstate = SOCKET_OPERATION_OK;
1111 }
1112 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1113 PyErr_SetString(PySSLErrorObject,
1114 "The write operation timed out");
1115 goto error;
1116 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1117 PyErr_SetString(PySSLErrorObject,
1118 "Underlying socket has been closed.");
1119 goto error;
1120 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1121 break;
1122 }
1123 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001124
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001125 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001126 PyBuffer_Release(&buf);
1127 if (len > 0)
1128 return PyLong_FromLong(len);
1129 else
1130 return PySSL_SetError(self, len, __FILE__, __LINE__);
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001131
1132error:
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001133 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001134 PyBuffer_Release(&buf);
1135 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001136}
1137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001138PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001139"write(s) -> len\n\
1140\n\
1141Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001143
Antoine Pitrou152efa22010-05-16 18:19:27 +00001144static PyObject *PySSL_SSLpending(PySSLSocket *self)
Bill Janssen6e027db2007-11-15 22:23:56 +00001145{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001146 int count = 0;
Bill Janssen6e027db2007-11-15 22:23:56 +00001147
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001148 PySSL_BEGIN_ALLOW_THREADS
1149 count = SSL_pending(self->ssl);
1150 PySSL_END_ALLOW_THREADS
1151 if (count < 0)
1152 return PySSL_SetError(self, count, __FILE__, __LINE__);
1153 else
1154 return PyLong_FromLong(count);
Bill Janssen6e027db2007-11-15 22:23:56 +00001155}
1156
1157PyDoc_STRVAR(PySSL_SSLpending_doc,
1158"pending() -> count\n\
1159\n\
1160Returns the number of already decrypted bytes available for read,\n\
1161pending on the connection.\n");
1162
Antoine Pitrou152efa22010-05-16 18:19:27 +00001163static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001164{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001165 PyObject *dest = NULL;
1166 Py_buffer buf;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001167 char *mem;
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001168 int len, count;
1169 int buf_passed = 0;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001170 int sockstate;
1171 int err;
1172 int nonblocking;
1173 PySocketSockObject *sock
1174 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen54cc54c2007-12-14 22:08:56 +00001175
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001176 if (((PyObject*)sock) == Py_None) {
1177 _setSSLError("Underlying socket connection gone",
1178 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1179 return NULL;
1180 }
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001181 Py_INCREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001182
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001183 buf.obj = NULL;
1184 buf.buf = NULL;
1185 if (!PyArg_ParseTuple(args, "i|w*:read", &len, &buf))
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001186 goto error;
1187
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001188 if ((buf.buf == NULL) && (buf.obj == NULL)) {
1189 dest = PyBytes_FromStringAndSize(NULL, len);
1190 if (dest == NULL)
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001191 goto error;
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001192 mem = PyBytes_AS_STRING(dest);
1193 }
1194 else {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001195 buf_passed = 1;
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001196 mem = buf.buf;
1197 if (len <= 0 || len > buf.len) {
1198 len = (int) buf.len;
1199 if (buf.len != len) {
1200 PyErr_SetString(PyExc_OverflowError,
1201 "maximum length can't fit in a C 'int'");
1202 goto error;
1203 }
1204 }
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001205 }
1206
1207 /* just in case the blocking state of the socket has been changed */
1208 nonblocking = (sock->sock_timeout >= 0.0);
1209 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1210 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1211
1212 /* first check if there are bytes ready to be read */
1213 PySSL_BEGIN_ALLOW_THREADS
1214 count = SSL_pending(self->ssl);
1215 PySSL_END_ALLOW_THREADS
1216
1217 if (!count) {
1218 sockstate = check_socket_and_wait_for_timeout(sock, 0);
1219 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1220 PyErr_SetString(PySSLErrorObject,
1221 "The read operation timed out");
1222 goto error;
1223 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1224 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou525807b2010-05-12 14:05:24 +00001225 "Underlying socket too large for select().");
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001226 goto error;
1227 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1228 count = 0;
1229 goto done;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001230 }
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001231 }
1232 do {
1233 err = 0;
1234 PySSL_BEGIN_ALLOW_THREADS
1235 count = SSL_read(self->ssl, mem, len);
1236 err = SSL_get_error(self->ssl, count);
1237 PySSL_END_ALLOW_THREADS
1238 if (PyErr_CheckSignals())
1239 goto error;
1240 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitrou525807b2010-05-12 14:05:24 +00001241 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001242 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitrou525807b2010-05-12 14:05:24 +00001243 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001244 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
1245 (SSL_get_shutdown(self->ssl) ==
1246 SSL_RECEIVED_SHUTDOWN))
1247 {
1248 count = 0;
1249 goto done;
1250 } else {
1251 sockstate = SOCKET_OPERATION_OK;
1252 }
1253 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1254 PyErr_SetString(PySSLErrorObject,
1255 "The read operation timed out");
1256 goto error;
1257 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1258 break;
1259 }
1260 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
1261 if (count <= 0) {
1262 PySSL_SetError(self, count, __FILE__, __LINE__);
1263 goto error;
1264 }
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001265
1266done:
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001267 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001268 if (!buf_passed) {
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001269 _PyBytes_Resize(&dest, count);
1270 return dest;
1271 }
1272 else {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001273 PyBuffer_Release(&buf);
1274 return PyLong_FromLong(count);
1275 }
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001276
1277error:
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001278 Py_DECREF(sock);
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001279 if (!buf_passed)
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001280 Py_XDECREF(dest);
Antoine Pitrou24e561a2010-09-03 18:38:17 +00001281 else
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001282 PyBuffer_Release(&buf);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001283 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001284}
1285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001286PyDoc_STRVAR(PySSL_SSLread_doc,
Bill Janssen6e027db2007-11-15 22:23:56 +00001287"read([len]) -> string\n\
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001288\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001289Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001290
Antoine Pitrou152efa22010-05-16 18:19:27 +00001291static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
Bill Janssen40a0f662008-08-12 16:56:25 +00001292{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001293 int err, ssl_err, sockstate, nonblocking;
1294 int zeros = 0;
1295 PySocketSockObject *sock
1296 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen40a0f662008-08-12 16:56:25 +00001297
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001298 /* Guard against closed socket */
1299 if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
1300 _setSSLError("Underlying socket connection gone",
1301 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1302 return NULL;
1303 }
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001304 Py_INCREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001305
1306 /* Just in case the blocking state of the socket has been changed */
1307 nonblocking = (sock->sock_timeout >= 0.0);
1308 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1309 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1310
1311 while (1) {
1312 PySSL_BEGIN_ALLOW_THREADS
1313 /* Disable read-ahead so that unwrap can work correctly.
1314 * Otherwise OpenSSL might read in too much data,
1315 * eating clear text data that happens to be
1316 * transmitted after the SSL shutdown.
1317 * Should be safe to call repeatedly everytime this
1318 * function is used and the shutdown_seen_zero != 0
1319 * condition is met.
1320 */
1321 if (self->shutdown_seen_zero)
1322 SSL_set_read_ahead(self->ssl, 0);
1323 err = SSL_shutdown(self->ssl);
1324 PySSL_END_ALLOW_THREADS
1325 /* If err == 1, a secure shutdown with SSL_shutdown() is complete */
1326 if (err > 0)
1327 break;
1328 if (err == 0) {
1329 /* Don't loop endlessly; instead preserve legacy
1330 behaviour of trying SSL_shutdown() only twice.
1331 This looks necessary for OpenSSL < 0.9.8m */
1332 if (++zeros > 1)
1333 break;
1334 /* Shutdown was sent, now try receiving */
1335 self->shutdown_seen_zero = 1;
1336 continue;
Bill Janssen40a0f662008-08-12 16:56:25 +00001337 }
1338
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001339 /* Possibly retry shutdown until timeout or failure */
1340 ssl_err = SSL_get_error(self->ssl, err);
1341 if (ssl_err == SSL_ERROR_WANT_READ)
1342 sockstate = check_socket_and_wait_for_timeout(sock, 0);
1343 else if (ssl_err == SSL_ERROR_WANT_WRITE)
1344 sockstate = check_socket_and_wait_for_timeout(sock, 1);
1345 else
1346 break;
1347 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1348 if (ssl_err == SSL_ERROR_WANT_READ)
1349 PyErr_SetString(PySSLErrorObject,
1350 "The read operation timed out");
1351 else
1352 PyErr_SetString(PySSLErrorObject,
1353 "The write operation timed out");
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001354 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001355 }
1356 else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1357 PyErr_SetString(PySSLErrorObject,
1358 "Underlying socket too large for select().");
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001359 goto error;
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001360 }
1361 else if (sockstate != SOCKET_OPERATION_OK)
1362 /* Retain the SSL error code */
1363 break;
1364 }
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +00001365
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001366 if (err < 0) {
1367 Py_DECREF(sock);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001368 return PySSL_SetError(self, err, __FILE__, __LINE__);
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001369 }
Antoine Pitrou8bae4ec2010-06-24 22:34:04 +00001370 else
1371 /* It's already INCREF'ed */
1372 return (PyObject *) sock;
1373
1374error:
1375 Py_DECREF(sock);
1376 return NULL;
Bill Janssen40a0f662008-08-12 16:56:25 +00001377}
1378
1379PyDoc_STRVAR(PySSL_SSLshutdown_doc,
1380"shutdown(s) -> socket\n\
1381\n\
1382Does the SSL shutdown handshake with the remote end, and returns\n\
1383the underlying socket object.");
1384
1385
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001386static PyMethodDef PySSLMethods[] = {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001387 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
1388 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
1389 PySSL_SSLwrite_doc},
1390 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
1391 PySSL_SSLread_doc},
1392 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
1393 PySSL_SSLpending_doc},
1394 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
1395 PySSL_peercert_doc},
1396 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
1397 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
1398 PySSL_SSLshutdown_doc},
1399 {NULL, NULL}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001400};
1401
Antoine Pitrou152efa22010-05-16 18:19:27 +00001402static PyTypeObject PySSLSocket_Type = {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001403 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou152efa22010-05-16 18:19:27 +00001404 "_ssl._SSLSocket", /*tp_name*/
1405 sizeof(PySSLSocket), /*tp_basicsize*/
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001406 0, /*tp_itemsize*/
1407 /* methods */
1408 (destructor)PySSL_dealloc, /*tp_dealloc*/
1409 0, /*tp_print*/
1410 0, /*tp_getattr*/
1411 0, /*tp_setattr*/
1412 0, /*tp_reserved*/
1413 0, /*tp_repr*/
1414 0, /*tp_as_number*/
1415 0, /*tp_as_sequence*/
1416 0, /*tp_as_mapping*/
1417 0, /*tp_hash*/
1418 0, /*tp_call*/
1419 0, /*tp_str*/
1420 0, /*tp_getattro*/
1421 0, /*tp_setattro*/
1422 0, /*tp_as_buffer*/
1423 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1424 0, /*tp_doc*/
1425 0, /*tp_traverse*/
1426 0, /*tp_clear*/
1427 0, /*tp_richcompare*/
1428 0, /*tp_weaklistoffset*/
1429 0, /*tp_iter*/
1430 0, /*tp_iternext*/
1431 PySSLMethods, /*tp_methods*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001432};
1433
Antoine Pitrou152efa22010-05-16 18:19:27 +00001434
1435/*
1436 * _SSLContext objects
1437 */
1438
1439static PyObject *
1440context_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1441{
1442 char *kwlist[] = {"protocol", NULL};
1443 PySSLContext *self;
1444 int proto_version = PY_SSL_VERSION_SSL23;
1445 SSL_CTX *ctx = NULL;
1446
1447 if (!PyArg_ParseTupleAndKeywords(
1448 args, kwds, "i:_SSLContext", kwlist,
1449 &proto_version))
1450 return NULL;
1451
1452 PySSL_BEGIN_ALLOW_THREADS
1453 if (proto_version == PY_SSL_VERSION_TLS1)
1454 ctx = SSL_CTX_new(TLSv1_method());
1455 else if (proto_version == PY_SSL_VERSION_SSL3)
1456 ctx = SSL_CTX_new(SSLv3_method());
1457 else if (proto_version == PY_SSL_VERSION_SSL2)
1458 ctx = SSL_CTX_new(SSLv2_method());
1459 else if (proto_version == PY_SSL_VERSION_SSL23)
1460 ctx = SSL_CTX_new(SSLv23_method());
1461 else
1462 proto_version = -1;
1463 PySSL_END_ALLOW_THREADS
1464
1465 if (proto_version == -1) {
1466 PyErr_SetString(PyExc_ValueError,
1467 "invalid protocol version");
1468 return NULL;
1469 }
1470 if (ctx == NULL) {
1471 PyErr_SetString(PySSLErrorObject,
1472 "failed to allocate SSL context");
1473 return NULL;
1474 }
1475
1476 assert(type != NULL && type->tp_alloc != NULL);
1477 self = (PySSLContext *) type->tp_alloc(type, 0);
1478 if (self == NULL) {
1479 SSL_CTX_free(ctx);
1480 return NULL;
1481 }
1482 self->ctx = ctx;
1483 /* Defaults */
1484 SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL);
1485 SSL_CTX_set_options(self->ctx, SSL_OP_ALL);
1486
Antoine Pitroufc113ee2010-10-13 12:46:13 +00001487#define SID_CTX "Python"
1488 SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX,
1489 sizeof(SID_CTX));
1490#undef SID_CTX
1491
Antoine Pitrou152efa22010-05-16 18:19:27 +00001492 return (PyObject *)self;
1493}
1494
1495static void
1496context_dealloc(PySSLContext *self)
1497{
1498 SSL_CTX_free(self->ctx);
1499 Py_TYPE(self)->tp_free(self);
1500}
1501
1502static PyObject *
1503set_ciphers(PySSLContext *self, PyObject *args)
1504{
1505 int ret;
1506 const char *cipherlist;
1507
1508 if (!PyArg_ParseTuple(args, "s:set_ciphers", &cipherlist))
1509 return NULL;
1510 ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist);
1511 if (ret == 0) {
Antoine Pitrou65ec8ae2010-05-16 19:56:32 +00001512 /* Clearing the error queue is necessary on some OpenSSL versions,
1513 otherwise the error will be reported again when another SSL call
1514 is done. */
1515 ERR_clear_error();
Antoine Pitrou152efa22010-05-16 18:19:27 +00001516 PyErr_SetString(PySSLErrorObject,
1517 "No cipher can be selected.");
1518 return NULL;
1519 }
1520 Py_RETURN_NONE;
1521}
1522
1523static PyObject *
1524get_verify_mode(PySSLContext *self, void *c)
1525{
1526 switch (SSL_CTX_get_verify_mode(self->ctx)) {
1527 case SSL_VERIFY_NONE:
1528 return PyLong_FromLong(PY_SSL_CERT_NONE);
1529 case SSL_VERIFY_PEER:
1530 return PyLong_FromLong(PY_SSL_CERT_OPTIONAL);
1531 case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT:
1532 return PyLong_FromLong(PY_SSL_CERT_REQUIRED);
1533 }
1534 PyErr_SetString(PySSLErrorObject,
1535 "invalid return value from SSL_CTX_get_verify_mode");
1536 return NULL;
1537}
1538
1539static int
1540set_verify_mode(PySSLContext *self, PyObject *arg, void *c)
1541{
1542 int n, mode;
1543 if (!PyArg_Parse(arg, "i", &n))
1544 return -1;
1545 if (n == PY_SSL_CERT_NONE)
1546 mode = SSL_VERIFY_NONE;
1547 else if (n == PY_SSL_CERT_OPTIONAL)
1548 mode = SSL_VERIFY_PEER;
1549 else if (n == PY_SSL_CERT_REQUIRED)
1550 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1551 else {
1552 PyErr_SetString(PyExc_ValueError,
1553 "invalid value for verify_mode");
1554 return -1;
1555 }
1556 SSL_CTX_set_verify(self->ctx, mode, NULL);
1557 return 0;
1558}
1559
1560static PyObject *
Antoine Pitroub5218772010-05-21 09:56:06 +00001561get_options(PySSLContext *self, void *c)
1562{
1563 return PyLong_FromLong(SSL_CTX_get_options(self->ctx));
1564}
1565
1566static int
1567set_options(PySSLContext *self, PyObject *arg, void *c)
1568{
1569 long new_opts, opts, set, clear;
1570 if (!PyArg_Parse(arg, "l", &new_opts))
1571 return -1;
1572 opts = SSL_CTX_get_options(self->ctx);
1573 clear = opts & ~new_opts;
1574 set = ~opts & new_opts;
1575 if (clear) {
1576#ifdef HAVE_SSL_CTX_CLEAR_OPTIONS
1577 SSL_CTX_clear_options(self->ctx, clear);
1578#else
1579 PyErr_SetString(PyExc_ValueError,
1580 "can't clear options before OpenSSL 0.9.8m");
1581 return -1;
1582#endif
1583 }
1584 if (set)
1585 SSL_CTX_set_options(self->ctx, set);
1586 return 0;
1587}
1588
1589static PyObject *
Antoine Pitrou152efa22010-05-16 18:19:27 +00001590load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwds)
1591{
1592 char *kwlist[] = {"certfile", "keyfile", NULL};
1593 PyObject *certfile, *keyfile = NULL;
1594 PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL;
1595 int r;
1596
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001597 errno = 0;
Antoine Pitrou67e8e562010-09-01 20:55:41 +00001598 ERR_clear_error();
Antoine Pitrou152efa22010-05-16 18:19:27 +00001599 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1600 "O|O:load_cert_chain", kwlist,
1601 &certfile, &keyfile))
1602 return NULL;
1603 if (keyfile == Py_None)
1604 keyfile = NULL;
1605 if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) {
1606 PyErr_SetString(PyExc_TypeError,
1607 "certfile should be a valid filesystem path");
1608 return NULL;
1609 }
1610 if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) {
1611 PyErr_SetString(PyExc_TypeError,
1612 "keyfile should be a valid filesystem path");
1613 goto error;
1614 }
1615 PySSL_BEGIN_ALLOW_THREADS
1616 r = SSL_CTX_use_certificate_chain_file(self->ctx,
1617 PyBytes_AS_STRING(certfile_bytes));
1618 PySSL_END_ALLOW_THREADS
1619 if (r != 1) {
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001620 if (errno != 0) {
Giampaolo Rodolàe0f98632010-09-01 19:28:49 +00001621 ERR_clear_error();
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001622 PyErr_SetFromErrno(PyExc_IOError);
1623 }
1624 else {
1625 _setSSLError(NULL, 0, __FILE__, __LINE__);
1626 }
Antoine Pitrou152efa22010-05-16 18:19:27 +00001627 goto error;
1628 }
1629 PySSL_BEGIN_ALLOW_THREADS
1630 r = SSL_CTX_use_RSAPrivateKey_file(self->ctx,
1631 PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes),
1632 SSL_FILETYPE_PEM);
1633 PySSL_END_ALLOW_THREADS
1634 Py_XDECREF(keyfile_bytes);
1635 Py_XDECREF(certfile_bytes);
1636 if (r != 1) {
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001637 if (errno != 0) {
Giampaolo Rodolàe0f98632010-09-01 19:28:49 +00001638 ERR_clear_error();
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001639 PyErr_SetFromErrno(PyExc_IOError);
1640 }
1641 else {
1642 _setSSLError(NULL, 0, __FILE__, __LINE__);
1643 }
Antoine Pitrou152efa22010-05-16 18:19:27 +00001644 return NULL;
1645 }
1646 PySSL_BEGIN_ALLOW_THREADS
1647 r = SSL_CTX_check_private_key(self->ctx);
1648 PySSL_END_ALLOW_THREADS
1649 if (r != 1) {
1650 _setSSLError(NULL, 0, __FILE__, __LINE__);
1651 return NULL;
1652 }
1653 Py_RETURN_NONE;
1654
1655error:
1656 Py_XDECREF(keyfile_bytes);
1657 Py_XDECREF(certfile_bytes);
1658 return NULL;
1659}
1660
1661static PyObject *
1662load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwds)
1663{
1664 char *kwlist[] = {"cafile", "capath", NULL};
1665 PyObject *cafile = NULL, *capath = NULL;
1666 PyObject *cafile_bytes = NULL, *capath_bytes = NULL;
1667 const char *cafile_buf = NULL, *capath_buf = NULL;
1668 int r;
1669
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001670 errno = 0;
Antoine Pitrou152efa22010-05-16 18:19:27 +00001671 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1672 "|OO:load_verify_locations", kwlist,
1673 &cafile, &capath))
1674 return NULL;
1675 if (cafile == Py_None)
1676 cafile = NULL;
1677 if (capath == Py_None)
1678 capath = NULL;
1679 if (cafile == NULL && capath == NULL) {
1680 PyErr_SetString(PyExc_TypeError,
1681 "cafile and capath cannot be both omitted");
1682 return NULL;
1683 }
1684 if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) {
1685 PyErr_SetString(PyExc_TypeError,
1686 "cafile should be a valid filesystem path");
1687 return NULL;
1688 }
1689 if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) {
1690 Py_DECREF(cafile_bytes);
1691 PyErr_SetString(PyExc_TypeError,
1692 "capath should be a valid filesystem path");
1693 return NULL;
1694 }
1695 if (cafile)
1696 cafile_buf = PyBytes_AS_STRING(cafile_bytes);
1697 if (capath)
1698 capath_buf = PyBytes_AS_STRING(capath_bytes);
1699 PySSL_BEGIN_ALLOW_THREADS
1700 r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf);
1701 PySSL_END_ALLOW_THREADS
1702 Py_XDECREF(cafile_bytes);
1703 Py_XDECREF(capath_bytes);
1704 if (r != 1) {
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001705 if (errno != 0) {
Giampaolo Rodolàe0f98632010-09-01 19:28:49 +00001706 ERR_clear_error();
Giampaolo Rodolà745ab382010-08-29 19:25:49 +00001707 PyErr_SetFromErrno(PyExc_IOError);
1708 }
1709 else {
1710 _setSSLError(NULL, 0, __FILE__, __LINE__);
1711 }
Antoine Pitrou152efa22010-05-16 18:19:27 +00001712 return NULL;
1713 }
1714 Py_RETURN_NONE;
1715}
1716
1717static PyObject *
1718context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds)
1719{
Antoine Pitroud5323212010-10-22 18:19:07 +00001720 char *kwlist[] = {"sock", "server_side", "server_hostname", NULL};
Antoine Pitrou152efa22010-05-16 18:19:27 +00001721 PySocketSockObject *sock;
1722 int server_side = 0;
Antoine Pitroud5323212010-10-22 18:19:07 +00001723 char *hostname = NULL;
1724 PyObject *hostname_obj, *res;
Antoine Pitrou152efa22010-05-16 18:19:27 +00001725
Antoine Pitroud5323212010-10-22 18:19:07 +00001726 /* server_hostname is either None (or absent), or to be encoded
1727 using the idna encoding. */
1728 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!i|O!:_wrap_socket", kwlist,
Antoine Pitrou152efa22010-05-16 18:19:27 +00001729 PySocketModule.Sock_Type,
Antoine Pitroud5323212010-10-22 18:19:07 +00001730 &sock, &server_side,
1731 Py_TYPE(Py_None), &hostname_obj)) {
1732 PyErr_Clear();
1733 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!iet:_wrap_socket", kwlist,
1734 PySocketModule.Sock_Type,
1735 &sock, &server_side,
1736 "idna", &hostname))
1737 return NULL;
1738#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
1739 PyMem_Free(hostname);
1740 PyErr_SetString(PyExc_ValueError, "server_hostname is not supported "
1741 "by your OpenSSL library");
Antoine Pitrou152efa22010-05-16 18:19:27 +00001742 return NULL;
Antoine Pitroud5323212010-10-22 18:19:07 +00001743#endif
1744 }
Antoine Pitrou152efa22010-05-16 18:19:27 +00001745
Antoine Pitroud5323212010-10-22 18:19:07 +00001746 res = (PyObject *) newPySSLSocket(self->ctx, sock, server_side,
1747 hostname);
1748 if (hostname != NULL)
1749 PyMem_Free(hostname);
1750 return res;
Antoine Pitrou152efa22010-05-16 18:19:27 +00001751}
1752
Antoine Pitroub0182c82010-10-12 20:09:02 +00001753static PyObject *
1754session_stats(PySSLContext *self, PyObject *unused)
1755{
1756 int r;
1757 PyObject *value, *stats = PyDict_New();
1758 if (!stats)
1759 return NULL;
1760
1761#define ADD_STATS(SSL_NAME, KEY_NAME) \
1762 value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \
1763 if (value == NULL) \
1764 goto error; \
1765 r = PyDict_SetItemString(stats, KEY_NAME, value); \
1766 Py_DECREF(value); \
1767 if (r < 0) \
1768 goto error;
1769
1770 ADD_STATS(number, "number");
1771 ADD_STATS(connect, "connect");
1772 ADD_STATS(connect_good, "connect_good");
1773 ADD_STATS(connect_renegotiate, "connect_renegotiate");
1774 ADD_STATS(accept, "accept");
1775 ADD_STATS(accept_good, "accept_good");
1776 ADD_STATS(accept_renegotiate, "accept_renegotiate");
1777 ADD_STATS(accept, "accept");
1778 ADD_STATS(hits, "hits");
1779 ADD_STATS(misses, "misses");
1780 ADD_STATS(timeouts, "timeouts");
1781 ADD_STATS(cache_full, "cache_full");
1782
1783#undef ADD_STATS
1784
1785 return stats;
1786
1787error:
1788 Py_DECREF(stats);
1789 return NULL;
1790}
1791
Antoine Pitrou152efa22010-05-16 18:19:27 +00001792static PyGetSetDef context_getsetlist[] = {
Antoine Pitroub5218772010-05-21 09:56:06 +00001793 {"options", (getter) get_options,
1794 (setter) set_options, NULL},
Antoine Pitrou152efa22010-05-16 18:19:27 +00001795 {"verify_mode", (getter) get_verify_mode,
1796 (setter) set_verify_mode, NULL},
1797 {NULL}, /* sentinel */
1798};
1799
1800static struct PyMethodDef context_methods[] = {
1801 {"_wrap_socket", (PyCFunction) context_wrap_socket,
1802 METH_VARARGS | METH_KEYWORDS, NULL},
1803 {"set_ciphers", (PyCFunction) set_ciphers,
1804 METH_VARARGS, NULL},
1805 {"load_cert_chain", (PyCFunction) load_cert_chain,
1806 METH_VARARGS | METH_KEYWORDS, NULL},
1807 {"load_verify_locations", (PyCFunction) load_verify_locations,
1808 METH_VARARGS | METH_KEYWORDS, NULL},
Antoine Pitroub0182c82010-10-12 20:09:02 +00001809 {"session_stats", (PyCFunction) session_stats,
1810 METH_NOARGS, NULL},
Antoine Pitrou152efa22010-05-16 18:19:27 +00001811 {NULL, NULL} /* sentinel */
1812};
1813
1814static PyTypeObject PySSLContext_Type = {
1815 PyVarObject_HEAD_INIT(NULL, 0)
1816 "_ssl._SSLContext", /*tp_name*/
1817 sizeof(PySSLContext), /*tp_basicsize*/
1818 0, /*tp_itemsize*/
1819 (destructor)context_dealloc, /*tp_dealloc*/
1820 0, /*tp_print*/
1821 0, /*tp_getattr*/
1822 0, /*tp_setattr*/
1823 0, /*tp_reserved*/
1824 0, /*tp_repr*/
1825 0, /*tp_as_number*/
1826 0, /*tp_as_sequence*/
1827 0, /*tp_as_mapping*/
1828 0, /*tp_hash*/
1829 0, /*tp_call*/
1830 0, /*tp_str*/
1831 0, /*tp_getattro*/
1832 0, /*tp_setattro*/
1833 0, /*tp_as_buffer*/
1834 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1835 0, /*tp_doc*/
1836 0, /*tp_traverse*/
1837 0, /*tp_clear*/
1838 0, /*tp_richcompare*/
1839 0, /*tp_weaklistoffset*/
1840 0, /*tp_iter*/
1841 0, /*tp_iternext*/
1842 context_methods, /*tp_methods*/
1843 0, /*tp_members*/
1844 context_getsetlist, /*tp_getset*/
1845 0, /*tp_base*/
1846 0, /*tp_dict*/
1847 0, /*tp_descr_get*/
1848 0, /*tp_descr_set*/
1849 0, /*tp_dictoffset*/
1850 0, /*tp_init*/
1851 0, /*tp_alloc*/
1852 context_new, /*tp_new*/
1853};
1854
1855
1856
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001857#ifdef HAVE_OPENSSL_RAND
1858
1859/* helper routines for seeding the SSL PRNG */
1860static PyObject *
1861PySSL_RAND_add(PyObject *self, PyObject *args)
1862{
1863 char *buf;
1864 int len;
1865 double entropy;
1866
1867 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
Antoine Pitrou525807b2010-05-12 14:05:24 +00001868 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001869 RAND_add(buf, len, entropy);
1870 Py_INCREF(Py_None);
1871 return Py_None;
1872}
1873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001875"RAND_add(string, entropy)\n\
1876\n\
1877Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001878bound on the entropy contained in string. See RFC 1750.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001879
1880static PyObject *
1881PySSL_RAND_status(PyObject *self)
1882{
Christian Heimes217cfd12007-12-02 14:31:20 +00001883 return PyLong_FromLong(RAND_status());
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001884}
1885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001887"RAND_status() -> 0 or 1\n\
1888\n\
Bill Janssen6e027db2007-11-15 22:23:56 +00001889Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
1890It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
1891using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001892
1893static PyObject *
Victor Stinnerf9faaad2010-05-16 21:36:37 +00001894PySSL_RAND_egd(PyObject *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001895{
Victor Stinnerf9faaad2010-05-16 21:36:37 +00001896 PyObject *path;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001897 int bytes;
1898
Victor Stinnerf9faaad2010-05-16 21:36:37 +00001899 if (!PyArg_ParseTuple(args, "O&|i:RAND_egd",
1900 PyUnicode_FSConverter, &path))
1901 return NULL;
1902
1903 bytes = RAND_egd(PyBytes_AsString(path));
1904 Py_DECREF(path);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001905 if (bytes == -1) {
Antoine Pitrou525807b2010-05-12 14:05:24 +00001906 PyErr_SetString(PySSLErrorObject,
1907 "EGD connection failed or EGD did not return "
1908 "enough data to seed the PRNG");
1909 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001910 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001911 return PyLong_FromLong(bytes);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001912}
1913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001915"RAND_egd(path) -> bytes\n\
1916\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001917Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
1918Returns number of bytes read. Raises SSLError if connection to EGD\n\
1919fails or if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001920
1921#endif
1922
Bill Janssen40a0f662008-08-12 16:56:25 +00001923
1924
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001925/* List of functions exported by this module. */
1926
1927static PyMethodDef PySSL_methods[] = {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001928 {"_test_decode_cert", PySSL_test_decode_certificate,
1929 METH_VARARGS},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001930#ifdef HAVE_OPENSSL_RAND
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001931 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
1932 PySSL_RAND_add_doc},
Victor Stinnerf9faaad2010-05-16 21:36:37 +00001933 {"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001934 PySSL_RAND_egd_doc},
1935 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
1936 PySSL_RAND_status_doc},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001937#endif
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001938 {NULL, NULL} /* Sentinel */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001939};
1940
1941
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001942#ifdef WITH_THREAD
1943
1944/* an implementation of OpenSSL threading operations in terms
1945 of the Python C thread library */
1946
1947static PyThread_type_lock *_ssl_locks = NULL;
1948
1949static unsigned long _ssl_thread_id_function (void) {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001950 return PyThread_get_thread_ident();
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001951}
1952
Bill Janssen6e027db2007-11-15 22:23:56 +00001953static void _ssl_thread_locking_function
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001954 (int mode, int n, const char *file, int line) {
1955 /* this function is needed to perform locking on shared data
1956 structures. (Note that OpenSSL uses a number of global data
1957 structures that will be implicitly shared whenever multiple
1958 threads use OpenSSL.) Multi-threaded applications will
1959 crash at random if it is not set.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001960
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001961 locking_function() must be able to handle up to
1962 CRYPTO_num_locks() different mutex locks. It sets the n-th
1963 lock if mode & CRYPTO_LOCK, and releases it otherwise.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001964
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001965 file and line are the file number of the function setting the
1966 lock. They can be useful for debugging.
1967 */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001968
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001969 if ((_ssl_locks == NULL) ||
1970 (n < 0) || ((unsigned)n >= _ssl_locks_count))
1971 return;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001972
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001973 if (mode & CRYPTO_LOCK) {
1974 PyThread_acquire_lock(_ssl_locks[n], 1);
1975 } else {
1976 PyThread_release_lock(_ssl_locks[n]);
1977 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001978}
1979
1980static int _setup_ssl_threads(void) {
1981
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001982 unsigned int i;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001983
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00001984 if (_ssl_locks == NULL) {
1985 _ssl_locks_count = CRYPTO_num_locks();
1986 _ssl_locks = (PyThread_type_lock *)
1987 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
1988 if (_ssl_locks == NULL)
1989 return 0;
1990 memset(_ssl_locks, 0,
1991 sizeof(PyThread_type_lock) * _ssl_locks_count);
1992 for (i = 0; i < _ssl_locks_count; i++) {
1993 _ssl_locks[i] = PyThread_allocate_lock();
1994 if (_ssl_locks[i] == NULL) {
1995 unsigned int j;
1996 for (j = 0; j < i; j++) {
1997 PyThread_free_lock(_ssl_locks[j]);
1998 }
1999 free(_ssl_locks);
2000 return 0;
2001 }
2002 }
2003 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
2004 CRYPTO_set_id_callback(_ssl_thread_id_function);
2005 }
2006 return 1;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00002007}
2008
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002009#endif /* def HAVE_THREAD */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00002010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002011PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002012"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002014
Martin v. Löwis1a214512008-06-11 05:26:20 +00002015
2016static struct PyModuleDef _sslmodule = {
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002017 PyModuleDef_HEAD_INIT,
2018 "_ssl",
2019 module_doc,
2020 -1,
2021 PySSL_methods,
2022 NULL,
2023 NULL,
2024 NULL,
2025 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002026};
2027
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002028PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002029PyInit__ssl(void)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002030{
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002031 PyObject *m, *d, *r;
2032 unsigned long libver;
2033 unsigned int major, minor, fix, patch, status;
2034 PySocketModule_APIObject *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002035
Antoine Pitrou152efa22010-05-16 18:19:27 +00002036 if (PyType_Ready(&PySSLContext_Type) < 0)
2037 return NULL;
2038 if (PyType_Ready(&PySSLSocket_Type) < 0)
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002039 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002040
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002041 m = PyModule_Create(&_sslmodule);
2042 if (m == NULL)
2043 return NULL;
2044 d = PyModule_GetDict(m);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002045
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002046 /* Load _socket module and its C API */
2047 socket_api = PySocketModule_ImportModuleAndAPI();
2048 if (!socket_api)
2049 return NULL;
2050 PySocketModule = *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002051
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002052 /* Init OpenSSL */
2053 SSL_load_error_strings();
2054 SSL_library_init();
Thomas Wouters1b7f8912007-09-19 03:06:30 +00002055#ifdef WITH_THREAD
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002056 /* note that this will start threading if not already started */
2057 if (!_setup_ssl_threads()) {
2058 return NULL;
2059 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +00002060#endif
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002061 OpenSSL_add_all_algorithms();
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002062
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002063 /* Add symbols to module dict */
2064 PySSLErrorObject = PyErr_NewException("ssl.SSLError",
2065 PySocketModule.error,
2066 NULL);
2067 if (PySSLErrorObject == NULL)
2068 return NULL;
2069 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0)
2070 return NULL;
Antoine Pitrou152efa22010-05-16 18:19:27 +00002071 if (PyDict_SetItemString(d, "_SSLContext",
2072 (PyObject *)&PySSLContext_Type) != 0)
2073 return NULL;
2074 if (PyDict_SetItemString(d, "_SSLSocket",
2075 (PyObject *)&PySSLSocket_Type) != 0)
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002076 return NULL;
2077 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
2078 PY_SSL_ERROR_ZERO_RETURN);
2079 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
2080 PY_SSL_ERROR_WANT_READ);
2081 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
2082 PY_SSL_ERROR_WANT_WRITE);
2083 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
2084 PY_SSL_ERROR_WANT_X509_LOOKUP);
2085 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
2086 PY_SSL_ERROR_SYSCALL);
2087 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
2088 PY_SSL_ERROR_SSL);
2089 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
2090 PY_SSL_ERROR_WANT_CONNECT);
2091 /* non ssl.h errorcodes */
2092 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
2093 PY_SSL_ERROR_EOF);
2094 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
2095 PY_SSL_ERROR_INVALID_ERROR_CODE);
2096 /* cert requirements */
2097 PyModule_AddIntConstant(m, "CERT_NONE",
2098 PY_SSL_CERT_NONE);
2099 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
2100 PY_SSL_CERT_OPTIONAL);
2101 PyModule_AddIntConstant(m, "CERT_REQUIRED",
2102 PY_SSL_CERT_REQUIRED);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00002103
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002104 /* protocol versions */
2105 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
2106 PY_SSL_VERSION_SSL2);
2107 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
2108 PY_SSL_VERSION_SSL3);
2109 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
2110 PY_SSL_VERSION_SSL23);
2111 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
2112 PY_SSL_VERSION_TLS1);
Antoine Pitrou04f6a322010-04-05 21:40:07 +00002113
Antoine Pitroub5218772010-05-21 09:56:06 +00002114 /* protocol options */
2115 PyModule_AddIntConstant(m, "OP_ALL", SSL_OP_ALL);
2116 PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
2117 PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
2118 PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
2119
Antoine Pitroud5323212010-10-22 18:19:07 +00002120#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2121 r = Py_True;
2122#else
2123 r = Py_False;
2124#endif
2125 Py_INCREF(r);
2126 PyModule_AddObject(m, "HAS_SNI", r);
2127
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002128 /* OpenSSL version */
2129 /* SSLeay() gives us the version of the library linked against,
2130 which could be different from the headers version.
2131 */
2132 libver = SSLeay();
2133 r = PyLong_FromUnsignedLong(libver);
2134 if (r == NULL)
2135 return NULL;
2136 if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
2137 return NULL;
2138 status = libver & 0xF;
2139 libver >>= 4;
2140 patch = libver & 0xFF;
2141 libver >>= 8;
2142 fix = libver & 0xFF;
2143 libver >>= 8;
2144 minor = libver & 0xFF;
2145 libver >>= 8;
2146 major = libver & 0xFF;
2147 r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
2148 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
2149 return NULL;
2150 r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
2151 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
2152 return NULL;
Antoine Pitrou04f6a322010-04-05 21:40:07 +00002153
Antoine Pitroucbb82eb2010-05-05 15:57:33 +00002154 return m;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002155}