Skip to content

Commit 1d8337c

Browse files
authored
Merge pull request #535 from baloo/baloo/libtpms-backend
tcti: Adds support for libtpms backend
2 parents 8ec8381 + bc7c440 commit 1d8337c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

tss-esapi/src/context.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod handle_manager;
44
use crate::{
55
attributes::SessionAttributesBuilder,
6-
constants::{CapabilityType, PropertyTag, SessionType},
6+
constants::{CapabilityType, PropertyTag, SessionType, StartupType},
77
handles::{ObjectHandle, SessionHandle},
88
interface_types::{algorithm::HashingAlgorithm, session_handles::AuthSession},
99
structures::{CapabilityData, SymmetricDefinition},
@@ -91,6 +91,9 @@ impl Context {
9191
pub fn new(tcti_name_conf: TctiNameConf) -> Result<Self> {
9292
let mut esys_context = null_mut();
9393

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

9699
ReturnCode::ensure_success(
@@ -107,13 +110,19 @@ impl Context {
107110
)?;
108111

109112
let esys_context = unsafe { Some(Malloced::from_raw(esys_context)) };
110-
Ok(Context {
113+
let mut context = Context {
111114
esys_context,
112115
sessions: (None, None, None),
113116
_tcti_context,
114117
handle_manager: HandleManager::new(),
115118
cached_tpm_properties: HashMap::new(),
116-
})
119+
};
120+
121+
if needs_clear_startup {
122+
context.startup(StartupType::Clear)?;
123+
}
124+
125+
Ok(context)
117126
}
118127

119128
/// Create a new ESYS context based on the TAB Resource Manager Daemon.

tss-esapi/src/tcti_ldr.rs

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const DEVICE: &str = "device";
2121
const MSSIM: &str = "mssim";
2222
const SWTPM: &str = "swtpm";
2323
const TABRMD: &str = "tabrmd";
24+
const LIBTPMS: &str = "libtpms";
2425

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

179185
let tcti_conf = match tcti {
@@ -204,6 +210,9 @@ impl TryFrom<TctiNameConf> for CString {
204210
TctiNameConf::Tabrmd(config) => {
205211
format!("bus_name={},bus_type={}", config.bus_name, config.bus_type)
206212
}
213+
TctiNameConf::LibTpms { state } => {
214+
state.map(|s| s.display().to_string()).unwrap_or_default()
215+
}
207216
};
208217

209218
if tcti_conf.is_empty() {
@@ -247,6 +256,15 @@ impl FromStr for TctiNameConf {
247256
)?));
248257
}
249258

259+
let libtpms_pattern = Regex::new(r"^libtpms(:(.*))?$").unwrap(); //should not fail
260+
if let Some(captures) = libtpms_pattern.captures(config_str) {
261+
return Ok(TctiNameConf::LibTpms {
262+
state: captures
263+
.get(2)
264+
.and_then(|s| PathBuf::from_str(s.as_str()).ok()),
265+
});
266+
}
267+
250268
Err(Error::WrapperError(WrapperErrorKind::InvalidParam))
251269
}
252270
}
@@ -327,6 +345,17 @@ fn validate_from_str_tcti() {
327345

328346
let tcti = TctiNameConf::from_str("tabrmd").unwrap();
329347
assert_eq!(tcti, TctiNameConf::Tabrmd(Default::default()));
348+
349+
let tcti = TctiNameConf::from_str("libtpms:/try/this/path").unwrap();
350+
assert_eq!(
351+
tcti,
352+
TctiNameConf::LibTpms {
353+
state: Some(PathBuf::from("/try/this/path"))
354+
}
355+
);
356+
357+
let tcti = TctiNameConf::from_str("libtpms").unwrap();
358+
assert_eq!(tcti, TctiNameConf::LibTpms { state: None });
330359
}
331360

332361
/// Configuration for a Device TCTI context

0 commit comments

Comments
 (0)