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:org.openo.nfvo.vnfmadapter.service.csm.connect.AbstractSslContext.java

protected static TrustManager[] createTrustManager(JSONObject sslConf) {
    TrustManager[] tms = null;//from w w w . j  av a2s.c om
    try {

        String TRUST_STORE = "etc/conf/trust.jks";
        String TRUST_STORE_PASSWORD = "Changeme_123";
        String TRUST_STORE_TYPE = "jks";
        if (sslConf != null) {
            TRUST_STORE = sslConf.getString("trustStore");
            TRUST_STORE_PASSWORD = sslConf.getString("trustStorePass");
            TRUST_STORE_TYPE = sslConf.getString("trustStoreType");
        }
        FileInputStream f_trustStore = new FileInputStream(TRUST_STORE);
        KeyStore ks = KeyStore.getInstance(TRUST_STORE_TYPE);
        ks.load(f_trustStore, TRUST_STORE_PASSWORD.toCharArray());
        f_trustStore.close();

        String alg = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
        tmFact.init(ks);
        tms = tmFact.getTrustManagers();

    } catch (Exception e) {
        LOG.error("create TrustManager fail!", e);
    }
    return tms;
}

From source file:com.microsoft.aad.adal4j.MSCAPIAsymmetricKeyCredential.java

/**
 * Static method to create KeyCredential instance.
 * /*from   ww  w  . ja  v a  2s . com*/
 * @param clientId
 *            Identifier of the client requesting the token.
 * @param pkcs12Certificate
 *            PKCS12 certificate stream containing public and private key.
 *            Caller is responsible to handling the inputstream.
 * @param password
 *            certificate password
 * @return KeyCredential instance
 * @throws KeyStoreException
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws UnrecoverableKeyException
 */
public static MSCAPIAsymmetricKeyCredential create(final String clientId, final InputStream pkcs12Certificate,
        final String password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException,
        CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
    final KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
    keystore.load(pkcs12Certificate, password.toCharArray());
    final Enumeration<String> aliases = keystore.aliases();
    final String alias = aliases.nextElement();
    final PrivateKey key = (PrivateKey) keystore.getKey(alias, password.toCharArray());
    final X509Certificate publicCertificate = (X509Certificate) keystore.getCertificate(alias);
    return create(clientId, key, publicCertificate);
}

From source file:com.jonbanjo.ssl.JfSSLScheme.java

public static Scheme getScheme() {

    FileInputStream fis = null;//  w  ww . j  a  va2 s .  c  o  m
    Scheme scheme;

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

        try {
            fis = CupsPrintApp.getContext().openFileInput(trustfile);
            trustStore.load(fis, password.toCharArray());
        } catch (Exception e) {
            trustStore.load(null, null);
        }

        SSLSocketFactory sf = new AdditionalKeyStoresSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        scheme = new Scheme("https", sf, 443);
    } catch (Exception e) {
        scheme = getDefaultScheme();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e1) {
            }
        }
    }
    return scheme;
}

From source file:keywhiz.TestClients.java

private static KeyStore keyStoreFromResource(String path, String password) {
    KeyStore keyStore;
    try (InputStream stream = Resources.getResource(path).openStream()) {
        keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(stream, password.toCharArray());
    } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException e) {
        throw new AssertionError(e);
    }/*from   w w w. j  a  v a 2  s  .c  om*/
    return keyStore;
}

From source file:org.apache.hadoop.gateway.jetty.JettyHttpsTest.java

private static KeyStore loadKeyStore(String type, String path, String password)
        throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException {
    KeyStore keyStore = KeyStore.getInstance(type);
    InputStream keystoreInput = new FileInputStream(path);
    keyStore.load(keystoreInput, password.toCharArray());
    return keyStore;
}

From source file:com.mani.fileupload.http.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//  w w  w .  j  av  a2s. c  o  m

        // Client should send the valid key to Server 
        InputStream clientStream = null;
        char[] password = null;

        clientStream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.client);
        password = "fileupload".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // CA key obtained from server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.ca);

        try {
            trustStore.load(instream, "casecret".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

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

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:com.tlabs.eve.HttpClientTest.java

@BeforeClass
public static final void setHttpClient() throws Exception {
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    //FIXME check about the deprecated
    try {/*w ww .  java  2s  .c om*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sslf = new NoCheckSSLSocketFactory(trustStore);
        sslf.setHostnameVerifier(new AllowAllHostnameVerifier());
        schemeRegistry.register(new Scheme("https", sslf, 443));
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    connectionManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
}

From source file:io.hops.hopsworks.util.CertificateHelper.java

private static KeyStore keystore(InputStream is, String certPswd) throws IllegalStateException {
    try {//from w  ww  .  jav a 2  s  . c  o  m
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(is, certPswd.toCharArray());
        return keystore;
    } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex) {
        LOG.log(Level.SEVERE, "keystore ex. {0}", ex);
        throw new IllegalStateException("keystore ex", ex);
    }
}

From source file:com.rumblefish.friendlymusic.api.WebRequest.java

public static HttpClient getNewHttpClient(int timelimit) {
    try {/*from w w w . ja va2s . c  om*/
        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);
        HttpConnectionParams.setConnectionTimeout(params, timelimit);
        HttpConnectionParams.setSoTimeout(params, timelimit);

        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) {
        e.printStackTrace();
        return new DefaultHttpClient();
    }
}

From source file:no.difi.sdp.client.ObjectMother.java

public static Noekkelpar noekkelpar() {
    try {//w  w w  . j  a  v  a 2s  . co m
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(new ClassPathResource("/selfsigned-keystore.jks").getInputStream(),
                "password1234".toCharArray());
        return Noekkelpar.fraKeyStore(keyStore, "avsender", "password1234");
    } catch (Exception e) {
        throw new RuntimeException("Kunne ikke laste keystore", e);
    }
}