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
//! # Credentials
//!
//! A [`Credential`] contains identifying information about the client that
//! created it. [`Credential`]s represent clients in MLS groups and are
//! used to authenticate their messages. Each
//! [`KeyPackage`](crate::key_packages::KeyPackage) as well as each client (leaf node)
//! in the group (tree) contains a [`Credential`] and is authenticated.
//! The [`Credential`] must the be checked by an authentication server and the
//! application, which is out of scope of MLS.
//!
//! Clients can create a [`Credential`].
//!
//! The MLS protocol spec allows the [`Credential`] that represents a client in a group to
//! change over time. Concretely, members can issue an Update proposal or a Full
//! Commit to update their [`LeafNode`](crate::treesync::LeafNode), as
//! well as the [`Credential`] in it. The Update has to be authenticated by the
//! signature public key corresponding to the old [`Credential`].
//!
//! When receiving a credential update from another member, applications must
//! query the Authentication Service to ensure that the new credential is valid.
//!
//! There are multiple [`CredentialType`]s, although OpenMLS currently only
//! supports the [`BasicCredential`].
use std::io::{Read, Write};
use serde::{Deserialize, Serialize};
use tls_codec::{TlsDeserialize, TlsSerialize, TlsSize, VLBytes};
// Private
mod codec;
#[cfg(test)]
mod tests;
use errors::*;
use crate::ciphersuite::SignaturePublicKey;
// Public
pub mod errors;
/// CredentialType.
///
/// This enum contains variants for the different Credential Types.
///
/// ```c
/// // See IANA registry for registered values
/// uint16 CredentialType;
/// ```
///
/// **IANA Considerations**
///
/// | Value | Name | R | Ref |
/// |:-----------------|:-------------------------|:--|:---------|
/// | 0x0000 | RESERVED | - | RFC XXXX |
/// | 0x0001 | basic | Y | RFC XXXX |
/// | 0x0002 | x509 | Y | RFC XXXX |
/// | 0x0A0A | GREASE | Y | RFC XXXX |
/// | 0x1A1A | GREASE | Y | RFC XXXX |
/// | 0x2A2A | GREASE | Y | RFC XXXX |
/// | 0x3A3A | GREASE | Y | RFC XXXX |
/// | 0x4A4A | GREASE | Y | RFC XXXX |
/// | 0x5A5A | GREASE | Y | RFC XXXX |
/// | 0x6A6A | GREASE | Y | RFC XXXX |
/// | 0x7A7A | GREASE | Y | RFC XXXX |
/// | 0x8A8A | GREASE | Y | RFC XXXX |
/// | 0x9A9A | GREASE | Y | RFC XXXX |
/// | 0xAAAA | GREASE | Y | RFC XXXX |
/// | 0xBABA | GREASE | Y | RFC XXXX |
/// | 0xCACA | GREASE | Y | RFC XXXX |
/// | 0xDADA | GREASE | Y | RFC XXXX |
/// | 0xEAEA | GREASE | Y | RFC XXXX |
/// | 0xF000 - 0xFFFF | Reserved for Private Use | - | RFC XXXX |
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum CredentialType {
/// A [`BasicCredential`]
Basic,
/// An X.509 [`Certificate`]
X509,
/// A currently unknown credential.
Unknown(u16),
}
impl tls_codec::Size for CredentialType {
fn tls_serialized_len(&self) -> usize {
2
}
}
impl tls_codec::Deserialize for CredentialType {
fn tls_deserialize<R: Read>(bytes: &mut R) -> Result<Self, tls_codec::Error>
where
Self: Sized,
{
let mut extension_type = [0u8; 2];
bytes.read_exact(&mut extension_type)?;
Ok(CredentialType::from(u16::from_be_bytes(extension_type)))
}
}
impl tls_codec::Serialize for CredentialType {
fn tls_serialize<W: Write>(&self, writer: &mut W) -> Result<usize, tls_codec::Error> {
writer.write_all(&u16::from(*self).to_be_bytes())?;
Ok(2)
}
}
impl From<u16> for CredentialType {
fn from(value: u16) -> Self {
match value {
1 => CredentialType::Basic,
2 => CredentialType::X509,
unknown => CredentialType::Unknown(unknown),
}
}
}
impl From<CredentialType> for u16 {
fn from(value: CredentialType) -> Self {
match value {
CredentialType::Basic => 1,
CredentialType::X509 => 2,
CredentialType::Unknown(unknown) => unknown,
}
}
}
/// X.509 Certificate.
///
/// This struct contains an X.509 certificate chain. Note that X.509
/// certificates are not yet supported by OpenMLS.
///
/// ```c
/// struct {
/// opaque cert_data<V>;
/// } Certificate;
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Certificate {
cert_data: Vec<u8>,
}
/// MlsCredentialType.
///
/// This enum contains variants containing the different available credentials.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum MlsCredentialType {
/// A [`BasicCredential`]
Basic(BasicCredential),
/// An X.509 [`Certificate`]
X509(Certificate),
}
/// Credential.
///
/// This struct contains MLS credential data, where the data depends on the
/// type. The [`CredentialType`] always matches the [`MlsCredentialType`].
///
/// ```c
/// struct {
/// CredentialType credential_type;
/// select (Credential.credential_type) {
/// case basic:
/// opaque identity<V>;
///
/// case x509:
/// Certificate chain<V>;
/// };
/// } Credential;
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Credential {
credential_type: CredentialType,
credential: MlsCredentialType,
}
impl Credential {
/// Returns the credential type.
pub fn credential_type(&self) -> CredentialType {
self.credential_type
}
/// Creates and returns a new [`Credential`] of the given
/// [`CredentialType`] for the given identity.
/// If the credential holds key material, this is generated and stored in
/// the key store.
///
/// Returns an error if the given [`CredentialType`] is not supported.
pub fn new(
identity: Vec<u8>,
credential_type: CredentialType,
) -> Result<Self, CredentialError> {
let mls_credential = match credential_type {
CredentialType::Basic => BasicCredential {
identity: identity.into(),
},
_ => return Err(CredentialError::UnsupportedCredentialType),
};
let credential = Credential {
credential_type,
credential: MlsCredentialType::Basic(mls_credential),
};
Ok(credential)
}
/// Returns the identity of a given credential.
pub fn identity(&self) -> &[u8] {
match &self.credential {
MlsCredentialType::Basic(basic_credential) => basic_credential.identity.as_slice(),
// TODO: implement getter for identity for X509 certificates. See issue #134.
MlsCredentialType::X509(_) => panic!("X509 certificates are not yet implemented."),
}
}
}
impl From<MlsCredentialType> for Credential {
fn from(mls_credential_type: MlsCredentialType) -> Self {
Credential {
credential_type: match mls_credential_type {
MlsCredentialType::Basic(_) => CredentialType::Basic,
MlsCredentialType::X509(_) => CredentialType::X509,
},
credential: mls_credential_type,
}
}
}
/// Basic Credential.
///
/// A `BasicCredential` as defined in the MLS protocol spec. It exposes only an
/// `identity` to represent the client.
///
/// Note that this credential does not contain any key material or any other
/// information.
///
/// OpenMLS provides an implementation of signature keys for convenience in the
/// `openmls_basic_credential` crate.
#[derive(
Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TlsSerialize, TlsDeserialize, TlsSize,
)]
pub struct BasicCredential {
identity: VLBytes,
}
#[derive(Debug, Clone)]
/// A wrapper around a credential with a corresponding public key.
pub struct CredentialWithKey {
/// The [`Credential`].
pub credential: Credential,
/// The corresponding public key as [`SignaturePublicKey`].
pub signature_key: SignaturePublicKey,
}
#[cfg(test)]
impl CredentialWithKey {
pub fn from_parts(credential: Credential, key: &[u8]) -> Self {
Self {
credential,
signature_key: key.into(),
}
}
}
#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils {
use openmls_basic_credential::SignatureKeyPair;
use openmls_traits::{types::SignatureScheme, OpenMlsCryptoProvider};
use super::{Credential, CredentialType, CredentialWithKey};
/// Convenience function that generates a new credential and a key pair for
/// it (using the basic credential crate).
/// The signature keys are stored in the key store.
///
/// Returns the [`Credential`] and the [`SignatureKeyPair`].
pub fn new_credential(
backend: &impl OpenMlsCryptoProvider,
identity: &[u8],
credential_type: CredentialType,
signature_scheme: SignatureScheme,
) -> (CredentialWithKey, SignatureKeyPair) {
let credential = Credential::new(identity.into(), credential_type).unwrap();
let signature_keys = SignatureKeyPair::new(signature_scheme).unwrap();
signature_keys.store(backend.key_store()).unwrap();
(
CredentialWithKey {
credential,
signature_key: signature_keys.public().into(),
},
signature_keys,
)
}
}