0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.net.ssl;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport sun.security.jca.GetInstance;
0N/A
0N/A/**
0N/A * Instances of this class represent a secure socket protocol
0N/A * implementation which acts as a factory for secure socket
0N/A * factories or <code>SSLEngine</code>s. This class is initialized
0N/A * with an optional set of key and trust managers and source of
0N/A * secure random bytes.
0N/A *
3465N/A * <p> Every implementation of the Java platform is required to support the
3465N/A * following standard <code>SSLContext</code> protocol:
3465N/A * <ul>
3465N/A * <li><tt>TLSv1</tt></li>
3465N/A * </ul>
3465N/A * This protocol is described in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext">
3465N/A * SSLContext section</a> of the
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation.
3465N/A * Consult the release documentation for your implementation to see if any
3465N/A * other algorithms are supported.
3465N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class SSLContext {
0N/A private final Provider provider;
0N/A
0N/A private final SSLContextSpi contextSpi;
0N/A
0N/A private final String protocol;
0N/A
0N/A /**
0N/A * Creates an SSLContext object.
0N/A *
0N/A * @param contextSpi the delegate
0N/A * @param provider the provider
0N/A * @param protocol the protocol
0N/A */
0N/A protected SSLContext(SSLContextSpi contextSpi, Provider provider,
0N/A String protocol) {
0N/A this.contextSpi = contextSpi;
0N/A this.provider = provider;
0N/A this.protocol = protocol;
0N/A }
0N/A
0N/A private static SSLContext defaultContext;
0N/A
0N/A /**
0N/A * Returns the default SSL context.
0N/A *
0N/A * <p>If a default context was set using the {@link #setDefault
0N/A * SSLContext.setDefault()} method, it is returned. Otherwise, the first
0N/A * call of this method triggers the call
0N/A * <code>SSLContext.getInstance("Default")</code>.
0N/A * If successful, that object is made the default SSL context and returned.
0N/A *
0N/A * <p>The default context is immediately
0N/A * usable and does not require {@linkplain #init initialization}.
0N/A *
0N/A * @return the default SSL context
0N/A * @throws NoSuchAlgorithmException if the
0N/A * {@link SSLContext#getInstance SSLContext.getInstance()} call fails
0N/A * @since 1.6
0N/A */
0N/A public static synchronized SSLContext getDefault()
0N/A throws NoSuchAlgorithmException {
0N/A if (defaultContext == null) {
0N/A defaultContext = SSLContext.getInstance("Default");
0N/A }
0N/A return defaultContext;
0N/A }
0N/A
0N/A /**
0N/A * Sets the default SSL context. It will be returned by subsequent calls
0N/A * to {@link #getDefault}. The default context must be immediately usable
0N/A * and not require {@linkplain #init initialization}.
0N/A *
0N/A * @param context the SSLContext
0N/A * @throws NullPointerException if context is null
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkPermission</code> method does not allow
0N/A * <code>SSLPermission("setDefaultSSLContext")</code>
0N/A * @since 1.6
0N/A */
0N/A public static synchronized void setDefault(SSLContext context) {
0N/A if (context == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(new SSLPermission("setDefaultSSLContext"));
0N/A }
0N/A defaultContext = context;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>SSLContext</code> object that implements the
0N/A * specified secure socket protocol.
0N/A *
0N/A * <p> This method traverses the list of registered security Providers,
0N/A * starting with the most preferred Provider.
0N/A * A new SSLContext object encapsulating the
0N/A * SSLContextSpi implementation from the first
0N/A * Provider that supports the specified protocol is returned.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param protocol the standard name of the requested protocol.
3465N/A * See the SSLContext section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext">
3465N/A * Java Cryptography Architecture Standard Algorithm Name
3465N/A * Documentation</a>
0N/A * for information about standard protocol names.
0N/A *
0N/A * @return the new <code>SSLContext</code> object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no Provider supports a
0N/A * TrustManagerFactorySpi implementation for the
0N/A * specified protocol.
250N/A * @exception NullPointerException if protocol is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static SSLContext getInstance(String protocol)
0N/A throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("SSLContext", SSLContextSpi.class, protocol);
0N/A return new SSLContext((SSLContextSpi)instance.impl, instance.provider,
0N/A protocol);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>SSLContext</code> object that implements the
0N/A * specified secure socket protocol.
0N/A *
0N/A * <p> A new SSLContext object encapsulating the
0N/A * SSLContextSpi implementation from the specified provider
0N/A * is returned. The specified provider must be registered
0N/A * in the security provider list.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param protocol the standard name of the requested protocol.
3465N/A * See the SSLContext section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext">
3465N/A * Java Cryptography Architecture Standard Algorithm Name
3465N/A * Documentation</a>
0N/A * for information about standard protocol names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return the new <code>SSLContext</code> object.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a SSLContextSpi
0N/A * implementation for the specified protocol is not
0N/A * available from the specified provider.
0N/A *
0N/A * @throws NoSuchProviderException if the specified provider is not
0N/A * registered in the security provider list.
0N/A *
0N/A * @throws IllegalArgumentException if the provider name is null or empty.
250N/A * @throws NullPointerException if protocol is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static SSLContext getInstance(String protocol, String provider)
0N/A throws NoSuchAlgorithmException, NoSuchProviderException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("SSLContext", SSLContextSpi.class, protocol, provider);
0N/A return new SSLContext((SSLContextSpi)instance.impl, instance.provider,
0N/A protocol);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>SSLContext</code> object that implements the
0N/A * specified secure socket protocol.
0N/A *
0N/A * <p> A new SSLContext object encapsulating the
0N/A * SSLContextSpi implementation from the specified Provider
0N/A * object is returned. Note that the specified Provider object
0N/A * does not have to be registered in the provider list.
0N/A *
0N/A * @param protocol the standard name of the requested protocol.
3465N/A * See the SSLContext section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext">
3465N/A * Java Cryptography Architecture Standard Algorithm Name
3465N/A * Documentation</a>
0N/A * for information about standard protocol names.
0N/A *
0N/A * @param provider an instance of the provider.
0N/A *
0N/A * @return the new <code>SSLContext</code> object.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a KeyManagerFactorySpi
0N/A * implementation for the specified protocol is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @throws IllegalArgumentException if the provider name is null.
250N/A * @throws NullPointerException if protocol is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static SSLContext getInstance(String protocol, Provider provider)
0N/A throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("SSLContext", SSLContextSpi.class, protocol, provider);
0N/A return new SSLContext((SSLContextSpi)instance.impl, instance.provider,
0N/A protocol);
0N/A }
0N/A
0N/A /**
0N/A * Returns the protocol name of this <code>SSLContext</code> object.
0N/A *
0N/A * <p>This is the same name that was specified in one of the
0N/A * <code>getInstance</code> calls that created this
0N/A * <code>SSLContext</code> object.
0N/A *
0N/A * @return the protocol name of this <code>SSLContext</code> object.
0N/A */
0N/A public final String getProtocol() {
0N/A return this.protocol;
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this <code>SSLContext</code> object.
0N/A *
0N/A * @return the provider of this <code>SSLContext</code> object
0N/A */
0N/A public final Provider getProvider() {
0N/A return this.provider;
0N/A }
0N/A
0N/A /**
0N/A * Initializes this context. Either of the first two parameters
0N/A * may be null in which case the installed security providers will
0N/A * be searched for the highest priority implementation of the
0N/A * appropriate factory. Likewise, the secure random parameter may
0N/A * be null in which case the default implementation will be used.
0N/A * <P>
0N/A * Only the first instance of a particular key and/or trust manager
0N/A * implementation type in the array is used. (For example, only
0N/A * the first javax.net.ssl.X509KeyManager in the array will be used.)
0N/A *
0N/A * @param km the sources of authentication keys or null
0N/A * @param tm the sources of peer authentication trust decisions or null
0N/A * @param random the source of randomness for this generator or null
0N/A * @throws KeyManagementException if this operation fails
0N/A */
0N/A public final void init(KeyManager[] km, TrustManager[] tm,
0N/A SecureRandom random)
0N/A throws KeyManagementException {
0N/A contextSpi.engineInit(km, tm, random);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>SocketFactory</code> object for this
0N/A * context.
0N/A *
0N/A * @return the <code>SocketFactory</code> object
0N/A * @throws IllegalStateException if the SSLContextImpl requires
0N/A * initialization and the <code>init()</code> has not been called
0N/A */
0N/A public final SSLSocketFactory getSocketFactory() {
0N/A return contextSpi.engineGetSocketFactory();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>ServerSocketFactory</code> object for
0N/A * this context.
0N/A *
0N/A * @return the <code>ServerSocketFactory</code> object
0N/A * @throws IllegalStateException if the SSLContextImpl requires
0N/A * initialization and the <code>init()</code> has not been called
0N/A */
0N/A public final SSLServerSocketFactory getServerSocketFactory() {
0N/A return contextSpi.engineGetServerSocketFactory();
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>SSLEngine</code> using this context.
0N/A * <P>
0N/A * Applications using this factory method are providing no hints
0N/A * for an internal session reuse strategy. If hints are desired,
0N/A * {@link #createSSLEngine(String, int)} should be used
0N/A * instead.
0N/A * <P>
0N/A * Some cipher suites (such as Kerberos) require remote hostname
0N/A * information, in which case this factory method should not be used.
0N/A *
0N/A * @return the <code>SSLEngine</code> object
0N/A * @throws UnsupportedOperationException if the underlying provider
0N/A * does not implement the operation.
0N/A * @throws IllegalStateException if the SSLContextImpl requires
0N/A * initialization and the <code>init()</code> has not been called
0N/A * @since 1.5
0N/A */
0N/A public final SSLEngine createSSLEngine() {
0N/A try {
0N/A return contextSpi.engineCreateSSLEngine();
0N/A } catch (AbstractMethodError e) {
0N/A UnsupportedOperationException unsup =
0N/A new UnsupportedOperationException(
0N/A "Provider: " + getProvider() +
0N/A " doesn't support this operation");
0N/A unsup.initCause(e);
0N/A throw unsup;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>SSLEngine</code> using this context using
0N/A * advisory peer information.
0N/A * <P>
0N/A * Applications using this factory method are providing hints
0N/A * for an internal session reuse strategy.
0N/A * <P>
0N/A * Some cipher suites (such as Kerberos) require remote hostname
0N/A * information, in which case peerHost needs to be specified.
0N/A *
0N/A * @param peerHost the non-authoritative name of the host
0N/A * @param peerPort the non-authoritative port
0N/A * @return the new <code>SSLEngine</code> object
0N/A * @throws UnsupportedOperationException if the underlying provider
0N/A * does not implement the operation.
0N/A * @throws IllegalStateException if the SSLContextImpl requires
0N/A * initialization and the <code>init()</code> has not been called
0N/A * @since 1.5
0N/A */
0N/A public final SSLEngine createSSLEngine(String peerHost, int peerPort) {
0N/A try {
0N/A return contextSpi.engineCreateSSLEngine(peerHost, peerPort);
0N/A } catch (AbstractMethodError e) {
0N/A UnsupportedOperationException unsup =
0N/A new UnsupportedOperationException(
0N/A "Provider: " + getProvider() +
0N/A " does not support this operation");
0N/A unsup.initCause(e);
0N/A throw unsup;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the server session context, which represents the set of
0N/A * SSL sessions available for use during the handshake phase of
0N/A * server-side SSL sockets.
0N/A * <P>
0N/A * This context may be unavailable in some environments, in which
0N/A * case this method returns null. For example, when the underlying
0N/A * SSL provider does not provide an implementation of SSLSessionContext
0N/A * interface, this method returns null. A non-null session context
0N/A * is returned otherwise.
0N/A *
0N/A * @return server session context bound to this SSL context
0N/A */
0N/A public final SSLSessionContext getServerSessionContext() {
0N/A return contextSpi.engineGetServerSessionContext();
0N/A }
0N/A
0N/A /**
0N/A * Returns the client session context, which represents the set of
0N/A * SSL sessions available for use during the handshake phase of
0N/A * client-side SSL sockets.
0N/A * <P>
0N/A * This context may be unavailable in some environments, in which
0N/A * case this method returns null. For example, when the underlying
0N/A * SSL provider does not provide an implementation of SSLSessionContext
0N/A * interface, this method returns null. A non-null session context
0N/A * is returned otherwise.
0N/A *
0N/A * @return client session context bound to this SSL context
0N/A */
0N/A public final SSLSessionContext getClientSessionContext() {
0N/A return contextSpi.engineGetClientSessionContext();
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the SSLParameters indicating the default
0N/A * settings for this SSL context.
0N/A *
0N/A * <p>The parameters will always have the ciphersuites and protocols
0N/A * arrays set to non-null values.
0N/A *
0N/A * @return a copy of the SSLParameters object with the default settings
0N/A * @throws UnsupportedOperationException if the default SSL parameters
0N/A * could not be obtained.
0N/A * @since 1.6
0N/A */
0N/A public final SSLParameters getDefaultSSLParameters() {
0N/A return contextSpi.engineGetDefaultSSLParameters();
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the SSLParameters indicating the supported
0N/A * settings for this SSL context.
0N/A *
0N/A * <p>The parameters will always have the ciphersuites and protocols
0N/A * arrays set to non-null values.
0N/A *
0N/A * @return a copy of the SSLParameters object with the supported
0N/A * settings
0N/A * @throws UnsupportedOperationException if the supported SSL parameters
0N/A * could not be obtained.
0N/A * @since 1.6
0N/A */
0N/A public final SSLParameters getSupportedSSLParameters() {
0N/A return contextSpi.engineGetSupportedSSLParameters();
0N/A }
0N/A
0N/A}