Skip to main content

rtci_dtls/cipher_suite/
mod.rs

1/// Shared implementation for the AES-128-CCM suites.
2pub mod cipher_suite_aes_128_ccm;
3/// Shared implementation for the AES-128-GCM-SHA256 suites.
4pub mod cipher_suite_aes_128_gcm_sha256;
5/// Shared implementation for the AES-256-CBC-SHA suites.
6pub mod cipher_suite_aes_256_cbc_sha;
7/// Shared implementation for the ChaCha20-Poly1305-SHA256 suites.
8pub mod cipher_suite_chacha20_poly1305_sha256;
9/// An ECDHE-ECDSA suite with AES-128, the pairing WebRTC normally negotiates.
10/// An ECDHE-ECDSA suite with AES-128-CCM and a truncated 8-byte tag.
11pub mod cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm;
12/// An ECDHE-ECDSA suite with AES-128-CCM and a full 16-byte tag.
13pub mod cipher_suite_tls_ecdhe_ecdsa_with_aes_128_ccm8;
14/// `TLS_PSK_WITH_AES_128_CCM`, for pre-shared-key handshakes.
15pub mod cipher_suite_tls_psk_with_aes_128_ccm;
16/// `TLS_PSK_WITH_AES_128_CCM_8`, with a truncated 8-byte tag.
17pub mod cipher_suite_tls_psk_with_aes_128_ccm8;
18/// `TLS_PSK_WITH_AES_128_GCM_SHA256`, for pre-shared-key handshakes.
19pub 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// CipherSuiteID is an ID for our supported CipherSuites
42// Supported Cipher Suites
43#[allow(non_camel_case_types)]
44#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45/// The cipher suites this crate can negotiate, by their IANA code points.
46#[non_exhaustive]
47pub enum CipherSuiteId {
48    // AES-128-CCM
49    /// `TLS_ECDHE_ECDSA_WITH_AES_128_CCM` (`0xc0ac`).
50    Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm = 0xc0ac,
51    /// `TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8` (`0xc0ae`).
52    Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8 = 0xc0ae,
53
54    // AES-128-GCM-SHA256
55    /// `TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256` (`0xc02b`).
56    Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256 = 0xc02b,
57    /// `TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256` (`0xc02f`).
58    Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256 = 0xc02f,
59
60    // AES-256-CBC-SHA
61    /// `TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA` (`0xc00a`).
62    Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha = 0xc00a,
63    /// `TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA` (`0xc014`).
64    Tls_Ecdhe_Rsa_With_Aes_256_Cbc_Sha = 0xc014,
65
66    /// `TLS_PSK_WITH_AES_128_CCM` (`0xc0a4`).
67    Tls_Psk_With_Aes_128_Ccm = 0xc0a4,
68    /// `TLS_PSK_WITH_AES_128_CCM_8` (`0xc0a8`).
69    Tls_Psk_With_Aes_128_Ccm_8 = 0xc0a8,
70    /// `TLS_PSK_WITH_AES_128_GCM_SHA256` (`0x00a8`).
71    Tls_Psk_With_Aes_128_Gcm_Sha256 = 0x00a8,
72
73    // CHACHA20_POLY1305_SHA256
74    /// `TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256` (`0xcca8`).
75    Tls_Ecdhe_Rsa_With_ChaCha20_Poly1305_Sha256 = 0xcca8,
76    /// `TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256` (`0xcca9`).
77    Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256 = 0xcca9,
78
79    // TLS 1.3 suites (RFC 8446 appendix B.4), negotiated with DTLS 1.3 only.
80    /// `TLS_AES_128_GCM_SHA256` (`0x1301`), DTLS 1.3.
81    Tls_Aes_128_Gcm_Sha256 = 0x1301,
82    /// `TLS_AES_256_GCM_SHA384` (`0x1302`), DTLS 1.3.
83    Tls_Aes_256_Gcm_Sha384 = 0x1302,
84    /// `TLS_CHACHA20_POLY1305_SHA256` (`0x1303`), DTLS 1.3.
85    Tls_ChaCha20_Poly1305_Sha256 = 0x1303,
86    /// `TLS_AES_128_CCM_SHA256` (`0x1304`), DTLS 1.3. Not in the default list.
87    Tls_Aes_128_Ccm_Sha256 = 0x1304,
88    /// `TLS_AES_128_CCM_8_SHA256` (`0x1305`), DTLS 1.3. Never in the default list: RFC 9147
89    /// section 4.5.3 forbids it in DTLS without further protection against forgery, so it is
90    /// negotiated only when a configuration names it, and then under a small limit of
91    /// records that may fail authentication.
92    Tls_Aes_128_Ccm_8_Sha256 = 0x1305,
93
94    /// `TLS_EMPTY_RENEGOTIATION_INFO_SCSV` (`0x00ff`): not a cipher suite. A client lists it
95    /// to signal secure renegotiation without the renegotiation_info extension (RFC 5746 3.3).
96    /// It is recognised in a ClientHello and never negotiated or offered.
97    Tls_Empty_Renegotiation_Info_Scsv = 0x00ff,
98
99    /// A code point this crate does not implement.
100    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            // AES-128-CCM
157            0xc0ac => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm,
158            0xc0ae => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Ccm_8,
159
160            // AES-128-GCM-SHA256
161            0xc02b => CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256,
162            0xc02f => CipherSuiteId::Tls_Ecdhe_Rsa_With_Aes_128_Gcm_Sha256,
163
164            // AES-256-CBC-SHA
165            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            // CHACHA20_POLY1305_SHA256
173            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    /// Whether this is a TLS 1.3 suite, which DTLS 1.3 negotiates and DTLS 1.2 cannot.
229    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/// The hash a suite uses in its PRF and `Finished` computation.
275#[non_exhaustive]
276pub enum CipherSuiteHash {
277    /// SHA-256.
278    Sha256,
279    /// SHA-384 (TLS 1.3 `_SHA384` suites).
280    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
306/// A negotiated cipher suite: its identity, and the record encryption it performs once keys
307/// are installed.
308pub trait CipherSuite: Send {
309    /// The suite's IANA name.
310    fn to_string(&self) -> String;
311    /// The suite's code point.
312    fn id(&self) -> CipherSuiteId;
313    /// The certificate type this suite requires of a peer.
314    fn certificate_type(&self) -> ClientCertificateType;
315    /// The hash used for the PRF and `Finished`.
316    fn hash_func(&self) -> CipherSuiteHash;
317    /// Whether this suite authenticates with a pre-shared key rather than certificates.
318    fn is_psk(&self) -> bool;
319    /// Whether keys have been installed, so records can be protected.
320    fn is_initialized(&self) -> bool;
321    /// The most bytes this suite's record protection adds to a record's payload (explicit
322    /// nonce or IV, authentication tag or MAC, padding), on top of the 13-byte record header.
323    /// An application sizing its datagrams for a path MTU subtracts both (RFC 6347 4.1.1.1).
324    fn record_expansion(&self) -> usize;
325
326    // Generate the internal encryption state
327    /// Installs the keying material derived from the handshake.
328    ///
329    /// # Errors
330    ///
331    /// Fails if the key or salt lengths do not match what this suite expects.
332    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    /// Protects one record, returning the encrypted record including its header.
342    ///
343    /// # Errors
344    ///
345    /// Fails if keys are not installed, or the cipher rejects the input.
346    fn encrypt(&mut self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>>;
347    /// Unprotects one record.
348    ///
349    /// # Errors
350    ///
351    /// Fails if authentication fails, or the record is malformed.
352    fn decrypt(&mut self, input: &[u8]) -> Result<Vec<u8>>;
353
354    /// [`Self::encrypt`] for a `tls12_cid` record (RFC 9146): `pkt_rlh` and `raw` have the
355    /// content type `tls12_cid` and the DTLSInnerPlaintext, without the connection id,
356    /// which the additional data (or MAC) covers. The result has the 13-byte header too;
357    /// the caller puts the connection id into it.
358    ///
359    /// # Errors
360    ///
361    /// Fails as [`Self::encrypt`] does, and for a suite without `tls12_cid` records.
362    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    /// [`Self::decrypt`] for a `tls12_cid` record (RFC 9146), given without its connection
374    /// id; the result is the header and the DTLSInnerPlaintext.
375    ///
376    /// # Errors
377    ///
378    /// Fails as [`Self::decrypt`] does, and for a suite without `tls12_cid` records.
379    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
390// Taken from https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
391// A cipher_suite is a specific combination of key agreement, cipher and MAC
392// function.
393/// Builds the [`CipherSuite`] implementation for `id`.
394///
395/// # Errors
396///
397/// Fails if the id is not one this crate implements.
398pub 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
440/// The DTLS 1.2 suites this crate offers and takes when none are configured, in order of
441/// preference: the forward-secret AEAD suites first, then the CBC ones. RFC 8446 appendix
442/// D.5: "Implementations negotiating the use of older versions of TLS SHOULD prefer forward
443/// secret and AEAD cipher suites, when available." BoringSSL orders them the same way (ECDHE
444/// AES-GCM and ChaCha20-Poly1305, then the "legacy" CBC suites: `ssl_create_cipher_list` in
445/// `ssl/ssl_cipher.cc`, revision 289645a4).
446pub(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}