1
1
package com .acuity .iot .dsa .dslink .sys .cert ;
2
2
3
3
import java .security .KeyStore ;
4
+ import java .security .NoSuchAlgorithmException ;
5
+ import java .security .NoSuchProviderException ;
4
6
import java .security .Provider ;
5
7
import java .security .Security ;
6
8
import java .security .cert .CertificateException ;
9
+ import java .security .cert .PKIXCertPathBuilderResult ;
10
+ import java .security .cert .TrustAnchor ;
7
11
import java .security .cert .X509Certificate ;
8
12
import java .util .Arrays ;
13
+ import java .util .Collections ;
14
+ import java .util .HashSet ;
9
15
import java .util .List ;
16
+ import java .util .Set ;
10
17
import javax .net .ssl .*;
11
18
12
19
/**
13
20
* Adds support for self signed SSL. If anonymous is not allowed
14
21
* falls back to the default Java trust manager.
15
22
*
16
23
* @author Aaron Hansen
24
+ * @author Daniel Shapiro
17
25
*/
18
26
public class AnonymousTrustFactory extends TrustManagerFactorySpi {
19
27
@@ -114,8 +122,13 @@ public void checkClientTrusted(X509Certificate[] chain, String authType)
114
122
return ;
115
123
}
116
124
if (defaultX509Mgr != null ) {
117
- defaultX509Mgr .checkClientTrusted (chain , authType );
125
+ try {
126
+ defaultX509Mgr .checkClientTrusted (chain , authType );
127
+ return ;
128
+ } catch (CertificateException e ) {
129
+ }
118
130
}
131
+ checkLocally (chain , authType );
119
132
}
120
133
121
134
@ Override
@@ -125,7 +138,43 @@ public void checkServerTrusted(X509Certificate[] chain, String authType)
125
138
return ;
126
139
}
127
140
if (defaultX509Mgr != null ) {
128
- defaultX509Mgr .checkServerTrusted (chain , authType );
141
+ try {
142
+ defaultX509Mgr .checkServerTrusted (chain , authType );
143
+ return ;
144
+ } catch (CertificateException e ) {
145
+ }
146
+ }
147
+ checkLocally (chain , authType );
148
+ }
149
+
150
+ private void checkLocally (X509Certificate [] chain , String authType ) throws CertificateException {
151
+ Set <X509Certificate > chainAsSet = new HashSet <X509Certificate >();
152
+ Collections .addAll (chainAsSet , chain );
153
+ X509Certificate anchorCert ;
154
+ try {
155
+ if (CertificateVerifier .isSelfSigned (chain [0 ])) {
156
+ anchorCert = chain [0 ];
157
+ } else {
158
+ PKIXCertPathBuilderResult result = CertificateVerifier .verifyCertificate (chain [0 ], chainAsSet );
159
+ TrustAnchor anchor = result .getTrustAnchor ();
160
+ anchorCert = anchor .getTrustedCert ();
161
+ }
162
+
163
+ if (anchorCert == null ) {
164
+ throw new CertificateException ();
165
+ }
166
+
167
+ if (!certManager .isInTrustStore (anchorCert )) {
168
+ certManager .addToQuarantine (anchorCert );
169
+ throw new CertificateException ();
170
+ }
171
+
172
+ } catch (CertificateVerificationException e1 ) {
173
+ throw new CertificateException ();
174
+ } catch (NoSuchAlgorithmException e ) {
175
+ throw new CertificateException ();
176
+ } catch (NoSuchProviderException e ) {
177
+ throw new CertificateException ();
129
178
}
130
179
}
131
180
0 commit comments