|
15 | 15 | #include "lxml.h" |
16 | 16 |
|
17 | 17 | #include <xmlsec/xmlenc.h> |
| 18 | +#include <xmlsec/xmltree.h> |
18 | 19 |
|
19 | 20 | typedef struct { |
20 | 21 | PyObject_HEAD |
@@ -105,6 +106,18 @@ static int PyXmlSec_EncryptionContextKeySet(PyObject* self, PyObject* value, voi |
105 | 106 | return 0; |
106 | 107 | } |
107 | 108 |
|
| 109 | +static const char PyXmlSec_EncryptionContextReset__doc__[] = \ |
| 110 | + "Resets *context*, user settings are not touched.\n"; |
| 111 | +static PyObject* PyXmlSec_EncryptionContextReset(PyObject* self, PyObject* args, PyObject* kwargs) { |
| 112 | + PYXMLSEC_DEBUGF("%p: reset context - start", self); |
| 113 | + xmlSecEncCtxPtr ctx = ((PyXmlSec_EncryptionContext*)self)->handle; |
| 114 | + Py_BEGIN_ALLOW_THREADS; |
| 115 | + xmlSecEncCtxReset(ctx); |
| 116 | + Py_END_ALLOW_THREADS; |
| 117 | + PYXMLSEC_DEBUGF("%p: reset context - ok", self); |
| 118 | + Py_RETURN_NONE; |
| 119 | +} |
| 120 | + |
108 | 121 | static const char PyXmlSec_EncryptionContextEncryptBinary__doc__[] = \ |
109 | 122 | "Encrypts binary *data* according to `EncryptedData` template *template*\n"\ |
110 | 123 | "Note: *template* is modified in place.\n\n" |
@@ -163,12 +176,9 @@ static const char PyXmlSec_EncryptionContextEncryptXml__doc__[] = \ |
163 | 176 | "Note: The `Type` attribute of *template* decides whether *node* itself is encrypted\n"\ |
164 | 177 | "(`http://www.w3.org/2001/04/xmlenc#Element`) or its content (`http://www.w3.org/2001/04/xmlenc#Content`).\n"\ |
165 | 178 | "It must have one of these two values (or an exception is raised).\n"\ |
166 | | - "The operation modifies the tree containing *node* in a way that\n"\ |
167 | | - "`lxml` references to or into this tree may see a surprising state.\n"\ |
168 | | - "You should no longer rely on them. Especially, you should use\n"\ |
169 | | - "`getroottree()` on the result to obtain the encrypted result tree.\n\n" |
170 | | - ":param template: the pointer to <enc:EncryptedData/> template node\n" |
171 | | - ":param node: the pointer to node for encryption\n" |
| 179 | + "The operation modifies the tree and removes replaced nodes.\n"\ |
| 180 | + ":param template: the pointer to <enc:EncryptedData/> template node\n"\ |
| 181 | + ":param node: the pointer to node for encryption\n"\ |
172 | 182 | ":return: the pointer to newly created <enc:EncryptedData/> node\n"; |
173 | 183 | static PyObject* PyXmlSec_EncryptionContextEncryptXml(PyObject* self, PyObject* args, PyObject* kwargs) { |
174 | 184 | static char *kwlist[] = { "template", "node", NULL}; |
@@ -273,14 +283,12 @@ static PyObject* PyXmlSec_EncryptionContextEncryptUri(PyObject* self, PyObject* |
273 | 283 | } |
274 | 284 |
|
275 | 285 | static const char PyXmlSec_EncryptionContextDecrypt__doc__[] = \ |
276 | | - "Decrypts *node* (an `EncryptedData` element) and return the result.\n"\ |
| 286 | + "Decrypts *node* (an `EncryptedData` or `EncryptedKey` element) and return the result.\n"\ |
277 | 287 | "The decryption may result in binary data or an XML subtree.\n"\ |
278 | 288 | "In the former case, the binary data is returned. In the latter case,\n"\ |
279 | 289 | "the input tree is modified and a reference to the decrypted XML subtree is returned.\n"\ |
280 | | - "If the operation modifies the tree, `lxml` references to or into this tree may see a surprising state.\n"\ |
281 | | - "You should no longer rely on them. Especially, you should use `getroottree()` on the result\n"\ |
282 | | - "to obtain the decrypted result tree.\n\n" |
283 | | - ":param node: the pointer to <enc:EncryptedData/> node\n" |
| 290 | + "If the operation modifies the tree, it removes replaced nodes.\n"\ |
| 291 | + ":param node: the pointer to <enc:EncryptedData/> or <enc:EncryptedKey/> node\n" |
284 | 292 | ":return: depends on input parameters\n"; |
285 | 293 |
|
286 | 294 | static PyObject* PyXmlSec_EncryptionContextDecrypt(PyObject* self, PyObject* args, PyObject* kwargs) { |
@@ -310,14 +318,16 @@ static PyObject* PyXmlSec_EncryptionContextDecrypt(PyObject* self, PyObject* arg |
310 | 318 | } |
311 | 319 | // get index of node |
312 | 320 | node_num = PyObject_CallMethod(parent, "index", "O", node); |
313 | | - PYXMLSEC_DEBUGF("%p, %p", parent, node_num); |
| 321 | + PYXMLSEC_DEBUGF("parent: %p, %p", parent, node_num); |
314 | 322 | } |
315 | 323 |
|
316 | 324 | xmlSecEncCtxPtr ctx = ((PyXmlSec_EncryptionContext*)self)->handle; |
317 | | - ctx->flags = XMLSEC_ENC_RETURN_REPLACED_NODE; |
318 | 325 | int rv; |
319 | 326 |
|
320 | 327 | Py_BEGIN_ALLOW_THREADS; |
| 328 | + ctx->flags = XMLSEC_ENC_RETURN_REPLACED_NODE; |
| 329 | + ctx->mode = xmlSecCheckNodeName(node->_c_node, xmlSecNodeEncryptedKey, xmlSecEncNs) ? xmlEncCtxModeEncryptedKey : xmlEncCtxModeEncryptedData; |
| 330 | + PYXMLSEC_DEBUGF("mode: %d", ctx->mode); |
321 | 331 | rv = xmlSecEncCtxDecrypt(ctx, node->_c_node); |
322 | 332 | Py_END_ALLOW_THREADS; |
323 | 333 |
|
@@ -385,6 +395,12 @@ static PyGetSetDef PyXmlSec_EncryptionContextGetSet[] = { |
385 | 395 | }; |
386 | 396 |
|
387 | 397 | static PyMethodDef PyXmlSec_EncryptionContextMethods[] = { |
| 398 | + { |
| 399 | + "reset", |
| 400 | + (PyCFunction)PyXmlSec_EncryptionContextReset, |
| 401 | + METH_NOARGS, |
| 402 | + PyXmlSec_EncryptionContextReset__doc__, |
| 403 | + }, |
388 | 404 | { |
389 | 405 | "encrypt_binary", |
390 | 406 | (PyCFunction)PyXmlSec_EncryptionContextEncryptBinary, |
|
0 commit comments