-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
275 lines (237 loc) · 9.08 KB
/
mod.rs
File metadata and controls
275 lines (237 loc) · 9.08 KB
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
use binary_codec::{
BinaryDeserializer, BinarySerializer, BitStreamReader, BitStreamWriter, FromBytes,
SerializerConfig, ToBytes,
};
use serde::{Deserialize, Serialize};
use serde_with::base64::{Base64, UrlSafe};
use serde_with::formats::Unpadded;
use serde_with::serde_as;
use settings::CryptoSettings;
use crate::errors::{DeserializationError, SerializationError};
use crate::packets::context::PlabbleConnectionContext;
pub mod settings;
/// Plabble Protocol Packet base
#[serde_as]
#[derive(FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Debug, Clone)]
#[codec_ser_error("crate::errors::SerializationError")]
#[codec_de_error("crate::errors::DeserializationError")]
pub struct PlabblePacketBase {
/// Plabble Protocol version
/// 0 = debug
#[bits = 4]
pub version: u8,
/// If set to true, this packet is sent outside of a session
/// and no follow-up responses are expected.
#[serde(default)]
#[toggles("fire_and_forget")]
pub fire_and_forget: bool,
/// If set to true, this packet uses a pre-shared key for encryption.
#[serde(default)]
#[toggles("pre_shared_key")]
pub pre_shared_key: bool,
/// If set to true, this packet uses encryption. If false, add a MAC (Message Authentication Code) to the packet
#[serde(default)]
pub use_encryption: bool,
/// If set to true, use custom encryption settings.
#[serde(default)]
#[toggles("crypto_settings")]
pub specify_crypto_settings: bool,
/// Encryption settings
#[toggled_by = "crypto_settings"]
pub crypto_settings: Option<CryptoSettings>,
/// Pre-shared key ID, if using a pre-shared key
#[serde_as(as = "Option<Base64<UrlSafe, Unpadded>>")]
#[toggled_by = "pre_shared_key"]
pub psk_id: Option<[u8; 12]>,
/// Pre-shared key salt, if using a pre-shared key
#[serde_as(as = "Option<Base64<UrlSafe, Unpadded>>")]
#[toggled_by = "pre_shared_key"]
pub psk_salt: Option<[u8; 16]>,
}
impl Default for PlabblePacketBase {
fn default() -> Self {
PlabblePacketBase {
version: 1,
fire_and_forget: false,
pre_shared_key: false,
use_encryption: false,
specify_crypto_settings: false,
crypto_settings: None,
psk_id: None,
psk_salt: None,
}
}
}
/// Helper function to start decrypting a packet by reading the base and applying crypto settings from it and/or the context
///
/// This is used by both request and response deserialization, as they share the same base structure and crypto settings application logic
/// This also sets the offset for the MAC if that is enabled, so that the packet body can be read and decrypted before verifying the MAC at the end of the packet
///
/// # Arguments
/// - `stream`: The bit stream reader to read from
/// - `config`: The optional serializer config, which may contain a context with crypto settings and
///
/// # Returns
/// - `PlabblePacketBase`: The deserialized packet base, with crypto settings applied to the stream if needed
pub fn read_base_packet(
stream: &mut BitStreamReader,
config: &mut SerializerConfig<PlabbleConnectionContext>,
) -> Result<PlabblePacketBase, DeserializationError> {
// If full encryption is enabled (in provided context), try set it
if let Some(ctx) = &config.data
&& ctx.full_encryption
{
stream.set_crypto(ctx.create_crypto_stream(None, true));
}
let base = PlabblePacketBase::read_bytes(stream, Some(config))?;
// If crypto settings are provided in the packet, overwrite context settings
if let Some(settings) = &base.crypto_settings
&& let Some(ctx) = config.data.as_mut()
{
ctx.crypto_settings = base.crypto_settings;
settings.apply_to(config);
} else {
// TODO: investigate if this is correct. Don't we want to persist crypto settings in connection? if so, document that.
CryptoSettings::default().apply_to(config);
}
// If encryption enabled (and context provided), try set it (might overwrite the full packet encryption key, if that was the case)
if base.use_encryption
&& let Some(ctx) = &config.data
{
stream.set_crypto(ctx.create_crypto_stream(Some(&base), true));
}
// If MAC is enabled (and context provided), keep an offset of 16 on the reader
if !base.use_encryption && config.data.is_some() {
stream.set_offset_end(16);
}
Ok(base)
}
/// Helper function to write the base packet and apply crypto settings from it and/or the context
///
/// This is used by both request and response serialization, as they share the same base structure and crypto settings application logic
/// This also sets the crypto stream for the packet if encryption is enabled, so that the packet body can be encrypted as it is written to the stream
///
/// # Arguments
/// - `stream`: The bit stream writer to write to
/// - `base`: The packet base to write, which may contain crypto settings to apply to the stream
/// - `config`: The optional serializer config, which may contain a context with crypto settings and full encryption toggle
///
/// # Returns
/// - `Result<(), SerializationError>`: Ok if the base was written successfully, Err if writing failed
pub fn write_base_packet(
stream: &mut BitStreamWriter,
base: &PlabblePacketBase,
config: &mut SerializerConfig<PlabbleConnectionContext>,
) -> Result<(), SerializationError> {
// If full encryption is enabled (in provided context), try set it
if let Some(ctx) = &config.data
&& ctx.full_encryption
{
stream.set_crypto(ctx.create_crypto_stream(None, true));
}
// Write base packet
base.write_bytes(stream, Some(config))?;
// If crypto settings are provided in the packet, overwrite context settings
if let Some(settings) = &base.crypto_settings
&& let Some(ctx) = config.data.as_mut()
{
ctx.crypto_settings = base.crypto_settings;
settings.apply_to(config);
} else {
CryptoSettings::default().apply_to(config);
}
// If encryption enabled (and context provided), try set it (might overwrite the full packet encryption key, if that was the case)
if base.use_encryption
&& let Some(ctx) = &config.data
{
stream.set_crypto(ctx.create_crypto_stream(Some(base), true));
}
Ok(())
}
#[cfg(test)]
mod tests {
use binary_codec::{BinaryDeserializer, BinarySerializer, SerializerConfig};
use crate::errors::{DeserializationError, SerializationError};
use super::*;
#[test]
fn can_serialize_empty_base_packet() {
let toml = r#"
version = 1
fire_and_forget = true
use_encryption = true
"#;
let packet: PlabblePacketBase = toml::from_str(toml).unwrap();
let bytes = BinarySerializer::<(), SerializationError>::to_bytes(&packet, None).unwrap();
let deserialized_packet =
BinaryDeserializer::<(), DeserializationError>::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized_packet);
assert_eq!(packet.fire_and_forget, true);
assert_eq!(packet.use_encryption, true);
// Check some defaults
assert_eq!(packet.pre_shared_key, false);
assert_eq!(packet.specify_crypto_settings, false);
assert_eq!(vec![0b0101_0001], bytes);
}
#[test]
fn can_serialize_packet_with_full_settings_and_psk_id() {
let toml = r#"
version = 1
use_encryption = true
pre_shared_key = true
specify_crypto_settings = true
psk_id = "AQIDBAUGBwgJEBES"
psk_salt = "BwAAAAAAAAAAAAAAAAAABw"
[crypto_settings]
use_post_quantum = true
[crypto_settings.post_quantum_settings]
sign_pqc_dsa_44 = true
"#;
let packet: PlabblePacketBase = toml::from_str(toml).unwrap();
let mut config = SerializerConfig::<()>::new(None);
let bytes = packet.to_bytes(Some(&mut config)).unwrap();
let deserialized_packet =
BinaryDeserializer::<(), DeserializationError>::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized_packet);
assert_eq!(config.get_toggle("fire_and_forget"), Some(false));
assert_eq!(config.get_toggle("pre_shared_key"), Some(true));
assert_eq!(config.get_toggle("crypto_settings"), Some(true));
assert_eq!(config.get_toggle("dsa44"), Some(true));
assert_eq!(config.get_toggle("dsa65"), Some(false));
assert_eq!(
vec![
0b1110_0001,
0b1011_0001,
0b0000_0001,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0x10,
0x11,
0x12,
7,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
7
],
bytes
);
}
}