Search in sources :

Example 76 with NoSuchProviderException

use of java.security.NoSuchProviderException 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)

Example 77 with NoSuchProviderException

use of java.security.NoSuchProviderException in project otertool by wuntee.

the class JarSigner method loadKeyStore.

void loadKeyStore(String keyStoreName, boolean prompt) {
    if (!nullStream && keyStoreName == null) {
        keyStoreName = System.getProperty("user.home") + File.separator + ".keystore";
    }
    try {
        if (providerName == null) {
            store = KeyStore.getInstance(storetype);
        } else {
            store = KeyStore.getInstance(storetype, providerName);
        }
        if (nullStream) {
            store.load(null, storepass);
        } else {
            keyStoreName = keyStoreName.replace(File.separatorChar, '/');
            URL url = null;
            try {
                url = new URL(keyStoreName);
            } catch (java.net.MalformedURLException e) {
                // try as file
                url = new File(keyStoreName).toURI().toURL();
            }
            InputStream is = null;
            try {
                is = url.openStream();
                store.load(is, storepass);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }
    } catch (IOException ioe) {
        throw new RuntimeException(rb.getString("keystore load: ") + ioe.getMessage());
    } catch (java.security.cert.CertificateException ce) {
        throw new RuntimeException(rb.getString("certificate exception: ") + ce.getMessage());
    } catch (NoSuchProviderException pe) {
        throw new RuntimeException(rb.getString("keystore load: ") + pe.getMessage());
    } catch (NoSuchAlgorithmException nsae) {
        throw new RuntimeException(rb.getString("keystore load: ") + nsae.getMessage());
    } catch (KeyStoreException kse) {
        throw new RuntimeException(rb.getString("unable to instantiate keystore class: ") + kse.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DerInputStream(sun.security.util.DerInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) URL(java.net.URL) NoSuchProviderException(java.security.NoSuchProviderException) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 78 with NoSuchProviderException

use of java.security.NoSuchProviderException in project nhin-d by DirectProject.

the class SplitDirectRecipientInformation method getContentStream.

/**
	 * {@inheritDoc}
	 */
@Override
public CMSTypedStream getContentStream(Key key, /*private key*/
String prov) throws /*ignored, use class variables instead*/
CMSException, NoSuchProviderException {
    // this is the symmetric key
    final byte[] encryptedKey = info.getEncryptedKey().getOctets();
    // this is the algorithm that protects the symmetric key
    final String keyExchangeAlgorithm = getExchangeEncryptionAlgorithmName(_keyEncAlg.getObjectId());
    // this is the algorithm of the symmetric key to actually decrypt the content
    final String alg = EncryptionAlgorithm.fromOID(_encAlg.getObjectId().getId(), EncryptionAlgorithm.AES128_CBC).getAlgName();
    try {
        Cipher keyCipher = Cipher.getInstance(keyExchangeAlgorithm, keyEncProvider);
        Key sKey;
        try {
            // the original BC libraries attempted to do an UNWRAP assuming that the 
            // same provider was used for secret key decryption and message decryption
            // when these two operations are split into separate providers, using an unwrap method
            // may result in a secret key handle that may not be usable by the another provider
            // for that reason, this class will do a straight up decrypt of the message's internal
            // secret key and hand that key off to the "encProvider" provider
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (GeneralSecurityException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (IllegalStateException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (UnsupportedOperationException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (ProviderException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        }
        return getContentFromSessionKey(sKey, encProvider);
    } catch (NoSuchAlgorithmException e) {
        throw new CMSException("can't find algorithm.", e);
    } catch (InvalidKeyException e) {
        throw new CMSException("key invalid in message.", e);
    } catch (NoSuchPaddingException e) {
        throw new CMSException("required padding not supported.", e);
    } catch (IllegalBlockSizeException e) {
        throw new CMSException("illegal blocksize in message.", e);
    } catch (BadPaddingException e) {
        throw new CMSException("bad padding in message.", e);
    }
}
Also used : ProviderException(java.security.ProviderException) NoSuchProviderException(java.security.NoSuchProviderException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) Key(java.security.Key) CMSException(org.bouncycastle.cms.CMSException)

Example 79 with NoSuchProviderException

use of java.security.NoSuchProviderException in project robovm by robovm.

the class KeyPairGenerator2Test method GetInstance02.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code>
     * method
     * Assertions:
     * throws NullPointerException  when algorithm is null
     * throws NoSuchAlgorithmException when algorithm is incorrect;
     * throws IllegalArgumentException when provider is null;
     * throws NoSuchProviderException when provider is available;
     * returns
     * KeyPairGenerator object
     */
public void GetInstance02(int mode) throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        KeyPairGenerator.getInstance(null, mProv.getName());
        fail("NullPointerException or KeyStoreException must be thrown");
    } catch (NoSuchAlgorithmException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            KeyPairGenerator.getInstance(invalidValues[i], mProv.getName());
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    String prov = null;
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        try {
            KeyPairGenerator.getInstance(alg, prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(alg).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        for (int j = 1; j < invalidValues.length; j++) {
            try {
                KeyPairGenerator.getInstance(alg, invalidValues[j]);
                fail("NoSuchProviderException must be thrown (algorithm: ".concat(alg).concat(" provider: ").concat(invalidValues[j]).concat(")"));
            } catch (NoSuchProviderException e) {
            }
        }
    }
    KeyPairGenerator kpG;
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        kpG = KeyPairGenerator.getInstance(alg, mProv.getName());
        assertEquals("Incorrect algorithm", kpG.getAlgorithm().toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
        assertEquals("Incorrect provider", kpG.getProvider().getName(), mProv.getName());
        checkResult(kpG, mode);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchProviderException(java.security.NoSuchProviderException)

Example 80 with NoSuchProviderException

use of java.security.NoSuchProviderException in project robovm by robovm.

the class myCertPathBuilder method testCertPathBuilder13.

/**
     * Test for <code>getAlgorithm()</code> method Assertion: returns
     * CertPathBuilder object
     */
public void testCertPathBuilder13() throws NoSuchAlgorithmException {
    if (!PKIXSupport) {
        fail(NotSupportMsg);
        return;
    }
    for (int i = 0; i < validValues.length; i++) {
        CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]);
        try {
            cpb = CertPathBuilder.getInstance(validValues[i], defaultProviderName);
            assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]);
        } catch (NoSuchProviderException e) {
            fail("Unexpected NoSuchProviderException exeption " + e.getMessage());
        }
        try {
            cpb = CertPathBuilder.getInstance(validValues[i], defaultProviderName);
            assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]);
        } catch (NoSuchProviderException e) {
            fail("Unexpected NoSuchProviderException " + e.getMessage());
        }
    }
}
Also used : CertPathBuilder(java.security.cert.CertPathBuilder) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)102 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)75 InvalidKeyException (java.security.InvalidKeyException)33 IOException (java.io.IOException)31 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 CertificateException (java.security.cert.CertificateException)19 SignatureException (java.security.SignatureException)15 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 KeyStoreException (java.security.KeyStoreException)12 X509Certificate (java.security.cert.X509Certificate)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 KeyFactory (java.security.KeyFactory)8 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8