Struct rustls::conn::ConnectionCommon
source · pub struct ConnectionCommon<Data> {
pub(crate) core: ConnectionCore<Data>,
}
Expand description
Interface shared by client and server connections.
Fields§
§core: ConnectionCore<Data>
Implementations§
source§impl<Data> ConnectionCommon<Data>
impl<Data> ConnectionCommon<Data>
sourcepub fn complete_io<T>(&mut self, io: &mut T) -> Result<(usize, usize), Error>
pub fn complete_io<T>(&mut self, io: &mut T) -> Result<(usize, usize), Error>
This function uses io
to complete any outstanding IO for
this connection.
This is a convenience function which solely uses other parts of the public API.
What this means depends on the connection state:
- If the connection
is_handshaking
, then IO is performed until the handshake is complete. - Otherwise, if
wants_write
is true,write_tls
is invoked until it is all written. - Otherwise, if
wants_read
is true,read_tls
is invoked once.
The return value is the number of bytes read from and written
to io
, respectively.
This function will block if io
blocks.
Errors from TLS record handling (i.e., from process_new_packets
)
are wrapped in an io::ErrorKind::InvalidData
-kind error.
sourcepub(crate) fn first_handshake_message(
&mut self,
) -> Result<Option<Message>, Error>
pub(crate) fn first_handshake_message( &mut self, ) -> Result<Option<Message>, Error>
Extract the first handshake message.
This is a shortcut to the process_new_packets()
-> process_msg()
->
process_handshake_messages()
path, specialized for the first handshake message.
pub(crate) fn replace_state(&mut self, new: Box<dyn State<Data>>)
sourcepub fn process_new_packets(&mut self) -> Result<IoState, Error>
pub fn process_new_packets(&mut self) -> Result<IoState, Error>
Processes any new packets read by a previous call to
Connection::read_tls
.
Errors from this function relate to TLS protocol errors, and
are fatal to the connection. Future calls after an error will do
no new work and will return the same error. After an error is
received from process_new_packets
, you should not call read_tls
any more (it will fill up buffers to no purpose). However, you
may call the other methods on the connection, including write
,
send_close_notify
, and write_tls
. Most likely you will want to
call write_tls
to send any alerts queued by the error and then
close the underlying connection.
Success from this function comes with some sundry state data about the connection.
sourcepub fn read_tls(&mut self, rd: &mut dyn Read) -> Result<usize, Error>
pub fn read_tls(&mut self, rd: &mut dyn Read) -> Result<usize, Error>
Read TLS content from rd
into the internal buffer.
Due to the internal buffering, rd
can supply TLS messages in arbitrary-sized chunks (like
a socket or pipe might).
You should call process_new_packets()
each time a call to this function succeeds in order
to empty the incoming TLS data buffer.
This function returns Ok(0)
when the underlying rd
does so. This typically happens when
a socket is cleanly closed, or a file is at EOF. Errors may result from the IO done through
rd
; additionally, errors of ErrorKind::Other
are emitted to signal backpressure:
- In order to empty the incoming TLS data buffer, you should call
process_new_packets()
each time a call to this function succeeds. - In order to empty the incoming plaintext data buffer, you should empty it through
the
reader()
after the call toprocess_new_packets()
.
sourcepub fn write_tls(&mut self, wr: &mut dyn Write) -> Result<usize, Error>
pub fn write_tls(&mut self, wr: &mut dyn Write) -> Result<usize, Error>
Writes TLS messages to wr
.
On success, this function returns Ok(n)
where n
is a number of bytes written to wr
(after encoding and encryption).
After this function returns, the connection buffer may not yet be fully flushed. The
CommonState::wants_write
function can be used to check if the output buffer is empty.
sourcepub fn export_keying_material<T: AsMut<[u8]>>(
&self,
output: T,
label: &[u8],
context: Option<&[u8]>,
) -> Result<T, Error>
pub fn export_keying_material<T: AsMut<[u8]>>( &self, output: T, label: &[u8], context: Option<&[u8]>, ) -> Result<T, Error>
Derives key material from the agreed connection secrets.
This function fills in output
with output.len()
bytes of key
material derived from the master session secret using label
and context
for diversification. Ownership of the buffer is taken
by the function and returned via the Ok result to ensure no key
material leaks if the function fails.
See RFC5705 for more details on what this does and is for.
For TLS1.3 connections, this function does not use the “early” exporter at any point.
This function fails if called prior to the handshake completing;
check with CommonState::is_handshaking
first.
Methods from Deref<Target = CommonState>§
sourcepub fn wants_write(&self) -> bool
pub fn wants_write(&self) -> bool
Returns true if the caller should call Connection::write_tls
as soon as possible.
sourcepub fn is_handshaking(&self) -> bool
pub fn is_handshaking(&self) -> bool
Returns true if the connection is currently performing the TLS handshake.
During this time plaintext written to the connection is buffered in memory. After
Connection::process_new_packets()
has been called, this might start to return false
while the final handshake packets still need to be extracted from the connection’s buffers.
sourcepub fn peer_certificates(&self) -> Option<&[Certificate]>
pub fn peer_certificates(&self) -> Option<&[Certificate]>
Retrieves the certificate chain used by the peer to authenticate.
The order of the certificate chain is as it appears in the TLS protocol: the first certificate relates to the peer, the second certifies the first, the third certifies the second, and so on.
This is made available for both full and resumed handshakes.
For clients, this is the certificate chain of the server.
For servers, this is the certificate chain of the client, if client authentication was completed.
The return value is None until this value is available.
sourcepub fn alpn_protocol(&self) -> Option<&[u8]>
pub fn alpn_protocol(&self) -> Option<&[u8]>
Retrieves the protocol agreed with the peer via ALPN.
A return value of None
after handshake completion
means no protocol was agreed (because no protocols
were offered or accepted by the peer).
sourcepub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite>
pub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite>
Retrieves the ciphersuite agreed with the peer.
This returns None until the ciphersuite is agreed.
sourcepub fn protocol_version(&self) -> Option<ProtocolVersion>
pub fn protocol_version(&self) -> Option<ProtocolVersion>
Retrieves the protocol version agreed with the peer.
This returns None
until the version is agreed.
pub(crate) fn is_tls13(&self) -> bool
pub(crate) fn process_main_protocol<Data>( &mut self, msg: Message, state: Box<dyn State<Data>>, data: &mut Data, ) -> Result<Box<dyn State<Data>>, Error>
sourcepub(crate) fn send_some_plaintext(&mut self, data: &[u8]) -> usize
pub(crate) fn send_some_plaintext(&mut self, data: &[u8]) -> usize
Send plaintext application data, fragmenting and encrypting it as it goes out.
If internal buffers are too small, this function will not accept all the data.
pub(crate) fn send_early_plaintext(&mut self, data: &[u8]) -> usize
pub(crate) fn check_aligned_handshake(&mut self) -> Result<(), Error>
sourcepub(crate) fn send_msg_encrypt(&mut self, m: PlainMessage)
pub(crate) fn send_msg_encrypt(&mut self, m: PlainMessage)
Fragment m
, encrypt the fragments, and then queue
the encrypted fragments for sending.
sourcefn send_appdata_encrypt(&mut self, payload: &[u8], limit: Limit) -> usize
fn send_appdata_encrypt(&mut self, payload: &[u8], limit: Limit) -> usize
Like send_msg_encrypt, but operate on an appdata directly.
fn send_single_fragment(&mut self, m: BorrowedPlainMessage<'_>)
sourcefn send_plain(&mut self, data: &[u8], limit: Limit) -> usize
fn send_plain(&mut self, data: &[u8], limit: Limit) -> usize
Encrypt and send some plaintext data
. limit
controls
whether the per-connection buffer limits apply.
Returns the number of bytes written from data
: this might
be less than data.len()
if buffer limits were exceeded.
pub(crate) fn start_outgoing_traffic(&mut self)
pub(crate) fn start_traffic(&mut self)
sourcepub fn set_buffer_limit(&mut self, limit: Option<usize>)
pub fn set_buffer_limit(&mut self, limit: Option<usize>)
Sets a limit on the internal buffers used to buffer
unsent plaintext (prior to completing the TLS handshake)
and unsent TLS records. This limit acts only on application
data written through Connection::writer
.
By default the limit is 64KB. The limit can be set at any time, even if the current buffer use is higher.
None
means no limit applies, and will mean that written
data is buffered without bound – it is up to the application
to appropriately schedule its plaintext and TLS writes to bound
memory usage.
For illustration: Some(1)
means a limit of one byte applies:
Connection::writer
will accept only one byte, encrypt it and
add a TLS header. Once this is sent via Connection::write_tls
,
another byte may be sent.
§Internal write-direction buffering
rustls has two buffers whose size are bounded by this setting:
§Buffering of unsent plaintext data prior to handshake completion
Calls to Connection::writer
before or during the handshake
are buffered (up to the limit specified here). Once the
handshake completes this data is encrypted and the resulting
TLS records are added to the outgoing buffer.
§Buffering of outgoing TLS records
This buffer is used to store TLS records that rustls needs to send to the peer. It is used in these two circumstances:
- by
Connection::process_new_packets
when a handshake or alert TLS record needs to be sent. - by
Connection::writer
post-handshake: the plaintext is encrypted and the resulting TLS record is buffered.
This buffer is emptied by Connection::write_tls
.
sourcefn flush_plaintext(&mut self)
fn flush_plaintext(&mut self)
Send any buffered plaintext. Plaintext is buffered if written during handshake.
fn queue_tls_message(&mut self, m: OpaqueMessage)
sourcepub(crate) fn send_msg(&mut self, m: Message, must_encrypt: bool)
pub(crate) fn send_msg(&mut self, m: Message, must_encrypt: bool)
Send a raw TLS message, fragmenting it if needed.
pub(crate) fn take_received_plaintext(&mut self, bytes: Payload)
pub(crate) fn start_encryption_tls12( &mut self, secrets: &ConnectionSecrets, side: Side, )
fn send_warning_alert(&mut self, desc: AlertDescription)
pub(crate) fn process_alert( &mut self, alert: &AlertMessagePayload, ) -> Result<(), Error>
pub(crate) fn send_cert_verify_error_alert(&mut self, err: Error) -> Error
pub(crate) fn send_fatal_alert( &mut self, desc: AlertDescription, err: impl Into<Error>, ) -> Error
sourcepub fn send_close_notify(&mut self)
pub fn send_close_notify(&mut self)
Queues a close_notify warning alert to be sent in the next
Connection::write_tls
call. This informs the peer that the
connection is being closed.
fn send_warning_alert_no_log(&mut self, desc: AlertDescription)
pub(crate) fn set_max_fragment_size( &mut self, new: Option<usize>, ) -> Result<(), Error>
pub(crate) fn get_alpn_protocol(&self) -> Option<&[u8]>
sourcepub fn wants_read(&self) -> bool
pub fn wants_read(&self) -> bool
Returns true if the caller should call Connection::read_tls
as soon
as possible.
If there is pending plaintext data to read with Connection::reader
,
this returns false. If your application respects this mechanism,
only one full TLS message will be buffered by rustls.