Search in sources :

Example 1 with CMSTypedData

use of org.bouncycastle.cms.CMSTypedData in project nhin-d by DirectProject.

the class CreateUnSignedPKCS7 method create.

/**
	 * Creates a pcks7 file from the certificate and key files.
	 * @param certFile The X509 DER encoded certificate file.
	 * @param keyFile The PCKS8 DER encoded private key file.
	 * @param password Option password for the private key file.  This is required if the private key file is encrypted.  Should be null or empty
	 * if the private key file is not encrypted.
	 * @param createFile Optional file descriptor for the output file of the pkcs12 file.  If this is null, the file name is based on the 
	 * certificate file name.
	 * @return File descriptor of the created pcks7 file.  Null if an error occurred.  
	 */
public File create(String anchorDir, File createFile, File metaFile, boolean metaExists) {
    File pkcs7File = null;
    FileOutputStream outStr = null;
    InputStream inStr = null;
    // load cert file
    try {
        File userDir = new File(anchorDir);
        File[] files = userDir.listFiles();
        X509Certificate[] certs = new X509Certificate[files.length];
        ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
        int counter = 0;
        for (File certFile : files) {
            if (certFile.isFile() && !certFile.isHidden()) {
                if (certFile.getName().endsWith(".der")) {
                    byte[] certData = loadFileData(certFile);
                    certs[counter] = getX509Certificate(certData);
                    certList.add(certs[counter]);
                    counter++;
                }
            }
        }
        if (counter == 0) {
            error = "Trust Anchors are not available in specified folder!";
            return null;
        }
        byte[] metaDataByte;
        if (metaExists) {
            metaDataByte = loadFileData(metaFile);
        } else {
            metaDataByte = "Absent".getBytes();
        }
        CMSTypedData msg = new CMSProcessableByteArray(metaDataByte);
        Store certStores = new JcaCertStore(certList);
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        //SignedData data = new SignedData(arg0, arg1, arg2, arg3, arg4)
        gen.addCertificates(certStores);
        CMSSignedData sigData = gen.generate(msg, metaExists);
        //System.out.println("Inside Unsigned area: Create File:"+createFile);
        pkcs7File = getPKCS7OutFile(createFile);
        outStr = new FileOutputStream(pkcs7File);
        outStr.write(sigData.getEncoded());
    } catch (CMSException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (IOException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (KeyStoreException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (NoSuchProviderException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (NoSuchAlgorithmException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (CertificateException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (UnrecoverableKeyException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (OperatorCreationException e) {
        //e.printStackTrace(System.err);
        return null;
    } catch (Exception e) {
        //e.printStackTrace(System.err);
        return null;
    } finally {
        IOUtils.closeQuietly(outStr);
        IOUtils.closeQuietly(inStr);
    }
    return pkcs7File;
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) ArrayList(java.util.ArrayList) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) Store(org.bouncycastle.util.Store) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) CMSTypedData(org.bouncycastle.cms.CMSTypedData) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) CMSException(org.bouncycastle.cms.CMSException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) FileOutputStream(java.io.FileOutputStream) NoSuchProviderException(java.security.NoSuchProviderException) File(java.io.File) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 JcaCertStore (org.bouncycastle.cert.jcajce.JcaCertStore)1 CMSException (org.bouncycastle.cms.CMSException)1 CMSProcessableByteArray (org.bouncycastle.cms.CMSProcessableByteArray)1 CMSSignedData (org.bouncycastle.cms.CMSSignedData)1 CMSSignedDataGenerator (org.bouncycastle.cms.CMSSignedDataGenerator)1 CMSTypedData (org.bouncycastle.cms.CMSTypedData)1 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)1