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
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// 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.

use crate::client::id::SafeKey;
use crate::client::MDataInfo;
use crate::crypto::{shared_box, shared_secretbox};
use crate::errors::CoreError;
use crate::utils::{SymEncKey, SymEncNonce, SYM_ENC_KEY_LEN, SYM_ENC_NONCE_LEN};
use crate::DIR_TAG;
use bincode::{deserialize, serialize};
use hmac::Hmac;
use miscreant::aead::Aead;
use miscreant::aead::Aes128SivAead;
use pbkdf2;
use rand::{thread_rng, CryptoRng, Rng};
use safe_nd::{ClientFullId, MDataKind, PublicKey, XorName, XOR_NAME_LEN};
use serde::{Deserialize, Serialize};
use sha3::Sha3_256;
use std::convert::TryInto;
use threshold_crypto;
use tiny_keccak::sha3_256;
use unwrap::unwrap;

const ITERATIONS: usize = 10000;

/// Object representing the User Account information on the network.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Account {
    /// The User Account Keys.
    pub maid_keys: ClientKeys,
    /// The user's access container.
    pub access_container: MDataInfo,
    /// The user's configuration directory.
    pub config_root: MDataInfo,
    /// Set to `true` when all root and standard containers
    /// have been created successfully. `false` signifies that
    /// previous attempt might have failed - check on login.
    pub root_dirs_created: bool,
}

impl Account {
    /// Creates new Account with a provided set of keys.
    pub fn new(maid_keys: ClientKeys) -> Result<Self, CoreError> {
        Ok(Self {
            maid_keys,
            access_container: MDataInfo::random_private(MDataKind::Seq, DIR_TAG)?,
            config_root: MDataInfo::random_private(MDataKind::Seq, DIR_TAG)?,
            root_dirs_created: false,
        })
    }

    /// Symmetrically encrypts Account using User's credentials.
    ///
    /// Credentials are passed through key-derivation-function first.
    pub fn encrypt(&self, password: &[u8], pin: &[u8]) -> Result<Vec<u8>, CoreError> {
        let serialised_self = serialize(self)?;
        let (key, nonce) = Self::generate_crypto_keys(password, pin)?;
        let mut cipher = Aes128SivAead::new(&key);

        Ok(cipher.seal(&nonce, &[], &serialised_self))
    }

    /// Symmetrically decrypts Account using User's credentials.
    ///
    /// Credentials are passed through key-derivation-function first.
    pub fn decrypt(encrypted_self: &[u8], password: &[u8], pin: &[u8]) -> Result<Self, CoreError> {
        let (key, nonce) = Self::generate_crypto_keys(password, pin)?;
        let mut cipher = Aes128SivAead::new(&key);

        let decrypted_self = cipher
            .open(&nonce, &[], &encrypted_self)
            .map_err(|_| CoreError::SymmetricDecipherFailure)?;

        Ok(deserialize(&decrypted_self)?)
    }

    /// Generates User's Identity for the network using supplied credentials in
    /// a deterministic way.  This is similar to the username in various places.
    pub fn generate_network_id(keyword: &[u8], pin: &[u8]) -> Result<XorName, CoreError> {
        let mut id = XorName([0; XOR_NAME_LEN]);
        Self::derive_key(&mut id.0[..], keyword, pin);

        Ok(id)
    }

    fn generate_crypto_keys(
        password: &[u8],
        pin: &[u8],
    ) -> Result<(SymEncKey, SymEncNonce), CoreError> {
        let mut output = [0; SYM_ENC_KEY_LEN + SYM_ENC_NONCE_LEN];
        Self::derive_key(&mut output[..], password, pin);

        // OK to unwrap here, as we guaranteed the slices have the correct length.
        let key: SymEncKey = unwrap!(output[..SYM_ENC_KEY_LEN].try_into());
        let nonce: SymEncNonce = unwrap!(output[SYM_ENC_KEY_LEN..].try_into());

        Ok((key, nonce))
    }

    fn derive_key(output: &mut [u8], input: &[u8], user_salt: &[u8]) {
        let salt = sha3_256(user_salt);
        pbkdf2::pbkdf2::<Hmac<Sha3_256>>(input, &salt, ITERATIONS, output)
    }
}

/// Client signing and encryption keypairs.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ClientKeys {
    /// This is the identity of the Client in the Network.
    pub client_id: ClientFullId,
    /// Symmetric encryption key.
    pub enc_key: shared_secretbox::Key,
    /// Encryption public key.
    pub enc_public_key: threshold_crypto::PublicKey,
    /// Encryption private key.
    pub enc_secret_key: shared_box::SecretKey,
}

