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 |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 16 | # define PYOBJECT_GETATTRSTRING_TYPE const char* |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 17 | #else |
| 18 | # define PYARG_PARSETUPLE_FORMAT char |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 19 | # define PYOBJECT_GETATTRSTRING_TYPE char* |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 20 | #endif |
| 21 | |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 22 | #ifndef MS_WINDOWS |
| 23 | # include <sys/socket.h> |
| 24 | # include <netinet/in.h> |
| 25 | # if !(defined(__BEOS__) || defined(__CYGWIN__)) |
| 26 | # include <netinet/tcp.h> |
| 27 | # endif |
| 28 | #else |
| 29 | # include <winsock.h> |
| 30 | # include <wincrypt.h> |
| 31 | #endif |
| 32 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 33 | #define SSL_MODULE |
| 34 | #include "ssl.h" |
| 35 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 36 | /* |
| 37 | * CALLBACKS |
| 38 | * |
| 39 | * Callbacks work like this: We provide a "global" callback in C which |
| 40 | * transforms the arguments into a Python argument tuple and calls the |
| 41 | * corresponding Python callback, and then parsing the return value back into |
| 42 | * things the C function can return. |
| 43 | * |
| 44 | * Three caveats: |
| 45 | * + How do we find the Context object where the Python callbacks are stored? |
| 46 | * + What about multithreading and execution frames? |
| 47 | * + What about Python callbacks that raise exceptions? |
| 48 | * |
| 49 | * The solution to the first issue is trivial if the callback provides |
| 50 | * "userdata" functionality. Since the only callbacks that don't provide |
| 51 | * userdata do provide a pointer to an SSL structure, we can associate an SSL |
| 52 | * object and a Connection one-to-one via the SSL_set/get_app_data() |
| 53 | * functions. |
| 54 | * |
| 55 | * The solution to the other issue is to rewrite the Py_BEGIN_ALLOW_THREADS |
| 56 | * macro allowing it (or rather a new macro) to specify where to save the |
| 57 | * thread state (in our case, as a member of the Connection/Context object) so |
| 58 | * we can retrieve it again before calling the Python callback. |
| 59 | */ |
| 60 | |
| 61 | /* |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 62 | * Globally defined passphrase callback. This is called from OpenSSL |
| 63 | * internally. The GIL will not be held when this function is invoked. It |
| 64 | * must not be held when the function returns. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 65 | * |
| 66 | * Arguments: buf - Buffer to store the returned passphrase in |
| 67 | * maxlen - Maximum length of the passphrase |
| 68 | * verify - If true, the passphrase callback should ask for a |
| 69 | * password twice and verify they're equal. If false, only |
| 70 | * ask once. |
| 71 | * arg - User data, always a Context object |
| 72 | * Returns: The length of the password if successful, 0 otherwise |
| 73 | */ |
| 74 | static int |
| 75 | global_passphrase_callback(char *buf, int maxlen, int verify, void *arg) |
| 76 | { |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 77 | /* |
| 78 | * Initialize len here because we're always going to return it, and we |
| 79 | * might jump to the return before it gets initialized in any other way. |
| 80 | */ |
| 81 | int len = 0; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 82 | char *str; |
| 83 | PyObject *argv, *ret = NULL; |
| 84 | ssl_ContextObj *ctx = (ssl_ContextObj *)arg; |
| 85 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 86 | /* |
| 87 | * GIL isn't held yet. First things first - acquire it, or any Python API |
| 88 | * we invoke might segfault or blow up the sun. The reverse will be done |
| 89 | * before returning. |
| 90 | */ |
| 91 | MY_END_ALLOW_THREADS(ctx->tstate); |
| 92 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 93 | /* The Python callback is called with a (maxlen,verify,userdata) tuple */ |
| 94 | argv = Py_BuildValue("(iiO)", maxlen, verify, ctx->passphrase_userdata); |
| 95 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 96 | /* |
| 97 | * XXX Didn't check argv to see if it was NULL. -exarkun |
| 98 | */ |
| 99 | ret = PyEval_CallObject(ctx->passphrase_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 100 | Py_DECREF(argv); |
| 101 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 102 | if (ret == NULL) { |
| 103 | /* |
| 104 | * XXX The callback raised an exception. At the very least, it should |
| 105 | * be printed out here. An *actual* solution would be to raise it up |
| 106 | * through OpenSSL. That might be a bit tricky, but it's probably |
| 107 | * possible. -exarkun |
| 108 | */ |
| 109 | goto out; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 112 | if (!PyObject_IsTrue(ret)) { |
| 113 | /* |
| 114 | * Returned "", or None, or something. Treat it as no passphrase. |
| 115 | */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 116 | Py_DECREF(ret); |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | if (!PyString_Check(ret)) { |
| 121 | /* |
| 122 | * XXX Returned something that wasn't a string. This is bogus. We |
| 123 | * should report an error or raise an exception (again, through OpenSSL |
| 124 | * - tricky). -exarkun |
| 125 | */ |
| 126 | Py_DECREF(ret); |
| 127 | goto out; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | len = PyString_Size(ret); |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 131 | if (len > maxlen) { |
| 132 | /* |
| 133 | * XXX Returned more than we said they were allowed to return. Report |
| 134 | * an error or raise an exception (tricky blah blah). -exarkun |
| 135 | */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 136 | len = maxlen; |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 137 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 138 | |
| 139 | str = PyString_AsString(ret); |
| 140 | strncpy(buf, str, len); |
| 141 | Py_XDECREF(ret); |
| 142 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 143 | out: |
| 144 | /* |
| 145 | * This function is returning into OpenSSL. Release the GIL again. |
| 146 | */ |
| 147 | MY_BEGIN_ALLOW_THREADS(ctx->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 148 | return len; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Globally defined verify callback |
| 153 | * |
| 154 | * Arguments: ok - True everything is OK "so far", false otherwise |
| 155 | * x509_ctx - Contains the certificate being checked, the current |
| 156 | * error number and depth, and the Connection we're |
| 157 | * dealing with |
| 158 | * Returns: True if everything is okay, false otherwise |
| 159 | */ |
| 160 | static int |
| 161 | global_verify_callback(int ok, X509_STORE_CTX *x509_ctx) |
| 162 | { |
| 163 | PyObject *argv, *ret; |
| 164 | SSL *ssl; |
| 165 | ssl_ConnectionObj *conn; |
| 166 | crypto_X509Obj *cert; |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 167 | int errnum, errdepth, c_ret; |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 168 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 169 | // Get Connection object to check thread state |
| 170 | ssl = (SSL *)X509_STORE_CTX_get_app_data(x509_ctx); |
| 171 | conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 172 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 173 | MY_END_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 174 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 175 | cert = crypto_X509_New(X509_STORE_CTX_get_current_cert(x509_ctx), 0); |
| 176 | errnum = X509_STORE_CTX_get_error(x509_ctx); |
| 177 | errdepth = X509_STORE_CTX_get_error_depth(x509_ctx); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 178 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 179 | argv = Py_BuildValue("(OOiii)", (PyObject *)conn, (PyObject *)cert, |
| 180 | errnum, errdepth, ok); |
| 181 | Py_DECREF(cert); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 182 | ret = PyEval_CallObject(conn->context->verify_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 183 | Py_DECREF(argv); |
| 184 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 185 | if (ret != NULL && PyObject_IsTrue(ret)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 186 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 187 | Py_DECREF(ret); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 188 | c_ret = 1; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 189 | } else { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 190 | c_ret = 0; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 191 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 192 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 193 | MY_BEGIN_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 194 | return c_ret; |
| 195 | } |
| 196 | |
| 197 | /* |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 198 | * Globally defined info callback. This is called from OpenSSL internally. |
| 199 | * The GIL will not be held when this function is invoked. It must not be held |
| 200 | * when the function returns. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 201 | * |
| 202 | * Arguments: ssl - The Connection |
| 203 | * where - The part of the SSL code that called us |
| 204 | * _ret - The return code of the SSL function that called us |
| 205 | * Returns: None |
| 206 | */ |
| 207 | static void |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 208 | global_info_callback(const SSL *ssl, int where, int _ret) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 209 | { |
| 210 | ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl); |
| 211 | PyObject *argv, *ret; |
| 212 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 213 | /* |
| 214 | * GIL isn't held yet. First things first - acquire it, or any Python API |
| 215 | * we invoke might segfault or blow up the sun. The reverse will be done |
| 216 | * before returning. |
| 217 | */ |
| 218 | MY_END_ALLOW_THREADS(conn->tstate); |
| 219 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 220 | argv = Py_BuildValue("(Oii)", (PyObject *)conn, where, _ret); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 221 | ret = PyEval_CallObject(conn->context->info_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 222 | Py_DECREF(argv); |
| 223 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 224 | if (ret == NULL) { |
| 225 | /* |
| 226 | * XXX - This should be reported somehow. -exarkun |
| 227 | */ |
| 228 | PyErr_Clear(); |
| 229 | } else { |
| 230 | Py_DECREF(ret); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * This function is returning into OpenSSL. Release the GIL again. |
| 235 | */ |
| 236 | MY_BEGIN_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 241 | static char ssl_Context_doc[] = "\n\ |
| 242 | Context(method) -> Context instance\n\ |
| 243 | \n\ |
| 244 | OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\ |
| 245 | connections.\n\ |
| 246 | \n\ |
| 247 | @param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or\n\ |
| 248 | TLSv1_METHOD.\n\ |
| 249 | "; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 250 | |
| 251 | static char ssl_Context_load_verify_locations_doc[] = "\n\ |
| 252 | Let SSL know where we can find trusted certificates for the certificate\n\ |
| 253 | chain\n\ |
| 254 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 255 | @param cafile: In which file we can find the certificates\n\ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 256 | @param capath: In which directory we can find the certificates\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 257 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 258 | "; |
| 259 | static PyObject * |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 260 | ssl_Context_load_verify_locations(ssl_ContextObj *self, PyObject *args) { |
| 261 | char *cafile = NULL; |
| 262 | char *capath = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 263 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 264 | if (!PyArg_ParseTuple(args, "z|z:load_verify_locations", &cafile, &capath)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 265 | return NULL; |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 266 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 267 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 268 | if (!SSL_CTX_load_verify_locations(self->ctx, cafile, capath)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 269 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 270 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 271 | return NULL; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | Py_INCREF(Py_None); |
| 276 | return Py_None; |
| 277 | } |
| 278 | } |
| 279 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 280 | static char ssl_Context_set_default_verify_paths_doc[] = "\n\ |
| 281 | Use the platform-specific CA certificate locations\n\ |
| 282 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 283 | @return: None\n\ |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 284 | "; |
| 285 | static PyObject * |
| 286 | ssl_Context_set_default_verify_paths(ssl_ContextObj *self, PyObject *args) { |
Jean-Paul Calderone | 9eadb96 | 2008-09-07 21:20:44 -0400 | [diff] [blame] | 287 | if (!PyArg_ParseTuple(args, ":set_default_verify_paths")) { |
| 288 | return NULL; |
| 289 | } |
| 290 | |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 291 | /* |
| 292 | * XXX Error handling for SSL_CTX_set_default_verify_paths is untested. |
| 293 | * -exarkun |
| 294 | */ |
| 295 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 296 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 299 | Py_INCREF(Py_None); |
| 300 | return Py_None; |
| 301 | }; |
| 302 | |
| 303 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 304 | static char ssl_Context_set_passwd_cb_doc[] = "\n\ |
| 305 | Set the passphrase callback\n\ |
| 306 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 307 | @param callback: The Python callback to use\n\ |
| 308 | @param userdata: (optional) A Python object which will be given as\n\ |
| 309 | argument to the callback\n\ |
| 310 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 311 | "; |
| 312 | static PyObject * |
| 313 | ssl_Context_set_passwd_cb(ssl_ContextObj *self, PyObject *args) |
| 314 | { |
| 315 | PyObject *callback = NULL, *userdata = Py_None; |
| 316 | |
| 317 | if (!PyArg_ParseTuple(args, "O|O:set_passwd_cb", &callback, &userdata)) |
| 318 | return NULL; |
| 319 | |
| 320 | if (!PyCallable_Check(callback)) |
| 321 | { |
| 322 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | Py_DECREF(self->passphrase_callback); |
| 327 | Py_INCREF(callback); |
| 328 | self->passphrase_callback = callback; |
| 329 | SSL_CTX_set_default_passwd_cb(self->ctx, global_passphrase_callback); |
| 330 | |
| 331 | Py_DECREF(self->passphrase_userdata); |
| 332 | Py_INCREF(userdata); |
| 333 | self->passphrase_userdata = userdata; |
| 334 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, (void *)self); |
| 335 | |
| 336 | Py_INCREF(Py_None); |
| 337 | return Py_None; |
| 338 | } |
| 339 | |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 340 | static PyTypeObject * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 341 | type_modified_error(const char *name) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 342 | PyErr_Format(PyExc_RuntimeError, |
| 343 | "OpenSSL.crypto's '%s' attribute has been modified", |
| 344 | name); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | static PyTypeObject * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 349 | import_crypto_type(const char *name, size_t objsize) { |
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 | } |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 358 | type = PyObject_GetAttrString(module, (PYOBJECT_GETATTRSTRING_TYPE)name); |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 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 * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [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 | static PyTypeObject *crypto_X509_type = NULL; |
| 386 | crypto_X509Obj *cert; |
| 387 | |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 388 | if (!crypto_X509_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 389 | crypto_X509_type = import_crypto_type("X509", sizeof(crypto_X509Obj)); |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 390 | if (!crypto_X509_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 391 | return NULL; |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 392 | } |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 393 | } |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 394 | if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format, |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 395 | crypto_X509_type, &cert)) { |
| 396 | return NULL; |
| 397 | } |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 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 * |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 565 | ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 566 | static PyTypeObject *crypto_PKey_type = NULL; |
| 567 | crypto_PKeyObj *pkey; |
| 568 | |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 569 | if (!crypto_PKey_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 570 | crypto_PKey_type = import_crypto_type("PKey", sizeof(crypto_PKeyObj)); |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 571 | if (!crypto_PKey_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 572 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 573 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 574 | } |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 575 | if (!PyArg_ParseTuple(args, "O!:use_privatekey", crypto_PKey_type, &pkey)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 576 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 577 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 578 | |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 579 | if (!SSL_CTX_use_PrivateKey(self->ctx, pkey->pkey)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 580 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 581 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame^] | 582 | } else { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 583 | Py_INCREF(Py_None); |
| 584 | return Py_None; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | static char ssl_Context_check_privatekey_doc[] = "\n\ |
| 589 | Check that the private key and certificate match up\n\ |
| 590 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 591 | @return: None (raises an exception if something's wrong)\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 592 | "; |
| 593 | static PyObject * |
| 594 | ssl_Context_check_privatekey(ssl_ContextObj *self, PyObject *args) |
| 595 | { |
| 596 | if (!PyArg_ParseTuple(args, ":check_privatekey")) |
| 597 | return NULL; |
| 598 | |
| 599 | if (!SSL_CTX_check_private_key(self->ctx)) |
| 600 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 601 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 602 | return NULL; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | Py_INCREF(Py_None); |
| 607 | return Py_None; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | static char ssl_Context_load_client_ca_doc[] = "\n\ |
| 612 | Load the trusted certificates that will be sent to the client (basically\n\ |
| 613 | telling the client \"These are the guys I trust\")\n\ |
| 614 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 615 | @param cafile: The name of the certificates file\n\ |
| 616 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 617 | "; |
| 618 | static PyObject * |
| 619 | ssl_Context_load_client_ca(ssl_ContextObj *self, PyObject *args) |
| 620 | { |
| 621 | char *cafile; |
| 622 | |
| 623 | if (!PyArg_ParseTuple(args, "s:load_client_ca", &cafile)) |
| 624 | return NULL; |
| 625 | |
| 626 | SSL_CTX_set_client_CA_list(self->ctx, SSL_load_client_CA_file(cafile)); |
| 627 | |
| 628 | Py_INCREF(Py_None); |
| 629 | return Py_None; |
| 630 | } |
| 631 | |
| 632 | static char ssl_Context_set_session_id_doc[] = "\n\ |
| 633 | Set the session identifier, this is needed if you want to do session\n\ |
| 634 | resumption (which, ironically, isn't implemented yet)\n\ |
| 635 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 636 | @param buf: A Python object that can be safely converted to a string\n\ |
| 637 | @returns: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 638 | "; |
| 639 | static PyObject * |
| 640 | ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args) |
| 641 | { |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 642 | unsigned char *buf; |
| 643 | unsigned int len; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 644 | |
| 645 | if (!PyArg_ParseTuple(args, "s#:set_session_id", &buf, &len)) |
| 646 | return NULL; |
| 647 | |
| 648 | if (!SSL_CTX_set_session_id_context(self->ctx, buf, len)) |
| 649 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 650 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | Py_INCREF(Py_None); |
| 656 | return Py_None; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | static char ssl_Context_set_verify_doc[] = "\n\ |
| 661 | Set the verify mode and verify callback\n\ |
| 662 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 663 | @param mode: The verify mode, this is either VERIFY_NONE or\n\ |
| 664 | VERIFY_PEER combined with possible other flags\n\ |
| 665 | @param callback: The Python callback to use\n\ |
| 666 | @return: None\n\ |
Jean-Paul Calderone | 24aedf4 | 2008-03-06 22:01:16 -0500 | [diff] [blame] | 667 | \n\ |
| 668 | See SSL_CTX_set_verify(3SSL) for further details.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 669 | "; |
| 670 | static PyObject * |
| 671 | ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args) |
| 672 | { |
| 673 | int mode; |
| 674 | PyObject *callback = NULL; |
| 675 | |
| 676 | if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback)) |
| 677 | return NULL; |
| 678 | |
| 679 | if (!PyCallable_Check(callback)) |
| 680 | { |
| 681 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 682 | return NULL; |
| 683 | } |
| 684 | |
| 685 | Py_DECREF(self->verify_callback); |
| 686 | Py_INCREF(callback); |
| 687 | self->verify_callback = callback; |
| 688 | SSL_CTX_set_verify(self->ctx, mode, global_verify_callback); |
| 689 | |
| 690 | Py_INCREF(Py_None); |
| 691 | return Py_None; |
| 692 | } |
| 693 | |
| 694 | static char ssl_Context_set_verify_depth_doc[] = "\n\ |
| 695 | Set the verify depth\n\ |
| 696 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 697 | @param depth: An integer specifying the verify depth\n\ |
| 698 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 699 | "; |
| 700 | static PyObject * |
| 701 | ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args) |
| 702 | { |
| 703 | int depth; |
| 704 | |
| 705 | if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth)) |
| 706 | return NULL; |
| 707 | |
| 708 | SSL_CTX_set_verify_depth(self->ctx, depth); |
| 709 | Py_INCREF(Py_None); |
| 710 | return Py_None; |
| 711 | } |
| 712 | |
| 713 | static char ssl_Context_get_verify_mode_doc[] = "\n\ |
| 714 | Get the verify mode\n\ |
| 715 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 716 | @return: The verify mode\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 717 | "; |
| 718 | static PyObject * |
| 719 | ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args) |
| 720 | { |
| 721 | int mode; |
| 722 | |
| 723 | if (!PyArg_ParseTuple(args, ":get_verify_mode")) |
| 724 | return NULL; |
| 725 | |
| 726 | mode = SSL_CTX_get_verify_mode(self->ctx); |
| 727 | return PyInt_FromLong((long)mode); |
| 728 | } |
| 729 | |
| 730 | static char ssl_Context_get_verify_depth_doc[] = "\n\ |
| 731 | Get the verify depth\n\ |
| 732 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 733 | @return: The verify depth\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 734 | "; |
| 735 | static PyObject * |
| 736 | ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args) |
| 737 | { |
| 738 | int depth; |
| 739 | |
| 740 | if (!PyArg_ParseTuple(args, ":get_verify_depth")) |
| 741 | return NULL; |
| 742 | |
| 743 | depth = SSL_CTX_get_verify_depth(self->ctx); |
| 744 | return PyInt_FromLong((long)depth); |
| 745 | } |
| 746 | |
| 747 | static char ssl_Context_load_tmp_dh_doc[] = "\n\ |
| 748 | Load parameters for Ephemeral Diffie-Hellman\n\ |
| 749 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 750 | @param dhfile: The file to load EDH parameters from\n\ |
| 751 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 752 | "; |
| 753 | static PyObject * |
| 754 | ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args) |
| 755 | { |
| 756 | char *dhfile; |
| 757 | BIO *bio; |
| 758 | DH *dh; |
| 759 | |
| 760 | if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile)) |
| 761 | return NULL; |
| 762 | |
| 763 | bio = BIO_new_file(dhfile, "r"); |
| 764 | if (bio == NULL) |
| 765 | return PyErr_NoMemory(); |
| 766 | |
| 767 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 768 | SSL_CTX_set_tmp_dh(self->ctx, dh); |
| 769 | DH_free(dh); |
| 770 | BIO_free(bio); |
| 771 | |
| 772 | Py_INCREF(Py_None); |
| 773 | return Py_None; |
| 774 | } |
| 775 | |
| 776 | static char ssl_Context_set_cipher_list_doc[] = "\n\ |
| 777 | Change the cipher list\n\ |
| 778 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 779 | @param cipher_list: A cipher list, see ciphers(1)\n\ |
| 780 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 781 | "; |
| 782 | static PyObject * |
| 783 | ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args) |
| 784 | { |
| 785 | char *cipher_list; |
| 786 | |
| 787 | if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list)) |
| 788 | return NULL; |
| 789 | |
| 790 | if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list)) |
| 791 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 792 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | Py_INCREF(Py_None); |
| 798 | return Py_None; |
| 799 | } |
| 800 | } |
| 801 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 802 | static char ssl_Context_set_client_ca_list_doc[] = "\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 803 | Set the list of preferred client certificate signers for this server context.\n\ |
| 804 | \n\ |
| 805 | This list of certificate authorities will be sent to the client when the\n\ |
| 806 | server requests a client certificate.\n\ |
| 807 | \n\ |
| 808 | @param certificate_authorities: a sequence of X509Names.\n\ |
| 809 | @return: None\n\ |
| 810 | "; |
| 811 | |
| 812 | static PyObject * |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 813 | ssl_Context_set_client_ca_list(ssl_ContextObj *self, PyObject *args) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 814 | { |
| 815 | static PyTypeObject *X509NameType; |
| 816 | PyObject *sequence, *tuple, *item; |
| 817 | crypto_X509NameObj *name; |
| 818 | X509_NAME *sslname; |
| 819 | STACK_OF(X509_NAME) *CANames; |
| 820 | Py_ssize_t length; |
| 821 | int i; |
| 822 | |
| 823 | if (X509NameType == NULL) { |
| 824 | X509NameType = import_crypto_type("X509Name", sizeof(crypto_X509NameObj)); |
| 825 | if (X509NameType == NULL) { |
| 826 | return NULL; |
| 827 | } |
| 828 | } |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 829 | if (!PyArg_ParseTuple(args, "O:set_client_ca_list", &sequence)) { |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 830 | return NULL; |
| 831 | } |
| 832 | tuple = PySequence_Tuple(sequence); |
| 833 | if (tuple == NULL) { |
| 834 | return NULL; |
| 835 | } |
| 836 | length = PyTuple_Size(tuple); |
| 837 | if (length >= INT_MAX) { |
| 838 | PyErr_SetString(PyExc_ValueError, "client CA list is too long"); |
| 839 | Py_DECREF(tuple); |
| 840 | return NULL; |
| 841 | } |
| 842 | CANames = sk_X509_NAME_new_null(); |
| 843 | if (CANames == NULL) { |
| 844 | Py_DECREF(tuple); |
| 845 | exception_from_error_queue(ssl_Error); |
| 846 | return NULL; |
| 847 | } |
| 848 | for (i = 0; i < length; i++) { |
| 849 | item = PyTuple_GetItem(tuple, i); |
| 850 | if (item->ob_type != X509NameType) { |
| 851 | PyErr_Format(PyExc_TypeError, |
| 852 | "client CAs must be X509Name objects, not %s objects", |
| 853 | item->ob_type->tp_name); |
| 854 | sk_X509_NAME_free(CANames); |
| 855 | Py_DECREF(tuple); |
| 856 | return NULL; |
| 857 | } |
| 858 | name = (crypto_X509NameObj *)item; |
| 859 | sslname = X509_NAME_dup(name->x509_name); |
| 860 | if (sslname == NULL) { |
| 861 | sk_X509_NAME_free(CANames); |
| 862 | Py_DECREF(tuple); |
| 863 | exception_from_error_queue(ssl_Error); |
| 864 | return NULL; |
| 865 | } |
| 866 | if (!sk_X509_NAME_push(CANames, sslname)) { |
| 867 | X509_NAME_free(sslname); |
| 868 | sk_X509_NAME_free(CANames); |
| 869 | Py_DECREF(tuple); |
| 870 | exception_from_error_queue(ssl_Error); |
| 871 | return NULL; |
| 872 | } |
| 873 | } |
| 874 | Py_DECREF(tuple); |
| 875 | SSL_CTX_set_client_CA_list(self->ctx, CANames); |
| 876 | Py_INCREF(Py_None); |
| 877 | return Py_None; |
| 878 | } |
| 879 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 880 | static char ssl_Context_add_client_ca_doc[] = "\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 881 | Add the CA certificate to the list of preferred signers for this context.\n\ |
| 882 | \n\ |
| 883 | The list of certificate authorities will be sent to the client when the\n\ |
| 884 | server requests a client certificate.\n\ |
| 885 | \n\ |
| 886 | @param certificate_authority: certificate authority's X509 certificate.\n\ |
| 887 | @return: None\n\ |
| 888 | "; |
| 889 | |
| 890 | static PyObject * |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 891 | ssl_Context_add_client_ca(ssl_ContextObj *self, PyObject *args) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 892 | { |
| 893 | crypto_X509Obj *cert; |
| 894 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 895 | cert = parse_certificate_argument("O!:add_client_ca", args); |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 896 | if (cert == NULL) { |
| 897 | return NULL; |
| 898 | } |
| 899 | if (!SSL_CTX_add_client_CA(self->ctx, cert->x509)) { |
| 900 | exception_from_error_queue(ssl_Error); |
| 901 | return NULL; |
| 902 | } |
| 903 | Py_INCREF(Py_None); |
| 904 | return Py_None; |
| 905 | } |
| 906 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 907 | static char ssl_Context_set_timeout_doc[] = "\n\ |
| 908 | Set session timeout\n\ |
| 909 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 910 | @param timeout: The timeout in seconds\n\ |
| 911 | @return: The previous session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 912 | "; |
| 913 | static PyObject * |
| 914 | ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args) |
| 915 | { |
| 916 | long t, ret; |
| 917 | |
| 918 | if (!PyArg_ParseTuple(args, "l:set_timeout", &t)) |
| 919 | return NULL; |
| 920 | |
| 921 | ret = SSL_CTX_set_timeout(self->ctx, t); |
| 922 | return PyLong_FromLong(ret); |
| 923 | } |
| 924 | |
| 925 | static char ssl_Context_get_timeout_doc[] = "\n\ |
| 926 | Get the session timeout\n\ |
| 927 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 928 | @return: The session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 929 | "; |
| 930 | static PyObject * |
| 931 | ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args) |
| 932 | { |
| 933 | long ret; |
| 934 | |
| 935 | if (!PyArg_ParseTuple(args, ":get_timeout")) |
| 936 | return NULL; |
| 937 | |
| 938 | ret = SSL_CTX_get_timeout(self->ctx); |
| 939 | return PyLong_FromLong(ret); |
| 940 | } |
| 941 | |
| 942 | static char ssl_Context_set_info_callback_doc[] = "\n\ |
| 943 | Set the info callback\n\ |
| 944 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 945 | @param callback: The Python callback to use\n\ |
| 946 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 947 | "; |
| 948 | static PyObject * |
| 949 | ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args) |
| 950 | { |
| 951 | PyObject *callback; |
| 952 | |
| 953 | if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback)) |
| 954 | return NULL; |
| 955 | |
| 956 | if (!PyCallable_Check(callback)) |
| 957 | { |
| 958 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 959 | return NULL; |
| 960 | } |
| 961 | |
| 962 | Py_DECREF(self->info_callback); |
| 963 | Py_INCREF(callback); |
| 964 | self->info_callback = callback; |
| 965 | SSL_CTX_set_info_callback(self->ctx, global_info_callback); |
| 966 | |
| 967 | Py_INCREF(Py_None); |
| 968 | return Py_None; |
| 969 | } |
| 970 | |
| 971 | static char ssl_Context_get_app_data_doc[] = "\n\ |
| 972 | Get the application data (supplied via set_app_data())\n\ |
| 973 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 974 | @return: The application data\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 975 | "; |
| 976 | static PyObject * |
| 977 | ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args) |
| 978 | { |
| 979 | if (!PyArg_ParseTuple(args, ":get_app_data")) |
| 980 | return NULL; |
| 981 | |
| 982 | Py_INCREF(self->app_data); |
| 983 | return self->app_data; |
| 984 | } |
| 985 | |
| 986 | static char ssl_Context_set_app_data_doc[] = "\n\ |
| 987 | Set the application data (will be returned from get_app_data())\n\ |
| 988 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 989 | @param data: Any Python object\n\ |
| 990 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 991 | "; |
| 992 | static PyObject * |
| 993 | ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args) |
| 994 | { |
| 995 | PyObject *data; |
| 996 | |
| 997 | if (!PyArg_ParseTuple(args, "O:set_app_data", &data)) |
| 998 | return NULL; |
| 999 | |
| 1000 | Py_DECREF(self->app_data); |
| 1001 | Py_INCREF(data); |
| 1002 | self->app_data = data; |
| 1003 | |
| 1004 | Py_INCREF(Py_None); |
| 1005 | return Py_None; |
| 1006 | } |
| 1007 | |
| 1008 | static char ssl_Context_get_cert_store_doc[] = "\n\ |
| 1009 | Get the certificate store for the context\n\ |
| 1010 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1011 | @return: A X509Store object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1012 | "; |
| 1013 | static PyObject * |
| 1014 | ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args) |
| 1015 | { |
| 1016 | X509_STORE *store; |
| 1017 | |
| 1018 | if (!PyArg_ParseTuple(args, ":get_cert_store")) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL) |
| 1022 | { |
| 1023 | Py_INCREF(Py_None); |
| 1024 | return Py_None; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | return (PyObject *)crypto_X509Store_New(store, 0); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | static char ssl_Context_set_options_doc[] = "\n\ |
| 1033 | Add options. Options set before are not cleared!\n\ |
| 1034 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1035 | @param options: The options to add.\n\ |
| 1036 | @return: The new option bitmask.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1037 | "; |
| 1038 | static PyObject * |
| 1039 | ssl_Context_set_options(ssl_ContextObj *self, PyObject *args) |
| 1040 | { |
| 1041 | long options; |
| 1042 | |
| 1043 | if (!PyArg_ParseTuple(args, "l:set_options", &options)) |
| 1044 | return NULL; |
| 1045 | |
| 1046 | return PyInt_FromLong(SSL_CTX_set_options(self->ctx, options)); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | /* |
| 1051 | * Member methods in the Context object |
| 1052 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 1053 | * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS } |
| 1054 | * for convenience |
| 1055 | * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real |
| 1056 | * function with the name 'name' |
| 1057 | */ |
| 1058 | #define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc } |
| 1059 | static PyMethodDef ssl_Context_methods[] = { |
| 1060 | ADD_METHOD(load_verify_locations), |
| 1061 | ADD_METHOD(set_passwd_cb), |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1062 | ADD_METHOD(set_default_verify_paths), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1063 | ADD_METHOD(use_certificate_chain_file), |
| 1064 | ADD_METHOD(use_certificate_file), |
| 1065 | ADD_METHOD(use_certificate), |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 1066 | ADD_METHOD(add_extra_chain_cert), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1067 | ADD_METHOD(use_privatekey_file), |
| 1068 | ADD_METHOD(use_privatekey), |
| 1069 | ADD_METHOD(check_privatekey), |
| 1070 | ADD_METHOD(load_client_ca), |
| 1071 | ADD_METHOD(set_session_id), |
| 1072 | ADD_METHOD(set_verify), |
| 1073 | ADD_METHOD(set_verify_depth), |
| 1074 | ADD_METHOD(get_verify_mode), |
| 1075 | ADD_METHOD(get_verify_depth), |
| 1076 | ADD_METHOD(load_tmp_dh), |
| 1077 | ADD_METHOD(set_cipher_list), |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 1078 | ADD_METHOD(set_client_ca_list), |
| 1079 | ADD_METHOD(add_client_ca), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1080 | ADD_METHOD(set_timeout), |
| 1081 | ADD_METHOD(get_timeout), |
| 1082 | ADD_METHOD(set_info_callback), |
| 1083 | ADD_METHOD(get_app_data), |
| 1084 | ADD_METHOD(set_app_data), |
| 1085 | ADD_METHOD(get_cert_store), |
| 1086 | ADD_METHOD(set_options), |
| 1087 | { NULL, NULL } |
| 1088 | }; |
| 1089 | #undef ADD_METHOD |
| 1090 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1091 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1092 | * Despite the name which might suggest otherwise, this is not the tp_init for |
| 1093 | * the Context type. It's just the common initialization code shared by the |
| 1094 | * two _{Nn}ew functions below. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1095 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1096 | static ssl_ContextObj* |
| 1097 | ssl_Context_init(ssl_ContextObj *self, int i_method) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1098 | SSL_METHOD *method; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1099 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1100 | switch (i_method) { |
| 1101 | case ssl_SSLv2_METHOD: |
| 1102 | method = SSLv2_method(); |
| 1103 | break; |
| 1104 | case ssl_SSLv23_METHOD: |
| 1105 | method = SSLv23_method(); |
| 1106 | break; |
| 1107 | case ssl_SSLv3_METHOD: |
| 1108 | method = SSLv3_method(); |
| 1109 | break; |
| 1110 | case ssl_TLSv1_METHOD: |
| 1111 | method = TLSv1_method(); |
| 1112 | break; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1113 | default: |
| 1114 | PyErr_SetString(PyExc_ValueError, "No such protocol"); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1118 | self->ctx = SSL_CTX_new(method); |
| 1119 | Py_INCREF(Py_None); |
| 1120 | self->passphrase_callback = Py_None; |
| 1121 | Py_INCREF(Py_None); |
| 1122 | self->verify_callback = Py_None; |
| 1123 | Py_INCREF(Py_None); |
| 1124 | self->info_callback = Py_None; |
| 1125 | |
| 1126 | Py_INCREF(Py_None); |
| 1127 | self->passphrase_userdata = Py_None; |
| 1128 | |
| 1129 | Py_INCREF(Py_None); |
| 1130 | self->app_data = Py_None; |
| 1131 | |
| 1132 | /* Some initialization that's required to operate smoothly in Python */ |
| 1133 | SSL_CTX_set_app_data(self->ctx, self); |
| 1134 | SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1135 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 1136 | SSL_MODE_AUTO_RETRY); |
| 1137 | |
| 1138 | self->tstate = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1139 | |
| 1140 | return self; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1144 | * 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] | 1145 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1146 | ssl_ContextObj* |
| 1147 | ssl_Context_New(int i_method) { |
| 1148 | ssl_ContextObj *self; |
| 1149 | |
| 1150 | self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type); |
| 1151 | if (self == NULL) { |
| 1152 | return (ssl_ContextObj *)PyErr_NoMemory(); |
| 1153 | } |
| 1154 | self = ssl_Context_init(self, i_method); |
| 1155 | PyObject_GC_Track((PyObject *)self); |
| 1156 | return self; |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | /* |
| 1161 | * This one is the tp_new of the Context type. It's great. |
| 1162 | */ |
| 1163 | static PyObject* |
| 1164 | ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) { |
| 1165 | int i_method; |
| 1166 | ssl_ContextObj *self; |
| 1167 | static char *kwlist[] = {"method", NULL}; |
| 1168 | |
| 1169 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) { |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | |
| 1173 | self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1); |
| 1174 | if (self == NULL) { |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | |
| 1178 | return (PyObject *)ssl_Context_init(self, i_method); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * Call the visitproc on all contained objects. |
| 1183 | * |
| 1184 | * Arguments: self - The Context object |
| 1185 | * visit - Function to call |
| 1186 | * arg - Extra argument to visit |
| 1187 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 1188 | * call that gave non-zero result. |
| 1189 | */ |
| 1190 | static int |
| 1191 | ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg) |
| 1192 | { |
| 1193 | int ret = 0; |
| 1194 | |
| 1195 | if (ret == 0 && self->passphrase_callback != NULL) |
| 1196 | ret = visit((PyObject *)self->passphrase_callback, arg); |
| 1197 | if (ret == 0 && self->passphrase_userdata != NULL) |
| 1198 | ret = visit((PyObject *)self->passphrase_userdata, arg); |
| 1199 | if (ret == 0 && self->verify_callback != NULL) |
| 1200 | ret = visit((PyObject *)self->verify_callback, arg); |
| 1201 | if (ret == 0 && self->info_callback != NULL) |
| 1202 | ret = visit((PyObject *)self->info_callback, arg); |
| 1203 | if (ret == 0 && self->app_data != NULL) |
| 1204 | ret = visit(self->app_data, arg); |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * Decref all contained objects and zero the pointers. |
| 1210 | * |
| 1211 | * Arguments: self - The Context object |
| 1212 | * Returns: Always 0. |
| 1213 | */ |
| 1214 | static int |
| 1215 | ssl_Context_clear(ssl_ContextObj *self) |
| 1216 | { |
| 1217 | Py_XDECREF(self->passphrase_callback); |
| 1218 | self->passphrase_callback = NULL; |
| 1219 | Py_XDECREF(self->passphrase_userdata); |
| 1220 | self->passphrase_userdata = NULL; |
| 1221 | Py_XDECREF(self->verify_callback); |
| 1222 | self->verify_callback = NULL; |
| 1223 | Py_XDECREF(self->info_callback); |
| 1224 | self->info_callback = NULL; |
| 1225 | Py_XDECREF(self->app_data); |
| 1226 | self->app_data = NULL; |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * Deallocate the memory used by the Context object |
| 1232 | * |
| 1233 | * Arguments: self - The Context object |
| 1234 | * Returns: None |
| 1235 | */ |
| 1236 | static void |
| 1237 | ssl_Context_dealloc(ssl_ContextObj *self) |
| 1238 | { |
| 1239 | PyObject_GC_UnTrack((PyObject *)self); |
| 1240 | SSL_CTX_free(self->ctx); |
| 1241 | ssl_Context_clear(self); |
| 1242 | PyObject_GC_Del(self); |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | PyTypeObject ssl_Context_Type = { |
| 1247 | PyObject_HEAD_INIT(NULL) |
| 1248 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1249 | "OpenSSL.SSL.Context", |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1250 | sizeof(ssl_ContextObj), |
| 1251 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1252 | (destructor)ssl_Context_dealloc, /* tp_dealloc */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1253 | NULL, /* print */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1254 | NULL, /* tp_getattr */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1255 | NULL, /* setattr */ |
| 1256 | NULL, /* compare */ |
| 1257 | NULL, /* repr */ |
| 1258 | NULL, /* as_number */ |
| 1259 | NULL, /* as_sequence */ |
| 1260 | NULL, /* as_mapping */ |
| 1261 | NULL, /* hash */ |
| 1262 | NULL, /* call */ |
| 1263 | NULL, /* str */ |
| 1264 | NULL, /* getattro */ |
| 1265 | NULL, /* setattro */ |
| 1266 | NULL, /* as_buffer */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1267 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1268 | ssl_Context_doc, /* tp_doc */ |
| 1269 | (traverseproc)ssl_Context_traverse, /* tp_traverse */ |
| 1270 | (inquiry)ssl_Context_clear, /* tp_clear */ |
| 1271 | NULL, /* tp_richcompare */ |
| 1272 | 0, /* tp_weaklistoffset */ |
| 1273 | NULL, /* tp_iter */ |
| 1274 | NULL, /* tp_iternext */ |
| 1275 | ssl_Context_methods, /* tp_methods */ |
| 1276 | NULL, /* tp_members */ |
| 1277 | NULL, /* tp_getset */ |
| 1278 | NULL, /* tp_base */ |
| 1279 | NULL, /* tp_dict */ |
| 1280 | NULL, /* tp_descr_get */ |
| 1281 | NULL, /* tp_descr_set */ |
| 1282 | 0, /* tp_dictoffset */ |
| 1283 | NULL, /* tp_init */ |
| 1284 | NULL, /* tp_alloc */ |
| 1285 | ssl_Context_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1286 | }; |
| 1287 | |
| 1288 | |
| 1289 | /* |
| 1290 | * Initialize the Context part of the SSL sub module |
| 1291 | * |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1292 | * Arguments: dict - The OpenSSL.SSL module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1293 | * Returns: 1 for success, 0 otherwise |
| 1294 | */ |
| 1295 | int |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1296 | init_ssl_context(PyObject *module) { |
| 1297 | |
| 1298 | if (PyType_Ready(&ssl_Context_Type) < 0) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1299 | return 0; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) { |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) { |
| 1307 | return 0; |
| 1308 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1309 | |
| 1310 | return 1; |
| 1311 | } |
| 1312 | |