Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * context.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 5 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * |
| 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 Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 13 | |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 14 | #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 Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 20 | #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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 31 | #define SSL_MODULE |
| 32 | #include "ssl.h" |
| 33 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 34 | /* |
| 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 Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 60 | * 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 63 | * |
| 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 | */ |
| 72 | static int |
| 73 | global_passphrase_callback(char *buf, int maxlen, int verify, void *arg) |
| 74 | { |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 75 | /* |
| 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 80 | char *str; |
| 81 | PyObject *argv, *ret = NULL; |
| 82 | ssl_ContextObj *ctx = (ssl_ContextObj *)arg; |
| 83 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 84 | /* |
| 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 Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 91 | /* The Python callback is called with a (maxlen,verify,userdata) tuple */ |
| 92 | argv = Py_BuildValue("(iiO)", maxlen, verify, ctx->passphrase_userdata); |
| 93 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 94 | /* |
| 95 | * XXX Didn't check argv to see if it was NULL. -exarkun |
| 96 | */ |
| 97 | ret = PyEval_CallObject(ctx->passphrase_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 98 | Py_DECREF(argv); |
| 99 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 100 | 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 110 | if (!PyObject_IsTrue(ret)) { |
| 111 | /* |
| 112 | * Returned "", or None, or something. Treat it as no passphrase. |
| 113 | */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 114 | Py_DECREF(ret); |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 115 | 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | len = PyString_Size(ret); |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 129 | 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 134 | len = maxlen; |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 135 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 136 | |
| 137 | str = PyString_AsString(ret); |
| 138 | strncpy(buf, str, len); |
| 139 | Py_XDECREF(ret); |
| 140 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 141 | out: |
| 142 | /* |
| 143 | * This function is returning into OpenSSL. Release the GIL again. |
| 144 | */ |
| 145 | MY_BEGIN_ALLOW_THREADS(ctx->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 146 | 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 | */ |
| 158 | static int |
| 159 | global_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 Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 165 | int errnum, errdepth, c_ret; |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 166 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 167 | // 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 Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 170 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 171 | MY_END_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 172 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 173 | 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 Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 176 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 177 | argv = Py_BuildValue("(OOiii)", (PyObject *)conn, (PyObject *)cert, |
| 178 | errnum, errdepth, ok); |
| 179 | Py_DECREF(cert); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 180 | ret = PyEval_CallObject(conn->context->verify_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 181 | Py_DECREF(argv); |
| 182 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 183 | if (ret != NULL && PyObject_IsTrue(ret)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 184 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 185 | Py_DECREF(ret); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 186 | c_ret = 1; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 187 | } else { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 188 | c_ret = 0; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 189 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 190 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 191 | MY_BEGIN_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 192 | return c_ret; |
| 193 | } |
| 194 | |
| 195 | /* |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 196 | * 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 199 | * |
| 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 | */ |
| 205 | static void |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 206 | global_info_callback(const SSL *ssl, int where, int _ret) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 207 | { |
| 208 | ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl); |
| 209 | PyObject *argv, *ret; |
| 210 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 211 | /* |
| 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 218 | argv = Py_BuildValue("(Oii)", (PyObject *)conn, where, _ret); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 219 | ret = PyEval_CallObject(conn->context->info_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 220 | Py_DECREF(argv); |
| 221 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 222 | 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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
| 238 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 239 | static char ssl_Context_doc[] = "\n\ |
| 240 | Context(method) -> Context instance\n\ |
| 241 | \n\ |
| 242 | OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\ |
| 243 | connections.\n\ |
| 244 | \n\ |
| 245 | @param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or\n\ |
| 246 | TLSv1_METHOD.\n\ |
| 247 | "; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 248 | |
| 249 | static char ssl_Context_load_verify_locations_doc[] = "\n\ |
| 250 | Let SSL know where we can find trusted certificates for the certificate\n\ |
| 251 | chain\n\ |
| 252 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 253 | @param cafile: In which file we can find the certificates\n\ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 254 | @param capath: In which directory we can find the certificates\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 255 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 256 | "; |
| 257 | static PyObject * |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 258 | ssl_Context_load_verify_locations(ssl_ContextObj *self, PyObject *args) { |
| 259 | char *cafile = NULL; |
| 260 | char *capath = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 261 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 262 | if (!PyArg_ParseTuple(args, "z|z:load_verify_locations", &cafile, &capath)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 263 | return NULL; |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 264 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 265 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 266 | if (!SSL_CTX_load_verify_locations(self->ctx, cafile, capath)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 267 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 268 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 269 | return NULL; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | Py_INCREF(Py_None); |
| 274 | return Py_None; |
| 275 | } |
| 276 | } |
| 277 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 278 | static char ssl_Context_set_default_verify_paths_doc[] = "\n\ |
| 279 | Use the platform-specific CA certificate locations\n\ |
| 280 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 281 | @return: None\n\ |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 282 | "; |
| 283 | static PyObject * |
| 284 | ssl_Context_set_default_verify_paths(ssl_ContextObj *self, PyObject *args) { |
Jean-Paul Calderone | 9eadb96 | 2008-09-07 21:20:44 -0400 | [diff] [blame] | 285 | if (!PyArg_ParseTuple(args, ":set_default_verify_paths")) { |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 289 | /* |
| 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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 294 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 297 | Py_INCREF(Py_None); |
| 298 | return Py_None; |
| 299 | }; |
| 300 | |
| 301 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 302 | static char ssl_Context_set_passwd_cb_doc[] = "\n\ |
| 303 | Set the passphrase callback\n\ |
| 304 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 305 | @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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 309 | "; |
| 310 | static PyObject * |
| 311 | ssl_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 Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 338 | static PyTypeObject * |
| 339 | type_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 | |
| 347 | static PyTypeObject * |
| 348 | import_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 Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 381 | static crypto_X509Obj * |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 382 | parse_certificate_argument(const char* format, PyObject* args) |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 383 | { |
| 384 | static PyTypeObject *crypto_X509_type = NULL; |
| 385 | crypto_X509Obj *cert; |
| 386 | |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 387 | if (!crypto_X509_type) |
| 388 | { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 389 | crypto_X509_type = import_crypto_type("X509", sizeof(crypto_X509Obj)); |
| 390 | if (!crypto_X509_type) |
| 391 | return NULL; |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 392 | } |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 393 | if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format, |
| 394 | crypto_X509_type, &cert)) |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 395 | return NULL; |
| 396 | return cert; |
| 397 | } |
| 398 | |
| 399 | static char ssl_Context_add_extra_chain_cert_doc[] = "\n\ |
| 400 | Add certificate to chain\n\ |
| 401 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 402 | @param certobj: The X509 certificate object to add to the chain\n\ |
| 403 | @return: None\n\ |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 404 | "; |
| 405 | |
| 406 | static PyObject * |
| 407 | ssl_Context_add_extra_chain_cert(ssl_ContextObj *self, PyObject *args) |
| 408 | { |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 409 | X509* cert_original; |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 410 | crypto_X509Obj *cert = parse_certificate_argument( |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 411 | "O!:add_extra_chain_cert", args); |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 412 | if (cert == NULL) |
| 413 | { |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 414 | return NULL; |
| 415 | } |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 416 | if (!(cert_original = X509_dup(cert->x509))) |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 417 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 418 | /* exception_from_error_queue(ssl_Error); */ |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 419 | 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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 425 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 426 | return NULL; |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | Py_INCREF(Py_None); |
| 431 | return Py_None; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 436 | static char ssl_Context_use_certificate_chain_file_doc[] = "\n\ |
| 437 | Load a certificate chain from a file\n\ |
| 438 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 439 | @param certfile: The name of the certificate chain file\n\ |
| 440 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 441 | "; |
| 442 | static PyObject * |
| 443 | ssl_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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 452 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 453 | return NULL; |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | Py_INCREF(Py_None); |
| 458 | return Py_None; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static char ssl_Context_use_certificate_file_doc[] = "\n\ |
| 464 | Load a certificate from a file\n\ |
| 465 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 466 | @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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 469 | "; |
| 470 | static PyObject * |
| 471 | ssl_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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 481 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | Py_INCREF(Py_None); |
| 487 | return Py_None; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static char ssl_Context_use_certificate_doc[] = "\n\ |
| 492 | Load a certificate from a X509 object\n\ |
| 493 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 494 | @param cert: The X509 object\n\ |
| 495 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 496 | "; |
| 497 | static PyObject * |
| 498 | ssl_Context_use_certificate(ssl_ContextObj *self, PyObject *args) |
| 499 | { |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 500 | crypto_X509Obj *cert = parse_certificate_argument( |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 501 | "O!:use_certificate", args); |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 502 | if (cert == NULL) { |
| 503 | return NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 504 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 505 | |
| 506 | if (!SSL_CTX_use_certificate(self->ctx, cert->x509)) |
| 507 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 508 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | Py_INCREF(Py_None); |
| 514 | return Py_None; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static char ssl_Context_use_privatekey_file_doc[] = "\n\ |
| 519 | Load a private key from a file\n\ |
| 520 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 521 | @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 Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 524 | "; |
| 525 | static PyObject * |
| 526 | ssl_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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 546 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | Py_INCREF(Py_None); |
| 552 | return Py_None; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static char ssl_Context_use_privatekey_doc[] = "\n\ |
| 557 | Load a private key from a PKey object\n\ |
| 558 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 559 | @param pkey: The PKey object\n\ |
| 560 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 561 | "; |
| 562 | static PyObject * |
| 563 | ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args) |
| 564 | { |
| 565 | static PyTypeObject *crypto_PKey_type = NULL; |
| 566 | crypto_PKeyObj *pkey; |
| 567 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 568 | if (!crypto_PKey_type) |
| 569 | { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame^] | 570 | crypto_PKey_type = import_crypto_type("PKey", sizeof(crypto_PKeyObj)); |
| 571 | if (!crypto_PKey_type) |
| 572 | return NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 573 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 574 | 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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 579 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 580 | return NULL; |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | Py_INCREF(Py_None); |
| 585 | return Py_None; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static char ssl_Context_check_privatekey_doc[] = "\n\ |
| 590 | Check that the private key and certificate match up\n\ |
| 591 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 592 | @return: None (raises an exception if something's wrong)\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 593 | "; |
| 594 | static PyObject * |
| 595 | ssl_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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 602 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 603 | return NULL; |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | Py_INCREF(Py_None); |
| 608 | return Py_None; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static char ssl_Context_load_client_ca_doc[] = "\n\ |
| 613 | Load the trusted certificates that will be sent to the client (basically\n\ |
| 614 | telling the client \"These are the guys I trust\")\n\ |
| 615 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 616 | @param cafile: The name of the certificates file\n\ |
| 617 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 618 | "; |
| 619 | static PyObject * |
| 620 | ssl_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 | |
| 633 | static char ssl_Context_set_session_id_doc[] = "\n\ |
| 634 | Set the session identifier, this is needed if you want to do session\n\ |
| 635 | resumption (which, ironically, isn't implemented yet)\n\ |
| 636 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 637 | @param buf: A Python object that can be safely converted to a string\n\ |
| 638 | @returns: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 639 | "; |
| 640 | static PyObject * |
| 641 | ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args) |
| 642 | { |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 643 | unsigned char *buf; |
| 644 | unsigned int len; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 645 | |
| 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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 651 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | else |
| 655 | { |
| 656 | Py_INCREF(Py_None); |
| 657 | return Py_None; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | static char ssl_Context_set_verify_doc[] = "\n\ |
| 662 | Set the verify mode and verify callback\n\ |
| 663 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 664 | @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 Calderone | 24aedf4 | 2008-03-06 22:01:16 -0500 | [diff] [blame] | 668 | \n\ |
| 669 | See SSL_CTX_set_verify(3SSL) for further details.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 670 | "; |
| 671 | static PyObject * |
| 672 | ssl_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 | |
| 695 | static char ssl_Context_set_verify_depth_doc[] = "\n\ |
| 696 | Set the verify depth\n\ |
| 697 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 698 | @param depth: An integer specifying the verify depth\n\ |
| 699 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 700 | "; |
| 701 | static PyObject * |
| 702 | ssl_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 | |
| 714 | static char ssl_Context_get_verify_mode_doc[] = "\n\ |
| 715 | Get the verify mode\n\ |
| 716 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 717 | @return: The verify mode\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 718 | "; |
| 719 | static PyObject * |
| 720 | ssl_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 | |
| 731 | static char ssl_Context_get_verify_depth_doc[] = "\n\ |
| 732 | Get the verify depth\n\ |
| 733 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 734 | @return: The verify depth\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 735 | "; |
| 736 | static PyObject * |
| 737 | ssl_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 | |
| 748 | static char ssl_Context_load_tmp_dh_doc[] = "\n\ |
| 749 | Load parameters for Ephemeral Diffie-Hellman\n\ |
| 750 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 751 | @param dhfile: The file to load EDH parameters from\n\ |
| 752 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 753 | "; |
| 754 | static PyObject * |
| 755 | ssl_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 | |
| 777 | static char ssl_Context_set_cipher_list_doc[] = "\n\ |
| 778 | Change the cipher list\n\ |
| 779 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 780 | @param cipher_list: A cipher list, see ciphers(1)\n\ |
| 781 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 782 | "; |
| 783 | static PyObject * |
| 784 | ssl_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 Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 793 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 794 | return NULL; |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | Py_INCREF(Py_None); |
| 799 | return Py_None; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static char ssl_Context_set_timeout_doc[] = "\n\ |
| 804 | Set session timeout\n\ |
| 805 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 806 | @param timeout: The timeout in seconds\n\ |
| 807 | @return: The previous session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 808 | "; |
| 809 | static PyObject * |
| 810 | ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args) |
| 811 | { |
| 812 | long t, ret; |
| 813 | |
| 814 | if (!PyArg_ParseTuple(args, "l:set_timeout", &t)) |
| 815 | return NULL; |
| 816 | |
| 817 | ret = SSL_CTX_set_timeout(self->ctx, t); |
| 818 | return PyLong_FromLong(ret); |
| 819 | } |
| 820 | |
| 821 | static char ssl_Context_get_timeout_doc[] = "\n\ |
| 822 | Get the session timeout\n\ |
| 823 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 824 | @return: The session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 825 | "; |
| 826 | static PyObject * |
| 827 | ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args) |
| 828 | { |
| 829 | long ret; |
| 830 | |
| 831 | if (!PyArg_ParseTuple(args, ":get_timeout")) |
| 832 | return NULL; |
| 833 | |
| 834 | ret = SSL_CTX_get_timeout(self->ctx); |
| 835 | return PyLong_FromLong(ret); |
| 836 | } |
| 837 | |
| 838 | static char ssl_Context_set_info_callback_doc[] = "\n\ |
| 839 | Set the info callback\n\ |
| 840 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 841 | @param callback: The Python callback to use\n\ |
| 842 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 843 | "; |
| 844 | static PyObject * |
| 845 | ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args) |
| 846 | { |
| 847 | PyObject *callback; |
| 848 | |
| 849 | if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback)) |
| 850 | return NULL; |
| 851 | |
| 852 | if (!PyCallable_Check(callback)) |
| 853 | { |
| 854 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 855 | return NULL; |
| 856 | } |
| 857 | |
| 858 | Py_DECREF(self->info_callback); |
| 859 | Py_INCREF(callback); |
| 860 | self->info_callback = callback; |
| 861 | SSL_CTX_set_info_callback(self->ctx, global_info_callback); |
| 862 | |
| 863 | Py_INCREF(Py_None); |
| 864 | return Py_None; |
| 865 | } |
| 866 | |
| 867 | static char ssl_Context_get_app_data_doc[] = "\n\ |
| 868 | Get the application data (supplied via set_app_data())\n\ |
| 869 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 870 | @return: The application data\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 871 | "; |
| 872 | static PyObject * |
| 873 | ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args) |
| 874 | { |
| 875 | if (!PyArg_ParseTuple(args, ":get_app_data")) |
| 876 | return NULL; |
| 877 | |
| 878 | Py_INCREF(self->app_data); |
| 879 | return self->app_data; |
| 880 | } |
| 881 | |
| 882 | static char ssl_Context_set_app_data_doc[] = "\n\ |
| 883 | Set the application data (will be returned from get_app_data())\n\ |
| 884 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 885 | @param data: Any Python object\n\ |
| 886 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 887 | "; |
| 888 | static PyObject * |
| 889 | ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args) |
| 890 | { |
| 891 | PyObject *data; |
| 892 | |
| 893 | if (!PyArg_ParseTuple(args, "O:set_app_data", &data)) |
| 894 | return NULL; |
| 895 | |
| 896 | Py_DECREF(self->app_data); |
| 897 | Py_INCREF(data); |
| 898 | self->app_data = data; |
| 899 | |
| 900 | Py_INCREF(Py_None); |
| 901 | return Py_None; |
| 902 | } |
| 903 | |
| 904 | static char ssl_Context_get_cert_store_doc[] = "\n\ |
| 905 | Get the certificate store for the context\n\ |
| 906 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 907 | @return: A X509Store object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 908 | "; |
| 909 | static PyObject * |
| 910 | ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args) |
| 911 | { |
| 912 | X509_STORE *store; |
| 913 | |
| 914 | if (!PyArg_ParseTuple(args, ":get_cert_store")) |
| 915 | return NULL; |
| 916 | |
| 917 | if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL) |
| 918 | { |
| 919 | Py_INCREF(Py_None); |
| 920 | return Py_None; |
| 921 | } |
| 922 | else |
| 923 | { |
| 924 | return (PyObject *)crypto_X509Store_New(store, 0); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | static char ssl_Context_set_options_doc[] = "\n\ |
| 929 | Add options. Options set before are not cleared!\n\ |
| 930 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 931 | @param options: The options to add.\n\ |
| 932 | @return: The new option bitmask.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 933 | "; |
| 934 | static PyObject * |
| 935 | ssl_Context_set_options(ssl_ContextObj *self, PyObject *args) |
| 936 | { |
| 937 | long options; |
| 938 | |
| 939 | if (!PyArg_ParseTuple(args, "l:set_options", &options)) |
| 940 | return NULL; |
| 941 | |
| 942 | return PyInt_FromLong(SSL_CTX_set_options(self->ctx, options)); |
| 943 | } |
| 944 | |
| 945 | |
| 946 | /* |
| 947 | * Member methods in the Context object |
| 948 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 949 | * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS } |
| 950 | * for convenience |
| 951 | * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real |
| 952 | * function with the name 'name' |
| 953 | */ |
| 954 | #define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc } |
| 955 | static PyMethodDef ssl_Context_methods[] = { |
| 956 | ADD_METHOD(load_verify_locations), |
| 957 | ADD_METHOD(set_passwd_cb), |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 958 | ADD_METHOD(set_default_verify_paths), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 959 | ADD_METHOD(use_certificate_chain_file), |
| 960 | ADD_METHOD(use_certificate_file), |
| 961 | ADD_METHOD(use_certificate), |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 962 | ADD_METHOD(add_extra_chain_cert), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 963 | ADD_METHOD(use_privatekey_file), |
| 964 | ADD_METHOD(use_privatekey), |
| 965 | ADD_METHOD(check_privatekey), |
| 966 | ADD_METHOD(load_client_ca), |
| 967 | ADD_METHOD(set_session_id), |
| 968 | ADD_METHOD(set_verify), |
| 969 | ADD_METHOD(set_verify_depth), |
| 970 | ADD_METHOD(get_verify_mode), |
| 971 | ADD_METHOD(get_verify_depth), |
| 972 | ADD_METHOD(load_tmp_dh), |
| 973 | ADD_METHOD(set_cipher_list), |
| 974 | ADD_METHOD(set_timeout), |
| 975 | ADD_METHOD(get_timeout), |
| 976 | ADD_METHOD(set_info_callback), |
| 977 | ADD_METHOD(get_app_data), |
| 978 | ADD_METHOD(set_app_data), |
| 979 | ADD_METHOD(get_cert_store), |
| 980 | ADD_METHOD(set_options), |
| 981 | { NULL, NULL } |
| 982 | }; |
| 983 | #undef ADD_METHOD |
| 984 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 985 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 986 | * Despite the name which might suggest otherwise, this is not the tp_init for |
| 987 | * the Context type. It's just the common initialization code shared by the |
| 988 | * two _{Nn}ew functions below. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 989 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 990 | static ssl_ContextObj* |
| 991 | ssl_Context_init(ssl_ContextObj *self, int i_method) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 992 | SSL_METHOD *method; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 993 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 994 | switch (i_method) { |
| 995 | case ssl_SSLv2_METHOD: |
| 996 | method = SSLv2_method(); |
| 997 | break; |
| 998 | case ssl_SSLv23_METHOD: |
| 999 | method = SSLv23_method(); |
| 1000 | break; |
| 1001 | case ssl_SSLv3_METHOD: |
| 1002 | method = SSLv3_method(); |
| 1003 | break; |
| 1004 | case ssl_TLSv1_METHOD: |
| 1005 | method = TLSv1_method(); |
| 1006 | break; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1007 | default: |
| 1008 | PyErr_SetString(PyExc_ValueError, "No such protocol"); |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1012 | self->ctx = SSL_CTX_new(method); |
| 1013 | Py_INCREF(Py_None); |
| 1014 | self->passphrase_callback = Py_None; |
| 1015 | Py_INCREF(Py_None); |
| 1016 | self->verify_callback = Py_None; |
| 1017 | Py_INCREF(Py_None); |
| 1018 | self->info_callback = Py_None; |
| 1019 | |
| 1020 | Py_INCREF(Py_None); |
| 1021 | self->passphrase_userdata = Py_None; |
| 1022 | |
| 1023 | Py_INCREF(Py_None); |
| 1024 | self->app_data = Py_None; |
| 1025 | |
| 1026 | /* Some initialization that's required to operate smoothly in Python */ |
| 1027 | SSL_CTX_set_app_data(self->ctx, self); |
| 1028 | SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1029 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 1030 | SSL_MODE_AUTO_RETRY); |
| 1031 | |
| 1032 | self->tstate = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1033 | |
| 1034 | return self; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1038 | * This one is exposed in the CObject API. I want to deprecate it. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1039 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1040 | ssl_ContextObj* |
| 1041 | ssl_Context_New(int i_method) { |
| 1042 | ssl_ContextObj *self; |
| 1043 | |
| 1044 | self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type); |
| 1045 | if (self == NULL) { |
| 1046 | return (ssl_ContextObj *)PyErr_NoMemory(); |
| 1047 | } |
| 1048 | self = ssl_Context_init(self, i_method); |
| 1049 | PyObject_GC_Track((PyObject *)self); |
| 1050 | return self; |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | /* |
| 1055 | * This one is the tp_new of the Context type. It's great. |
| 1056 | */ |
| 1057 | static PyObject* |
| 1058 | ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) { |
| 1059 | int i_method; |
| 1060 | ssl_ContextObj *self; |
| 1061 | static char *kwlist[] = {"method", NULL}; |
| 1062 | |
| 1063 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) { |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | |
| 1067 | self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1); |
| 1068 | if (self == NULL) { |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
| 1072 | return (PyObject *)ssl_Context_init(self, i_method); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Call the visitproc on all contained objects. |
| 1077 | * |
| 1078 | * Arguments: self - The Context object |
| 1079 | * visit - Function to call |
| 1080 | * arg - Extra argument to visit |
| 1081 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 1082 | * call that gave non-zero result. |
| 1083 | */ |
| 1084 | static int |
| 1085 | ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg) |
| 1086 | { |
| 1087 | int ret = 0; |
| 1088 | |
| 1089 | if (ret == 0 && self->passphrase_callback != NULL) |
| 1090 | ret = visit((PyObject *)self->passphrase_callback, arg); |
| 1091 | if (ret == 0 && self->passphrase_userdata != NULL) |
| 1092 | ret = visit((PyObject *)self->passphrase_userdata, arg); |
| 1093 | if (ret == 0 && self->verify_callback != NULL) |
| 1094 | ret = visit((PyObject *)self->verify_callback, arg); |
| 1095 | if (ret == 0 && self->info_callback != NULL) |
| 1096 | ret = visit((PyObject *)self->info_callback, arg); |
| 1097 | if (ret == 0 && self->app_data != NULL) |
| 1098 | ret = visit(self->app_data, arg); |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Decref all contained objects and zero the pointers. |
| 1104 | * |
| 1105 | * Arguments: self - The Context object |
| 1106 | * Returns: Always 0. |
| 1107 | */ |
| 1108 | static int |
| 1109 | ssl_Context_clear(ssl_ContextObj *self) |
| 1110 | { |
| 1111 | Py_XDECREF(self->passphrase_callback); |
| 1112 | self->passphrase_callback = NULL; |
| 1113 | Py_XDECREF(self->passphrase_userdata); |
| 1114 | self->passphrase_userdata = NULL; |
| 1115 | Py_XDECREF(self->verify_callback); |
| 1116 | self->verify_callback = NULL; |
| 1117 | Py_XDECREF(self->info_callback); |
| 1118 | self->info_callback = NULL; |
| 1119 | Py_XDECREF(self->app_data); |
| 1120 | self->app_data = NULL; |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Deallocate the memory used by the Context object |
| 1126 | * |
| 1127 | * Arguments: self - The Context object |
| 1128 | * Returns: None |
| 1129 | */ |
| 1130 | static void |
| 1131 | ssl_Context_dealloc(ssl_ContextObj *self) |
| 1132 | { |
| 1133 | PyObject_GC_UnTrack((PyObject *)self); |
| 1134 | SSL_CTX_free(self->ctx); |
| 1135 | ssl_Context_clear(self); |
| 1136 | PyObject_GC_Del(self); |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | PyTypeObject ssl_Context_Type = { |
| 1141 | PyObject_HEAD_INIT(NULL) |
| 1142 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1143 | "OpenSSL.SSL.Context", |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1144 | sizeof(ssl_ContextObj), |
| 1145 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1146 | (destructor)ssl_Context_dealloc, /* tp_dealloc */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1147 | NULL, /* print */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1148 | NULL, /* tp_getattr */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1149 | NULL, /* setattr */ |
| 1150 | NULL, /* compare */ |
| 1151 | NULL, /* repr */ |
| 1152 | NULL, /* as_number */ |
| 1153 | NULL, /* as_sequence */ |
| 1154 | NULL, /* as_mapping */ |
| 1155 | NULL, /* hash */ |
| 1156 | NULL, /* call */ |
| 1157 | NULL, /* str */ |
| 1158 | NULL, /* getattro */ |
| 1159 | NULL, /* setattro */ |
| 1160 | NULL, /* as_buffer */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1161 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1162 | ssl_Context_doc, /* tp_doc */ |
| 1163 | (traverseproc)ssl_Context_traverse, /* tp_traverse */ |
| 1164 | (inquiry)ssl_Context_clear, /* tp_clear */ |
| 1165 | NULL, /* tp_richcompare */ |
| 1166 | 0, /* tp_weaklistoffset */ |
| 1167 | NULL, /* tp_iter */ |
| 1168 | NULL, /* tp_iternext */ |
| 1169 | ssl_Context_methods, /* tp_methods */ |
| 1170 | NULL, /* tp_members */ |
| 1171 | NULL, /* tp_getset */ |
| 1172 | NULL, /* tp_base */ |
| 1173 | NULL, /* tp_dict */ |
| 1174 | NULL, /* tp_descr_get */ |
| 1175 | NULL, /* tp_descr_set */ |
| 1176 | 0, /* tp_dictoffset */ |
| 1177 | NULL, /* tp_init */ |
| 1178 | NULL, /* tp_alloc */ |
| 1179 | ssl_Context_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1180 | }; |
| 1181 | |
| 1182 | |
| 1183 | /* |
| 1184 | * Initialize the Context part of the SSL sub module |
| 1185 | * |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1186 | * Arguments: dict - The OpenSSL.SSL module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1187 | * Returns: 1 for success, 0 otherwise |
| 1188 | */ |
| 1189 | int |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1190 | init_ssl_context(PyObject *module) { |
| 1191 | |
| 1192 | if (PyType_Ready(&ssl_Context_Type) < 0) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1193 | return 0; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) { |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) { |
| 1201 | return 0; |
| 1202 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1203 | |
| 1204 | return 1; |
| 1205 | } |
| 1206 | |