2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-20 10:06:49 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-05-05 11:02:33 +02:00
|
|
|
use ethkey::{self, KeyPair, sign, Address, Signature, Message, Public, Secret};
|
|
|
|
use ethkey::crypto::ecdh::agree;
|
|
|
|
use {json, Error};
|
2017-01-30 11:44:09 +01:00
|
|
|
use account::Version;
|
2018-05-05 11:02:33 +02:00
|
|
|
use crypto;
|
2017-01-30 11:44:09 +01:00
|
|
|
use super::crypto::Crypto;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account representation.
|
2016-06-20 00:10:34 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct SafeAccount {
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account ID
|
2016-06-20 00:10:34 +02:00
|
|
|
pub id: [u8; 16],
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account version
|
2016-06-20 00:10:34 +02:00
|
|
|
pub version: Version,
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account address
|
2016-06-20 00:10:34 +02:00
|
|
|
pub address: Address,
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account private key derivation definition.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub crypto: Crypto,
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account filename
|
2016-08-03 17:58:22 +02:00
|
|
|
pub filename: Option<String>,
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account name
|
2016-07-24 17:38:21 +02:00
|
|
|
pub name: String,
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Account metadata
|
2016-07-24 17:38:21 +02:00
|
|
|
pub meta: String,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<json::KeyFile> for SafeAccount {
|
|
|
|
fn into(self) -> json::KeyFile {
|
|
|
|
json::KeyFile {
|
|
|
|
id: From::from(self.id),
|
|
|
|
version: self.version.into(),
|
2016-08-10 17:57:40 +02:00
|
|
|
address: self.address.into(),
|
2016-06-20 00:10:34 +02:00
|
|
|
crypto: self.crypto.into(),
|
2016-07-24 17:38:21 +02:00
|
|
|
name: Some(self.name.into()),
|
|
|
|
meta: Some(self.meta.into()),
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SafeAccount {
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Create a new account
|
2016-08-03 17:58:22 +02:00
|
|
|
pub fn create(
|
|
|
|
keypair: &KeyPair,
|
|
|
|
id: [u8; 16],
|
|
|
|
password: &str,
|
|
|
|
iterations: u32,
|
|
|
|
name: String,
|
|
|
|
meta: String
|
2018-05-05 11:02:33 +02:00
|
|
|
) -> Result<Self, crypto::Error> {
|
|
|
|
Ok(SafeAccount {
|
2016-06-20 00:10:34 +02:00
|
|
|
id: id,
|
|
|
|
version: Version::V3,
|
2018-05-05 11:02:33 +02:00
|
|
|
crypto: Crypto::with_secret(keypair.secret(), password, iterations)?,
|
2016-06-20 00:10:34 +02:00
|
|
|
address: keypair.address(),
|
2016-08-03 17:58:22 +02:00
|
|
|
filename: None,
|
2016-07-24 17:38:21 +02:00
|
|
|
name: name,
|
|
|
|
meta: meta,
|
2018-05-05 11:02:33 +02:00
|
|
|
})
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 17:57:40 +02:00
|
|
|
/// Create a new `SafeAccount` from the given `json`; if it was read from a
|
|
|
|
/// file, the `filename` should be `Some` name. If it is as yet anonymous, then it
|
|
|
|
/// can be left `None`.
|
|
|
|
pub fn from_file(json: json::KeyFile, filename: Option<String>) -> Self {
|
2016-07-25 10:45:45 +02:00
|
|
|
SafeAccount {
|
|
|
|
id: json.id.into(),
|
|
|
|
version: json.version.into(),
|
|
|
|
address: json.address.into(),
|
|
|
|
crypto: json.crypto.into(),
|
2016-08-10 17:57:40 +02:00
|
|
|
filename: filename,
|
2016-07-25 10:45:45 +02:00
|
|
|
name: json.name.unwrap_or(String::new()),
|
|
|
|
meta: json.meta.unwrap_or("{}".to_owned()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Create a new `SafeAccount` from the given vault `json`; if it was read from a
|
|
|
|
/// file, the `filename` should be `Some` name. If it is as yet anonymous, then it
|
|
|
|
/// can be left `None`.
|
|
|
|
pub fn from_vault_file(password: &str, json: json::VaultKeyFile, filename: Option<String>) -> Result<Self, Error> {
|
|
|
|
let meta_crypto: Crypto = json.metacrypto.into();
|
|
|
|
let meta_plain = meta_crypto.decrypt(password)?;
|
|
|
|
let meta_plain = json::VaultKeyMeta::load(&meta_plain).map_err(|e| Error::Custom(format!("{:?}", e)))?;
|
|
|
|
|
|
|
|
Ok(SafeAccount::from_file(json::KeyFile {
|
|
|
|
id: json.id,
|
|
|
|
version: json.version,
|
|
|
|
crypto: json.crypto,
|
|
|
|
address: meta_plain.address,
|
|
|
|
name: meta_plain.name,
|
|
|
|
meta: meta_plain.meta,
|
|
|
|
}, filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new `VaultKeyFile` from the given `self`
|
|
|
|
pub fn into_vault_file(self, iterations: u32, password: &str) -> Result<json::VaultKeyFile, Error> {
|
|
|
|
let meta_plain = json::VaultKeyMeta {
|
|
|
|
address: self.address.into(),
|
|
|
|
name: Some(self.name),
|
|
|
|
meta: Some(self.meta),
|
|
|
|
};
|
|
|
|
let meta_plain = meta_plain.write().map_err(|e| Error::Custom(format!("{:?}", e)))?;
|
2018-05-05 11:02:33 +02:00
|
|
|
let meta_crypto = Crypto::with_plain(&meta_plain, password, iterations)?;
|
2017-01-30 11:44:09 +01:00
|
|
|
|
|
|
|
Ok(json::VaultKeyFile {
|
|
|
|
id: self.id.into(),
|
|
|
|
version: self.version.into(),
|
|
|
|
crypto: self.crypto.into(),
|
|
|
|
metacrypto: meta_crypto.into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Sign a message.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub fn sign(&self, password: &str, message: &Message) -> Result<Signature, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let secret = self.crypto.secret(password)?;
|
2016-06-20 00:10:34 +02:00
|
|
|
sign(&secret, message).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Decrypt a message.
|
2016-09-22 14:48:22 +02:00
|
|
|
pub fn decrypt(&self, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let secret = self.crypto.secret(password)?;
|
2018-05-05 11:02:33 +02:00
|
|
|
ethkey::crypto::ecies::decrypt(&secret, shared_mac, message).map_err(From::from)
|
2016-09-22 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 11:09:40 +02:00
|
|
|
/// Agree on shared key.
|
|
|
|
pub fn agree(&self, password: &str, other: &Public) -> Result<Secret, Error> {
|
|
|
|
let secret = self.crypto.secret(password)?;
|
|
|
|
agree(&secret, other).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Derive public key.
|
2016-10-15 14:44:08 +02:00
|
|
|
pub fn public(&self, password: &str) -> Result<Public, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let secret = self.crypto.secret(password)?;
|
|
|
|
Ok(KeyPair::from_secret(secret)?.public().clone())
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Change account's password.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub fn change_password(&self, old_password: &str, new_password: &str, iterations: u32) -> Result<Self, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let secret = self.crypto.secret(old_password)?;
|
2016-06-20 00:10:34 +02:00
|
|
|
let result = SafeAccount {
|
|
|
|
id: self.id.clone(),
|
|
|
|
version: self.version.clone(),
|
2018-05-05 11:02:33 +02:00
|
|
|
crypto: Crypto::with_secret(&secret, new_password, iterations)?,
|
2016-06-20 00:10:34 +02:00
|
|
|
address: self.address.clone(),
|
2016-08-03 17:58:22 +02:00
|
|
|
filename: self.filename.clone(),
|
2016-07-24 17:38:21 +02:00
|
|
|
name: self.name.clone(),
|
|
|
|
meta: self.meta.clone(),
|
2016-06-20 00:10:34 +02:00
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Check if password matches the account.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub fn check_password(&self, password: &str) -> bool {
|
|
|
|
self.crypto.secret(password).is_ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-06-20 10:02:02 +02:00
|
|
|
use ethkey::{Generator, Random, verify_public, Message};
|
2017-01-30 11:44:09 +01:00
|
|
|
use super::SafeAccount;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
#[test]
|
2016-06-20 10:02:02 +02:00
|
|
|
fn sign_and_verify_public() {
|
2016-06-20 00:10:34 +02:00
|
|
|
let keypair = Random.generate().unwrap();
|
|
|
|
let password = "hello world";
|
|
|
|
let message = Message::default();
|
2016-07-24 17:38:21 +02:00
|
|
|
let account = SafeAccount::create(&keypair, [0u8; 16], password, 10240, "Test".to_owned(), "{}".to_owned());
|
2018-05-05 11:02:33 +02:00
|
|
|
let signature = account.unwrap().sign(password, &message).unwrap();
|
2016-06-20 10:02:02 +02:00
|
|
|
assert!(verify_public(keypair.public(), &signature, &message).unwrap());
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_password() {
|
|
|
|
let keypair = Random.generate().unwrap();
|
|
|
|
let first_password = "hello world";
|
|
|
|
let sec_password = "this is sparta";
|
|
|
|
let i = 10240;
|
|
|
|
let message = Message::default();
|
2018-05-05 11:02:33 +02:00
|
|
|
let account = SafeAccount::create(&keypair, [0u8; 16], first_password, i, "Test".to_owned(), "{}".to_owned()).unwrap();
|
2016-06-20 00:10:34 +02:00
|
|
|
let new_account = account.change_password(first_password, sec_password, i).unwrap();
|
|
|
|
assert!(account.sign(first_password, &message).is_ok());
|
|
|
|
assert!(account.sign(sec_password, &message).is_err());
|
|
|
|
assert!(new_account.sign(first_password, &message).is_err());
|
|
|
|
assert!(new_account.sign(sec_password, &message).is_ok());
|
|
|
|
}
|
|
|
|
}
|