2017-02-20 16:13:21 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
2017-04-03 11:13:51 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde_json;
|
|
|
|
use ethkey::{Secret, Public};
|
2017-02-20 16:13:21 +01:00
|
|
|
use util::Database;
|
2017-04-03 11:13:51 +02:00
|
|
|
use types::all::{Error, ServiceConfiguration, DocumentAddress, NodeId};
|
|
|
|
use serialization::{SerializablePublic, SerializableSecret};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
/// Encrypted key share, stored by key storage on the single key server.
|
|
|
|
pub struct DocumentKeyShare {
|
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<NodeId, Secret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: Secret,
|
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: Public,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: Public,
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
/// Document encryption keys storage
|
|
|
|
pub trait KeyStorage: Send + Sync {
|
|
|
|
/// Insert document encryption key
|
2017-04-03 11:13:51 +02:00
|
|
|
fn insert(&self, document: DocumentAddress, key: DocumentKeyShare) -> Result<(), Error>;
|
2017-02-20 16:13:21 +01:00
|
|
|
/// Get document encryption key
|
2017-04-03 11:13:51 +02:00
|
|
|
fn get(&self, document: &DocumentAddress) -> Result<DocumentKeyShare, Error>;
|
2017-04-08 11:26:16 +02:00
|
|
|
/// Check if storage contains document encryption key
|
|
|
|
fn contains(&self, document: &DocumentAddress) -> bool;
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Persistent document encryption keys storage
|
|
|
|
pub struct PersistentKeyStorage {
|
|
|
|
db: Database,
|
|
|
|
}
|
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
/// Encrypted key share, as it is stored by key storage on the single key server.
|
|
|
|
struct SerializableDocumentKeyShare {
|
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<SerializablePublic, SerializableSecret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: SerializableSecret,
|
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: SerializablePublic,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: SerializablePublic,
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
impl PersistentKeyStorage {
|
|
|
|
/// Create new persistent document encryption keys storage
|
|
|
|
pub fn new(config: &ServiceConfiguration) -> Result<Self, Error> {
|
|
|
|
let mut db_path = PathBuf::from(&config.data_path);
|
|
|
|
db_path.push("db");
|
|
|
|
let db_path = db_path.to_str().ok_or(Error::Database("Invalid secretstore path".to_owned()))?;
|
|
|
|
|
|
|
|
Ok(PersistentKeyStorage {
|
|
|
|
db: Database::open_default(&db_path).map_err(Error::Database)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyStorage for PersistentKeyStorage {
|
2017-04-03 11:13:51 +02:00
|
|
|
fn insert(&self, document: DocumentAddress, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
let key: SerializableDocumentKeyShare = key.into();
|
|
|
|
let key = serde_json::to_vec(&key).map_err(|e| Error::Database(e.to_string()))?;
|
2017-02-20 16:13:21 +01:00
|
|
|
let mut batch = self.db.transaction();
|
|
|
|
batch.put(None, &document, &key);
|
|
|
|
self.db.write(batch).map_err(Error::Database)
|
|
|
|
}
|
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
fn get(&self, document: &DocumentAddress) -> Result<DocumentKeyShare, Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.db.get(None, document)
|
|
|
|
.map_err(Error::Database)?
|
|
|
|
.ok_or(Error::DocumentNotFound)
|
|
|
|
.map(|key| key.to_vec())
|
2017-04-03 11:13:51 +02:00
|
|
|
.and_then(|key| serde_json::from_slice::<SerializableDocumentKeyShare>(&key).map_err(|e| Error::Database(e.to_string())))
|
|
|
|
.map(Into::into)
|
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
|
|
|
|
fn contains(&self, document: &DocumentAddress) -> bool {
|
|
|
|
self.db.get(None, document)
|
|
|
|
.map(|k| k.is_some())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DocumentKeyShare> for SerializableDocumentKeyShare {
|
|
|
|
fn from(key: DocumentKeyShare) -> Self {
|
|
|
|
SerializableDocumentKeyShare {
|
|
|
|
threshold: key.threshold,
|
|
|
|
id_numbers: key.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: key.secret_share.into(),
|
|
|
|
common_point: key.common_point.into(),
|
|
|
|
encrypted_point: key.encrypted_point.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SerializableDocumentKeyShare> for DocumentKeyShare {
|
|
|
|
fn from(key: SerializableDocumentKeyShare) -> Self {
|
|
|
|
DocumentKeyShare {
|
|
|
|
threshold: key.threshold,
|
|
|
|
id_numbers: key.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: key.secret_share.into(),
|
|
|
|
common_point: key.common_point.into(),
|
|
|
|
encrypted_point: key.encrypted_point.into(),
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2017-04-03 11:13:51 +02:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2017-02-20 16:13:21 +01:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use devtools::RandomTempPath;
|
2017-04-03 11:13:51 +02:00
|
|
|
use ethkey::{Random, Generator};
|
2017-04-25 21:34:03 +02:00
|
|
|
use super::super::types::all::{Error, NodeAddress, ServiceConfiguration, ClusterConfiguration, DocumentAddress};
|
2017-04-03 11:13:51 +02:00
|
|
|
use super::{KeyStorage, PersistentKeyStorage, DocumentKeyShare};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
/// In-memory document encryption keys storage
|
|
|
|
pub struct DummyKeyStorage {
|
2017-04-03 11:13:51 +02:00
|
|
|
keys: RwLock<HashMap<DocumentAddress, DocumentKeyShare>>,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyStorage for DummyKeyStorage {
|
2017-04-03 11:13:51 +02:00
|
|
|
fn insert(&self, document: DocumentAddress, key: DocumentKeyShare) -> Result<(), Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.keys.write().insert(document, key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
fn get(&self, document: &DocumentAddress) -> Result<DocumentKeyShare, Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.keys.read().get(document).cloned().ok_or(Error::DocumentNotFound)
|
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
|
|
|
|
fn contains(&self, document: &DocumentAddress) -> bool {
|
|
|
|
self.keys.read().contains_key(document)
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn persistent_key_storage() {
|
|
|
|
let path = RandomTempPath::create_dir();
|
|
|
|
let config = ServiceConfiguration {
|
2017-04-03 11:13:51 +02:00
|
|
|
listener_address: NodeAddress {
|
|
|
|
address: "0.0.0.0".to_owned(),
|
|
|
|
port: 8082,
|
|
|
|
},
|
2017-02-20 16:13:21 +01:00
|
|
|
data_path: path.as_str().to_owned(),
|
2017-04-03 11:13:51 +02:00
|
|
|
cluster_config: ClusterConfiguration {
|
|
|
|
threads: 1,
|
|
|
|
self_private: (**Random.generate().unwrap().secret().clone()).into(),
|
|
|
|
listener_address: NodeAddress {
|
|
|
|
address: "0.0.0.0".to_owned(),
|
|
|
|
port: 8083,
|
|
|
|
},
|
|
|
|
nodes: BTreeMap::new(),
|
|
|
|
allow_connecting_to_higher_nodes: false,
|
|
|
|
},
|
2017-02-20 16:13:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let key1 = DocumentAddress::from(1);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value1 = DocumentKeyShare {
|
|
|
|
threshold: 100,
|
|
|
|
id_numbers: vec![
|
|
|
|
(Random.generate().unwrap().public().clone(), Random.generate().unwrap().secret().clone())
|
|
|
|
].into_iter().collect(),
|
|
|
|
secret_share: Random.generate().unwrap().secret().clone(),
|
|
|
|
common_point: Random.generate().unwrap().public().clone(),
|
|
|
|
encrypted_point: Random.generate().unwrap().public().clone(),
|
|
|
|
};
|
2017-02-20 16:13:21 +01:00
|
|
|
let key2 = DocumentAddress::from(2);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value2 = DocumentKeyShare {
|
|
|
|
threshold: 200,
|
|
|
|
id_numbers: vec![
|
|
|
|
(Random.generate().unwrap().public().clone(), Random.generate().unwrap().secret().clone())
|
|
|
|
].into_iter().collect(),
|
|
|
|
secret_share: Random.generate().unwrap().secret().clone(),
|
|
|
|
common_point: Random.generate().unwrap().public().clone(),
|
|
|
|
encrypted_point: Random.generate().unwrap().public().clone(),
|
|
|
|
};
|
2017-02-20 16:13:21 +01:00
|
|
|
let key3 = DocumentAddress::from(3);
|
|
|
|
|
|
|
|
let key_storage = PersistentKeyStorage::new(&config).unwrap();
|
|
|
|
key_storage.insert(key1.clone(), value1.clone()).unwrap();
|
|
|
|
key_storage.insert(key2.clone(), value2.clone()).unwrap();
|
|
|
|
assert_eq!(key_storage.get(&key1), Ok(value1.clone()));
|
|
|
|
assert_eq!(key_storage.get(&key2), Ok(value2.clone()));
|
|
|
|
assert_eq!(key_storage.get(&key3), Err(Error::DocumentNotFound));
|
|
|
|
drop(key_storage);
|
|
|
|
|
|
|
|
let key_storage = PersistentKeyStorage::new(&config).unwrap();
|
|
|
|
assert_eq!(key_storage.get(&key1), Ok(value1));
|
|
|
|
assert_eq!(key_storage.get(&key2), Ok(value2));
|
|
|
|
assert_eq!(key_storage.get(&key3), Err(Error::DocumentNotFound));
|
|
|
|
}
|
|
|
|
}
|