blob: a92a476b415b7cb3d6c6bfe36199954c1725c3a8 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * context.c
3 *
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05004 * Copyright (C) AB Strakt
5 * Copyright (C) Jean-Paul Calderone
6 * See LICENSE for details.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05007 *
8 * SSL Context objects and their methods.
9 * See the file RATIONALE for a short explanation of why this module was written.
10 *
11 * Reviewed 2001-07-23
12 */
13#include <Python.h>
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050014
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -050015#if PY_VERSION_HEX >= 0x02050000
16# define PYARG_PARSETUPLE_FORMAT const char
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -040017# define PYOBJECT_GETATTRSTRING_TYPE const char*
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -050018#else
19# define PYARG_PARSETUPLE_FORMAT char
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -040020# define PYOBJECT_GETATTRSTRING_TYPE char*
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -050021#endif
22
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050023#ifndef MS_WINDOWS
24# include <sys/socket.h>
25# include <netinet/in.h>
26# if !(defined(__BEOS__) || defined(__CYGWIN__))
27# include <netinet/tcp.h>
28# endif
29#else
30# include <winsock.h>
31# include <wincrypt.h>
32#endif
33
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050034#define SSL_MODULE
35#include "ssl.h"
36
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050037/*
38 * CALLBACKS
39 *
40 * Callbacks work like this: We provide a "global" callback in C which
41 * transforms the arguments into a Python argument tuple and calls the
42 * corresponding Python callback, and then parsing the return value back into
43 * things the C function can return.
44 *
45 * Three caveats:
46 * + How do we find the Context object where the Python callbacks are stored?
47 * + What about multithreading and execution frames?
48 * + What about Python callbacks that raise exceptions?
49 *
50 * The solution to the first issue is trivial if the callback provides
51 * "userdata" functionality. Since the only callbacks that don't provide
52 * userdata do provide a pointer to an SSL structure, we can associate an SSL
53 * object and a Connection one-to-one via the SSL_set/get_app_data()
54 * functions.
55 *
56 * The solution to the other issue is to rewrite the Py_BEGIN_ALLOW_THREADS
57 * macro allowing it (or rather a new macro) to specify where to save the
58 * thread state (in our case, as a member of the Connection/Context object) so
59 * we can retrieve it again before calling the Python callback.
60 */
61
62/*
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040063 * Globally defined passphrase callback. This is called from OpenSSL
64 * internally. The GIL will not be held when this function is invoked. It
65 * must not be held when the function returns.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050066 *
67 * Arguments: buf - Buffer to store the returned passphrase in
68 * maxlen - Maximum length of the passphrase
69 * verify - If true, the passphrase callback should ask for a
70 * password twice and verify they're equal. If false, only
71 * ask once.
72 * arg - User data, always a Context object
73 * Returns: The length of the password if successful, 0 otherwise
74 */
75static int
76global_passphrase_callback(char *buf, int maxlen, int verify, void *arg)
77{
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040078 /*
79 * Initialize len here because we're always going to return it, and we
80 * might jump to the return before it gets initialized in any other way.
81 */
82 int len = 0;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050083 char *str;
84 PyObject *argv, *ret = NULL;
85 ssl_ContextObj *ctx = (ssl_ContextObj *)arg;
86
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040087 /*
88 * GIL isn't held yet. First things first - acquire it, or any Python API
89 * we invoke might segfault or blow up the sun. The reverse will be done
90 * before returning.
91 */
92 MY_END_ALLOW_THREADS(ctx->tstate);
93
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -040094 /* The Python callback is called with a (maxlen,verify,userdata) tuple */
95 argv = Py_BuildValue("(iiO)", maxlen, verify, ctx->passphrase_userdata);
96
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040097 /*
98 * XXX Didn't check argv to see if it was NULL. -exarkun
99 */
100 ret = PyEval_CallObject(ctx->passphrase_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500101 Py_DECREF(argv);
102
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400103 if (ret == NULL) {
104 /*
Jean-Paul Calderonee1fc4ea2010-07-30 18:18:00 -0400105 * The callback raised an exception. It will be raised by whatever
106 * Python API triggered this callback.
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400107 */
108 goto out;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500109 }
110
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400111 if (!PyObject_IsTrue(ret)) {
112 /*
113 * Returned "", or None, or something. Treat it as no passphrase.
114 */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500115 Py_DECREF(ret);
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400116 goto out;
117 }
118
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400119 if (!PyBytes_Check(ret)) {
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400120 /*
Jean-Paul Calderonee1fc4ea2010-07-30 18:18:00 -0400121 * XXX Returned something that wasn't a string. This is bogus. We'll
122 * return 0 and OpenSSL will treat it as an error, resulting in an
123 * exception from whatever Python API triggered this callback.
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400124 */
125 Py_DECREF(ret);
126 goto out;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500127 }
128
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400129 len = PyBytes_Size(ret);
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400130 if (len > maxlen) {
131 /*
Jean-Paul Calderonee1fc4ea2010-07-30 18:18:00 -0400132 * Returned more than we said they were allowed to return. Just
133 * truncate it. Might be better to raise an exception,
134 * instead. -exarkun
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400135 */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500136 len = maxlen;
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400137 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500138
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400139 str = PyBytes_AsString(ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500140 strncpy(buf, str, len);
141 Py_XDECREF(ret);
142
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400143 out:
144 /*
145 * This function is returning into OpenSSL. Release the GIL again.
146 */
147 MY_BEGIN_ALLOW_THREADS(ctx->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500148 return len;
149}
150
151/*
152 * Globally defined verify callback
153 *
154 * Arguments: ok - True everything is OK "so far", false otherwise
155 * x509_ctx - Contains the certificate being checked, the current
156 * error number and depth, and the Connection we're
157 * dealing with
158 * Returns: True if everything is okay, false otherwise
159 */
160static int
161global_verify_callback(int ok, X509_STORE_CTX *x509_ctx)
162{
163 PyObject *argv, *ret;
164 SSL *ssl;
165 ssl_ConnectionObj *conn;
166 crypto_X509Obj *cert;
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500167 int errnum, errdepth, c_ret;
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400168
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400169 // Get Connection object to check thread state
170 ssl = (SSL *)X509_STORE_CTX_get_app_data(x509_ctx);
171 conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400172
Jean-Paul Calderone26aea022008-09-21 18:47:06 -0400173 MY_END_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400174
Jean-Paul Calderone1e9312e2010-10-31 21:26:18 -0400175 cert = new_x509(X509_STORE_CTX_get_current_cert(x509_ctx), 0);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500176 errnum = X509_STORE_CTX_get_error(x509_ctx);
177 errdepth = X509_STORE_CTX_get_error_depth(x509_ctx);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400178
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500179 argv = Py_BuildValue("(OOiii)", (PyObject *)conn, (PyObject *)cert,
180 errnum, errdepth, ok);
181 Py_DECREF(cert);
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400182 ret = PyEval_CallObject(conn->context->verify_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500183 Py_DECREF(argv);
184
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400185 if (ret != NULL && PyObject_IsTrue(ret)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500186 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400187 Py_DECREF(ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500188 c_ret = 1;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400189 } else {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500190 c_ret = 0;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400191 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500192
Jean-Paul Calderone26aea022008-09-21 18:47:06 -0400193 MY_BEGIN_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500194 return c_ret;
195}
196
197/*
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400198 * Globally defined info callback. This is called from OpenSSL internally.
199 * The GIL will not be held when this function is invoked. It must not be held
200 * when the function returns.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500201 *
202 * Arguments: ssl - The Connection
203 * where - The part of the SSL code that called us
204 * _ret - The return code of the SSL function that called us
205 * Returns: None
206 */
207static void
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500208global_info_callback(const SSL *ssl, int where, int _ret)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500209{
210 ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl);
211 PyObject *argv, *ret;
212
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400213 /*
214 * GIL isn't held yet. First things first - acquire it, or any Python API
215 * we invoke might segfault or blow up the sun. The reverse will be done
216 * before returning.
217 */
218 MY_END_ALLOW_THREADS(conn->tstate);
219
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500220 argv = Py_BuildValue("(Oii)", (PyObject *)conn, where, _ret);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400221 ret = PyEval_CallObject(conn->context->info_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500222 Py_DECREF(argv);
223
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400224 if (ret == NULL) {
225 /*
226 * XXX - This should be reported somehow. -exarkun
227 */
228 PyErr_Clear();
229 } else {
230 Py_DECREF(ret);
231 }
232
233 /*
234 * This function is returning into OpenSSL. Release the GIL again.
235 */
236 MY_BEGIN_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500237 return;
238}
239
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -0400240/*
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -0400241 * Globally defined TLS extension server name callback. This is called from
242 * OpenSSL internally. The GIL will not be held when this function is invoked.
243 * It must not be held when the function returns.
244 *
245 * ssl represents the connection this callback is for
246 *
247 * alert is a pointer to the alert value which maybe will be emitted to the
248 * client if there is an error handling the client hello (which contains the
249 * server name). This is an out parameter, maybe.
250 *
251 * arg is an arbitrary pointer specified by SSL_CTX_set_tlsext_servername_arg.
252 * It will be NULL for all pyOpenSSL uses.
253 */
254static int
255global_tlsext_servername_callback(const SSL *ssl, int *alert, void *arg) {
256 int result = 0;
257 PyObject *argv, *ret;
258 ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl);
259
260 /*
261 * GIL isn't held yet. First things first - acquire it, or any Python API
262 * we invoke might segfault or blow up the sun. The reverse will be done
263 * before returning.
264 */
265 MY_END_ALLOW_THREADS(conn->tstate);
266
267 argv = Py_BuildValue("(O)", (PyObject *)conn);
268 ret = PyEval_CallObject(conn->context->tlsext_servername_callback, argv);
269 Py_DECREF(argv);
270 Py_DECREF(ret);
271
272 /*
273 * This function is returning into OpenSSL. Release the GIL again.
274 */
275 MY_BEGIN_ALLOW_THREADS(conn->tstate);
276 return result;
277}
278
279/*
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -0400280 * More recent builds of OpenSSL may have SSLv2 completely disabled.
281 */
282#ifdef OPENSSL_NO_SSL2
283#define SSLv2_METHOD_TEXT ""
284#else
285#define SSLv2_METHOD_TEXT "SSLv2_METHOD, "
286#endif
287
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500288
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400289static char ssl_Context_doc[] = "\n\
290Context(method) -> Context instance\n\
291\n\
292OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\
293connections.\n\
294\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900295:param method: One of " SSLv2_METHOD_TEXT "SSLv3_METHOD, SSLv23_METHOD, or\n\
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400296 TLSv1_METHOD.\n\
297";
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500298
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -0400299#undef SSLv2_METHOD_TEXT
300
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500301static char ssl_Context_load_verify_locations_doc[] = "\n\
302Let SSL know where we can find trusted certificates for the certificate\n\
303chain\n\
304\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900305:param cafile: In which file we can find the certificates\n\
306:param capath: In which directory we can find the certificates\n\
307:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500308";
309static PyObject *
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400310ssl_Context_load_verify_locations(ssl_ContextObj *self, PyObject *args) {
311 char *cafile = NULL;
312 char *capath = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500313
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400314 if (!PyArg_ParseTuple(args, "z|z:load_verify_locations", &cafile, &capath)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500315 return NULL;
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400316 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500317
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400318 if (!SSL_CTX_load_verify_locations(self->ctx, cafile, capath))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500319 {
Rick Deand369c932009-07-08 11:48:33 -0500320 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500321 return NULL;
322 }
323 else
324 {
325 Py_INCREF(Py_None);
326 return Py_None;
327 }
328}
329
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400330static char ssl_Context_set_default_verify_paths_doc[] = "\n\
331Use the platform-specific CA certificate locations\n\
332\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900333:return: None\n\
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400334";
335static PyObject *
336ssl_Context_set_default_verify_paths(ssl_ContextObj *self, PyObject *args) {
Jean-Paul Calderone9eadb962008-09-07 21:20:44 -0400337 if (!PyArg_ParseTuple(args, ":set_default_verify_paths")) {
338 return NULL;
339 }
340
Jean-Paul Calderone286b1922008-09-07 21:35:38 -0400341 /*
342 * XXX Error handling for SSL_CTX_set_default_verify_paths is untested.
343 * -exarkun
344 */
345 if (!SSL_CTX_set_default_verify_paths(self->ctx)) {
Rick Deand369c932009-07-08 11:48:33 -0500346 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone286b1922008-09-07 21:35:38 -0400347 return NULL;
348 }
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400349 Py_INCREF(Py_None);
350 return Py_None;
351};
352
353
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500354static char ssl_Context_set_passwd_cb_doc[] = "\n\
355Set the passphrase callback\n\
356\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900357:param callback: The Python callback to use\n\
358:param userdata: (optional) A Python object which will be given as\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400359 argument to the callback\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900360:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500361";
362static PyObject *
363ssl_Context_set_passwd_cb(ssl_ContextObj *self, PyObject *args)
364{
365 PyObject *callback = NULL, *userdata = Py_None;
366
367 if (!PyArg_ParseTuple(args, "O|O:set_passwd_cb", &callback, &userdata))
368 return NULL;
369
370 if (!PyCallable_Check(callback))
371 {
372 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
373 return NULL;
374 }
375
376 Py_DECREF(self->passphrase_callback);
377 Py_INCREF(callback);
378 self->passphrase_callback = callback;
379 SSL_CTX_set_default_passwd_cb(self->ctx, global_passphrase_callback);
380
381 Py_DECREF(self->passphrase_userdata);
382 Py_INCREF(userdata);
383 self->passphrase_userdata = userdata;
384 SSL_CTX_set_default_passwd_cb_userdata(self->ctx, (void *)self);
385
386 Py_INCREF(Py_None);
387 return Py_None;
388}
389
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200390static PyTypeObject *
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400391type_modified_error(const char *name) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200392 PyErr_Format(PyExc_RuntimeError,
393 "OpenSSL.crypto's '%s' attribute has been modified",
394 name);
395 return NULL;
396}
397
398static PyTypeObject *
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400399import_crypto_type(const char *name, size_t objsize) {
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200400 PyObject *module, *type, *name_attr;
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200401 PyTypeObject *res;
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200402 int right_name;
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200403
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200404 module = PyImport_ImportModule("OpenSSL.crypto");
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200405 if (module == NULL) {
406 return NULL;
407 }
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400408 type = PyObject_GetAttrString(module, (PYOBJECT_GETATTRSTRING_TYPE)name);
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200409 Py_DECREF(module);
410 if (type == NULL) {
411 return NULL;
412 }
413 if (!(PyType_Check(type))) {
414 Py_DECREF(type);
415 return type_modified_error(name);
416 }
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200417 name_attr = PyObject_GetAttrString(type, "__name__");
418 if (name_attr == NULL) {
419 Py_DECREF(type);
420 return NULL;
421 }
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -0400422
423#ifdef PY3
424 {
425 PyObject* asciiname = PyUnicode_AsASCIIString(name_attr);
426 Py_DECREF(name_attr);
427 name_attr = asciiname;
428 }
429#endif
Jean-Paul Calderone9e4eeae2010-08-22 21:32:52 -0400430 right_name = (PyBytes_CheckExact(name_attr) &&
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -0400431 strcmp(name, PyBytes_AsString(name_attr)) == 0);
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200432 Py_DECREF(name_attr);
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200433 res = (PyTypeObject *)type;
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200434 if (!right_name || res->tp_basicsize != objsize) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200435 Py_DECREF(type);
436 return type_modified_error(name);
437 }
438 return res;
439}
440
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500441static crypto_X509Obj *
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400442parse_certificate_argument(const char* format, PyObject* args) {
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500443 static PyTypeObject *crypto_X509_type = NULL;
444 crypto_X509Obj *cert;
445
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400446 if (!crypto_X509_type) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200447 crypto_X509_type = import_crypto_type("X509", sizeof(crypto_X509Obj));
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400448 if (!crypto_X509_type) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200449 return NULL;
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400450 }
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500451 }
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200452 if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format,
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400453 crypto_X509_type, &cert)) {
454 return NULL;
455 }
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500456 return cert;
457}
458
459static char ssl_Context_add_extra_chain_cert_doc[] = "\n\
460Add certificate to chain\n\
461\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900462:param certobj: The X509 certificate object to add to the chain\n\
463:return: None\n\
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500464";
465
466static PyObject *
467ssl_Context_add_extra_chain_cert(ssl_ContextObj *self, PyObject *args)
468{
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500469 X509* cert_original;
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500470 crypto_X509Obj *cert = parse_certificate_argument(
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200471 "O!:add_extra_chain_cert", args);
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500472 if (cert == NULL)
473 {
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500474 return NULL;
475 }
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500476 if (!(cert_original = X509_dup(cert->x509)))
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500477 {
Rick Deand369c932009-07-08 11:48:33 -0500478 /* exception_from_error_queue(ssl_Error); */
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500479 PyErr_SetString(PyExc_RuntimeError, "X509_dup failed");
480 return NULL;
481 }
482 if (!SSL_CTX_add_extra_chain_cert(self->ctx, cert_original))
483 {
484 X509_free(cert_original);
Rick Deand369c932009-07-08 11:48:33 -0500485 exception_from_error_queue(ssl_Error);
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500486 return NULL;
487 }
488 else
489 {
490 Py_INCREF(Py_None);
491 return Py_None;
492 }
493}
494
495
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500496static char ssl_Context_use_certificate_chain_file_doc[] = "\n\
497Load a certificate chain from a file\n\
498\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900499:param certfile: The name of the certificate chain file\n\
500:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500501";
502static PyObject *
503ssl_Context_use_certificate_chain_file(ssl_ContextObj *self, PyObject *args)
504{
505 char *certfile;
506
507 if (!PyArg_ParseTuple(args, "s:use_certificate_chain_file", &certfile))
508 return NULL;
509
510 if (!SSL_CTX_use_certificate_chain_file(self->ctx, certfile))
511 {
Rick Deand369c932009-07-08 11:48:33 -0500512 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500513 return NULL;
514 }
515 else
516 {
517 Py_INCREF(Py_None);
518 return Py_None;
519 }
520}
521
522
523static char ssl_Context_use_certificate_file_doc[] = "\n\
524Load a certificate from a file\n\
525\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900526:param certfile: The name of the certificate file\n\
527:param filetype: (optional) The encoding of the file, default is PEM\n\
528:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500529";
530static PyObject *
531ssl_Context_use_certificate_file(ssl_ContextObj *self, PyObject *args)
532{
533 char *certfile;
534 int filetype = SSL_FILETYPE_PEM;
535
536 if (!PyArg_ParseTuple(args, "s|i:use_certificate_file", &certfile, &filetype))
537 return NULL;
538
539 if (!SSL_CTX_use_certificate_file(self->ctx, certfile, filetype))
540 {
Rick Deand369c932009-07-08 11:48:33 -0500541 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500542 return NULL;
543 }
544 else
545 {
546 Py_INCREF(Py_None);
547 return Py_None;
548 }
549}
550
551static char ssl_Context_use_certificate_doc[] = "\n\
552Load a certificate from a X509 object\n\
553\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900554:param cert: The X509 object\n\
555:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500556";
557static PyObject *
558ssl_Context_use_certificate(ssl_ContextObj *self, PyObject *args)
559{
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500560 crypto_X509Obj *cert = parse_certificate_argument(
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200561 "O!:use_certificate", args);
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500562 if (cert == NULL) {
563 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500564 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500565
566 if (!SSL_CTX_use_certificate(self->ctx, cert->x509))
567 {
Rick Deand369c932009-07-08 11:48:33 -0500568 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500569 return NULL;
570 }
571 else
572 {
573 Py_INCREF(Py_None);
574 return Py_None;
575 }
576}
577
578static char ssl_Context_use_privatekey_file_doc[] = "\n\
579Load a private key from a file\n\
580\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900581:param keyfile: The name of the key file\n\
582:param filetype: (optional) The encoding of the file, default is PEM\n\
583:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500584";
585static PyObject *
586ssl_Context_use_privatekey_file(ssl_ContextObj *self, PyObject *args)
587{
588 char *keyfile;
589 int filetype = SSL_FILETYPE_PEM, ret;
590
591 if (!PyArg_ParseTuple(args, "s|i:use_privatekey_file", &keyfile, &filetype))
592 return NULL;
593
594 MY_BEGIN_ALLOW_THREADS(self->tstate);
595 ret = SSL_CTX_use_PrivateKey_file(self->ctx, keyfile, filetype);
596 MY_END_ALLOW_THREADS(self->tstate);
597
598 if (PyErr_Occurred())
599 {
600 flush_error_queue();
601 return NULL;
602 }
603
604 if (!ret)
605 {
Rick Deand369c932009-07-08 11:48:33 -0500606 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500607 return NULL;
608 }
609 else
610 {
611 Py_INCREF(Py_None);
612 return Py_None;
613 }
614}
615
616static char ssl_Context_use_privatekey_doc[] = "\n\
617Load a private key from a PKey object\n\
618\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900619:param pkey: The PKey object\n\
620:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500621";
622static PyObject *
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400623ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500624 static PyTypeObject *crypto_PKey_type = NULL;
625 crypto_PKeyObj *pkey;
626
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400627 if (!crypto_PKey_type) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200628 crypto_PKey_type = import_crypto_type("PKey", sizeof(crypto_PKeyObj));
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400629 if (!crypto_PKey_type) {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200630 return NULL;
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400631 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500632 }
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400633 if (!PyArg_ParseTuple(args, "O!:use_privatekey", crypto_PKey_type, &pkey)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500634 return NULL;
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400635 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500636
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400637 if (!SSL_CTX_use_PrivateKey(self->ctx, pkey->pkey)) {
Rick Deand369c932009-07-08 11:48:33 -0500638 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500639 return NULL;
Jean-Paul Calderonef606a842009-10-24 14:08:29 -0400640 } else {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500641 Py_INCREF(Py_None);
642 return Py_None;
643 }
644}
645
646static char ssl_Context_check_privatekey_doc[] = "\n\
647Check that the private key and certificate match up\n\
648\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900649:return: None (raises an exception if something's wrong)\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500650";
651static PyObject *
652ssl_Context_check_privatekey(ssl_ContextObj *self, PyObject *args)
653{
654 if (!PyArg_ParseTuple(args, ":check_privatekey"))
655 return NULL;
656
657 if (!SSL_CTX_check_private_key(self->ctx))
658 {
Rick Deand369c932009-07-08 11:48:33 -0500659 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500660 return NULL;
661 }
662 else
663 {
664 Py_INCREF(Py_None);
665 return Py_None;
666 }
667}
668
669static char ssl_Context_load_client_ca_doc[] = "\n\
Jonathan Ballet6a0b57b2011-07-16 14:22:14 +0900670Load the trusted certificates that will be sent to the client (basically\n\
Jean-Paul Calderone0294e3d2010-09-09 18:17:48 -0400671telling the client \"These are the guys I trust\"). Does not actually\n\
672imply any of the certificates are trusted; that must be configured\n\
673separately.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500674\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900675:param cafile: The name of the certificates file\n\
676:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500677";
678static PyObject *
679ssl_Context_load_client_ca(ssl_ContextObj *self, PyObject *args)
680{
681 char *cafile;
682
683 if (!PyArg_ParseTuple(args, "s:load_client_ca", &cafile))
684 return NULL;
685
686 SSL_CTX_set_client_CA_list(self->ctx, SSL_load_client_CA_file(cafile));
687
688 Py_INCREF(Py_None);
689 return Py_None;
690}
691
692static char ssl_Context_set_session_id_doc[] = "\n\
693Set the session identifier, this is needed if you want to do session\n\
694resumption (which, ironically, isn't implemented yet)\n\
695\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900696:param buf: A Python object that can be safely converted to a string\n\
697:returns: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500698";
699static PyObject *
700ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args)
701{
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500702 unsigned char *buf;
703 unsigned int len;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500704
705 if (!PyArg_ParseTuple(args, "s#:set_session_id", &buf, &len))
706 return NULL;
707
708 if (!SSL_CTX_set_session_id_context(self->ctx, buf, len))
709 {
Rick Deand369c932009-07-08 11:48:33 -0500710 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500711 return NULL;
712 }
713 else
714 {
715 Py_INCREF(Py_None);
716 return Py_None;
717 }
718}
719
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500720static char ssl_Context_set_session_cache_mode_doc[] = "\n\
721Enable/disable session caching and the mode used.\n\
722\n\
723@param mode: One or more of the SESS_CACHE_* flags (combine using bitwise or)\n\
724@return: The previously set caching mode.n\
725";
726static PyObject *
727ssl_Context_set_session_cache_mode(ssl_ContextObj *self, PyObject *args)
728{
729 long mode, result;
730
731 if (!PyArg_ParseTuple(args, "l:set_session_cache_mode", &mode))
732 return NULL;
733
734 result = SSL_CTX_set_session_cache_mode(self->ctx, mode);
735 return PyLong_FromLong(result);
736
737}
738
739static char ssl_Context_get_session_cache_mode_doc[] = "\n\
740Returns the currently used cache mode.\n\
741\n\
742@return: The currently used cache mode.\n\
743";
744static PyObject *
745ssl_Context_get_session_cache_mode(ssl_ContextObj *self, PyObject *args)
746{
747 if (!PyArg_ParseTuple(args, ":get_session_cache_mode"))
748 return NULL;
749 return PyLong_FromLong((long)SSL_CTX_get_session_cache_mode(self->ctx));
750}
751
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500752static char ssl_Context_set_verify_doc[] = "\n\
753Set the verify mode and verify callback\n\
754\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900755:param mode: The verify mode, this is either VERIFY_NONE or\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400756 VERIFY_PEER combined with possible other flags\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900757:param callback: The Python callback to use\n\
758:return: None\n\
Jean-Paul Calderone24aedf42008-03-06 22:01:16 -0500759\n\
760See SSL_CTX_set_verify(3SSL) for further details.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500761";
762static PyObject *
763ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args)
764{
765 int mode;
766 PyObject *callback = NULL;
767
768 if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback))
769 return NULL;
770
771 if (!PyCallable_Check(callback))
772 {
773 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
774 return NULL;
775 }
776
777 Py_DECREF(self->verify_callback);
778 Py_INCREF(callback);
779 self->verify_callback = callback;
780 SSL_CTX_set_verify(self->ctx, mode, global_verify_callback);
781
782 Py_INCREF(Py_None);
783 return Py_None;
784}
785
786static char ssl_Context_set_verify_depth_doc[] = "\n\
787Set the verify depth\n\
788\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900789:param depth: An integer specifying the verify depth\n\
790:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500791";
792static PyObject *
793ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args)
794{
795 int depth;
796
797 if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth))
798 return NULL;
799
800 SSL_CTX_set_verify_depth(self->ctx, depth);
801 Py_INCREF(Py_None);
802 return Py_None;
803}
804
805static char ssl_Context_get_verify_mode_doc[] = "\n\
806Get the verify mode\n\
807\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900808:return: The verify mode\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500809";
810static PyObject *
811ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args)
812{
813 int mode;
814
815 if (!PyArg_ParseTuple(args, ":get_verify_mode"))
816 return NULL;
817
818 mode = SSL_CTX_get_verify_mode(self->ctx);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400819 return PyLong_FromLong((long)mode);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500820}
821
822static char ssl_Context_get_verify_depth_doc[] = "\n\
823Get the verify depth\n\
824\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900825:return: The verify depth\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500826";
827static PyObject *
828ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args)
829{
830 int depth;
831
832 if (!PyArg_ParseTuple(args, ":get_verify_depth"))
833 return NULL;
834
835 depth = SSL_CTX_get_verify_depth(self->ctx);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400836 return PyLong_FromLong((long)depth);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500837}
838
839static char ssl_Context_load_tmp_dh_doc[] = "\n\
840Load parameters for Ephemeral Diffie-Hellman\n\
841\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900842:param dhfile: The file to load EDH parameters from\n\
843:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500844";
845static PyObject *
846ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args)
847{
848 char *dhfile;
849 BIO *bio;
850 DH *dh;
851
852 if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile))
853 return NULL;
854
855 bio = BIO_new_file(dhfile, "r");
Jean-Paul Calderone6ace4782010-09-09 18:43:40 -0400856 if (bio == NULL) {
857 exception_from_error_queue(ssl_Error);
858 return NULL;
859 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500860
861 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
862 SSL_CTX_set_tmp_dh(self->ctx, dh);
863 DH_free(dh);
864 BIO_free(bio);
865
866 Py_INCREF(Py_None);
867 return Py_None;
868}
869
870static char ssl_Context_set_cipher_list_doc[] = "\n\
871Change the cipher list\n\
872\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900873:param cipher_list: A cipher list, see ciphers(1)\n\
874:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500875";
876static PyObject *
877ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args)
878{
879 char *cipher_list;
880
881 if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list))
882 return NULL;
883
884 if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list))
885 {
Rick Deand369c932009-07-08 11:48:33 -0500886 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500887 return NULL;
888 }
889 else
890 {
891 Py_INCREF(Py_None);
892 return Py_None;
893 }
894}
895
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200896static char ssl_Context_set_client_ca_list_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200897Set the list of preferred client certificate signers for this server context.\n\
898\n\
899This list of certificate authorities will be sent to the client when the\n\
900server requests a client certificate.\n\
901\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900902:param certificate_authorities: a sequence of X509Names.\n\
903:return: None\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200904";
905
906static PyObject *
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200907ssl_Context_set_client_ca_list(ssl_ContextObj *self, PyObject *args)
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200908{
909 static PyTypeObject *X509NameType;
910 PyObject *sequence, *tuple, *item;
911 crypto_X509NameObj *name;
912 X509_NAME *sslname;
913 STACK_OF(X509_NAME) *CANames;
914 Py_ssize_t length;
915 int i;
916
917 if (X509NameType == NULL) {
918 X509NameType = import_crypto_type("X509Name", sizeof(crypto_X509NameObj));
919 if (X509NameType == NULL) {
920 return NULL;
921 }
922 }
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200923 if (!PyArg_ParseTuple(args, "O:set_client_ca_list", &sequence)) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200924 return NULL;
925 }
926 tuple = PySequence_Tuple(sequence);
927 if (tuple == NULL) {
928 return NULL;
929 }
930 length = PyTuple_Size(tuple);
931 if (length >= INT_MAX) {
932 PyErr_SetString(PyExc_ValueError, "client CA list is too long");
933 Py_DECREF(tuple);
934 return NULL;
935 }
936 CANames = sk_X509_NAME_new_null();
937 if (CANames == NULL) {
938 Py_DECREF(tuple);
939 exception_from_error_queue(ssl_Error);
940 return NULL;
941 }
942 for (i = 0; i < length; i++) {
943 item = PyTuple_GetItem(tuple, i);
944 if (item->ob_type != X509NameType) {
945 PyErr_Format(PyExc_TypeError,
946 "client CAs must be X509Name objects, not %s objects",
947 item->ob_type->tp_name);
948 sk_X509_NAME_free(CANames);
949 Py_DECREF(tuple);
950 return NULL;
951 }
952 name = (crypto_X509NameObj *)item;
953 sslname = X509_NAME_dup(name->x509_name);
954 if (sslname == NULL) {
955 sk_X509_NAME_free(CANames);
956 Py_DECREF(tuple);
957 exception_from_error_queue(ssl_Error);
958 return NULL;
959 }
960 if (!sk_X509_NAME_push(CANames, sslname)) {
961 X509_NAME_free(sslname);
962 sk_X509_NAME_free(CANames);
963 Py_DECREF(tuple);
964 exception_from_error_queue(ssl_Error);
965 return NULL;
966 }
967 }
968 Py_DECREF(tuple);
969 SSL_CTX_set_client_CA_list(self->ctx, CANames);
970 Py_INCREF(Py_None);
971 return Py_None;
972}
973
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200974static char ssl_Context_add_client_ca_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200975Add the CA certificate to the list of preferred signers for this context.\n\
976\n\
977The list of certificate authorities will be sent to the client when the\n\
978server requests a client certificate.\n\
979\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900980:param certificate_authority: certificate authority's X509 certificate.\n\
981:return: None\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200982";
983
984static PyObject *
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200985ssl_Context_add_client_ca(ssl_ContextObj *self, PyObject *args)
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200986{
987 crypto_X509Obj *cert;
988
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200989 cert = parse_certificate_argument("O!:add_client_ca", args);
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200990 if (cert == NULL) {
991 return NULL;
992 }
993 if (!SSL_CTX_add_client_CA(self->ctx, cert->x509)) {
994 exception_from_error_queue(ssl_Error);
995 return NULL;
996 }
997 Py_INCREF(Py_None);
998 return Py_None;
999}
1000
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001001static char ssl_Context_set_timeout_doc[] = "\n\
1002Set session timeout\n\
1003\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001004:param timeout: The timeout in seconds\n\
1005:return: The previous session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001006";
1007static PyObject *
1008ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args)
1009{
1010 long t, ret;
1011
1012 if (!PyArg_ParseTuple(args, "l:set_timeout", &t))
1013 return NULL;
1014
1015 ret = SSL_CTX_set_timeout(self->ctx, t);
1016 return PyLong_FromLong(ret);
1017}
1018
1019static char ssl_Context_get_timeout_doc[] = "\n\
1020Get the session timeout\n\
1021\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001022:return: The session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001023";
1024static PyObject *
1025ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args)
1026{
1027 long ret;
1028
1029 if (!PyArg_ParseTuple(args, ":get_timeout"))
1030 return NULL;
1031
1032 ret = SSL_CTX_get_timeout(self->ctx);
1033 return PyLong_FromLong(ret);
1034}
1035
1036static char ssl_Context_set_info_callback_doc[] = "\n\
1037Set the info callback\n\
1038\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001039:param callback: The Python callback to use\n\
1040:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001041";
1042static PyObject *
1043ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args)
1044{
1045 PyObject *callback;
1046
1047 if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback))
1048 return NULL;
1049
1050 if (!PyCallable_Check(callback))
1051 {
1052 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
1053 return NULL;
1054 }
1055
1056 Py_DECREF(self->info_callback);
1057 Py_INCREF(callback);
1058 self->info_callback = callback;
1059 SSL_CTX_set_info_callback(self->ctx, global_info_callback);
1060
1061 Py_INCREF(Py_None);
1062 return Py_None;
1063}
1064
1065static char ssl_Context_get_app_data_doc[] = "\n\
1066Get the application data (supplied via set_app_data())\n\
1067\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001068:return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001069";
1070static PyObject *
1071ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args)
1072{
1073 if (!PyArg_ParseTuple(args, ":get_app_data"))
1074 return NULL;
1075
1076 Py_INCREF(self->app_data);
1077 return self->app_data;
1078}
1079
1080static char ssl_Context_set_app_data_doc[] = "\n\
1081Set the application data (will be returned from get_app_data())\n\
1082\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001083:param data: Any Python object\n\
1084:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001085";
1086static PyObject *
1087ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args)
1088{
1089 PyObject *data;
1090
1091 if (!PyArg_ParseTuple(args, "O:set_app_data", &data))
1092 return NULL;
1093
1094 Py_DECREF(self->app_data);
1095 Py_INCREF(data);
1096 self->app_data = data;
1097
1098 Py_INCREF(Py_None);
1099 return Py_None;
1100}
1101
1102static char ssl_Context_get_cert_store_doc[] = "\n\
1103Get the certificate store for the context\n\
1104\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001105:return: A X509Store object\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001106";
1107static PyObject *
1108ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args)
1109{
1110 X509_STORE *store;
1111
1112 if (!PyArg_ParseTuple(args, ":get_cert_store"))
1113 return NULL;
1114
1115 if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL)
1116 {
1117 Py_INCREF(Py_None);
1118 return Py_None;
1119 }
1120 else
1121 {
Jean-Paul Calderone1e9312e2010-10-31 21:26:18 -04001122 return (PyObject *)new_x509store(store, 0);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001123 }
1124}
1125
1126static char ssl_Context_set_options_doc[] = "\n\
1127Add options. Options set before are not cleared!\n\
1128\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001129:param options: The options to add.\n\
1130:return: The new option bitmask.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001131";
1132static PyObject *
1133ssl_Context_set_options(ssl_ContextObj *self, PyObject *args)
1134{
1135 long options;
1136
1137 if (!PyArg_ParseTuple(args, "l:set_options", &options))
1138 return NULL;
1139
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001140 return PyLong_FromLong(SSL_CTX_set_options(self->ctx, options));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001141}
1142
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001143static char ssl_Context_set_mode_doc[] = "\n\
1144Add modes via bitmask. Modes set before are not cleared!\n\
1145\n\
Jean-Paul Calderone975a64a2011-09-11 09:35:32 -04001146:param mode: The mode to add.\n\
1147:return: The new mode bitmask.\n\
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001148";
1149static PyObject *
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001150ssl_Context_set_mode(ssl_ContextObj *self, PyObject *args) {
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001151 long mode;
1152
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001153 if (!PyArg_ParseTuple(args, "l:set_mode", &mode)) {
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001154 return NULL;
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001155 }
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001156
1157 return PyLong_FromLong(SSL_CTX_set_mode(self->ctx, mode));
1158}
1159
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001160static char ssl_Context_set_tlsext_servername_callback_doc[] = "\n\
1161Specify a callback function to be called when clients specify a server name.\n\
1162\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001163:param callback: The callback function. It will be invoked with one\n\
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001164 argument, the Connection instance.\n\
1165\n\
1166";
1167static PyObject *
1168ssl_Context_set_tlsext_servername_callback(ssl_ContextObj *self, PyObject *args) {
1169 PyObject *callback;
1170 PyObject *old;
1171
1172 if (!PyArg_ParseTuple(args, "O:set_tlsext_servername_callback", &callback)) {
1173 return NULL;
1174 }
1175
1176 Py_INCREF(callback);
1177 old = self->tlsext_servername_callback;
1178 self->tlsext_servername_callback = callback;
1179 Py_DECREF(old);
1180
1181 SSL_CTX_set_tlsext_servername_callback(self->ctx, global_tlsext_servername_callback);
1182 SSL_CTX_set_tlsext_servername_arg(self->ctx, NULL);
1183
1184 Py_INCREF(Py_None);
1185 return Py_None;
1186}
1187
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001188
1189/*
1190 * Member methods in the Context object
1191 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
1192 * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS }
1193 * for convenience
1194 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real
1195 * function with the name 'name'
1196 */
1197#define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc }
1198static PyMethodDef ssl_Context_methods[] = {
1199 ADD_METHOD(load_verify_locations),
1200 ADD_METHOD(set_passwd_cb),
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -04001201 ADD_METHOD(set_default_verify_paths),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001202 ADD_METHOD(use_certificate_chain_file),
1203 ADD_METHOD(use_certificate_file),
1204 ADD_METHOD(use_certificate),
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -05001205 ADD_METHOD(add_extra_chain_cert),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001206 ADD_METHOD(use_privatekey_file),
1207 ADD_METHOD(use_privatekey),
1208 ADD_METHOD(check_privatekey),
1209 ADD_METHOD(load_client_ca),
1210 ADD_METHOD(set_session_id),
Jean-Paul Calderone313bf012012-02-08 13:02:49 -05001211 ADD_METHOD(set_session_cache_mode),
1212 ADD_METHOD(get_session_cache_mode),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001213 ADD_METHOD(set_verify),
1214 ADD_METHOD(set_verify_depth),
1215 ADD_METHOD(get_verify_mode),
1216 ADD_METHOD(get_verify_depth),
1217 ADD_METHOD(load_tmp_dh),
1218 ADD_METHOD(set_cipher_list),
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001219 ADD_METHOD(set_client_ca_list),
1220 ADD_METHOD(add_client_ca),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001221 ADD_METHOD(set_timeout),
1222 ADD_METHOD(get_timeout),
1223 ADD_METHOD(set_info_callback),
1224 ADD_METHOD(get_app_data),
1225 ADD_METHOD(set_app_data),
1226 ADD_METHOD(get_cert_store),
1227 ADD_METHOD(set_options),
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001228 ADD_METHOD(set_mode),
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001229 ADD_METHOD(set_tlsext_servername_callback),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001230 { NULL, NULL }
1231};
1232#undef ADD_METHOD
1233
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001234/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001235 * Despite the name which might suggest otherwise, this is not the tp_init for
1236 * the Context type. It's just the common initialization code shared by the
1237 * two _{Nn}ew functions below.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001238 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001239static ssl_ContextObj*
1240ssl_Context_init(ssl_ContextObj *self, int i_method) {
Jean-Paul Calderone1c198f92011-04-14 09:46:23 -04001241#if (OPENSSL_VERSION_NUMBER >> 28) == 0x01
1242 const
1243#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001244 SSL_METHOD *method;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001245
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001246 switch (i_method) {
1247 case ssl_SSLv2_METHOD:
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -04001248#ifdef OPENSSL_NO_SSL2
1249 PyErr_SetString(PyExc_ValueError, "SSLv2_METHOD not supported by this version of OpenSSL");
1250 return NULL;
1251#else
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001252 method = SSLv2_method();
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -04001253#endif
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001254 break;
1255 case ssl_SSLv23_METHOD:
1256 method = SSLv23_method();
1257 break;
1258 case ssl_SSLv3_METHOD:
1259 method = SSLv3_method();
1260 break;
1261 case ssl_TLSv1_METHOD:
1262 method = TLSv1_method();
1263 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001264 default:
1265 PyErr_SetString(PyExc_ValueError, "No such protocol");
1266 return NULL;
1267 }
1268
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001269 self->ctx = SSL_CTX_new(method);
1270 Py_INCREF(Py_None);
1271 self->passphrase_callback = Py_None;
1272 Py_INCREF(Py_None);
1273 self->verify_callback = Py_None;
1274 Py_INCREF(Py_None);
1275 self->info_callback = Py_None;
1276
1277 Py_INCREF(Py_None);
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001278 self->tlsext_servername_callback = Py_None;
1279
1280 Py_INCREF(Py_None);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001281 self->passphrase_userdata = Py_None;
1282
1283 Py_INCREF(Py_None);
1284 self->app_data = Py_None;
1285
1286 /* Some initialization that's required to operate smoothly in Python */
1287 SSL_CTX_set_app_data(self->ctx, self);
1288 SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
1289 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1290 SSL_MODE_AUTO_RETRY);
1291
1292 self->tstate = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001293
1294 return self;
1295}
1296
1297/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001298 * This one is exposed in the CObject API. I want to deprecate it.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001299 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001300ssl_ContextObj*
1301ssl_Context_New(int i_method) {
1302 ssl_ContextObj *self;
1303
1304 self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type);
1305 if (self == NULL) {
1306 return (ssl_ContextObj *)PyErr_NoMemory();
1307 }
1308 self = ssl_Context_init(self, i_method);
1309 PyObject_GC_Track((PyObject *)self);
1310 return self;
1311}
1312
1313
1314/*
1315 * This one is the tp_new of the Context type. It's great.
1316 */
1317static PyObject*
1318ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
1319 int i_method;
1320 ssl_ContextObj *self;
1321 static char *kwlist[] = {"method", NULL};
1322
1323 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) {
1324 return NULL;
1325 }
1326
1327 self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1);
1328 if (self == NULL) {
1329 return NULL;
1330 }
1331
1332 return (PyObject *)ssl_Context_init(self, i_method);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001333}
1334
1335/*
1336 * Call the visitproc on all contained objects.
1337 *
1338 * Arguments: self - The Context object
1339 * visit - Function to call
1340 * arg - Extra argument to visit
1341 * Returns: 0 if all goes well, otherwise the return code from the first
1342 * call that gave non-zero result.
1343 */
1344static int
1345ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg)
1346{
1347 int ret = 0;
1348
1349 if (ret == 0 && self->passphrase_callback != NULL)
1350 ret = visit((PyObject *)self->passphrase_callback, arg);
1351 if (ret == 0 && self->passphrase_userdata != NULL)
1352 ret = visit((PyObject *)self->passphrase_userdata, arg);
1353 if (ret == 0 && self->verify_callback != NULL)
1354 ret = visit((PyObject *)self->verify_callback, arg);
1355 if (ret == 0 && self->info_callback != NULL)
1356 ret = visit((PyObject *)self->info_callback, arg);
1357 if (ret == 0 && self->app_data != NULL)
1358 ret = visit(self->app_data, arg);
1359 return ret;
1360}
1361
1362/*
1363 * Decref all contained objects and zero the pointers.
1364 *
1365 * Arguments: self - The Context object
1366 * Returns: Always 0.
1367 */
1368static int
1369ssl_Context_clear(ssl_ContextObj *self)
1370{
1371 Py_XDECREF(self->passphrase_callback);
1372 self->passphrase_callback = NULL;
1373 Py_XDECREF(self->passphrase_userdata);
1374 self->passphrase_userdata = NULL;
1375 Py_XDECREF(self->verify_callback);
1376 self->verify_callback = NULL;
1377 Py_XDECREF(self->info_callback);
1378 self->info_callback = NULL;
1379 Py_XDECREF(self->app_data);
1380 self->app_data = NULL;
1381 return 0;
1382}
1383
1384/*
1385 * Deallocate the memory used by the Context object
1386 *
1387 * Arguments: self - The Context object
1388 * Returns: None
1389 */
1390static void
1391ssl_Context_dealloc(ssl_ContextObj *self)
1392{
1393 PyObject_GC_UnTrack((PyObject *)self);
1394 SSL_CTX_free(self->ctx);
1395 ssl_Context_clear(self);
1396 PyObject_GC_Del(self);
1397}
1398
1399
1400PyTypeObject ssl_Context_Type = {
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -04001401 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001402 "OpenSSL.SSL.Context",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001403 sizeof(ssl_ContextObj),
1404 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001405 (destructor)ssl_Context_dealloc, /* tp_dealloc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001406 NULL, /* print */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001407 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001408 NULL, /* setattr */
1409 NULL, /* compare */
1410 NULL, /* repr */
1411 NULL, /* as_number */
1412 NULL, /* as_sequence */
1413 NULL, /* as_mapping */
1414 NULL, /* hash */
1415 NULL, /* call */
1416 NULL, /* str */
1417 NULL, /* getattro */
1418 NULL, /* setattro */
1419 NULL, /* as_buffer */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */
1421 ssl_Context_doc, /* tp_doc */
1422 (traverseproc)ssl_Context_traverse, /* tp_traverse */
1423 (inquiry)ssl_Context_clear, /* tp_clear */
1424 NULL, /* tp_richcompare */
1425 0, /* tp_weaklistoffset */
1426 NULL, /* tp_iter */
1427 NULL, /* tp_iternext */
1428 ssl_Context_methods, /* tp_methods */
1429 NULL, /* tp_members */
1430 NULL, /* tp_getset */
1431 NULL, /* tp_base */
1432 NULL, /* tp_dict */
1433 NULL, /* tp_descr_get */
1434 NULL, /* tp_descr_set */
1435 0, /* tp_dictoffset */
1436 NULL, /* tp_init */
1437 NULL, /* tp_alloc */
1438 ssl_Context_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001439};
1440
1441
1442/*
1443 * Initialize the Context part of the SSL sub module
1444 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001445 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001446 * Returns: 1 for success, 0 otherwise
1447 */
1448int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001449init_ssl_context(PyObject *module) {
1450
1451 if (PyType_Ready(&ssl_Context_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001452 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001453 }
1454
Jean-Paul Calderone86ad7112010-05-11 16:08:45 -04001455 /* PyModule_AddObject steals a reference.
1456 */
1457 Py_INCREF((PyObject *)&ssl_Context_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001458 if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) {
1459 return 0;
1460 }
1461
Jean-Paul Calderoneaed23582011-03-12 22:45:02 -05001462 /* PyModule_AddObject steals a reference.
1463 */
1464 Py_INCREF((PyObject *)&ssl_Context_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001465 if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) {
1466 return 0;
1467 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001468
1469 return 1;
1470}
1471