1515#include "lxml.h"
1616
1717#include <xmlsec/xmlenc.h>
18+ #include <xmlsec/xmltree.h>
1819
1920typedef struct {
2021 PyObject_HEAD
@@ -105,6 +106,19 @@ static int PyXmlSec_EncryptionContextKeySet(PyObject* self, PyObject* value, voi
105106 return 0 ;
106107}
107108
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+ PYXMLSEC_DUMP (xmlSecEncCtxDebugDump , ctx );
117+ Py_END_ALLOW_THREADS ;
118+ PYXMLSEC_DEBUGF ("%p: reset context - ok" , self );
119+ Py_RETURN_NONE ;
120+ }
121+
108122static const char PyXmlSec_EncryptionContextEncryptBinary__doc__ [] = \
109123 "Encrypts binary *data* according to `EncryptedData` template *template*\n" \
110124 "Note: *template* is modified in place.\n\n"
@@ -128,6 +142,7 @@ static PyObject* PyXmlSec_EncryptionContextEncryptBinary(PyObject* self, PyObjec
128142 int rv ;
129143 Py_BEGIN_ALLOW_THREADS ;
130144 rv = xmlSecEncCtxBinaryEncrypt (ctx , template -> _c_node , (const xmlSecByte * )data , (xmlSecSize )data_size );
145+ PYXMLSEC_DUMP (xmlSecEncCtxDebugDump , ctx );
131146 Py_END_ALLOW_THREADS ;
132147
133148 if (rv < 0 ) {
@@ -163,12 +178,9 @@ static const char PyXmlSec_EncryptionContextEncryptXml__doc__[] = \
163178 "Note: The `Type` attribute of *template* decides whether *node* itself is encrypted\n" \
164179 "(`http://www.w3.org/2001/04/xmlenc#Element`) or its content (`http://www.w3.org/2001/04/xmlenc#Content`).\n" \
165180 "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"
181+ "The operation modifies the tree and removes replaced nodes.\n" \
182+ ":param template: the pointer to <enc:EncryptedData/> template node\n" \
183+ ":param node: the pointer to node for encryption\n" \
172184 ":return: the pointer to newly created <enc:EncryptedData/> node\n" ;
173185static PyObject * PyXmlSec_EncryptionContextEncryptXml (PyObject * self , PyObject * args , PyObject * kwargs ) {
174186 static char * kwlist [] = { "template" , "node" , NULL };
@@ -216,6 +228,7 @@ static PyObject* PyXmlSec_EncryptionContextEncryptXml(PyObject* self, PyObject*
216228 xnew_node = NULL ;
217229 }
218230 }
231+ PYXMLSEC_DUMP (xmlSecEncCtxDebugDump , ctx );
219232 Py_END_ALLOW_THREADS ;
220233
221234 PyXmlSec_ClearReplacedNodes (ctx , node -> _doc );
@@ -258,6 +271,7 @@ static PyObject* PyXmlSec_EncryptionContextEncryptUri(PyObject* self, PyObject*
258271 int rv ;
259272 Py_BEGIN_ALLOW_THREADS ;
260273 rv = xmlSecEncCtxUriEncrypt (ctx , template -> _c_node , (const xmlSecByte * )uri );
274+ PYXMLSEC_DUMP (xmlSecEncCtxDebugDump , ctx );
261275 Py_END_ALLOW_THREADS ;
262276
263277 if (rv < 0 ) {
@@ -273,14 +287,12 @@ static PyObject* PyXmlSec_EncryptionContextEncryptUri(PyObject* self, PyObject*
273287}
274288
275289static const char PyXmlSec_EncryptionContextDecrypt__doc__ [] = \
276- "Decrypts *node* (an `EncryptedData` element) and return the result.\n" \
290+ "Decrypts *node* (an `EncryptedData` or `EncryptedKey` element) and return the result.\n" \
277291 "The decryption may result in binary data or an XML subtree.\n" \
278292 "In the former case, the binary data is returned. In the latter case,\n" \
279293 "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"
294+ "If the operation modifies the tree, it removes replaced nodes.\n" \
295+ ":param node: the pointer to <enc:EncryptedData/> or <enc:EncryptedKey/> node\n"
284296 ":return: depends on input parameters\n" ;
285297
286298static PyObject * PyXmlSec_EncryptionContextDecrypt (PyObject * self , PyObject * args , PyObject * kwargs ) {
@@ -310,15 +322,18 @@ static PyObject* PyXmlSec_EncryptionContextDecrypt(PyObject* self, PyObject* arg
310322 }
311323 // get index of node
312324 node_num = PyObject_CallMethod (parent , "index" , "O" , node );
313- PYXMLSEC_DEBUGF ("%p, %p" , parent , node_num );
325+ PYXMLSEC_DEBUGF ("parent: %p, %p" , parent , node_num );
314326 }
315327
316328 xmlSecEncCtxPtr ctx = ((PyXmlSec_EncryptionContext * )self )-> handle ;
317- ctx -> flags = XMLSEC_ENC_RETURN_REPLACED_NODE ;
318329 int rv ;
319330
320331 Py_BEGIN_ALLOW_THREADS ;
332+ ctx -> flags = XMLSEC_ENC_RETURN_REPLACED_NODE ;
333+ ctx -> mode = xmlSecCheckNodeName (node -> _c_node , xmlSecNodeEncryptedKey , xmlSecEncNs ) ? xmlEncCtxModeEncryptedKey : xmlEncCtxModeEncryptedData ;
334+ PYXMLSEC_DEBUGF ("mode: %d" , ctx -> mode );
321335 rv = xmlSecEncCtxDecrypt (ctx , node -> _c_node );
336+ PYXMLSEC_DUMP (xmlSecEncCtxDebugDump , ctx );
322337 Py_END_ALLOW_THREADS ;
323338
324339 PyXmlSec_ClearReplacedNodes (ctx , node -> _doc );
@@ -385,6 +400,12 @@ static PyGetSetDef PyXmlSec_EncryptionContextGetSet[] = {
385400};
386401
387402static PyMethodDef PyXmlSec_EncryptionContextMethods [] = {
403+ {
404+ "reset" ,
405+ (PyCFunction )PyXmlSec_EncryptionContextReset ,
406+ METH_NOARGS ,
407+ PyXmlSec_EncryptionContextReset__doc__ ,
408+ },
388409 {
389410 "encrypt_binary" ,
390411 (PyCFunction )PyXmlSec_EncryptionContextEncryptBinary ,
0 commit comments