Skip to content

Commit 3182f72

Browse files
author
Steve Shenouda
committed
Fix lint checks around requirement of api 17
1 parent b12c35d commit 3182f72

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.datatheorem.android.trustkit.pinning;
22

3+
import android.os.Build;
4+
35
import androidx.annotation.NonNull;
46
import androidx.annotation.RequiresApi;
57

@@ -13,9 +15,16 @@
1315
import javax.net.ssl.SSLSocketFactory;
1416
import javax.net.ssl.X509TrustManager;
1517

16-
@RequiresApi(api = 17)
1718
public class OkHttp2Helper {
18-
private static OkHttpRootTrustManager trustManager = new OkHttpRootTrustManager();
19+
private static X509TrustManager trustManager;
20+
21+
static {
22+
if (Build.VERSION.SDK_INT < 17) {
23+
trustManager = SystemTrustManager.getInstance();
24+
} else {
25+
trustManager = new OkHttpRootTrustManager();
26+
}
27+
}
1928

2029
/**
2130
* Retrieve an {@code SSLSSocketFactory} that implements SSL pinning validation based on the
@@ -46,7 +55,8 @@ public static SSLSocketFactory getSSLSocketFactory() {
4655
* later be used for Certificate Pinning.
4756
*/
4857
@NonNull
58+
@RequiresApi(api = 17)
4959
public static Interceptor getPinningInterceptor() {
50-
return new OkHttp2PinningInterceptor(trustManager);
60+
return new OkHttp2PinningInterceptor((OkHttpRootTrustManager)trustManager);
5161
}
5262
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.datatheorem.android.trustkit.pinning;
22

3+
import android.os.Build;
4+
35
import androidx.annotation.NonNull;
46
import androidx.annotation.RequiresApi;
57

@@ -13,9 +15,16 @@
1315
import okhttp3.Interceptor;
1416
import okhttp3.Request;
1517

16-
@RequiresApi(api = 17)
1718
public class OkHttp3Helper {
18-
private static OkHttpRootTrustManager trustManager = new OkHttpRootTrustManager();
19+
private static X509TrustManager trustManager;
20+
21+
static {
22+
if (Build.VERSION.SDK_INT < 17) {
23+
trustManager = SystemTrustManager.getInstance();
24+
} else {
25+
trustManager = new OkHttpRootTrustManager();
26+
}
27+
}
1928

2029
/**
2130
* Retrieve an {@code SSLSSocketFactory} that implements SSL pinning validation based on the
@@ -46,15 +55,16 @@ public static SSLSocketFactory getSSLSocketFactory() {
4655
* Certificate Pinning.
4756
*/
4857
@NonNull
58+
@RequiresApi(api = 17)
4959
public static Interceptor getPinningInterceptor() {
50-
return new OkHttp3PinningInterceptor(trustManager);
60+
return new OkHttp3PinningInterceptor((OkHttpRootTrustManager)trustManager);
5161
}
5262

5363
/**
5464
* Returns an instance of the {@link OkHttpRootTrustManager} used for Certificate Pinning.
5565
*/
5666
@NonNull
57-
public static OkHttpRootTrustManager getTrustManager() {
67+
public static X509TrustManager getTrustManager() {
5868
return trustManager;
5969
}
6070
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.datatheorem.android.trustkit.pinning;
22

33
import android.net.http.X509TrustManagerExtensions;
4+
import android.os.Build;
45

56
import androidx.annotation.NonNull;
67
import androidx.annotation.RequiresApi;
@@ -19,7 +20,6 @@
1920
* <p>This trust manager delegates to the appropriate {@link PinningTrustManager} decided by the
2021
* hostname set by the {@link OkHttp3PinningInterceptor}.</p>
2122
*/
22-
@RequiresApi(api = 17)
2323
class OkHttpRootTrustManager implements X509TrustManager {
2424
private final ThreadLocal<String> mServerHostname = new ThreadLocal<>();
2525

@@ -33,14 +33,17 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
3333
String host = mServerHostname.get();
3434
DomainPinningPolicy serverConfig =
3535
TrustKit.getInstance().getConfiguration().getPolicyForHostname(host);
36-
//This check is needed for compatibility with the Platform default's implementation of
36+
X509TrustManager trustManager = TrustKit.getInstance().getTrustManager(host);
37+
38+
//The first check is needed for compatibility with the Platform default's implementation of
3739
//the Trust Manager. For APIs 24 and greater, the Platform's default TrustManager states
3840
//that it requires usage of the hostname-aware version of checkServerTrusted for app's that
39-
//implement Android's network_security_config file.
40-
if (serverConfig == null) {
41-
new X509TrustManagerExtensions(TrustKit.getInstance().getTrustManager(host)).checkServerTrusted(chain, authType, host);
41+
//implement Android's network_security_config file. The 2nd check is to allow usage of the
42+
//X509TrustManagerExtensions class. Any API below will default to the baseline trust manager.
43+
if (serverConfig == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
44+
new X509TrustManagerExtensions(trustManager).checkServerTrusted(chain, authType, host);
4245
} else {
43-
TrustKit.getInstance().getTrustManager(host).checkServerTrusted(chain, authType);
46+
trustManager.checkServerTrusted(chain, authType);
4447
}
4548
}
4649

0 commit comments

Comments
 (0)