dymokomi/luce-tls
Source of release 0.7.1
0.7.1 / tests / x509_parse / main.lucb
## Real-world test: the OpenRouter leaf certificate, parsed and checked
## against the Google WE1 intermediate key (ECDSA-P256 over SHA-256), and a
## minted self-signed certificate.
from x509 import mint_self_signed
from luce_crypto import native as crypto_native
import certificate
import chain
import signature
let leaf_der = b"\x30\x82\x03\xa1\x30\x82\x03\x48\xa0\x03\x02\x01\x02\x02\x10\x3d\x1d\xe2\x1e\xfc\x44\x08\xcc\x0e\xa7\x7a\x56\xc9\xd5\x06\x40\x30\x0a\x06\x08\x2a\x86\x48\xce\x3d\x04\x03\x02\x30\x3b\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x55\x53\x31\x1e\x30\x1c\x06\x03\x55\x04\x0a\x13\x15\x47\x6f\x6f\x67\x6c\x65\x20\x54\x72\x75\x73\x74\x20\x53\x65\x72\x76\x69\x63\x65\x73\x31\x0c\x30\x0a\x06\x03\x55\x04\x03\x13\x03\x57\x45\x31\x30\x1e\x17\x0d\x32\x36\x30\x39\x30\x33\x31\x39\x31\x34\x31\x30\x5a\x17\x0d\x32\x36\x31\x32\x30\x32\x32\x30\x31\x34\x30\x37\x5a\x30\x18\x31\x16\x30\x14\x06\x03\x55\x04\x03\x13\x0d\x6f\x70\x65\x6e\x72\x6f\x75\x74\x65\x72\x2e\x61\x69\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42\x00\x04\x21\x54\x63\x28\x23\x11\xb5\x1b\x0a\xf9\xa2\x3a\x92\x08\x54\x22\xc8\x1e\x64\xec\x5e\x77\xd7\x6c\xe8\xbb\xfe\xa2\xc1\x3f\x30\x8a\xae\x23\x63\x6b\x49\x65\xd2\x5d\x68\x72\xb6\xb6\x5b\xf9\x43\xb7\x41\xd2\x59\x1d\xb3\x46\xab\x13\xe2\x9f\x93\x41\x3f\x8c\x6a\x4e\xa3\x82\x02\x4f\x30\x82\x02\x4b\x30\x0e\x06\x03\x55\x1d\x0f\x01\x01\xff\x04\x04\x03\x02\x07\x80\x30\x13\x06\x03\x55\x1d\x25\x04\x0c\x30\x0a\x06\x08\x2b\x06\x01\x05\x05\x07\x03\x01\x30\x0c\x06\x03\x55\x1d\x13\x01\x01\xff\x04\x02\x30\x00\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16\x04\x14\xde\x01\x09\xe1\x94\x38\x4a\xaf\xb2\xe9\xaf\xd0\x92\xb8\x6e\x6a\xb2\x43\xbe\x23\x30\x1f\x06\x03\x55\x1d\x23\x04\x18\x30\x16\x80\x14\x90\x77\x92\x35\x67\xc4\xff\xa8\xcc\xa9\xe6\x7b\xd9\x80\x79\x7b\xcc\x93\xf9\x38\x30\x35\x06\x08\x2b\x06\x01\x05\x05\x07\x01\x01\x04\x29\x30\x27\x30\x25\x06\x08\x2b\x06\x01\x05\x05\x07\x30\x02\x86\x19\x68\x74\x74\x70\x3a\x2f\x2f\x69\x2e\x70\x6b\x69\x2e\x67\x6f\x6f\x67\x2f\x77\x65\x31\x2e\x63\x72\x74\x30\x4c\x06\x03\x55\x1d\x11\x04\x45\x30\x43\x82\x0d\x6f\x70\x65\x6e\x72\x6f\x75\x74\x65\x72\x2e\x61\x69\x82\x17\x73\x74\x6f\x72\x79\x62\x6f\x6f\x6b\x2e\x6f\x70\x65\x6e\x72\x6f\x75\x74\x65\x72\x2e\x61\x69\x82\x19\x2a\x2e\x73\x74\x6f\x72\x79\x62\x6f\x6f\x6b\x2e\x6f\x70\x65\x6e\x72\x6f\x75\x74\x65\x72\x2e\x61\x69\x30\x13\x06\x03\x55\x1d\x20\x04\x0c\x30\x0a\x30\x08\x06\x06\x67\x81\x0c\x01\x02\x01\x30\x36\x06\x03\x55\x1d\x1f\x04\x2f\x30\x2d\x30\x2b\xa0\x29\xa0\x27\x86\x25\x68\x74\x74\x70\x3a\x2f\x2f\x63\x2e\x70\x6b\x69\x2e\x67\x6f\x6f\x67\x2f\x77\x65\x31\x2f\x31\x48\x43\x4e\x55\x4b\x47\x30\x50\x57\x41\x2e\x63\x72\x6c\x30\x82\x01\x02\x06\x0a\x2b\x06\x01\x04\x01\xd6\x79\x02\x04\x02\x04\x81\xf3\x04\x81\xf0\x00\xee\x00\x75\x00\x74\xdb\x9d\x58\xf7\xd4\x7e\x9d\xfd\x78\x7a\x16\x2a\x99\x1c\x18\xcf\x69\x8d\xa7\xc7\x29\x91\x8c\x9a\x18\xb0\x45\x0d\xba\x44\xbc\x00\x00\x01\xa0\x68\xe8\x53\xbf\x00\x00\x04\x03\x00\x46\x30\x44\x02\x20\x75\x5c\xd2\x8e\x6f\x1c\xc2\xe2\x65\x4f\xc2\xec\x5b\x1d\x40\xc6\xbd\x05\xd7\xd7\xb3\x2d\x75\x66\xc8\x0a\xe0\x55\x51\x4f\x2f\x8b\x02\x20\x1a\xa9\x89\x29\x9d\xf3\xb9\x03\x42\x71\x6a\x3f\x2c\x55\xc8\xe0\x71\x6f\xeb\x2b\x25\xa3\x84\x07\x7e\x76\x9e\x27\x06\xd2\x1f\xe8\x00\x75\x00\xd7\x6d\x7d\x10\xd1\xa7\xf5\x77\xc2\xc7\xe9\x5f\xd7\x00\xbf\xf9\x82\xc9\x33\x5a\x65\xe1\xd0\xb3\x01\x73\x17\xc0\xc8\xc5\x69\x77\x00\x00\x01\xa0\x68\xe8\x53\x94\x00\x00\x04\x03\x00\x46\x30\x44\x02\x20\x56\x8c\xed\x03\x72\x07\x58\xfb\x62\x94\xe1\xb1\x37\xc5\x1a\xe7\xee\xb5\xa5\x05\xe2\xb2\x07\xfc\xb6\xeb\x85\x39\x10\x76\xca\x5d\x02\x20\x57\xa0\x71\xcb\x34\x83\xd8\x13\xb0\x91\xc9\x50\x28\x9f\x12\x27\xd4\x1a\x69\x5a\x71\xd2\x47\x4c\x6c\x44\x77\xba\x5e\xa1\x27\x7c\x30\x0a\x06\x08\x2a\x86\x48\xce\x3d\x04\x03\x02\x03\x47\x00\x30\x44\x02\x20\x3e\x2c\x9e\x93\x4f\x10\x81\x28\x0f\x4d\x44\xda\x2e\x61\xa6\x65\x79\xa1\xb6\x26\xb8\x8b\x44\xff\xc1\x1f\xb0\x7b\x44\x66\xde\x0f\x02\x20\x7c\xe3\x48\xe6\x33\xad\xc5\x80\x97\x3d\x06\x85\x2c\xe5\xed\x72\x0a\xbd\x89\xd2\x7b\x61\x31\x49\xff\x47\x35\x5b\x0a\x0d\x62\xf4"
let we1_x = b"\x6f\xcd\x3a\xfe\x67\x57\x47\x4c\x21\x03\x85\x40\xc2\x47\x5d\xbb\x58\x47\x0f\x40\xc1\x5c\x17\x85\xc6\x19\x37\xe7\xd5\x7c\xed\x86"
let we1_y = b"\x4b\x9b\x81\xd9\xd7\x1a\x13\xa5\x0a\x03\xf8\x98\xc4\xc6\xe8\x9e\xff\x10\x59\x8f\x2c\x26\x98\xf5\xe6\x26\x25\xbb\x0f\x02\xfa\x56"
func test_openrouter() -> !:
let cert = try certificate.parse(leaf_der)
assert(chain.matches_dns_name(leaf_der, &cert, "openrouter.ai"))
assert(chain.matches_dns_name(leaf_der, &cert, "x.storybook.openrouter.ai"))
assert(not chain.matches_dns_name(leaf_der, &cert, "example.com"))
let issuer = try signature.PublicKey.p256(we1_x, we1_y)
assert(try cert.signed_by(leaf_der, &issuer))
# A wrong issuer key must reject the signature.
let wrong = try signature.PublicKey.p256(we1_y, we1_x)
assert(not (try cert.signed_by(leaf_der, &wrong)))
func test_self_signed() -> !:
var d: u8[32]
var qx: u8[32]
var qy: u8[32]
try crypto_native.ecdsa_p256_keygen(d, qx, qy)
var der: u8[2048]
let n = try mint_self_signed("localhost", d, qx, qy, der)
let cert = try certificate.parse(der[..<n])
assert(chain.matches_dns_name(der[..<n], &cert, "localhost"))
let key = try signature.PublicKey.p256(qx, qy)
assert(try cert.signed_by(der[..<n], &key))
pub func main(arguments: str[]) -> i32!:
_ = arguments
try test_openrouter()
try test_self_signed()
print("PASS X.509 parsing of a public P-256 leaf and minted self-signed certificates")
return 0