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
use prost::Message as ProstMessage;
use serde::{Deserialize, Serialize};
use super::Message;
use crate::{
olm::SessionKeys,
utilities::{base64_decode, base64_encode},
Curve25519PublicKey, DecodeError,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PreKeyMessage {
pub(crate) session_keys: SessionKeys,
pub(crate) message: Message,
}
impl PreKeyMessage {
const VERSION: u8 = 3;
pub fn one_time_key(&self) -> Curve25519PublicKey {
self.session_keys.one_time_key
}
pub fn base_key(&self) -> Curve25519PublicKey {
self.session_keys.base_key
}
pub fn identity_key(&self) -> Curve25519PublicKey {
self.session_keys.identity_key
}
pub fn session_keys(&self) -> SessionKeys {
self.session_keys
}
pub fn message(&self) -> &Message {
&self.message
}
pub fn from_bytes(message: &[u8]) -> Result<Self, DecodeError> {
Self::try_from(message)
}
pub fn to_bytes(&self) -> Vec<u8> {
let message = ProtoBufPreKeyMessage {
one_time_key: self.session_keys.one_time_key.as_bytes().to_vec(),
base_key: self.session_keys.base_key.as_bytes().to_vec(),
identity_key: self.session_keys.identity_key.as_bytes().to_vec(),
message: self.message.to_bytes(),
};
let mut output: Vec<u8> = vec![0u8; message.encoded_len() + 1];
output[0] = Self::VERSION;
message
.encode(&mut output[1..].as_mut())
.expect("Couldn't encode our message into a protobuf");
output
}
pub fn from_base64(message: &str) -> Result<Self, DecodeError> {
Self::try_from(message)
}
pub fn to_base64(&self) -> String {
base64_encode(self.to_bytes())
}
#[cfg(feature = "low-level-api")]
pub fn wrap(session_keys: SessionKeys, message: Message) -> Self {
PreKeyMessage::new(session_keys, message)
}
pub(crate) fn new(session_keys: SessionKeys, message: Message) -> Self {
Self { session_keys, message }
}
}
impl Serialize for PreKeyMessage {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let message = self.to_base64();
serializer.serialize_str(&message)
}
}
impl<'de> Deserialize<'de> for PreKeyMessage {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let ciphertext = String::deserialize(d)?;
PreKeyMessage::from_base64(&ciphertext).map_err(serde::de::Error::custom)
}
}
impl TryFrom<&str> for PreKeyMessage {
type Error = DecodeError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let decoded = base64_decode(value)?;
Self::try_from(decoded)
}
}
impl TryFrom<Vec<u8>> for PreKeyMessage {
type Error = DecodeError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
Self::try_from(value.as_slice())
}
}
impl TryFrom<&[u8]> for PreKeyMessage {
type Error = DecodeError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let version = *value.get(0).ok_or(DecodeError::MissingVersion)?;
if version != Self::VERSION {
Err(DecodeError::InvalidVersion(Self::VERSION, version))
} else {
let decoded = ProtoBufPreKeyMessage::decode(&value[1..value.len()])?;
let one_time_key = Curve25519PublicKey::from_slice(&decoded.one_time_key)?;
let base_key = Curve25519PublicKey::from_slice(&decoded.base_key)?;
let identity_key = Curve25519PublicKey::from_slice(&decoded.identity_key)?;
let message = decoded.message.try_into()?;
let session_keys = SessionKeys { one_time_key, identity_key, base_key };
Ok(Self { session_keys, message })
}
}
}
#[derive(Clone, ProstMessage)]
struct ProtoBufPreKeyMessage {
#[prost(bytes, tag = "1")]
one_time_key: Vec<u8>,
#[prost(bytes, tag = "2")]
base_key: Vec<u8>,
#[prost(bytes, tag = "3")]
identity_key: Vec<u8>,
#[prost(bytes, tag = "4")]
message: Vec<u8>,
}