Java Code Examples for java.security.KeyStore.deleteEntry()

The following are Jave code examples for showing how to use deleteEntry() of the java.security.KeyStore class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaide   File: KeyStoreFileManager.java   View Source Code Vote up 7 votes
public static String renameKey( String keystorePath, String storePass, String oldKeyName, String newKeyName, String keyPass)
    throws Exception
{
    char[] keyPw = null;

    try {
        KeyStore ks = loadKeyStore(keystorePath, storePass);
        if (ks instanceof JksKeyStore) newKeyName = newKeyName.toLowerCase();

        if (ks.containsAlias(newKeyName)) throw new KeyNameConflictException();

        keyPw = PasswordObfuscator.getInstance().decodeAliasPassword( keystorePath, oldKeyName, keyPass);
        Key key = ks.getKey(oldKeyName, keyPw);
        Certificate cert = ks.getCertificate( oldKeyName);

        ks.setKeyEntry(newKeyName, key, keyPw, new Certificate[] { cert});
        ks.deleteEntry( oldKeyName);

        writeKeyStore(ks, keystorePath, storePass);
        return newKeyName;
    }
    finally {
        PasswordObfuscator.flush(keyPw);
    }
}
 
Example 2
Project: jdk8u-jdk   File: CastError.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream fis = new FileInputStream(
            new File(System.getProperty("test.src"),
                    "../tools/jarsigner/JarSigning.keystore"));
    ks.load(fis, "bbbbbb".toCharArray());

    PrivateKey pk = (PrivateKey) ks.getKey("c", "bbbbbb".toCharArray());
    Certificate cert = ks.getCertificate("c");

    ks = KeyStore.getInstance("Windows-MY");
    ks.load(null, null);

    ks.setKeyEntry("8143913", pk, null, new Certificate[]{cert});
    ks.deleteEntry("8143913");
}
 
Example 3
Project: javaide   File: KeyStoreFileManager.java   View Source Code Vote up 5 votes
public static void deleteKey(String storePath, String storePass, String keyName)
    throws Exception
{
    KeyStore ks = loadKeyStore( storePath, storePass);
    ks.deleteEntry( keyName);
    writeKeyStore(ks, storePath, storePass);
}
 
Example 4
Project: ZeroKit-Android-SDK   File: Zerokit.java   View Source Code Vote up 5 votes
@SuppressWarnings("WeakerAccess")
void deleteSecret(@NonNull String alias) {
    try {
        KeyStore keyStore = KeyStore.getInstance(ANDROID_KEYSTORE);
        keyStore.load(null);
        keyStore.deleteEntry(alias);
    } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException | UnsupportedOperationException e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }
}
 
Example 5
Project: trust-wallet-android   File: KS.java   View Source Code Vote up 5 votes
private synchronized static void removeAliasAndFiles(Context context, String alias, String dataFileName, String ivFileName) {
	KeyStore keyStore;
	try {
		keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
		keyStore.load(null);
		keyStore.deleteEntry(alias);
		new File(getFilePath(context, dataFileName)).delete();
		new File(getFilePath(context, ivFileName)).delete();
	} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
		e.printStackTrace();
	}
}
 
Example 6
Project: openjdk-jdk10   File: CastError.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    KeyStore ks = KeyStore.getInstance(
            new File(System.getProperty("test.src"),
                    "../tools/jarsigner/JarSigning.keystore"),
            "bbbbbb".toCharArray());

    PrivateKey pk = (PrivateKey) ks.getKey("c", "bbbbbb".toCharArray());
    Certificate cert = ks.getCertificate("c");

    ks = KeyStore.getInstance("Windows-MY");
    ks.load(null, null);

    ks.setKeyEntry("8143913", pk, null, new Certificate[]{cert});
    ks.deleteEntry("8143913");
}
 
