gpt4 book ai didi

java - 无法使用 Apache Santuario 1.4.6 验证 XML 签名

转载 作者:行者123 更新时间:2023-11-29 09:18:12 27 4
gpt4 key购买 nike

我正在寻求一些帮助来解决我在使用 Apache Santuario Java 库版本 1.4.6 验证 XML 签名时遇到的问题。我有一个客户端/服务器解决方案,其中客户端在将文档发送到服务器之前签署 DOM 文档。我像这样将签名应用于文档:

public static void applySignature(X509Certificate cert, PrivateKey privateKey, Document doc)
{
try
{
XMLSignature sig = new XMLSignature(doc,
"",
XMLSignature.ALGO_ID_SIGNATURE_RSA);

sig.addResourceResolver(new XmlSignatureResolver());
doc.getDocumentElement().appendChild(sig.getElement());

Transforms transforms = new Transforms(doc);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);

sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);

sig.addKeyInfo(cert);
sig.addKeyInfo(cert.getPublicKey());

sig.sign(privateKey);
}
catch (XMLSecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我验证签名如下:

public static boolean verifySignature(X509Certificate cert, Document doc)
{
boolean validSignature = false;

try
{
Element nscontext = createDSctx(doc, "ds", Constants.SignatureSpecNS);

// Remove any attributes of Signed Info
Node signInfoNode = XPathAPI.selectSingleNode(doc, "//ds:SignedInfo", nscontext);

int numAttributes = signInfoNode.getAttributes().getLength();
if (numAttributes > 0)
{
for (int i = 0; i < numAttributes; i++)
{
String attrName = signInfoNode.getAttributes().item(0).getNodeName();
signInfoNode.getAttributes().removeNamedItem(attrName);
}
}

Element sigElement =
(Element) XPathAPI.selectSingleNode(doc, "//ds:Signature", nscontext);
XMLSignature signature = new XMLSignature(sigElement, "");

signature.setFollowNestedManifests(true);
signature.addResourceResolver(new XmlSignatureResolver());

validSignature = signature.checkSignatureValue(cert);

// Remove the signature
sigElement.getParentNode().removeChild(sigElement);
}
catch (XMLSignatureException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (XMLSecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (TransformerException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return validSignature;// validSignature;
}

我遇到的问题是,当我在服务器上验证签名时(如果我在应用签名后立即验证,它会在客户端上工作),我收到以下警告:

2011-11-12 18:30:27 Reference [WARN] Verification failed for URI ""
2011-11-12 18:30:27 Reference [WARN] Expected Digest: EEl+J/jsY8Im2rgjsozBXRxkQjQ=
2011-11-12 18:30:27 Reference [WARN] Actual Digest: Y7C0HCjugZbegkZT4E8A7Bd4qm0=

谢谢你的帮助,
厄尼伯利森

=============

我用来将 dom 从客户端发送到服务器的代码是:

                // Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(m_SenderOutput);

m_Logger.debug("Transforming...");
transformer.transform(source, result);
m_SenderOutput.flush();
m_Logger.debug("Transform complete...");
m_ClientSocket.shutdownOutput();

服务端读取dom的代码是:

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();

m_Logger.debug("Parsing Document");
Document doc = db.parse(m_SenderInput);
m_Logger.debug("Received DOM");

申请签名前的dom类似如下(申请签名前数据加密):

<?xml version="1.0" encoding="UTF-8"?><SmMessageSet xmlns="urn:ccsds:recommendation:service_management:schema:sccs:R1.0" xmlns:ns2="urn:ccsds:recommendation:navigation:schema:ndmxml:R1.5"><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Content"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/><xenc:CipherData><xenc:CipherValue>VosyFTcuAkzo6WPPLnnM2Nka+gpyD9r2cNy3fbSX8RjGg5dKktK9SGZAar5t3ci2mU6Nw9Ski2Td
g1WNei+kgns6vFET5Ff8m5/VIO24sBz30DPO5cAwfLax0slTjZWDRu7XXs/ORSK2PrB8B8qaO+me
W5iPLXjkkL4LnLwZfIvCSdG3JJoOTUhR6CstquTejRBLvTdvry8jB2RncjpV244eng7Bmk7HWcNd
Mz20DujfX14MTyKAQcVAgUhM9MpisveiDRdKYtXWCkma2NcUhpxqzjyPtyJtHVJQfaPZ2kla2NQV
DcMPUvmM+V0Y3kI5NBZq1vlIAg1i5JsZRniB+Q==</xenc:CipherValue></xenc:CipherData></xenc:EncryptedKey></ds:KeyInfo><xenc:CipherData><xenc:CipherValue>8UCDr2ZzDvD5JczkPU7UnxRYBdxs6ZgL5s2ksHyn/FZvBVSwYh6o/Rnx41fnN6uygcylW++zoxSq
a9qcpuS8rFxtw6TtRzZeixJYBgZWVHp9NYiB4WbtZF6iR5EjaGKZdghUgCVtvKKbpbMQTTPRCBym
7HA2iQzNpGH1tcGegDB8+w3ALDP8QN5q3PG2uFBk880KXRozxAxKZVNKZfEZyat0fnzf6J8bTCac
n1lxV02jCWyz1/2Gd/jfo8B2BLXVMZWm0WiM7Z/uk4PFsTQjPmb1CD+E+7Oh8TRJzIqC1dyPQVV+
kgdoJbM/2sZka1VCuUzEIEQ1fhH+iUE0ymtuw+djwhfqDAow1pfRJOsak2cXzLoYO7mwqmIHoeaM
hN9IAtI/TfXDHNSL8ledhYT/ZL2gmNSR1Jze6JZPaXgqkmBEGVgqbzLex/5drxOf/DQVcugSnqEw
uHrikLsjU4jHozNg4PGidJNPCKLPgJaiLX1rgyo9N6pUDMVrNH+Tz1G7EFydzZOrZt+yY8Je17NL
ah8mBQb/S5zGD7642aDR4UmVQthD3LTMIG/oxbzMIh/OOcC432SZ+ShAvUD+bU+GDDdcOKzemLPB
EV6QLstFqonyHLSTQqgIMU5z2NxFpJIKRBClX09q5fOytaRVrGIZgJtOfuT4zFwjmwF66yuiQp0H
gD9O95A+ifmwe8k9KUsAO9Q8alxrXrqhptfsySCYDo2nSXbhSn5cKgsdK4jw5B6zsoQQZxJmzsYT
ZSbo0DhjAbZVszsU0HeTMRKRNlOABXAJPxSmqz2hT/wgYnWSMZt839swJyOZhaMuOUfShAP1iVo+
m5xM+zw7SnsAwFozNw==</xenc:CipherValue></xenc:CipherData></xenc:EncryptedData></SmMessageSet>

在 applySignature 之后和在 verifysignature 方法中修改 SignedInfo 之后,XML 是相同的。当我通过套接字发送文档时,问题似乎出在 SignedInfo Referenced Items 上;不知何故,验证期间发生的转换添加了 46 个字节,导致检查失败。

不知道这是从哪里来的。有人有什么想法吗?

最佳答案

这与您的错误没有直接关系,但稍后会导致失败:您不应该删除或更改 SignedInfo 元素中的任何内容。最终签名值是根据规范化 SignedInfo 元素的摘要计算的。因此,如果您在验证签名之前更改任何内容,您肯定会破坏它。

至于为什么您的文档摘要无效 - 这可能有多种原因,仅凭猜测很难分析。如果您可以在此处发布未签名的文档,那么我可以告诉您正确的摘要值是多少,这会有所帮助,至少...

关于java - 无法使用 Apache Santuario 1.4.6 验证 XML 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com