Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / net / ssl / SSLContext.java
blob6cab45351bf4e3fe416103110613b3fedb5c68dc
1 /* SSLContext.java -- an SSL protocol context.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package javax.net.ssl;
41 import gnu.java.security.Engine;
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;
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.
57 * @author Casey Marshall (rsdio@metastatic.org)
59 public class SSLContext
61 // Constants and fields.
62 // ------------------------------------------------------------------
64 /** Service name for SSL contexts. */
65 private static final String SSL_CONTEXT = "SSLContext";
67 /** The underlying engine. */
68 private final SSLContextSpi ctxSpi;
70 /** The provider of the engine class. */
71 private final Provider provider;
73 /** The protocal name. */
74 private final String protocol;
76 // Constructor.
77 // ------------------------------------------------------------------
79 /**
80 * Create a new SSL context.
82 * @param ctxSpi The context engine.
83 * @param provider The provider of the implementation.
84 * @param protocol The name of the SSL protocol.
86 protected SSLContext(SSLContextSpi ctxSpi, Provider provider,
87 String protocol)
89 this.ctxSpi = ctxSpi;
90 this.provider = provider;
91 this.protocol = protocol;
94 // Class methods.
95 // ------------------------------------------------------------------
97 /**
98 * Get an instance of a context for the specified protocol from the
99 * first provider that implements it.
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.
106 public static final SSLContext getInstance(String protocol)
107 throws NoSuchAlgorithmException
109 Provider[] provs = Security.getProviders();
110 for (int i = 0; i < provs.length; i++)
114 return getInstance(protocol, provs[i]);
116 catch (NoSuchAlgorithmException ignore)
120 throw new NoSuchAlgorithmException(protocol);
124 * Get an instance of a context for the specified protocol from the
125 * named provider.
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.
137 public static final SSLContext getInstance(String protocol,
138 String provider)
139 throws NoSuchAlgorithmException, NoSuchProviderException
141 if (provider == null)
143 throw new IllegalArgumentException("null provider");
145 Provider p = Security.getProvider(provider);
146 if (p == null)
148 throw new NoSuchProviderException(provider);
150 return getInstance(protocol, p);
154 * Get an instance of a context for the specified protocol from the
155 * specified provider.
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.
165 public static final SSLContext getInstance(String protocol,
166 Provider provider)
167 throws NoSuchAlgorithmException
171 return new SSLContext((SSLContextSpi)
172 Engine.getInstance(SSL_CONTEXT, protocol, provider),
173 provider, protocol);
175 catch (InvocationTargetException ite)
177 NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(protocol);
178 throw (NoSuchAlgorithmException) nsae.initCause(ite);
180 catch (ClassCastException cce)
182 NoSuchAlgorithmException nsae = new NoSuchAlgorithmException(protocol);
183 throw (NoSuchAlgorithmException) nsae.initCause(cce);
187 // Instance methods.
188 // -----------------------------------------------------------------
191 * Returns the set of SSL contexts available for client connections.
193 * @return The set of SSL contexts available for client connections.
195 public final SSLSessionContext getClientSessionContext()
197 return ctxSpi.engineGetClientSessionContext();
201 * Returns the protocol name of this context.
203 * @return The protocol name of this context.
205 public final String getProtocol()
207 return protocol;
211 * Returns the provider of this implementation.
213 * @return The provider of this implementation.
215 public final Provider getProvider()
217 return provider;
221 * Returns the set of SSL contexts available for server connections.
223 * @return The set of SSL contexts available for server connections.
225 public final SSLSessionContext getServerSessionContext()
227 return ctxSpi.engineGetServerSessionContext();
231 * Returns the factory for server SSL sockets.
233 * @return The factory for server SSL sockets.
235 public final SSLServerSocketFactory getServerSocketFactory()
237 return ctxSpi.engineGetServerSocketFactory();
241 * Returns the factory for client SSL sockets.
243 * @return The factory for client SSL sockets.
245 public final SSLSocketFactory getSocketFactory()
247 return ctxSpi.engineGetSocketFactory();
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.
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.
260 public final void init(KeyManager[] keyManagers,
261 TrustManager[] trustManagers,
262 SecureRandom random)
263 throws KeyManagementException
265 ctxSpi.engineInit(keyManagers, trustManagers, random);