dymokomi/luce-tls
Source of release 0.7.0
## Public keys taken from certificates and trust anchors, and every signature
## this client checks with them: X.509 certificate signatures (ECDSA, RSA
## PKCS#1 v1.5, RSA-PSS) and TLS 1.3 CertificateVerify schemes (RFC 8446
## §4.2.3). Verification only.
from luce_crypto import native as crypto
pub let invalid = crypto.invalid
## Public-key kinds.
pub let key_none: u8 = 0
pub let key_p256: u8 = 1
pub let key_p384: u8 = 2
pub let key_rsa: u8 = 3
## Signature algorithms: a hash with ECDSA, RSA PKCS#1 v1.5 or RSA-PSS (MGF1
## with the same hash, salt as long as the hash). SHA-224 is accepted in
## certificates; SHA-1 and MD5 signatures are not.
pub let ecdsa_sha256: u8 = 1
pub let ecdsa_sha384: u8 = 2
pub let ecdsa_sha512: u8 = 3
pub let rsa_pkcs1_sha256: u8 = 4
pub let rsa_pkcs1_sha384: u8 = 5
pub let rsa_pkcs1_sha512: u8 = 6
pub let rsa_pss_sha256: u8 = 7
pub let rsa_pss_sha384: u8 = 8
pub let rsa_pss_sha512: u8 = 9
pub let ecdsa_sha224: u8 = 10
pub let rsa_pkcs1_sha224: u8 = 11
## TLS signature schemes.
pub let ecdsa_secp256r1_sha256: u16 = 0x0403
pub let ecdsa_secp384r1_sha384: u16 = 0x0503
pub let rsa_pss_rsae_sha256: u16 = 0x0804
pub let rsa_pss_rsae_sha384: u16 = 0x0805
pub let rsa_pss_rsae_sha512: u16 = 0x0806
## The signature_algorithms the client offers, as wire bytes: what it accepts
## in CertificateVerify, then RSA PKCS#1 v1.5, which RFC 8446 allows only in
## certificates.
pub let offered_schemes = b"\x04\x03\x05\x03\x08\x04\x08\x05\x08\x06\x04\x01\x05\x01\x06\x01"
## A subject public key. EC coordinates are big-endian and use the first 32
## (P-256) or 48 (P-384) bytes; an RSA modulus and exponent are big-endian
## without leading zeros.
pub struct PublicKey:
pub var kind: u8
var x: u8[48]
var y: u8[48]
var modulus: u8[1024]
var modulus_size: usize
var exponent: u8[8]
var exponent_size: usize
## A P-256 key from 32-byte coordinates.
pub static func p256(x: const u8[], y: const u8[]) -> PublicKey!:
if x.length != 32 or y.length != 32:
error(invalid, "P-256 coordinates must be 32 bytes")
var key = PublicKey(kind = key_p256)
for i in 0..<32:
key.x[i] = x[i]
key.y[i] = y[i]
return key
## A P-384 key from 48-byte coordinates.
pub static func p384(x: const u8[], y: const u8[]) -> PublicKey!:
if x.length != 48 or y.length != 48:
error(invalid, "P-384 coordinates must be 48 bytes")
var key = PublicKey(kind = key_p384)
for i in 0..<48:
key.x[i] = x[i]
key.y[i] = y[i]
return key
## An RSA key from big-endian modulus and exponent bytes (2048–8192 bits).
pub static func rsa(modulus: const u8[], exponent: const u8[]) -> PublicKey!:
var key = PublicKey(kind = key_rsa)
var start: usize = 0
while start < modulus.length and modulus[start] == 0:
start += 1
if modulus.length - start < 256 or modulus.length - start > 1024:
error(invalid, "RSA modulus must be 2048 to 8192 bits")
for i in start..<modulus.length:
key.modulus[i - start] = modulus[i]
key.modulus_size = modulus.length - start
start = 0
while start < exponent.length and exponent[start] == 0:
start += 1
if exponent.length - start == 0 or exponent.length - start > 8:
error(invalid, "RSA exponent must be 1 to 8 bytes")
for i in start..<exponent.length:
key.exponent[i - start] = exponent[i]
key.exponent_size = exponent.length - start
return key
## The P-256 coordinates; fails for any other kind.
pub func p256_point(x: u8[], y: u8[]) -> !:
if self.kind != key_p256:
error(invalid, "not a P-256 key")
for i in 0..<32:
x[i] = self.x[i]
y[i] = self.y[i]
## Whether two keys are the same.
pub func equals(other: const PublicKey*) -> bool:
if self.kind != other.kind or self.modulus_size != other.modulus_size or self.exponent_size != other.exponent_size:
return false
let point = crypto.equal(self.x, other.x) and crypto.equal(self.y, other.y)
let modulus = crypto.equal(self.modulus[..<self.modulus_size], other.modulus[..<other.modulus_size])
return point and modulus and crypto.equal(self.exponent[..<self.exponent_size], other.exponent[..<other.exponent_size])
func is_ecdsa(algorithm: u8) -> bool:
return algorithm == ecdsa_sha224 or algorithm == ecdsa_sha256 or algorithm == ecdsa_sha384 or algorithm == ecdsa_sha512
func is_pkcs1(algorithm: u8) -> bool:
return algorithm == rsa_pkcs1_sha224 or (algorithm >= rsa_pkcs1_sha256 and algorithm <= rsa_pkcs1_sha512)
func hash_name(algorithm: u8) -> str:
if algorithm == ecdsa_sha224 or algorithm == rsa_pkcs1_sha224:
return "sha224"
if algorithm == ecdsa_sha256 or algorithm == rsa_pkcs1_sha256 or algorithm == rsa_pss_sha256:
return "sha256"
if algorithm == ecdsa_sha384 or algorithm == rsa_pkcs1_sha384 or algorithm == rsa_pss_sha384:
return "sha384"
return "sha512"
## Verify `signature` over `signed` with `key` under `algorithm`. False for a
## wrong signature or a key that cannot use the algorithm.
pub func verify(key: const PublicKey*, algorithm: u8, signed: const u8[], signature: const u8[]) -> bool!:
if algorithm < ecdsa_sha256 or algorithm > rsa_pkcs1_sha224:
return false
var digest: u8[64]
let size = try crypto.digest(signed, digest, hash_name(algorithm))
if is_ecdsa(algorithm):
if key.kind == key_p256:
var r: u8[32]
var s: u8[32]
if not parse_ecdsa(signature, r, s):
return false
return try crypto.ecdsa_p256_verify(key.x[..<32], key.y[..<32], digest[..<size], r, s)
if key.kind == key_p384:
var r: u8[48]
var s: u8[48]
if not parse_ecdsa(signature, r, s):
return false
return try crypto.ecdsa_p384_verify(key.x, key.y, digest[..<size], r, s)
return false
if key.kind != key_rsa:
return false
let modulus = key.modulus[..<key.modulus_size]
let exponent = key.exponent[..<key.exponent_size]
if is_pkcs1(algorithm):
return crypto.rsa_verify_pkcs1(modulus, exponent, hash_name(algorithm), digest[..<size], signature) catch: recover false
return crypto.rsa_verify_pss(modulus, exponent, hash_name(algorithm), digest[..<size], signature, size) catch: recover false
## Verify a TLS 1.3 CertificateVerify signature `scheme` over `content`. The
## scheme must suit the key; RSA PKCS#1 v1.5 is not allowed here.
pub func verify_scheme(key: const PublicKey*, scheme: u16, content: const u8[], signature: const u8[]) -> !:
var algorithm: u8 = 0
if scheme == ecdsa_secp256r1_sha256 and key.kind == key_p256:
algorithm = ecdsa_sha256
elif scheme == ecdsa_secp384r1_sha384 and key.kind == key_p384:
algorithm = ecdsa_sha384
elif scheme == rsa_pss_rsae_sha256 and key.kind == key_rsa:
algorithm = rsa_pss_sha256
elif scheme == rsa_pss_rsae_sha384 and key.kind == key_rsa:
algorithm = rsa_pss_sha384
elif scheme == rsa_pss_rsae_sha512 and key.kind == key_rsa:
algorithm = rsa_pss_sha512
else:
error(invalid, "the server used a signature scheme that does not match its key")
if not (try verify(key, algorithm, content, signature)):
error(invalid, "TLS CertificateVerify signature is invalid")
## Decode a DER Ecdsa-Sig-Value into fixed-width big-endian r and s. False
## when it is malformed or an integer is wider than the curve.
pub func parse_ecdsa(der: const u8[], r: u8[], s: u8[]) -> bool:
if der.length < 8 or der[0] != 0x30:
return false
var at: usize = 2
var length = (usize)der[1]
if der[1] == 0x81:
if der[2] < 0x80:
return false
length = (usize)der[2]
at = 3
elif der[1] >= 0x80:
return false
if length != der.length - at:
return false
let after_r = parse_integer(der, at, r) else return false
let after_s = parse_integer(der, after_r, s) else return false
return after_s == der.length
func parse_integer(der: const u8[], at: usize, out: u8[]) -> usize?:
if der.length - at < 3 or der[at] != 0x02 or der[at + 1] >= 0x80:
return none
let length = (usize)der[at + 1]
let start = at + 2
if length == 0 or length > der.length - start or (der[start] & 0x80) != 0:
return none
if length > 1 and der[start] == 0 and der[start + 1] < 0x80:
return none
var first = start
var count = length
if count > 1 and der[first] == 0:
first += 1
count -= 1
if count > out.length:
return none
for i in 0..<out.length:
out[i] = 0
for i in 0..<count:
out[out.length - count + i] = der[first + i]
return start + length