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:at.univie.sensorium.extinterfaces.HTTPSUploader.java

public HttpClient getNewHttpClient() {
    try {/*from w  w w.j a  v  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();
        int timeout = 10 * 1000;
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
        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.floragunn.searchguard.test.helper.rest.RestHelper.java

protected final CloseableHttpClient getHTTPClient() throws Exception {

    final HttpClientBuilder hcb = HttpClients.custom();

    if (enableHTTPClientSSL) {

        log.debug("Configure HTTP client with SSL");

        final KeyStore myTrustStore = KeyStore.getInstance("JKS");
        myTrustStore.load(new FileInputStream(FileHelper.getAbsoluteFilePathFromClassPath(truststore)),
                "changeit".toCharArray());

        final KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(new FileInputStream(FileHelper.getAbsoluteFilePathFromClassPath(keystore)),
                "changeit".toCharArray());

        final SSLContextBuilder sslContextbBuilder = SSLContexts.custom().useTLS();

        if (trustHTTPServerCertificate) {
            sslContextbBuilder.loadTrustMaterial(myTrustStore);
        }// w  w w . j  a  v  a  2 s.c o  m

        if (sendHTTPClientCertificate) {
            sslContextbBuilder.loadKeyMaterial(keyStore, "changeit".toCharArray());
        }

        final SSLContext sslContext = sslContextbBuilder.build();

        String[] protocols = null;

        if (enableHTTPClientSSLv3Only) {
            protocols = new String[] { "SSLv3" };
        } else {
            protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
        }

        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, protocols, null,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        hcb.setSSLSocketFactory(sslsf);
    }

    hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(60 * 1000).build());

    return hcb.build();
}

From source file:com.github.technosf.posterer.models.impl.KeyStoreBeanTest.java

/**
 * Create clean temp key store files and ensure we can access the main test
 * key store file/*from w  ww  .j av a2  s  . c  o  m*/
 */
@BeforeClass
private void init() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    // Delete preexisting testing keystores
    FileUtils.deleteQuietly(FileUtils.getFile(missingKeyStore));
    FileUtils.deleteQuietly(FileUtils.getFile(unknownKeyStore));
    FileUtils.deleteQuietly(FileUtils.getFile(emptyKeyStore));

    // Get the keystore algo and create the ks in memory
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, passwordchr);

    // Write out unknown pw keystore
    FileOutputStream fos = new FileOutputStream(unknownKeyStore);
    ks.store(fos, "unknownpw".toCharArray());
    fos.close();
    assertFalse(FileUtils.getFile(missingKeyStore).exists());

    // Write out empty key store
    fos = new FileOutputStream(emptyKeyStore);
    ks.store(fos, passwordchr);
    fos.close();
    assertFalse(FileUtils.getFile(missingKeyStore).exists());

    // Check the main test key store
    URL testKeystoreURL = this.getClass().getResource("/testkeystore.jks");
    testKeyStoreFile = FileUtils.toFile(testKeystoreURL);
    assertNotNull(testKeyStoreFile);
}

From source file:com.rsmart.kuali.kfs.sys.context.PropertyLoadingFactoryBean.java

/**
 * Decrypts encrypted values in properties. Interprets that any property in the {@link Properties} instance
 * provided with a key ending with the {@code ENCRYPTED_PROPERTY_EXTENSION} is considered to be encrypted.
 * It is then decrypted and replaced with a key of the same name only using the {@code PASSWORD_PROPERTY_EXTENSION}
 * //  w  w w . j av a 2 s.  co  m
 * @param props the {@link Properties} to decrypt
 * @throws {@link Exception} if there's any problem decrypting/encrypting properties.
 */
protected void decryptProps(final Properties props) throws Exception {
    final String keystore = props.getProperty(KEYSTORE_LOCATION_PROPERTY);
    final String storepass = props.getProperty(KEYSTORE_PASSWORD_PROPERTY);
    final FileInputStream fs = new FileInputStream(keystore);
    final KeyStore jks = KeyStore.getInstance(KEYSTORE_TYPE);
    jks.load(fs, storepass.toCharArray());
    fs.close();

    final Cipher cipher = Cipher.getInstance(ENCRYPTION_STRATEGY);
    cipher.init(Cipher.DECRYPT_MODE, (PrivateKey) jks.getKey(RICE_RSA_KEY_NAME, storepass.toCharArray()));

    for (final String key : props.stringPropertyNames()) {
        if (key.endsWith(ENCRYPTED_PROPERTY_EXTENSION)) {
            final String prefix = key.substring(0, key.indexOf(ENCRYPTED_PROPERTY_EXTENSION));
            final String encrypted_str = props.getProperty(key);
            props.setProperty(prefix + PASSWORD_PROPERTY_EXTENSION,
                    new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(encrypted_str))));
        }
    }

}