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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![allow(clippy::ptr_arg)]
#![allow(dead_code)]

use puffin::algebra::error::FnError;
use puffin::codec::{Codec, Reader};

use crate::tls::key_exchange::tls12_new_secrets;
use crate::tls::key_schedule::dhe_key_schedule;
use crate::tls::rustls::hash_hs::HandshakeHash;
use crate::tls::rustls::key_log::NoKeyLog;
use crate::tls::rustls::msgs::enums::{
    CipherSuite, Compression, ExtensionType, NamedGroup, ProtocolVersion,
};
use crate::tls::rustls::msgs::handshake::{
    ClientExtension, HasServerExtensions, Random, ServerExtension, SessionID,
};

pub fn fn_protocol_version13() -> Result<ProtocolVersion, FnError> {
    Ok(ProtocolVersion::TLSv1_3)
}

pub fn fn_protocol_version12() -> Result<ProtocolVersion, FnError> {
    Ok(ProtocolVersion::TLSv1_2)
}

pub fn fn_new_session_id() -> Result<SessionID, FnError> {
    let mut id: Vec<u8> = Vec::from([3u8; 32]);
    id.insert(0, 32);
    let id = SessionID::read(&mut Reader::init(id.as_slice()))
        .ok_or_else(|| FnError::Unknown("Failed to create session id".to_string()))?;
    Ok(id)
}

pub fn fn_empty_session_id() -> Result<SessionID, FnError> {
    let mut id: Vec<u8> = Vec::from([]);
    id.insert(0, 0);
    let id = SessionID::read(&mut Reader::init(id.as_slice()));
    Ok(id.unwrap())
}

pub fn fn_new_random() -> Result<Random, FnError> {
    let random_data: [u8; 32] = [1; 32];
    Ok(Random::from(random_data))
}

pub fn fn_compressions() -> Result<Vec<Compression>, FnError> {
    Ok(vec![Compression::Null])
}

pub fn fn_compression() -> Result<Compression, FnError> {
    Ok(Compression::Null)
}

pub fn fn_no_key_share() -> Result<Option<Vec<u8>>, FnError> {
    Ok(None)
}

pub fn fn_get_server_key_share(
    server_extensions: &Vec<ServerExtension>,
) -> Result<Option<Vec<u8>>, FnError> {
    let server_extension = server_extensions
        .find_extension(ExtensionType::KeyShare)
        .ok_or(FnError::Unknown("KeyShare extension not found".to_string()))?;

    if let ServerExtension::KeyShare(keyshare) = server_extension {
        Ok(Some(keyshare.payload.0.clone()))
    } else {
        Err(FnError::Unknown("KeyShare extension not found".to_string()))
    }
}

pub fn fn_get_client_key_share(
    client_extensions: &Vec<ClientExtension>,
    group: &NamedGroup,
) -> Result<Option<Vec<u8>>, FnError> {
    let client_extension = client_extensions
        .iter()
        .find(|x| x.get_type() == ExtensionType::KeyShare)
        .ok_or(FnError::Unknown("KeyShare extension not found".to_string()))?;

    if let ClientExtension::KeyShare(keyshares) = client_extension {
        let keyshare = keyshares
            .0
            .iter()
            .find(|keyshare| keyshare.group == *group)
            .ok_or(FnError::Unknown("Keyshare not found".to_string()))?;
        Ok(Some(keyshare.payload.0.clone()))
    } else {
        Err(FnError::Unknown("KeyShare extension not found".to_string()))
    }
}

pub fn fn_get_any_client_curve(
    client_extensions: &Vec<ClientExtension>,
) -> Result<NamedGroup, FnError> {
    let client_extension = client_extensions
        .iter()
        .find(|x| x.get_type() == ExtensionType::KeyShare)
        .ok_or(FnError::Unknown("KeyShare extension not found".to_string()))?;

    if let ClientExtension::KeyShare(keyshares) = client_extension {
        Ok(keyshares
            .0
            .first()
            .ok_or(FnError::Unknown("Keyshare not found".to_string()))?
            .group)
    } else {
        Err(FnError::Unknown("KeyShare extension not found".to_string()))
    }
}

