routing 0.4.0

A secured storage DHT
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct ConnectRequest {
    pub local_endpoints: Vec<::crust::Endpoint>,
    pub external_endpoints: Vec<::crust::Endpoint>,
    pub requester_fob: ::public_id::PublicId,
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct ConnectResponse {
    pub local_endpoints: Vec<::crust::Endpoint>,
    pub external_endpoints: Vec<::crust::Endpoint>,
    pub receiver_fob: ::public_id::PublicId,
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, RustcEncodable, RustcDecodable)]
/// SignedToken.
pub struct SignedToken {
    /// Encoded request to be signed.
    pub serialised_request: Vec<u8>,
    /// Signature of the serialised_request signed by secret sign key.
    pub signature: ::sodiumoxide::crypto::sign::Signature,
}

impl SignedToken {

    /// Verify the request was signed by the secret key corresponding to the passed in public key. 
    pub fn verify_signature(&self, public_sign_key: &::sodiumoxide::crypto::sign::PublicKey)
            -> bool {
        ::sodiumoxide::crypto::sign::verify_detached(
            &self.signature, &self.serialised_request, &public_sign_key)
    }
}

impl ::std::fmt::Debug for SignedToken {
    fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        formatter.write_str(&format!("SignedToken"))
    }
}

/// These are the message types routing provides.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
/// ExternalRequest.
pub enum ExternalRequest {
    /// Request to get data from the network.
    Get(::data::DataRequest, u8),
    /// Request to put data onto the network.
    Put(::data::Data),
    /// Request to mutate data on the network.
    Post(::data::Data),
    /// Request to delete data from the network.
    Delete(::data::Data),
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
/// ExternalResponse.
pub enum ExternalResponse {
    // TODO: Applies to Get: Technical depth: if the third param here is Some(...) then
    // the it shares most of the data with the second argument, which
    // needlessly increases bandwidth.

    /// Response to get data request.
    Get(::data::Data, ::data::DataRequest, Option<SignedToken>),
    /// Response to put data request on error.
    Put(::error::ResponseError, Option<SignedToken>),
    /// Response to post data request on error.
    Post(::error::ResponseError, Option<SignedToken>),
    /// Response to delete data request on error.
    Delete(::error::ResponseError, Option<SignedToken>),
}

impl ExternalResponse {

