Package org.bouncycastle.tls
Class TlsProtocol
java.lang.Object
org.bouncycastle.tls.TlsProtocol
- All Implemented Interfaces:
TlsCloseable
- Direct Known Subclasses:
TlsClientProtocol
,TlsServerProtocol
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final short
protected static final short
protected static final short
protected boolean
protected Hashtable
protected short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected static final short
protected boolean
protected static final Integer
protected static final Integer
protected ByteQueueInputStream
protected ByteQueueOutputStream
protected boolean
protected byte[]
protected int
protected boolean
protected Hashtable
protected TlsSecret
protected SessionParameters
protected TlsSession
-
Constructor Summary
ModifierConstructorDescriptionprotected
protected
TlsProtocol
(InputStream input, OutputStream output) -
Method Summary
Modifier and TypeMethodDescriptionint
protected void
applyMaxFragmentLengthExtension
(short maxFragmentLength) protected static void
Make sure the InputStream 'buf' now empty.protected void
beginHandshake
(boolean renegotiation) protected void
protected void
protected void
checkReceivedChangeCipherSpec
(boolean expected) protected void
void
close()
Closes this connection.protected void
void
Should be called in non-blocking mode when the input data reaches EOF.protected void
protected static byte[]
createRandomBlock
(boolean useGMTUnixTime, TlsContext context) protected static byte[]
createRenegotiationInfo
(byte[] renegotiated_connection) protected static void
establishMasterSecret
(TlsContext context, TlsKeyExchange keyExchange) protected boolean
establishSession
(TlsSession sessionToResume) void
flush()
int
int
int
Gets the amount of received application data.int
Gets the amount of encrypted data available to be sent.protected abstract TlsContext
protected abstract TlsPeer
getPeer()
protected int
protected void
handleAlertMessage
(short alertLevel, short alertDescription) protected void
handleAlertWarningMessage
(short alertDescription) protected void
protected void
handleClose
(boolean user_canceled) protected void
handleException
(short alertDescription, String message, Throwable e) protected void
protected abstract void
handleHandshakeMessage
(short type, HandshakeMessageInput buf) protected boolean
protected void
boolean
isClosed()
boolean
boolean
protected boolean
boolean
protected boolean
void
offerInput
(byte[] input) Equivalent toofferInput(input, 0, input.length)
void
offerInput
(byte[] input, int inputOff, int inputLen) Offer input from an arbitrary source.previewInputRecord
(byte[] recordHeader) int
previewOutputRecord
(int applicationDataSize) protected void
protected void
protected short
processMaxFragmentLengthExtension
(Hashtable clientExtensions, Hashtable serverExtensions, short alertDescription) Deprecated.Will be removed.protected void
processRecord
(short protocol, byte[] buf, int off, int len) protected void
raiseAlertFatal
(short alertDescription, String message, Throwable cause) protected void
raiseAlertWarning
(short alertDescription, String message) int
readApplicationData
(byte[] buf, int off, int len) Read data from the network.protected static Hashtable
protected static Hashtable
readExtensionsData
(byte[] extBytes) protected static Hashtable
readExtensionsData13
(int handshakeType, byte[] extBytes) protected static Hashtable
readExtensionsDataClientHello
(byte[] extBytes) int
readInput
(byte[] buffer, int offset, int length) Retrieves received application data.int
readInput
(ByteBuffer buffer, int length) Retrieves received application data into aByteBuffer
.int
readOutput
(byte[] buffer, int offset, int length) Retrieves encrypted data to be sent.int
readOutput
(ByteBuffer buffer, int length) Retrieves encrypted data to be sent.protected static Vector
protected void
protected void
void
protected RecordPreview
safePreviewRecordHeader
(byte[] recordHeader) protected boolean
safeReadFullRecord
(byte[] input, int inputOff, int inputLen) protected void
protected void
safeWriteRecord
(short type, byte[] buf, int offset, int len) protected void
send13CertificateMessage
(Certificate certificate) protected void
send13CertificateVerifyMessage
(DigitallySigned certificateVerify) protected void
protected void
send13KeyUpdate
(boolean updateRequested) protected void
sendCertificateMessage
(Certificate certificate, OutputStream endPointHash) protected void
protected void
protected void
protected void
sendSupplementalDataMessage
(Vector supplementalData) void
setAppDataSplitMode
(int appDataSplitMode) void
setResumableHandshake
(boolean resumableHandshake) void
writeApplicationData
(byte[] buf, int off, int len) Write some application data.protected static void
writeExtensions
(OutputStream output, Hashtable extensions) protected static void
writeExtensions
(OutputStream output, Hashtable extensions, int bindersSize) protected static byte[]
writeExtensionsData
(Hashtable extensions) protected static byte[]
writeExtensionsData
(Hashtable extensions, int bindersSize) protected static void
writeExtensionsData
(Hashtable extensions, int bindersSize, ByteArrayOutputStream buf) protected static void
writePreSharedKeyExtension
(OutputStream output, Hashtable extensions, int bindersSize) protected static void
writeSelectedExtensions
(OutputStream output, Hashtable extensions, boolean selectEmpty) protected static void
writeSupplementalData
(OutputStream output, Vector supplementalData)
-
Field Details
-
EXT_RenegotiationInfo
-
EXT_SessionTicket
-
CS_START
protected static final short CS_START- See Also:
-
CS_CLIENT_HELLO
protected static final short CS_CLIENT_HELLO- See Also:
-
CS_SERVER_HELLO_RETRY_REQUEST
protected static final short CS_SERVER_HELLO_RETRY_REQUEST- See Also:
-
CS_CLIENT_HELLO_RETRY
protected static final short CS_CLIENT_HELLO_RETRY- See Also:
-
CS_SERVER_HELLO
protected static final short CS_SERVER_HELLO- See Also:
-
CS_SERVER_ENCRYPTED_EXTENSIONS
protected static final short CS_SERVER_ENCRYPTED_EXTENSIONS- See Also:
-
CS_SERVER_SUPPLEMENTAL_DATA
protected static final short CS_SERVER_SUPPLEMENTAL_DATA- See Also:
-
CS_SERVER_CERTIFICATE
protected static final short CS_SERVER_CERTIFICATE- See Also:
-
CS_SERVER_CERTIFICATE_STATUS
protected static final short CS_SERVER_CERTIFICATE_STATUS- See Also:
-
CS_SERVER_CERTIFICATE_VERIFY
protected static final short CS_SERVER_CERTIFICATE_VERIFY- See Also:
-
CS_SERVER_KEY_EXCHANGE
protected static final short CS_SERVER_KEY_EXCHANGE- See Also:
-
CS_SERVER_CERTIFICATE_REQUEST
protected static final short CS_SERVER_CERTIFICATE_REQUEST- See Also:
-
CS_SERVER_HELLO_DONE
protected static final short CS_SERVER_HELLO_DONE- See Also:
-
CS_CLIENT_END_OF_EARLY_DATA
protected static final short CS_CLIENT_END_OF_EARLY_DATA- See Also:
-
CS_CLIENT_SUPPLEMENTAL_DATA
protected static final short CS_CLIENT_SUPPLEMENTAL_DATA- See Also:
-
CS_CLIENT_CERTIFICATE
protected static final short CS_CLIENT_CERTIFICATE- See Also:
-
CS_CLIENT_KEY_EXCHANGE
protected static final short CS_CLIENT_KEY_EXCHANGE- See Also:
-
CS_CLIENT_CERTIFICATE_VERIFY
protected static final short CS_CLIENT_CERTIFICATE_VERIFY- See Also:
-
CS_CLIENT_FINISHED
protected static final short CS_CLIENT_FINISHED- See Also:
-
CS_SERVER_SESSION_TICKET
protected static final short CS_SERVER_SESSION_TICKET- See Also:
-
CS_SERVER_FINISHED
protected static final short CS_SERVER_FINISHED- See Also:
-
CS_END
protected static final short CS_END- See Also:
-
ADS_MODE_1_Nsub1
protected static final short ADS_MODE_1_Nsub1- See Also:
-
ADS_MODE_0_N
protected static final short ADS_MODE_0_N- See Also:
-
ADS_MODE_0_N_FIRSTONLY
protected static final short ADS_MODE_0_N_FIRSTONLY- See Also:
-
tlsSession
-
sessionParameters
-
sessionMasterSecret
-
retryCookie
protected byte[] retryCookie -
retryGroup
protected int retryGroup -
clientExtensions
-
serverExtensions
-
connection_state
protected short connection_state -
selectedPSK13
protected boolean selectedPSK13 -
receivedChangeCipherSpec
protected boolean receivedChangeCipherSpec -
expectSessionTicket
protected boolean expectSessionTicket -
blocking
protected boolean blocking -
inputBuffers
-
outputBuffer
-
-
Constructor Details
-
TlsProtocol
protected TlsProtocol() -
TlsProtocol
-
-
Method Details
-
isLegacyConnectionState
protected boolean isLegacyConnectionState() -
isTLSv13ConnectionState
protected boolean isTLSv13ConnectionState() -
resumeHandshake
- Throws:
IOException
-
closeConnection
- Throws:
IOException
-
getContext
-
getPeer
-
getRenegotiationPolicy
protected int getRenegotiationPolicy() -
handleAlertMessage
- Throws:
IOException
-
handleAlertWarningMessage
- Throws:
IOException
-
handleChangeCipherSpecMessage
- Throws:
IOException
-
handleClose
- Throws:
IOException
-
handleException
protected void handleException(short alertDescription, String message, Throwable e) throws IOException - Throws:
IOException
-
handleFailure
- Throws:
IOException
-
handleHandshakeMessage
protected abstract void handleHandshakeMessage(short type, HandshakeMessageInput buf) throws IOException - Throws:
IOException
-
handleRenegotiation
- Throws:
IOException
-
applyMaxFragmentLengthExtension
- Throws:
IOException
-
checkReceivedChangeCipherSpec
- Throws:
IOException
-
blockForHandshake
- Throws:
IOException
-
beginHandshake
- Throws:
IOException
-
cleanupHandshake
protected void cleanupHandshake() -
completeHandshake
- Throws:
IOException
-
processRecord
- Throws:
IOException
-
applicationDataAvailable
public int applicationDataAvailable() -
readApplicationData
Read data from the network. The method will return immediately, if there is still some data left in the buffer, or block until some application data has been read from the network.- Parameters:
buf
- The buffer where the data will be copied to.off
- The position where the data will be placed in the buffer.len
- The maximum number of bytes to read.- Returns:
- The number of bytes read.
- Throws:
IOException
- If something goes wrong during reading data.
-
safePreviewRecordHeader
- Throws:
IOException
-
safeReadRecord
- Throws:
IOException
-
safeReadFullRecord
- Throws:
IOException
-
safeWriteRecord
- Throws:
IOException
-
writeApplicationData
Write some application data. Fragmentation is handled internally. Usable in both blocking/non-blocking modes.
In blocking mode, the output will be automatically sent via the underlying transport. In non-blocking mode, callreadOutput(byte[], int, int)
to get the output bytes to send to the peer.
This method must not be called until after the initial handshake is complete. Attempting to call it earlier will result in anIllegalStateException
.- Parameters:
buf
- The buffer containing application data to sendoff
- The offset at which the application data beginslen
- The number of bytes of application data- Throws:
IllegalStateException
- If called before the initial handshake has completed.IOException
- If connection is already closed, or for encryption or transport errors.
-
getAppDataSplitMode
public int getAppDataSplitMode() -
setAppDataSplitMode
public void setAppDataSplitMode(int appDataSplitMode) -
isResumableHandshake
public boolean isResumableHandshake() -
setResumableHandshake
public void setResumableHandshake(boolean resumableHandshake) -
getOutputStream
- Returns:
- An OutputStream which can be used to send data. Only allowed in blocking mode.
-
getInputStream
- Returns:
- An InputStream which can be used to read data. Only allowed in blocking mode.
-
closeInput
Should be called in non-blocking mode when the input data reaches EOF.- Throws:
IOException
-
previewInputRecord
- Throws:
IOException
-
previewOutputRecord
public int previewOutputRecord() -
previewOutputRecord
- Throws:
IOException
-
offerInput
Equivalent toofferInput(input, 0, input.length)
- Parameters:
input
- The input buffer to offer- Throws:
IOException
- If an error occurs while decrypting or processing a record- See Also:
-
offerInput
Offer input from an arbitrary source. Only allowed in non-blocking mode.
This method will decrypt and process all records that are fully available. If only part of a record is available, the buffer will be retained until the remainder of the record is offered.
If any records containing application data were processed, the decrypted data can be obtained usingreadInput(byte[], int, int)
. If any records containing protocol data were processed, a response may have been generated. You should always check to see if there is any available output after calling this method by callinggetAvailableOutputBytes()
.- Parameters:
input
- The input buffer to offerinputOff
- The offset within the input buffer that input beginsinputLen
- The number of bytes of input being offered- Throws:
IOException
- If an error occurs while decrypting or processing a record
-
getApplicationDataLimit
public int getApplicationDataLimit() -
getAvailableInputBytes
public int getAvailableInputBytes()Gets the amount of received application data. A call toreadInput(byte[], int, int)
is guaranteed to be able to return at least this much data.
Only allowed in non-blocking mode.- Returns:
- The number of bytes of available application data
-
readInput
public int readInput(byte[] buffer, int offset, int length) Retrieves received application data. UsegetAvailableInputBytes()
to check how much application data is currently available. This method functions similarly toInputStream.read(byte[], int, int)
, except that it never blocks. If no data is available, nothing will be copied and zero will be returned.
Only allowed in non-blocking mode.- Parameters:
buffer
- The buffer to hold the application dataoffset
- The start offset in the buffer at which the data is writtenlength
- The maximum number of bytes to read- Returns:
- The total number of bytes copied to the buffer. May be less than the length specified if the length was greater than the amount of available data.
-
readInput
Retrieves received application data into aByteBuffer
. UsegetAvailableInputBytes()
to check how much application data is currently available. This method functions similarly toInputStream.read(byte[], int, int)
, except that it never blocks. If no data is available, nothing will be copied and zero will be returned.
Only allowed in non-blocking mode.- Parameters:
buffer
- TheByteBuffer
to hold the application datalength
- The maximum number of bytes to read- Returns:
- The total number of bytes copied to the buffer. May be less than the length specified if the length was greater than the amount of available data.
-
getAvailableOutputBytes
public int getAvailableOutputBytes()Gets the amount of encrypted data available to be sent. A call toreadOutput(byte[], int, int)
is guaranteed to be able to return at least this much data.
Only allowed in non-blocking mode.- Returns:
- The number of bytes of available encrypted data
-
readOutput
public int readOutput(byte[] buffer, int offset, int length) Retrieves encrypted data to be sent. UsegetAvailableOutputBytes()
to check how much encrypted data is currently available. This method functions similarly toInputStream.read(byte[], int, int)
, except that it never blocks. If no data is available, nothing will be copied and zero will be returned.
Only allowed in non-blocking mode.- Parameters:
buffer
- The buffer to hold the encrypted dataoffset
- The start offset in the buffer at which the data is writtenlength
- The maximum number of bytes to read- Returns:
- The total number of bytes copied to the buffer. May be less than the length specified if the length was greater than the amount of available data.
-
readOutput
Retrieves encrypted data to be sent. UsegetAvailableOutputBytes()
to check how much encrypted data is currently available. This method functions similarly toInputStream.read(byte[], int, int)
, except that it never blocks. If no data is available, nothing will be copied and zero will be returned.
Only allowed in non-blocking mode.- Parameters:
buffer
- TheByteBuffer
to hold the encrypted datalength
- The maximum number of bytes to read- Returns:
- The total number of bytes copied to the buffer. May be less than the length specified if the length was greater than the amount of available data.
-
establishSession
-
cancelSession
protected void cancelSession() -
invalidateSession
protected void invalidateSession() -
processFinishedMessage
- Throws:
IOException
-
process13FinishedMessage
- Throws:
IOException
-
raiseAlertFatal
protected void raiseAlertFatal(short alertDescription, String message, Throwable cause) throws IOException - Throws:
IOException
-
raiseAlertWarning
- Throws:
IOException
-
receive13KeyUpdate
- Throws:
IOException
-
sendCertificateMessage
protected void sendCertificateMessage(Certificate certificate, OutputStream endPointHash) throws IOException - Throws:
IOException
-
send13CertificateMessage
- Throws:
IOException
-
send13CertificateVerifyMessage
- Throws:
IOException
-
sendChangeCipherSpec
- Throws:
IOException
-
sendChangeCipherSpecMessage
- Throws:
IOException
-
sendFinishedMessage
- Throws:
IOException
-
send13FinishedMessage
- Throws:
IOException
-
send13KeyUpdate
- Throws:
IOException
-
sendSupplementalDataMessage
- Throws:
IOException
-
close
Closes this connection.- Specified by:
close
in interfaceTlsCloseable
- Throws:
IOException
- If something goes wrong during closing.
-
flush
- Throws:
IOException
-
isClosed
public boolean isClosed() -
isConnected
public boolean isConnected() -
isHandshaking
public boolean isHandshaking() -
processMaxFragmentLengthExtension
protected short processMaxFragmentLengthExtension(Hashtable clientExtensions, Hashtable serverExtensions, short alertDescription) throws IOException Deprecated.Will be removed.- Throws:
IOException
-
refuseRenegotiation
- Throws:
IOException
-
assertEmpty
Make sure the InputStream 'buf' now empty. Fail otherwise.- Parameters:
buf
- The InputStream to check.- Throws:
IOException
- If 'buf' is not empty.
-
createRandomBlock
-
createRenegotiationInfo
- Throws:
IOException
-
establishMasterSecret
protected static void establishMasterSecret(TlsContext context, TlsKeyExchange keyExchange) throws IOException - Throws:
IOException
-
readExtensions
- Throws:
IOException
-
readExtensionsData
- Throws:
IOException
-
readExtensionsData13
protected static Hashtable readExtensionsData13(int handshakeType, byte[] extBytes) throws IOException - Throws:
IOException
-
readExtensionsDataClientHello
- Throws:
IOException
-
readSupplementalDataMessage
- Throws:
IOException
-
writeExtensions
- Throws:
IOException
-
writeExtensions
protected static void writeExtensions(OutputStream output, Hashtable extensions, int bindersSize) throws IOException - Throws:
IOException
-
writeExtensionsData
- Throws:
IOException
-
writeExtensionsData
protected static byte[] writeExtensionsData(Hashtable extensions, int bindersSize) throws IOException - Throws:
IOException
-
writeExtensionsData
protected static void writeExtensionsData(Hashtable extensions, int bindersSize, ByteArrayOutputStream buf) throws IOException - Throws:
IOException
-
writeSelectedExtensions
protected static void writeSelectedExtensions(OutputStream output, Hashtable extensions, boolean selectEmpty) throws IOException - Throws:
IOException
-
writeSupplementalData
protected static void writeSupplementalData(OutputStream output, Vector supplementalData) throws IOException - Throws:
IOException
-