# fixtures.rs - source
Source of the Rust file `crates/core/src/fixtures.rs`.

Skip to main content

#
tlsn_core/
fixtures.rs

    1//! Fixtures for testing
    2
    3pub mod transcript;
    4
    5use hex::FromHex;
    6
    7use crate::{
    8    connection::{
    9        CertBinding, CertBindingV1_2, ConnectionInfo, DnsName, HandshakeData, KeyType,
    10        ServerEphemKey, ServerName, ServerSignature, SignatureAlgorithm, TlsVersion,
    11        TranscriptLength,
    12    },
    13    webpki::CertificateDer,
    14};
    15
    16/// A fixture containing various TLS connection data.
    17#[derive(Clone)]
    18#[allow(missing_docs)]
    19pub struct ConnectionFixture {
    20    pub server_name: ServerName,
    21    pub connection_info: ConnectionInfo,
    22    pub server_cert_data: HandshakeData,
    23}
    24
    25impl ConnectionFixture {
    26    /// Returns a connection fixture for tlsnotary.org.
    27    pub fn tlsnotary(transcript_length: TranscriptLength) -> Self {
    28        ConnectionFixture {
    29            server_name: ServerName::Dns(DnsName::try_from("tlsnotary.org").unwrap()),
    30            connection_info: ConnectionInfo {
    31                time: 1671637529,
    32                version: TlsVersion::V1_2,
    33                transcript_length,
    34            },
    35            server_cert_data: HandshakeData {
    36                certs: vec![
    37                    CertificateDer(include_bytes!("fixtures/data/tlsnotary.org/ee.der").to_vec()),
    38                    CertificateDer(
    39                        include_bytes!("fixtures/data/tlsnotary.org/inter.der").to_vec(),
    40                    ),
    41                    CertificateDer(include_bytes!("fixtures/data/tlsnotary.org/ca.der").to_vec()),
    42                ],
    43                sig: ServerSignature {
    44                    alg: SignatureAlgorithm::RSA_PKCS1_2048_8192_SHA256,
    45                    sig: Vec::<u8>::from_hex(include_bytes!(
    46                        "fixtures/data/tlsnotary.org/signature"
    47                    ))
    48                    .unwrap(),
    49                },
    50                binding: CertBinding::V1_2(CertBindingV1_2 {
    51                    client_random: <[u8; 32]>::from_hex(include_bytes!(
    52                        "fixtures/data/tlsnotary.org/client_random"
    53                    ))
    54                    .unwrap(),
    55                    server_random: <[u8; 32]>::from_hex(include_bytes!(
    56                        "fixtures/data/tlsnotary.org/server_random"
    57                    ))
    58                    .unwrap(),
    59                    server_ephemeral_key: ServerEphemKey {
    60                        typ: KeyType::SECP256R1,
    61                        key: Vec::<u8>::from_hex(include_bytes!(
    62                            "fixtures/data/tlsnotary.org/pubkey"
    63                        ))
    64                        .unwrap(),
    65                    },
    66                }),
    67            },
    68        }
    69    }
    70
    71    /// Returns a connection fixture for appliedzkp.org.
    72    pub fn appliedzkp(transcript_length: TranscriptLength) -> Self {
    73        ConnectionFixture {
    74            server_name: ServerName::Dns(DnsName::try_from("appliedzkp.org").unwrap()),
    75            connection_info: ConnectionInfo {
    76                time: 1671637529,
    77                version: TlsVersion::V1_2,
    78                transcript_length,
    79            },
    80            server_cert_data: HandshakeData {
    81                certs: vec![
    82                    CertificateDer(include_bytes!("fixtures/data/appliedzkp.org/ee.der").to_vec()),
    83                    CertificateDer(
    84                        include_bytes!("fixtures/data/appliedzkp.org/inter.der").to_vec(),
    85                    ),
    86                    CertificateDer(include_bytes!("fixtures/data/appliedzkp.org/ca.der").to_vec()),
    87                ],
    88                sig: ServerSignature {
    89                    alg: SignatureAlgorithm::ECDSA_NISTP256_SHA256,
    90                    sig: Vec::<u8>::from_hex(include_bytes!(
    91                        "fixtures/data/appliedzkp.org/signature"
    92                    ))
    93                    .unwrap(),
    94                },
    95                binding: CertBinding::V1_2(CertBindingV1_2 {
    96                    client_random: <[u8; 32]>::from_hex(include_bytes!(
    97                        "fixtures/data/appliedzkp.org/client_random"
    98                    ))
    99                    .unwrap(),
    100                    server_random: <[u8; 32]>::from_hex(include_bytes!(
    101                        "fixtures/data/appliedzkp.org/server_random"
    102                    ))
    103                    .unwrap(),
    104                    server_ephemeral_key: ServerEphemKey {
    105                        typ: KeyType::SECP256R1,
    106                        key: Vec::<u8>::from_hex(include_bytes!(
    107                            "fixtures/data/appliedzkp.org/pubkey"
    108                        ))
    109                        .unwrap(),
    110                    },
    111                }),
    112            },
    113        }
    114    }
    115
    116    /// Returns the server_ephemeral_key fixture.
    117    pub fn server_ephemeral_key(&self) -> &ServerEphemKey {
    118        let CertBinding::V1_2(CertBindingV1_2 {
    119            server_ephemeral_key,
    120            ..
    121        }) = &self.server_cert_data.binding;
    122        server_ephemeral_key
    123    }
    124}