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:com.owncloud.android.lib.common.network.NetworkUtils.java

public static void removeCertFromKnownServersStore(String alias, Context context)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore knownServers = getKnownServersStore(context);
    if (knownServers.containsAlias(alias)) {
        knownServers.deleteEntry(alias);
    }// w  w w.j av a  2  s  . c om
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
        knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
    } finally {
        fos.close();
    }
}

From source file:org.openanzo.rdf.utils.KeystoreUtils.java

/**
 * //from   ww  w.  jav  a2  s .  c  o m
 * @param keyStoreFile
 * @param keystoreType
 * @param password
 * @param alias
 * @param cert
 * @throws AnzoException
 */
public static void addTrustedCert(String keyStoreFile, String keystoreType, String password, String alias,
        X509Certificate cert) throws AnzoException {
    try {
        KeyStore keyStore = KeyStore.getInstance(keystoreType);
        keyStore.load(new FileInputStream(keyStoreFile), password.toCharArray());

        if (keyStore.containsAlias(alias)) {
            keyStore.deleteEntry(alias);
        }
        keyStore.setCertificateEntry(alias, cert);

        writeStoreToFile(keyStoreFile, password, keyStore);

    } catch (Exception cce) {
        throw new AnzoException(ExceptionConstants.OSGI.INTERNAL_COMPONENT_ERROR, cce);
    }
}

From source file:org.paxml.util.CryptoUtils.java

private static void deleteKey(KeyStore keyStore, String keyName) {
    try {//from  www  . j  a v a2 s.  com
        if (keyStore.containsAlias(keyName)) {
            keyStore.deleteEntry(keyName);
        }
    } catch (KeyStoreException e) {
        throw new PaxmlRuntimeException(e);
    }
}

From source file:it.cnr.icar.eric.client.xml.registry.util.CertificateUtil.java

/**
 * Generate a self signed certificate and store it in the keystore.
 * //from  ww  w. ja  v a 2  s  .  c o  m
 * @param userRegInfo
 * @throws JAXRException
 */
public static void generateRegistryIssuedCertificate(UserRegistrationInfo userRegInfo) throws JAXRException {
    User user = userRegInfo.getUser();
    LifeCycleManager lcm = user.getLifeCycleManager();
    String dname = getDNameFromUser(userRegInfo);
    File keystoreFile = KeystoreUtil.getKeystoreFile();
    KeystoreUtil.createKeystoreDirectory(keystoreFile);
    String keystoreType = ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.storetype", "JKS");
    String storePassStr = new String(userRegInfo.getStorePassword());
    String keyPassStr = new String(userRegInfo.getKeyPassword());
    String alias = userRegInfo.getAlias();
    String keyAlg = "RSA"; // XWSS does not support DSA which is default is
    // KeyTool. Hmm. Weird.

    String[] args = { "-genkey", "-keyAlg", keyAlg, "-alias", alias, "-keypass", keyPassStr, "-keystore",
            keystoreFile.getAbsolutePath(), "-storepass", storePassStr, "-storetype", keystoreType, "-dname",
            dname };

    try {
        KeyTool keytool = new KeyTool();
        keytool.run(args, System.out);

        // Now load the KeyStore and get the cert
        FileInputStream fis = new FileInputStream(keystoreFile);

        KeyStore keyStore = KeyStore.getInstance(keystoreType);
        keyStore.load(fis, storePassStr.toCharArray());
        fis.close();

        X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
        Certificate[] certChain = getCertificateSignedByRegistry(lcm, cert);
        Key key = keyStore.getKey(alias, userRegInfo.getKeyPassword());

        // Now overwrite original cert with signed cert
        keyStore.deleteEntry(alias);

        // keyStore.setCertificateEntry(alias, cert);
        keyStore.setKeyEntry(alias, key, userRegInfo.getKeyPassword(), certChain);
        FileOutputStream fos = new java.io.FileOutputStream(keystoreFile);
        keyStore.store(fos, storePassStr.toCharArray());
        fos.flush();
        fos.close();
    } catch (Exception e) {
        throw new JAXRException(JAXRResourceBundle.getInstance().getString("message.CertGenFailed"), e);
    }

    log.debug(JAXRResourceBundle.getInstance().getString("message.StoredUserInKeyStore",
            new Object[] { alias, keystoreFile.getAbsolutePath() }));

    try {
        // Export registry issued cert to certFile so it can be available
        // for import into a web browser for SSL access to registry
        exportRegistryIssuedCert(userRegInfo);
    } catch (Exception e) {
        String msg = JAXRResourceBundle.getInstance().getString(
                "message.UnableToExportCertificateSeeNextExceptionNoteThatThisFeatureRequiresUseOfJDK5");
        log.warn(msg, e);
        // Do not throw exception as user reg can be done despite not
        // exporting the p12 file for the web browser.
    }
}

