class

Beryl::TLS::Socket

Inherits IO::Buffered < IO < Reference < Object

A TLS 1.3 connection over another IO, shaped like OpenSSL::SSL::Socket: an IO with IO::Buffered, and the same accessors (alpn_protocol, cipher, tls_version, hostname, peer_certificate).

tcp = TCPSocket.new("example.com", 443)
tls = Beryl::TLS::Socket::Client.new(tcp, hostname: "example.com", sync_close: true)
tls << "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
tls.flush
tls.gets_to_end

Reads and writes may run in two fibers at once (one each). A failure sends the alert the error names before it is raised.

The socket keeps no IO::Buffered buffers of its own. Reads (read, peek, gets) are served from the plaintext of the record last opened, so read_buffering? has no effect. Writes gather, up to one record's worth, in a buffer that starts at the size of the first write and grows with what is written, and are sealed into a record at flush, when that record is full, or at close.

Instance methods

alpn_protocol

The negotiated ALPN protocol, or nil.

Source
cipher

The IANA name of the negotiated suite, e.g. TLS_AES_128_GCM_SHA256.

Source
cipher_suite

The negotiated suite.

Source
close

Seals the gathered plaintext, then closes as unbuffered_close does.

Source
closed?

Whether close has been called. Reads and writes after it raise IO::Error ("Closed stream"), as on any closed IO, and a pool or a driver that checks closed? (as OpenSSL::SSL::Socket answers it) sees the connection is gone.

Source
export_keying_material(label : String, context : Bytes, bytesize : Int32) : Bytes

Returns keying material exported from the connection (RFC 9846 §7.5), e.g. for channel binding.

Source
flush

Seals the gathered plaintext into a record and flushes the IO.

Source
group

The negotiated key exchange group.

Source
is_raising_io_errors=(is_raising_io_errors : Bool)

Whether reads and writes raise a TLS failure as ConnectionFailed (an IO::Error) instead of the TLS::Error itself. A server that treats IO::Error as the end of a connection, as HTTP::Server does, then ends a failed handshake quietly; Server turns it on for the sockets it accepts.

Source
is_raising_io_errors?

Whether reads and writes raise a TLS failure as ConnectionFailed (an IO::Error) instead of the TLS::Error itself. A server that treats IO::Error as the end of a connection, as HTTP::Server does, then ends a failed handshake quietly; Server turns it on for the sockets it accepts.

Source
local_address
Source
peek

Returns the plaintext waiting to be read, reading the next record when none is waiting; empty at the end of the stream.

Source
read(slice : Bytes) : Int32

Reads straight from the plaintext of the record last opened.

Source
read_timeout
Source
read_timeout=(timeout)
Source
remote_address
Source
resumed?

Whether the session was resumed from a ticket.

Source
sent_key_update_count

How many KeyUpdates this end sent.

Source
sync_close=(sync_close : Bool)

Whether closing this socket closes the IO under it.

Source
sync_close?

Whether closing this socket closes the IO under it.

Source
tls_version

Always "TLSv1.3", the string OpenSSL reports.

Source
unbuffered_close

Sends close_notify (once the handshake is done), wipes the connection's secrets, then closes the IO below when sync_close?. Closing again does nothing.

Source
unbuffered_flush

Flushes the wrapped IO.

TODO: Add return type restriction Nil

Source
unbuffered_read(slice : Bytes) : Int32

Reads at most slice.size bytes from the wrapped IO into slice. Returns the number of bytes read.

TODO: Add return type restriction Int32

Source
unbuffered_rewind

Rewinds the wrapped IO.

TODO: Add return type restriction Nil

Source
unbuffered_write(slice : Bytes) : Nil

Writes slice entirely into the wrapped IO.

TODO: Add return type restriction Nil

Source
update_keys(request_peer_update : Bool = false) : Nil

Sends a KeyUpdate and moves to the next sending key (§4.6.3). With request_peer_update the peer is asked to update its key too.

Source
write(slice : Bytes) : Nil

Gathers slice with what was written before it, sealing the gathered plaintext into a record first when slice would not fit in the same record.

Source
write_timeout=(timeout)
Source

Nested types