libsignal_protocol/state/
signed_prekey.rs1use std::convert::AsRef;
7use std::fmt;
8
9use prost::Message;
10
11use crate::proto::storage::SignedPreKeyRecordStructure;
12use crate::{kem, KeyPair, PrivateKey, PublicKey, Result, SignalProtocolError, Timestamp};
13
14#[derive(
16 Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, derive_more::From, derive_more::Into,
17)]
18pub struct SignedPreKeyId(u32);
19
20impl fmt::Display for SignedPreKeyId {
21 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 write!(f, "{}", self.0)
23 }
24}
25
26#[derive(Debug, Clone)]
27pub struct SignedPreKeyRecord {
28 signed_pre_key: SignedPreKeyRecordStructure,
29}
30
31impl SignedPreKeyRecord {
32 pub fn private_key(&self) -> Result<PrivateKey> {
33 Ok(PrivateKey::deserialize(&self.get_storage().private_key)?)
34 }
35}
36
37impl GenericSignedPreKey for SignedPreKeyRecord {
38 type KeyPair = KeyPair;
39 type Id = SignedPreKeyId;
40
41 fn get_storage(&self) -> &SignedPreKeyRecordStructure {
42 &self.signed_pre_key
43 }
44
45 fn from_storage(storage: SignedPreKeyRecordStructure) -> Self {
46 Self {
47 signed_pre_key: storage,
48 }
49 }
50}
51
52pub trait GenericSignedPreKey {
53 type KeyPair: KeyPairSerde;
54 type Id: From<u32> + Into<u32>;
55
56 fn get_storage(&self) -> &SignedPreKeyRecordStructure;
57 fn from_storage(storage: SignedPreKeyRecordStructure) -> Self;
58
59 fn new(id: Self::Id, timestamp: Timestamp, key_pair: &Self::KeyPair, signature: &[u8]) -> Self
60 where
61 Self: Sized,
62 {
63 let timestamp = timestamp.epoch_millis();
64 let public_key = key_pair.get_public().serialize();
65 let private_key = key_pair.get_private().serialize();
66 let signature = signature.to_vec();
67 Self::from_storage(SignedPreKeyRecordStructure {
68 id: id.into(),
69 timestamp,
70 public_key,
71 private_key,
72 signature,
73 })
74 }
75
76 fn serialize(&self) -> Result<Vec<u8>> {
77 Ok(self.get_storage().encode_to_vec())
78 }
79
80 fn deserialize(data: &[u8]) -> Result<Self>
81 where
82 Self: Sized,
83 {
84 Ok(Self::from_storage(
85 SignedPreKeyRecordStructure::decode(data)
86 .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?,
87 ))
88 }
89
90 fn id(&self) -> Result<Self::Id> {
91 Ok(self.get_storage().id.into())
92 }
93
94 fn timestamp(&self) -> Result<Timestamp> {
95 Ok(Timestamp::from_epoch_millis(self.get_storage().timestamp))
96 }
97
98 fn signature(&self) -> Result<Vec<u8>> {
99 Ok(self.get_storage().signature.clone())
100 }
101
102 fn public_key(&self) -> Result<<Self::KeyPair as KeyPairSerde>::PublicKey> {
103 <Self::KeyPair as KeyPairSerde>::PublicKey::deserialize(&self.get_storage().public_key)
104 }
105
106 fn key_pair(&self) -> Result<Self::KeyPair> {
107 Self::KeyPair::from_public_and_private(
108 &self.get_storage().public_key,
109 &self.get_storage().private_key,
110 )
111 }
112}
113
114pub trait KeySerde {
115 fn serialize(&self) -> Vec<u8>;
116 fn deserialize<T: AsRef<[u8]>>(bytes: T) -> Result<Self>
117 where
118 Self: Sized;
119}
120
121pub trait KeyPairSerde {
122 type PublicKey: KeySerde;
123 type PrivateKey: KeySerde;
124 fn from_public_and_private(public_key: &[u8], private_key: &[u8]) -> Result<Self>
125 where
126 Self: Sized;
127 fn get_public(&self) -> &Self::PublicKey;
128 fn get_private(&self) -> &Self::PrivateKey;
129}
130
131impl KeySerde for PublicKey {
132 fn serialize(&self) -> Vec<u8> {
133 self.serialize().to_vec()
134 }
135
136 fn deserialize<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
137 Ok(Self::deserialize(bytes.as_ref())?)
138 }
139}
140
141impl KeySerde for PrivateKey {
142 fn serialize(&self) -> Vec<u8> {
143 self.serialize()
144 }
145
146 fn deserialize<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
147 Ok(Self::deserialize(bytes.as_ref())?)
148 }
149}
150
151impl KeySerde for kem::PublicKey {
152 fn serialize(&self) -> Vec<u8> {
153 self.serialize().to_vec()
154 }
155
156 fn deserialize<T: AsRef<[u8]>>(bytes: T) -> Result<Self>
157 where
158 Self: Sized,
159 {
160 Self::deserialize(bytes.as_ref())
161 }
162}
163
164impl KeySerde for kem::SecretKey {
165 fn serialize(&self) -> Vec<u8> {
166 self.serialize().to_vec()
167 }
168 fn deserialize<T: AsRef<[u8]>>(bytes: T) -> Result<Self>
169 where
170 Self: Sized,
171 {
172 Self::deserialize(bytes.as_ref())
173 }
174}
175
176impl KeyPairSerde for KeyPair {
177 type PublicKey = PublicKey;
178 type PrivateKey = PrivateKey;
179
180 fn from_public_and_private(public_key: &[u8], private_key: &[u8]) -> Result<Self> {
181 Ok(KeyPair::from_public_and_private(public_key, private_key)?)
182 }
183
184 fn get_public(&self) -> &PublicKey {
185 &self.public_key
186 }
187
188 fn get_private(&self) -> &PrivateKey {
189 &self.private_key
190 }
191}
192
193impl KeyPairSerde for kem::KeyPair {
194 type PublicKey = kem::PublicKey;
195 type PrivateKey = kem::SecretKey;
196
197 fn from_public_and_private(public_key: &[u8], private_key: &[u8]) -> Result<Self> {
198 kem::KeyPair::from_public_and_private(public_key, private_key)
199 }
200
201 fn get_public(&self) -> &kem::PublicKey {
202 &self.public_key
203 }
204
205 fn get_private(&self) -> &kem::SecretKey {
206 &self.secret_key
207 }
208}