diff --git a/core/org.wso2.carbon.utils/pom.xml b/core/org.wso2.carbon.utils/pom.xml index 067bd077135..474d04aea27 100644 --- a/core/org.wso2.carbon.utils/pom.xml +++ b/core/org.wso2.carbon.utils/pom.xml @@ -229,6 +229,10 @@ com.google.code.gson gson + + org.wso2.orbit.org.apache.httpcomponents + httpclient + diff --git a/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/CustomHostNameVerifier.java b/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/CustomHostNameVerifier.java new file mode 100644 index 00000000000..5e8f0fc655c --- /dev/null +++ b/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/CustomHostNameVerifier.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.utils; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.http.conn.ssl.AbstractVerifier; +import javax.net.ssl.SSLException; +import java.util.Optional; + +/** + * Custom hostname verifier class. + */ +public class CustomHostNameVerifier extends AbstractVerifier { + + private final static String[] LOCALHOSTS = {"::1", "127.0.0.1", "localhost", "localhost.localdomain"}; + + @Override + public void verify(String hostname, String[] commonNames, String[] subjectAlternativeNames) throws SSLException { + + String[] subjectAltsWithLocalhosts = (String[]) ArrayUtils.addAll(subjectAlternativeNames, LOCALHOSTS); + + boolean hasValidCommonNames = Optional.ofNullable(commonNames) + .filter(names -> names.length > 0) + .map(names -> names[0]) + .isPresent(); + if (hasValidCommonNames && !ArrayUtils.contains(subjectAlternativeNames, commonNames[0])) { + subjectAltsWithLocalhosts = (String[]) ArrayUtils.add(subjectAltsWithLocalhosts, commonNames[0]); + } + super.verify(hostname, commonNames, subjectAltsWithLocalhosts, false); + } +} diff --git a/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/HTTPClientUtils.java b/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/HTTPClientUtils.java new file mode 100644 index 00000000000..45ebc00abbc --- /dev/null +++ b/core/org.wso2.carbon.utils/src/main/java/org/wso2/carbon/utils/HTTPClientUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.utils; + +import org.apache.http.conn.ssl.X509HostnameVerifier; +import org.apache.http.impl.client.HttpClientBuilder; + +/** + * Util methods for HTTP Client. + */ +public class HTTPClientUtils { + + public static final String DEFAULT_AND_LOCALHOST = "DefaultAndLocalhost"; + public static final String HOST_NAME_VERIFIER = "httpclient.hostnameVerifier"; + + private HTTPClientUtils() { + //disable external instantiation + } + + /** + * Get the httpclient builder with custom hostname verifier. + * + * @return HttpClientBuilder. + */ + public static HttpClientBuilder createClientWithCustomVerifier() { + + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties(); + if (DEFAULT_AND_LOCALHOST.equals(System.getProperty(HOST_NAME_VERIFIER))) { + X509HostnameVerifier hostnameVerifier = new CustomHostNameVerifier(); + httpClientBuilder.setHostnameVerifier(hostnameVerifier); + } + + return httpClientBuilder; + } +} diff --git a/parent/pom.xml b/parent/pom.xml index 4bb9373d852..40a7759664b 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -654,6 +654,9 @@ 1.0.13 1.8.3 1.5.0 + + 4.5.13.wso2v1 + [4.3.1.wso2v2,5.0.0) @@ -1917,6 +1920,11 @@ stax-ex ${version.stax.ex} + + org.wso2.orbit.org.apache.httpcomponents + httpclient + ${httpcomponents-httpclient.wso2.version} +