      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

    use connection::{Connection, ConnectionType};
    use crypto::curve25519;
    use crypto::digest::Digest;
    use crypto::sha2::Sha256;
    use key_exchange::{KexResult, KeyExchange};
    use message::MessageType;
    use num_bigint::{BigInt, Sign};
    use packet::{Packet, ReadPacketExt, WritePacketExt};
    use rand::{self, Rng};

    const ECDH_KEX_INIT: u8 = 30;
    const ECDH_KEX_REPLY: u8 = 31;

    pub struct Curve25519 {
        shared_secret: Option<Vec<u8>>,
        exchange_hash: Option<Vec<u8>>,
    }

    impl Curve25519 {
        pub fn new() -> Curve25519 {
            Curve25519 {
                shared_secret: None,
                exchange_hash: None,
            }
        }
    }

    impl KeyExchange for Curve25519 {
        fn shared_secret<'a>(&'a self) -> Option<&'a [u8]> {
            self.shared_secret.as_ref().map(|x| x as &[u8])
        }

        fn exchange_hash<'a>(&'a self) -> Option<&'a [u8]> {
            self.exchange_hash.as_ref().map(|x| x.as_slice())
        }

        fn hash(&self, data: &[&[u8]]) -> Vec<u8> {
            let mut hash = [0; 32];
            let mut hasher = Sha256::new();

            for item in data {
                hasher.input(item);
            }

            hasher.result(&mut hash);
            hash.to_vec()
        }

        fn process(&mut self, conn: &mut Connection, packet: Packet) -> KexResult {
            match packet.msg_type()
            {
                MessageType::KeyExchange(ECDH_KEX_INIT) => {
                    let mut reader = packet.reader();
                    let client_public = reader.read_string().unwrap();

                    let config = match &conn.conn_type
                    {
                        &ConnectionType::Server(ref config) => config.clone(),
                        _ => return KexResult::Error,
                    };

                    let public_key = {
                        let mut key = Vec::new();
                        config.as_ref().key.write_public(&mut key).unwrap();
                        key
                    };

                    let mut packet =
                        Packet::new(MessageType::KeyExchange(ECDH_KEX_REPLY));

                    let server_secret = {
                        let mut secret = [0; 32];
                        let mut rng = rand::thread_rng();
                        rng.fill_bytes(&mut secret);

                        secret[0] &= 248;
                        secret[31] &= 127;
                        secret[31] |= 64;

                        secret
                    };

                    let server_public = curve25519::curve25519_base(&server_secret);
                    let shared_secret = {
                        let mut buf = Vec::new();
                        buf.write_mpint(BigInt::from_bytes_be(
                            Sign::Plus,
                            &curve25519::curve25519(&server_secret, &client_public),
                        )).ok();
                        buf
                    };

                    let hash_data = {
                        let mut buf = Vec::new();
                        let data = &conn.hash_data;

                        let items =
                            [
                                data.client_id.as_ref().unwrap().as_bytes(),
                                data.server_id.as_ref().unwrap().as_bytes(),
                                data.client_kexinit.as_ref().unwrap().as_slice(),
                                data.server_kexinit.as_ref().unwrap().as_slice(),
                                public_key.as_slice(),
                                client_public.as_slice(),
                                &server_public,
                            ];

                        for item in items.iter() {
                            buf.write_bytes(item).ok();
                        }

                        buf.write_raw_bytes(&shared_secret).ok();

                        buf
                    };

                    // Calculate hash
                    let hash = self.hash(&[hash_data.as_slice()]);
                    let signature = config.as_ref().key.sign(&hash).unwrap();

                    packet.write_bytes(public_key.as_slice()).unwrap();
                    packet.write_bytes(&server_public).unwrap();
                    packet.write_bytes(signature.as_slice()).unwrap(); // Signature

                    self.exchange_hash = Some(hash);
                    self.shared_secret = Some(shared_secret);

                    KexResult::Done(packet)
                }
                _ => {
                    debug!("Unhandled key exchange packet: {:?}", packet);
                    KexResult::Error
                }
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)