Example usage for java.security KeyStoreException KeyStoreException

List of usage examples for java.security KeyStoreException KeyStoreException

Introduction

In this page you can find the example usage for java.security KeyStoreException KeyStoreException.

Prototype

public KeyStoreException(Throwable cause) 

Source Link

Document

Creates a KeyStoreException with the specified cause and a detail message of (cause==null ?

Usage

From source file:org.talend.daikon.security.SSLContextProvider.java

private static KeyManager[] buildKeyManagers(String path, String storePass, String keytype)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException {
    InputStream stream = null;/*from w  w w .  j a  va2 s. c  om*/
    try {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!new File(path).exists()) {
            throw new KeyStoreException("Key store not exist");
        }
        stream = new FileInputStream(path);

        KeyStore tks = KeyStore.getInstance(keytype);
        tks.load(stream, storePass.toCharArray());

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
        kmf.init(tks, storePass.toCharArray());

        return kmf.getKeyManagers();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:be.dnsbelgium.rdap.client.RDAPClient.java

public static KeyStore getKeyStoreFromFile(File file, String type, String password) throws KeyStoreException {
    KeyStore result = KeyStore.getInstance(type);
    FileInputStream fis = null;//from w  ww  .  j av a2  s  .  c  o  m
    try {
        fis = new FileInputStream(file);
        result.load(fis, password.toCharArray());
    } catch (IOException e) {
        LOGGER.error("Could not load keystore file", e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                LOGGER.debug("Could not close keystore file", e);
            }
        }
    }
    return result;
}

From source file:org.talend.daikon.security.SSLContextProvider.java

private static TrustManager[] buildTrustManagers(String path, String storePass, String trusttype)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException {
    InputStream stream = null;/*from w w w .  ja  v  a  2  s.  co  m*/
    try {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (StringUtils.isEmpty(path) || !new File(path).exists()) {
            throw new KeyStoreException("Trust store not exist");
        }
        stream = new FileInputStream(path);

        KeyStore tks = KeyStore.getInstance(trusttype);
        tks.load(stream, storePass.toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
        tmf.init(tks);

        return tmf.getTrustManagers();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:net.theblackchamber.crypto.util.KeystoreUtils.java

/**
 * Method which will generate a random AES key and add it to a keystore with
 * the entry name provided./* w  ww  .j  a  v a  2  s  .  c o m*/
 * 
 * @param config
 *            Configuration for generation of key.
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 * @throws CertificateException
 * @throws IOException
 */
public static void generateAESSecretKey(KeyConfig config)
        throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {

    if (config == null || config.getKeyStoreFile() == null || StringUtils.isEmpty(config.getKeyEntryName())
            || config.getAlgorithm() == null) {
        throw new KeyStoreException("Missing parameters, unable to create keystore.");
    }

    SecureRandom random = new SecureRandom();

    KeyGenerator keygen = KeyGenerator.getInstance(config.getAlgorithm().toString(),
            new BouncyCastleProvider());
    keygen.init(config.getKeySize(), random);

    SecretKey key = keygen.generateKey();

    KeyStore keyStore = KeyStore.getInstance("JCEKS");
    FileInputStream fis = null;
    if (config.getKeyStoreFile().exists() && FileUtils.sizeOf(config.getKeyStoreFile()) > 0) {
        fis = new FileInputStream(config.getKeyStoreFile());
    }

    keyStore.load(fis, config.getKeyStorePassword().toCharArray());

    KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection(
            config.getKeyStorePassword().toCharArray());
    KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(key);

    keyStore.setEntry(config.getKeyEntryName(), secretKeyEntry, protectionParameter);
    if (fis != null) {
        fis.close();
    }
    FileOutputStream fos = new FileOutputStream(config.getKeyStoreFile());

    keyStore.store(fos, config.getKeyStorePassword().toCharArray());

    fos.close();

}

From source file:com.amalto.workbench.utils.SSLContextProvider.java

private static KeyManager[] buildKeyManagers(String path, String storePass, String keytype)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException {
    InputStream stream = null;/*from w w  w .  j a  v a 2s  .  c o m*/
    try {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!new File(path).exists()) {
            throw new KeyStoreException(Messages.bind(Messages.noKeystoreFile_error, path));
        }
        stream = new FileInputStream(path);

        KeyStore tks = KeyStore.getInstance(keytype);
        tks.load(stream, storePass.toCharArray());

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
        kmf.init(tks, storePass.toCharArray());

        return kmf.getKeyManagers();
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:it.jnrpe.server.CBindingThread.java

/**
 * Returns the SSL factory to be used to create the Server Socket
 * @throws KeyStoreException /*from www . ja  v a  2 s . c o  m*/
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws CertificateException 
 * @throws UnrecoverableKeyException 
 * @throws KeyManagementException 
 * 
 * @see it.intesa.fi2.client.network.ISSLObjectsFactory#getSSLSocketFactory(String, String, String)
 */
public SSLServerSocketFactory getSSLSocketFactory(String sKeyStoreFile, String sKeyStorePwd,
        String sKeyStoreType) throws KeyStoreException, CertificateException, FileNotFoundException,
        IOException, UnrecoverableKeyException, KeyManagementException {
    if (sKeyStoreFile == null)
        throw new KeyStoreException("KEYSTORE HAS NOT BEEN SPECIFIED");
    if (this.getClass().getClassLoader().getResourceAsStream(sKeyStoreFile) == null)
        throw new KeyStoreException("COULD NOT FIND KEYSTORE '" + sKeyStoreFile + "'");

    if (sKeyStorePwd == null)
        throw new KeyStoreException("KEYSTORE PASSWORD HAS NOT BEEN SPECIFIED");

    SSLContext ctx;
    KeyManagerFactory kmf;

    try {
        ctx = SSLContext.getInstance("SSLv3");

        kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

        //KeyStore ks = getKeystore(sKeyStoreFile, sKeyStorePwd, sKeyStoreType);
        KeyStore ks = KeyStore.getInstance(sKeyStoreType);
        ks.load(this.getClass().getClassLoader().getResourceAsStream(sKeyStoreFile),
                sKeyStorePwd.toCharArray());

        char[] passphrase = sKeyStorePwd.toCharArray();
        kmf.init(ks, passphrase);
        ctx.init(kmf.getKeyManagers(), null, new java.security.SecureRandom());

    } catch (NoSuchAlgorithmException e) {
        throw new SSLException("Unable to initialize SSLSocketFactory.\n" + e.getMessage());
    }

    return ctx.getServerSocketFactory();
}

From source file:com.amalto.workbench.utils.SSLContextProvider.java

private static TrustManager[] buildTrustManagers(String path, String storePass, String trusttype)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException {
    InputStream stream = null;//from w ww .  ja v  a2s .  co  m
    try {
        if (StringUtils.isEmpty(path)) {
            return new TrustManager[] { TRUST_ALL };
        }
        if (!new File(path).exists()) {
            throw new KeyStoreException(Messages.bind(Messages.noKeystoreFile_error, path));
        }
        stream = new FileInputStream(path);

        KeyStore tks = KeyStore.getInstance(trusttype);
        tks.load(stream, storePass.toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
        tmf.init(tks);

        return tmf.getTrustManagers();
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:com.loopj.android.http.sample.CustomCASample.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {/*from  w w  w .  ja v  a  2 s .  com*/
        InputStream is = null;
        try {
            // Configure the library to use a custom 'bks' file to perform
            // SSL negotiation.
            KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
            is = getResources().openRawResource(R.raw.store);
            store.load(is, STORE_PASS.toCharArray());
            getAsyncHttpClient().setSSLSocketFactory(new SecureSocketFactory(store, STORE_ALIAS));
        } catch (IOException e) {
            throw new KeyStoreException(e);
        } catch (CertificateException e) {
            throw new KeyStoreException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new KeyStoreException(e);
        } catch (KeyManagementException e) {
            throw new KeyStoreException(e);
        } catch (UnrecoverableKeyException e) {
            throw new KeyStoreException(e);
        } finally {
            AsyncHttpClient.silentCloseInputStream(is);
        }
    } catch (KeyStoreException e) {
        Log.e(LOG_TAG, "Unable to initialize key store", e);
        showCustomCAHelp();
    }
}

From source file:org.hyperic.util.security.DatabaseSSLProviderImpl.java

private KeyManagerFactory getKeyManagerFactory(final KeyStore keystore, final String password)
        throws KeyStoreException {
    try {/*from ww  w .j  a  v  a 2  s .c  o  m*/
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, password.toCharArray());
        return keyManagerFactory;
    } catch (NoSuchAlgorithmException e) {
        // no support for algorithm, if this happens we're kind of screwed
        // we're using the default so it should never happen
        throw new KeyStoreException("The algorithm is not supported. Error message:" + e.getMessage());
    } catch (UnrecoverableKeyException e) {
        // invalid password, should never happen
        throw new KeyStoreException("Password for the keystore is invalid. Error message:" + e.getMessage());
    }
}

From source file:org.hyperic.util.security.KeystoreManager.java

public KeyStore getKeyStore(KeystoreConfig keystoreConfig) throws KeyStoreException, IOException {
    FileInputStream keyStoreFileInputStream = null;

    String filePath = keystoreConfig.getFilePath();
    String filePassword = keystoreConfig.getFilePassword();

    //check if keystoreConfig valid (block if it's null or "")
    String errorMsg = "";
    if (keystoreConfig.getAlias() == null) {
        errorMsg += " alias is null. ";
    }//from   www  . j  av  a 2 s.c om
    if (keystoreConfig.getFilePath() == null) {
        errorMsg += " filePath is null. ";
    }
    if (keystoreConfig.getFilePassword() == null) {
        errorMsg += " password is null. ";
    }
    if (!"".equals(errorMsg)) {
        throw new KeyStoreException(errorMsg);
    }

    try {
        KeyStore keystore = DbKeyStore.getInstance(KeyStore.getDefaultType(), isDB);
        File file = new File(filePath);
        char[] password = null;

        if (!file.exists()) {
            // ...if file doesn't exist, and path was user specified throw IOException...
            if (StringUtils.hasText(filePath) && !keystoreConfig.isHqDefault()) {
                throw new IOException("User specified keystore [" + filePath + "] does not exist.");
            }

            password = filePassword.toCharArray();
            createInternalKeystore(keystoreConfig);
            FileUtil.setReadWriteOnlyByOwner(file);
        }

        // ...keystore exist, so init the file input stream...
        keyStoreFileInputStream = new FileInputStream(file);

        keystore.load(keyStoreFileInputStream, password);

        return keystore;
    } catch (NoSuchAlgorithmException e) {
        // can't check integrity of keystore, if this happens we're kind of screwed
        // is there anything we can do to self heal this problem?
        errorMsg = "The algorithm used to check the integrity of the keystore cannot be found.";
        throw new KeyStoreException(errorMsg, e);
    } catch (CertificateException e) {
        // there are some corrupted certificates in the keystore, a bad thing
        // is there anything we can do to self heal this problem?
        errorMsg = "Keystore cannot be loaded. One possibility is that the password is incorrect.";
        throw new KeyStoreException(errorMsg, e);
    } finally {
        if (keyStoreFileInputStream != null) {
            keyStoreFileInputStream.close();
            keyStoreFileInputStream = null;
        }
    }
}