blob: a6c3eb8b2c9475a0b697468026e4dcb523f0e274 [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?
12
Bill Janssen6e027db2007-11-15 22:23:56 +000013 XXX what about SSL_MODE_AUTO_RETRY?
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000014*/
15
16#include "Python.h"
Thomas Woutersed03b412007-08-28 21:37:11 +000017
Thomas Wouters1b7f8912007-09-19 03:06:30 +000018#ifdef WITH_THREAD
19#include "pythread.h"
20#define PySSL_BEGIN_ALLOW_THREADS { \
Bill Janssen6e027db2007-11-15 22:23:56 +000021 PyThreadState *_save = NULL; \
Thomas Wouters1b7f8912007-09-19 03:06:30 +000022 if (_ssl_locks_count>0) {_save = PyEval_SaveThread();}
23#define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)};
24#define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()};
25#define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \
26 }
27
28#else /* no WITH_THREAD */
29
30#define PySSL_BEGIN_ALLOW_THREADS
31#define PySSL_BLOCK_THREADS
32#define PySSL_UNBLOCK_THREADS
33#define PySSL_END_ALLOW_THREADS
34
35#endif
36
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000037enum py_ssl_error {
38 /* these mirror ssl.h */
Thomas Woutersed03b412007-08-28 21:37:11 +000039 PY_SSL_ERROR_NONE,
40 PY_SSL_ERROR_SSL,
41 PY_SSL_ERROR_WANT_READ,
42 PY_SSL_ERROR_WANT_WRITE,
43 PY_SSL_ERROR_WANT_X509_LOOKUP,
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000044 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
Thomas Woutersed03b412007-08-28 21:37:11 +000045 PY_SSL_ERROR_ZERO_RETURN,
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000046 PY_SSL_ERROR_WANT_CONNECT,
Thomas Woutersed03b412007-08-28 21:37:11 +000047 /* start of non ssl.h errorcodes */
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000048 PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */
Bill Janssen54cc54c2007-12-14 22:08:56 +000049 PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000050 PY_SSL_ERROR_INVALID_ERROR_CODE
51};
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000052
Thomas Woutersed03b412007-08-28 21:37:11 +000053enum py_ssl_server_or_client {
54 PY_SSL_CLIENT,
55 PY_SSL_SERVER
56};
57
58enum py_ssl_cert_requirements {
59 PY_SSL_CERT_NONE,
60 PY_SSL_CERT_OPTIONAL,
61 PY_SSL_CERT_REQUIRED
62};
63
64enum py_ssl_version {
65 PY_SSL_VERSION_SSL2,
66 PY_SSL_VERSION_SSL3,
67 PY_SSL_VERSION_SSL23,
68 PY_SSL_VERSION_TLS1,
69};
70
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000071/* Include symbols from _socket module */
72#include "socketmodule.h"
73
Benjamin Petersonb173f782009-05-05 22:31:58 +000074static PySocketModule_APIObject PySocketModule;
75
Thomas Woutersed03b412007-08-28 21:37:11 +000076#if defined(HAVE_POLL_H)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#include <poll.h>
78#elif defined(HAVE_SYS_POLL_H)
79#include <sys/poll.h>
80#endif
81
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000082/* Include OpenSSL header files */
83#include "openssl/rsa.h"
84#include "openssl/crypto.h"
85#include "openssl/x509.h"
Thomas Wouters1b7f8912007-09-19 03:06:30 +000086#include "openssl/x509v3.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000087#include "openssl/pem.h"
88#include "openssl/ssl.h"
89#include "openssl/err.h"
90#include "openssl/rand.h"
91
92/* SSL error object */
93static PyObject *PySSLErrorObject;
94
Thomas Wouters1b7f8912007-09-19 03:06:30 +000095#ifdef WITH_THREAD
96
97/* serves as a flag to see whether we've initialized the SSL thread support. */
98/* 0 means no, greater than 0 means yes */
99
100static unsigned int _ssl_locks_count = 0;
101
102#endif /* def WITH_THREAD */
103
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000104/* SSL socket object */
105
106#define X509_NAME_MAXLEN 256
107
108/* RAND_* APIs got added to OpenSSL in 0.9.5 */
109#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
110# define HAVE_OPENSSL_RAND 1
111#else
112# undef HAVE_OPENSSL_RAND
113#endif
114
115typedef struct {
116 PyObject_HEAD
Bill Janssen54cc54c2007-12-14 22:08:56 +0000117 PyObject *Socket; /* weakref to socket on which we're layered */
Thomas Woutersed03b412007-08-28 21:37:11 +0000118 SSL_CTX* ctx;
119 SSL* ssl;
120 X509* peer_cert;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000121
122} PySSLObject;
123
Jeremy Hylton938ace62002-07-17 16:30:39 +0000124static PyTypeObject PySSL_Type;
125static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
126static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
Thomas Woutersed03b412007-08-28 21:37:11 +0000127static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000128 int writing);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000129static PyObject *PySSL_peercert(PySSLObject *self, PyObject *args);
130static PyObject *PySSL_cipher(PySSLObject *self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000131
Christian Heimes90aa7642007-12-19 02:45:37 +0000132#define PySSLObject_Check(v) (Py_TYPE(v) == &PySSL_Type)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000133
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000134typedef enum {
135 SOCKET_IS_NONBLOCKING,
136 SOCKET_IS_BLOCKING,
137 SOCKET_HAS_TIMED_OUT,
138 SOCKET_HAS_BEEN_CLOSED,
Neal Norwitz389cea82006-02-13 00:35:21 +0000139 SOCKET_TOO_LARGE_FOR_SELECT,
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000140 SOCKET_OPERATION_OK
141} timeout_state;
142
Thomas Woutersed03b412007-08-28 21:37:11 +0000143/* Wrap error strings with filename and line # */
144#define STRINGIFY1(x) #x
145#define STRINGIFY2(x) STRINGIFY1(x)
146#define ERRSTR1(x,y,z) (x ":" y ": " z)
147#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
148
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000149/* XXX It might be helpful to augment the error message generated
150 below with the name of the SSL function that generated the error.
151 I expect it's obvious most of the time.
152*/
153
154static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000155PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000156{
Thomas Woutersed03b412007-08-28 21:37:11 +0000157 PyObject *v;
158 char buf[2048];
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000159 char *errstr;
160 int err;
Thomas Woutersed03b412007-08-28 21:37:11 +0000161 enum py_ssl_error p = PY_SSL_ERROR_NONE;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000162
163 assert(ret <= 0);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000164
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000165 if (obj->ssl != NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000166 err = SSL_get_error(obj->ssl, ret);
167
168 switch (err) {
169 case SSL_ERROR_ZERO_RETURN:
170 errstr = "TLS/SSL connection has been closed";
171 p = PY_SSL_ERROR_ZERO_RETURN;
172 break;
173 case SSL_ERROR_WANT_READ:
174 errstr = "The operation did not complete (read)";
175 p = PY_SSL_ERROR_WANT_READ;
176 break;
177 case SSL_ERROR_WANT_WRITE:
178 p = PY_SSL_ERROR_WANT_WRITE;
179 errstr = "The operation did not complete (write)";
180 break;
181 case SSL_ERROR_WANT_X509_LOOKUP:
182 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
183 errstr =
184 "The operation did not complete (X509 lookup)";
185 break;
186 case SSL_ERROR_WANT_CONNECT:
187 p = PY_SSL_ERROR_WANT_CONNECT;
188 errstr = "The operation did not complete (connect)";
189 break;
190 case SSL_ERROR_SYSCALL:
191 {
192 unsigned long e = ERR_get_error();
193 if (e == 0) {
Bill Janssen54cc54c2007-12-14 22:08:56 +0000194 PySocketSockObject *s
195 = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
196 if (ret == 0 || (((PyObject *)s) == Py_None)) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000197 p = PY_SSL_ERROR_EOF;
198 errstr =
199 "EOF occurred in violation of protocol";
200 } else if (ret == -1) {
201 /* underlying BIO reported an I/O error */
Bill Janssen54cc54c2007-12-14 22:08:56 +0000202 return s->errorhandler();
Thomas Woutersed03b412007-08-28 21:37:11 +0000203 } else { /* possible? */
204 p = PY_SSL_ERROR_SYSCALL;
205 errstr = "Some I/O error occurred";
206 }
207 } else {
Jeremy Hylton4e547302002-07-02 18:25:00 +0000208 p = PY_SSL_ERROR_SYSCALL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000209 /* XXX Protected by global interpreter lock */
210 errstr = ERR_error_string(e, NULL);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000211 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000212 break;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000213 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000214 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? */
222 errstr =
223 "A failure in the SSL library occurred";
224 }
225 break;
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000226 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000227 default:
228 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
229 errstr = "Invalid error code";
230 }
231 } else {
232 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000233 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000234 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
235 v = Py_BuildValue("(is)", p, buf);
236 if (v != NULL) {
237 PyErr_SetObject(PySSLErrorObject, v);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238 Py_DECREF(v);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000239 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000240 return NULL;
241}
242
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000243static PyObject *
244_setSSLError (char *errstr, int errcode, char *filename, int lineno) {
245
246 char buf[2048];
247 PyObject *v;
248
249 if (errstr == NULL) {
250 errcode = ERR_peek_last_error();
251 errstr = ERR_error_string(errcode, NULL);
252 }
253 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
254 v = Py_BuildValue("(is)", errcode, buf);
255 if (v != NULL) {
256 PyErr_SetObject(PySSLErrorObject, v);
257 Py_DECREF(v);
258 }
259 return NULL;
260}
261
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000262static PySSLObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000263newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file,
264 enum py_ssl_server_or_client socket_type,
265 enum py_ssl_cert_requirements certreq,
266 enum py_ssl_version proto_version,
267 char *cacerts_file)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000268{
269 PySSLObject *self;
270 char *errstr = NULL;
271 int ret;
Thomas Woutersed03b412007-08-28 21:37:11 +0000272 int verification_mode;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000273
Guido van Rossume6650f92007-12-06 19:05:55 +0000274 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275 if (self == NULL)
276 return NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000277 self->peer_cert = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000278 self->ssl = NULL;
279 self->ctx = NULL;
280 self->Socket = NULL;
281
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000282 /* Make sure the SSL error state is initialized */
283 (void) ERR_get_state();
284 ERR_clear_error();
285
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000286 if ((key_file && !cert_file) || (!key_file && cert_file)) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000287 errstr = ERRSTR("Both the key & certificate files "
288 "must be specified");
289 goto fail;
290 }
291
292 if ((socket_type == PY_SSL_SERVER) &&
293 ((key_file == NULL) || (cert_file == NULL))) {
294 errstr = ERRSTR("Both the key & certificate files "
295 "must be specified for server-side operation");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000296 goto fail;
297 }
298
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000299 PySSL_BEGIN_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000300 if (proto_version == PY_SSL_VERSION_TLS1)
301 self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
302 else if (proto_version == PY_SSL_VERSION_SSL3)
303 self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
304 else if (proto_version == PY_SSL_VERSION_SSL2)
305 self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000306 else if (proto_version == PY_SSL_VERSION_SSL23)
Thomas Woutersed03b412007-08-28 21:37:11 +0000307 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000308 PySSL_END_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000309
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000310 if (self->ctx == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000311 errstr = ERRSTR("Invalid SSL protocol variant specified.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000312 goto fail;
313 }
314
Thomas Woutersed03b412007-08-28 21:37:11 +0000315 if (certreq != PY_SSL_CERT_NONE) {
316 if (cacerts_file == NULL) {
317 errstr = ERRSTR("No root certificates specified for "
318 "verification of other-side certificates.");
319 goto fail;
320 } else {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000321 PySSL_BEGIN_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000322 ret = SSL_CTX_load_verify_locations(self->ctx,
323 cacerts_file,
324 NULL);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000325 PySSL_END_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000326 if (ret != 1) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000327 _setSSLError(NULL, 0, __FILE__, __LINE__);
Thomas Woutersed03b412007-08-28 21:37:11 +0000328 goto fail;
329 }
330 }
331 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000332 if (key_file) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000333 PySSL_BEGIN_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000334 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
Thomas Woutersed03b412007-08-28 21:37:11 +0000335 SSL_FILETYPE_PEM);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000336 PySSL_END_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000337 if (ret != 1) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000338 _setSSLError(NULL, ret, __FILE__, __LINE__);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000339 goto fail;
340 }
341
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000342 PySSL_BEGIN_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000343 ret = SSL_CTX_use_certificate_chain_file(self->ctx,
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000344 cert_file);
345 PySSL_END_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000346 if (ret != 1) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000347 /*
348 fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n",
349 ret, ERR_peek_error(), ERR_peek_last_error(), cert_file);
350 */
351 if (ERR_peek_last_error() != 0) {
352 _setSSLError(NULL, ret, __FILE__, __LINE__);
353 goto fail;
354 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000355 }
356 }
357
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000358 /* ssl compatibility */
359 SSL_CTX_set_options(self->ctx, SSL_OP_ALL);
360
Thomas Woutersed03b412007-08-28 21:37:11 +0000361 verification_mode = SSL_VERIFY_NONE;
362 if (certreq == PY_SSL_CERT_OPTIONAL)
363 verification_mode = SSL_VERIFY_PEER;
364 else if (certreq == PY_SSL_CERT_REQUIRED)
365 verification_mode = (SSL_VERIFY_PEER |
366 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
367 SSL_CTX_set_verify(self->ctx, verification_mode,
368 NULL); /* set verify lvl */
369
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000370 PySSL_BEGIN_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000371 self->ssl = SSL_new(self->ctx); /* New ssl struct */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000372 PySSL_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000373 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000374
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000375 /* If the socket is in non-blocking mode or timeout mode, set the BIO
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000376 * to non-blocking mode (blocking is the default)
377 */
378 if (Sock->sock_timeout >= 0.0) {
379 /* Set both the read and write BIO's to non-blocking mode */
380 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
381 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
382 }
383
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000384 PySSL_BEGIN_ALLOW_THREADS
Thomas Woutersed03b412007-08-28 21:37:11 +0000385 if (socket_type == PY_SSL_CLIENT)
386 SSL_set_connect_state(self->ssl);
387 else
388 SSL_set_accept_state(self->ssl);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000389 PySSL_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000390
Bill Janssen54cc54c2007-12-14 22:08:56 +0000391 self->Socket = PyWeakref_NewRef((PyObject *) Sock, Py_None);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000392 return self;
393 fail:
394 if (errstr)
395 PyErr_SetString(PySSLErrorObject, errstr);
396 Py_DECREF(self);
397 return NULL;
398}
399
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000400static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000401PySSL_sslwrap(PyObject *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000402{
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000403 PySocketSockObject *Sock;
Thomas Woutersed03b412007-08-28 21:37:11 +0000404 int server_side = 0;
405 int verification_mode = PY_SSL_CERT_NONE;
406 int protocol = PY_SSL_VERSION_SSL23;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000407 char *key_file = NULL;
408 char *cert_file = NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000409 char *cacerts_file = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000410
Thomas Woutersed03b412007-08-28 21:37:11 +0000411 if (!PyArg_ParseTuple(args, "O!i|zziiz:sslwrap",
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000412 PySocketModule.Sock_Type,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000413 &Sock,
Thomas Woutersed03b412007-08-28 21:37:11 +0000414 &server_side,
415 &key_file, &cert_file,
416 &verification_mode, &protocol,
417 &cacerts_file))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000418 return NULL;
419
Thomas Woutersed03b412007-08-28 21:37:11 +0000420 /*
421 fprintf(stderr,
422 "server_side is %d, keyfile %p, certfile %p, verify_mode %d, "
423 "protocol %d, certs %p\n",
424 server_side, key_file, cert_file, verification_mode,
425 protocol, cacerts_file);
426 */
427
428 return (PyObject *) newPySSLObject(Sock, key_file, cert_file,
429 server_side, verification_mode,
430 protocol, cacerts_file);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000431}
432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000433PyDoc_STRVAR(ssl_doc,
Thomas Woutersed03b412007-08-28 21:37:11 +0000434"sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n"
435" cacertsfile]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000436
437/* SSL object methods */
438
Bill Janssen6e027db2007-11-15 22:23:56 +0000439static PyObject *PySSL_SSLdo_handshake(PySSLObject *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000440{
Bill Janssen6e027db2007-11-15 22:23:56 +0000441 int ret;
442 int err;
443 int sockstate;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000444
Bill Janssen6e027db2007-11-15 22:23:56 +0000445 /* Actually negotiate SSL connection */
446 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
447 sockstate = 0;
448 do {
Bill Janssen54cc54c2007-12-14 22:08:56 +0000449 PySocketSockObject *sock
450 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
451 if (((PyObject*)sock) == Py_None) {
452 _setSSLError("Underlying socket connection gone",
453 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
454 return NULL;
455 }
456
Bill Janssen6e027db2007-11-15 22:23:56 +0000457 PySSL_BEGIN_ALLOW_THREADS
458 ret = SSL_do_handshake(self->ssl);
459 err = SSL_get_error(self->ssl, ret);
460 PySSL_END_ALLOW_THREADS
461 if(PyErr_CheckSignals()) {
462 return NULL;
463 }
464 if (err == SSL_ERROR_WANT_READ) {
Bill Janssen54cc54c2007-12-14 22:08:56 +0000465 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Bill Janssen6e027db2007-11-15 22:23:56 +0000466 } else if (err == SSL_ERROR_WANT_WRITE) {
Bill Janssen54cc54c2007-12-14 22:08:56 +0000467 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Bill Janssen6e027db2007-11-15 22:23:56 +0000468 } else {
469 sockstate = SOCKET_OPERATION_OK;
470 }
471 if (sockstate == SOCKET_HAS_TIMED_OUT) {
472 PyErr_SetString(PySSLErrorObject,
473 ERRSTR("The handshake operation timed out"));
474 return NULL;
475 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
476 PyErr_SetString(PySSLErrorObject,
477 ERRSTR("Underlying socket has been closed."));
478 return NULL;
479 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
480 PyErr_SetString(PySSLErrorObject,
481 ERRSTR("Underlying socket too large for select()."));
482 return NULL;
483 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
484 break;
485 }
486 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
487 if (ret < 1)
488 return PySSL_SetError(self, ret, __FILE__, __LINE__);
489 self->ssl->debug = 1;
490
491 if (self->peer_cert)
492 X509_free (self->peer_cert);
493 PySSL_BEGIN_ALLOW_THREADS
494 self->peer_cert = SSL_get_peer_certificate(self->ssl);
495 PySSL_END_ALLOW_THREADS
496
497 Py_INCREF(Py_None);
498 return Py_None;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000499}
500
Thomas Woutersed03b412007-08-28 21:37:11 +0000501static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000502_create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000503
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000504 char namebuf[X509_NAME_MAXLEN];
505 int buflen;
506 PyObject *name_obj;
507 PyObject *value_obj;
508 PyObject *attr;
509 unsigned char *valuebuf = NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000510
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000511 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
512 if (buflen < 0) {
513 _setSSLError(NULL, 0, __FILE__, __LINE__);
514 goto fail;
Thomas Woutersed03b412007-08-28 21:37:11 +0000515 }
Bill Janssen6e027db2007-11-15 22:23:56 +0000516 name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000517 if (name_obj == NULL)
518 goto fail;
Guido van Rossumf06628b2007-11-21 20:01:53 +0000519
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000520 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
521 if (buflen < 0) {
522 _setSSLError(NULL, 0, __FILE__, __LINE__);
523 Py_DECREF(name_obj);
524 goto fail;
525 }
526 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
527 buflen, "strict");
528 OPENSSL_free(valuebuf);
529 if (value_obj == NULL) {
530 Py_DECREF(name_obj);
531 goto fail;
532 }
533 attr = PyTuple_New(2);
534 if (attr == NULL) {
535 Py_DECREF(name_obj);
536 Py_DECREF(value_obj);
537 goto fail;
538 }
539 PyTuple_SET_ITEM(attr, 0, name_obj);
540 PyTuple_SET_ITEM(attr, 1, value_obj);
541 return attr;
Thomas Woutersed03b412007-08-28 21:37:11 +0000542
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000543 fail:
Thomas Woutersed03b412007-08-28 21:37:11 +0000544 return NULL;
545}
546
547static PyObject *
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000548_create_tuple_for_X509_NAME (X509_NAME *xname)
Thomas Woutersed03b412007-08-28 21:37:11 +0000549{
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000550 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
551 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
552 PyObject *rdnt;
553 PyObject *attr = NULL; /* tuple to hold an attribute */
554 int entry_count = X509_NAME_entry_count(xname);
555 X509_NAME_ENTRY *entry;
556 ASN1_OBJECT *name;
557 ASN1_STRING *value;
558 int index_counter;
559 int rdn_level = -1;
560 int retcode;
561
562 dn = PyList_New(0);
563 if (dn == NULL)
564 return NULL;
565 /* now create another tuple to hold the top-level RDN */
566 rdn = PyList_New(0);
567 if (rdn == NULL)
568 goto fail0;
569
570 for (index_counter = 0;
571 index_counter < entry_count;
572 index_counter++)
573 {
574 entry = X509_NAME_get_entry(xname, index_counter);
575
576 /* check to see if we've gotten to a new RDN */
577 if (rdn_level >= 0) {
578 if (rdn_level != entry->set) {
579 /* yes, new RDN */
580 /* add old RDN to DN */
581 rdnt = PyList_AsTuple(rdn);
582 Py_DECREF(rdn);
583 if (rdnt == NULL)
584 goto fail0;
585 retcode = PyList_Append(dn, rdnt);
586 Py_DECREF(rdnt);
587 if (retcode < 0)
588 goto fail0;
589 /* create new RDN */
590 rdn = PyList_New(0);
591 if (rdn == NULL)
592 goto fail0;
593 }
594 }
595 rdn_level = entry->set;
596
597 /* now add this attribute to the current RDN */
598 name = X509_NAME_ENTRY_get_object(entry);
599 value = X509_NAME_ENTRY_get_data(entry);
600 attr = _create_tuple_for_attribute(name, value);
601 /*
602 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
603 entry->set,
Christian Heimes72b710a2008-05-26 13:28:38 +0000604 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
605 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000606 */
607 if (attr == NULL)
608 goto fail1;
609 retcode = PyList_Append(rdn, attr);
610 Py_DECREF(attr);
611 if (retcode < 0)
612 goto fail1;
613 }
614 /* now, there's typically a dangling RDN */
615 if ((rdn != NULL) && (PyList_Size(rdn) > 0)) {
616 rdnt = PyList_AsTuple(rdn);
617 Py_DECREF(rdn);
618 if (rdnt == NULL)
619 goto fail0;
620 retcode = PyList_Append(dn, rdnt);
621 Py_DECREF(rdnt);
622 if (retcode < 0)
623 goto fail0;
624 }
625
626 /* convert list to tuple */
627 rdnt = PyList_AsTuple(dn);
628 Py_DECREF(dn);
629 if (rdnt == NULL)
630 return NULL;
631 return rdnt;
632
633 fail1:
634 Py_XDECREF(rdn);
635
636 fail0:
637 Py_XDECREF(dn);
638 return NULL;
639}
640
641static PyObject *
642_get_peer_alt_names (X509 *certificate) {
Guido van Rossumf06628b2007-11-21 20:01:53 +0000643
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000644 /* this code follows the procedure outlined in
645 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
646 function to extract the STACK_OF(GENERAL_NAME),
647 then iterates through the stack to add the
648 names. */
649
650 int i, j;
651 PyObject *peer_alt_names = Py_None;
652 PyObject *v, *t;
653 X509_EXTENSION *ext = NULL;
654 GENERAL_NAMES *names = NULL;
655 GENERAL_NAME *name;
Guido van Rossumf06628b2007-11-21 20:01:53 +0000656 X509V3_EXT_METHOD *method;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000657 BIO *biobuf = NULL;
658 char buf[2048];
659 char *vptr;
660 int len;
Victor Stinner7124a412010-03-02 22:48:17 +0000661 /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
662#if OPENSSL_VERSION_NUMBER >= 0x009060dfL
663 const unsigned char *p;
664#else
Benjamin Peterson0289b152009-06-28 17:22:03 +0000665 unsigned char *p;
Victor Stinner7124a412010-03-02 22:48:17 +0000666#endif
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000667
668 if (certificate == NULL)
669 return peer_alt_names;
670
671 /* get a memory buffer */
672 biobuf = BIO_new(BIO_s_mem());
673
674 i = 0;
675 while ((i = X509_get_ext_by_NID(
676 certificate, NID_subject_alt_name, i)) >= 0) {
677
678 if (peer_alt_names == Py_None) {
679 peer_alt_names = PyList_New(0);
680 if (peer_alt_names == NULL)
681 goto fail;
682 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000683
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000684 /* now decode the altName */
685 ext = X509_get_ext(certificate, i);
686 if(!(method = X509V3_EXT_get(ext))) {
Bill Janssen6e027db2007-11-15 22:23:56 +0000687 PyErr_SetString
688 (PySSLErrorObject,
689 ERRSTR("No method for internalizing subjectAltName!"));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000690 goto fail;
691 }
692
693 p = ext->value->data;
Christian Heimes412dc9c2008-01-27 18:55:54 +0000694 if (method->it)
Bill Janssen6e027db2007-11-15 22:23:56 +0000695 names = (GENERAL_NAMES*)
696 (ASN1_item_d2i(NULL,
697 &p,
698 ext->value->length,
699 ASN1_ITEM_ptr(method->it)));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000700 else
Bill Janssen6e027db2007-11-15 22:23:56 +0000701 names = (GENERAL_NAMES*)
702 (method->d2i(NULL,
703 &p,
704 ext->value->length));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000705
706 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
707
708 /* get a rendering of each name in the set of names */
709
710 name = sk_GENERAL_NAME_value(names, j);
711 if (name->type == GEN_DIRNAME) {
712
Bill Janssen6e027db2007-11-15 22:23:56 +0000713 /* we special-case DirName as a tuple of
714 tuples of attributes */
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000715
716 t = PyTuple_New(2);
717 if (t == NULL) {
718 goto fail;
719 }
720
Bill Janssen6e027db2007-11-15 22:23:56 +0000721 v = PyUnicode_FromString("DirName");
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000722 if (v == NULL) {
723 Py_DECREF(t);
724 goto fail;
725 }
726 PyTuple_SET_ITEM(t, 0, v);
727
728 v = _create_tuple_for_X509_NAME (name->d.dirn);
729 if (v == NULL) {
730 Py_DECREF(t);
731 goto fail;
732 }
733 PyTuple_SET_ITEM(t, 1, v);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000734
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000735 } else {
736
737 /* for everything else, we use the OpenSSL print form */
738
739 (void) BIO_reset(biobuf);
740 GENERAL_NAME_print(biobuf, name);
741 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
742 if (len < 0) {
743 _setSSLError(NULL, 0, __FILE__, __LINE__);
744 goto fail;
745 }
746 vptr = strchr(buf, ':');
747 if (vptr == NULL)
748 goto fail;
749 t = PyTuple_New(2);
750 if (t == NULL)
751 goto fail;
Bill Janssen6e027db2007-11-15 22:23:56 +0000752 v = PyUnicode_FromStringAndSize(buf, (vptr - buf));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000753 if (v == NULL) {
754 Py_DECREF(t);
755 goto fail;
756 }
757 PyTuple_SET_ITEM(t, 0, v);
Bill Janssen6e027db2007-11-15 22:23:56 +0000758 v = PyUnicode_FromStringAndSize((vptr + 1),
759 (len - (vptr - buf + 1)));
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000760 if (v == NULL) {
761 Py_DECREF(t);
762 goto fail;
763 }
764 PyTuple_SET_ITEM(t, 1, v);
765 }
766
767 /* and add that rendering to the list */
768
769 if (PyList_Append(peer_alt_names, t) < 0) {
770 Py_DECREF(t);
771 goto fail;
772 }
773 Py_DECREF(t);
774 }
775 }
776 BIO_free(biobuf);
777 if (peer_alt_names != Py_None) {
778 v = PyList_AsTuple(peer_alt_names);
779 Py_DECREF(peer_alt_names);
780 return v;
781 } else {
782 return peer_alt_names;
783 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000784
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000785
786 fail:
787 if (biobuf != NULL)
788 BIO_free(biobuf);
789
790 if (peer_alt_names != Py_None) {
791 Py_XDECREF(peer_alt_names);
792 }
793
794 return NULL;
795}
796
797static PyObject *
798_decode_certificate (X509 *certificate, int verbose) {
799
Thomas Woutersed03b412007-08-28 21:37:11 +0000800 PyObject *retval = NULL;
801 BIO *biobuf = NULL;
802 PyObject *peer;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000803 PyObject *peer_alt_names = NULL;
Thomas Woutersed03b412007-08-28 21:37:11 +0000804 PyObject *issuer;
805 PyObject *version;
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000806 PyObject *sn_obj;
807 ASN1_INTEGER *serialNumber;
Thomas Woutersed03b412007-08-28 21:37:11 +0000808 char buf[2048];
809 int len;
810 ASN1_TIME *notBefore, *notAfter;
811 PyObject *pnotBefore, *pnotAfter;
Thomas Woutersed03b412007-08-28 21:37:11 +0000812
813 retval = PyDict_New();
814 if (retval == NULL)
815 return NULL;
816
Thomas Wouters89d996e2007-09-08 17:39:28 +0000817 peer = _create_tuple_for_X509_NAME(
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000818 X509_get_subject_name(certificate));
Thomas Woutersed03b412007-08-28 21:37:11 +0000819 if (peer == NULL)
820 goto fail0;
821 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
822 Py_DECREF(peer);
823 goto fail0;
824 }
825 Py_DECREF(peer);
826
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000827 if (verbose) {
828 issuer = _create_tuple_for_X509_NAME(
829 X509_get_issuer_name(certificate));
830 if (issuer == NULL)
831 goto fail0;
832 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
833 Py_DECREF(issuer);
834 goto fail0;
835 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000836 Py_DECREF(issuer);
Guido van Rossumf06628b2007-11-21 20:01:53 +0000837
Christian Heimes217cfd12007-12-02 14:31:20 +0000838 version = PyLong_FromLong(X509_get_version(certificate) + 1);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000839 if (PyDict_SetItemString(retval, "version", version) < 0) {
840 Py_DECREF(version);
841 goto fail0;
842 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000843 Py_DECREF(version);
Thomas Woutersed03b412007-08-28 21:37:11 +0000844 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000845
Thomas Woutersed03b412007-08-28 21:37:11 +0000846 /* get a memory buffer */
847 biobuf = BIO_new(BIO_s_mem());
Guido van Rossumf06628b2007-11-21 20:01:53 +0000848
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000849 if (verbose) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000850
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000851 (void) BIO_reset(biobuf);
852 serialNumber = X509_get_serialNumber(certificate);
853 /* should not exceed 20 octets, 160 bits, so buf is big enough */
854 i2a_ASN1_INTEGER(biobuf, serialNumber);
855 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
856 if (len < 0) {
857 _setSSLError(NULL, 0, __FILE__, __LINE__);
858 goto fail1;
859 }
Bill Janssen6e027db2007-11-15 22:23:56 +0000860 sn_obj = PyUnicode_FromStringAndSize(buf, len);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000861 if (sn_obj == NULL)
862 goto fail1;
863 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
864 Py_DECREF(sn_obj);
865 goto fail1;
866 }
867 Py_DECREF(sn_obj);
868
869 (void) BIO_reset(biobuf);
870 notBefore = X509_get_notBefore(certificate);
871 ASN1_TIME_print(biobuf, notBefore);
872 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
873 if (len < 0) {
874 _setSSLError(NULL, 0, __FILE__, __LINE__);
875 goto fail1;
876 }
Bill Janssen6e027db2007-11-15 22:23:56 +0000877 pnotBefore = PyUnicode_FromStringAndSize(buf, len);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000878 if (pnotBefore == NULL)
879 goto fail1;
880 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
881 Py_DECREF(pnotBefore);
882 goto fail1;
883 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000884 Py_DECREF(pnotBefore);
Thomas Woutersed03b412007-08-28 21:37:11 +0000885 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000886
887 (void) BIO_reset(biobuf);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000888 notAfter = X509_get_notAfter(certificate);
Thomas Woutersed03b412007-08-28 21:37:11 +0000889 ASN1_TIME_print(biobuf, notAfter);
890 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000891 if (len < 0) {
892 _setSSLError(NULL, 0, __FILE__, __LINE__);
893 goto fail1;
894 }
Bill Janssen6e027db2007-11-15 22:23:56 +0000895 pnotAfter = PyUnicode_FromStringAndSize(buf, len);
Thomas Woutersed03b412007-08-28 21:37:11 +0000896 if (pnotAfter == NULL)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000897 goto fail1;
Thomas Woutersed03b412007-08-28 21:37:11 +0000898 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
899 Py_DECREF(pnotAfter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000900 goto fail1;
Thomas Woutersed03b412007-08-28 21:37:11 +0000901 }
902 Py_DECREF(pnotAfter);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000903
904 /* Now look for subjectAltName */
905
906 peer_alt_names = _get_peer_alt_names(certificate);
907 if (peer_alt_names == NULL)
908 goto fail1;
909 else if (peer_alt_names != Py_None) {
910 if (PyDict_SetItemString(retval, "subjectAltName",
911 peer_alt_names) < 0) {
912 Py_DECREF(peer_alt_names);
913 goto fail1;
914 }
915 Py_DECREF(peer_alt_names);
916 }
Guido van Rossumf06628b2007-11-21 20:01:53 +0000917
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000918 BIO_free(biobuf);
Thomas Woutersed03b412007-08-28 21:37:11 +0000919 return retval;
920
921 fail1:
922 if (biobuf != NULL)
923 BIO_free(biobuf);
924 fail0:
925 Py_XDECREF(retval);
926 return NULL;
927}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000928
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000929
930static PyObject *
931PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
932
933 PyObject *retval = NULL;
934 char *filename = NULL;
935 X509 *x=NULL;
936 BIO *cert;
937 int verbose = 1;
938
Bill Janssen6e027db2007-11-15 22:23:56 +0000939 if (!PyArg_ParseTuple(args, "s|i:test_decode_certificate",
940 &filename, &verbose))
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000941 return NULL;
942
943 if ((cert=BIO_new(BIO_s_file())) == NULL) {
Bill Janssen6e027db2007-11-15 22:23:56 +0000944 PyErr_SetString(PySSLErrorObject,
945 "Can't malloc memory to read file");
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000946 goto fail0;
947 }
948
949 if (BIO_read_filename(cert,filename) <= 0) {
Bill Janssen6e027db2007-11-15 22:23:56 +0000950 PyErr_SetString(PySSLErrorObject,
951 "Can't open file");
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000952 goto fail0;
953 }
954
955 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
956 if (x == NULL) {
Bill Janssen6e027db2007-11-15 22:23:56 +0000957 PyErr_SetString(PySSLErrorObject,
958 "Error decoding PEM-encoded file");
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000959 goto fail0;
960 }
961
962 retval = _decode_certificate(x, verbose);
963
964 fail0:
Guido van Rossumf06628b2007-11-21 20:01:53 +0000965
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000966 if (cert != NULL) BIO_free(cert);
967 return retval;
968}
969
970
971static PyObject *
972PySSL_peercert(PySSLObject *self, PyObject *args)
973{
974 PyObject *retval = NULL;
975 int len;
976 int verification;
977 PyObject *binary_mode = Py_None;
978
979 if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
980 return NULL;
981
982 if (!self->peer_cert)
983 Py_RETURN_NONE;
984
985 if (PyObject_IsTrue(binary_mode)) {
986 /* return cert in DER-encoded format */
987
988 unsigned char *bytes_buf = NULL;
989
990 bytes_buf = NULL;
991 len = i2d_X509(self->peer_cert, &bytes_buf);
992 if (len < 0) {
993 PySSL_SetError(self, len, __FILE__, __LINE__);
994 return NULL;
995 }
Bill Janssen6e027db2007-11-15 22:23:56 +0000996 /* this is actually an immutable bytes sequence */
Christian Heimes72b710a2008-05-26 13:28:38 +0000997 retval = PyBytes_FromStringAndSize
Bill Janssen6e027db2007-11-15 22:23:56 +0000998 ((const char *) bytes_buf, len);
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000999 OPENSSL_free(bytes_buf);
1000 return retval;
1001
1002 } else {
1003
1004 verification = SSL_CTX_get_verify_mode(self->ctx);
1005 if ((verification & SSL_VERIFY_PEER) == 0)
1006 return PyDict_New();
1007 else
1008 return _decode_certificate (self->peer_cert, 0);
1009 }
1010}
1011
1012PyDoc_STRVAR(PySSL_peercert_doc,
1013"peer_certificate([der=False]) -> certificate\n\
1014\n\
1015Returns the certificate for the peer. If no certificate was provided,\n\
1016returns None. If a certificate was provided, but not validated, returns\n\
1017an empty dictionary. Otherwise returns a dict containing information\n\
1018about the peer certificate.\n\
1019\n\
1020If the optional argument is True, returns a DER-encoded copy of the\n\
1021peer certificate, or None if no certificate was provided. This will\n\
1022return the certificate even if it wasn't validated.");
1023
1024static PyObject *PySSL_cipher (PySSLObject *self) {
1025
1026 PyObject *retval, *v;
1027 SSL_CIPHER *current;
1028 char *cipher_name;
1029 char *cipher_protocol;
1030
1031 if (self->ssl == NULL)
1032 return Py_None;
1033 current = SSL_get_current_cipher(self->ssl);
1034 if (current == NULL)
1035 return Py_None;
1036
1037 retval = PyTuple_New(3);
1038 if (retval == NULL)
1039 return NULL;
1040
1041 cipher_name = (char *) SSL_CIPHER_get_name(current);
1042 if (cipher_name == NULL) {
1043 PyTuple_SET_ITEM(retval, 0, Py_None);
1044 } else {
Bill Janssen6e027db2007-11-15 22:23:56 +00001045 v = PyUnicode_FromString(cipher_name);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001046 if (v == NULL)
1047 goto fail0;
1048 PyTuple_SET_ITEM(retval, 0, v);
1049 }
1050 cipher_protocol = SSL_CIPHER_get_version(current);
1051 if (cipher_protocol == NULL) {
1052 PyTuple_SET_ITEM(retval, 1, Py_None);
1053 } else {
Bill Janssen6e027db2007-11-15 22:23:56 +00001054 v = PyUnicode_FromString(cipher_protocol);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001055 if (v == NULL)
1056 goto fail0;
1057 PyTuple_SET_ITEM(retval, 1, v);
1058 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001059 v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL));
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001060 if (v == NULL)
1061 goto fail0;
1062 PyTuple_SET_ITEM(retval, 2, v);
1063 return retval;
Guido van Rossumf06628b2007-11-21 20:01:53 +00001064
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001065 fail0:
1066 Py_DECREF(retval);
1067 return NULL;
1068}
1069
Guido van Rossume6650f92007-12-06 19:05:55 +00001070static void PySSL_dealloc(PySSLObject *self)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001071{
Thomas Woutersed03b412007-08-28 21:37:11 +00001072 if (self->peer_cert) /* Possible not to have one? */
Guido van Rossume6650f92007-12-06 19:05:55 +00001073 X509_free (self->peer_cert);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001074 if (self->ssl)
Thomas Woutersed03b412007-08-28 21:37:11 +00001075 SSL_free(self->ssl);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001076 if (self->ctx)
Thomas Woutersed03b412007-08-28 21:37:11 +00001077 SSL_CTX_free(self->ctx);
Guido van Rossume6650f92007-12-06 19:05:55 +00001078 Py_XDECREF(self->Socket);
1079 PyObject_Del(self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001080}
1081
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001082/* If the socket has a timeout, do a select()/poll() on the socket.
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001083 The argument writing indicates the direction.
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001084 Returns one of the possibilities in the timeout_state enum (above).
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001085 */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001086
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001087static int
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001088check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001089{
1090 fd_set fds;
1091 struct timeval tv;
1092 int rc;
1093
1094 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001095 if (s->sock_timeout < 0.0)
1096 return SOCKET_IS_BLOCKING;
1097 else if (s->sock_timeout == 0.0)
1098 return SOCKET_IS_NONBLOCKING;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001099
1100 /* Guard against closed socket */
1101 if (s->sock_fd < 0)
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001102 return SOCKET_HAS_BEEN_CLOSED;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001103
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001104 /* Prefer poll, if available, since you can poll() any fd
1105 * which can't be done with select(). */
1106#ifdef HAVE_POLL
1107 {
1108 struct pollfd pollfd;
1109 int timeout;
1110
1111 pollfd.fd = s->sock_fd;
1112 pollfd.events = writing ? POLLOUT : POLLIN;
1113
1114 /* s->sock_timeout is in seconds, timeout in ms */
1115 timeout = (int)(s->sock_timeout * 1000 + 0.5);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001116 PySSL_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001117 rc = poll(&pollfd, 1, timeout);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001118 PySSL_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001119
1120 goto normal_return;
1121 }
1122#endif
1123
Neal Norwitz082b2df2006-02-07 07:04:46 +00001124 /* Guard against socket too large for select*/
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001125#ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
Neal Norwitz082b2df2006-02-07 07:04:46 +00001126 if (s->sock_fd >= FD_SETSIZE)
Neal Norwitz389cea82006-02-13 00:35:21 +00001127 return SOCKET_TOO_LARGE_FOR_SELECT;
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001128#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +00001129
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001130 /* Construct the arguments to select */
1131 tv.tv_sec = (int)s->sock_timeout;
1132 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1133 FD_ZERO(&fds);
1134 FD_SET(s->sock_fd, &fds);
1135
1136 /* See if the socket is ready */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001137 PySSL_BEGIN_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001138 if (writing)
1139 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
1140 else
1141 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001142 PySSL_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001143
Bill Janssen6e027db2007-11-15 22:23:56 +00001144#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001145normal_return:
Bill Janssen6e027db2007-11-15 22:23:56 +00001146#endif
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001147 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
1148 (when we are able to write or when there's something to read) */
1149 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001150}
1151
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001152static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
1153{
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001154 Py_buffer buf;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001155 int len;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001156 int sockstate;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001157 int err;
Bill Janssen6e027db2007-11-15 22:23:56 +00001158 int nonblocking;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001159 PySocketSockObject *sock
1160 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
1161
1162 if (((PyObject*)sock) == Py_None) {
1163 _setSSLError("Underlying socket connection gone",
1164 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1165 return NULL;
1166 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001167
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001168 if (!PyArg_ParseTuple(args, "y*:write", &buf))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001169 return NULL;
1170
Bill Janssen6e027db2007-11-15 22:23:56 +00001171 /* just in case the blocking state of the socket has been changed */
Bill Janssen54cc54c2007-12-14 22:08:56 +00001172 nonblocking = (sock->sock_timeout >= 0.0);
Bill Janssen6e027db2007-11-15 22:23:56 +00001173 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1174 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
1175
Bill Janssen54cc54c2007-12-14 22:08:56 +00001176 sockstate = check_socket_and_wait_for_timeout(sock, 1);
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001177 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001178 PyErr_SetString(PySSLErrorObject,
1179 "The write operation timed out");
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001180 goto error;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001181 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001182 PyErr_SetString(PySSLErrorObject,
1183 "Underlying socket has been closed.");
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001184 goto error;
Neal Norwitz389cea82006-02-13 00:35:21 +00001185 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001186 PyErr_SetString(PySSLErrorObject,
1187 "Underlying socket too large for select().");
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001188 goto error;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001189 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001190 do {
1191 err = 0;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001192 PySSL_BEGIN_ALLOW_THREADS
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001193 len = SSL_write(self->ssl, buf.buf, buf.len);
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001194 err = SSL_get_error(self->ssl, len);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001195 PySSL_END_ALLOW_THREADS
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001196 if (PyErr_CheckSignals()) {
1197 goto error;
Martin v. Löwisafec8e32003-06-28 07:40:23 +00001198 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001199 if (err == SSL_ERROR_WANT_READ) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001200 sockstate =
Bill Janssen54cc54c2007-12-14 22:08:56 +00001201 check_socket_and_wait_for_timeout(sock, 0);
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001202 } else if (err == SSL_ERROR_WANT_WRITE) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001203 sockstate =
Bill Janssen54cc54c2007-12-14 22:08:56 +00001204 check_socket_and_wait_for_timeout(sock, 1);
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001205 } else {
1206 sockstate = SOCKET_OPERATION_OK;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001207 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001208 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1209 PyErr_SetString(PySSLErrorObject,
1210 "The write operation timed out");
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001211 goto error;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001212 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001213 PyErr_SetString(PySSLErrorObject,
1214 "Underlying socket has been closed.");
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001215 goto error;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001216 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1217 break;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001218 }
1219 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001220
1221 PyBuffer_Release(&buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001222 if (len > 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001223 return PyLong_FromLong(len);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001224 else
Thomas Woutersed03b412007-08-28 21:37:11 +00001225 return PySSL_SetError(self, len, __FILE__, __LINE__);
Antoine Pitrou7d7aede2009-11-25 18:55:32 +00001226
1227error:
1228 PyBuffer_Release(&buf);
1229 return NULL;
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{
1240 int count = 0;
1241
1242 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
Christian Heimes217cfd12007-12-02 14:31:20 +00001248 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{
Benjamin Peterson56420b42009-02-28 19:06:54 +00001259 PyObject *dest = NULL;
1260 Py_buffer buf;
Bill Janssen6e027db2007-11-15 22:23:56 +00001261 int buf_passed = 0;
1262 int count = -1;
Benjamin Peterson56420b42009-02-28 19:06:54 +00001263 char *mem;
1264 /* XXX this should use Py_ssize_t */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001265 int len = 1024;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001266 int sockstate;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001267 int err;
Bill Janssen6e027db2007-11-15 22:23:56 +00001268 int nonblocking;
Bill Janssen54cc54c2007-12-14 22:08:56 +00001269 PySocketSockObject *sock
1270 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
1271
1272 if (((PyObject*)sock) == Py_None) {
1273 _setSSLError("Underlying socket connection gone",
1274 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1275 return NULL;
1276 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001277
Benjamin Peterson56420b42009-02-28 19:06:54 +00001278 if (!PyArg_ParseTuple(args, "|Oi:read", &dest, &count))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001279 return NULL;
Benjamin Peterson56420b42009-02-28 19:06:54 +00001280 if ((dest == NULL) || (dest == Py_None)) {
1281 if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len)))
Bill Janssen6e027db2007-11-15 22:23:56 +00001282 return NULL;
Benjamin Peterson56420b42009-02-28 19:06:54 +00001283 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)))
Bill Janssen6e027db2007-11-15 22:23:56 +00001287 return NULL;
Benjamin Peterson56420b42009-02-28 19:06:54 +00001288 mem = PyByteArray_AS_STRING(dest);
Bill Janssen6e027db2007-11-15 22:23:56 +00001289 } else {
Benjamin Peterson56420b42009-02-28 19:06:54 +00001290 if (PyObject_GetBuffer(dest, &buf, PyBUF_CONTIG) < 0)
Bill Janssen6e027db2007-11-15 22:23:56 +00001291 return NULL;
Benjamin Peterson56420b42009-02-28 19:06:54 +00001292 mem = buf.buf;
1293 len = buf.len;
Bill Janssen6e027db2007-11-15 22:23:56 +00001294 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 */
Bill Janssen54cc54c2007-12-14 22:08:56 +00001300 nonblocking = (sock->sock_timeout >= 0.0);
Bill Janssen6e027db2007-11-15 22:23:56 +00001301 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1302 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Thomas Woutersed03b412007-08-28 21:37:11 +00001303
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 /* first check if there are bytes ready to be read */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001305 PySSL_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 count = SSL_pending(self->ssl);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001307 PySSL_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001309 if (!count) {
Bill Janssen54cc54c2007-12-14 22:08:56 +00001310 sockstate = check_socket_and_wait_for_timeout(sock, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001311 if (sockstate == SOCKET_HAS_TIMED_OUT) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001312 PyErr_SetString(PySSLErrorObject,
1313 "The read operation timed out");
Benjamin Peterson56420b42009-02-28 19:06:54 +00001314 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001316 PyErr_SetString(PySSLErrorObject,
1317 "Underlying socket too large for select().");
Benjamin Peterson56420b42009-02-28 19:06:54 +00001318 goto error;
Thomas Woutersed03b412007-08-28 21:37:11 +00001319 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
Guido van Rossumf06628b2007-11-21 20:01:53 +00001320 count = 0;
1321 goto done;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 }
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001323 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001324 do {
1325 err = 0;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001326 PySSL_BEGIN_ALLOW_THREADS
Benjamin Peterson56420b42009-02-28 19:06:54 +00001327 count = SSL_read(self->ssl, mem, len);
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001328 err = SSL_get_error(self->ssl, count);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001329 PySSL_END_ALLOW_THREADS
Benjamin Peterson56420b42009-02-28 19:06:54 +00001330 if (PyErr_CheckSignals())
1331 goto error;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001332 if (err == SSL_ERROR_WANT_READ) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001333 sockstate =
Bill Janssen54cc54c2007-12-14 22:08:56 +00001334 check_socket_and_wait_for_timeout(sock, 0);
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001335 } else if (err == SSL_ERROR_WANT_WRITE) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001336 sockstate =
Bill Janssen54cc54c2007-12-14 22:08:56 +00001337 check_socket_and_wait_for_timeout(sock, 1);
Thomas Woutersed03b412007-08-28 21:37:11 +00001338 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
1339 (SSL_get_shutdown(self->ssl) ==
1340 SSL_RECEIVED_SHUTDOWN))
1341 {
Guido van Rossumf06628b2007-11-21 20:01:53 +00001342 count = 0;
1343 goto done;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001344 } else {
1345 sockstate = SOCKET_OPERATION_OK;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001346 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001347 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1348 PyErr_SetString(PySSLErrorObject,
1349 "The read operation timed out");
Benjamin Peterson56420b42009-02-28 19:06:54 +00001350 goto error;
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001351 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1352 break;
Guido van Rossum4f707ac2003-01-31 18:13:18 +00001353 }
1354 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Thomas Woutersed03b412007-08-28 21:37:11 +00001355 if (count <= 0) {
Benjamin Peterson56420b42009-02-28 19:06:54 +00001356 PySSL_SetError(self, count, __FILE__, __LINE__);
1357 goto error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001358 }
Guido van Rossumf06628b2007-11-21 20:01:53 +00001359 done:
Bill Janssen6e027db2007-11-15 22:23:56 +00001360 if (!buf_passed) {
Benjamin Peterson56420b42009-02-28 19:06:54 +00001361 PyObject *res = PyBytes_FromStringAndSize(mem, count);
1362 Py_DECREF(dest);
Guido van Rossumf06628b2007-11-21 20:01:53 +00001363 return res;
Bill Janssen6e027db2007-11-15 22:23:56 +00001364 } else {
Benjamin Peterson56420b42009-02-28 19:06:54 +00001365 PyBuffer_Release(&buf);
Christian Heimes217cfd12007-12-02 14:31:20 +00001366 return PyLong_FromLong(count);
Bill Janssen6e027db2007-11-15 22:23:56 +00001367 }
Benjamin Peterson56420b42009-02-28 19:06:54 +00001368 error:
1369 if (!buf_passed) {
1370 Py_DECREF(dest);
1371 } else {
1372 PyBuffer_Release(&buf);
1373 }
1374 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001375}
1376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001377PyDoc_STRVAR(PySSL_SSLread_doc,
Bill Janssen6e027db2007-11-15 22:23:56 +00001378"read([len]) -> string\n\
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001379\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001380Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001381
Bill Janssen40a0f662008-08-12 16:56:25 +00001382static PyObject *PySSL_SSLshutdown(PySSLObject *self)
1383{
1384 int err;
1385 PySocketSockObject *sock
1386 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
1387
1388 /* Guard against closed socket */
1389 if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
1390 _setSSLError("Underlying socket connection gone",
1391 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
1392 return NULL;
1393 }
1394
1395 PySSL_BEGIN_ALLOW_THREADS
1396 err = SSL_shutdown(self->ssl);
1397 if (err == 0) {
1398 /* we need to call it again to finish the shutdown */
1399 err = SSL_shutdown(self->ssl);
1400 }
1401 PySSL_END_ALLOW_THREADS
1402
1403 if (err < 0)
1404 return PySSL_SetError(self, err, __FILE__, __LINE__);
1405 else {
1406 Py_INCREF(sock);
1407 return (PyObject *) sock;
1408 }
1409}
1410
1411PyDoc_STRVAR(PySSL_SSLshutdown_doc,
1412"shutdown(s) -> socket\n\
1413\n\
1414Does the SSL shutdown handshake with the remote end, and returns\n\
1415the underlying socket object.");
1416
1417
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001418static PyMethodDef PySSLMethods[] = {
Bill Janssen6e027db2007-11-15 22:23:56 +00001419 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001420 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001421 PySSL_SSLwrite_doc},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001422 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001423 PySSL_SSLread_doc},
Bill Janssen6e027db2007-11-15 22:23:56 +00001424 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
1425 PySSL_SSLpending_doc},
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001426 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
1427 PySSL_peercert_doc},
1428 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
Bill Janssen40a0f662008-08-12 16:56:25 +00001429 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
1430 PySSL_SSLshutdown_doc},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001431 {NULL, NULL}
1432};
1433
Jeremy Hylton938ace62002-07-17 16:30:39 +00001434static PyTypeObject PySSL_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001435 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossume6650f92007-12-06 19:05:55 +00001436 "ssl.SSLContext", /*tp_name*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001437 sizeof(PySSLObject), /*tp_basicsize*/
1438 0, /*tp_itemsize*/
1439 /* methods */
1440 (destructor)PySSL_dealloc, /*tp_dealloc*/
1441 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001442 0, /*tp_getattr*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001443 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001444 0, /*tp_reserved*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001445 0, /*tp_repr*/
1446 0, /*tp_as_number*/
1447 0, /*tp_as_sequence*/
1448 0, /*tp_as_mapping*/
1449 0, /*tp_hash*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001450 0, /*tp_call*/
1451 0, /*tp_str*/
1452 0, /*tp_getattro*/
1453 0, /*tp_setattro*/
1454 0, /*tp_as_buffer*/
1455 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1456 0, /*tp_doc*/
1457 0, /*tp_traverse*/
1458 0, /*tp_clear*/
1459 0, /*tp_richcompare*/
1460 0, /*tp_weaklistoffset*/
1461 0, /*tp_iter*/
1462 0, /*tp_iternext*/
1463 PySSLMethods, /*tp_methods*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001464};
1465
1466#ifdef HAVE_OPENSSL_RAND
1467
1468/* helper routines for seeding the SSL PRNG */
1469static PyObject *
1470PySSL_RAND_add(PyObject *self, PyObject *args)
1471{
1472 char *buf;
1473 int len;
1474 double entropy;
1475
1476 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
1477 return NULL;
1478 RAND_add(buf, len, entropy);
1479 Py_INCREF(Py_None);
1480 return Py_None;
1481}
1482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001484"RAND_add(string, entropy)\n\
1485\n\
1486Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001487bound on the entropy contained in string. See RFC 1750.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001488
1489static PyObject *
1490PySSL_RAND_status(PyObject *self)
1491{
Christian Heimes217cfd12007-12-02 14:31:20 +00001492 return PyLong_FromLong(RAND_status());
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001493}
1494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001496"RAND_status() -> 0 or 1\n\
1497\n\
Bill Janssen6e027db2007-11-15 22:23:56 +00001498Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
1499It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
1500using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001501
1502static PyObject *
1503PySSL_RAND_egd(PyObject *self, PyObject *arg)
1504{
1505 int bytes;
1506
Bill Janssen6e027db2007-11-15 22:23:56 +00001507 if (!PyUnicode_Check(arg))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001508 return PyErr_Format(PyExc_TypeError,
1509 "RAND_egd() expected string, found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +00001510 Py_TYPE(arg)->tp_name);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001511 bytes = RAND_egd(_PyUnicode_AsString(arg));
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001512 if (bytes == -1) {
1513 PyErr_SetString(PySSLErrorObject,
1514 "EGD connection failed or EGD did not return "
1515 "enough data to seed the PRNG");
1516 return NULL;
1517 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001518 return PyLong_FromLong(bytes);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001519}
1520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001521PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001522"RAND_egd(path) -> bytes\n\
1523\n\
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001524Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
1525Returns number of bytes read. Raises SSLError if connection to EGD\n\
1526fails or if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001527
1528#endif
1529
Bill Janssen40a0f662008-08-12 16:56:25 +00001530
1531
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001532/* List of functions exported by this module. */
1533
1534static PyMethodDef PySSL_methods[] = {
Thomas Woutersed03b412007-08-28 21:37:11 +00001535 {"sslwrap", PySSL_sslwrap,
1536 METH_VARARGS, ssl_doc},
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001537 {"_test_decode_cert", PySSL_test_decode_certificate,
1538 METH_VARARGS},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001539#ifdef HAVE_OPENSSL_RAND
Thomas Woutersed03b412007-08-28 21:37:11 +00001540 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001541 PySSL_RAND_add_doc},
1542 {"RAND_egd", PySSL_RAND_egd, METH_O,
1543 PySSL_RAND_egd_doc},
1544 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
1545 PySSL_RAND_status_doc},
1546#endif
Thomas Woutersed03b412007-08-28 21:37:11 +00001547 {NULL, NULL} /* Sentinel */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001548};
1549
1550
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001551#ifdef WITH_THREAD
1552
1553/* an implementation of OpenSSL threading operations in terms
1554 of the Python C thread library */
1555
1556static PyThread_type_lock *_ssl_locks = NULL;
1557
1558static unsigned long _ssl_thread_id_function (void) {
1559 return PyThread_get_thread_ident();
1560}
1561
Bill Janssen6e027db2007-11-15 22:23:56 +00001562static void _ssl_thread_locking_function
1563 (int mode, int n, const char *file, int line) {
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001564 /* this function is needed to perform locking on shared data
1565 structures. (Note that OpenSSL uses a number of global data
Bill Janssen6e027db2007-11-15 22:23:56 +00001566 structures that will be implicitly shared whenever multiple
1567 threads use OpenSSL.) Multi-threaded applications will
1568 crash at random if it is not set.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001569
Bill Janssen6e027db2007-11-15 22:23:56 +00001570 locking_function() must be able to handle up to
1571 CRYPTO_num_locks() different mutex locks. It sets the n-th
1572 lock if mode & CRYPTO_LOCK, and releases it otherwise.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001573
1574 file and line are the file number of the function setting the
1575 lock. They can be useful for debugging.
1576 */
1577
1578 if ((_ssl_locks == NULL) ||
Christian Heimesba4af492008-03-28 00:55:15 +00001579 (n < 0) || ((unsigned)n >= _ssl_locks_count))
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001580 return;
1581
1582 if (mode & CRYPTO_LOCK) {
1583 PyThread_acquire_lock(_ssl_locks[n], 1);
1584 } else {
1585 PyThread_release_lock(_ssl_locks[n]);
1586 }
1587}
1588
1589static int _setup_ssl_threads(void) {
1590
Christian Heimesba4af492008-03-28 00:55:15 +00001591 unsigned int i;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001592
1593 if (_ssl_locks == NULL) {
1594 _ssl_locks_count = CRYPTO_num_locks();
1595 _ssl_locks = (PyThread_type_lock *)
1596 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
1597 if (_ssl_locks == NULL)
1598 return 0;
Bill Janssen6e027db2007-11-15 22:23:56 +00001599 memset(_ssl_locks, 0,
1600 sizeof(PyThread_type_lock) * _ssl_locks_count);
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001601 for (i = 0; i < _ssl_locks_count; i++) {
1602 _ssl_locks[i] = PyThread_allocate_lock();
1603 if (_ssl_locks[i] == NULL) {
Raymond Hettinger26dd7602009-01-26 16:53:29 +00001604 unsigned int j;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001605 for (j = 0; j < i; j++) {
1606 PyThread_free_lock(_ssl_locks[j]);
1607 }
1608 free(_ssl_locks);
1609 return 0;
1610 }
1611 }
1612 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
1613 CRYPTO_set_id_callback(_ssl_thread_id_function);
1614 }
1615 return 1;
1616}
1617
1618#endif /* def HAVE_THREAD */
1619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001620PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001621"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001623
Martin v. Löwis1a214512008-06-11 05:26:20 +00001624
1625static struct PyModuleDef _sslmodule = {
1626 PyModuleDef_HEAD_INIT,
1627 "_ssl",
1628 module_doc,
1629 -1,
1630 PySSL_methods,
1631 NULL,
1632 NULL,
1633 NULL,
1634 NULL
1635};
1636
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001637PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001638PyInit__ssl(void)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001639{
1640 PyObject *m, *d;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001641 PySocketModule_APIObject *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001642
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001643 if (PyType_Ready(&PySSL_Type) < 0)
1644 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001645
Martin v. Löwis1a214512008-06-11 05:26:20 +00001646 m = PyModule_Create(&_sslmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001647 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001648 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001649 d = PyModule_GetDict(m);
1650
1651 /* Load _socket module and its C API */
Benjamin Petersonb173f782009-05-05 22:31:58 +00001652 socket_api = PySocketModule_ImportModuleAndAPI();
1653 if (!socket_api)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001654 return NULL;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001655 PySocketModule = *socket_api;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001656
1657 /* Init OpenSSL */
1658 SSL_load_error_strings();
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001659#ifdef WITH_THREAD
1660 /* note that this will start threading if not already started */
1661 if (!_setup_ssl_threads()) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001662 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001663 }
1664#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001665 SSLeay_add_ssl_algorithms();
1666
1667 /* Add symbols to module dict */
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001668 PySSLErrorObject = PyErr_NewException("ssl.SSLError",
Thomas Woutersed03b412007-08-28 21:37:11 +00001669 PySocketModule.error,
1670 NULL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001671 if (PySSLErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001672 return NULL;
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001673 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001674 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001675 if (PyDict_SetItemString(d, "SSLType",
1676 (PyObject *)&PySSL_Type) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001677 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001678 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001679 PY_SSL_ERROR_ZERO_RETURN);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001680 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001681 PY_SSL_ERROR_WANT_READ);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001682 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001683 PY_SSL_ERROR_WANT_WRITE);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001684 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001685 PY_SSL_ERROR_WANT_X509_LOOKUP);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001686 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001687 PY_SSL_ERROR_SYSCALL);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001688 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001689 PY_SSL_ERROR_SSL);
1690 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
1691 PY_SSL_ERROR_WANT_CONNECT);
1692 /* non ssl.h errorcodes */
1693 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
1694 PY_SSL_ERROR_EOF);
1695 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
1696 PY_SSL_ERROR_INVALID_ERROR_CODE);
Thomas Woutersed03b412007-08-28 21:37:11 +00001697 /* cert requirements */
1698 PyModule_AddIntConstant(m, "CERT_NONE",
1699 PY_SSL_CERT_NONE);
1700 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
1701 PY_SSL_CERT_OPTIONAL);
1702 PyModule_AddIntConstant(m, "CERT_REQUIRED",
1703 PY_SSL_CERT_REQUIRED);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001704
Thomas Woutersed03b412007-08-28 21:37:11 +00001705 /* protocol versions */
1706 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
1707 PY_SSL_VERSION_SSL2);
1708 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
1709 PY_SSL_VERSION_SSL3);
1710 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
1711 PY_SSL_VERSION_SSL23);
1712 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
1713 PY_SSL_VERSION_TLS1);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001714 return m;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001715}