Example usage for java.security KeyStore deleteEntry

List of usage examples for java.security KeyStore deleteEntry

Introduction

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

Prototype

public final void deleteEntry(String alias) throws KeyStoreException 

Source Link

Document

Deletes the entry identified by the given alias from this keystore.

Usage

From source file:org.apache.hadoop.gateway.services.security.impl.BaseKeystoreService.java

public void removeCredential(String alias, KeyStore ks) {
    if (ks != null) {
        try {//from w w w  . jav a  2s .c  o m
            if (ks.containsAlias(alias)) {
                ks.deleteEntry(alias);
            }
        } catch (KeyStoreException e) {
            LOG.failedToAddCredential(e);
        }
    }
}

From source file:org.apache.directory.studio.connection.core.StudioKeyStoreManager.java

private void removeFromKeyStore(X509Certificate certificate, KeyStore keyStore) throws Exception {
    String alias = keyStore.getCertificateAlias(certificate);
    if (alias != null) {
        keyStore.deleteEntry(alias);
    }//  w  w w  .j a  va  2  s. c  o m
}

From source file:gov.nih.nci.cacisweb.action.SecureFTPAction.java

/**
 * //from   w ww  .  jav a2 s.  com
 * @return
 * @throws Exception
 */
public String delete() throws Exception {
    log.debug("delete() - START");
    String secureFTPPropertyFileLocation = CaCISUtil
            .getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECFTP_PROPERTIES_FILE_LOCATION);
    String secureFTPKeystoreLocation = CaCISUtil.getPropertyFromPropertiesFile(secureFTPPropertyFileLocation,
            CaCISUtil.getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECFTP_TRUSTSTORE_LOCATION_PROP_NAME));
    String secureFTPKeystorePassword = CaCISUtil.getPropertyFromPropertiesFile(secureFTPPropertyFileLocation,
            CaCISUtil.getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECFTP_TRUSTSTORE_PASSWORD_PROP_NAME));
    try {
        CaCISUtil caCISUtil = new CaCISUtil();
        KeyStore keystore = caCISUtil.getKeystore(secureFTPKeystoreLocation,
                CaCISWebConstants.COM_KEYSTORE_TYPE_JKS, secureFTPKeystorePassword);
        caCISUtil.releaseKeystore();
        // Delete the certificate
        keystore.deleteEntry(secureFTPBean.getCertificateAlias());

        // Save the new keystore contents
        FileOutputStream out = new FileOutputStream(new File(secureFTPKeystoreLocation));
        keystore.store(out, secureFTPKeystorePassword.toCharArray());
        out.close();

        // delete the entry from FTP configuration properties file
        PropertiesConfiguration config = new PropertiesConfiguration(
                CaCISUtil.getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECFTP_CONFIG_FILE_LOCATION));
        config.clearProperty(secureFTPBean.getCertificateAlias());
        config.save();
    } catch (KeystoreInstantiationException kie) {
        log.error(kie.getMessage());
        addActionError(getText("exception.keystoreInstantiation"));
        return ERROR;
    }
    addActionMessage(getText("secureFTPBean.deleteCertificateSuccessful"));
    log.debug("delete() - END");
    return SUCCESS;
}

From source file:com.jefftharris.passwdsafe.SavedPasswordsMgr.java

/**
 * Remove all saved passwords and keys//  w ww . j  a  va  2  s  . co m
 */
