0

我正在尝试验证 pdf 文件中的签名。其中有三个。我已经用我在互联网上找到的代码签署了该文件并采用了我的需要,所以它也可能是不正确的。这是签名文件pdf文件

验证码在这里:

package com.mycompany.verifysignature;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.bouncycastle.crypto.digests.GOST3411Digest;
import ru.CryptoPro.CAdES.CAdESSignature;
import ru.CryptoPro.CAdES.CAdESType;

public class Main {

public static void main(String args[]) {

    try {
        ArrayList<Map<String, String>> resList = new ArrayList<Map<String, String>>();

                    InputStream pdfIs = new FileInputStream("/home/user1/Desktop/321-17.pdf");

        com.itextpdf.text.pdf.PdfReader reader = new com.itextpdf.text.pdf.PdfReader(pdfIs);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        com.itextpdf.text.pdf.PdfStamper stamper = com.itextpdf.text.pdf.PdfStamper.createSignature(reader, baos, '\0');
        com.itextpdf.text.pdf.PdfSignatureAppearance sap = stamper.getSignatureAppearance();

        com.itextpdf.text.pdf.AcroFields fields = reader.getAcroFields();
        for (String signame : fields.getSignatureNames()) {
            HashMap<String, String> m = new HashMap();
            m.put("name", signame.toString());
                            System.out.println("name:"+signame);
            com.itextpdf.text.pdf.PdfDictionary sig = fields.getSignatureDictionary(signame);
            if (sig != null && sig.getAsString(com.itextpdf.text.pdf.PdfName.REASON) != null) {
                m.put("reason", sig.getAsString(com.itextpdf.text.pdf.PdfName.REASON).toString()
                    .replaceAll("\"", "\\\""));
                                    System.out.println("reason:"+sig.getAsString(com.itextpdf.text.pdf.PdfName.REASON).toString()
                    .replaceAll("\"", "\\\""));
            } else {
                m.put("reason", "undefined");
                                    System.out.println("reason:undefined");
            }


            byte signature[] = null;

            if (sig != null && sig.getBytes() != null) {
                signature = sig.getBytes();
            }

            byte hash[] = calcHash(sap.getRangeStream());

            if (hash != null) {

                CAdESSignature  cadesSignature = new CAdESSignature(signature, hash, CAdESType.CAdES_X_Long_Type_1);

                try {
                    cadesSignature.verify(null);
                    m.put("valid", "true");
                                            System.out.println("valid:true");
                } catch(Exception ex) {
                    m.put("valid", "false");
                                            System.out.println("valid:false");
                }
            } else {
                m.put("valid", "\"undefined\"");
                                    System.out.println("valid:undefined");
            }


//              com.itextpdf.text.pdf.security.PdfPKCS7 pk = fields.verifySignature(signame);
//              
//              m.put("valid", new Boolean(pk.verify()).toString());
//                                System.out.println("valid:"+new Boolean(pk.verify()).toString());

            resList.add(m);
        }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

}



public static byte[] calcHash(InputStream is) {
    if (is == null) return null;
    try {
        GOST3411Digest digest = new GOST3411Digest();

        byte node[] = readBytesFromStream(is);
        digest.update(node, 0, node.length);

        byte[] resBuf = new byte[digest.getDigestSize()];
        digest.doFinal(resBuf, 0);

        return resBuf;
    } catch (Throwable e) {
        e.printStackTrace();
        //throw new Exception(e);
    }
    return null;
}   

private static byte[] readBytesFromStream(InputStream is) throws Exception {
    ArrayList<Object[]> c = new ArrayList();
    int n, size = 0;
    byte b[] = null;
    if (is == null) throw new Exception("input stream is null");
    try {
        while ((n = is.read(b = new byte[1024])) > 0) {
            c.add(new Object[] { n, b });
            size += n;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte rv[] = new byte[size];
    int pos = 0;
    for (Object[] bb : c) {
        for (int i = 0; i < (Integer) bb[0]; i++) {
            rv[pos++] = ((byte[]) bb[1])[i];
        }
    }
    return rv;
}



}

我已经签署了使用 GOST3411 制作的文件摘要,并带有在 cryptopro 网站上生成的测试证书。

当我用 pdf 阅读器打开这个文件时,它说有 3 个签名。我真的签了三遍。但是上面的代码从pdf签名中取出了不等于我写的名字的名字。它们看起来像 Signature1、Signature2 等。在所有三种情况下都应该写“CN”。请帮忙。我做错了什么?

4

1 回答 1

1

OP 提供的文件 321-174.pdf 仅使用一个签名而不是三个签名进行签名,主要错误是签名字典内容的Contents不是CMS 签名,而是文本内容,可能是 base64编码的。因此,在您的代码之间进行一些解码似乎是必要的。

话虽如此,我在ISO 32000-1规范的表 257 - SubFilter 值算法支持- 中找不到 GOST3410 - 因此它在这种情况下的使用很可能不会被接受。

于 2014-08-19T21:23:16.887 回答