Top Description Inners Fields Constructors Methods
org.conscrypt

pack-priv final Class SSLUtils

extends Object
Class Inheritance
Static Imports
java.lang.Math.min, org.conscrypt.NativeConstants.SSL3_RT_ALERT, .NativeConstants.SSL3_RT_APPLICATION_DATA, .NativeConstants.SSL3_RT_CHANGE_CIPHER_SPEC, .NativeConstants.SSL3_RT_HANDSHAKE, .NativeConstants.SSL3_RT_HEADER_LENGTH, .NativeConstants.SSL3_RT_MAX_PACKET_SIZE

Utility methods for SSL packet processing. Copied from the Netty project.

This is a public class to allow testing to occur on Android via CTS.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SSLUtils.EngineStates

States for SSL engines.

pack-priv static enum

Field Summary

Modifier and TypeField and Description
private static final String
KEY_TYPE_EC

Key type: Elliptic Curve certificate.

private static final String
KEY_TYPE_RSA

Key type: RSA certificate.

private static final int
private static final int
MAX_ENCRYPTION_OVERHEAD_LENGTH

This is the maximum overhead when encrypting plaintext as defined by rfc5264, rfc5289, and the BoringSSL implementation itself.

private static final int
private static final Charset
pack-priv static final boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static int
calculateOutNetBufSize(int pendingBytes)

Calculates the minimum bytes required in the encrypted output buffer for the given number of plaintext source bytes.

pack-priv static String[]
concat(String[]... arrays)

pack-priv static String[]

Returns:

the decoded protocols or EmptyArray#BYTE if protocols is empty.
decodeProtocols
(byte[]
the encoded protocol list
protocols
)

Decodes the given list of protocols into Strings.

private static X509Certificate
decodeX509Certificate(CertificateFactory certificateFactory, byte[] bytes)

pack-priv static X509Certificate[]
decodeX509CertificateChain(byte[][] certChain)

pack-priv static byte[]

Returns:

the encoded form of the protocol list.
encodeProtocols
(String[]
the list of protocols to be encoded
protocols
)

Encodes a list of protocols into the wire-format (length-prefixed 8-bit strings).

pack-priv static byte[][]
private static CertificateFactory
pack-priv static String
getClientKeyType(byte clientCertificateType)

Similar to getServerKeyType, but returns value given TLS ClientCertificateType byte values from a CertificateRequest message for use with X509KeyManager.chooseClientAlias or X509ExtendedKeyManager.chooseEngineClientAlias.

pack-priv static String
pack-priv static int

Returns:

length The length of the encrypted packet that is included in the buffer. This will return -1 if the given ByteBuffer is not encrypted at all.
getEncryptedPacketLength
(ByteBuffer[]
The ByteBuffers to read from. Be aware that they must have at least org.conscrypt.NativeConstants#SSL3_RT_HEADER_LENGTH bytes to read, otherwise it will throw an IllegalArgumentException.
buffers
,
int offset)

Return how much bytes can be read out of the encrypted data.

private static int
pack-priv static String
getServerX509KeyType(long sslCipherNative)

Returns key type constant suitable for calling X509KeyManager.chooseServerAlias or X509ExtendedKeyManager.chooseEngineServerAlias.

pack-priv static Set<String>

Returns:

supported key types that can be used in X509KeyManager.chooseClientAlias and X509ExtendedKeyManager.chooseEngineClientAlias. If the inputs imply a preference order, the returned set will have an iteration order that respects that preference order, otherwise it will be in an arbitrary order. Visible for testing.
getSupportedClientKeyTypes
(byte[]
ClientCertificateType values provided by the server. See https://www.ietf.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-2.
clientCertificateTypes
,
int[]
SignatureScheme values provided by the server. See https://www.ietf.org/assignments/tls-parameters/tls-parameters.xml#tls-signaturescheme
signatureAlgs
)

