diff --git a/httpclient/src/main/java/org/apache/http/conn/ssl/DefaultHostnameVerifier.java b/httpclient/src/main/java/org/apache/http/conn/ssl/DefaultHostnameVerifier.java index fae15866d..4a0ae1fbe 100644 --- a/httpclient/src/main/java/org/apache/http/conn/ssl/DefaultHostnameVerifier.java +++ b/httpclient/src/main/java/org/apache/http/conn/ssl/DefaultHostnameVerifier.java @@ -36,7 +36,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Locale; import java.util.NoSuchElementException; import javax.naming.InvalidNameException; @@ -55,6 +54,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.annotation.Contract; import org.apache.http.annotation.ThreadingBehavior; +import org.apache.http.conn.util.DnsUtils; import org.apache.http.conn.util.DomainType; import org.apache.http.conn.util.InetAddressUtils; import org.apache.http.conn.util.PublicSuffixMatcher; @@ -164,11 +164,11 @@ public final class DefaultHostnameVerifier implements HostnameVerifier { static void matchDNSName(final String host, final List subjectAlts, final PublicSuffixMatcher publicSuffixMatcher) throws SSLException { - final String normalizedHost = host.toLowerCase(Locale.ROOT); + final String normalizedHost = DnsUtils.normalize(host); for (int i = 0; i < subjectAlts.size(); i++) { final SubjectName subjectAlt = subjectAlts.get(i); if (subjectAlt.getType() == SubjectName.DNS) { - final String normalizedSubjectAlt = subjectAlt.getValue().toLowerCase(Locale.ROOT); + final String normalizedSubjectAlt = DnsUtils.normalize(subjectAlt.getValue()); if (matchIdentityStrict(normalizedHost, normalizedSubjectAlt, publicSuffixMatcher, DomainType.ICANN)) { return; } @@ -180,8 +180,8 @@ public final class DefaultHostnameVerifier implements HostnameVerifier { static void matchCN(final String host, final String cn, final PublicSuffixMatcher publicSuffixMatcher) throws SSLException { - final String normalizedHost = host.toLowerCase(Locale.ROOT); - final String normalizedCn = cn.toLowerCase(Locale.ROOT); + final String normalizedHost = DnsUtils.normalize(host); + final String normalizedCn = DnsUtils.normalize(cn); if (!matchIdentityStrict(normalizedHost, normalizedCn, publicSuffixMatcher, DomainType.ICANN)) { throw new SSLPeerUnverifiedException("Certificate for <" + host + "> doesn't match " + "common name of the certificate subject: " + cn); diff --git a/httpclient/src/main/java/org/apache/http/conn/util/DnsUtils.java b/httpclient/src/main/java/org/apache/http/conn/util/DnsUtils.java new file mode 100644 index 000000000..1cdcb9c22 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/conn/util/DnsUtils.java @@ -0,0 +1,76 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.conn.util; + +/** + * A collection of utilities relating to Domain Name System. + * + * @since 4.5 + */ +public class DnsUtils { + + private DnsUtils() { + } + + private static boolean isUpper(final char c) { + return c >= 'A' && c <= 'Z'; + } + + public static String normalize(final String s) { + if (s == null) { + return null; + } + int pos = 0; + int remaining = s.length(); + while (remaining > 0) { + if (isUpper(s.charAt(pos))) { + break; + } + pos++; + remaining--; + } + if (remaining > 0) { + final StringBuilder buf = new StringBuilder(s.length()); + buf.append(s, 0, pos); + while (remaining > 0) { + final char c = s.charAt(pos); + if (isUpper(c)) { + buf.append((char) (c + ('a' - 'A'))); + } else { + buf.append(c); + } + pos++; + remaining--; + } + return buf.toString(); + } else { + return s; + } + } + +} diff --git a/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java b/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java index c4c8e0665..2f63c38fc 100644 --- a/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java +++ b/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java @@ -29,7 +29,6 @@ package org.apache.http.conn.util; import java.net.IDN; import java.util.Collection; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -142,7 +141,7 @@ public final class PublicSuffixMatcher { if (domain.startsWith(".")) { return null; } - final String normalized = domain.toLowerCase(Locale.ROOT); + final String normalized = DnsUtils.normalize(domain); String segment = normalized; String result = null; while (segment != null) { diff --git a/httpclient/src/test/java/org/apache/http/conn/util/TesDnsUtils.java b/httpclient/src/test/java/org/apache/http/conn/util/TesDnsUtils.java new file mode 100644 index 000000000..4fdd4a369 --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/conn/util/TesDnsUtils.java @@ -0,0 +1,51 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.conn.util; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit tests for DnsUtils. + */ +public class TesDnsUtils { + + @Test + public void testNormalize() { + Assert.assertThat(DnsUtils.normalize(null), CoreMatchers.equalTo(null)); + Assert.assertThat(DnsUtils.normalize(""), CoreMatchers.equalTo("")); + Assert.assertThat(DnsUtils.normalize("blah"), CoreMatchers.equalTo("blah")); + Assert.assertThat(DnsUtils.normalize("BLAH"), CoreMatchers.equalTo("blah")); + Assert.assertThat(DnsUtils.normalize("blAh"), CoreMatchers.equalTo("blah")); + Assert.assertThat(DnsUtils.normalize("blaH"), CoreMatchers.equalTo("blah")); + Assert.assertThat(DnsUtils.normalize("blaH"), CoreMatchers.equalTo("blah")); + Assert.assertThat(DnsUtils.normalize("hac\u212A!!!"), CoreMatchers.equalTo("hac\u212A!!!")); + } + +}