Table of Contents

Class TlsProtocol

Namespace
Org.BouncyCastle.Tls
Assembly
BouncyCastle.Cryptography.dll
public abstract class TlsProtocol : TlsCloseable
Inheritance
TlsProtocol
Implements
Derived
Inherited Members

Constructors

TlsProtocol()

protected TlsProtocol()

TlsProtocol(Stream)

public TlsProtocol(Stream stream)

Parameters

stream Stream

TlsProtocol(Stream, Stream)

public TlsProtocol(Stream input, Stream output)

Parameters

input Stream
output Stream

Fields

ADS_MODE_0_N

protected const short ADS_MODE_0_N = 1

Field Value

short

ADS_MODE_0_N_FIRSTONLY

protected const short ADS_MODE_0_N_FIRSTONLY = 2

Field Value

short

ADS_MODE_1_Nsub1

protected const short ADS_MODE_1_Nsub1 = 0

Field Value

short

CS_CLIENT_CERTIFICATE

protected const short CS_CLIENT_CERTIFICATE = 15

Field Value

short

CS_CLIENT_CERTIFICATE_VERIFY

protected const short CS_CLIENT_CERTIFICATE_VERIFY = 17

Field Value

short

CS_CLIENT_END_OF_EARLY_DATA

protected const short CS_CLIENT_END_OF_EARLY_DATA = 13

Field Value

short

CS_CLIENT_FINISHED

protected const short CS_CLIENT_FINISHED = 18

Field Value

short

CS_CLIENT_HELLO

protected const short CS_CLIENT_HELLO = 1

Field Value

short

CS_CLIENT_HELLO_RETRY

protected const short CS_CLIENT_HELLO_RETRY = 3

Field Value

short

CS_CLIENT_KEY_EXCHANGE

protected const short CS_CLIENT_KEY_EXCHANGE = 16

Field Value

short

CS_CLIENT_SUPPLEMENTAL_DATA

protected const short CS_CLIENT_SUPPLEMENTAL_DATA = 14

Field Value

short

CS_END

protected const short CS_END = 21

Field Value

short

CS_SERVER_CERTIFICATE

protected const short CS_SERVER_CERTIFICATE = 7

Field Value

short

CS_SERVER_CERTIFICATE_REQUEST

protected const short CS_SERVER_CERTIFICATE_REQUEST = 11

Field Value

short

CS_SERVER_CERTIFICATE_STATUS

protected const short CS_SERVER_CERTIFICATE_STATUS = 8

Field Value

short

CS_SERVER_CERTIFICATE_VERIFY

protected const short CS_SERVER_CERTIFICATE_VERIFY = 9

Field Value

short

CS_SERVER_ENCRYPTED_EXTENSIONS

protected const short CS_SERVER_ENCRYPTED_EXTENSIONS = 5

Field Value

short

CS_SERVER_FINISHED

protected const short CS_SERVER_FINISHED = 20

Field Value

short

CS_SERVER_HELLO

protected const short CS_SERVER_HELLO = 4

Field Value

short

CS_SERVER_HELLO_DONE

protected const short CS_SERVER_HELLO_DONE = 12

Field Value

short

CS_SERVER_HELLO_RETRY_REQUEST

protected const short CS_SERVER_HELLO_RETRY_REQUEST = 2

Field Value

short

CS_SERVER_KEY_EXCHANGE

protected const short CS_SERVER_KEY_EXCHANGE = 10

Field Value

short

CS_SERVER_SESSION_TICKET

protected const short CS_SERVER_SESSION_TICKET = 19

Field Value

short

CS_SERVER_SUPPLEMENTAL_DATA

protected const short CS_SERVER_SUPPLEMENTAL_DATA = 6

Field Value

short

CS_START

protected const short CS_START = 0

Field Value

short

m_blocking

protected readonly bool m_blocking

Field Value

bool

m_clientExtensions

protected IDictionary<int, byte[]> m_clientExtensions

Field Value

IDictionary<int, byte[]>

m_connectionState

protected short m_connectionState

Field Value

short

m_expectSessionTicket

protected bool m_expectSessionTicket

Field Value

bool

