Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements Keystore and Truststore supports in Httpclient new impleme… #739

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions java/src/main/java/com/genexus/internet/HttpClientJavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.*;
import com.genexus.ModelContext;
import com.genexus.util.IniFile;
Expand All @@ -16,6 +18,7 @@
import com.genexus.specific.java.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.protocol.HttpContext;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
Expand Down Expand Up @@ -227,16 +230,28 @@ private String getURLValid(String url) {

private static SSLConnectionSocketFactory getSSLSecureInstance() {
try {
SSLContext sslContext = SSLContextBuilder
SSLContextBuilder sslContextBuilder = SSLContextBuilder
.create()
.loadTrustMaterial(new TrustSelfSignedStrategy())
.build();
.loadTrustMaterial(new TrustSelfSignedStrategy());

String pathToKeystore = System.getProperty("javax.net.ssl.keyStore");
String keystorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
if (pathToKeystore != null && keystorePassword != null)
sslContextBuilder.loadKeyMaterial(new File(pathToKeystore), keystorePassword.toCharArray(), keystorePassword.toCharArray());

String pathToTruststore = System.getProperty("javax.net.ssl.trustStore");
String truststorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
if (pathToTruststore != null && truststorePassword != null)
sslContextBuilder.loadTrustMaterial(new File(pathToTruststore), truststorePassword.toCharArray());

SSLContext sslContext = sslContextBuilder.build();

return new SSLConnectionSocketFactory(
sslContext,
new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
NoopHostnameVerifier.INSTANCE);
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException | CertificateException | IOException e) {
e.printStackTrace();
}
return new SSLConnectionSocketFactory(
Expand Down