public synchronized void removeAllSavedPasswords() {
    getPrefs().edit().clear().apply();
    if (isAvailable()) {
        try {
            KeyStore keyStore = getKeystore();
            for (String key : Collections.list(keyStore.aliases())) {
                PasswdSafeUtil.dbginfo(TAG, "removeAllSavedPasswords key: %s", key);
                keyStore.deleteEntry(key);
            }
        } catch (CertificateException | NoSuchAlgorithmException | IOException | KeyStoreException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.sufficientlysecure.keychain.ui.SettingsSmartPGPAuthorityFragment.java

private boolean deleteAuthority(final int position) {
    try {/*from  w  w w  .j a  v  a 2 s.co  m*/
        final KeyStore ks = SettingsSmartPGPAuthoritiesActivity.readKeystore(getContext());

        if (ks == null) {
            return false;
        }

        ks.deleteEntry(mAuthorities.get(position));

        SettingsSmartPGPAuthoritiesActivity.writeKeystore(getContext(), ks);

        mAuthorities.remove(mAuthorities.get(position));
        mAdapter.notifyItemRemoved(position);

        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.opendaylight.aaa.cert.impl.ODLKeyTool.java

public boolean addCertificate(final String keyStoreName, final String keyStorePwd, final String certificate,
        final String alias) {
    try {/*ww  w .j ava 2  s .  c  o  m*/
        final X509Certificate newCert = getCertificate(certificate);
        final KeyStore keyStore = KeyStore.getInstance("JKS");
        final FileInputStream fInputStream = new FileInputStream(workingDir + keyStoreName);
        keyStore.load(fInputStream, keyStorePwd.toCharArray());
        if (keyStore.isCertificateEntry(alias)) {
            keyStore.deleteEntry(alias);
        }
        keyStore.setCertificateEntry(alias, newCert);
        keyStore.store(new FileOutputStream(workingDir + keyStoreName), keyStorePwd.toCharArray());
        LOG.info("Certificate {}  Added to keyStore {}", alias, keyStoreName);
        return true;
    } catch (CertificateException | KeyStoreException | NoSuchAlgorithmException | IOException e) {
        LOG.error("failed to add certificate", e);
        return false;
    }
}

From source file:gov.nih.nci.cacisweb.action.SecureXDSNAVAction.java

/**
 * /*from  ww w . j  a  v  a2 s  .  co m*/
 * @return
 * @throws Exception
 */
public String delete() throws Exception {
    log.debug("delete() - START");
    String secureXDSNAVKeystoreLocation = CaCISUtil
            .getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECXDSNAV_RECEPIENT_TRUSTSTORE_LOCATION);
    try {
        CaCISUtil caCISUtil = new CaCISUtil();
        KeyStore keystore = caCISUtil.getKeystore(secureXDSNAVKeystoreLocation,
                CaCISWebConstants.COM_KEYSTORE_TYPE_JKS, CaCISUtil.getProperty(
                        CaCISWebConstants.COM_PROPERTY_NAME_SECXDSNAV_RECEPIENT_TRUSTSTORE_PASSWORD));
        caCISUtil.releaseKeystore();
        // Delete the certificate
        keystore.deleteEntry(secureXDSNAVBean.getCertificateAlias());

        // Save the new keystore contents
        FileOutputStream out = new FileOutputStream(new File(secureXDSNAVKeystoreLocation));
        keystore.store(out,
                CaCISUtil
                        .getProperty(
                                CaCISWebConstants.COM_PROPERTY_NAME_SECXDSNAV_RECEPIENT_TRUSTSTORE_PASSWORD)
                        .toCharArray());
        out.close();

        // delete the entry from XDSNAV configuration properties file
        PropertiesConfiguration config = new PropertiesConfiguration(CaCISUtil
                .getProperty(CaCISWebConstants.COM_PROPERTY_NAME_SECXDSNAV_RECEPIENT_CONFIG_FILE_LOCATION));
        config.clearProperty(secureXDSNAVBean.getCertificateAlias());
        config.save();
    } catch (KeystoreInstantiationException kie) {
        log.error(kie.getMessage());
        addActionError(getText("exception.keystoreInstantiation"));
        return ERROR;
    }
    addActionMessage(getText("secureXDSNAVBean.deleteCertificateSuccessful"));
    log.debug("delete() - END");
    return SUCCESS;
}

From source file:org.opendaylight.aaa.cert.impl.ODLMdsalKeyTool.java

public KeyStore addCertificate(final KeyStore keyStore, final String certificate, final String alias,
        final boolean deleteOld) {
    try {//w  w  w  .  java2s  .  co  m
        final X509Certificate newCert = getCertificate(certificate);
        if (keyStore.isCertificateEntry(alias) && deleteOld) {
            keyStore.deleteEntry(alias);
        }
        if (newCert != null) {
            keyStore.setCertificateEntry(alias, newCert);
        } else {
            LOG.warn("{} Not a valid certificate {}", alias, certificate);
            return null;
        }
        return keyStore;
    } catch (final KeyStoreException e) {
        LOG.error("failed to add certificate", e);
        return null;
    }
}

From source file:com.streamsets.datacollector.credential.javakeystore.JavaKeyStoreCredentialStore.java

public void deleteCredential(String name) {
    Utils.checkNotNull(name, "name cannot be NULL");

    try {// w  w w  .  j av a  2s .c o  m
        KeyStore keyStore = loadKeyStore();
        if (keyStore == null) {
            keyStore = createKeyStore();
        }
        if (keyStore.getKey(name, getKeystorePassword().toCharArray()) != null) {
            keyStore.deleteEntry(name);
            persistStore(keyStore);
        } else {
            throw new IOException(
                    Utils.format("Credential Store '{}', alias '{}' not found", getContext().getId(), name));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.wso2.carbon.apimgt.impl.utils.CertificateMgtUtils.java

/**
 * This method will remove certificate from the trust store which matches the given alias.
 *
 * @param alias : The alias which the certificate should be deleted.
 * @return : ResponseCode based on the execution.
 * <p>/*from w  w  w  .j a  v a 2  s.com*/
 * Response Codes
 * SUCCESS : If the certificate is deleted successfully.
 * INTERNAL_SERVER_ERROR : If any exception occurred.
 * CERTIFICATE_NOT_FOUND : If the Alias is not found in the key store.
 */
public ResponseCode removeCertificateFromTrustStore(String alias) {

    boolean isExists; //Check for the existence of the certificate in trust store.
    try {
        File trustStoreFile = new File(TRUST_STORE);
        localTrustStoreStream = new FileInputStream(trustStoreFile);
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(localTrustStoreStream, TRUST_STORE_PASSWORD);

        if (trustStore.containsAlias(alias)) {
            trustStore.deleteEntry(alias);
            isExists = true;
        } else {
            isExists = false;
            if (log.isDebugEnabled()) {
                log.debug("Certificate for alias '" + alias + "' not found in the trust store.");
            }
        }

        fileOutputStream = new FileOutputStream(trustStoreFile);
        trustStore.store(fileOutputStream, TRUST_STORE_PASSWORD);
        responseCode = isExists ? ResponseCode.SUCCESS : ResponseCode.CERTIFICATE_NOT_FOUND;
    } catch (IOException e) {
        log.error("Error in loading the certificate.", e);
        responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    } catch (CertificateException e) {
        log.error("Error loading certificate.", e);
        responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    } catch (NoSuchAlgorithmException e) {
        log.error("Could not find the algorithm to load the certificate.", e);
        responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    } catch (KeyStoreException e) {
        log.error("Error reading certificate contents.", e);
        responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    } finally {
        closeStreams(localTrustStoreStream, fileOutputStream);
    }
    return responseCode;
}