[Pyxmlsec-devel] xmlsec in python

Dolf Andringa dolfandringa at gmail.com
Tue Feb 21 14:14:44 CET 2012


Hi Everyone.

I am trying to decrypt an xml message in python using PyXMLSec.
I have found the examples on
http://pyxmlsec.labs.libre-entreprise.org/index.php?section=examples&id=11
and accordingly did the following, but am receiving errors from the c
library, which I really don't understand.
The xml seems to be fine, since I can read the xml file and find the
EncryptedData node.
The private key file is an RSA private key, which is valid and I can
successfully use it in the M2Crypto module in python.
I hope anyone can help. Thanks in advance for the effort.

Cheers,

Dolf.

The python code:

private_key_file='my.private.key'
xmlstring=open('temp.xml','rb').read()

import libxml2
import xmlsec

libxml2.initParser()
libxml2.substituteEntitiesDefault(1)
xmlsec.init()
xmlsec.cryptoAppInit(None)
xmlsec.cryptoInit()

doc=libxml2.parseMemory(xmlstring,len(xmlstring))
node=xmlsec.findNode(doc.getRootElement(),xmlsec.NodeEncryptedData,xmlsec.EncNs)
node.get_name()
'''EncryptedData'''
print(node.children)
'''<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc
"/>'''
key=xmlsec.keyReadBinaryFile(xmlsec.keyDataRsaId(),private_key_file)
'''
func=xmlSecKeyDataBinRead:file=keysdata.c:line=349:obj=unknown:subj=id->binRead
!= NULL:error=100:assertion:
func=xmlSecKeyReadBuffer:file=keys.c:line=1190:obj=rsa:subj=xmlSecKeyDataBinRead:error=1:xmlsec
library function failed:
func=xmlSecKeyReadBinaryFile:file=keys.c:line=1247:obj=rsa:subj=xmlSecKeyReadBuffer:error=1:xmlsec
library function failed:filename=my.private.key
'''
key.setName(private_key_file)
enc_ctx = xmlsec.EncCtx(None)
enc_ctx.encKey=key

enc_ctx.decrypt(node)
'''func=xmlSecEncCtxEncDataNodeRead:file=xmlenc.c:line=809:obj=unknown:subj=encCtx->mimeType
== NULL:error=100:assertion:
func=xmlSecEncCtxDecryptToBuffer:file=xmlenc.c:line=715:obj=unknown:subj=xmlSecEncCtxEncDataNodeRead:error=1:xmlsec
library function failed:
func=xmlSecEncCtxDecrypt:file=xmlenc.c:line=623:obj=unknown:subj=xmlSecEncCtxDecryptToBuffer:error=1:xmlsec
library function failed:
-1
'''
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.labs.libre-entreprise.org/pipermail/pyxmlsec-devel/attachments/20120221/37c2a668/attachment.html>


More information about the Pyxmlsec-devel mailing list