11package com .acuity .iot .dsa .dslink .sys .cert ;
22
33import java .security .KeyStore ;
4+ import java .security .KeyStoreException ;
45import java .security .NoSuchAlgorithmException ;
56import java .security .NoSuchProviderException ;
67import java .security .Provider ;
1920import javax .net .ssl .TrustManagerFactory ;
2021import javax .net .ssl .TrustManagerFactorySpi ;
2122import javax .net .ssl .X509TrustManager ;
23+ import org .iot .dsa .logging .DSLogger ;
2224
2325/**
2426 * Adds support for self signed SSL. If anonymous is not allowed
@@ -33,9 +35,12 @@ public class AnonymousTrustFactory extends TrustManagerFactorySpi {
3335 // Fields
3436 /////////////////////////////////////////////////////////////////
3537
38+ private static String defaultAlgorithm = TrustManagerFactory .getDefaultAlgorithm ();
3639 private static SysCertService certManager ;
3740 private static X509TrustManager defaultX509Mgr ;
41+ private static X509TrustManager localX509Mgr ;
3842 private static TrustManager [] trustManagers ;
43+ private static DSLogger log = new DSLogger ();
3944
4045 /////////////////////////////////////////////////////////////////
4146 // Methods - Public and in alphabetical order by method TrustAnon.
@@ -53,15 +58,26 @@ public void engineInit(KeyStore ks) {
5358 @ Override
5459 public void engineInit (ManagerFactoryParameters spec ) {
5560 }
61+
62+ // This gets called once on startup, and again every time a new certificate is added to the local truststore.
63+ public static void initLocalTrustManager () throws NoSuchAlgorithmException , KeyStoreException {
64+ TrustManagerFactory fac = TrustManagerFactory .getInstance (defaultAlgorithm );
65+ fac .init (certManager .getLocalTruststore ());
66+ for (TrustManager locTm : fac .getTrustManagers ()) {
67+ if (locTm instanceof X509TrustManager ) {
68+ localX509Mgr = (X509TrustManager ) locTm ;
69+ break ;
70+ }
71+ }
72+ }
5673
5774 /**
5875 * Captures the default trust factory and installs this one.
5976 */
6077 static void init (SysCertService mgr ) {
6178 certManager = mgr ;
6279 try {
63- TrustManagerFactory fac = TrustManagerFactory .getInstance (
64- TrustManagerFactory .getDefaultAlgorithm ());
80+ TrustManagerFactory fac = TrustManagerFactory .getInstance (defaultAlgorithm );
6581 fac .init ((KeyStore ) null );
6682 trustManagers = fac .getTrustManagers ();
6783 if (trustManagers == null ) {
@@ -82,6 +98,8 @@ static void init(SysCertService mgr) {
8298 list .add (new MyTrustManager ());
8399 trustManagers = list .toArray (new TrustManager [list .size ()]);
84100 }
101+
102+ initLocalTrustManager ();
85103 } catch (Exception x ) {
86104 certManager .error (certManager .getPath (), x );
87105 }
@@ -129,9 +147,15 @@ public void checkClientTrusted(X509Certificate[] chain, String authType)
129147 defaultX509Mgr .checkClientTrusted (chain , authType );
130148 return ;
131149 } catch (CertificateException e ) {
150+ try {
151+ localX509Mgr .checkClientTrusted (chain , authType );
152+ return ;
153+ } catch (CertificateException e1 ) {
154+ tryAddingRootCertToQuarantine (chain , authType );
155+ throw e1 ;
156+ }
132157 }
133158 }
134- checkLocally (chain , authType );
135159 }
136160
137161 @ Override
@@ -145,9 +169,15 @@ public void checkServerTrusted(X509Certificate[] chain, String authType)
145169 defaultX509Mgr .checkServerTrusted (chain , authType );
146170 return ;
147171 } catch (CertificateException e ) {
172+ try {
173+ localX509Mgr .checkServerTrusted (chain , authType );
174+ return ;
175+ } catch (CertificateException e1 ) {
176+ tryAddingRootCertToQuarantine (chain , authType );
177+ throw e1 ;
178+ }
148179 }
149180 }
150- checkLocally (chain , authType );
151181 }
152182
153183 @ Override
@@ -158,7 +188,7 @@ public X509Certificate[] getAcceptedIssuers() {
158188 return new X509Certificate [0 ];
159189 }
160190
161- private void checkLocally (X509Certificate [] chain , String authType )
191+ private void tryAddingRootCertToQuarantine (X509Certificate [] chain , String authType )
162192 throws CertificateException {
163193 Set <X509Certificate > chainAsSet = new HashSet <X509Certificate >();
164194 Collections .addAll (chainAsSet , chain );
@@ -174,20 +204,16 @@ private void checkLocally(X509Certificate[] chain, String authType)
174204 }
175205
176206 if (anchorCert == null ) {
177- throw new CertificateException ();
178- }
179-
180- if (!certManager .isInTrustStore (anchorCert )) {
181- certManager .addToQuarantine (anchorCert );
182- throw new CertificateException ();
207+ return ;
183208 }
184209
185- } catch (CertificateVerificationException e1 ) {
186- throw new CertificateException ();
210+ certManager .addToQuarantine (anchorCert );
211+ } catch (CertificateVerificationException e ) {
212+ log .debug ("" , e );
187213 } catch (NoSuchAlgorithmException e ) {
188- throw new CertificateException ( );
214+ log . debug ( "" , e );
189215 } catch (NoSuchProviderException e ) {
190- throw new CertificateException ( );
216+ log . debug ( "" , e );
191217 }
192218 }
193219
0 commit comments