Package org.bouncycastle.tls
Class TlsProtocol
java.lang.Object
org.bouncycastle.tls.TlsProtocol
- All Implemented Interfaces:
TlsCloseable
- Direct Known Subclasses:
TlsClientProtocol,TlsServerProtocol
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final shortprotected static final shortprotected static final shortprotected booleanprotected Hashtableprotected shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected static final shortprotected booleanprotected static final Integerprotected static final Integerprotected ByteQueueInputStreamprotected ByteQueueOutputStreamprotected booleanprotected byte[]protected intprotected booleanprotected Hashtableprotected TlsSecretprotected SessionParametersprotected TlsSession -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedTlsProtocol(InputStream input, OutputStream output) -
Method Summary
Modifier and TypeMethodDescriptionintprotected voidapplyMaxFragmentLengthExtension(short maxFragmentLength) protected static voidMake sure the InputStream 'buf' now empty.protected voidbeginHandshake(boolean renegotiation) protected voidprotected voidprotected voidcheckReceivedChangeCipherSpec(boolean expected) protected voidvoidclose()Closes this connection.protected voidvoidShould be called in non-blocking mode when the input data reaches EOF.protected voidprotected static byte[]createRandomBlock(boolean useGMTUnixTime, TlsContext context) protected static byte[]createRenegotiationInfo(byte[] renegotiated_connection) protected static voidestablishMasterSecret(TlsContext context, TlsKeyExchange keyExchange) protected booleanestablishSession(TlsSession sessionToResume) voidflush()intintintGets the amount of received application data.intGets the amount of encrypted data available to be sent.protected abstract TlsContextprotected abstract TlsPeergetPeer()protected intprotected voidhandleAlertMessage(short alertLevel, short alertDescription) protected voidhandleAlertWarningMessage(short alertDescription) protected voidprotected voidhandleClose(boolean user_canceled) protected voidhandleException(short alertDescription, String message, Throwable e) protected voidprotected abstract voidhandleHandshakeMessage(short type, HandshakeMessageInput buf) protected booleanprotected voidbooleanisClosed()booleanbooleanprotected booleanbooleanprotected booleanvoidofferInput(byte[] input) Equivalent toofferInput(input, 0, input.length)voidofferInput(byte[] input, int inputOff, int inputLen) Offer input from an arbitrary source.previewInputRecord(byte[] recordHeader) intpreviewOutputRecord(int applicationDataSize) protected voidprotected voidprotected shortprocessMaxFragmentLengthExtension(Hashtable clientExtensions, Hashtable serverExtensions, short alertDescription) Deprecated.Will be removed.protected voidprocessRecord(short protocol, byte[] buf, int off, int len) protected voidraiseAlertFatal(short alertDescription, String message, Throwable cause) protected voidraiseAlertWarning(short alertDescription, String message) intreadApplicationData(byte[] buf, int off, int len) Read data from the network.protected static Hashtableprotected static HashtablereadExtensionsData(byte[] extBytes) protected static HashtablereadExtensionsData13(int handshakeType, byte[] extBytes) protected static HashtablereadExtensionsDataClientHello(byte[] extBytes) intreadInput(byte[] buffer, int offset, int length) Retrieves received application data.intreadInput(ByteBuffer buffer, int length) Retrieves received application data into aByteBuffer.intreadOutput(byte[] buffer, int offset, int length) Retrieves encrypted data to be sent.intreadOutput(ByteBuffer buffer, int length) Retrieves encrypted data to be sent.protected static Vectorprotected voidprotected voidvoidprotected RecordPreviewsafePreviewRecordHeader(byte[] recordHeader) protected booleansafeReadFullRecord(byte[] input, int inputOff, int inputLen) protected voidprotected voidsafeWriteRecord(short type, byte[] buf, int offset, int len) protected voidsend13CertificateMessage(Certificate certificate) protected voidsend13CertificateVerifyMessage(DigitallySigned certificateVerify) protected voidprotected voidsend13KeyUpdate(boolean updateRequested) protected voidsendCertificateMessage(Certificate certificate, OutputStream endPointHash) protected voidprotected voidprotected voidprotected voidsendSupplementalDataMessage(Vector supplementalData) voidsetAppDataSplitMode(int appDataSplitMode) voidsetResumableHandshake(boolean resumableHandshake) voidwriteApplicationData(byte[] buf, int off, int len) Write some application data.protected static voidwriteExtensions(OutputStream output, Hashtable extensions) protected static voidwriteExtensions(OutputStream output, Hashtable extensions, int bindersSize) protected static byte[]writeExtensionsData(Hashtable extensions) protected static byte[]writeExtensionsData(Hashtable extensions, int bindersSize) protected static voidwriteExtensionsData(Hashtable extensions, int bindersSize, ByteArrayOutputStream buf) protected static voidwritePreSharedKeyExtension(OutputStream output, Hashtable extensions, int bindersSize) protected static voidwriteSelectedExtensions(OutputStream output, Hashtable extensions, boolean selectEmpty) protected static voidwriteSupplementalData(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- TheByteBufferto 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- TheByteBufferto 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:
closein 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
-