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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use openmls_traits::types::{Ciphersuite, VerifiableCiphersuite};
use serde::{Deserialize, Serialize};
use tls_codec::{TlsDeserialize, TlsDeserializeBytes, TlsSerialize, TlsSize};
#[cfg(doc)]
use super::LeafNode;
use crate::{
credentials::CredentialType,
extensions::{
Extension, ExtensionType, ExtensionValidator, Extensions, RequiredCapabilitiesExtension,
},
messages::proposals::ProposalType,
treesync::errors::LeafNodeValidationError,
versions::ProtocolVersion,
};
/// Capabilities of [`LeafNode`]s.
///
/// ```text
/// struct {
/// ProtocolVersion versions<V>;
/// CipherSuite ciphersuites<V>;
/// ExtensionType extensions<V>;
/// ProposalType proposals<V>;
/// CredentialType credentials<V>;
/// } Capabilities;
/// ```
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
TlsSerialize,
TlsDeserialize,
TlsDeserializeBytes,
TlsSize,
)]
pub struct Capabilities {
pub(super) versions: Vec<ProtocolVersion>,
pub(super) ciphersuites: Vec<VerifiableCiphersuite>,
pub(super) extensions: Vec<ExtensionType>,
pub(super) proposals: Vec<ProposalType>,
pub(super) credentials: Vec<CredentialType>,
}
impl Capabilities {
/// Create a new [`Capabilities`] struct with the given configuration.
/// Any argument that is `None` is filled with the default values from the
/// global configuration.
// TODO(#1232)
pub fn new(
versions: Option<&[ProtocolVersion]>,
ciphersuites: Option<&[Ciphersuite]>,
extensions: Option<&[ExtensionType]>,
proposals: Option<&[ProposalType]>,
credentials: Option<&[CredentialType]>,
) -> Self {
Self {
versions: match versions {
Some(v) => v.into(),
None => default_versions(),
},
ciphersuites: match ciphersuites {
Some(c) => c.iter().map(|c| VerifiableCiphersuite::from(*c)).collect(),
None => default_ciphersuites()
.into_iter()
.map(VerifiableCiphersuite::from)
.collect(),
},
extensions: match extensions {
Some(e) => e.into(),
None => vec![],
},
proposals: match proposals {
Some(p) => p.into(),
None => vec![],
},
credentials: match credentials {
Some(c) => c.into(),
None => default_credentials(),
},
}
}
/// Create new empty [`Capabilities`].
pub fn empty() -> Self {
Self {
versions: Vec::new(),
ciphersuites: Vec::new(),
extensions: Vec::new(),
proposals: Vec::new(),
credentials: Vec::new(),
}
}
/// Creates a new [`CapabilitiesBuilder`] for constructing [`Capabilities`]
pub fn builder() -> CapabilitiesBuilder {
CapabilitiesBuilder(Self::default())
}
// ---------------------------------------------------------------------------------------------
/// Get a reference to the list of versions in this extension.
pub fn versions(&self) -> &[ProtocolVersion] {
&self.versions
}
/// Get a reference to the list of ciphersuites in this extension.
pub fn ciphersuites(&self) -> &[VerifiableCiphersuite] {
&self.ciphersuites
}
/// Get a reference to the list of supported extensions.
pub fn extensions(&self) -> &[ExtensionType] {
&self.extensions
}
/// Get a reference to the list of supported proposals.
pub fn proposals(&self) -> &[ProposalType] {
&self.proposals
}
/// Get a reference to the list of supported credential types.
pub fn credentials(&self) -> &[CredentialType] {
&self.credentials
}
// ---------------------------------------------------------------------------------------------
/// Check if these [`Capabilities`] support all the capabilities required by
/// the given [`RequiredCapabilitiesExtension`].
///
/// # Errors
///
/// Returns a [`LeafNodeValidationError`] error if any of the required
/// capabilities is not supported.
pub(crate) fn supports_required_capabilities(
&self,
required_capabilities: &RequiredCapabilitiesExtension,
) -> Result<(), LeafNodeValidationError> {
// Check if all required extensions are supported.
let unsupported_extension_types = required_capabilities
.extension_types()
.iter()
.filter(|&e| !self.contains_extension(*e))
.collect::<Vec<_>>();
if !unsupported_extension_types.is_empty() {
log::error!(
"Leaf node does not support all required extension types\n
Supported extensions: {:?}\n
Required extensions: {:?}",
self.extensions(),
required_capabilities.extension_types()
);
return Err(LeafNodeValidationError::UnsupportedExtensions);
}
// Check if all required proposals are supported.
if required_capabilities
.proposal_types()
.iter()
.any(|p| !self.contains_proposal(*p))
{
return Err(LeafNodeValidationError::UnsupportedProposals);
}
// Check if all required credential types are supported.
if required_capabilities
.credential_types()
.iter()
.any(|c| !self.contains_credential(*c))
{
return Err(LeafNodeValidationError::UnsupportedCredentials);
}
Ok(())
}
/// Check if these [`Capabilities`] contain all the extensions.
pub(crate) fn contains_extensions(
&self,
extensions: &Extensions<impl ExtensionValidator>,
) -> bool {
extensions
.iter()
.map(Extension::extension_type)
.all(|e| e.is_default() || self.extensions().contains(&e))
}
/// Check if these [`Capabilities`] contains the credential.
pub(crate) fn contains_credential(&self, credential_type: CredentialType) -> bool {
self.credentials().contains(&credential_type)
}
/// Check if these [`Capabilities`] contain the extension.
pub(crate) fn contains_extension(&self, extension_type: ExtensionType) -> bool {
extension_type.is_default() || self.extensions().contains(&extension_type)
}
/// Check if these [`Capabilities`] contain the proposal.
pub(crate) fn contains_proposal(&self, proposal_type: ProposalType) -> bool {
proposal_type.is_default() || self.proposals().contains(&proposal_type)
}
/// Check if these [`Capabilities`] contain the version.
pub(crate) fn contains_version(&self, version: ProtocolVersion) -> bool {
self.versions().contains(&version)
}
/// Check if these [`Capabilities`] contain the ciphersuite.
pub(crate) fn contains_ciphersuite(&self, ciphersuite: VerifiableCiphersuite) -> bool {
self.ciphersuites().contains(&ciphersuite)
}
/// Add random GREASE values to the capabilities to ensure extensibility.
///
/// This adds one random GREASE value to each capability list if no GREASE
/// value is already present:
/// - Ciphersuites
/// - Extensions
/// - Proposals
/// - Credentials
///
/// GREASE values are used per [RFC 9420 Section 13.5](https://www.rfc-editor.org/rfc/rfc9420.html#section-13.5)
/// to help prevent extensibility failures by ensuring implementations properly
/// handle unknown values.
///
/// # Example
///
/// ```
/// use openmls::prelude::*;
/// use openmls_rust_crypto::OpenMlsRustCrypto;
///
/// let provider = OpenMlsRustCrypto::default();
///
/// // Create capabilities with GREASE values injected
/// let capabilities = Capabilities::builder()
/// .build()
/// .with_grease(provider.rand());
///
/// // Verify GREASE values were added
/// assert!(capabilities.ciphersuites().iter().any(|cs| cs.is_grease()));
/// assert!(capabilities.extensions().iter().any(|ext| ext.is_grease()));
/// assert!(capabilities.proposals().iter().any(|prop| prop.is_grease()));
/// assert!(capabilities.credentials().iter().any(|cred| cred.is_grease()));
/// ```
pub fn with_grease(mut self, rand: &impl openmls_traits::random::OpenMlsRand) -> Self {
use crate::credentials::CredentialType;
use crate::extensions::ExtensionType;
use crate::messages::proposals::ProposalType;
use openmls_traits::types::VerifiableCiphersuite;
// Add GREASE ciphersuite if none present
if !self.ciphersuites.iter().any(|cs| cs.is_grease()) {
let grease_cs = VerifiableCiphersuite::new(crate::grease::random_grease_value(rand));
self.ciphersuites.push(grease_cs);
}
// Add GREASE extension if none present
if !self.extensions.iter().any(|ext| ext.is_grease()) {
let grease_ext = ExtensionType::Grease(crate::grease::random_grease_value(rand));
self.extensions.push(grease_ext);
}
// Add GREASE proposal if none present
if !self.proposals.iter().any(|prop| prop.is_grease()) {
let grease_prop = ProposalType::Grease(crate::grease::random_grease_value(rand));
self.proposals.push(grease_prop);
}
// Add GREASE credential if none present
if !self.credentials.iter().any(|cred| cred.is_grease()) {
let grease_cred = CredentialType::Grease(crate::grease::random_grease_value(rand));
self.credentials.push(grease_cred);
}
self
}
}
/// A helper for building [`Capabilities`]
#[derive(Debug, Clone)]
pub struct CapabilitiesBuilder(Capabilities);
impl CapabilitiesBuilder {
/// Sets the `versions` field on the [`Capabilities`].
pub fn versions(self, versions: Vec<ProtocolVersion>) -> Self {
Self(Capabilities { versions, ..self.0 })
}
/// Sets the `ciphersuites` field on the [`Capabilities`].
pub fn ciphersuites(self, ciphersuites: Vec<Ciphersuite>) -> Self {
let ciphersuites = ciphersuites.into_iter().map(|cs| cs.into()).collect();
Self(Capabilities {
ciphersuites,
..self.0
})
}
/// Sets the `extensions` field on the [`Capabilities`].
pub fn extensions(self, extensions: Vec<ExtensionType>) -> Self {
Self(Capabilities {
extensions,
..self.0
})
}
/// Sets the `proposals` field on the [`Capabilities`].
pub fn proposals(self, proposals: Vec<ProposalType>) -> Self {
Self(Capabilities {
proposals,
..self.0
})
}
/// Sets the `credentials` field on the [`Capabilities`].
pub fn credentials(self, credentials: Vec<CredentialType>) -> Self {
Self(Capabilities {
credentials,
..self.0
})
}
/// Adds random GREASE values to the capabilities being built.
///
/// This is a convenience method that calls [`Capabilities::with_grease`] on the
/// built capabilities. See that method for more details.
///
/// # Example
///
/// ```
/// use openmls::prelude::*;
/// use openmls_rust_crypto::OpenMlsRustCrypto;
///
/// let provider = OpenMlsRustCrypto::default();
///
/// let capabilities = Capabilities::builder()
/// .with_grease(provider.rand())
/// .build();
///
/// // GREASE values were added
/// assert!(capabilities.ciphersuites().iter().any(|cs| cs.is_grease()));
/// ```
pub fn with_grease(self, rand: &impl openmls_traits::random::OpenMlsRand) -> Self {
Self(self.0.with_grease(rand))
}
/// Builds the [`Capabilities`].
pub fn build(self) -> Capabilities {
self.0
}
}
#[cfg(test)]
impl Capabilities {
/// Set the versions list.
pub fn set_versions(&mut self, versions: Vec<ProtocolVersion>) {
self.versions = versions;
}
/// Set the ciphersuites list.
pub fn set_ciphersuites(&mut self, ciphersuites: Vec<VerifiableCiphersuite>) {
self.ciphersuites = ciphersuites;
}
}
impl Default for Capabilities {
fn default() -> Self {
Capabilities {
versions: default_versions(),
ciphersuites: default_ciphersuites()
.into_iter()
.map(VerifiableCiphersuite::from)
.collect(),
extensions: vec![],
proposals: vec![],
credentials: default_credentials(),
}
}
}
pub(super) fn default_versions() -> Vec<ProtocolVersion> {
vec![ProtocolVersion::Mls10]
}
pub(super) fn default_ciphersuites() -> Vec<Ciphersuite> {
vec![
Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519,
Ciphersuite::MLS_128_DHKEMP256_AES128GCM_SHA256_P256,
Ciphersuite::MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519,
Ciphersuite::MLS_256_XWING_CHACHA20POLY1305_SHA256_Ed25519,
]
}
// TODO(#1231)
pub(super) fn default_credentials() -> Vec<CredentialType> {
vec![CredentialType::Basic]
}
#[cfg(test)]
mod tests {
use openmls_traits::types::{Ciphersuite, VerifiableCiphersuite};
use tls_codec::{Deserialize, Serialize};
use super::Capabilities;
use crate::{
credentials::CredentialType, messages::proposals::ProposalType, prelude::ExtensionType,
versions::ProtocolVersion,
};
#[test]
fn that_unknown_capabilities_are_de_serialized_correctly() {
let versions = vec![ProtocolVersion::Mls10, ProtocolVersion::Other(999)];
let ciphersuites = vec![
Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519.into(),
Ciphersuite::MLS_128_DHKEMP256_AES128GCM_SHA256_P256.into(),
Ciphersuite::MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519.into(),
Ciphersuite::MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448.into(),
Ciphersuite::MLS_256_DHKEMP521_AES256GCM_SHA512_P521.into(),
Ciphersuite::MLS_256_DHKEMX448_CHACHA20POLY1305_SHA512_Ed448.into(),
Ciphersuite::MLS_256_DHKEMP384_AES256GCM_SHA384_P384.into(),
VerifiableCiphersuite::new(0x0000),
// Use non-GREASE values (GREASE pattern is 0x_A_A)
VerifiableCiphersuite::new(0x0B0B),
VerifiableCiphersuite::new(0x7C7C),
VerifiableCiphersuite::new(0xF000),
VerifiableCiphersuite::new(0xFFFF),
];
let extensions = vec![
ExtensionType::Unknown(0x0000),
ExtensionType::Unknown(0xFAFA),
];
// Use non-GREASE values
let proposals = vec![ProposalType::Custom(0x7C7C)];
let credentials = vec![
CredentialType::Basic,
CredentialType::X509,
CredentialType::Other(0x0000),
// Use non-GREASE values
CredentialType::Other(0x7C7C),
CredentialType::Other(0xFFFF),
];
let expected = Capabilities {
versions,
ciphersuites,
extensions,
proposals,
credentials,
};
let test_serialized = expected.tls_serialize_detached().unwrap();
let got = Capabilities::tls_deserialize_exact(test_serialized).unwrap();
assert_eq!(expected, got);
}
}