use crate::security::hsm::error::HsmError;
use crate::security::hsm::types::*;
use crate::security::hsm::HsmStatus;
pub trait HsmProvider {
fn initialize(&mut self) -> Result<(), HsmError>;
fn generate_key(&self, params: KeyGenParams) -> Result<KeyPair, HsmError>;
fn sign(&self, key_id: &str, data: &[u8]) -> Result<Vec<u8>, HsmError>;
fn verify(&self, key_id: &str, data: &[u8], signature: &[u8]) -> Result<bool, HsmError>;
fn export_public_key(&self, key_id: &str) -> Result<Vec<u8>, HsmError>;
fn list_keys(&self) -> Result<Vec<String>, HsmError>;
fn delete_key(&self, key_id: &str) -> Result<(), HsmError>;
fn get_status(&self) -> Result<HsmStatus, HsmError>;
}
pub mod bitcoin {
use super::*;
pub struct BitcoinHsmProvider {
initialized: bool,
config: BitcoinHsmConfig,
}
pub struct BitcoinHsmConfig {
pub network: String,
pub path: Option<String>,
}
impl Default for BitcoinHsmConfig {
fn default() -> Self {
Self {
network: "testnet".to_string(),
path: None,
}
}
}
impl BitcoinHsmProvider {
pub fn new(config: BitcoinHsmConfig) -> Self {
Self {
initialized: false,
config,
}
}
}
impl HsmProvider for BitcoinHsmProvider {
fn initialize(&mut self) -> Result<(), HsmError> {
self.initialized = true;
Ok(())
}
fn generate_key(&self, _params: KeyGenParams) -> Result<KeyPair, HsmError> {
Err(HsmError::NotImplemented("BitcoinHsmProvider::generate_key".to_string()))
}
fn sign(&self, _key_id: &str, _data: &[u8]) -> Result<Vec<u8>, HsmError> {
Err(HsmError::NotImplemented("BitcoinHsmProvider::sign".to_string()))
}
fn verify(&self, _key_id: &str, _data: &[u8], _signature: &[u8]) -> Result<bool, HsmError> {
Err(HsmError::NotImplemented("BitcoinHsmProvider::verify".to_string()))
}
fn export_public_key(&self, _key_id: &str) -> Result<Vec<u8>, HsmError> {
Err(HsmError::NotImplemented("BitcoinHsmProvider::export_public_key".to_string()))
}
fn list_keys(&self) -> Result<Vec<String>, HsmError> {
Ok(vec![])
}
fn delete_key(&self, _key_id: &str) -> Result<(), HsmError> {
Err(HsmError::NotImplemented("BitcoinHsmProvider::delete_key".to_string()))
}
fn get_status(&self) -> Result<HsmStatus, HsmError> {
Ok(HsmStatus::Enabled)
}
}
}
pub mod cloud {
use super::*;
pub struct CloudHsmProvider {
initialized: bool,
config: CloudHsmConfig,
}
pub struct CloudHsmConfig {
pub provider: String,
pub region: String,
pub endpoint: Option<String>,
}
impl Default for CloudHsmConfig {
fn default() -> Self {
Self {
provider: "aws".to_string(),
region: "us-east-1".to_string(),
endpoint: None,
}
}
}
impl CloudHsmProvider {
pub fn new(config: CloudHsmConfig) -> Self {
Self {
initialized: false,
config,
}
}
}
impl HsmProvider for CloudHsmProvider {
fn initialize(&mut self) -> Result<(), HsmError> {
self.initialized = true;
Ok(())
}
fn generate_key(&self, _params: KeyGenParams) -> Result<KeyPair, HsmError> {
Err(HsmError::NotImplemented("CloudHsmProvider::generate_key".to_string()))
}
fn sign(&self, _key_id: &str, _data: &[u8]) -> Result<Vec<u8>, HsmError> {
Err(HsmError::NotImplemented("CloudHsmProvider::sign".to_string()))
}
fn verify(&self, _key_id: &str, _data: &[u8], _signature: &[u8]) -> Result<bool, HsmError> {
Err(HsmError::NotImplemented("CloudHsmProvider::verify".to_string()))
}
fn export_public_key(&self, _key_id: &str) -> Result<Vec<u8>, HsmError> {
Err(HsmError::NotImplemented("CloudHsmProvider::export_public_key".to_string()))
}
fn list_keys(&self) -> Result<Vec<String>, HsmError> {
Ok(vec![])
}
fn delete_key(&self, _key_id: &str) -> Result<(), HsmError> {
Err(HsmError::NotImplemented("CloudHsmProvider::delete_key".to_string()))
}
fn get_status(&self) -> Result<HsmStatus, HsmError> {
Ok(HsmStatus::Enabled)
}
}
}