-
Notifications
You must be signed in to change notification settings - Fork 9
feat: optimize SSLSocketFactory implementation #223
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
Merged
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/aliyun/tea/utils/DefaultHostnameVerifier.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.aliyun.tea.utils; | ||
|
|
||
| import okhttp3.internal.tls.OkHostnameVerifier; | ||
|
|
||
| import javax.net.ssl.HostnameVerifier; | ||
| import javax.net.ssl.SSLSession; | ||
|
|
||
| public class DefaultHostnameVerifier implements HostnameVerifier { | ||
| private final boolean ignoreSSLCert; | ||
| private static final HostnameVerifier NOOP_INSTANCE = new DefaultHostnameVerifier(true); | ||
| private static final HostnameVerifier DEFAULT_INSTANCE = OkHostnameVerifier.INSTANCE; | ||
|
|
||
| private DefaultHostnameVerifier(boolean ignoreSSLCert) { | ||
| this.ignoreSSLCert = ignoreSSLCert; | ||
| } | ||
|
|
||
| public static HostnameVerifier getInstance(boolean ignoreSSLCert) { | ||
| if (ignoreSSLCert) { | ||
| return NOOP_INSTANCE; | ||
| } else { | ||
| return DEFAULT_INSTANCE; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean verify(String s, SSLSession sslSession) { | ||
| return ignoreSSLCert; | ||
| } | ||
| } |
11 changes: 0 additions & 11 deletions
11
src/main/java/com/aliyun/tea/utils/TrueHostnameVerifier.java
This file was deleted.
Oops, something went wrong.
44 changes: 41 additions & 3 deletions
44
src/main/java/com/aliyun/tea/utils/X509TrustManagerImp.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 |
|---|---|---|
| @@ -1,19 +1,57 @@ | ||
| package com.aliyun.tea.utils; | ||
|
|
||
| import javax.net.ssl.X509TrustManager; | ||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class X509TrustManagerImp implements X509TrustManager { | ||
| private List<X509TrustManager> trustManagers = new ArrayList<X509TrustManager>(); | ||
|
|
||
| private boolean ignoreSSLCert = false; | ||
|
|
||
| public boolean isIgnoreSSLCert() { | ||
| return ignoreSSLCert; | ||
| } | ||
|
|
||
| public X509TrustManagerImp(boolean ignoreSSLCert) { | ||
| this.ignoreSSLCert = ignoreSSLCert; | ||
| } | ||
|
|
||
| public X509TrustManagerImp(List<X509TrustManager> trustManagers) { | ||
| this.trustManagers = trustManagers; | ||
| } | ||
|
|
||
| @Override | ||
| public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { | ||
| public void checkClientTrusted(X509Certificate[] chain, String authType) { | ||
| // do nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { | ||
| public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| if (this.ignoreSSLCert) { | ||
| return; | ||
| } | ||
| for (X509TrustManager trustManager : this.trustManagers) { | ||
| try { | ||
| trustManager.checkServerTrusted(chain, authType); | ||
| return; // someone trusts them. success! | ||
| } catch (CertificateException e) { | ||
| // maybe someone else will trust them | ||
| } | ||
| } | ||
| throw new CertificateException("None of the TrustManagers trust this certificate chain"); | ||
| } | ||
|
|
||
| @Override | ||
| public X509Certificate[] getAcceptedIssuers() { | ||
| return new X509Certificate[0]; | ||
| List<X509Certificate> certificates = new ArrayList<X509Certificate>(); | ||
| for (X509TrustManager trustManager : this.trustManagers) { | ||
| certificates.addAll(Arrays.asList(trustManager.getAcceptedIssuers())); | ||
| } | ||
| X509Certificate[] certificatesArray = new X509Certificate[certificates.size()]; | ||
| return certificates.toArray(certificatesArray); | ||
| } | ||
| } | ||
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
92 changes: 82 additions & 10 deletions
92
src/test/java/com/aliyun/tea/utils/X509TrustManagerImpTest.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 |
|---|---|---|
| @@ -1,34 +1,106 @@ | ||
| package com.aliyun.tea.utils; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
|
|
||
| import javax.net.ssl.X509TrustManager; | ||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class X509TrustManagerImpTest { | ||
|
|
||
| @Rule | ||
| public ExpectedException thrown = ExpectedException.none(); | ||
|
|
||
| @Test | ||
| public void getSetIgnoreSSLCert() { | ||
| X509TrustManagerImp trustManager = new X509TrustManagerImp(Collections.<X509TrustManager>emptyList()); | ||
| Assert.assertFalse(trustManager.isIgnoreSSLCert()); | ||
| trustManager = new X509TrustManagerImp(true); | ||
| Assert.assertTrue(trustManager.isIgnoreSSLCert()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCheckClientTrusted(){ | ||
| public void testCheckClientTrusted() { | ||
| try { | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(); | ||
| trustManagerImp.checkClientTrusted(new X509Certificate[0],"authType"); | ||
| }catch (Exception e){ | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(Collections.<X509TrustManager>emptyList()); | ||
| trustManagerImp.checkClientTrusted(new X509Certificate[0], "authType"); | ||
| } catch (Exception e) { | ||
| Assert.fail(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCheckServerTrusted(){ | ||
| public void testCheckServerTrustedAndIgnoreSSLCert() { | ||
| try { | ||
| X509TrustManagerImp trustManager = new X509TrustManagerImp(true); | ||
| trustManager.checkServerTrusted(new X509Certificate[0], "authType"); | ||
| } catch (Exception e) { | ||
| Assert.fail(); | ||
| } | ||
|
|
||
| try { | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(); | ||
| trustManagerImp.checkServerTrusted(new X509Certificate[0],"authType"); | ||
| }catch (Exception e){ | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(Collections.<X509TrustManager>emptyList()); | ||
| trustManagerImp.checkServerTrusted(new X509Certificate[0], "authType"); | ||
| Assert.fail(); | ||
| } catch (Exception e) { | ||
| Assert.assertEquals("None of the TrustManagers trust this certificate chain", e.getMessage()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCheckServerTrustedSucceedTwice() { | ||
| try { | ||
| final X509TrustManager trustManager0 = mock(X509TrustManager.class); | ||
| doThrow(CertificateException.class).when(trustManager0).checkServerTrusted(any(X509Certificate[].class), anyString()); | ||
| final X509TrustManager trustManager1 = mock(X509TrustManager.class); | ||
| doNothing().when(trustManager0).checkServerTrusted(any(X509Certificate[].class), anyString()); | ||
| List<X509TrustManager> trustManagerList = new ArrayList<X509TrustManager>(); | ||
| trustManagerList.add(trustManager0); | ||
| trustManagerList.add(trustManager1); | ||
| X509TrustManagerImp trustManager = new X509TrustManagerImp(trustManagerList); | ||
| trustManager.checkServerTrusted(new X509Certificate[0], "authType"); | ||
| } catch (Exception e) { | ||
| Assert.fail(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCheckServerTrustedFailed() throws CertificateException { | ||
| thrown.expect(CertificateException.class); | ||
| final X509TrustManager trustManager0 = mock(X509TrustManager.class); | ||
| doThrow(CertificateException.class).when(trustManager0).checkServerTrusted(any(X509Certificate[].class), anyString()); | ||
| List<X509TrustManager> trustManagerList = new ArrayList<X509TrustManager>(); | ||
| trustManagerList.add(trustManager0); | ||
| X509TrustManagerImp trustManager = new X509TrustManagerImp(trustManagerList); | ||
| trustManager.checkServerTrusted(new X509Certificate[0], "authType"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetAcceptedIssuers(){ | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(); | ||
| public void testGetAcceptedIssuers() { | ||
| X509TrustManagerImp trustManagerImp = new X509TrustManagerImp(Collections.<X509TrustManager>emptyList()); | ||
| trustManagerImp.getAcceptedIssuers(); | ||
| X509Certificate[] res = trustManagerImp.getAcceptedIssuers(); | ||
| Assert.assertEquals(0, res.length); | ||
| final X509TrustManager trustManager0 = mock(X509TrustManager.class); | ||
| X509Certificate certificate = mock(X509Certificate.class); | ||
| when(trustManager0.getAcceptedIssuers()).thenReturn(new X509Certificate[]{certificate}); | ||
| List<X509TrustManager> trustManagerList = new ArrayList<X509TrustManager>(); | ||
| trustManagerList.add(trustManager0); | ||
| X509TrustManagerImp trustManager = new X509TrustManagerImp(trustManagerList); | ||
| res = trustManager.getAcceptedIssuers(); | ||
| Assert.assertEquals(1, res.length); | ||
| Assert.assertEquals(certificate, res[0]); | ||
| } | ||
| } |
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.
这个是不是应该把异常抛出来啊
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.
参考:https://github.com/aliyun/gm-jsse/blob/master/src/main/java/com/aliyun/gmsse/GMX509TrustManager.java#L37 异常应该抛出去。
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.
噢噢不是,这里是个链式检查,检查到最后一个还是不trust的话,会抛出CertificateException("None of the TrustManagers trust this certificate chain")