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

added new set and get for ssl context #865

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/main/java/org/htmlunit/WebClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import org.apache.commons.io.FileUtils;

import javax.net.ssl.SSLContext;

/**
* Represents options of a {@link WebClient}.
*
Expand Down Expand Up @@ -65,6 +67,7 @@ public class WebClientOptions implements Serializable {

private boolean useInsecureSSL_; // default is secure SSL
private String sslInsecureProtocol_;
private SSLContext sslContext_;

private boolean fileProtocolForXMLHttpRequestsAllowed_;

Expand Down Expand Up @@ -515,6 +518,23 @@ public String getSSLInsecureProtocol() {
return sslInsecureProtocol_;
}

/**
* Sets the SSL Context, used only when {@link #setUseInsecureSSL(boolean)} is set to {@code true}.
* @param sslContext the SSL Context for insecure SSL connections,
* {@code null} to use for default value
*/
public void setSSLContext(final SSLContext sslContext) {
sslContext_ = sslContext;
}

/**
* Gets the SSL Context, to be used only when {@link #setUseInsecureSSL(boolean)} is set to {@code true}.
* @return the SSL Context for insecure SSL connections
*/
public SSLContext getSSLContext() {
return sslContext_;
}

/**
* Sets the SSL server certificate trust store. All server certificates will be validated against
* this trust store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ public static SSLConnectionSocketFactory buildSSLSocketFactory(final WebClientOp
sslClientProtocols, sslClientCipherSuites);
}

// we need insecure SSL + SOCKS awareness
String protocol = options.getSSLInsecureProtocol();
if (protocol == null) {
protocol = "SSL";
SSLContext sslContext = options.getSSLContext();
if (sslContext == null) {
// we need insecure SSL + SOCKS awareness
String protocol = options.getSSLInsecureProtocol();
if (protocol == null) {
protocol = "SSL";
}

sslContext = SSLContext.getInstance(protocol);
sslContext.init(getKeyManagers(options),
new X509ExtendedTrustManager[]{new InsecureTrustManager()}, null);
}
final SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(getKeyManagers(options), new X509ExtendedTrustManager[] {new InsecureTrustManager()}, null);

return new HtmlUnitSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE,
useInsecureSSL, sslClientProtocols, sslClientCipherSuites);
Expand Down