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.taverna.server.master.localworker.SecurityContextDelegateImpl.java

@Override
public void addCredentialToKeystore(Credential c) throws KeyStoreException {
    try {//w  ww.  j a v a 2s. com
        if (c instanceof Credential.Password)
            addUserPassToKeystore((Credential.Password) c);
        else if (c instanceof Credential.KeyPair)
            addKeypairToKeystore((Credential.KeyPair) c);
        else
            throw new KeyStoreException("unknown credential type");
    } catch (KeyStoreException e) {
        throw e;
    } catch (Exception e) {
        throw new KeyStoreException(e);
    }
}

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

private TrustManagerFactory getTrustManagerFactory(final KeyStore keystore)
        throws KeyStoreException, IOException {
    try {//w  ww .ja v  a 2 s  . c  o  m
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keystore);
        return trustManagerFactory;
    } 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
        log.error("The algorithm is not supported: " + e, e);
        throw new KeyStoreException(e);
    }
}

From source file:mitm.common.security.keystore.KeyStoreLoader.java

private void determineKeyStoreTypeFromFile(File file) throws KeyStoreException {
    String extension = StringUtils
            .defaultString(StringUtils.lowerCase(FilenameUtils.getExtension(file.getName())));

    keyStoreType = extensionMap.get(extension);

    if (keyStoreType == null) {
        throw new KeyStoreException("Unable to determine key store type for extension " + extension);
    }/*w w w .  j ava  2 s  . c om*/
}

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

/**
 * Method which will load a secret key from disk with the specified entry
 * name./*from   www  . j  a  v a2s. c  om*/
 * 
 * @param keystore {@link KeyStore} file to read.
 * @param entryName Entry name of the key to be retrieved
 * @param keyStorePassword Password used to open the {@link KeyStore}
 * @return
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws UnrecoverableEntryException
 */
public static SecretKey getAESSecretKey(File keystore, String entryName, String keyStorePassword)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException,
        IOException, UnrecoverableEntryException {
    KeyStore keyStore = KeyStore.getInstance("JCEKS");
    FileInputStream fis = null;
    if (keystore == null || !keystore.exists() || FileUtils.sizeOf(keystore) == 0) {
        throw new FileNotFoundException();
    }
    if (StringUtils.isEmpty(keyStorePassword)) {
        throw new KeyStoreException("No Keystore password provided.");
    }
    if (StringUtils.isEmpty(entryName)) {
        throw new KeyStoreException("No Keystore entry name provided.");
    }

    fis = new FileInputStream(keystore);

    return getAESSecretKey(fis, entryName, keyStorePassword);

}

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

private TrustManagerFactory getTrustManagerFactory(final KeyStore keystore)
        throws KeyStoreException, IOException {
    try {/*from ww w .j av  a2s .c om*/
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keystore);
        return trustManagerFactory;
    } 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
        log.error("The algorithm is not supported. Error message:" + e.getMessage());
        throw new KeyStoreException(e);
    }
}

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

/**
* Method which will load a secret key from an input stream with the specified entry
* name.//from  ww  w . j a  v a  2  s  .  co  m
* 
* @param keystore {@link KeyStore} file to read.
* @param entryName Entry name of the key to be retrieved
* @param keyStorePassword Password used to open the {@link KeyStore}
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws UnrecoverableEntryException
*/
public static SecretKey getAESSecretKey(InputStream keyInputStream, String entryName, String keyStorePassword)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableEntryException {
    KeyStore keyStore = KeyStore.getInstance("JCEKS");

    if (keyInputStream == null) {
        throw new KeyStoreException("No Keystore stream provided.");
    }
    if (StringUtils.isEmpty(keyStorePassword)) {
        throw new KeyStoreException("No Keystore password provided.");
    }
    if (StringUtils.isEmpty(entryName)) {
        throw new KeyStoreException("No Keystore entry name provided.");
    }

    keyStore.load(keyInputStream, keyStorePassword.toCharArray());
    KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection(
            keyStorePassword.toCharArray());
    KeyStore.SecretKeyEntry pkEntry = (KeyStore.SecretKeyEntry) keyStore.getEntry(entryName,
            protectionParameter);
    try {
        return pkEntry.getSecretKey();
    } finally {
        keyInputStream.close();
    }

}

From source file:eidassaml.starterkit.Utils.java

/**
 * /*ww  w.  j a v a 2  s. co  m*/
 * @param stream
 * @param password
 * @param alias
 * @return
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws IOException
 * @throws UnrecoverableKeyException
 * @throws NoSuchProviderException 
 */
public static X509KeyPair ReadPKCS12(InputStream stream, char[] password, String alias)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException, NoSuchProviderException {
    KeyStore p12 = KeyStore.getInstance("pkcs12", "BC");
    p12.load(stream, password);
    Enumeration<String> e = p12.aliases();
    PrivateKey key = null;
    X509Certificate cert = null;
    StringBuffer aliasBuf = new StringBuffer();
    while (e.hasMoreElements()) {
        String currentalias = (String) e.nextElement();
        aliasBuf.append(currentalias);
        aliasBuf.append(" ||| ");
        cert = (X509Certificate) p12.getCertificate(currentalias);
        key = (PrivateKey) p12.getKey(currentalias, password);
        if (Utils.IsNullOrEmpty(alias) && key != null) {
            //take the first one
            break;
        } else if (currentalias.equals(alias) && key != null) {
            break;
        }
    }
    if (key != null) {
        return new X509KeyPair(key, cert);
    } else {
        StringBuffer errbuf = new StringBuffer();
        errbuf.append("keystore does not contains alias " + alias + ". Try alias " + aliasBuf.toString());
        throw new KeyStoreException(errbuf.toString());
    }

}

