@@ -22,8 +22,9 @@ public class TLSFactory {
2222
2323 private static final Logger log = LoggerFactory .getLogger (TLSFactory .class );
2424
25- private static final boolean isDebug = log .isDebugEnabled ();
25+ private static final boolean isDebug = log .isDebugEnabled (), isTrace = log . isTraceEnabled () ;
2626
27+ // shared thread-safe random
2728 private static final SecureRandom RANDOM = new SecureRandom ();
2829
2930 public static final int MAX_HANDSHAKE_LOOPS = 200 ;
@@ -44,20 +45,19 @@ public class TLSFactory {
4445 */
4546 private static String storeType = "PKCS12" ; // JKS or PKCS12
4647
47- private static String keyStoreFile = String .format ("server.%s" , "PKCS12" .equals (storeType ) ? "p12" : "jks" );
48+ private static String keyStoreFile = String .format ("server.%s" , "PKCS12" .equals (storeType ) ? "p12" : "jks" ), trustStoreFile = String . format ( "truststore.%s" , "PKCS12" . equals ( storeType ) ? "p12" : "jks" ) ;
4849
49- private static String trustStoreFile = String . format ( "truststore.%s" , "PKCS12" . equals ( storeType ) ? "p12" : "jks" );
50+ private static String keystorePath = Paths . get ( System . getProperty ( "user.dir" ) , "conf" , keyStoreFile ). toString (), truststorePath = Paths . get ( System . getProperty ( "user.dir" ), "conf" , trustStoreFile ). toString ( );
5051
5152 private static String passwd = "password123" ;
5253
53- private static String keyFilename = Paths .get (System .getProperty ("user.dir" ), "conf" , keyStoreFile ).toString ();
54-
55- private static String trustFilename = Paths .get (System .getProperty ("user.dir" ), "conf" , trustStoreFile ).toString ();
56-
5754 static {
5855 if (isDebug ) {
59- //System.setProperty("javax.net.debug", "all");
60- System .setProperty ("javax.net.debug" , "SSL,handshake,verbose,trustmanager,keymanager,record,plaintext" );
56+ if (isTrace ) {
57+ System .setProperty ("javax.net.debug" , "SSL,handshake,verbose,trustmanager,keymanager,record,plaintext" );
58+ } else {
59+ System .setProperty ("javax.net.debug" , "all" );
60+ }
6161 }
6262 // set unlimited crypto policy
6363 Security .setProperty ("crypto.policy" , "unlimited" );
@@ -79,27 +79,27 @@ public class TLSFactory {
7979 }
8080
8181 public static SSLContext getTLSContext () throws Exception {
82- log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keyFilename , trustFilename , storeType );
82+ log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keystorePath , truststorePath , storeType );
8383 KeyStore ks = KeyStore .getInstance (storeType );
8484 KeyStore ts = KeyStore .getInstance (storeType );
8585 char [] passphrase = passwd .toCharArray ();
86- try (FileInputStream fis = new FileInputStream (keyFilename )) {
86+ try (FileInputStream fis = new FileInputStream (keystorePath )) {
8787 ks .load (fis , passphrase );
8888 } catch (Exception e ) {
89- log .error ("Failed to load keystore: {}" , keyFilename , e );
89+ log .error ("Failed to load keystore: {}" , keystorePath , e );
9090 throw e ;
9191 }
92- try (FileInputStream fis = new FileInputStream (trustFilename )) {
92+ try (FileInputStream fis = new FileInputStream (truststorePath )) {
9393 ts .load (fis , passphrase );
9494 } catch (Exception e ) {
95- log .error ("Failed to load truststore: {}" , trustFilename , e );
95+ log .error ("Failed to load truststore: {}" , truststorePath , e );
9696 throw e ;
9797 }
9898 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
9999 try {
100100 kmf .init (ks , passphrase );
101101 } catch (UnrecoverableKeyException e ) {
102- log .error ("Failed to initialize KeyManagerFactory with keystore: {}" , keyFilename , e );
102+ log .error ("Failed to initialize KeyManagerFactory with keystore: {}" , keystorePath , e );
103103 throw e ;
104104 }
105105 TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
@@ -110,21 +110,21 @@ public static SSLContext getTLSContext() throws Exception {
110110 }
111111
112112 public static SSLContext getTLSContext (String storeType , char [] passphrase ) throws Exception {
113- log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keyFilename , trustFilename , storeType );
114- log .debug ("Keystore - file name : {} password: {}" , keyFilename , passphrase );
115- log .debug ("Truststore - file name : {} password: {}" , trustFilename , passphrase );
113+ log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keystorePath , truststorePath , storeType );
114+ log .debug ("Keystore - file: {} password: {}" , keystorePath , passphrase );
115+ log .debug ("Truststore - file: {} password: {}" , truststorePath , passphrase );
116116 KeyStore ks = KeyStore .getInstance (storeType );
117117 KeyStore ts = KeyStore .getInstance (storeType );
118- try (FileInputStream fis = new FileInputStream (keyFilename )) {
118+ try (FileInputStream fis = new FileInputStream (keystorePath )) {
119119 ks .load (fis , passphrase );
120120 } catch (Exception e ) {
121- log .error ("Failed to load keystore: {}" , keyFilename , e );
121+ log .error ("Failed to load keystore: {}" , keystorePath , e );
122122 throw e ;
123123 }
124- try (FileInputStream fis = new FileInputStream (trustFilename )) {
124+ try (FileInputStream fis = new FileInputStream (truststorePath )) {
125125 ts .load (fis , passphrase );
126126 } catch (Exception e ) {
127- log .error ("Failed to load truststore: {}" , trustFilename , e );
127+ log .error ("Failed to load truststore: {}" , truststorePath , e );
128128 throw e ;
129129 }
130130 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
@@ -133,7 +133,7 @@ public static SSLContext getTLSContext(String storeType, char[] passphrase) thro
133133 log .debug ("Private key: {}" , privateKey );
134134 kmf .init (ks , passphrase );
135135 } catch (UnrecoverableKeyException e ) {
136- log .error ("Failed to initialize KeyManagerFactory with keystore: {}" , keyFilename , e );
136+ log .error ("Failed to initialize KeyManagerFactory with keystore: {}" , keystorePath , e );
137137 throw e ;
138138 }
139139 TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
@@ -143,18 +143,18 @@ public static SSLContext getTLSContext(String storeType, char[] passphrase) thro
143143 return sslCtx ;
144144 }
145145
146- public static SSLContext getTLSContext (String storeType , String keystorePassword , String keyFilename , String truststorePassword , String trustFilename ) throws Exception {
147- log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keyFilename , trustFilename , storeType );
148- log .debug ("Keystore - file name : {} password: {}" , keyFilename , keystorePassword );
149- log .debug ("Truststore - file name : {} password: {}" , trustFilename , truststorePassword );
146+ public static SSLContext getTLSContext (String storeType , String keystorePassword , String keystorePath , String truststorePassword , String truststorePath ) throws Exception {
147+ log .info ("Creating SSL context with keystore: {} and truststore: {} using {}" , keystorePath , truststorePath , storeType );
148+ log .debug ("Keystore - file: {} password: {}" , keystorePath , keystorePassword );
149+ log .debug ("Truststore - file: {} password: {}" , truststorePath , truststorePassword );
150150 KeyStore ks = KeyStore .getInstance (storeType );
151151 KeyStore ts = KeyStore .getInstance (storeType );
152152 char [] keyStrorePassphrase = keystorePassword .toCharArray ();
153153 char [] trustStorePassphrase = truststorePassword .toCharArray ();
154- try (FileInputStream fis = new FileInputStream (keyFilename )) {
154+ try (FileInputStream fis = new FileInputStream (keystorePath )) {
155155 ks .load (fis , keyStrorePassphrase );
156156 }
157- try (FileInputStream fis = new FileInputStream (trustFilename )) {
157+ try (FileInputStream fis = new FileInputStream (truststorePath )) {
158158 ts .load (fis , trustStorePassphrase );
159159 }
160160 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
@@ -208,20 +208,20 @@ public static void setPasswd(String passwd) {
208208 TLSFactory .passwd = passwd ;
209209 }
210210
211- public static String getKeyFilename () {
212- return keyFilename ;
211+ public static String getKeystorePath () {
212+ return keystorePath ;
213213 }
214214
215- public static void setKeyFilename (String keyFilename ) {
216- TLSFactory .keyFilename = keyFilename ;
215+ public static void setKeystorePath (String keystorePath ) {
216+ TLSFactory .keystorePath = keystorePath ;
217217 }
218218
219- public static String getTrustFilename () {
220- return trustFilename ;
219+ public static String getTruststorePath () {
220+ return truststorePath ;
221221 }
222222
223- public static void setTrustFilename (String trustFilename ) {
224- TLSFactory .trustFilename = trustFilename ;
223+ public static void setTruststorePath (String truststorePath ) {
224+ TLSFactory .truststorePath = truststorePath ;
225225 }
226226
227227}
0 commit comments