使用.net验证SOAP信封中的xml签名
我必须在SOAP响应中验证xml签名。我是用Java来实现的,但我必须用.Net来实现,但我无法实现。 下面是执行此任务的Java代码:使用.net验证SOAP信封中的xml签名,.net,validation,soap,x509,.net,Validation,Soap,X509,我必须在SOAP响应中验证xml签名。我是用Java来实现的,但我必须用.Net来实现,但我无法实现。 下面是执行此任务的Java代码: public class SigVal { private PublicKey publicKey; public XMLSignatureValidator(PublicKey publicKey) { this.publicKey = publicKey; } public boolean isValid(Node dsSigna
public class SigVal {
private PublicKey publicKey;
public XMLSignatureValidator(PublicKey publicKey) {
this.publicKey = publicKey;
}
public boolean isValid(Node dsSignature)
{
DOMValidateContext context = new DOMValidateContext(publicKey, dsSignature);
String providerName = System.getProperty "jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM", (Provider) Class
.forName(providerName).newInstance());
XMLSignature signature = factory.unmarshalXMLSignature(context);
boolean coreValidity = signature.validate(context);
}
}
公钥来自从wsse:BinarySecurityToken生成的X509证书。dsSignature节点是ds:Signature元素
这就是SOAP响应:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<SOAP-ENV:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
<wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="CertId-33637684">MII..==</wsse:BinarySecurityToken>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<ds:Reference URI="#id-23582192">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>VvfpzVGZkr/AQ3krGcdklXujj3w=</ds:DigestValue>
</ds:Reference>
<ds:Reference URI="#id-961374">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>KGBEkW1e0fkV3ooAptSIldv9ftQ=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
StF...=
</ds:SignatureValue>
<ds:KeyInfo Id="KeyId-12282214">
<wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="STRId-314846">
<wsse:Reference URI="#CertId-33637684" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-23582192">
<wsu:Created>2013-06-06T07:08:19.323Z</wsu:Created>
<wsu:Expires>2013-06-06T07:13:19.323Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
<wsa:MessageID SOAP-ENV:mustUnderstand="0">uuid:dddca070-ce77-11e2-b9ac-d6977a2ed2bf</wsa:MessageID>
<wsa:To SOAP-ENV:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsa:Action SOAP-ENV:mustUnderstand="0">...</wsa:Action>
<From xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing" SOAP-ENV:mustUnderstand="0">
<Address>...</Address>
</From>
<wsa:RelatesTo RelationshipType="wsa:Reply" SOAP-ENV:mustUnderstand="0">urn:uuid:3f4f070b-9e28-410d-b10b-a21a7d62ee86</wsa:RelatesTo>
</SOAP-ENV:Header>
<SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-961374">
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但是CheckSignature返回的总是False。我覆盖了SignedXml.getIdeElement以获取引用元素。但这不起作用
我是否理解正确:我必须从Soap消息(ds:Signature)中获取签名元素,从Soap消息(wsse:BinarySecurityToken)中获取证书并对其进行验证。这就是我在处理Java web服务时所做的工作:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(xmlResponse);
XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName("wsse:BinarySecurityToken");
string binarySecurityToken = xmlNodeList[0].InnerText;
X509Certificate2 x509Certificate2 = new X509Certificate2(Convert.FromBase64String(binarySecurityToken));
xmlNodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#");
XmlElement signature = (XmlElement)xmlNodeList[0];
SignedXmlWithId signedXml = new SignedXmlWithId(xmlDocument);
signedXml.LoadXml(signature);
bool isOk = signedXml.CheckSignature(x509Certificate2, true);
首先,在将xml加载到XmlDocument
对象之前,需要将PreserveWhitespace
设置为true
如果签名元素的id具有名称空间前缀(如本例中的wsu:id
),则SignedXml
将失败,因此替代方法是对其进行子类化并重写GetIdeElement
方法
SignedXmlWithId
类来自
最后,您需要将证书传递给CheckSignature
方法
此外,您不需要设置签名的规范化方法
或其他参数-所有内容都已写入XML
由于.NET3.5和.NET4.0之间的规范化实现发生了变化,一些人遇到了问题(请参阅以获取解决方案),但我没有遇到这个问题
此外,在检查签名之前,请注意不要对响应XML进行任何格式化。(起初,我使用编辑器将响应保存到XML文件中,并在测试时使用该XML。这不起作用,但当我直接从web服务测试响应时,它起作用了。)为什么不使用WSDL和检查生成一个客户端,而不是使用此XML?嗨,Patrick,我在Microsoft.Web.Services3.WebServicesClientProtocol类中得到了soap答案。但我不知道如何在协议类中检查签名?
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(xmlResponse);
XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName("wsse:BinarySecurityToken");
string binarySecurityToken = xmlNodeList[0].InnerText;
X509Certificate2 x509Certificate2 = new X509Certificate2(Convert.FromBase64String(binarySecurityToken));
xmlNodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#");
XmlElement signature = (XmlElement)xmlNodeList[0];
SignedXmlWithId signedXml = new SignedXmlWithId(xmlDocument);
signedXml.LoadXml(signature);
bool isOk = signedXml.CheckSignature(x509Certificate2, true);
public class SignedXmlWithId : SignedXml
{
public SignedXmlWithId(XmlDocument xml) : base(xml)
{
}
public SignedXmlWithId(XmlElement xmlElement)
: base(xmlElement)
{
}
public override XmlElement GetIdElement(XmlDocument doc, string id)
{
// check to see if it's a standard ID reference
XmlElement idElem = base.GetIdElement(doc, id);
if (idElem == null)
{
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
idElem = doc.SelectSingleNode("//*[@wsu:Id=\"" + id + "\"]", nsManager) as XmlElement;
}
return idElem;
}
}