Code example for KeyStore

Methods: load

    public static AdvancedSslSocketFactory getAdvancedSslSocketFactory(Context context) throws GeneralSecurityException, IOException {
        if (mAdvancedSslSocketFactory  == null) {
            KeyStore trustStore = getKnownServersStore(context);
            AdvancedX509TrustManager trustMgr = new AdvancedX509TrustManager(trustStore);
            TrustManager[] tms = new TrustManager[] { trustMgr };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tms, null);
            mHostnameVerifier = new BrowserCompatHostnameVerifier();
            mAdvancedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, trustMgr, mHostnameVerifier);
        return mAdvancedSslSocketFactory;
    private static String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks";
    private static String LOCAL_TRUSTSTORE_PASSWORD = "password";
    private static KeyStore mKnownServersStore = null;
     * Returns the local store of reliable server certificates, explicitly accepted by the user. 
     * Returns a KeyStore instance with empty content if the local store was never created. 
     * Loads the store from the storage environment if needed. 
     * @param context                       Android context where the operation is being performed. 
     * @return                              KeyStore instance with explicitly-accepted server certificates.  
     * @throws KeyStoreException            When the KeyStore instance could not be created. 
     * @throws IOException                  When an existing local trust store could not be loaded. 
     * @throws NoSuchAlgorithmException     When the existing local trust store was saved with an unsupported algorithm. 
     * @throws CertificateException         When an exception occurred while loading the certificates from the local trust store. 
    private static KeyStore getKnownServersStore(Context context) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
        if (mKnownServersStore == null) {
            //mKnownServersStore = KeyStore.getInstance("BKS"); 
            mKnownServersStore = KeyStore.getInstance(KeyStore.getDefaultType());
            File localTrustStoreFile = new File(context.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME);
            Log.d(TAG, "Searching known-servers store at " + localTrustStoreFile.getAbsolutePath());
            if (localTrustStoreFile.exists()) {
                InputStream in = new FileInputStream(localTrustStoreFile);
                try { 
                    mKnownServersStore.load(in, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
                } finally { 
            } else { 
                mKnownServersStore.load(null, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); // necessary to initialize an empty KeyStore instance
        return mKnownServersStore;