Tls¶
| Spice | |
|---|---|
TlsContext struct¶
Owns an SSL_CTX, i.e. the certificate/trust/verification setup that is shared by every connection made or accepted through it. Build one with newClientTlsContext or newServerTlsContext, and hand it to openTlsClientSocket or acceptTlsConnection.
A default-constructed context wraps no SSL_CTX and must not be used - it only exists so that structs which embed a TlsContext (like HttpServer) can be default-constructed themselves.
Methods¶
close¶
| Spice | |
|---|---|
Releases the underlying SSL_CTX. Safe to call more than once and on a default-constructed, never-initialized context.
TlsSocket struct¶
A TCP socket with a TLS record layer on top. Exposes the same read/writeAll/setTimeout shape as Socket, so the generic wire transport of "std/net/http" (sendRequest, receiveResponse, ...) works on it unchanged.
Methods¶
read¶
| Spice | |
|---|---|
Reads up to size bytes from the connection into the given buffer. Note: The given buffer needs to be at least of the given size.
Parameters
| Name | Type | Description |
|---|---|---|
buffer |
byte* |
Buffer to write the result into |
size |
long |
Number of bytes to read |
Returns: long — Number of bytes read, or a value <= 0 on error/EOF
writeAll¶
| Spice | |
|---|---|
Writes an array of bytes to the connection, retrying until either all of them were handed over to TLS or the connection breaks. Note: The given buffer needs to be at least of the given size.
Parameters
| Name | Type | Description |
|---|---|---|
content |
byte* |
Buffer of bytes to send |
size |
unsigned long |
Number of bytes from the buffer to send |
Returns: bool — true if all bytes were written, false if the connection broke in between
setTimeout¶
| Spice | |
|---|---|
Limits how long a single read or write on this connection may block. Forwards to the underlying Socket, see Socket.setTimeout.
Parameters
| Name | Type | Description |
|---|---|---|
milliseconds |
unsigned long |
Timeout in milliseconds, 0 to block indefinitely again |
Returns: bool — Setting the timeout was successful or not
closeConnection¶
| Spice | |
|---|---|
Shuts down the TLS layer and closes the current connection, while keeping the listening socket (for a server) open. This is what a server does after it has finished serving a single client.
Returns: bool — Closing the connection was successful or not
close¶
| Spice | |
|---|---|
Shuts down the TLS layer and closes the socket. This method should always be called by the user before exiting the program.
Returns: bool — Closing the connection was successful or not
Functions¶
newClientTlsContext¶
| Spice | |
|---|---|
Builds the TLS context a client hands to openTlsClientSocket. Server certificates are always verified; there is no escape hatch to turn that off, since a client that does not verify the server it talks to gets no confidentiality guarantee worth having.
Parameters
| Name | Type | Description |
|---|---|---|
caFile |
string |
Path to a PEM file with trusted CA certificates, or "" to trust the operating system's default trust store (default: "") |
Returns: Result<TlsContext> — A ready-to-use client context, or an error describing why it could not be built
newServerTlsContext¶
| Spice | |
|---|---|
Builds the TLS context an HttpServer hands to acceptTlsConnection, loading the certificate chain and private key it presents to clients.
Parameters
| Name | Type | Description |
|---|---|---|
certFile |
string |
Path to a PEM file with the server's certificate (chain) |
keyFile |
string |
Path to a PEM file with the private key matching the certificate |
Returns: Result<TlsContext> — A ready-to-use server context, or an error describing why it could not be built
openTlsClientSocket¶
| Spice | |
|---|---|
Opens a TCP connection to the given host and port and performs a TLS client handshake on top of it, verifying the server's certificate against context and its name against host.
Parameters
| Name | Type | Description |
|---|---|---|
host |
string |
Host to connect to, either as dotted IPv4 address or as hostname |
port |
unsigned short |
Port to connect to |
context |
const TlsContext& |
Client context that decides which certificates are trusted |
timeoutMillis |
unsigned long |
Read/write timeout applied before the handshake, 0 to block indefinitely. Without this, a peer that accepts the TCP connection but never sends any TLS data would hang the handshake forever, regardless of any timeout the caller applies to the socket afterwards. (default: 0l) |
Returns: Result<TlsSocket> — The established TLS connection, or an error describing why it could not be established
acceptTlsConnection¶
| Spice | |
|---|---|
Performs a TLS server handshake on the connection sock most recently accepted via Socket.acceptConnection.
This is a free function rather than a method on Socket because Socket is defined per-platform (see "std/net/socket_linux" and friends) and Spice does not support adding methods to a struct from outside the file that defines it.
Parameters
| Name | Type | Description |
|---|---|---|
sock |
Socket& |
Socket whose current connection to handshake |
context |
const TlsContext& |
Server context that supplies the certificate and private key |
Returns: Result<TlsSocket> — The established TLS connection, or an error describing why it could not be established