openmls/ciphersuite/
signature.rs

1//! Signatures.
2//!
3//! This module contains structs for creating signature keys, issuing signatures and verifying them.
4
5use super::{LABEL_PREFIX, *};
6
7/// Signature.
8#[derive(
9    Debug,
10    PartialEq,
11    Eq,
12    Clone,
13    Serialize,
14    Deserialize,
15    TlsDeserialize,
16    TlsDeserializeBytes,
17    TlsSerialize,
18    TlsSize,
19)]
20pub struct Signature {
21    value: VLBytes,
22}
23
24impl From<Vec<u8>> for Signature {
25    fn from(value: Vec<u8>) -> Self {
26        Self {
27            value: value.into(),
28        }
29    }
30}
31
32/// Labeled signature content.
33///
34/// ```text
35/// struct {
36///     opaque label<V> = "MLS 1.0 " + Label;
37///     opaque content<V> = Content;
38/// } SignContent;
39/// ```
40#[derive(Debug, Clone, TlsSerialize, TlsDeserialize, TlsDeserializeBytes, TlsSize)]
41pub struct SignContent {
42    label: VLBytes,
43    content: VLBytes,
44}
45
46impl SignContent {
47    /// Create a new [`SignContent`] from a string label and the content bytes.
48    pub fn new(label: &str, content: VLBytes) -> Self {
49        let label_string = LABEL_PREFIX.to_owned() + label;
50        let label = label_string.as_bytes().into();
51        Self { label, content }
52    }
53}
54
55/// A public signature key.
56#[derive(
57    Eq,
58    PartialEq,
59    Hash,
60    Debug,
61    Clone,
62    Serialize,
63    Deserialize,
64    TlsSerialize,
65    TlsDeserialize,
66    TlsDeserializeBytes,
67    TlsSize,
68)]
69pub struct SignaturePublicKey {
70    pub(in crate::ciphersuite) value: VLBytes,
71}
72
73impl From<Vec<u8>> for SignaturePublicKey {
74    fn from(value: Vec<u8>) -> Self {
75        Self {
76            value: value.into(),
77        }
78    }
79}
80
81impl From<&[u8]> for SignaturePublicKey {
82    fn from(value: &[u8]) -> Self {
83        Self {
84            value: value.into(),
85        }
86    }
87}
88
89impl SignaturePublicKey {
90    /// Convert the "raw" signature into an enriched form, [OpenMlsSignaturePublicKey], that
91    /// already contains the signature scheme.
92    pub fn into_signature_public_key_enriched(
93        self,
94        signature_scheme: SignatureScheme,
95    ) -> OpenMlsSignaturePublicKey {
96        OpenMlsSignaturePublicKey {
97            signature_scheme,
98            value: self.value,
99        }
100    }
101
102    /// Returns the bytes of the signature public key.
103    pub fn as_slice(&self) -> &[u8] {
104        self.value.as_ref()
105    }
106}
107
108impl From<OpenMlsSignaturePublicKey> for SignaturePublicKey {
109    fn from(signature_public_key_enriched: OpenMlsSignaturePublicKey) -> Self {
110        SignaturePublicKey {
111            value: signature_public_key_enriched.value,
112        }
113    }
114}
115
116/// A public signature key.
117#[derive(Eq, PartialEq, Hash, Debug, Clone, Serialize, Deserialize)]
118pub struct OpenMlsSignaturePublicKey {
119    signature_scheme: SignatureScheme,
120    pub(in crate::ciphersuite) value: VLBytes,
121}
122
123#[cfg(any(test, feature = "test-utils"))]
124impl Signature {
125    #[cfg(test)]
126    pub(crate) fn modify(&mut self, value: &[u8]) {
127        self.value = value.to_vec().into();
128    }
129    pub(crate) fn as_slice(&self) -> &[u8] {
130        self.value.as_slice()
131    }
132}
133
134impl Signature {
135    /// Get this signature as slice.
136    pub(super) fn value(&self) -> &[u8] {
137        self.value.as_slice()
138    }
139}
140
141impl<T> SignedStruct<T> for Signature {
142    fn from_payload(_payload: T, signature: Signature) -> Self {
143        signature
144    }
145}
146
147impl OpenMlsSignaturePublicKey {
148    /// Create a new signature public key from raw key bytes.
149    pub fn new(value: VLBytes, signature_scheme: SignatureScheme) -> Result<Self, CryptoError> {
150        Ok(Self {
151            value,
152            signature_scheme,
153        })
154    }
155
156    /// Create a new signature public key from raw key.
157    pub fn from_signature_key(key: SignaturePublicKey, signature_scheme: SignatureScheme) -> Self {
158        Self {
159            value: key.value,
160            signature_scheme,
161        }
162    }
163
164    /// Get the signature scheme of the public key.
165    pub fn signature_scheme(&self) -> SignatureScheme {
166        self.signature_scheme
167    }
168
169    /// Returns the bytes of the signature public key.
170    pub fn as_slice(&self) -> &[u8] {
171        self.value.as_ref()
172    }
173}