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
use serde::{Deserialize, Serialize};
use crate::{
types::{Curve25519SecretKey, KeyId},
Curve25519PublicKey,
};
#[derive(Serialize, Deserialize, Clone)]
pub(super) struct FallbackKey {
pub key_id: KeyId,
pub key: Curve25519SecretKey,
pub published: bool,
}
impl FallbackKey {
fn new(key_id: KeyId) -> Self {
let key = Curve25519SecretKey::new();
Self { key_id, key, published: false }
}
pub fn public_key(&self) -> Curve25519PublicKey {
Curve25519PublicKey::from(&self.key)
}
pub fn secret_key(&self) -> &Curve25519SecretKey {
&self.key
}
pub fn key_id(&self) -> KeyId {
self.key_id
}
pub fn mark_as_published(&mut self) {
self.published = true;
}
pub fn published(&self) -> bool {
self.published
}
}
#[derive(Serialize, Deserialize, Clone)]
pub(super) struct FallbackKeys {
pub key_id: u64,
pub fallback_key: Option<FallbackKey>,
pub previous_fallback_key: Option<FallbackKey>,
}
impl FallbackKeys {
pub fn new() -> Self {
Self { key_id: 0, fallback_key: None, previous_fallback_key: None }
}
pub fn mark_as_published(&mut self) {
if let Some(f) = self.fallback_key.as_mut() {
f.mark_as_published()
}
}
pub fn generate_fallback_key(&mut self) {
let key_id = KeyId(self.key_id);
self.key_id += 1;
self.previous_fallback_key = self.fallback_key.take();
self.fallback_key = Some(FallbackKey::new(key_id))
}
pub fn get_secret_key(&self, public_key: &Curve25519PublicKey) -> Option<&Curve25519SecretKey> {
self.fallback_key
.as_ref()
.filter(|f| f.public_key() == *public_key)
.or_else(|| {
self.previous_fallback_key.as_ref().filter(|f| f.public_key() == *public_key)
})
.map(|f| f.secret_key())
}
pub fn forget_previous_fallback_key(&mut self) -> Option<FallbackKey> {
self.previous_fallback_key.take()
}
pub fn unpublished_fallback_key(&self) -> Option<&FallbackKey> {
self.fallback_key.as_ref().filter(|f| !f.published())
}
}
#[cfg(test)]
mod test {
use super::FallbackKeys;
#[test]
fn fallback_key_fetching() {
let err = "Missing fallback key";
let mut fallback_keys = FallbackKeys::new();
fallback_keys.generate_fallback_key();
let public_key = fallback_keys.fallback_key.as_ref().expect(err).public_key();
let secret_bytes = fallback_keys.fallback_key.as_ref().expect(err).key.to_bytes();
let fetched_key = fallback_keys.get_secret_key(&public_key).expect(err);
assert_eq!(secret_bytes, fetched_key.to_bytes());
fallback_keys.generate_fallback_key();
let fetched_key = fallback_keys.get_secret_key(&public_key).expect(err);
assert_eq!(secret_bytes, fetched_key.to_bytes());
let public_key = fallback_keys.fallback_key.as_ref().expect(err).public_key();
let secret_bytes = fallback_keys.fallback_key.as_ref().expect(err).key.to_bytes();
let fetched_key = fallback_keys.get_secret_key(&public_key).expect(err);
assert_eq!(secret_bytes, fetched_key.to_bytes());
}
}