pub mod system_hardening;
pub mod constant_time;
pub mod crypto;
pub mod encryption {
pub use super::crypto::symmetric::*;
}
#[cfg(feature = "hsm")]
pub mod hsm;
#[cfg(not(feature = "hsm"))]
pub mod hsm_shim;
pub use system_hardening::ConfigStatus;
pub use system_hardening::HardeningConfig;
pub use system_hardening::SecurityLevel;
pub use system_hardening::SystemHardening;
#[cfg(feature = "hsm")]
pub use hsm::config::HsmConfig;
#[cfg(feature = "hsm")]
pub use hsm::provider::{HsmProvider, KeyGenParams, KeyType, SigningAlgorithm};
#[cfg(feature = "hsm")]
pub use hsm::{HsmManager, HsmStatus};
#[cfg(not(feature = "hsm"))]
pub use hsm_shim::{HsmManager, HsmStatus, HsmStubError, KeyType, SigningAlgorithm};
pub fn create_system_hardening() -> SystemHardening {
SystemHardening::new(20)
}
pub fn create_basic_security_config(
component_name: &str,
) -> std::collections::HashMap<String, String> {
let mut settings = std::collections::HashMap::new();
settings.insert("firewall".to_string(), "enabled".to_string());
settings.insert("encryption".to_string(), "enabled".to_string());
settings.insert("access_control".to_string(), "strict".to_string());
settings.insert("audit_logging".to_string(), "enabled".to_string());
settings.insert("intrusion_detection".to_string(), "enabled".to_string());
match component_name {
"network" => {
settings.insert(
"port_scanning_protection".to_string(),
"enabled".to_string(),
);
settings.insert("ddos_protection".to_string(), "enabled".to_string());
}
"database" => {
settings.insert("query_sanitization".to_string(), "strict".to_string());
settings.insert("data_encryption".to_string(), "aes-256".to_string());
}
"api" => {
settings.insert("rate_limiting".to_string(), "enabled".to_string());
settings.insert("input_validation".to_string(), "strict".to_string());
}
_ => {
settings.insert("default_deny".to_string(), "enabled".to_string());
}
}
settings
}
use log::info;
#[cfg(feature = "hsm")]
pub use hsm::{
audit::{AuditEvent, AuditLoggerConfig, AuditStorageType},
error::HsmError,
};
pub async fn initialize() -> Result<(), Box<dyn std::error::Error>> {
info!("Initializing security subsystem");
info!("Security subsystem initialized");
Ok(())
}
#[cfg(feature = "hsm")]
pub async fn create_bitcoin_hsm_provider(
_base_provider: std::sync::Arc<dyn hsm::provider::HsmProvider>,
) -> Result<hsm::providers::bitcoin::BitcoinHsmProvider, hsm::error::HsmError> {
#[cfg(feature = "hsm")]
let config = hsm::config::BitcoinConfig {
network: hsm::config::BitcoinNetworkType::Testnet, rpc_url: Some("http://127.0.0.1:18332".to_string()),
rpc_username: Some("user".to_string()),
rpc_password: Some("password".to_string()),
derivation_path_template: "m/84'/0'/0'/{index}".to_string(),
use_segwit: true,
use_taproot: true,
confirm_transactions: false,
default_fee_rate: 5,
};
hsm::providers::bitcoin::BitcoinHsmProvider::new(&config).await
}
#[cfg(not(feature = "hsm"))]
pub fn create_bitcoin_hsm_provider(
_base_provider: std::sync::Arc<dyn hsm_shim::HsmProvider>,
) -> hsm_shim::BitcoinHsmProvider {
#[allow(clippy::default_constructed_unit_structs)]
hsm_shim::BitcoinHsmProvider::default()
}
#[cfg(feature = "hsm")]
pub async fn verify_bitcoin_payment(
_bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
_proof_data: Vec<u8>,
) -> Result<bool, hsm::error::HsmError> {
Ok(true)
}
#[cfg(not(feature = "hsm"))]
pub async fn verify_bitcoin_payment(
_bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
_proof_data: Vec<u8>,
) -> Result<bool, hsm_shim::HsmStubError> {
Err(hsm_shim::HsmStubError::feature_disabled())
}
#[cfg(feature = "hsm")]
pub async fn create_taproot_asset(
bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
metadata: &str,
supply: u64,
) -> Result<String, hsm::error::HsmError> {
let mut attributes = std::collections::HashMap::new();
attributes.insert("metadata".to_string(), metadata.to_string());
attributes.insert("supply".to_string(), supply.to_string());
let key_params = hsm::provider::KeyGenParams {
id: Some("asset".to_string()),
label: Some(format!("Asset key for {}", metadata)),
key_type: hsm::provider::KeyType::Ec {
curve: hsm::provider::EcCurve::Secp256k1,
},
extractable: false,
usages: vec![hsm::provider::KeyUsage::Sign],
expires_at: None,
attributes,
};
let (key_pair, _key_info) = bitcoin_provider.generate_key(key_params).await?;
Ok(key_pair.id)
}
#[cfg(not(feature = "hsm"))]
pub async fn create_taproot_asset(
_bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
_metadata: &str,
_supply: u64,
) -> Result<String, hsm_shim::HsmStubError> {
Err(hsm_shim::HsmStubError::feature_disabled())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_security_config() {
let network_config = create_basic_security_config("network");
let db_config = create_basic_security_config("database");
assert_eq!(network_config.get("firewall"), Some(&"enabled".to_string()));
assert_eq!(db_config.get("firewall"), Some(&"enabled".to_string()));
assert_eq!(
network_config.get("ddos_protection"),
Some(&"enabled".to_string())
);
assert_eq!(
db_config.get("data_encryption"),
Some(&"aes-256".to_string())
);
}
}