Code example for KeyStore

Methods: getTypesetEntrystore

0
        FileOutputStream fos = null;
        try 
        { 
            if (oldPassword != null)
            { 
                if (loadKeystore(keyStoreFile, oldPassword, keyStore.getType()) != null)
                { 
                    fos = new FileOutputStream(keyStoreFile);
                    keyStore.store(fos, newPassword);
                } 
            } 
            else 
            { 
                fos = new FileOutputStream(keyStoreFile);
                keyStore.store(fos, newPassword);
            } 
        } 
        finally 
        { 
            if (fos != null)
            { 
                try 
                { 
                    fos.close();
                } 
                catch (IOException e)
                { 
                    StudioLogger.error("Could not close steam while writing keystore file. " 
                            + e.getMessage());
                } 
            } 
        } 
    } 
 
    /** 
     * Loads a KeyStore from a given file from the default type, usually JKS. 
     * If keyStoreFile path don't exist then a new empty KeyStore will be created on the given location. 
     *   <b>Note:</b> Calling this method is the same as calling loadKeystore(keyStoreFile, password, KeyStore.getDefaultType()) 
     * @param keyStoreFile The keyStore location. 
     * @param password The KeyStore password 
     * @return the {@link KeyStore} representing the file. 
     * @throws KeyStoreManagerException  
     * @throws InvalidPasswordException  
     */ 
    public static KeyStore loadKeystore(File keyStoreFile, char[] password)
            throws KeyStoreManagerException, InvalidPasswordException 
    { 
        return loadKeystore(keyStoreFile, password, KeyStore.getDefaultType());
    } 
 
    /** 
     * Loads a KeyStore from a given file. 
     * If keyStoreFile path don't exist then a new empty KeyStore will be created on memory. 
     * If you want o create a new KeyStore file, calling createStore is recommended. 
     * @param keyStoreFile The keyStore location. 
     * @param password The KeyStore password 
     * @param storeType The Type of the keystore o be loaded. 
     * @return the {@link KeyStore} representing the file. 
     * @throws KeyStoreManagerException  
     * @throws InvalidPasswordException  
     */ 
    public static KeyStore loadKeystore(File keyStoreFile, char[] password, String storeType)
            throws KeyStoreManagerException, InvalidPasswordException 
    { 
        KeyStore keyStore = null;
        FileInputStream fis = null;
        try 
        { 
            keyStore = KeyStore.getInstance(storeType);
 
            if ((keyStoreFile != null) && keyStoreFile.exists() && (keyStoreFile.length() > 0))
            { 
                fis = new FileInputStream(keyStoreFile);
            } 
 
            //fis = null means a new keyStore will be created 
            keyStore.load(fis, password);
        } 
        catch (IOException e)
        { 
            if (e.getMessage().contains("password was incorrect")
                    || (e.getCause() instanceof UnrecoverableKeyException))
            { 
                throw new InvalidPasswordException(e.getMessage());
            } 
            else 
            { 
                throw new KeyStoreManagerException(NLS.bind( 
                        CertificateManagerNLS.KeyStoreUtils_Error_LoadKeyStore, keyStoreFile), e);
            } 
        } 
        catch (Exception e)
        { 
            throw new KeyStoreManagerException(NLS.bind( 
                    CertificateManagerNLS.KeyStoreUtils_Error_LoadKeyStore, keyStoreFile), e);
        } 
        finally 
        { 
            if (fis != null)
            { 
                try 
                { 
                    fis.close();
                } 
                catch (IOException e)
                { 
                    StudioLogger.error("Could not close steam while loading keystore. " 
                            + e.getMessage());
                } 
            } 
        } 
 
        return keyStore;
    } 
 
    /** 
     * Simply deletes the KeyStore File 
     * @param keyStoreFile teh KeyStore file to be deleted. 
     * @throws KeyStoreException If any error occur. 
     */ 
    public static void deleteKeystore(File keyStoreFile) throws KeyStoreManagerException
    { 
        try 
        { 
            FileUtil.deleteFile(keyStoreFile);
        } 
        catch (IOException e)
        { 
            throw new KeyStoreManagerException(NLS.bind( 
                    CertificateManagerNLS.KeyStoreUtils_Error_DeleteKeyStore, keyStoreFile), e);
        } 
    } 
 
    /** 
     * Write the keyStore in to the given file, protecting it with password. 
     * Warn: Since there's actually no way to change the password this method will overwrite the existing file with the keyStore contents, 
     *  without further warning. 
     * @param keyStore the {@link KeyStore} to be written. 
     * @param keyStoreFile The KeyStore location 
     * @param oldPassword  
     * @param sourcePassword the new Password 
     * @throws KeyStoreException If file could no be write. 
     */ 
    public static void changeKeystorePasswd(KeyStore keyStore, File keyStoreFile,
            char[] oldPassword, char[] newPassword) throws KeyStoreManagerException
    { 
        try 
        { 
            keyStore = loadKeystore(keyStoreFile, oldPassword, keyStore.getType());
            writeKeyStore(keyStore, oldPassword, newPassword, keyStoreFile);
        } 
        catch (Exception e)
        { 
            throw new KeyStoreManagerException(NLS.bind( 
                    CertificateManagerNLS.KeyStoreUtils_Error_WriteKeyStore, keyStoreFile), e);
        } 
    } 
 
    /** 
     * Adds a new enty to a given keyStore. 
     * @param keyStore The Keystore that will receive the entry 
     * @param keyStorePassword The KeyStore password 
     * @param keyStoreFile The KeyStore file path 
     * @param alias The new entry alias 
     * @param entry The Entry to be added 
     * @param entryPassword The password to protect the entry 
     * @throws KeyStoreManagerException if any error occurs. 
     */ 
    public static void addEntry(KeyStore keyStore, char[] keyStorePassword, File keyStoreFile,
            String alias, Entry entry, char[] entryPassword) throws KeyStoreManagerException
    { 
        try 
        { 
            PasswordProtection passwordProtection = new KeyStore.PasswordProtection(entryPassword);
            keyStore = loadKeystore(keyStoreFile, keyStorePassword, keyStore.getType());
 
            if (!keyStore.containsAlias(alias))
            { 
                keyStore.setEntry(alias, entry, passwordProtection);
                writeKeyStore(keyStore, keyStorePassword, keyStoreFile);
            } 
            else 
            { 
                throw new KeyStoreManagerException(NLS.bind("Alias \"{0}\" already exists.", alias));
            } 
 
        } 
        catch (KeyStoreManagerException e)
        { 
            throw e;
        } 
        catch (Exception e)
        { 
            throw new KeyStoreManagerException(NLS.bind( 
                    CertificateManagerNLS.KeyStoreUtils_Error_AddEntryToKeyStore, alias), e);
        } 
    } 
 
    /** 
     * Adds a new enty to a given keyStore. 
     * @param keyStore The Keystore that will receive the entry 
     * @param keyStorePassword The KeyStore password 
     * @param keyStoreFile The KeyStore file path 
     * @param alias The new entry alias 
     * @param entry The Entry to be added 
     * @param entryPassword The password to protect the entry 
     * @throws KeyStoreManagerException if any error occurs. 
     */ 
    public static void changeEntryPassword(KeyStore keyStore, char[] keyStorePassword,
            File keyStoreFile, String alias, Entry entry, char[] entryPassword)
            throws KeyStoreManagerException 
    { 
        try 
        { 
            PasswordProtection passwordProtection = new KeyStore.PasswordProtection(entryPassword);
            keyStore.setEntry(alias, entry, passwordProtection);
            writeKeyStore(keyStore, keyStorePassword, keyStoreFile);
        } 
        catch (Exception e)
        { 
            throw new KeyStoreManagerException(NLS.bind(