Class Socket

extends Object with CloseableOutMixin CloseableInMixin implements Socket Reader
TLS socket implementation that can upgrade a TCP socket to a secure TLS socket.

Class summary


client socket_/Socket --server-name/string= --certificate/Certificate= --root-certificates/any= --handshake-timeout/Duration= --skip-certificate-validation/bool= -> Socket
Creates a new TLS socket for a client-side TCP socket.
server socket_/Socket --certificate/Certificate --root-certificates/any= --handshake-timeout/Duration= -> Socket
Creates a new TLS socket for a server-side TCP socket.

Statics

client socket_/Socket --server-name/string=null --certificate/Certificate=null --root-certificates/any=[] --handshake-timeout/Duration=Session.DEFAULT-HANDSHAKE-TIMEOUT --skip-certificate-validation/bool=false -> Socket
Creates a new TLS socket for a client-side TCP socket.
If server-name is provided, it will validate the peer certificate against that. If the server-name is omitted, it will skip validation.
The root-certificates are used to validate the peer certificate. It is generally preferred to install root certificates on a process level, rather than passing them to each TLS socket.
If certificate is given, the certificate is used by the server to validate the authority of the client. This is not done using e.g. HTTPS communication.
The handshake routine requires at most handshake-timeout between each step in the handshake process.
Validation of the server certificate can be disabled by setting skip-certificate-validation to true. This is not recommended, as it allows man-in-the-middle attacks. However, establishing a connection without verification consumes less resources, and can be useful in some cases.
When connecting to a server that uses a self-signed certificate prefer to install the server's certificate as root certificate.

server socket_/Socket --certificate/Certificate --root-certificates/any=[] --handshake-timeout/Duration=Session.DEFAULT-HANDSHAKE-TIMEOUT -> Socket
Creates a new TLS socket for a server-side TCP socket.
The root-certificates are used to validate the peer certificate if present.
If certificate is used as the authority of the server.
The handshake routine requires at most handshake-timeout between each step in the handshake process.

Methods

close -> none

close-write -> none
Deprecated. Use out.close.

handshake -> none
Explicitly completes the handshake step.
This method will automatically be called by read and write if the handshake is not completed yet.

mtu -> int

no-delay= value/bool -> none

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

Deprecated. Use in.read.

Returns true if the session was successfully resumed, rather than going through a full handshake with asymmetric crypto.
Returns false until the handshake is complete.

Gets the session state, a ByteArray that can be used to resume a TLS session at a later point.
The session can be read at any point after a handshake, but before the session is closed.

Set the state from a previous connection to the same TLS server.
This can dramatically speed up the handshake process.
Note that we don't currently have the ability to fall back from a resumed session to a full handshake, so if the session is invalid, or the server has forgotten about it, the handshake will fail.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

write data/Data from/int=0 to/int=data.byte-size -> int
Deprecated. Use out.write.