1pub mod cipher_suite_aes_128_ccm;
3pub mod cipher_suite_aes_128_gcm_sha256;
5pub mod cipher_suite_aes_256_cbc_sha;
7pub mod cipher_suite_chacha20_poly1305_sha256;
9pub mod cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm;
12pub mod cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm8;
14pub mod cipher_suite_tls_psk_with_aes_128_ccm;
16pub mod cipher_suite_tls_psk_with_aes_128_ccm8;
18pub mod cipher_suite_tls_psk_with_aes_128_gcm_sha256;
20
21use crypto::RTCCryptoProvider;
22use crypto::{
23 AeadAlgorithm, CbcAlgorithm, CryptoAlgorithm, HashAlgorithm, HmacAlgorithm, RTCCrypto,
24};
25use std::fmt;
26use std::sync::Arc;
27
28use super::client_certificate_type::*;
29use super::record_layer::record_layer_header::*;
30use shared::error::*;
31
32use cipher_suite_aes_128_gcm_sha256::*;
33use cipher_suite_aes_256_cbc_sha::*;
34use cipher_suite_chacha20_poly1305_sha256::*;
35use cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm::*;
36use cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm8::*;
37use cipher_suite_tls_psk_with_aes_128_ccm::*;
38use cipher_suite_tls_psk_with_aes_128_ccm8::*;
39use cipher_suite_tls_psk_with_aes_128_gcm_sha256::*;
40
41#[allow(non_camel_case_types)]
44#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45#[non_exhaustive]
47pub enum CipherSuiteId {
48 Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm = 0xc0ac,
51 Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8 = 0xc0ae,
53
54 Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256 = 0xc02b,
57 Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256 = 0xc02f,
59
60 Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha = 0xc00a,
63 Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha = 0xc014,
65
66 Tls_Psk_With_Aes_128_Ccm = 0xc0a4,
68 Tls_Psk_With_Aes_128_Ccm_8 = 0xc0a8,
70 Tls_Psk_With_Aes_128_Gcm_Sha256 = 0x00a8,
72
73 Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256 = 0xcca8,
76 Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256 = 0xcca9,
78
79 Tls_Aes_128_Gcm_Sha256 = 0x1301,
82 Tls_Aes_256_Gcm_Sha384 = 0x1302,
84 Tls_ChaCha20_Poly1305_Sha256 = 0x1303,
86 Tls_Aes_128_Ccm_Sha256 = 0x1304,
88 Tls_Aes_128_Ccm_8_Sha256 = 0x1305,
93
94 Tls_Empty_Renegotiation_Info_Scsv = 0x00ff,
98
99 Unsupported,
101}
102
103impl fmt::Display for CipherSuiteId {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 match *self {
106 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm => {
107 write!(f, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM")
108 }
109 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8 => {
110 write!(f, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8")
111 }
112 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256 => {
113 write!(f, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256")
114 }
115 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256 => {
116 write!(f, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")
117 }
118 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha => {
119 write!(f, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA")
120 }
121 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha => {
122 write!(f, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA")
123 }
124 CipherSuiteId::Tls_Psk_With_Aes_128_Ccm => write!(f, "TLS_PSK_WITH_AES_128_CCM"),
125 CipherSuiteId::Tls_Psk_With_Aes_128_Ccm_8 => write!(f, "TLS_PSK_WITH_AES_128_CCM_8"),
126 CipherSuiteId::Tls_Psk_With_Aes_128_Gcm_Sha256 => {
127 write!(f, "TLS_PSK_WITH_AES_128_GCM_SHA256")
128 }
129 CipherSuiteId::Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256 => {
130 write!(f, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")
131 }
132 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256 => {
133 write!(f, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256")
134 }
135
136 CipherSuiteId::Tls_Aes_128_Gcm_Sha256 => write!(f, "TLS_AES_128_GCM_SHA256"),
137 CipherSuiteId::Tls_Aes_256_Gcm_Sha384 => write!(f, "TLS_AES_256_GCM_SHA384"),
138 CipherSuiteId::Tls_ChaCha20_Poly1305_Sha256 => {
139 write!(f, "TLS_CHACHA20_POLY1305_SHA256")
140 }
141 CipherSuiteId::Tls_Aes_128_Ccm_Sha256 => write!(f, "TLS_AES_128_CCM_SHA256"),
142 CipherSuiteId::Tls_Aes_128_Ccm_8_Sha256 => write!(f, "TLS_AES_128_CCM_8_SHA256"),
143
144 CipherSuiteId::Tls_Empty_Renegotiation_Info_Scsv => {
145 write!(f, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV")
146 }
147
148 _ => write!(f, "Unsupported CipherSuiteID"),
149 }
150 }
151}
152
153impl From<u16> for CipherSuiteId {
154 fn from(val: u16) -> Self {
155 match val {
156 0xc0ac => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm,
158 0xc0ae => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8,
159
160 0xc02b => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256,
162 0xc02f => CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256,
163
164 0xc00a => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha,
166 0xc014 => CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha,
167
168 0xc0a4 => CipherSuiteId::Tls_Psk_With_Aes_128_Ccm,
169 0xc0a8 => CipherSuiteId::Tls_Psk_With_Aes_128_Ccm_8,
170 0x00a8 => CipherSuiteId::Tls_Psk_With_Aes_128_Gcm_Sha256,
171
172 0xcca8 => CipherSuiteId::Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256,
174 0xcca9 => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256,
175
176 0x1301 => CipherSuiteId::Tls_Aes_128_Gcm_Sha256,
177 0x1302 => CipherSuiteId::Tls_Aes_256_Gcm_Sha384,
178 0x1303 => CipherSuiteId::Tls_ChaCha20_Poly1305_Sha256,
179 0x1304 => CipherSuiteId::Tls_Aes_128_Ccm_Sha256,
180 0x1305 => CipherSuiteId::Tls_Aes_128_Ccm_8_Sha256,
181
182 0x00ff => CipherSuiteId::Tls_Empty_Renegotiation_Info_Scsv,
183
184 _ => CipherSuiteId::Unsupported,
185 }
186 }
187}
188
189impl From<&str> for CipherSuiteId {
190 fn from(val: &str) -> Self {
191 match val {
192 "TLS_ECDHE_ECDSA_WITH_AES_128_CCM" => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm,
193 "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" => {
194 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8
195 }
196 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" => {
197 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256
198 }
199 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" => {
200 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256
201 }
202 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" => {
203 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha
204 }
205 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" => {
206 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha
207 }
208 "TLS_PSK_WITH_AES_128_CCM" => CipherSuiteId::Tls_Psk_With_Aes_128_Ccm,
209 "TLS_PSK_WITH_AES_128_CCM_8" => CipherSuiteId::Tls_Psk_With_Aes_128_Ccm_8,
210 "TLS_PSK_WITH_AES_128_GCM_SHA256" => CipherSuiteId::Tls_Psk_With_Aes_128_Gcm_Sha256,
211 "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" => {
212 CipherSuiteId::Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256
213 }
214 "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" => {
215 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256
216 }
217 "TLS_AES_128_GCM_SHA256" => CipherSuiteId::Tls_Aes_128_Gcm_Sha256,
218 "TLS_AES_256_GCM_SHA384" => CipherSuiteId::Tls_Aes_256_Gcm_Sha384,
219 "TLS_CHACHA20_POLY1305_SHA256" => CipherSuiteId::Tls_ChaCha20_Poly1305_Sha256,
220 "TLS_AES_128_CCM_SHA256" => CipherSuiteId::Tls_Aes_128_Ccm_Sha256,
221 "TLS_AES_128_CCM_8_SHA256" => CipherSuiteId::Tls_Aes_128_Ccm_8_Sha256,
222 _ => CipherSuiteId::Unsupported,
223 }
224 }
225}
226
227impl CipherSuiteId {
228 pub fn is_tls13(self) -> bool {
230 crate::dtls13::suite::Suite13::for_id(self).is_some()
231 }
232
233 pub(crate) fn supported_by(self, crypto: &dyn RTCCrypto) -> bool {
234 if let Some(suite) = crate::dtls13::suite::Suite13::for_id(self) {
235 return suite.supported_by(crypto);
236 }
237 let record_algorithm = match self {
238 Self::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm | Self::Tls_Psk_With_Aes_128_Ccm => {
239 CryptoAlgorithm::Aead(AeadAlgorithm::Aes128Ccm)
240 }
241 Self::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8 | Self::Tls_Psk_With_Aes_128_Ccm_8 => {
242 CryptoAlgorithm::Aead(AeadAlgorithm::Aes128Ccm8)
243 }
244 Self::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256
245 | Self::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256
246 | Self::Tls_Psk_With_Aes_128_Gcm_Sha256 => {
247 CryptoAlgorithm::Aead(AeadAlgorithm::Aes128Gcm)
248 }
249 Self::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha
250 | Self::Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha => {
251 CryptoAlgorithm::Cbc(CbcAlgorithm::Aes256Cbc)
252 }
253 Self::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256
254 | Self::Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256 => {
255 CryptoAlgorithm::Aead(AeadAlgorithm::ChaCha20Poly1305)
256 }
257 Self::Tls_Aes_128_Gcm_Sha256
258 | Self::Tls_Aes_256_Gcm_Sha384
259 | Self::Tls_ChaCha20_Poly1305_Sha256
260 | Self::Tls_Aes_128_Ccm_Sha256
261 | Self::Tls_Aes_128_Ccm_8_Sha256
262 | Self::Tls_Empty_Renegotiation_Info_Scsv
263 | Self::Unsupported => return false,
264 };
265 crypto.supports(CryptoAlgorithm::Hash(HashAlgorithm::Sha256))
266 && crypto.supports(CryptoAlgorithm::Hmac(HmacAlgorithm::Sha256))
267 && crypto.supports(record_algorithm)
268 && (!matches!(record_algorithm, CryptoAlgorithm::Cbc(_))
269 || crypto.supports(CryptoAlgorithm::Hmac(HmacAlgorithm::Sha1)))
270 }
271}
272
273#[derive(Copy, Clone, Debug)]
274#[non_exhaustive]
276pub enum CipherSuiteHash {
277 Sha256,
279 Sha384,
281}
282
283impl CipherSuiteHash {
284 pub(crate) fn size(&self) -> usize {
285 match *self {
286 CipherSuiteHash::Sha256 => 32,
287 CipherSuiteHash::Sha384 => 48,
288 }
289 }
290
291 pub(crate) fn hmac_algorithm(&self) -> HmacAlgorithm {
292 match *self {
293 CipherSuiteHash::Sha256 => HmacAlgorithm::Sha256,
294 CipherSuiteHash::Sha384 => HmacAlgorithm::Sha384,
295 }
296 }
297
298 pub(crate) fn hash_algorithm(&self) -> HashAlgorithm {
299 match *self {
300 CipherSuiteHash::Sha256 => HashAlgorithm::Sha256,
301 CipherSuiteHash::Sha384 => HashAlgorithm::Sha384,
302 }
303 }
304}
305
306pub trait CipherSuite: Send {
309 fn to_string(&self) -> String;
311 fn id(&self) -> CipherSuiteId;
313 fn certificate_type(&self) -> ClientCertificateType;
315 fn hash_func(&self) -> CipherSuiteHash;
317 fn is_psk(&self) -> bool;
319 fn is_initialized(&self) -> bool;
321 fn record_expansion(&self) -> usize;
325
326 fn init(
333 &mut self,
334 provider: Arc<dyn RTCCryptoProvider>,
335 master_secret: &[u8],
336 client_random: &[u8],
337 server_random: &[u8],
338 is_client: bool,
339 ) -> Result<()>;
340
341 fn encrypt(&mut self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>>;
347 fn decrypt(&mut self, input: &[u8]) -> Result<Vec<u8>>;
353
354 fn encrypt_with_connection_id(
363 &mut self,
364 _pkt_rlh: &RecordLayerHeader,
365 _raw: &[u8],
366 _connection_id: &[u8],
367 ) -> Result<Vec<u8>> {
368 Err(Error::Other(
369 "the cipher suite has no tls12_cid records".to_owned(),
370 ))
371 }
372
373 fn decrypt_with_connection_id(
380 &mut self,
381 _input: &[u8],
382 _connection_id: &[u8],
383 ) -> Result<Vec<u8>> {
384 Err(Error::Other(
385 "the cipher suite has no tls12_cid records".to_owned(),
386 ))
387 }
388}
389
390pub fn cipher_suite_for_id(id: CipherSuiteId) -> Result<Box<dyn CipherSuite>> {
399 match id {
400 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm => {
401 Ok(Box::new(new_cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm()))
402 }
403 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8 => Ok(Box::new(
404 new_cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm8(),
405 )),
406 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256 => {
407 Ok(Box::new(CipherSuiteAes128GcmSha256::new(false)))
408 }
409 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256 => {
410 Ok(Box::new(CipherSuiteAes128GcmSha256::new(true)))
411 }
412 CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha => {
413 Ok(Box::new(CipherSuiteAes256CbcSha::new(true)))
414 }
415 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha => {
416 Ok(Box::new(CipherSuiteAes256CbcSha::new(false)))
417 }
418 CipherSuiteId::Tls_Psk_With_Aes_128_Ccm => {
419 Ok(Box::new(new_cipher_suite_tls_psk_with_aes_128_ccm()))
420 }
421 CipherSuiteId::Tls_Psk_With_Aes_128_Ccm_8 => {
422 Ok(Box::new(new_cipher_suite_tls_psk_with_aes_128_ccm8()))
423 }
424 CipherSuiteId::Tls_Psk_With_Aes_128_Gcm_Sha256 => {
425 Ok(Box::<CipherSuiteTlsPskWithAes128GcmSha256>::default())
426 }
427 CipherSuiteId::Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256 => {
428 Ok(Box::new(CipherSuiteChaCha20Poly1305Sha256::new(true)))
429 }
430 CipherSuiteId::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256 => {
431 Ok(Box::new(CipherSuiteChaCha20Poly1305Sha256::new(false)))
432 }
433
434 id => crate::dtls13::suite::Suite13::for_id(id)
435 .map(|suite| Box::new(suite) as Box<dyn CipherSuite>)
436 .ok_or(Error::ErrInvalidCipherSuite),
437 }
438}
439
440pub(crate) fn default_cipher_suites() -> Vec<Box<dyn CipherSuite>> {
447 vec![
448 Box::new(CipherSuiteAes128GcmSha256::new(false)),
449 Box::new(CipherSuiteAes128GcmSha256::new(true)),
450 Box::new(CipherSuiteChaCha20Poly1305Sha256::new(false)),
451 Box::new(CipherSuiteAes256CbcSha::new(false)),
452 Box::new(CipherSuiteAes256CbcSha::new(true)),
453 ]
454}
455
456fn all_cipher_suites() -> Vec<Box<dyn CipherSuite>> {
457 vec![
458 Box::new(new_cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm()),
459 Box::new(new_cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm8()),
460 Box::new(CipherSuiteAes128GcmSha256::new(false)),
461 Box::new(CipherSuiteAes128GcmSha256::new(true)),
462 Box::new(CipherSuiteAes256CbcSha::new(false)),
463 Box::new(CipherSuiteAes256CbcSha::new(true)),
464 Box::new(new_cipher_suite_tls_psk_with_aes_128_ccm()),
465 Box::new(new_cipher_suite_tls_psk_with_aes_128_ccm8()),
466 Box::<CipherSuiteTlsPskWithAes128GcmSha256>::default(),
467 Box::new(CipherSuiteChaCha20Poly1305Sha256::new(false)),
468 Box::new(CipherSuiteChaCha20Poly1305Sha256::new(true)),
469 ]
470}
471
472fn cipher_suites_for_ids(ids: &[CipherSuiteId]) -> Result<Vec<Box<dyn CipherSuite>>> {
473 let mut cipher_suites = vec![];
474 for id in ids {
475 cipher_suites.push(cipher_suite_for_id(*id)?);
476 }
477 Ok(cipher_suites)
478}
479
480pub(crate) fn parse_cipher_suites(
481 user_selected_suites: &[CipherSuiteId],
482 exclude_psk: bool,
483 exclude_non_psk: bool,
484) -> Result<Vec<Box<dyn CipherSuite>>> {
485 let cipher_suites = if !user_selected_suites.is_empty() {
486 cipher_suites_for_ids(user_selected_suites)?
487 } else {
488 default_cipher_suites()
489 };
490
491 let filtered_cipher_suites: Vec<Box<dyn CipherSuite>> = cipher_suites
492 .into_iter()
493 .filter(|c| !((exclude_psk && c.is_psk()) || (exclude_non_psk && !c.is_psk())))
494 .collect();
495
496 if filtered_cipher_suites.is_empty() {
497 Err(Error::ErrNoAvailableCipherSuites)
498 } else {
499 Ok(filtered_cipher_suites)
500 }
501}