Example 7
Project: openjdk-jdk10   File: KeyStoreEmptyCertChain.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

        try {

            KeyStore keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
            keyStore.load(null, null);

            // Generate a certificate to use for testing
            CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");
            gen.generate(2048);
            Certificate cert =
                gen.getSelfCertificate(new X500Name("CN=test"), 3600);
            String alias = "JDK-8172244";
            char[] password = "password".toCharArray();
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

            // generate a private key for the certificate
            kpg.initialize(2048);
            KeyPair keyPair = kpg.generateKeyPair();
            PrivateKey privKey = keyPair.getPrivate();
            // need to bypass checks to store the private key without the cert
            Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
            spiField.setAccessible(true);
            KeyStoreSpi spi = (KeyStoreSpi) spiField.get(keyStore);
            spi.engineSetKeyEntry(alias, privKey, password, new Certificate[0]);
            keyStore.store(null, null);

            keyStore.getCertificateAlias(cert);
            keyStore.deleteEntry(alias);
            // test passes if no exception is thrown
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
Example 8
Project: jdk8u-jdk   File: TestKeyStoreEntry.java   View Source Code Vote up 4 votes
public void runTest(Provider p) throws Exception {
    try (FileOutputStream fos = new FileOutputStream("jceks");
            FileInputStream fis = new FileInputStream("jceks");) {

        KeyStore ks = KeyStore.getInstance("jceks", p);
        // create an empty key store
        ks.load(null, null);

        // store the secret keys
        String aliasHead = new String("secretKey");
        for (int j = 0; j < NUM_ALGOS; j++) {
            ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);
        }

        // write the key store out to a file
        ks.store(fos, PASSWDF);
        // wipe clean the existing key store
        for (int k = 0; k < NUM_ALGOS; k++) {
            ks.deleteEntry(aliasHead + k);
        }
        if (ks.size() != 0) {
            throw new RuntimeException("ERROR: re-initialization failed");
        }

        // reload the key store with the file
        ks.load(fis, PASSWDF);

        // check the integrity/validaty of the key store
        Key temp = null;
        String alias = null;
        if (ks.size() != NUM_ALGOS) {
            throw new RuntimeException("ERROR: wrong number of key"
                    + " entries");
        }

        for (int m = 0; m < ks.size(); m++) {
            alias = aliasHead + m;
            temp = ks.getKey(alias, PASSWDK);
            // compare the keys
            if (!temp.equals(sks[m])) {
                throw new RuntimeException("ERROR: key comparison (" + m
                        + ") failed");
            }
            // check the type of key
            if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {
                throw new RuntimeException("ERROR: type identification ("
                        + m + ") failed");
            }
        }
    }
}
 
Example 9
Project: openjdk-jdk10   File: TestKeyStoreEntry.java   View Source Code Vote up 4 votes
public void runTest(Provider p) throws Exception {
    try (FileOutputStream fos = new FileOutputStream("jceks");
            FileInputStream fis = new FileInputStream("jceks");) {

        KeyStore ks = KeyStore.getInstance("jceks", p);
        // create an empty key store
        ks.load(null, null);

        // store the secret keys
        String aliasHead = new String("secretKey");
        for (int j = 0; j < NUM_ALGOS; j++) {
            ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);
        }

        // write the key store out to a file
        ks.store(fos, PASSWDF);
        // wipe clean the existing key store
        for (int k = 0; k < NUM_ALGOS; k++) {
            ks.deleteEntry(aliasHead + k);
        }
        if (ks.size() != 0) {
            throw new RuntimeException("ERROR: re-initialization failed");
        }

        // reload the key store with the file
        ks.load(fis, PASSWDF);

        // check the integrity/validaty of the key store
        Key temp = null;
        String alias = null;
        if (ks.size() != NUM_ALGOS) {
            throw new RuntimeException("ERROR: wrong number of key"
                    + " entries");
        }

        for (int m = 0; m < ks.size(); m++) {
            alias = aliasHead + m;
            temp = ks.getKey(alias, PASSWDK);
            // compare the keys
            if (!temp.equals(sks[m])) {
                throw new RuntimeException("ERROR: key comparison (" + m
                        + ") failed");
            }
            // check the type of key
            if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {
                throw new RuntimeException("ERROR: type identification ("
                        + m + ") failed");
            }
        }
    }
}
 
