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 | /* |
Jean-Paul Calderone | e1fc4ea | 2010-07-30 18:18:00 -0400 | [diff] [blame^] | 104 | * The callback raised an exception. It will be raised by whatever |
| 105 | * Python API triggered this callback. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 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 | /* |
Jean-Paul Calderone | e1fc4ea | 2010-07-30 18:18:00 -0400 | [diff] [blame^] | 120 | * XXX Returned something that wasn't a string. This is bogus. We'll |
| 121 | * return 0 and OpenSSL will treat it as an error, resulting in an |
| 122 | * exception from whatever Python API triggered this callback. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 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 | /* |
Jean-Paul Calderone | e1fc4ea | 2010-07-30 18:18:00 -0400 | [diff] [blame^] | 131 | * Returned more than we said they were allowed to return. Just |
| 132 | * truncate it. Might be better to raise an exception, |
| 133 | * instead. -exarkun |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 134 | */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 135 | len = maxlen; |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 136 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 137 | |
| 138 | str = PyString_AsString(ret); |
| 139 | strncpy(buf, str, len); |
| 140 | Py_XDECREF(ret); |
| 141 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 142 | out: |
| 143 | /* |
| 144 | * This function is returning into OpenSSL. Release the GIL again. |
| 145 | */ |
| 146 | MY_BEGIN_ALLOW_THREADS(ctx->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 147 | return len; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Globally defined verify callback |
| 152 | * |
| 153 | * Arguments: ok - True everything is OK "so far", false otherwise |
| 154 | * x509_ctx - Contains the certificate being checked, the current |
| 155 | * error number and depth, and the Connection we're |
| 156 | * dealing with |
| 157 | * Returns: True if everything is okay, false otherwise |
| 158 | */ |
| 159 | static int |
| 160 | global_verify_callback(int ok, X509_STORE_CTX *x509_ctx) |
| 161 | { |
| 162 | PyObject *argv, *ret; |
| 163 | SSL *ssl; |
| 164 | ssl_ConnectionObj *conn; |
| 165 | crypto_X509Obj *cert; |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 166 | int errnum, errdepth, c_ret; |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 167 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 168 | // Get Connection object to check thread state |
| 169 | ssl = (SSL *)X509_STORE_CTX_get_app_data(x509_ctx); |
| 170 | conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 171 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 172 | MY_END_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 173 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 174 | cert = crypto_X509_New(X509_STORE_CTX_get_current_cert(x509_ctx), 0); |
| 175 | errnum = X509_STORE_CTX_get_error(x509_ctx); |
| 176 | errdepth = X509_STORE_CTX_get_error_depth(x509_ctx); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 177 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 178 | argv = Py_BuildValue("(OOiii)", (PyObject *)conn, (PyObject *)cert, |
| 179 | errnum, errdepth, ok); |
| 180 | Py_DECREF(cert); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 181 | ret = PyEval_CallObject(conn->context->verify_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 182 | Py_DECREF(argv); |
| 183 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 184 | if (ret != NULL && PyObject_IsTrue(ret)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 185 | X509_STORE_CTX_set_error(x509_ctx, X509_V_OK); |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 186 | Py_DECREF(ret); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 187 | c_ret = 1; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 188 | } else { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 189 | c_ret = 0; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 190 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 191 | |
Jean-Paul Calderone | 26aea02 | 2008-09-21 18:47:06 -0400 | [diff] [blame] | 192 | MY_BEGIN_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 193 | return c_ret; |
| 194 | } |
| 195 | |
| 196 | /* |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 197 | * Globally defined info callback. This is called from OpenSSL internally. |
| 198 | * The GIL will not be held when this function is invoked. It must not be held |
| 199 | * when the function returns. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 200 | * |
| 201 | * Arguments: ssl - The Connection |
| 202 | * where - The part of the SSL code that called us |
| 203 | * _ret - The return code of the SSL function that called us |
| 204 | * Returns: None |
| 205 | */ |
| 206 | static void |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 207 | global_info_callback(const SSL *ssl, int where, int _ret) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 208 | { |
| 209 | ssl_ConnectionObj *conn = (ssl_ConnectionObj *)SSL_get_app_data(ssl); |
| 210 | PyObject *argv, *ret; |
| 211 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 212 | /* |
| 213 | * GIL isn't held yet. First things first - acquire it, or any Python API |
| 214 | * we invoke might segfault or blow up the sun. The reverse will be done |
| 215 | * before returning. |
| 216 | */ |
| 217 | MY_END_ALLOW_THREADS(conn->tstate); |
| 218 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 219 | argv = Py_BuildValue("(Oii)", (PyObject *)conn, where, _ret); |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 220 | ret = PyEval_CallObject(conn->context->info_callback, argv); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 221 | Py_DECREF(argv); |
| 222 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 223 | if (ret == NULL) { |
| 224 | /* |
| 225 | * XXX - This should be reported somehow. -exarkun |
| 226 | */ |
| 227 | PyErr_Clear(); |
| 228 | } else { |
| 229 | Py_DECREF(ret); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This function is returning into OpenSSL. Release the GIL again. |
| 234 | */ |
| 235 | MY_BEGIN_ALLOW_THREADS(conn->tstate); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 240 | static char ssl_Context_doc[] = "\n\ |
| 241 | Context(method) -> Context instance\n\ |
| 242 | \n\ |
| 243 | OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\ |
| 244 | connections.\n\ |
| 245 | \n\ |
| 246 | @param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or\n\ |
| 247 | TLSv1_METHOD.\n\ |
| 248 | "; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 249 | |
| 250 | static char ssl_Context_load_verify_locations_doc[] = "\n\ |
| 251 | Let SSL know where we can find trusted certificates for the certificate\n\ |
| 252 | chain\n\ |
| 253 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 254 | @param cafile: In which file we can find the certificates\n\ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 255 | @param capath: In which directory we can find the certificates\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 256 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 257 | "; |
| 258 | static PyObject * |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 259 | ssl_Context_load_verify_locations(ssl_ContextObj *self, PyObject *args) { |
| 260 | char *cafile = NULL; |
| 261 | char *capath = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 262 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 263 | if (!PyArg_ParseTuple(args, "z|z:load_verify_locations", &cafile, &capath)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 264 | return NULL; |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 265 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 266 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 267 | if (!SSL_CTX_load_verify_locations(self->ctx, cafile, capath)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 268 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 269 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 270 | return NULL; |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | Py_INCREF(Py_None); |
| 275 | return Py_None; |
| 276 | } |
| 277 | } |
| 278 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 279 | static char ssl_Context_set_default_verify_paths_doc[] = "\n\ |
| 280 | Use the platform-specific CA certificate locations\n\ |
| 281 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 282 | @return: None\n\ |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 283 | "; |
| 284 | static PyObject * |
| 285 | ssl_Context_set_default_verify_paths(ssl_ContextObj *self, PyObject *args) { |
Jean-Paul Calderone | 9eadb96 | 2008-09-07 21:20:44 -0400 | [diff] [blame] | 286 | if (!PyArg_ParseTuple(args, ":set_default_verify_paths")) { |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 290 | /* |
| 291 | * XXX Error handling for SSL_CTX_set_default_verify_paths is untested. |
| 292 | * -exarkun |
| 293 | */ |
| 294 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 295 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 286b192 | 2008-09-07 21:35:38 -0400 | [diff] [blame] | 296 | return NULL; |
| 297 | } |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 298 | Py_INCREF(Py_None); |
| 299 | return Py_None; |
| 300 | }; |
| 301 | |
| 302 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 303 | static char ssl_Context_set_passwd_cb_doc[] = "\n\ |
| 304 | Set the passphrase callback\n\ |
| 305 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 306 | @param callback: The Python callback to use\n\ |
| 307 | @param userdata: (optional) A Python object which will be given as\n\ |
| 308 | argument to the callback\n\ |
| 309 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 310 | "; |
| 311 | static PyObject * |
| 312 | ssl_Context_set_passwd_cb(ssl_ContextObj *self, PyObject *args) |
| 313 | { |
| 314 | PyObject *callback = NULL, *userdata = Py_None; |
| 315 | |
| 316 | if (!PyArg_ParseTuple(args, "O|O:set_passwd_cb", &callback, &userdata)) |
| 317 | return NULL; |
| 318 | |
| 319 | if (!PyCallable_Check(callback)) |
| 320 | { |
| 321 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | Py_DECREF(self->passphrase_callback); |
| 326 | Py_INCREF(callback); |
| 327 | self->passphrase_callback = callback; |
| 328 | SSL_CTX_set_default_passwd_cb(self->ctx, global_passphrase_callback); |
| 329 | |
| 330 | Py_DECREF(self->passphrase_userdata); |
| 331 | Py_INCREF(userdata); |
| 332 | self->passphrase_userdata = userdata; |
| 333 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, (void *)self); |
| 334 | |
| 335 | Py_INCREF(Py_None); |
| 336 | return Py_None; |
| 337 | } |
| 338 | |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 339 | static PyTypeObject * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 340 | type_modified_error(const char *name) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 341 | PyErr_Format(PyExc_RuntimeError, |
| 342 | "OpenSSL.crypto's '%s' attribute has been modified", |
| 343 | name); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | static PyTypeObject * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 348 | import_crypto_type(const char *name, size_t objsize) { |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 349 | PyObject *module, *type, *name_attr; |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 350 | PyTypeObject *res; |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 351 | int right_name; |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 352 | |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 353 | module = PyImport_ImportModule("OpenSSL.crypto"); |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 354 | if (module == NULL) { |
| 355 | return NULL; |
| 356 | } |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 357 | type = PyObject_GetAttrString(module, (PYOBJECT_GETATTRSTRING_TYPE)name); |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 358 | Py_DECREF(module); |
| 359 | if (type == NULL) { |
| 360 | return NULL; |
| 361 | } |
| 362 | if (!(PyType_Check(type))) { |
| 363 | Py_DECREF(type); |
| 364 | return type_modified_error(name); |
| 365 | } |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 366 | name_attr = PyObject_GetAttrString(type, "__name__"); |
| 367 | if (name_attr == NULL) { |
| 368 | Py_DECREF(type); |
| 369 | return NULL; |
| 370 | } |
| 371 | right_name = (PyString_CheckExact(name_attr) && |
| 372 | strcmp(name, PyString_AsString(name_attr)) == 0); |
| 373 | Py_DECREF(name_attr); |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 374 | res = (PyTypeObject *)type; |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 375 | if (!right_name || res->tp_basicsize != objsize) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 376 | Py_DECREF(type); |
| 377 | return type_modified_error(name); |
| 378 | } |
| 379 | return res; |
| 380 | } |
| 381 | |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 382 | static crypto_X509Obj * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 383 | parse_certificate_argument(const char* format, PyObject* args) { |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 384 | static PyTypeObject *crypto_X509_type = NULL; |
| 385 | crypto_X509Obj *cert; |
| 386 | |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 387 | if (!crypto_X509_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 388 | crypto_X509_type = import_crypto_type("X509", sizeof(crypto_X509Obj)); |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 389 | if (!crypto_X509_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 390 | return NULL; |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 391 | } |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 392 | } |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 393 | if (!PyArg_ParseTuple(args, (PYARG_PARSETUPLE_FORMAT *)format, |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 394 | crypto_X509_type, &cert)) { |
| 395 | return NULL; |
| 396 | } |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 397 | return cert; |
| 398 | } |
| 399 | |
| 400 | static char ssl_Context_add_extra_chain_cert_doc[] = "\n\ |
| 401 | Add certificate to chain\n\ |
| 402 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 403 | @param certobj: The X509 certificate object to add to the chain\n\ |
| 404 | @return: None\n\ |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 405 | "; |
| 406 | |
| 407 | static PyObject * |
| 408 | ssl_Context_add_extra_chain_cert(ssl_ContextObj *self, PyObject *args) |
| 409 | { |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 410 | X509* cert_original; |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 411 | crypto_X509Obj *cert = parse_certificate_argument( |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 412 | "O!:add_extra_chain_cert", args); |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 413 | if (cert == NULL) |
| 414 | { |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 415 | return NULL; |
| 416 | } |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 417 | if (!(cert_original = X509_dup(cert->x509))) |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 418 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 419 | /* exception_from_error_queue(ssl_Error); */ |
Jean-Paul Calderone | 0ce9807 | 2008-02-18 23:22:29 -0500 | [diff] [blame] | 420 | PyErr_SetString(PyExc_RuntimeError, "X509_dup failed"); |
| 421 | return NULL; |
| 422 | } |
| 423 | if (!SSL_CTX_add_extra_chain_cert(self->ctx, cert_original)) |
| 424 | { |
| 425 | X509_free(cert_original); |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 426 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 427 | return NULL; |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | Py_INCREF(Py_None); |
| 432 | return Py_None; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 437 | static char ssl_Context_use_certificate_chain_file_doc[] = "\n\ |
| 438 | Load a certificate chain from a file\n\ |
| 439 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 440 | @param certfile: The name of the certificate chain file\n\ |
| 441 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 442 | "; |
| 443 | static PyObject * |
| 444 | ssl_Context_use_certificate_chain_file(ssl_ContextObj *self, PyObject *args) |
| 445 | { |
| 446 | char *certfile; |
| 447 | |
| 448 | if (!PyArg_ParseTuple(args, "s:use_certificate_chain_file", &certfile)) |
| 449 | return NULL; |
| 450 | |
| 451 | if (!SSL_CTX_use_certificate_chain_file(self->ctx, certfile)) |
| 452 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 453 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 454 | return NULL; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | Py_INCREF(Py_None); |
| 459 | return Py_None; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | static char ssl_Context_use_certificate_file_doc[] = "\n\ |
| 465 | Load a certificate from a file\n\ |
| 466 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 467 | @param certfile: The name of the certificate file\n\ |
| 468 | @param filetype: (optional) The encoding of the file, default is PEM\n\ |
| 469 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 470 | "; |
| 471 | static PyObject * |
| 472 | ssl_Context_use_certificate_file(ssl_ContextObj *self, PyObject *args) |
| 473 | { |
| 474 | char *certfile; |
| 475 | int filetype = SSL_FILETYPE_PEM; |
| 476 | |
| 477 | if (!PyArg_ParseTuple(args, "s|i:use_certificate_file", &certfile, &filetype)) |
| 478 | return NULL; |
| 479 | |
| 480 | if (!SSL_CTX_use_certificate_file(self->ctx, certfile, filetype)) |
| 481 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 482 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 483 | return NULL; |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | Py_INCREF(Py_None); |
| 488 | return Py_None; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static char ssl_Context_use_certificate_doc[] = "\n\ |
| 493 | Load a certificate from a X509 object\n\ |
| 494 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 495 | @param cert: The X509 object\n\ |
| 496 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 497 | "; |
| 498 | static PyObject * |
| 499 | ssl_Context_use_certificate(ssl_ContextObj *self, PyObject *args) |
| 500 | { |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 501 | crypto_X509Obj *cert = parse_certificate_argument( |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 502 | "O!:use_certificate", args); |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 503 | if (cert == NULL) { |
| 504 | return NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 505 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 506 | |
| 507 | if (!SSL_CTX_use_certificate(self->ctx, cert->x509)) |
| 508 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 509 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 510 | return NULL; |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | Py_INCREF(Py_None); |
| 515 | return Py_None; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static char ssl_Context_use_privatekey_file_doc[] = "\n\ |
| 520 | Load a private key from a file\n\ |
| 521 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 522 | @param keyfile: The name of the key file\n\ |
| 523 | @param filetype: (optional) The encoding of the file, default is PEM\n\ |
| 524 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 525 | "; |
| 526 | static PyObject * |
| 527 | ssl_Context_use_privatekey_file(ssl_ContextObj *self, PyObject *args) |
| 528 | { |
| 529 | char *keyfile; |
| 530 | int filetype = SSL_FILETYPE_PEM, ret; |
| 531 | |
| 532 | if (!PyArg_ParseTuple(args, "s|i:use_privatekey_file", &keyfile, &filetype)) |
| 533 | return NULL; |
| 534 | |
| 535 | MY_BEGIN_ALLOW_THREADS(self->tstate); |
| 536 | ret = SSL_CTX_use_PrivateKey_file(self->ctx, keyfile, filetype); |
| 537 | MY_END_ALLOW_THREADS(self->tstate); |
| 538 | |
| 539 | if (PyErr_Occurred()) |
| 540 | { |
| 541 | flush_error_queue(); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | if (!ret) |
| 546 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 547 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 548 | return NULL; |
| 549 | } |
| 550 | else |
| 551 | { |
| 552 | Py_INCREF(Py_None); |
| 553 | return Py_None; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static char ssl_Context_use_privatekey_doc[] = "\n\ |
| 558 | Load a private key from a PKey object\n\ |
| 559 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 560 | @param pkey: The PKey object\n\ |
| 561 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 562 | "; |
| 563 | static PyObject * |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 564 | ssl_Context_use_privatekey(ssl_ContextObj *self, PyObject *args) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 565 | static PyTypeObject *crypto_PKey_type = NULL; |
| 566 | crypto_PKeyObj *pkey; |
| 567 | |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 568 | if (!crypto_PKey_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 569 | crypto_PKey_type = import_crypto_type("PKey", sizeof(crypto_PKeyObj)); |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 570 | if (!crypto_PKey_type) { |
Ziga Seilnacht | 6079e37 | 2009-08-31 20:52:30 +0200 | [diff] [blame] | 571 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 572 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 573 | } |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 574 | if (!PyArg_ParseTuple(args, "O!:use_privatekey", crypto_PKey_type, &pkey)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 575 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 576 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 577 | |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 578 | if (!SSL_CTX_use_PrivateKey(self->ctx, pkey->pkey)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 579 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 580 | return NULL; |
Jean-Paul Calderone | f606a84 | 2009-10-24 14:08:29 -0400 | [diff] [blame] | 581 | } else { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 582 | Py_INCREF(Py_None); |
| 583 | return Py_None; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static char ssl_Context_check_privatekey_doc[] = "\n\ |
| 588 | Check that the private key and certificate match up\n\ |
| 589 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 590 | @return: None (raises an exception if something's wrong)\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 591 | "; |
| 592 | static PyObject * |
| 593 | ssl_Context_check_privatekey(ssl_ContextObj *self, PyObject *args) |
| 594 | { |
| 595 | if (!PyArg_ParseTuple(args, ":check_privatekey")) |
| 596 | return NULL; |
| 597 | |
| 598 | if (!SSL_CTX_check_private_key(self->ctx)) |
| 599 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 600 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | Py_INCREF(Py_None); |
| 606 | return Py_None; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static char ssl_Context_load_client_ca_doc[] = "\n\ |
| 611 | Load the trusted certificates that will be sent to the client (basically\n\ |
| 612 | telling the client \"These are the guys I trust\")\n\ |
| 613 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 614 | @param cafile: The name of the certificates file\n\ |
| 615 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 616 | "; |
| 617 | static PyObject * |
| 618 | ssl_Context_load_client_ca(ssl_ContextObj *self, PyObject *args) |
| 619 | { |
| 620 | char *cafile; |
| 621 | |
| 622 | if (!PyArg_ParseTuple(args, "s:load_client_ca", &cafile)) |
| 623 | return NULL; |
| 624 | |
| 625 | SSL_CTX_set_client_CA_list(self->ctx, SSL_load_client_CA_file(cafile)); |
| 626 | |
| 627 | Py_INCREF(Py_None); |
| 628 | return Py_None; |
| 629 | } |
| 630 | |
| 631 | static char ssl_Context_set_session_id_doc[] = "\n\ |
| 632 | Set the session identifier, this is needed if you want to do session\n\ |
| 633 | resumption (which, ironically, isn't implemented yet)\n\ |
| 634 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 635 | @param buf: A Python object that can be safely converted to a string\n\ |
| 636 | @returns: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 637 | "; |
| 638 | static PyObject * |
| 639 | ssl_Context_set_session_id(ssl_ContextObj *self, PyObject *args) |
| 640 | { |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 641 | unsigned char *buf; |
| 642 | unsigned int len; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 643 | |
| 644 | if (!PyArg_ParseTuple(args, "s#:set_session_id", &buf, &len)) |
| 645 | return NULL; |
| 646 | |
| 647 | if (!SSL_CTX_set_session_id_context(self->ctx, buf, len)) |
| 648 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 649 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | Py_INCREF(Py_None); |
| 655 | return Py_None; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | static char ssl_Context_set_verify_doc[] = "\n\ |
| 660 | Set the verify mode and verify callback\n\ |
| 661 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 662 | @param mode: The verify mode, this is either VERIFY_NONE or\n\ |
| 663 | VERIFY_PEER combined with possible other flags\n\ |
| 664 | @param callback: The Python callback to use\n\ |
| 665 | @return: None\n\ |
Jean-Paul Calderone | 24aedf4 | 2008-03-06 22:01:16 -0500 | [diff] [blame] | 666 | \n\ |
| 667 | See SSL_CTX_set_verify(3SSL) for further details.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 668 | "; |
| 669 | static PyObject * |
| 670 | ssl_Context_set_verify(ssl_ContextObj *self, PyObject *args) |
| 671 | { |
| 672 | int mode; |
| 673 | PyObject *callback = NULL; |
| 674 | |
| 675 | if (!PyArg_ParseTuple(args, "iO:set_verify", &mode, &callback)) |
| 676 | return NULL; |
| 677 | |
| 678 | if (!PyCallable_Check(callback)) |
| 679 | { |
| 680 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | Py_DECREF(self->verify_callback); |
| 685 | Py_INCREF(callback); |
| 686 | self->verify_callback = callback; |
| 687 | SSL_CTX_set_verify(self->ctx, mode, global_verify_callback); |
| 688 | |
| 689 | Py_INCREF(Py_None); |
| 690 | return Py_None; |
| 691 | } |
| 692 | |
| 693 | static char ssl_Context_set_verify_depth_doc[] = "\n\ |
| 694 | Set the verify depth\n\ |
| 695 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 696 | @param depth: An integer specifying the verify depth\n\ |
| 697 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 698 | "; |
| 699 | static PyObject * |
| 700 | ssl_Context_set_verify_depth(ssl_ContextObj *self, PyObject *args) |
| 701 | { |
| 702 | int depth; |
| 703 | |
| 704 | if (!PyArg_ParseTuple(args, "i:set_verify_depth", &depth)) |
| 705 | return NULL; |
| 706 | |
| 707 | SSL_CTX_set_verify_depth(self->ctx, depth); |
| 708 | Py_INCREF(Py_None); |
| 709 | return Py_None; |
| 710 | } |
| 711 | |
| 712 | static char ssl_Context_get_verify_mode_doc[] = "\n\ |
| 713 | Get the verify mode\n\ |
| 714 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 715 | @return: The verify mode\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 716 | "; |
| 717 | static PyObject * |
| 718 | ssl_Context_get_verify_mode(ssl_ContextObj *self, PyObject *args) |
| 719 | { |
| 720 | int mode; |
| 721 | |
| 722 | if (!PyArg_ParseTuple(args, ":get_verify_mode")) |
| 723 | return NULL; |
| 724 | |
| 725 | mode = SSL_CTX_get_verify_mode(self->ctx); |
| 726 | return PyInt_FromLong((long)mode); |
| 727 | } |
| 728 | |
| 729 | static char ssl_Context_get_verify_depth_doc[] = "\n\ |
| 730 | Get the verify depth\n\ |
| 731 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 732 | @return: The verify depth\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 733 | "; |
| 734 | static PyObject * |
| 735 | ssl_Context_get_verify_depth(ssl_ContextObj *self, PyObject *args) |
| 736 | { |
| 737 | int depth; |
| 738 | |
| 739 | if (!PyArg_ParseTuple(args, ":get_verify_depth")) |
| 740 | return NULL; |
| 741 | |
| 742 | depth = SSL_CTX_get_verify_depth(self->ctx); |
| 743 | return PyInt_FromLong((long)depth); |
| 744 | } |
| 745 | |
| 746 | static char ssl_Context_load_tmp_dh_doc[] = "\n\ |
| 747 | Load parameters for Ephemeral Diffie-Hellman\n\ |
| 748 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 749 | @param dhfile: The file to load EDH parameters from\n\ |
| 750 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 751 | "; |
| 752 | static PyObject * |
| 753 | ssl_Context_load_tmp_dh(ssl_ContextObj *self, PyObject *args) |
| 754 | { |
| 755 | char *dhfile; |
| 756 | BIO *bio; |
| 757 | DH *dh; |
| 758 | |
| 759 | if (!PyArg_ParseTuple(args, "s:load_tmp_dh", &dhfile)) |
| 760 | return NULL; |
| 761 | |
| 762 | bio = BIO_new_file(dhfile, "r"); |
| 763 | if (bio == NULL) |
| 764 | return PyErr_NoMemory(); |
| 765 | |
| 766 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 767 | SSL_CTX_set_tmp_dh(self->ctx, dh); |
| 768 | DH_free(dh); |
| 769 | BIO_free(bio); |
| 770 | |
| 771 | Py_INCREF(Py_None); |
| 772 | return Py_None; |
| 773 | } |
| 774 | |
| 775 | static char ssl_Context_set_cipher_list_doc[] = "\n\ |
| 776 | Change the cipher list\n\ |
| 777 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 778 | @param cipher_list: A cipher list, see ciphers(1)\n\ |
| 779 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 780 | "; |
| 781 | static PyObject * |
| 782 | ssl_Context_set_cipher_list(ssl_ContextObj *self, PyObject *args) |
| 783 | { |
| 784 | char *cipher_list; |
| 785 | |
| 786 | if (!PyArg_ParseTuple(args, "s:set_cipher_list", &cipher_list)) |
| 787 | return NULL; |
| 788 | |
| 789 | if (!SSL_CTX_set_cipher_list(self->ctx, cipher_list)) |
| 790 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 791 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 792 | return NULL; |
| 793 | } |
| 794 | else |
| 795 | { |
| 796 | Py_INCREF(Py_None); |
| 797 | return Py_None; |
| 798 | } |
| 799 | } |
| 800 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 801 | static char ssl_Context_set_client_ca_list_doc[] = "\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 802 | Set the list of preferred client certificate signers for this server context.\n\ |
| 803 | \n\ |
| 804 | This list of certificate authorities will be sent to the client when the\n\ |
| 805 | server requests a client certificate.\n\ |
| 806 | \n\ |
| 807 | @param certificate_authorities: a sequence of X509Names.\n\ |
| 808 | @return: None\n\ |
| 809 | "; |
| 810 | |
| 811 | static PyObject * |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 812 | ssl_Context_set_client_ca_list(ssl_ContextObj *self, PyObject *args) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 813 | { |
| 814 | static PyTypeObject *X509NameType; |
| 815 | PyObject *sequence, *tuple, *item; |
| 816 | crypto_X509NameObj *name; |
| 817 | X509_NAME *sslname; |
| 818 | STACK_OF(X509_NAME) *CANames; |
| 819 | Py_ssize_t length; |
| 820 | int i; |
| 821 | |
| 822 | if (X509NameType == NULL) { |
| 823 | X509NameType = import_crypto_type("X509Name", sizeof(crypto_X509NameObj)); |
| 824 | if (X509NameType == NULL) { |
| 825 | return NULL; |
| 826 | } |
| 827 | } |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 828 | if (!PyArg_ParseTuple(args, "O:set_client_ca_list", &sequence)) { |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 829 | return NULL; |
| 830 | } |
| 831 | tuple = PySequence_Tuple(sequence); |
| 832 | if (tuple == NULL) { |
| 833 | return NULL; |
| 834 | } |
| 835 | length = PyTuple_Size(tuple); |
| 836 | if (length >= INT_MAX) { |
| 837 | PyErr_SetString(PyExc_ValueError, "client CA list is too long"); |
| 838 | Py_DECREF(tuple); |
| 839 | return NULL; |
| 840 | } |
| 841 | CANames = sk_X509_NAME_new_null(); |
| 842 | if (CANames == NULL) { |
| 843 | Py_DECREF(tuple); |
| 844 | exception_from_error_queue(ssl_Error); |
| 845 | return NULL; |
| 846 | } |
| 847 | for (i = 0; i < length; i++) { |
| 848 | item = PyTuple_GetItem(tuple, i); |
| 849 | if (item->ob_type != X509NameType) { |
| 850 | PyErr_Format(PyExc_TypeError, |
| 851 | "client CAs must be X509Name objects, not %s objects", |
| 852 | item->ob_type->tp_name); |
| 853 | sk_X509_NAME_free(CANames); |
| 854 | Py_DECREF(tuple); |
| 855 | return NULL; |
| 856 | } |
| 857 | name = (crypto_X509NameObj *)item; |
| 858 | sslname = X509_NAME_dup(name->x509_name); |
| 859 | if (sslname == NULL) { |
| 860 | sk_X509_NAME_free(CANames); |
| 861 | Py_DECREF(tuple); |
| 862 | exception_from_error_queue(ssl_Error); |
| 863 | return NULL; |
| 864 | } |
| 865 | if (!sk_X509_NAME_push(CANames, sslname)) { |
| 866 | X509_NAME_free(sslname); |
| 867 | sk_X509_NAME_free(CANames); |
| 868 | Py_DECREF(tuple); |
| 869 | exception_from_error_queue(ssl_Error); |
| 870 | return NULL; |
| 871 | } |
| 872 | } |
| 873 | Py_DECREF(tuple); |
| 874 | SSL_CTX_set_client_CA_list(self->ctx, CANames); |
| 875 | Py_INCREF(Py_None); |
| 876 | return Py_None; |
| 877 | } |
| 878 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 879 | static char ssl_Context_add_client_ca_doc[] = "\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 880 | Add the CA certificate to the list of preferred signers for this context.\n\ |
| 881 | \n\ |
| 882 | The list of certificate authorities will be sent to the client when the\n\ |
| 883 | server requests a client certificate.\n\ |
| 884 | \n\ |
| 885 | @param certificate_authority: certificate authority's X509 certificate.\n\ |
| 886 | @return: None\n\ |
| 887 | "; |
| 888 | |
| 889 | static PyObject * |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 890 | ssl_Context_add_client_ca(ssl_ContextObj *self, PyObject *args) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 891 | { |
| 892 | crypto_X509Obj *cert; |
| 893 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 894 | cert = parse_certificate_argument("O!:add_client_ca", args); |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 895 | if (cert == NULL) { |
| 896 | return NULL; |
| 897 | } |
| 898 | if (!SSL_CTX_add_client_CA(self->ctx, cert->x509)) { |
| 899 | exception_from_error_queue(ssl_Error); |
| 900 | return NULL; |
| 901 | } |
| 902 | Py_INCREF(Py_None); |
| 903 | return Py_None; |
| 904 | } |
| 905 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 906 | static char ssl_Context_set_timeout_doc[] = "\n\ |
| 907 | Set session timeout\n\ |
| 908 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 909 | @param timeout: The timeout in seconds\n\ |
| 910 | @return: The previous session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 911 | "; |
| 912 | static PyObject * |
| 913 | ssl_Context_set_timeout(ssl_ContextObj *self, PyObject *args) |
| 914 | { |
| 915 | long t, ret; |
| 916 | |
| 917 | if (!PyArg_ParseTuple(args, "l:set_timeout", &t)) |
| 918 | return NULL; |
| 919 | |
| 920 | ret = SSL_CTX_set_timeout(self->ctx, t); |
| 921 | return PyLong_FromLong(ret); |
| 922 | } |
| 923 | |
| 924 | static char ssl_Context_get_timeout_doc[] = "\n\ |
| 925 | Get the session timeout\n\ |
| 926 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 927 | @return: The session timeout\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 928 | "; |
| 929 | static PyObject * |
| 930 | ssl_Context_get_timeout(ssl_ContextObj *self, PyObject *args) |
| 931 | { |
| 932 | long ret; |
| 933 | |
| 934 | if (!PyArg_ParseTuple(args, ":get_timeout")) |
| 935 | return NULL; |
| 936 | |
| 937 | ret = SSL_CTX_get_timeout(self->ctx); |
| 938 | return PyLong_FromLong(ret); |
| 939 | } |
| 940 | |
| 941 | static char ssl_Context_set_info_callback_doc[] = "\n\ |
| 942 | Set the info callback\n\ |
| 943 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 944 | @param callback: The Python callback to use\n\ |
| 945 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 946 | "; |
| 947 | static PyObject * |
| 948 | ssl_Context_set_info_callback(ssl_ContextObj *self, PyObject *args) |
| 949 | { |
| 950 | PyObject *callback; |
| 951 | |
| 952 | if (!PyArg_ParseTuple(args, "O:set_info_callback", &callback)) |
| 953 | return NULL; |
| 954 | |
| 955 | if (!PyCallable_Check(callback)) |
| 956 | { |
| 957 | PyErr_SetString(PyExc_TypeError, "expected PyCallable"); |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | Py_DECREF(self->info_callback); |
| 962 | Py_INCREF(callback); |
| 963 | self->info_callback = callback; |
| 964 | SSL_CTX_set_info_callback(self->ctx, global_info_callback); |
| 965 | |
| 966 | Py_INCREF(Py_None); |
| 967 | return Py_None; |
| 968 | } |
| 969 | |
| 970 | static char ssl_Context_get_app_data_doc[] = "\n\ |
| 971 | Get the application data (supplied via set_app_data())\n\ |
| 972 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 973 | @return: The application data\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 974 | "; |
| 975 | static PyObject * |
| 976 | ssl_Context_get_app_data(ssl_ContextObj *self, PyObject *args) |
| 977 | { |
| 978 | if (!PyArg_ParseTuple(args, ":get_app_data")) |
| 979 | return NULL; |
| 980 | |
| 981 | Py_INCREF(self->app_data); |
| 982 | return self->app_data; |
| 983 | } |
| 984 | |
| 985 | static char ssl_Context_set_app_data_doc[] = "\n\ |
| 986 | Set the application data (will be returned from get_app_data())\n\ |
| 987 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 988 | @param data: Any Python object\n\ |
| 989 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 990 | "; |
| 991 | static PyObject * |
| 992 | ssl_Context_set_app_data(ssl_ContextObj *self, PyObject *args) |
| 993 | { |
| 994 | PyObject *data; |
| 995 | |
| 996 | if (!PyArg_ParseTuple(args, "O:set_app_data", &data)) |
| 997 | return NULL; |
| 998 | |
| 999 | Py_DECREF(self->app_data); |
| 1000 | Py_INCREF(data); |
| 1001 | self->app_data = data; |
| 1002 | |
| 1003 | Py_INCREF(Py_None); |
| 1004 | return Py_None; |
| 1005 | } |
| 1006 | |
| 1007 | static char ssl_Context_get_cert_store_doc[] = "\n\ |
| 1008 | Get the certificate store for the context\n\ |
| 1009 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1010 | @return: A X509Store object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1011 | "; |
| 1012 | static PyObject * |
| 1013 | ssl_Context_get_cert_store(ssl_ContextObj *self, PyObject *args) |
| 1014 | { |
| 1015 | X509_STORE *store; |
| 1016 | |
| 1017 | if (!PyArg_ParseTuple(args, ":get_cert_store")) |
| 1018 | return NULL; |
| 1019 | |
| 1020 | if ((store = SSL_CTX_get_cert_store(self->ctx)) == NULL) |
| 1021 | { |
| 1022 | Py_INCREF(Py_None); |
| 1023 | return Py_None; |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | return (PyObject *)crypto_X509Store_New(store, 0); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | static char ssl_Context_set_options_doc[] = "\n\ |
| 1032 | Add options. Options set before are not cleared!\n\ |
| 1033 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1034 | @param options: The options to add.\n\ |
| 1035 | @return: The new option bitmask.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1036 | "; |
| 1037 | static PyObject * |
| 1038 | ssl_Context_set_options(ssl_ContextObj *self, PyObject *args) |
| 1039 | { |
| 1040 | long options; |
| 1041 | |
| 1042 | if (!PyArg_ParseTuple(args, "l:set_options", &options)) |
| 1043 | return NULL; |
| 1044 | |
| 1045 | return PyInt_FromLong(SSL_CTX_set_options(self->ctx, options)); |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | /* |
| 1050 | * Member methods in the Context object |
| 1051 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 1052 | * { 'name', (PyCFunction)ssl_Context_name, METH_VARARGS } |
| 1053 | * for convenience |
| 1054 | * ADD_ALIAS(name,real) creates an "alias" of the ssl_Context_real |
| 1055 | * function with the name 'name' |
| 1056 | */ |
| 1057 | #define ADD_METHOD(name) { #name, (PyCFunction)ssl_Context_##name, METH_VARARGS, ssl_Context_##name##_doc } |
| 1058 | static PyMethodDef ssl_Context_methods[] = { |
| 1059 | ADD_METHOD(load_verify_locations), |
| 1060 | ADD_METHOD(set_passwd_cb), |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1061 | ADD_METHOD(set_default_verify_paths), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1062 | ADD_METHOD(use_certificate_chain_file), |
| 1063 | ADD_METHOD(use_certificate_file), |
| 1064 | ADD_METHOD(use_certificate), |
Jean-Paul Calderone | d3ada85 | 2008-02-18 21:17:29 -0500 | [diff] [blame] | 1065 | ADD_METHOD(add_extra_chain_cert), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1066 | ADD_METHOD(use_privatekey_file), |
| 1067 | ADD_METHOD(use_privatekey), |
| 1068 | ADD_METHOD(check_privatekey), |
| 1069 | ADD_METHOD(load_client_ca), |
| 1070 | ADD_METHOD(set_session_id), |
| 1071 | ADD_METHOD(set_verify), |
| 1072 | ADD_METHOD(set_verify_depth), |
| 1073 | ADD_METHOD(get_verify_mode), |
| 1074 | ADD_METHOD(get_verify_depth), |
| 1075 | ADD_METHOD(load_tmp_dh), |
| 1076 | ADD_METHOD(set_cipher_list), |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 1077 | ADD_METHOD(set_client_ca_list), |
| 1078 | ADD_METHOD(add_client_ca), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1079 | ADD_METHOD(set_timeout), |
| 1080 | ADD_METHOD(get_timeout), |
| 1081 | ADD_METHOD(set_info_callback), |
| 1082 | ADD_METHOD(get_app_data), |
| 1083 | ADD_METHOD(set_app_data), |
| 1084 | ADD_METHOD(get_cert_store), |
| 1085 | ADD_METHOD(set_options), |
| 1086 | { NULL, NULL } |
| 1087 | }; |
| 1088 | #undef ADD_METHOD |
| 1089 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1090 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1091 | * Despite the name which might suggest otherwise, this is not the tp_init for |
| 1092 | * the Context type. It's just the common initialization code shared by the |
| 1093 | * two _{Nn}ew functions below. |
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 | static ssl_ContextObj* |
| 1096 | ssl_Context_init(ssl_ContextObj *self, int i_method) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1097 | SSL_METHOD *method; |
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 | switch (i_method) { |
| 1100 | case ssl_SSLv2_METHOD: |
| 1101 | method = SSLv2_method(); |
| 1102 | break; |
| 1103 | case ssl_SSLv23_METHOD: |
| 1104 | method = SSLv23_method(); |
| 1105 | break; |
| 1106 | case ssl_SSLv3_METHOD: |
| 1107 | method = SSLv3_method(); |
| 1108 | break; |
| 1109 | case ssl_TLSv1_METHOD: |
| 1110 | method = TLSv1_method(); |
| 1111 | break; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1112 | default: |
| 1113 | PyErr_SetString(PyExc_ValueError, "No such protocol"); |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1117 | self->ctx = SSL_CTX_new(method); |
| 1118 | Py_INCREF(Py_None); |
| 1119 | self->passphrase_callback = Py_None; |
| 1120 | Py_INCREF(Py_None); |
| 1121 | self->verify_callback = Py_None; |
| 1122 | Py_INCREF(Py_None); |
| 1123 | self->info_callback = Py_None; |
| 1124 | |
| 1125 | Py_INCREF(Py_None); |
| 1126 | self->passphrase_userdata = Py_None; |
| 1127 | |
| 1128 | Py_INCREF(Py_None); |
| 1129 | self->app_data = Py_None; |
| 1130 | |
| 1131 | /* Some initialization that's required to operate smoothly in Python */ |
| 1132 | SSL_CTX_set_app_data(self->ctx, self); |
| 1133 | SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1134 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 1135 | SSL_MODE_AUTO_RETRY); |
| 1136 | |
| 1137 | self->tstate = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1138 | |
| 1139 | return self; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1143 | * 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] | 1144 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1145 | ssl_ContextObj* |
| 1146 | ssl_Context_New(int i_method) { |
| 1147 | ssl_ContextObj *self; |
| 1148 | |
| 1149 | self = PyObject_GC_New(ssl_ContextObj, &ssl_Context_Type); |
| 1150 | if (self == NULL) { |
| 1151 | return (ssl_ContextObj *)PyErr_NoMemory(); |
| 1152 | } |
| 1153 | self = ssl_Context_init(self, i_method); |
| 1154 | PyObject_GC_Track((PyObject *)self); |
| 1155 | return self; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | /* |
| 1160 | * This one is the tp_new of the Context type. It's great. |
| 1161 | */ |
| 1162 | static PyObject* |
| 1163 | ssl_Context_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) { |
| 1164 | int i_method; |
| 1165 | ssl_ContextObj *self; |
| 1166 | static char *kwlist[] = {"method", NULL}; |
| 1167 | |
| 1168 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:Context", kwlist, &i_method)) { |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | |
| 1172 | self = (ssl_ContextObj *)subtype->tp_alloc(subtype, 1); |
| 1173 | if (self == NULL) { |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | return (PyObject *)ssl_Context_init(self, i_method); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Call the visitproc on all contained objects. |
| 1182 | * |
| 1183 | * Arguments: self - The Context object |
| 1184 | * visit - Function to call |
| 1185 | * arg - Extra argument to visit |
| 1186 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 1187 | * call that gave non-zero result. |
| 1188 | */ |
| 1189 | static int |
| 1190 | ssl_Context_traverse(ssl_ContextObj *self, visitproc visit, void *arg) |
| 1191 | { |
| 1192 | int ret = 0; |
| 1193 | |
| 1194 | if (ret == 0 && self->passphrase_callback != NULL) |
| 1195 | ret = visit((PyObject *)self->passphrase_callback, arg); |
| 1196 | if (ret == 0 && self->passphrase_userdata != NULL) |
| 1197 | ret = visit((PyObject *)self->passphrase_userdata, arg); |
| 1198 | if (ret == 0 && self->verify_callback != NULL) |
| 1199 | ret = visit((PyObject *)self->verify_callback, arg); |
| 1200 | if (ret == 0 && self->info_callback != NULL) |
| 1201 | ret = visit((PyObject *)self->info_callback, arg); |
| 1202 | if (ret == 0 && self->app_data != NULL) |
| 1203 | ret = visit(self->app_data, arg); |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Decref all contained objects and zero the pointers. |
| 1209 | * |
| 1210 | * Arguments: self - The Context object |
| 1211 | * Returns: Always 0. |
| 1212 | */ |
| 1213 | static int |
| 1214 | ssl_Context_clear(ssl_ContextObj *self) |
| 1215 | { |
| 1216 | Py_XDECREF(self->passphrase_callback); |
| 1217 | self->passphrase_callback = NULL; |
| 1218 | Py_XDECREF(self->passphrase_userdata); |
| 1219 | self->passphrase_userdata = NULL; |
| 1220 | Py_XDECREF(self->verify_callback); |
| 1221 | self->verify_callback = NULL; |
| 1222 | Py_XDECREF(self->info_callback); |
| 1223 | self->info_callback = NULL; |
| 1224 | Py_XDECREF(self->app_data); |
| 1225 | self->app_data = NULL; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * Deallocate the memory used by the Context object |
| 1231 | * |
| 1232 | * Arguments: self - The Context object |
| 1233 | * Returns: None |
| 1234 | */ |
| 1235 | static void |
| 1236 | ssl_Context_dealloc(ssl_ContextObj *self) |
| 1237 | { |
| 1238 | PyObject_GC_UnTrack((PyObject *)self); |
| 1239 | SSL_CTX_free(self->ctx); |
| 1240 | ssl_Context_clear(self); |
| 1241 | PyObject_GC_Del(self); |
| 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | PyTypeObject ssl_Context_Type = { |
| 1246 | PyObject_HEAD_INIT(NULL) |
| 1247 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1248 | "OpenSSL.SSL.Context", |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1249 | sizeof(ssl_ContextObj), |
| 1250 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1251 | (destructor)ssl_Context_dealloc, /* tp_dealloc */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1252 | NULL, /* print */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1253 | NULL, /* tp_getattr */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1254 | NULL, /* setattr */ |
| 1255 | NULL, /* compare */ |
| 1256 | NULL, /* repr */ |
| 1257 | NULL, /* as_number */ |
| 1258 | NULL, /* as_sequence */ |
| 1259 | NULL, /* as_mapping */ |
| 1260 | NULL, /* hash */ |
| 1261 | NULL, /* call */ |
| 1262 | NULL, /* str */ |
| 1263 | NULL, /* getattro */ |
| 1264 | NULL, /* setattro */ |
| 1265 | NULL, /* as_buffer */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1266 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1267 | ssl_Context_doc, /* tp_doc */ |
| 1268 | (traverseproc)ssl_Context_traverse, /* tp_traverse */ |
| 1269 | (inquiry)ssl_Context_clear, /* tp_clear */ |
| 1270 | NULL, /* tp_richcompare */ |
| 1271 | 0, /* tp_weaklistoffset */ |
| 1272 | NULL, /* tp_iter */ |
| 1273 | NULL, /* tp_iternext */ |
| 1274 | ssl_Context_methods, /* tp_methods */ |
| 1275 | NULL, /* tp_members */ |
| 1276 | NULL, /* tp_getset */ |
| 1277 | NULL, /* tp_base */ |
| 1278 | NULL, /* tp_dict */ |
| 1279 | NULL, /* tp_descr_get */ |
| 1280 | NULL, /* tp_descr_set */ |
| 1281 | 0, /* tp_dictoffset */ |
| 1282 | NULL, /* tp_init */ |
| 1283 | NULL, /* tp_alloc */ |
| 1284 | ssl_Context_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1285 | }; |
| 1286 | |
| 1287 | |
| 1288 | /* |
| 1289 | * Initialize the Context part of the SSL sub module |
| 1290 | * |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1291 | * Arguments: dict - The OpenSSL.SSL module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1292 | * Returns: 1 for success, 0 otherwise |
| 1293 | */ |
| 1294 | int |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1295 | init_ssl_context(PyObject *module) { |
| 1296 | |
| 1297 | if (PyType_Ready(&ssl_Context_Type) < 0) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1298 | return 0; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) { |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | if (PyModule_AddObject(module, "ContextType", (PyObject *)&ssl_Context_Type) < 0) { |
| 1306 | return 0; |
| 1307 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1308 | |
| 1309 | return 1; |
| 1310 | } |
| 1311 | |