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-07-06 14:02:10 +02:00
|
|
|
use types::all::{Error, ServiceConfiguration, ServerKeyId, NodeId};
|
2017-04-03 11:13:51 +02:00
|
|
|
use serialization::{SerializablePublic, SerializableSecret};
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
/// Key of version value.
|
|
|
|
const DB_META_KEY_VERSION: &'static [u8; 7] = b"version";
|
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
/// Encrypted key share, stored by key storage on the single key server.
|
|
|
|
pub struct DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
/// Author of the entry.
|
|
|
|
pub author: Public,
|
2017-04-03 11:13:51 +02:00
|
|
|
/// 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.
|
2017-07-06 14:02:10 +02:00
|
|
|
pub common_point: Option<Public>,
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Encrypted point.
|
2017-07-06 14:02:10 +02:00
|
|
|
pub encrypted_point: Option<Public>,
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
/// Document encryption keys storage
|
|
|
|
pub trait KeyStorage: Send + Sync {
|
|
|
|
/// Insert document encryption key
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error>;
|
|
|
|
/// Update document encryption key
|
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error>;
|
2017-02-20 16:13:21 +01:00
|
|
|
/// Get document encryption key
|
2017-07-06 14:02:10 +02:00
|
|
|
fn get(&self, document: &ServerKeyId) -> Result<DocumentKeyShare, Error>;
|
2017-04-08 11:26:16 +02:00
|
|
|
/// Check if storage contains document encryption key
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> 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)]
|
2017-07-06 14:02:10 +02:00
|
|
|
/// V0 of encrypted key share, as it is stored by key storage on the single key server.
|
|
|
|
struct SerializableDocumentKeyShareV0 {
|
2017-04-03 11:13:51 +02:00
|
|
|
/// 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-07-06 14:02:10 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
/// V1 of encrypted key share, as it is stored by key storage on the single key server.
|
|
|
|
struct SerializableDocumentKeyShareV1 {
|
|
|
|
/// Authore of the entry.
|
|
|
|
pub author: SerializablePublic,
|
|
|
|
/// 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: Option<SerializablePublic>,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: Option<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()))?;
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
let db = Database::open_default(&db_path).map_err(Error::Database)?;
|
|
|
|
let db = upgrade_db(db)?;
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
Ok(PersistentKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
db: db,
|
2017-02-20 16:13:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn upgrade_db(db: Database) -> Result<Database, Error> {
|
|
|
|
let version = db.get(None, DB_META_KEY_VERSION).map_err(Error::Database)?;
|
|
|
|
let version = version.and_then(|v| v.get(0).cloned()).unwrap_or(0);
|
|
|
|
match version {
|
|
|
|
0 => {
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[1]);
|
|
|
|
for (db_key, db_value) in db.iter(None).into_iter().flat_map(|inner| inner) {
|
|
|
|
let v0_key = serde_json::from_slice::<SerializableDocumentKeyShareV0>(&db_value).map_err(|e| Error::Database(e.to_string()))?;
|
|
|
|
let v1_key = SerializableDocumentKeyShareV1 {
|
|
|
|
// author is used in separate generation + encrypt sessions.
|
|
|
|
// in v0 there have been only simultaneous GenEnc sessions.
|
|
|
|
author: Public::default().into(),
|
|
|
|
threshold: v0_key.threshold,
|
|
|
|
id_numbers: v0_key.id_numbers,
|
|
|
|
secret_share: v0_key.secret_share,
|
|
|
|
common_point: Some(v0_key.common_point),
|
|
|
|
encrypted_point: Some(v0_key.encrypted_point),
|
|
|
|
};
|
|
|
|
let db_value = serde_json::to_vec(&v1_key).map_err(|e| Error::Database(e.to_string()))?;
|
|
|
|
batch.put(None, &*db_key, &*db_value);
|
|
|
|
}
|
|
|
|
db.write(batch).map_err(Error::Database)?;
|
|
|
|
Ok(db)
|
|
|
|
},
|
|
|
|
1 => Ok(db),
|
|
|
|
_ => Err(Error::Database(format!("unsupported SecretStore database version:? {}", version))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
impl KeyStorage for PersistentKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
let key: SerializableDocumentKeyShareV1 = key.into();
|
2017-04-03 11:13:51 +02:00
|
|
|
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-07-06 14:02:10 +02:00
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
self.insert(document, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, document: &ServerKeyId) -> Result<DocumentKeyShare, Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.db.get(None, document)
|
|
|
|
.map_err(Error::Database)?
|
|
|
|
.ok_or(Error::DocumentNotFound)
|
2017-06-28 14:16:53 +02:00
|
|
|
.map(|key| key.into_vec())
|
2017-07-06 14:02:10 +02:00
|
|
|
.and_then(|key| serde_json::from_slice::<SerializableDocumentKeyShareV1>(&key).map_err(|e| Error::Database(e.to_string())))
|
2017-04-03 11:13:51 +02:00
|
|
|
.map(Into::into)
|
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> bool {
|
2017-04-08 11:26:16 +02:00
|
|
|
self.db.get(None, document)
|
|
|
|
.map(|k| k.is_some())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
impl From<DocumentKeyShare> for SerializableDocumentKeyShareV1 {
|
2017-04-03 11:13:51 +02:00
|
|
|
fn from(key: DocumentKeyShare) -> Self {
|
2017-07-06 14:02:10 +02:00
|
|
|
SerializableDocumentKeyShareV1 {
|
|
|
|
author: key.author.into(),
|
2017-04-03 11:13:51 +02:00
|
|
|
threshold: key.threshold,
|
|
|
|
id_numbers: key.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: key.secret_share.into(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: key.common_point.map(Into::into),
|
|
|
|
encrypted_point: key.encrypted_point.map(Into::into),
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
impl From<SerializableDocumentKeyShareV1> for DocumentKeyShare {
|
|
|
|
fn from(key: SerializableDocumentKeyShareV1) -> Self {
|
2017-04-03 11:13:51 +02:00
|
|
|
DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
author: key.author.into(),
|
2017-04-03 11:13:51 +02:00
|
|
|
threshold: key.threshold,
|
|
|
|
id_numbers: key.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: key.secret_share.into(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: key.common_point.map(Into::into),
|
|
|
|
encrypted_point: key.encrypted_point.map(Into::into),
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
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;
|
2017-07-06 14:02:10 +02:00
|
|
|
use serde_json;
|
2017-02-20 16:13:21 +01:00
|
|
|
use devtools::RandomTempPath;
|
2017-07-06 14:02:10 +02:00
|
|
|
use ethkey::{Random, Generator, Public, Secret};
|
|
|
|
use util::Database;
|
|
|
|
use super::super::types::all::{Error, NodeAddress, ServiceConfiguration, ClusterConfiguration, ServerKeyId};
|
|
|
|
use super::{DB_META_KEY_VERSION, KeyStorage, PersistentKeyStorage, DocumentKeyShare,
|
|
|
|
SerializableDocumentKeyShareV0, SerializableDocumentKeyShareV1, upgrade_db};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
/// In-memory document encryption keys storage
|
|
|
|
pub struct DummyKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
keys: RwLock<HashMap<ServerKeyId, DocumentKeyShare>>,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyStorage for DummyKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
self.keys.write().insert(document, key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.keys.write().insert(document, key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn get(&self, document: &ServerKeyId) -> 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
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> bool {
|
2017-04-08 11:26:16 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
let key1 = ServerKeyId::from(1);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value1 = DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
author: Public::default(),
|
2017-04-03 11:13:51 +02:00
|
|
|
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(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: Some(Random.generate().unwrap().public().clone()),
|
|
|
|
encrypted_point: Some(Random.generate().unwrap().public().clone()),
|
2017-04-03 11:13:51 +02:00
|
|
|
};
|
2017-07-06 14:02:10 +02:00
|
|
|
let key2 = ServerKeyId::from(2);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value2 = DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
author: Public::default(),
|
2017-04-03 11:13:51 +02:00
|
|
|
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(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: Some(Random.generate().unwrap().public().clone()),
|
|
|
|
encrypted_point: Some(Random.generate().unwrap().public().clone()),
|
2017-04-03 11:13:51 +02:00
|
|
|
};
|
2017-07-06 14:02:10 +02:00
|
|
|
let key3 = ServerKeyId::from(3);
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2017-07-06 14:02:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upgrade_db_0_to_1() {
|
|
|
|
let db_path = RandomTempPath::create_dir();
|
|
|
|
let db = Database::open_default(db_path.as_str()).unwrap();
|
|
|
|
|
|
|
|
// prepare v0 database
|
|
|
|
{
|
|
|
|
let key = serde_json::to_vec(&SerializableDocumentKeyShareV0 {
|
|
|
|
threshold: 777,
|
|
|
|
id_numbers: vec![(
|
|
|
|
"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".into(),
|
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap().into(),
|
|
|
|
)].into_iter().collect(),
|
|
|
|
secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap().into(),
|
|
|
|
common_point: "99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".into(),
|
|
|
|
encrypted_point: "7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".into(),
|
|
|
|
}).unwrap();
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, &[7], &key);
|
|
|
|
db.write(batch).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgrade database
|
|
|
|
let db = upgrade_db(db).unwrap();
|
|
|
|
|
|
|
|
// check upgrade
|
|
|
|
assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], 1);
|
|
|
|
let key = serde_json::from_slice::<SerializableDocumentKeyShareV1>(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap();
|
|
|
|
assert_eq!(Public::default(), key.author.clone().into());
|
|
|
|
assert_eq!(777, key.threshold);
|
|
|
|
assert_eq!(vec![(
|
|
|
|
"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::<Public>().unwrap(),
|
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap(),
|
|
|
|
)], key.id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::<Vec<(Public, Secret)>>());
|
|
|
|
assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap(), key.secret_share.into());
|
|
|
|
assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::<Public>().unwrap()), key.common_point.clone().map(Into::into));
|
|
|
|
assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::<Public>().unwrap()), key.encrypted_point.clone().map(Into::into));
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|