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.seedstack.seed.crypto.internal.EncryptionServiceFactoryTest.java

/**
 * Test method for/*from w ww  .  ja v  a2  s. c om*/
 * {@link org.seedstack.seed.crypto.internal.EncryptionServiceFactory#createEncryptionService(org.seedstack.seed.crypto.internal.KeyStoreDefinition, org.seedstack.seed.crypto.internal.CertificateDefinition)}
 * . Test a {@link KeyStoreException} if no Provider supports a KeyStoreSpi implementation for the specified type.
 *
 * @throws Exception if an error occurred
 */
@Test(expected = RuntimeException.class)
public void testCreateEncryptionServiceWithKeystoreException(
        @Mocked final KeyStoreDefinition keyStoreDefinition,
        @Mocked final CertificateDefinition certificateDefinition,
        @SuppressWarnings("unused") @Mocked final KeyStore keyStore) throws Exception {
    new Expectations() {
        final String pathToKeystore = "pathToKeystore";

        {
            keyStoreDefinition.getPath();
            returns(pathToKeystore);

            KeyStore.getInstance(KeyStore.getDefaultType());
            result = new KeyStoreException("dummy exception");
        }
    };

    EncryptionServiceFactory factory = new EncryptionServiceFactory();
    factory.createEncryptionService(keyStoreDefinition, certificateDefinition);

}

From source file:com.alliander.osgp.adapter.ws.smartmetering.infra.ws.WebServiceTemplateFactory.java

/**
 * @throws WebServiceSecurityException//w  w  w.j a  v  a2  s.  c o  m
 *             if an error occurs while attempting to create a secured
 *             connection.
 */
private HttpComponentsMessageSender webServiceMessageSender(final String keystore)
        throws WebServiceSecurityException {

    try {
        // Open keystore, assuming same identity
        final KeyStoreFactoryBean keyStoreFactory = new KeyStoreFactoryBean();
        keyStoreFactory.setType(this.keyStoreType);
        keyStoreFactory.setLocation(new FileSystemResource(this.keyStoreLocation + "/" + keystore + ".pfx"));
        keyStoreFactory.setPassword(this.keyStorePassword);
        keyStoreFactory.afterPropertiesSet();

        final KeyStore keyStore = keyStoreFactory.getObject();
        if (keyStore == null || keyStore.size() == 0) {
            throw new KeyStoreException("Key store is empty");
        }

        // Create HTTP sender and associate keystore to it
        final HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
        final HttpClient client = sender.getHttpClient();
        final SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, this.keyStorePassword,
                this.trustStoreFactory.getObject());

        final Scheme scheme = new Scheme("https", 443, socketFactory);
        client.getConnectionManager().getSchemeRegistry().register(scheme);

        return sender;

    } catch (IOException | GeneralSecurityException e) {
        throw new WebServiceSecurityException("An exception occured while creating a secured connection.", e);
    }

}

From source file:mitm.common.security.keystore.hibernate.SerializableKeyEntry.java

private Format toFormat(String format) throws KeyStoreException {
    if (format.equals("PKCS#8") || format.equals("PKCS8")) {
        return Format.PKCS8;
    }//  w  w  w.  j av  a 2 s .  c  o  m

    if (format.equals("X.509") || format.equals("X509")) {
        return Format.X509;
    }

    if (format.equals("RAW")) {
        return Format.RAW;
    }

    throw new KeyStoreException("Unknown key format " + format);
}

From source file:mitm.test.TestUtils.java

public static KeyStore loadKeyStore(File file, String password) throws KeyStoreException {
    try {//  w ww . ja v a 2s. co  m
        KeyStore keyStore = SecurityFactoryFactory.getSecurityFactory().createKeyStore("PKCS12");

        // initialize key store
        keyStore.load(new FileInputStream(file), password.toCharArray());

        return keyStore;
    } catch (NoSuchProviderException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e);
    } catch (FileNotFoundException e) {
        throw new KeyStoreException(e);
    } catch (IOException e) {
        throw new KeyStoreException(e);
    }
}

