From c6fa5398451b1ab83b2ff5708188aa0e7e7d965b Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Tue, 4 Feb 2020 12:14:09 +0000 Subject: [PATCH] Pemstore capable of using slightly more generic types --- common/pemstore/src/pemstore.rs | 37 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index 88d0fe3ced..b123c1e2ce 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -1,6 +1,7 @@ use crate::pathfinder::PathFinder; use crypto::identity::MixIdentityKeyPair; -use crypto::PemStorable; +use crypto::PemStorableKey; +use crypto::{encryption, PemStorableKeyPair}; use log::info; use pem::{encode, parse, Pem}; use std::fs::File; @@ -8,11 +9,6 @@ use std::io; use std::io::prelude::*; use std::path::PathBuf; -#[allow(dead_code)] -pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::KeyPair { - unimplemented!() -} - pub struct PemStore { config_dir: PathBuf, private_mix_key_file: PathBuf, @@ -28,11 +24,11 @@ impl PemStore { } } - pub fn read_identity(&self) -> io::Result { + pub fn read_keypair(&self) -> io::Result { let private_pem = self.read_pem_file(self.private_mix_key_file.clone())?; let public_pem = self.read_pem_file(self.public_mix_key_file.clone())?; - let key_pair = MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); + let key_pair = T::from_bytes(&private_pem.contents, &public_pem.contents); if key_pair.private_key().pem_type() != private_pem.tag { return Err(io::Error::new( @@ -51,16 +47,22 @@ impl PemStore { Ok(key_pair) } + pub fn read_encryption(&self) -> io::Result { + self.read_keypair() + } + + pub fn read_identity(&self) -> io::Result { + self.read_keypair() + } + fn read_pem_file(&self, filepath: PathBuf) -> io::Result { let mut pem_bytes = File::open(filepath)?; let mut buf = Vec::new(); pem_bytes.read_to_end(&mut buf)?; parse(&buf).map_err(|e| io::Error::new(io::ErrorKind::Other, e)) } - // This should be refactored and made more generic for when we have other kinds of - // KeyPairs that we want to persist (e.g. validator keypairs, or keys for - // signing vs encryption). However, for the moment, it does the job. - pub fn write_identity(&self, key_pair: MixIdentityKeyPair) -> io::Result<()> { + + fn write_keypair(&self, key_pair: impl PemStorableKeyPair) -> io::Result<()> { let private_key = key_pair.private_key(); let public_key = key_pair.public_key(); @@ -85,6 +87,17 @@ impl PemStore { Ok(()) } + // This should be refactored and made more generic for when we have other kinds of + // KeyPairs that we want to persist (e.g. validator keypairs, or keys for + // signing vs encryption). However, for the moment, it does the job. + pub fn write_identity(&self, key_pair: MixIdentityKeyPair) -> io::Result<()> { + self.write_keypair(key_pair) + } + + pub fn write_encryption_keys(&self, key_pair: encryption::KeyPair) -> io::Result<()> { + self.write_keypair(key_pair) + } + fn write_pem_file(&self, filepath: PathBuf, data: Vec, tag: String) -> io::Result<()> { // ensure the whole directory structure exists if let Some(parent_dir) = filepath.parent() {