impl ClientKeys {
    /// Generates random client keys, with an optional seed.
    ///
    /// Only signing keys are generated from the seed.
    pub fn new<T: CryptoRng + Rng>(rng: &mut T) -> Self {
        let (enc_public_key, enc_secret_key) = shared_box::gen_keypair();
        let enc_key = shared_secretbox::gen_key();
        let client_id = ClientFullId::new_bls(rng);

        Self {
            enc_public_key,
            enc_secret_key,
            enc_key,
            client_id,
        }
    }

    /// Converts `ClientKeys` into a Client `SafeKey`.
    pub fn client_safe_key(&self) -> SafeKey {
        SafeKey::client(self.client_id.clone())
    }

    /// Returns the associated public key.
    pub fn public_key(&self) -> PublicKey {
        *self.client_id.public_id().public_key()
    }
}

impl Default for ClientKeys {
    fn default() -> Self {
        Self::new(&mut thread_rng())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bincode::{deserialize, serialize};
    use std::u32;

    // Test deterministically generating User's Identity for the network using supplied credentials.
    #[test]
    fn generate_network_id() {
        let keyword1 = b"user1";

        let user1_id1 = unwrap!(Account::generate_network_id(keyword1, b"0"));
        let user1_id2 = unwrap!(Account::generate_network_id(keyword1, b"1234"));
        let user1_id3 = unwrap!(Account::generate_network_id(
            keyword1,
            u32::MAX.to_string().as_bytes(),
        ));

        assert_ne!(user1_id1, user1_id2);
        assert_ne!(user1_id1, user1_id3);
        assert_ne!(user1_id2, user1_id3);

        assert_eq!(
            user1_id1,
            unwrap!(Account::generate_network_id(keyword1, b"0"))
        );
        assert_eq!(
            user1_id2,
            unwrap!(Account::generate_network_id(keyword1, b"1234"))
        );
        assert_eq!(
            user1_id3,
            unwrap!(Account::generate_network_id(
                keyword1,
                u32::MAX.to_string().as_bytes(),
            ))
        );

        let keyword2 = b"user2";
        let user1_id = unwrap!(Account::generate_network_id(keyword1, b"248"));
        let user2_id = unwrap!(Account::generate_network_id(keyword2, b"248"));

        assert_ne!(user1_id, user2_id);
    }

    // Test deterministically generating cryptographic keys.
    #[test]
    fn generate_crypto_keys() {
        let password1 = b"super great password";
        let password2 = b"even better password";

        let keys1 = unwrap!(Account::generate_crypto_keys(password1, b"0"));
        let keys2 = unwrap!(Account::generate_crypto_keys(password1, b"1234"));
        let keys3 = unwrap!(Account::generate_crypto_keys(
            password1,
            u32::MAX.to_string().as_bytes(),
        ));
        assert_ne!(keys1, keys2);
        assert_ne!(keys1, keys3);
        assert_ne!(keys2, keys3);

        let keys1 = unwrap!(Account::generate_crypto_keys(password1, b"0"));
        let keys2 = unwrap!(Account::generate_crypto_keys(password2, b"0"));
        assert_ne!(keys1, keys2);

        let keys1 = unwrap!(Account::generate_crypto_keys(password1, b"0"));
        let keys2 = unwrap!(Account::generate_crypto_keys(password1, b"0"));
        assert_eq!(keys1, keys2);
    }

    // Test serialising and deserialising accounts.
    #[test]
    fn serialisation() {
        let account = unwrap!(Account::new(ClientKeys::default()));
        let encoded = unwrap!(serialize(&account));
        let decoded: Account = unwrap!(deserialize(&encoded));

        assert_eq!(decoded, account);
    }

    // Test encryption and decryption of accounts.
    #[test]
    fn encryption() {
        let account = unwrap!(Account::new(ClientKeys::default()));

        let password = b"impossible to guess";
        let pin = b"1000";

        let encrypted = unwrap!(account.encrypt(password, pin));
        let encoded = unwrap!(serialize(&account));
        assert!(!encrypted.is_empty());
        assert_ne!(encrypted, encoded);

        let decrypted = unwrap!(Account::decrypt(&encrypted, password, pin));
        assert_eq!(account, decrypted);
    }
}