Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshiAthapaththu committed Jul 17, 2023
1 parent 71d3137 commit 2d903fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*
* 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.lang3.ArrayUtils;
import org.apache.http.conn.ssl.AbstractVerifier;

import javax.net.ssl.SSLException;
import java.util.Optional;

/**
* Custom hostname verifier class.
Expand All @@ -13,16 +30,17 @@ public class CustomHostNameVerifier extends AbstractVerifier {
private final static String[] LOCALHOSTS = {"::1", "127.0.0.1", "localhost", "localhost.localdomain"};

@Override
public void verify(String s, String[] strings, String[] subjectAlts) throws SSLException {

String[] subjectAltsWithLocalhosts = ArrayUtils.addAll(subjectAlts, LOCALHOSTS);
public void verify(String hostname, String[] commonNames, String[] subjectAlternativeNames) throws SSLException {

if (strings != null && strings.length > 0 && strings[0] != null) {
String[] subjectAltsWithLocalhosts = ArrayUtils.addAll(subjectAlternativeNames, LOCALHOSTS);

String[] subjectAltsWithLocalhostsAndCN = ArrayUtils.add(subjectAltsWithLocalhosts, strings[0]);
this.verify(s, strings, subjectAltsWithLocalhostsAndCN, false);
} else {
this.verify(s, strings, subjectAltsWithLocalhosts, false);
boolean isValidCommonNames = Optional.ofNullable(commonNames)
.filter(names -> names.length > 0)
.map(names -> names[0])
.isPresent();
if (isValidCommonNames && !ArrayUtils.contains(subjectAlternativeNames, commonNames[0])) {
subjectAltsWithLocalhosts = ArrayUtils.add(subjectAltsWithLocalhosts, commonNames[0]);
}
this.verify(hostname, commonNames, subjectAltsWithLocalhosts, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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;
Expand All @@ -20,7 +37,7 @@ private HTTPClientUtils() {
*
* @return HttpClientBuilder.
*/
public static HttpClientBuilder getHTTPClientWithCustomHostNameVerifier() {
public static HttpClientBuilder createClientWithCustomVerifier() {

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
if (DEFAULT_AND_LOCALHOST.equals(System.getProperty(HOST_NAME_VERIFIER))) {
Expand Down

0 comments on commit 2d903fb

Please sign in to comment.