    /// If the *request* was from a group entity, then there is no signed token.
    pub fn get_signed_token(&self) -> &Option<SignedToken> {
        match *self {
            ExternalResponse::Get(_, _, ref r) => r,
            ExternalResponse::Put(_, ref r) => r,
            ExternalResponse::Post(_, ref r) => r,
            ExternalResponse::Delete(_, ref r) => r,
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub enum InternalRequest {
    Connect(ConnectRequest),
    RequestNetworkName(::public_id::PublicId),
    // a client can send RequestNetworkName
    CacheNetworkName(::public_id::PublicId, SignedToken),
    //               ~~|~~~~~  ~~|~~~~~~~~
    //                 |         | SignedToken contains Request::RequestNetworkName and needs to
    //                 |         | be forwarded in the Request::CacheNetworkName;
    //                 |         | from it the original reply to authority can be read.
    //                 | contains the PublicId from RequestNetworkName, but mutated with
    //                 | the network assigned name
    /// Refresh allows a persona to republish account records (identified with type_tag:u64 and
    /// the serialised payload:Vec<u8>).  The cause of the Refresh is the NameType of the node
    //. that caused the churn event.
    Refresh(u64, Vec<u8>, ::NameType),
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub enum InternalResponse {
    Connect(ConnectResponse, SignedToken),
    // FindGroup(Vec<::public_id::PublicId>, SignedToken),
    // GetGroupKey(::std::collections::BTreeMap<
    //      ::NameType, ::sodiumoxide::crypto::sign::PublicKey>, SignedToken),
    CacheNetworkName(::public_id::PublicId, Vec<::public_id::PublicId>, SignedToken),
    //               ~~|~~~~~  ~~|~~~~~~~~~~  ~~|~~~~~~~~
    //                 |         |              | the original Request::RequestNetworkName
    //                 |         | the group public keys to combine FindGroup in this response
    //                 | the cached PublicId in the group
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub enum Content {
    ExternalRequest(ExternalRequest),
    InternalRequest(InternalRequest),
    ExternalResponse(ExternalResponse),
    InternalResponse(InternalResponse),
}

/// the bare (unsigned) routing message
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct RoutingMessage {
    // version_number     : u8
    pub from_authority: ::authority::Authority,
    pub to_authority: ::authority::Authority,
    pub content: Content,
}

impl RoutingMessage {

    #[allow(dead_code)]
    pub fn source(&self) -> ::authority::Authority {
        self.from_authority.clone()
    }

    pub fn destination(&self) -> ::authority::Authority {
        self.to_authority.clone()
    }

    pub fn client_key(&self) -> Option<::sodiumoxide::crypto::sign::PublicKey> {
        match self.from_authority {
            ::authority::Authority::ClientManager(_) => None,
            ::authority::Authority::NaeManager(_) => None,
            ::authority::Authority::NodeManager(_) => None,
            ::authority::Authority::ManagedNode(_) => None,
            ::authority::Authority::Client(_, key) => Some(key),
        }
    }

    pub fn client_key_as_name(&self) -> Option<::NameType> {
        self.client_key().map(|n| ::utils::public_key_to_client_name(&n))
    }

    pub fn from_group(&self) -> Option<::NameType> {
        match self.from_authority {
            ::authority::Authority::ClientManager(name) => Some(name),
            ::authority::Authority::NaeManager(name) => Some(name),
            ::authority::Authority::NodeManager(name) => Some(name),
            ::authority::Authority::ManagedNode(_) => None,
            ::authority::Authority::Client(_, _) => None,
        }
    }
}

/// All messages sent / received are constructed as signed message.
#[derive(PartialEq, Eq, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct SignedMessage {
    body: RoutingMessage,
    claimant: ::types::Address,
    //          when signed by Client(PublicKey) the data needs to contain it as an owner
    //          when signed by a Node(NameType), Sentinel needs to validate the signature
    random_bits: u8,
    signature: ::sodiumoxide::crypto::sign::Signature,
}

#[allow(unused)]
impl SignedMessage {
    pub fn new(claimant: ::types::Address,
               message: RoutingMessage,
               private_sign_key: &::sodiumoxide::crypto::sign::SecretKey)
               -> Result<SignedMessage, ::cbor::CborError> {
        use ::rand::Rng;

        let mut rng = ::rand::thread_rng();
        let random_bits = rng.gen::<u8>();
        let encoded_body = try!(::utils::encode(&(&message, &claimant, &random_bits)));
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, private_sign_key);

        Ok(SignedMessage { body: message, claimant: claimant,
            random_bits: random_bits, signature: signature })
    }

    /// Construct a signed message passing in the signature.
    pub fn with_signature(claimant: ::types::Address,
                          message: RoutingMessage,
                          random_bits: u8,
                          signature: ::sodiumoxide::crypto::sign::Signature)
                          -> Result<SignedMessage, ::cbor::CborError> {

        Ok(SignedMessage { body: message, claimant: claimant,
              random_bits: random_bits, signature: signature })
    }

    /// Construct a signed message from a signed token.
    pub fn new_from_token(signed_token: SignedToken) -> Result<SignedMessage, ::cbor::CborError> {
        let (message, claimant, random_bits) =
            try!(::utils::decode(&signed_token.serialised_request));

        Ok(SignedMessage { body: message, claimant: claimant,
            random_bits: random_bits, signature: signed_token.signature })
    }

    /// Verify the signature using the given public key.
    pub fn verify_signature(&self, public_sign_key: &::sodiumoxide::crypto::sign::PublicKey)
            -> bool {
        let encoded_body = match ::utils::encode(&(&self.body, &self.claimant, &self.random_bits)) {
            Ok(x) => x,
            Err(_) => return false,
        };

        ::sodiumoxide::crypto::sign::verify_detached(
            &self.signature, &encoded_body, public_sign_key)
    }

    /// Return the internal routing message.
    pub fn get_routing_message(&self) -> &RoutingMessage {
        &self.body
    }

    /// Return the signature.
    pub fn signature(&self) -> &::sodiumoxide::crypto::sign::Signature {
        &self.signature
    }

    /// Return the encoded unsigned body of the message.
    pub fn encoded_body(&self) -> Result<Vec<u8>, ::cbor::CborError> {
        ::utils::encode(&(&self.body, &self.claimant, &self.random_bits))
    }

    /// Return the associated signed token.
    pub fn as_token(&self) -> Result<SignedToken, ::cbor::CborError> {
        Ok(SignedToken {
                serialised_request: try!(self.encoded_body()),
                signature: self.signature().clone(),
            })
    }

    /// Return the message claimant.
    pub fn claimant(&self) -> &::types::Address {
        &self.claimant
    }
}


#[cfg(test)]
mod test{

    #[test]
    fn signed_message_new() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let signed_message =
            super::SignedMessage::new(claimant.clone(), routing_message.clone(), &keys.1);

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let encoded_body = signed_message.encoded_body();

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, &keys.1);

        assert_eq!(signed_message.signature(), &signature);
        assert!(signed_message.verify_signature(&keys.0));
    }

    #[test]
    fn invalid_signed_message_new() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let invalid_keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let signed_message =
            super::SignedMessage::new(claimant.clone(), routing_message.clone(), &invalid_keys.1);

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let encoded_body = signed_message.encoded_body();

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, &keys.1);

