SOAP消息中的数字签名

时间:2012-04-12 06:39:19

标签: java xml soap xml-serialization digital-signature

我正在尝试使用Java中的签名(俄语GOST34.10-2001算法)为某些政府服务创建SOAP消息。问题是该服务告诉我Signature无效,我无法看到我在哪里犯了错误。我从中读取私钥和证书的密钥容器是有效的。 哪里出错了?我知道我的问题非常具体,但我仍然希望有人能指引我。

update1:​​一点点更新:消息正文有一些西里尔(utf)-8节点和值 UPDATE2: 所以我通读了this question并尝试以相同的方式获取PrivateKey并获得此异常: java.lang.IllegalArgumentException:私钥算法与最终实体证书中的公钥算法不匹配

以下是一些代码:

这里我将类编组到SOAP正文中:

try {
            MessageFactory mf = MessageFactory.newInstance();
            SOAPMessage sm = mf.createMessage();
            SOAPPart sp = sm.getSOAPPart();
            SOAPEnvelope envelope = sp.getEnvelope();

            QName bodyId = new QName("http://ws.unisoft/", "SendFullULRequest");

            SOAPBody sb = (SOAPBody) envelope.getBody();
            SOAPHeader sh = (SOAPHeader) envelope.getHeader();

            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(FullULRq.class);
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();

            marshaller.marshal(new JAXBElement<FullULRq>(bodyId, FullULRq.class, fulr),
                    sm.getSOAPBody());

            sm.saveChanges();

然后我计算摘要值:

ReferenceType rt2 = new ReferenceType();
            rt2.setURI("#body");
            ////Пункт 7
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            Source xmlSource = new DOMSource(sm.getSOAPBody());
            Result outputTarget = new StreamResult(outputStream);
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                transformer.setOutputProperty(OutputKeys.INDENT, "no");
                transformer.transform(xmlSource, outputTarget);
                Init.init();

                Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
                final ByteArrayInputStream is = new ByteArrayInputStream(canon.canonicalize(outputStream.toByteArray()));


                rt2.setDigestValue(computeDigestWithStream(is));
            } catch (Exception e) {
                e.printStackTrace();
            }

使用这种方法:

public static byte[] computeDigestWithStream(ByteArrayInputStream stream) throws Exception {
    final MessageDigest digest =
            MessageDigest.getInstance("GOST3411");

    // processing data
    final DigestInputStream digestStream =
            new DigestInputStream(stream, digest);
    while (digestStream.available() != 0) {
        digestStream.read();
    }

最后我从块中生成一个签名(其中存在元素):

        ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
        Source xmlSource2 = new DOMSource(sm.getSOAPBody());
        Result outputTarget2 = new StreamResult(outputStream2);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.transform(xmlSource2, outputTarget2);
        Init.init();
        Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
        InputStream is2 = new ByteArrayInputStream(outputStream2.toByteArray());
        byte[] bb = canon.canonicalize(outputStream2.toByteArray());
        byte[] signval = sign("CryptoProSignature", key, bb);

sign()方法:

public static byte[] sign(String algorithmName, PrivateKey privateKey, byte[] data) throws Exception {
        final Signature sig = Signature.getInstance(algorithmName);
        sig.initSign(privateKey);
        sig.update(data);
        return sig.sign();
    }

如果它是相关的,这就是我从容器中读取privateKey的方式:

 KeyStore ks = KeyStore.getInstance("FloppyStore");

        String passwd = "*";
        String kalias = null; 

        ks.load(null, null);

        Enumeration enum1 = ks.aliases();
        for (; enum1.hasMoreElements();) {
            String tAlias = (String) enum1.nextElement();
            if (ks.isKeyEntry(tAlias)) {
                kalias = tAlias;
            }
            System.out.println(tAlias);
        }


        PrivateKey key = (PrivateKey) ks.getKey(kalias, passwd.toCharArray());

谢谢!

0 个答案:

没有答案