blob: 6ed0f4fa0a84031b122ea710443f64e93a279b1f [file] [log] [blame]
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +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.
Bill Janssen98d19da2007-09-10 21:51:02 +00004 Re-worked a bit by Bill Janssen to add server-side support and
Bill Janssen934b16d2008-06-28 22:19:33 +00005 certificate decoding. Chris Stawarz contributed some non-blocking
6 patches.
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00007
Bill Janssen98d19da2007-09-10 21:51:02 +00008 This module is imported by ssl.py. It should *not* be used
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00009 directly.
10
Bill Janssen98d19da2007-09-10 21:51:02 +000011 XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE?
Antoine Pitroua5c4b552010-04-22 23:33:02 +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"
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000018
Bill Janssen98d19da2007-09-10 21:51:02 +000019#ifdef WITH_THREAD
20#include "pythread.h"
21#define PySSL_BEGIN_ALLOW_THREADS { \
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +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 }
Bill Janssen98d19da2007-09-10 21:51:02 +000028
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +000029#else /* no WITH_THREAD */
Bill Janssen98d19da2007-09-10 21:51:02 +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 Pitroua4c2a5c2010-05-05 15:53:45 +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_INVALID_ERROR_CODE
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +000051};
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000052
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000053enum py_ssl_server_or_client {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +000054 PY_SSL_CLIENT,
55 PY_SSL_SERVER
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000056};
57
58enum py_ssl_cert_requirements {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +000059 PY_SSL_CERT_NONE,
60 PY_SSL_CERT_OPTIONAL,
61 PY_SSL_CERT_REQUIRED
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000062};
63
64enum py_ssl_version {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +000065 PY_SSL_VERSION_SSL2,
66 PY_SSL_VERSION_SSL3,
67 PY_SSL_VERSION_SSL23,
68 PY_SSL_VERSION_TLS1
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000069};
70
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000071/* Include symbols from _socket module */
72#include "socketmodule.h"
73
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000074#if defined(HAVE_POLL_H)
Anthony Baxter93ab5fa2006-07-11 02:04:09 +000075#include <poll.h>
76#elif defined(HAVE_SYS_POLL_H)
77#include <sys/poll.h>
78#endif
79
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000080/* Include OpenSSL header files */
81#include "openssl/rsa.h"
82#include "openssl/crypto.h"
83#include "openssl/x509.h"
Bill Janssen98d19da2007-09-10 21:51:02 +000084#include "openssl/x509v3.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000085#include "openssl/pem.h"
86#include "openssl/ssl.h"
87#include "openssl/err.h"
88#include "openssl/rand.h"
89
90/* SSL error object */
91static PyObject *PySSLErrorObject;
92
Bill Janssen98d19da2007-09-10 21:51:02 +000093#ifdef WITH_THREAD
94
95/* serves as a flag to see whether we've initialized the SSL thread support. */
96/* 0 means no, greater than 0 means yes */
97
98static unsigned int _ssl_locks_count = 0;
99
100#endif /* def WITH_THREAD */
101
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000102/* SSL socket object */
103
104#define X509_NAME_MAXLEN 256
105
106/* RAND_* APIs got added to OpenSSL in 0.9.5 */
107#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
108# define HAVE_OPENSSL_RAND 1
109#else
110# undef HAVE_OPENSSL_RAND
111#endif
112
113typedef struct {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000114 PyObject_HEAD
115 PySocketSockObject *Socket; /* Socket on which we're layered */
116 SSL_CTX* ctx;
117 SSL* ssl;
118 X509* peer_cert;
119 char server[X509_NAME_MAXLEN];
120 char issuer[X509_NAME_MAXLEN];
121 int shutdown_seen_zero;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000122
123} PySSLObject;
124
Jeremy Hylton938ace62002-07-17 16:30:39 +0000125static PyTypeObject PySSL_Type;
126static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
127static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000128static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000129 int writing);
Bill Janssen98d19da2007-09-10 21:51:02 +0000130static PyObject *PySSL_peercert(PySSLObject *self, PyObject *args);
131static PyObject *PySSL_cipher(PySSLObject *self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000132
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000133#define PySSLObject_Check(v) (Py_TYPE(v) == &PySSL_Type)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000134
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000135typedef enum {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000136 SOCKET_IS_NONBLOCKING,
137 SOCKET_IS_BLOCKING,
138 SOCKET_HAS_TIMED_OUT,
139 SOCKET_HAS_BEEN_CLOSED,
140 SOCKET_TOO_LARGE_FOR_SELECT,
141 SOCKET_OPERATION_OK
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +0000142} timeout_state;
143
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000144/* Wrap error strings with filename and line # */
145#define STRINGIFY1(x) #x
146#define STRINGIFY2(x) STRINGIFY1(x)
147#define ERRSTR1(x,y,z) (x ":" y ": " z)
148#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
149
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000150/* XXX It might be helpful to augment the error message generated
151 below with the name of the SSL function that generated the error.
152 I expect it's obvious most of the time.
153*/
154
155static PyObject *
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000156PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000157{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000158 PyObject *v;
159 char buf[2048];
160 char *errstr;
161 int err;
162 enum py_ssl_error p = PY_SSL_ERROR_NONE;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000164 assert(ret <= 0);
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000165
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000166 if (obj->ssl != NULL) {
167 err = SSL_get_error(obj->ssl, ret);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +0000168
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000169 switch (err) {
170 case SSL_ERROR_ZERO_RETURN:
171 errstr = "TLS/SSL connection has been closed";
172 p = PY_SSL_ERROR_ZERO_RETURN;
173 break;
174 case SSL_ERROR_WANT_READ:
175 errstr = "The operation did not complete (read)";
176 p = PY_SSL_ERROR_WANT_READ;
177 break;
178 case SSL_ERROR_WANT_WRITE:
179 p = PY_SSL_ERROR_WANT_WRITE;
180 errstr = "The operation did not complete (write)";
181 break;
182 case SSL_ERROR_WANT_X509_LOOKUP:
183 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000184 errstr = "The operation did not complete (X509 lookup)";
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000185 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) {
194 if (ret == 0 || !obj->Socket) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000195 p = PY_SSL_ERROR_EOF;
196 errstr = "EOF occurred in violation of protocol";
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000197 } else if (ret == -1) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000198 /* underlying BIO reported an I/O error */
Antoine Pitrou508a2372010-05-16 23:11:46 +0000199 ERR_clear_error();
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000200 return obj->Socket->errorhandler();
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000201 } else { /* possible? */
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000202 p = PY_SSL_ERROR_SYSCALL;
203 errstr = "Some I/O error occurred";
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000204 }
205 } else {
206 p = PY_SSL_ERROR_SYSCALL;
207 /* XXX Protected by global interpreter lock */
208 errstr = ERR_error_string(e, NULL);
209 }
210 break;
211 }
212 case SSL_ERROR_SSL:
213 {
214 unsigned long e = ERR_get_error();
215 p = PY_SSL_ERROR_SSL;
216 if (e != 0)
217 /* XXX Protected by global interpreter lock */
218 errstr = ERR_error_string(e, NULL);
219 else { /* possible? */
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000220 errstr = "A failure in the SSL library occurred";
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000221 }
222 break;
223 }
224 default:
225 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
226 errstr = "Invalid error code";
227 }
228 } else {
229 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
230 }
231 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
Antoine Pitrou508a2372010-05-16 23:11:46 +0000232 ERR_clear_error();
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000233 v = Py_BuildValue("(is)", p, buf);
234 if (v != NULL) {
235 PyErr_SetObject(PySSLErrorObject, v);
236 Py_DECREF(v);
237 }
238 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000239}
240
Bill Janssen98d19da2007-09-10 21:51:02 +0000241static PyObject *
242_setSSLError (char *errstr, int errcode, char *filename, int lineno) {
243
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000244 char buf[2048];
245 PyObject *v;
Bill Janssen98d19da2007-09-10 21:51:02 +0000246
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000247 if (errstr == NULL) {
248 errcode = ERR_peek_last_error();
249 errstr = ERR_error_string(errcode, NULL);
250 }
251 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
Antoine Pitrou508a2372010-05-16 23:11:46 +0000252 ERR_clear_error();
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000253 v = Py_BuildValue("(is)", errcode, buf);
254 if (v != NULL) {
255 PyErr_SetObject(PySSLErrorObject, v);
256 Py_DECREF(v);
257 }
258 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +0000259}
260
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000261static PySSLObject *
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000262newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file,
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +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, char *ciphers)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000267{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000268 PySSLObject *self;
269 char *errstr = NULL;
270 int ret;
271 int verification_mode;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000273 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
274 if (self == NULL)
275 return NULL;
276 memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN);
277 memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
278 self->peer_cert = NULL;
279 self->ssl = NULL;
280 self->ctx = NULL;
281 self->Socket = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000283 /* Make sure the SSL error state is initialized */
284 (void) ERR_get_state();
285 ERR_clear_error();
Bill Janssen98d19da2007-09-10 21:51:02 +0000286
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000287 if ((key_file && !cert_file) || (!key_file && cert_file)) {
288 errstr = ERRSTR("Both the key & certificate files "
289 "must be specified");
290 goto fail;
291 }
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000292
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000293 if ((socket_type == PY_SSL_SERVER) &&
294 ((key_file == NULL) || (cert_file == NULL))) {
295 errstr = ERRSTR("Both the key & certificate files "
296 "must be specified for server-side operation");
297 goto fail;
298 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000299
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000300 PySSL_BEGIN_ALLOW_THREADS
301 if (proto_version == PY_SSL_VERSION_TLS1)
302 self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
303 else if (proto_version == PY_SSL_VERSION_SSL3)
304 self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
305 else if (proto_version == PY_SSL_VERSION_SSL2)
306 self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */
307 else if (proto_version == PY_SSL_VERSION_SSL23)
308 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
309 PySSL_END_ALLOW_THREADS
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000310
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000311 if (self->ctx == NULL) {
312 errstr = ERRSTR("Invalid SSL protocol variant specified.");
313 goto fail;
314 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000315
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000316 if (ciphers != NULL) {
317 ret = SSL_CTX_set_cipher_list(self->ctx, ciphers);
318 if (ret == 0) {
319 errstr = ERRSTR("No cipher can be selected.");
320 goto fail;
321 }
322 }
Antoine Pitrou0a6373c2010-04-17 17:10:38 +0000323
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000324 if (certreq != PY_SSL_CERT_NONE) {
325 if (cacerts_file == NULL) {
326 errstr = ERRSTR("No root certificates specified for "
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000327 "verification of other-side certificates.");
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000328 goto fail;
329 } else {
330 PySSL_BEGIN_ALLOW_THREADS
331 ret = SSL_CTX_load_verify_locations(self->ctx,
332 cacerts_file,
333 NULL);
334 PySSL_END_ALLOW_THREADS
335 if (ret != 1) {
336 _setSSLError(NULL, 0, __FILE__, __LINE__);
337 goto fail;
338 }
339 }
340 }
341 if (key_file) {
342 PySSL_BEGIN_ALLOW_THREADS
343 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
344 SSL_FILETYPE_PEM);
345 PySSL_END_ALLOW_THREADS
346 if (ret != 1) {
347 _setSSLError(NULL, ret, __FILE__, __LINE__);
348 goto fail;
349 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000350
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000351 PySSL_BEGIN_ALLOW_THREADS
352 ret = SSL_CTX_use_certificate_chain_file(self->ctx,
353 cert_file);
354 PySSL_END_ALLOW_THREADS
355 if (ret != 1) {
356 /*
357 fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n",
358 ret, ERR_peek_error(), ERR_peek_last_error(), cert_file);
359 */
360 if (ERR_peek_last_error() != 0) {
361 _setSSLError(NULL, ret, __FILE__, __LINE__);
362 goto fail;
363 }
364 }
365 }
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000366
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000367 /* ssl compatibility */
368 SSL_CTX_set_options(self->ctx, SSL_OP_ALL);
Bill Janssen98d19da2007-09-10 21:51:02 +0000369
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000370 verification_mode = SSL_VERIFY_NONE;
371 if (certreq == PY_SSL_CERT_OPTIONAL)
372 verification_mode = SSL_VERIFY_PEER;
373 else if (certreq == PY_SSL_CERT_REQUIRED)
374 verification_mode = (SSL_VERIFY_PEER |
375 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
376 SSL_CTX_set_verify(self->ctx, verification_mode,
377 NULL); /* set verify lvl */
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000378
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000379 PySSL_BEGIN_ALLOW_THREADS
380 self->ssl = SSL_new(self->ctx); /* New ssl struct */
381 PySSL_END_ALLOW_THREADS
382 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
Antoine Pitrou92719c52010-04-09 20:38:39 +0000383#ifdef SSL_MODE_AUTO_RETRY
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000384 SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY);
Antoine Pitrou92719c52010-04-09 20:38:39 +0000385#endif
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000386
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000387 /* If the socket is in non-blocking mode or timeout mode, set the BIO
388 * to non-blocking mode (blocking is the default)
389 */
390 if (Sock->sock_timeout >= 0.0) {
391 /* Set both the read and write BIO's to non-blocking mode */
392 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
393 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
394 }
Guido van Rossum4f707ac2003-01-31 18:13:18 +0000395
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000396 PySSL_BEGIN_ALLOW_THREADS
397 if (socket_type == PY_SSL_CLIENT)
398 SSL_set_connect_state(self->ssl);
399 else
400 SSL_set_accept_state(self->ssl);
401 PySSL_END_ALLOW_THREADS
Martin v. Löwis09c35f72002-07-28 09:57:45 +0000402
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000403 self->Socket = Sock;
404 Py_INCREF(self->Socket);
405 return self;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000406 fail:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000407 if (errstr)
408 PyErr_SetString(PySSLErrorObject, errstr);
409 Py_DECREF(self);
410 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000411}
412
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000413static PyObject *
Guido van Rossum780b80d2007-08-27 18:42:23 +0000414PySSL_sslwrap(PyObject *self, PyObject *args)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000415{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000416 PySocketSockObject *Sock;
417 int server_side = 0;
418 int verification_mode = PY_SSL_CERT_NONE;
419 int protocol = PY_SSL_VERSION_SSL23;
420 char *key_file = NULL;
421 char *cert_file = NULL;
422 char *cacerts_file = NULL;
423 char *ciphers = NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000424
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000425 if (!PyArg_ParseTuple(args, "O!i|zziizz:sslwrap",
426 PySocketModule.Sock_Type,
427 &Sock,
428 &server_side,
429 &key_file, &cert_file,
430 &verification_mode, &protocol,
431 &cacerts_file, &ciphers))
432 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000433
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000434 /*
435 fprintf(stderr,
436 "server_side is %d, keyfile %p, certfile %p, verify_mode %d, "
437 "protocol %d, certs %p\n",
438 server_side, key_file, cert_file, verification_mode,
439 protocol, cacerts_file);
440 */
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000441
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000442 return (PyObject *) newPySSLObject(Sock, key_file, cert_file,
443 server_side, verification_mode,
444 protocol, cacerts_file,
445 ciphers);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000446}
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(ssl_doc,
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000449"sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n"
Antoine Pitrou0a6373c2010-04-17 17:10:38 +0000450" cacertsfile, ciphers]) -> sslobject");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000451
452/* SSL object methods */
453
Bill Janssen934b16d2008-06-28 22:19:33 +0000454static PyObject *PySSL_SSLdo_handshake(PySSLObject *self)
455{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000456 int ret;
457 int err;
458 int sockstate, nonblocking;
Antoine Pitrou4d3e3722010-04-24 19:57:01 +0000459
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000460 /* just in case the blocking state of the socket has been changed */
461 nonblocking = (self->Socket->sock_timeout >= 0.0);
462 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
463 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Bill Janssen934b16d2008-06-28 22:19:33 +0000464
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000465 /* Actually negotiate SSL connection */
466 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
467 do {
468 PySSL_BEGIN_ALLOW_THREADS
469 ret = SSL_do_handshake(self->ssl);
470 err = SSL_get_error(self->ssl, ret);
471 PySSL_END_ALLOW_THREADS
472 if(PyErr_CheckSignals()) {
473 return NULL;
474 }
475 if (err == SSL_ERROR_WANT_READ) {
476 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
477 } else if (err == SSL_ERROR_WANT_WRITE) {
478 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
479 } else {
480 sockstate = SOCKET_OPERATION_OK;
481 }
482 if (sockstate == SOCKET_HAS_TIMED_OUT) {
483 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000484 ERRSTR("The handshake operation timed out"));
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000485 return NULL;
486 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
487 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000488 ERRSTR("Underlying socket has been closed."));
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000489 return NULL;
490 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
491 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000492 ERRSTR("Underlying socket too large for select()."));
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000493 return NULL;
494 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
495 break;
496 }
497 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
498 if (ret < 1)
499 return PySSL_SetError(self, ret, __FILE__, __LINE__);
Bill Janssen934b16d2008-06-28 22:19:33 +0000500
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000501 if (self->peer_cert)
502 X509_free (self->peer_cert);
503 PySSL_BEGIN_ALLOW_THREADS
504 if ((self->peer_cert = SSL_get_peer_certificate(self->ssl))) {
505 X509_NAME_oneline(X509_get_subject_name(self->peer_cert),
506 self->server, X509_NAME_MAXLEN);
507 X509_NAME_oneline(X509_get_issuer_name(self->peer_cert),
508 self->issuer, X509_NAME_MAXLEN);
509 }
510 PySSL_END_ALLOW_THREADS
Bill Janssen934b16d2008-06-28 22:19:33 +0000511
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000512 Py_INCREF(Py_None);
513 return Py_None;
Bill Janssen934b16d2008-06-28 22:19:33 +0000514}
515
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000516static PyObject *
517PySSL_server(PySSLObject *self)
518{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000519 return PyString_FromString(self->server);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000520}
521
522static PyObject *
523PySSL_issuer(PySSLObject *self)
524{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000525 return PyString_FromString(self->issuer);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000526}
527
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000528static PyObject *
Bill Janssen98d19da2007-09-10 21:51:02 +0000529_create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000530
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000531 char namebuf[X509_NAME_MAXLEN];
532 int buflen;
533 PyObject *name_obj;
534 PyObject *value_obj;
535 PyObject *attr;
536 unsigned char *valuebuf = NULL;
Guido van Rossum780b80d2007-08-27 18:42:23 +0000537
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000538 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
539 if (buflen < 0) {
540 _setSSLError(NULL, 0, __FILE__, __LINE__);
541 goto fail;
542 }
543 name_obj = PyString_FromStringAndSize(namebuf, buflen);
544 if (name_obj == NULL)
545 goto fail;
546
547 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
548 if (buflen < 0) {
549 _setSSLError(NULL, 0, __FILE__, __LINE__);
550 Py_DECREF(name_obj);
551 goto fail;
552 }
553 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +0000554 buflen, "strict");
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000555 OPENSSL_free(valuebuf);
556 if (value_obj == NULL) {
557 Py_DECREF(name_obj);
558 goto fail;
559 }
560 attr = PyTuple_New(2);
561 if (attr == NULL) {
562 Py_DECREF(name_obj);
563 Py_DECREF(value_obj);
564 goto fail;
565 }
566 PyTuple_SET_ITEM(attr, 0, name_obj);
567 PyTuple_SET_ITEM(attr, 1, value_obj);
568 return attr;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000569
Bill Janssen98d19da2007-09-10 21:51:02 +0000570 fail:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000571 return NULL;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000572}
573
574static PyObject *
Bill Janssen98d19da2007-09-10 21:51:02 +0000575_create_tuple_for_X509_NAME (X509_NAME *xname)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000576{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000577 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
578 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
579 PyObject *rdnt;
580 PyObject *attr = NULL; /* tuple to hold an attribute */
581 int entry_count = X509_NAME_entry_count(xname);
582 X509_NAME_ENTRY *entry;
583 ASN1_OBJECT *name;
584 ASN1_STRING *value;
585 int index_counter;
586 int rdn_level = -1;
587 int retcode;
Bill Janssen98d19da2007-09-10 21:51:02 +0000588
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000589 dn = PyList_New(0);
590 if (dn == NULL)
591 return NULL;
592 /* now create another tuple to hold the top-level RDN */
593 rdn = PyList_New(0);
594 if (rdn == NULL)
595 goto fail0;
Bill Janssen98d19da2007-09-10 21:51:02 +0000596
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000597 for (index_counter = 0;
598 index_counter < entry_count;
599 index_counter++)
600 {
601 entry = X509_NAME_get_entry(xname, index_counter);
Bill Janssen98d19da2007-09-10 21:51:02 +0000602
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000603 /* check to see if we've gotten to a new RDN */
604 if (rdn_level >= 0) {
605 if (rdn_level != entry->set) {
606 /* yes, new RDN */
607 /* add old RDN to DN */
608 rdnt = PyList_AsTuple(rdn);
609 Py_DECREF(rdn);
610 if (rdnt == NULL)
611 goto fail0;
612 retcode = PyList_Append(dn, rdnt);
613 Py_DECREF(rdnt);
614 if (retcode < 0)
615 goto fail0;
616 /* create new RDN */
617 rdn = PyList_New(0);
618 if (rdn == NULL)
619 goto fail0;
620 }
621 }
622 rdn_level = entry->set;
Bill Janssen98d19da2007-09-10 21:51:02 +0000623
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000624 /* now add this attribute to the current RDN */
625 name = X509_NAME_ENTRY_get_object(entry);
626 value = X509_NAME_ENTRY_get_data(entry);
627 attr = _create_tuple_for_attribute(name, value);
628 /*
629 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
630 entry->set,
631 PyString_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
632 PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
633 */
634 if (attr == NULL)
635 goto fail1;
636 retcode = PyList_Append(rdn, attr);
637 Py_DECREF(attr);
638 if (retcode < 0)
639 goto fail1;
640 }
641 /* now, there's typically a dangling RDN */
642 if ((rdn != NULL) && (PyList_Size(rdn) > 0)) {
643 rdnt = PyList_AsTuple(rdn);
644 Py_DECREF(rdn);
645 if (rdnt == NULL)
646 goto fail0;
647 retcode = PyList_Append(dn, rdnt);
648 Py_DECREF(rdnt);
649 if (retcode < 0)
650 goto fail0;
651 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000652
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000653 /* convert list to tuple */
654 rdnt = PyList_AsTuple(dn);
655 Py_DECREF(dn);
656 if (rdnt == NULL)
657 return NULL;
658 return rdnt;
Bill Janssen98d19da2007-09-10 21:51:02 +0000659
660 fail1:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000661 Py_XDECREF(rdn);
Bill Janssen98d19da2007-09-10 21:51:02 +0000662
663 fail0:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000664 Py_XDECREF(dn);
665 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +0000666}
667
668static PyObject *
669_get_peer_alt_names (X509 *certificate) {
Bill Janssen98d19da2007-09-10 21:51:02 +0000670
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000671 /* this code follows the procedure outlined in
672 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
673 function to extract the STACK_OF(GENERAL_NAME),
674 then iterates through the stack to add the
675 names. */
676
677 int i, j;
678 PyObject *peer_alt_names = Py_None;
679 PyObject *v, *t;
680 X509_EXTENSION *ext = NULL;
681 GENERAL_NAMES *names = NULL;
682 GENERAL_NAME *name;
Benjamin Peterson8e734032010-10-13 22:10:31 +0000683 const X509V3_EXT_METHOD *method;
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000684 BIO *biobuf = NULL;
685 char buf[2048];
686 char *vptr;
687 int len;
688 /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
Victor Stinner3f75cc52010-03-02 22:44:42 +0000689#if OPENSSL_VERSION_NUMBER >= 0x009060dfL
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000690 const unsigned char *p;
Victor Stinner3f75cc52010-03-02 22:44:42 +0000691#else
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000692 unsigned char *p;
Victor Stinner3f75cc52010-03-02 22:44:42 +0000693#endif
Bill Janssen98d19da2007-09-10 21:51:02 +0000694
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000695 if (certificate == NULL)
696 return peer_alt_names;
Bill Janssen98d19da2007-09-10 21:51:02 +0000697
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000698 /* get a memory buffer */
699 biobuf = BIO_new(BIO_s_mem());
Bill Janssen98d19da2007-09-10 21:51:02 +0000700
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000701 i = 0;
702 while ((i = X509_get_ext_by_NID(
703 certificate, NID_subject_alt_name, i)) >= 0) {
Bill Janssen98d19da2007-09-10 21:51:02 +0000704
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000705 if (peer_alt_names == Py_None) {
706 peer_alt_names = PyList_New(0);
707 if (peer_alt_names == NULL)
708 goto fail;
709 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000710
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000711 /* now decode the altName */
712 ext = X509_get_ext(certificate, i);
713 if(!(method = X509V3_EXT_get(ext))) {
714 PyErr_SetString(PySSLErrorObject,
715 ERRSTR("No method for internalizing subjectAltName!"));
716 goto fail;
717 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000718
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000719 p = ext->value->data;
720 if (method->it)
721 names = (GENERAL_NAMES*) (ASN1_item_d2i(NULL,
722 &p,
723 ext->value->length,
724 ASN1_ITEM_ptr(method->it)));
725 else
726 names = (GENERAL_NAMES*) (method->d2i(NULL,
727 &p,
728 ext->value->length));
Bill Janssen98d19da2007-09-10 21:51:02 +0000729
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000730 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
Bill Janssen98d19da2007-09-10 21:51:02 +0000731
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000732 /* get a rendering of each name in the set of names */
Bill Janssen98d19da2007-09-10 21:51:02 +0000733
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000734 name = sk_GENERAL_NAME_value(names, j);
735 if (name->type == GEN_DIRNAME) {
Bill Janssen98d19da2007-09-10 21:51:02 +0000736
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000737 /* we special-case DirName as a tuple of tuples of attributes */
Bill Janssen98d19da2007-09-10 21:51:02 +0000738
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000739 t = PyTuple_New(2);
740 if (t == NULL) {
741 goto fail;
742 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000743
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000744 v = PyString_FromString("DirName");
745 if (v == NULL) {
746 Py_DECREF(t);
747 goto fail;
748 }
749 PyTuple_SET_ITEM(t, 0, v);
Bill Janssen98d19da2007-09-10 21:51:02 +0000750
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000751 v = _create_tuple_for_X509_NAME (name->d.dirn);
752 if (v == NULL) {
753 Py_DECREF(t);
754 goto fail;
755 }
756 PyTuple_SET_ITEM(t, 1, v);
Bill Janssen98d19da2007-09-10 21:51:02 +0000757
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000758 } else {
Bill Janssen98d19da2007-09-10 21:51:02 +0000759
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000760 /* for everything else, we use the OpenSSL print form */
Bill Janssen98d19da2007-09-10 21:51:02 +0000761
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000762 (void) BIO_reset(biobuf);
763 GENERAL_NAME_print(biobuf, name);
764 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
765 if (len < 0) {
766 _setSSLError(NULL, 0, __FILE__, __LINE__);
767 goto fail;
768 }
769 vptr = strchr(buf, ':');
770 if (vptr == NULL)
771 goto fail;
772 t = PyTuple_New(2);
773 if (t == NULL)
774 goto fail;
775 v = PyString_FromStringAndSize(buf, (vptr - buf));
776 if (v == NULL) {
777 Py_DECREF(t);
778 goto fail;
779 }
780 PyTuple_SET_ITEM(t, 0, v);
781 v = PyString_FromStringAndSize((vptr + 1), (len - (vptr - buf + 1)));
782 if (v == NULL) {
783 Py_DECREF(t);
784 goto fail;
785 }
786 PyTuple_SET_ITEM(t, 1, v);
787 }
788
789 /* and add that rendering to the list */
790
791 if (PyList_Append(peer_alt_names, t) < 0) {
792 Py_DECREF(t);
793 goto fail;
794 }
795 Py_DECREF(t);
796 }
797 }
798 BIO_free(biobuf);
799 if (peer_alt_names != Py_None) {
800 v = PyList_AsTuple(peer_alt_names);
801 Py_DECREF(peer_alt_names);
802 return v;
803 } else {
804 return peer_alt_names;
805 }
806
Bill Janssen98d19da2007-09-10 21:51:02 +0000807
808 fail:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000809 if (biobuf != NULL)
810 BIO_free(biobuf);
Bill Janssen98d19da2007-09-10 21:51:02 +0000811
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000812 if (peer_alt_names != Py_None) {
813 Py_XDECREF(peer_alt_names);
814 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000815
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000816 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +0000817}
818
819static PyObject *
820_decode_certificate (X509 *certificate, int verbose) {
821
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000822 PyObject *retval = NULL;
823 BIO *biobuf = NULL;
824 PyObject *peer;
825 PyObject *peer_alt_names = NULL;
826 PyObject *issuer;
827 PyObject *version;
828 PyObject *sn_obj;
829 ASN1_INTEGER *serialNumber;
830 char buf[2048];
831 int len;
832 ASN1_TIME *notBefore, *notAfter;
833 PyObject *pnotBefore, *pnotAfter;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000834
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000835 retval = PyDict_New();
836 if (retval == NULL)
837 return NULL;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000838
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000839 peer = _create_tuple_for_X509_NAME(
840 X509_get_subject_name(certificate));
841 if (peer == NULL)
842 goto fail0;
843 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
844 Py_DECREF(peer);
845 goto fail0;
846 }
847 Py_DECREF(peer);
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000848
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000849 if (verbose) {
850 issuer = _create_tuple_for_X509_NAME(
851 X509_get_issuer_name(certificate));
852 if (issuer == NULL)
853 goto fail0;
854 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
855 Py_DECREF(issuer);
856 goto fail0;
857 }
858 Py_DECREF(issuer);
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000859
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000860 version = PyInt_FromLong(X509_get_version(certificate) + 1);
861 if (PyDict_SetItemString(retval, "version", version) < 0) {
862 Py_DECREF(version);
863 goto fail0;
864 }
865 Py_DECREF(version);
866 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000867
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000868 /* get a memory buffer */
869 biobuf = BIO_new(BIO_s_mem());
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000870
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000871 if (verbose) {
Bill Janssen98d19da2007-09-10 21:51:02 +0000872
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000873 (void) BIO_reset(biobuf);
874 serialNumber = X509_get_serialNumber(certificate);
875 /* should not exceed 20 octets, 160 bits, so buf is big enough */
876 i2a_ASN1_INTEGER(biobuf, serialNumber);
877 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
878 if (len < 0) {
879 _setSSLError(NULL, 0, __FILE__, __LINE__);
880 goto fail1;
881 }
882 sn_obj = PyString_FromStringAndSize(buf, len);
883 if (sn_obj == NULL)
884 goto fail1;
885 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
886 Py_DECREF(sn_obj);
887 goto fail1;
888 }
889 Py_DECREF(sn_obj);
Bill Janssen98d19da2007-09-10 21:51:02 +0000890
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000891 (void) BIO_reset(biobuf);
892 notBefore = X509_get_notBefore(certificate);
893 ASN1_TIME_print(biobuf, notBefore);
894 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
895 if (len < 0) {
896 _setSSLError(NULL, 0, __FILE__, __LINE__);
897 goto fail1;
898 }
899 pnotBefore = PyString_FromStringAndSize(buf, len);
900 if (pnotBefore == NULL)
901 goto fail1;
902 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
903 Py_DECREF(pnotBefore);
904 goto fail1;
905 }
906 Py_DECREF(pnotBefore);
907 }
908
909 (void) BIO_reset(biobuf);
910 notAfter = X509_get_notAfter(certificate);
911 ASN1_TIME_print(biobuf, notAfter);
912 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
913 if (len < 0) {
914 _setSSLError(NULL, 0, __FILE__, __LINE__);
915 goto fail1;
916 }
917 pnotAfter = PyString_FromStringAndSize(buf, len);
918 if (pnotAfter == NULL)
919 goto fail1;
920 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
921 Py_DECREF(pnotAfter);
922 goto fail1;
923 }
924 Py_DECREF(pnotAfter);
925
926 /* Now look for subjectAltName */
927
928 peer_alt_names = _get_peer_alt_names(certificate);
929 if (peer_alt_names == NULL)
930 goto fail1;
931 else if (peer_alt_names != Py_None) {
932 if (PyDict_SetItemString(retval, "subjectAltName",
933 peer_alt_names) < 0) {
934 Py_DECREF(peer_alt_names);
935 goto fail1;
936 }
937 Py_DECREF(peer_alt_names);
938 }
939
940 BIO_free(biobuf);
941 return retval;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000942
943 fail1:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000944 if (biobuf != NULL)
945 BIO_free(biobuf);
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000946 fail0:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000947 Py_XDECREF(retval);
948 return NULL;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000949}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000950
Bill Janssen98d19da2007-09-10 21:51:02 +0000951
952static PyObject *
953PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
954
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000955 PyObject *retval = NULL;
956 char *filename = NULL;
957 X509 *x=NULL;
958 BIO *cert;
959 int verbose = 1;
Bill Janssen98d19da2007-09-10 21:51:02 +0000960
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000961 if (!PyArg_ParseTuple(args, "s|i:test_decode_certificate", &filename, &verbose))
962 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +0000963
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000964 if ((cert=BIO_new(BIO_s_file())) == NULL) {
965 PyErr_SetString(PySSLErrorObject, "Can't malloc memory to read file");
966 goto fail0;
967 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000968
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000969 if (BIO_read_filename(cert,filename) <= 0) {
970 PyErr_SetString(PySSLErrorObject, "Can't open file");
971 goto fail0;
972 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000973
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000974 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
975 if (x == NULL) {
976 PyErr_SetString(PySSLErrorObject, "Error decoding PEM-encoded file");
977 goto fail0;
978 }
Bill Janssen98d19da2007-09-10 21:51:02 +0000979
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000980 retval = _decode_certificate(x, verbose);
Mark Dickinson793c71c2010-08-03 18:34:53 +0000981 X509_free(x);
Bill Janssen98d19da2007-09-10 21:51:02 +0000982
983 fail0:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000984
985 if (cert != NULL) BIO_free(cert);
986 return retval;
Bill Janssen98d19da2007-09-10 21:51:02 +0000987}
988
989
990static PyObject *
991PySSL_peercert(PySSLObject *self, PyObject *args)
992{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000993 PyObject *retval = NULL;
994 int len;
995 int verification;
996 PyObject *binary_mode = Py_None;
Bill Janssen98d19da2007-09-10 21:51:02 +0000997
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +0000998 if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
999 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +00001000
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001001 if (!self->peer_cert)
1002 Py_RETURN_NONE;
Bill Janssen98d19da2007-09-10 21:51:02 +00001003
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001004 if (PyObject_IsTrue(binary_mode)) {
1005 /* return cert in DER-encoded format */
Bill Janssen98d19da2007-09-10 21:51:02 +00001006
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001007 unsigned char *bytes_buf = NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +00001008
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001009 bytes_buf = NULL;
1010 len = i2d_X509(self->peer_cert, &bytes_buf);
1011 if (len < 0) {
1012 PySSL_SetError(self, len, __FILE__, __LINE__);
1013 return NULL;
1014 }
1015 retval = PyString_FromStringAndSize((const char *) bytes_buf, len);
1016 OPENSSL_free(bytes_buf);
1017 return retval;
Bill Janssen98d19da2007-09-10 21:51:02 +00001018
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001019 } else {
Bill Janssen98d19da2007-09-10 21:51:02 +00001020
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001021 verification = SSL_CTX_get_verify_mode(self->ctx);
1022 if ((verification & SSL_VERIFY_PEER) == 0)
1023 return PyDict_New();
1024 else
1025 return _decode_certificate (self->peer_cert, 0);
1026 }
Bill Janssen98d19da2007-09-10 21:51:02 +00001027}
1028
1029PyDoc_STRVAR(PySSL_peercert_doc,
1030"peer_certificate([der=False]) -> certificate\n\
1031\n\
1032Returns the certificate for the peer. If no certificate was provided,\n\
1033returns None. If a certificate was provided, but not validated, returns\n\
1034an empty dictionary. Otherwise returns a dict containing information\n\
1035about the peer certificate.\n\
1036\n\
1037If the optional argument is True, returns a DER-encoded copy of the\n\
1038peer certificate, or None if no certificate was provided. This will\n\
1039return the certificate even if it wasn't validated.");
1040
1041static PyObject *PySSL_cipher (PySSLObject *self) {
1042
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001043 PyObject *retval, *v;
Benjamin Peterson8e734032010-10-13 22:10:31 +00001044 const SSL_CIPHER *current;
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001045 char *cipher_name;
1046 char *cipher_protocol;
Bill Janssen98d19da2007-09-10 21:51:02 +00001047
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001048 if (self->ssl == NULL)
Hirokazu Yamamotoa9b16892010-12-09 12:12:42 +00001049 Py_RETURN_NONE;
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001050 current = SSL_get_current_cipher(self->ssl);
1051 if (current == NULL)
Hirokazu Yamamotoa9b16892010-12-09 12:12:42 +00001052 Py_RETURN_NONE;
Bill Janssen98d19da2007-09-10 21:51:02 +00001053
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001054 retval = PyTuple_New(3);
1055 if (retval == NULL)
1056 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +00001057
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001058 cipher_name = (char *) SSL_CIPHER_get_name(current);
1059 if (cipher_name == NULL) {
Hirokazu Yamamotoa9b16892010-12-09 12:12:42 +00001060 Py_INCREF(Py_None);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001061 PyTuple_SET_ITEM(retval, 0, Py_None);
1062 } else {
1063 v = PyString_FromString(cipher_name);
1064 if (v == NULL)
1065 goto fail0;
1066 PyTuple_SET_ITEM(retval, 0, v);
1067 }
1068 cipher_protocol = SSL_CIPHER_get_version(current);
1069 if (cipher_protocol == NULL) {
Hirokazu Yamamotoa9b16892010-12-09 12:12:42 +00001070 Py_INCREF(Py_None);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001071 PyTuple_SET_ITEM(retval, 1, Py_None);
1072 } else {
1073 v = PyString_FromString(cipher_protocol);
1074 if (v == NULL)
1075 goto fail0;
1076 PyTuple_SET_ITEM(retval, 1, v);
1077 }
1078 v = PyInt_FromLong(SSL_CIPHER_get_bits(current, NULL));
1079 if (v == NULL)
1080 goto fail0;
1081 PyTuple_SET_ITEM(retval, 2, v);
1082 return retval;
1083
Bill Janssen98d19da2007-09-10 21:51:02 +00001084 fail0:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001085 Py_DECREF(retval);
1086 return NULL;
Bill Janssen98d19da2007-09-10 21:51:02 +00001087}
1088
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001089static void PySSL_dealloc(PySSLObject *self)
1090{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001091 if (self->peer_cert) /* Possible not to have one? */
1092 X509_free (self->peer_cert);
1093 if (self->ssl)
1094 SSL_free(self->ssl);
1095 if (self->ctx)
1096 SSL_CTX_free(self->ctx);
1097 Py_XDECREF(self->Socket);
1098 PyObject_Del(self);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001099}
1100
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001101/* If the socket has a timeout, do a select()/poll() on the socket.
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001102 The argument writing indicates the direction.
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001103 Returns one of the possibilities in the timeout_state enum (above).
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001104 */
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001105
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001106static int
Andrew M. Kuchling9c3efe32004-07-10 21:15:17 +00001107check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001108{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001109 fd_set fds;
1110 struct timeval tv;
1111 int rc;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001112
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001113 /* Nothing to do unless we're in timeout mode (not non-blocking) */
1114 if (s->sock_timeout < 0.0)
1115 return SOCKET_IS_BLOCKING;
1116 else if (s->sock_timeout == 0.0)
1117 return SOCKET_IS_NONBLOCKING;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001118
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001119 /* Guard against closed socket */
1120 if (s->sock_fd < 0)
1121 return SOCKET_HAS_BEEN_CLOSED;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001122
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001123 /* Prefer poll, if available, since you can poll() any fd
1124 * which can't be done with select(). */
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001125#ifdef HAVE_POLL
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001126 {
1127 struct pollfd pollfd;
1128 int timeout;
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001129
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001130 pollfd.fd = s->sock_fd;
1131 pollfd.events = writing ? POLLOUT : POLLIN;
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001132
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001133 /* s->sock_timeout is in seconds, timeout in ms */
1134 timeout = (int)(s->sock_timeout * 1000 + 0.5);
1135 PySSL_BEGIN_ALLOW_THREADS
1136 rc = poll(&pollfd, 1, timeout);
1137 PySSL_END_ALLOW_THREADS
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001138
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001139 goto normal_return;
1140 }
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001141#endif
1142
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001143 /* Guard against socket too large for select*/
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001144#ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001145 if (s->sock_fd >= FD_SETSIZE)
1146 return SOCKET_TOO_LARGE_FOR_SELECT;
Martin v. Löwisf84d1b92006-02-11 09:27:05 +00001147#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +00001148
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001149 /* Construct the arguments to select */
1150 tv.tv_sec = (int)s->sock_timeout;
1151 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1152 FD_ZERO(&fds);
1153 FD_SET(s->sock_fd, &fds);
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001154
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001155 /* See if the socket is ready */
1156 PySSL_BEGIN_ALLOW_THREADS
1157 if (writing)
1158 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
1159 else
1160 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
1161 PySSL_END_ALLOW_THREADS
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001162
Bill Janssen934b16d2008-06-28 22:19:33 +00001163#ifdef HAVE_POLL
Anthony Baxter93ab5fa2006-07-11 02:04:09 +00001164normal_return:
Bill Janssen934b16d2008-06-28 22:19:33 +00001165#endif
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001166 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
1167 (when we are able to write or when there's something to read) */
1168 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
Guido van Rossum99d4abf2003-01-27 22:22:50 +00001169}
1170
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001171static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
1172{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001173 Py_buffer buf;
1174 int len;
1175 int sockstate;
1176 int err;
1177 int nonblocking;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001178
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001179 if (!PyArg_ParseTuple(args, "s*:write", &buf))
1180 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001181
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001182 /* just in case the blocking state of the socket has been changed */
1183 nonblocking = (self->Socket->sock_timeout >= 0.0);
1184 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1185 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Bill Janssen934b16d2008-06-28 22:19:33 +00001186
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001187 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
1188 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1189 PyErr_SetString(PySSLErrorObject,
1190 "The write operation timed out");
1191 goto error;
1192 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1193 PyErr_SetString(PySSLErrorObject,
1194 "Underlying socket has been closed.");
1195 goto error;
1196 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1197 PyErr_SetString(PySSLErrorObject,
1198 "Underlying socket too large for select().");
1199 goto error;
1200 }
1201 do {
1202 PySSL_BEGIN_ALLOW_THREADS
1203 len = SSL_write(self->ssl, buf.buf, buf.len);
1204 err = SSL_get_error(self->ssl, len);
1205 PySSL_END_ALLOW_THREADS
1206 if (PyErr_CheckSignals()) {
1207 goto error;
1208 }
1209 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001210 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001211 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001212 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001213 } else {
1214 sockstate = SOCKET_OPERATION_OK;
1215 }
1216 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1217 PyErr_SetString(PySSLErrorObject,
1218 "The write operation timed out");
1219 goto error;
1220 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1221 PyErr_SetString(PySSLErrorObject,
1222 "Underlying socket has been closed.");
1223 goto error;
1224 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1225 break;
1226 }
1227 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
Antoine Pitrou5ba84912009-10-19 17:59:07 +00001228
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001229 PyBuffer_Release(&buf);
1230 if (len > 0)
1231 return PyInt_FromLong(len);
1232 else
1233 return PySSL_SetError(self, len, __FILE__, __LINE__);
Antoine Pitrou5ba84912009-10-19 17:59:07 +00001234
1235error:
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001236 PyBuffer_Release(&buf);
1237 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001238}
1239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001240PyDoc_STRVAR(PySSL_SSLwrite_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001241"write(s) -> len\n\
1242\n\
1243Writes the string s into the SSL object. Returns the number\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001244of bytes written.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001245
Bill Janssen934b16d2008-06-28 22:19:33 +00001246static PyObject *PySSL_SSLpending(PySSLObject *self)
1247{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001248 int count = 0;
Bill Janssen934b16d2008-06-28 22:19:33 +00001249
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001250 PySSL_BEGIN_ALLOW_THREADS
1251 count = SSL_pending(self->ssl);
1252 PySSL_END_ALLOW_THREADS
1253 if (count < 0)
1254 return PySSL_SetError(self, count, __FILE__, __LINE__);
1255 else
1256 return PyInt_FromLong(count);
Bill Janssen934b16d2008-06-28 22:19:33 +00001257}
1258
1259PyDoc_STRVAR(PySSL_SSLpending_doc,
1260"pending() -> count\n\
1261\n\
1262Returns the number of already decrypted bytes available for read,\n\
1263pending on the connection.\n");
1264
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001265static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
1266{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001267 PyObject *buf;
1268 int count = 0;
1269 int len = 1024;
1270 int sockstate;
1271 int err;
1272 int nonblocking;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001273
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001274 if (!PyArg_ParseTuple(args, "|i:read", &len))
1275 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001276
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001277 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
1278 return NULL;
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +00001279
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001280 /* just in case the blocking state of the socket has been changed */
1281 nonblocking = (self->Socket->sock_timeout >= 0.0);
1282 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1283 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Bill Janssen934b16d2008-06-28 22:19:33 +00001284
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001285 /* first check if there are bytes ready to be read */
1286 PySSL_BEGIN_ALLOW_THREADS
1287 count = SSL_pending(self->ssl);
1288 PySSL_END_ALLOW_THREADS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001289
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001290 if (!count) {
1291 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
1292 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1293 PyErr_SetString(PySSLErrorObject,
1294 "The read operation timed out");
1295 Py_DECREF(buf);
1296 return NULL;
1297 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1298 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001299 "Underlying socket too large for select().");
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001300 Py_DECREF(buf);
1301 return NULL;
1302 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
1303 if (SSL_get_shutdown(self->ssl) !=
1304 SSL_RECEIVED_SHUTDOWN)
1305 {
1306 Py_DECREF(buf);
1307 PyErr_SetString(PySSLErrorObject,
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001308 "Socket closed without SSL shutdown handshake");
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001309 return NULL;
1310 } else {
1311 /* should contain a zero-length string */
1312 _PyString_Resize(&buf, 0);
1313 return buf;
1314 }
1315 }
1316 }
1317 do {
1318 PySSL_BEGIN_ALLOW_THREADS
1319 count = SSL_read(self->ssl, PyString_AsString(buf), len);
1320 err = SSL_get_error(self->ssl, count);
1321 PySSL_END_ALLOW_THREADS
1322 if(PyErr_CheckSignals()) {
1323 Py_DECREF(buf);
1324 return NULL;
1325 }
1326 if (err == SSL_ERROR_WANT_READ) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001327 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001328 } else if (err == SSL_ERROR_WANT_WRITE) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001329 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001330 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
1331 (SSL_get_shutdown(self->ssl) ==
1332 SSL_RECEIVED_SHUTDOWN))
1333 {
1334 _PyString_Resize(&buf, 0);
1335 return buf;
1336 } else {
1337 sockstate = SOCKET_OPERATION_OK;
1338 }
1339 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1340 PyErr_SetString(PySSLErrorObject,
1341 "The read operation timed out");
1342 Py_DECREF(buf);
1343 return NULL;
1344 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
1345 break;
1346 }
1347 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
1348 if (count <= 0) {
1349 Py_DECREF(buf);
1350 return PySSL_SetError(self, count, __FILE__, __LINE__);
1351 }
1352 if (count != len)
1353 _PyString_Resize(&buf, count);
1354 return buf;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001355}
1356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(PySSL_SSLread_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001358"read([len]) -> string\n\
1359\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001360Read up to len bytes from the SSL socket.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001361
Bill Janssen934b16d2008-06-28 22:19:33 +00001362static PyObject *PySSL_SSLshutdown(PySSLObject *self)
1363{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001364 int err, ssl_err, sockstate, nonblocking;
1365 int zeros = 0;
Bill Janssen934b16d2008-06-28 22:19:33 +00001366
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001367 /* Guard against closed socket */
1368 if (self->Socket->sock_fd < 0) {
1369 PyErr_SetString(PySSLErrorObject,
1370 "Underlying socket has been closed.");
1371 return NULL;
1372 }
Bill Janssen934b16d2008-06-28 22:19:33 +00001373
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001374 /* Just in case the blocking state of the socket has been changed */
1375 nonblocking = (self->Socket->sock_timeout >= 0.0);
1376 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
1377 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
Antoine Pitroua5c4b552010-04-22 23:33:02 +00001378
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001379 while (1) {
1380 PySSL_BEGIN_ALLOW_THREADS
1381 /* Disable read-ahead so that unwrap can work correctly.
1382 * Otherwise OpenSSL might read in too much data,
1383 * eating clear text data that happens to be
1384 * transmitted after the SSL shutdown.
1385 * Should be safe to call repeatedly everytime this
1386 * function is used and the shutdown_seen_zero != 0
1387 * condition is met.
1388 */
1389 if (self->shutdown_seen_zero)
1390 SSL_set_read_ahead(self->ssl, 0);
1391 err = SSL_shutdown(self->ssl);
1392 PySSL_END_ALLOW_THREADS
1393 /* If err == 1, a secure shutdown with SSL_shutdown() is complete */
1394 if (err > 0)
1395 break;
1396 if (err == 0) {
1397 /* Don't loop endlessly; instead preserve legacy
1398 behaviour of trying SSL_shutdown() only twice.
1399 This looks necessary for OpenSSL < 0.9.8m */
1400 if (++zeros > 1)
1401 break;
1402 /* Shutdown was sent, now try receiving */
1403 self->shutdown_seen_zero = 1;
1404 continue;
1405 }
Antoine Pitroua5c4b552010-04-22 23:33:02 +00001406
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001407 /* Possibly retry shutdown until timeout or failure */
1408 ssl_err = SSL_get_error(self->ssl, err);
1409 if (ssl_err == SSL_ERROR_WANT_READ)
1410 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
1411 else if (ssl_err == SSL_ERROR_WANT_WRITE)
1412 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
1413 else
1414 break;
1415 if (sockstate == SOCKET_HAS_TIMED_OUT) {
1416 if (ssl_err == SSL_ERROR_WANT_READ)
1417 PyErr_SetString(PySSLErrorObject,
1418 "The read operation timed out");
1419 else
1420 PyErr_SetString(PySSLErrorObject,
1421 "The write operation timed out");
1422 return NULL;
1423 }
1424 else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
1425 PyErr_SetString(PySSLErrorObject,
1426 "Underlying socket too large for select().");
1427 return NULL;
1428 }
1429 else if (sockstate != SOCKET_OPERATION_OK)
1430 /* Retain the SSL error code */
1431 break;
1432 }
Bill Janssen934b16d2008-06-28 22:19:33 +00001433
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001434 if (err < 0)
1435 return PySSL_SetError(self, err, __FILE__, __LINE__);
1436 else {
1437 Py_INCREF(self->Socket);
1438 return (PyObject *) (self->Socket);
1439 }
Bill Janssen934b16d2008-06-28 22:19:33 +00001440}
1441
1442PyDoc_STRVAR(PySSL_SSLshutdown_doc,
1443"shutdown(s) -> socket\n\
1444\n\
1445Does the SSL shutdown handshake with the remote end, and returns\n\
1446the underlying socket object.");
1447
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001448static PyMethodDef PySSLMethods[] = {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001449 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
1450 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
1451 PySSL_SSLwrite_doc},
1452 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
1453 PySSL_SSLread_doc},
1454 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
1455 PySSL_SSLpending_doc},
1456 {"server", (PyCFunction)PySSL_server, METH_NOARGS},
1457 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
1458 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
1459 PySSL_peercert_doc},
1460 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
1461 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
1462 PySSL_SSLshutdown_doc},
1463 {NULL, NULL}
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001464};
1465
1466static PyObject *PySSL_getattr(PySSLObject *self, char *name)
1467{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001468 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001469}
1470
Jeremy Hylton938ace62002-07-17 16:30:39 +00001471static PyTypeObject PySSL_Type = {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001472 PyVarObject_HEAD_INIT(NULL, 0)
1473 "ssl.SSLContext", /*tp_name*/
1474 sizeof(PySSLObject), /*tp_basicsize*/
1475 0, /*tp_itemsize*/
1476 /* methods */
1477 (destructor)PySSL_dealloc, /*tp_dealloc*/
1478 0, /*tp_print*/
1479 (getattrfunc)PySSL_getattr, /*tp_getattr*/
1480 0, /*tp_setattr*/
1481 0, /*tp_compare*/
1482 0, /*tp_repr*/
1483 0, /*tp_as_number*/
1484 0, /*tp_as_sequence*/
1485 0, /*tp_as_mapping*/
1486 0, /*tp_hash*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001487};
1488
1489#ifdef HAVE_OPENSSL_RAND
1490
1491/* helper routines for seeding the SSL PRNG */
1492static PyObject *
1493PySSL_RAND_add(PyObject *self, PyObject *args)
1494{
1495 char *buf;
1496 int len;
1497 double entropy;
1498
1499 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001500 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001501 RAND_add(buf, len, entropy);
1502 Py_INCREF(Py_None);
1503 return Py_None;
1504}
1505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001506PyDoc_STRVAR(PySSL_RAND_add_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001507"RAND_add(string, entropy)\n\
1508\n\
1509Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
Bill Janssen98d19da2007-09-10 21:51:02 +00001510bound on the entropy contained in string. See RFC 1750.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001511
1512static PyObject *
1513PySSL_RAND_status(PyObject *self)
1514{
1515 return PyInt_FromLong(RAND_status());
1516}
1517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001518PyDoc_STRVAR(PySSL_RAND_status_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001519"RAND_status() -> 0 or 1\n\
1520\n\
1521Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
1522It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523using the ssl() function.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001524
1525static PyObject *
1526PySSL_RAND_egd(PyObject *self, PyObject *arg)
1527{
1528 int bytes;
1529
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001530 if (!PyString_Check(arg))
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001531 return PyErr_Format(PyExc_TypeError,
1532 "RAND_egd() expected string, found %s",
1533 Py_TYPE(arg)->tp_name);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001534 bytes = RAND_egd(PyString_AS_STRING(arg));
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001535 if (bytes == -1) {
Antoine Pitrou2e136ab2010-05-12 14:02:34 +00001536 PyErr_SetString(PySSLErrorObject,
1537 "EGD connection failed or EGD did not return "
1538 "enough data to seed the PRNG");
1539 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001540 }
1541 return PyInt_FromLong(bytes);
1542}
1543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001544PyDoc_STRVAR(PySSL_RAND_egd_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001545"RAND_egd(path) -> bytes\n\
1546\n\
Bill Janssen98d19da2007-09-10 21:51:02 +00001547Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
1548Returns number of bytes read. Raises SSLError if connection to EGD\n\
1549fails or if it does provide enough data to seed PRNG.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001550
1551#endif
1552
1553/* List of functions exported by this module. */
1554
1555static PyMethodDef PySSL_methods[] = {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001556 {"sslwrap", PySSL_sslwrap,
1557 METH_VARARGS, ssl_doc},
1558 {"_test_decode_cert", PySSL_test_decode_certificate,
1559 METH_VARARGS},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001560#ifdef HAVE_OPENSSL_RAND
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001561 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
1562 PySSL_RAND_add_doc},
1563 {"RAND_egd", PySSL_RAND_egd, METH_O,
1564 PySSL_RAND_egd_doc},
1565 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
1566 PySSL_RAND_status_doc},
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001567#endif
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001568 {NULL, NULL} /* Sentinel */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001569};
1570
1571
Bill Janssen98d19da2007-09-10 21:51:02 +00001572#ifdef WITH_THREAD
1573
1574/* an implementation of OpenSSL threading operations in terms
1575 of the Python C thread library */
1576
1577static PyThread_type_lock *_ssl_locks = NULL;
1578
1579static unsigned long _ssl_thread_id_function (void) {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001580 return PyThread_get_thread_ident();
Bill Janssen98d19da2007-09-10 21:51:02 +00001581}
1582
1583static void _ssl_thread_locking_function (int mode, int n, const char *file, int line) {
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001584 /* this function is needed to perform locking on shared data
1585 structures. (Note that OpenSSL uses a number of global data
1586 structures that will be implicitly shared whenever multiple threads
1587 use OpenSSL.) Multi-threaded applications will crash at random if
1588 it is not set.
Bill Janssen98d19da2007-09-10 21:51:02 +00001589
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001590 locking_function() must be able to handle up to CRYPTO_num_locks()
1591 different mutex locks. It sets the n-th lock if mode & CRYPTO_LOCK, and
1592 releases it otherwise.
Bill Janssen98d19da2007-09-10 21:51:02 +00001593
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001594 file and line are the file number of the function setting the
1595 lock. They can be useful for debugging.
1596 */
Bill Janssen98d19da2007-09-10 21:51:02 +00001597
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001598 if ((_ssl_locks == NULL) ||
1599 (n < 0) || ((unsigned)n >= _ssl_locks_count))
1600 return;
Bill Janssen98d19da2007-09-10 21:51:02 +00001601
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001602 if (mode & CRYPTO_LOCK) {
1603 PyThread_acquire_lock(_ssl_locks[n], 1);
1604 } else {
1605 PyThread_release_lock(_ssl_locks[n]);
1606 }
Bill Janssen98d19da2007-09-10 21:51:02 +00001607}
1608
1609static int _setup_ssl_threads(void) {
1610
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001611 unsigned int i;
Bill Janssen98d19da2007-09-10 21:51:02 +00001612
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001613 if (_ssl_locks == NULL) {
1614 _ssl_locks_count = CRYPTO_num_locks();
1615 _ssl_locks = (PyThread_type_lock *)
1616 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
1617 if (_ssl_locks == NULL)
1618 return 0;
1619 memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);
1620 for (i = 0; i < _ssl_locks_count; i++) {
1621 _ssl_locks[i] = PyThread_allocate_lock();
1622 if (_ssl_locks[i] == NULL) {
1623 unsigned int j;
1624 for (j = 0; j < i; j++) {
1625 PyThread_free_lock(_ssl_locks[j]);
1626 }
1627 free(_ssl_locks);
1628 return 0;
1629 }
1630 }
1631 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
1632 CRYPTO_set_id_callback(_ssl_thread_id_function);
1633 }
1634 return 1;
Bill Janssen98d19da2007-09-10 21:51:02 +00001635}
1636
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001637#endif /* def HAVE_THREAD */
Bill Janssen98d19da2007-09-10 21:51:02 +00001638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001639PyDoc_STRVAR(module_doc,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001640"Implementation module for SSL socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641for documentation.");
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001642
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001643PyMODINIT_FUNC
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001644init_ssl(void)
1645{
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001646 PyObject *m, *d, *r;
1647 unsigned long libver;
1648 unsigned int major, minor, fix, patch, status;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001649
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001650 Py_TYPE(&PySSL_Type) = &PyType_Type;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001651
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001652 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
1653 if (m == NULL)
1654 return;
1655 d = PyModule_GetDict(m);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001656
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001657 /* Load _socket module and its C API */
1658 if (PySocketModule_ImportModuleAndAPI())
1659 return;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001660
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001661 /* Init OpenSSL */
1662 SSL_load_error_strings();
1663 SSL_library_init();
Bill Janssen98d19da2007-09-10 21:51:02 +00001664#ifdef WITH_THREAD
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001665 /* note that this will start threading if not already started */
1666 if (!_setup_ssl_threads()) {
1667 return;
1668 }
Bill Janssen98d19da2007-09-10 21:51:02 +00001669#endif
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001670 OpenSSL_add_all_algorithms();
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001671
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001672 /* Add symbols to module dict */
1673 PySSLErrorObject = PyErr_NewException("ssl.SSLError",
1674 PySocketModule.error,
1675 NULL);
1676 if (PySSLErrorObject == NULL)
1677 return;
1678 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0)
1679 return;
1680 if (PyDict_SetItemString(d, "SSLType",
1681 (PyObject *)&PySSL_Type) != 0)
1682 return;
1683 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
1684 PY_SSL_ERROR_ZERO_RETURN);
1685 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
1686 PY_SSL_ERROR_WANT_READ);
1687 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
1688 PY_SSL_ERROR_WANT_WRITE);
1689 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
1690 PY_SSL_ERROR_WANT_X509_LOOKUP);
1691 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
1692 PY_SSL_ERROR_SYSCALL);
1693 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
1694 PY_SSL_ERROR_SSL);
1695 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
1696 PY_SSL_ERROR_WANT_CONNECT);
1697 /* non ssl.h errorcodes */
1698 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
1699 PY_SSL_ERROR_EOF);
1700 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
1701 PY_SSL_ERROR_INVALID_ERROR_CODE);
1702 /* cert requirements */
1703 PyModule_AddIntConstant(m, "CERT_NONE",
1704 PY_SSL_CERT_NONE);
1705 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
1706 PY_SSL_CERT_OPTIONAL);
1707 PyModule_AddIntConstant(m, "CERT_REQUIRED",
1708 PY_SSL_CERT_REQUIRED);
Martin v. Löwis6af3e2d2002-04-20 07:47:40 +00001709
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001710 /* protocol versions */
1711 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
1712 PY_SSL_VERSION_SSL2);
1713 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
1714 PY_SSL_VERSION_SSL3);
1715 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
1716 PY_SSL_VERSION_SSL23);
1717 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
1718 PY_SSL_VERSION_TLS1);
Antoine Pitrouf9de5342010-04-05 21:35:07 +00001719
Antoine Pitroua4c2a5c2010-05-05 15:53:45 +00001720 /* OpenSSL version */
1721 /* SSLeay() gives us the version of the library linked against,
1722 which could be different from the headers version.
1723 */
1724 libver = SSLeay();
1725 r = PyLong_FromUnsignedLong(libver);
1726 if (r == NULL)
1727 return;
1728 if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
1729 return;
1730 status = libver & 0xF;
1731 libver >>= 4;
1732 patch = libver & 0xFF;
1733 libver >>= 8;
1734 fix = libver & 0xFF;
1735 libver >>= 8;
1736 minor = libver & 0xFF;
1737 libver >>= 8;
1738 major = libver & 0xFF;
1739 r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
1740 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
1741 return;
1742 r = PyString_FromString(SSLeay_version(SSLEAY_VERSION));
1743 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
1744 return;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001745}