        assert!(signed_message.signature() != &signature);
        assert!(!signed_message.verify_signature(&keys.0));
    }

    #[test]
    fn signed_message_with_signature() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let random_bits = ::test_utils::messages_util::generate_random_u8();
        let encoded_body = ::utils::encode(&(&routing_message, &claimant, &random_bits));

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, &keys.1);
        let signed_message = super::SignedMessage::with_signature(
                claimant.clone(), routing_message.clone(), random_bits, signature);

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let signed_message_encoded_body = signed_message.encoded_body();

        assert!(signed_message_encoded_body.is_ok());

        let signed_message_encoded_body = signed_message_encoded_body.unwrap();

        assert_eq!(signed_message_encoded_body, encoded_body);

        let signature =
                ::sodiumoxide::crypto::sign::sign_detached(&signed_message_encoded_body, &keys.1);

        assert_eq!(signed_message.signature(), &signature);
        assert!(signed_message.verify_signature(&keys.0));
    }

    #[test]
    fn invalid_signed_message_with_signature() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let random_bits = ::test_utils::messages_util::generate_random_u8();
        let encoded_body = ::utils::encode(&(&routing_message, &claimant, &random_bits));

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let invalid_keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, &invalid_keys.1);
        let signed_message = super::SignedMessage::with_signature(
                claimant.clone(), routing_message.clone(), random_bits, signature);

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let signed_message_encoded_body = signed_message.encoded_body();

        assert!(signed_message_encoded_body.is_ok());

        let signed_message_encoded_body = signed_message_encoded_body.unwrap();

        assert_eq!(signed_message_encoded_body, encoded_body);

        let signature =
                ::sodiumoxide::crypto::sign::sign_detached(&signed_message_encoded_body, &keys.1);

        assert!(signed_message.signature() != &signature);
        assert!(!signed_message.verify_signature(&keys.0));
    }

    #[test]
    fn signed_message_new_from_token() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let random_bits = ::test_utils::messages_util::generate_random_u8();
        let encoded_body = ::utils::encode(&(&routing_message, &claimant, &random_bits));

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let signature = ::sodiumoxide::crypto::sign::sign_detached(&encoded_body, &keys.1);
        let signed_token = super::SignedToken {
            serialised_request: encoded_body.clone(), signature:  signature
        };
        let signed_message = super::SignedMessage::new_from_token(signed_token.clone());

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let signed_message_encoded_body = signed_message.encoded_body();

        assert!(signed_message_encoded_body.is_ok());

        let signed_message_encoded_body = signed_message_encoded_body.unwrap();

        assert_eq!(signed_message_encoded_body, encoded_body);

        let signature =
                ::sodiumoxide::crypto::sign::sign_detached(&signed_message_encoded_body, &keys.1);

        assert_eq!(signed_message.signature(), &signature);
        assert!(signed_message.verify_signature(&keys.0));

        let signed_message_as_token = signed_message.as_token();

        assert!(signed_message_as_token.is_ok());
        assert_eq!(signed_message_as_token.unwrap(), signed_token);
    }

    #[test]
    fn invalid_signed_message_new_from_token() {
        let claimant = ::types::Address::Node(::test_utils::Random::generate_random());
        let keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let routing_message =
            ::test_utils::messages_util::arbitrary_routing_message(&keys.0, &keys.1);
        let random_bits = ::test_utils::messages_util::generate_random_u8();
        let encoded_body = ::utils::encode(&(&routing_message, &claimant, &random_bits));

        assert!(encoded_body.is_ok());

        let encoded_body = encoded_body.unwrap();
        let invalid_keys = ::sodiumoxide::crypto::sign::gen_keypair();
        let signature =
                ::sodiumoxide::crypto::sign::sign_detached(&encoded_body.clone(), &invalid_keys.1);
        let signed_token = super::SignedToken {
            serialised_request: encoded_body.clone(), signature:  signature
        };
        let signed_message = super::SignedMessage::new_from_token(signed_token.clone());

        assert!(signed_message.is_ok());

        let signed_message = signed_message.unwrap();

        assert_eq!(signed_message.get_routing_message(), &routing_message);
        assert_eq!(signed_message.claimant(), &claimant);

        let signed_message_encoded_body = signed_message.encoded_body();

        assert!(signed_message_encoded_body.is_ok());

        let signed_message_encoded_body = signed_message_encoded_body.unwrap();

        assert_eq!(signed_message_encoded_body, encoded_body);

        let signature =
                ::sodiumoxide::crypto::sign::sign_detached(&signed_message_encoded_body, &keys.1);

        assert!(signed_message.signature() != &signature);
        assert!(!signed_message.verify_signature(&keys.0));

        let signed_message_as_token = signed_message.as_token();

        assert!(signed_message_as_token.is_ok());
        assert_eq!(signed_message_as_token.unwrap(), signed_token);
    }
}