1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use crate::{
    handles::{KeyHandle, ObjectHandle},
    structures::{Attest, AttestBuffer, Data, PcrSelectionList, Signature, SignatureScheme},
    tss2_esys::{Esys_Certify, Esys_Quote},
    Context, Error, Result,
};
use log::error;
use std::convert::TryFrom;
use std::ptr::null_mut;

impl Context {
    /// Prove that an object is loaded in the TPM
    ///
    /// # Arguments
    /// * `object_handle` - Handle of the object to be certified
    /// * `signing_key_handle` - Handle of the key used to sign the attestation buffer
    /// * `qualifying_data` - Qualifying data
    /// * `signing_scheme` - Signing scheme to use if the scheme for `signing_key_handle` is `Null`.
    ///
    /// The object may be any object that is loaded with [Self::load()] or [Self::create_primary()]. An object that
    /// only has its public area loaded may not be certified.
    ///
    /// The `signing_key_handle` must be usable for signing.
    ///
    /// If `signing_key_handle` has the Restricted attribute set to `true` then `signing_scheme` must be
    /// [SignatureScheme::Null].
    ///
    /// # Returns
    /// The command returns a tuple consisting of:
    /// * `attest_data` - TPM-generated attestation data.
    /// * `signature` - Signature for the attestation data.
    ///
    /// # Errors
    /// * if the qualifying data provided is too long, a `WrongParamSize` wrapper error will be returned
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use tss_esapi::{Context, TctiNameConf};
    /// # use std::convert::TryFrom;
    /// # use tss_esapi::{
    /// #     abstraction::cipher::Cipher,
    /// #     handles::KeyHandle,
    /// #     interface_types::{
    /// #         algorithm::{HashingAlgorithm, RsaSchemeAlgorithm, SignatureSchemeAlgorithm},
    /// #         key_bits::RsaKeyBits,
    /// #         resource_handles::Hierarchy,
    /// #     },
    /// #     structures::{
    /// #         RsaExponent, RsaScheme, SymmetricDefinition,
    /// #     },
    /// #     utils::{create_unrestricted_signing_rsa_public, create_restricted_decryption_rsa_public},
    /// # };
    /// use std::convert::TryInto;
    /// use tss_esapi::{
    ///     structures::{Data, SignatureScheme},
    ///     interface_types::session_handles::AuthSession,
    /// };
    /// # let mut context =
    /// #     Context::new(
    /// #         TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
    /// #     ).expect("Failed to create Context");
    /// let qualifying_data = vec![0xff; 16];
    /// # let signing_key_pub = create_unrestricted_signing_rsa_public(
    /// #         RsaScheme::create(RsaSchemeAlgorithm::RsaSsa, Some(HashingAlgorithm::Sha256))
    /// #         .expect("Failed to create RSA scheme"),
    /// #     RsaKeyBits::Rsa2048,
    /// #     RsaExponent::default(),
    /// # )
    /// # .expect("Failed to create an unrestricted signing rsa public structure");
    /// # let sign_key_handle = context
    /// #     .execute_with_nullauth_session(|ctx| {
    /// #         ctx.create_primary(Hierarchy::Owner, signing_key_pub, None, None, None, None)
    /// #     })
    /// #     .unwrap()
    /// #     .key_handle;
    /// # let decryption_key_pub = create_restricted_decryption_rsa_public(
    /// #         Cipher::aes_256_cfb()
    /// #         .try_into()
    /// #         .expect("Failed to create symmetric object"),
    /// #     RsaKeyBits::Rsa2048,
    /// #     RsaExponent::default(),
    /// # )
    /// # .expect("Failed to create a restricted decryption rsa public structure");
    /// # let obj_key_handle = context
    /// #     .execute_with_nullauth_session(|ctx| {
    /// #         ctx.create_primary(
    /// #             Hierarchy::Owner,
    /// #             decryption_key_pub,
    /// #             None,
    /// #             None,
    /// #             None,
    /// #             None,
    /// #         )
    /// #     })
    /// #     .unwrap()
    /// #     .key_handle;
    /// let (attest, signature) = context
    ///     .execute_with_sessions(
    ///         (
    ///             Some(AuthSession::Password),
    ///             Some(AuthSession::Password),
    ///             None,
    ///         ),
    ///         |ctx| {
    ///             ctx.certify(
    ///                 obj_key_handle.into(),
    ///                 sign_key_handle,
    ///                 Data::try_from(qualifying_data).unwrap(),
    ///                 SignatureScheme::Null,
    ///             )
    ///         },
    ///     )
    ///     .expect("Failed to certify object handle");
    /// ```
    pub fn certify(
        &mut self,
        object_handle: ObjectHandle,
        signing_key_handle: KeyHandle,
        qualifying_data: Data,
        signing_scheme: SignatureScheme,
    ) -> Result<(Attest, Signature)> {
        let mut certify_info_ptr = null_mut();
        let mut signature_ptr = null_mut();
        let ret = unsafe {
            Esys_Certify(
                self.mut_context(),
                object_handle.into(),
                signing_key_handle.into(),
                self.required_session_1()?,
                self.required_session_2()?,
                self.optional_session_3(),
                &qualifying_data.into(),
                &signing_scheme.into(),
                &mut certify_info_ptr,
                &mut signature_ptr,
            )
        };
        let ret = Error::from_tss_rc(ret);

        if ret.is_success() {
            let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
            let signature = Context::ffi_data_to_owned(signature_ptr);
            Ok((
                Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
                Signature::try_from(signature)?,
            ))
        } else {
            error!("Error in certifying: {}", ret);
            Err(ret)
        }
    }

    // Missing function: CertifyCreation

    /// Generate a quote on the selected PCRs
    ///
    /// # Errors
    /// * if the qualifying data provided is too long, a `WrongParamSize` wrapper error will be returned
    pub fn quote(
        &mut self,
        signing_key_handle: KeyHandle,
        qualifying_data: Data,
        signing_scheme: SignatureScheme,
        pcr_selection_list: PcrSelectionList,
    ) -> Result<(Attest, Signature)> {
        let mut quoted_ptr = null_mut();
        let mut signature_ptr = null_mut();
        let ret = unsafe {
            Esys_Quote(
                self.mut_context(),
                signing_key_handle.into(),
                self.optional_session_1(),
                self.optional_session_2(),
                self.optional_session_3(),
                &qualifying_data.into(),
                &signing_scheme.into(),
                &pcr_selection_list.into(),
                &mut quoted_ptr,
                &mut signature_ptr,
            )
        };
        let ret = Error::from_tss_rc(ret);

        if ret.is_success() {
            let quoted = Context::ffi_data_to_owned(quoted_ptr);
            let signature = Context::ffi_data_to_owned(signature_ptr);
            Ok((
                Attest::try_from(AttestBuffer::try_from(quoted)?)?,
                Signature::try_from(signature)?,
            ))
        } else {
            error!("Error in quoting PCR: {}", ret);
            Err(ret)
        }
    }

    // Missing function: GetSessionAuditDigest
    // Missing function: GestCommandAuditDigest
    // Missing function: GetTime
    // Missing function: CertifyX509
}