Gets the supported key types for client certificates based on the ClientCertificateType values provided by the server.

pack-priv static X509Certificate[]
toCertificateChain(X509Certificate[] certificates)

Deprecated as a consequence of X509Certificate being deprecated.
Converts the peer certificates into a cert chain.
pack-priv static byte[]
pack-priv static String
toProtocolString(byte[] bytes)

pack-priv static SSLException
toSSLException(Throwable e)

Wraps the given exception if it's not already a SSLException.

pack-priv static SSLHandshakeException
toSSLHandshakeException(Throwable e)

Wraps the given exception if it's not already a SSLHandshakeException.

private static short
unsignedByte(byte b)

private static int
unsignedShort(short s)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

KEY_TYPE_ECback to summary
private static final String KEY_TYPE_EC

Key type: Elliptic Curve certificate.

KEY_TYPE_RSAback to summary
private static final String KEY_TYPE_RSA

Key type: RSA certificate.

MAX_ENCRYPTION_OVERHEAD_DIFFback to summary
private static final int MAX_ENCRYPTION_OVERHEAD_DIFF
MAX_ENCRYPTION_OVERHEAD_LENGTHback to summary
private static final int MAX_ENCRYPTION_OVERHEAD_LENGTH

This is the maximum overhead when encrypting plaintext as defined by rfc5264, rfc5289, and the BoringSSL implementation itself. Please note that we use a padding of 16 here as BoringSSL uses PKCS#5 which uses 16 bytes while the spec itself allow up to 255 bytes. 16 bytes is the max for PKCS#5 (which handles it the same way as PKCS#7) as we use a block size of 16. See rfc5652#section-6.3. 16 (IV) + 48 (MAC) + 1 (Padding_length field) + 15 (Padding) + 1 (ContentType in TLSCiphertext) + 2 (ProtocolVersion) + 2 (Length) + 1 (ContentType in TLSInnerPlaintext)

MAX_PROTOCOL_LENGTHback to summary
private static final int MAX_PROTOCOL_LENGTH
US_ASCIIback to summary
private static final Charset US_ASCII
USE_ENGINE_SOCKET_BY_DEFAULTback to summary
pack-priv static final boolean USE_ENGINE_SOCKET_BY_DEFAULT

Constructor Detail

SSLUtilsback to summary
private SSLUtils()

Method Detail

calculateOutNetBufSizeback to summary
pack-priv static int calculateOutNetBufSize(int pendingBytes)

Calculates the minimum bytes required in the encrypted output buffer for the given number of plaintext source bytes.

concatback to summary
pack-priv static String[] concat(String[]... arrays)
decodeProtocolsback to summary
pack-priv static String[] decodeProtocols(byte[] protocols)

Decodes the given list of protocols into Strings.

Parameters
protocols:byte[]

the encoded protocol list

Returns:String[]

the decoded protocols or EmptyArray#BYTE if protocols is empty.

Exceptions
NullPointerException:
if protocols is null.
decodeX509Certificateback to summary
private static X509Certificate decodeX509Certificate(CertificateFactory certificateFactory, byte[] bytes) throws CertificateException
decodeX509CertificateChainback to summary
pack-priv static X509Certificate[] decodeX509CertificateChain(byte[][] certChain) throws CertificateException
encodeProtocolsback to summary
pack-priv static byte[] encodeProtocols(String[] protocols)

Encodes a list of protocols into the wire-format (length-prefixed 8-bit strings). Requires that all strings be encoded with US-ASCII.

Parameters
protocols:String[]

the list of protocols to be encoded

Returns:byte[]

the encoded form of the protocol list.

Exceptions
IllegalArgumentException:
if protocols is null, or if any element is null or an empty string.
encodeSubjectX509Principalsback to summary
pack-priv static byte[][] encodeSubjectX509Principals(X509Certificate[] certificates) throws CertificateEncodingException
getCertificateFactoryback to summary
private static CertificateFactory getCertificateFactory()
getClientKeyTypeback to summary
pack-priv static String getClientKeyType(byte clientCertificateType)

