Class OpenSSLEngineImpl
- java.lang.Object
-
- javax.net.ssl.SSLEngine
-
- org.conscrypt.OpenSSLEngineImpl
-
- All Implemented Interfaces:
NativeCrypto.SSLHandshakeCallbacks,SSLParametersImpl.AliasChooser,SSLParametersImpl.PSKCallbacks
public class OpenSSLEngineImpl extends SSLEngine implements NativeCrypto.SSLHandshakeCallbacks, SSLParametersImpl.AliasChooser, SSLParametersImpl.PSKCallbacks
Implements theSSLEngineAPI using OpenSSL's non-blocking interfaces.
-
-
Constructor Summary
Constructors Constructor Description OpenSSLEngineImpl(String host, int port, SSLParametersImpl sslParameters)OpenSSLEngineImpl(SSLParametersImpl sslParameters)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginHandshake()Initiates a handshake on this engine.StringchooseClientAlias(X509KeyManager keyManager, X500Principal[] issuers, String[] keyTypes)StringchooseClientPSKIdentity(PSKKeyManager keyManager, String identityHint)StringchooseServerAlias(X509KeyManager keyManager, String keyType)StringchooseServerPSKIdentityHint(PSKKeyManager keyManager)voidclientCertificateRequested(byte[] keyTypeBytes, byte[][] asn1DerEncodedPrincipals)Called on an SSL client when the server requests (or requires a certificate).intclientPSKKeyRequested(String identityHint, byte[] identity, byte[] key)Gets the key to be used in client mode for this connection in Pre-Shared Key (PSK) key exchange.voidcloseInbound()Notifies this engine instance that no more inbound network data will be sent to this engine.voidcloseOutbound()Notifies this engine instance that no more outbound application data will be sent to this engine.protected voidfinalize()Invoked when the garbage collector has detected that this instance is no longer reachable.RunnablegetDelegatedTask()Returns a delegate task for this engine instance.String[]getEnabledCipherSuites()Returns the SSL cipher suite names that are enabled in this engine instance.String[]getEnabledProtocols()Returns the protocol version names that are enabled in this engine instance.booleangetEnableSessionCreation()Returns whether new SSL sessions may be established by this engine.SSLEngineResult.HandshakeStatusgetHandshakeStatus()Returns the status of the handshake of this engine instance.booleangetNeedClientAuth()Returns whether this engine instance will require client authentication.SecretKeygetPSKKey(PSKKeyManager keyManager, String identityHint, String identity)SSLSessiongetSession()Returns the SSL session for this engine instance.String[]getSupportedCipherSuites()Returns the SSL cipher suite names that are supported by this engine.String[]getSupportedProtocols()Returns the protocol names that are supported by this engine.booleangetUseClientMode()Returns whether this engine is set to act in client mode when handshaking.booleangetWantClientAuth()Returns whether this engine will request client authentication.booleanisInboundDone()Returns whether no more inbound data will be accepted by this engine.booleanisOutboundDone()Returns whether no more outbound data will be produced by this engine.voidonSSLStateChange(long sslSessionNativePtr, int type, int val)Called when SSL state changes.intserverPSKKeyRequested(String identityHint, String identity, byte[] key)Gets the key to be used in server mode for this connection in Pre-Shared Key (PSK) key exchange.voidsetEnabledCipherSuites(String[] suites)Sets the SSL cipher suite names that should be enabled in this engine instance.voidsetEnabledProtocols(String[] protocols)Sets the protocol version names that should be enabled in this engine instance.voidsetEnableSessionCreation(boolean flag)Sets whether new SSL sessions may be established by this engine instance.voidsetNeedClientAuth(boolean need)Sets whether this engine must require client authentication.voidsetUseClientMode(boolean mode)Sets whether this engine should act in client (or server) mode when handshaking.voidsetWantClientAuth(boolean want)Sets whether this engine should request client authentication.SSLEngineResultunwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length)Decodes the incoming network data buffer into application data buffers.voidverifyCertificateChain(long sslSessionNativePtr, long[] certRefs, String authMethod)Verify that we trust the certificate chain is trusted.SSLEngineResultwrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst)Encodes the outgoing application data buffers into the network data buffer.-
Methods inherited from class javax.net.ssl.SSLEngine
getPeerHost, getPeerPort, getSSLParameters, setSSLParameters, unwrap, unwrap, wrap, wrap
-
-
-
-
Constructor Detail
-
OpenSSLEngineImpl
public OpenSSLEngineImpl(SSLParametersImpl sslParameters)
-
OpenSSLEngineImpl
public OpenSSLEngineImpl(String host, int port, SSLParametersImpl sslParameters)
-
-
Method Detail
-
beginHandshake
public void beginHandshake() throws SSLExceptionDescription copied from class:SSLEngineInitiates a handshake on this engine.Calling this method is not needed for the initial handshake: it will be called by
wraporunwrapif the initial handshake has not been started yet.- Specified by:
beginHandshakein classSSLEngine- Throws:
SSLException- if starting the handshake fails.
-
closeInbound
public void closeInbound() throws SSLExceptionDescription copied from class:SSLEngineNotifies this engine instance that no more inbound network data will be sent to this engine.- Specified by:
closeInboundin classSSLEngine- Throws:
SSLException- if this engine did not receive a needed protocol specific close notification message from the peer.
-
closeOutbound
public void closeOutbound()
Description copied from class:SSLEngineNotifies this engine instance that no more outbound application data will be sent to this engine.- Specified by:
closeOutboundin classSSLEngine
-
getDelegatedTask
public Runnable getDelegatedTask()
Description copied from class:SSLEngineReturns a delegate task for this engine instance. Some engine operations may require the results of blocking or long running operations, and theSSLEngineResultinstances returned by this engine may indicate that a delegated task result is needed. In this case therunmethod of the returnedRunnabledelegated task must be called.- Specified by:
getDelegatedTaskin classSSLEngine- Returns:
- a delegate task, or
nullif none are available.
-
getEnabledCipherSuites
public String[] getEnabledCipherSuites()
Description copied from class:SSLEngineReturns the SSL cipher suite names that are enabled in this engine instance.- Specified by:
getEnabledCipherSuitesin classSSLEngine- Returns:
- the SSL cipher suite names that are enabled in this engine instance.
-
getEnabledProtocols
public String[] getEnabledProtocols()
Description copied from class:SSLEngineReturns the protocol version names that are enabled in this engine instance.- Specified by:
getEnabledProtocolsin classSSLEngine- Returns:
- the protocol version names that are enabled in this engine instance.
-
getEnableSessionCreation
public boolean getEnableSessionCreation()
Description copied from class:SSLEngineReturns whether new SSL sessions may be established by this engine.- Specified by:
getEnableSessionCreationin classSSLEngine- Returns:
trueif new session may be established,falseif existing sessions must be reused.
-
getHandshakeStatus
public SSLEngineResult.HandshakeStatus getHandshakeStatus()
Description copied from class:SSLEngineReturns the status of the handshake of this engine instance.- Specified by:
getHandshakeStatusin classSSLEngine- Returns:
- the status of the handshake of this engine instance.
-
getNeedClientAuth
public boolean getNeedClientAuth()
Description copied from class:SSLEngineReturns whether this engine instance will require client authentication.- Specified by:
getNeedClientAuthin classSSLEngine- Returns:
trueif this engine will require client authentication,falseif no client authentication is needed.
-
getSession
public SSLSession getSession()
Description copied from class:SSLEngineReturns the SSL session for this engine instance.- Specified by:
getSessionin classSSLEngine- Returns:
- the SSL session for this engine instance.
-
getSupportedCipherSuites
public String[] getSupportedCipherSuites()
Description copied from class:SSLEngineReturns the SSL cipher suite names that are supported by this engine. These cipher suites can be enabled usingSSLEngine.setEnabledCipherSuites(String[]).- Specified by:
getSupportedCipherSuitesin classSSLEngine- Returns:
- the SSL cipher suite names that are supported by this engine.
-
getSupportedProtocols
public String[] getSupportedProtocols()
Description copied from class:SSLEngineReturns the protocol names that are supported by this engine. These protocols can be enables usingSSLEngine.setEnabledProtocols(String[]).- Specified by:
getSupportedProtocolsin classSSLEngine- Returns:
- the protocol names that are supported by this engine.
-
getUseClientMode
public boolean getUseClientMode()
Description copied from class:SSLEngineReturns whether this engine is set to act in client mode when handshaking.- Specified by:
getUseClientModein classSSLEngine- Returns:
trueif the engine is set to do handshaking in client mode.
-
getWantClientAuth
public boolean getWantClientAuth()
Description copied from class:SSLEngineReturns whether this engine will request client authentication.- Specified by:
getWantClientAuthin classSSLEngine- Returns:
trueif client authentication will be requested,falseotherwise.
-
isInboundDone
public boolean isInboundDone()
Description copied from class:SSLEngineReturns whether no more inbound data will be accepted by this engine.- Specified by:
isInboundDonein classSSLEngine- Returns:
trueif no more inbound data will be accepted by this engine,falseotherwise.
-
isOutboundDone
public boolean isOutboundDone()
Description copied from class:SSLEngineReturns whether no more outbound data will be produced by this engine.- Specified by:
isOutboundDonein classSSLEngine- Returns:
trueif no more outbound data will be producted by this engine,otherwisefalse.
-
setEnabledCipherSuites
public void setEnabledCipherSuites(String[] suites)
Description copied from class:SSLEngineSets the SSL cipher suite names that should be enabled in this engine instance. Only cipher suites listed bygetSupportedCipherSuites()are allowed.- Specified by:
setEnabledCipherSuitesin classSSLEngine- Parameters:
suites- the SSL cipher suite names to be enabled.
-
setEnabledProtocols
public void setEnabledProtocols(String[] protocols)
Description copied from class:SSLEngineSets the protocol version names that should be enabled in this engine instance. Only protocols listed bygetSupportedProtocols()are allowed.- Specified by:
setEnabledProtocolsin classSSLEngine- Parameters:
protocols- the protocol version names to be enabled.
-
setEnableSessionCreation
public void setEnableSessionCreation(boolean flag)
Description copied from class:SSLEngineSets whether new SSL sessions may be established by this engine instance.- Specified by:
setEnableSessionCreationin classSSLEngine- Parameters:
flag-trueif new SSL sessions may be established,falseif existing SSL sessions must be reused.
-
setNeedClientAuth
public void setNeedClientAuth(boolean need)
Description copied from class:SSLEngineSets whether this engine must require client authentication. The client authentication is one of:- authentication required
- authentication requested
- no authentication needed
SSLEngine.setWantClientAuth(boolean).- Specified by:
setNeedClientAuthin classSSLEngine- Parameters:
need-trueif client authentication is required,falseif no authentication is needed.
-
setUseClientMode
public void setUseClientMode(boolean mode)
Description copied from class:SSLEngineSets whether this engine should act in client (or server) mode when handshaking.- Specified by:
setUseClientModein classSSLEngine- Parameters:
mode-trueif this engine should act in client mode,falseif not.
-
setWantClientAuth
public void setWantClientAuth(boolean want)
Description copied from class:SSLEngineSets whether this engine should request client authentication. The client authentication is one of the following:- authentication required
- authentication requested
- no authentication needed
SSLEngine.setNeedClientAuth(boolean).- Specified by:
setWantClientAuthin classSSLEngine- Parameters:
want-trueif client authentication should be requested,falseif no authentication is needed.
-
unwrap
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) throws SSLException
Description copied from class:SSLEngineDecodes the incoming network data buffer into application data buffers. If a handshake has not been started yet, it will automatically be started.- Specified by:
unwrapin classSSLEngine- Parameters:
src- the buffer with incoming network datadsts- the array of destination buffers for incoming application data.offset- the offset in the array of destination buffers to which data is to be transferred.length- the maximum number of destination buffers to be used.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.
-
wrap
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) throws SSLException
Description copied from class:SSLEngineEncodes the outgoing application data buffers into the network data buffer. If a handshake has not been started yet, it will automatically be started.- Specified by:
wrapin classSSLEngine- Parameters:
srcs- the array of source buffers of outgoing application data.offset- the offset in the array of source buffers from which data is to be retrieved.length- the maximum number of source buffers to be used.dst- the destination buffer for network data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.
-
clientPSKKeyRequested
public int clientPSKKeyRequested(String identityHint, byte[] identity, byte[] key)
Description copied from interface:NativeCrypto.SSLHandshakeCallbacksGets the key to be used in client mode for this connection in Pre-Shared Key (PSK) key exchange.- Specified by:
clientPSKKeyRequestedin interfaceNativeCrypto.SSLHandshakeCallbacks- Parameters:
identityHint- PSK identity hint provided by the server ornullif no hint provided.identity- buffer to be populated with PSK identity (NULL-terminated modified UTF-8) by this method. This identity will be provided to the server.key- buffer to be populated with key material by this method.- Returns:
- number of bytes this method stored in the
keybuffer or0if an error occurred in which case the handshake will be aborted.
-
serverPSKKeyRequested
public int serverPSKKeyRequested(String identityHint, String identity, byte[] key)
Description copied from interface:NativeCrypto.SSLHandshakeCallbacksGets the key to be used in server mode for this connection in Pre-Shared Key (PSK) key exchange.- Specified by:
serverPSKKeyRequestedin interfaceNativeCrypto.SSLHandshakeCallbacks- Parameters:
identityHint- PSK identity hint provided by this server to the client ornullif no hint was provided.identity- PSK identity provided by the client.key- buffer to be populated with key material by this method.- Returns:
- number of bytes this method stored in the
keybuffer or0if an error occurred in which case the handshake will be aborted.
-
onSSLStateChange
public void onSSLStateChange(long sslSessionNativePtr, int type, int val)Description copied from interface:NativeCrypto.SSLHandshakeCallbacksCalled when SSL state changes. This could be handshake completion.- Specified by:
onSSLStateChangein interfaceNativeCrypto.SSLHandshakeCallbacks
-
verifyCertificateChain
public void verifyCertificateChain(long sslSessionNativePtr, long[] certRefs, String authMethod) throws CertificateExceptionDescription copied from interface:NativeCrypto.SSLHandshakeCallbacksVerify that we trust the certificate chain is trusted.- Specified by:
verifyCertificateChainin interfaceNativeCrypto.SSLHandshakeCallbacks- Parameters:
sslSessionNativePtr- pointer to a reference of the SSL_SESSIONcertRefs- chain of X.509 certificate referencesauthMethod- auth algorithm name- Throws:
CertificateException- if the certificate is untrusted
-
clientCertificateRequested
public void clientCertificateRequested(byte[] keyTypeBytes, byte[][] asn1DerEncodedPrincipals) throws CertificateEncodingException, SSLExceptionDescription copied from interface:NativeCrypto.SSLHandshakeCallbacksCalled on an SSL client when the server requests (or requires a certificate). The client can respond by using SSL_use_certificate and SSL_use_PrivateKey to set a certificate if has an appropriate one available, similar to how the server provides its certificate.- Specified by:
clientCertificateRequestedin interfaceNativeCrypto.SSLHandshakeCallbacks- Parameters:
keyTypeBytes- key types supported by the server, convertible to strings with #keyTypeasn1DerEncodedPrincipals- CAs known to the server- Throws:
CertificateEncodingExceptionSSLException
-
finalize
protected void finalize() throws ThrowableDescription copied from class:ObjectInvoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.Note that objects that override
finalizeare significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicitclosemethod (and implementCloseable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like aBigIntegerwhere typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.If you must use finalizers, consider at least providing your own
ReferenceQueueand having your own thread process that queue.Unlike constructors, finalizers are not automatically chained. You are responsible for calling
super.finalize()yourself.Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
-
chooseServerAlias
public String chooseServerAlias(X509KeyManager keyManager, String keyType)
- Specified by:
chooseServerAliasin interfaceSSLParametersImpl.AliasChooser
-
chooseClientAlias
public String chooseClientAlias(X509KeyManager keyManager, X500Principal[] issuers, String[] keyTypes)
- Specified by:
chooseClientAliasin interfaceSSLParametersImpl.AliasChooser
-
chooseServerPSKIdentityHint
public String chooseServerPSKIdentityHint(PSKKeyManager keyManager)
- Specified by:
chooseServerPSKIdentityHintin interfaceSSLParametersImpl.PSKCallbacks
-
chooseClientPSKIdentity
public String chooseClientPSKIdentity(PSKKeyManager keyManager, String identityHint)
- Specified by:
chooseClientPSKIdentityin interfaceSSLParametersImpl.PSKCallbacks
-
getPSKKey
public SecretKey getPSKKey(PSKKeyManager keyManager, String identityHint, String identity)
- Specified by:
getPSKKeyin interfaceSSLParametersImpl.PSKCallbacks
-
-