Question

J'ai essayé d'obtenir le support de XMLDSIG dans .NET pour qu'il se comporte correctement, plus particulièrement la classe SignedXml. J'implémente un service tiers et ils viennent tout juste d'exiger que tous les messages soient signés numériquement ...

Mon problème est que je n'arrive pas à générer des signatures valides. Le service tiers et un vérificateur de signature en ligne que j'ai trouvé signalent la signature comme non valide. Le service de vérification ( http://www.aleksey.com/xmlsec/xmldsig-verifier.html ) signale qu'il existe une discordance entre le résumé et les données, et je n'ai pas encore réussi à comprendre ce que je faisais mal.

Voici le code pertinent - j'espère que quelqu'un pourra repérer mon erreur;

public static XDocument SignDocument(XDocument originalDocument, X509Certificate2 certificate)
{
    var document = new XmlDocument();
    document.LoadXml(originalDocument.ToString(SaveOptions.DisableFormatting));
    if (document.DocumentElement == null)
        throw new InvalidOperationException("Invalid XML document; no root element found.");

    var signedDocument = new SignedXml(document);
    Reference signatureReference = GetSignatureReference();
    KeyInfo certificateKeyInfo = GetCertificateKeyInfo(certificate);
    var dataObject = new DataObject("", "text/xml", "utf-8", document.DocumentElement);

    signedDocument.AddReference(signatureReference);
    signedDocument.AddObject(dataObject);
    signedDocument.SigningKey = certificate.PrivateKey;
    signedDocument.KeyInfo = certificateKeyInfo;
    signedDocument.ComputeSignature();

    return XDocument.Parse(signedDocument.GetXml().OuterXml, LoadOptions.PreserveWhitespace);
}


private static Reference GetSignatureReference()
{
    var signatureReference = new Reference("");
    signatureReference.AddTransform(new XmlDsigEnvelopedSignatureTransform());

    return signatureReference;
}


private static KeyInfo GetCertificateKeyInfo(X509Certificate certificate)
{
    var certificateKeyInfo = new KeyInfo();
    certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate));

    return certificateKeyInfo;
}
Était-ce utile?

La solution

Au cas où quelqu'un serait intéressé, j'ai résolu le problème et écrit à ce sujet sur mon blog: http://thomasjo.com/blog/ 2009/08/04 / xmldsig-in-the-net-framework.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top