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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
mod plain_message;
mod src_authority;
pub use self::{plain_message::PlainMessageUtils, src_authority::SrcAuthorityUtils};
use crate::{
agreement::ProvenUtils,
crypto::{self, Verifier},
error::{Error, Result},
node::Node,
section::{SectionKeyShare, SectionUtils},
};
use bls_signature_aggregator::{Proof, ProofShare};
use secured_linked_list::{error::Error as SecuredLinkedListError, SecuredLinkedList};
use serde::Serialize;
use sn_messaging::{
node::{PlainMessage, RoutingMsg, SrcAuthority, Variant},
Aggregation, DstLocation, MessageId,
};
use std::fmt::Debug;
use thiserror::Error;
use xor_name::XorName;
pub trait RoutingMsgUtils {
fn check_signature(msg: &RoutingMsg) -> Result<()>;
fn new_signed(
src: SrcAuthority,
dst: DstLocation,
variant: Variant,
section_key: bls::PublicKey,
) -> Result<RoutingMsg, Error>;
fn for_dst_accumulation(
key_share: &SectionKeyShare,
src_name: XorName,
dst: DstLocation,
variant: Variant,
proof_chain: SecuredLinkedList,
) -> Result<RoutingMsg, Error>;
fn into_dst_accumulated(self, proof: Proof) -> Result<RoutingMsg>;
fn signable_view(&self) -> SignableView;
fn single_src(
node: &Node,
dst: DstLocation,
variant: Variant,
section_key: bls::PublicKey,
) -> Result<RoutingMsg>;
fn section_src(
plain: PlainMessage,
proof: Proof,
section_chain: SecuredLinkedList,
) -> Result<RoutingMsg>;
fn verify<'a, I: IntoIterator<Item = &'a bls::PublicKey>>(
&self,
trusted_keys: I,
) -> Result<VerifyStatus>;
fn proof(&self) -> Option<Proof>;
fn dst(&self) -> &DstLocation;
fn id(&self) -> &MessageId;
fn variant(&self) -> &Variant;
fn src(&self) -> &SrcAuthority;
fn verify_variant<'a, I: IntoIterator<Item = &'a bls::PublicKey>>(
&self,
trusted_keys: I,
) -> Result<VerifyStatus>;
fn updated_with_latest_key(&mut self, section_pk: bls::PublicKey);
}
impl RoutingMsgUtils for RoutingMsg {
fn check_signature(msg: &RoutingMsg) -> Result<()> {
let signed_bytes = bincode::serialize(&SignableView {
dst: &msg.dst,
variant: &msg.variant,
})
.map_err(|_| Error::InvalidMessage)?;
match &msg.src {
SrcAuthority::Node {
public_key,
signature,
..
} => {
if public_key.verify(&signed_bytes, signature).is_err() {
error!("Failed signature: {:?}", msg);
return Err(Error::CreateError(CreateError::FailedSignature));
}
}
SrcAuthority::BlsShare { proof_share, .. } => {
if !proof_share.verify(&signed_bytes) {
error!("Failed signature: {:?}", msg);
return Err(Error::CreateError(CreateError::FailedSignature));
}
if proof_share.public_key_set.public_key() != msg.section_pk() {
error!(
"Proof share public key doesn't match proof chain last key: {:?}",
msg
);
return Err(Error::CreateError(CreateError::FailedSignature));
}
}
SrcAuthority::Section { proof, .. } => {
if !msg.section_pk.verify(&proof.signature, &signed_bytes) {
error!(
"Failed signature: {:?} (Section PK: {:?})",
msg, msg.section_pk
);
return Err(Error::CreateError(CreateError::FailedSignature));
}
}
}
Ok(())
}
fn new_signed(
src: SrcAuthority,
dst: DstLocation,
variant: Variant,
section_pk: bls::PublicKey,
) -> Result<RoutingMsg, Error> {
let id = match &src {
SrcAuthority::Node { signature, .. } => MessageId::from_content(signature),
SrcAuthority::BlsShare { proof_share, .. } => {
MessageId::from_content(&proof_share.signature_share.0)
}
SrcAuthority::Section { proof, .. } => MessageId::from_content(&proof.signature),
}
.unwrap_or_default();
let msg = RoutingMsg {
id,
src,
dst,
aggregation: Aggregation::None,
variant,
section_pk,
};
Ok(msg)
}
fn for_dst_accumulation(
key_share: &SectionKeyShare,
src_name: XorName,
dst: DstLocation,
variant: Variant,
section_chain: SecuredLinkedList,
) -> Result<Self, Error> {
let serialized = bincode::serialize(&SignableView {
dst: &dst,
variant: &variant,
})
.map_err(|_| Error::InvalidMessage)?;
let signature_share = key_share.secret_key_share.sign(&serialized);
let proof_share = ProofShare {
public_key_set: key_share.public_key_set.clone(),
index: key_share.index,
signature_share,
};
let src = SrcAuthority::BlsShare {
src_name,
proof_share,
section_chain: section_chain.clone(),
};
Self::new_signed(src, dst, variant, *section_chain.last_key())
}
fn into_dst_accumulated(mut self, proof: Proof) -> Result<Self> {
let (proof_share, src_name, section_chain) = if let SrcAuthority::BlsShare {
proof_share,
src_name,
section_chain,
} = &self.src
{
(proof_share.clone(), *src_name, section_chain)
} else {
error!("not a message for dst accumulation");
return Err(Error::InvalidMessage);
};
if proof_share.public_key_set.public_key() != proof.public_key {
error!("proof public key doesn't match proof share public key");
return Err(Error::InvalidMessage);
}
if proof.public_key != self.section_pk {
error!("proof public key doesn't match the attached section PK");
return Err(Error::InvalidMessage);
}
let bytes = bincode::serialize(&self.signable_view()).map_err(|_| Error::InvalidMessage)?;
if !proof.verify(&bytes) {
return Err(Error::FailedSignature);
}
self.src = SrcAuthority::Section {
proof,
src_name,
section_chain: section_chain.clone(),
};
Ok(self)
}
fn signable_view(&self) -> SignableView {
SignableView {
dst: &self.dst,
variant: &self.variant,
}
}
fn single_src(
node: &Node,
dst: DstLocation,
variant: Variant,
section_pk: bls::PublicKey,
) -> Result<Self> {
let serialized = bincode::serialize(&SignableView {
dst: &dst,
variant: &variant,
})
.map_err(|_| Error::InvalidMessage)?;
let signature = crypto::sign(&serialized, &node.keypair);
let src = SrcAuthority::Node {
public_key: node.keypair.public,
signature,
};
RoutingMsg::new_signed(src, dst, variant, section_pk)
}
fn section_src(
plain: PlainMessage,
proof: Proof,
section_chain: SecuredLinkedList,
) -> Result<Self> {
Self::new_signed(
SrcAuthority::Section {
src_name: plain.src,
proof,
section_chain: section_chain.clone(),
},
plain.dst,
plain.variant,
*section_chain.last_key(),
)
}
fn verify<'a, I>(&self, trusted_keys: I) -> Result<VerifyStatus>
where
I: IntoIterator<Item = &'a bls::PublicKey>,
{
let bytes = bincode::serialize(&SignableView {
dst: &self.dst,
variant: &self.variant,
})
.map_err(|_| Error::InvalidMessage)?;
match &self.src {
SrcAuthority::Node {
public_key,
signature,
..
} => {
if public_key.verify(&bytes, signature).is_err() {
return Err(Error::FailedSignature);
}
self.verify_variant(trusted_keys)
}
SrcAuthority::BlsShare {
proof_share,
section_chain,
..
} => {
if proof_share.public_key_set.public_key() != self.section_pk {
return Err(Error::InvalidMessage);
}
if !proof_share.verify(&bytes) {
return Err(Error::FailedSignature);
}
if section_chain.check_trust(trusted_keys) {
Ok(VerifyStatus::Full)
} else {
Ok(VerifyStatus::Unknown)
}
}
SrcAuthority::Section {
proof,
section_chain,
..
} => {
if !self.section_pk.verify(&proof.signature, &bytes) {
return Err(Error::FailedSignature);
}
if section_chain.check_trust(trusted_keys) {
Ok(VerifyStatus::Full)
} else {
Ok(VerifyStatus::Unknown)
}
}
}
}
fn proof(&self) -> Option<Proof> {
if let SrcAuthority::Section { proof, .. } = &self.src {
Some(proof.clone())
} else {
None
}
}
fn dst(&self) -> &DstLocation {
&self.dst
}
fn id(&self) -> &MessageId {
&self.id
}
fn variant(&self) -> &Variant {
&self.variant
}
fn src(&self) -> &SrcAuthority {
&self.src
}
fn verify_variant<'a, I>(&self, trusted_keys: I) -> Result<VerifyStatus>
where
I: IntoIterator<Item = &'a bls::PublicKey>,
{
let proof_chain = match &self.variant {
Variant::NodeApproval {
section_auth,
member_info,
section_chain,
..
} => {
if !section_auth.verify(section_chain) {
return Err(Error::InvalidMessage);
}
if !member_info.verify(section_chain) {
return Err(Error::InvalidMessage);
}
section_chain
}
Variant::SectionKnowledge {
src_info: (_, chain),
..
} => chain,
Variant::Sync { section, .. } => section.chain(),
_ => return Ok(VerifyStatus::Full),
};
if proof_chain.check_trust(trusted_keys) {
Ok(VerifyStatus::Full)
} else {
Ok(VerifyStatus::Unknown)
}
}
fn updated_with_latest_key(&mut self, section_pk: bls::PublicKey) {
self.section_pk = section_pk
}
}
#[derive(Eq, PartialEq, Debug)]
pub enum VerifyStatus {
Full,
Unknown,
}
#[derive(Eq, PartialEq)]
pub enum MessageStatus {
Useful,
Useless,
Untrusted,
}
#[derive(Debug, Error)]
pub enum CreateError {
#[error("signature check failed")]
FailedSignature,
#[error("public key mismatch")]
PublicKeyMismatch,
}
#[derive(Debug, Error)]
pub enum ExtendProofChainError {
#[error("message has no proof chain")]
NoProofChain,
#[error("failed to extend proof chain: {}", .0)]
Extend(#[from] SecuredLinkedListError),
#[error("failed to re-create message: {}", .0)]
Create(#[from] CreateError),
}
#[derive(Serialize)]
pub struct SignableView<'a> {
pub dst: &'a DstLocation,
pub variant: &'a Variant,
}