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