#[cfg(feature = "cryptoauthlib-provider")]
use crate::providers::cryptoauthlib::Provider as CryptoAuthLibProvider;
#[cfg(feature = "mbed-crypto-provider")]
use crate::providers::mbed_crypto::Provider as MbedCryptoProvider;
#[cfg(feature = "pkcs11-provider")]
use crate::providers::pkcs11::Provider as Pkcs11Provider;
#[cfg(feature = "tpm-provider")]
use crate::providers::tpm::Provider as TpmProvider;
#[cfg(feature = "trusted-service-provider")]
use crate::providers::trusted_service::Provider as TrustedServiceProvider;
use log::LevelFilter;
#[cfg(not(all(
feature = "mbed-crypto-provider",
feature = "pkcs11-provider",
feature = "tpm-provider",
feature = "cryptoauthlib-provider",
feature = "trusted-service-provider"
)))]
use log::error;
use parsec_interface::requests::ProviderId;
use serde::Deserialize;
use std::io::Error;
#[cfg(not(all(
feature = "mbed-crypto-provider",
feature = "pkcs11-provider",
feature = "tpm-provider",
feature = "cryptoauthlib-provider",
feature = "trusted-service-provider"
)))]
use std::io::ErrorKind;
use zeroize::Zeroize;
#[derive(Copy, Clone, Deserialize, Debug)]
#[allow(missing_docs)]
pub struct CoreSettings {
pub thread_pool_size: Option<usize>,
pub idle_listener_sleep_duration: Option<u64>,
pub log_level: Option<LevelFilter>,
pub log_timestamp: Option<bool>,
pub body_len_limit: Option<usize>,
pub log_error_details: Option<bool>,
pub allow_root: Option<bool>,
pub buffer_size_limit: Option<usize>,
pub allow_deprecated: Option<bool>,
}
#[derive(Copy, Clone, Deserialize, Debug)]
pub enum ListenerType {
DomainSocket,
}
#[derive(Clone, Deserialize, Debug)]
pub struct ListenerConfig {
pub listener_type: ListenerType,
pub timeout: u64,
pub socket_path: Option<String>,
}
#[derive(Deserialize, Debug, Zeroize)]
#[zeroize(drop)]
#[serde(tag = "auth_type")]
pub enum AuthenticatorConfig {
Direct {
admins: Option<Vec<Admin>>,
},
UnixPeerCredentials {
admins: Option<Vec<Admin>>,
},
JwtSvid {
workload_endpoint: String,
admins: Option<Vec<Admin>>,
},
}
#[derive(Deserialize, Debug, Zeroize, Clone)]
#[zeroize(drop)]
pub struct Admin {
name: String,
}
impl Admin {
pub fn name(&self) -> &str {
&self.name
}
}
#[derive(Copy, Clone, Deserialize, Debug)]
pub enum KeyInfoManagerType {
OnDisk,
SQLite,
}
#[derive(Deserialize, Debug)]
pub struct KeyInfoManagerConfig {
pub name: String,
pub manager_type: KeyInfoManagerType,
pub store_path: Option<String>,
pub sqlite_db_path: Option<String>,
}
#[derive(Deserialize, Debug, Zeroize)]
#[zeroize(drop)]
#[serde(tag = "provider_type")]
pub enum ProviderConfig {
MbedCrypto {
name: Option<String>,
key_info_manager: String,
},
Pkcs11 {
name: Option<String>,
key_info_manager: String,
library_path: String,
slot_number: Option<u64>,
serial_number: Option<String>,
user_pin: Option<String>,
software_public_operations: Option<bool>,
allow_export: Option<bool>,
},
Tpm {
name: Option<String>,
key_info_manager: String,
tcti: String,
owner_hierarchy_auth: String,
endorsement_hierarchy_auth: Option<String>,
skip_if_no_tpm: Option<bool>,
},
CryptoAuthLib {
name: Option<String>,
key_info_manager: String,
device_type: String,
iface_type: String,
wake_delay: Option<u16>,
rx_retries: Option<i32>,
slave_address: Option<u8>,
bus: Option<u8>,
baud: Option<u32>,
access_key_file_name: Option<String>,
},
TrustedService {
name: Option<String>,
key_info_manager: String,
},
}
impl ProviderConfig {
pub fn key_info_manager(&self) -> &String {
match *self {
ProviderConfig::MbedCrypto {
ref key_info_manager,
..
} => key_info_manager,
ProviderConfig::Pkcs11 {
ref key_info_manager,
..
} => key_info_manager,
ProviderConfig::Tpm {
ref key_info_manager,
..
} => key_info_manager,
ProviderConfig::CryptoAuthLib {
ref key_info_manager,
..
} => key_info_manager,
ProviderConfig::TrustedService {
ref key_info_manager,
..
} => key_info_manager,
}
}
pub fn provider_id(&self) -> ProviderId {
match *self {
ProviderConfig::MbedCrypto { .. } => ProviderId::MbedCrypto,
ProviderConfig::Pkcs11 { .. } => ProviderId::Pkcs11,
ProviderConfig::Tpm { .. } => ProviderId::Tpm,
ProviderConfig::CryptoAuthLib { .. } => ProviderId::CryptoAuthLib,
ProviderConfig::TrustedService { .. } => ProviderId::TrustedService,
}
}
pub fn provider_name(&self) -> Result<String, Error> {
match *self {
#[cfg(feature = "mbed-crypto-provider")]
ProviderConfig::MbedCrypto { ref name, .. } => Ok(name
.clone()
.unwrap_or_else(|| String::from(MbedCryptoProvider::DEFAULT_PROVIDER_NAME))),
#[cfg(feature = "pkcs11-provider")]
ProviderConfig::Pkcs11 { ref name, .. } => Ok(name
.clone()
.unwrap_or_else(|| String::from(Pkcs11Provider::DEFAULT_PROVIDER_NAME))),
#[cfg(feature = "tpm-provider")]
ProviderConfig::Tpm { ref name, .. } => Ok(name
.clone()
.unwrap_or_else(|| String::from(TpmProvider::DEFAULT_PROVIDER_NAME))),
#[cfg(feature = "cryptoauthlib-provider")]
ProviderConfig::CryptoAuthLib { ref name, .. } => Ok(name
.clone()
.unwrap_or_else(|| String::from(CryptoAuthLibProvider::DEFAULT_PROVIDER_NAME))),
#[cfg(feature = "trusted-service-provider")]
ProviderConfig::TrustedService { ref name, .. } => Ok(name
.clone()
.unwrap_or_else(|| String::from(TrustedServiceProvider::DEFAULT_PROVIDER_NAME))),
#[cfg(not(all(
feature = "mbed-crypto-provider",
feature = "pkcs11-provider",
feature = "tpm-provider",
feature = "cryptoauthlib-provider",
feature = "trusted-service-provider"
)))]
_ => {
error!(
"Provider ({:?}) chosen in the configuration was not compiled in Parsec binary.",
self
);
Err(Error::new(ErrorKind::InvalidData, "provider not compiled"))
}
}
}
}
#[derive(Deserialize, Debug)]
#[allow(missing_docs)]
pub struct ServiceConfig {
pub core_settings: CoreSettings,
pub listener: ListenerConfig,
pub authenticator: AuthenticatorConfig,
pub key_manager: Option<Vec<KeyInfoManagerConfig>>,
pub provider: Option<Vec<ProviderConfig>>,
}