Example usage for java.security KeyStore getDefaultType

List of usage examples for java.security KeyStore getDefaultType

Introduction

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

Prototype

public static final String getDefaultType() 

Source Link

Document

Returns the default keystore type as specified by the keystore.type security property, or the string "jks" (acronym for "Java keystore" ) if no such property exists.

Usage

From source file:com.appfirst.communication.AFHttpClient.java

public DefaultHttpClient getAFHttpClient() {
    try {//from   w  w  w. j  a v  a  2  s  .  co m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        try {
            trustStore.load(null, null);
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        SSLSocketFactory sf = new AFSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (NoSuchAlgorithmException nsae) {
        Log.e(TAG, nsae.getMessage());
        return new DefaultHttpClient();
    } catch (KeyManagementException kme) {
        Log.e(TAG, kme.getMessage());
        return new DefaultHttpClient();
    } catch (KeyStoreException kse) {
        Log.e(TAG, kse.getMessage());
        return new DefaultHttpClient();
    } catch (UnrecoverableKeyException uke) {
        Log.e(TAG, uke.getMessage());
        return new DefaultHttpClient();
    }
}

From source file:org.eclipse.mylyn.internal.commons.http.PollingSslProtocolSocketFactory.java

public PollingSslProtocolSocketFactory() {
    KeyManager[] keymanagers = null;
    if (System.getProperty(KEY_STORE) != null && System.getProperty(KEY_STORE_PASSWORD) != null) {
        try {/*  www  .  j a  v a 2  s . c o  m*/
            String type = System.getProperty(KEY_STORE_TYPE, KeyStore.getDefaultType());
            KeyStore keyStore = KeyStore.getInstance(type);
            char[] password = System.getProperty(KEY_STORE_PASSWORD).toCharArray();
            keyStore.load(new FileInputStream(System.getProperty(KEY_STORE)), password);
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, password);
            keymanagers = keyManagerFactory.getKeyManagers();
        } catch (Exception e) {
            CommonsHttpPlugin.log(IStatus.ERROR, "Could not initialize keystore", e); //$NON-NLS-1$
        }
    }

    hasKeyManager = keymanagers != null;

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL"); //$NON-NLS-1$
        sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null);
        this.socketFactory = sslContext.getSocketFactory();
    } catch (Exception e) {
        CommonsHttpPlugin.log(IStatus.ERROR, "Could not initialize SSL context", e); //$NON-NLS-1$
    }
}

From source file:com.bright.json.JSonRequestor.java

private static HttpClient getNewHttpClient() {
    try {/*from   ww  w  . j  av  a  2 s .  com*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.silverpeas.util.security.SilverpeasX509TrustManager.java

public SilverpeasX509TrustManager(String trustStoreFile, char[] password) {
    InputStream fis = null;//  ww w .  j av  a 2 s. co m
    try {
        KeyStore trustore = KeyStore.getInstance(KeyStore.getDefaultType());
        fis = new FileInputStream(trustStoreFile);
        trustore.load(fis, password);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
        tmf.init(trustore);
        TrustManager tms[] = tmf.getTrustManagers();
        for (TrustManager trustManager : tms) {
            if (trustManager instanceof X509TrustManager) {
                defaultTrustManager = (X509TrustManager) trustManager;
                return;
            }
        }
    } catch (IOException ioex) {
        logger.error("Couldn't load trustore " + trustStoreFile, ioex);
    } catch (GeneralSecurityException secEx) {
        logger.error("Couldn't create trustore " + trustStoreFile, secEx);
    } finally {
        IOUtils.closeQuietly(fis);
    }

}

From source file:com.cloudhopper.httpclient.util.SchemeFactory.java

static public Scheme createHttpsScheme(File keystoreFile, String keystorePassword, File truststoreFile,
        String truststorePassword) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException,
        IOException, KeyManagementException, CertificateException, UnrecoverableKeyException {

    if (keystoreFile == null && truststoreFile == null) {
        // To insure we don't break anything, if keystore and trust store is not specified, 
        // call the legacy createHttpsScheme.
        return createHttpsScheme();
    } else {//from w w w .j  a  v a2  s. co  m
        // Configure https scheme with a keystore to authenticate ourselves to the server
        // and/or a truststore to verify the server's certificate.
        KeyStore keystore = null;
        if (keystoreFile != null) {
            keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream instream = new FileInputStream(keystoreFile);
            try {
                // A null password is valid when the keystore does not have a password.
                if (keystorePassword != null) {
                    keystore.load(instream, keystorePassword.toCharArray());
                } else {
                    keystore.load(instream, null);
                }
            } finally {
                instream.close();
            }

        }
        KeyStore truststore = null;
        if (truststoreFile != null) {
            truststore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream instream = new FileInputStream(truststoreFile);
            try {
                // A null password is valid when the keystore does not have a password.
                if (truststorePassword != null) {
                    truststore.load(instream, truststorePassword.toCharArray());
                } else {
                    truststore.load(instream, null);
                }
            } finally {
                instream.close();
            }
        }
        // Not sure if identifing which params were passed in as null and calling the 
        // appropriate constructor is necessary, because the Apache Docs don't describe
        // what happens when we pass in null. Play it conservative rather than test the
        // behavior. 
        SSLSocketFactory socketFactory;
        if (keystore != null && truststore != null) {
            socketFactory = new SSLSocketFactory(keystore, keystorePassword, truststore);
        } else if (keystore != null) {
            socketFactory = new SSLSocketFactory(keystore, keystorePassword);
        } else {
            socketFactory = new SSLSocketFactory(truststore);
        }
        return new Scheme("https", socketFactory, 443);
    }
}

From source file:org.owasp.goatdroid.herdfinancial.requestresponse.CustomSSLSocketFactory.java

public static HttpClient getNewHttpClient() {
    try {//from  w  w  w.  jav  a  2  s.c o m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClient(byte[] sslCertificateBytes) {
    DefaultHttpClient httpClient;//from   ww  w  .jav  a 2 s .  com
    Certificate[] sslCertificate;

    httpClient = new DefaultHttpClient();
    try {
        sslCertificate = convertByteArrayToCertificate(sslCertificateBytes);

        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:es.uja.photofirma.android.DoConnection.java

/**
 * //from  w  w w  .  j  a  va 2 s  .  c om
 * @return DefaultHttpClient(ccm, params)
 */
public HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        //aado timeout
        HttpConnectionParams.setConnectionTimeout(params, 6000); //timeout en establecer conexion
        HttpConnectionParams.setSoTimeout(params, 10000); //timeout en recibir respuesta

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);

    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.tvs.signaltracker.Utils.java

public static HttpClient getNewHttpClient() {
    try {/*from   w w  w. j av a2s  .  co  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
        //sf.setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:Main.java

/**
 * Returns the local store of reliable server certificates, explicitly accepted by the user.
 * /*from w  ww  . ja  v a 2 s  .c o m*/
 * 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 {
                in.close();
            }
        } else {
            mKnownServersStore.load(null, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); // necessary to initialize an empty KeyStore instance
        }
    }
    return mKnownServersStore;
}