pub fn fn_verify_data(
    server_finished: &HandshakeHash,
    server_hello: &HandshakeHash,
    server_key_share: &Option<Vec<u8>>,
    psk: &Option<Vec<u8>>,
    group: &NamedGroup,
) -> Result<Vec<u8>, FnError> {
    let client_random = &[1u8; 32]; // todo see op_random() https://github.com/tlspuffin/tlspuffin/issues/129
    let suite = &crate::tls::rustls::tls13::TLS13_AES_128_GCM_SHA256; // todo see op_cipher_suites()

    let key_schedule = dhe_key_schedule(suite, group, server_key_share, psk)?;

    let (hs, _client_secret, _server_secret) = key_schedule.derive_handshake_secrets(
        &server_hello.get_current_hash_raw(),
        &NoKeyLog,
        client_random,
    );

    let (pending, _client_secret, _server_secret) = hs
        .into_traffic_with_client_finished_pending_raw(
            &server_hello.get_current_hash_raw(),
            &NoKeyLog,
            client_random,
        );

    let (_traffic, tag, _client_secret) =
        pending.sign_client_finish_raw(&server_finished.get_current_hash_raw());
    Ok(Vec::from(tag.as_ref()))
}

pub fn fn_verify_data_server(
    server_finished: &HandshakeHash,
    server_hello: &HandshakeHash,
    server_key_share: &Option<Vec<u8>>,
    group: &NamedGroup,
    psk: &Option<Vec<u8>>,
) -> Result<Vec<u8>, FnError> {
    let client_random = &[1u8; 32]; // todo see op_random() https://github.com/tlspuffin/tlspuffin/issues/129
    let suite = &crate::tls::rustls::tls13::TLS13_AES_128_GCM_SHA256; // todo see op_cipher_suites()

    let key_schedule = dhe_key_schedule(suite, group, server_key_share, psk)?;

    let (hs, _client_secret, _server_secret) = key_schedule.derive_handshake_secrets(
        &server_hello.get_current_hash_raw(),
        &NoKeyLog,
        client_random,
    );

    let tag = hs.sign_server_finish_raw(&server_finished.get_current_hash_raw());
    let vec = Vec::from(tag.as_ref());
    Ok(vec)
}

// ----
// seed_client_attacker12()
// ----

pub fn fn_sign_transcript(
    server_random: &Random,
    server_ecdh_pubkey: &Vec<u8>,
    transcript: &HandshakeHash,
    group: &NamedGroup,
) -> Result<Vec<u8>, FnError> {
    let secrets = tls12_new_secrets(server_random, server_ecdh_pubkey, group)?;

    let vh = transcript.get_current_hash();
    Ok(secrets.client_verify_data(&vh))
}

// ----
// Cipher Suites
// ----

pub fn fn_new_cipher_suites() -> Result<Vec<CipherSuite>, FnError> {
    Ok(vec![])
}

// todo implement functions for all supported cipher suites as constants
//      https://github.com/tlspuffin/tlspuffin/issues/155
pub fn fn_append_cipher_suite(
    suites: &Vec<CipherSuite>,
    suite: &CipherSuite,
) -> Result<Vec<CipherSuite>, FnError> {
    let mut new: Vec<CipherSuite> = suites.clone();
    new.push(*suite);
    Ok(new)
}

pub fn fn_cipher_suite12() -> Result<CipherSuite, FnError> {
    Ok(
        CipherSuite::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        /*CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
        CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
        CipherSuite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
        CipherSuite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256*/
    )
}

pub fn fn_cipher_suite13_aes_128_gcm_sha256() -> Result<CipherSuite, FnError> {
    Ok(CipherSuite::TLS13_AES_128_GCM_SHA256)
}

pub fn fn_cipher_suite13_aes_256_gcm_sha384() -> Result<CipherSuite, FnError> {
    Ok(CipherSuite::TLS13_AES_256_GCM_SHA384)
}

pub fn fn_cipher_suite13_aes_128_ccm_sha256() -> Result<CipherSuite, FnError> {
    Ok(CipherSuite::TLS13_AES_128_CCM_SHA256)
}

pub fn fn_weak_export_cipher_suite() -> Result<CipherSuite, FnError> {
    Ok(CipherSuite::TLS_RSA_EXPORT_WITH_DES40_CBC_SHA)
}

pub fn fn_secure_rsa_cipher_suite12() -> Result<CipherSuite, FnError> {
    Ok(CipherSuite::TLS_RSA_WITH_AES_256_CBC_SHA256)
}