blob: 9ce73cfa494e1f0cdcf5672ef441b7f432f899ff [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 Pitrou5a1c4d12010-04-23 21:11:10 +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 Pitrou30dc1a72010-05-05 16:01:14 +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 Pitrou30dc1a72010-05-05 16:01:14 +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 Pitrou30dc1a72010-05-05 16:01:14 +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 Pitrou30dc1a72010-05-05 16:01:14 +000055 PY_SSL_CLIENT,
56 PY_SSL_SERVER
Thomas Woutersed03b412007-08-28 21:37:11 +000057};
58
59enum py_ssl_cert_requirements {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +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 Pitrou30dc1a72010-05-05 16:01:14 +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
116typedef struct {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000117 PyObject_HEAD
118 PyObject *Socket; /* weakref to socket on which we're layered */
119 SSL_CTX* ctx;
120 SSL* ssl;
121 X509* peer_cert;
122 int shutdown_seen_zero;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000123
124} PySSLObject;
125
Jeremy Hylton938ace62002-07-17 16:30:39 +0000126static PyTypeObject PySSL_Type;
127static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
128static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
Thomas Woutersed03b412007-08-28 21:37:11 +0000129static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000130 int writing);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000131static PyObject *PySSL_peercert(PySSLObject *self, PyObject *args);
132static PyObject *PySSL_cipher(PySSLObject *self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000133
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000134#define PySSLObject_Check(v) (Py_TYPE(v) == &PySSL_Type)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000135
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000136typedef enum {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000137 SOCKET_IS_NONBLOCKING,
138 SOCKET_IS_BLOCKING,
139 SOCKET_HAS_TIMED_OUT,
140 SOCKET_HAS_BEEN_CLOSED,
141 SOCKET_TOO_LARGE_FOR_SELECT,
142 SOCKET_OPERATION_OK
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000143} timeout_state;
144
Thomas Woutersed03b412007-08-28 21:37:11 +0000145/* Wrap error strings with filename and line # */
146#define STRINGIFY1(x) #x
147#define STRINGIFY2(x) STRINGIFY1(x)
148#define ERRSTR1(x,y,z) (x ":" y ": " z)
149#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
150
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000151/* XXX It might be helpful to augment the error message generated
152 below with the name of the SSL function that generated the error.
153 I expect it's obvious most of the time.
154*/
155
156static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000157PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000158{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000159 PyObject *v;
160 char buf[2048];
161 char *errstr;
162 int err;
163 enum py_ssl_error p = PY_SSL_ERROR_NONE;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000164
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000165 assert(ret <= 0);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000166
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000167 if (obj->ssl != NULL) {
168 err = SSL_get_error(obj->ssl, ret);
Thomas Woutersed03b412007-08-28 21:37:11 +0000169
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000170 switch (err) {
171 case SSL_ERROR_ZERO_RETURN:
172 errstr = "TLS/SSL connection has been closed";
173 p = PY_SSL_ERROR_ZERO_RETURN;
174 break;
175 case SSL_ERROR_WANT_READ:
176 errstr = "The operation did not complete (read)";
177 p = PY_SSL_ERROR_WANT_READ;
178 break;
179 case SSL_ERROR_WANT_WRITE:
180 p = PY_SSL_ERROR_WANT_WRITE;
181 errstr = "The operation did not complete (write)";
182 break;
183 case SSL_ERROR_WANT_X509_LOOKUP:
184 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
Antoine Pitroua29b1812010-05-12 14:08:45 +0000185 errstr = "The operation did not complete (X509 lookup)";
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000186 break;
187 case SSL_ERROR_WANT_CONNECT:
188 p = PY_SSL_ERROR_WANT_CONNECT;
189 errstr = "The operation did not complete (connect)";
190 break;
191 case SSL_ERROR_SYSCALL:
192 {
193 unsigned long e = ERR_get_error();
194 if (e == 0) {
195 PySocketSockObject *s
196 = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
197 if (ret == 0 || (((PyObject *)s) == Py_None)) {
Antoine Pitroua29b1812010-05-12 14:08:45 +0000198 p = PY_SSL_ERROR_EOF;
199 errstr = "EOF occurred in violation of protocol";
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000200 } else if (ret == -1) {
Antoine Pitroua29b1812010-05-12 14:08:45 +0000201 /* underlying BIO reported an I/O error */
202 return s->errorhandler();
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000203 } else { /* possible? */
Antoine Pitroua29b1812010-05-12 14:08:45 +0000204 p = PY_SSL_ERROR_SYSCALL;
205 errstr = "Some I/O error occurred";
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000206 }
207 } else {
208 p = PY_SSL_ERROR_SYSCALL;
209 /* XXX Protected by global interpreter lock */
210 errstr = ERR_error_string(e, NULL);
211 }
212 break;
213 }
214 case SSL_ERROR_SSL:
215 {
216 unsigned long e = ERR_get_error();
217 p = PY_SSL_ERROR_SSL;
218 if (e != 0)
219 /* XXX Protected by global interpreter lock */
220 errstr = ERR_error_string(e, NULL);
221 else { /* possible? */
Antoine Pitroua29b1812010-05-12 14:08:45 +0000222 errstr = "A failure in the SSL library occurred";
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000223 }
224 break;
225 }
226 default:
227 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
228 errstr = "Invalid error code";
229 }
230 } else {
231 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
232 }
233 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
234 v = Py_BuildValue("(is)", p, buf);
235 if (v != NULL) {
236 PyErr_SetObject(PySSLErrorObject, v);
237 Py_DECREF(v);
238 }
239 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000240}
241
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000242static PyObject *
243_setSSLError (char *errstr, int errcode, char *filename, int lineno) {
244
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000245 char buf[2048];
246 PyObject *v;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000247
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000248 if (errstr == NULL) {
249 errcode = ERR_peek_last_error();
250 errstr = ERR_error_string(errcode, NULL);
251 }
252 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
253 v = Py_BuildValue("(is)", errcode, buf);
254 if (v != NULL) {
255 PyErr_SetObject(PySSLErrorObject, v);
256 Py_DECREF(v);
257 }
258 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000259}
260
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000261static PySSLObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000262newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file,
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000263 enum py_ssl_server_or_client socket_type,
264 enum py_ssl_cert_requirements certreq,
265 enum py_ssl_version proto_version,
266 char *cacerts_file)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000267{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000268 PySSLObject *self;
269 char *errstr = NULL;
270 int ret;
271 int verification_mode;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000273 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
274 if (self == NULL)
275 return NULL;
276 self->peer_cert = NULL;
277 self->ssl = NULL;
278 self->ctx = NULL;
279 self->Socket = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000280
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000281 /* Make sure the SSL error state is initialized */
282 (void) ERR_get_state();
283 ERR_clear_error();
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000284
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000285 if ((key_file && !cert_file) || (!key_file && cert_file)) {
286 errstr = ERRSTR("Both the key & certificate files "
287 "must be specified");
288 goto fail;
289 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000290
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000291 if ((socket_type == PY_SSL_SERVER) &&
292 ((key_file == NULL) || (cert_file == NULL))) {
293 errstr = ERRSTR("Both the key & certificate files "
294 "must be specified for server-side operation");
295 goto fail;
296 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000297
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000298 PySSL_BEGIN_ALLOW_THREADS
299 if (proto_version == PY_SSL_VERSION_TLS1)
300 self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
301 else if (proto_version == PY_SSL_VERSION_SSL3)
302 self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
303 else if (proto_version == PY_SSL_VERSION_SSL2)
304 self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */
305 else if (proto_version == PY_SSL_VERSION_SSL23)
306 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
307 PySSL_END_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000308
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000309 if (self->ctx == NULL) {
310 errstr = ERRSTR("Invalid SSL protocol variant specified.");
311 goto fail;
312 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000313
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000314 if (certreq != PY_SSL_CERT_NONE) {
315 if (cacerts_file == NULL) {
316 errstr = ERRSTR("No root certificates specified for "
Antoine Pitroua29b1812010-05-12 14:08:45 +0000317 "verification of other-side certificates.");
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000318 goto fail;
319 } else {
320 PySSL_BEGIN_ALLOW_THREADS
321 ret = SSL_CTX_load_verify_locations(self->ctx,
322 cacerts_file,
323 NULL);
324 PySSL_END_ALLOW_THREADS
325 if (ret != 1) {
326 _setSSLError(NULL, 0, __FILE__, __LINE__);
327 goto fail;
328 }
329 }
330 }
331 if (key_file) {
332 PySSL_BEGIN_ALLOW_THREADS
333 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
334 SSL_FILETYPE_PEM);
335 PySSL_END_ALLOW_THREADS
336 if (ret != 1) {
337 _setSSLError(NULL, ret, __FILE__, __LINE__);
338 goto fail;
339 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000340
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000341 PySSL_BEGIN_ALLOW_THREADS
342 ret = SSL_CTX_use_certificate_chain_file(self->ctx,
343 cert_file);
344 PySSL_END_ALLOW_THREADS
345 if (ret != 1) {
346 /*
347 fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n",
348 ret, ERR_peek_error(), ERR_peek_last_error(), cert_file);
349 */
350 if (ERR_peek_last_error() != 0) {
351 _setSSLError(NULL, ret, __FILE__, __LINE__);
352 goto fail;
353 }
354 }
355 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000356
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000357 /* ssl compatibility */
358 SSL_CTX_set_options(self->ctx, SSL_OP_ALL);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000359
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000360 verification_mode = SSL_VERIFY_NONE;
361 if (certreq == PY_SSL_CERT_OPTIONAL)
362 verification_mode = SSL_VERIFY_PEER;
363 else if (certreq == PY_SSL_CERT_REQUIRED)
364 verification_mode = (SSL_VERIFY_PEER |
365 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
366 SSL_CTX_set_verify(self->ctx, verification_mode,
367 NULL); /* set verify lvl */
Thomas Woutersed03b412007-08-28 21:37:11 +0000368
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000369 PySSL_BEGIN_ALLOW_THREADS
370 self->ssl = SSL_new(self->ctx); /* New ssl struct */
371 PySSL_END_ALLOW_THREADS
372 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
Antoine Pitroud59ceb52010-04-09 20:47:00 +0000373#ifdef SSL_MODE_AUTO_RETRY
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000374 SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY);
Antoine Pitroud59ceb52010-04-09 20:47:00 +0000375#endif
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000376
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000377 /* If the socket is in non-blocking mode or timeout mode, set the BIO
378 * to non-blocking mode (blocking is the default)
379 */
380 if (Sock->sock_timeout >= 0.0) {
381 /* Set both the read and write BIO's to non-blocking mode */
382 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
383 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
384 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000385
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000386 PySSL_BEGIN_ALLOW_THREADS
387 if (socket_type == PY_SSL_CLIENT)
388 SSL_set_connect_state(self->ssl);
389 else
390 SSL_set_accept_state(self->ssl);
391 PySSL_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000392
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000393 self->Socket = PyWeakref_NewRef((PyObject *) Sock, Py_None);
394 return self;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000395 fail:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000396 if (errstr)
397 PyErr_SetString(PySSLErrorObject, errstr);
398 Py_DECREF(self);
399 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000400}
401
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000402static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000403PySSL_sslwrap(PyObject *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000404{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000405 PySocketSockObject *Sock;
406 int server_side = 0;
407 int verification_mode = PY_SSL_CERT_NONE;
408 int protocol = PY_SSL_VERSION_SSL23;
409 char *key_file = NULL;
410 char *cert_file = NULL;
411 char *cacerts_file = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000412
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000413 if (!PyArg_ParseTuple(args, "O!i|zziiz:sslwrap",
414 PySocketModule.Sock_Type,
415 &Sock,
416 &server_side,
417 &key_file, &cert_file,
418 &verification_mode, &protocol,
419 &cacerts_file))
420 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000421
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000422 /*
423 fprintf(stderr,
424 "server_side is %d, keyfile %p, certfile %p, verify_mode %d, "
425 "protocol %d, certs %p\n",
426 server_side, key_file, cert_file, verification_mode,
427 protocol, cacerts_file);
428 */
Thomas Woutersed03b412007-08-28 21:37:11 +0000429
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000430 return (PyObject *) newPySSLObject(Sock, key_file, cert_file,
431 server_side, verification_mode,
432 protocol, cacerts_file);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000433}
434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000435PyDoc_STRVAR(ssl_doc,
Thomas Woutersed03b412007-08-28 21:37:11 +0000436"sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n"
437" cacertsfile]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000438
439/* SSL object methods */
440
Bill Janssen6e027db2007-11-15 22:23:56 +0000441static PyObject *PySSL_SSLdo_handshake(PySSLObject *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000442{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000443 int ret;
444 int err;
445 int sockstate, nonblocking;
446 PySocketSockObject *sock
447 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Antoine Pitrouec146182010-04-24 21:30:20 +0000448
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000449 if (((PyObject*)sock) == Py_None) {
450 _setSSLError("Underlying socket connection gone",
451 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
452 return NULL;
453 }
Antoine Pitrouec146182010-04-24 21:30:20 +0000454
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000455 /* just in case the blocking state of the socket has been changed */
456 nonblocking = (sock->sock_timeout >= 0.0);
457 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
458 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000459
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000460 /* Actually negotiate SSL connection */
461 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
462 sockstate = 0;
463 do {
Bill Janssen6e027db2007-11-15 22:23:56 +0000464 PySSL_BEGIN_ALLOW_THREADS
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000465 ret = SSL_do_handshake(self->ssl);
466 err = SSL_get_error(self->ssl, ret);
467 PySSL_END_ALLOW_THREADS
468 if(PyErr_CheckSignals()) {
469 return NULL;
470 }
471 if (err == SSL_ERROR_WANT_READ) {
472 sockstate = check_socket_and_wait_for_timeout(sock, 0);
473 } else if (err == SSL_ERROR_WANT_WRITE) {
474 sockstate = check_socket_and_wait_for_timeout(sock, 1);
475 } else {
476 sockstate = SOCKET_OPERATION_OK;
477 }
478 if (sockstate == SOCKET_HAS_TIMED_OUT) {
479 PyErr_SetString(PySSLErrorObject,
Antoine Pitroua29b1812010-05-12 14:08:45 +0000480 ERRSTR("The handshake operation timed out"));
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000481 return NULL;
482 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
483 PyErr_SetString(PySSLErrorObject,
Antoine Pitroua29b1812010-05-12 14:08:45 +0000484 ERRSTR("Underlying socket has been closed."));
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000485 return NULL;
486 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
487 PyErr_SetString(PySSLErrorObject,
Antoine Pitroua29b1812010-05-12 14:08:45 +0000488 ERRSTR("Underlying socket too large for select()."));
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000489 return NULL;
490 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
491 break;
492 }
493 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
494 if (ret < 1)
495 return PySSL_SetError(self, ret, __FILE__, __LINE__);
496 self->ssl->debug = 1;
Bill Janssen6e027db2007-11-15 22:23:56 +0000497
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000498 if (self->peer_cert)
499 X509_free (self->peer_cert);
500 PySSL_BEGIN_ALLOW_THREADS
501 self->peer_cert = SSL_get_peer_certificate(self->ssl);
502 PySSL_END_ALLOW_THREADS
503
504 Py_INCREF(Py_None);
505 return Py_None;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000506}
507
Thomas Woutersed03b412007-08-28 21:37:11 +0000508static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000509_create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000510
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000511 char namebuf[X509_NAME_MAXLEN];
512 int buflen;
513 PyObject *name_obj;
514 PyObject *value_obj;
515 PyObject *attr;
516 unsigned char *valuebuf = NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000517
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000518 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
519 if (buflen < 0) {
520 _setSSLError(NULL, 0, __FILE__, __LINE__);
521 goto fail;
522 }
523 name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
524 if (name_obj == NULL)
525 goto fail;
Guido van Rossumf06628b2007-11-21 20:01:53 +0000526
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000527 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
528 if (buflen < 0) {
529 _setSSLError(NULL, 0, __FILE__, __LINE__);
530 Py_DECREF(name_obj);
531 goto fail;
532 }
533 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
Antoine Pitroua29b1812010-05-12 14:08:45 +0000534 buflen, "strict");
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000535 OPENSSL_free(valuebuf);
536 if (value_obj == NULL) {
537 Py_DECREF(name_obj);
538 goto fail;
539 }
540 attr = PyTuple_New(2);
541 if (attr == NULL) {
542 Py_DECREF(name_obj);
543 Py_DECREF(value_obj);
544 goto fail;
545 }
546 PyTuple_SET_ITEM(attr, 0, name_obj);
547 PyTuple_SET_ITEM(attr, 1, value_obj);
548 return attr;
Thomas Woutersed03b412007-08-28 21:37:11 +0000549
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000550 fail:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000551 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000552}
553
554static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000555_create_tuple_for_X509_NAME (X509_NAME *xname)
Thomas Woutersed03b412007-08-28 21:37:11 +0000556{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000557 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
558 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
559 PyObject *rdnt;
560 PyObject *attr = NULL; /* tuple to hold an attribute */
561 int entry_count = X509_NAME_entry_count(xname);
562 X509_NAME_ENTRY *entry;
563 ASN1_OBJECT *name;
564 ASN1_STRING *value;
565 int index_counter;
566 int rdn_level = -1;
567 int retcode;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000568
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000569 dn = PyList_New(0);
570 if (dn == NULL)
571 return NULL;
572 /* now create another tuple to hold the top-level RDN */
573 rdn = PyList_New(0);
574 if (rdn == NULL)
575 goto fail0;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000576
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000577 for (index_counter = 0;
578 index_counter < entry_count;
579 index_counter++)
580 {
581 entry = X509_NAME_get_entry(xname, index_counter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000582
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000583 /* check to see if we've gotten to a new RDN */
584 if (rdn_level >= 0) {
585 if (rdn_level != entry->set) {
586 /* yes, new RDN */
587 /* add old RDN to DN */
588 rdnt = PyList_AsTuple(rdn);
589 Py_DECREF(rdn);
590 if (rdnt == NULL)
591 goto fail0;
592 retcode = PyList_Append(dn, rdnt);
593 Py_DECREF(rdnt);
594 if (retcode < 0)
595 goto fail0;
596 /* create new RDN */
597 rdn = PyList_New(0);
598 if (rdn == NULL)
599 goto fail0;
600 }
601 }
602 rdn_level = entry->set;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000603
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000604 /* now add this attribute to the current RDN */
605 name = X509_NAME_ENTRY_get_object(entry);
606 value = X509_NAME_ENTRY_get_data(entry);
607 attr = _create_tuple_for_attribute(name, value);
608 /*
609 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
610 entry->set,
611 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
612 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
613 */
614 if (attr == NULL)
615 goto fail1;
616 retcode = PyList_Append(rdn, attr);
617 Py_DECREF(attr);
618 if (retcode < 0)
619 goto fail1;
620 }
621 /* now, there's typically a dangling RDN */
622 if ((rdn != NULL) && (PyList_Size(rdn) > 0)) {
623 rdnt = PyList_AsTuple(rdn);
624 Py_DECREF(rdn);
625 if (rdnt == NULL)
626 goto fail0;
627 retcode = PyList_Append(dn, rdnt);
628 Py_DECREF(rdnt);
629 if (retcode < 0)
630 goto fail0;
631 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000632
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000633 /* convert list to tuple */
634 rdnt = PyList_AsTuple(dn);
635 Py_DECREF(dn);
636 if (rdnt == NULL)
637 return NULL;
638 return rdnt;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000639
640 fail1:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000641 Py_XDECREF(rdn);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000642
643 fail0:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000644 Py_XDECREF(dn);
645 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000646}
647
648static PyObject *
649_get_peer_alt_names (X509 *certificate) {
Guido van Rossumf06628b2007-11-21 20:01:53 +0000650
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000651 /* this code follows the procedure outlined in
652 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
653 function to extract the STACK_OF(GENERAL_NAME),
654 then iterates through the stack to add the
655 names. */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000656
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000657 int i, j;
658 PyObject *peer_alt_names = Py_None;
659 PyObject *v, *t;
660 X509_EXTENSION *ext = NULL;
661 GENERAL_NAMES *names = NULL;
662 GENERAL_NAME *name;
663 X509V3_EXT_METHOD *method;
664 BIO *biobuf = NULL;
665 char buf[2048];
666 char *vptr;
667 int len;
668 /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
Victor Stinner117ff172010-03-02 22:49:30 +0000669#if OPENSSL_VERSION_NUMBER >= 0x009060dfL
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000670 const unsigned char *p;
Victor Stinner117ff172010-03-02 22:49:30 +0000671#else
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000672 unsigned char *p;
Victor Stinner117ff172010-03-02 22:49:30 +0000673#endif
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000674
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000675 if (certificate == NULL)
676 return peer_alt_names;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000677
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000678 /* get a memory buffer */
679 biobuf = BIO_new(BIO_s_mem());
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000680
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000681 i = 0;
682 while ((i = X509_get_ext_by_NID(
683 certificate, NID_subject_alt_name, i)) >= 0) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000684
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000685 if (peer_alt_names == Py_None) {
686 peer_alt_names = PyList_New(0);
687 if (peer_alt_names == NULL)
688 goto fail;
689 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000690
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000691 /* now decode the altName */
692 ext = X509_get_ext(certificate, i);
693 if(!(method = X509V3_EXT_get(ext))) {
694 PyErr_SetString
695 (PySSLErrorObject,
696 ERRSTR("No method for internalizing subjectAltName!"));
697 goto fail;
698 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000699
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000700 p = ext->value->data;
701 if (method->it)
702 names = (GENERAL_NAMES*)
703 (ASN1_item_d2i(NULL,
704 &p,
705 ext->value->length,
706 ASN1_ITEM_ptr(method->it)));
707 else
708 names = (GENERAL_NAMES*)
709 (method->d2i(NULL,
710 &p,
711 ext->value->length));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000712
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000713 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000714
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000715 /* get a rendering of each name in the set of names */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000716
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000717 name = sk_GENERAL_NAME_value(names, j);
718 if (name->type == GEN_DIRNAME) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000719
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000720 /* we special-case DirName as a tuple of
721 tuples of attributes */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000722
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000723 t = PyTuple_New(2);
724 if (t == NULL) {
725 goto fail;
726 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000727
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000728 v = PyUnicode_FromString("DirName");
729 if (v == NULL) {
730 Py_DECREF(t);
731 goto fail;
732 }
733 PyTuple_SET_ITEM(t, 0, v);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000734
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000735 v = _create_tuple_for_X509_NAME (name->d.dirn);
736 if (v == NULL) {
737 Py_DECREF(t);
738 goto fail;
739 }
740 PyTuple_SET_ITEM(t, 1, v);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000741
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000742 } else {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000743
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000744 /* for everything else, we use the OpenSSL print form */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000745
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000746 (void) BIO_reset(biobuf);
747 GENERAL_NAME_print(biobuf, name);
748 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
749 if (len < 0) {
750 _setSSLError(NULL, 0, __FILE__, __LINE__);
751 goto fail;
752 }
753 vptr = strchr(buf, ':');
754 if (vptr == NULL)
755 goto fail;
756 t = PyTuple_New(2);
757 if (t == NULL)
758 goto fail;
759 v = PyUnicode_FromStringAndSize(buf, (vptr - buf));
760 if (v == NULL) {
761 Py_DECREF(t);
762 goto fail;
763 }
764 PyTuple_SET_ITEM(t, 0, v);
765 v = PyUnicode_FromStringAndSize((vptr + 1),
766 (len - (vptr - buf + 1)));
767 if (v == NULL) {
768 Py_DECREF(t);
769 goto fail;
770 }
771 PyTuple_SET_ITEM(t, 1, v);
772 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000773
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000774 /* and add that rendering to the list */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000775
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000776 if (PyList_Append(peer_alt_names, t) < 0) {
777 Py_DECREF(t);
778 goto fail;
779 }
780 Py_DECREF(t);
781 }
782 }
783 BIO_free(biobuf);
784 if (peer_alt_names != Py_None) {
785 v = PyList_AsTuple(peer_alt_names);
786 Py_DECREF(peer_alt_names);
787 return v;
788 } else {
789 return peer_alt_names;
790 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000791
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000792
793 fail:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000794 if (biobuf != NULL)
795 BIO_free(biobuf);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000796
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000797 if (peer_alt_names != Py_None) {
798 Py_XDECREF(peer_alt_names);
799 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000800
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000801 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000802}
803
804static PyObject *
805_decode_certificate (X509 *certificate, int verbose) {
806
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000807 PyObject *retval = NULL;
808 BIO *biobuf = NULL;
809 PyObject *peer;
810 PyObject *peer_alt_names = NULL;
811 PyObject *issuer;
812 PyObject *version;
813 PyObject *sn_obj;
814 ASN1_INTEGER *serialNumber;
815 char buf[2048];
816 int len;
817 ASN1_TIME *notBefore, *notAfter;
818 PyObject *pnotBefore, *pnotAfter;
Thomas Woutersed03b412007-08-28 21:37:11 +0000819
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000820 retval = PyDict_New();
821 if (retval == NULL)
822 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000823
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000824 peer = _create_tuple_for_X509_NAME(
825 X509_get_subject_name(certificate));
826 if (peer == NULL)
827 goto fail0;
828 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
829 Py_DECREF(peer);
830 goto fail0;
831 }
832 Py_DECREF(peer);
Thomas Woutersed03b412007-08-28 21:37:11 +0000833
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000834 if (verbose) {
835 issuer = _create_tuple_for_X509_NAME(
836 X509_get_issuer_name(certificate));
837 if (issuer == NULL)
838 goto fail0;
839 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
840 Py_DECREF(issuer);
841 goto fail0;
842 }
843 Py_DECREF(issuer);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000844
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000845 version = PyLong_FromLong(X509_get_version(certificate) + 1);
846 if (PyDict_SetItemString(retval, "version", version) < 0) {
847 Py_DECREF(version);
848 goto fail0;
849 }
850 Py_DECREF(version);
851 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000852
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000853 /* get a memory buffer */
854 biobuf = BIO_new(BIO_s_mem());
Guido van Rossumf06628b2007-11-21 20:01:53 +0000855
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000856 if (verbose) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000857
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000858 (void) BIO_reset(biobuf);
859 serialNumber = X509_get_serialNumber(certificate);
860 /* should not exceed 20 octets, 160 bits, so buf is big enough */
861 i2a_ASN1_INTEGER(biobuf, serialNumber);
862 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
863 if (len < 0) {
864 _setSSLError(NULL, 0, __FILE__, __LINE__);
865 goto fail1;
866 }
867 sn_obj = PyUnicode_FromStringAndSize(buf, len);
868 if (sn_obj == NULL)
869 goto fail1;
870 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
871 Py_DECREF(sn_obj);
872 goto fail1;
873 }
874 Py_DECREF(sn_obj);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000875
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000876 (void) BIO_reset(biobuf);
877 notBefore = X509_get_notBefore(certificate);
878 ASN1_TIME_print(biobuf, notBefore);
879 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
880 if (len < 0) {
881 _setSSLError(NULL, 0, __FILE__, __LINE__);
882 goto fail1;
883 }
884 pnotBefore = PyUnicode_FromStringAndSize(buf, len);
885 if (pnotBefore == NULL)
886 goto fail1;
887 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
888 Py_DECREF(pnotBefore);
889 goto fail1;
890 }
891 Py_DECREF(pnotBefore);
892 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000893
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000894 (void) BIO_reset(biobuf);
895 notAfter = X509_get_notAfter(certificate);
896 ASN1_TIME_print(biobuf, notAfter);
897 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
898 if (len < 0) {
899 _setSSLError(NULL, 0, __FILE__, __LINE__);
900 goto fail1;
901 }
902 pnotAfter = PyUnicode_FromStringAndSize(buf, len);
903 if (pnotAfter == NULL)
904 goto fail1;
905 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
906 Py_DECREF(pnotAfter);
907 goto fail1;
908 }
909 Py_DECREF(pnotAfter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000910
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000911 /* Now look for subjectAltName */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000912
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000913 peer_alt_names = _get_peer_alt_names(certificate);
914 if (peer_alt_names == NULL)
915 goto fail1;
916 else if (peer_alt_names != Py_None) {
917 if (PyDict_SetItemString(retval, "subjectAltName",
918 peer_alt_names) < 0) {
919 Py_DECREF(peer_alt_names);
920 goto fail1;
921 }
922 Py_DECREF(peer_alt_names);
923 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000924
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000925 BIO_free(biobuf);
926 return retval;
Thomas Woutersed03b412007-08-28 21:37:11 +0000927
928 fail1:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000929 if (biobuf != NULL)
930 BIO_free(biobuf);
Thomas Woutersed03b412007-08-28 21:37:11 +0000931 fail0:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000932 Py_XDECREF(retval);
933 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000934}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000935
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000936
937static PyObject *
938PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
939
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000940 PyObject *retval = NULL;
941 char *filename = NULL;
942 X509 *x=NULL;
943 BIO *cert;
944 int verbose = 1;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000945
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000946 if (!PyArg_ParseTuple(args, "s|i:test_decode_certificate",
947 &filename, &verbose))
948 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000949
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000950 if ((cert=BIO_new(BIO_s_file())) == NULL) {
951 PyErr_SetString(PySSLErrorObject,
952 "Can't malloc memory to read file");
953 goto fail0;
954 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000955
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000956 if (BIO_read_filename(cert,filename) <= 0) {
957 PyErr_SetString(PySSLErrorObject,
958 "Can't open file");
959 goto fail0;
960 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000961
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000962 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
963 if (x == NULL) {
964 PyErr_SetString(PySSLErrorObject,
965 "Error decoding PEM-encoded file");
966 goto fail0;
967 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000968
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000969 retval = _decode_certificate(x, verbose);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000970
971 fail0:
Guido van Rossumf06628b2007-11-21 20:01:53 +0000972
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000973 if (cert != NULL) BIO_free(cert);
974 return retval;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000975}
976
977
978static PyObject *
979PySSL_peercert(PySSLObject *self, PyObject *args)
980{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000981 PyObject *retval = NULL;
982 int len;
983 int verification;
984 PyObject *binary_mode = Py_None;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000985
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000986 if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
987 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000988
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000989 if (!self->peer_cert)
990 Py_RETURN_NONE;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000991
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000992 if (PyObject_IsTrue(binary_mode)) {
993 /* return cert in DER-encoded format */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000994
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000995 unsigned char *bytes_buf = NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000996
Antoine Pitrou30dc1a72010-05-05 16:01:14 +0000997 bytes_buf = NULL;
998 len = i2d_X509(self->peer_cert, &bytes_buf);
999 if (len < 0) {
1000 PySSL_SetError(self, len, __FILE__, __LINE__);
1001 return NULL;
1002 }
1003 /* this is actually an immutable bytes sequence */
1004 retval = PyBytes_FromStringAndSize
1005 ((const char *) bytes_buf, len);
1006 OPENSSL_free(bytes_buf);
1007 return retval;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001008
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001009 } else {
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001010
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001011 verification = SSL_CTX_get_verify_mode(self->ctx);
1012 if ((verification & SSL_VERIFY_PEER) == 0)
1013 return PyDict_New();
1014 else
1015 return _decode_certificate (self->peer_cert, 0);
1016 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001017}
1018
1019PyDoc_STRVAR(PySSL_peercert_doc,
1020"peer_certificate([der=False]) -> certificate\n\
1021\n\
1022Returns the certificate for the peer. If no certificate was provided,\n\
1023returns None. If a certificate was provided, but not validated, returns\n\
1024an empty dictionary. Otherwise returns a dict containing information\n\
1025about the peer certificate.\n\
1026\n\
1027If the optional argument is True, returns a DER-encoded copy of the\n\
1028peer certificate, or None if no certificate was provided. This will\n\
1029return the certificate even if it wasn't validated.");
1030
1031static PyObject *PySSL_cipher (PySSLObject *self) {
1032
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001033 PyObject *retval, *v;
1034 SSL_CIPHER *current;
1035 char *cipher_name;
1036 char *cipher_protocol;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001037
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001038 if (self->ssl == NULL)
1039 return Py_None;
1040 current = SSL_get_current_cipher(self->ssl);
1041 if (current == NULL)
1042 return Py_None;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001043
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001044 retval = PyTuple_New(3);
1045 if (retval == NULL)
1046 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001047
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001048 cipher_name = (char *) SSL_CIPHER_get_name(current);
1049 if (cipher_name == NULL) {
1050 PyTuple_SET_ITEM(retval, 0, Py_None);
1051 } else {
1052 v = PyUnicode_FromString(cipher_name);
1053 if (v == NULL)
1054 goto fail0;
1055 PyTuple_SET_ITEM(retval, 0, v);
1056 }
1057 cipher_protocol = SSL_CIPHER_get_version(current);
1058 if (cipher_protocol == NULL) {
1059 PyTuple_SET_ITEM(retval, 1, Py_None);
1060 } else {
1061 v = PyUnicode_FromString(cipher_protocol);
1062 if (v == NULL)
1063 goto fail0;
1064 PyTuple_SET_ITEM(retval, 1, v);
1065 }
1066 v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL));
1067 if (v == NULL)
1068 goto fail0;
1069 PyTuple_SET_ITEM(retval, 2, v);
1070 return retval;
Guido van Rossumf06628b2007-11-21 20:01:53 +00001071
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001072 fail0:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001073 Py_DECREF(retval);
1074 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001075}
1076
Guido van Rossume6650f92007-12-06 19:05:55 +00001077static void PySSL_dealloc(PySSLObject *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001078{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001079 if (self->peer_cert) /* Possible not to have one? */
1080 X509_free (self->peer_cert);
1081 if (self->ssl)
1082 SSL_free(self->ssl);
1083 if (self->ctx)
1084 SSL_CTX_free(self->ctx);
1085 Py_XDECREF(self->Socket);
1086 PyObject_Del(self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001087}
1088
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001089/* If the socket has a timeout, do a select()/poll() on the socket.
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001090 The argument writing indicates the direction.
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001091 Returns one of the possibilities in the timeout_state enum (above).
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001092 */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001093
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001094static int
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001095check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001096{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001097 fd_set fds;
1098 struct timeval tv;
1099 int rc;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001100
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001101 /* Nothing to do unless we're in timeout mode (not non-blocking) */
1102 if (s->sock_timeout < 0.0)
1103 return SOCKET_IS_BLOCKING;
1104 else if (s->sock_timeout == 0.0)
1105 return SOCKET_IS_NONBLOCKING;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001106
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001107 /* Guard against closed socket */
1108 if (s->sock_fd < 0)
1109 return SOCKET_HAS_BEEN_CLOSED;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001110
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001111 /* Prefer poll, if available, since you can poll() any fd
1112 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001113#ifdef HAVE_POLL
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001114 {
1115 struct pollfd pollfd;
1116 int timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001117
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001118 pollfd.fd = s->sock_fd;
1119 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001120
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001121 /* s->sock_timeout is in seconds, timeout in ms */
1122 timeout = (int)(s->sock_timeout * 1000 + 0.5);
1123 PySSL_BEGIN_ALLOW_THREADS
1124 rc = poll(&pollfd, 1, timeout);
1125 PySSL_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001126
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001127 goto normal_return;
1128 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001129#endif
1130
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001131 /* Guard against socket too large for select*/
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001132#ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001133 if (s->sock_fd >= FD_SETSIZE)
1134 return SOCKET_TOO_LARGE_FOR_SELECT;
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001135#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +00001136
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001137 /* Construct the arguments to select */
1138 tv.tv_sec = (int)s->sock_timeout;
1139 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1140 FD_ZERO(&fds);
1141 FD_SET(s->sock_fd, &fds);
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001142
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001143 /* See if the socket is ready */
1144 PySSL_BEGIN_ALLOW_THREADS
1145 if (writing)
1146 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
1147 else
1148 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
1149 PySSL_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001150
Bill Janssen6e027db2007-11-15 22:23:56 +00001151#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001152normal_return:
Bill Janssen6e027db2007-11-15 22:23:56 +00001153#endif
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001154 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
1155 (when we are able to write or when there's something to read) */
1156 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001157}
1158
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001159static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
1160{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001161 char *data;
1162 int len;
1163 int count;
1164 int sockstate;
1165 int err;
1166 int nonblocking;
1167 PySocketSockObject *sock
1168 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen54cc54c2007-12-14 22:08:56 +00001169
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001170 if (((PyObject*)sock) == Py_None) {
1171 _setSSLError("Underlying socket connection gone",
1172 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1173 return NULL;
1174 }
1175
1176 if (!PyArg_ParseTuple(args, "y#:write", &data, &count))
1177 return NULL;
1178
1179 /* just in case the blocking state of the socket has been changed */
1180 nonblocking = (sock->sock_timeout >= 0.0);
1181 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1182 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1183
1184 sockstate = check_socket_and_wait_for_timeout(sock, 1);
1185 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1186 PyErr_SetString(PySSLErrorObject,
1187 "The write operation timed out");
1188 return NULL;
1189 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1190 PyErr_SetString(PySSLErrorObject,
1191 "Underlying socket has been closed.");
1192 return NULL;
1193 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1194 PyErr_SetString(PySSLErrorObject,
1195 "Underlying socket too large for select().");
1196 return NULL;
1197 }
1198 do {
1199 err = 0;
1200 PySSL_BEGIN_ALLOW_THREADS
1201 len = SSL_write(self->ssl, data, count);
1202 err = SSL_get_error(self->ssl, len);
1203 PySSL_END_ALLOW_THREADS
1204 if(PyErr_CheckSignals()) {
1205 return NULL;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001206 }
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001207 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitroua29b1812010-05-12 14:08:45 +00001208 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001209 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitroua29b1812010-05-12 14:08:45 +00001210 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001211 } else {
1212 sockstate = SOCKET_OPERATION_OK;
1213 }
1214 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1215 PyErr_SetString(PySSLErrorObject,
1216 "The write operation timed out");
1217 return NULL;
1218 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1219 PyErr_SetString(PySSLErrorObject,
1220 "Underlying socket has been closed.");
1221 return NULL;
1222 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1223 break;
1224 }
1225 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
1226 if (len > 0)
1227 return PyLong_FromLong(len);
1228 else
1229 return PySSL_SetError(self, len, __FILE__, __LINE__);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001230}
1231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001233"write(s) -> len\n\
1234\n\
1235Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001236of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001237
Bill Janssen6e027db2007-11-15 22:23:56 +00001238static PyObject *PySSL_SSLpending(PySSLObject *self)
1239{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001240 int count = 0;
Bill Janssen6e027db2007-11-15 22:23:56 +00001241
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001242 PySSL_BEGIN_ALLOW_THREADS
1243 count = SSL_pending(self->ssl);
1244 PySSL_END_ALLOW_THREADS
1245 if (count < 0)
1246 return PySSL_SetError(self, count, __FILE__, __LINE__);
1247 else
1248 return PyLong_FromLong(count);
Bill Janssen6e027db2007-11-15 22:23:56 +00001249}
1250
1251PyDoc_STRVAR(PySSL_SSLpending_doc,
1252"pending() -> count\n\
1253\n\
1254Returns the number of already decrypted bytes available for read,\n\
1255pending on the connection.\n");
1256
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001257static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
1258{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001259 PyObject *dest = NULL;
1260 Py_buffer buf;
1261 int buf_passed = 0;
1262 int count = -1;
1263 char *mem;
1264 /* XXX this should use Py_ssize_t */
1265 int len = 1024;
1266 int sockstate;
1267 int err;
1268 int nonblocking;
1269 PySocketSockObject *sock
1270 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen54cc54c2007-12-14 22:08:56 +00001271
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001272 if (((PyObject*)sock) == Py_None) {
1273 _setSSLError("Underlying socket connection gone",
1274 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1275 return NULL;
1276 }
1277
1278 if (!PyArg_ParseTuple(args, "|Oi:read", &dest, &count))
1279 return NULL;
1280 if ((dest == NULL) || (dest == Py_None)) {
1281 if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len)))
1282 return NULL;
1283 mem = PyByteArray_AS_STRING(dest);
1284 } else if (PyLong_Check(dest)) {
1285 len = PyLong_AS_LONG(dest);
1286 if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len)))
1287 return NULL;
1288 mem = PyByteArray_AS_STRING(dest);
1289 } else {
1290 if (PyObject_GetBuffer(dest, &buf, PyBUF_CONTIG) < 0)
1291 return NULL;
1292 mem = buf.buf;
1293 len = buf.len;
1294 if ((count > 0) && (count <= len))
1295 len = count;
1296 buf_passed = 1;
1297 }
1298
1299 /* just in case the blocking state of the socket has been changed */
1300 nonblocking = (sock->sock_timeout >= 0.0);
1301 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1302 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1303
1304 /* first check if there are bytes ready to be read */
1305 PySSL_BEGIN_ALLOW_THREADS
1306 count = SSL_pending(self->ssl);
1307 PySSL_END_ALLOW_THREADS
1308
1309 if (!count) {
1310 sockstate = check_socket_and_wait_for_timeout(sock, 0);
1311 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1312 PyErr_SetString(PySSLErrorObject,
1313 "The read operation timed out");
1314 goto error;
1315 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1316 PyErr_SetString(PySSLErrorObject,
Antoine Pitroua29b1812010-05-12 14:08:45 +00001317 "Underlying socket too large for select().");
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001318 goto error;
1319 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1320 count = 0;
1321 goto done;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001322 }
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001323 }
1324 do {
1325 err = 0;
1326 PySSL_BEGIN_ALLOW_THREADS
1327 count = SSL_read(self->ssl, mem, len);
1328 err = SSL_get_error(self->ssl, count);
1329 PySSL_END_ALLOW_THREADS
1330 if (PyErr_CheckSignals())
1331 goto error;
1332 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitroua29b1812010-05-12 14:08:45 +00001333 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001334 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitroua29b1812010-05-12 14:08:45 +00001335 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001336 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
1337 (SSL_get_shutdown(self->ssl) ==
1338 SSL_RECEIVED_SHUTDOWN))
1339 {
1340 count = 0;
1341 goto done;
1342 } else {
1343 sockstate = SOCKET_OPERATION_OK;
1344 }
1345 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1346 PyErr_SetString(PySSLErrorObject,
1347 "The read operation timed out");
1348 goto error;
1349 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1350 break;
1351 }
1352 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
1353 if (count <= 0) {
1354 PySSL_SetError(self, count, __FILE__, __LINE__);
1355 goto error;
1356 }
Guido van Rossumf06628b2007-11-21 20:01:53 +00001357 done:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001358 if (!buf_passed) {
1359 PyObject *res = PyBytes_FromStringAndSize(mem, count);
1360 Py_DECREF(dest);
1361 return res;
1362 } else {
1363 PyBuffer_Release(&buf);
1364 return PyLong_FromLong(count);
1365 }
Benjamin Peterson56420b42009-02-28 19:06:54 +00001366 error:
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001367 if (!buf_passed) {
1368 Py_DECREF(dest);
1369 } else {
1370 PyBuffer_Release(&buf);
1371 }
1372 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001373}
1374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375PyDoc_STRVAR(PySSL_SSLread_doc,
Bill Janssen6e027db2007-11-15 22:23:56 +00001376"read([len]) -> string\n\
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001377\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001378Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001379
Bill Janssen40a0f662008-08-12 16:56:25 +00001380static PyObject *PySSL_SSLshutdown(PySSLObject *self)
1381{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001382 int err, ssl_err, sockstate, nonblocking;
1383 int zeros = 0;
1384 PySocketSockObject *sock
1385 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
Bill Janssen40a0f662008-08-12 16:56:25 +00001386
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001387 /* Guard against closed socket */
1388 if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
1389 _setSSLError("Underlying socket connection gone",
1390 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1391 return NULL;
1392 }
1393
1394 /* Just in case the blocking state of the socket has been changed */
1395 nonblocking = (sock->sock_timeout >= 0.0);
1396 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1397 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1398
1399 while (1) {
1400 PySSL_BEGIN_ALLOW_THREADS
1401 /* Disable read-ahead so that unwrap can work correctly.
1402 * Otherwise OpenSSL might read in too much data,
1403 * eating clear text data that happens to be
1404 * transmitted after the SSL shutdown.
1405 * Should be safe to call repeatedly everytime this
1406 * function is used and the shutdown_seen_zero != 0
1407 * condition is met.
1408 */
1409 if (self->shutdown_seen_zero)
1410 SSL_set_read_ahead(self->ssl, 0);
1411 err = SSL_shutdown(self->ssl);
1412 PySSL_END_ALLOW_THREADS
1413 /* If err == 1, a secure shutdown with SSL_shutdown() is complete */
1414 if (err > 0)
1415 break;
1416 if (err == 0) {
1417 /* Don't loop endlessly; instead preserve legacy
1418 behaviour of trying SSL_shutdown() only twice.
1419 This looks necessary for OpenSSL < 0.9.8m */
1420 if (++zeros > 1)
1421 break;
1422 /* Shutdown was sent, now try receiving */
1423 self->shutdown_seen_zero = 1;
1424 continue;
Bill Janssen40a0f662008-08-12 16:56:25 +00001425 }
1426
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001427 /* Possibly retry shutdown until timeout or failure */
1428 ssl_err = SSL_get_error(self->ssl, err);
1429 if (ssl_err == SSL_ERROR_WANT_READ)
1430 sockstate = check_socket_and_wait_for_timeout(sock, 0);
1431 else if (ssl_err == SSL_ERROR_WANT_WRITE)
1432 sockstate = check_socket_and_wait_for_timeout(sock, 1);
1433 else
1434 break;
1435 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1436 if (ssl_err == SSL_ERROR_WANT_READ)
1437 PyErr_SetString(PySSLErrorObject,
1438 "The read operation timed out");
1439 else
1440 PyErr_SetString(PySSLErrorObject,
1441 "The write operation timed out");
1442 return NULL;
1443 }
1444 else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1445 PyErr_SetString(PySSLErrorObject,
1446 "Underlying socket too large for select().");
1447 return NULL;
1448 }
1449 else if (sockstate != SOCKET_OPERATION_OK)
1450 /* Retain the SSL error code */
1451 break;
1452 }
Antoine Pitrou5a1c4d12010-04-23 21:11:10 +00001453
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001454 if (err < 0)
1455 return PySSL_SetError(self, err, __FILE__, __LINE__);
1456 else {
1457 Py_INCREF(sock);
1458 return (PyObject *) sock;
1459 }
Bill Janssen40a0f662008-08-12 16:56:25 +00001460}
1461
1462PyDoc_STRVAR(PySSL_SSLshutdown_doc,
1463"shutdown(s) -> socket\n\
1464\n\
1465Does the SSL shutdown handshake with the remote end, and returns\n\
1466the underlying socket object.");
1467
1468
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001469static PyMethodDef PySSLMethods[] = {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001470 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
1471 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
1472 PySSL_SSLwrite_doc},
1473 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
1474 PySSL_SSLread_doc},
1475 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
1476 PySSL_SSLpending_doc},
1477 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
1478 PySSL_peercert_doc},
1479 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
1480 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
1481 PySSL_SSLshutdown_doc},
1482 {NULL, NULL}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001483};
1484
Jeremy Hylton938ace62002-07-17 16:30:39 +00001485static PyTypeObject PySSL_Type = {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001486 PyVarObject_HEAD_INIT(NULL, 0)
1487 "ssl.SSLContext", /*tp_name*/
1488 sizeof(PySSLObject), /*tp_basicsize*/
1489 0, /*tp_itemsize*/
1490 /* methods */
1491 (destructor)PySSL_dealloc, /*tp_dealloc*/
1492 0, /*tp_print*/
1493 0, /*tp_getattr*/
1494 0, /*tp_setattr*/
1495 0, /*tp_reserved*/
1496 0, /*tp_repr*/
1497 0, /*tp_as_number*/
1498 0, /*tp_as_sequence*/
1499 0, /*tp_as_mapping*/
1500 0, /*tp_hash*/
1501 0, /*tp_call*/
1502 0, /*tp_str*/
1503 0, /*tp_getattro*/
1504 0, /*tp_setattro*/
1505 0, /*tp_as_buffer*/
1506 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1507 0, /*tp_doc*/
1508 0, /*tp_traverse*/
1509 0, /*tp_clear*/
1510 0, /*tp_richcompare*/
1511 0, /*tp_weaklistoffset*/
1512 0, /*tp_iter*/
1513 0, /*tp_iternext*/
1514 PySSLMethods, /*tp_methods*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001515};
1516
1517#ifdef HAVE_OPENSSL_RAND
1518
1519/* helper routines for seeding the SSL PRNG */
1520static PyObject *
1521PySSL_RAND_add(PyObject *self, PyObject *args)
1522{
1523 char *buf;
1524 int len;
1525 double entropy;
1526
1527 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
Antoine Pitroua29b1812010-05-12 14:08:45 +00001528 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001529 RAND_add(buf, len, entropy);
1530 Py_INCREF(Py_None);
1531 return Py_None;
1532}
1533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001535"RAND_add(string, entropy)\n\
1536\n\
1537Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001538bound on the entropy contained in string. See RFC 1750.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001539
1540static PyObject *
1541PySSL_RAND_status(PyObject *self)
1542{
Christian Heimes217cfd12007-12-02 14:31:20 +00001543 return PyLong_FromLong(RAND_status());
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001544}
1545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001546PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001547"RAND_status() -> 0 or 1\n\
1548\n\
Bill Janssen6e027db2007-11-15 22:23:56 +00001549Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
1550It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
1551using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001552
1553static PyObject *
1554PySSL_RAND_egd(PyObject *self, PyObject *arg)
1555{
1556 int bytes;
1557
Bill Janssen6e027db2007-11-15 22:23:56 +00001558 if (!PyUnicode_Check(arg))
Antoine Pitroua29b1812010-05-12 14:08:45 +00001559 return PyErr_Format(PyExc_TypeError,
1560 "RAND_egd() expected string, found %s",
1561 Py_TYPE(arg)->tp_name);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001562 bytes = RAND_egd(_PyUnicode_AsString(arg));
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001563 if (bytes == -1) {
Antoine Pitroua29b1812010-05-12 14:08:45 +00001564 PyErr_SetString(PySSLErrorObject,
1565 "EGD connection failed or EGD did not return "
1566 "enough data to seed the PRNG");
1567 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001568 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001569 return PyLong_FromLong(bytes);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001570}
1571
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001573"RAND_egd(path) -> bytes\n\
1574\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001575Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
1576Returns number of bytes read. Raises SSLError if connection to EGD\n\
1577fails or if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001578
1579#endif
1580
Bill Janssen40a0f662008-08-12 16:56:25 +00001581
1582
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001583/* List of functions exported by this module. */
1584
1585static PyMethodDef PySSL_methods[] = {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001586 {"sslwrap", PySSL_sslwrap,
1587 METH_VARARGS, ssl_doc},
1588 {"_test_decode_cert", PySSL_test_decode_certificate,
1589 METH_VARARGS},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001590#ifdef HAVE_OPENSSL_RAND
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001591 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
1592 PySSL_RAND_add_doc},
1593 {"RAND_egd", PySSL_RAND_egd, METH_O,
1594 PySSL_RAND_egd_doc},
1595 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
1596 PySSL_RAND_status_doc},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001597#endif
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001598 {NULL, NULL} /* Sentinel */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001599};
1600
1601
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001602#ifdef WITH_THREAD
1603
1604/* an implementation of OpenSSL threading operations in terms
1605 of the Python C thread library */
1606
1607static PyThread_type_lock *_ssl_locks = NULL;
1608
1609static unsigned long _ssl_thread_id_function (void) {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001610 return PyThread_get_thread_ident();
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001611}
1612
Bill Janssen6e027db2007-11-15 22:23:56 +00001613static void _ssl_thread_locking_function
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001614 (int mode, int n, const char *file, int line) {
1615 /* this function is needed to perform locking on shared data
1616 structures. (Note that OpenSSL uses a number of global data
1617 structures that will be implicitly shared whenever multiple
1618 threads use OpenSSL.) Multi-threaded applications will
1619 crash at random if it is not set.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001620
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001621 locking_function() must be able to handle up to
1622 CRYPTO_num_locks() different mutex locks. It sets the n-th
1623 lock if mode & CRYPTO_LOCK, and releases it otherwise.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001624
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001625 file and line are the file number of the function setting the
1626 lock. They can be useful for debugging.
1627 */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001628
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001629 if ((_ssl_locks == NULL) ||
1630 (n < 0) || ((unsigned)n >= _ssl_locks_count))
1631 return;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001632
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001633 if (mode & CRYPTO_LOCK) {
1634 PyThread_acquire_lock(_ssl_locks[n], 1);
1635 } else {
1636 PyThread_release_lock(_ssl_locks[n]);
1637 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001638}
1639
1640static int _setup_ssl_threads(void) {
1641
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001642 unsigned int i;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001643
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001644 if (_ssl_locks == NULL) {
1645 _ssl_locks_count = CRYPTO_num_locks();
1646 _ssl_locks = (PyThread_type_lock *)
1647 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
1648 if (_ssl_locks == NULL)
1649 return 0;
1650 memset(_ssl_locks, 0,
1651 sizeof(PyThread_type_lock) * _ssl_locks_count);
1652 for (i = 0; i < _ssl_locks_count; i++) {
1653 _ssl_locks[i] = PyThread_allocate_lock();
1654 if (_ssl_locks[i] == NULL) {
1655 unsigned int j;
1656 for (j = 0; j < i; j++) {
1657 PyThread_free_lock(_ssl_locks[j]);
1658 }
1659 free(_ssl_locks);
1660 return 0;
1661 }
1662 }
1663 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
1664 CRYPTO_set_id_callback(_ssl_thread_id_function);
1665 }
1666 return 1;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001667}
1668
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001669#endif /* def HAVE_THREAD */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001672"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001673for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001674
Martin v. Löwis1a214512008-06-11 05:26:20 +00001675
1676static struct PyModuleDef _sslmodule = {
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001677 PyModuleDef_HEAD_INIT,
1678 "_ssl",
1679 module_doc,
1680 -1,
1681 PySSL_methods,
1682 NULL,
1683 NULL,
1684 NULL,
1685 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001686};
1687
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001688PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001689PyInit__ssl(void)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001690{
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001691 PyObject *m, *d;
1692 PySocketModule_APIObject *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001693
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001694 if (PyType_Ready(&PySSL_Type) < 0)
1695 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001696
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001697 m = PyModule_Create(&_sslmodule);
1698 if (m == NULL)
1699 return NULL;
1700 d = PyModule_GetDict(m);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001701
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001702 /* Load _socket module and its C API */
1703 socket_api = PySocketModule_ImportModuleAndAPI();
1704 if (!socket_api)
1705 return NULL;
1706 PySocketModule = *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001707
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001708 /* Init OpenSSL */
1709 SSL_load_error_strings();
1710 SSL_library_init();
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001711#ifdef WITH_THREAD
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001712 /* note that this will start threading if not already started */
1713 if (!_setup_ssl_threads()) {
1714 return NULL;
1715 }
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001716#endif
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001717 OpenSSL_add_all_algorithms();
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001718
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001719 /* Add symbols to module dict */
1720 PySSLErrorObject = PyErr_NewException("ssl.SSLError",
1721 PySocketModule.error,
1722 NULL);
1723 if (PySSLErrorObject == NULL)
1724 return NULL;
1725 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0)
1726 return NULL;
1727 if (PyDict_SetItemString(d, "SSLType",
1728 (PyObject *)&PySSL_Type) != 0)
1729 return NULL;
1730 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
1731 PY_SSL_ERROR_ZERO_RETURN);
1732 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
1733 PY_SSL_ERROR_WANT_READ);
1734 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
1735 PY_SSL_ERROR_WANT_WRITE);
1736 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
1737 PY_SSL_ERROR_WANT_X509_LOOKUP);
1738 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
1739 PY_SSL_ERROR_SYSCALL);
1740 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
1741 PY_SSL_ERROR_SSL);
1742 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
1743 PY_SSL_ERROR_WANT_CONNECT);
1744 /* non ssl.h errorcodes */
1745 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
1746 PY_SSL_ERROR_EOF);
1747 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
1748 PY_SSL_ERROR_INVALID_ERROR_CODE);
1749 /* cert requirements */
1750 PyModule_AddIntConstant(m, "CERT_NONE",
1751 PY_SSL_CERT_NONE);
1752 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
1753 PY_SSL_CERT_OPTIONAL);
1754 PyModule_AddIntConstant(m, "CERT_REQUIRED",
1755 PY_SSL_CERT_REQUIRED);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001756
Antoine Pitrou30dc1a72010-05-05 16:01:14 +00001757 /* protocol versions */
1758 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
1759 PY_SSL_VERSION_SSL2);
1760 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
1761 PY_SSL_VERSION_SSL3);
1762 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
1763 PY_SSL_VERSION_SSL23);
1764 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
1765 PY_SSL_VERSION_TLS1);
1766 return m;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001767}