Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-tls

Source of release 0.7.1

0.7.1 / src / session.lucb

## The connection-plus-state TLS API: the caller owns a `net.Connection` and a
## `Secrets` (the record channel) and passes both to `send`/`receive`. These
## calls block on the connection. New code should prefer `tls_stream.Stream`,
## which owns both, implements io.Reader/io.Writer and supports deadlines.
from luce_std import net
import c
import record
import client
import server
import trust
import chain

pub let invalid = record.invalid

## The record channel of one connection: traffic keys, sequence numbers and
## buffered plaintext. `Secrets()` is an empty channel for a new handshake.
pub type Secrets = record.Channel

## Re-exported trust policy; see `trust.Trust`.
pub type Trust = trust.Trust

func dial(host: str, port: u16) -> net.Connection!:
    var host_c: u8[256]
    if host.length + 1 > host_c.length:
        error(invalid, "host name too long")
    for i in 0..<host.length:
        host_c[i] = host.bytes[i]
    host_c[host.length] = 0
    let address = try net.resolve((c.str)(str)host_c[..<host.length], port)
    return try net.Connection.connect(address)

func dial_and_handshake(host: str, port: u16, policy: const Trust*, alpn: str, secrets: Secrets*) -> net.Connection!:
    var conn = try dial(host, port)
    errdefer conn.destroy()
    try client.connect(secrets, &conn, &conn, host, policy, alpn)
    return conn

## Connect to a public server and validate its chain against the bundled roots.
## No ALPN is offered.
pub func connect_trusted(host: str, port: u16, secrets: Secrets*) -> net.Connection!:
    let policy = Trust.public_roots()
    return try dial_and_handshake(host, port, &policy, "", secrets)

## Private-server profile: the P-256 leaf must be signed directly by this
## issuer key. Offers ALPN http/1.1.
pub func connect(host: str, port: u16, issuer_x: const u8[], issuer_y: const u8[], secrets: Secrets*) -> net.Connection!:
    let policy = try Trust.pinned_issuer(issuer_x, issuer_y)
    return try dial_and_handshake(host, port, &policy, "http/1.1", secrets)

## A strict chain ending at a caller-supplied P-384 root. Offers ALPN http/1.1.
pub func connect_p384_chain(host: str, port: u16, root_x: const u8[], root_y: const u8[], secrets: Secrets*) -> net.Connection!:
    let policy = try Trust.p384_root(root_x, root_y)
    return try dial_and_handshake(host, port, &policy, "http/1.1", secrets)

## The Lucia OS edge profile: the chain must end at ISRG Root X2. Offers ALPN
## http/1.1.
pub func connect_public(host: str, port: u16, secrets: Secrets*) -> net.Connection!:
    let policy = try Trust.p384_root(chain.isrg_root_x2_x, chain.isrg_root_x2_y)
    return try dial_and_handshake(host, port, &policy, "http/1.1", secrets)

## STARTTLS: run the client handshake on a connection already used for
## plaintext. Discard any plaintext the caller buffered after the STARTTLS
## reply before calling, as RFC 3207 and RFC 2595 require.
pub func upgrade_client(conn: net.Connection*, host: str, policy: const Trust*, secrets: Secrets*) -> !:
    try client.connect(secrets, conn, conn, host, policy)

## Server handshake for private endpoints: certificate `cert` with P-256 private
## key `d`. `host`, `qx` and `qy` are accepted for compatibility and unused.
pub func accept(conn: net.Connection*, host: str, cert: const u8[], d: const u8[], qx: const u8[], qy: const u8[], secrets: Secrets*) -> !:
    _ = host
    _ = qx
    _ = qy
    try server.accept(secrets, conn, conn, cert, d)

## Send application data, fragmenting into records of at most 2^14 bytes.
pub func send(conn: net.Connection*, secrets: Secrets*, data: const u8[]) -> !:
    try secrets.write_application(conn, data)

## Receive application data into `out`, returning the count; zero once the peer
## has sent close_notify. Blocks until at least one byte or the close arrives.
pub func receive(conn: net.Connection*, secrets: Secrets*, out: u8[]) -> usize!:
    return try secrets.read_application(conn, out)

## Decrypted bytes buffered in `secrets` that `receive` returns without reading
## the socket; check before waiting for socket readiness.
pub func pending(secrets: const Secrets*) -> usize:
    return secrets.pending()