Similar to getServerKeyType, but returns value given TLS ClientCertificateType byte values from a CertificateRequest message for use with X509KeyManager.chooseClientAlias or X509ExtendedKeyManager.chooseEngineClientAlias.

Visible for testing.

getClientKeyTypeFromSignatureAlgback to summary
pack-priv static String getClientKeyTypeFromSignatureAlg(int signatureAlg)
getEncryptedPacketLengthback to summary
pack-priv static int getEncryptedPacketLength(ByteBuffer[] buffers, int offset)

Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase the readerIndex of the given ByteBuffer.

Parameters
buffers:ByteBuffer[]

The ByteBuffers to read from. Be aware that they must have at least org.conscrypt.NativeConstants#SSL3_RT_HEADER_LENGTH bytes to read, otherwise it will throw an IllegalArgumentException.

Returns:int

length The length of the encrypted packet that is included in the buffer. This will return -1 if the given ByteBuffer is not encrypted at all.

Exceptions
IllegalArgumentException:
Is thrown if the given ByteBuffer has not at least org.conscrypt.NativeConstants#SSL3_RT_HEADER_LENGTH bytes to read.
getEncryptedPacketLengthback to summary
private static int getEncryptedPacketLength(ByteBuffer buffer)
getServerX509KeyTypeback to summary
pack-priv static String getServerX509KeyType(long sslCipherNative)

Returns key type constant suitable for calling X509KeyManager.chooseServerAlias or X509ExtendedKeyManager.chooseEngineServerAlias. Returns null for key exchanges that do not use X.509 for server authentication.

getSupportedClientKeyTypesback to summary
pack-priv static Set<String> getSupportedClientKeyTypes(byte[] clientCertificateTypes, int[] signatureAlgs)

Gets the supported key types for client certificates based on the ClientCertificateType values provided by the server.

Parameters
clientCertificateTypes:byte[]

ClientCertificateType values provided by the server. See https://www.ietf.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-2.

signatureAlgs:int[]

SignatureScheme values provided by the server. See https://www.ietf.org/assignments/tls-parameters/tls-parameters.xml#tls-signaturescheme

Returns:Set<String>

supported key types that can be used in X509KeyManager.chooseClientAlias and X509ExtendedKeyManager.chooseEngineClientAlias. If the inputs imply a preference order, the returned set will have an iteration order that respects that preference order, otherwise it will be in an arbitrary order. Visible for testing.

toCertificateChainback to summary
pack-priv static X509Certificate[] toCertificateChain(X509Certificate[] certificates) throws SSLPeerUnverifiedException

Deprecated

as a consequence of X509Certificate being deprecated. See corresponding docs for further information.

Converts the peer certificates into a cert chain.

toProtocolBytesback to summary
pack-priv static byte[] toProtocolBytes(String protocol)
toProtocolStringback to summary
pack-priv static String toProtocolString(byte[] bytes)
toSSLExceptionback to summary
pack-priv static SSLException toSSLException(Throwable e)

Wraps the given exception if it's not already a SSLException.

toSSLHandshakeExceptionback to summary
pack-priv static SSLHandshakeException toSSLHandshakeException(Throwable e)

Wraps the given exception if it's not already a SSLHandshakeException.

unsignedByteback to summary
private static short unsignedByte(byte b)
unsignedShortback to summary
private static int unsignedShort(short s)
org.conscrypt back to summary

pack-priv final Class SSLUtils.EngineStates

extends Object
Class Inheritance

States for SSL engines.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
STATE_CLOSED

The engine has been closed.

pack-priv static final int
STATE_CLOSED_INBOUND

The inbound direction of the engine has been closed.

pack-priv static final int
STATE_CLOSED_OUTBOUND

The outbound direction of the engine has been closed.

