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:mitm.common.security.KeyAndCertStoreImpl.java

/**
 * @See KeyAndCertStore.getMatchingKeys/*from  w w w.  ja v a2 s . c o  m*/
 */
@Override
public Collection<? extends PrivateKey> getMatchingKeys(KeyIdentifier keyIdentifier, Integer firstResult,
        Integer maxResults) throws KeyStoreException {
    Set<PrivateKey> keys = new HashSet<PrivateKey>();

    if (keyIdentifier == null) {
        logger.debug("null keyIdentifier.");

        return keys;
    }

    CertSelector certSelector = null;

    try {
        if (keyIdentifier instanceof CertSelectorKeyIdentifier) {
            CertSelectorKeyIdentifier certSelectorKeyIdentifier = (CertSelectorKeyIdentifier) keyIdentifier;

            certSelector = certSelectorKeyIdentifier.getSelector();
        } else {
            logger.warn("Unsupported KeyIdentifier '" + keyIdentifier.getClass().getCanonicalName() + "'");
        }

        /* only get entries with for which the key alias is set */
        CloseableIterator<? extends X509CertStoreEntry> certStoreIterator = certStore
                .getCertStoreIterator(certSelector, MissingKeyAlias.NOT_ALLOWED, firstResult, maxResults);

        try {
            while (certStoreIterator.hasNext()) {
                X509CertStoreEntry certStoreEntry = certStoreIterator.next();

                PrivateKey privateKey = getPrivateKey(certStoreEntry);

                if (privateKey != null) {
                    keys.add(privateKey);
                }
            }
        } finally {
            certStoreIterator.close();
        }
    } catch (IOException e) {
        throw new KeyStoreException(e);
    } catch (CertStoreException e) {
        throw new KeyStoreException(e);
    } catch (CloseableIteratorException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyStoreException(e);
    } catch (UnrecoverableKeyException e) {
        throw new KeyStoreException(e);
    }

    return keys;
}

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

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

        throw new KeyStoreException(e);
    }
}

From source file:org.hyperic.hq.hqapi1.HQConnection.java

private KeyManagerFactory getKeyManagerFactory(final KeyStore keystore, final String password)
        throws KeyStoreException {
    try {//from  w  ww.j  a  v a2  s .  com
        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(e);
    } catch (UnrecoverableKeyException e) {
        // invalid password, should never happen
        throw new KeyStoreException(e);
    }
}

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

/**
 * Delete a security object from this keystore.
 * //from  w w w.  j  a v a2s.c  o m
 * @param s
 *            The alias of the object to delete.
 * @throws KeyStoreException
 */
@Override
public void engineDeleteEntry(String s) throws KeyStoreException {

    SecurityObjectWrapper<?> object = this.aliasObjectMap.remove(s);
    if (object != null) {
        if (object instanceof ResourceTrustAnchor) {

            ResourceTrustAnchor descriptor = (ResourceTrustAnchor) object;
            Certificate cert;
            try {
                cert = descriptor.getTrustAnchor().getTrustedCert();
            } catch (ResourceStoreException e) {
                throw new KeyStoreException(e);
            }
            this.certFilenameMap.remove(cert);
            boolean success = descriptor.getFile().delete();
            if (!success) {
                // FIXME: warn? throw error?
                logger.info("Unable to delete certificate");
            }
        } else if (object instanceof ResourceProxyCredential) {

            ResourceProxyCredential proxy = (ResourceProxyCredential) object;
            try {
                proxy.getCredential();
            } catch (ResourceStoreException e) {
                throw new KeyStoreException(e);
            }
            boolean success = proxy.getFile().delete();
            if (!success) {
                // FIXME: warn? throw error?
                logger.info("Unable to delete credential");
            }
        }
    }
}

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

@Override
public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreException {
    throw new KeyStoreException("Not yet supported.");
}

From source file:org.hyperic.hq.hqapi1.HQConnection.java

private TrustManagerFactory getTrustManagerFactory(final KeyStore keystore)
        throws KeyStoreException, IOException {
    try {// w w  w  . ja  v a2s  .  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
        throw new KeyStoreException(e);
    }
}

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

@Override
public void engineSetKeyEntry(final String alias, final Key key, final char[] password,
        final Certificate[] chain) throws KeyStoreException {
    try {// w w w  . j a v  a  2s  . com
        databaseExecutor.executeTransaction(new DatabaseVoidAction() {
            @Override
            public void doAction(Session session) throws DatabaseException {
                setKeyEntryAction(alias, key, password, chain, session);
            }
        }, ACTION_RETRIES /* retry on a ConstraintViolationException */);
    } catch (DatabaseException e) {
        logger.error("Database exception.", e);

        throw new KeyStoreException(e);
    }
}

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

