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
use crate::cipher_suite::*;
use crate::crypto::*;
use crate::error::*;
use crate::extension::extension_use_srtp::SrtpProtectionProfile;
use crate::handshaker::VerifyPeerCertificateFn;
use crate::signature_hash_algorithm::SignatureScheme;
use anyhow::Result;
use std::sync::Arc;
use tokio::time::Duration;
#[derive(Clone)]
pub struct Config {
pub certificates: Vec<Certificate>,
pub cipher_suites: Vec<CipherSuiteId>,
pub signature_schemes: Vec<SignatureScheme>,
pub srtp_protection_profiles: Vec<SrtpProtectionProfile>,
pub client_auth: ClientAuthType,
pub extended_master_secret: ExtendedMasterSecretType,
pub flight_interval: Duration,
pub psk: Option<PskCallback>,
pub psk_identity_hint: Option<Vec<u8>>,
pub insecure_skip_verify: bool,
pub insecure_hashes: bool,
pub verify_peer_certificate: Option<VerifyPeerCertificateFn>,
pub roots_cas: rustls::RootCertStore,
pub client_cert_verifier: Option<Arc<dyn rustls::ClientCertVerifier>>,
pub server_name: String,
pub mtu: usize,
pub replay_protection_window: usize,
}
impl Default for Config {
fn default() -> Self {
Config {
certificates: vec![],
cipher_suites: vec![],
signature_schemes: vec![],
srtp_protection_profiles: vec![],
client_auth: ClientAuthType::default(),
extended_master_secret: ExtendedMasterSecretType::default(),
flight_interval: Duration::default(),
psk: None,
psk_identity_hint: None,
insecure_skip_verify: false,
insecure_hashes: false,
verify_peer_certificate: None,
roots_cas: rustls::RootCertStore::empty(),
client_cert_verifier: None,
server_name: String::default(),
mtu: 0,
replay_protection_window: 0,
}
}
}
pub(crate) const DEFAULT_MTU: usize = 1200;
pub(crate) type PskCallback = fn(&[u8]) -> Result<Vec<u8>>;
#[derive(Copy, Clone, PartialEq)]
pub enum ClientAuthType {
NoClientCert = 0,
RequestClientCert = 1,
RequireAnyClientCert = 2,
VerifyClientCertIfGiven = 3,
RequireAndVerifyClientCert = 4,
}
impl Default for ClientAuthType {
fn default() -> Self {
ClientAuthType::NoClientCert
}
}
#[derive(PartialEq, Copy, Clone)]
pub enum ExtendedMasterSecretType {
Request = 0,
Require = 1,
Disable = 2,
}
impl Default for ExtendedMasterSecretType {
fn default() -> Self {
ExtendedMasterSecretType::Request
}
}
pub(crate) fn validate_config(is_client: bool, config: &Config) -> Result<()> {
if is_client && config.psk.is_some() && config.psk_identity_hint.is_none() {
return Err(Error::ErrPskAndIdentityMustBeSetForClient.into());
}
if !is_client && config.psk.is_none() && config.certificates.is_empty() {
return Err(Error::ErrServerMustHaveCertificate.into());
}
if !config.certificates.is_empty() && config.psk.is_some() {
return Err(Error::ErrPskAndCertificate.into());
}
if config.psk_identity_hint.is_some() && config.psk.is_none() {
return Err(Error::ErrIdentityNoPsk.into());
}
for cert in &config.certificates {
match cert.private_key.kind {
CryptoPrivateKeyKind::Ed25519(_) => {}
CryptoPrivateKeyKind::Ecdsa256(_) => {}
_ => return Err(Error::ErrInvalidPrivateKey.into()),
}
}
parse_cipher_suites(
&config.cipher_suites,
config.psk.is_none(),
config.psk.is_some(),
)?;
Ok(())
}