Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that hostname is ascii in OkHostnameVerifier.java #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.security.auth.x500.X500Principal;
import com.google.common.base.Utf8;
import com.google.common.base.Ascii;

/**
* A HostnameVerifier consistent with <a
Expand Down Expand Up @@ -63,6 +66,9 @@ private OkHostnameVerifier() {

@Override
public boolean verify(String host, SSLSession session) {
if (!isAscii(host)) {
return false;
}
try {
Certificate[] certificates = session.getPeerCertificates();
return verify(host, (X509Certificate) certificates[0]);
Expand All @@ -71,7 +77,7 @@ public boolean verify(String host, SSLSession session) {
}
}

public boolean verify(String host, X509Certificate certificate) {
private boolean verify(String host, X509Certificate certificate) {
return verifyAsIpAddress(host)
? verifyIpAddress(host, certificate)
: verifyHostName(host, certificate);
Expand All @@ -98,7 +104,7 @@ private boolean verifyIpAddress(String ipAddress, X509Certificate certificate) {
* Returns true if {@code certificate} matches {@code hostName}.
*/
private boolean verifyHostName(String hostName, X509Certificate certificate) {
hostName = hostName.toLowerCase(Locale.US);
hostName = Ascii.toLowerCase(hostName);
boolean hasDns = false;
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
Expand Down Expand Up @@ -198,7 +204,7 @@ private boolean verifyHostName(String hostName, String pattern) {
}
// hostName and pattern are now absolute domain names.

pattern = pattern.toLowerCase(Locale.US);
pattern = Ascii.toLowerCase(pattern);
// hostName and pattern are now in lower case -- domain names are case-insensitive.

if (!pattern.contains("*")) {
Expand Down Expand Up @@ -254,4 +260,13 @@ private boolean verifyHostName(String hostName, String pattern) {
// hostName matches pattern
return true;
}

/**
* Returns true if {@code input} is an ASCII string.
* @param input the string to check.
*/
private static boolean isAscii(String input) {
// Only ASCII characters are 1 byte in UTF-8.
return Utf8.encodedLength(input) == input.length();
}
}
Loading