blob: 0cb483dd834005963040e5de06663438a618e5c0 [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\
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500693Set the session identifier. This is needed if you want to do session\n\
694resumption.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500695\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\
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500723:param mode: One or more of the SESS_CACHE_* flags (combine using bitwise or)\n\
724:returns: The previously set caching mode.\n\
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500725";
726static PyObject *
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500727ssl_Context_set_session_cache_mode(ssl_ContextObj *self, PyObject *args) {
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500728 long mode, result;
729
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500730 if (!PyArg_ParseTuple(args, "l:set_session_cache_mode", &mode)) {
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500731 return NULL;
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500732 }
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500733
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\
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500740:returns: The currently used cache mode.\n\
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500741";
742static PyObject *
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500743ssl_Context_get_session_cache_mode(ssl_ContextObj *self, PyObject *args) {
744 long result;
745
746 if (!PyArg_ParseTuple(args, ":get_session_cache_mode")) {
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500747 return NULL;
Jean-Paul Calderone5f671352012-02-08 13:07:47 -0500748 }
749 result = SSL_CTX_get_session_cache_mode(self->ctx);
750 return PyLong_FromLong(result);
Jean-Paul Calderone313bf012012-02-08 13:02:49 -0500751}
752
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500753static char ssl_Context_set_verify_doc[] = "\n\
754Set the verify mode and verify callback\n\
755\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900756:param mode: The verify mode, this is either VERIFY_NONE or\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400757 VERIFY_PEER combined with possible other flags\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900758:param callback: The Python callback to use\n\
759:return: None\n\
Jean-Paul Calderone24aedf42008-03-06 22:01:16 -0500760\n\
761See SSL_CTX_set_verify(3SSL) for further details.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500762";
763static PyObject *
764ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args)
765{
766 int mode;
767 PyObject *callback = NULL;
768
769 if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback))
770 return NULL;
771
772 if (!PyCallable_Check(callback))
773 {
774 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
775 return NULL;
776 }
777
778 Py_DECREF(self->verify_callback);
779 Py_INCREF(callback);
780 self->verify_callback = callback;
781 SSL_CTX_set_verify(self->ctx, mode, global_verify_callback);
782
783 Py_INCREF(Py_None);
784 return Py_None;
785}
786
787static char ssl_Context_set_verify_depth_doc[] = "\n\
788Set the verify depth\n\
789\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900790:param depth: An integer specifying the verify depth\n\
791:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500792";
793static PyObject *
794ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args)
795{
796 int depth;
797
798 if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth))
799 return NULL;
800
801 SSL_CTX_set_verify_depth(self->ctx, depth);
802 Py_INCREF(Py_None);
803 return Py_None;
804}
805
806static char ssl_Context_get_verify_mode_doc[] = "\n\
807Get the verify mode\n\
808\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900809:return: The verify mode\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500810";
811static PyObject *
812ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args)
813{
814 int mode;
815
816 if (!PyArg_ParseTuple(args, ":get_verify_mode"))
817 return NULL;
818
819 mode = SSL_CTX_get_verify_mode(self->ctx);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400820 return PyLong_FromLong((long)mode);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500821}
822
823static char ssl_Context_get_verify_depth_doc[] = "\n\
824Get the verify depth\n\
825\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900826:return: The verify depth\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500827";
828static PyObject *
829ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args)
830{
831 int depth;
832
833 if (!PyArg_ParseTuple(args, ":get_verify_depth"))
834 return NULL;
835
836 depth = SSL_CTX_get_verify_depth(self->ctx);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400837 return PyLong_FromLong((long)depth);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500838}
839
840static char ssl_Context_load_tmp_dh_doc[] = "\n\
841Load parameters for Ephemeral Diffie-Hellman\n\
842\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900843:param dhfile: The file to load EDH parameters from\n\
844:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500845";
846static PyObject *
847ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args)
848{
849 char *dhfile;
850 BIO *bio;
851 DH *dh;
852
853 if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile))
854 return NULL;
855
856 bio = BIO_new_file(dhfile, "r");
Jean-Paul Calderone6ace4782010-09-09 18:43:40 -0400857 if (bio == NULL) {
858 exception_from_error_queue(ssl_Error);
859 return NULL;
860 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500861
862 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
863 SSL_CTX_set_tmp_dh(self->ctx, dh);
864 DH_free(dh);
865 BIO_free(bio);
866
867 Py_INCREF(Py_None);
868 return Py_None;
869}
870
871static char ssl_Context_set_cipher_list_doc[] = "\n\
872Change the cipher list\n\
873\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900874:param cipher_list: A cipher list, see ciphers(1)\n\
875:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500876";
877static PyObject *
878ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args)
879{
880 char *cipher_list;
881
882 if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list))
883 return NULL;
884
885 if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list))
886 {
Rick Deand369c932009-07-08 11:48:33 -0500887 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500888 return NULL;
889 }
890 else
891 {
892 Py_INCREF(Py_None);
893 return Py_None;
894 }
895}
896
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200897static char ssl_Context_set_client_ca_list_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200898Set the list of preferred client certificate signers for this server context.\n\
899\n\
900This list of certificate authorities will be sent to the client when the\n\
901server requests a client certificate.\n\
902\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900903:param certificate_authorities: a sequence of X509Names.\n\
904:return: None\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200905";
906
907static PyObject *
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200908ssl_Context_set_client_ca_list(ssl_ContextObj *self, PyObject *args)
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200909{
910 static PyTypeObject *X509NameType;
911 PyObject *sequence, *tuple, *item;
912 crypto_X509NameObj *name;
913 X509_NAME *sslname;
914 STACK_OF(X509_NAME) *CANames;
915 Py_ssize_t length;
916 int i;
917
918 if (X509NameType == NULL) {
919 X509NameType = import_crypto_type("X509Name", sizeof(crypto_X509NameObj));
920 if (X509NameType == NULL) {
921 return NULL;
922 }
923 }
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200924 if (!PyArg_ParseTuple(args, "O:set_client_ca_list", &sequence)) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200925 return NULL;
926 }
927 tuple = PySequence_Tuple(sequence);
928 if (tuple == NULL) {
929 return NULL;
930 }
931 length = PyTuple_Size(tuple);
932 if (length >= INT_MAX) {
933 PyErr_SetString(PyExc_ValueError, "client CA list is too long");
934 Py_DECREF(tuple);
935 return NULL;
936 }
937 CANames = sk_X509_NAME_new_null();
938 if (CANames == NULL) {
939 Py_DECREF(tuple);
940 exception_from_error_queue(ssl_Error);
941 return NULL;
942 }
943 for (i = 0; i < length; i++) {
944 item = PyTuple_GetItem(tuple, i);
945 if (item->ob_type != X509NameType) {
946 PyErr_Format(PyExc_TypeError,
947 "client CAs must be X509Name objects, not %s objects",
948 item->ob_type->tp_name);
949 sk_X509_NAME_free(CANames);
950 Py_DECREF(tuple);
951 return NULL;
952 }
953 name = (crypto_X509NameObj *)item;
954 sslname = X509_NAME_dup(name->x509_name);
955 if (sslname == NULL) {
956 sk_X509_NAME_free(CANames);
957 Py_DECREF(tuple);
958 exception_from_error_queue(ssl_Error);
959 return NULL;
960 }
961 if (!sk_X509_NAME_push(CANames, sslname)) {
962 X509_NAME_free(sslname);
963 sk_X509_NAME_free(CANames);
964 Py_DECREF(tuple);
965 exception_from_error_queue(ssl_Error);
966 return NULL;
967 }
968 }
969 Py_DECREF(tuple);
970 SSL_CTX_set_client_CA_list(self->ctx, CANames);
971 Py_INCREF(Py_None);
972 return Py_None;
973}
974
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200975static char ssl_Context_add_client_ca_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200976Add the CA certificate to the list of preferred signers for this context.\n\
977\n\
978The list of certificate authorities will be sent to the client when the\n\
979server requests a client certificate.\n\
980\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900981:param certificate_authority: certificate authority's X509 certificate.\n\
982:return: None\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200983";
984
985static PyObject *
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200986ssl_Context_add_client_ca(ssl_ContextObj *self, PyObject *args)
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200987{
988 crypto_X509Obj *cert;
989
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200990 cert = parse_certificate_argument("O!:add_client_ca", args);
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200991 if (cert == NULL) {
992 return NULL;
993 }
994 if (!SSL_CTX_add_client_CA(self->ctx, cert->x509)) {
995 exception_from_error_queue(ssl_Error);
996 return NULL;
997 }
998 Py_INCREF(Py_None);
999 return Py_None;
1000}
1001
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001002static char ssl_Context_set_timeout_doc[] = "\n\
1003Set session timeout\n\
1004\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001005:param timeout: The timeout in seconds\n\
1006:return: The previous session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001007";
1008static PyObject *
1009ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args)
1010{
1011 long t, ret;
1012
1013 if (!PyArg_ParseTuple(args, "l:set_timeout", &t))
1014 return NULL;
1015
1016 ret = SSL_CTX_set_timeout(self->ctx, t);
1017 return PyLong_FromLong(ret);
1018}
1019
1020static char ssl_Context_get_timeout_doc[] = "\n\
1021Get the session timeout\n\
1022\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001023:return: The session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001024";
1025static PyObject *
1026ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args)
1027{
1028 long ret;
1029
1030 if (!PyArg_ParseTuple(args, ":get_timeout"))
1031 return NULL;
1032
1033 ret = SSL_CTX_get_timeout(self->ctx);
1034 return PyLong_FromLong(ret);
1035}
1036
1037static char ssl_Context_set_info_callback_doc[] = "\n\
1038Set the info callback\n\
1039\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001040:param callback: The Python callback to use\n\
1041:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001042";
1043static PyObject *
1044ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args)
1045{
1046 PyObject *callback;
1047
1048 if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback))
1049 return NULL;
1050
1051 if (!PyCallable_Check(callback))
1052 {
1053 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
1054 return NULL;
1055 }
1056
1057 Py_DECREF(self->info_callback);
1058 Py_INCREF(callback);
1059 self->info_callback = callback;
1060 SSL_CTX_set_info_callback(self->ctx, global_info_callback);
1061
1062 Py_INCREF(Py_None);
1063 return Py_None;
1064}
1065
1066static char ssl_Context_get_app_data_doc[] = "\n\
1067Get the application data (supplied via set_app_data())\n\
1068\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001069:return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001070";
1071static PyObject *
1072ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args)
1073{
1074 if (!PyArg_ParseTuple(args, ":get_app_data"))
1075 return NULL;
1076
1077 Py_INCREF(self->app_data);
1078 return self->app_data;
1079}
1080
1081static char ssl_Context_set_app_data_doc[] = "\n\
1082Set the application data (will be returned from get_app_data())\n\
1083\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001084:param data: Any Python object\n\
1085:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001086";
1087static PyObject *
1088ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args)
1089{
1090 PyObject *data;
1091
1092 if (!PyArg_ParseTuple(args, "O:set_app_data", &data))
1093 return NULL;
1094
1095 Py_DECREF(self->app_data);
1096 Py_INCREF(data);
1097 self->app_data = data;
1098
1099 Py_INCREF(Py_None);
1100 return Py_None;
1101}
1102
1103static char ssl_Context_get_cert_store_doc[] = "\n\
1104Get the certificate store for the context\n\
1105\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001106:return: A X509Store object\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001107";
1108static PyObject *
1109ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args)
1110{
1111 X509_STORE *store;
1112
1113 if (!PyArg_ParseTuple(args, ":get_cert_store"))
1114 return NULL;
1115
1116 if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL)
1117 {
1118 Py_INCREF(Py_None);
1119 return Py_None;
1120 }
1121 else
1122 {
Jean-Paul Calderone1e9312e2010-10-31 21:26:18 -04001123 return (PyObject *)new_x509store(store, 0);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001124 }
1125}
1126
1127static char ssl_Context_set_options_doc[] = "\n\
1128Add options. Options set before are not cleared!\n\
1129\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001130:param options: The options to add.\n\
1131:return: The new option bitmask.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001132";
1133static PyObject *
1134ssl_Context_set_options(ssl_ContextObj *self, PyObject *args)
1135{
1136 long options;
1137
1138 if (!PyArg_ParseTuple(args, "l:set_options", &options))
1139 return NULL;
1140
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001141 return PyLong_FromLong(SSL_CTX_set_options(self->ctx, options));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001142}
1143
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001144static char ssl_Context_set_mode_doc[] = "\n\
1145Add modes via bitmask. Modes set before are not cleared!\n\
1146\n\
Jean-Paul Calderone975a64a2011-09-11 09:35:32 -04001147:param mode: The mode to add.\n\
1148:return: The new mode bitmask.\n\
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001149";
1150static PyObject *
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001151ssl_Context_set_mode(ssl_ContextObj *self, PyObject *args) {
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001152 long mode;
1153
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001154 if (!PyArg_ParseTuple(args, "l:set_mode", &mode)) {
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001155 return NULL;
Jean-Paul Calderone59add692011-09-08 18:41:09 -04001156 }
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001157
1158 return PyLong_FromLong(SSL_CTX_set_mode(self->ctx, mode));
1159}
1160
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001161static char ssl_Context_set_tlsext_servername_callback_doc[] = "\n\
1162Specify a callback function to be called when clients specify a server name.\n\
1163\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001164:param callback: The callback function. It will be invoked with one\n\
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001165 argument, the Connection instance.\n\
1166\n\
1167";
1168static PyObject *
1169ssl_Context_set_tlsext_servername_callback(ssl_ContextObj *self, PyObject *args) {
1170 PyObject *callback;
1171 PyObject *old;
1172
1173 if (!PyArg_ParseTuple(args, "O:set_tlsext_servername_callback", &callback)) {
1174 return NULL;
1175 }
1176
1177 Py_INCREF(callback);
1178 old = self->tlsext_servername_callback;
1179 self->tlsext_servername_callback = callback;
1180 Py_DECREF(old);
1181
1182 SSL_CTX_set_tlsext_servername_callback(self->ctx, global_tlsext_servername_callback);
1183 SSL_CTX_set_tlsext_servername_arg(self->ctx, NULL);
1184
1185 Py_INCREF(Py_None);
1186 return Py_None;
1187}
1188
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001189
1190/*
1191 * Member methods in the Context object
1192 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
1193 * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS }
1194 * for convenience
1195 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real
1196 * function with the name 'name'
1197 */
1198#define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc }
1199static PyMethodDef ssl_Context_methods[] = {
1200 ADD_METHOD(load_verify_locations),
1201 ADD_METHOD(set_passwd_cb),
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -04001202 ADD_METHOD(set_default_verify_paths),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001203 ADD_METHOD(use_certificate_chain_file),
1204 ADD_METHOD(use_certificate_file),
1205 ADD_METHOD(use_certificate),
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -05001206 ADD_METHOD(add_extra_chain_cert),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001207 ADD_METHOD(use_privatekey_file),
1208 ADD_METHOD(use_privatekey),
1209 ADD_METHOD(check_privatekey),
1210 ADD_METHOD(load_client_ca),
1211 ADD_METHOD(set_session_id),
Jean-Paul Calderone313bf012012-02-08 13:02:49 -05001212 ADD_METHOD(set_session_cache_mode),
1213 ADD_METHOD(get_session_cache_mode),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001214 ADD_METHOD(set_verify),
1215 ADD_METHOD(set_verify_depth),
1216 ADD_METHOD(get_verify_mode),
1217 ADD_METHOD(get_verify_depth),
1218 ADD_METHOD(load_tmp_dh),
1219 ADD_METHOD(set_cipher_list),
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001220 ADD_METHOD(set_client_ca_list),
1221 ADD_METHOD(add_client_ca),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001222 ADD_METHOD(set_timeout),
1223 ADD_METHOD(get_timeout),
1224 ADD_METHOD(set_info_callback),
1225 ADD_METHOD(get_app_data),
1226 ADD_METHOD(set_app_data),
1227 ADD_METHOD(get_cert_store),
1228 ADD_METHOD(set_options),
Guillermo Gonzalez74a2c292011-08-29 16:16:58 -03001229 ADD_METHOD(set_mode),
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001230 ADD_METHOD(set_tlsext_servername_callback),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001231 { NULL, NULL }
1232};
1233#undef ADD_METHOD
1234
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001235/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001236 * Despite the name which might suggest otherwise, this is not the tp_init for
1237 * the Context type. It's just the common initialization code shared by the
1238 * two _{Nn}ew functions below.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001239 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001240static ssl_ContextObj*
1241ssl_Context_init(ssl_ContextObj *self, int i_method) {
Jean-Paul Calderone1c198f92011-04-14 09:46:23 -04001242#if (OPENSSL_VERSION_NUMBER >> 28) == 0x01
1243 const
1244#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001245 SSL_METHOD *method;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001246
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001247 switch (i_method) {
1248 case ssl_SSLv2_METHOD:
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -04001249#ifdef OPENSSL_NO_SSL2
1250 PyErr_SetString(PyExc_ValueError, "SSLv2_METHOD not supported by this version of OpenSSL");
1251 return NULL;
1252#else
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001253 method = SSLv2_method();
Jean-Paul Calderone9f2e38e2011-04-14 09:36:55 -04001254#endif
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001255 break;
1256 case ssl_SSLv23_METHOD:
1257 method = SSLv23_method();
1258 break;
1259 case ssl_SSLv3_METHOD:
1260 method = SSLv3_method();
1261 break;
1262 case ssl_TLSv1_METHOD:
1263 method = TLSv1_method();
1264 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001265 default:
1266 PyErr_SetString(PyExc_ValueError, "No such protocol");
1267 return NULL;
1268 }
1269
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001270 self->ctx = SSL_CTX_new(method);
1271 Py_INCREF(Py_None);
1272 self->passphrase_callback = Py_None;
1273 Py_INCREF(Py_None);
1274 self->verify_callback = Py_None;
1275 Py_INCREF(Py_None);
1276 self->info_callback = Py_None;
1277
1278 Py_INCREF(Py_None);
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001279 self->tlsext_servername_callback = Py_None;
1280
1281 Py_INCREF(Py_None);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001282 self->passphrase_userdata = Py_None;
1283
1284 Py_INCREF(Py_None);
1285 self->app_data = Py_None;
1286
1287 /* Some initialization that's required to operate smoothly in Python */
1288 SSL_CTX_set_app_data(self->ctx, self);
1289 SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
1290 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1291 SSL_MODE_AUTO_RETRY);
1292
1293 self->tstate = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001294
1295 return self;
1296}
1297
1298/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001299 * This one is exposed in the CObject API. I want to deprecate it.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001300 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001301ssl_ContextObj*
1302ssl_Context_New(int i_method) {
1303 ssl_ContextObj *self;
1304
1305 self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type);
1306 if (self == NULL) {
1307 return (ssl_ContextObj *)PyErr_NoMemory();
1308 }
1309 self = ssl_Context_init(self, i_method);
1310 PyObject_GC_Track((PyObject *)self);
1311 return self;
1312}
1313
1314
1315/*
1316 * This one is the tp_new of the Context type. It's great.
1317 */
1318static PyObject*
1319ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
1320 int i_method;
1321 ssl_ContextObj *self;
1322 static char *kwlist[] = {"method", NULL};
1323
1324 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) {
1325 return NULL;
1326 }
1327
1328 self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1);
1329 if (self == NULL) {
1330 return NULL;
1331 }
1332
1333 return (PyObject *)ssl_Context_init(self, i_method);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001334}
1335
1336/*
1337 * Call the visitproc on all contained objects.
1338 *
1339 * Arguments: self - The Context object
1340 * visit - Function to call
1341 * arg - Extra argument to visit
1342 * Returns: 0 if all goes well, otherwise the return code from the first
1343 * call that gave non-zero result.
1344 */
1345static int
1346ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg)
1347{
1348 int ret = 0;
1349
1350 if (ret == 0 && self->passphrase_callback != NULL)
1351 ret = visit((PyObject *)self->passphrase_callback, arg);
1352 if (ret == 0 && self->passphrase_userdata != NULL)
1353 ret = visit((PyObject *)self->passphrase_userdata, arg);
1354 if (ret == 0 && self->verify_callback != NULL)
1355 ret = visit((PyObject *)self->verify_callback, arg);
1356 if (ret == 0 && self->info_callback != NULL)
1357 ret = visit((PyObject *)self->info_callback, arg);
1358 if (ret == 0 && self->app_data != NULL)
1359 ret = visit(self->app_data, arg);
1360 return ret;
1361}
1362
1363/*
1364 * Decref all contained objects and zero the pointers.
1365 *
1366 * Arguments: self - The Context object
1367 * Returns: Always 0.
1368 */
1369static int
1370ssl_Context_clear(ssl_ContextObj *self)
1371{
1372 Py_XDECREF(self->passphrase_callback);
1373 self->passphrase_callback = NULL;
1374 Py_XDECREF(self->passphrase_userdata);
1375 self->passphrase_userdata = NULL;
1376 Py_XDECREF(self->verify_callback);
1377 self->verify_callback = NULL;
1378 Py_XDECREF(self->info_callback);
1379 self->info_callback = NULL;
1380 Py_XDECREF(self->app_data);
1381 self->app_data = NULL;
1382 return 0;
1383}
1384
1385/*
1386 * Deallocate the memory used by the Context object
1387 *
1388 * Arguments: self - The Context object
1389 * Returns: None
1390 */
1391static void
1392ssl_Context_dealloc(ssl_ContextObj *self)
1393{
1394 PyObject_GC_UnTrack((PyObject *)self);
1395 SSL_CTX_free(self->ctx);
1396 ssl_Context_clear(self);
1397 PyObject_GC_Del(self);
1398}
1399
1400
1401PyTypeObject ssl_Context_Type = {
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -04001402 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001403 "OpenSSL.SSL.Context",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001404 sizeof(ssl_ContextObj),
1405 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001406 (destructor)ssl_Context_dealloc, /* tp_dealloc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001407 NULL, /* print */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001408 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001409 NULL, /* setattr */
1410 NULL, /* compare */
1411 NULL, /* repr */
1412 NULL, /* as_number */
1413 NULL, /* as_sequence */
1414 NULL, /* as_mapping */
1415 NULL, /* hash */
1416 NULL, /* call */
1417 NULL, /* str */
1418 NULL, /* getattro */
1419 NULL, /* setattro */
1420 NULL, /* as_buffer */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001421 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */
1422 ssl_Context_doc, /* tp_doc */
1423 (traverseproc)ssl_Context_traverse, /* tp_traverse */
1424 (inquiry)ssl_Context_clear, /* tp_clear */
1425 NULL, /* tp_richcompare */
1426 0, /* tp_weaklistoffset */
1427 NULL, /* tp_iter */
1428 NULL, /* tp_iternext */
1429 ssl_Context_methods, /* tp_methods */
1430 NULL, /* tp_members */
1431 NULL, /* tp_getset */
1432 NULL, /* tp_base */
1433 NULL, /* tp_dict */
1434 NULL, /* tp_descr_get */
1435 NULL, /* tp_descr_set */
1436 0, /* tp_dictoffset */
1437 NULL, /* tp_init */
1438 NULL, /* tp_alloc */
1439 ssl_Context_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001440};
1441
1442
1443/*
1444 * Initialize the Context part of the SSL sub module
1445 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001446 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001447 * Returns: 1 for success, 0 otherwise
1448 */
1449int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001450init_ssl_context(PyObject *module) {
1451
1452 if (PyType_Ready(&ssl_Context_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001453 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001454 }
1455
Jean-Paul Calderone86ad7112010-05-11 16:08:45 -04001456 /* PyModule_AddObject steals a reference.
1457 */
1458 Py_INCREF((PyObject *)&ssl_Context_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001459 if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) {
1460 return 0;
1461 }
1462
Jean-Paul Calderoneaed23582011-03-12 22:45:02 -05001463 /* PyModule_AddObject steals a reference.
1464 */
1465 Py_INCREF((PyObject *)&ssl_Context_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001466 if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) {
1467 return 0;
1468 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001469
1470 return 1;
1471}
1472