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(String message, Throwable cause) 

Source Link

Document

Creates a KeyStoreException with the specified detail message and cause.

Usage

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

private KeyManagerFactory getKeyManagerFactory(final KeyStore keystore, final String password)
        throws KeyStoreException {
    try {//from w w  w  .j  a  v  a  2  s.  co 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: " + e, e);
    } catch (UnrecoverableKeyException e) {
        // invalid password, should never happen
        throw new KeyStoreException("Password for the keystore is invalid: " + e, e);
    }
}

From source file:org.pepstock.jem.node.security.keystore.KeyStoreUtil.java

/**
 * Returns a SSL socket factory creating asymmetric keys at runtime.
 * //ww  w  . ja v a 2  s. c  om
 * @return a SSL socket factory for HTTPS listener 
 * @throws KeyStoreException if any errors occurs to get keys
 */
public static SSLServerSocketFactory getSSLServerSocketFactory() throws KeyStoreException {
    try {
        // gets a key stores created at runtime
        ByteArrayInputStream baos = SelfSignedCertificate.getCertificate();
        KeyStore keystore = KeyStore.getInstance("jks");
        // loads the keystore
        keystore.load(baos, SelfSignedCertificate.CERTIFICATE_PASSWORD.toCharArray());
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

        // initialiazes the key manager
        kmfactory.init(keystore, SelfSignedCertificate.CERTIFICATE_PASSWORD.toCharArray());
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        // creates SSL socket factory
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(keymanagers, null, null);
        return sslcontext.getServerSocketFactory();
    } catch (UnrecoverableKeyException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (KeyManagementException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (SecurityException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (IOException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (OperatorCreationException e) {
        throw new KeyStoreException(e.getMessage(), e);
    }
}

From source file:org.pepstock.jem.node.security.keystore.KeyStoreUtil.java

/**
 * Gets a key store using a entity with teh information where it has to read and 
 * load into the keystore//from w w w .  j  a va 2 s  . com
 * 
 * @param keystoreInfo entity with information about keystore
 * @return a new keystore
 * @throws KeyStoreException if any error occurs during the keystore creation
 */
static KeyStore getKeystore(KeyStoreInfo keystoreInfo) throws KeyStoreException {
    // gets keystore
    KeyStore keystore = KeyStore.getInstance(keystoreInfo.getType());
    InputStream is = null;
    try {
        // if the entity must read the keystore from memory
        // used the bytes of the entity and
        if (keystoreInfo.getBytes() != null) {
            is = new ByteArrayInputStream(keystoreInfo.getBytes().toByteArray());
        } else {
            // otherwise it reads the keystore from the file system 
            is = new FileInputStream(keystoreInfo.getFile());
        }
        // loads the key store
        keystore.load(is, keystoreInfo.getPassword().toCharArray());
    } catch (FileNotFoundException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (IOException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } finally {
        // if inputstream is not null
        // it closes
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
                // ignore
                LogAppl.getInstance().ignore(e.getMessage(), e);
            }
        }
    }
    return keystore;
}

From source file:eu.europa.ec.markt.dss.signature.token.Pkcs12SignatureToken.java

@Override
public List<DSSPrivateKeyEntry> getKeys() throws KeyStoreException {

    List<DSSPrivateKeyEntry> list = new ArrayList<DSSPrivateKeyEntry>();

    InputStream input = null;//from   w  ww  .j av  a 2s  .c om
    try {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        if (pkcs12Data != null) {
            input = new ByteArrayInputStream(pkcs12Data);
        } else {
            input = new FileInputStream(pkcs12File);
        }

        keyStore.load(input, password);
        PasswordProtection pp = new KeyStore.PasswordProtection(password);
        Enumeration<String> aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {

            String alias = aliases.nextElement();
            if (keyStore.isKeyEntry(alias)) {

                PrivateKeyEntry entry = (PrivateKeyEntry) keyStore.getEntry(alias, pp);
                list.add(new KSPrivateKeyEntry(entry));
            }
        }
    } catch (Exception e) {
        if (e.getCause() instanceof BadPaddingException) {
            throw new BadPasswordException(MSG.PKCS12_BAD_PASSWORD);
        }
        throw new KeyStoreException(
                "Can't initialize Sun PKCS#12 security provider. Reason: " + getCauseMessage(e), e);
    } finally {
        DSSUtils.closeQuietly(input);
    }
    return list;
}

From source file:org.pepstock.jem.node.security.keystore.KeyStoreUtil.java

/**
 * Generate an empty key store where will be store the X509 certificate of
 * the user//from w w  w  .  ja  v  a  2  s  .co m
 * <p>
 * This key store will be used when the client will used a private key to
 * connect to the cluster and the cluster will used the relative public key
 * present in the x509 certificate to verify the identity of the client.
 * @param keystoreInfo entity with information about keystore
 * @throws KeyStoreException if any exception occurs during key store creation
 * 
 */
public static void generate(KeyStoreInfo keystoreInfo) throws KeyStoreException {
    try {
        // if the keystore exist load it else create a new one
        KeyStore keystore = null;
        if (keystoreInfo.getFile().exists()) {
            keystore = getKeystore(keystoreInfo);
        } else {
            keystore = KeyStore.getInstance(keystoreInfo.getType());
            keystore.load(null, null);
            save(keystore, keystoreInfo);
        }
        // if the keystore does not contain the given alias, create a new key
        // with that alias otherwise does nothing
        if (keystoreInfo.getSymmetricKeyAlias() != null && keystoreInfo.getSymmetricKeyPwd() != null
                && keystore.getKey(keystoreInfo.getSymmetricKeyAlias(),
                        keystoreInfo.getSymmetricKeyPwd().toCharArray()) == null) {
            // creates simmetricKey
            Key secretKey = Crypto.generateSymmetricKey();
            // adds the key
            keystore.setKeyEntry(keystoreInfo.getSymmetricKeyAlias(), secretKey,
                    keystoreInfo.getSymmetricKeyPwd().toCharArray(), null);
            // saves the keystore
            save(keystore, keystoreInfo);
        }
    } catch (UnrecoverableKeyException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (IOException e) {
        throw new KeyStoreException(e.getMessage(), e);
    }
}

From source file:org.pepstock.jem.node.security.keystore.KeyStoreUtil.java

/**
 * Saves the kwystore on the file//w  w w. j  a  va2 s  .  c  om
 * @param keystore keystore to be saved
 * @param info Key store info with all necessary info to save it 
 * @throws KeyStoreException if any error occurs saving the key store
 */
static void save(KeyStore keystore, KeyStoreInfo info) throws KeyStoreException {
    OutputStream os = null;
    try {
        // creates the file stream
        os = new FileOutputStream(info.getFile());
        // stores the file 
        keystore.store(os, info.getPassword().toCharArray());
        // checks if it must be backuped
        if (info.getBackupFile() != null) {
            // read keystore to check if is consistent
            getKeystore(info);
            FileUtils.copyFile(info.getFile(), info.getBackupFile());
        }
    } catch (FileNotFoundException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (IOException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e.getMessage(), e);
    } finally {
        // always it closes the outut stream
        if (os != null) {
            try {
                os.close();
            } catch (Exception e) {
                // ignore
                LogAppl.getInstance().ignore(e.getMessage(), e);
            }
        }
    }
}

From source file:gov.nih.nci.cacis.nav.SendEncryptedMail.java

private KeyStore getTrustStoreRef() throws KeyStoreException {
    /* Open the truststore */
    KeyStore truststoreRef = null;
    InputStream is = null;/*from  w  ww. java 2  s .  c  o m*/
    try {
        truststoreRef = KeyStore.getInstance(STORE_TYPE, PROVIDER_TYPE);
        is = new FileInputStream(truststore);
        truststoreRef.load(is, storepass.toCharArray());
        // CHECKSTYLE:OFF
    } catch (Exception e) { // NOPMD
        // CHECKSTYLE:ON
        throw new KeyStoreException("Error loading truststore!", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                LOG.debug("Error closing truststore reading stream!");
            }
        }
    }
    return truststoreRef;
}

From source file:org.opensc.pkcs11.spi.PKCS11KeyStoreSpi.java

@Override
public void engineSetCertificateEntry(String name, Certificate certificate) throws KeyStoreException {
    try {/*  w w w . j  ava2 s . c  o m*/
        PKCS11Certificate cert = PKCS11Certificate.storeCertificate(this.sessionStore.getSession(), certificate,
                name, true);

        PKCS11KSEntry entry = new PKCS11KSEntry(cert);

        String keyName = "ID_" + cert.getId();

        PKCS11KSEntry pk_entry = this.entries.get(keyName);

        if (pk_entry != null) {
            entry.privateKey = pk_entry.privateKey;
            this.entries.remove(keyName);
        }

        if (name == null)
            this.entries.put(cert.getSubject().toString(), entry);
        else
            this.entries.put(name, entry);

    } catch (CertificateEncodingException e) {
        throw new KeyStoreException("Error encoding certificate", e);
    } catch (PKCS11Exception e) {
        throw new KeyStoreException("Error storing certificate on the token", e);
    }
}

From source file:org.globus.security.stores.PEMKeyStore.java

private void storeWrapper(CredentialWrapper wrapper) throws KeyStoreException {
    try {//w  w w.ja  v  a2  s . co m
        wrapper.store();
    } catch (ResourceStoreException e) {
        throw new KeyStoreException("Error storing credential", e);
    }
}

From source file:org.globus.gsi.stores.PEMKeyStore.java

private void storeWrapper(CredentialWrapper wrapper) throws KeyStoreException {
    if (!inMemoryOnly) {
        try {/*from w w  w .j  a v  a  2  s  .  c o m*/
            wrapper.store();
        } catch (ResourceStoreException e) {
            throw new KeyStoreException("Error storing credential", e);
        }
    }
}