首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XAdES SignedXml.CheckSignature()总是返回false

XAdES SignedXml.CheckSignature()总是返回false
EN

Stack Overflow用户
提问于 2017-07-20 11:13:07
回答 1查看 1.4K关注 0票数 0

我使用了来自MSDN的以下方法来签名和验证XML文件。

问题是我无法验证签名的XML文件。SignedXML.CheckSignature()方法总是返回false,甚至不会对出错的地方抛出任何异常。

方法用于对XML文件进行签名。

代码语言:javascript
复制
public static void SignXMLFile(XmlDocument xmlDoc, string XmlSigFileName)
{
    // Create a SignedXml object.
    SignedXml signedXml = new SignedXml(xmlDoc);

    string certPath = @"path to PFX file.pfx";

    X509Certificate2 cert = new X509Certificate2(certPath, "password", X509KeyStorageFlags.Exportable);

    var exportedKeyMaterial = cert.PrivateKey.ToXmlString(true);

    var Key = new RSACryptoServiceProvider(new CspParameters(24));
    Key.PersistKeyInCsp = false;

    Key.FromXmlString(exportedKeyMaterial);

    // Assign the key to the SignedXml object.
    signedXml.SigningKey = Key;

    //// Create a reference to be signed.
    //Reference reference = new Reference(System.IO.File.Open(@"D:\test.docx",System.IO.FileMode.Open));

    //// Add the passed URI to the reference object.
    //reference.Uri = URIString;

    //// Add the reference to the SignedXml object.
    //signedXml.AddReference(reference);


    // Create a reference to be signed.
    Reference reference = new Reference();

    // Add the passed URI to the reference object.
    reference.Uri = "";

    // Add the reference to the SignedXml object.
    signedXml.AddReference(reference);

    //Save the public key into the KeyValue node of the Signature
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new RSAKeyValue(Key));
    signedXml.KeyInfo = keyInfo;

    // Compute the signature.
    signedXml.ComputeSignature();

    // Get the XML representation of the signature and save
    // it to an XmlElement object.
    XmlElement xmlDigitalSignature = signedXml.GetXml();

    // Save the signed XML document to a file specified
    //using the passed string.
    XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
    xmlDigitalSignature.WriteTo(xmltw);
    xmltw.Close();

}

方法,用于验证XML文件的签名。

代码语言:javascript
复制
// Verify the signature of an XML file and return the result.
public static Boolean VerifyXmlFile(String Name)
{
    // Check the arguments.  
    if (Name == null)
        throw new ArgumentNullException("Name");

    // Create a new XML document.
    XmlDocument xmlDocument = new XmlDocument();

    // Format using white spaces.
    xmlDocument.PreserveWhitespace = true;

    // Load the passed XML file into the document. 
    xmlDocument.Load(Name);

    // Create a new SignedXml object and pass it
    // the XML document class.
    SignedXml signedXml = new SignedXml(xmlDocument);

    // Find the "Signature" node and create a new
    // XmlNodeList object.
    XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");

    // Load the signature node.
    signedXml.LoadXml((XmlElement)nodeList[0]);

    signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigBase64TransformUrl;

    X509Certificate2 cert = new X509Certificate2(@"path to PFX file.pfx", "password");

    // Check the signature and return the result.
    return signedXml.CheckSignature(cert, true);
}

我试过从堆叠溢出的所有建议,但没有运气。这里的任何帮助都是非常感谢的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-07-21 15:16:54

根据注释中的数据,您的问题是试图使用外部(分离)签名,尽管使用""的Uri显示了代码,该Uri表示“整个文档”。

MSDN上的SignedXml备注部分中,我们得到了几个宝石:

还有第四种签名,称为外部分离签名,当数据和签名位于单独的XML文档中时。SignedXml类不支持外部分离签名。

..。

元素的URI属性 ..。

  • 将URI属性设置为空字符串表示正在对文档的根元素进行签名,这是一种信封签名形式。

..。

  • 其他任何东西都被认为是外部资源分离的签名,SignedXml类不支持。

还有一个完整的章节叫做“外部引用的问题”。该部分提供了一个指向应用安全更新3141780之后,.NET框架应用程序在处理包含SignedXml的文件时会遇到异常错误或意外故障的链接。

该链接讨论了一个注册表项(我不打算在这里发布,请随时遵循它),它说明了如何选择退出MS16-035安全修复的特定部分。它还附有一张便条:

启用此注册表项的警告可能允许安全漏洞,包括拒绝服务、分布式反射拒绝服务、信息泄漏、签名旁路和远程代码执行。

所以,如果你需要的话.注意。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45213055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档