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:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLSocketFactory.java

/**
 *
 * @param sslCerts these certs will be used to validate the ssl connection
 * @return/*from w w  w  . ja v a 2s. c o m*/
 * @throws IOException
 * @throws GeneralSecurityException
 */
static public SSLSocketFactory getSSLSocketFactory(List<X509Certificate> sslCerts)
        throws IOException, GeneralSecurityException {
    SSLContext sc = null;
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null); //Important, this intializes the keystore
    int counter = 0;
    for (X509Certificate cert : sslCerts) {
        ks.setCertificateEntry("rhn-key-" + counter, cert);
        counter++;
    }
    sc = SSLContext.getInstance("SSL");
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    sc.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
    return sc.getSocketFactory();
}

From source file:com.sh.util.SslHttpClientFactoryBean.java

@Override
public HttpClient getObject() throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustStrategy allTrust = new TrustStrategy() {
        @Override//from www .j ava  2 s  .com
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };
    SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

From source file:fi.iki.dezgeg.matkakorttiwidget.matkakortti.NonverifyingSSLSocketFactory.java

public static AbstractHttpClient createNonverifyingHttpClient() throws Exception {

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);// w  ww  . j  av  a  2 s.  c o  m

    SSLSocketFactory sf = new NonverifyingSSLSocketFactory(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);
}

From source file:org.exem.flamingo.shared.util.SslHttpClientFactoryBean.java

@Override
public HttpClient getObject() throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustStrategy allTrust = new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }/*from w w w .  j  a v  a2 s  . com*/
    };
    SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

From source file:org.apache.abdera2.common.security.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());/*from   w w  w.j ava  2  s.  co  m*/
    in.close();
    return ks;
}

From source file:com.yodlee.sampleapps.helper.OpenSamlHelper.java

/**
 * Initilize the Keystore.//ww w  .  j  av  a 2s  .c o m
 */
private static void initKeyStore() {
    InputStream fileInput = null;
    try {
        fileInput = new FileInputStream(keystoreFilename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    KeyStore keystore = null;
    try {
        keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(fileInput, keystorePassword.toCharArray());
        privateKey = (PrivateKey) keystore.getKey(keystoreAlias, keystorePassword.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }

    if (privateKey == null)
        throw new RuntimeException(keystoreAlias + " key not found in keystore " + keystoreFilename);

    X509Certificate cert = null;
    Certificate[] certificates = new Certificate[0];
    try {
        cert = (X509Certificate) keystore.getCertificate(keystoreAlias);
        certificates = keystore.getCertificateChain(keystoreAlias);
    } catch (KeyStoreException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    if (cert == null)
        throw new RuntimeException(keystoreAlias + " cert not found in keystore " + keystoreFilename);

    if (certificates == null)
        throw new RuntimeException(keystoreAlias + " cert chain not found in keystore " + keystoreFilename);

    certs = new X509Certificate[certificates.length];
    System.arraycopy(certificates, 0, certs, 0, certs.length);
}

From source file:org.gluu.oxtrust.util.EasyCASSLProtocolSocketFactory.java

protected SSLContext createEasySSLContext(ApplicationConfiguration applicationConfiguration) {
    try {/*from ww  w  .  ja va  2s  .c o  m*/

        String password = applicationConfiguration.getCaCertsPassphrase();
        char[] passphrase = null;
        if (password != null) {
            passphrase = StringEncrypter.defaultInstance().decrypt(password, cryptoConfigurationSalt)
                    .toCharArray();
        }
        KeyStore cacerts = null;
        String cacertsFN = applicationConfiguration.getCaCertsLocation();
        if (cacertsFN != null) {
            cacerts = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream cacertsFile = new FileInputStream(cacertsFN);
            cacerts.load(cacertsFile, passphrase);
            cacertsFile.close();
        }

        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(cacerts) }, null);
        return context;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:org.changhong.sync.web.MySSLSocketFactory.java

public static DefaultHttpClient getNewHttpClient() {
    try {//from w  w w .j  a  v a2s .co 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);

        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:br.gov.frameworkdemoiselle.behave.integration.alm.httpsclient.HttpsClient.java

public static HttpClient getNewHttpClient(String encoding) {
    try {//  w w  w.j  a v 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, encoding);

        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:net.di2e.ecdr.source.rest.TLSUtil.java

public static void setTLSOptions(WebClient client, boolean disableCNCheck) {
    ClientConfiguration clientConfiguration = WebClient.getConfig(client);

    HTTPConduit httpConduit = clientConfiguration.getHttpConduit();

    String keyStorePath = System.getProperty(SSL_KEYSTORE_JAVA_PROPERTY);
    String keyStorePassword = System.getProperty(SSL_KEYSTORE_PASSWORD_JAVA_PROPERTY);
    if (StringUtils.isNotBlank(keyStorePath) && StringUtils.isNotBlank(keyStorePassword)) {
        try {/*ww w  .  ja  v  a 2 s.c o  m*/
            TLSClientParameters tlsParams = new TLSClientParameters();
            LOGGER.debug("Setting disable of CN check on client URL {} to [{}]", client.getCurrentURI(),
                    disableCNCheck);
            tlsParams.setDisableCNCheck(disableCNCheck);

            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

            // add the keystore if it exists
            File keystore = new File(keyStorePath);
            if (keystore.exists() && keyStorePassword != null) {
                FileInputStream fis = new FileInputStream(keystore);
                try {
                    LOGGER.debug("Loading keyStore {}", keystore);
                    keyStore.load(fis, keyStorePassword.toCharArray());
                } catch (IOException e) {
                    LOGGER.error("Unable to load keystore. {}", keystore, e);
                } catch (CertificateException e) {
                    LOGGER.error("Unable to load certificates from keystore. {}", keystore, e);
                } finally {
                    IOUtils.closeQuietly(fis);
                }
                KeyManagerFactory keyFactory = KeyManagerFactory
                        .getInstance(KeyManagerFactory.getDefaultAlgorithm());
                keyFactory.init(keyStore, keyStorePassword.toCharArray());
                KeyManager[] km = keyFactory.getKeyManagers();
                tlsParams.setKeyManagers(km);
            }

            httpConduit.setTlsClientParameters(tlsParams);
        } catch (KeyStoreException e) {
            LOGGER.error("Unable to read keystore: ", e);
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("Problems creating SSL socket. Usually this is "
                    + "referring to the certificate sent by the server not being trusted by the client.", e);
        } catch (FileNotFoundException e) {
            LOGGER.error("Unable to locate one of the SSL stores: {} | {}", keyStorePath, e);
        } catch (UnrecoverableKeyException e) {
            LOGGER.error("Unable to read keystore: ", e);
        }
    }
}