28
28
public abstract class HttpServlet {
29
29
30
30
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 ;
35
33
private String sslProvider ;
36
34
private ServletContext servletContext ;
37
35
private SessionDataStore sessionStore ;
38
36
private RequestHandler handler ;
39
37
40
- //This variable are used in the HttpServletRequest class.
41
- //protected String servletPath = "";
42
38
43
39
44
40
//**************************************************************************
@@ -125,25 +121,21 @@ protected Authenticator getAuthenticator(HttpServletRequest request){
125
121
//**************************************************************************
126
122
//** setKeyStore
127
123
//**************************************************************************
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.
130
126
*/
131
127
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" );
138
129
kmf .init (keystore , passphrase .toCharArray ());
130
+ kms = kmf .getKeyManagers ();
139
131
}
140
132
141
133
142
134
//**************************************************************************
143
135
//** setKeyStore
144
136
//**************************************************************************
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.
147
139
*/
148
140
public void setKeyStore (java .io .File keyStoreFile , String passphrase ) throws Exception {
149
141
char [] pw = passphrase .toCharArray ();
@@ -154,12 +146,16 @@ public void setKeyStore(java.io.File keyStoreFile, String passphrase) throws Exc
154
146
155
147
156
148
//**************************************************************************
157
- //** getKeyStore
149
+ //** setKeyManager
158
150
//**************************************************************************
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.
160
156
*/
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 } ;
163
159
}
164
160
165
161
@@ -170,13 +166,9 @@ public KeyStore getKeyStore(){
170
166
* TrustStore is used to store public keys and certificates for SSL.
171
167
*/
172
168
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" );
179
170
tmf .init (truststore );
171
+ tms = tmf .getTrustManagers ();
180
172
}
181
173
182
174
@@ -194,16 +186,6 @@ public void setTrustStore(java.io.File trustStoreFile, String passphrase) throws
194
186
}
195
187
196
188
197
- //**************************************************************************
198
- //** getTrustStore
199
- //**************************************************************************
200
- /** Returns the the TrustStore associated with this Servlet.
201
- */
202
- public KeyStore getTrustStore (){
203
- return truststore ;
204
- }
205
-
206
-
207
189
//**************************************************************************
208
190
//** setSSLProvider
209
191
//**************************************************************************
@@ -229,12 +211,12 @@ public void setSSLProvider(String provider){
229
211
230
212
231
213
//**************************************************************************
232
- //** SSLContext
214
+ //** getSSLContext
233
215
//**************************************************************************
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.
235
218
*/
236
- public SSLContext getSSLContext () {
237
-
219
+ public SSLContext getSSLContext () throws ServletException {
238
220
239
221
/*//Debug use only!
240
222
java.security.Provider provider = new SSLProvider();
@@ -243,23 +225,16 @@ public SSLContext getSSLContext() {
243
225
*/
244
226
245
227
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
-
253
228
SSLContext sslContext = null ;
254
229
try {
255
230
if (sslProvider ==null ) sslContext = SSLContext .getInstance ("TLS" );
256
231
else sslContext = SSLContext .getInstance ("TLS" , sslProvider );
257
- sslContext .init (km , tm , null );
232
+ sslContext .init (kms , tms , null );
258
233
}
259
234
catch (Exception e ){
260
- ServletException se = new ServletException ("Failed to instantiate SSLEngine ." );
235
+ ServletException se = new ServletException ("Failed to initialize SSLContext ." );
261
236
se .initCause (e );
262
- // throw se;
237
+ throw se ;
263
238
}
264
239
265
240
return sslContext ;
0 commit comments