【问题标题】:How can I extract xades signed content in Java xades4j?如何在 Java xades4j 中提取 xades 签名的内容?
【发布时间】:2013-07-03 13:42:27
【问题描述】:

我有 xades XML 作为 InputStream。我不在乎证书是否有效、检查签名等。我无法提供任何 CA 或任何其他类型的证书存储/验证。我需要的只是将文档作为流或磁盘上的临时文件嵌入到 xades 文件中,这样我就可以处理它们,因为它们是磁盘上的普通文件。有人可以提供提取嵌入式文档的 sn-p 吗? TIA

【问题讨论】:

  • 到目前为止你尝试过什么?你可以发布一些代码,以及你想要做什么的更多细节吗?很难确定如何提供其他帮助。
  • 我不知道如何开始。我知道我必须先解组 XML,但是 xades4j 是否为此提供接口?我必须自己解析 XML 吗?我完全是绿色的......
  • 好的。我在这里找到了一些代码:code.google.com/p/xades4j/issues/detail?id=37,它指向我使用 DocumentBuilderFactory 和 DocumentBuilder 来解析 xml 文件。我会沿着这条路走。
  • 到目前为止,我可以获得 Document 对象,它实际上是纯 XML 节点对象...如何获取 SignedInfo 对象(我想我需要它来获取嵌入文件数据)?在我发现的测试/示例中,我必须提供摘要/签名算法、证书类型等——我没有关于它们的信息。我想打开提供的 xades 文件并获取内容。有什么帮助吗?
  • 抱歉,我不熟悉 xades4j - 我刚刚查看了您的帖子。如果您只是读入然后从纯 XML 中提取一些元素,而没有 xades4j 验证它或任何东西,我建议使用XOM 而不是内置的 XML 解析(DocumentBuilderFactory、DocumentBuilder 等)。我发现它更容易使用。查看上面链接中的“解析 XML 文档”和“导航”部分。不用担心验证或 SAX。希望这会有所帮助。

标签: xades4j


【解决方案1】:

要从 XAdES 签名文件中提取 Base64 编码的签名内容,我使用如下代码。它根本不使用 xades4j。

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.bouncycastle.util.encoders.Base64;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Utils {

    /**
     * extract ds:Object from .xades file
     *  
     * @param xadesIn .xades file input stream
     * @return base64 decoded bytes
     * @throws Exception
     */
    public static byte[] extractContentFromXadesSignedFile(InputStream xadesIn) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().parse(xadesIn);
        xadesIn.close();                
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        xpath.setNamespaceContext(new SimpleNamespaceContext(new HashMap<String, String>() {{
            put("ds", "http://www.w3.org/2000/09/xmldsig#");
        }}));

        XPathExpression expr = xpath.compile("//ds:SignedInfo/ds:Reference");
        NodeList referenceNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

        /**
         * loop over all Reference nodes
         * i need to find Object node with Id that fits URI value of Reference
         */
        for(int i=0;i<referenceNodes.getLength();i++){
            Node referenceNode = referenceNodes.item(i);
            NamedNodeMap attributes = referenceNode.getAttributes(); 
            if(attributes != null) {
                Node uri = attributes.getNamedItem("URI");
                if(uri != null) {
                    String objectId = uri.getNodeValue();
                    XPathExpression expr2 = xpath.compile("//ds:Object[@Id='"+objectId.substring(1)+"']");
                    Node contentNode = (Node) expr2.evaluate(doc, XPathConstants.NODE);
                    if(contentNode != null) {
                        String base64 = contentNode.getFirstChild().getNodeValue();
                        return Base64.decode(base64);
                    }
                }
            }
        }

        return null;
    }

    /**
     * http://stackoverflow.com/a/6392700/404395
     */
    private static class SimpleNamespaceContext implements NamespaceContext {
        private final Map<String, String> PREF_MAP = new HashMap<String, String>();

        public SimpleNamespaceContext(final Map<String, String> prefMap) {
            PREF_MAP.putAll(prefMap);       
        }

        @Override
        public String getNamespaceURI(String prefix) {
            return PREF_MAP.get(prefix);
        }

        @Override
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }

    }       
}

示例用法:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

public class XadesExtractTest {

    @Test
    public void extract() throws Exception {
        InputStream in = XadesExtractTest.class.getClassLoader().getResourceAsStream("test.xades");
        byte[] bytes = Utils.extractContentFromXadesSignedFile(in);
        Assert.assertNotNull(bytes);
        in.close();
        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
        File f = File.createTempFile("test", ".zip");
        System.out.println(f.getAbsolutePath());
        FileOutputStream fout = new FileOutputStream(f);
        IOUtils.copy(bin, fout);
        bin.close();
        fout.close();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2011-08-26
    • 1970-01-01
    • 2019-03-20
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多