1 |
14 |
jlechner |
/* SSLContext.java -- an SSL protocol context.
|
2 |
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.net.ssl;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Engine;
|
42 |
|
|
|
43 |
|
|
import java.lang.reflect.InvocationTargetException;
|
44 |
|
|
import java.security.KeyManagementException;
|
45 |
|
|
import java.security.NoSuchAlgorithmException;
|
46 |
|
|
import java.security.NoSuchProviderException;
|
47 |
|
|
import java.security.Provider;
|
48 |
|
|
import java.security.SecureRandom;
|
49 |
|
|
import java.security.Security;
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* A "meta-factory" for protocol-specific socket and server socket
|
53 |
|
|
* factories. This class serves as a clearinghouse for socket
|
54 |
|
|
* factories and cached session contexts for a particular protocol,
|
55 |
|
|
* such as SSLv3.
|
56 |
|
|
*
|
57 |
|
|
* @author Casey Marshall (rsdio@metastatic.org)
|
58 |
|
|
*/
|
59 |
|
|
public class SSLContext
|
60 |
|
|
{
|
61 |
|
|
// Constants and fields.
|
62 |
|
|
// ------------------------------------------------------------------
|
63 |
|
|
|
64 |
|
|
/** Service name for SSL contexts. */
|
65 |
|
|
private static final String SSL_CONTEXT = "SSLContext";
|
66 |
|
|
|
67 |
|
|
/** The underlying engine. */
|
68 |
|
|
private final SSLContextSpi ctxSpi;
|
69 |
|
|
|
70 |
|
|
/** The provider of the engine class. */
|
71 |
|
|
private final Provider provider;
|
72 |
|
|
|
73 |
|
|
/** The protocal name. */
|
74 |
|
|
private final String protocol;
|
75 |
|
|
|
76 |
|
|
// Constructor.
|
77 |
|
|
// ------------------------------------------------------------------
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Create a new SSL context.
|
81 |
|
|
*
|
82 |
|
|
* @param ctxSpi The context engine.
|
83 |
|
|
* @param provider The provider of the implementation.
|
84 |
|
|
* @param protocol The name of the SSL protocol.
|
85 |
|
|
*/
|
86 |
|
|
protected SSLContext(SSLContextSpi ctxSpi, Provider provider,
|
87 |
|
|
String protocol)
|
88 |
|
|
{
|
89 |
|
|
this.ctxSpi = ctxSpi;
|
90 |
|
|
this.provider = provider;
|
91 |
|
|
this.protocol = protocol;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
// Class methods.
|
95 |
|
|
// ------------------------------------------------------------------
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Get an instance of a context for the specified protocol from the
|
99 |
|
|
* first provider that implements it.
|
100 |
|
|
*
|
101 |
|
|
* @param protocol The name of the protocol to get a context for.
|
102 |
|
|
* @return The new context.
|
103 |
|
|
* @throws NoSuchAlgorithm If no provider implements the given
|
104 |
|
|
* protocol.
|
105 |
|
|
*/
|
106 |
|
|
public static final SSLContext getInstance(String protocol)
|
107 |
|
|
throws NoSuchAlgorithmException
|
108 |
|
|
{
|
109 |
|
|
Provider[] provs = Security.getProviders();
|
110 |
|
|
for (int i = 0; i < provs.length; i++)
|
111 |
|
|
{
|
112 |
|
|
try
|
113 |
|
|
{
|
114 |
|
|
return getInstance(protocol, provs[i]);
|
115 |
|
|
}
|
116 |
|
|
catch (NoSuchAlgorithmException ignore)
|
117 |
|
|
{
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
throw new NoSuchAlgorithmException(protocol);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
/**
|
124 |
|
|
* Get an instance of a context for the specified protocol from the
|
125 |
|
|
* named provider.
|
126 |
|
|
*
|
127 |
|
|
* @param protocol The name of the protocol to get a context for.
|
128 |
|
|
* @param provider The name of the provider to get the
|
129 |
|
|
* implementation from.
|
130 |
|
|
* @return The new context.
|
131 |
|
|
* @throws NoSuchAlgorithmException If the provider does not
|
132 |
|
|
* implement the given protocol.
|
133 |
|
|
* @throws NoSuchProviderException If the named provider does not
|
134 |
|
|
* exist.
|
135 |
|
|
* @throws IllegalArgumentException If <i>provider</i> is null.
|
136 |
|
|
*/
|
137 |
|
|
public static final SSLContext getInstance(String protocol,
|
138 |
|
|
String provider)
|
139 |
|
|
throws NoSuchAlgorithmException, NoSuchProviderException
|
140 |
|
|
{
|
141 |
|
|
if (provider == null)
|
142 |
|
|
{
|
143 |
|
|
throw new IllegalArgumentException("null provider");
|
144 |
|
|
}
|
145 |
|
|
Provider p = Security.getProvider(provider);
|
146 |
|
|
if (p == null)
|
147 |
|
|
{
|
148 |
|
|
throw new NoSuchProviderException(provider);
|
149 |
|
|
}
|
150 |
|
|
return getInstance(protocol, p);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
/**
|
154 |
|
|
* Get an instance of a context for the specified protocol from the
|
155 |
|
|
* specified provider.
|
156 |
|
|
*
|
157 |
|
|
* @param protocol The name of the protocol to get a context for.
|
158 |
|
|
* @param provider The name of the provider to get the
|
159 |
|
|
* implementation from.
|
160 |
|
|
* @return The new context.
|
161 |
|
|
* @throws NoSuchAlgorithmException If the provider does not
|
162 |
|
|
* implement the given protocol.
|
163 |
|
|
* @throws IllegalArgumentException If <i>provider</i> is null.
|
164 |
|
|
*/
|
165 |
|
|
public static final SSLContext getInstance(String protocol,
|
166 |
|
|
Provider provider)
|
167 |
|
|
throws NoSuchAlgorithmException
|
168 |
|
|
{
|
169 |
|
|
try
|
170 |
|
|
{
|
171 |
|
|
return new SSLContext((SSLContextSpi)
|
172 |
|
|
Engine.getInstance(SSL_CONTEXT, protocol, provider),
|
173 |
|
|
provider, protocol);
|
174 |
|
|
}
|
175 |
|
|
catch (InvocationTargetException ite)
|
176 |
|
|
{
|
177 |
|
|
NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(protocol);
|
178 |
|
|
throw (NoSuchAlgorithmException) nsae.initCause(ite);
|
179 |
|
|
}
|
180 |
|
|
catch (ClassCastException cce)
|
181 |
|
|
{
|
182 |
|
|
NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(protocol);
|
183 |
|
|
throw (NoSuchAlgorithmException) nsae.initCause(cce);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
// Instance methods.
|
188 |
|
|
// -----------------------------------------------------------------
|
189 |
|
|
|
190 |
|
|
/**
|
191 |
|
|
* Returns the set of SSL contexts available for client connections.
|
192 |
|
|
*
|
193 |
|
|
* @return The set of SSL contexts available for client connections.
|
194 |
|
|
*/
|
195 |
|
|
public final SSLSessionContext getClientSessionContext()
|
196 |
|
|
{
|
197 |
|
|
return ctxSpi.engineGetClientSessionContext();
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/**
|
201 |
|
|
* Returns the protocol name of this context.
|
202 |
|
|
*
|
203 |
|
|
* @return The protocol name of this context.
|
204 |
|
|
*/
|
205 |
|
|
public final String getProtocol()
|
206 |
|
|
{
|
207 |
|
|
return protocol;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* Returns the provider of this implementation.
|
212 |
|
|
*
|
213 |
|
|
* @return The provider of this implementation.
|
214 |
|
|
*/
|
215 |
|
|
public final Provider getProvider()
|
216 |
|
|
{
|
217 |
|
|
return provider;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/**
|
221 |
|
|
* Returns the set of SSL contexts available for server connections.
|
222 |
|
|
*
|
223 |
|
|
* @return The set of SSL contexts available for server connections.
|
224 |
|
|
*/
|
225 |
|
|
public final SSLSessionContext getServerSessionContext()
|
226 |
|
|
{
|
227 |
|
|
return ctxSpi.engineGetServerSessionContext();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
/**
|
231 |
|
|
* Returns the factory for server SSL sockets.
|
232 |
|
|
*
|
233 |
|
|
* @return The factory for server SSL sockets.
|
234 |
|
|
*/
|
235 |
|
|
public final SSLServerSocketFactory getServerSocketFactory()
|
236 |
|
|
{
|
237 |
|
|
return ctxSpi.engineGetServerSocketFactory();
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/**
|
241 |
|
|
* Returns the factory for client SSL sockets.
|
242 |
|
|
*
|
243 |
|
|
* @return The factory for client SSL sockets.
|
244 |
|
|
*/
|
245 |
|
|
public final SSLSocketFactory getSocketFactory()
|
246 |
|
|
{
|
247 |
|
|
return ctxSpi.engineGetSocketFactory();
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* Initializes this context and prepares it for producing socket
|
252 |
|
|
* factories. All of the parameters are optional; default values are
|
253 |
|
|
* used if left unspecified.
|
254 |
|
|
*
|
255 |
|
|
* @param keyManagers The set of key managers to use.
|
256 |
|
|
* @param trustManagers The set of trust managers to use.
|
257 |
|
|
* @param random A source of random bits to use.
|
258 |
|
|
* @throws KeyManagementException If initialization fails.
|
259 |
|
|
*/
|
260 |
|
|
public final void init(KeyManager[] keyManagers,
|
261 |
|
|
TrustManager[] trustManagers,
|
262 |
|
|
SecureRandom random)
|
263 |
|
|
throws KeyManagementException
|
264 |
|
|
{
|
265 |
|
|
ctxSpi.engineInit(keyManagers, trustManagers, random);
|
266 |
|
|
}
|
267 |
|
|
}
|