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
use super::{DataIdentifier, PrivAppendedData, verify_detached};
use error::RoutingError;
use maidsafe_utilities::serialisation::serialise;
use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
use std::collections::BTreeSet;
use xor_name::XorName;
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, RustcDecodable, RustcEncodable)]
pub enum Filter {
BlackList(BTreeSet<PublicKey>),
WhiteList(BTreeSet<PublicKey>),
}
impl Filter {
pub fn black_list<T: IntoIterator<Item = PublicKey>>(keys: T) -> Filter {
Filter::BlackList(keys.into_iter().collect())
}
pub fn white_list<T: IntoIterator<Item = PublicKey>>(keys: T) -> Filter {
Filter::WhiteList(keys.into_iter().collect())
}
}
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, RustcDecodable, RustcEncodable, Debug)]
pub struct AppendedData {
pub pointer: DataIdentifier,
pub sign_key: PublicKey,
pub signature: Signature,
}
impl AppendedData {
pub fn new(pointer: DataIdentifier,
pub_key: PublicKey,
secret_key: &SecretKey)
-> Result<AppendedData, RoutingError> {
let data_to_sign = serialise(&(&pointer, &pub_key))?;
let signature = sign::sign_detached(&data_to_sign, secret_key);
Ok(AppendedData {
pointer: pointer,
sign_key: pub_key,
signature: signature,
})
}
pub fn verify_signature(&self) -> bool {
let data_to_sign = match serialise(&(&self.pointer, &self.sign_key)) {
Err(_) => return false,
Ok(data) => data,
};
verify_detached(&self.signature, &data_to_sign, &self.sign_key)
}
}
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, RustcDecodable, RustcEncodable, Debug)]
pub enum AppendWrapper {
Pub {
append_to: XorName,
data: AppendedData,
version: u64,
},
Priv {
append_to: XorName,
data: PrivAppendedData,
sign_key: PublicKey,
version: u64,
signature: Signature,
},
}
impl AppendWrapper {
pub fn new_pub(append_to: XorName, data: AppendedData, version: u64) -> Self {
AppendWrapper::Pub {
append_to: append_to,
data: data,
version: version,
}
}
pub fn new_priv(append_to: XorName,
data: PrivAppendedData,
sign_pair: (&PublicKey, &SecretKey),
version: u64)
-> Result<AppendWrapper, RoutingError> {
let data_to_sign = serialise(&(&append_to, &data, &sign_pair.0, &version))?;
let signature = sign::sign_detached(&data_to_sign, sign_pair.1);
Ok(AppendWrapper::Priv {
append_to: append_to,
data: data,
sign_key: *sign_pair.0,
version: version,
signature: signature,
})
}
pub fn identifier(&self) -> DataIdentifier {
match *self {
AppendWrapper::Priv { append_to, .. } => DataIdentifier::PrivAppendable(append_to),
AppendWrapper::Pub { append_to, .. } => DataIdentifier::PubAppendable(append_to),
}
}
pub fn verify_signature(&self) -> bool {
match *self {
AppendWrapper::Pub { ref data, .. } => data.verify_signature(),
AppendWrapper::Priv { ref append_to,
ref data,
ref sign_key,
ref version,
ref signature } => {
let data_to_sign = match serialise(&(append_to, data, sign_key, version)) {
Err(_) => return false,
Ok(data) => data,
};
verify_detached(signature, &data_to_sign, sign_key)
}
}
}
pub fn sign_key(&self) -> &PublicKey {
match *self {
AppendWrapper::Pub { ref data, .. } => &data.sign_key,
AppendWrapper::Priv { ref sign_key, .. } => sign_key,
}
}
pub fn version(&self) -> &u64 {
match *self {
AppendWrapper::Pub { ref version, .. } |
AppendWrapper::Priv { ref version, .. } => version,
}
}
pub fn priv_appended_data(&self) -> Option<&PrivAppendedData> {
match *self {
AppendWrapper::Pub { .. } => None,
AppendWrapper::Priv { ref data, .. } => Some(data),
}
}
pub fn pub_appended_data(&self) -> Option<&AppendedData> {
match *self {
AppendWrapper::Pub { ref data, .. } => Some(data),
AppendWrapper::Priv { .. } => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use data::{DataIdentifier, PrivAppendedData};
use rand;
use rust_sodium::crypto::{box_, sign};
#[test]
fn pub_signatures() {
let pointer = DataIdentifier::Immutable(rand::random());
let (pub_key, secret_key) = sign::gen_keypair();
let mut ad = unwrap!(AppendedData::new(pointer, pub_key, &secret_key));
assert!(AppendWrapper::new_pub(rand::random(), ad.clone(), 5).verify_signature());
ad.pointer = DataIdentifier::Structured(rand::random(), 10000);
assert!(!AppendWrapper::new_pub(rand::random(), ad, 5).verify_signature());
}
#[test]
fn priv_signatures() {
let pointer = DataIdentifier::Immutable(rand::random());
let (pub_sign_key, secret_sign_key) = sign::gen_keypair();
let (pub_encrypt_key, secret_encrypt_key) = box_::gen_keypair();
let ad = unwrap!(AppendedData::new(pointer, pub_sign_key, &secret_sign_key));
let pad = unwrap!(PrivAppendedData::new(&ad, &pub_encrypt_key));
assert_eq!(ad, unwrap!(pad.open(&pub_encrypt_key, &secret_encrypt_key)));
let mut wrapper = unwrap!(AppendWrapper::new_priv(rand::random(),
pad,
(&pub_sign_key, &secret_sign_key),
5));
assert!(wrapper.verify_signature());
match wrapper {
AppendWrapper::Pub { .. } => unreachable!(),
AppendWrapper::Priv { ref mut version, .. } => *version = 6,
}
assert!(!wrapper.verify_signature());
}
}