2020import com .sun .net .httpserver .HttpsServer ;
2121import es .tid .keyserver .config .ConfigFile ;
2222import es .tid .keyserver .database .DataBase ;
23+ import es .tid .keyserver .httpkeyserver .whitelist .WhiteList ;
2324import java .io .FileInputStream ;
2425import java .io .IOException ;
2526import java .net .InetAddress ;
3031import java .security .NoSuchAlgorithmException ;
3132import java .security .UnrecoverableKeyException ;
3233import java .security .cert .CertificateException ;
34+ import java .util .Arrays ;
35+ import java .util .List ;
36+ import java .util .concurrent .Executor ;
3337import javax .net .ssl .KeyManagerFactory ;
3438import javax .net .ssl .SSLContext ;
3539import javax .net .ssl .SSLEngine ;
@@ -55,14 +59,14 @@ public class HttpKeyServer implements CheckObject{
5559 /**
5660 * Flag for check if the object is correctly initialized.
5761 */
58- boolean isInitializated = false ;
62+ private boolean isInitializated = false ;
5963 /**
6064 * Main constructor for the HTTPS Server class.
6165 * @param parameters Object with program parameters.
6266 * @param objDB REDIS database Object.
6367 * @see <a href="http://stackoverflow.com/questions/2308479/simple-java-https-server">More info about HttpsServer</a>
6468 */
65- public HttpKeyServer (ConfigFile parameters , DataBase objDB ){
69+ public HttpKeyServer (final ConfigFile parameters , DataBase objDB ){
6670 try {
6771 // Getting the Server parameters from properties file.
6872 int port = Integer .parseInt (parameters .getParameter ("serverPort" ));
@@ -71,16 +75,16 @@ public HttpKeyServer(ConfigFile parameters, DataBase objDB){
7175 InetSocketAddress address = new InetSocketAddress ( ipaddress , port );
7276 // Create basic Server object
7377 server = HttpsServer .create ();
74- SSLContext sslContext = SSLContext .getInstance ("TLS" );
75- // initialise the keystore
78+ SSLContext sslContext = SSLContext .getInstance (parameters . getParameter ( "serverSSLContext" ) );
79+ // Initialize the key store object
7680 char [] keystorePassword = parameters .getParameter ("serverKeyPass" ).toCharArray ();
77- KeyStore ks = KeyStore .getInstance ("JKS" );
81+ KeyStore ks = KeyStore .getInstance (parameters . getParameter ( "serverKeyStore" ) );
7882 ks .load (new FileInputStream (parameters .getParameter ("serverKeyFile" )), keystorePassword );
7983 // setup the key manager factory
80- KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
84+ KeyManagerFactory kmf = KeyManagerFactory .getInstance (parameters . getParameter ( "serverKeyManagerFactory" ) );
8185 kmf .init (ks , keystorePassword );
8286 // setup the trust manager factory
83- TrustManagerFactory tmf = TrustManagerFactory .getInstance ( "SunX509" );
87+ TrustManagerFactory tmf = TrustManagerFactory .getInstance (parameters . getParameter ( "serverTrustManagerFactory" ) );
8488 tmf .init ( ks );
8589 // Setup the HTTPs context and parameters.
8690 sslContext .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
@@ -97,27 +101,62 @@ public HttpKeyServer(ConfigFile parameters, DataBase objDB){
97101 @ Override
98102 public void configure (HttpsParameters params ){
99103 try {
100- // Initialise the SSL context
104+ // Initialize the SSL context
101105 SSLContext c = SSLContext .getDefault ();
102106 SSLEngine engine = c .createSSLEngine ();
103107 params .setNeedClientAuth (false );
104108 params .setCipherSuites (engine .getEnabledCipherSuites ());
105109 params .setProtocols (engine .getEnabledProtocols ());
106110 // Get the default parameters
107111 SSLParameters defaultSSLParameters = c .getDefaultSSLParameters ();
112+
113+ if (parameters .containsKey ("serverCiphersSuites" ) && getCiphers (parameters )!=null ){
114+ String [] ciphersuites = getCiphers (parameters );
115+ defaultSSLParameters .setCipherSuites (ciphersuites );
116+ }
108117 params .setSSLParameters (defaultSSLParameters );
109118 } catch (NoSuchAlgorithmException ex ) {
110119 logger .error ("Problem with SSL context parameters." );
111120 logger .trace ("Exceiption message: {}" , ex .toString ());
112121 }
122+ }
123+ /**
124+ * This method parse the field string to array of values with
125+ * the ciphers names. If the configuration label is not present
126+ * inside configuration file. This, will use all available
127+ * ciphers by default.
128+ * @param parameters Configuration file object.
129+ * @return Array of strings with the ciphers name.
130+ */
131+ private String [] getCiphers (ConfigFile parameters ) {
132+ String [] returnValue = null ;
133+ String cipString = parameters .getParameter ("serverCiphersSuites" );
134+ if (!cipString .equalsIgnoreCase ("" )){
135+ List <String > items = Arrays .asList (cipString .split ("\\ s*,\\ s*" ));
136+ returnValue = (String []) items .toArray ();
137+ }
138+ //logger.trace("List {} of ciphers inside KeyServer configuration file: {}", returnValue.length, returnValue);
139+ return returnValue ;
113140 }
114141 });
115- // Setting config for the server and accepting only BACKLOG variable
116- // as maximum input conection (0 = System default).
142+ // Setting configuration for the server and accepting only BACKLOG variable
143+ // as maximum input connection (0 = System default).
117144 server .bind (address , backlog );
118- IncomingRequestProcess processor =new IncomingRequestProcess (objDB );
145+ WhiteList allowedIPs = new WhiteList (parameters .getParameter ("whiteList" ));
146+ IncomingRequestProcess processor =new IncomingRequestProcess (objDB , allowedIPs );
119147 server .createContext ("/" , processor );
120- server .setExecutor (processor );
148+ server .setExecutor (new Executor (){
149+ /**
150+ * Launch the runner for the incoming HTTP request. This method
151+ * call to the handle method to process the incoming HTTP proxy
152+ * request.
153+ * @param r Runnable object for the incoming petition.
154+ */
155+ @ Override
156+ public void execute (Runnable r ) {
157+ r .run ();
158+ }
159+ });
121160 // Starting server
122161 server .start ();
123162 isInitializated = true ;
0 commit comments