Skip to content

Commit

Permalink
tcti: Adds support for libtpms backend
Browse files Browse the repository at this point in the history
This is useful for running tests without having to spawn a simulator in
another process.

Signed-off-by: Arthur Gautier <arthur.gautier@arista.com>
  • Loading branch information
baloo committed Sep 11, 2024
1 parent 8ec8381 commit aed6ccf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 10 additions & 4 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,7 +91,7 @@ impl Context {
pub fn new(tcti_name_conf: TctiNameConf) -> Result<Self> {
let mut esys_context = null_mut();

let mut _tcti_context = TctiContext::initialize(tcti_name_conf)?;
let mut _tcti_context = TctiContext::initialize(tcti_name_conf.clone())?;

ReturnCode::ensure_success(
unsafe {
Expand All @@ -107,13 +107,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 matches!(tcti_name_conf, TctiNameConf::LibTpms { .. }) {
context.startup(StartupType::Clear)?;
}

Ok(context)
}

/// Create a new ESYS context based on the TAB Resource Manager Daemon.
Expand Down
19 changes: 19 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
.and_then(|s| s.to_str().map(str::to_string))
.unwrap_or_default(),
};

if tcti_conf.is_empty() {
Expand Down Expand Up @@ -247,6 +256,16 @@ 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)
.map(|m| m.as_str())
.and_then(|s| PathBuf::from_str(s).ok()),
});
}

Err(Error::WrapperError(WrapperErrorKind::InvalidParam))
}
}
Expand Down

0 comments on commit aed6ccf

Please sign in to comment.