m_inputBuffers

protected readonly ByteQueueInputStream m_inputBuffers

Field Value

ByteQueueInputStream

m_outputBuffer

protected readonly ByteQueueOutputStream m_outputBuffer

Field Value

ByteQueueOutputStream

m_receivedChangeCipherSpec

protected bool m_receivedChangeCipherSpec

Field Value

bool

m_retryCookie

protected byte[] m_retryCookie

Field Value

byte[]

m_retryGroup

protected int m_retryGroup

Field Value

int

m_selectedPsk13

protected bool m_selectedPsk13

Field Value

bool

m_serverExtensions

protected IDictionary<int, byte[]> m_serverExtensions

Field Value

IDictionary<int, byte[]>

m_sessionMasterSecret

protected TlsSecret m_sessionMasterSecret

Field Value

TlsSecret

m_sessionParameters

protected SessionParameters m_sessionParameters

Field Value

SessionParameters

m_tlsSession

protected TlsSession m_tlsSession

Field Value

TlsSession

Properties

AppDataSplitMode

public virtual int AppDataSplitMode { get; set; }

Property Value

int

ApplicationDataAvailable

public virtual int ApplicationDataAvailable { get; }

Property Value

int

ApplicationDataLimit

public virtual int ApplicationDataLimit { get; }

Property Value

int

Context

protected abstract TlsContext Context { get; }

Property Value

TlsContext

IsClosed

public virtual bool IsClosed { get; }

Property Value

bool

IsConnected

public virtual bool IsConnected { get; }

Property Value

bool

IsHandshaking

public virtual bool IsHandshaking { get; }

Property Value

bool

IsResumableHandshake

public virtual bool IsResumableHandshake { get; set; }

Property Value

bool

Peer

protected abstract TlsPeer Peer { get; }

Property Value

TlsPeer

Stream

The secure bidirectional stream for this connection

public virtual Stream Stream { get; }

Property Value

Stream

Remarks

Only allowed in blocking mode.

Methods

ApplyMaxFragmentLengthExtension(short)

protected virtual void ApplyMaxFragmentLengthExtension(short maxFragmentLength)

Parameters

maxFragmentLength short

Exceptions

IOException

BeginHandshake()

protected virtual void BeginHandshake()

Exceptions

IOException

BlockForHandshake()

protected virtual void BlockForHandshake()

Exceptions

IOException

CancelSession()

protected virtual void CancelSession()

CheckReceivedChangeCipherSpec(bool)

protected virtual void CheckReceivedChangeCipherSpec(bool expected)

Parameters

expected bool

Exceptions

IOException

CleanupHandshake()

protected virtual void CleanupHandshake()

Close()

public virtual void Close()

Exceptions

IOException

CloseConnection()

protected virtual void CloseConnection()

Exceptions

IOException

CloseInput()

Should be called in non-blocking mode when the input data reaches EOF.

public virtual void CloseInput()

Exceptions

IOException

CompleteHandshake()

protected virtual void CompleteHandshake()

Exceptions

IOException

EstablishSession(TlsSession)

protected virtual bool EstablishSession(TlsSession sessionToResume)

Parameters

sessionToResume TlsSession

Returns

bool

Flush()

public virtual void Flush()

GetAvailableInputBytes()

Gets the amount of received application data.

public virtual int GetAvailableInputBytes()

Returns

int

The number of bytes of available application data.

Remarks

A call to ReadInput(byte[], int, int) is guaranteed to be able to return at least this much data.

Only allowed in non-blocking mode.

GetAvailableOutputBytes()

Gets the amount of encrypted data available to be sent.

public virtual int GetAvailableOutputBytes()

Returns

int

The number of bytes of available encrypted data.

Remarks

A call to ReadOutput(byte[], int, int) is guaranteed to be able to return at least this much data. Only allowed in non-blocking mode.

HandleAlertMessage(short, short)

protected virtual void HandleAlertMessage(short alertLevel, short alertDescription)

Parameters

alertLevel short
alertDescription short

Exceptions

IOException

HandleAlertWarningMessage(short)

protected virtual void HandleAlertWarningMessage(short alertDescription)

