core_crypto/mls/credential/
mod.rs1pub(crate) mod credential_ref;
6pub(crate) mod credential_type;
7pub(crate) mod crl;
8mod error;
9pub(crate) mod ext;
10mod persistence;
11pub(crate) mod x509;
12
13use core_crypto_keystore::entities::StoredCredential;
14use openmls::prelude::{Credential as MlsCredential, CredentialWithKey, SignatureScheme};
15use openmls_basic_credential::SignatureKeyPair;
16use openmls_traits::crypto::OpenMlsCrypto;
17use tls_codec::Deserialize as _;
18
19pub(crate) use self::error::Result;
20pub use self::{
21 credential_ref::{CredentialRef, FindFilters, FindFiltersBuilder},
22 credential_type::CredentialType,
23 error::Error,
24};
25use crate::{Ciphersuite, ClientId, ClientIdRef, ClientIdentifier, MlsError, RecursiveError, mls_provider::CRYPTO};
26
27#[derive(core_crypto_macros::Debug, Clone, serde::Serialize, serde::Deserialize)]
35pub struct Credential {
36 pub(crate) ciphersuite: Ciphersuite,
38 pub(crate) credential_type: CredentialType,
40 pub(crate) mls_credential: MlsCredential,
42 #[sensitive]
44 pub(crate) signature_key_pair: SignatureKeyPair,
45 pub(crate) earliest_validity: u64,
52}
53
54impl TryFrom<&StoredCredential> for Credential {
55 type Error = Error;
56
57 fn try_from(stored_credential: &StoredCredential) -> Result<Credential> {
58 let mls_credential = MlsCredential::tls_deserialize(&mut stored_credential.credential.as_slice())
59 .map_err(Error::tls_deserialize("mls credential"))?;
60 let ciphersuite = Ciphersuite::try_from(stored_credential.ciphersuite)
61 .map_err(RecursiveError::mls("loading ciphersuite from db"))?;
62 let signature_key_pair = openmls_basic_credential::SignatureKeyPair::from_raw(
63 ciphersuite.signature_algorithm(),
64 stored_credential.private_key.to_owned(),
65 stored_credential.public_key.to_owned(),
66 );
67 let credential_type = mls_credential
68 .credential_type()
69 .try_into()
70 .map_err(RecursiveError::mls_credential("loading credential from db"))?;
71 let earliest_validity = stored_credential.created_at;
72 Ok(Credential {
73 ciphersuite,
74 signature_key_pair,
75 credential_type,
76 mls_credential,
77 earliest_validity,
78 })
79 }
80}
81
82impl Credential {
83 pub fn basic(ciphersuite: Ciphersuite, client_id: ClientId) -> Result<Self> {
90 let signature_scheme = ciphersuite.signature_algorithm();
91 let (private_key, public_key) = CRYPTO
92 .signature_key_gen(signature_scheme)
93 .map_err(MlsError::wrap("generating signature key"))?;
94 let signature_key_pair = SignatureKeyPair::from_raw(signature_scheme, private_key, public_key);
95
96 Ok(Self {
97 ciphersuite,
98 credential_type: CredentialType::Basic,
99 mls_credential: MlsCredential::new_basic(client_id.into_inner()),
100 signature_key_pair,
101 earliest_validity: 0,
102 })
103 }
104
105 pub fn mls_credential(&self) -> &MlsCredential {
109 &self.mls_credential
110 }
111
112 pub fn credential_type(&self) -> CredentialType {
114 self.credential_type
115 }
116
117 pub(crate) fn signature_key(&self) -> &SignatureKeyPair {
119 &self.signature_key_pair
120 }
121
122 pub fn signature_scheme(&self) -> SignatureScheme {
124 self.signature_key_pair.signature_scheme()
125 }
126
127 pub fn ciphersuite(&self) -> Ciphersuite {
129 self.ciphersuite
130 }
131
132 pub fn to_mls_credential_with_key(&self) -> CredentialWithKey {
134 CredentialWithKey {
135 credential: self.mls_credential.clone(),
136 signature_key: self.signature_key_pair.to_public_vec().into(),
137 }
138 }
139
140 pub fn earliest_validity(&self) -> u64 {
148 self.earliest_validity
149 }
150
151 pub fn client_id(&self) -> &ClientIdRef {
153 self.mls_credential.identity().into()
154 }
155}
156
157impl Credential {
158 #[cfg_attr(not(test), expect(dead_code))]
161 pub(crate) fn from_identifier(identifier: &ClientIdentifier, ciphersuite: Ciphersuite) -> Result<Self> {
162 match identifier {
163 ClientIdentifier::Basic(client_id) => Self::basic(ciphersuite, client_id.clone()),
164 ClientIdentifier::X509(certs) => {
165 let signature_scheme = ciphersuite.signature_algorithm();
166 let cert = certs
167 .get(&signature_scheme)
168 .ok_or(Error::SignatureSchemeNotPresentInX509Identity(signature_scheme))?;
169 Self::x509(ciphersuite, cert.clone())
170 }
171 }
172 }
173}
174
175impl From<Credential> for CredentialWithKey {
176 fn from(cb: Credential) -> Self {
177 Self {
178 credential: cb.mls_credential,
179 signature_key: cb.signature_key_pair.public().into(),
180 }
181 }
182}
183
184impl Eq for Credential {}
185impl PartialEq for Credential {
186 fn eq(&self, other: &Self) -> bool {
187 self.mls_credential == other.mls_credential && self.earliest_validity == other.earliest_validity && {
188 let sk = &self.signature_key_pair;
189 let ok = &other.signature_key_pair;
190 sk.signature_scheme() == ok.signature_scheme() && sk.public() == ok.public()
191 }
193 }
194}
195
196#[cfg(test)]
199mod tests {
200 use std::collections::HashMap;
201
202 use super::{x509::CertificateBundle, *};
203 use crate::{
204 ClientIdentifier, CredentialType, E2eiConversationState,
205 mls::{conversation::Conversation as _, credential::x509::CertificatePrivateKey},
206 mls_provider::PkiKeypair,
207 test_utils::{
208 x509::{CertificateParams, X509TestChain},
209 *,
210 },
211 };
212
213 #[apply(all_cred_cipher)]
214 async fn basic_clients_can_send_messages(case: TestContext) {
215 if !case.is_basic() {
216 return;
217 }
218 let [alice, bob] = case.sessions_basic().await;
219 let conversation = case.create_conversation([&alice, &bob]).await;
220 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
221 }
222
223 #[apply(all_cred_cipher)]
224 async fn certificate_clients_can_send_messages(case: TestContext) {
225 if !case.is_x509() {
226 return;
227 }
228 let [alice, bob] = case.sessions_x509().await;
229 let conversation = case.create_conversation([&alice, &bob]).await;
230 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
231 }
232
233 #[apply(all_cred_cipher)]
234 async fn heterogeneous_clients_can_send_messages(case: TestContext) {
235 let ([x509_session], [basic_session]) = case.sessions_mixed_credential_types().await;
237
238 let (alice, bob) = match case.credential_type {
240 CredentialType::Basic => (x509_session, basic_session),
241 CredentialType::X509 => (basic_session, x509_session),
242 };
243
244 let conversation = case.create_conversation([&alice, &bob]).await;
245 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
246 }
247
248 #[apply(all_cred_cipher)]
249 async fn should_fail_when_certificate_chain_is_empty(case: TestContext) {
250 let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
251
252 let x509_intermediate = x509_test_chain.find_local_intermediate_ca();
253
254 let mut certs = CertificateBundle::rand(&"alice".into(), x509_intermediate);
255 certs.certificate_chain = vec![];
256 let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), certs)]));
257
258 let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
259 .await
260 .unwrap_err();
261 assert!(innermost_source_matches!(err, Error::InvalidIdentity));
262 }
263
264 #[apply(all_cred_cipher)]
265 async fn should_fail_when_signature_key_doesnt_match_certificate_public_key(case: TestContext) {
266 if !case.is_x509() {
267 return;
268 }
269 let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
270 let x509_intermediate = x509_test_chain.find_local_intermediate_ca();
271
272 let certs = CertificateBundle::rand(&"alice".into(), x509_intermediate);
273 let new_pki_kp = PkiKeypair::rand(case.signature_scheme(), CRYPTO.as_ref()).unwrap();
274
275 let eve_key = CertificatePrivateKey::new(new_pki_kp.signing_key_bytes());
276 let cb = CertificateBundle {
277 certificate_chain: certs.certificate_chain,
278 private_key: eve_key,
279 signature_scheme: case.ciphersuite().signature_algorithm(),
280 };
281 let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), cb)]));
282
283 let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
284 .await
285 .unwrap_err();
286 assert!(innermost_source_matches!(
287 err,
288 crate::MlsErrorKind::MlsCryptoError(openmls::prelude::CryptoError::MismatchKeypair),
289 ));
290 }
291
292 #[apply(all_cred_cipher)]
293 async fn should_not_fail_but_degrade_when_certificate_expired(case: TestContext) {
294 if !case.is_x509() {
295 return;
296 }
297 Box::pin(async move {
298 let mut x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
299
300 let expiration_time = core::time::Duration::from_secs(14);
301 let start = web_time::Instant::now();
302
303 let (alice_identifier, _) = x509_test_chain.issue_simple_certificate_bundle("alice", None);
304 let (bob_identifier, _) = x509_test_chain.issue_simple_certificate_bundle("bob", Some(expiration_time));
305 let alice = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
306 .await
307 .unwrap();
308 let bob = SessionContext::new_with_identifier(&case, bob_identifier, Some(&x509_test_chain))
309 .await
310 .unwrap();
311
312 let conversation = case.create_conversation([&alice, &bob]).await;
313 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
315
316 assert_eq!(
317 conversation.guard().await.e2ei_conversation_state().await.unwrap(),
318 E2eiConversationState::Verified
319 );
320
321 let elapsed = start.elapsed();
322 if expiration_time > elapsed {
324 smol::Timer::after(expiration_time - elapsed + core::time::Duration::from_secs(2)).await;
325 }
326
327 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
328 assert_eq!(
329 conversation.guard().await.e2ei_conversation_state().await.unwrap(),
330 E2eiConversationState::NotVerified
331 );
332 })
333 .await;
334 }
335
336 #[apply(all_cred_cipher)]
337 async fn should_not_fail_but_degrade_when_basic_joins(case: TestContext) {
338 if !case.is_x509() {
339 return;
340 }
341 Box::pin(async {
342 let ([alice, bob], [charlie]) = case.sessions_mixed_credential_types().await;
343 let conversation = case.create_conversation([&alice, &bob]).await;
344
345 assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
347 assert_eq!(
348 conversation.guard().await.e2ei_conversation_state().await.unwrap(),
349 E2eiConversationState::Verified
350 );
351 assert_eq!(
352 conversation
353 .guard_of(&bob)
354 .await
355 .e2ei_conversation_state()
356 .await
357 .unwrap(),
358 E2eiConversationState::Verified
359 );
360
361 let conversation = conversation
363 .invite_with_credential_notify([(&charlie, &charlie.initial_credential)])
364 .await;
365
366 assert_eq!(
367 conversation.guard().await.e2ei_conversation_state().await.unwrap(),
368 E2eiConversationState::NotVerified
369 );
370 assert!(conversation.is_functional_and_contains([&alice, &bob, &charlie]).await);
371 assert_eq!(
372 conversation.guard().await.e2ei_conversation_state().await.unwrap(),
373 E2eiConversationState::NotVerified
374 );
375 })
376 .await;
377 }
378
379 #[apply(all_cred_cipher)]
380 async fn should_fail_when_certificate_not_valid_yet(case: TestContext) {
381 use crate::MlsErrorKind;
382
383 if !case.is_x509() {
384 return;
385 }
386 let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
387
388 let tomorrow = now_std() + core::time::Duration::from_secs(3600 * 24);
389 let local_ca = x509_test_chain.find_local_intermediate_ca();
390 let alice_cert = {
391 let name = "alice";
392 let common_name = format!("{name} Smith");
393 let handle = format!("{}_wire", name.to_lowercase());
394 let client_id: String = crate::test_utils::x509::qualified_e2ei_cid_with_domain("wire.com")
395 .try_into()
396 .unwrap();
397 local_ca.create_and_sign_end_identity(CertificateParams {
398 common_name: Some(common_name.clone()),
399 handle: Some(handle.clone()),
400 client_id: Some(client_id.clone()),
401 validity_start: Some(tomorrow),
402 ..Default::default()
403 })
404 };
405 let cb = CertificateBundle::from_certificate_and_issuer(&alice_cert, local_ca);
406 let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), cb)]));
407 let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
408 .await
409 .unwrap_err();
410
411 assert!(innermost_source_matches!(
412 err,
413 MlsErrorKind::MlsCryptoError(openmls::prelude::CryptoError::ExpiredCertificate),
414 ))
415 }
416
417 pub(crate) fn now_std() -> std::time::Duration {
423 let now = web_time::SystemTime::now();
424 now.duration_since(web_time::UNIX_EPOCH).unwrap()
425 }
426}