2323//import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
2424import org .iot .dsa .node .DSBool ;
2525import org .iot .dsa .node .DSInfo ;
26+ import org .iot .dsa .node .DSList ;
2627import org .iot .dsa .node .DSMap ;
2728import org .iot .dsa .node .DSNode ;
2829import org .iot .dsa .node .DSString ;
3435import org .iot .dsa .node .action .DSActionValues ;
3536import org .iot .dsa .security .DSPasswordAes128 ;
3637import org .iot .dsa .util .DSException ;
38+ import com .acuity .iot .dsa .dslink .sys .cert .HostnameWhitelist .WhitelistValue ;
3739
3840/**
3941 * Certificate management for the whole process. This is basically a stub for future
@@ -51,6 +53,7 @@ public class SysCertManager extends DSNode {
5153 private static final String ALLOW_CLIENTS = "Allow_Anonymous_Clients" ;
5254 private static final String ALLOW_SERVERS = "Allow_Anonymous_Servers" ;
5355 private static final String VERIFY_HOSTNAMES = "Enable Hostname Verification" ;
56+ private static final String HOSTNAME_WHITELIST = "Hostname Whitelist" ;
5457 private static final String CERTFILE = "Cert_File" ;
5558 private static final String CERTFILE_PASS = "Cert_File_Pass" ;
5659 private static final String CERTFILE_TYPE = "Cert_File_Type" ;
@@ -74,6 +77,7 @@ public class SysCertManager extends DSNode {
7477 private DSInfo keystoreType = getInfo (CERTFILE_TYPE );
7578 private CertCollection localTruststore ;
7679 private CertCollection quarantine ;
80+ private HostnameWhitelist whitelist ;
7781 private static SysCertManager inst ;
7882
7983 private static HostnameVerifier oldHostnameVerifier = HttpsURLConnection .getDefaultHostnameVerifier ();
@@ -104,6 +108,13 @@ private CertCollection getQuarantine() {
104108 return quarantine ;
105109 }
106110
111+ private HostnameWhitelist getHostnameWhitelist () {
112+ if (whitelist == null ) {
113+ whitelist = (HostnameWhitelist ) getInfo (HOSTNAME_WHITELIST ).getObject ();
114+ }
115+ return whitelist ;
116+ }
117+
107118 // Methods
108119 // -------
109120
@@ -130,6 +141,7 @@ public void declareDefaults() {
130141 declareDefault (ALLOW_CLIENTS , DSBool .FALSE );
131142 declareDefault (ALLOW_SERVERS , DSBool .TRUE );
132143 declareDefault (VERIFY_HOSTNAMES , DSBool .TRUE );
144+ declareDefault (HOSTNAME_WHITELIST , new HostnameWhitelist ());
133145 declareDefault (CERTFILE , DSString .valueOf ("dslink.jks" ));
134146 declareDefault (CERTFILE_TYPE , DSString .valueOf ("JKS" ));
135147 declareDefault (CERTFILE_PASS , DSPasswordAes128 .valueOf ("dsarocks" ));
@@ -343,8 +355,18 @@ public void allow(DSInfo certInfo) {
343355 private class SysHostnameVerifier implements HostnameVerifier {
344356 @ Override
345357 public boolean verify (String hostname , SSLSession session ) {
358+ if (getHostnameWhitelist ().isEnabled ()) {
359+ WhitelistValue wlval = getHostnameWhitelist ().checkHostname (hostname );
360+ if (wlval != null ) {
361+ switch (wlval ) {
362+ case ALLOWED :
363+ return true ;
364+ case FORBIDDEN :
365+ return false ;
366+ }
367+ }
368+ }
346369 if (hostnameVerificationEnabled ()) {
347- //TODO implement whitelist
348370 return oldHostnameVerifier .verify (hostname , session );
349371 } else {
350372 return true ;
0 commit comments