Example usage for java.security KeyStore load

List of usage examples for java.security KeyStore load

Introduction

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

Prototype

public final void load(InputStream stream, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException 

Source Link

Document

Loads this KeyStore from the given input stream.

Usage

From source file:common.rest.client.transport.HttpClientSSLKeyStore.java

private KeyStore loadKeyStore(final InputStream input, final String password) throws Exception {
    final KeyStore keystore = KeyStore.getInstance("jks");
    keystore.load(input, password.toCharArray());
    return keystore;
}

From source file:org.metaeffekt.dcc.shell.RemoteAgentTest.java

private HttpClient newHttpClient() throws GeneralSecurityException, IOException {
    final char[] password = "DYKK8T8m9nKqBRPZ".toCharArray();

    final KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(getClass().getResourceAsStream("/dcc-shell.keystore"), password);

    final KeyStore trustStore = KeyStore.getInstance("JKS");
    trustStore.load(getClass().getResourceAsStream("/dcc-shell.truststore"), password);

    final SSLContextBuilder sslContextBuilder = SSLContexts.custom();
    sslContextBuilder.loadKeyMaterial(keyStore, password);
    sslContextBuilder.loadTrustMaterial(trustStore);

    final HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setSslcontext(sslContextBuilder.build());
    builder.setHostnameVerifier(new AllowAllHostnameVerifier());

    final HttpClient client = builder.build();
    return client;
}

From source file:com.gsf.dowload.nfe.HSProtocolSocketFactory.java

public TrustManager[] createTrustManagers()
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    KeyStore trustStore = KeyStore.getInstance("JKS");

    trustStore.load(new FileInputStream(TRUSTSTORE), "sistec".toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    return trustManagerFactory.getTrustManagers();
}

From source file:org.qi4j.library.http.AbstractSecureJettyTest.java

@Before
public void beforeSecure() throws GeneralSecurityException, IOException {
    // Trust HTTP Client
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());

    AllowAllHostnameVerifier verifier = new AllowAllHostnameVerifier();

    DefaultHttpClient trustClient = new DefaultHttpClient();
    SSLSocketFactory trustSslFactory = new SSLSocketFactory(truststore);
    trustSslFactory.setHostnameVerifier(verifier);
    SchemeRegistry trustSchemeRegistry = trustClient.getConnectionManager().getSchemeRegistry();
    trustSchemeRegistry.unregister(HTTPS);
    trustSchemeRegistry.register(new Scheme(HTTPS, HTTPS_PORT, trustSslFactory));
    trustHttpClient = trustClient;/* w  w  w  .j  av a  2  s  .com*/

    // Mutual HTTP Client
    KeyStore keystore = KeyStore.getInstance("JCEKS");
    keystore.load(new FileInputStream(CLIENT_KEYSTORE_FILE), KS_PASSWORD.toCharArray());

    DefaultHttpClient mutualClient = new DefaultHttpClient();
    SSLSocketFactory mutualSslFactory = new SSLSocketFactory(keystore, KS_PASSWORD, truststore);
    mutualSslFactory.setHostnameVerifier(verifier);
    SchemeRegistry mutualSchemeRegistry = mutualClient.getConnectionManager().getSchemeRegistry();
    mutualSchemeRegistry.unregister(HTTPS);
    mutualSchemeRegistry.register(new Scheme(HTTPS, HTTPS_PORT, mutualSslFactory));
    mutualHttpClient = mutualClient;
}

From source file:com.thoughtworks.go.security.KeyStoreManager.java

private void loadStore(KeyStore store) {
    try {/*from w w w . j  ava 2  s.c  om*/
        store.load(null, null);
    } catch (Exception e) {
        throw bomb(e);
    }
}

From source file:org.kuali.mobility.push.factory.iOSFeedbackConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);/*from   ww w  .j a  v  a  2s  .  c  o m*/
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:org.kuali.mobility.push.factory.iOSConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    SSLSocket socket = null;/*from   w w w . j  ava  2  s .c om*/
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:nu.yona.server.AppServiceApplication.java

@Bean
@Qualifier("appleMobileConfigSigningKeyStore")
public KeyStore appleMobileConfigSigningKeyStore() {
    String fileName = yonaProperties.getAppleMobileConfig().getSigningKeyStoreFile();
    logger.info("Loading Apple mobile config signing key store from {}", fileName);
    try (InputStream inStream = new FileInputStream(fileName)) {
        KeyStore keyStore = java.security.KeyStore.getInstance("PKCS12");
        keyStore.load(inStream,
                yonaProperties.getAppleMobileConfig().getSigningKeyStorePassword().toCharArray());
        assertKeyStoreContent(keyStore);
        return keyStore;
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
        throw YonaException.unexpected(e);
    }//  www .j  a v a2s  .  c  o m
}

From source file:KeystoreGeneratorTest.java

@Test
public void test() throws Exception {
    File dir = null;// w ww.  j  a v  a2s .co m
    FileInputStream fis = null;
    try {
        dir = Files.createTempDir();
        File keystoreFile = new File(dir, KEYSTORE_NAME);

        String config = GSON.toJson(ImmutableMap.builder().put("password", KEYSTORE_PASSWORD)
                .put("entries", ImmutableList.builder()
                        .add(ImmutableMap.builder().put("label", "rsatest1").put("algorithm", "SHA256WithRSA")
                                .put("keyAlgorithm", "RSA").put("rsaKeySize", "2048").build())
                        .add(ImmutableMap.builder().put("label", "ecdsatest1")
                                .put("algorithm", "SHA256WithECDSA").put("keyAlgorithm", "ECDSA")
                                .put("ecdsaNamedCurve", "secp192r1").build())
                        .add(ImmutableMap.builder().put("label", "ecdsatest2")
                                .put("algorithm", "SHA256WithECDSA").put("keyAlgorithm", "ECDSA")
                                .put("ecdsaNamedCurve", "secp256r1").build())
                        .build())
                .build());
        // generate
        KeyStore store = new KeystoreGenerator().generate(GSON.fromJson(config, KeystoreConfig.class));
        // write to disk
        try (FileOutputStream out = new FileOutputStream(keystoreFile)) {
            store.store(out, KEYSTORE_PASSWORD.toCharArray());
        }
        // load
        fis = new FileInputStream(keystoreFile);
        KeyStore ks = KeyStore.getInstance("PKCS12", "SunJSSE");
        ks.load(fis, KEYSTORE_PASSWORD.toCharArray());
        Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            String al = aliases.nextElement();
            System.out.println("Label: [" + al + "]");
            X509Certificate cert = (X509Certificate) ks.getCertificate(al);
            System.out.println("  Algorithm: [" + cert.getSigAlgName() + "]");
            PublicKey key = cert.getPublicKey();
            if (key instanceof ECKey) {
                ECKey eckey = (ECKey) key;
                ECParameterSpec spec = eckey.getParams();
                System.out.println("  EC spec: [" + spec + "]");
            }
        }
    } finally {
        closeQuietly(fis);
        FileUtils.deleteDirectory(dir);
    }
}

From source file:org.elasticsearch.xpack.ssl.SSLClientAuthTests.java

private SSLContext getSSLContext() {
    try (InputStream in = Files.newInputStream(
            getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks"))) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(in, "testclient".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);//from w  w  w. j  a v a 2 s  .c  o m
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, "testclient".toCharArray());
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
    }
}