diff --git a/.vscode/launch.json b/.vscode/launch.json index def447a..f80181c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,16 +7,17 @@ { "type": "lldb", "request": "launch", - "name": "Debug executable 'ct'", + "name": "Debug unit tests in library 'ct'", "cargo": { "args": [ - "build", - "--bin=ct", + "test", + "--no-run", + "--lib", "--package=ct" ], "filter": { "name": "ct", - "kind": "bin" + "kind": "lib" } }, "args": [], @@ -25,17 +26,17 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'ct'", + "name": "Debug integration test 'log_entry_parsing'", "cargo": { "args": [ "test", "--no-run", - "--bin=ct", + "--test=log_entry_parsing", "--package=ct" ], "filter": { - "name": "ct", - "kind": "bin" + "name": "log_entry_parsing", + "kind": "test" } }, "args": [], diff --git a/Cargo.lock b/Cargo.lock index 6b5bead..f750430 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,7 @@ dependencies = [ "serde", "serde_json", "sha2", + "tokio", "x509-parser", ] @@ -1199,9 +1200,21 @@ dependencies = [ "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.52.0", ] +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 3b09e17..99f848f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,6 @@ x509-parser = "0.16.0" [dev-dependencies] base64ct = "1.6.0" +reqwest = { version = "0.12.8", features = ["json"] } +serde = { version = "1.0.210", features = ["derive"] } +tokio = { version = "1.41.0", features = ["rt-multi-thread", "macros"] } diff --git a/src/lib.rs b/src/lib.rs index 308c392..2723638 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ //! Parsing logic for CT structures is defined in the [`parsing`] module. This //! primarily uses the [`nom`] crate for parsing, doing zero-allocation parsing //! followed by parsing it into a struct. -//! +//! //! The [`merkle`] module contains the rest of the merkle tree logic, such as //! the specific data types and hashing logic //! diff --git a/src/merkle/consts.rs b/src/merkle/consts.rs index 4812c9d..a6f71e9 100644 --- a/src/merkle/consts.rs +++ b/src/merkle/consts.rs @@ -1,74 +1,5 @@ pub const LEAF_BASE64_BUFFER_SIZE: usize = 3072; +pub const EXTRA_DATA_BASE64_BUFFER_SIZE: usize = 10240; pub const LEAF_HASH_PREFIX: u8 = 0x00; pub const INTERIOR_HASH_PREFIX: u8 = 0x01; - -#[cfg(test)] -pub(crate) mod test_constants { - // TODO Write a script to populate this with random entries - pub(crate) const LEAF_INPUT_EXAMPLES: &[(&str, &str, u64)] = &[ - ( - "AAAAAAGSnKkrxAAAAAOJMIIDhTCCAwygAwIBAgISBIMWztGBRQE+UJezy64M233lMAoGCCqGSM49BAMDMDIxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQDEwJFNjAeFw0yNDEwMTcyMTQ3NTRaFw0yNTAxMTUyMTQ3NTNaMB0xGzAZBgNVBAMTEnRlc3QubXlyaWF0aW9uLnh5ejBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABCcExgJVV0VmpNBZLxu0fs4Gd/HY5a00y9gopHs8bHe7in2X+TqP+20WsT11e1RvddEXX7L+k2U9/jBiODsz7x6jggIVMIICETAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFBGQZpFKlhRGYfUFTZCnc1mk50owMB8GA1UdIwQYMBaAFJMnRpgDqVFojpjWxEJI2yO/WJTSMFUGCCsGAQUFBwEBBEkwRzAhBggrBgEFBQcwAYYVaHR0cDovL2U2Lm8ubGVuY3Iub3JnMCIGCCsGAQUFBzAChhZodHRwOi8vZTYuaS5sZW5jci5v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je8frPOA7fTYqLFzeeDAUfaNXmizQoDB25ifJIhvRqX+gl6Ff+i+uXIeHc097UbRK2bPKIf8XtqD94n48OnfmJBvcGnTl5PaI4Bec3J4tpZ/w6FhZ17FIvP3hxaERmFt89SyYKzCLC2bhnVByHi2f8/jYJVv2nMycjoDxMTJUFRDCn+3BAcAyYbibc+BQLNlGMS04YDZerEJ4BmWTDd1nQZgAA", - "C=GB, L=London, O=Google Certificate Transparency, serialNumber=1692183670230477", - 1692183670314 - ), - ( - "AAAAAAGKCs8mKgAAAAU2MIIFMjCCAxqgAwIBAgIJAPXCmTa0J/XPMA0GCSqGSIb3DQEBCwUAMH8xCzAJBgNVBAYTAkdCMQ8wDQYDVQQIDAZMb25kb24xFzAVBgNVBAoMDkdvb2dsZSBVSyBMdGQuMSEwHwYDVQQLDBhDZXJ0aWZpY2F0ZSBUcmFuc3BhcmVuY3kxIzAhBgNVBAMMGk1lcmdlIERlbGF5IEludGVybWVkaWF0ZSAxMB4XDTI1MDgxNzA5NDIyMloXDTI1MDgxODA5NDIyMlowdTELMAkGA1UEBhMCR0IxDzANBgNVBAcTBkxvbmRvbjEPMA0GA1UEChMGR29vZ2xlMSEwHwYDVQQLExhDZXJ0aWZpY2F0ZSBUcmFuc3BhcmVuY3kxITAfBgNVBAMTGGZsb3dlcnMtdG8tdGhlLXdvcmxkLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJtHYIEazPHQhDE0LDSZlFexMoMoRmAZ1711p5HW1LjQcEFpULoTa52o+LsqEC7plZd7V77TaTfYG51NRE9cOwl/ndxtItJTECnp09NL1LOvXHWckeR+EAUfA2uewCmdgJ+mjndVwIH98jqapH4+ANgiGQEf5TsYvQAQQUYUmXX2zkoTJlQ2ZRMr1pwxAXMQJrEsOq/IZhirK7TGu5gRikQuvWco3ZX2/G587HnC2uEVd8FkRzZjxVjMk9TZY3p+LVF+K51tArquD8zID8P8BFdOiZl4oEaWveRollsrBOSalaXLiH8m26j1UqhVNKgEuxGBZW2DIOzU/Z1hXr7dZRkCAwEAAaOBujCBtzAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBTpPAThgC/ChBMtJnCe8v0az6r+xjBhBgNVHREEWjBYghhmbG93ZXJzLXRvLXRoZS13b3JsZC5jb22CPDIyLjE4LjA4LjIwMjMubGV0c2VuY3J5cHRfb2FrMjAyNWgyLmZsb3dlcnMtdG8tdGhlLXdvcmxkLmNvbTANBgkqhkiG9w0BAQsFAAOCAgEAgxaaRC6InFBZbrGbhytR6Am5EZv++G0VBTeh5JWJTNyhwF1wD0yyCP0uPMdlWBpQFBXmp/pwGQ72pBE4hboKDMz9wLPpSu1kHh0KUCt8ugjssfk59P9ECGcCxmg2mGIXE0kCFwlJ5xGlIzNiD/hyJGM28zwM6iZ+sITmF+2dzxBa54bVFPDtD3+A5d47ad5XIFAzzMIsJjfrEnFwLU69MffyuhABhIBUzlzU6mPKOPoXpsP9MDCwAEljiyksHKdOcxoPDlchTRvPAn+FngkELiR80HbHsi703UG3KGBSNrDiQCHNCmO/lF6c1kN+/Ij29zu+Wle8Q8veJAanKE8WDGnlU1pFw9nuCaIAtxX8AiMUDuLrAHj38Mj7MIQS4CzloAp4UMqvaupOc+cxJ/8psbUy+MkI6dMww3N5gZE6EV6WCF21Ogm/zqMQwy/Fj1P7dpl+BFYEmJzP0yhiZMh2a9okbpienKL8zaiz/b/27ffO/IxaxT5IwJ60k6Bj9kB9M3H9leFA5EgSpe7buoK86z8LQJ+KHCWErl616TsW2KocycD+dAyNsGk9OlSL8WpKDV5KFmyO1HWDCIpC8WYE/miWH1zwKbmDVJJOEVUBbfNWwy9EbgxHdDvwYChC9/EfIOxCOEj/JQZoEiHjVUy0PGsO59JaxG/citHaQr5Uv48AAA==", - "C=GB, L=London, O=Google, OU=Certificate Transparency, CN=flowers-to-the-world.com", - 1692398462506 - ), - ( - "AAAAAAGKD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7lwIDAQABo4G6MIG3MA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFOk8BOGAL8KEEy0mcJ7y/RrPqv7GMGEGA1UdEQRaMFiCGGZsb3dlcnMtdG8tdGhlLXdvcmxkLmNvbYI8MDkuMDQuMDkuMjAyMy5sZXRzZW5jcnlwdF9vYWsyMDI1aDIuZmxvd2Vycy10by10aGUtd29ybGQuY29tMA0GCSqGSIb3DQEBCwUAA4ICAQB8B5jCukPDWYAwTTL2tVKyMcQAZNU8N5C2pn4hnuvjHIIpXeDuGtDwW8iGQYoi7AN/a8+ImWbjcTgdlxicICc/52gNyLXNKLpPVK5Hwloq8JRkyShezS9izBLX9NzNhGN4DGUPaptIgcMcLPv1hjCQy3ZEbh2lip5j9ZqhUuMPaR8f+K0S6y8NlRjPgwXtJXIQMwuIvSfWIHPgwnf16v9p/eW8vcoLPD2aM5VD2XUZJrA/AT6EjVRH2rvdF0urDODFtm4EP6dDvRpcCAgAmmFUEyrTQIlvC0S6+c4PsIDcdI3TiF+XrAree54jBXGYH2pBqShH/sVfEKSJw17DTvat1tNgyBxIHWl+7AKH23L0W1w4xR35eZVyGsEkGi8EFQT70V7VFuGiUw53W1NF/2GxBet/ywDD5x/R6XyrbNP56B/KGSE24Z7iIyIl3FRKE4LHZ97fH2o8SkLHSskSwiVSQd3/gk9jf6d8fVIyFj2ZDvzFks0ACepvJ2hVbTJHvqVWoRyT5lxJ6Mmb01bhocJsxgcCgvs361lBSa4URS5a7u2jULcsFnx8niHjhMJHjXnhc+Me3UduIYGA/c2gJ+IX8dpPBeL/eYx+3bsCmcVH2w94j3IiC49APA6ZMhuBXh351/X4RcHODDiNLqe0rLPqPbfdl+d3r+pVLuRnb4lKygAA", - "C=GB, L=London, O=Google, OU=Certificate Transparency, CN=flowers-to-the-world.com", - 1693821536835 - ), - // Entries from https://oak.ct.letsencrypt.org/2024h1 - ( - "AAAAA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kgSW50ZXJtZWRpYXRlIDEwHhcNMjIwOTA5MDIzNzM4WhcNMjQwNjEzMjE0OTAwWjBjMQswCQYDVQQGEwJHQjEPMA0GA1UEBwwGTG9uZG9uMSgwJgYDVQQKDB9Hb29nbGUgQ2VydGlmaWNhdGUgVHJhbnNwYXJlbmN5MRkwFwYDVQQFExAxNjYyNjkxMDU4Mjk1NTQ1MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv139MZdfpcq6+OQMmxbsO0a9UkyXjyObBcgJzQ+g8KtUdvQQptLmN++pqfnxmI18IyqtZyekpH/enlbEVIdcJ9O0hSMtTM92CaHudKLpV46SEhJcbzwUd9u1w7EdnKjjUq6gmj9zckOjTg09KJyJ95EKbv0ZpYFhsMOeTyhHidYy2wNCNJhO9nx3YE/cnQ9UVo8R3m9dlcDRgHtdQwMjMjPomKgsmJgEXVPPwjKMhVR25O2u21cqsVxItOgEa8WyeR/0awkm1+OMUdEtv0CDSqc5mwKc63dNAnHjJjVq68LJAPfzbR72lW0CxZETOIKI5Hd/jSrL96Cu57RXsC7YRQIDAQABo4GLMIGIMBMGA1UdJQQMMAoGCCsGAQUFBwMBMCMGA1UdEQQcMBqCGGZsb3dlcnMtdG8tdGhlLXdvcmxkLmNvbTAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFOk8BOGAL8KEEy0mcJ7y/RrPqv7GMB0GA1UdDgQWBBSRIw7tu+hiIeKf0gO+lTZfhwxK6gAA", - "C=GB, L=London, O=Google Certificate Transparency, serialNumber=1662691058295545", - 1662691058345 - ), - // Entries from https://ct.googleapis.com/logs/eu1/xenon2025h1 - ( - "AAAAAAGSl0salwAAAAPKMIIDxjCCA0ygAwIBAgISBPmkPV0s/bLTwy6BR95mo9omMAoGCCqGSM49BAMDMDIxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQDEwJFNTAeFw0yNDEwMTYyMDQ3MDNaFw0yNTAxM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J4wvoGAHCHcGUKeszXXBycjfvlSeh+l4DhXiLpAD6fn4ANfwAA", - "CN=pochtabank.o.avito.ryginmld1awo0ve.eip.ensimpoc.com", - 1729115134615 - ) - ]; -} diff --git a/src/merkle/types.rs b/src/merkle/types.rs index 483a5c1..97e59e0 100644 --- a/src/merkle/types.rs +++ b/src/merkle/types.rs @@ -1,4 +1,4 @@ -use x509_parser::prelude::TbsCertificate; +use x509_parser::prelude::{TbsCertificate, X509Certificate}; #[repr(u8)] #[non_exhaustive] @@ -35,3 +35,15 @@ pub struct MerkleTreeLeaf<'a> { pub version: Version, pub leaf_type: MerkleLeafType<'a> } + +#[derive(Debug)] +pub struct ChainEntry<'a> { + pub main_certificate: X509Certificate<'a>, + pub certificate_chain: Vec> +} + +#[derive(Debug)] +pub enum EntryExtraData<'a> { + X509Certificate(Vec>), + Precertificate(ChainEntry<'a>) +} diff --git a/src/parsing/entry.rs b/src/parsing/entry.rs new file mode 100644 index 0000000..70dd30b --- /dev/null +++ b/src/parsing/entry.rs @@ -0,0 +1,182 @@ +use x509_parser::{error::X509Error, prelude::X509Certificate}; + +use super::{ + structures::{parse_tbs_certificate_der, parse_x509_der}, + LeafParsingEnumType, + LeafParsingError +}; +use crate::merkle::types::{ + ChainEntry, + EntryExtraData, + LogEntryType, + MerkleLeafType, + MerkleTreeLeaf, + Version +}; + +/// Parses a MerkleTreeLeaf structure as specified in [RFC6962](https://datatracker.ietf.org/doc/html/rfc6962): +/// ```txt +/// struct { +/// Version version; +/// MerkleLeafType leaf_type; +/// select (leaf_type) { +/// case timestamped_entry: TimestampedEntry; +/// } +/// } MerkleTreeLeaf; +/// ``` +/// +/// This function assumes a binary format for the leaf, rather than a +/// base64-encoded version, so make sure to manually decode it from the HTTP +/// response before use. +pub fn parse_merkle_tree_leaf( + input: &[u8] +) -> nom::IResult<&[u8], MerkleTreeLeaf, LeafParsingError<&[u8]>> { + let (input, version /* Version version; */) = nom::number::complete::u8(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + let (input, leaf_type /* MerkleLeafType leaf_type; */) = + nom::number::complete::u8(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + + let version = match version { + 0 => Version::V1, + _ => + return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { + input, + enum_type: LeafParsingEnumType::Version + })), + }; + let (input, leaf_type) = match leaf_type { + // struct { + // uint64 timestamp; + // LogEntryType entry_type; /* 2 bytes */ + // select(entry_type) { + // case x509_entry: ASN.1Cert; + // case precert_entry: PreCert; + // } signed_entry; + // CtExtensions extensions; + // } TimestampedEntry; + 0 => { + let (input, timestamp /* uint64 timestamp; */) = + nom::number::complete::be_u64(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + let (input, entry_type /* LogEntryType entry_type; */) = + nom::number::complete::be_u16(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + + let (input, entry_type) = match entry_type { + 0 => { + let (input, der /* opaque ASN.1Cert<1..2^24-1> */) = + parse_x509_der(input) + .map_err(|e| e.map(|e| LeafParsingError::DerParsing(e)))?; + + (input, LogEntryType::X509Entry(der)) + } + // case precert_entry: PreCert; + 1 => { + let ( + input, + issuer_key_hash // opaque issuer_key_hash[32]; + ) = nom::bytes::complete::take(32usize)(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + let ( + input, + tbs_certificate // TBSCertificate tbs_certificate; + ) = parse_tbs_certificate_der(input) + .map_err(|e| e.map(|e| LeafParsingError::DerParsing(e)))?; + + (input, LogEntryType::PrecertEntry { + issuer_key_hash: issuer_key_hash.try_into().map_err(|_| { + nom::Err::Failure(LeafParsingError::InvalidTakeLength) + })?, + tbs_certificate + }) + } + _ => + return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { + input, + enum_type: LeafParsingEnumType::LogEntryType + })), + }; + + let ( + input, + extensions // opaque CtExtensions<0..2^16-1> + ) = nom::multi::length_data(nom::number::complete::be_u16)(input) + .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; + + (input, MerkleLeafType::TimeStampedEntry { + timestamp, + entry_type, + extensions + }) + } + _ => + return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { + input, + enum_type: LeafParsingEnumType::MerkleLeafType + })), + }; + + Ok((input, MerkleTreeLeaf { version, leaf_type })) +} + +/// Constructs a parser for an entry `extra_data`, with prior knowledge of the +/// leaf_input being an x509 entry rather than a precert entry +pub fn parse_entry_extra_data_x509<'a>( + input: &[u8] +) -> nom::IResult<&[u8], EntryExtraData, X509Error> { + nom::combinator::map(parse_certificate_chain, |chain| { + EntryExtraData::X509Certificate(chain) + })(input) +} + +/// Constructs a parser for an entry `extra_data`, with prior knowledge of the +/// leaf_input being a precert entry rather than an x509 entry +pub fn parse_entry_extra_data_precert<'a>( + input: &[u8] +) -> nom::IResult<&[u8], EntryExtraData, X509Error> { + nom::combinator::map(parse_chain_entry, |entry| { + EntryExtraData::Precertificate(entry) + })(input) +} + +/// Parses an `ASN.1Cert [pre]certificate_chain<0..2^24-1>;` from the +/// X509ChainEntry or PrecertChainEntry structs +pub fn parse_certificate_chain( + input: &[u8] +) -> nom::IResult<&[u8], Vec, X509Error> { + dbg!(nom::combinator::map_parser( + nom::multi::length_data( + // Get slice containing the full chain + nom::number::complete::be_u24 // certificate_chain has a length field of u24 + ), + nom::multi::many0( + // Parse a length-data x509 DER as many times as possible + parse_x509_der + ) + )(input)) +} + +/// Pares an X509ChainEntry or PrecertChainEntry as specified in [RFC6962]: +/// ```txt +/// struct { +/// ASN.1Cert leaf_certificate; +/// ASN.1Cert certificate_chain<0..2^24-1>; +/// } X509ChainEntry; +/// +/// struct { +/// ASN.1Cert pre_certificate; +/// ASN.1Cert precertificate_chain<0..2^24-1>; +/// } PrecertChainEntry; +/// ``` +/// +/// [RFC6962]: https://datatracker.ietf.org/doc/html/rfc6962 +pub fn parse_chain_entry(input: &[u8]) -> nom::IResult<&[u8], ChainEntry, X509Error> { + nom::combinator::map( + nom::sequence::pair(parse_x509_der, parse_certificate_chain), + |(main_certificate, certificate_chain)| ChainEntry { + main_certificate, + certificate_chain + } + )(input) +} diff --git a/src/parsing/leaf.rs b/src/parsing/leaf.rs deleted file mode 100644 index 93c5a2c..0000000 --- a/src/parsing/leaf.rs +++ /dev/null @@ -1,115 +0,0 @@ -use super::{ - structures::{parse_tbs_certificate_der, parse_x509_der}, - LeafParsingEnumType, - LeafParsingError -}; -use crate::merkle::types::{LogEntryType, MerkleLeafType, MerkleTreeLeaf, Version}; - -/// Parses a MerkleTreeLeaf structure as specified in [RFC6962](https://datatracker.ietf.org/doc/html/rfc6962): -/// ```txt -/// struct { -/// Version version; -/// MerkleLeafType leaf_type; -/// select (leaf_type) { -/// case timestamped_entry: TimestampedEntry; -/// } -/// } MerkleTreeLeaf; -/// ``` -/// -/// This function assumes a binary format for the leaf, rather than a -/// base64-encoded version, so make sure to manually decode it from the HTTP -/// response before use. -pub fn parse_merkle_tree_leaf( - input: &[u8] -) -> nom::IResult<&[u8], MerkleTreeLeaf, LeafParsingError<&[u8]>> { - let (input, version /* Version version; */) = nom::number::complete::u8(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - let (input, leaf_type /* MerkleLeafType leaf_type; */) = - nom::number::complete::u8(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - - Ok((input, MerkleTreeLeaf { - version: match version { - 0 => Version::V1, - _ => - return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { - input, - enum_type: LeafParsingEnumType::Version - })), - }, - leaf_type: match leaf_type { - // struct { - // uint64 timestamp; - // LogEntryType entry_type; /* 2 bytes */ - // select(entry_type) { - // case x509_entry: ASN.1Cert; - // case precert_entry: PreCert; - // } signed_entry; - // CtExtensions extensions; - // } TimestampedEntry; - 0 => { - let (input, timestamp /* uint64 timestamp; */) = - nom::number::complete::u64(nom::number::Endianness::Big)(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - let (input, entry_type /* LogEntryType entry_type; */) = - nom::number::complete::u16(nom::number::Endianness::Big)(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - - let (input, entry_type) = match entry_type { - 0 => { - let (input, der /* opaque ASN.1Cert<1..2^24-1> */) = - parse_x509_der(input).map_err(|e| { - e.map(|e| LeafParsingError::DerParsing(e)) - })?; - - (input, LogEntryType::X509Entry(der)) - } - // case precert_entry: PreCert; - 1 => { - let ( - input, - issuer_key_hash // opaque issuer_key_hash[32]; - ) = nom::bytes::complete::take(32usize)(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - let ( - input, - tbs_certificate // TBSCertificate tbs_certificate; - ) = parse_tbs_certificate_der(input) - .map_err(|e| e.map(|e| LeafParsingError::DerParsing(e)))?; - - (input, LogEntryType::PrecertEntry { - issuer_key_hash: issuer_key_hash.try_into().map_err( - |_| { - nom::Err::Failure(LeafParsingError::InvalidTakeLength) - } - )?, - tbs_certificate - }) - } - _ => - return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { - input, - enum_type: LeafParsingEnumType::LogEntryType - })), - }; - - let ( - _, // no more to parse - extensions // opaque CtExtensions<0..2^16-1> - ) = nom::multi::length_data(nom::number::complete::be_u16)(input) - .map_err(|e| e.map(|e| LeafParsingError::Nom(e)))?; - - MerkleLeafType::TimeStampedEntry { - timestamp, - entry_type, - extensions - } - } - _ => - return Err(nom::Err::Failure(LeafParsingError::InvalidEnum { - input, - enum_type: LeafParsingEnumType::MerkleLeafType - })), - } - })) -} diff --git a/src/parsing/mod.rs b/src/parsing/mod.rs index 1238f9a..30cc98e 100644 --- a/src/parsing/mod.rs +++ b/src/parsing/mod.rs @@ -1,4 +1,4 @@ -pub mod leaf; +pub mod entry; pub mod structures; #[derive(Debug)] @@ -18,68 +18,3 @@ pub enum LeafParsingEnumType { MerkleLeafType, LogEntryType } - -#[cfg(test)] -mod test { - use base64ct::Encoding; - - use crate::{ - merkle::{ - consts::{test_constants, LEAF_BASE64_BUFFER_SIZE}, - types::{LogEntryType, MerkleLeafType} - }, - parsing::leaf::parse_merkle_tree_leaf - }; - - #[test] - fn parse_leaf_inputs() { - let mut decoded_base64 = [0u8; LEAF_BASE64_BUFFER_SIZE]; - for (i, (leaf_input, subject, timestamp)) in - test_constants::LEAF_INPUT_EXAMPLES.into_iter().enumerate() - { - decoded_base64[..leaf_input.len()].copy_from_slice(leaf_input.as_bytes()); - let decoded_base64 = base64ct::Base64::decode_in_place( - &mut decoded_base64[..leaf_input.len()] - ) - .expect("Should parse base64 properly"); - - let (_, parsed) = - parse_merkle_tree_leaf(decoded_base64).expect("should complete"); - - let MerkleLeafType::TimeStampedEntry { - timestamp: parsed_timestamp, - entry_type, - extensions: _ - } = parsed.leaf_type; - - assert_eq!( - *timestamp, parsed_timestamp, - "leaf_input should have expected timestamp" - ); - - match entry_type { - LogEntryType::X509Entry(x509_cert) => { - assert_eq!( - &x509_cert.subject.to_string(), - subject, - "leaf_entry x509 cert should have expected subject field" - ); - } - LogEntryType::PrecertEntry { - issuer_key_hash: _, - tbs_certificate - } => { - assert_eq!( - &tbs_certificate.subject.to_string(), - subject, - "leaf_entry tbs cert should have expected subject field" - ); - } - } - println!( - "[{i}] Correctly parsed leaf_input for subject '{subject}' issued at \ - {timestamp}" - ); - } - } -} diff --git a/tests/log_entry_parsing.rs b/tests/log_entry_parsing.rs new file mode 100644 index 0000000..897a208 --- /dev/null +++ b/tests/log_entry_parsing.rs @@ -0,0 +1,106 @@ +use std::sync::Arc; + +use base64ct::Encoding; +use ct::{ + merkle::{ + consts::{EXTRA_DATA_BASE64_BUFFER_SIZE, LEAF_BASE64_BUFFER_SIZE}, + types::{LogEntryType, MerkleLeafType, Version} + }, + parsing::entry::{parse_entry_extra_data_precert, parse_entry_extra_data_x509} +}; +use serde::Deserialize; +use tokio::task::JoinSet; + +#[derive(Debug, Deserialize)] +pub struct Entry { + leaf_input: String, + extra_data: String +} + +#[derive(Debug, Deserialize)] +pub struct GetEntriesResponse { + entries: Vec +} + +async fn fetch_entries() -> impl Iterator { + let client = Arc::new(reqwest::Client::new()); + let mut join_set: JoinSet = JoinSet::new(); + for i in 0..12u32 { + let client = client.clone(); + join_set.spawn(async move { + client + .get("https://oak.ct.letsencrypt.org/2024h2/ct/v1/get-entries") + .query(&[("start", i * 256), ("end", (i + 1) * 256 - 1)]) + .send() + .await + .expect("Request to ct log should succeed") + .json() + .await + .expect("Request to ct log should parse properly") + }); + } + join_set + .join_all() + .await + .into_iter() + .flat_map(|e| e.entries) +} + +#[tokio::test] +async fn test_letsencrypt_2024h2_parsing() { + let mut buffer = [0u8; EXTRA_DATA_BASE64_BUFFER_SIZE]; + + for (i, entry) in fetch_entries().await.enumerate() { + // Parse leaf_input + buffer[..entry.leaf_input.len()].copy_from_slice(entry.leaf_input.as_bytes()); + let parsed = + base64ct::Base64::decode_in_place(&mut buffer[..entry.leaf_input.len()]) + .expect("leaf_input should parse as base64 properly"); + + let (input, merkle_tree_leaf) = + ct::parsing::entry::parse_merkle_tree_leaf(parsed) + .expect("leaf_input should parse properly"); + + // leaf_input Assertions + assert_eq!(input.len(), 0, "All input should be parsed"); + let MerkleLeafType::TimeStampedEntry { entry_type, .. } = + merkle_tree_leaf.leaf_type + else { + panic!("Merkle tree leaf should be TimestampedEntry"); + }; + let Version::V1 = merkle_tree_leaf.version else { + panic!("Merkle tree leaf should be Version::V1"); + }; + + // Calculate this before we have to mutably borrow the buffer again to avoid + // multiple mutable references + let parse_entry_extra_data = match entry_type { + ct::merkle::types::LogEntryType::X509Entry(..) => parse_entry_extra_data_x509, + ct::merkle::types::LogEntryType::PrecertEntry { .. } => + parse_entry_extra_data_precert, + }; + + // Parse extra_data + buffer[..entry.extra_data.len()].copy_from_slice(entry.extra_data.as_bytes()); + let parsed = + base64ct::Base64::decode_in_place(&mut buffer[..entry.extra_data.len()]) + .expect("extra_data should parse as base64 properly"); + + let (input, parsed) = + parse_entry_extra_data(parsed).expect("extra_data should parse properly"); + + // extra_data assertions + assert_eq!(input.len(), 0, "All input should be parsed at entry {i}"); + match parsed { + ct::merkle::types::EntryExtraData::X509Certificate(chain) => + assert_ne!(chain.len(), 0, "x509 chain should not be 0 length"), + // TODO: Verify main_certificate against the tbs certificate loaded from + // leaf_entry + ct::merkle::types::EntryExtraData::Precertificate(chain_entry) => assert_ne!( + chain_entry.certificate_chain.len(), + 0, + "precert chain should not be 0 length" + ) + } + } +}