1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * NOTE:  this file was copied from javax.net.ssl.SSLContext
  28  */
  29 
  30 package com.sun.net.ssl;
  31 
  32 import java.security.*;
  33 import java.util.*;
  34 import javax.net.ssl.*;
  35 
  36 import sun.security.ssl.SSLSocketFactoryImpl;
  37 import sun.security.ssl.SSLServerSocketFactoryImpl;
  38 
  39 /**
  40  * Instances of this class represent a secure socket protocol
  41  * implementation which acts as a factory for secure socket
  42  * factories. This class is initialized with an optional set of
  43  * key and trust managers and source of secure random bytes.
  44  *
  45  * @deprecated As of JDK 1.4, this implementation-specific class was
  46  *      replaced by {@link javax.net.ssl.SSLContext}.
  47  */
  48 @Deprecated(since="1.4")
  49 public class SSLContext {
  50     private Provider provider;
  51 
  52     private SSLContextSpi contextSpi;
  53 
  54     private String protocol;
  55 
  56     /**
  57      * Creates an SSLContext object.
  58      *
  59      * @param contextSpi the delegate
  60      * @param provider the provider
  61      * @param protocol the protocol
  62      */
  63     protected SSLContext(SSLContextSpi contextSpi, Provider provider,
  64         String protocol) {
  65         this.contextSpi = contextSpi;
  66         this.provider = provider;
  67         this.protocol = protocol;
  68     }
  69 
  70     /**
  71      * Generates a <code>SSLContext</code> object that implements the
  72      * specified secure socket protocol.
  73      *
  74      * @param protocol the standard name of the requested protocol.
  75      *
  76      * @return the new <code>SSLContext</code> object
  77      *
  78      * @exception NoSuchAlgorithmException if the specified protocol is not
  79      * available in the default provider package or any of the other provider
  80      * packages that were searched.
  81      */
  82     public static SSLContext getInstance(String protocol)
  83         throws NoSuchAlgorithmException
  84     {
  85         try {
  86             Object[] objs = SSLSecurity.getImpl(protocol, "SSLContext",
  87                                                 (String) null);
  88             return new SSLContext((SSLContextSpi)objs[0], (Provider)objs[1],
  89                 protocol);
  90         } catch (NoSuchProviderException e) {
  91             throw new NoSuchAlgorithmException(protocol + " not found");
  92         }
  93     }
  94 
  95     /**
  96      * Generates a <code>SSLContext</code> object that implements the
  97      * specified secure socket protocol.
  98      *
  99      * @param protocol the standard name of the requested protocol.
 100      * @param provider the name of the provider
 101      *
 102      * @return the new <code>SSLContext</code> object
 103      *
 104      * @exception NoSuchAlgorithmException if the specified protocol is not
 105      * available from the specified provider.
 106      * @exception NoSuchProviderException if the specified provider has not
 107      * been configured.
 108      */
 109     public static SSLContext getInstance(String protocol, String provider)
 110         throws NoSuchAlgorithmException, NoSuchProviderException
 111     {
 112         if (provider == null || provider.isEmpty())
 113             throw new IllegalArgumentException("missing provider");
 114         Object[] objs = SSLSecurity.getImpl(protocol, "SSLContext",
 115                                             provider);
 116         return new SSLContext((SSLContextSpi)objs[0], (Provider)objs[1],
 117             protocol);
 118     }
 119 
 120     /**
 121      * Generates a <code>SSLContext</code> object that implements the
 122      * specified secure socket protocol.
 123      *
 124      * @param protocol the standard name of the requested protocol.
 125      * @param provider an instance of the provider
 126      *
 127      * @return the new <code>SSLContext</code> object
 128      *
 129      * @exception NoSuchAlgorithmException if the specified protocol is not
 130      * available from the specified provider.
 131      */
 132     public static SSLContext getInstance(String protocol, Provider provider)
 133         throws NoSuchAlgorithmException
 134     {
 135         if (provider == null)
 136             throw new IllegalArgumentException("missing provider");
 137         Object[] objs = SSLSecurity.getImpl(protocol, "SSLContext",
 138                                             provider);
 139         return new SSLContext((SSLContextSpi)objs[0], (Provider)objs[1],
 140             protocol);
 141     }
 142 
 143     /**
 144      * Returns the protocol name of this <code>SSLContext</code> object.
 145      *
 146      * <p>This is the same name that was specified in one of the
 147      * <code>getInstance</code> calls that created this
 148      * <code>SSLContext</code> object.
 149      *
 150      * @return the protocol name of this <code>SSLContext</code> object.
 151      */
 152     public final String getProtocol() {
 153         return this.protocol;
 154     }
 155 
 156     /**
 157      * Returns the provider of this <code>SSLContext</code> object.
 158      *
 159      * @return the provider of this <code>SSLContext</code> object
 160      */
 161     public final Provider getProvider() {
 162         return this.provider;
 163     }
 164 
 165     /**
 166      * Initializes this context. Either of the first two parameters
 167      * may be null in which case the installed security providers will
 168      * be searched for the highest priority implementation of the
 169      * appropriate factory. Likewise, the secure random parameter may
 170      * be null in which case the default implementation will be used.
 171      *
 172      * @param km the sources of authentication keys or null
 173      * @param tm the sources of peer authentication trust decisions or null
 174      * @param random the source of randomness for this generator or null
 175      */
 176     public final void init(KeyManager[] km, TrustManager[] tm,
 177                                 SecureRandom random)
 178         throws KeyManagementException {
 179         contextSpi.engineInit(km, tm, random);
 180     }
 181 
 182     /**
 183      * Returns a <code>SocketFactory</code> object for this
 184      * context.
 185      *
 186      * @return the factory
 187      */
 188     public final SSLSocketFactory getSocketFactory() {
 189         return contextSpi.engineGetSocketFactory();
 190     }
 191 
 192     /**
 193      * Returns a <code>ServerSocketFactory</code> object for
 194      * this context.
 195      *
 196      * @return the factory
 197      */
 198     public final SSLServerSocketFactory getServerSocketFactory() {
 199         return contextSpi.engineGetServerSocketFactory();
 200     }
 201 }