44import java .io .IOException ;
55import java .security .cert .CertificateEncodingException ;
66import java .security .cert .X509Certificate ;
7+ import javax .net .ssl .HostnameVerifier ;
8+ import javax .net .ssl .HttpsURLConnection ;
9+ import javax .net .ssl .SSLSession ;
710//import org.bouncycastle.asn1.ASN1ObjectIdentifier;
811//import org.bouncycastle.asn1.x500.X500Name;
912//import org.bouncycastle.asn1.x509.BasicConstraints;
@@ -47,6 +50,7 @@ public class SysCertManager extends DSNode {
4750
4851 private static final String ALLOW_CLIENTS = "Allow_Anonymous_Clients" ;
4952 private static final String ALLOW_SERVERS = "Allow_Anonymous_Servers" ;
53+ private static final String VERIFY_HOSTNAMES = "Enable Hostname Verification" ;
5054 private static final String CERTFILE = "Cert_File" ;
5155 private static final String CERTFILE_PASS = "Cert_File_Pass" ;
5256 private static final String CERTFILE_TYPE = "Cert_File_Type" ;
@@ -64,11 +68,27 @@ public class SysCertManager extends DSNode {
6468
6569 private DSInfo allowClients = getInfo (ALLOW_CLIENTS );
6670 private DSInfo allowServers = getInfo (ALLOW_SERVERS );
67- private DSInfo keystore = getInfo (CERTFILE );
71+ private DSInfo verifyHostnames = getInfo (VERIFY_HOSTNAMES );
72+ private DSInfo keystorePath = getInfo (CERTFILE );
6873 private DSInfo keystorePass = getInfo (CERTFILE_PASS );
6974 private DSInfo keystoreType = getInfo (CERTFILE_TYPE );
7075 private CertCollection localTruststore ;
7176 private CertCollection quarantine ;
77+ private static SysCertManager inst ;
78+
79+ private static HostnameVerifier oldHostnameVerifier = HttpsURLConnection .getDefaultHostnameVerifier ();
80+ private HostnameVerifier hostnameVerifier = new SysHostnameVerifier ();
81+
82+ public SysCertManager () {
83+ }
84+
85+ public static SysCertManager getInstance () {
86+ return inst ;
87+ }
88+
89+ public HostnameVerifier getHostnameVerifier () {
90+ return hostnameVerifier ;
91+ }
7292
7393 private CertCollection getLocalTruststore () {
7494 if (localTruststore == null ) {
@@ -100,11 +120,16 @@ public boolean allowAnonymousClients() {
100120 public boolean allowAnonymousServers () {
101121 return allowServers .getElement ().toBoolean ();
102122 }
123+
124+ public boolean hostnameVerificationEnabled () {
125+ return verifyHostnames .getElement ().toBoolean ();
126+ }
103127
104128 @ Override
105129 public void declareDefaults () {
106130 declareDefault (ALLOW_CLIENTS , DSBool .FALSE );
107131 declareDefault (ALLOW_SERVERS , DSBool .TRUE );
132+ declareDefault (VERIFY_HOSTNAMES , DSBool .TRUE );
108133 declareDefault (CERTFILE , DSString .valueOf ("dslink.jks" ));
109134 declareDefault (CERTFILE_TYPE , DSString .valueOf ("JKS" ));
110135 declareDefault (CERTFILE_PASS , DSPasswordAes128 .valueOf ("dsarocks" ));
@@ -265,7 +290,7 @@ private String getCertFilePass() {
265290 }
266291
267292 private String getKeystorePath () {
268- return keystore .getElement ().toString ();
293+ return keystorePath .getElement ().toString ();
269294 }
270295
271296 /**
@@ -277,8 +302,9 @@ private void keytoolGenkey() {
277302
278303 @ Override
279304 public void onStarted () {
305+ inst = this ;
280306 AnonymousTrustFactory .init (this );
281- String keystore = this .keystore .getElement ().toString ();
307+ String keystore = this .keystorePath .getElement ().toString ();
282308 File f = new File (keystore );
283309 if (!f .exists ()) {
284310 keytoolGenkey ();
@@ -291,6 +317,7 @@ public void onStarted() {
291317 } catch (Exception x ) {
292318 error (getParent (), x );
293319 }
320+ HttpsURLConnection .setDefaultHostnameVerifier (hostnameVerifier );
294321 }
295322
296323 public boolean isInTrustStore (X509Certificate cert ) {
@@ -313,6 +340,18 @@ public void allow(DSInfo certInfo) {
313340 getLocalTruststore ().addCertificate (name , certStr );
314341 }
315342
343+ private class SysHostnameVerifier implements HostnameVerifier {
344+ @ Override
345+ public boolean verify (String hostname , SSLSession session ) {
346+ if (hostnameVerificationEnabled ()) {
347+ //TODO implement whitelist
348+ return oldHostnameVerifier .verify (hostname , session );
349+ } else {
350+ return true ;
351+ }
352+ }
353+ }
354+
316355// private static String generateCSR() {
317356// KeyPairGenerator keyGen;
318357// try {
0 commit comments