romeroqj
romeroqj

Reputation: 839

AssertionError: Unknown node type: 3 when encrypting SOAP body content with dm.xmlsec.binding

I'm getting an error when I try to encrypt the body of a SOAP XML message using https://pypi.org/project/dm.xmlsec.binding/.

Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/romeroqj/Desktop/management/commands/encrypt.py", line 63, in handle
    encrypted_xml = enc_ctx.encryptXml(enc_data, doc.getroot())
  File "_xmlsec.pyx", line 555, in dm.xmlsec.binding._xmlsec.EncCtx.encryptXml (src/_xmlsec.c:7336)
  File "_xmlsec.pyx", line 637, in dm.xmlsec.binding._xmlsec.lxml_safe_dealloc (src/_xmlsec.c:8132)
  File "src/lxml/public-api.pxi", line 29, in lxml.etree.elementFactory
  File "src/lxml/etree.pyx", line 1607, in lxml.etree._elementFactory
  File "src/lxml/classlookup.pxi", line 403, in lxml.etree._parser_class_lookup
  File "src/lxml/nsclasses.pxi", line 174, in lxml.etree._find_nselement_class
  File "src/lxml/classlookup.pxi", line 257, in lxml.etree._callLookupFallback
  File "src/lxml/classlookup.pxi", line 336, in lxml.etree._lookupDefaultElementClass
AssertionError: Unknown node type: 3

The error occurs when I try to encrypt the content of the body (xmlsec.TypeEncContent), but it works when I try to encrypt the body element (xmlsec.TypeEncElement). Here's the code:

import dm.xmlsec.binding as xmlsec
from dm.xmlsec.binding.tmpl import EncData, fromstring

key_mngr = xmlsec.KeysMngr()
key = xmlsec.Key.load('cert.pem', xmlsec.KeyDataFormatCertPem)
key.name = 'cert.pem'
key_mngr.addKey(key)
enc_data = EncData(
    xmlsec.TransformAes128Cbc, type=xmlsec.TypeEncContent
)
enc_data.ensureCipherValue()  # target for encryption result
enc_ctx = xmlsec.EncCtx(key_mngr)
enc_ctx.encKey = xmlsec.Key.generate(
    xmlsec.KeyDataAes, 192, xmlsec.KeyDataTypeSession
)
doc = fromstring(xml_data)
body = doc.find('soap:Body', namespaces=NAMESPACES)  # NAMESPACES is redacted for brevity
encrypted_xml = enc_ctx.encryptXml(enc_data, doc.getroot())

The XML data looks something like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
    ...
    </soap:Header>
    <soap:Body Id="Body">
    ...
    </soap:Body>
</soap:Envelope>

What could be wrong here?

I'm not sure if this is relevant or related, but I tried to do the same using a different library https://github.com/mehcode/python-xmlsec/issues/83 and I got a Segmentation fault error, however, it worked when I tried to encrypt the element. This pattern makes me believe there's something wrong in this approach.

Any hints are appreciated. Thanks in advance!

Upvotes: 1

Views: 195

Answers (0)

Related Questions