blob: a3d798e87e36d6b5762df8c402e6a73a854f09f0 [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 {
Rick Deand369c932009-07-08 11:48:33 -0500268 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500269 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)) {
Rick Deand369c932009-07-08 11:48:33 -0500294 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone286b1922008-09-07 21:35:38 -0400295 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
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200338static PyTypeObject *
339type_modified_error(const char *name)
340{
341 PyErr_Format(PyExc_RuntimeError,
342 "OpenSSL.crypto's '%s' attribute has been modified",
343 name);
344 return NULL;
345}
346
347static PyTypeObject *
348import_crypto_type(const char *name, size_t objsize)
349{
350 PyObject *module, *type;
351 PyTypeObject *res;
352 char modname[] = "OpenSSL.crypto";
353 char buffer[256] = "OpenSSL.crypto.";
354
355 if (strlen(buffer) + strlen(name) >= sizeof(buffer)) {
356 PyErr_BadInternalCall();
357 return NULL;
358 }
359 strcat(buffer, name);
360 module = PyImport_ImportModule(modname);
361 if (module == NULL) {
362 return NULL;
363 }
364 type = PyObject_GetAttrString(module, name);
365 Py_DECREF(module);
366 if (type == NULL) {
367 return NULL;
368 }
369 if (!(PyType_Check(type))) {
370 Py_DECREF(type);
371 return type_modified_error(name);
372 }
373 res = (PyTypeObject *)type;
374 if (strcmp(buffer, res->tp_name) != 0 || res->tp_basicsize != objsize) {
375 Py_DECREF(type);
376 return type_modified_error(name);
377 }
378 return res;
379}
380
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500381static crypto_X509Obj *
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200382parse_certificate_argument(const char* format, PyObject* args)
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500383{
384 static PyTypeObject *crypto_X509_type = NULL;
385 crypto_X509Obj *cert;
386
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500387 if (!crypto_X509_type)
388 {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200389 crypto_X509_type = import_crypto_type("X509", sizeof(crypto_X509Obj));
390 if (!crypto_X509_type)
391 return NULL;
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500392 }
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200393 if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format,
394 crypto_X509_type, &cert))
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500395 return NULL;
396 return cert;
397}
398
399static char ssl_Context_add_extra_chain_cert_doc[] = "\n\
400Add certificate to chain\n\
401\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400402@param certobj: The X509 certificate object to add to the chain\n\
403@return: None\n\
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500404";
405
406static PyObject *
407ssl_Context_add_extra_chain_cert(ssl_ContextObj *self, PyObject *args)
408{
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500409 X509* cert_original;
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500410 crypto_X509Obj *cert = parse_certificate_argument(
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200411 "O!:add_extra_chain_cert", args);
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500412 if (cert == NULL)
413 {
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500414 return NULL;
415 }
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500416 if (!(cert_original = X509_dup(cert->x509)))
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500417 {
Rick Deand369c932009-07-08 11:48:33 -0500418 /* exception_from_error_queue(ssl_Error); */
Jean-Paul Calderone0ce98072008-02-18 23:22:29 -0500419 PyErr_SetString(PyExc_RuntimeError, "X509_dup failed");
420 return NULL;
421 }
422 if (!SSL_CTX_add_extra_chain_cert(self->ctx, cert_original))
423 {
424 X509_free(cert_original);
Rick Deand369c932009-07-08 11:48:33 -0500425 exception_from_error_queue(ssl_Error);
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500426 return NULL;
427 }
428 else
429 {
430 Py_INCREF(Py_None);
431 return Py_None;
432 }
433}
434
435
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500436static char ssl_Context_use_certificate_chain_file_doc[] = "\n\
437Load a certificate chain from a file\n\
438\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400439@param certfile: The name of the certificate chain file\n\
440@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500441";
442static PyObject *
443ssl_Context_use_certificate_chain_file(ssl_ContextObj *self, PyObject *args)
444{
445 char *certfile;
446
447 if (!PyArg_ParseTuple(args, "s:use_certificate_chain_file", &certfile))
448 return NULL;
449
450 if (!SSL_CTX_use_certificate_chain_file(self->ctx, certfile))
451 {
Rick Deand369c932009-07-08 11:48:33 -0500452 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500453 return NULL;
454 }
455 else
456 {
457 Py_INCREF(Py_None);
458 return Py_None;
459 }
460}
461
462
463static char ssl_Context_use_certificate_file_doc[] = "\n\
464Load a certificate from a file\n\
465\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400466@param certfile: The name of the certificate file\n\
467@param filetype: (optional) The encoding of the file, default is PEM\n\
468@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500469";
470static PyObject *
471ssl_Context_use_certificate_file(ssl_ContextObj *self, PyObject *args)
472{
473 char *certfile;
474 int filetype = SSL_FILETYPE_PEM;
475
476 if (!PyArg_ParseTuple(args, "s|i:use_certificate_file", &certfile, &filetype))
477 return NULL;
478
479 if (!SSL_CTX_use_certificate_file(self->ctx, certfile, filetype))
480 {
Rick Deand369c932009-07-08 11:48:33 -0500481 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500482 return NULL;
483 }
484 else
485 {
486 Py_INCREF(Py_None);
487 return Py_None;
488 }
489}
490
491static char ssl_Context_use_certificate_doc[] = "\n\
492Load a certificate from a X509 object\n\
493\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400494@param cert: The X509 object\n\
495@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500496";
497static PyObject *
498ssl_Context_use_certificate(ssl_ContextObj *self, PyObject *args)
499{
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500500 crypto_X509Obj *cert = parse_certificate_argument(
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200501 "O!:use_certificate", args);
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -0500502 if (cert == NULL) {
503 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500504 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500505
506 if (!SSL_CTX_use_certificate(self->ctx, cert->x509))
507 {
Rick Deand369c932009-07-08 11:48:33 -0500508 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500509 return NULL;
510 }
511 else
512 {
513 Py_INCREF(Py_None);
514 return Py_None;
515 }
516}
517
518static char ssl_Context_use_privatekey_file_doc[] = "\n\
519Load a private key from a file\n\
520\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400521@param keyfile: The name of the key file\n\
522@param filetype: (optional) The encoding of the file, default is PEM\n\
523@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500524";
525static PyObject *
526ssl_Context_use_privatekey_file(ssl_ContextObj *self, PyObject *args)
527{
528 char *keyfile;
529 int filetype = SSL_FILETYPE_PEM, ret;
530
531 if (!PyArg_ParseTuple(args, "s|i:use_privatekey_file", &keyfile, &filetype))
532 return NULL;
533
534 MY_BEGIN_ALLOW_THREADS(self->tstate);
535 ret = SSL_CTX_use_PrivateKey_file(self->ctx, keyfile, filetype);
536 MY_END_ALLOW_THREADS(self->tstate);
537
538 if (PyErr_Occurred())
539 {
540 flush_error_queue();
541 return NULL;
542 }
543
544 if (!ret)
545 {
Rick Deand369c932009-07-08 11:48:33 -0500546 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500547 return NULL;
548 }
549 else
550 {
551 Py_INCREF(Py_None);
552 return Py_None;
553 }
554}
555
556static char ssl_Context_use_privatekey_doc[] = "\n\
557Load a private key from a PKey object\n\
558\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400559@param pkey: The PKey object\n\
560@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500561";
562static PyObject *
563ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args)
564{
565 static PyTypeObject *crypto_PKey_type = NULL;
566 crypto_PKeyObj *pkey;
567
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500568 if (!crypto_PKey_type)
569 {
Ziga Seilnacht6079e372009-08-31 20:52:30 +0200570 crypto_PKey_type = import_crypto_type("PKey", sizeof(crypto_PKeyObj));
571 if (!crypto_PKey_type)
572 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500573 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500574 if (!PyArg_ParseTuple(args, "O!:use_privatekey", crypto_PKey_type, &pkey))
575 return NULL;
576
577 if (!SSL_CTX_use_PrivateKey(self->ctx, pkey->pkey))
578 {
Rick Deand369c932009-07-08 11:48:33 -0500579 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500580 return NULL;
581 }
582 else
583 {
584 Py_INCREF(Py_None);
585 return Py_None;
586 }
587}
588
589static char ssl_Context_check_privatekey_doc[] = "\n\
590Check that the private key and certificate match up\n\
591\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400592@return: None (raises an exception if something's wrong)\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500593";
594static PyObject *
595ssl_Context_check_privatekey(ssl_ContextObj *self, PyObject *args)
596{
597 if (!PyArg_ParseTuple(args, ":check_privatekey"))
598 return NULL;
599
600 if (!SSL_CTX_check_private_key(self->ctx))
601 {
Rick Deand369c932009-07-08 11:48:33 -0500602 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500603 return NULL;
604 }
605 else
606 {
607 Py_INCREF(Py_None);
608 return Py_None;
609 }
610}
611
612static char ssl_Context_load_client_ca_doc[] = "\n\
613Load the trusted certificates that will be sent to the client (basically\n\
614telling the client \"These are the guys I trust\")\n\
615\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400616@param cafile: The name of the certificates file\n\
617@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500618";
619static PyObject *
620ssl_Context_load_client_ca(ssl_ContextObj *self, PyObject *args)
621{
622 char *cafile;
623
624 if (!PyArg_ParseTuple(args, "s:load_client_ca", &cafile))
625 return NULL;
626
627 SSL_CTX_set_client_CA_list(self->ctx, SSL_load_client_CA_file(cafile));
628
629 Py_INCREF(Py_None);
630 return Py_None;
631}
632
633static char ssl_Context_set_session_id_doc[] = "\n\
634Set the session identifier, this is needed if you want to do session\n\
635resumption (which, ironically, isn't implemented yet)\n\
636\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400637@param buf: A Python object that can be safely converted to a string\n\
638@returns: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500639";
640static PyObject *
641ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args)
642{
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500643 unsigned char *buf;
644 unsigned int len;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500645
646 if (!PyArg_ParseTuple(args, "s#:set_session_id", &buf, &len))
647 return NULL;
648
649 if (!SSL_CTX_set_session_id_context(self->ctx, buf, len))
650 {
Rick Deand369c932009-07-08 11:48:33 -0500651 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500652 return NULL;
653 }
654 else
655 {
656 Py_INCREF(Py_None);
657 return Py_None;
658 }
659}
660
661static char ssl_Context_set_verify_doc[] = "\n\
662Set the verify mode and verify callback\n\
663\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400664@param mode: The verify mode, this is either VERIFY_NONE or\n\
665 VERIFY_PEER combined with possible other flags\n\
666@param callback: The Python callback to use\n\
667@return: None\n\
Jean-Paul Calderone24aedf42008-03-06 22:01:16 -0500668\n\
669See SSL_CTX_set_verify(3SSL) for further details.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500670";
671static PyObject *
672ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args)
673{
674 int mode;
675 PyObject *callback = NULL;
676
677 if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback))
678 return NULL;
679
680 if (!PyCallable_Check(callback))
681 {
682 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
683 return NULL;
684 }
685
686 Py_DECREF(self->verify_callback);
687 Py_INCREF(callback);
688 self->verify_callback = callback;
689 SSL_CTX_set_verify(self->ctx, mode, global_verify_callback);
690
691 Py_INCREF(Py_None);
692 return Py_None;
693}
694
695static char ssl_Context_set_verify_depth_doc[] = "\n\
696Set the verify depth\n\
697\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400698@param depth: An integer specifying the verify depth\n\
699@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500700";
701static PyObject *
702ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args)
703{
704 int depth;
705
706 if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth))
707 return NULL;
708
709 SSL_CTX_set_verify_depth(self->ctx, depth);
710 Py_INCREF(Py_None);
711 return Py_None;
712}
713
714static char ssl_Context_get_verify_mode_doc[] = "\n\
715Get the verify mode\n\
716\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400717@return: The verify mode\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500718";
719static PyObject *
720ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args)
721{
722 int mode;
723
724 if (!PyArg_ParseTuple(args, ":get_verify_mode"))
725 return NULL;
726
727 mode = SSL_CTX_get_verify_mode(self->ctx);
728 return PyInt_FromLong((long)mode);
729}
730
731static char ssl_Context_get_verify_depth_doc[] = "\n\
732Get the verify depth\n\
733\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400734@return: The verify depth\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500735";
736static PyObject *
737ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args)
738{
739 int depth;
740
741 if (!PyArg_ParseTuple(args, ":get_verify_depth"))
742 return NULL;
743
744 depth = SSL_CTX_get_verify_depth(self->ctx);
745 return PyInt_FromLong((long)depth);
746}
747
748static char ssl_Context_load_tmp_dh_doc[] = "\n\
749Load parameters for Ephemeral Diffie-Hellman\n\
750\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400751@param dhfile: The file to load EDH parameters from\n\
752@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500753";
754static PyObject *
755ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args)
756{
757 char *dhfile;
758 BIO *bio;
759 DH *dh;
760
761 if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile))
762 return NULL;
763
764 bio = BIO_new_file(dhfile, "r");
765 if (bio == NULL)
766 return PyErr_NoMemory();
767
768 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
769 SSL_CTX_set_tmp_dh(self->ctx, dh);
770 DH_free(dh);
771 BIO_free(bio);
772
773 Py_INCREF(Py_None);
774 return Py_None;
775}
776
777static char ssl_Context_set_cipher_list_doc[] = "\n\
778Change the cipher list\n\
779\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400780@param cipher_list: A cipher list, see ciphers(1)\n\
781@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500782";
783static PyObject *
784ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args)
785{
786 char *cipher_list;
787
788 if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list))
789 return NULL;
790
791 if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list))
792 {
Rick Deand369c932009-07-08 11:48:33 -0500793 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500794 return NULL;
795 }
796 else
797 {
798 Py_INCREF(Py_None);
799 return Py_None;
800 }
801}
802
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200803static char ssl_Context_set_client_CA_list_doc[] = "\n\
804Set the list of preferred client certificate signers for this server context.\n\
805\n\
806This list of certificate authorities will be sent to the client when the\n\
807server requests a client certificate.\n\
808\n\
809@param certificate_authorities: a sequence of X509Names.\n\
810@return: None\n\
811";
812
813static PyObject *
814ssl_Context_set_client_CA_list(ssl_ContextObj *self, PyObject *args)
815{
816 static PyTypeObject *X509NameType;
817 PyObject *sequence, *tuple, *item;
818 crypto_X509NameObj *name;
819 X509_NAME *sslname;
820 STACK_OF(X509_NAME) *CANames;
821 Py_ssize_t length;
822 int i;
823
824 if (X509NameType == NULL) {
825 X509NameType = import_crypto_type("X509Name", sizeof(crypto_X509NameObj));
826 if (X509NameType == NULL) {
827 return NULL;
828 }
829 }
830 if (!PyArg_ParseTuple(args, "O:set_client_CA_list", &sequence)) {
831 return NULL;
832 }
833 tuple = PySequence_Tuple(sequence);
834 if (tuple == NULL) {
835 return NULL;
836 }
837 length = PyTuple_Size(tuple);
838 if (length >= INT_MAX) {
839 PyErr_SetString(PyExc_ValueError, "client CA list is too long");
840 Py_DECREF(tuple);
841 return NULL;
842 }
843 CANames = sk_X509_NAME_new_null();
844 if (CANames == NULL) {
845 Py_DECREF(tuple);
846 exception_from_error_queue(ssl_Error);
847 return NULL;
848 }
849 for (i = 0; i < length; i++) {
850 item = PyTuple_GetItem(tuple, i);
851 if (item->ob_type != X509NameType) {
852 PyErr_Format(PyExc_TypeError,
853 "client CAs must be X509Name objects, not %s objects",
854 item->ob_type->tp_name);
855 sk_X509_NAME_free(CANames);
856 Py_DECREF(tuple);
857 return NULL;
858 }
859 name = (crypto_X509NameObj *)item;
860 sslname = X509_NAME_dup(name->x509_name);
861 if (sslname == NULL) {
862 sk_X509_NAME_free(CANames);
863 Py_DECREF(tuple);
864 exception_from_error_queue(ssl_Error);
865 return NULL;
866 }
867 if (!sk_X509_NAME_push(CANames, sslname)) {
868 X509_NAME_free(sslname);
869 sk_X509_NAME_free(CANames);
870 Py_DECREF(tuple);
871 exception_from_error_queue(ssl_Error);
872 return NULL;
873 }
874 }
875 Py_DECREF(tuple);
876 SSL_CTX_set_client_CA_list(self->ctx, CANames);
877 Py_INCREF(Py_None);
878 return Py_None;
879}
880
881static char ssl_Context_add_client_CA_doc[] = "\n\
882Add the CA certificate to the list of preferred signers for this context.\n\
883\n\
884The list of certificate authorities will be sent to the client when the\n\
885server requests a client certificate.\n\
886\n\
887@param certificate_authority: certificate authority's X509 certificate.\n\
888@return: None\n\
889";
890
891static PyObject *
892ssl_Context_add_client_CA(ssl_ContextObj *self, PyObject *args)
893{
894 crypto_X509Obj *cert;
895
896 cert = parse_certificate_argument("O!:add_client_CA", args);
897 if (cert == NULL) {
898 return NULL;
899 }
900 if (!SSL_CTX_add_client_CA(self->ctx, cert->x509)) {
901 exception_from_error_queue(ssl_Error);
902 return NULL;
903 }
904 Py_INCREF(Py_None);
905 return Py_None;
906}
907
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500908static char ssl_Context_set_timeout_doc[] = "\n\
909Set session timeout\n\
910\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400911@param timeout: The timeout in seconds\n\
912@return: The previous session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500913";
914static PyObject *
915ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args)
916{
917 long t, ret;
918
919 if (!PyArg_ParseTuple(args, "l:set_timeout", &t))
920 return NULL;
921
922 ret = SSL_CTX_set_timeout(self->ctx, t);
923 return PyLong_FromLong(ret);
924}
925
926static char ssl_Context_get_timeout_doc[] = "\n\
927Get the session timeout\n\
928\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400929@return: The session timeout\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500930";
931static PyObject *
932ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args)
933{
934 long ret;
935
936 if (!PyArg_ParseTuple(args, ":get_timeout"))
937 return NULL;
938
939 ret = SSL_CTX_get_timeout(self->ctx);
940 return PyLong_FromLong(ret);
941}
942
943static char ssl_Context_set_info_callback_doc[] = "\n\
944Set the info callback\n\
945\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400946@param callback: The Python callback to use\n\
947@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500948";
949static PyObject *
950ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args)
951{
952 PyObject *callback;
953
954 if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback))
955 return NULL;
956
957 if (!PyCallable_Check(callback))
958 {
959 PyErr_SetString(PyExc_TypeError, "expected PyCallable");
960 return NULL;
961 }
962
963 Py_DECREF(self->info_callback);
964 Py_INCREF(callback);
965 self->info_callback = callback;
966 SSL_CTX_set_info_callback(self->ctx, global_info_callback);
967
968 Py_INCREF(Py_None);
969 return Py_None;
970}
971
972static char ssl_Context_get_app_data_doc[] = "\n\
973Get the application data (supplied via set_app_data())\n\
974\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400975@return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500976";
977static PyObject *
978ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args)
979{
980 if (!PyArg_ParseTuple(args, ":get_app_data"))
981 return NULL;
982
983 Py_INCREF(self->app_data);
984 return self->app_data;
985}
986
987static char ssl_Context_set_app_data_doc[] = "\n\
988Set the application data (will be returned from get_app_data())\n\
989\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400990@param data: Any Python object\n\
991@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500992";
993static PyObject *
994ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args)
995{
996 PyObject *data;
997
998 if (!PyArg_ParseTuple(args, "O:set_app_data", &data))
999 return NULL;
1000
1001 Py_DECREF(self->app_data);
1002 Py_INCREF(data);
1003 self->app_data = data;
1004
1005 Py_INCREF(Py_None);
1006 return Py_None;
1007}
1008
1009static char ssl_Context_get_cert_store_doc[] = "\n\
1010Get the certificate store for the context\n\
1011\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -04001012@return: A X509Store object\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001013";
1014static PyObject *
1015ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args)
1016{
1017 X509_STORE *store;
1018
1019 if (!PyArg_ParseTuple(args, ":get_cert_store"))
1020 return NULL;
1021
1022 if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL)
1023 {
1024 Py_INCREF(Py_None);
1025 return Py_None;
1026 }
1027 else
1028 {
1029 return (PyObject *)crypto_X509Store_New(store, 0);
1030 }
1031}
1032
1033static char ssl_Context_set_options_doc[] = "\n\
1034Add options. Options set before are not cleared!\n\
1035\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -04001036@param options: The options to add.\n\
1037@return: The new option bitmask.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001038";
1039static PyObject *
1040ssl_Context_set_options(ssl_ContextObj *self, PyObject *args)
1041{
1042 long options;
1043
1044 if (!PyArg_ParseTuple(args, "l:set_options", &options))
1045 return NULL;
1046
1047 return PyInt_FromLong(SSL_CTX_set_options(self->ctx, options));
1048}
1049
1050
1051/*
1052 * Member methods in the Context object
1053 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
1054 * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS }
1055 * for convenience
1056 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real
1057 * function with the name 'name'
1058 */
1059#define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc }
1060static PyMethodDef ssl_Context_methods[] = {
1061 ADD_METHOD(load_verify_locations),
1062 ADD_METHOD(set_passwd_cb),
Jean-Paul Calderone1cb5d022008-09-07 20:58:50 -04001063 ADD_METHOD(set_default_verify_paths),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001064 ADD_METHOD(use_certificate_chain_file),
1065 ADD_METHOD(use_certificate_file),
1066 ADD_METHOD(use_certificate),
Jean-Paul Calderoned3ada852008-02-18 21:17:29 -05001067 ADD_METHOD(add_extra_chain_cert),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001068 ADD_METHOD(use_privatekey_file),
1069 ADD_METHOD(use_privatekey),
1070 ADD_METHOD(check_privatekey),
1071 ADD_METHOD(load_client_ca),
1072 ADD_METHOD(set_session_id),
1073 ADD_METHOD(set_verify),
1074 ADD_METHOD(set_verify_depth),
1075 ADD_METHOD(get_verify_mode),
1076 ADD_METHOD(get_verify_depth),
1077 ADD_METHOD(load_tmp_dh),
1078 ADD_METHOD(set_cipher_list),
Ziga Seilnacht679c4262009-09-01 01:32:29 +02001079 ADD_METHOD(set_client_CA_list),
1080 ADD_METHOD(add_client_CA),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001081 ADD_METHOD(set_timeout),
1082 ADD_METHOD(get_timeout),
1083 ADD_METHOD(set_info_callback),
1084 ADD_METHOD(get_app_data),
1085 ADD_METHOD(set_app_data),
1086 ADD_METHOD(get_cert_store),
1087 ADD_METHOD(set_options),
1088 { NULL, NULL }
1089};
1090#undef ADD_METHOD
1091
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001092/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001093 * Despite the name which might suggest otherwise, this is not the tp_init for
1094 * the Context type. It's just the common initialization code shared by the
1095 * two _{Nn}ew functions below.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001096 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001097static ssl_ContextObj*
1098ssl_Context_init(ssl_ContextObj *self, int i_method) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001099 SSL_METHOD *method;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001100
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001101 switch (i_method) {
1102 case ssl_SSLv2_METHOD:
1103 method = SSLv2_method();
1104 break;
1105 case ssl_SSLv23_METHOD:
1106 method = SSLv23_method();
1107 break;
1108 case ssl_SSLv3_METHOD:
1109 method = SSLv3_method();
1110 break;
1111 case ssl_TLSv1_METHOD:
1112 method = TLSv1_method();
1113 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001114 default:
1115 PyErr_SetString(PyExc_ValueError, "No such protocol");
1116 return NULL;
1117 }
1118
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001119 self->ctx = SSL_CTX_new(method);
1120 Py_INCREF(Py_None);
1121 self->passphrase_callback = Py_None;
1122 Py_INCREF(Py_None);
1123 self->verify_callback = Py_None;
1124 Py_INCREF(Py_None);
1125 self->info_callback = Py_None;
1126
1127 Py_INCREF(Py_None);
1128 self->passphrase_userdata = Py_None;
1129
1130 Py_INCREF(Py_None);
1131 self->app_data = Py_None;
1132
1133 /* Some initialization that's required to operate smoothly in Python */
1134 SSL_CTX_set_app_data(self->ctx, self);
1135 SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
1136 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1137 SSL_MODE_AUTO_RETRY);
1138
1139 self->tstate = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001140
1141 return self;
1142}
1143
1144/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001145 * This one is exposed in the CObject API. I want to deprecate it.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001146 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001147ssl_ContextObj*
1148ssl_Context_New(int i_method) {
1149 ssl_ContextObj *self;
1150
1151 self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type);
1152 if (self == NULL) {
1153 return (ssl_ContextObj *)PyErr_NoMemory();
1154 }
1155 self = ssl_Context_init(self, i_method);
1156 PyObject_GC_Track((PyObject *)self);
1157 return self;
1158}
1159
1160
1161/*
1162 * This one is the tp_new of the Context type. It's great.
1163 */
1164static PyObject*
1165ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
1166 int i_method;
1167 ssl_ContextObj *self;
1168 static char *kwlist[] = {"method", NULL};
1169
1170 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) {
1171 return NULL;
1172 }
1173
1174 self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1);
1175 if (self == NULL) {
1176 return NULL;
1177 }
1178
1179 return (PyObject *)ssl_Context_init(self, i_method);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001180}
1181
1182/*
1183 * Call the visitproc on all contained objects.
1184 *
1185 * Arguments: self - The Context object
1186 * visit - Function to call
1187 * arg - Extra argument to visit
1188 * Returns: 0 if all goes well, otherwise the return code from the first
1189 * call that gave non-zero result.
1190 */
1191static int
1192ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg)
1193{
1194 int ret = 0;
1195
1196 if (ret == 0 && self->passphrase_callback != NULL)
1197 ret = visit((PyObject *)self->passphrase_callback, arg);
1198 if (ret == 0 && self->passphrase_userdata != NULL)
1199 ret = visit((PyObject *)self->passphrase_userdata, arg);
1200 if (ret == 0 && self->verify_callback != NULL)
1201 ret = visit((PyObject *)self->verify_callback, arg);
1202 if (ret == 0 && self->info_callback != NULL)
1203 ret = visit((PyObject *)self->info_callback, arg);
1204 if (ret == 0 && self->app_data != NULL)
1205 ret = visit(self->app_data, arg);
1206 return ret;
1207}
1208
1209/*
1210 * Decref all contained objects and zero the pointers.
1211 *
1212 * Arguments: self - The Context object
1213 * Returns: Always 0.
1214 */
1215static int
1216ssl_Context_clear(ssl_ContextObj *self)
1217{
1218 Py_XDECREF(self->passphrase_callback);
1219 self->passphrase_callback = NULL;
1220 Py_XDECREF(self->passphrase_userdata);
1221 self->passphrase_userdata = NULL;
1222 Py_XDECREF(self->verify_callback);
1223 self->verify_callback = NULL;
1224 Py_XDECREF(self->info_callback);
1225 self->info_callback = NULL;
1226 Py_XDECREF(self->app_data);
1227 self->app_data = NULL;
1228 return 0;
1229}
1230
1231/*
1232 * Deallocate the memory used by the Context object
1233 *
1234 * Arguments: self - The Context object
1235 * Returns: None
1236 */
1237static void
1238ssl_Context_dealloc(ssl_ContextObj *self)
1239{
1240 PyObject_GC_UnTrack((PyObject *)self);
1241 SSL_CTX_free(self->ctx);
1242 ssl_Context_clear(self);
1243 PyObject_GC_Del(self);
1244}
1245
1246
1247PyTypeObject ssl_Context_Type = {
1248 PyObject_HEAD_INIT(NULL)
1249 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001250 "OpenSSL.SSL.Context",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001251 sizeof(ssl_ContextObj),
1252 0,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001253 (destructor)ssl_Context_dealloc, /* tp_dealloc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001254 NULL, /* print */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001255 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001256 NULL, /* setattr */
1257 NULL, /* compare */
1258 NULL, /* repr */
1259 NULL, /* as_number */
1260 NULL, /* as_sequence */
1261 NULL, /* as_mapping */
1262 NULL, /* hash */
1263 NULL, /* call */
1264 NULL, /* str */
1265 NULL, /* getattro */
1266 NULL, /* setattro */
1267 NULL, /* as_buffer */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */
1269 ssl_Context_doc, /* tp_doc */
1270 (traverseproc)ssl_Context_traverse, /* tp_traverse */
1271 (inquiry)ssl_Context_clear, /* tp_clear */
1272 NULL, /* tp_richcompare */
1273 0, /* tp_weaklistoffset */
1274 NULL, /* tp_iter */
1275 NULL, /* tp_iternext */
1276 ssl_Context_methods, /* tp_methods */
1277 NULL, /* tp_members */
1278 NULL, /* tp_getset */
1279 NULL, /* tp_base */
1280 NULL, /* tp_dict */
1281 NULL, /* tp_descr_get */
1282 NULL, /* tp_descr_set */
1283 0, /* tp_dictoffset */
1284 NULL, /* tp_init */
1285 NULL, /* tp_alloc */
1286 ssl_Context_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001287};
1288
1289
1290/*
1291 * Initialize the Context part of the SSL sub module
1292 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001293 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001294 * Returns: 1 for success, 0 otherwise
1295 */
1296int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001297init_ssl_context(PyObject *module) {
1298
1299 if (PyType_Ready(&ssl_Context_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001300 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001301 }
1302
1303 if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) {
1304 return 0;
1305 }
1306
1307 if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) {
1308 return 0;
1309 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001310
1311 return 1;
1312}
1313