вторник, 30 сентября 2014 г.

Bouncy Castle and Crypto Pro. Detach signature from signed data.

In this post I will write how to:

1) detach signature that is attached to the data
2) view the content of signature.

Bouncy Castle is a good library but it is hard to find something in its documentation.

Here is the class that detaches the signature and writes the content of data to the File:


public class DetachSign {

    private File inFile;
    private File outputFile;

    public DetachSign(File inFile, File outputFile) {
 super();
 this.inFile = inFile;
 this.outputFile = outputFile;
    }

    public void detachSign() throws CMSException, IOException, CertStoreException {
 // Input file stream data+signature.
 FileInputStream fInSig = new FileInputStream(inFile);
 CMSSignedDataParser parser = null;
 parser = new CMSSignedDataParser(fInSig);

 InputStream isream = parser.getSignedContent().getContentStream();
 FileOutputStream fos = new FileOutputStream(outputFile);

 try {
     IOUtils.copy(isream, fos);

     // Signers list.
     SignerInformationStore signers = parser.getSignerInfos();
     Store cs = parser.getCertificates();

     Collection signerInfos = signers.getSigners();
     Iterator it = signerInfos.iterator();

     while (it.hasNext()) {

  
  SignerInformation nextSigner = it.next();
  Collection certCollection = cs.getMatches(nextSigner.getSID());

  if (certCollection.isEmpty()) {
      break;
  }
  else {
      Iterator certIt = certCollection.iterator();
      X509CertificateHolder certHolder = certIt.next();

      System.out.println("Version:" + certHolder.getVersion());
      System.out.println("Serial number:" + certHolder.getSerialNumber().toString());
      System.out.println("Signature algorithm:" + certHolder.getSignatureAlgorithm().getAlgorithm().getId());
      System.out.println("Date before:" + certHolder.getNotBefore());
      System.out.println("Date after:" + certHolder.getNotAfter());

      System.out.println("Subject:");
      RDN[] rdns = certHolder.getSubject().getRDNs();

      for (RDN rdn : rdns) {
   System.out.println("oid:" + rdn.getFirst().getType() + " value:" + IETFUtils.valueToString(rdn.getFirst().getValue()));
      }
      // /////////////////
      System.out.println("Issuer:");
      rdns = certHolder.getIssuer().getRDNs();

      for (RDN rdn : rdns) {
   System.out.println("oid:" + rdn.getFirst().getType() + " value:" + IETFUtils.valueToString(rdn.getFirst().getValue()));
      }
      // ///////////
      List oids = certHolder.getExtensionOIDs();
      System.out.println("list of oids:" + oids);
      for (ASN1ObjectIdentifier oid : oids) {
   X509Extension ext = certHolder.getExtension(oid);
   System.out.println(IETFUtils.valueToString(ext.getParsedValue()));
      }

  }

     }

 } finally {
     IOUtils.closeQuietly(isream);
     IOUtils.closeQuietly(fInSig);
     IOUtils.closeQuietly(fos);
 }
    }

 public static void main(String[] args) {
 // Add secutiry provider.
 if (Security.getProvider("BC") == null) {
     Security.addProvider(new BouncyCastleProvider());
 }

 

 File inFile = new File("d:\\infile.pdf.sig");
 File outFile = new File("d:\\out.pdf");

 DetachSign sign = new DetachSign(inFile, outFile);
 try {
     sign.detachSign();
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 } catch (CMSException e) {
     e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 } catch (CertStoreException e) {
     e.printStackTrace();
 }

 System.out.println("finished");
    }

}

Комментариев нет:

Отправить комментарий