Skip to main content

Connection

Trait Connection 

Source
pub trait Connection: Debug + Deref<Target = ConnectionOutputs> {
Show 14 methods // Required methods fn write_tls(&mut self, wr: &mut dyn Write) -> Result<usize, Error>; fn wants_read(&self) -> bool; fn wants_write(&self) -> bool; fn reader(&mut self) -> Reader<'_> ; fn writer(&mut self) -> Writer<'_> ; fn process_new_packets( &mut self, input: &mut dyn TlsInputBuffer, ) -> Result<IoState, Error>; fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error>; fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error>; fn set_buffer_limit(&mut self, limit: Option<usize>); fn set_plaintext_buffer_limit(&mut self, limit: Option<usize>); fn refresh_traffic_keys(&mut self) -> Result<(), Error>; fn send_close_notify(&mut self); fn is_handshaking(&self) -> bool; fn fips(&self) -> FipsStatus;
}
Expand description

A trait generalizing over buffered client or server connections.

Required Methods§

Source

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 Self::wants_write() function can be used to check if the output buffer is empty.

Source

fn wants_read(&self) -> bool

Returns true if the caller should call Self::process_new_packets() as soon as possible.

If there is pending plaintext data to read with Self::reader(), this returns false. If your application respects this mechanism, only one full TLS message will be buffered by rustls.

Source

fn wants_write(&self) -> bool

Returns true if the caller should call Self::write_tls() as soon as possible.

Source

fn reader(&mut self) -> Reader<'_>

Returns an object that allows reading plaintext.

Source

fn writer(&mut self) -> Writer<'_>

Returns an object that allows writing plaintext.

Source

fn process_new_packets( &mut self, input: &mut dyn TlsInputBuffer, ) -> Result<IoState, Error>

Processes any new packets from the buffer supplied in buf.

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 this function, you should not continue to fill up the buffer. However, you may call the other methods on the connection, including Self::writer(), Self::send_close_notify(), and Self::write_tls(). Most likely you will want to call Self::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.

Source

fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error>

Returns an object that can derive key material from the agreed connection secrets.

See RFC5705 for more details on what this is for.

This function can be called at most once per connection.

This function will error:

  • if called prior to the handshake completing; (check with Self::is_handshaking() first).
  • if called more than once per connection.
Source

fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error>

Extract secrets, so they can be used when configuring kTLS, for example.

Should be used with care as it exposes secret key material.

Source

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 Self::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: Self::writer() will accept only one byte, encrypt it and add a TLS header. Once this is sent via Self::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 Self::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:

This buffer is emptied by Self::write_tls().

Source

fn set_plaintext_buffer_limit(&mut self, limit: Option<usize>)

Sets a limit on the internal buffers used to buffer decoded plaintext.

See Self::set_buffer_limit() for more information on how limits are applied.

Source

fn refresh_traffic_keys(&mut self) -> Result<(), Error>

Sends a TLS1.3 key_update message to refresh a connection’s keys.

This call refreshes our encryption keys. Once the peer receives the message, it refreshes its encryption and decryption keys and sends a response. Once we receive that response, we refresh our decryption keys to match. At the end of this process, keys in both directions have been refreshed.

Note that this process does not happen synchronously: this call just arranges that the key_update message will be included in the next Self::write_tls() output.

This fails with Error::HandshakeNotComplete if called before the initial handshake is complete, or if a version prior to TLS1.3 is negotiated.

§Usage advice

Note that other implementations (including rustls) may enforce limits on the number of key_update messages allowed on a given connection to prevent denial of service. Therefore, this should be called sparingly.

rustls implicitly and automatically refreshes traffic keys when needed according to the selected cipher suite’s cryptographic constraints. There is therefore no need to call this manually to avoid cryptographic keys “wearing out”.

The main reason to call this manually is to roll keys when it is known a connection will be idle for a long period.

Source

fn send_close_notify(&mut self)

Queues a close_notify warning alert to be sent in the next Self::write_tls call.

This informs the peer that the connection is being closed.

Does nothing if any close_notify or fatal alert was already sent.

Source

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 Self::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.

Source

fn fips(&self) -> FipsStatus

Return the FIPS validation status of the connection.

This is different from CryptoProvider::fips(): it is concerned only with cryptography, whereas this also covers TLS-level configuration that NIST recommends, as well as ECH HPKE suites if applicable.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§