dymokomi/luce-tls
Source of release 0.7.1
0.7.1 / tests / x509_suite / main.lucb
## Ported X.509 path-validation suites: BearSSL's test/x509/alltests.txt
## (tests/x509/bearssl, converted by tools/bearssl_x509.py) and OpenSSL's
## test/recipes/25-test_verify.t (tests/x509/openssl, tools/openssl_verify.py).
## Each case lists trust anchors, the certificates in TLS order, a host name
## (or none) and a time; the expected verdict is the reference's except for
## the documented policy differences in each suite's POLICY.txt.
import anchors
import chain
import der
import signature
import vector_file as vectors
## A TLS 1.3 Certificate message with the listed files in order.
func certificate_message(directory: str, names: const u8[], out: u8[]) -> usize!:
out[0] = 11
out[4] = 0
var at: usize = 8
var i: usize = 0
while vectors.item(names, i).length != 0:
var path: u8[1024]
let file = try vectors.read(vectors.join(directory, vectors.item(names, i), path))
defer file.release()
let cert = file.value
out[at] = (u8)(cert.length >> 16)
out[at + 1] = (u8)((cert.length >> 8) & 0xff)
out[at + 2] = (u8)(cert.length & 0xff)
for j in 0..<cert.length: out[at + 3 + j] = cert[j]
at += 3 + cert.length
out[at] = 0
out[at + 1] = 0
at += 2
i += 1
let body = at - 4
out[1] = (u8)(body >> 16)
out[2] = (u8)((body >> 8) & 0xff)
out[3] = (u8)(body & 0xff)
let list = at - 8
out[5] = (u8)(list >> 16)
out[6] = (u8)((list >> 8) & 0xff)
out[7] = (u8)(list & 0xff)
return at
## Whether the chain validates; an empty host checks no name.
func accepted(message: const u8[], host: const u8[], now: i64, trusted: const anchors.Anchors*) -> bool:
if host.length == 0:
_ = chain.verify_chain_without_name(message, now, trusted) catch: return false
else:
_ = chain.verify_server_chain(message, (str)host, now, trusted) catch: return false
return true
struct Counts:
var accepted: usize
var rejected: usize
var skipped: usize
## Validate the case on the current line of `cases` (fields: case NAME
## EXPECT TIME HOST ANCHORS CHAIN) and fail when the verdict differs.
func check(cases: const vectors.Lines*, directory: str, trusted: const anchors.Anchors*) -> !:
var message: u8[65536]
let size = try certificate_message(directory, try cases.field(6), message)
let verdict = accepted(message[..<size], try cases.field(4), vectors.decimal(try cases.field(3)), trusted)
if verdict != try cases.is(2, b"accept"):
print(f"X.509 case {(str)try cases.field(1)}: expected {(str)try cases.field(2)}")
error(chain.invalid, "X.509 case disagrees with the reference")
if verdict:
self.accepted += 1
else:
self.rejected += 1
## Add the BearSSL anchor on the current line (fields: anchor NAME KIND
## SUBJECT KEY_A KEY_B). An "ee" anchor (direct trust) or a P-521 key has no
## equivalent here and only appears in skipped cases.
func bearssl_anchor(line: const vectors.Lines*, trusted: anchors.Anchors*) -> !:
if try line.is(2, b"ee") or try line.is(2, b"p521"):
return
var subject: u8[512]
var a: u8[600]
var b: u8[100]
let s = try line.decode(3, subject)
let a_size = try line.decode(4, a)
let b_size = try line.decode(5, b)
var key = signature.PublicKey()
if try line.is(2, b"rsa"):
key = try signature.PublicKey.rsa(a[..<a_size], b[..<b_size])
elif try line.is(2, b"p256"):
key = try signature.PublicKey.p256(a[..<a_size], b[..<b_size])
else:
key = try signature.PublicKey.p384(a[..<a_size], b[..<b_size])
try trusted.add(subject[..<s], key)
func bearssl(directory: str, counts: Counts*) -> !:
var cases = try vectors.Lines.open(directory, b"cases.txt")
defer cases.close()
var anchor_lines = try vectors.Lines.open(directory, b"cases.txt")
defer anchor_lines.close()
while cases.next():
if not try cases.is(0, b"case"):
continue
if try cases.is(2, b"skip"):
counts.skipped += 1
continue
var trusted = anchors.Anchors.named_list()
let wanted = try cases.field(5)
anchor_lines.rewind()
while anchor_lines.next():
if not try anchor_lines.is(0, b"anchor"):
continue
var i: usize = 0
while vectors.item(wanted, i).length != 0:
if vectors.item(wanted, i) == (try anchor_lines.field(1)):
try bearssl_anchor(&anchor_lines, &trusted)
i += 1
try counts.check(&cases, directory, &trusted)
## OpenSSL cases: trusted certificates contribute their Name and key as
## anchors; anchors that do not parse (unsupported keys) are left out.
func openssl(directory: str, counts: Counts*) -> !:
var cases = try vectors.Lines.open(directory, b"cases.txt")
defer cases.close()
while cases.next():
var trusted = anchors.Anchors.named_list()
let wanted = try cases.field(5)
var i: usize = 0
while vectors.item(wanted, i).length != 0:
var path: u8[1024]
let cert = try vectors.read(vectors.join(directory, vectors.item(wanted, i), path))
trusted.add_certificate(cert.value) catch failure:
_ = failure
recover ()
cert.release()
i += 1
try counts.check(&cases, directory, &trusted)
## Optional argument: the tests/x509 directory.
pub func main(arguments: str[]) -> i32!:
let directory = arguments[1] if arguments.length > 1 else "tests/x509"
var bear = Counts()
var path: u8[1024]
try bearssl(vectors.join(directory, b"bearssl", path), &bear)
var ossl = Counts()
var openssl_path: u8[1024]
try openssl(vectors.join(directory, b"openssl", openssl_path), &ossl)
print(f"PASS X.509 suites: BearSSL {bear.accepted} accepted, {bear.rejected} rejected (+{bear.skipped} not expressible); OpenSSL {ossl.accepted} accepted, {ossl.rejected} rejected")
return 0