core_crypto/mls/credential/
mod.rs

1//! This module focuses on [`Credential`]s: cryptographic assertions of identity.
2//!
3//! Credentials can be basic, or based on an x509 certificate chain.
4
5pub(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, MlsCredentialType, 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::{
26    Ciphersuite, ClientId, ClientIdRef, ClientIdentifier, MlsError, RecursiveError,
27    mls::credential::{error::CredentialValidationError, ext::CredentialExt as _},
28};
29
30/// A cryptographic credential.
31///
32/// This is tied to a particular client via either its client id or certificate bundle,
33/// depending on its credential type, but is independent of any client instance or storage.
34///
35/// To attach to a particular client instance and store, see
36/// [`TransactionContext::add_credential`][crate::transaction_context::TransactionContext::add_credential].
37///
38/// Note: the current database design makes some questionable assumptions:
39///
40/// - There are always either 0 or 1 `StoredSignatureKeypair` instances in the DB for a particular signature scheme
41/// - There may be multiple `StoredCredential` instances in the DB for a particular signature scheme, but they all share
42///   the same `ClientId` / signing key. In other words, the same signing keypair is _reused_ between credentials.
43/// - Practically, the code ensures that there is a 1:1 correspondence between signing scheme <-> identity/credential,
44///   and we need to maintain that property for now.
45///
46/// Work is ongoing to fix those limitations; see WPB-20844. Until that is resolved, we enforce those restrictions by
47/// raising errors as required to preserve DB integrity.
48#[derive(core_crypto_macros::Debug, Clone, serde::Serialize, serde::Deserialize)]
49pub struct Credential {
50    /// Ciphersuite used by this credential
51    pub(crate) ciphersuite: Ciphersuite,
52    /// Credential type
53    pub(crate) credential_type: CredentialType,
54    /// MLS internal credential. Stores the MLS credential
55    pub(crate) mls_credential: MlsCredential,
56    /// Public and private keys, and the signature scheme.
57    #[sensitive]
58    pub(crate) signature_key_pair: SignatureKeyPair,
59    /// Earliest valid time of creation for this credential.
60    ///
61    /// This is represented as seconds after the unix epoch.
62    ///
63    /// Only meaningful for X509, where it is the "valid_from" claim of the leaf credential.
64    /// For basic credentials, this is always 0.
65    pub(crate) earliest_validity: u64,
66}
67
68impl TryFrom<&StoredCredential> for Credential {
69    type Error = Error;
70
71    fn try_from(stored_credential: &StoredCredential) -> Result<Credential> {
72        let mls_credential = MlsCredential::tls_deserialize(&mut stored_credential.credential.as_slice())
73            .map_err(Error::tls_deserialize("mls credential"))?;
74        let ciphersuite = Ciphersuite::try_from(stored_credential.ciphersuite)
75            .map_err(RecursiveError::mls("loading ciphersuite from db"))?;
76        let signature_key_pair = openmls_basic_credential::SignatureKeyPair::from_raw(
77            ciphersuite.signature_algorithm(),
78            stored_credential.private_key.to_owned(),
79            stored_credential.public_key.to_owned(),
80        );
81        let credential_type = mls_credential
82            .credential_type()
83            .try_into()
84            .map_err(RecursiveError::mls_credential("loading credential from db"))?;
85        let earliest_validity = stored_credential.created_at;
86        Ok(Credential {
87            ciphersuite,
88            signature_key_pair,
89            credential_type,
90            mls_credential,
91            earliest_validity,
92        })
93    }
94}
95
96impl Credential {
97    /// Ensure that the provided `MlsCredential` matches the client id / signature key provided
98    pub(crate) fn validate_mls_credential(
99        mls_credential: &MlsCredential,
100        client_id: &ClientIdRef,
101        signature_key: &SignatureKeyPair,
102    ) -> Result<(), CredentialValidationError> {
103        match mls_credential.mls_credential() {
104            MlsCredentialType::Basic(_) => {
105                if client_id.as_slice() != mls_credential.identity() {
106                    return Err(CredentialValidationError::WrongCredential);
107                }
108            }
109            MlsCredentialType::X509(cert) => {
110                let certificate_public_key = cert
111                    .extract_public_key()
112                    .map_err(RecursiveError::mls_credential(
113                        "extracting public key from certificate in credential validation",
114                    ))?
115                    .ok_or(CredentialValidationError::NoPublicKey)?;
116                if signature_key.public() != certificate_public_key {
117                    return Err(CredentialValidationError::WrongCredential);
118                }
119            }
120        }
121        Ok(())
122    }
123
124    /// Generate a basic credential.
125    ///
126    /// The result is independent of any client instance and the database; it lives in memory only.
127    ///
128    /// The earliest validity of this credential is always 0. It will be updated once the credential is added to a
129    /// session.
130    pub fn basic(ciphersuite: Ciphersuite, client_id: ClientId, crypto: impl OpenMlsCrypto) -> Result<Self> {
131        let signature_scheme = ciphersuite.signature_algorithm();
132        let (private_key, public_key) = crypto
133            .signature_key_gen(signature_scheme)
134            .map_err(MlsError::wrap("generating signature key"))?;
135        let signature_key_pair = SignatureKeyPair::from_raw(signature_scheme, private_key, public_key);
136
137        Ok(Self {
138            ciphersuite,
139            credential_type: CredentialType::Basic,
140            mls_credential: MlsCredential::new_basic(client_id.into_inner()),
141            signature_key_pair,
142            earliest_validity: 0,
143        })
144    }
145
146    /// Get the Openmls Credential type.
147    ///
148    /// This stores the credential type (basic/x509).
149    pub fn mls_credential(&self) -> &MlsCredential {
150        &self.mls_credential
151    }
152
153    /// Get the credential type
154    pub fn credential_type(&self) -> CredentialType {
155        self.credential_type
156    }
157
158    /// Get a reference to the `SignatureKeyPair`.
159    pub(crate) fn signature_key(&self) -> &SignatureKeyPair {
160        &self.signature_key_pair
161    }
162
163    /// Get the signature scheme
164    pub fn signature_scheme(&self) -> SignatureScheme {
165        self.signature_key_pair.signature_scheme()
166    }
167
168    /// Get the ciphersuite
169    pub fn ciphersuite(&self) -> Ciphersuite {
170        self.ciphersuite
171    }
172
173    /// Generate a `CredentialWithKey`, which combines the credential type with the public portion of the keypair.
174    pub fn to_mls_credential_with_key(&self) -> CredentialWithKey {
175        CredentialWithKey {
176            credential: self.mls_credential.clone(),
177            signature_key: self.signature_key_pair.to_public_vec().into(),
178        }
179    }
180
181    /// Earliest valid time of creation for this credential.
182    ///
183    /// This is represented as seconds after the unix epoch.
184    ///
185    /// Only meaningful for X509, where it is the "valid_from" claim of the leaf credential.
186    /// For basic credentials, this is always 0 when the credential is first created.
187    /// It is updated upon being persisted to the database.
188    pub fn earliest_validity(&self) -> u64 {
189        self.earliest_validity
190    }
191
192    /// Get the client ID associated with this credential
193    pub fn client_id(&self) -> &ClientIdRef {
194        self.mls_credential.identity().into()
195    }
196}
197
198impl Credential {
199    /// Create a credential from an identifier
200    // currently only used in test code, but generally applicable
201    #[cfg_attr(not(test), expect(dead_code))]
202    pub(crate) fn from_identifier(
203        identifier: &ClientIdentifier,
204        ciphersuite: Ciphersuite,
205        crypto: impl OpenMlsCrypto,
206    ) -> Result<Self> {
207        match identifier {
208            ClientIdentifier::Basic(client_id) => Self::basic(ciphersuite, client_id.clone(), crypto),
209            ClientIdentifier::X509(certs) => {
210                let signature_scheme = ciphersuite.signature_algorithm();
211                let cert = certs
212                    .get(&signature_scheme)
213                    .ok_or(Error::SignatureSchemeNotPresentInX509Identity(signature_scheme))?;
214                Self::x509(ciphersuite, cert.clone())
215            }
216        }
217    }
218}
219
220impl From<Credential> for CredentialWithKey {
221    fn from(cb: Credential) -> Self {
222        Self {
223            credential: cb.mls_credential,
224            signature_key: cb.signature_key_pair.public().into(),
225        }
226    }
227}
228
229impl Eq for Credential {}
230impl PartialEq for Credential {
231    fn eq(&self, other: &Self) -> bool {
232        self.mls_credential == other.mls_credential && self.earliest_validity == other.earliest_validity && {
233            let sk = &self.signature_key_pair;
234            let ok = &other.signature_key_pair;
235            sk.signature_scheme() == ok.signature_scheme() && sk.public() == ok.public()
236            // public key equality implies private key equality
237        }
238    }
239}
240
241// TODO: ensure certificate signature must match the group's ciphersuite ; fails otherwise. Tracking issue: WPB-9632
242// Requires more than 1 ciphersuite supported at the moment.
243#[cfg(test)]
244mod tests {
245    use std::collections::HashMap;
246
247    use mls_crypto_provider::PkiKeypair;
248
249    use super::{x509::CertificateBundle, *};
250    use crate::{
251        ClientIdentifier, CredentialType, E2eiConversationState,
252        mls::{conversation::Conversation as _, credential::x509::CertificatePrivateKey},
253        test_utils::{
254            x509::{CertificateParams, X509TestChain},
255            *,
256        },
257    };
258
259    #[apply(all_cred_cipher)]
260    async fn basic_clients_can_send_messages(case: TestContext) {
261        if !case.is_basic() {
262            return;
263        }
264        let [alice, bob] = case.sessions_basic().await;
265        let conversation = case.create_conversation([&alice, &bob]).await;
266        assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
267    }
268
269    #[apply(all_cred_cipher)]
270    async fn certificate_clients_can_send_messages(case: TestContext) {
271        if !case.is_x509() {
272            return;
273        }
274        let [alice, bob] = case.sessions_x509().await;
275        let conversation = case.create_conversation([&alice, &bob]).await;
276        assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
277    }
278
279    #[apply(all_cred_cipher)]
280    async fn heterogeneous_clients_can_send_messages(case: TestContext) {
281        // check that both credentials can initiate/join a group
282        let ([x509_session], [basic_session]) = case.sessions_mixed_credential_types().await;
283
284        // That way the conversation creator (Alice) will have a different credential type than Bob
285        let (alice, bob) = match case.credential_type {
286            CredentialType::Basic => (x509_session, basic_session),
287            CredentialType::X509 => (basic_session, x509_session),
288        };
289
290        let conversation = case.create_conversation([&alice, &bob]).await;
291        assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
292    }
293
294    #[apply(all_cred_cipher)]
295    async fn should_fail_when_certificate_chain_is_empty(case: TestContext) {
296        let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
297
298        let x509_intermediate = x509_test_chain.find_local_intermediate_ca();
299
300        let mut certs = CertificateBundle::rand(&"alice".into(), x509_intermediate);
301        certs.certificate_chain = vec![];
302        let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), certs)]));
303
304        let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
305            .await
306            .unwrap_err();
307        assert!(innermost_source_matches!(err, Error::InvalidIdentity));
308    }
309
310    #[apply(all_cred_cipher)]
311    async fn should_fail_when_certificate_chain_has_a_single_self_signed(case: TestContext) {
312        use crate::MlsErrorKind;
313
314        if !case.is_x509() {
315            return;
316        }
317        let mut x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
318
319        let (_alice_identifier, alice_cert) = x509_test_chain.issue_simple_certificate_bundle("alice", None);
320
321        let new_cert = alice_cert
322            .pki_keypair
323            .re_sign(&alice_cert.certificate, &alice_cert.certificate, None)
324            .unwrap();
325        let mut alice_cert = alice_cert.clone();
326        alice_cert.certificate = new_cert;
327        let cb = CertificateBundle::from_self_signed_certificate(&alice_cert);
328        let alice_identifier = ClientIdentifier::X509([(case.signature_scheme(), cb)].into());
329
330        let alice = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
331            .await
332            .unwrap();
333        let [bob] = case.sessions_x509().await;
334        let bob_key_package = bob.new_keypackage(&case).await;
335        let conversation = case.create_conversation([&alice]).await;
336        let err = conversation
337            .guard()
338            .await
339            .add_members([bob_key_package.into()].into())
340            .await
341            .unwrap_err();
342        assert!(innermost_source_matches!(err, MlsErrorKind::MlsAddMembersError(_)));
343    }
344
345    #[apply(all_cred_cipher)]
346    async fn should_fail_when_signature_key_doesnt_match_certificate_public_key(case: TestContext) {
347        if !case.is_x509() {
348            return;
349        }
350        let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
351        let x509_intermediate = x509_test_chain.find_local_intermediate_ca();
352
353        let certs = CertificateBundle::rand(&"alice".into(), x509_intermediate);
354        let new_pki_kp = PkiKeypair::rand_unchecked(case.signature_scheme());
355
356        let eve_key = CertificatePrivateKey::new(new_pki_kp.signing_key_bytes());
357        let cb = CertificateBundle {
358            certificate_chain: certs.certificate_chain,
359            private_key: eve_key,
360            signature_scheme: case.ciphersuite().signature_algorithm(),
361        };
362        let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), cb)]));
363
364        let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
365            .await
366            .unwrap_err();
367        assert!(innermost_source_matches!(
368            err,
369            crate::MlsErrorKind::MlsCryptoError(openmls::prelude::CryptoError::MismatchKeypair),
370        ));
371    }
372
373    #[apply(all_cred_cipher)]
374    async fn should_not_fail_but_degrade_when_certificate_expired(case: TestContext) {
375        if !case.is_x509() {
376            return;
377        }
378        Box::pin(async move {
379            let mut x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
380
381            let expiration_time = core::time::Duration::from_secs(14);
382            let start = web_time::Instant::now();
383
384            let (alice_identifier, _) = x509_test_chain.issue_simple_certificate_bundle("alice", None);
385            let (bob_identifier, _) = x509_test_chain.issue_simple_certificate_bundle("bob", Some(expiration_time));
386            let alice = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
387                .await
388                .unwrap();
389            let bob = SessionContext::new_with_identifier(&case, bob_identifier, Some(&x509_test_chain))
390                .await
391                .unwrap();
392
393            let conversation = case.create_conversation([&alice, &bob]).await;
394            // this should work since the certificate is not yet expired
395            assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
396
397            assert_eq!(
398                conversation.guard().await.e2ei_conversation_state().await.unwrap(),
399                E2eiConversationState::Verified
400            );
401
402            let elapsed = start.elapsed();
403            // Give time to the certificate to expire
404            if expiration_time > elapsed {
405                smol::Timer::after(expiration_time - elapsed + core::time::Duration::from_secs(2)).await;
406            }
407
408            assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
409            assert_eq!(
410                conversation.guard().await.e2ei_conversation_state().await.unwrap(),
411                E2eiConversationState::NotVerified
412            );
413        })
414        .await;
415    }
416
417    #[apply(all_cred_cipher)]
418    async fn should_not_fail_but_degrade_when_basic_joins(case: TestContext) {
419        if !case.is_x509() {
420            return;
421        }
422        Box::pin(async {
423            let ([alice, bob], [charlie]) = case.sessions_mixed_credential_types().await;
424            let conversation = case.create_conversation([&alice, &bob]).await;
425
426            // this should work since the certificate is not yet expired
427            assert!(conversation.is_functional_and_contains([&alice, &bob]).await);
428            assert_eq!(
429                conversation.guard().await.e2ei_conversation_state().await.unwrap(),
430                E2eiConversationState::Verified
431            );
432            assert_eq!(
433                conversation
434                    .guard_of(&bob)
435                    .await
436                    .e2ei_conversation_state()
437                    .await
438                    .unwrap(),
439                E2eiConversationState::Verified
440            );
441
442            // Charlie is a basic client that tries to join (i.e. emulates guest links in Wire)
443            let conversation = conversation
444                .invite_with_credential_notify([(&charlie, &charlie.initial_credential)])
445                .await;
446
447            assert_eq!(
448                conversation.guard().await.e2ei_conversation_state().await.unwrap(),
449                E2eiConversationState::NotVerified
450            );
451            assert!(conversation.is_functional_and_contains([&alice, &bob, &charlie]).await);
452            assert_eq!(
453                conversation.guard().await.e2ei_conversation_state().await.unwrap(),
454                E2eiConversationState::NotVerified
455            );
456        })
457        .await;
458    }
459
460    #[apply(all_cred_cipher)]
461    async fn should_fail_when_certificate_not_valid_yet(case: TestContext) {
462        use crate::MlsErrorKind;
463
464        if !case.is_x509() {
465            return;
466        }
467        let x509_test_chain = X509TestChain::init_empty(case.signature_scheme());
468
469        let tomorrow = now_std() + core::time::Duration::from_secs(3600 * 24);
470        let local_ca = x509_test_chain.find_local_intermediate_ca();
471        let alice_cert = {
472            let name = "alice";
473            let common_name = format!("{name} Smith");
474            let handle = format!("{}_wire", name.to_lowercase());
475            let client_id: String = crate::e2e_identity::id::QualifiedE2eiClientId::generate_with_domain("wire.com")
476                .try_into()
477                .unwrap();
478            local_ca.create_and_sign_end_identity(CertificateParams {
479                common_name: Some(common_name.clone()),
480                handle: Some(handle.clone()),
481                client_id: Some(client_id.clone()),
482                validity_start: Some(tomorrow),
483                ..Default::default()
484            })
485        };
486        let cb = CertificateBundle::from_certificate_and_issuer(&alice_cert, local_ca);
487        let alice_identifier = ClientIdentifier::X509(HashMap::from([(case.signature_scheme(), cb)]));
488        let err = SessionContext::new_with_identifier(&case, alice_identifier, Some(&x509_test_chain))
489            .await
490            .unwrap_err();
491
492        assert!(innermost_source_matches!(
493            err,
494            MlsErrorKind::MlsCryptoError(openmls::prelude::CryptoError::ExpiredCertificate),
495        ))
496    }
497
498    /// In order to be WASM-compatible
499    // pub fn now() -> wire_e2e_identity::prelude::OffsetDateTime {
500    //     let now_since_epoch = now_std().as_secs() as i64;
501    //     wire_e2e_identity::prelude::OffsetDateTime::from_unix_timestamp(now_since_epoch).unwrap()
502    // }
503    pub(crate) fn now_std() -> std::time::Duration {
504        let now = web_time::SystemTime::now();
505        now.duration_since(web_time::UNIX_EPOCH).unwrap()
506    }
507}