/**
 * Add a new private key to the keystore.
 * //w  w  w  . j  a v a  2  s  .  c o  m
 * @param s
 *            The alias for the object.
 * @param key
 *            The private key.
 * @param chars
 *            The password.
 * @param certificates
 *            The key's certificate chain.
 * @throws KeyStoreException
 */
@Override
public void engineSetKeyEntry(String s, Key key, char[] chars, Certificate[] certificates)
        throws KeyStoreException {

    if (!(key instanceof PrivateKey)) {
        throw new KeyStoreException("PrivateKey expected");
    }

    if (!(certificates instanceof X509Certificate[])) {
        throw new KeyStoreException("Certificate chain of X509Certificate expected");
    }
    CredentialWrapper wrapper;
    X509Credential credential = new X509Credential((PrivateKey) key, (X509Certificate[]) certificates);
    if (credential.isEncryptedKey()) {
        wrapper = createCertKeyCredential(s, credential);
    } else {
        wrapper = createProxyCredential(s, credential);
    }
    storeWrapper(wrapper);
    this.aliasObjectMap.put(wrapper.getAlias(), wrapper);
}

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

/**
 * Delete a security object from this keystore.
 *
 * @param s/*  www  .j a v  a  2 s.com*/
 *            The alias of the object to delete.
 * @throws KeyStoreException
 */
@Override
public void engineDeleteEntry(String s) throws KeyStoreException {

    SecurityObjectWrapper<?> object = this.aliasObjectMap.remove(s);
    if (object != null) {
        if (object instanceof ResourceTrustAnchor) {

            ResourceTrustAnchor descriptor = (ResourceTrustAnchor) object;
            Certificate cert;
            try {
                cert = descriptor.getTrustAnchor().getTrustedCert();
            } catch (ResourceStoreException e) {
                throw new KeyStoreException(e);
            }
            this.certFilenameMap.remove(cert);
            boolean success = descriptor.getFile().delete();
            if (!success) {
                // JGLOBUS-91 : warn? throw error?
                logger.info("Unable to delete certificate");
            }
        } else if (object instanceof ResourceProxyCredential) {

            ResourceProxyCredential proxy = (ResourceProxyCredential) object;
            try {
                proxy.getCredential();
            } catch (ResourceStoreException e) {
                throw new KeyStoreException(e);
            }
            boolean success = proxy.getFile().delete();
            if (!success) {
                // JGLOBUS-91 : warn? throw error?
                logger.info("Unable to delete credential");
            }
        }
    }
}

From source file:ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection.java

/**
 * TODO: rename/* w w w.  j  a  v  a2 s  .  c om*/
 */
public KeyStore getHohSignatureKeystore_() throws KeyStoreException {
    if (isBlank(getHohSignatureKeystore())) {
        return null;
    }
    if (myHohSignatureKeystore_ != null) {
        return myHohSignatureKeystore_;
    }

    File jksFile = new File(getHohSignatureKeystore());
    if (!jksFile.exists() || !jksFile.canRead()) {
        throw new KeyStoreException("File does not exist or can not be read: " + jksFile.getAbsolutePath());
    }

    char[] password = null;
    if (isNotBlank(myHohSignatureKeystorePassword)) {
        password = myHohSignatureKeystorePassword.toCharArray();
    }

    KeyStore keystore;
    try {
        keystore = KeystoreUtils.loadKeystore(jksFile, password);
    } catch (NoSuchAlgorithmException e) {
        ourLog.error("Failed to load keystore!", e);
        throw new KeyStoreException("Failed to load keystore: " + e.getMessage());
    } catch (CertificateException e) {
        ourLog.error("Failed to load keystore!", e);
        throw new KeyStoreException("Failed to load keystore: " + e.getMessage());
    } catch (IOException e) {
        ourLog.error("Failed to load keystore!", e);
        if (e.getCause() instanceof UnrecoverableKeyException) {
            throw new KeyStoreException("Keystore password appears to be incorrect");
        }
        throw new KeyStoreException("Failed to load keystore: " + e.getMessage());
    }

    if (this instanceof InboundConnection) {
        if (!KeystoreUtils.validateKeystoreForSignatureVerifying(keystore)) {
            throw new KeyStoreException("Keystore contains no keys appropriate for receiving data");
        }
    } else if (this instanceof OutboundConnection) {
        if (!KeystoreUtils.validateKeystoreForSignatureSigning(keystore)) {
            throw new KeyStoreException("Keystore contains no keys appropriate for receiving data");
        }
    }

    myHohSignatureKeystore_ = keystore;
    return myHohSignatureKeystore_;
}