1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(clippy::all)]

use std::ffi::c_void;
use std::fmt;

pub type TLSLike = *const c_void;

pub const CLAIM_INTERFACE_H: &str =
    include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/claim-interface.h"));

include!(concat!(env!("OUT_DIR"), "/claim-interface.rs"));

impl fmt::Display for Claim {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "\
            typ: {},\
            write: {},\
            version: {},\
            server: {},\
            session_id: {},\
            server_random: {},\
            client_random: {},\
            cert: {},\
            peer_cert: {},\
            peer_tmp_skey_type: {},\
            peer_tmp_skey_security_bits: {},\
            tmp_skey_type: {},\
            tmp_skey_group_id: {},\
            signature_algorithm: {},\
            peer_signature_algorithm: {},\
            early_secret: {},\
            handshake_secret: {},\
            master_secret: {},\
            resumption_master_secret: {},\
            client_finished_secret: {},\
            server_finished_secret: {},\
            server_finished_hash: {},\
            handshake_traffic_hash: {},\
            client_app_traffic_secret: {},\
            server_app_traffic_secret: {},\
            exporter_master_secret: {},\
            early_exporter_master_secret: {},\
            master_secret_12: {},\
            available_ciphers: {},\
            chosen_cipher: {},\
            transcript: {},\
            ",
            self.typ,
            self.write,
            self.version,
            self.server,
            self.session_id,
            self.server_random,
            self.client_random,
            self.cert,
            self.peer_cert,
            self.peer_tmp_skey_type,
            self.peer_tmp_skey_security_bits,
            self.tmp_skey_type,
            self.tmp_skey_group_id,
            self.signature_algorithm,
            self.peer_signature_algorithm,
            self.early_secret,
            self.handshake_secret,
            self.master_secret,
            self.resumption_master_secret,
            self.client_finished_secret,
            self.server_finished_secret,
            self.server_finished_hash,
            self.handshake_traffic_hash,
            self.client_app_traffic_secret,
            self.server_app_traffic_secret,
            self.exporter_master_secret,
            self.early_exporter_master_secret,
            self.master_secret_12,
            self.available_ciphers,
            self.chosen_cipher,
            self.transcript,
        )
    }
}

impl fmt::Display for ClaimVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.data)
    }
}

impl fmt::Display for ClaimTranscript {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.data))
    }
}

impl fmt::Display for ClaimCipher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.data.to_be_bytes()))
    }
}

impl fmt::Display for ClaimSessionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(&self.data[0..self.length as usize]),)
    }
}

impl fmt::Display for ClaimRandom {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.data))
    }
}

impl fmt::Display for ClaimCertData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:?}({}b)",
            self.key_type,
            if self.key_length == 0 {
                "?".to_string()
            } else {
                self.key_length.to_string()
            }
        )
    }
}

impl fmt::Display for ClaimCiphers {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            self.ciphers[0..self.length as usize]
                .iter()
                .map(|c| hex::encode(c.data.to_be_bytes()))
                .collect::<Vec<String>>()
                .join(", ")
        )
    }
}

impl fmt::Display for ClaimType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Display for ClaimKeyType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Display for ClaimSecret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let secret = self.secret;
        // print if any byte is set
        if secret.iter().any(|v| *v != 0) {
            write!(f, "{}", hex::encode(secret))?;
        }

        Ok(())
    }
}