ruma_events/secret_storage/
key.rs

1//! Types for the [`m.secret_storage.key.*`] event.
2//!
3//! [`m.secret_storage.key.*`]: https://spec.matrix.org/latest/client-server-api/#key-storage
4
5use std::borrow::Cow;
6
7use js_int::{uint, UInt};
8use ruma_common::{
9    serde::{Base64, JsonObject},
10    KeyDerivationAlgorithm,
11};
12use serde::{Deserialize, Serialize};
13use serde_json::Value as JsonValue;
14
15mod secret_encryption_algorithm_serde;
16
17use crate::macros::EventContent;
18
19/// A passphrase from which a key is to be derived.
20#[derive(Clone, Debug, Deserialize, Serialize)]
21#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
22pub struct PassPhrase {
23    /// The algorithm to use to generate the key from the passphrase.
24    ///
25    /// Must be `m.pbkdf2`.
26    pub algorithm: KeyDerivationAlgorithm,
27
28    /// The salt used in PBKDF2.
29    pub salt: String,
30
31    /// The number of iterations to use in PBKDF2.
32    pub iterations: UInt,
33
34    /// The number of bits to generate for the key.
35    ///
36    /// Defaults to 256
37    #[serde(default = "default_bits", skip_serializing_if = "is_default_bits")]
38    pub bits: UInt,
39}
40
41impl PassPhrase {
42    /// Creates a new `PassPhrase` with a given salt and number of iterations.
43    pub fn new(salt: String, iterations: UInt) -> Self {
44        Self { algorithm: KeyDerivationAlgorithm::Pbkfd2, salt, iterations, bits: default_bits() }
45    }
46}
47
48fn default_bits() -> UInt {
49    uint!(256)
50}
51
52fn is_default_bits(val: &UInt) -> bool {
53    *val == default_bits()
54}
55
56/// A key description encrypted using a specified algorithm.
57///
58/// The only algorithm currently specified is `m.secret_storage.v1.aes-hmac-sha2`, so this
59/// essentially represents `AesHmacSha2KeyDescription` in the
60/// [spec](https://spec.matrix.org/v1.14/client-server-api/#msecret_storagev1aes-hmac-sha2).
61#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
62#[derive(Clone, Debug, Serialize, EventContent)]
63#[ruma_event(type = "m.secret_storage.key.*", kind = GlobalAccountData)]
64pub struct SecretStorageKeyEventContent {
65    /// The ID of the key.
66    #[ruma_event(type_fragment)]
67    #[serde(skip)]
68    pub key_id: String,
69
70    /// The name of the key.
71    pub name: Option<String>,
72
73    /// The encryption algorithm used for this key.
74    ///
75    /// Currently, only `m.secret_storage.v1.aes-hmac-sha2` is supported.
76    #[serde(flatten)]
77    pub algorithm: SecretStorageEncryptionAlgorithm,
78
79    /// The passphrase from which to generate the key.
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub passphrase: Option<PassPhrase>,
82}
83
84impl SecretStorageKeyEventContent {
85    /// Creates a `KeyDescription` with the given name.
86    pub fn new(key_id: String, algorithm: SecretStorageEncryptionAlgorithm) -> Self {
87        Self { key_id, name: None, algorithm, passphrase: None }
88    }
89}
90
91/// An algorithm and its properties, used to encrypt a secret.
92#[derive(Debug, Clone)]
93#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
94pub enum SecretStorageEncryptionAlgorithm {
95    /// Encrypted using the `m.secret_storage.v1.aes-hmac-sha2` algorithm.
96    ///
97    /// Secrets using this method are encrypted using AES-CTR-256 and authenticated using
98    /// HMAC-SHA-256.
99    V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties),
100
101    /// Encrypted using a custom algorithm.
102    #[doc(hidden)]
103    _Custom(CustomSecretEncryptionAlgorithm),
104}
105
106impl SecretStorageEncryptionAlgorithm {
107    /// The `algorithm` string.
108    pub fn algorithm(&self) -> &str {
109        match self {
110            Self::V1AesHmacSha2(_) => "m.secret_storage.v1.aes-hmac-sha2",
111            Self::_Custom(c) => &c.algorithm,
112        }
113    }
114
115    /// The algorithm-specific properties.
116    ///
117    /// The returned JSON object won't contain the `algorithm` field, use [`Self::algorithm()`] to
118    /// access it.
119    ///
120    /// Prefer to use the public variants of `SecretStorageEncryptionAlgorithm` where possible; this
121    /// method is meant to be used for custom algorithms only.
122    pub fn properties(&self) -> Cow<'_, JsonObject> {
123        fn serialize<T: Serialize>(obj: &T) -> JsonObject {
124            match serde_json::to_value(obj).expect("secret properties serialization to succeed") {
125                JsonValue::Object(obj) => obj,
126                _ => panic!("all secret properties must serialize to objects"),
127            }
128        }
129
130        match self {
131            Self::V1AesHmacSha2(p) => Cow::Owned(serialize(p)),
132            Self::_Custom(c) => Cow::Borrowed(&c.properties),
133        }
134    }
135}
136
137/// The key properties for the `m.secret_storage.v1.aes-hmac-sha2` algorithm.
138///
139/// Corresponds to the AES-specific properties of `AesHmacSha2KeyDescription` in the
140/// [spec](https://spec.matrix.org/v1.14/client-server-api/#msecret_storagev1aes-hmac-sha2).
141#[derive(Debug, Clone, Deserialize, Serialize)]
142#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
143pub struct SecretStorageV1AesHmacSha2Properties {
144    /// The 16-byte initialization vector, encoded as base64.
145    pub iv: Option<Base64>,
146
147    /// The MAC, encoded as base64.
148    pub mac: Option<Base64>,
149}
150
151impl SecretStorageV1AesHmacSha2Properties {
152    /// Creates a new `SecretStorageV1AesHmacSha2Properties` with the given
153    /// initialization vector and MAC.
154    pub fn new(iv: Option<Base64>, mac: Option<Base64>) -> Self {
155        Self { iv, mac }
156    }
157}
158
159/// The payload for a custom secret encryption algorithm.
160#[doc(hidden)]
161#[derive(Clone, Debug, Deserialize, Serialize)]
162pub struct CustomSecretEncryptionAlgorithm {
163    /// The encryption algorithm to be used for the key.
164    algorithm: String,
165
166    /// Algorithm-specific properties.
167    #[serde(flatten)]
168    properties: JsonObject,
169}
170
171#[cfg(test)]
172mod tests {
173    use assert_matches2::assert_matches;
174    use js_int::uint;
175    use ruma_common::{serde::Base64, KeyDerivationAlgorithm};
176    use serde_json::{
177        from_value as from_json_value, json, to_value as to_json_value,
178        value::to_raw_value as to_raw_json_value,
179    };
180
181    use super::{
182        PassPhrase, SecretStorageEncryptionAlgorithm, SecretStorageKeyEventContent,
183        SecretStorageV1AesHmacSha2Properties,
184    };
185    use crate::{AnyGlobalAccountDataEvent, EventContentFromType, GlobalAccountDataEvent};
186
187    #[test]
188    fn key_description_serialization() {
189        let mut content = SecretStorageKeyEventContent::new(
190            "my_key".into(),
191            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
192                iv: Some(Base64::parse("YWJjZGVmZ2hpamtsbW5vcA").unwrap()),
193                mac: Some(Base64::parse("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U").unwrap()),
194            }),
195        );
196        content.name = Some("my_key".to_owned());
197
198        let json = json!({
199            "name": "my_key",
200            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
201            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
202            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
203        });
204
205        assert_eq!(to_json_value(&content).unwrap(), json);
206    }
207
208    #[test]
209    fn key_description_deserialization() {
210        let json = to_raw_json_value(&json!({
211            "name": "my_key",
212            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
213            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
214            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
215        }))
216        .unwrap();
217
218        let content =
219            SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap();
220        assert_eq!(content.name.unwrap(), "my_key");
221        assert_matches!(content.passphrase, None);
222
223        assert_matches!(
224            content.algorithm,
225            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
226                iv: Some(iv),
227                mac: Some(mac)
228            })
229        );
230
231        assert_eq!(iv.encode(), "YWJjZGVmZ2hpamtsbW5vcA");
232        assert_eq!(mac.encode(), "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U");
233    }
234
235    #[test]
236    fn key_description_deserialization_without_name() {
237        let json = to_raw_json_value(&json!({
238            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
239            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
240            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
241        }))
242        .unwrap();
243
244        let content =
245            SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap();
246        assert!(content.name.is_none());
247        assert_matches!(content.passphrase, None);
248
249        assert_matches!(
250            content.algorithm,
251            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
252                iv: Some(iv),
253                mac: Some(mac)
254            })
255        );
256        assert_eq!(iv.encode(), "YWJjZGVmZ2hpamtsbW5vcA");
257        assert_eq!(mac.encode(), "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U");
258    }
259
260    #[test]
261    fn key_description_with_passphrase_serialization() {
262        let mut content = SecretStorageKeyEventContent {
263            passphrase: Some(PassPhrase::new("rocksalt".into(), uint!(8))),
264            ..SecretStorageKeyEventContent::new(
265                "my_key".into(),
266                SecretStorageEncryptionAlgorithm::V1AesHmacSha2(
267                    SecretStorageV1AesHmacSha2Properties {
268                        iv: Some(Base64::parse("YWJjZGVmZ2hpamtsbW5vcA").unwrap()),
269                        mac: Some(Base64::parse("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U").unwrap()),
270                    },
271                ),
272            )
273        };
274        content.name = Some("my_key".to_owned());
275
276        let json = json!({
277            "name": "my_key",
278            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
279            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
280            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U",
281            "passphrase": {
282                "algorithm": "m.pbkdf2",
283                "salt": "rocksalt",
284                "iterations": 8
285            }
286        });
287
288        assert_eq!(to_json_value(&content).unwrap(), json);
289    }
290
291    #[test]
292    fn key_description_with_passphrase_deserialization() {
293        let json = to_raw_json_value(&json!({
294            "name": "my_key",
295            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
296            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
297            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U",
298            "passphrase": {
299                "algorithm": "m.pbkdf2",
300                "salt": "rocksalt",
301                "iterations": 8,
302                "bits": 256
303            }
304        }))
305        .unwrap();
306
307        let content =
308            SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap();
309        assert_eq!(content.name.unwrap(), "my_key");
310
311        let passphrase = content.passphrase.unwrap();
312        assert_eq!(passphrase.algorithm, KeyDerivationAlgorithm::Pbkfd2);
313        assert_eq!(passphrase.salt, "rocksalt");
314        assert_eq!(passphrase.iterations, uint!(8));
315        assert_eq!(passphrase.bits, uint!(256));
316
317        assert_matches!(
318            content.algorithm,
319            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
320                iv: Some(iv),
321                mac: Some(mac)
322            })
323        );
324        assert_eq!(iv.encode(), "YWJjZGVmZ2hpamtsbW5vcA");
325        assert_eq!(mac.encode(), "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U");
326    }
327
328    #[test]
329    fn event_content_serialization() {
330        let mut content = SecretStorageKeyEventContent::new(
331            "my_key_id".into(),
332            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
333                iv: Some(Base64::parse("YWJjZGVmZ2hpamtsbW5vcA").unwrap()),
334                mac: Some(Base64::parse("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U").unwrap()),
335            }),
336        );
337        content.name = Some("my_key".to_owned());
338
339        let json = json!({
340            "name": "my_key",
341            "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
342            "iv": "YWJjZGVmZ2hpamtsbW5vcA",
343            "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
344        });
345
346        assert_eq!(to_json_value(&content).unwrap(), json);
347    }
348
349    #[test]
350    fn event_serialization() {
351        let mut content = SecretStorageKeyEventContent::new(
352            "my_key_id".into(),
353            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
354                iv: Some(Base64::parse("YWJjZGVmZ2hpamtsbW5vcA").unwrap()),
355                mac: Some(Base64::parse("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U").unwrap()),
356            }),
357        );
358        content.name = Some("my_key".to_owned());
359        let event = GlobalAccountDataEvent { content };
360
361        let json = json!({
362            "type": "m.secret_storage.key.my_key_id",
363            "content": {
364                "name": "my_key",
365                "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
366                "iv": "YWJjZGVmZ2hpamtsbW5vcA",
367                "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
368            }
369        });
370
371        assert_eq!(to_json_value(&event).unwrap(), json);
372    }
373
374    #[test]
375    fn event_deserialization() {
376        let json = json!({
377            "type": "m.secret_storage.key.my_key_id",
378            "content": {
379                "name": "my_key",
380                "algorithm": "m.secret_storage.v1.aes-hmac-sha2",
381                "iv": "YWJjZGVmZ2hpamtsbW5vcA",
382                "mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
383            }
384        });
385
386        let any_ev = from_json_value::<AnyGlobalAccountDataEvent>(json).unwrap();
387        assert_matches!(any_ev, AnyGlobalAccountDataEvent::SecretStorageKey(ev));
388        assert_eq!(ev.content.key_id, "my_key_id");
389        assert_eq!(ev.content.name.unwrap(), "my_key");
390        assert_matches!(ev.content.passphrase, None);
391
392        assert_matches!(
393            ev.content.algorithm,
394            SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
395                iv: Some(iv),
396                mac: Some(mac)
397            })
398        );
399        assert_eq!(iv.encode(), "YWJjZGVmZ2hpamtsbW5vcA");
400        assert_eq!(mac.encode(), "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U");
401    }
402
403    #[test]
404    fn custom_algorithm_key_description_deserialization() {
405        let json = to_raw_json_value(&json!({
406            "name": "my_key",
407            "algorithm": "io.ruma.custom_alg",
408            "io.ruma.custom_prop1": "YWJjZGVmZ2hpamtsbW5vcA",
409            "io.ruma.custom_prop2": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
410        }))
411        .unwrap();
412
413        let content =
414            SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap();
415        assert_eq!(content.name.unwrap(), "my_key");
416        assert_matches!(content.passphrase, None);
417
418        let algorithm = content.algorithm;
419        assert_eq!(algorithm.algorithm(), "io.ruma.custom_alg");
420        let properties = algorithm.properties();
421        assert_eq!(properties.len(), 2);
422        assert_eq!(
423            properties.get("io.ruma.custom_prop1").unwrap().as_str(),
424            Some("YWJjZGVmZ2hpamtsbW5vcA")
425        );
426        assert_eq!(
427            properties.get("io.ruma.custom_prop2").unwrap().as_str(),
428            Some("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U")
429        );
430    }
431}