首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java xades4j中提取xades签名内容?

如何在Java xades4j中提取xades签名内容?
EN

Stack Overflow用户
提问于 2013-07-03 21:42:27
回答 1查看 2K关注 0票数 1

我得到了xades XML作为InputStream。我不关心证书是否有效,复选签名等。我不能提供任何CA或任何其他类型的证书存储/验证。我所需要的就是将文档嵌入到xades文件中作为流或磁盘上的临时文件,这样我就可以处理它们,因为它们是磁盘上的普通文件。有人能提供提取嵌入式文档的代码片段吗?提亚

EN

回答 1

Stack Overflow用户

发布于 2016-04-21 15:23:13

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

代码语言:javascript
复制
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();
        }

    }       
}

它的示例用法:

代码语言:javascript
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17449640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档