pdfbox sign pdf PKCS12无效签名bouncycastle

时间:2015-06-05 21:02:53

标签: pdf cryptography digital-signature pdfbox

已经使用各种版本的PDFBox测试了同样的错误,在pdf阅读器adobe中生成签名无效并且没有显示证书,我使用的是Java Jdk jdk1.8.0_40,PDFBox -app- 2.0.0-20150529.000527 -1365.jar代码如下(mycert22.p12是sha256withrsa 2048bit key):

(pdf显示:签名无效,格式错误或此签名中包含的信息未对用户进行验证)

并且没有看到证书详细说明将会发生什么?

JAVA:

包ca_c4_sign_v2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.CMSSignedGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


public class CA_c4_sign_v2 implements SignatureInterface{

    private static BouncyCastleProvider provider = new BouncyCastleProvider();

  private PrivateKey privKey;

  private Certificate[] cert;

  private SignatureOptions options;

  /**
   * Initialize the signature creator with a keystore (pkcs12) and pin that
   * should be used for the signature.
   * 
   * @param keystore
   *          is a pkcs12 keystore.
   * @param pin
   *          is the pin for the keystore / private key
   */
  public CA_c4_sign_v2(KeyStore keystore, char[] pin)
  {
    try
    {
      /*
       * grabs the first alias from the keystore and get the private key. An
       * alternative method or constructor could be used for setting a specific
       * alias that should be used.
       */
      Enumeration<String> aliases = keystore.aliases();
      String alias = null;
      if (aliases.hasMoreElements())
      {
          alias = aliases.nextElement();
      }
      else
      {
          throw new RuntimeException("Could not find alias");
      }
      privKey = (PrivateKey) keystore.getKey(alias, pin); 
      cert = keystore.getCertificateChain(alias);

      System.out.println("alias: " + alias + "\n" + "cert: " + cert + "\n");

    }
    catch (KeyStoreException e)
    {
      e.printStackTrace();
    }
    catch (UnrecoverableKeyException e)
    {
      System.err.println("Could not extract private key.");
      e.printStackTrace();
    }
    catch (NoSuchAlgorithmException e)
    {
      System.err.println("Unknown algorithm.");
      e.printStackTrace();
    }
  }



  /**
   * Signs the given pdf file.
   * 
   * @param document is the pdf document
   * @return the signed pdf document
   * @throws IOException 
   * @throws COSVisitorException
   * @throws SignatureException
   */
  public File signPDF(File document) throws IOException, COSVisitorException,
      SignatureException
  {
    byte[] buffer = new byte[8 * 1024];
    if (document == null || !document.exists())
    {
        new RuntimeException("Documento nao existe!");
    }

    // creating output document and prepare the IO streams.
    String name = document.getName();
    String substring = name.substring(0, name.lastIndexOf("."));

    File outputDocument = new File(document.getParent(), substring+"_signed.pdf");
    FileInputStream fis = new FileInputStream(document);
    FileOutputStream fos = new FileOutputStream(outputDocument);

    int c;
    while ((c = fis.read(buffer)) != -1)
    {
      fos.write(buffer, 0, c);
    }
    fis.close();
    fis = new FileInputStream(outputDocument);

    File scratchFile = File.createTempFile("pdfbox_scratch", ".bin");
    RandomAccessFile randomAccessFile = new RandomAccessFile(scratchFile, "rw");

    try
    {
      // load document
      PDDocument doc = PDDocument.load(document, randomAccessFile);

      // create signature dictionary
      PDSignature signature = new PDSignature();
      signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
      // subfilter for basic and PAdES Part 2 signatures
      signature.setSubFilter(PDSignature.SUBFILTER_ADBE_X509_RSA_SHA1);
      signature.setName("name");
      signature.setLocation("loc");
      signature.setReason("reason");

      // the signing date, needed for valid signature
      signature.setSignDate(Calendar.getInstance());

      // register signature dictionary and sign interface
      if (options==null)
      {
        doc.addSignature(signature, this);
      } 
      else 
      {
        doc.addSignature(signature, this, options);
      }

      // write incremental (only for signing purpose)
      doc.saveIncremental(fis, fos);
    } 
    finally
    {
      if (randomAccessFile!= null) 
      {
        randomAccessFile.close();
      }
      if (scratchFile != null && scratchFile.exists() && !scratchFile.delete())
      {
        scratchFile.deleteOnExit();
      }
    }
    return outputDocument;
  }

  /**
   * <p>
   * SignatureInterface implementation.
   * </p>
   * 
   * <p>
   * This method will be called from inside of the pdfbox and create the pkcs7
   * signature. The given InputStream contains the bytes that are providen by
   * the byte range.
   * </p>
   * 
   * <p>
   * This method is for internal use only.
   * </p>
   * 
   * <p>
   * Here the user should use his favorite cryptographic library and implement a
   * pkcs7 signature creation.
   * </p>
   */
  public byte[] sign(InputStream content) throws SignatureException,
      IOException
  {
    CMSProcessableInputStream input = new CMSProcessableInputStream(content);
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    // CertificateChain
    List<Certificate> certList = Arrays.asList(cert);

    CertStore certStore = null;
    try
    {
      certStore = CertStore.getInstance("Collection",
          new CollectionCertStoreParameters(certList), provider);
      gen.addSigner(privKey, (X509Certificate) certList.get(0),
          CMSSignedGenerator.DIGEST_SHA256);
      gen.addCertificatesAndCRLs(certStore);
      CMSSignedData signedData = gen.generate(input, false, provider);
      return signedData.getEncoded();
    }
    catch (Exception e)
    {
      // should be handled
      System.err.println("Erro ao criar a assinatura.");
      e.printStackTrace();
    }
    throw new RuntimeException("Problem while preparing signature");
  }

  public static void main(String[] args) throws KeyStoreException,
      NoSuchAlgorithmException, CertificateException, FileNotFoundException,
      IOException, COSVisitorException, SignatureException
  {

      File ksFile = new File("mycert22.p12");
      KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
      char[] pin = "mycert22".toCharArray();

      keystore.load(new FileInputStream(ksFile), pin);

      File document = new File("C4_p.pdf");

      CA_c4_sign_v2 signing = new CA_c4_sign_v2(keystore, pin.clone()); 


      signing.signPDF(document);



  }


}

/**
 * Wrap a InputStream into a CMSProcessable object for bouncy castle. It's an
 * alternative to the CMSProcessableByteArray.
 * 
 * @author Thomas Chojecki
 * 
 */
class CMSProcessableInputStream implements CMSProcessable
{

  InputStream in;

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

  public Object getContent()
  {
    return in;
  }

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

}

0 个答案:

没有答案