Skip to content

Commit

Permalink
Add configurations to add SNI host name
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhashinee authored and TharmiganK committed Dec 5, 2024
1 parent cfb4c7e commit 780ea9a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ballerina-tests/http-security-tests/tests/http2_ssl_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ http:ClientConfiguration http2SslClientConf1 = {
cert: {
path: common:TRUSTSTORE_PATH,
password: "ballerina"
}
},
sniHostName: "localhost2"
}
};

Expand Down
Empty file.
2 changes: 2 additions & 0 deletions ballerina/http_client_config.bal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public type RetryConfig record {|
# + shareSession - Enable/disable new SSL session creation
# + handshakeTimeout - SSL handshake time out
# + sessionTimeout - SSL session time out
# + sniHostName - Server name indication(SNI) to be used. If this is not present, hostname from the target URL will be used
public type ClientSecureSocket record {|
boolean enable = true;
crypto:TrustStore|string cert?;
Expand All @@ -114,6 +115,7 @@ public type ClientSecureSocket record {|
boolean shareSession = true;
decimal handshakeTimeout?;
decimal sessionTimeout?;
string sniHostName?;
|};

# Provides configurations for controlling the endpoint's behaviour in response to HTTP redirect related responses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,11 @@ private static void evaluateCommonFields(BMap<BString, Object> secureSocket, Ssl
Parameter enableSessionCreationParam = new Parameter(HttpConstants.SECURESOCKET_CONFIG_SHARE_SESSION.getValue(),
enableSessionCreation);
paramList.add(enableSessionCreationParam);
if (secureSocket.containsKey(HttpConstants.SECURESOCKET_CONFIG_SNI_HOST_NAME)) {
Parameter sniHostNameParam = new Parameter(HttpConstants.SECURESOCKET_CONFIG_SNI_HOST_NAME.getValue(),
String.valueOf(secureSocket.getStringValue(HttpConstants.SECURESOCKET_CONFIG_SNI_HOST_NAME)));
paramList.add(sniHostNameParam);
}
}

private static BMap<BString, Object> getBMapValueIfPresent(BMap<BString, Object> map, BString key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class Constants {
public static final String CLIENT_SUPPORT_CIPHERS = "ciphers";
public static final String CLIENT_SUPPORT_SSL_PROTOCOLS = "sslEnabledProtocols";
public static final String CLIENT_ENABLE_SESSION_CREATION = "shareSession";
public static final String SNI_SERVER_NAME = "sniHostName";
public static final String MUTUAL_SSL_PASSED = "passed";
public static final String MUTUAL_SSL_FAILED = "failed";
public static final String MUTUAL_SSL_DISABLED = "disabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static io.ballerina.stdlib.http.transport.contract.Constants.SERVER_SUPPORTED_SNIMATCHERS;
import static io.ballerina.stdlib.http.transport.contract.Constants.SERVER_SUPPORT_CIPHERS;
import static io.ballerina.stdlib.http.transport.contract.Constants.SERVER_SUPPORT_SSL_PROTOCOLS;
import static io.ballerina.stdlib.http.transport.contract.Constants.SNI_SERVER_NAME;
import static io.ballerina.stdlib.http.transport.contract.Constants.TLS_PROTOCOL;
/**
* SSL configuration for HTTP connection.
Expand Down Expand Up @@ -277,6 +278,9 @@ private SSLConfig getSSLConfigForSender() {
case CLIENT_ENABLE_SESSION_CREATION:
sslConfig.setEnableSessionCreation(Boolean.parseBoolean(parameter.getValue()));
break;
case SNI_SERVER_NAME:
sslConfig.setSniHostName(parameter.getValue());
break;
default:
//do nothing
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private static SSLEngine getSslEngineForCerts(SocketChannel socketChannel, Strin
SslHandler sslHandler = sslContext.newHandler(socketChannel.alloc(), host, port);
SSLEngine sslEngine = sslHandler.engine();
sslHandlerFactory.addCommonConfigs(sslEngine);
sslHandlerFactory.setSNIServerNames(sslEngine, host);
setSniServerName(sslConfig, host, sslHandlerFactory, sslEngine);
if (sslConfig.isHostNameVerificationEnabled()) {
setHostNameVerfication(sslEngine);
}
Expand Down Expand Up @@ -507,14 +507,22 @@ private static SSLEngine instantiateAndConfigSSL(SSLConfig sslConfig, String hos
if (sslConfig != null) {
sslEngine = sslHandlerFactory.buildClientSSLEngine(host, port);
sslEngine.setUseClientMode(true);
sslHandlerFactory.setSNIServerNames(sslEngine, host);
setSniServerName(sslConfig, host, sslHandlerFactory, sslEngine);
if (hostNameVerificationEnabled) {
sslHandlerFactory.setHostNameVerfication(sslEngine);
}
}
return sslEngine;
}

private static void setSniServerName(SSLConfig sslConfig, String host, SSLHandlerFactory sslHandlerFactory, SSLEngine sslEngine) {
if (sslConfig.getSniHostName().isEmpty()) {
sslHandlerFactory.setSNIServerNames(sslEngine, host);
} else {
sslHandlerFactory.setSNIServerNames(sslEngine, sslConfig.getSniHostName());
}
}

/**
* Get integer type property value from a property map.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class SSLConfig {
private long handshakeTimeOut;
private boolean disableSsl = false;
private boolean useJavaDefaults = false;
private String sniHostName;

public SSLConfig() {}

Expand Down Expand Up @@ -179,6 +180,14 @@ public void setEnableSessionCreation(boolean enableSessionCreation) {
this.enableSessionCreation = enableSessionCreation;
}

public void setSniHostName(String sniHostName) {
this.sniHostName = sniHostName;
}

public String getSniHostName() {
return sniHostName;
}

public String[] getEnableProtocols() {
return enableProtocols == null ? null : enableProtocols.clone();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ private void configureSslForHttp2(SocketChannel ch, ChannelPipeline clientPipeli
SslContext sslCtx = sslHandlerFactory.createHttp2TLSContextForClient(false);
SslHandler sslHandler = sslCtx.newHandler(ch.alloc(), httpRoute.getHost(), httpRoute.getPort());
SSLEngine sslEngine = sslHandler.engine();
sslHandlerFactory.setSNIServerNames(sslEngine, httpRoute.getHost());
if (sslConfig.getSniHostName().isEmpty()) {
sslHandlerFactory.setSNIServerNames(sslEngine, httpRoute.getHost());
} else {
sslHandlerFactory.setSNIServerNames(sslEngine, sslConfig.getSniHostName());
}
if (sslConfig.isHostNameVerificationEnabled()) {
setHostNameVerfication(sslEngine);
}
Expand Down

0 comments on commit 780ea9a

Please sign in to comment.