-
Notifications
You must be signed in to change notification settings - Fork 91
Add support for pinning multiple domains with a single OkHttpClient #63
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
Merged
nabla-c0d3
merged 6 commits into
datatheorem:master
from
SteveShenouda:support-multiple-domains
Mar 9, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7986e3
Add multiple domain pinning support for OkHttp3
26c5d32
Add multiple domain pinning support for OkHttp2
febbdb8
Add documentation for multiple domain pinning support
a105150
Add OkHttp Helper classes for easier dev implementation
cb6723f
Add documentation for hostname-aware invocation of SSL
7273b2d
Add documentation stating no support of url redirects
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,10 @@ ext{ | |
| ], | ||
| google: [ | ||
| material: '1.0.0' | ||
| ], | ||
| squareup: [ | ||
| okhttp3: '3.11.0', | ||
| okhttp2: '2.4.0' | ||
| ] | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/OkHttp2Helper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.datatheorem.android.trustkit.pinning; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.RequiresApi; | ||
|
|
||
| import com.squareup.okhttp.Interceptor; | ||
| import com.squareup.okhttp.Request; | ||
|
|
||
| import java.security.KeyManagementException; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLSocketFactory; | ||
| import javax.net.ssl.X509TrustManager; | ||
|
|
||
| @RequiresApi(api = 17) | ||
| public class OkHttp2Helper { | ||
| private static RootTrustManager trustManager = new RootTrustManager(); | ||
|
|
||
| /** | ||
| * Retrieve an {@code SSLSSocketFactory} that implements SSL pinning validation based on the | ||
| * current TrustKit configuration. It can be used with an OkHttpClient to add SSL | ||
| * pinning validation to the connections. | ||
| * | ||
| * <p> | ||
| * The {@code SSLSocketFactory} is configured for the current TrustKit configuration and | ||
| * will enforce the configuration's pinning policy. | ||
| * </p> | ||
| */ | ||
| @NonNull | ||
| public static SSLSocketFactory getSSLSocketFactory() { | ||
| try { | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, new X509TrustManager[]{trustManager}, null); | ||
|
|
||
| return sslContext.getSocketFactory(); | ||
| } catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
| e.printStackTrace(); | ||
| throw new IllegalStateException("SSLSocketFactory creation failed"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns an {@link com.squareup.okhttp.Interceptor} used to parse the hostname of the | ||
| * {@link Request} URL and then save the hostname in the {@link RootTrustManager} which will | ||
| * later be used for Certificate Pinning. | ||
| */ | ||
| @NonNull | ||
| public static Interceptor getPinningInterceptor() { | ||
| return new PinningInterceptor2(trustManager); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/OkHttp3Helper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.datatheorem.android.trustkit.pinning; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.RequiresApi; | ||
|
|
||
| import java.security.KeyManagementException; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLSocketFactory; | ||
| import javax.net.ssl.X509TrustManager; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
|
|
||
| @RequiresApi(api = 17) | ||
| public class OkHttp3Helper { | ||
| private static RootTrustManager trustManager = new RootTrustManager(); | ||
|
|
||
| /** | ||
| * Retrieve an {@code SSLSSocketFactory} that implements SSL pinning validation based on the | ||
| * current TrustKit configuration. It can be used with an OkHttpClient to add SSL | ||
| * pinning validation to the connections. | ||
| * | ||
| * <p> | ||
| * The {@code SSLSocketFactory} is configured for the current TrustKit configuration and | ||
| * will enforce the configuration's pinning policy. | ||
| * </p> | ||
| */ | ||
| @NonNull | ||
| public static SSLSocketFactory getSSLSocketFactory() { | ||
| try { | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, new X509TrustManager[]{trustManager}, null); | ||
|
|
||
| return sslContext.getSocketFactory(); | ||
| } catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
| e.printStackTrace(); | ||
| throw new IllegalStateException("SSLSocketFactory creation failed"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns an {@link okhttp3.Interceptor} used to parse the hostname of the {@link Request} URL | ||
| * and then save the hostname in the {@link RootTrustManager} which will later be used for | ||
| * Certificate Pinning. | ||
| */ | ||
| @NonNull | ||
| public static Interceptor getPinningInterceptor() { | ||
| return new PinningInterceptor(trustManager); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an instance of the {@link RootTrustManager} used for Certificate Pinning. | ||
| */ | ||
| @NonNull | ||
| public static RootTrustManager getTrustManager() { | ||
| return trustManager; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/PinningInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.datatheorem.android.trustkit.pinning; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
||
| /** | ||
| * {@link Interceptor} used to parse the hostname of the {@link Request} URL and then save the | ||
| * hostname in the {@link RootTrustManager} which will later be used for Certificate Pinning. | ||
| */ | ||
| public class PinningInterceptor implements Interceptor { | ||
| private final RootTrustManager mTrustManager; | ||
|
|
||
| public PinningInterceptor(@NonNull RootTrustManager trustManager) { | ||
| mTrustManager = trustManager; | ||
| } | ||
|
|
||
| @Override public Response intercept(Interceptor.Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
| String serverHostname = request.url().host(); | ||
|
|
||
| mTrustManager.setServerHostname(serverHostname); | ||
| return chain.proceed(request); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/PinningInterceptor2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.datatheorem.android.trustkit.pinning; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import com.squareup.okhttp.Interceptor; | ||
| import com.squareup.okhttp.Request; | ||
| import com.squareup.okhttp.Response; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * {@link Interceptor} used to parse the hostname of the {@link Request} URL and then save the | ||
| * hostname in the {@link RootTrustManager} which will later be used for Certificate Pinning. | ||
| */ | ||
| public class PinningInterceptor2 implements Interceptor { | ||
| private final RootTrustManager mTrustManager; | ||
|
|
||
| public PinningInterceptor2(@NonNull RootTrustManager trustManager) { | ||
| mTrustManager = trustManager; | ||
| } | ||
|
|
||
| @Override public Response intercept(Interceptor.Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
| String serverHostname = request.url().getHost(); | ||
|
|
||
| mTrustManager.setServerHostname(serverHostname); | ||
| return chain.proceed(request); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
trustkit/src/main/java/com/datatheorem/android/trustkit/pinning/RootTrustManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.datatheorem.android.trustkit.pinning; | ||
|
|
||
| import android.net.http.X509TrustManagerExtensions; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.RequiresApi; | ||
|
|
||
| import com.datatheorem.android.trustkit.TrustKit; | ||
| import com.datatheorem.android.trustkit.config.DomainPinningPolicy; | ||
|
|
||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.X509Certificate; | ||
|
|
||
| import javax.net.ssl.X509TrustManager; | ||
|
|
||
| /** | ||
| * {@link X509TrustManager} used for Certificate Pinning. | ||
| * | ||
| * <p>This trust manager delegates to the appropriate {@link PinningTrustManager} decided by the | ||
| * hostname set by the {@link PinningInterceptor}.</p> | ||
| */ | ||
| @RequiresApi(api = 17) | ||
| class RootTrustManager implements X509TrustManager { | ||
| private final ThreadLocal<String> mServerHostname = new ThreadLocal<>(); | ||
|
|
||
| @Override | ||
| public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| TrustKit.getInstance().getTrustManager(mServerHostname.get()).checkClientTrusted(chain, authType); | ||
| } | ||
|
|
||
| @Override | ||
| public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| String host = mServerHostname.get(); | ||
| DomainPinningPolicy serverConfig = | ||
| TrustKit.getInstance().getConfiguration().getPolicyForHostname(host); | ||
| //This check is needed for compatibility with the Platform default's implementation of | ||
| //the Trust Manager. For APIs 24 and greater, the Platform's default TrustManager states | ||
| //that it requires usage of the hostname-aware version of checkServerTrusted for app's that | ||
| //implement Android's network_security_config file. | ||
| if (serverConfig == null) { | ||
| new X509TrustManagerExtensions(TrustKit.getInstance().getTrustManager(host)).checkServerTrusted(chain, authType, host); | ||
| } else { | ||
| TrustKit.getInstance().getTrustManager(host).checkServerTrusted(chain, authType); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public X509Certificate[] getAcceptedIssuers() { | ||
| return new X509Certificate[0]; | ||
| } | ||
|
|
||
| void setServerHostname(@NonNull String serverHostname) { | ||
| mServerHostname.set(serverHostname); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the need for the if/else here? What about
TrustKit.getInstance().getTrustManager(host).checkServerTrusted(chain, authType)in all cases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment to address the need