|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +use super::{ObjectWrapper, TransientKeyContext}; |
| 4 | +use crate::{ |
| 5 | + abstraction::ek, |
| 6 | + constants::SessionType, |
| 7 | + handles::{AuthHandle, KeyHandle, SessionHandle}, |
| 8 | + interface_types::{ |
| 9 | + algorithm::{AsymmetricAlgorithm, HashingAlgorithm}, |
| 10 | + session_handles::{AuthSession, PolicySession}, |
| 11 | + }, |
| 12 | + structures::{EncryptedSecret, IDObject, SymmetricDefinition}, |
| 13 | + tss2_esys::{Tss2_MU_TPMT_PUBLIC_Marshal, TPM2B_PUBLIC, TPMT_PUBLIC}, |
| 14 | + utils::PublicKey, |
| 15 | + Error, Result, WrapperErrorKind, |
| 16 | +}; |
| 17 | +use log::error; |
| 18 | +use std::convert::{TryFrom, TryInto}; |
| 19 | + |
| 20 | +#[derive(Debug)] |
| 21 | +/// Wrapper for the parameters needed by MakeCredential |
| 22 | +/// |
| 23 | +/// The 3rd party requesting proof that the key is indeed backed |
| 24 | +/// by a TPM would perform a MakeCredential and would thus require |
| 25 | +/// `name` and `attesting_key_pub` as inputs for that operation. |
| 26 | +/// |
| 27 | +/// `public` is not strictly needed, however it is returned as a |
| 28 | +/// convenience block of data. Since the MakeCredential operation |
| 29 | +/// bakes into the encrypted credential the identity of the key to |
| 30 | +/// be attested via its `name`, the correctness of the `name` must |
| 31 | +/// be verifiable by the said 3rd party. `public` bridges this gap: |
| 32 | +/// |
| 33 | +/// * it includes all the public parameters of the attested key |
| 34 | +/// * can be hashed (in its marshaled form) with the name hash |
| 35 | +/// (found by unmarshaling it) to obtain `name` |
| 36 | +pub struct MakeCredParams { |
| 37 | + /// TPM name of the object being attested |
| 38 | + pub name: Vec<u8>, |
| 39 | + /// Encoding of the public parameters of the object whose name |
| 40 | + /// will be included in the credential computations |
| 41 | + pub public: Vec<u8>, |
| 42 | + /// Public part of the key used to protect the credential |
| 43 | + pub attesting_key_pub: PublicKey, |
| 44 | +} |
| 45 | + |
| 46 | +impl TransientKeyContext { |
| 47 | + /// Get the data required to perform a MakeCredential |
| 48 | + /// |
| 49 | + /// # Parameters |
| 50 | + /// |
| 51 | + /// * `object` - the object whose TPM name will be included in |
| 52 | + /// the credential |
| 53 | + /// * `key` - the key to be used to encrypt the secret that wraps |
| 54 | + /// the credential |
| 55 | + /// |
| 56 | + /// **Note**: If no `key` is given, the default Endorsement Key |
| 57 | + /// will be used. |
| 58 | + pub fn get_make_cred_params( |
| 59 | + &mut self, |
| 60 | + object: ObjectWrapper, |
| 61 | + key: Option<ObjectWrapper>, |
| 62 | + ) -> Result<MakeCredParams> { |
| 63 | + let object_handle = self.load_key(object.params, object.material, None)?; |
| 64 | + let (object_public, object_name, _) = |
| 65 | + self.context.read_public(object_handle).or_else(|e| { |
| 66 | + self.context.flush_context(object_handle.into())?; |
| 67 | + Err(e) |
| 68 | + })?; |
| 69 | + self.context.flush_context(object_handle.into())?; |
| 70 | + |
| 71 | + // Name of objects is derived from their publicArea, i.e. the marshaled TPMT_PUBLIC |
| 72 | + let public = TPM2B_PUBLIC::from(object_public); |
| 73 | + let mut pub_buf = [0u8; std::mem::size_of::<TPMT_PUBLIC>()]; |
| 74 | + let mut offset = 0; |
| 75 | + let result = unsafe { |
| 76 | + Tss2_MU_TPMT_PUBLIC_Marshal( |
| 77 | + &public.publicArea, |
| 78 | + &mut pub_buf as *mut u8, |
| 79 | + std::mem::size_of::<TPMT_PUBLIC>() |
| 80 | + .try_into() |
| 81 | + .map_err(|_| Error::local_error(WrapperErrorKind::InternalError))?, |
| 82 | + &mut offset, |
| 83 | + ) |
| 84 | + }; |
| 85 | + let result = Error::from_tss_rc(result); |
| 86 | + if !result.is_success() { |
| 87 | + error!("Error in marshalling TPM2B"); |
| 88 | + return Err(result); |
| 89 | + } |
| 90 | + // `offset` will be small, so no risk in the conversion below |
| 91 | + let public = pub_buf[..offset as usize].to_vec(); |
| 92 | + |
| 93 | + let attesting_key_pub = match key { |
| 94 | + None => get_ek_object_public(&mut self.context)?, |
| 95 | + Some(key) => key.material.public, |
| 96 | + }; |
| 97 | + Ok(MakeCredParams { |
| 98 | + name: object_name.value().to_vec(), |
| 99 | + public, |
| 100 | + attesting_key_pub, |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + /// Perform an ActivateCredential operation for the given object |
| 105 | + /// |
| 106 | + /// # Parameters |
| 107 | + /// |
| 108 | + /// * `object` - the object whose TPM name is included in the credential |
| 109 | + /// * `key` - the key used to encrypt the secret that wraps the credential |
| 110 | + /// * `credential_blob` - encrypted credential that will be returned by the |
| 111 | + /// TPM |
| 112 | + /// * `secret` - encrypted secret that was used to encrypt the credential |
| 113 | + /// |
| 114 | + /// **Note**: if no `key` is given, the default Endorsement Key |
| 115 | + /// will be used. You can find more information about the default Endorsement |
| 116 | + /// Key in the [ek] module. |
| 117 | + pub fn activate_credential( |
| 118 | + &mut self, |
| 119 | + object: ObjectWrapper, |
| 120 | + key: Option<ObjectWrapper>, |
| 121 | + credential_blob: Vec<u8>, |
| 122 | + secret: Vec<u8>, |
| 123 | + ) -> Result<Vec<u8>> { |
| 124 | + let credential_blob = IDObject::try_from(credential_blob)?; |
| 125 | + let secret = EncryptedSecret::try_from(secret)?; |
| 126 | + let object_handle = self.load_key(object.params, object.material, object.auth)?; |
| 127 | + let (key_handle, session_2) = match key { |
| 128 | + Some(key) => self.prepare_key_activate_cred(key), |
| 129 | + None => self.prepare_ek_activate_cred(), |
| 130 | + } |
| 131 | + .or_else(|e| { |
| 132 | + self.context.flush_context(object_handle.into())?; |
| 133 | + Err(e) |
| 134 | + })?; |
| 135 | + |
| 136 | + let (session_1, _, _) = self.context.sessions(); |
| 137 | + let credential = self |
| 138 | + .context |
| 139 | + .execute_with_sessions((session_1, session_2, None), |ctx| { |
| 140 | + ctx.activate_credential(object_handle, key_handle, credential_blob, secret) |
| 141 | + }) |
| 142 | + .or_else(|e| { |
| 143 | + self.context.flush_context(object_handle.into())?; |
| 144 | + self.context.flush_context(key_handle.into())?; |
| 145 | + self.context |
| 146 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 147 | + Err(e) |
| 148 | + })?; |
| 149 | + |
| 150 | + self.context.flush_context(object_handle.into())?; |
| 151 | + self.context.flush_context(key_handle.into())?; |
| 152 | + self.context |
| 153 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 154 | + Ok(credential.value().to_vec()) |
| 155 | + } |
| 156 | + |
| 157 | + // No key was given, use the EK. This requires using a Policy session |
| 158 | + fn prepare_ek_activate_cred(&mut self) -> Result<(KeyHandle, Option<AuthSession>)> { |
| 159 | + let session = self.context.start_auth_session( |
| 160 | + None, |
| 161 | + None, |
| 162 | + None, |
| 163 | + SessionType::Policy, |
| 164 | + SymmetricDefinition::AES_128_CFB, |
| 165 | + HashingAlgorithm::Sha256, |
| 166 | + )?; |
| 167 | + let _ = self.context.policy_secret( |
| 168 | + PolicySession::try_from(session.unwrap()) |
| 169 | + .expect("Failed to convert auth session to policy session"), |
| 170 | + AuthHandle::Endorsement, |
| 171 | + Default::default(), |
| 172 | + Default::default(), |
| 173 | + Default::default(), |
| 174 | + None, |
| 175 | + ); |
| 176 | + Ok(( |
| 177 | + ek::create_ek_object(&mut self.context, AsymmetricAlgorithm::Rsa, None).or_else( |
| 178 | + |e| { |
| 179 | + self.context |
| 180 | + .flush_context(SessionHandle::from(session).into())?; |
| 181 | + Err(e) |
| 182 | + }, |
| 183 | + )?, |
| 184 | + session, |
| 185 | + )) |
| 186 | + } |
| 187 | + |
| 188 | + // Load key and create a HMAC session for it |
| 189 | + fn prepare_key_activate_cred( |
| 190 | + &mut self, |
| 191 | + key: ObjectWrapper, |
| 192 | + ) -> Result<(KeyHandle, Option<AuthSession>)> { |
| 193 | + let session = self.context.start_auth_session( |
| 194 | + None, |
| 195 | + None, |
| 196 | + None, |
| 197 | + SessionType::Hmac, |
| 198 | + SymmetricDefinition::AES_128_CFB, |
| 199 | + HashingAlgorithm::Sha256, |
| 200 | + )?; |
| 201 | + Ok(( |
| 202 | + self.load_key(key.params, key.material, key.auth) |
| 203 | + .or_else(|e| { |
| 204 | + self.context |
| 205 | + .flush_context(SessionHandle::from(session).into())?; |
| 206 | + Err(e) |
| 207 | + })?, |
| 208 | + session, |
| 209 | + )) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +fn get_ek_object_public(context: &mut crate::Context) -> Result<PublicKey> { |
| 214 | + let key_handle = ek::create_ek_object(context, AsymmetricAlgorithm::Rsa, None)?; |
| 215 | + let (attesting_key_pub, _, _) = context.read_public(key_handle).or_else(|e| { |
| 216 | + context.flush_context(key_handle.into())?; |
| 217 | + Err(e) |
| 218 | + })?; |
| 219 | + context.flush_context(key_handle.into())?; |
| 220 | + |
| 221 | + PublicKey::try_from(attesting_key_pub) |
| 222 | +} |
0 commit comments