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
/*!
for async examples see <a href="https://crates.io/crates/networking">crates.io</a>

## Sync Server
```
use networking::{syncronous::SyncHost, test_config, ArtificeConfig, ArtificePeer, ArtificeStream};

    let (peer, config) = test_config();
    let host = SyncHost::from_host_data(&config).unwrap();
    for netstream in host {
        println!("new connection");
        let mut stream = netstream.unwrap().verify(&peer).unwrap();
        stream.send(b"hello world").unwrap();
        break;
    }
```
## Sync Client
```
use networking::{syncronous::SyncHost, test_config, ArtificeConfig, ArtificePeer};
use std::{thread, time::Duration};

    let (peer, config) = test_config();
    //thread::sleep(Duration::from_millis(200));
    let host = SyncHost::client_only(&config).unwrap();
    let mut stream = host.connect(peer).unwrap();
    println!("connected");
    let mut buffer = Vec::new();
    println!("about to read from sream");
    println!(
        "got {} bytes from server",
        stream.recv(&mut buffer).unwrap()
    );
    println!("read from stream");
    let string = String::from_utf8(buffer).unwrap();
    println!("got message: {} from server", string);

```
*/
#![feature(maybe_uninit_ref)]
#![feature(ip)]
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate lazy_static;
/// contains blowfish encryption wrapper, as well as storage solution (serde) for BigUint principly BigNum
pub mod encryption;
pub mod error;
pub use encryption::*;
pub mod asyncronous;
/// contains the ArtificePeer struct
pub mod peers;
/// used for permission requests in the manager crate
pub mod query;
pub mod syncronous;
use crate::encryption::PubKeyComp;
use crate::error::NetworkError;
pub use peers::*;
use rsa::{RSAPrivateKey, RSAPublicKey};
use std::net::{IpAddr, SocketAddr};
use std::{
    convert::TryInto,
    net::UdpSocket,
    sync::mpsc::{channel, RecvTimeoutError, Sender},
    thread,
    time::Duration,
};
/// this function is used for testing since it takes so much time to generate private key
fn get_private_key() -> RSAPrivateKey {
    use num_bigint_dig::BigUint;

    let n = BigUint::from_bytes_be(&[
        214, 82, 135, 64, 34, 118, 248, 217, 78, 42, 130, 198, 138, 28, 117, 66, 23, 74, 77, 139,
        51, 187, 14, 166, 48, 66, 217, 18, 157, 202, 175, 238, 60, 29, 39, 85, 58, 33, 145, 25, 55,
        214, 51, 72, 184, 142, 141, 183, 54, 186, 102, 59, 131, 86, 167, 220, 74, 65, 65, 43, 42,
        104, 182, 108, 144, 96, 238, 52, 145, 126, 239, 25, 41, 16, 221, 86, 26, 27, 87, 100, 171,
        205, 80, 216, 235, 202, 125, 204, 231, 254, 67, 77, 100, 154, 173, 137, 14, 154, 200, 188,
        123, 41, 96, 177, 19, 105, 23, 230, 2, 249, 66, 147, 107, 152, 108, 37, 203, 68, 228, 119,
        255, 64, 4, 53, 106, 145, 228, 191, 24, 199, 3, 232, 39, 170, 123, 227, 204, 68, 97, 216,
        182, 169, 82, 144, 88, 184, 84, 127, 118, 168, 50, 192, 241, 111, 15, 32, 12, 130, 25, 74,
        107, 187, 126, 154, 154, 194, 29, 56, 122, 3, 208, 65, 206, 216, 3, 22, 113, 227, 55, 55,
        206, 184, 132, 85, 236, 144, 7, 146, 98, 220, 37, 208, 81, 82, 21, 166, 151, 67, 14, 73,
        194, 84, 242, 234, 202, 111, 159, 5, 100, 93, 52, 120, 243, 86, 116, 123, 114, 52, 184, 74,
        99, 96, 120, 124, 19, 53, 161, 84, 133, 163, 144, 88, 163, 17, 171, 101, 164, 223, 169, 15,
        63, 80, 2, 207, 95, 248, 66, 244, 196, 207, 68, 49, 45, 25, 95, 255,
    ]);
    let e = BigUint::from_bytes_be(&[1, 0, 1]);
    let d = BigUint::from_bytes_be(&[
        11, 49, 174, 76, 196, 64, 16, 137, 81, 18, 217, 87, 195, 88, 239, 42, 239, 138, 122, 248,
        93, 80, 84, 216, 139, 70, 194, 141, 189, 94, 27, 200, 70, 173, 131, 35, 105, 112, 221, 98,
        66, 207, 86, 72, 99, 19, 87, 5, 141, 191, 56, 157, 189, 35, 102, 139, 19, 249, 202, 140,
        149, 159, 120, 127, 189, 30, 194, 242, 77, 243, 34, 75, 223, 32, 57, 95, 72, 231, 202, 173,
        192, 103, 109, 228, 150, 148, 49, 243, 228, 222, 27, 231, 203, 236, 100, 73, 247, 80, 80,
        81, 191, 225, 14, 98, 209, 79, 184, 230, 211, 154, 239, 70, 65, 229, 105, 40, 164, 48, 73,
        250, 150, 131, 98, 116, 227, 199, 16, 250, 109, 169, 223, 94, 194, 200, 235, 244, 81, 141,
        113, 70, 119, 176, 15, 189, 108, 182, 211, 139, 9, 72, 167, 91, 225, 129, 247, 159, 121,
        242, 5, 23, 91, 140, 221, 19, 184, 19, 24, 255, 255, 220, 67, 192, 150, 40, 159, 117, 191,
        35, 175, 156, 230, 134, 246, 25, 89, 45, 105, 99, 44, 246, 35, 232, 239, 248, 1, 250, 42,
        150, 86, 234, 244, 142, 204, 108, 149, 31, 236, 200, 217, 36, 169, 27, 254, 17, 99, 82, 80,
        46, 228, 129, 124, 79, 28, 156, 82, 145, 111, 220, 16, 204, 203, 24, 220, 246, 241, 15,
        206, 116, 152, 29, 56, 164, 237, 164, 101, 52, 139, 219, 66, 24, 55, 128, 116, 219, 189,
        217,
    ]);
    let primes = vec![
        BigUint::from_bytes_be(&[
            218, 18, 255, 185, 216, 198, 202, 164, 130, 230, 249, 202, 68, 69, 5, 246, 219, 99,
            170, 211, 253, 14, 210, 149, 41, 44, 110, 173, 245, 102, 19, 175, 3, 243, 129, 86, 166,
            104, 105, 78, 11, 162, 161, 239, 8, 206, 97, 222, 183, 132, 115, 39, 49, 172, 164, 33,
            43, 135, 173, 254, 37, 14, 185, 213, 129, 255, 31, 152, 100, 138, 247, 156, 59, 183,
            46, 242, 184, 222, 208, 55, 159, 29, 5, 192, 123, 12, 186, 176, 32, 237, 151, 159, 190,
            44, 144, 186, 37, 149, 107, 154, 19, 116, 16, 196, 53, 166, 113, 122, 192, 87, 122,
            124, 252, 84, 221, 91, 56, 15, 55, 74, 92, 56, 82, 176, 104, 149, 87, 195,
        ]),
        BigUint::from_bytes_be(&[
            251, 152, 125, 227, 130, 36, 160, 198, 157, 132, 251, 120, 127, 208, 105, 53, 3, 219,
            28, 218, 154, 192, 227, 251, 141, 201, 136, 237, 210, 125, 215, 149, 134, 32, 240, 187,
            116, 102, 200, 255, 174, 116, 14, 121, 133, 65, 96, 31, 211, 100, 81, 152, 87, 243,
            210, 108, 0, 120, 144, 148, 74, 68, 183, 245, 111, 38, 138, 127, 60, 84, 53, 100, 203,
            208, 82, 220, 91, 30, 137, 202, 143, 93, 123, 7, 206, 144, 171, 174, 124, 125, 114,
            116, 226, 238, 225, 189, 243, 122, 47, 123, 185, 100, 250, 200, 25, 248, 70, 89, 234,
            28, 113, 190, 180, 50, 253, 221, 215, 209, 8, 176, 82, 10, 248, 85, 73, 236, 79, 21,
        ]),
    ];
    RSAPrivateKey::from_components(n, e, d, primes)
}
lazy_static! {
    static ref PEERCONFIG: (ArtificePeer, ArtificeConfig) = {
        let private_key = get_private_key();
        let global_hash = "CzlFhuFsF7fCYIk1b8CnGuERPeJ2ywOzsMxSKyxTFKTAQlU8Fl".to_string();
        let peer_hash = "2o4iPLBTDzLDedP4xvijngPfaw99TszSrMS99IwxxuXhZZyOqi".to_string();
        let peer = ArtificePeer::new(
            global_hash.clone(),
            peer_hash,
            Layer3SocketAddr::from_layer3_addr(Layer3Addr::V4([127, 0, 0, 1]), 6464),
            PubKeyComp::from(&private_key),
        );
        let host_data = ArtificeHostData::new(&private_key, global_hash);
        let config = ArtificeConfig::new(Layer3Addr::V4([0, 0, 0, 0]), 6464, host_data, false);
        (peer, config)
    };
}
pub fn test_config() -> (ArtificePeer, ArtificeConfig){
    (PEERCONFIG.0.clone(), PEERCONFIG.1.clone())
}
/// used to build and configure the local host
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ArtificeConfig {
    broadcast: bool,
    address: Layer3Addr,
    port: u16,
    host: ArtificeHostData,
}
impl ArtificeConfig {
    pub fn new(address: Layer3Addr, port: u16, host: ArtificeHostData, broadcast: bool) -> Self {
        Self {
            broadcast,
            address,
            port,
            host,
        }
    }
    /// used to create new host, primarily designed for use by the installer crate
    pub fn generate(address: Layer3Addr) -> Self {
        let broadcast = false;
        let port = 6464;
        let host = ArtificeHostData::default();
        Self {
            broadcast,
            address,
            port,
            host,
        }
    }
    pub fn host_data(&self) -> ArtificeHostData {
        self.host.clone()
    }
    pub fn broadcast(&self) -> bool {
        self.broadcast
    }
    pub fn port(&self) -> u16 {
        self.port
    }
    pub fn address(&self) -> Layer3Addr {
        self.address
    }
}

