Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlsec1 sign works on command line but fails on Python code

I'm trying to sign a XML file using pyxmlsec/xmlsec/libxml2 but it fails (sigCtx->signValueNode == NULL:error=100:assertion).

When I run from command line it works smoothly:

xmlsec1 sign --output signed.xml --pkcs12 c.pfx --pwd mypwd tobesigned.xml

My Python code:

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

doc_xml = libxml2.parseMemory(xml, len(xml))
signature_node = xmlsec.findNode(
    doc_xml.getRootElement(),
    xmlsec.NodeSignature,
    xmlsec.DSigNs,
)

signer = xmlsec.DSigCtx()

key = xmlsec.cryptoAppKeyLoad(
    filename=str(self.issuer.certificate.path),
    format=xmlsec.KeyDataFormatPkcs12,
    pwd=str(self.issuer.certificate_password),
    pwdCallback=None,
    pwdCallbackCtx=None,
)

signer.signKey = key
signer.sign(signature_node) # error!
...

Full error trace:

func=xmlSecDSigCtxProcessSignatureNode:file=xmldsig.c:line=463:obj=unknown:subj=dsigCtx->signValueNode == NULL:error=100:assertion:
func=xmlSecDSigCtxSign:file=xmldsig.c:line=303:obj=unknown:subj=xmlSecDSigCtxSigantureProcessNode:error=1:xmlsec library function failed: 

Any tips?

Thanks!

like image 974
rpedigoni Avatar asked Oct 25 '12 14:10

rpedigoni


1 Answers

xmlsec was compiled with -DXMLSEC_NO_SIZE_T option and pyxmlsec with -UXMLSEC_NO_SIZE_T.

Edit setup.py and add the following line after t = tuple(flag[2:].split('='))

   if len(t) == 1: t = tuple([t[0],""])

Works well after patching.

Make sure to clean, rebuild and reinstall!

like image 168
user1815339 Avatar answered Oct 03 '22 06:10

user1815339