AssertionError:未知节点类型:3使用dm.xmlsec.binding加密SOAP正文内容时

时间:2018-07-24 16:46:06

标签: python encryption soap encryption-asymmetric xmlsec

当我尝试使用https://pypi.org/project/dm.xmlsec.binding/对SOAP XML消息的主体进行加密时,出现错误。

Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/romeroqj/.virtualenv/unhaggle/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/romeroqj/.virtualenv/unhaggle/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/unhaggle/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/unhaggle/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/romeroqj/Desktop/unhaggle/unhaggle/audi/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

当我尝试加密正文(xmlsec.TypeEncContent)的内容时发生错误,但是当我尝试加密正文元素(xmlsec.TypeEncElement)时,该错误起作用。这是代码:

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())

XML数据如下所示:

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

这有什么问题吗?

我不确定这是否相关,但是我尝试使用不同的库https://github.com/mehcode/python-xmlsec/issues/83进行同样的操作,但遇到了Segmentation Fault错误,但是当我尝试加密元素时它起作用了。这种模式使我相信这种方法存在问题。

任何提示都值得赞赏。预先感谢!

0 个答案:

没有答案