From source file:com.vmware.identity.idm.server.ClientCertTestUtils.java

/**
 * @return selfsigned valid cert/*from  www .  j a  v  a2s  . co m*/
 * @throws KeyStoreException
 */
public X509Certificate[] getValidCert() throws KeyStoreException {
    KeyStore ks = loadKeyStore(clientStoreName, storePass);
    if (!ks.isCertificateEntry(validCertAlias)) {
        throw new KeyStoreException("Cert not in the store");
    }
    X509Certificate leaf = (X509Certificate) ks.getCertificate(validCertAlias);
    X509Certificate[] certs = { leaf };
    return certs;
}

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

/**
 * Utility which will take an existing Properties file on disk and replace
 * any -unencrypted values with encrypted.<br>
 * //w w w.ja v a 2 s  .  co  m
 * @param clearProperties
 *            Un-encrypted properties file to be secured
 * @param keyPath
 *            Path to the keystore file.
 * @param keyPass
 *            Password to be used to open and secure the Keystore password.
 * @param keyEntry
 *            Entry name of the key to use from the keystore.
 * @param retainCrytoConfigProperties
 *            Boolean to indicate if the encryption field parameters should
 *            be stored in the resulting SecureProperties file. True they
 *            will be, False they wont.
 * @return
 * @throws FileNotFoundException
 *             Properties file not found on disk.
 * @throws IOException
 *             Error reading/writing From the clear properties or to the
 *             secure properties
 * @throws KeyStoreException
 *             Error accessing or using the keystore.
 */
public static SecureProperties encryptPropertiesFile(File clearProperties, String keyPath, String keyPass,
        String keyEntry, boolean retainCrytoConfigProperties)
        throws FileNotFoundException, IOException, KeyStoreException {

    // Save filename/Path
    String propertiesFilePath = clearProperties.getPath();

    // Create new SecureProperties
    SecureProperties sProperties = new SecureProperties();

    // Open clear properties file and load it
    Properties cProperties = new Properties();
    FileInputStream fis = new FileInputStream(clearProperties);
    cProperties.load(fis);
    fis.close();

    // Ensure the encryption parameters are not empty.
    if (StringUtils.isEmpty(ENTRY_NAME_PROPERTY_KEY) || StringUtils.isEmpty(KEY_PATH_PROPERTY_KEY)
            || StringUtils.isEmpty(KEYSTORE_PASSWORD_PROPERTY_KEY)) {
        throw new KeyStoreException("Unable to configure due to missing configurations");
    }

    // Loop over clear properties and construct new SecureProperties object
    // First add crypto entries this will initialize the encryption support.
    sProperties.setProperty(ENTRY_NAME_PROPERTY_KEY, keyEntry);
    sProperties.setProperty(KEYSTORE_PASSWORD_PROPERTY_KEY, keyPass);
    sProperties.setProperty(KEY_PATH_PROPERTY_KEY, keyPath);

    for (Object key : cProperties.keySet()) {

        String keyStr = (String) key;
        if (!StringUtils.equals(keyStr, ENTRY_NAME_PROPERTY_KEY)
                && !StringUtils.equals(keyStr, KEYSTORE_PASSWORD_PROPERTY_KEY)
                && !StringUtils.equals(keyStr, KEY_PATH_PROPERTY_KEY)) {
            sProperties.setProperty(keyStr, cProperties.getProperty(keyStr));
        }

    }

    if (!retainCrytoConfigProperties) {
        // Remove the crypto entries from the secure file. Since its passed
        // in...
        sProperties.remove(ENTRY_NAME_PROPERTY_KEY);
        sProperties.remove(KEYSTORE_PASSWORD_PROPERTY_KEY);
        sProperties.remove(KEY_PATH_PROPERTY_KEY);
    }

    // Delete original file from disk
    clearProperties.delete();

    // Write SecureProperties out in its place
    OutputStream fos = new FileOutputStream(new File(propertiesFilePath));
    sProperties.store(fos, "File Encrypted by SecurePropertiesUtils");
    fos.flush();
    fos.close();

    // Return completed SecureProperties object
    return sProperties;

}

From source file:mitm.application.djigzo.workflow.impl.KeyAndCertificateWorkflowImpl.java

@Override
public int importKeyStore(final KeyStore keyStore, final MissingKey missingKey) throws KeyStoreException {
    Check.notNull(keyStore, "keyStore");
    Check.notNull(missingKey, "missingKey");

    int importedEntries = 0;

    try {//w ww  .j a v  a 2 s .c  o m
        importedEntries = getActionExecutor().executeTransaction(new DatabaseAction<Integer>() {
            @Override
            public Integer doAction(Session session) throws DatabaseException {
                try {
                    Session previousSession = getSessionManager().getSession();

                    getSessionManager().setSession(session);

                    try {

                        return importKeyStoreTransacted(keyStore, missingKey);
                    } finally {
                        /* restore the session */
                        getSessionManager().setSession(previousSession);
                    }
                } catch (KeyStoreException e) {
                    throw new DatabaseException(e);
                }
            }
        });
    } catch (DatabaseException e) {
        Throwable cause = e.getCause();

        if (cause == null) {
            cause = e;
        }

        if (cause instanceof KeyStoreException) {
            throw (KeyStoreException) cause;
        }

        throw new KeyStoreException(cause);
    } catch (ConstraintViolationException e) {
        logger.warn(
                "ConstraintViolationException. A certificate was probably already in the certStore. Message: "
                        + e.getMessage());
    }

    return importedEntries;
}