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.wso2.carbon.mss.security.JWTSecurityInterceptor.java

private PublicKey getPublicKey(String keyStorePath, String keyStorePassword, String alias) throws IOException,
        KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException {

    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(keyStorePath)) {
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(inputStream, keyStorePassword.toCharArray());

        Key key = keystore.getKey(alias, keyStorePassword.toCharArray());
        if (key instanceof PrivateKey) {
            // Get certificate of public key
            java.security.cert.Certificate cert = keystore.getCertificate(alias);

            // Get public key
            return cert.getPublicKey();
        }/*from   w  w  w  . j  av a 2 s.  c  o m*/
    }
    return null;
}

From source file:cn.com.loopj.android.http.MySSLSocketFactory.java

/**
 * Gets a Default KeyStore/*from  www  .  j  a va2  s. co m*/
 *
 * @return KeyStore
 */
public static KeyStore getKeystore() {
    KeyStore trustStore = null;
    try {
        trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return trustStore;
}

From source file:org.sonatype.nexus.testsuite.NexusHttpsITSupport.java

/**
 * @return Client trust store containing exported Nexus certificate
 *///  ww w .j  ava  2  s  .  com
protected KeyStore trustStore() throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (FileInputStream instream = new FileInputStream(
            resolveBaseFile("src/test/it-resources/ssl/client.jks"))) {
        trustStore.load(instream, "password".toCharArray());
    }
    return trustStore;
}

From source file:at.diamonddogs.net.SSLHelper.java

private void makeAllTrustManagerForApache() throws KeyStoreException, NoSuchAlgorithmException,
        CertificateException, IOException, KeyManagementException, UnrecoverableKeyException {
    KeyStore store;//from w  w  w .jav a 2 s  .  com
    store = KeyStore.getInstance(KeyStore.getDefaultType());
    store.load(null, null);
    SSL_FACTORY_APACHE = new AllTrustingApacheSSLFactory(null);
}

From source file:com.peopleapi.RegisterWithApi.java

private DefaultHttpClient getNewHttpClient() {
    //I mocked out a key store, you will want to generate a real store. this is for testing only!
    try {//from w  w  w .j  a v  a  2  s. co  m
        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.aliyun.api.gateway.demo.Client.java

/**
 * <br>/* w ww  . j a  va 2 s. c o m*/
 * Client?httpsURL?keystore?storePasswordkeystore??? 
 * <a href="http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html">keytool</a>
 * 
 * @param appKey
 *            APP Key?APIAPP?
 * @param appSecret
 *            APP?APIAPP?
 * @param testEnv
 *            ?truefalse
 */
public Client(String appKey, String appSecret, boolean testEnv) {
    HttpClientBuilder builder = HttpClients.custom();
    try {
        SSLContext sslContext = null;
        if (testEnv) {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    //truetrue
                    return true;
                }
            }).build();
        } else {
            //keytool?keystorekeystore
            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(null, null);
            sslContext = SSLContexts.custom().loadTrustMaterial(ks, new TrustSelfSignedStrategy()).build();
        }
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" },
                null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        builder.setSSLSocketFactory(sslsf);
    } catch (KeyStoreException | KeyManagementException | NoSuchAlgorithmException | CertificateException
            | IOException e) {
        log.error(e.getMessage(), e);
    }
    httpClient = builder.setUserAgent(Constants.USER_AGENT).build();
    this.appKey = appKey;
    this.appSecret = appSecret;
    this.testEnv = testEnv;
}

From source file:com.jonbanjo.cupsprint.CertificateActivity.java

private KeyStore loadTrustStore() {
    KeyStore ts = null;//from  w ww . j a v a 2  s.co m

    try {
        ts = KeyStore.getInstance(KeyStore.getDefaultType());
    } catch (Exception e) {
        System.out.println(e.toString());
        return null;
    }

    FileInputStream fis = null;
    try {
        fis = openFileInput(JfSSLScheme.trustfile);
        ts.load(fis, JfSSLScheme.password.toCharArray());
    } catch (Exception e) {
        try {
            ts.load(null, JfSSLScheme.password.toCharArray());
        } catch (Exception e1) {
            System.out.println(e.toString());
            return null;
        }
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e1) {
            }
        }
    }
    return ts;
}

From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java

private KeyStore getKeyStore() throws KeyStoreException {
    return KeyStore.getInstance(KeyStore.getDefaultType());
}

From source file:org.thingsboard.rule.engine.mqtt.credentials.CertPemClientCredentials.java

private TrustManagerFactory createAndInitTrustManagerFactory() throws Exception {
    X509Certificate caCertHolder;
    caCertHolder = readCertFile(caCert);

    KeyStore caKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    caKeyStore.load(null, null);/*from w w w. jav a  2s.  co m*/
    caKeyStore.setCertificateEntry("caCert-cert", caCertHolder);

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(caKeyStore);
    return trustManagerFactory;
}

From source file:org.springframework.cloud.vault.ClientHttpRequestFactoryFactory.java

private static TrustManagerFactory createTrustManagerFactory(Resource trustFile, String storePassword)
        throws GeneralSecurityException, IOException {

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

    try (InputStream inputStream = trustFile.getInputStream()) {
        trustStore.load(inputStream, StringUtils.hasText(storePassword) ? storePassword.toCharArray() : null);
    }//  w  w w .  j  a v a 2  s .  c  o m

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);

    return trustManagerFactory;
}