Skip to content

Commit b445bd4

Browse files
authored
Merge pull request #24 from datatheorem/pr_update_okhostnameverifier
Update OkHostnameVerifier in TrustKit-Android
2 parents e3348bf + 9a6d50c commit b445bd4

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/OkHostnameVerifier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public boolean verify(String host, SSLSession session) {
5757
}
5858

5959
public boolean verify(String host, X509Certificate certificate) {
60-
// TrustKit: Removed support here for IP addresses so we don't need to import more files
61-
// from OkHttp
62-
return verifyHostname(host, certificate);
60+
return Utils.verifyAsIpAddress(host)
61+
? verifyIpAddress(host, certificate)
62+
: verifyHostname(host, certificate);
6363
}
6464

6565
/** Returns true if {@code certificate} matches {@code ipAddress}. */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datatheorem.android.trustkit.pinning;
17+
18+
import java.util.regex.Pattern;
19+
20+
/** Junk drawer of utility methods. */
21+
final class Utils {
22+
/**
23+
* Quick and dirty pattern to differentiate IP addresses from hostnames. This is an approximation
24+
* of Android's private InetAddress#isNumeric API.
25+
*
26+
* <p>This matches IPv6 addresses as a hex string containing at least one colon, and possibly
27+
* including dots after the first colon. It matches IPv4 addresses as strings containing only
28+
* decimal digits and dots. This pattern matches strings like "a:.23" and "54" that are neither IP
29+
* addresses nor hostnames; they will be verified as IP addresses (which is a more strict
30+
* verification).
31+
*/
32+
private static final Pattern VERIFY_AS_IP_ADDRESS = Pattern.compile(
33+
"([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");
34+
35+
36+
/** Returns true if {@code host} is not a host name and might be an IP address. */
37+
public static boolean verifyAsIpAddress(String host) {
38+
return VERIFY_AS_IP_ADDRESS.matcher(host).matches();
39+
}
40+
}

0 commit comments

Comments
 (0)