8000 - Updated logic used to initialize SSLContext in the HttpServlet class. · javaxt-project/javaxt-jetty@63a9cdc · GitHub
[go: up one dir, main page]

Skip to content

Commit 63a9cdc

Browse files
author
pborissow
committed
- Updated logic used to initialize SSLContext in the HttpServlet class.
- Added new HttpServlet.setKeyManager() method. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-jetty@846 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 3efcb60 commit 63a9cdc

File tree

3 files changed

+75
-87
lines changed

3 files changed

+75
-87
lines changed

src/javaxt/http/Server.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -204,42 +204,47 @@ public void run() {
204204
//Create a new SocketListener for each port/address
205205
for (InetSocketAddress address : addresses){
206206
String hostName = address.getHostName();
207-
208-
HttpConnectionFactory http1 = new HttpConnectionFactory(httpConfig);
209-
//HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(httpConfig);
210-
//HTTP2ServerConnectionFactory http2c = new HTTP2ServerConnectionFactory(httpConfig);
211-
212-
213-
//Create server connector
214-
ServerConnector http;
215-
javax.net.ssl.SSLContext sslContext = servlet.getSSLContext();
216-
if (sslContext!=null){
217-
SslContextFactory sslContextFactory = new SslContextFactory();
218-
sslContextFactory.setExcludeCipherSuites( //For TLSv1 and TLSv1.1
219-
"SSL_RSA_WITH_DES_CBC_SHA",
220-
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
221-
"SSL_DHE_DSS_WITH_DES_CBC_SHA",
222-
"SSL_RSA_EXPORT_WITH_RC4_40_MD5",
223-
"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
224-
"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
225-
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"
226-
);
227-
sslContextFactory.setSslContext(sslContext);
228-
_SslConnectionFactory ssl = new _SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
229-
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
230-
http = new ServerConnector(server, ssl, new HttpConnectionFactory(httpsConfig));
231-
}
232-
else{
233-
http = new ServerConnector(server, http1); // new ServerConnector(server, http1, http2, http2c)
207+
try{
208+
209+
HttpConnectionFactory http1 = new HttpConnectionFactory(httpConfig);
210+
//HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(httpConfig);
211+
//HTTP2ServerConnectionFactory http2c = new HTTP2ServerConnectionFactory(httpConfig);
212+
213+
214+
//Create server connector
215+
ServerConnector http;
216+
javax.net.ssl.SSLContext sslContext = servlet.getSSLContext();
217+
if (sslContext!=null){
218+
SslContextFactory sslContextFactory = new SslContextFactory();
219+
sslContextFactory.setExcludeCipherSuites( //For TLSv1 and TLSv1.1
220+
"SSL_RSA_WITH_DES_CBC_SHA",
221+
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
222+
"SSL_DHE_DSS_WITH_DES_CBC_SHA",
223+
"SSL_RSA_EXPORT_WITH_RC4_40_MD5",
224+
"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
225+
"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
226+
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"
227+
);
228+
sslContextFactory.setSslContext(sslContext);
229+
_SslConnectionFactory ssl = new _SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
230+
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
231+
http = new ServerConnector(server, ssl, new HttpConnectionFactory(httpsConfig));
232+
}
233+
else{
234+
http = new ServerConnector(server, http1); // new ServerConnector(server, http1, http2, http2c)
235+
}
236+
237+
238+
http.setHost(hostName);
239+
http.setPort(address.getPort());
240+
http.setIdleTimeout(30000);
241+
server.addConnector(http);
242+
243+
System.out.print("Accepting connections on " + hostName + ":" + address.getPort() + "\r\n");
244+
}
245+
catch(Exception e){
246+
e.printStackTrace();
234247
}
235-
236-
237-
http.setHost(hostName);
238-
http.setPort(address.getPort());
239-
http.setIdleTimeout(30000);
240-
server.addConnector(http);
241-
242-
System.out.print("Accepting connections on " + hostName + ":" + address.getPort() + "\r\n");
243248
}
244249

245250

src/javaxt/http/TODOs.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
How to handle:
2-
- Sessions
2+
- Session
33
- Cookies
44
- Range requests
5+
- DoS Filter
56

67

78
Changes to the Jetty source:
@@ -41,4 +42,11 @@ To circumvent this issue, I added a try/catch block in the org.eclipse.jetty.ser
4142
Commented out unused import for org.eclipse.jetty.server.handler.RequestLogHandler;
4243

4344
(4) org.eclipse.jetty.util.log.Log
44-
Made the getMutableLoggers() method public so I can replace/update loggers assigned at startup.
45+
Made the getMutableLoggers() method public so I can replace/update loggers assigned at startup.
46+
47+
48+
49+
50+
Bugs:
51+
- HttpServletResponse.sendRedirect will cause the server to hang and log file to fill up.
52+
As a workaround the caller must return immediately after calling sendRedirect.

src/javaxt/http/servlet/HttpServlet.java

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
public abstract class HttpServlet {
2929

3030
private Authenticator authenticator;
31-
private KeyStore keystore;
32-
private KeyStore truststore;
33-
private KeyManagerFactory kmf;
34-
private TrustManagerFactory tmf;
31+
private javax.net.ssl.KeyManager[] kms;
32+
private javax.net.ssl.TrustManager[] tms;
3533
private String sslProvider;
3634
private ServletContext servletContext;
3735
private SessionDataStore sessionStore;
3836
private RequestHandler handler;
3937

40-
//This variable are used in the HttpServletRequest class.
41-
//protected String servletPath = "";
4238

4339

4440
//**************************************************************************
@@ -125,25 +121,21 @@ protected Authenticator getAuthenticator(HttpServletRequest request){
125121
//**************************************************************************
126122
//** setKeyStore
127123
//**************************************************************************
128-
/** Used to set the KeyStore and initialize the KeyManagerFactory. The
129-
* KeyStore is used to store keys and certificates for SSL.
124+
/** Used to specify a KeyStore. The KeyStore is used to store keys and
125+
* certificates for SSL.
130126
*/
131127
public void setKeyStore(KeyStore keystore, String passphrase) throws Exception {
132-
133-
//Update class variable
134-
this.keystore = keystore;
135-
136-
//Initialize the KeyManagerFactory
137-
kmf = KeyManagerFactory.getInstance("SunX509");
128+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
138129
kmf.init(keystore, passphrase.toCharArray());
130+
kms = kmf.getKeyManagers();
139131
}
140132

141133

142134
//**************************************************************************
143135
//** setKeyStore
144136
//**************************************************************************
145-
/** Used to set the KeyStore and initialize the KeyManagerFactory. The
146-
* KeyStore is used to store keys and certificates for SSL.
137+
/** Used to specify a KeyStore. The KeyStore is used to store keys and
138+
* certificates for SSL.
147139
*/
148140
public void setKeyStore(java.io.File keyStoreFile, String passphrase) throws Exception {
149141
char[] pw = passphrase.toCharArray();
@@ -154,12 +146,16 @@ public void setKeyStore(java.io.File keyStoreFile, String passphrase) throws Exc
154146

155147

156148
//**************************************************************************
157-
//** getKeyStore
149+
//** setKeyManager
158150
//**************************************************************************
159-
/** Returns the the KeyStore associated with this Servlet.
151+
/** Used to specify a KeyManager. The KeyManager is responsible for managing
152+
* keys and certificates found in a KeyStore. Typically, you are not
153+
* required to specify a KeyManager. Instead, a KeyManager is selected when
154+
* you call the setKeyStore method. This method is intended for users who
155+
* require more fine grained control over the SSLEngine.
160156
*/
161-
public KeyStore getKeyStore(){
162-
return keystore;
157+
public void setKeyManager(javax.net.ssl.KeyManager keyManager) throws Exception {
158+
kms = new javax.net.ssl.KeyManager[]{keyManager};
163159
}
164160

165161

@@ -170,13 +166,9 @@ public KeyStore getKeyStore(){
170166
* TrustStore is used to store public keys and certificates for SSL.
171167
*/
172168
public void setTrustStore(KeyStore truststore) throws Exception {
173-
174-
//Update class variable
175-
this.truststore = truststore;
176-
177-
//Initialize the TrustManagerFactory
178-
tmf = TrustManagerFactory.getInstance("SunX509");
169+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
179170
tmf.init(truststore);
171+
tms = tmf.getTrustManagers();
180172
}
181173

182174

@@ -194,16 +186,6 @@ public void setTrustStore(java.io.File trustStoreFile, String passphrase) throws
194186
}
195187

196188

197-
//**************************************************************************
198-
//** getTrustStore
199-
//**************************************************************************
200-
/** Returns the the TrustStore associated with this Servlet.
201-
*/
202-
public KeyStore getTrustStore(){
203-
return truststore;
204-
}
205-
206-
207189
//**************************************************************************
208190
//** setSSLProvider
209191
//**************************************************************************
@@ -229,12 +211,12 @@ public void setSSLProvider(String provider){
229211

230212

231213
//**************************************************************************
232-
//** SSLContext
214+
//** getSSLContext
233215
//**************************************************************************
234-
/** Used to instantiate an SSLEngine used to decrypt SSL/TLS messages.
216+
/** Used to instantiate an SSLContext which, in turn is used by an SSLEngine
217+
* decrypt SSL/TLS messages.
235218
*/
236-
public SSLContext getSSLContext() {
237-
219+
public SSLContext getSSLContext() throws ServletException {
238220

239221
/*//Debug use only!
240222
java.security.Provider provider = new SSLProvider();
@@ -243,23 +225,16 @@ public SSLContext getSSLContext() {
243225
*/
244226

245227

246-
javax.net.ssl.KeyManager[] km = null;
247-
javax.net.ssl.TrustManager[] tm = null;
248-
249-
if (kmf!=null) km = kmf.getKeyManagers();
250-
if (tmf!=null) tm = tmf.getTrustManagers();
251-
252-
253228
SSLContext sslContext = null;
254229
try{
255230
if (sslProvider==null) sslContext = SSLContext.getInstance("TLS");
256231
else sslContext = SSLContext.getInstance("TLS", sslProvider);
257-
sslContext.init(km, tm, null);
232+
sslContext.init(kms, tms, null);
258233
}
259234
catch(Exception e){
260-
ServletException se = new ServletException("Failed to instantiate SSLEngine.");
235+
ServletException se = new ServletException("Failed to initialize SSLContext.");
261236
se.initCause(e);
262-
//throw se;
237+
throw se;
263238
}
264239

265240
return sslContext;

0 commit comments

Comments
 (0)
0