From source file:org.signserver.server.cryptotokens.CryptoTokenHelper.java

/**
 * Remove a key with the specified alias from the keystore.
 * @param keyStore to remove from//from w w  w. j  a v  a  2  s.  c  om
 * @param alias of key to remove
 * @return true if the key alias was removed
 * @throws CryptoTokenOfflineException if the keystore was null
 * @throws KeyStoreException for keystore related errors
 * @throws SignServerException if the keystore did not contain a key with the specified alias
 */
public static boolean removeKey(final KeyStore keyStore, final String alias)
        throws CryptoTokenOfflineException, KeyStoreException, SignServerException {
    if (keyStore == null) {
        throw new CryptoTokenOfflineException("Token offline");
    }
    if (!keyStore.containsAlias(alias)) {
        throw new SignServerException("No such alias in token: " + alias);
    }
    keyStore.deleteEntry(alias);
    return !keyStore.containsAlias(alias);
}

From source file:org.strongswan.android.ui.activity.TrustedCertificatesActivity.java

@Override
public void onDelete(String alias) {
    try {//from  w w  w.  j a v a2s  . c om
        KeyStore store = KeyStore.getInstance("LocalCertificateStore");
        store.load(null, null);
        store.deleteEntry(alias);
        reloadCertificates();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * Removed the saved password and key for a file
 *///from w  w w.  ja va 2 s .c om
public synchronized void removeSavedPassword(Uri fileUri) {
    String keyName = getPrefsKey(fileUri);
    getPrefs().edit().remove(keyName).remove(getIvPrefsKey(keyName)).apply();
    if (isAvailable()) {
        PasswdSafeUtil.dbginfo(TAG, "removeSavedPassword: %s", fileUri);
        try {
            KeyStore keyStore = getKeystore();
            keyStore.deleteEntry(keyName);
        } catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

From source file:eu.europa.esig.dss.x509.KeyStoreCertificateSource.java

public void deleteCertificateFromKeyStore(String dssId) {
    KeyStore keyStore = getKeyStore();
    try {//ww  w .  j  a v a2s.  co  m
        if (keyStore.containsAlias(dssId)) {
            keyStore.deleteEntry(dssId);
            persistKeyStore(keyStore);
            logger.info("Certificate with ID " + dssId + " successfuly removed from the keystore");
        } else {
            logger.warn("Certificate " + dssId + " not found in the keystore");
        }
    } catch (Exception e) {
        logger.error("Unable to delete certificate from the keystore : " + e.getMessage(), e);
    }
}

From source file:com.netscape.cmstools.pkcs11.PKCS11KeyRemoveCLI.java

public void execute(String[] args) throws Exception {

    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        printHelp();//from   ww w . j  ava2  s .  c  om
        return;
    }

    if (cmd.hasOption("verbose")) {
        PKILogger.setLevel(PKILogger.Level.INFO);

    } else if (cmd.hasOption("debug")) {
        PKILogger.setLevel(PKILogger.Level.DEBUG);
    }

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length < 1) {
        throw new Exception("Missing key ID.");
    }

    String alias = cmdArgs[0];

    String tokenName = getConfig().getTokenName();
    CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);

    KeyStore ks = KeyStore.getInstance("pkcs11");
    ks.load(new JSSLoadStoreParameter(token));

    Key key = ks.getKey(alias, null);

    if (key == null) {
        throw new Exception("Key not found: " + alias);
    }

    ks.deleteEntry(alias);
}

From source file:com.netscape.cmstools.pkcs11.PKCS11CertRemoveCLI.java

public void execute(String[] args) throws Exception {

    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        printHelp();/*w w w.j a  v  a2 s. co m*/
        return;
    }

    if (cmd.hasOption("verbose")) {
        PKILogger.setLevel(PKILogger.Level.INFO);

    } else if (cmd.hasOption("debug")) {
        PKILogger.setLevel(PKILogger.Level.DEBUG);
    }

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length < 1) {
        throw new Exception("Missing cert ID.");
    }

    String alias = cmdArgs[0];

    String tokenName = getConfig().getTokenName();
    CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);

    KeyStore ks = KeyStore.getInstance("pkcs11");
    ks.load(new JSSLoadStoreParameter(token));

    Certificate cert = ks.getCertificate(alias);

    if (cert == null) {
        throw new Exception("Certificate not found: " + alias);
    }

    ks.deleteEntry(alias);
}