Search code examples
saml-2.0adfsx509certificate2signedxml

SignedXml.CheckSignature() is true, but SignedXml.CheckSignature(certificate) is false


I'm generating a SAML2 token from ADFS, signed by certificate. Now I'm trying to verify that signature, using the same certificate.

X509Certificate2 cert = LoadCert();
XmlDocument token = LoadXmlToken(); //SAML2 token
XmlElement signature = GetSignatureElement(token);

SignedXml signedXml = new SignedXml(token);
signedXml.LoadXml(signature);
bool result1 = signedXml.CheckSignature();            //true
bool result2 = signedXml.CheckSignature(cert, false); //false

CheckSignature() verifies signature against the public key contained in the token. CheckSignature(cert, [true/false]) verifies signature against the private key from the certificate.

How can it be that one works and the other doesn't?


Solution

  • The method signedXml.CheckSignature() evaluates the xml signature integrity against the certificate contained inside the own signature.

    The method SignedXml.CheckSignature(X509Certificate2, Boolean) evaluates the xml signature integrity against the certificate passed as first parameter, and optionally if the second parameter is false it checks also the validity of the certificate in the first parameter.

    Probably the second method returns false because you are specifying a wrong certificate: is not the certificate which performs the signature or its state is revoked or expired or it is issued by an untrusted certificate authority.