Example 10
Project: openjdk-jdk10   File: SmallPrimeExponentP.java   View Source Code Vote up 4 votes
public static void main(String argv[]) throws Exception {

        long seed = Long.parseLong(argv[0]);
        System.out.println("Seed for SecureRandom = " + seed + "L");

        KeyStore ks = KeyStore.getInstance("Windows-MY");
        ks.load(null, null);

        CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
        ckg.setRandom(new MySecureRandom(seed));

        String alias = "anything";
        int count = 0;

        boolean see63 = false;
        boolean see65 = false;
        while (!see63 || !see65) {
            ckg.generate(1024);
            RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();

            int len = k.getPrimeExponentP().toByteArray().length;
            System.out.println("Length of P = " + len);
            if (len == 63 || len == 65) {
                if (len == 63) {
                    if (see63) {
                        continue;
                    } else {
                        see63 = true;
                    }
                }
                if (len == 65) {
                    if (see65) {
                        continue;
                    } else {
                        see65 = true;
                    }
                }
                ks.setKeyEntry(alias, k, null, new X509Certificate[]{
                    ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)
                });
                count++;
            }
        }

        // Because of JDK-8185844, it has to reload the key store after
        // deleting an entry.
        for (int i = 0; i < count; i++) {
            ks.deleteEntry(alias);
            ks.load(null, null);
        }
    }
 
Example 11
Project: BiglyBT   File: SESecurityManagerImpl.java   View Source Code Vote up 2 votes
protected void
addCertToKeyStore(
	String								alias,
	Key									public_key,
	java.security.cert.Certificate[] 	certChain )

	throws Exception
{
	try{
		this_mon.enter();

		KeyStore key_store = loadKeyStore();

		if( key_store.containsAlias( alias )){

			key_store.deleteEntry( alias );
		}

		key_store.setKeyEntry( alias, public_key, SESecurityManager.SSL_PASSWORD.toCharArray(), certChain );

		FileOutputStream	out = null;

		try{
			out = new FileOutputStream(keystore_name);

			key_store.store(out, SESecurityManager.SSL_PASSWORD.toCharArray());

		}catch( Throwable e ){

			Debug.printStackTrace( e );

		}finally{

			if ( out != null ){

				out.close();
			}
		}
	}finally{

		this_mon.exit();
	}
}
 
Example 12
Project: BiglyBT   File: SESecurityManagerImpl.java   View Source Code Vote up 2 votes
protected SSLSocketFactory
addCertToTrustStore(
	String							alias,
	java.security.cert.Certificate	cert,
	boolean							update_https_factory )

	throws Exception
{
	try{
		this_mon.enter();

		KeyStore keystore = getTrustStore();

		if ( cert != null ){

			if ( keystore.containsAlias( alias )){

				keystore.deleteEntry( alias );
			}

			keystore.setCertificateEntry(alias, cert);

			FileOutputStream	out = null;

			try{
				out = new FileOutputStream(truststore_name);

				keystore.store(out, SESecurityManager.SSL_PASSWORD.toCharArray());

			}finally{

				if ( out != null ){

					out.close();
				}
			}
		}

			// pick up the changed trust store

		TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

		tmf.init(keystore);

		SSLContext ctx = SSLContext.getInstance("SSL");

		ctx.init(null, tmf.getTrustManagers(), null);

		SSLSocketFactory	factory = ctx.getSocketFactory();

		if ( update_https_factory ){

			HttpsURLConnection.setDefaultSSLSocketFactory( factory );
		}

		return( factory );
	}finally{

		this_mon.exit();
	}
}