Skip to main content

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, Result, ReturnCode,
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        ReturnCode::ensure_success(
53            unsafe {
54                Esys_GetCapability(
55                    self.mut_context(),
56                    self.optional_session_1(),
57                    self.optional_session_2(),
58                    self.optional_session_3(),
59                    capability.into(),
60                    property,
61                    property_count,
62                    &mut more_data,
63                    &mut capability_data_ptr,
64                )
65            },
66            |ret| {
67                error!("Error when getting capabilities: {:#010X}", ret);
68            },
69        )?;
70
71        Ok((
72            CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr)?)?,
73            YesNo::try_from(more_data)?.into(),
74        ))
75    }
76
77    /// Test if the given parameters are supported by the TPM.
78    ///
79    /// # Errors
80    /// * if any of the public parameters is not compatible with the TPM,
81    ///   an `Err` containing the specific unmarshalling error will be returned.
82    pub fn test_parms(&mut self, public_parmeters: PublicParameters) -> Result<()> {
83        ReturnCode::ensure_success(
84            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            |ret| {
94                warn!(
95                    "Parameters under test could not be unmarshalled: {:#010X}",
96                    ret
97                );
98            },
99        )
100    }
101}