Skip to content

Commit

Permalink
Merge pull request #535 from baloo/baloo/libtpms-backend
Browse files Browse the repository at this point in the history
tcti: Adds support for libtpms backend
  • Loading branch information
Superhepper authored Sep 16, 2024
2 parents 8ec8381 + bc7c440 commit 1d8337c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tss-esapi/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod handle_manager;
use crate::{
attributes::SessionAttributesBuilder,
constants::{CapabilityType, PropertyTag, SessionType},
constants::{CapabilityType, PropertyTag, SessionType, StartupType},
handles::{ObjectHandle, SessionHandle},
interface_types::{algorithm::HashingAlgorithm, session_handles::AuthSession},
structures::{CapabilityData, SymmetricDefinition},
Expand Down Expand Up @@ -91,6 +91,9 @@ impl Context {
pub fn new(tcti_name_conf: TctiNameConf) -> Result<Self> {
let mut esys_context = null_mut();

// Some TCTI backend will not automatically send a clear and we need to send a clear
// manually before being to operate.
let needs_clear_startup = matches!(tcti_name_conf, TctiNameConf::LibTpms { .. });
let mut _tcti_context = TctiContext::initialize(tcti_name_conf)?;

ReturnCode::ensure_success(
Expand All @@ -107,13 +110,19 @@ impl Context {
)?;

let esys_context = unsafe { Some(Malloced::from_raw(esys_context)) };
Ok(Context {
let mut context = Context {
esys_context,
sessions: (None, None, None),
_tcti_context,
handle_manager: HandleManager::new(),
cached_tpm_properties: HashMap::new(),
})
};

if needs_clear_startup {
context.startup(StartupType::Clear)?;
}

Ok(context)
}

/// Create a new ESYS context based on the TAB Resource Manager Daemon.
Expand Down
29 changes: 29 additions & 0 deletions tss-esapi/src/tcti_ldr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DEVICE: &str = "device";
const MSSIM: &str = "mssim";
const SWTPM: &str = "swtpm";
const TABRMD: &str = "tabrmd";
const LIBTPMS: &str = "libtpms";

/// TCTI Context created via a TCTI Loader Library.
/// Wrapper around the TSS2_TCTI_CONTEXT structure.
Expand Down Expand Up @@ -139,6 +140,10 @@ pub enum TctiNameConf {
///
/// For more information about configuration, see [this page](https://www.mankier.com/3/Tss2_Tcti_Mssim_Init)
Swtpm(TpmSimulatorConfig),
/// Connect to a TPM (simulator) available as a library
///
/// This allows for an optional state file
LibTpms { state: Option<PathBuf> },
/// Connect to a TPM through an Access Broker/Resource Manager daemon
///
/// For more information about configuration, see [this page](https://www.mankier.com/3/Tss2_Tcti_Tabrmd_Init)
Expand Down Expand Up @@ -174,6 +179,7 @@ impl TryFrom<TctiNameConf> for CString {
TctiNameConf::Mssim(..) => MSSIM,
TctiNameConf::Swtpm(..) => SWTPM,
TctiNameConf::Tabrmd(..) => TABRMD,
TctiNameConf::LibTpms { .. } => LIBTPMS,
};

let tcti_conf = match tcti {
Expand Down Expand Up @@ -204,6 +210,9 @@ impl TryFrom<TctiNameConf> for CString {
TctiNameConf::Tabrmd(config) => {
format!("bus_name={},bus_type={}", config.bus_name, config.bus_type)
}
TctiNameConf::LibTpms { state } => {
state.map(|s| s.display().to_string()).unwrap_or_default()
}
};

if tcti_conf.is_empty() {
Expand Down Expand Up @@ -247,6 +256,15 @@ impl FromStr for TctiNameConf {
)?));
}

let libtpms_pattern = Regex::new(r"^libtpms(:(.*))?$").unwrap(); //should not fail
if let Some(captures) = libtpms_pattern.captures(config_str) {
return Ok(TctiNameConf::LibTpms {
state: captures
.get(2)
.and_then(|s| PathBuf::from_str(s.as_str()).ok()),
});
}

Err(Error::WrapperError(WrapperErrorKind::InvalidParam))
}
}
Expand Down Expand Up @@ -327,6 +345,17 @@ fn validate_from_str_tcti() {

let tcti = TctiNameConf::from_str("tabrmd").unwrap();
assert_eq!(tcti, TctiNameConf::Tabrmd(Default::default()));

let tcti = TctiNameConf::from_str("libtpms:/try/this/path").unwrap();
assert_eq!(
tcti,
TctiNameConf::LibTpms {
state: Some(PathBuf::from("/try/this/path"))
}
);

let tcti = TctiNameConf::from_str("libtpms").unwrap();
assert_eq!(tcti, TctiNameConf::LibTpms { state: None });
}

/// Configuration for a Device TCTI context
Expand Down

0 comments on commit 1d8337c

Please sign in to comment.