1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68:
69:
74: public final class SSLContextImpl extends SSLContextSpi
75: {
76: AbstractSessionContext serverContext;
77: AbstractSessionContext clientContext;
78:
79: PreSharedKeyManager pskManager;
80: X509ExtendedKeyManager keyManager;
81: X509TrustManager trustManager;
82: SRPTrustManager srpTrustManager;
83: SecureRandom random;
84:
85: public SSLContextImpl()
86: {
87: }
88:
89:
92: protected @Override SSLEngine engineCreateSSLEngine()
93: {
94: return engineCreateSSLEngine(null, -1);
95: }
96:
97:
100: protected @Override SSLEngine engineCreateSSLEngine(String host, int port)
101: {
102: return new SSLEngineImpl(this, host, port);
103: }
104:
105:
108: protected @Override synchronized SSLSessionContext engineGetClientSessionContext()
109: {
110: if (clientContext == null)
111: {
112: try
113: {
114: clientContext = AbstractSessionContext.newInstance();
115: }
116: catch (SSLException ssle)
117: {
118:
119: }
120: }
121: return clientContext;
122: }
123:
124:
127: protected @Override synchronized SSLSessionContext engineGetServerSessionContext()
128: {
129: if (serverContext == null)
130: {
131: try
132: {
133: serverContext = AbstractSessionContext.newInstance();
134: }
135: catch (SSLException ssle)
136: {
137:
138: }
139: }
140: return serverContext;
141: }
142:
143:
146: protected @Override SSLServerSocketFactory engineGetServerSocketFactory()
147: {
148: return new SSLServerSocketFactoryImpl(this);
149: }
150:
151:
154: protected @Override SSLSocketFactory engineGetSocketFactory()
155: {
156: return new SSLSocketFactoryImpl(this);
157: }
158:
159:
162: protected @Override void engineInit(KeyManager[] keyManagers,
163: TrustManager[] trustManagers,
164: SecureRandom random)
165: throws KeyManagementException
166: {
167: keyManager = null;
168: trustManager = null;
169: srpTrustManager = null;
170: if (keyManagers != null)
171: {
172: for (int i = 0; i < keyManagers.length; i++)
173: {
174: if ((keyManagers[i] instanceof X509ExtendedKeyManager)
175: && keyManager == null)
176: keyManager = (X509ExtendedKeyManager) keyManagers[i];
177: if (keyManagers[i] instanceof PreSharedKeyManager
178: && pskManager == null)
179: pskManager = (PreSharedKeyManager) keyManagers[i];
180: }
181: }
182: if (keyManager == null)
183: keyManager = defaultKeyManager();
184: if (trustManagers != null)
185: {
186: for (int i = 0; i < trustManagers.length; i++)
187: {
188: if (trustManagers[i] instanceof X509TrustManager)
189: {
190: if (trustManager == null)
191: trustManager = (X509TrustManager) trustManagers[i];
192: }
193: else if (trustManagers[i] instanceof SRPTrustManager)
194: {
195: if (srpTrustManager == null)
196: srpTrustManager = (SRPTrustManager) trustManagers[i];
197: }
198: }
199: }
200: if (trustManager == null && srpTrustManager == null)
201: {
202: trustManager = defaultTrustManager();
203: }
204: if (random != null)
205: {
206: this.random = random;
207: }
208: else
209: {
210: this.random = defaultRandom();
211: }
212: }
213:
214:
221: private X509ExtendedKeyManager defaultKeyManager() throws KeyManagementException
222: {
223: KeyManagerFactory fact = null;
224: try
225: {
226: fact = KeyManagerFactory.getInstance("JessieX509", "Jessie");
227: }
228: catch (NoSuchAlgorithmException nsae)
229: {
230: throw new KeyManagementException(nsae);
231: }
232: catch (NoSuchProviderException nspe)
233: {
234: throw new KeyManagementException(nspe);
235: }
236: try
237: {
238: fact.init(null, null);
239: return (X509ExtendedKeyManager) fact.getKeyManagers()[0];
240: }
241: catch (NoSuchAlgorithmException nsae) { }
242: catch (KeyStoreException kse) { }
243: catch (UnrecoverableKeyException uke) { }
244: catch (IllegalStateException ise) { }
245:
246: try
247: {
248: fact.init(new NullManagerParameters());
249: return (X509ExtendedKeyManager) fact.getKeyManagers()[0];
250: }
251: catch (Exception shouldNotHappen)
252: {
253: throw new Error(shouldNotHappen.toString());
254: }
255: }
256:
257:
264: private X509TrustManager defaultTrustManager() throws KeyManagementException
265: {
266: try
267: {
268: TrustManagerFactory fact =
269: TrustManagerFactory.getInstance("JessieX509", "Jessie");
270: fact.init((KeyStore) null);
271: return (X509TrustManager) fact.getTrustManagers()[0];
272: }
273: catch (NoSuchAlgorithmException nsae)
274: {
275: throw new KeyManagementException(nsae);
276: }
277: catch (NoSuchProviderException nspe)
278: {
279: throw new KeyManagementException(nspe);
280: }
281: catch (KeyStoreException kse)
282: {
283: throw new KeyManagementException(kse);
284: }
285: }
286:
287:
296: private SecureRandom defaultRandom()
297: {
298: GetSecurityPropertyAction gspa
299: = new GetSecurityPropertyAction("gnu.javax.net.ssl.secureRandom");
300: String alg = AccessController.doPrivileged(gspa);
301: if (alg == null)
302: alg = "Fortuna";
303: SecureRandom rand = null;
304: try
305: {
306: rand = SecureRandom.getInstance(alg);
307: }
308: catch (NoSuchAlgorithmException nsae)
309: {
310: rand = new SecureRandom();
311: }
312:
313: return rand;
314: }
315: }