forked from matrix-org/matrix-rust-sdk-crypto-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.rs
319 lines (281 loc) · 10.2 KB
/
encryption.rs
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! Encryption types & siblings.
use std::time::Duration;
use matrix_sdk_common::deserialized_responses::ShieldState as RustShieldState;
pub use matrix_sdk_common::deserialized_responses::ShieldStateCode;
use wasm_bindgen::prelude::*;
use crate::events;
/// Settings for an encrypted room.
///
/// This determines the algorithm and rotation periods of a group
/// session.
#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct EncryptionSettings {
/// The encryption algorithm that should be used in the room.
pub algorithm: EncryptionAlgorithm,
/// How long the session should be used before changing it,
/// expressed in microseconds.
#[wasm_bindgen(js_name = "rotationPeriod")]
pub rotation_period: u64,
/// How many messages should be sent before changing the session.
#[wasm_bindgen(js_name = "rotationPeriodMessages")]
pub rotation_period_messages: u64,
/// The history visibility of the room when the session was
/// created.
#[wasm_bindgen(js_name = "historyVisibility")]
pub history_visibility: events::HistoryVisibility,
/// Should untrusted devices receive the room key, or should they be
/// excluded from the conversation.
#[wasm_bindgen(js_name = "sharingStrategy")]
pub sharing_strategy: CollectStrategy,
}
impl Default for EncryptionSettings {
fn default() -> Self {
let default = matrix_sdk_crypto::olm::EncryptionSettings::default();
Self {
algorithm: default.algorithm.into(),
rotation_period: default.rotation_period.as_micros().try_into().unwrap(),
rotation_period_messages: default.rotation_period_msgs,
history_visibility: default.history_visibility.into(),
sharing_strategy: default.sharing_strategy.into(),
}
}
}
#[wasm_bindgen]
impl EncryptionSettings {
/// Create a new `EncryptionSettings` with default values.
#[wasm_bindgen(constructor)]
pub fn new() -> EncryptionSettings {
Self::default()
}
}
impl From<&EncryptionSettings> for matrix_sdk_crypto::olm::EncryptionSettings {
fn from(value: &EncryptionSettings) -> Self {
let algorithm = value.algorithm.clone().into();
Self {
algorithm,
rotation_period: Duration::from_micros(value.rotation_period),
rotation_period_msgs: value.rotation_period_messages,
history_visibility: value.history_visibility.clone().into(),
sharing_strategy: value.sharing_strategy.clone().into(),
}
}
}
/// An encryption algorithm to be used to encrypt messages sent to a
/// room.
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncryptionAlgorithm {
/// Olm version 1 using Curve25519, AES-256, and SHA-256.
OlmV1Curve25519AesSha2,
/// Megolm version 1 using AES-256 and SHA-256.
MegolmV1AesSha2,
/// Unsupported algorithm.
///
/// Applications should ignore this value if it is received, and should
/// never set it.
Unknown,
}
impl From<EncryptionAlgorithm> for matrix_sdk_crypto::types::EventEncryptionAlgorithm {
fn from(value: EncryptionAlgorithm) -> Self {
use EncryptionAlgorithm::*;
match value {
OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
MegolmV1AesSha2 => Self::MegolmV1AesSha2,
_ => unreachable!("Unknown variant"),
}
}
}
impl From<matrix_sdk_crypto::types::EventEncryptionAlgorithm> for EncryptionAlgorithm {
fn from(value: matrix_sdk_crypto::types::EventEncryptionAlgorithm) -> Self {
use matrix_sdk_crypto::types::EventEncryptionAlgorithm::*;
match value {
OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
MegolmV1AesSha2 => Self::MegolmV1AesSha2,
_ => Self::Unknown,
}
}
}
/// Strategy to collect the devices that should receive room keys for the
/// current discussion.
#[wasm_bindgen()]
#[derive(Debug, Clone, PartialEq)]
pub struct CollectStrategy {
inner: matrix_sdk_crypto::CollectStrategy,
}
#[wasm_bindgen]
impl CollectStrategy {
/// Tests for equality between two [`CollectStrategy`]s.
#[wasm_bindgen]
pub fn eq(&self, other: &CollectStrategy) -> bool {
self == other
}
}
impl From<CollectStrategy> for matrix_sdk_crypto::CollectStrategy {
fn from(value: CollectStrategy) -> Self {
value.inner
}
}
impl From<matrix_sdk_crypto::CollectStrategy> for CollectStrategy {
fn from(value: matrix_sdk_crypto::CollectStrategy) -> Self {
Self { inner: value }
}
}
#[wasm_bindgen]
impl CollectStrategy {
/// Device based sharing strategy.
///
/// If `only_allow_trusted_devices` is `true`, devices that are not trusted
/// will be excluded from the conversation. A device is trusted if any of
/// the following is true:
/// - It was manually marked as trusted.
/// - It was marked as verified via interactive verification.
/// - It is signed by its owner identity, and this identity has been
/// trusted via interactive verification.
/// - It is the current own device of the user.
///
/// If `error_on_verified_user` is `true`, and a verified user has an
/// unsigned device, key sharing will fail with an error.
///
/// If `error_on_verified_user` is `true`, and a verified user has replaced
/// their identity, key sharing will fail with an error.
///
/// Otherwise, keys are shared with unsigned devices as normal.
///
/// Once the problematic devices are blacklisted or whitelisted the
/// caller can retry to share a second time.
#[wasm_bindgen(js_name = "deviceBasedStrategy")]
pub fn device_based_strategy(
only_allow_trusted_devices: bool,
error_on_verified_user_problem: bool,
) -> CollectStrategy {
Self {
inner: matrix_sdk_crypto::CollectStrategy::DeviceBasedStrategy {
only_allow_trusted_devices,
error_on_verified_user_problem,
},
}
}
/// Share based on identity. Only distribute to devices signed by their
/// owner. If a user has no published identity he will not receive
/// any room keys.
#[wasm_bindgen(js_name = "identityBasedStrategy")]
pub fn identity_based_strategy() -> CollectStrategy {
Self { inner: matrix_sdk_crypto::CollectStrategy::IdentityBasedStrategy }
}
}
/// The trust level required to decrypt an event
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrustRequirement {
/// Decrypt events from everyone regardless of trust
Untrusted,
/// Only decrypt events from cross-signed or legacy devices
CrossSignedOrLegacy,
/// Only decrypt events from cross-signed devices
CrossSigned,
}
impl From<TrustRequirement> for matrix_sdk_crypto::TrustRequirement {
fn from(value: TrustRequirement) -> Self {
match value {
TrustRequirement::Untrusted => Self::Untrusted,
TrustRequirement::CrossSignedOrLegacy => Self::CrossSignedOrLegacy,
TrustRequirement::CrossSigned => Self::CrossSigned,
}
}
}
/// Settings for decrypting messages
#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct DecryptionSettings {
/// The trust level required to decrypt the event
pub sender_device_trust_requirement: TrustRequirement,
}
#[wasm_bindgen]
impl DecryptionSettings {
/// Create a new `DecryptionSettings` with the given trust requirement.
#[wasm_bindgen(constructor)]
pub fn new(sender_device_trust_requirement: TrustRequirement) -> DecryptionSettings {
Self { sender_device_trust_requirement }
}
}
impl From<&DecryptionSettings> for matrix_sdk_crypto::DecryptionSettings {
fn from(value: &DecryptionSettings) -> Self {
Self {
sender_device_trust_requirement: value.sender_device_trust_requirement.clone().into(),
}
}
}
/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
/// for more info.
#[wasm_bindgen]
#[derive(Debug, Clone, Copy)]
pub enum ShieldColor {
/// Important warning
Red,
/// Low warning
Grey,
/// No warning
None,
}
/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
/// for more info.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct ShieldState {
/// The shield color
pub color: ShieldColor,
/// A machine-readable representation of the authenticity for a
/// `ShieldState`.
pub code: Option<ShieldStateCode>,
message: Option<String>,
}
#[wasm_bindgen]
impl ShieldState {
/// Error message that can be displayed as a tooltip
#[wasm_bindgen(getter)]
pub fn message(&self) -> Option<String> {
self.message.clone()
}
}
impl From<RustShieldState> for ShieldState {
fn from(value: RustShieldState) -> Self {
match value {
RustShieldState::Red { message, code } => Self {
color: ShieldColor::Red,
code: Some(code),
message: Some(message.to_owned()),
},
RustShieldState::Grey { message, code } => Self {
color: ShieldColor::Grey,
code: Some(code),
message: Some(message.to_owned()),
},
RustShieldState::None => Self { color: ShieldColor::None, code: None, message: None },
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use wasm_bindgen_test::wasm_bindgen_test;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_node_experimental);
use super::EncryptionAlgorithm;
#[wasm_bindgen_test]
fn test_convert_encryption_algorithm_to_js() {
assert!(
EncryptionAlgorithm::from(
matrix_sdk_crypto::types::EventEncryptionAlgorithm::OlmV1Curve25519AesSha2
) == EncryptionAlgorithm::OlmV1Curve25519AesSha2
);
assert!(
EncryptionAlgorithm::from(
matrix_sdk_crypto::types::EventEncryptionAlgorithm::MegolmV1AesSha2
) == EncryptionAlgorithm::MegolmV1AesSha2
);
assert!(
EncryptionAlgorithm::from(matrix_sdk_crypto::types::EventEncryptionAlgorithm::from(
"unknown"
)) == EncryptionAlgorithm::Unknown
);
}
}