Parameters

alertDescription short

Exceptions

IOException

HandleChangeCipherSpecMessage()

protected virtual void HandleChangeCipherSpecMessage()

Exceptions

IOException

HandleClose(bool)

protected virtual void HandleClose(bool user_canceled)

Parameters

user_canceled bool

Exceptions

IOException

HandleException(short, string, Exception)

protected virtual void HandleException(short alertDescription, string message, Exception e)

Parameters

alertDescription short
message string
e Exception

Exceptions

IOException

HandleFailure()

protected virtual void HandleFailure()

Exceptions

IOException

HandleHandshakeMessage(short, HandshakeMessageInput)

protected abstract void HandleHandshakeMessage(short type, HandshakeMessageInput buf)

Parameters

type short
buf HandshakeMessageInput

Exceptions

IOException

InvalidateSession()

protected virtual void InvalidateSession()

IsLegacyConnectionState()

protected bool IsLegacyConnectionState()

Returns

bool

IsTlsV13ConnectionState()

protected bool IsTlsV13ConnectionState()

Returns

bool

OfferInput(byte[])

Equivalent to

OfferInput(input, 0, input.Length)
.
public virtual void OfferInput(byte[] input)

Parameters

input byte[]

The input buffer to offer.

Exceptions

IOException
See Also

OfferInput(byte[], int, int)

Offer input from an arbitrary source.

public virtual void OfferInput(byte[] input, int inputOff, int inputLen)

Parameters

input byte[]

The input buffer to offer.

inputOff int

The offset within the input buffer that input begins.

inputLen int

The number of bytes of input being offered.