/// provides a means of saving private keys to files, because the process of generating the keys takes a really long time, but creating them from existing values does not
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ArtificeHostData {
    priv_key: PrivKeyComp,
    global_peer_hash: String,
}
impl Default for ArtificeHostData {
    fn default() -> Self {
        let global_peer_hash = random_string(50);
        let priv_key = PrivKeyComp::generate().unwrap();
        Self {
            priv_key,
            global_peer_hash,
        }
    }
}
impl ArtificeHostData {
    pub fn new(private_key: &RSAPrivateKey, global_peer_hash: String) -> Self {
        let priv_key = PrivKeyComp::from(private_key);
        Self {
            priv_key,
            global_peer_hash,
        }
    }
    pub fn private_key(&self) -> PrivKeyComp {
        self.priv_key.clone()
    }
    pub fn global_peer_hash(&self) -> String {
        self.global_peer_hash.clone()
    }
}
/// contains peer information sent accross the network in an effort to prevent man in the middle attacks
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct Header {
    peer: ArtificePeer,
    pubkey: PubKeyComp,
    packet_len: usize,
    new_connection: bool,
}
impl PartialEq for Header {
    fn eq(&self, other: &Self) -> bool {
        self.peer == other.peer && self.pubkey == other.pubkey
    }
}
impl PartialEq<StreamHeader> for Header {
    fn eq(&self, other: &StreamHeader) -> bool {
        self.peer.global_peer_hash() == other.global_hash
            && self.peer.peer_hash() == other.peer_hash
    }
}
impl PartialEq<Header> for StreamHeader {
    fn eq(&self, other: &Header) -> bool {
        self.global_hash == other.peer.global_peer_hash()
            && self.peer_hash == other.peer.peer_hash()
    }
}
impl Header {
    pub fn new(peer: ArtificePeer, pubkey: PubKeyComp) -> Self {
        Self {
            peer,
            pubkey,
            packet_len: 0,
            new_connection: false,
        }
    }
    pub fn new_connection(peer: ArtificePeer, pubkey: PubKeyComp) -> Self {
        Self {
            peer,
            pubkey,
            packet_len: 0,
            new_connection: true,
        }
    }
    pub fn stream_header(&self) -> StreamHeader {
        StreamHeader::new(
            self.peer.global_peer_hash(),
            self.peer.peer_hash(),
            self.packet_len,
        )
    }
    pub fn peer(&self) -> &ArtificePeer {
        &self.peer
    }
    pub fn pubkey(&self) -> RSAPublicKey {
        RSAPublicKey::new(self.pubkey.n(), self.pubkey.e()).unwrap()
    }
    pub fn packet_len(&self) -> usize {
        self.packet_len
    }
    pub fn set_len(&mut self, len: usize) {
        self.packet_len = len;
    }
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub struct StreamHeader {
    global_hash: String,
    peer_hash: String,
    aes_key: Vec<u8>,
    packet_len: usize,
    remander: u8,
}
impl StreamHeader {
    pub fn new(global_hash: String, peer_hash: String, packet_len: usize) -> Self {
        let aes_key = random_string(16).into_bytes();
        Self {
            global_hash,
            peer_hash,
            aes_key,
            packet_len,
            remander: 0,
        }
    }
    pub fn key(&self) -> &[u8]{
        &self.aes_key
    }
    pub fn packet_len(&self) -> usize {
        self.packet_len
    }
    pub fn data_len(&self) -> usize{
        self.packet_len - (self.remander as usize)
    }
    pub fn set_packet_len(&mut self, packet_len: usize) {
        self.packet_len = packet_len;
    }
    /// remander is calculated by 128 - (packet_len % 128) to break into encryptable blocks
    pub fn remander(&self) -> u8 {
        self.remander
    }
    pub fn set_remander(&mut self, remander: u8) {
        self.remander = remander;
    }
    /// used in place of serde_json::to_string(), owing to that method generating extra bytes
    pub fn to_raw(&self) -> Vec<u8> {
        let mut outvec = Vec::with_capacity(125);
        outvec.extend_from_slice(&self.global_hash.as_bytes());
        outvec.extend_from_slice(&self.peer_hash.as_bytes());
        outvec.extend_from_slice(&self.aes_key);
        outvec.extend_from_slice(&self.packet_len.to_be_bytes());
        outvec.push(self.remander);
        outvec
    }
    pub fn from_raw(data: &[u8]) -> Result<Self, NetworkError> {
        assert_eq!(data.len(), 125);
        let global_hash = String::from_utf8(data[0..50].to_vec())?;
        let peer_hash = String::from_utf8(data[50..100].to_vec())?;
        let aes_key = data[100..116].to_vec();
        let packet_len = usize::from_be_bytes(data[116..124].try_into()?);
        let remander = data[124];
        Ok(Self {
            global_hash,
            peer_hash,
            aes_key,
            packet_len,
            remander,
        })
    }
}
#[test]
fn header_to_raw_from_raw() {
    let stream_header = StreamHeader::new(random_string(50), random_string(50), 0);
    let raw = stream_header.to_raw();
    let new_header = StreamHeader::from_raw(&raw).unwrap();
    assert_eq!(stream_header, new_header);
}
/// trait used to implement common features between async and sync implementations of networking
pub trait ArtificeStream {
    type NetStream;
    fn new(
        stream: Self::NetStream,
        priv_key: RSAPrivateKey,
        peer: ArtificePeer,
        remote_addr: SocketAddr,
    ) -> Self;
    fn addr(&self) -> IpAddr {
        self.socket_addr().ip()
    }
    fn socket_addr(&self) -> SocketAddr;
    fn pubkey(&self) -> RSAPublicKey;
    fn peer(&self) -> &ArtificePeer;
    fn header(&self) -> Header;
}
pub trait ArtificeHost {
    fn begin_broadcast(socket_addr: SocketAddr) -> std::io::Result<Sender<bool>> {
        let (sender, recv) = channel();
        let socket = UdpSocket::bind(socket_addr)?;
        socket.set_broadcast(true)?;
        thread::spawn(move || loop {
            match recv.recv_timeout(Duration::from_millis(200)) {
                Ok(_) => break,
                Err(e) => match e {
                    RecvTimeoutError::Timeout => continue,
                    RecvTimeoutError::Disconnected => break,
                },
            }
        });
        Ok(sender)
    }
    fn stop_broadcasting(&self);
}
pub struct ConnectionRequest<T: ArtificeStream> {
    stream: T,
}
impl<T: ArtificeStream> ConnectionRequest<T> {
    pub fn new(stream: T) -> Self {
        Self { stream }
    }
    /// used to ensure only known peers are allow to connect
    pub fn verify<L: PeerList>(self, list: &L) -> Result<T, NetworkError> {
        if list.verify_peer(&self.stream.peer()) {
            Ok(self.stream)
        } else {
            Err(NetworkError::ConnectionDenied(
                "verification of peer failed".to_string(),
            ))
        }
    }
    /// # Safety
    /// this function allows unauthorized peers to connect to this device
    /// should only be used if a pair request is being run
    pub unsafe fn unverify(self) -> T {
        self.stream
    }
}