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.android.providers.downloads.ui.network.SslSocketFactory.java

private static KeyStore createKeyStore(InputStream keyStore, String password)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException {
    KeyStore key = KeyStore.getInstance(KeyStore.getDefaultType());
    try {/*from  w w w.  j  a v a2  s. c om*/
        key.load(keyStore, password.toCharArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        try {
            key.load(null, null);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    return key;
}

From source file:ru.elifantiev.yandex.SSLHttpClientFactory.java

public static HttpClient getNewHttpClient() {
    try {/*w w  w  . j a  v  a  2s.  co  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new YandexSSLSocketFactory(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

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;//w ww. j a  v  a 2 s . c o m
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}

From source file:be.dnsbelgium.rdap.client.RDAPClient.java

public static KeyStore getKeyStoreFromFile(File file, String password) throws KeyStoreException {
    return getKeyStoreFromFile(file, KeyStore.getDefaultType(), password);
}

From source file:ro.nextreports.designer.util.KeyStoreUtil.java

public static void setKeystore() {
    File file = new File(KEYSTORE_FILE);
    if (!file.exists()) {
        OutputStream out = null;/*  w  ww .ja v a2  s. c o m*/
        ;
        try {
            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(null, KEYSTORE_PASS.toCharArray());
            out = new FileOutputStream(KEYSTORE_FILE);
            ks.store(out, KEYSTORE_PASS.toCharArray());
        } catch (Exception e) {
            LOG.error("Could not create keystore file : " + KEYSTORE_FILE, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        }
    }
    System.setProperty("javax.net.ssl.trustStore", KEYSTORE_FILE);
}

From source file:org.commonjava.maven.galley.transport.htcli.internal.SSLUtils.java

public static KeyStore readKeyAndCert(final String pemContent, final String keyPass)
        throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException,
        InvalidKeySpecException {
    final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null);/*www .j  a va  2s.  c o  m*/

    final CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    final KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    final List<String> lines = readLines(pemContent);

    String currentHeader = null;
    final StringBuilder current = new StringBuilder();
    final Map<String, String> entries = new LinkedHashMap<String, String>();
    for (final String line : lines) {
        if (line == null) {
            continue;
        }

        if (line.startsWith("-----BEGIN")) {
            currentHeader = line.trim();
            current.setLength(0);
        } else if (line.startsWith("-----END")) {
            entries.put(currentHeader, current.toString());
        } else {
            current.append(line.trim());
        }
    }

    final List<Certificate> certs = new ArrayList<Certificate>();
    for (int pass = 0; pass < 2; pass++) {
        for (final Map.Entry<String, String> entry : entries.entrySet()) {
            final String header = entry.getKey();
            final byte[] data = decodeBase64(entry.getValue());

            if (pass > 0 && header.contains("BEGIN PRIVATE KEY")) {
                final KeySpec spec = new PKCS8EncodedKeySpec(data);
                final PrivateKey key = keyFactory.generatePrivate(spec);
                ks.setKeyEntry("key", key, keyPass.toCharArray(), certs.toArray(new Certificate[] {}));
            } else if (pass < 1 && header.contains("BEGIN CERTIFICATE")) {
                final Certificate c = certFactory.generateCertificate(new ByteArrayInputStream(data));

                ks.setCertificateEntry("certificate", c);
                certs.add(c);
            }
        }
    }

    return ks;
}

From source file:Main.java

/**
 * Generate a SSLSocketFactory wich checks the certificate given
 * @param context Context to use// w ww. j  a v  a  2 s.  c  om
 * @param rResource int with url of the resource to read the certificate
 * @parma password String to use with certificate
 * @return SSLSocketFactory generated to validate this certificate
 */
public static SSLSocketFactory newSslSocketFactory(Context context, int rResource, String password)
        throws CertificateException, NoSuchProviderException, KeyStoreException, NoSuchAlgorithmException,
        IOException, UnrecoverableKeyException, KeyManagementException {

    // Get an instance of the Bouncy Castle KeyStore format
    KeyStore trusted = KeyStore.getInstance("BKS");
    // Get the raw resource, which contains the keystore with
    // your trusted certificates (root and any intermediate certs)
    InputStream is = context.getApplicationContext().getResources().openRawResource(rResource);

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
    X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is);
    String alias = "alias";//cert.getSubjectX500Principal().getName();

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);
    trustStore.setCertificateEntry(alias, cert);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    kmf.init(trustStore, null);
    KeyManager[] keyManagers = kmf.getKeyManagers();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    return sslContext.getSocketFactory();

}

From source file:org.cvasilak.jboss.mobile.admin.net.ssl.CustomHTTPClient.java

public static synchronized AbstractHttpClient getHttpClient() {
    try {/*from   w w  w. jav  a 2 s .  c o m*/
        if (client == null) {
            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);

            client = new DefaultHttpClient(ccm, params);
        }
    } catch (Exception e) {
        Log.d(TAG, "unable to create http client", e);
    }

    return client;
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static KeyStore loadKeystore(String file, String pass)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
    if (in == null)
        in = new FileInputStream(file);
    ks.load(in, pass.toCharArray());//  w  w w .  jav a2 s. c  om
    return ks;
}

From source file:com.redwoodsystems.android.apps.utils.HttpUtil.java

public static HttpClient getNewHttpClient() {
    try {//w ww.  j  av  a2s  .c o m
        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);

        ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);

        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();
    }
}