Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traceback and "silent" crash when running example with TypeEncContent #265

Open
radovanhorvat opened this issue Sep 20, 2023 · 0 comments
Open

Comments

@radovanhorvat
Copy link

Hello,

When trying out the "Encrypt" example found in:

https://xmlsec.readthedocs.io/en/stable/examples.html,

I get two errors, both using xmlsec.constants.TypeEncContent, eg with:

from lxml import etree

import xmlsec

manager = xmlsec.KeysManager()
key = xmlsec.Key.from_file('rsacert.pem', xmlsec.constants.KeyDataFormatCertPem, None)
manager.add_key(key)
template = etree.parse('enc1-doc.xml').getroot()
enc_data = xmlsec.template.encrypted_data_create(
    template,
    xmlsec.constants.TransformAes128Cbc,
    type=xmlsec.constants.TypeEncContent,
    ns="xenc",
)

xmlsec.template.encrypted_data_ensure_cipher_value(enc_data)
key_info = xmlsec.template.encrypted_data_ensure_key_info(enc_data, ns="dsig")
enc_key = xmlsec.template.add_encrypted_key(key_info, xmlsec.constants.TransformRsaOaep)
xmlsec.template.encrypted_data_ensure_cipher_value(enc_key)
data = template.find('./Data')

# Encryption
enc_ctx = xmlsec.EncryptionContext(manager)
enc_ctx.key = xmlsec.Key.generate(
    xmlsec.constants.KeyDataAes, 128, xmlsec.constants.KeyDataTypeSession
)
enc_data = enc_ctx.encrypt_xml(enc_data, data)
enc_method = xmlsec.tree.find_child(
    enc_data, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs
)
key_info = xmlsec.tree.find_child(
    enc_data, xmlsec.constants.NodeKeyInfo, xmlsec.constants.DSigNs
)
enc_method = xmlsec.tree.find_node(
    key_info, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs
)
cipher_value = xmlsec.tree.find_node(
    key_info, xmlsec.constants.NodeCipherValue, xmlsec.constants.EncNs
)
print(etree.tostring(cipher_value))
  1. For the first case, the contents of enc1-doc.xml are as found in https://github.com/xmlsec/python-xmlsec/tree/master/doc/source/examples:
<?xml version="1.0" encoding="UTF-8"?>
<!--
XML Security Library example: Original XML doc file for enc example.
-->
<Envelope>
<Data>Hello, World!</Data>
</Envelope>

When running the snippet above, I get:

Traceback (most recent call last):
  File "encrypt.py", line 30, in <module>
    enc_data = enc_ctx.encrypt_xml(enc_data, data)
  File "src\lxml\public-api.pxi", line 29, in lxml.etree.elementFactory
  File "src\lxml\etree.pyx", line 1629, in lxml.etree._elementFactory
  File "src\lxml\classlookup.pxi", line 405, in lxml.etree._parser_class_lookup
  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
  1. For the second case, when modifying the xml content to:
<?xml version="1.0" encoding="UTF-8"?>
<!--
XML Security Library example: Original XML doc file for enc example.
-->
<Envelope>
<Data>
<Data2>Hello, World!</Data2>
</Data>
</Envelope>

I get a crash with no Traceback, eg the print statement at the end of the snippet is never even executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant