Warix3
Warix3

Reputation: 101

.NET SignedXml giving wrong digest value

I'm trying to make a signed XML document and the web service I'm sending it to is refusing it and stating that the signature is invalid. I have some other code in php and it works, the web service accepts the php generated and signed xml but not the vb.net one. I've made the php and the vb.net input xml to be exactly equal and still they generate different digest values. I also tried with an online tool to verify xml signatures and the php one is correct there and for the vb.net it says the digest value is wrong. This is my function in vb for calculating the signature:

Private Function PotpisiXml(cert As X509Certificate2, ByVal rsa As RSACryptoServiceProvider, ByVal xmlStream As MemoryStream) As XmlDocument
    xmlStream.Position = 0
    Dim xmlDoc As New XmlDocument()
    xmlDoc.Load(xmlStream)
    Dim signedXml As New SignedXml(xmlDoc)
    signedXml.SigningKey = rsa

    Dim reference As New Reference()
    reference.Uri = "#racunId"
    Dim env As New XmlDsigEnvelopedSignatureTransform()
    Dim ec14n As New XmlDsigExcC14NTransform()
    reference.AddTransform(env)
    reference.AddTransform(ec14n)
    signedXml.AddReference(reference)

    Dim keyInfo As New KeyInfo()
    Dim kdata As New KeyInfoX509Data(cert)
    Dim xserial As X509IssuerSerial
    xserial.IssuerName = cert.IssuerName.Name
    xserial.SerialNumber = cert.SerialNumber
    kdata.AddIssuerSerial(xserial.IssuerName, xserial.SerialNumber)
    keyInfo.AddClause(kdata)

    signedXml.ComputeSignature()
    signedXml.KeyInfo = keyInfo
    Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()

    xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
    Return xmlDoc
End Function

The vb.net generated xml: https://pastebin.com/jrzLsv4k

The php generated xml: https://pastebin.com/kbsE2PH7

The xmlDoc that goes into signing is exactly the same for both codes. Encoding is UTF-8 for both sides.

Upvotes: 0

Views: 865

Answers (1)

Warix3
Warix3

Reputation: 101

I've fixed this by removing all the whitespace and newlines everywhere in the process.

Upvotes: 1

Related Questions