XML Canonicalization算法在直接调用时比在作为xml数字签名的一部分调用时给出两个不同的结果?
-
06-07-2019 - |
题
当我直接规范某些xml时,我得到两个不同的同一个xml文档的哈希,而不是在我执行数字签名时,它还会在对其进行哈希处理之前对xml执行相同的规范化算法?我发现数字签名规范化包括新的行字符'\ n'和规范化时的间距字符,而直接算法则没有。
虽然包含新行字符+空格不在规范化规范中?我特别关注这个版本的 http://www.w3.org / TR / 2001 / REC-xml的-C14N-20010315
有谁知道发生了什么事?我已经包含了xml doc和代码的两种实现,所以你可以看到。
这真让我感到困惑,我想知道为什么,我错过了一些明显的东西吗?
<root>
<child1>some text</child1>
<child2 attr="1" />
</root>
直接规范化代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;
namespace XML_SignatureGenerator
{
class XML_C14N
{
private String _filename;
private Boolean isCommented = false;
private XmlDocument xmlDoc = null;
public XML_C14N(String filename)
{
_filename = filename;
xmlDoc = new XmlDocument();
xmlDoc.Load(_filename);
}
//implement this spec http://www.w3.org/TR/2001/REC-xml-c14n-20010315
public String XML_Canonalize(System.Windows.Forms.RichTextBox tb)
{
//create c14n instance and load in xml file
XmlDsigC14NTransform c14n = new XmlDsigC14NTransform(isCommented);
c14n.LoadInput(xmlDoc);
//get canonalised stream
Stream s1 = (Stream)c14n.GetOutput(typeof(Stream));
SHA1 sha1 = new SHA1CryptoServiceProvider();
Byte[] output = sha1.ComputeHash(s1);
tb.Text = Convert.ToBase64String(output);
//create new xmldocument and save
String newFilename = _filename.Substring(0, _filename.Length - 4) + "C14N.xml";
XmlDocument xmldoc2 = new XmlDocument();
xmldoc2.Load(s1);
xmldoc2.Save(newFilename);
return newFilename;
}
public void set_isCommented(Boolean value)
{
isCommented = value;
}
public Boolean get_isCommented()
{
return isCommented;
}
}
}
xml数字签名代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
namespace XML_SignatureGenerator
{
class xmlSignature
{
public xmlSignature(String filename)
{
_filename = filename;
}
public Boolean SignXML()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
String fname = _filename; //"C:\\SigTest.xml";
xmlDoc.Load(fname);
SignedXml xmlSig = new SignedXml(xmlDoc);
xmlSig.SigningKey = rsa;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigC14NTransform env = new XmlDsigC14NTransform(false);
reference.AddTransform(env);
xmlSig.AddReference(reference);
xmlSig.ComputeSignature();
XmlElement xmlDigitalSignature = xmlSig.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
xmlDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/SignedXML.xml");
return true;
}
private String _filename;
}
}
任何想法都会很棒!顺便说一下,这都是C#代码。
提前致谢
乔恩
解决方案
在我看来,XML Sig处理空白的方式被打破了。它肯定不符合大多数正确思考的人所谓的规范化。更改空格应该不影响摘要,但在xmlsig中,确实如此。
一种可能的解决方法是在将文档传递给签名生成代码之前,将文档传递给规范化程序。这应该让事情变得更加可预测。
这篇文章可能有所帮助澄清事情。
其他提示
看起来你的第二段代码
xmlDoc.PreserveWhitespace = true;
而在第一次你没有。
据我了解,规范化规范要求保留元素之间的空白,所以我建议你在两者中包含这一行。
不隶属于 StackOverflow