使用xmldsig语法签名的 XML 有 3 个重要部分:
Signature -> KeyInfo
包含有关从用于签署数据的私钥派生的公钥的信息
Signature -> SignedInfo
包含将使用上述私钥签名的数据;数据包含有关应如何计算验证的信息,例如:CanonicalizationMethod
、、、SignatureMethod
Reference
Signature -> SignatureValue
Signature -> SignedInfo
包含使用私钥签名生成的签名值
从理论上讲,这就是代码应该如何查找 rsa-sha1 算法(由 指定Signature -> SignedInfo -> SignatureMethod
),具有以下规范化方法:Exclusive XML Canonicalization 1.0(省略注释),并提供 x509 证书:
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xmlString);
$xpath = new DOMXPath($xmlDoc);
$xpath->registerNamespace('secdsig', 'http://www.w3.org/2000/09/xmldsig#');
// fetch Signature node from XML
$query = ".//secdsig:Signature";
$nodeset = $xpath->query($query, $xmlDoc);
$signatureNode = $nodeset->item(0);
// fetch SignedInfo node from XML
$query = "./secdsig:SignedInfo";
$nodeset = $xpath->query($query, $signatureNode);
$signedInfoNode = $nodeset->item(0);
// canonicalize SignedInfo using the method descried in
// ./secdsig:SignedInfo/secdsig:CanonicalizationMethod/@Algorithm
$signedInfoNodeCanonicalized = $signedInfoNode->C14N(true, false);
// fetch the x509 certificate from XML
$query = 'string(./secdsig:KeyInfo/secdsig:X509Data/secdsig:X509Certificate)';
$x509cert = $xpath->evaluate($query, $signatureNode);
// we have to re-wrap the certificate from XML to respect the PEM standard
$x509cert = "-----BEGIN CERTIFICATE-----\n"
. $x509cert . "\n"
. "-----END CERTIFICATE-----";
// fetch public key from x509 certificate
$publicKey = openssl_get_publickey($x509cert);
// fetch the signature from XML
$query = 'string(./secdsig:SignatureValue)';
$signature = base64_decode($xpath->evaluate($query, $signatureNode));
// verify the signature
$ok = openssl_verify($signedInfoNodeCanonicalized, $signature, $publicKey);
这个库在 php 中实现 xmldsig 方面做得很好:xmlseclibs;可以在此处找到如何验证 xmldsig 的示例:https ://github.com/robrichards/xmlseclibs/blob/master/tests/xmlsec-verify.phpt 。该库还验证来自 的摘要值Signature -> SignedInfo -> Reference
,我在上面省略了一个步骤。