|
@@ -1,10 +1,79 @@
|
|
|
+use super::{
|
|
|
+ Error,
|
|
|
+ Result,
|
|
|
+ rand_array,
|
|
|
+};
|
|
|
|
|
|
use std::{
|
|
|
+ io::{Read, Write},
|
|
|
os::raw::c_char,
|
|
|
- ffi::CStr,
|
|
|
+ ffi::CStr, path::{Path},
|
|
|
+ fs::{OpenOptions}
|
|
|
};
|
|
|
-use tss_esapi::constants::response_code::Tss2ResponseCode;
|
|
|
+use tss_esapi::{constants::response_code::Tss2ResponseCode};
|
|
|
use tss_esapi_sys::TSS2_RC;
|
|
|
+ use core::str::FromStr;
|
|
|
+ use tss_esapi::{
|
|
|
+ Context,
|
|
|
+ constants::{
|
|
|
+ session_type::{SessionType},
|
|
|
+ },
|
|
|
+ tcti_ldr::{DeviceConfig, TctiNameConf, TabrmdConfig},
|
|
|
+ interface_types::{
|
|
|
+ resource_handles::{Hierarchy},
|
|
|
+ algorithm::{HashingAlgorithm},
|
|
|
+ ecc::{EccCurve},
|
|
|
+ },
|
|
|
+ structures::{
|
|
|
+ Digest,
|
|
|
+ EccPoint,
|
|
|
+ EccScheme,
|
|
|
+ KeyDerivationFunctionScheme,
|
|
|
+ HashScheme,
|
|
|
+ Public,
|
|
|
+ PublicEccParameters,
|
|
|
+ SymmetricDefinition,
|
|
|
+ SymmetricDefinitionObject,
|
|
|
+ },
|
|
|
+ attributes::{
|
|
|
+ object::{ObjectAttributes},
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+const COOKIE_LEN: usize = 64;
|
|
|
+type Cookie = [u8; COOKIE_LEN];
|
|
|
+
|
|
|
+pub(crate) struct TpmCredStore {
|
|
|
+ context: Context,
|
|
|
+ cookie: Cookie,
|
|
|
+}
|
|
|
+
|
|
|
+impl TpmCredStore {
|
|
|
+ fn new<P: AsRef<Path>>(tpm_path: &str, cookie_path: P) -> Result<TpmCredStore> {
|
|
|
+ let config = TctiNameConf::Device(DeviceConfig::from_str(tpm_path).unwrap());
|
|
|
+ let context = Context::new(config)
|
|
|
+ .tss2_expect("Failed to create context");
|
|
|
+ let cookie = match OpenOptions::new().read(true).open(&cookie_path) {
|
|
|
+ Ok(mut file) => {
|
|
|
+ let mut cookie: Cookie = [0; COOKIE_LEN];
|
|
|
+ file.read_exact(cookie.as_mut_slice()).map_err(Error::from)?;
|
|
|
+ cookie
|
|
|
+ },
|
|
|
+ Err(other) => {
|
|
|
+ if std::io::ErrorKind::NotFound != other.kind() {
|
|
|
+ return Err(Error::from(other));
|
|
|
+ }
|
|
|
+ let cookie: Cookie = rand_array()?;
|
|
|
+ let mut file = OpenOptions::new().write(true).create_new(true).open(&cookie_path)
|
|
|
+ .map_err(Error::from)?;
|
|
|
+ file.write_all(cookie.as_slice()).map_err(Error::from)?;
|
|
|
+ cookie
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Ok(TpmCredStore { context, cookie })
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
#[link(name = "tss2-rc")]
|
|
|
extern {
|
|
@@ -66,33 +135,6 @@ impl<T> Tss2Expect<T> for tss_esapi::Result<T> {
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
use super::*;
|
|
|
- use core::str::FromStr;
|
|
|
- use tss_esapi::{
|
|
|
- Context,
|
|
|
- constants::{
|
|
|
- session_type::{SessionType},
|
|
|
- },
|
|
|
- tcti_ldr::{DeviceConfig, TctiNameConf, TabrmdConfig},
|
|
|
- interface_types::{
|
|
|
- resource_handles::{Hierarchy},
|
|
|
- algorithm::{HashingAlgorithm},
|
|
|
- ecc::{EccCurve},
|
|
|
- },
|
|
|
- structures::{
|
|
|
- Digest,
|
|
|
- EccPoint,
|
|
|
- EccScheme,
|
|
|
- KeyDerivationFunctionScheme,
|
|
|
- HashScheme,
|
|
|
- Public,
|
|
|
- PublicEccParameters,
|
|
|
- SymmetricDefinition,
|
|
|
- SymmetricDefinitionObject,
|
|
|
- },
|
|
|
- attributes::{
|
|
|
- object::{ObjectAttributes},
|
|
|
- },
|
|
|
- };
|
|
|
|
|
|
#[test]
|
|
|
fn create_context() {
|