discret 0.6.2

A backend to create peer to peer (P2P) applications, using a GraphQL inspired syntax
Documentation
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::{
    fs::File,
    io::{Read, Write},
    path::PathBuf,
};

use crate::date_utils::now;
use argon2::{self, Config, Variant, Version};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD as enc64, Engine as _};
use ed25519_dalek::{SignatureError, Signer, Verifier};
use rand::{rngs::OsRng, RngCore};
use rcgen::{CertificateParams, KeyPair, SanType};
use serde::{Deserialize, Serialize};
use sysinfo::System;
use thiserror::Error;
use x25519_dalek::{PublicKey, StaticSecret};

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    InvalidKeyType(u8),

    #[error("{0}")]
    InvalidKeyLenght(String),

    #[error("{0}")]
    InvalidSignature(String),

    #[error(transparent)]
    Signature(#[from] SignatureError),

    #[error(transparent)]
    Decode(#[from] base64::DecodeError),

    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error("Invalid Base64 encoded Uid")]
    Uid(),

    #[error("Invalid Base64 encoded MeetingToken")]
    MeetingToken(),
}

///
/// when exporting a key the first byte is a flag indicating the public key algorithm used
/// currenlty useless but might become usefull in the future to implement new key algorithms
///
const KEY_TYPE_ED_2519: u8 = 1;

///
/// import a existing signing key, using the first byte flag to detect the signature scheme
///
///
#[cfg(test)]
pub fn import_signing_key(keypair: &[u8]) -> Result<impl SigningKey, Error> {
    if keypair[0] != KEY_TYPE_ED_2519 {
        return Err(Error::InvalidKeyType(KEY_TYPE_ED_2519));
    }
    if keypair.len() != 33 {
        return Err(Error::InvalidKeyLenght(format!(
            "key lenght must be 33,  value: {} ",
            keypair.len()
        )));
    }

    let ke: [u8; 32] = keypair[1..33].try_into().unwrap();
    let keypair = ed25519_dalek::SigningKey::from(ke);

    Ok(Ed25519SigningKey {
        signing_key: keypair,
    })
}

///
/// import a existing verifying key, using the first byte flag to detect the signature scheme
///
pub fn import_verifying_key(veriying_key: &[u8]) -> Result<Box<dyn VerifyingKey>, Error> {
    if veriying_key[0] != KEY_TYPE_ED_2519 {
        return Err(Error::InvalidKeyType(KEY_TYPE_ED_2519));
    }
    if veriying_key.len() != 33 {
        return Err(Error::InvalidKeyLenght(format!(
            "key lenght must be 33,  value: {} ",
            veriying_key.len()
        )));
    }

    let ke: [u8; 32] = veriying_key[1..33].try_into().unwrap();

    let veriying_key = ed25519_dalek::VerifyingKey::from_bytes(&ke)?;
    Ok(Box::new(Ed2519VerifyingKey { veriying_key }))
}

///
/// Signing key using Ed25519 signature scheme
///
pub struct Ed25519SigningKey {
    signing_key: ed25519_dalek::SigningKey,
}

impl Ed25519SigningKey {
    ///
    /// new key using a random number
    ///
    pub fn new() -> Self {
        let random: [u8; 32] = random32();
        Ed25519SigningKey::create_from(&random)
    }

    ///
    /// creates a signing key using a provided random number
    /// usefull to create a predictable key from a key derivation function
    ///
    pub fn create_from(random: &[u8; 32]) -> Self {
        let sk: &ed25519_dalek::SecretKey = random;
        Ed25519SigningKey {
            signing_key: ed25519_dalek::SigningKey::from(sk),
        }
    }
}

///
/// Defines the necessary functions to sign  data  
///
pub trait SigningKey {
    ///
    /// Export the signing key, adding a flag to detect the encryption scheme
    ///
    fn export(&self) -> Vec<u8>;

    ///
    /// Exports the verifying key, adding a a flag to detect the encryption scheme
    ///
    fn export_verifying_key(&self) -> Vec<u8>;

    ///
    /// Provides a copy of the verifying key
    ///
    fn verifying_key(&self) -> impl VerifyingKey;

    ///
    /// Sign a message, returning the signature
    /// passed message should be small, like a hash of the real message
    ///
    fn sign(&self, message: &[u8]) -> Vec<u8>;
}

impl SigningKey for Ed25519SigningKey {
    fn export(&self) -> Vec<u8> {
        let mut export = vec![KEY_TYPE_ED_2519];
        let keyp = self.signing_key.to_bytes();
        export.extend(keyp);
        export
    }

