从你的第二个异常猜测,因为你想验证 xml 签名,你可能使用了一些类似下面的代码,来自https://www.massapi.com/class/xm/XMLSignatureFactory-2.html的代码
// Step 1: Load an XMLSignatureFactory instance. This factory class will
// be responsible for constructing almost all the major objects we need
// in working with XML Signature in JSR-105 APIs, except those related
// to KeyInfo.
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Step 3 : Find all Xml Signature element into the provided XML
// document (here for sample use only the first)
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new Exception("Cannot find Signature element!");
}
// Step 4: Create a DOMValidateContext instance (extract public key from
// the "KeyInfo" bloc using overrided KeySelector impl.)
DOMValidateContext valContext = new DOMValidateContext(new KeyValueKeySelector(), nl.item(0));
// Step 5: Unmarshal the Signature node into an XMLSiganture object.
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
// Step 6 : Validate signature
boolean isValid = signature.validate(valContext);
if (isValid) {
System.out.println("OK");
}
但 xml 表示有类似的东西
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2007/05/xmldsig-more#ecdsa-ripemd160"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>bHS+6uf8KbJV4AGzoHNHLfnXvKM=</DigestValue>
</Reference>
</SignedInfo>
...
</Signature>
因此,工厂未能将 xml 表示解组为 XMLSignature 对象。实际上fac
是由org.jcp.xml.dsig.internal.dom.XMLDSigRI
提供者提供的,请检查$JRE_HOME/lib/security/java.security
确定。fac
是DOMXMLSignatureFactory
类的一个实例。unmarshall 动作将尝试形成Signature
对象,并且必须首先形成子对象,例如CanonicalizationMethod
然后SignatureMethod
,所以最后到达引发异常的调用,在DOMSignedInfo
构造函数DOMSignedInfo(Element var1, XMLCryptoContext var2, Provider var3) throws MarshalException
Element var5 = DOMUtils.getNextSiblingElement(var4, "SignatureMethod");
this.signatureMethod = DOMSignatureMethod.unmarshal(var5);
DOMSignatureMethod.unmarshal
只是静态方法的一步,它用于http://www.w3.org/2007/05/xmldsig-more#ecdsa-ripemd160
查找数字签名算法,但DOMSignatureMethod
仅支持以下算法,
rsa-sha1
rsa-sha256
rsa-sha384
rsa-sha512
dsa-sha1
dsa-sha256
hmac-sha1
hmac-sha256
hmac-sha384
hmac-sha512
ecdsa-sha1
ecdsa-sha256
ecdsa-sha384
ecdsa-sha512
和解决方案:将 BC 提供程序添加到 Security 并使用org.apache.santuario -xmlsec项目中的 XMLSignature 进行验证,
public boolean verify(String signedXML) throws Exception {
Document doc = null;
try (InputStream is = new ByteArrayInputStream(signedXML.getBytes(Charset.forName("utf-8")))) {
doc = MyXMLUtils.read(is, false);
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Signature[1]";
Element sigElement =
(Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
XMLSignature signature = new XMLSignature(sigElement, "");
KeyInfo ki = signature.getKeyInfo();
if (ki == null) {
throw new RuntimeException("No keyinfo");
}
PublicKey pk = signature.getKeyInfo().getPublicKey();
if (pk == null) {
throw new RuntimeException("No public key");
}
return signature.checkSignatureValue(pk);
}
来自 repo 的更多演示:https ://github.com/Honwhy/xml-sec