3

I have attempted to sign a entities descriptor file, but the signature is always incorrect. xmlsectool states that the signature is expected digest is not the same as the actual digest.

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

I followed the README example on goxmldsig to create the following code. The full example is on pastebin(stackoverflow wouldn't let me post it here).

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)
fuglede
  • 17,388
  • 2
  • 54
  • 99
smokedice
  • 900
  • 2
  • 10
  • 25
  • Can you confirm that the contents of `saml.crt` are identical to those of the PEM block you've put on pastebin? If I validate using `goxmldsig`, it goes through as expected. – fuglede Aug 16 '17 at 19:44
  • @fuglede yes they are the same – smokedice Aug 17 '17 at 18:43

1 Answers1

2

It seems the problem is related to including this attribute in some of your elements:

xml:lang="en"

For example in:

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

If you remove the xml:lang="en" for all elements, the generated signature turns to be valid and correctly verified.

As far as I can see, when you include that attribute, the elements written on the actual XML document seem to turn into this:

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

And that makes the signature invalid.

eugenioy
  • 11,825
  • 28
  • 35
  • very well spotted! Would you know why this happens? The example came from the [shibboleth](https://wiki.shibboleth.net/confluence/display/SHIB2/MetadataExample) website & the xml:lang attributes are also described in the offical XSD. – smokedice Aug 17 '17 at 21:56
  • @smokedice: I am not sure why this happens. It looks like a problem in github.com/russellhaering/goxmldsig. Might be worth reporting an issue there for them to investigate. – eugenioy Aug 18 '17 at 01:24
  • 1
    for the reader: [github issue](https://github.com/russellhaering/goxmldsig/issues/28) – smokedice Aug 18 '17 at 08:56