    fn export_verifying_key(&self) -> Vec<u8> {
        let mut export = vec![KEY_TYPE_ED_2519];
        let keyp = self.signing_key.verifying_key().to_bytes();
        export.extend(keyp);
        export
    }

    fn sign(&self, message: &[u8]) -> Vec<u8> {
        self.signing_key.sign(message).to_bytes().into()
    }

    fn verifying_key(&self) -> impl VerifyingKey {
        Ed2519VerifyingKey {
            veriying_key: self.signing_key.verifying_key(),
        }
    }
}

///
/// Defines the necessary function to verify data  
///
pub trait VerifyingKey {
    ///
    /// Export the verifying key, adding a flag to detect the encryption scheme
    ///
    //  fn export(&self) -> Vec<u8>;

    ///
    /// verify the signature against the provided message
    ///
    fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), Error>;
}

///
/// verification key using Ed25519 signature scheme
///
pub struct Ed2519VerifyingKey {
    pub veriying_key: ed25519_dalek::VerifyingKey,
}
impl VerifyingKey for Ed2519VerifyingKey {
    // fn export(&self) -> Vec<u8> {
    //     let mut export = vec![KEY_TYPE_ED_2519];
    //     let keyp = self.veriying_key.to_bytes();
    //     export.extend(keyp);
    //     export
    // }

    fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), Error> {
        if signature.len() != 64 {
            return Err(Error::InvalidKeyLenght(format!(
                "signatue lenght must be 64,  value: {} ",
                signature.len()
            )));
        }
        let sign: [u8; 64] = signature.try_into().unwrap();

        let sig = ed25519_dalek::Signature::from_bytes(&sign);
        self.veriying_key.verify(data, &sig)?;
        Ok(())
    }
}
///
/// generates a self signed certificate
///
pub fn generate_x509_certificate(name: &str) -> rcgen::CertifiedKey {
    let mut params: CertificateParams = Default::default();

    params.subject_alt_names = vec![SanType::DnsName(name.try_into().unwrap())];
    let key_pair = KeyPair::generate_for(&rcgen::PKCS_ED25519).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();
    // let cert: rcgen::CertifiedKey = rcgen::generate_simple_self_signed(vec![name]).unwrap();
    rcgen::CertifiedKey { cert, key_pair }
}

///
/// The quick connection initiate connection with a domain name used during TLS negociations to avoid man in the middle attack.
/// As we are using p2p and self signed certificates, we don't have a domain name to connect to we have to generate one.
///
/// This name is transmitted in clear text over the network as part of the QUIC handshake.
/// If the name is too weird (like a base64 encoded value), it could be easy to detect that the connection is a Discret connection, and not a standard HTTP3 one.
/// So we try to be a little bit sneaky by creating a plausible domain names.
///
/// This functiion is public because it is used by the discret_beacon projet
///
pub fn random_domain_name() -> String {
    let min_value = 5;
    let max_value = 8;
    let divider = max_value - min_value;
    let offset: usize = OsRng.next_u32().try_into().unwrap();
    let num = offset % divider;

    let alphabet = [
        'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'v', 'w', 'x', 'z',
    ];
    let vowels = ['a', 'e', 'i', 'o', 'u', 'y'];
    let extension = [
        ".com", ".org", ".net", ".us", ".co", ".biz", ".info", ".fr", ".uk", ".me", ".cn", ".de",
        ".ly", ".in", ".eu", ".ca", ".coop", ".asia", ".cat", ".pro", ".ac", ".ad", ".ae", ".af",
        ".ai", ".am",
    ];
    let size = num + min_value;
    let mut domain = String::new();

    let mut vowel = OsRng.next_u32() % 2 == 1;
    for _ in 0..size {
        let mut index: usize = OsRng.next_u32().try_into().unwrap();
        if vowel {
            index %= vowels.len();
            domain.push(vowels[index]);
            vowel = false;
        } else {
            index %= alphabet.len();
            domain.push(alphabet[index]);
            vowel = true;
        }
    }
    let mut index: usize = OsRng.next_u32().try_into().unwrap();
    index %= extension.len();
    domain.push_str(extension[index]);

    domain
}

pub fn random32() -> [u8; 32] {
    let mut random: [u8; 32] = [0; 32];

    OsRng.fill_bytes(&mut random);
    random
}

