• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4 
5 use core::{
6     convert::Infallible,
7     fmt::{self, Debug},
8 };
9 
10 use alloc::vec::Vec;
11 use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
12 
13 use super::{Credential, CredentialType, MlsCredential};
14 
15 #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, MlsSize, MlsEncode, MlsDecode)]
16 #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
17 #[cfg_attr(
18     all(feature = "ffi", not(test)),
19     safer_ffi_gen::ffi_type(clone, opaque)
20 )]
21 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22 /// Bare assertion of an identity without any additional information.
23 ///
24 /// The format of the encoded identity is defined by the application.
25 ///
26 ///
27 /// # Warning
28 ///
29 /// Basic credentials are inherently insecure since they can not be
30 /// properly validated. It is not recommended to use [`BasicCredential`]
31 /// in production applications.
32 pub struct BasicCredential {
33     /// Underlying identifier as raw bytes.
34     #[mls_codec(with = "mls_rs_codec::byte_vec")]
35     #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
36     pub identifier: Vec<u8>,
37 }
38 
39 impl Debug for BasicCredential {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41         crate::debug::pretty_bytes(&self.identifier)
42             .named("BasicCredential")
43             .fmt(f)
44     }
45 }
46 
47 #[cfg_attr(all(feature = "ffi", not(test)), safer_ffi_gen::safer_ffi_gen)]
48 impl BasicCredential {
49     /// Create a new basic credential with raw bytes.
new(identifier: Vec<u8>) -> BasicCredential50     pub fn new(identifier: Vec<u8>) -> BasicCredential {
51         BasicCredential { identifier }
52     }
53 
54     /// Underlying identifier as raw bytes.
55     #[cfg(feature = "ffi")]
identifier(&self) -> &[u8]56     pub fn identifier(&self) -> &[u8] {
57         &self.identifier
58     }
59 }
60 
61 impl BasicCredential {
credential_type() -> CredentialType62     pub fn credential_type() -> CredentialType {
63         CredentialType::BASIC
64     }
65 
into_credential(self) -> Credential66     pub fn into_credential(self) -> Credential {
67         Credential::Basic(self)
68     }
69 }
70 
71 impl MlsCredential for BasicCredential {
72     type Error = Infallible;
73 
credential_type() -> CredentialType74     fn credential_type() -> CredentialType {
75         Self::credential_type()
76     }
77 
into_credential(self) -> Result<Credential, Self::Error>78     fn into_credential(self) -> Result<Credential, Self::Error> {
79         Ok(self.into_credential())
80     }
81 }
82