From f57474a8857d6180855e4f78e65709037a29f5d7 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 5 Dec 2024 17:34:24 +0530 Subject: [PATCH] Fix the SNI for default certs --- .../tests/ssl_disable_ssl_test.bal | 16 -------------- .../tests/ssl_sni_host_name_test.bal | 22 +++++++++++++++++++ .../ballerina/stdlib/http/api/HttpUtil.java | 8 +------ .../contract/config/SslConfiguration.java | 5 ++++- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/ballerina-tests/http-security-tests/tests/ssl_disable_ssl_test.bal b/ballerina-tests/http-security-tests/tests/ssl_disable_ssl_test.bal index 21bf5b34f6..f1b003b61c 100644 --- a/ballerina-tests/http-security-tests/tests/ssl_disable_ssl_test.bal +++ b/ballerina-tests/http-security-tests/tests/ssl_disable_ssl_test.bal @@ -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"); - } -} diff --git a/ballerina-tests/http-security-tests/tests/ssl_sni_host_name_test.bal b/ballerina-tests/http-security-tests/tests/ssl_sni_host_name_test.bal index 7b341472c5..7eaa52b7ea 100644 --- a/ballerina-tests/http-security-tests/tests/ssl_sni_host_name_test.bal +++ b/ballerina-tests/http-security-tests/tests/ssl_sni_host_name_test.bal @@ -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"); + } +} diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java index defdadd137..970c857701 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java @@ -1407,13 +1407,7 @@ public static void populateSSLConfiguration(SslConfiguration senderConfiguration } Object cert = secureSocket.get(HttpConstants.SECURESOCKET_CONFIG_CERT); if (cert == null) { - BMap 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); } diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contract/config/SslConfiguration.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contract/config/SslConfiguration.java index 0db8ffdeb8..23975cbc82 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contract/config/SslConfiguration.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contract/config/SslConfiguration.java @@ -245,6 +245,7 @@ private SSLConfig getSSLConfigForListener() { } private SSLConfig getSSLConfigForSender() { + setSslParameters(); if (sslConfig.isDisableSsl() || sslConfig.useJavaDefaults()) { return sslConfig; } @@ -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()) { @@ -287,6 +291,5 @@ private SSLConfig getSSLConfigForSender() { } } } - return sslConfig; } }