2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-05-05 15:57:29 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-05-05 15:57:29 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-05-05 15:57:29 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-03-01 10:00:44 +01:00
|
|
|
use crypto::DEFAULT_MAC;
|
2017-05-05 15:57:29 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2017-11-16 17:34:23 +01:00
|
|
|
use ethkey::{KeyPair, Signature, verify_public};
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
use serde_json;
|
|
|
|
use jsonrpc_core::{IoHandler, Success};
|
|
|
|
use v1::metadata::Metadata;
|
|
|
|
use v1::SecretStoreClient;
|
|
|
|
use v1::traits::secretstore::SecretStore;
|
2017-11-16 17:34:23 +01:00
|
|
|
use v1::helpers::secretstore::ordered_servers_keccak;
|
2018-03-01 10:00:44 +01:00
|
|
|
use v1::types::{H256, EncryptedDocumentKey};
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
struct Dependencies {
|
|
|
|
pub accounts: Arc<AccountProvider>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dependencies {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Dependencies {
|
|
|
|
accounts: Arc::new(AccountProvider::transient_provider()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn client(&self) -> SecretStoreClient {
|
2018-06-01 16:49:55 +02:00
|
|
|
SecretStoreClient::new(&self.accounts)
|
2017-05-05 15:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_client(&self) -> IoHandler<Metadata> {
|
|
|
|
let mut io = IoHandler::default();
|
|
|
|
io.extend_with(self.client().to_delegate());
|
|
|
|
io
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rpc_secretstore_encrypt_and_decrypt() {
|
|
|
|
let deps = Dependencies::new();
|
|
|
|
let io = deps.default_client();
|
|
|
|
|
2017-11-16 17:34:23 +01:00
|
|
|
// insert new account
|
2017-05-05 15:57:29 +02:00
|
|
|
let secret = "c1f1cfe279a5c350d13795bce162941967340c8a228e6ba175489afc564a5bef".parse().unwrap();
|
2018-06-22 15:09:15 +02:00
|
|
|
deps.accounts.insert_account(secret, &"password".into()).unwrap();
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
// execute encryption request
|
|
|
|
let encryption_request = r#"{"jsonrpc": "2.0", "method": "secretstore_encrypt", "params":[
|
|
|
|
"0x5c2f3b4ec0c2234f8358697edc8b82a62e3ac995", "password",
|
|
|
|
"0x0440262acc06f1e13cb11b34e792cdf698673a16bb812163cb52689ac34c94ae47047b58f58d8b596d21ac7b03a55896132d07a7dc028b2dad88f6c5a90623fa5b30ff4b1ba385a98c970432d13417cf6d7facd62f86faaef15ca993735890da0cb3e417e2740fc72de7501eef083a12dd5a9ebe513b592b1740848576a936a1eb88fc553fc624b1cae41a0a4e074e34e2aaae686709f08d70e505c5acba12ef96017e89be675a2adb07c72c4e95814fbf",
|
|
|
|
"0xdeadbeef"
|
|
|
|
], "id": 1}"#;
|
|
|
|
let encryption_response = io.handle_request_sync(encryption_request).unwrap();
|
|
|
|
let encryption_response: Success = serde_json::from_str(&encryption_response).unwrap();
|
|
|
|
|
|
|
|
// execute decryption request
|
|
|
|
let decryption_request_left = r#"{"jsonrpc": "2.0", "method": "secretstore_decrypt", "params":[
|
|
|
|
"0x5c2f3b4ec0c2234f8358697edc8b82a62e3ac995", "password",
|
|
|
|
"0x0440262acc06f1e13cb11b34e792cdf698673a16bb812163cb52689ac34c94ae47047b58f58d8b596d21ac7b03a55896132d07a7dc028b2dad88f6c5a90623fa5b30ff4b1ba385a98c970432d13417cf6d7facd62f86faaef15ca993735890da0cb3e417e2740fc72de7501eef083a12dd5a9ebe513b592b1740848576a936a1eb88fc553fc624b1cae41a0a4e074e34e2aaae686709f08d70e505c5acba12ef96017e89be675a2adb07c72c4e95814fbf",""#;
|
|
|
|
let decryption_request_mid = encryption_response.result.as_str().unwrap();
|
|
|
|
let decryption_request_right = r#""
|
|
|
|
], "id": 2}"#;
|
|
|
|
let decryption_request = decryption_request_left.to_owned() + decryption_request_mid + decryption_request_right;
|
|
|
|
let decryption_response = io.handle_request_sync(&decryption_request).unwrap();
|
|
|
|
assert_eq!(decryption_response, r#"{"jsonrpc":"2.0","result":"0xdeadbeef","id":2}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rpc_secretstore_shadow_decrypt() {
|
|
|
|
let deps = Dependencies::new();
|
|
|
|
let io = deps.default_client();
|
|
|
|
|
2017-11-16 17:34:23 +01:00
|
|
|
// insert new account
|
2017-05-05 15:57:29 +02:00
|
|
|
let secret = "82758356bf46b42710d3946a8efa612b7bf5e125e4d49f28facf1139db4a46f4".parse().unwrap();
|
2018-06-22 15:09:15 +02:00
|
|
|
deps.accounts.insert_account(secret, &"password".into()).unwrap();
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
// execute decryption request
|
|
|
|
let decryption_request = r#"{"jsonrpc": "2.0", "method": "secretstore_shadowDecrypt", "params":[
|
|
|
|
"0x00dfE63B22312ab4329aD0d28CaD8Af987A01932", "password",
|
|
|
|
"0x843645726384530ffb0c52f175278143b5a93959af7864460f5a4fec9afd1450cfb8aef63dec90657f43f55b13e0a73c7524d4e9a13c051b4e5f1e53f39ecd91",
|
|
|
|
"0x07230e34ebfe41337d3ed53b186b3861751f2401ee74b988bba55694e2a6f60c757677e194be2e53c3523cc8548694e636e6acb35c4e8fdc5e29d28679b9b2f3",
|
|
|
|
["0x049ce50bbadb6352574f2c59742f78df83333975cbd5cbb151c6e8628749a33dc1fa93bb6dffae5994e3eb98ae859ed55ee82937538e6adb054d780d1e89ff140f121529eeadb1161562af9d3342db0008919ca280a064305e5a4e518e93279de7a9396fe5136a9658e337e8e276221248c381c5384cd1ad28e5921f46ff058d5fbcf8a388fc881d0dd29421c218d51761"],
|
|
|
|
"0x2ddec1f96229efa2916988d8b2a82a47ef36f71c"
|
|
|
|
], "id": 1}"#;
|
|
|
|
let decryption_response = io.handle_request_sync(&decryption_request).unwrap();
|
|
|
|
assert_eq!(decryption_response, r#"{"jsonrpc":"2.0","result":"0xdeadbeef","id":1}"#);
|
|
|
|
}
|
2017-11-16 17:34:23 +01:00
|
|
|
|
|
|
|
#[test]
|
2017-12-19 14:59:37 +01:00
|
|
|
fn rpc_secretstore_servers_set_hash() {
|
|
|
|
let deps = Dependencies::new();
|
|
|
|
let io = deps.default_client();
|
|
|
|
|
|
|
|
// execute hashing request
|
|
|
|
let hashing_request = r#"{"jsonrpc": "2.0", "method": "secretstore_serversSetHash", "params":[
|
|
|
|
["0x843645726384530ffb0c52f175278143b5a93959af7864460f5a4fec9afd1450cfb8aef63dec90657f43f55b13e0a73c7524d4e9a13c051b4e5f1e53f39ecd91",
|
|
|
|
"0x07230e34ebfe41337d3ed53b186b3861751f2401ee74b988bba55694e2a6f60c757677e194be2e53c3523cc8548694e636e6acb35c4e8fdc5e29d28679b9b2f3"]
|
|
|
|
], "id": 1}"#;
|
|
|
|
let hashing_response = io.handle_request_sync(&hashing_request).unwrap();
|
|
|
|
let hashing_response = hashing_response.replace(r#"{"jsonrpc":"2.0","result":"0x"#, "");
|
|
|
|
let hashing_response = hashing_response.replace(r#"","id":1}"#, "");
|
|
|
|
let hash: H256 = hashing_response.parse().unwrap();
|
|
|
|
|
|
|
|
let servers_set_keccak = ordered_servers_keccak(vec![
|
|
|
|
"843645726384530ffb0c52f175278143b5a93959af7864460f5a4fec9afd1450cfb8aef63dec90657f43f55b13e0a73c7524d4e9a13c051b4e5f1e53f39ecd91".parse().unwrap(),
|
|
|
|
"07230e34ebfe41337d3ed53b186b3861751f2401ee74b988bba55694e2a6f60c757677e194be2e53c3523cc8548694e636e6acb35c4e8fdc5e29d28679b9b2f3".parse().unwrap()
|
|
|
|
].into_iter().collect());
|
|
|
|
assert_eq!(hash, servers_set_keccak);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rpc_secretstore_sign_raw_hash() {
|
2017-11-16 17:34:23 +01:00
|
|
|
let deps = Dependencies::new();
|
|
|
|
let io = deps.default_client();
|
|
|
|
|
|
|
|
// insert new account
|
|
|
|
let secret = "82758356bf46b42710d3946a8efa612b7bf5e125e4d49f28facf1139db4a46f4".parse().unwrap();
|
|
|
|
let key_pair = KeyPair::from_secret(secret).unwrap();
|
2018-06-22 15:09:15 +02:00
|
|
|
deps.accounts.insert_account(key_pair.secret().clone(), &"password".into()).unwrap();
|
2017-11-16 17:34:23 +01:00
|
|
|
|
|
|
|
// execute signing request
|
2017-12-19 14:59:37 +01:00
|
|
|
let signing_request = r#"{"jsonrpc": "2.0", "method": "secretstore_signRawHash", "params":[
|
|
|
|
"0x00dfE63B22312ab4329aD0d28CaD8Af987A01932", "password", "0x0000000000000000000000000000000000000000000000000000000000000001"
|
2017-11-16 17:34:23 +01:00
|
|
|
], "id": 1}"#;
|
|
|
|
let signing_response = io.handle_request_sync(&signing_request).unwrap();
|
|
|
|
let signing_response = signing_response.replace(r#"{"jsonrpc":"2.0","result":"0x"#, "");
|
|
|
|
let signing_response = signing_response.replace(r#"","id":1}"#, "");
|
|
|
|
let signature: Signature = signing_response.parse().unwrap();
|
|
|
|
|
2017-12-19 14:59:37 +01:00
|
|
|
let hash = "0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
|
|
|
|
assert!(verify_public(key_pair.public(), &signature, &hash).unwrap());
|
2017-11-16 17:34:23 +01:00
|
|
|
}
|
2018-03-01 10:00:44 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rpc_secretstore_generate_document_key() {
|
|
|
|
let deps = Dependencies::new();
|
|
|
|
let io = deps.default_client();
|
|
|
|
|
|
|
|
// insert new account
|
|
|
|
let secret = "82758356bf46b42710d3946a8efa612b7bf5e125e4d49f28facf1139db4a46f4".parse().unwrap();
|
|
|
|
let key_pair = KeyPair::from_secret(secret).unwrap();
|
2018-06-22 15:09:15 +02:00
|
|
|
deps.accounts.insert_account(key_pair.secret().clone(), &"password".into()).unwrap();
|
2018-03-01 10:00:44 +01:00
|
|
|
|
|
|
|
// execute generation request
|
|
|
|
let generation_request = r#"{"jsonrpc": "2.0", "method": "secretstore_generateDocumentKey", "params":[
|
|
|
|
"0x00dfE63B22312ab4329aD0d28CaD8Af987A01932", "password",
|
|
|
|
"0x843645726384530ffb0c52f175278143b5a93959af7864460f5a4fec9afd1450cfb8aef63dec90657f43f55b13e0a73c7524d4e9a13c051b4e5f1e53f39ecd91"
|
|
|
|
], "id": 1}"#;
|
|
|
|
let generation_response = io.handle_request_sync(&generation_request).unwrap();
|
|
|
|
let generation_response = generation_response.replace(r#"{"jsonrpc":"2.0","result":"#, "");
|
|
|
|
let generation_response = generation_response.replace(r#","id":1}"#, "");
|
|
|
|
let generation_response: EncryptedDocumentKey = serde_json::from_str(&generation_response).unwrap();
|
|
|
|
|
|
|
|
// the only thing we can check is that 'encrypted_key' can be decrypted by passed account
|
|
|
|
assert!(deps.accounts.decrypt(
|
|
|
|
"00dfE63B22312ab4329aD0d28CaD8Af987A01932".parse().unwrap(),
|
|
|
|
Some("password".into()),
|
|
|
|
&DEFAULT_MAC,
|
|
|
|
&generation_response.encrypted_key.0).is_ok());
|
|
|
|
}
|