pub const MEETING_TOKEN_SIZE: usize = 7;
pub type MeetingToken = [u8; MEETING_TOKEN_SIZE];
///
/// Use Diffie Hellman to create an id to be used to announce yourself on the network to the other peers.
/// The id is way too small to be secured, but it is big enougth to have a low collision rate
/// this will allow peer to recognize themselves on the network
/// The authentication is performed using a signature
///
/// The connection protocol is
///     generate a self signed certificate
///     for each allowed peer generate the diffie hellman id an put it in an array
///     sign the cerificate and the array with the public_key used to insert data
///     broadcast this data
///     in one single packet you can announce yoursef to other peers
///     upeon retrieval
///         peer will check if there is an id corresponding
///         this is will allow to retrieve the peer verifying key
///         verify authenticity
///         start connection
///         
pub struct MeetingSecret {
    secret: StaticSecret,
}
impl MeetingSecret {
    pub fn new(bytes: [u8; 32]) -> Self {
        Self {
            secret: StaticSecret::from(bytes),
        }
    }

    pub fn public_key(&self) -> PublicKey {
        PublicKey::from(&self.secret)
    }

    pub fn token(&self, their_public: &PublicKey) -> MeetingToken {
        //if both public key are the same, it is the same user and hash the private key instead of using diffie hellman
        let hash = if their_public.eq(&self.public_key()) {
            hash(self.secret.as_bytes())
        } else {
            let df = self.secret.diffie_hellman(their_public);
            hash(df.as_bytes())
        };
        let mut token: MeetingToken = [0; MEETING_TOKEN_SIZE];
        token.copy_from_slice(&hash[0..MEETING_TOKEN_SIZE]);
        token
    }

    /// derive a token from a string context and a secret
    /// provided by the Blake3 hash function  
    ///
    pub fn derive_token(context: &str, key_material: &[u8]) -> MeetingToken {
        let hash = blake3::derive_key(context, key_material);
        let mut token: MeetingToken = [0; MEETING_TOKEN_SIZE];
        token.copy_from_slice(&hash[0..MEETING_TOKEN_SIZE]);
        token
    }

    pub fn decode_token(base64: &str) -> Result<MeetingToken, Error> {
        let r = base64_decode(base64.as_bytes()).map_err(|_| Error::MeetingToken())?;
        let mut token: MeetingToken = [0; MEETING_TOKEN_SIZE];
        if r.len() < MEETING_TOKEN_SIZE {
            return Err(Error::MeetingToken());
        }
        token.copy_from_slice(&r[0..MEETING_TOKEN_SIZE]);

        Ok(token)
    }
}

///
/// Derive a password using argon2id
///  using parameters slighly greater than the minimum recommended by OSWAP <https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html>
/// - 20480 KB of memory
/// - an iteration count of 2
/// - parallelism count of 2
/// - the login is used as a salt
///
pub fn derive_pass_phrase(login: &str, pass_phrase: &str) -> [u8; 32] {
    let password = pass_phrase.as_bytes();
    let salt = hash(login.as_bytes());

    let config = Config::<'_> {
        mem_cost: 20480,
        time_cost: 2,
        variant: Variant::Argon2id,
        lanes: 2,
        version: Version::Version13,
        ..Default::default()
    };

    let hashed = argon2::hash_encoded(password, &salt, &config).unwrap();
    let matches = argon2::verify_encoded(&hashed, password).unwrap();
    assert!(matches);
    hash(hashed.as_bytes())
}

///
/// hash a byte array using the Blake3 hash function
///
pub fn hash(bytes: &[u8]) -> [u8; 32] {
    *blake3::hash(bytes).as_bytes()
}

///
/// derive a ket from a string context and a secret
/// provided by the Blake3 hash function  
///
pub fn derive_key(context: &str, key_material: &[u8]) -> [u8; 32] {
    blake3::derive_key(context, key_material)
}

///
/// encode bytes into a base 64 String
///
pub fn base64_encode(data: &[u8]) -> String {
    enc64.encode(data)
}

///
/// decode a base 64 String into bytes
///
pub fn base64_decode(data: &[u8]) -> Result<Vec<u8>, Error> {
    enc64.decode(data).map_err(Error::from)
}

pub const UID_SIZE: usize = 16;
pub type Uid = [u8; UID_SIZE];
const DEFAULT_UID: Uid = [0; UID_SIZE];
///
/// generate a 16 byte uid with the time on the first 6 bytes to improve index locality
///
///
pub fn new_uid() -> Uid {
    const TIME_BYTES: usize = 6;
    let time = now();
    let time = &time.to_be_bytes()[TIME_BYTES..];

    let mut uid = DEFAULT_UID;
    let (one, two) = uid.split_at_mut(time.len());

    one.copy_from_slice(time);
    OsRng.fill_bytes(two);

    uid
}

/// derive a ket from a string context and a secret
/// provided by the Blake3 hash function  
///
pub fn derive_uid(context: &str, key_material: &[u8]) -> Uid {
    let hash = blake3::derive_key(context, key_material);
    let mut uid = default_uid();
    uid.copy_from_slice(&hash[0..UID_SIZE]);
    uid
}

