@@ -21,6 +21,7 @@ const DEVICE: &str = "device";
2121const MSSIM : & str = "mssim" ;
2222const SWTPM : & str = "swtpm" ;
2323const 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