use crate::config::TeeProvider;
pub fn detect_tee_provider() -> Option<TeeProvider> {
if let Ok(file) = std::fs::File::open("/dev/nsm") {
use std::os::unix::fs::FileTypeExt;
if let Ok(metadata) = file.metadata() {
if metadata.file_type().is_char_device() {
return Some(TeeProvider::AwsNitro);
}
}
}
if is_char_device("/dev/tdx-guest") {
return Some(TeeProvider::IntelTdx);
}
if is_char_device("/dev/tdx_guest") {
return Some(TeeProvider::IntelTdx);
}
if std::path::Path::new("/sys/firmware/acpi/tables/TDEL").exists()
|| std::path::Path::new("/sys/firmware/acpi/tables/CCEL").exists()
{
return Some(TeeProvider::IntelTdx);
}
if is_char_device("/dev/sev-guest") {
return Some(TeeProvider::AmdSevSnp);
}
if std::path::Path::new("/run/container_launcher/attestation_verifier_claims_token").exists() {
return Some(TeeProvider::GcpConfidential);
}
if std::env::var("CONFIDENTIAL_SPACE_VERSION").is_ok() {
return Some(TeeProvider::GcpConfidential);
}
if azure_cvm_detected() {
return Some(TeeProvider::AzureSnp);
}
None
}
fn is_char_device(path: &str) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::FileTypeExt;
std::fs::metadata(path)
.map(|m| m.file_type().is_char_device())
.unwrap_or(false)
}
#[cfg(not(unix))]
{
let _ = path;
false
}
}
fn azure_cvm_detected() -> bool {
let is_azure = std::fs::read_to_string("/sys/class/dmi/id/board_vendor")
.map(|v| v.trim().contains("Microsoft"))
.unwrap_or(false);
if !is_azure {
return false;
}
std::path::Path::new("/sys/kernel/mm/sev").exists() || is_char_device("/dev/sev-guest")
}