python - xmlsec1 sign 在命令行上有效,但在 Python 代码上失败

标签 python x509certificate libxml2 xmlsec

我正在尝试使用 pyxmlsec/xmlsec/libxml2 对 XML 文件进行签名,但它失败了 (sigCtx->signValueNode == NULL:error=100:assertion)。

当我从命令行运行时,它运行顺利:

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

我的 Python 代码:

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!
...

完整的错误跟踪:

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: 

有什么建议吗?

谢谢!

最佳答案

xmlsec 使用 -DXMLSEC_NO_SIZE_T 选项编译,pyxmlsec 使用 -UXMLSEC_NO_SIZE_T 编译。

编辑 setup.py 并在 t = tuple(flag[2:].split('=')) 之后添加以下行

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

打补丁后效果很好。

确保清理、重建和重新安装!

关于python - xmlsec1 sign 在命令行上有效,但在 Python 代码上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071246/

相关文章:

python - 邻接表的错误在哪里?

ssl - 如何将主题备用名称添加到 ssl 证书?

ssl - 使用服务器 key 签署客户端证书时出现无法加载证书错误

azure - 管理证书不在云中

ios - SDK ios6 libxml2 预期为 ';'

python - 如何停止在 vim 中调试 python

Python - Selenium 取消代理使用

python - 循环遍历包含内容和索引的列表

Cygwin 上 virtualenv 中的 Python 2.7 pip install lxml 失败

iphone - XPath如何在不加载整个xml的情况下选择第一个元素?