anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
//! Security Module
//!
//! This module provides security functionality for the Anya Core platform,
//! including system hardening, input validation, and hardware security module (HSM)
//! support for cryptographic operations.

// Basic security modules always available
pub mod system_hardening;

pub mod constant_time;

// Cryptographic operations module
pub mod crypto;

// Add encryption sub-module for easier access
pub mod encryption {
    pub use super::crypto::symmetric::*;
}

// Hardware Security Module (conditionally included)
#[cfg(feature = "hsm")]
pub mod hsm;

// Include shim implementation when HSM feature is disabled
#[cfg(not(feature = "hsm"))]
pub mod hsm_shim;

// Re-exports for convenience
pub use system_hardening::ConfigStatus;
pub use system_hardening::HardeningConfig;
pub use system_hardening::SecurityLevel;
pub use system_hardening::SystemHardening;

// Conditionally re-export HSM types based on feature flag
#[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};

// When HSM feature is disabled, use the shim implementation instead
#[cfg(not(feature = "hsm"))]
pub use hsm_shim::{HsmManager, HsmStatus, HsmStubError, KeyType, SigningAlgorithm};

/// Helper function to create a system hardening manager with default auto-save frequency (20)
pub fn create_system_hardening() -> SystemHardening {
    SystemHardening::new(20)
}

/// Helper function to create a basic security configuration for a component
pub fn create_basic_security_config(
    component_name: &str,
) -> std::collections::HashMap<String, String> {
    let mut settings = std::collections::HashMap::new();
    // Basic security settings
    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());

    // Component-specific settings
    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());
        }
        _ => {
            // Generic settings for other components
            settings.insert("default_deny".to_string(), "enabled".to_string());
        }
    }

    settings
}

// Security module
// Implements security features for Bitcoin operations
// as per official Bitcoin Improvement Proposals (BIPs) requirements

use log::info;
// [AIR-3][AIS-3][BPC-3][RES-3] Constant time module already declared above

// [AIR-3][AIS-3][BPC-3][RES-3] Conditionally export HSM types when the feature is enabled
// This follows official Bitcoin Improvement Proposals (BIPs) standards for HSM implementations
#[cfg(feature = "hsm")]
pub use hsm::{
    audit::{AuditEvent, AuditLoggerConfig, AuditStorageType},
    // Remove the bitcoin import from hsm since it's not available
    // bitcoin::{
    //     BitcoinHsmProvider,
    //     BitcoinHsmConfig,
    //     BitcoinKeyInfo,
    //     BitcoinKeyType,
    //     BitcoinNetwork,
    //     BitcoinSignatureType,
    //     TaprootOutputInfo,
    //     TaprootScriptTree,
    //     BitcoinScriptDetails,
    //     BitcoinScriptType,
    //     BitcoinSpvProof,
    //     DlcInfo,
    //     DlcParams,
    //     create_dlc,
    // },
    // Only export the types that are actually used in the codebase
    error::HsmError,
};

// Other security modules - to be implemented
// pub mod authentication;
// pub mod authorization;
// pub mod compliance;
// pub mod crypto;
// pub mod secrets;
// pub mod validation;

/// Initialize the security subsystem
///
/// This function initializes the security subsystem, including the HSM manager
/// if configured. It follows the security requirements specified in the
/// official Bitcoin Improvement Proposals (BIPs).
///
/// # Returns
/// `Ok(())` on success, `Err` on failure
pub async fn initialize() -> Result<(), Box<dyn std::error::Error>> {
    info!("Initializing security subsystem");

    // Initialize HSM if configured
    // This is just placeholder code - actual initialization would be handled by the application

    // let hsm_config = HsmConfig::development();
    // let hsm_manager = HsmManager::new(hsm_config);
    // hsm_manager.initialize().await?;

    info!("Security subsystem initialized");
    Ok(())
}

/// Create a Bitcoin HSM provider with default configuration
///
/// This function creates a Bitcoin HSM provider with default configuration,
/// using the specified HSM provider as the base provider.
///
/// # Arguments
/// * `base_provider` - Base HSM provider to use
///
/// # Returns
/// BitcoinHsmProvider configured for Bitcoin operations
#[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, // Default to testnet for safety
        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()
}

/// Verify a Bitcoin payment using SPV proof
///
/// This function verifies a Bitcoin payment using SPV proof, as described
/// in official Bitcoin Improvement Proposals (BIPs) requirements.
///
/// # Arguments
/// * `bitcoin_provider` - Bitcoin HSM provider
/// * `proof` - SPV proof of the payment
///
/// # Returns
/// `Ok(true)` if payment is valid, `Ok(false)` if not, `Err` on failure
#[cfg(feature = "hsm")]
pub async fn verify_bitcoin_payment(
    _bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
    _proof_data: Vec<u8>,
) -> Result<bool, hsm::error::HsmError> {
    // This would normally verify an SPV proof
    // For now, it simply returns success as a placeholder
    Ok(true)
}

#[cfg(not(feature = "hsm"))]
// [AIR-3][AIS-3][BPC-3][RES-3]
pub async fn verify_bitcoin_payment(
    _bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
    _proof_data: Vec<u8>,
) -> Result<bool, hsm_shim::HsmStubError> {
    // Fallback stub for when HSM is not enabled
    // [AIR-3][AIS-3][BPC-3][RES-3]
    Err(hsm_shim::HsmStubError::feature_disabled())
}

/// Create a Taproot asset
///
/// This function creates a Taproot asset as specified in the Bitcoin Development
/// Framework v2.5 requirements, using the provided metadata.
///
/// # Arguments
/// * `bitcoin_provider` - Bitcoin HSM provider
/// * `metadata` - Asset metadata
/// * `supply` - Asset supply
///
/// # Returns
/// `Ok(asset_id)` on success, `Err` on failure
#[cfg(feature = "hsm")]
pub async fn create_taproot_asset(
    bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
    metadata: &str,
    supply: u64,
) -> Result<String, hsm::error::HsmError> {
    // Generate a key for the asset using the correct KeyGenParams structure
    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,
    };

    // [AIR-3][AIS-3][BPC-3][RES-3] Generate the key and return its ID
    let (key_pair, _key_info) = bitcoin_provider.generate_key(key_params).await?;
    Ok(key_pair.id)
}

#[cfg(not(feature = "hsm"))]
// [AIR-3][AIS-3][BPC-3][RES-3]
pub async fn create_taproot_asset(
    _bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
    _metadata: &str,
    _supply: u64,
) -> Result<String, hsm_shim::HsmStubError> {
    // Fallback stub for when HSM is not enabled
    // [AIR-3][AIS-3][BPC-3][RES-3]
    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");

        // Check common settings
        assert_eq!(network_config.get("firewall"), Some(&"enabled".to_string()));
        assert_eq!(db_config.get("firewall"), Some(&"enabled".to_string()));

        // Check component-specific settings
        assert_eq!(
            network_config.get("ddos_protection"),
            Some(&"enabled".to_string())
        );
        assert_eq!(
            db_config.get("data_encryption"),
            Some(&"aes-256".to_string())
        );
    }
}