pack-priv static final int
STATE_HANDSHAKE_COMPLETED

Listeners of the handshake have been notified of completion but the handshake call hasn't returned.

pack-priv static final int
STATE_HANDSHAKE_STARTED

The handshake has been started

pack-priv static final int
STATE_MODE_SET

The client/server mode of the engine has been set.

pack-priv static final int
STATE_NEW

The engine is constructed, but the initial handshake hasn't been started

pack-priv static final int
STATE_READY

The handshake call has returned and the listeners have been notified.

pack-priv static final int
STATE_READY_HANDSHAKE_CUT_THROUGH

The handshake call returned but the listeners have not yet been notified.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

STATE_CLOSEDback to summary
pack-priv static final int STATE_CLOSED

The engine has been closed.

STATE_CLOSED_INBOUNDback to summary
pack-priv static final int STATE_CLOSED_INBOUND

The inbound direction of the engine has been closed.

STATE_CLOSED_OUTBOUNDback to summary
pack-priv static final int STATE_CLOSED_OUTBOUND

The outbound direction of the engine has been closed.

STATE_HANDSHAKE_COMPLETEDback to summary
pack-priv static final int STATE_HANDSHAKE_COMPLETED

Listeners of the handshake have been notified of completion but the handshake call hasn't returned.

STATE_HANDSHAKE_STARTEDback to summary
pack-priv static final int STATE_HANDSHAKE_STARTED

The handshake has been started

STATE_MODE_SETback to summary
pack-priv static final int STATE_MODE_SET

The client/server mode of the engine has been set.

STATE_NEWback to summary
pack-priv static final int STATE_NEW

The engine is constructed, but the initial handshake hasn't been started

STATE_READYback to summary
pack-priv static final int STATE_READY

The handshake call has returned and the listeners have been notified. Ready to begin writing data.

STATE_READY_HANDSHAKE_CUT_THROUGHback to summary
pack-priv static final int STATE_READY_HANDSHAKE_CUT_THROUGH

The handshake call returned but the listeners have not yet been notified. This is expected behaviour in cut-through mode, where SSL_do_handshake returns before the handshake is complete. We can now start writing data to the socket.

Constructor Detail

EngineStatesback to summary
private EngineStates()
org.conscrypt back to summary

pack-priv final Enum SSLUtils.SessionType

extends Enum<SSLUtils.SessionType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final SSLUtils.SessionType
OPEN_SSL

Identifies OpenSSL sessions.

public static final SSLUtils.SessionType
OPEN_SSL_WITH_OCSP

Identifies OpenSSL sessions with OCSP stapled data.

public static final SSLUtils.SessionType
OPEN_SSL_WITH_TLS_SCT

Identifies OpenSSL sessions with TLS SCT data.

pack-priv final int

Constructor Summary

AccessConstructor and Description
private
SessionType(int value)

Method Summary

Modifier and TypeMethod and Description
pack-priv static boolean
isSupportedType(int type)

public static SSLUtils.SessionType
public static SSLUtils.SessionType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

OPEN_SSLback to summary
public static final SSLUtils.SessionType OPEN_SSL

Identifies OpenSSL sessions.

OPEN_SSL_WITH_OCSPback to summary
public static final SSLUtils.SessionType OPEN_SSL_WITH_OCSP

Identifies OpenSSL sessions with OCSP stapled data.

OPEN_SSL_WITH_TLS_SCTback to summary
public static final SSLUtils.SessionType OPEN_SSL_WITH_TLS_SCT

Identifies OpenSSL sessions with TLS SCT data.

valueback to summary
pack-priv final int value

Constructor Detail

SessionTypeback to summary
private SessionType(int value)

Method Detail

isSupportedTypeback to summary
pack-priv static boolean isSupportedType(int type)
valueOfback to summary
public static SSLUtils.SessionType valueOf(String name)
valuesback to summary
public static SSLUtils.SessionType[] values()