golang/goxmldig - 签名验证失败

golang/goxmldig - Signature Verification Failed

我试图签署一个实体描述符文件,但签名总是不正确。 xmlsectool 指出签名是预期摘要与实际摘要不同。

xmlsectool-2.0.0/xmlsectool.sh --verifySignature --certificate saml.crt --inFile example.xml
INFO  XMLSecTool - Reading XML document from file 'example.xml'
INFO  XMLSecTool - XML document parsed and is well-formed.
WARN  Reference - Verification failed for URI "#id1234"
WARN  Reference - Expected Digest: D+SEh34cA7/atdQ8ojV9rzUcJcJSAslFZ0aOIwplGfI=
WARN  Reference - Actual Digest: EYun0wngsN35ci20wRziCXs0Io7J4bZN+NYRnnTR5QM=
ERROR XMLSecTool - XML document signature verification failed

我遵循了 goxmldsig to create the following code. The full example is on pastebin 上的 README 示例(Whosebug 不允许我 post 它在这里)。

xmlBytes := []byte(`<></>`)
keyPair, err := tls.X509KeyPair(certBytes, keyBytes)
failOnError(err, "invalided to load keypair")

keyStore := dsig.TLSCertKeyStore(keyPair)

signingContext := dsig.NewDefaultSigningContext(keyStore)
signingContext.Canonicalizer = dsig.MakeC14N10ExclusiveCanonicalizerWithPrefixList("")
err = signingContext.SetSignatureMethod(dsig.RSASHA256SignatureMethod)
failOnError(err, "failed to set signature method")

readXMLDoc := etree.NewDocument()
err = readXMLDoc.ReadFromBytes(xmlBytes)
failOnError(err, "cannot parse xml")

elementToSign := readXMLDoc.Root()
elementToSign.CreateAttr("ID", "id1234")

signedElement, err := signingContext.SignEnveloped(elementToSign)
failOnError(err, "failed to sign envelop")

var signedAssertionBuf []byte
{
    readXMLDoc.SetRoot(signedElement)
    signedAssertionBuf, err = readXMLDoc.WriteToBytes()
    failOnError(err, "failed to convert doc to bytes")
}

ioutil.WriteFile("/tmp/test/example.xml", signedAssertionBuf, 0775)

问题似乎与在您的某些元素中包含此属性有关:

xml:lang="en"

例如:

<OrganizationName xml:lang="en">Your Identities</OrganizationName>

如果您删除所有元素的 xml:lang="en",生成的签名将变为有效并正确验证。

据我所知,当你包含那个属性时,写在实际 XML 文档上的元素似乎变成了这个:

<OrganizationName xmlns:xml="http://www.w3.org/XML/1998/namespace" xml:lang="en">Your Identities</OrganizationName>

这会使签名无效。