blob: cb9aa8860be8186d9e5293dd4f8b38a9c12a5478 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * context.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04005 * Copyright (C) Jean-Paul Calderone 2008, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
7 * SSL Context objects and their methods.
8 * See the file RATIONALE for a short explanation of why this module was written.
9 *
10 * Reviewed 2001-07-23
11 */
12#include <Python.h>
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050013
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -050014#if PY_VERSION_HEX >= 0x02050000
15# define PYARG_PARSETUPLE_FORMAT const char
16#else
17# define PYARG_PARSETUPLE_FORMAT char
18#endif
19
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050020#ifndef MS_WINDOWS
21# include <sys/socket.h>
22# include <netinet/in.h>
23# if !(defined(__BEOS__) || defined(__CYGWIN__))
24# include <netinet/tcp.h>
25# endif
26#else
27# include <winsock.h>
28# include <wincrypt.h>
29#endif
30
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050031#define SSL_MODULE
32#include "ssl.h"
33
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050034/*
35 * CALLBACKS
36 *
37 * Callbacks work like this: We provide a "global" callback in C which
38 * transforms the arguments into a Python argument tuple and calls the
39 * corresponding Python callback, and then parsing the return value back into
40 * things the C function can return.
41 *
42 * Three caveats:
43 * + How do we find the Context object where the Python callbacks are stored?
44 * + What about multithreading and execution frames?
45 * + What about Python callbacks that raise exceptions?
46 *
47 * The solution to the first issue is trivial if the callback provides
48 * "userdata" functionality. Since the only callbacks that don't provide
49 * userdata do provide a pointer to an SSL structure, we can associate an SSL
50 * object and a Connection one-to-one via the SSL_set/get_app_data()
51 * functions.
52 *
53 * The solution to the other issue is to rewrite the Py_BEGIN_ALLOW_THREADS
54 * macro allowing it (or rather a new macro) to specify where to save the
55 * thread state (in our case, as a member of the Connection/Context object) so
56 * we can retrieve it again before calling the Python callback.
57 */
58
59/*
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040060 * Globally defined passphrase callback. This is called from OpenSSL
61 * internally. The GIL will not be held when this function is invoked. It
62 * must not be held when the function returns.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050063 *
64 * Arguments: buf - Buffer to store the returned passphrase in
65 * maxlen - Maximum length of the passphrase
66 * verify - If true, the passphrase callback should ask for a
67 * password twice and verify they're equal. If false, only
68 * ask once.
69 * arg - User data, always a Context object
70 * Returns: The length of the password if successful, 0 otherwise
71 */
72static int
73global_passphrase_callback(char *buf, int maxlen, int verify, void *arg)
74{
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040075 /*
76 * Initialize len here because we're always going to return it, and we
77 * might jump to the return before it gets initialized in any other way.
78 */
79 int len = 0;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050080 char *str;
81 PyObject *argv, *ret = NULL;
82 ssl_ContextObj *ctx = (ssl_ContextObj *)arg;
83
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040084 /*
85 * GIL isn't held yet. First things first - acquire it, or any Python API
86 * we invoke might segfault or blow up the sun. The reverse will be done
87 * before returning.
88 */
89 MY_END_ALLOW_THREADS(ctx->tstate);
90
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -040091 /* The Python callback is called with a (maxlen,verify,userdata) tuple */
92 argv = Py_BuildValue("(iiO)", maxlen, verify, ctx->passphrase_userdata);
93
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -040094 /*
95 * XXX Didn't check argv to see if it was NULL. -exarkun
96 */
97 ret = PyEval_CallObject(ctx->passphrase_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050098 Py_DECREF(argv);
99
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400100 if (ret == NULL) {
101 /*
102 * XXX The callback raised an exception. At the very least, it should
103 * be printed out here. An *actual* solution would be to raise it up
104 * through OpenSSL. That might be a bit tricky, but it's probably
105 * possible. -exarkun
106 */
107 goto out;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500108 }
109
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400110 if (!PyObject_IsTrue(ret)) {
111 /*
112 * Returned "", or None, or something. Treat it as no passphrase.
113 */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500114 Py_DECREF(ret);
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400115 goto out;
116 }
117
118 if (!PyString_Check(ret)) {
119 /*
120 * XXX Returned something that wasn't a string. This is bogus. We
121 * should report an error or raise an exception (again, through OpenSSL
122 * - tricky). -exarkun
123 */
124 Py_DECREF(ret);
125 goto out;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500126 }
127
128 len = PyString_Size(ret);
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400129 if (len > maxlen) {
130 /*
131 * XXX Returned more than we said they were allowed to return. Report
132 * an error or raise an exception (tricky blah blah). -exarkun
133 */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500134 len = maxlen;
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400135 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500136
137 str = PyString_AsString(ret);
138 strncpy(buf, str, len);
139 Py_XDECREF(ret);
140
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -0400141 out:
142 /*
143 * This function is returning into OpenSSL. Release the GIL again.
144 */
145 MY_BEGIN_ALLOW_THREADS(ctx->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500146 return len;
147}
148
149/*
150 * Globally defined verify callback
151 *
152 * Arguments: ok - True everything is OK "so far", false otherwise
153 * x509_ctx - Contains the certificate being checked, the current
154 * error number and depth, and the Connection we're
155 * dealing with
156 * Returns: True if everything is okay, false otherwise
157 */
158static int
159global_verify_callback(int ok, X509_STORE_CTX *x509_ctx)
160{
161 PyObject *argv, *ret;
162 SSL *ssl;
163 ssl_ConnectionObj *conn;
164 crypto_X509Obj *cert;
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500165 int errnum, errdepth, c_ret;
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400166
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400167 // Get Connection object to check thread state
168 ssl = (SSL *)X509_STORE_CTX_get_app_data(x509_ctx);
169 conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400170
Jean-Paul Calderone26aea022008-09-21 18:47:06 -0400171 MY_END_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400172
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500173 cert = crypto_X509_New(X509_STORE_CTX_get_current_cert(x509_ctx), 0);
174 errnum = X509_STORE_CTX_get_error(x509_ctx);
175 errdepth = X509_STORE_CTX_get_error_depth(x509_ctx);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400176
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500177 argv = Py_BuildValue("(OOiii)", (PyObject *)conn, (PyObject *)cert,
178 errnum, errdepth, ok);
179 Py_DECREF(cert);
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400180 ret = PyEval_CallObject(conn->context->verify_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500181 Py_DECREF(argv);
182
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400183 if (ret != NULL && PyObject_IsTrue(ret)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500184 X509_STORE_CTX_set_error(x509_ctx, X509_V_OK);
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400185 Py_DECREF(ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500186 c_ret = 1;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400187 } else {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500188 c_ret = 0;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400189 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500190
Jean-Paul Calderone26aea022008-09-21 18:47:06 -0400191 MY_BEGIN_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500192 return c_ret;
193}
194
195/*
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400196 * Globally defined info callback. This is called from OpenSSL internally.
197 * The GIL will not be held when this function is invoked. It must not be held
198 * when the function returns.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500199 *
200 * Arguments: ssl - The Connection
201 * where - The part of the SSL code that called us
202 * _ret - The return code of the SSL function that called us
203 * Returns: None
204 */
205static void
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500206global_info_callback(const SSL *ssl, int where, int _ret)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500207{
208 ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl);
209 PyObject *argv, *ret;
210
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400211 /*
212 * GIL isn't held yet. First things first - acquire it, or any Python API
213 * we invoke might segfault or blow up the sun. The reverse will be done
214 * before returning.
215 */
216 MY_END_ALLOW_THREADS(conn->tstate);
217
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500218 argv = Py_BuildValue("(Oii)", (PyObject *)conn, where, _ret);
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400219 ret = PyEval_CallObject(conn->context->info_callback, argv);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500220 Py_DECREF(argv);
221
Jean-Paul Calderone5ef86512008-04-26 19:06:28 -0400222 if (ret == NULL) {
223 /*
224 * XXX - This should be reported somehow. -exarkun
225 */
226 PyErr_Clear();
227 } else {
228 Py_DECREF(ret);
229 }
230
231 /*
232 * This function is returning into OpenSSL. Release the GIL again.
233 */
234 MY_BEGIN_ALLOW_THREADS(conn->tstate);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500235 return;
236}
237
238
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400239static char ssl_Context_doc[] = "\n\
240Context(method) -> Context instance\n\
241\n\
242OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\
243connections.\n\
244\n\
245@param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or\n\
246 TLSv1_METHOD.\n\
247";
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500248
249static char ssl_Context_load_verify_locations_doc[] = "\n\
250Let SSL know where we can find trusted certificates for the certificate\n\
251chain\n\
252\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400253@param cafile: In which file we can find the certificates\n\
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400254@param capath: In which directory we can find the certificates\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400255@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500256";
257static PyObject *
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400258ssl_Context_load_verify_locations(ssl_ContextObj *self, PyObject *args) {
259 char *cafile = NULL;
260 char *capath = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500261
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400262 if (!PyArg_ParseTuple(args, "z|z:load_verify_locations", &cafile, &capath)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500263 return NULL;
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400264 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500265
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400266 if (!SSL_CTX_load_verify_locations(self->ctx, cafile, capath))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500267 {
268 exception_from_error_queue();
269 return NULL;
270 }
271 else
272 {
273 Py_INCREF(Py_None);
274 return Py_None;
275 }
276}
277
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400278static char ssl_Context_set_default_verify_paths_doc[] = "\n\
279Use the platform-specific CA certificate locations\n\
280\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400281@return: None\n\
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400282";
283static PyObject *
284ssl_Context_set_default_verify_paths(ssl_ContextObj *self, PyObject *args) {
Jean-Paul Calderone9eadb962008-09-07 21:20:44 -0400285 if (!PyArg_ParseTuple(args, ":set_default_verify_paths")) {
286 return NULL;
287 }
288
Jean-Paul Calderone286b1922008-09-07 21:35:38 -0400289 /*
290 * XXX Error handling for SSL_CTX_set_default_verify_paths is untested.
291 * -exarkun
292 */
293 if (!SSL_CTX_set_default_verify_paths(self->ctx)) {
294 exception_from_error_queue();
295 return NULL;
296 }
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400297 Py_INCREF(Py_None);
298 return Py_None;
299};
300
301
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500302static char ssl_Context_set_passwd_cb_doc[] = "\n\
303Set the passphrase callback\n\
304\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400305@param callback: The Python callback to use\n\
306@param userdata: (optional) A Python object which will be given as\n\
307 argument to the callback\n\
308@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500309";
310static PyObject *
311ssl_Context_set_passwd_cb(ssl_ContextObj *self, PyObject *args)
312{
313 PyObject *callback = NULL, *userdata = Py_None;
314
315 if (!PyArg_ParseTuple(args, "O|O:set_passwd_cb", &callback, &userdata))
316 return NULL;
317
318 if (!PyCallable_Check(callback))
319 {
320 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
321 return NULL;
322 }
323
324 Py_DECREF(self->passphrase_callback);
325 Py_INCREF(callback);
326 self->passphrase_callback = callback;
327 SSL_CTX_set_default_passwd_cb(self->ctx, global_passphrase_callback);
328
329 Py_DECREF(self->passphrase_userdata);
330 Py_INCREF(userdata);
331 self->passphrase_userdata = userdata;
332 SSL_CTX_set_default_passwd_cb_userdata(self->ctx, (void *)self);
333
334 Py_INCREF(Py_None);
335 return Py_None;
336}
337
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500338static crypto_X509Obj *
339parse_certificate_argument(const char* format1, const char* format2, PyObject* args)
340{
341 static PyTypeObject *crypto_X509_type = NULL;
342 crypto_X509Obj *cert;
343
344 /* We need to check that cert really is an X509 object before
345 we deal with it. The problem is we can't just quickly verify
346 the type (since that comes from another module). This should
347 do the trick (reasonably well at least): Once we have one
348 verified object, we use it's type object for future
349 comparisons. */
350
351 if (!crypto_X509_type)
352 {
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500353 if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format1, &cert))
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500354 return NULL;
355
356 if (strcmp(cert->ob_type->tp_name, "X509") != 0 ||
357 cert->ob_type->tp_basicsize != sizeof(crypto_X509Obj))
358 {
359 PyErr_SetString(PyExc_TypeError, "Expected an X509 object");
360 return NULL;
361 }
362
363 crypto_X509_type = cert->ob_type;
364 }
365 else
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500366 if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format2, crypto_X509_type,
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500367 &cert))
368 return NULL;
369 return cert;
370}
371
372static char ssl_Context_add_extra_chain_cert_doc[] = "\n\
373Add certificate to chain\n\
374\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400375@param certobj: The X509 certificate object to add to the chain\n\
376@return: None\n\
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500377";
378
379static PyObject *
380ssl_Context_add_extra_chain_cert(ssl_ContextObj *self, PyObject *args)
381{
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500382 X509* cert_original;
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500383 crypto_X509Obj *cert = parse_certificate_argument(
384 "O:add_extra_chain_cert", "O!:add_extra_chain_cert", args);
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500385 if (cert == NULL)
386 {
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500387 return NULL;
388 }
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500389 if (!(cert_original = X509_dup(cert->x509)))
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500390 {
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500391 /* exception_from_error_queue(); */
392 PyErr_SetString(PyExc_RuntimeError, "X509_dup failed");
393 return NULL;
394 }
395 if (!SSL_CTX_add_extra_chain_cert(self->ctx, cert_original))
396 {
397 X509_free(cert_original);
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500398 exception_from_error_queue();
399 return NULL;
400 }
401 else
402 {
403 Py_INCREF(Py_None);
404 return Py_None;
405 }
406}
407
408
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500409static char ssl_Context_use_certificate_chain_file_doc[] = "\n\
410Load a certificate chain from a file\n\
411\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400412@param certfile: The name of the certificate chain file\n\
413@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500414";
415static PyObject *
416ssl_Context_use_certificate_chain_file(ssl_ContextObj *self, PyObject *args)
417{
418 char *certfile;
419
420 if (!PyArg_ParseTuple(args, "s:use_certificate_chain_file", &certfile))
421 return NULL;
422
423 if (!SSL_CTX_use_certificate_chain_file(self->ctx, certfile))
424 {
425 exception_from_error_queue();
426 return NULL;
427 }
428 else
429 {
430 Py_INCREF(Py_None);
431 return Py_None;
432 }
433}
434
435
436static char ssl_Context_use_certificate_file_doc[] = "\n\
437Load a certificate from a file\n\
438\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400439@param certfile: The name of the certificate file\n\
440@param filetype: (optional) The encoding of the file, default is PEM\n\
441@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500442";
443static PyObject *
444ssl_Context_use_certificate_file(ssl_ContextObj *self, PyObject *args)
445{
446 char *certfile;
447 int filetype = SSL_FILETYPE_PEM;
448
449 if (!PyArg_ParseTuple(args, "s|i:use_certificate_file", &certfile, &filetype))
450 return NULL;
451
452 if (!SSL_CTX_use_certificate_file(self->ctx, certfile, filetype))
453 {
454 exception_from_error_queue();
455 return NULL;
456 }
457 else
458 {
459 Py_INCREF(Py_None);
460 return Py_None;
461 }
462}
463
464static char ssl_Context_use_certificate_doc[] = "\n\
465Load a certificate from a X509 object\n\
466\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400467@param cert: The X509 object\n\
468@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500469";
470static PyObject *
471ssl_Context_use_certificate(ssl_ContextObj *self, PyObject *args)
472{
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500473 crypto_X509Obj *cert = parse_certificate_argument(
474 "O:use_certificate", "O!:use_certificate", args);
475 if (cert == NULL) {
476 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500477 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500478
479 if (!SSL_CTX_use_certificate(self->ctx, cert->x509))
480 {
481 exception_from_error_queue();
482 return NULL;
483 }
484 else
485 {
486 Py_INCREF(Py_None);
487 return Py_None;
488 }
489}
490
491static char ssl_Context_use_privatekey_file_doc[] = "\n\
492Load a private key from a file\n\
493\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400494@param keyfile: The name of the key file\n\
495@param filetype: (optional) The encoding of the file, default is PEM\n\
496@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500497";
498static PyObject *
499ssl_Context_use_privatekey_file(ssl_ContextObj *self, PyObject *args)
500{
501 char *keyfile;
502 int filetype = SSL_FILETYPE_PEM, ret;
503
504 if (!PyArg_ParseTuple(args, "s|i:use_privatekey_file", &keyfile, &filetype))
505 return NULL;
506
507 MY_BEGIN_ALLOW_THREADS(self->tstate);
508 ret = SSL_CTX_use_PrivateKey_file(self->ctx, keyfile, filetype);
509 MY_END_ALLOW_THREADS(self->tstate);
510
511 if (PyErr_Occurred())
512 {
513 flush_error_queue();
514 return NULL;
515 }
516
517 if (!ret)
518 {
519 exception_from_error_queue();
520 return NULL;
521 }
522 else
523 {
524 Py_INCREF(Py_None);
525 return Py_None;
526 }
527}
528
529static char ssl_Context_use_privatekey_doc[] = "\n\
530Load a private key from a PKey object\n\
531\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400532@param pkey: The PKey object\n\
533@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500534";
535static PyObject *
536ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args)
537{
538 static PyTypeObject *crypto_PKey_type = NULL;
539 crypto_PKeyObj *pkey;
540
541 /* We need to check that cert really is a PKey object before
542 we deal with it. The problem is we can't just quickly verify
543 the type (since that comes from another module). This should
544 do the trick (reasonably well at least): Once we have one
545 verified object, we use it's type object for future
546 comparisons. */
547
548 if (!crypto_PKey_type)
549 {
550 if (!PyArg_ParseTuple(args, "O:use_privatekey", &pkey))
551 return NULL;
552
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400553 if (strcmp(pkey->ob_type->tp_name, "PKey") != 0 ||
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500554 pkey->ob_type->tp_basicsize != sizeof(crypto_PKeyObj))
555 {
556 PyErr_SetString(PyExc_TypeError, "Expected a PKey object");
557 return NULL;
558 }
559
560 crypto_PKey_type = pkey->ob_type;
561 }
562 else
563 if (!PyArg_ParseTuple(args, "O!:use_privatekey", crypto_PKey_type, &pkey))
564 return NULL;
565
566 if (!SSL_CTX_use_PrivateKey(self->ctx, pkey->pkey))
567 {
568 exception_from_error_queue();
569 return NULL;
570 }
571 else
572 {
573 Py_INCREF(Py_None);
574 return Py_None;
575 }
576}
577
578static char ssl_Context_check_privatekey_doc[] = "\n\
579Check that the private key and certificate match up\n\
580\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400581@return: None (raises an exception if something's wrong)\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500582";
583static PyObject *
584ssl_Context_check_privatekey(ssl_ContextObj *self, PyObject *args)
585{
586 if (!PyArg_ParseTuple(args, ":check_privatekey"))
587 return NULL;
588
589 if (!SSL_CTX_check_private_key(self->ctx))
590 {
591 exception_from_error_queue();
592 return NULL;
593 }
594 else
595 {
596 Py_INCREF(Py_None);
597 return Py_None;
598 }
599}
600
601static char ssl_Context_load_client_ca_doc[] = "\n\
602Load the trusted certificates that will be sent to the client (basically\n\
603telling the client \"These are the guys I trust\")\n\
604\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400605@param cafile: The name of the certificates file\n\
606@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500607";
608static PyObject *
609ssl_Context_load_client_ca(ssl_ContextObj *self, PyObject *args)
610{
611 char *cafile;
612
613 if (!PyArg_ParseTuple(args, "s:load_client_ca", &cafile))
614 return NULL;
615
616 SSL_CTX_set_client_CA_list(self->ctx, SSL_load_client_CA_file(cafile));
617
618 Py_INCREF(Py_None);
619 return Py_None;
620}
621
622static char ssl_Context_set_session_id_doc[] = "\n\
623Set the session identifier, this is needed if you want to do session\n\
624resumption (which, ironically, isn't implemented yet)\n\
625\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400626@param buf: A Python object that can be safely converted to a string\n\
627@returns: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500628";
629static PyObject *
630ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args)
631{
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500632 unsigned char *buf;
633 unsigned int len;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500634
635 if (!PyArg_ParseTuple(args, "s#:set_session_id", &buf, &len))
636 return NULL;
637
638 if (!SSL_CTX_set_session_id_context(self->ctx, buf, len))
639 {
640 exception_from_error_queue();
641 return NULL;
642 }
643 else
644 {
645 Py_INCREF(Py_None);
646 return Py_None;
647 }
648}
649
650static char ssl_Context_set_verify_doc[] = "\n\
651Set the verify mode and verify callback\n\
652\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400653@param mode: The verify mode, this is either VERIFY_NONE or\n\
654 VERIFY_PEER combined with possible other flags\n\
655@param callback: The Python callback to use\n\
656@return: None\n\
Jean-Paul Calderone24aedf42008-03-06 22:01:16 -0500657\n\
658See SSL_CTX_set_verify(3SSL) for further details.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500659";
660static PyObject *
661ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args)
662{
663 int mode;
664 PyObject *callback = NULL;
665
666 if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback))
667 return NULL;
668
669 if (!PyCallable_Check(callback))
670 {
671 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
672 return NULL;
673 }
674
675 Py_DECREF(self->verify_callback);
676 Py_INCREF(callback);
677 self->verify_callback = callback;
678 SSL_CTX_set_verify(self->ctx, mode, global_verify_callback);
679
680 Py_INCREF(Py_None);
681 return Py_None;
682}
683
684static char ssl_Context_set_verify_depth_doc[] = "\n\
685Set the verify depth\n\
686\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400687@param depth: An integer specifying the verify depth\n\
688@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500689";
690static PyObject *
691ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args)
692{
693 int depth;
694
695 if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth))
696 return NULL;
697
698 SSL_CTX_set_verify_depth(self->ctx, depth);
699 Py_INCREF(Py_None);
700 return Py_None;
701}
702
703static char ssl_Context_get_verify_mode_doc[] = "\n\
704Get the verify mode\n\
705\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400706@return: The verify mode\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500707";
708static PyObject *
709ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args)
710{
711 int mode;
712
713 if (!PyArg_ParseTuple(args, ":get_verify_mode"))
714 return NULL;
715
716 mode = SSL_CTX_get_verify_mode(self->ctx);
717 return PyInt_FromLong((long)mode);
718}
719
720static char ssl_Context_get_verify_depth_doc[] = "\n\
721Get the verify depth\n\
722\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400723@return: The verify depth\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500724";
725static PyObject *
726ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args)
727{
728 int depth;
729
730 if (!PyArg_ParseTuple(args, ":get_verify_depth"))
731 return NULL;
732
733 depth = SSL_CTX_get_verify_depth(self->ctx);
734 return PyInt_FromLong((long)depth);
735}
736
737static char ssl_Context_load_tmp_dh_doc[] = "\n\
738Load parameters for Ephemeral Diffie-Hellman\n\
739\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400740@param dhfile: The file to load EDH parameters from\n\
741@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500742";
743static PyObject *
744ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args)
745{
746 char *dhfile;
747 BIO *bio;
748 DH *dh;
749
750 if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile))
751 return NULL;
752
753 bio = BIO_new_file(dhfile, "r");
754 if (bio == NULL)
755 return PyErr_NoMemory();
756
757 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
758 SSL_CTX_set_tmp_dh(self->ctx, dh);
759 DH_free(dh);
760 BIO_free(bio);
761
762 Py_INCREF(Py_None);
763 return Py_None;
764}
765
766static char ssl_Context_set_cipher_list_doc[] = "\n\
767Change the cipher list\n\
768\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400769@param cipher_list: A cipher list, see ciphers(1)\n\
770@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500771";
772static PyObject *
773ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args)
774{
775 char *cipher_list;
776
777 if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list))
778 return NULL;
779
780 if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list))
781 {
782 exception_from_error_queue();
783 return NULL;
784 }
785 else
786 {
787 Py_INCREF(Py_None);
788 return Py_None;
789 }
790}
791
792static char ssl_Context_set_timeout_doc[] = "\n\
793Set session timeout\n\
794\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400795@param timeout: The timeout in seconds\n\
796@return: The previous session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500797";
798static PyObject *
799ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args)
800{
801 long t, ret;
802
803 if (!PyArg_ParseTuple(args, "l:set_timeout", &t))
804 return NULL;
805
806 ret = SSL_CTX_set_timeout(self->ctx, t);
807 return PyLong_FromLong(ret);
808}
809
810static char ssl_Context_get_timeout_doc[] = "\n\
811Get the session timeout\n\
812\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400813@return: The session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500814";
815static PyObject *
816ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args)
817{
818 long ret;
819
820 if (!PyArg_ParseTuple(args, ":get_timeout"))
821 return NULL;
822
823 ret = SSL_CTX_get_timeout(self->ctx);
824 return PyLong_FromLong(ret);
825}
826
827static char ssl_Context_set_info_callback_doc[] = "\n\
828Set the info callback\n\
829\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400830@param callback: The Python callback to use\n\
831@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500832";
833static PyObject *
834ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args)
835{
836 PyObject *callback;
837
838 if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback))
839 return NULL;
840
841 if (!PyCallable_Check(callback))
842 {
843 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
844 return NULL;
845 }
846
847 Py_DECREF(self->info_callback);
848 Py_INCREF(callback);
849 self->info_callback = callback;
850 SSL_CTX_set_info_callback(self->ctx, global_info_callback);
851
852 Py_INCREF(Py_None);
853 return Py_None;
854}
855
856static char ssl_Context_get_app_data_doc[] = "\n\
857Get the application data (supplied via set_app_data())\n\
858\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400859@return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500860";
861static PyObject *
862ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args)
863{
864 if (!PyArg_ParseTuple(args, ":get_app_data"))
865 return NULL;
866
867 Py_INCREF(self->app_data);
868 return self->app_data;
869}
870
871static char ssl_Context_set_app_data_doc[] = "\n\
872Set the application data (will be returned from get_app_data())\n\
873\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400874@param data: Any Python object\n\
875@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500876";
877static PyObject *
878ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args)
879{
880 PyObject *data;
881
882 if (!PyArg_ParseTuple(args, "O:set_app_data", &data))
883 return NULL;
884
885 Py_DECREF(self->app_data);
886 Py_INCREF(data);
887 self->app_data = data;
888
889 Py_INCREF(Py_None);
890 return Py_None;
891}
892
893static char ssl_Context_get_cert_store_doc[] = "\n\
894Get the certificate store for the context\n\
895\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400896@return: A X509Store object\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500897";
898static PyObject *
899ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args)
900{
901 X509_STORE *store;
902
903 if (!PyArg_ParseTuple(args, ":get_cert_store"))
904 return NULL;
905
906 if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL)
907 {
908 Py_INCREF(Py_None);
909 return Py_None;
910 }
911 else
912 {
913 return (PyObject *)crypto_X509Store_New(store, 0);
914 }
915}
916
917static char ssl_Context_set_options_doc[] = "\n\
918Add options. Options set before are not cleared!\n\
919\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400920@param options: The options to add.\n\
921@return: The new option bitmask.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500922";
923static PyObject *
924ssl_Context_set_options(ssl_ContextObj *self, PyObject *args)
925{
926 long options;
927
928 if (!PyArg_ParseTuple(args, "l:set_options", &options))
929 return NULL;
930
931 return PyInt_FromLong(SSL_CTX_set_options(self->ctx, options));
932}
933
934
935/*
936 * Member methods in the Context object
937 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
938 * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS }
939 * for convenience
940 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real
941 * function with the name 'name'
942 */
943#define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc }
944static PyMethodDef ssl_Context_methods[] = {
945 ADD_METHOD(load_verify_locations),
946 ADD_METHOD(set_passwd_cb),
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -0400947 ADD_METHOD(set_default_verify_paths),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500948 ADD_METHOD(use_certificate_chain_file),
949 ADD_METHOD(use_certificate_file),
950 ADD_METHOD(use_certificate),
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500951 ADD_METHOD(add_extra_chain_cert),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500952 ADD_METHOD(use_privatekey_file),
953 ADD_METHOD(use_privatekey),
954 ADD_METHOD(check_privatekey),
955 ADD_METHOD(load_client_ca),
956 ADD_METHOD(set_session_id),
957 ADD_METHOD(set_verify),
958 ADD_METHOD(set_verify_depth),
959 ADD_METHOD(get_verify_mode),
960 ADD_METHOD(get_verify_depth),
961 ADD_METHOD(load_tmp_dh),
962 ADD_METHOD(set_cipher_list),
963 ADD_METHOD(set_timeout),
964 ADD_METHOD(get_timeout),
965 ADD_METHOD(set_info_callback),
966 ADD_METHOD(get_app_data),
967 ADD_METHOD(set_app_data),
968 ADD_METHOD(get_cert_store),
969 ADD_METHOD(set_options),
970 { NULL, NULL }
971};
972#undef ADD_METHOD
973
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500974/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400975 * Despite the name which might suggest otherwise, this is not the tp_init for
976 * the Context type. It's just the common initialization code shared by the
977 * two _{Nn}ew functions below.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500978 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400979static ssl_ContextObj*
980ssl_Context_init(ssl_ContextObj *self, int i_method) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500981 SSL_METHOD *method;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500982
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400983 switch (i_method) {
984 case ssl_SSLv2_METHOD:
985 method = SSLv2_method();
986 break;
987 case ssl_SSLv23_METHOD:
988 method = SSLv23_method();
989 break;
990 case ssl_SSLv3_METHOD:
991 method = SSLv3_method();
992 break;
993 case ssl_TLSv1_METHOD:
994 method = TLSv1_method();
995 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500996 default:
997 PyErr_SetString(PyExc_ValueError, "No such protocol");
998 return NULL;
999 }
1000
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001001 self->ctx = SSL_CTX_new(method);
1002 Py_INCREF(Py_None);
1003 self->passphrase_callback = Py_None;
1004 Py_INCREF(Py_None);
1005 self->verify_callback = Py_None;
1006 Py_INCREF(Py_None);
1007 self->info_callback = Py_None;
1008
1009 Py_INCREF(Py_None);
1010 self->passphrase_userdata = Py_None;
1011
1012 Py_INCREF(Py_None);
1013 self->app_data = Py_None;
1014
1015 /* Some initialization that's required to operate smoothly in Python */
1016 SSL_CTX_set_app_data(self->ctx, self);
1017 SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
1018 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1019 SSL_MODE_AUTO_RETRY);
1020
1021 self->tstate = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001022
1023 return self;
1024}
1025
1026/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001027 * This one is exposed in the CObject API. I want to deprecate it.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001028 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001029ssl_ContextObj*
1030ssl_Context_New(int i_method) {
1031 ssl_ContextObj *self;
1032
1033 self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type);
1034 if (self == NULL) {
1035 return (ssl_ContextObj *)PyErr_NoMemory();
1036 }
1037 self = ssl_Context_init(self, i_method);
1038 PyObject_GC_Track((PyObject *)self);
1039 return self;
1040}
1041
1042
1043/*
1044 * This one is the tp_new of the Context type. It's great.
1045 */
1046static PyObject*
1047ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
1048 int i_method;
1049 ssl_ContextObj *self;
1050 static char *kwlist[] = {"method", NULL};
1051
1052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) {
1053 return NULL;
1054 }
1055
1056 self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1);
1057 if (self == NULL) {
1058 return NULL;
1059 }
1060
1061 return (PyObject *)ssl_Context_init(self, i_method);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001062}
1063
1064/*
1065 * Call the visitproc on all contained objects.
1066 *
1067 * Arguments: self - The Context object
1068 * visit - Function to call
1069 * arg - Extra argument to visit
1070 * Returns: 0 if all goes well, otherwise the return code from the first
1071 * call that gave non-zero result.
1072 */
1073static int
1074ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg)
1075{
1076 int ret = 0;
1077
1078 if (ret == 0 && self->passphrase_callback != NULL)
1079 ret = visit((PyObject *)self->passphrase_callback, arg);
1080 if (ret == 0 && self->passphrase_userdata != NULL)
1081 ret = visit((PyObject *)self->passphrase_userdata, arg);
1082 if (ret == 0 && self->verify_callback != NULL)
1083 ret = visit((PyObject *)self->verify_callback, arg);
1084 if (ret == 0 && self->info_callback != NULL)
1085 ret = visit((PyObject *)self->info_callback, arg);
1086 if (ret == 0 && self->app_data != NULL)
1087 ret = visit(self->app_data, arg);
1088 return ret;
1089}
1090
1091/*
1092 * Decref all contained objects and zero the pointers.
1093 *
1094 * Arguments: self - The Context object
1095 * Returns: Always 0.
1096 */
1097static int
1098ssl_Context_clear(ssl_ContextObj *self)
1099{
1100 Py_XDECREF(self->passphrase_callback);
1101 self->passphrase_callback = NULL;
1102 Py_XDECREF(self->passphrase_userdata);
1103 self->passphrase_userdata = NULL;
1104 Py_XDECREF(self->verify_callback);
1105 self->verify_callback = NULL;
1106 Py_XDECREF(self->info_callback);
1107 self->info_callback = NULL;
1108 Py_XDECREF(self->app_data);
1109 self->app_data = NULL;
1110 return 0;
1111}
1112
1113/*
1114 * Deallocate the memory used by the Context object
1115 *
1116 * Arguments: self - The Context object
1117 * Returns: None
1118 */
1119static void
1120ssl_Context_dealloc(ssl_ContextObj *self)
1121{
1122 PyObject_GC_UnTrack((PyObject *)self);
1123 SSL_CTX_free(self->ctx);
1124 ssl_Context_clear(self);
1125 PyObject_GC_Del(self);
1126}
1127
1128
1129PyTypeObject ssl_Context_Type = {
1130 PyObject_HEAD_INIT(NULL)
1131 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001132 "OpenSSL.SSL.Context",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001133 sizeof(ssl_ContextObj),
1134 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001135 (destructor)ssl_Context_dealloc, /* tp_dealloc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001136 NULL, /* print */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001137 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001138 NULL, /* setattr */
1139 NULL, /* compare */
1140 NULL, /* repr */
1141 NULL, /* as_number */
1142 NULL, /* as_sequence */
1143 NULL, /* as_mapping */
1144 NULL, /* hash */
1145 NULL, /* call */
1146 NULL, /* str */
1147 NULL, /* getattro */
1148 NULL, /* setattro */
1149 NULL, /* as_buffer */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001150 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */
1151 ssl_Context_doc, /* tp_doc */
1152 (traverseproc)ssl_Context_traverse, /* tp_traverse */
1153 (inquiry)ssl_Context_clear, /* tp_clear */
1154 NULL, /* tp_richcompare */
1155 0, /* tp_weaklistoffset */
1156 NULL, /* tp_iter */
1157 NULL, /* tp_iternext */
1158 ssl_Context_methods, /* tp_methods */
1159 NULL, /* tp_members */
1160 NULL, /* tp_getset */
1161 NULL, /* tp_base */
1162 NULL, /* tp_dict */
1163 NULL, /* tp_descr_get */
1164 NULL, /* tp_descr_set */
1165 0, /* tp_dictoffset */
1166 NULL, /* tp_init */
1167 NULL, /* tp_alloc */
1168 ssl_Context_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001169};
1170
1171
1172/*
1173 * Initialize the Context part of the SSL sub module
1174 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001175 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001176 * Returns: 1 for success, 0 otherwise
1177 */
1178int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001179init_ssl_context(PyObject *module) {
1180
1181 if (PyType_Ready(&ssl_Context_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001182 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001183 }
1184
1185 if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) {
1186 return 0;
1187 }
1188
1189 if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) {
1190 return 0;
1191 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001192
1193 return 1;
1194}
1195