pub fn default_uid() -> Uid {
    DEFAULT_UID
}

pub fn uid_decode(base64: &str) -> Result<Uid, Error> {
    let s = base64_decode(base64.as_bytes())?;
    let uid: Uid = s.try_into().map_err(|_| Error::Uid())?;
    Ok(uid)
}

pub fn uid_encode(uid: &Uid) -> String {
    base64_encode(uid)
}

pub fn uid_from(v: Vec<u8>) -> Result<Uid, Error> {
    let uid: Uid = v.try_into().map_err(|_| Error::Uid())?;
    Ok(uid)
}

///
/// try to get a unique identifier from the underlying hardware
/// returns a unique identifier and  the machine name.
///
/// This is only used to identifies devices you own, not devices of other peers
///
/// if the platform is not supported by sysinfo,
/// creates a file named discret_fingerprint.bin and stores a random number
///
#[derive(Serialize, Deserialize, Clone)]
pub struct HardwareFingerprint {
    pub id: Uid,
    pub name: String,
}
impl HardwareFingerprint {
    pub fn get(file_path: &PathBuf) -> Result<Self, Error> {
        let id = if file_path.is_file() {
            let mut file = File::open(file_path)?;
            let mut uid = default_uid();
            file.read(&mut uid)?;
            uid
        } else {
            let uid = new_uid();
            let mut file = File::create(file_path)?;
            file.write_all(&uid)?;
            uid
        };

        let mut name = "Unknown Device".to_string();
        if sysinfo::IS_SUPPORTED_SYSTEM {
            let host = System::host_name().unwrap_or_default();
            //let system = System::name().unwrap_or_default();
            //let networks = Networks::new_with_refreshed_list();
            let osname = System::long_os_version().unwrap_or_default();
            name = format!("{osname}, {host}");
        };

        Ok(Self { id, name })
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;
    #[test]
    fn control_derive_pass_phrase() {
        let login = "test";
        let pass_phrase = "testphrase";

        let hashed = derive_pass_phrase(login, pass_phrase);

        assert_eq!(
            base64_encode(&hashed),
            "KER9-vDQvEeLBD5EAnPo52l8XEiuEO5vuaZDXOpQId0"
        );
    }

    #[test]
    fn control_hash() {
        assert_eq!(
            base64_encode(&hash(b"bytes")),
            "3xmx8QX_kpGRzknQu_3FtO3CpxpA9QLclVNZ6zNkniQ"
        );
    }

    #[test]
    fn control_ed25519() {
        let rd = hash(b"not random");
        let signing_key = Ed25519SigningKey::create_from(&rd);

        let exp_kp = signing_key.export();

        assert_eq!(
            base64_encode(signing_key.signing_key.as_bytes()),
            "RkG04WSsJLl3i6STKCBmU-sB2xqREK0VCM-qnjoq8Ik"
        );

        assert_eq!(
            base64_encode(&signing_key.export_verifying_key()[0..]),
            "AVS6nzDyEt5jfykx0UObb32ySqI4sN89Y6nL9KFp_tWO"
        );

        let msg = b"message to sign";
        let signature = signing_key.sign(&msg.to_vec());

        let keypair = import_signing_key(&exp_kp).unwrap();

        let exp_pub = keypair.export_verifying_key();
        let imp_pub = import_verifying_key(&exp_pub).unwrap();

        imp_pub.verify(msg, &signature).unwrap();
    }

    #[test]
    pub fn meeting_secret() {
        let peer1 = MeetingSecret::new(random32());
        let peer1_public = peer1.public_key();
        let peer1_public = bincode::serialize(&peer1_public.as_bytes()).unwrap();

        let peer2 = MeetingSecret::new(random32());
        let peer2_public = peer2.public_key();
        let peer2_public = bincode::serialize(&peer2_public).unwrap();

        let peer1_public: PublicKey = bincode::deserialize(&peer1_public).unwrap();
        let peer2_public: PublicKey = bincode::deserialize(&peer2_public).unwrap();

        let id1 = peer2.token(&peer1_public);
        let id2 = peer1.token(&peer2_public);
        assert_eq!(id1, id2);
    }

    #[test]
    pub fn hardware_fingerprint() {
        let path: PathBuf = "test_data/hardware_fingerprint.bin".into();
        let _ = fs::remove_file(&path);

        let id1 = HardwareFingerprint::get(&path).unwrap();
        let id2 = HardwareFingerprint::get(&path).unwrap();
        assert_eq!(id1.id, id2.id);
    }

    #[test]
    pub fn random_domain() {
        for _ in 0..50 {
            // println!("{}", random_domain_name());
            assert!(random_domain_name().len() <= 14);
        }
    }
}