Remarks

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 using ReadInput(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 calling GetAvailableOutputBytes().

Exceptions

IOException

If an error occurs while decrypting or processing a record.

PreviewInputRecord(byte[])

public virtual RecordPreview PreviewInputRecord(byte[] recordHeader)

Parameters

recordHeader byte[]

Returns

RecordPreview

Exceptions

IOException

PreviewOutputRecord()

public virtual int PreviewOutputRecord()

Returns

int

PreviewOutputRecord(int)

public virtual RecordPreview PreviewOutputRecord(int applicationDataSize)

Parameters

applicationDataSize int

Returns

RecordPreview

Exceptions

IOException

Process13FinishedMessage(MemoryStream)

protected virtual void Process13FinishedMessage(MemoryStream buf)

Parameters

buf MemoryStream

Exceptions

IOException

ProcessFinishedMessage(MemoryStream)

protected virtual void ProcessFinishedMessage(MemoryStream buf)

Parameters

buf MemoryStream

Exceptions

IOException

ProcessMaxFragmentLengthExtension(IDictionary<int, byte[]>, IDictionary<int, byte[]>, short)

[Obsolete("Will be removed")]
protected virtual short ProcessMaxFragmentLengthExtension(IDictionary<int, byte[]> clientExtensions, IDictionary<int, byte[]> serverExtensions, short alertDescription)

Parameters

clientExtensions IDictionary<int, byte[]>
serverExtensions IDictionary<int, byte[]>
alertDescription short

Returns

short

Exceptions

IOException

RaiseAlertFatal(short, string, Exception)

protected virtual void RaiseAlertFatal(short alertDescription, string message, Exception cause)

Parameters

alertDescription short
message string
cause Exception

Exceptions

IOException

RaiseAlertWarning(short, string)

protected virtual void RaiseAlertWarning(short alertDescription, string message)

Parameters

alertDescription short
message string

Exceptions

IOException

ReadApplicationData(byte[], int, int)

Read data from the network.

public virtual int ReadApplicationData(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

The buffer where the data will be copied to.

offset int

The position where the data will be placed in the buffer.

count int

The maximum number of bytes to read.

Returns

int

The number of bytes read.

Remarks

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.

Exceptions

IOException

If something goes wrong during reading data.

ReadApplicationData(Span<byte>)

public virtual int ReadApplicationData(Span<byte> buffer)

Parameters

buffer Span<byte>

Returns

int

ReadInput(byte[], int, int)

Retrieves received application data.

public virtual int ReadInput(byte[] buf, int off, int len)

Parameters

buf byte[]

The buffer to hold the application data.

off int

The start offset in the buffer at which the data is written.

len int

The maximum number of bytes to read.

Returns

int

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.

Remarks

Use GetAvailableInputBytes() to check how much application data is currently available. This method functions similarly to 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.

ReadOutput(byte[], int, int)

Retrieves encrypted data to be sent.

public virtual int ReadOutput(byte[] buffer, int offset, int length)

Parameters

buffer byte[]

The buffer to hold the encrypted data.

offset int

The start offset in the buffer at which the data is written.

length int

The maximum number of bytes to read.

Returns

int

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.

Remarks

Use GetAvailableOutputBytes() to check how much encrypted data is currently available. This method functions similarly to 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.

Receive13KeyUpdate(MemoryStream)

protected virtual void Receive13KeyUpdate(MemoryStream buf)

Parameters

buf MemoryStream

Exceptions

IOException

RefuseRenegotiation()

protected virtual void RefuseRenegotiation()

Exceptions

IOException

ResumeHandshake()

public virtual void ResumeHandshake()

Exceptions

IOException

SafePreviewRecordHeader(byte[])

protected virtual RecordPreview SafePreviewRecordHeader(byte[] recordHeader)

Parameters

recordHeader byte[]

Returns

RecordPreview

Exceptions

IOException

SafeReadFullRecord(byte[], int, int)

protected virtual bool SafeReadFullRecord(byte[] input, int inputOff, int inputLen)

Parameters

input byte[]
inputOff int
inputLen int

Returns

bool

Exceptions

IOException

SafeReadRecord()

protected virtual void SafeReadRecord()

Exceptions

IOException

SafeWriteRecord(short, byte[], int, int)

protected virtual void SafeWriteRecord(short type, byte[] buf, int offset, int len)

Parameters

type short
buf byte[]
offset int
len int

Exceptions

IOException

SafeWriteRecord(short, ReadOnlySpan<byte>)

protected virtual void SafeWriteRecord(short type, ReadOnlySpan<byte> buffer)

Parameters

type short
buffer ReadOnlySpan<byte>

Exceptions

IOException

Send13CertificateMessage(Certificate)

protected virtual void Send13CertificateMessage(Certificate certificate)

Parameters

certificate Certificate

Exceptions

IOException

Send13CertificateVerifyMessage(DigitallySigned)

protected virtual void Send13CertificateVerifyMessage(DigitallySigned certificateVerify)

Parameters

certificateVerify DigitallySigned

Exceptions

IOException

Send13FinishedMessage()

protected virtual void Send13FinishedMessage()

Exceptions

IOException

Send13KeyUpdate(bool)

protected virtual void Send13KeyUpdate(bool updateRequested)

Parameters

updateRequested bool

Exceptions

IOException

SendCertificateMessage(Certificate, Stream)

protected virtual void SendCertificateMessage(Certificate certificate, Stream endPointHash)

Parameters

certificate Certificate
endPointHash Stream

Exceptions

IOException

SendChangeCipherSpec()

protected virtual void SendChangeCipherSpec()

Exceptions

IOException

SendChangeCipherSpecMessage()

protected virtual void SendChangeCipherSpecMessage()

Exceptions

IOException

SendFinishedMessage()

protected virtual void SendFinishedMessage()

Exceptions

IOException

SendSupplementalDataMessage(IList<SupplementalDataEntry>)

protected virtual void SendSupplementalDataMessage(IList<SupplementalDataEntry> supplementalData)

Parameters

supplementalData IList<SupplementalDataEntry>

Exceptions

IOException

WriteApplicationData(byte[], int, int)

Write some application data.

public virtual void WriteApplicationData(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

The buffer containing application data to send.

offset int

The offset at which the application data begins

count int

The number of bytes of application data.

Remarks

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, call ReadOutput(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 an InvalidOperationException.

Exceptions

InvalidOperationException

If called before the initial handshake has completed.

IOException

If connection is already closed, or for encryption or transport errors.

WriteApplicationData(ReadOnlySpan<byte>)

public virtual void WriteApplicationData(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>