dymokomi/luce-tls
Source of release 0.7.0
## What a client trusts, and how it authenticates a server's TLS Certificate
## message under that trust to obtain the leaf public key.
from luce_crypto import native as crypto
import anchors
import chain
import signature
pub let invalid = crypto.invalid
let kind_public: u8 = 0
let kind_issuer: u8 = 1
let kind_p384_root: u8 = 2
## A trust policy. The zero value is the public-roots policy.
pub struct Trust:
var kind: u8
var x: u8[48]
var y: u8[48]
## Public servers: a path to one of the bundled Mozilla roots.
pub static func public_roots() -> Trust:
return Trust(kind = kind_public)
## A private server whose chain ends at this P-256 key: typically a leaf
## signed by it or self-signed with it, as `x509.mint_self_signed` makes.
pub static func pinned_issuer(x: const u8[], y: const u8[]) -> Trust!:
if x.length != 32 or y.length != 32:
error(invalid, "pinned P-256 issuer coordinates must be 32 bytes")
var trust = Trust(kind = kind_issuer)
for i in 0..<32:
trust.x[i] = x[i]
trust.y[i] = y[i]
return trust
## A chain that ends at this explicit P-384 root key.
pub static func p384_root(x: const u8[], y: const u8[]) -> Trust!:
if x.length != 48 or y.length != 48:
error(invalid, "P-384 root coordinates must be 48 bytes")
var trust = Trust(kind = kind_p384_root)
for i in 0..<48:
trust.x[i] = x[i]
trust.y[i] = y[i]
return trust
## Authenticate a complete Certificate handshake message for `host` at Unix
## time `now` and return the leaf key that must sign CertificateVerify.
pub func authenticate(trust: const Trust*, message: const u8[], host: str, now: i64) -> signature.PublicKey!:
var trusted = anchors.Anchors.mozilla()
if trust.kind == kind_issuer:
trusted = anchors.Anchors.single(try signature.PublicKey.p256(trust.x[..<32], trust.y[..<32]))
elif trust.kind == kind_p384_root:
trusted = anchors.Anchors.single(try signature.PublicKey.p384(trust.x, trust.y))
return try chain.verify_server_chain(message, host, now, &trusted)