tss_esapi/context/tpm_commands/
testing.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    interface_types::YesNo,
5    structures::MaxBuffer,
6    tss2_esys::{Esys_GetTestResult, Esys_SelfTest},
7    Context, Error, Result,
8};
9use log::error;
10use std::convert::TryFrom;
11use std::ptr::null_mut;
12
13impl Context {
14    /// Execute the TPM self test and returns the result
15    pub fn self_test(&mut self, full_test: bool) -> Result<()> {
16        let ret = unsafe {
17            Esys_SelfTest(
18                self.mut_context(),
19                self.optional_session_1(),
20                self.optional_session_2(),
21                self.optional_session_3(),
22                YesNo::from(full_test).into(),
23            )
24        };
25        let ret = Error::from_tss_rc(ret);
26
27        if ret.is_success() {
28            Ok(())
29        } else {
30            error!("Error in self-test: {}", ret);
31            Err(ret)
32        }
33    }
34
35    // Missing function: incremental_self_test
36
37    /// Get the TPM self test result
38    ///
39    /// The returned buffer data is manufacturer-specific information.
40    pub fn get_test_result(&mut self) -> Result<(MaxBuffer, Result<()>)> {
41        let mut out_data_ptr = null_mut();
42        let mut test_result: u32 = 0;
43
44        let ret = unsafe {
45            Esys_GetTestResult(
46                self.mut_context(),
47                self.optional_session_1(),
48                self.optional_session_2(),
49                self.optional_session_3(),
50                &mut out_data_ptr,
51                &mut test_result,
52            )
53        };
54        let ret = Error::from_tss_rc(ret);
55
56        if ret.is_success() {
57            let out_data = MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr))?;
58            let test_result_rc = Error::from_tss_rc(test_result);
59            let test_result_rc = if test_result_rc.is_success() {
60                Ok(())
61            } else {
62                Err(test_result_rc)
63            };
64            Ok((out_data, test_result_rc))
65        } else {
66            error!("Error getting test result: {}", ret);
67            Err(ret)
68        }
69    }
70}