From source file:mitm.djigzo.web.pages.admin.SSLCertificateManager.java

private KeyStore getKeyStore() throws KeyStoreException {
    SecurityFactory securityFactory = SecurityFactoryFactory.getSecurityFactory();

    try {//from  w w  w.ja va  2  s . com
        KeyStore keyStore = securityFactory.createKeyStore("PKCS12");

        keyStore.load(file.getStream(), password.toCharArray());

        return keyStore;
    } catch (NoSuchProviderException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e);
    } catch (IOException e) {
        throw new KeyStoreException(e);
    }
}

From source file:SignatureInfos.java

/**
* @return/*w  w w  .ja  v a 2  s.com*/
* @throws KeyStoreException
*/
public PrivateKey getPrivateKey() throws KeyStoreException {
    try {

        return (PrivateKey) getKeystore().getKey(getKeyAlias(), getPrivateKeyPassword().toCharArray());

    } catch (final NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } catch (final UnrecoverableKeyException e) {
        throw new KeyStoreException(e);
    }
}

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

public X509Certificate[] getDodValidCert1() throws KeyStoreException {
    KeyStore ks = loadKeyStore(clientStoreName, storePass);
    if (!ks.isCertificateEntry(validDodCertAlias1)) {
        throw new KeyStoreException("Cert not in the store");
    }//from   w  w w.  j a va2 s  .  com
    X509Certificate leaf = (X509Certificate) ks.getCertificate(validDodCertAlias1);
    X509Certificate[] certs = { leaf };
    return certs;

}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

private static KeyStore loadKeyStore(File file, String password) throws KeyStoreException {
    try {/* w ww.  ja v  a  2s .  c  o m*/
        KeyStore keyStore = securityFactory.createKeyStore("PKCS12");

        // initialize key store
        keyStore.load(new FileInputStream(file), password.toCharArray());

        return keyStore;
    } catch (NoSuchProviderException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } catch (CertificateException e) {
        throw new KeyStoreException(e);
    } catch (FileNotFoundException e) {
        throw new KeyStoreException(e);
    } catch (IOException e) {
        throw new KeyStoreException(e);
    }
}

From source file:mitm.common.security.keystore.jce.KeyStoreHibernate.java

@Override
public void engineDeleteEntry(final String alias) throws KeyStoreException {
    try {/*  ww w. j  a  v a  2s.  c o  m*/
        databaseExecutor.executeTransaction(new DatabaseVoidAction() {
            @Override
            public void doAction(Session session) {
                deleteEntryByAliasAction(alias, session);
            }
        }, ACTION_RETRIES /* retry on a ConstraintViolationException */);
    } catch (DatabaseException e) {
        logger.error("Database exception.", e);

        throw new KeyStoreException(e);
    }
}

From source file:mitm.common.security.keystore.hibernate.SerializableKeyEntry.java

/**
 * Creates the key from the given byte array using the stored format and type.
 * //from  w ww  .  j  av  a  2 s  . c  o  m
 * @param keyBytes
 * @return the key
 * @throws KeyStoreException
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
private Key getKey(byte[] rawKey)
        throws KeyStoreException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
    KeySpec keySpec;

    Format keyFormat = toFormat(format);

    switch (keyFormat) {
    case PKCS8:
        keySpec = new PKCS8EncodedKeySpec(rawKey);
        break;
    case X509:
        keySpec = new X509EncodedKeySpec(rawKey);
        break;
    case RAW:
        return new SecretKeySpec(rawKey, algorithm);

    default:
        throw new KeyStoreException("Unknown key format " + keyFormat);
    }

    SecurityFactory securityFactory = getSecurityFactory();

    switch (keyType) {
    case PRIVATE:
        return securityFactory.createKeyFactory(algorithm).generatePrivate(keySpec);
    case PUBLIC:
        return securityFactory.createKeyFactory(algorithm).generatePublic(keySpec);
    case SECRET:
        return securityFactory.createSecretKeyFactory(algorithm).generateSecret(keySpec);

    default:
        throw new KeyStoreException("Unknown key type " + keyType);
    }
}