Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLContextSpi.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.net.ssl;2627import java.security.*;2829/**30* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)31* for the {@code SSLContext} class.32*33* <p> All the abstract methods in this class must be implemented by each34* cryptographic service provider who wishes to supply the implementation35* of a particular SSL context.36*37* @since 1.438* @see SSLContext39*/40public abstract class SSLContextSpi {41/**42* Constructor for subclasses to call.43*/44public SSLContextSpi() {}4546/**47* Initializes this context.48*49* @param km the sources of authentication keys50* @param tm the sources of peer authentication trust decisions51* @param sr the source of randomness52* @throws KeyManagementException if this operation fails53* @see SSLContext#init(KeyManager [], TrustManager [], SecureRandom)54*/55protected abstract void engineInit(KeyManager[] km, TrustManager[] tm,56SecureRandom sr) throws KeyManagementException;5758/**59* Returns a {@code SocketFactory} object for this60* context.61*62* @return the {@code SocketFactory} object63* @throws UnsupportedOperationException if the underlying provider64* does not implement the operation.65* @throws IllegalStateException if the SSLContextImpl requires66* initialization and the {@code engineInit()}67* has not been called68* @see javax.net.ssl.SSLContext#getSocketFactory()69*/70protected abstract SSLSocketFactory engineGetSocketFactory();7172/**73* Returns a {@code ServerSocketFactory} object for74* this context.75*76* @return the {@code ServerSocketFactory} object77* @throws UnsupportedOperationException if the underlying provider78* does not implement the operation.79* @throws IllegalStateException if the SSLContextImpl requires80* initialization and the {@code engineInit()}81* has not been called82* @see javax.net.ssl.SSLContext#getServerSocketFactory()83*/84protected abstract SSLServerSocketFactory engineGetServerSocketFactory();8586/**87* Creates a new {@code SSLEngine} using this context.88* <P>89* Applications using this factory method are providing no hints90* for an internal session reuse strategy. If hints are desired,91* {@link #engineCreateSSLEngine(String, int)} should be used92* instead.93* <P>94* Some cipher suites (such as Kerberos) require remote hostname95* information, in which case this factory method should not be used.96*97* @implNote98* It is provider-specific if the returned SSLEngine uses client or99* server mode by default for the (D)TLS connection. The JDK SunJSSE100* provider implementation uses server mode by default. However, it101* is recommended to always set the desired mode explicitly by calling102* {@link SSLEngine#setUseClientMode(boolean) SSLEngine.setUseClientMode()}103* before invoking other methods of the SSLEngine.104*105* @return the {@code SSLEngine} Object106* @throws IllegalStateException if the SSLContextImpl requires107* initialization and the {@code engineInit()}108* has not been called109*110* @see SSLContext#createSSLEngine()111*112* @since 1.5113*/114protected abstract SSLEngine engineCreateSSLEngine();115116/**117* Creates a {@code SSLEngine} using this context.118* <P>119* Applications using this factory method are providing hints120* for an internal session reuse strategy.121* <P>122* Some cipher suites (such as Kerberos) require remote hostname123* information, in which case peerHost needs to be specified.124*125* @implNote126* It is provider-specific if the returned SSLEngine uses client or127* server mode by default for the (D)TLS connection. The JDK SunJSSE128* provider implementation uses server mode by default. However, it129* is recommended to always set the desired mode explicitly by calling130* {@link SSLEngine#setUseClientMode(boolean) SSLEngine.setUseClientMode()}131* before invoking other methods of the SSLEngine.132*133* @param host the non-authoritative name of the host134* @param port the non-authoritative port135* @return the {@code SSLEngine} Object136* @throws IllegalStateException if the SSLContextImpl requires137* initialization and the {@code engineInit()}138* has not been called139*140* @see SSLContext#createSSLEngine(String, int)141*142* @since 1.5143*/144protected abstract SSLEngine engineCreateSSLEngine(String host, int port);145146/**147* Returns a server {@code SSLSessionContext} object for148* this context.149*150* @return the {@code SSLSessionContext} object151* @see javax.net.ssl.SSLContext#getServerSessionContext()152*/153protected abstract SSLSessionContext engineGetServerSessionContext();154155/**156* Returns a client {@code SSLSessionContext} object for157* this context.158*159* @return the {@code SSLSessionContext} object160* @see javax.net.ssl.SSLContext#getClientSessionContext()161*/162protected abstract SSLSessionContext engineGetClientSessionContext();163164private SSLSocket getDefaultSocket() {165try {166SSLSocketFactory factory = engineGetSocketFactory();167return (SSLSocket)factory.createSocket();168} catch (java.io.IOException e) {169throw new UnsupportedOperationException("Could not obtain parameters", e);170}171}172173/**174* Returns a copy of the SSLParameters indicating the default175* settings for this SSL context.176*177* <p>The parameters will always have the ciphersuite and protocols178* arrays set to non-null values.179*180* <p>The default implementation obtains the parameters from an181* SSLSocket created by calling the182* {@linkplain javax.net.SocketFactory#createSocket183* SocketFactory.createSocket()} method of this context's SocketFactory.184*185* @return a copy of the SSLParameters object with the default settings186* @throws UnsupportedOperationException if the default SSL parameters187* could not be obtained.188*189* @since 1.6190*/191protected SSLParameters engineGetDefaultSSLParameters() {192SSLSocket socket = getDefaultSocket();193return socket.getSSLParameters();194}195196/**197* Returns a copy of the SSLParameters indicating the maximum supported198* settings for this SSL context.199*200* <p>The parameters will always have the ciphersuite and protocols201* arrays set to non-null values.202*203* <p>The default implementation obtains the parameters from an204* SSLSocket created by calling the205* {@linkplain javax.net.SocketFactory#createSocket206* SocketFactory.createSocket()} method of this context's SocketFactory.207*208* @return a copy of the SSLParameters object with the maximum supported209* settings210* @throws UnsupportedOperationException if the supported SSL parameters211* could not be obtained.212*213* @since 1.6214*/215protected SSLParameters engineGetSupportedSSLParameters() {216SSLSocket socket = getDefaultSocket();217SSLParameters params = socket.getSSLParameters();218params.setCipherSuites(socket.getSupportedCipherSuites());219params.setProtocols(socket.getSupportedProtocols());220return params;221}222}223224225