質問

.NETのXMLDSIGサポート、具体的にはSignedXmlクラスを適切に動作させようと試みました。私はサードパーティのサービスを実装していますが、最近、すべてのメッセージにデジタル署名が必要になることを要求し始めました...

私の問題は、有効な署名を生成できないように見えることです。サードパーティのサービスと、私が見つけたオンライン署名検証者の両方が、署名を無効として報告します。検証サービス( http://www.aleksey.com/xmlsec/xmldsig-verifier.html )は、ダイジェストとデータの間に不一致があると報告しており、これまでのところ、私が何を間違えているかを理解することができませんでした。

関連するコードは次のとおりです-誰かが私の間違いを見つけることができれば幸いです;

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;
}
役に立ちましたか?

解決

誰かが興味を持っている場合、私は問題を解決してブログに書いた: http://thomasjo.com/blog/ 2009/08/04 / xmldsig-in-the-net-framework.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top