使用PDFBox和BouncyCastle签名PDF

时间:2015-05-22 15:35:56

标签: java pdf pdfbox

我尝试使用PDFBox签名PDF,但它确实签名但是当我在adobe reader中打开文档时,我收到以下消息"文档自签名后被更改或损坏&#34 ;有人可以帮我找到问题。

密钥库是使用" keytool -genkeypair -storepass创建的123456 -storetype pkcs12 -alias test -validity 365 -v -keyalg RSA -keystore keystore.p12"

使用pdfbox-1.8.9和bcpkix-jdk15on-1.52

这是我的代码:

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaCertStore;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.Store;

import java.io.*;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.util.Calendar;
import java.util.Collections;
import java.util.Enumeration;

public class CreateSignature implements SignatureInterface {
    private static PrivateKey privateKey;
    private static Certificate certificate;

    boolean signPdf(File pdfFile, File signedPdfFile) {

        try (
                FileInputStream fis1 = new FileInputStream(pdfFile);
                FileInputStream fis = new FileInputStream(pdfFile);
                FileOutputStream fos = new FileOutputStream(signedPdfFile);
                PDDocument doc = PDDocument.load(pdfFile)) {
            int readCount;
            byte[] buffer = new byte[8 * 1024];
            while ((readCount = fis1.read(buffer)) != -1) {
                fos.write(buffer, 0, readCount);
            }

            PDSignature signature = new PDSignature();
            signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
            signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
            signature.setName("NAME");
            signature.setLocation("LOCATION");
            signature.setReason("REASON");
            signature.setSignDate(Calendar.getInstance());
            doc.addSignature(signature, this);
            doc.saveIncremental(fis, fos);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public byte[] sign(InputStream is) throws SignatureException, IOException {
        try {
            BouncyCastleProvider BC = new BouncyCastleProvider();
            Store certStore = new JcaCertStore(Collections.singletonList(certificate));

            CMSTypedDataInputStream input = new CMSTypedDataInputStream(is);
            CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
            ContentSigner sha512Signer = new JcaContentSignerBuilder("SHA256WithRSA").setProvider(BC).build(privateKey);

            gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
                    new JcaDigestCalculatorProviderBuilder().setProvider(BC).build()).build(sha512Signer, new X509CertificateHolder(certificate.getEncoded())
            ));
            gen.addCertificates(certStore);
            CMSSignedData signedData = gen.generate(input, false);

            return signedData.getEncoded();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException, GeneralSecurityException, SignatureException, COSVisitorException {
        char[] password = "123456".toCharArray();

        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(new FileInputStream("/home/user/Desktop/keystore.p12"), password);

        Enumeration<String> aliases = keystore.aliases();
        String alias;
        if (aliases.hasMoreElements()) {
            alias = aliases.nextElement();
        } else {
            throw new KeyStoreException("Keystore is empty");
        }
        privateKey = (PrivateKey) keystore.getKey(alias, password);
        Certificate[] certificateChain = keystore.getCertificateChain(alias);
        certificate = certificateChain[0];

        File inFile = new File("/home/user/Desktop/sign.pdf");
        File outFile = new File("/home/user/Desktop/sign_signed.pdf");
        new CreateSignature().signPdf(inFile, outFile);
    }
}


class CMSTypedDataInputStream implements CMSTypedData {
    InputStream in;

    public CMSTypedDataInputStream(InputStream is) {
        in = is;
    }

    @Override
    public ASN1ObjectIdentifier getContentType() {
        return PKCSObjectIdentifiers.data;
    }

    @Override
    public Object getContent() {
        return in;
    }

    @Override
    public void write(OutputStream out) throws IOException,
            CMSException {
        byte[] buffer = new byte[8 * 1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
    }
}

1 个答案:

答案 0 :(得分:6)

修复“文档已被更改或损坏”

错误的是,您使用PDDocument.saveIncremental仅使用原始PDF调用InputStream

FileInputStream fis1 = new FileInputStream(pdfFile);
FileInputStream fis = new FileInputStream(pdfFile);
FileOutputStream fos = new FileOutputStream(signedPdfFile);
...
doc.saveIncremental(fis, fos);

但该方法希望InputStream覆盖原始文件以及为准备签名所做的更改。

因此,fis也需要指向signedPdfFile,并且由于之前可能不存在该文件,因此必须切换创建fisfos的顺序&gt; < / p>

FileInputStream fis1 = new FileInputStream(pdfFile);
FileOutputStream fos = new FileOutputStream(signedPdfFile);
FileInputStream fis = new FileInputStream(signedPdfFile);
...
doc.saveIncremental(fis, fos);

不幸的是,JavaDocs并不指出这一点。

另一个问题

生成的签名还有另一个问题。如果您查看示例结果的ASN.1转储,您将看到如下所示的内容:

    <30 80>
   0 NDEF: SEQUENCE {
    <06 09>
   2    9:   OBJECT IDENTIFIER signedData (1 2 840 113549 1 7 2)
         :     (PKCS #7)
    <A0 80>
  13 NDEF:   [0] {
    <30 80>
  15 NDEF:     SEQUENCE {
    <02 01>
  17    1:       INTEGER 1
    <31 0F>
  20   15:       SET {

NDEF长度指示表明不定长度方法用于编码签名容器的这些外层。在基本编码规则(BER)中允许使用此方法,但在更严格的可分辨编码规则(DER)中不允许使用此方法。虽然通用PKCS#7 / CMS签名允许使用外层BER,但PDF规范明确要求:

  

当使用PKCS#7签名时,内容的值应为包含签名的DER编码的PKCS#7二进制数据对象。

     

(第12.8.3.3.1节“ISO 32000中使用的PKCS#7签名”/ ISO 32000-1中的“常规”)

因此,严格来说,您的签名甚至在结构上无效。但是,通常情况下,PDF签名验证服务无法检测到这一点,因为大多数都使用标准的PKCS#7 / CMS库或方法来验证签名容器。

如果您想确保您的签名是真正有效的PDF签名,您可以通过替换

来实现此目的
return signedData.getEncoded();

类似

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DEROutputStream dos = new DEROutputStream(baos);
dos.writeObject(signedData.toASN1Structure());
return baos.toByteArray();

现在整个签名对象都是DER编码的。

(您可以在此处找到使用原始代码和固定代码创建签名的测试,无论是否有改进的编码:SignLikeLoneWolf.java