class

Beryl::TLS::RecordLayer

Inherits Reference < Object

The record layer over an IO (RFC 9846 §5): writes and reads records, protects them once keys are installed, reassembles handshake messages and applies the plan's limits.

It does not decide what a message means; the handshake state machines and Socket do. What it refuses on its own, with the alert RFC 9846 names: records over the size limits (record_overflow), records that fail authentication (bad_record_mac), plaintext application data or a protected record with an outer type other than application_data (unexpected_message), zero-length handshake or alert fragments, a handshake message interleaved with another content type, an alert that is not two bytes (decode_error), and the floods the limits bound.

Every buffer it keeps starts at the size of the first record that needs it (the read-ahead buffer at a record header, then the first record) and grows, doubling, up to the record limits only when a larger record arrives, so an idle connection with small requests holds a few kilobytes. It reads ahead into its own buffer, so it turns off read buffering on an IO::Buffered under it, for good: the bytes would otherwise be buffered twice.

Constants

HANDSHAKE_HEADER_BYTESIZE = 4

A handshake message header: type and a 24-bit length.

LARGEST_FRAGMENT_BYTESIZE = 16384

The largest plaintext fragment (§5.1).

LARGEST_PROTECTED_RECORD_BYTESIZE = (RecordProtection::HEADER_BYTESIZE + RecordProtection::LARGEST_INNER_PLAINTEXT_BYTESIZE) + 16

The largest protected record this end writes: its header, the largest inner plaintext (content and type, no padding) and the tag.

LARGEST_RECORD_BYTESIZE = RecordProtection::HEADER_BYTESIZE + RecordProtection::LARGEST_CIPHERTEXT_BYTESIZE

The largest record on the wire: its header and the largest TLSCiphertext fragment.

Constructors

new(io : IO, limits : Limits = Limits.new)
Source

Instance methods

closed_for_writing?

Whether a close_notify was written.

Source
early_data_skip_budget

Bytes of records that fail to open which a server still skips, while it refuses the 0-RTT data a client offered (RFC 9846 §4.2.10).

Source
early_data_skip_budget=(early_data_skip_budget : Int32)

Bytes of records that fail to open which a server still skips, while it refuses the 0-RTT data a client offered (RFC 9846 §4.2.10).

Source
flush

Flushes the IO.

Source
handshake_buffer_empty?

Returns whether no part of a handshake message is waiting. A key change must not split a message (§5.1).

Source
is_accepting_plaintext_alerts=(is_accepting_plaintext_alerts : Bool)

Whether a plaintext alert is read while protection is installed: a server sets it while it waits for the client's flight, since a client that refuses the ServerHello has no keys to protect its alert with.

Source
is_accepting_plaintext_alerts?

Whether a plaintext alert is read while protection is installed: a server sets it while it waits for the client's flight, since a client that refuses the ServerHello has no keys to protect its alert with.

Source
is_change_cipher_spec_allowed=(is_change_cipher_spec_allowed : Bool)

Whether one compatibility change_cipher_spec is still accepted (RFC 9846 §5, Appendix D.4). A server turns it off until the first ClientHello is read; both ends turn it off once the handshake ends.

Source
is_change_cipher_spec_allowed?

Whether one compatibility change_cipher_spec is still accepted (RFC 9846 §5, Appendix D.4). A server turns it off until the first ClientHello is read; both ends turn it off once the handshake ends.

Source
is_skipping_plaintext_early_data=(is_skipping_plaintext_early_data : Bool)

Whether plaintext application_data records are skipped (within the same budget): a client that offered 0-RTT and got a HelloRetryRequest may still send them (§4.2.10).

Source
is_skipping_plaintext_early_data?

Whether plaintext application_data records are skipped (within the same budget): a client that offered 0-RTT and got a HelloRetryRequest may still send them (§4.2.10).

Source
largest_incoming_inner_plaintext_bytesize

The largest inner plaintext (content, type and padding) a protected record may carry: this end's record_size_limit once both ends sent one (RFC 8449 §4), 2^14 + 1 bytes until then.

Source
largest_incoming_inner_plaintext_bytesize=(largest_incoming_inner_plaintext_bytesize : Int32)

The largest inner plaintext (content, type and padding) a protected record may carry: this end's record_size_limit once both ends sent one (RFC 8449 §4), 2^14 + 1 bytes until then.

Source
largest_outgoing_fragment_bytesize

The most content bytes per record written (the peer's record_size_limit less one, RFC 8449 §4).

Source
largest_outgoing_fragment_bytesize=(largest_outgoing_fragment_bytesize : Int32)

The most content bytes per record written (the peer's record_size_limit less one, RFC 8449 §4).

Source
peer_closed?

Whether the peer sent close_notify.

Source
read_after_handshake

Reads the next record after the handshake. Returns application data as {ApplicationData, bytes}, a whole post-handshake message as {Handshake, message}, or nil once the peer sent close_notify.

Source
read_handshake_message

Reads the next whole handshake message, header included. Raises when anything but handshake records arrives, or the peer closes.

Source
read_protection

Keys for reading, or nil before the ServerHello.

Source
read_protection=(read_protection : RecordProtection | Nil)

Keys for reading, or nil before the ServerHello.

Source
write_alert(level : AlertLevel, description : AlertDescription) : Nil

Writes an alert. Errors from the IO are swallowed: an alert is the last thing sent on a failing connection.

Source
write_application_data(content : Bytes) : Nil

Writes content as application data records.

Source
write_change_cipher_spec

Writes the one-byte compatibility change_cipher_spec record (Appendix D.4), always in plaintext.

Source
write_handshake(message : Bytes, *, legacy_record_version : UInt16 = ProtocolVersion::TLS_1_2) : Nil

Writes message (a whole handshake message) in as many handshake records as it takes. The first ClientHello goes out with the legacy record version 0x0301 (§5.1).

Source
write_protection

Keys for writing, or nil before the ServerHello.

Source
write_protection=(write_protection : RecordProtection | Nil)

Keys for writing, or nil before the ServerHello.

Source