Java Code Examples for org.apache.http.conn.ssl.SSLContextBuilder#loadKeyMaterial()

The following examples show how to use org.apache.http.conn.ssl.SSLContextBuilder#loadKeyMaterial() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostHTTP.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
private SSLContext createSSLContext(final SSLContextService service)
        throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException {
    SSLContextBuilder builder = SSLContexts.custom();
    final String trustFilename = service.getTrustStoreFile();
    if (trustFilename != null) {
        final KeyStore truststore = KeyStoreUtils.getTrustStore(service.getTrustStoreType());
        try (final InputStream in = new FileInputStream(new File(service.getTrustStoreFile()))) {
            truststore.load(in, service.getTrustStorePassword().toCharArray());
        }
        builder = builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
    }

    final String keyFilename = service.getKeyStoreFile();
    if (keyFilename != null) {
        final KeyStore keystore = KeyStoreUtils.getKeyStore(service.getKeyStoreType());
        try (final InputStream in = new FileInputStream(new File(service.getKeyStoreFile()))) {
            keystore.load(in, service.getKeyStorePassword().toCharArray());
        }
        builder = builder.loadKeyMaterial(keystore, service.getKeyStorePassword().toCharArray());
    }

    builder = builder.useProtocol(service.getSslAlgorithm());

    final SSLContext sslContext = builder.build();
    return sslContext;
}
 
Example 2
Source File: SSLConnectionSocketFactoryBuilder.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
protected void createKeystore(SSLContextBuilder sslContextBuilder, boolean useClientCert) {
    if (useClientCert) {
        KeyStore clientKeyStore;
        try {
            clientKeyStore = createKeyStore(new URL(keystore), keystorePassword);
            sslContextBuilder.loadKeyMaterial(clientKeyStore, keystorePassword.toCharArray());
        } catch (UnrecoverableKeyException | IOException ue) {
            throw new IllegalArgumentException(ue.getMessage() + ". " + BAD_KEYSTORE_ERROR, ue);
        } catch (GeneralSecurityException gse) {
            throw new IllegalArgumentException(gse.getMessage() + ". " + INVALID_KEYSTORE_ERROR, gse);
        }
    }
}