tss_esapi/context/tpm_commands/command_audit.rs
1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4 Context, Result, ReturnCode, handles::AuthHandle, interface_types::algorithm::HashingAlgorithm,
5 structures::CommandCodeList, tss2_esys::Esys_SetCommandCodeAuditStatus,
6};
7use log::error;
8
9impl Context {
10 /// Set the command code audit status.
11 ///
12 /// # Arguments
13 ///
14 /// * `auth_handle` - An [AuthHandle] for the authorization (Owner or Platform).
15 /// * `audit_algorithm` - The [HashingAlgorithm] for the audit digest.
16 /// * `set_list` - A [CommandCodeList] of command codes to add to the audit list.
17 /// * `clear_list` - A [CommandCodeList] of command codes to remove from the audit list.
18 ///
19 /// # Details
20 ///
21 /// *From the specification*
22 /// > This command may be used by the Privacy Administrator or platform
23 /// > to change the audit status of a command or to set the hash
24 /// > algorithm used for the audit digest.
25 ///
26 /// # Example
27 ///
28 /// ```rust
29 /// # use tss_esapi::{Context, TctiNameConf};
30 /// # use tss_esapi::handles::AuthHandle;
31 /// # use tss_esapi::interface_types::{
32 /// # algorithm::HashingAlgorithm,
33 /// # session_handles::AuthSession,
34 /// # };
35 /// # use tss_esapi::structures::CommandCodeList;
36 /// # let mut context =
37 /// # Context::new(
38 /// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
39 /// # ).expect("Failed to create Context");
40 /// context
41 /// .execute_with_sessions((Some(AuthSession::Password), None, None), |ctx| {
42 /// ctx.set_command_code_audit_status(
43 /// AuthHandle::Owner,
44 /// HashingAlgorithm::Sha256,
45 /// CommandCodeList::new(),
46 /// CommandCodeList::new(),
47 /// )
48 /// })
49 /// .expect("Failed to set command code audit status");
50 /// ```
51 pub fn set_command_code_audit_status(
52 &mut self,
53 auth_handle: AuthHandle,
54 audit_algorithm: HashingAlgorithm,
55 set_list: CommandCodeList,
56 clear_list: CommandCodeList,
57 ) -> Result<()> {
58 ReturnCode::ensure_success(
59 unsafe {
60 Esys_SetCommandCodeAuditStatus(
61 self.mut_context(),
62 auth_handle.into(),
63 self.required_session_1()?,
64 self.optional_session_2(),
65 self.optional_session_3(),
66 audit_algorithm.into(),
67 &set_list.into(),
68 &clear_list.into(),
69 )
70 },
71 |ret| {
72 error!("Error setting command code audit status: {:#010X}", ret);
73 },
74 )
75 }
76}