Skip to content

Commit

Permalink
Fix the SNI for default certs
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Dec 5, 2024
1 parent ebeb5b7 commit f57474a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
16 changes: 0 additions & 16 deletions ballerina-tests/http-security-tests/tests/ssl_disable_ssl_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,3 @@ public function testSslDisabledClient1() returns error? {
test:assertFail(msg = "Found unexpected output: " + resp.message());
}
}

http:ClientConfiguration disableSslClientConf2 = {
secureSocket: {
}
};

@test:Config {}
public function testSslDisabledClient2() {
http:Client|error httpClient = new ("https://localhost:9238", disableSslClientConf2);
string expectedErrMsg = "Need to configure cert with client SSL certificates file";
if (httpClient is error) {
test:assertEquals(httpClient.message(), expectedErrMsg);
} else {
test:assertFail(msg = "Expected mutual SSL error not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,25 @@ public function testSniFailure() returns error? {
test:assertFail("Test `testSniFailure` is expecting an error. But received a success response");
}
}

@test:Config {}
public function testSniWhenUsingDefaultCerts() returns error? {
http:Client httpClient = check new("https://www.google.com", http2SniClientConf3);
string|error resp = httpClient->get("/");
// This response is success because even though we send a wrong server name, google.com sends the default cert which
// is valid and trusted by the client.
if resp is error {
test:assertFail("Found unexpected output: " + resp.message());
}
}

@test:Config {}
public function testSniFailureWhenUsingDefaultCerts() returns error? {
http:Client clientEP = check new ("https://127.0.0.1:9208", http2SniClientConf3);
string|error resp = clientEP->get("/http1SniService/");
if resp is error {
common:assertTrueTextPayload(resp.message(), "SSL connection failed:javax.net.ssl.SSLHandshakeException:");
} else {
test:assertFail("Test `testSniFailureWhenUsingDefaultCerts` is expecting an error. But received a success response");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1407,13 +1407,7 @@ public static void populateSSLConfiguration(SslConfiguration senderConfiguration
}
Object cert = secureSocket.get(HttpConstants.SECURESOCKET_CONFIG_CERT);
if (cert == null) {
BMap<BString, Object> key = getBMapValueIfPresent(secureSocket, HttpConstants.SECURESOCKET_CONFIG_KEY);
if (key != null) {
senderConfiguration.useJavaDefaults();
} else {
throw createHttpError("Need to configure cert with client SSL certificates file",
HttpErrorType.SSL_ERROR);
}
senderConfiguration.useJavaDefaults();
} else {
evaluateCertField(cert, senderConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private SSLConfig getSSLConfigForListener() {
}

private SSLConfig getSSLConfigForSender() {
setSslParameters();
if (sslConfig.isDisableSsl() || sslConfig.useJavaDefaults()) {
return sslConfig;
}
Expand All @@ -265,7 +266,10 @@ private SSLConfig getSSLConfigForSender() {
sslConfig.setSSLProtocol(sslProtocol);
String tlsStoreType = sslConfig.getTLSStoreType() != null ? sslConfig.getTLSStoreType() : JKS;
sslConfig.setTLSStoreType(tlsStoreType);
return sslConfig;
}

private void setSslParameters() {
if (parameters != null) {
for (Parameter parameter : parameters) {
switch (parameter.getName()) {
Expand All @@ -287,6 +291,5 @@ private SSLConfig getSSLConfigForSender() {
}
}
}
return sslConfig;
}
}

0 comments on commit f57474a

Please sign in to comment.