tss_esapi/context/tpm_commands/capability_commands.rs
1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4 constants::CapabilityType,
5 interface_types::YesNo,
6 structures::{CapabilityData, PublicParameters},
7 tss2_esys::{Esys_GetCapability, Esys_TestParms},
8 Context, Error, Result,
9};
10use log::{error, warn};
11use std::convert::TryFrom;
12use std::ptr::null_mut;
13
14impl Context {
15 /// Get current capability information about the TPM.
16 ///
17 /// # Warning
18 /// - If [CapabilityType::AuthPolicies] is used but the version of the
19 /// tpm2-tss library used does not have the 'authPolicies' field
20 /// in the TPMU_CAPABILITIES defined then the call using this method
21 /// will fail.
22 ///
23 /// - If [CapabilityType::Act] is used but the the version of the
24 /// tpm2-tss library used does not have the 'actData' field in the
25 /// TPMU_CAPABILITIES defined then the call using this method will fail.
26 ///
27 /// # Example
28 ///
29 /// ```rust
30 /// # use tss_esapi::{Context, TctiNameConf};
31 /// # // Create context
32 /// # let mut context =
33 /// # Context::new(
34 /// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
35 /// # ).expect("Failed to create Context");
36 /// #
37 /// use tss_esapi::constants::CapabilityType;
38 ///
39 /// let (_capabilities, _more) = context
40 /// .get_capability(CapabilityType::Algorithms, 0, 80)
41 /// .expect("Failed to call get_capability");
42 /// ```
43 pub fn get_capability(
44 &mut self,
45 capability: CapabilityType,
46 property: u32,
47 property_count: u32,
48 ) -> Result<(CapabilityData, bool)> {
49 let mut capability_data_ptr = null_mut();
50 let mut more_data = YesNo::No.into();
51
52 let ret = unsafe {
53 Esys_GetCapability(
54 self.mut_context(),
55 self.optional_session_1(),
56 self.optional_session_2(),
57 self.optional_session_3(),
58 capability.into(),
59 property,
60 property_count,
61 &mut more_data,
62 &mut capability_data_ptr,
63 )
64 };
65 let ret = Error::from_tss_rc(ret);
66
67 if ret.is_success() {
68 Ok((
69 CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr))?,
70 YesNo::try_from(more_data)?.into(),
71 ))
72 } else {
73 error!("Error when getting capabilities: {}", ret);
74 Err(ret)
75 }
76 }
77
78 /// Test if the given parameters are supported by the TPM.
79 ///
80 /// # Errors
81 /// * if any of the public parameters is not compatible with the TPM,
82 /// an `Err` containing the specific unmarshalling error will be returned.
83 pub fn test_parms(&mut self, public_parmeters: PublicParameters) -> Result<()> {
84 let ret = unsafe {
85 Esys_TestParms(
86 self.mut_context(),
87 self.optional_session_1(),
88 self.optional_session_2(),
89 self.optional_session_3(),
90 &public_parmeters.into(),
91 )
92 };
93
94 let ret = Error::from_tss_rc(ret);
95 if ret.is_success() {
96 Ok(())
97 } else {
98 warn!("Parameters under test could not be unmarshalled: {}", ret);
99 Err(ret)
100 }
101 }
102}