|
| 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, SessionHandle}, |
| 8 | + interface_types::{ |
| 9 | + algorithm::{AsymmetricAlgorithm, HashingAlgorithm}, |
| 10 | + session_handles::PolicySession, |
| 11 | + }, |
| 12 | + structures::{EncryptedSecret, IDObject, SymmetricDefinition}, |
| 13 | + tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC}, |
| 14 | + utils::PublicKey, |
| 15 | + Result, |
| 16 | +}; |
| 17 | +use std::convert::{TryFrom, TryInto}; |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +/// Wrapper for the parameters needed by MakeCredential |
| 21 | +pub struct MakeCredParams { |
| 22 | + /// TPM name of the object |
| 23 | + name: Vec<u8>, |
| 24 | + /// Encoding of the public parameters of the object whose name |
| 25 | + /// will be included in the credential computations |
| 26 | + public: Vec<u8>, |
| 27 | + /// Public part of the key used to protect the credential |
| 28 | + attesting_key_pub: PublicKey, |
| 29 | +} |
| 30 | + |
| 31 | +impl MakeCredParams { |
| 32 | + pub fn name(&self) -> &[u8] { |
| 33 | + &self.name |
| 34 | + } |
| 35 | + |
| 36 | + pub fn public(&self) -> &[u8] { |
| 37 | + &self.public |
| 38 | + } |
| 39 | + |
| 40 | + pub fn attesting_key_pub(&self) -> &PublicKey { |
| 41 | + &self.attesting_key_pub |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl TransientKeyContext { |
| 46 | + /// Get the data required to perform a MakeCredential |
| 47 | + /// |
| 48 | + /// # Parameters |
| 49 | + /// |
| 50 | + /// * `object` - the object whose TPM name will be included in |
| 51 | + /// the credential |
| 52 | + /// * `key` - the key to be used to encrypt the secret that wraps |
| 53 | + /// the credential |
| 54 | + /// |
| 55 | + /// **Note**: If no `key` is given, the default Endorsement Key |
| 56 | + /// will be used. |
| 57 | + pub fn get_make_cred_params( |
| 58 | + &mut self, |
| 59 | + object: ObjectWrapper, |
| 60 | + key: Option<ObjectWrapper>, |
| 61 | + ) -> Result<MakeCredParams> { |
| 62 | + let object_handle = self.load_key(object.params, object.material, None)?; |
| 63 | + let (object_public, object_name, _) = |
| 64 | + self.context.read_public(object_handle).or_else(|e| { |
| 65 | + self.context.flush_context(object_handle.into())?; |
| 66 | + Err(e) |
| 67 | + })?; |
| 68 | + self.context.flush_context(object_handle.into())?; |
| 69 | + |
| 70 | + let public = TPM2B_PUBLIC::from(object_public); |
| 71 | + let public = unsafe { |
| 72 | + std::mem::transmute::<TPMT_PUBLIC, [u8; std::mem::size_of::<TPMT_PUBLIC>()]>( |
| 73 | + public.publicArea, |
| 74 | + ) |
| 75 | + }; |
| 76 | + let attesting_key_pub = match key { |
| 77 | + None => { |
| 78 | + let key_handle = |
| 79 | + ek::create_ek_object(&mut self.context, AsymmetricAlgorithm::Rsa, None)?; |
| 80 | + let (attesting_key_pub, _, _) = |
| 81 | + self.context.read_public(key_handle).or_else(|e| { |
| 82 | + self.context.flush_context(key_handle.into())?; |
| 83 | + Err(e) |
| 84 | + })?; |
| 85 | + self.context.flush_context(key_handle.into())?; |
| 86 | + |
| 87 | + attesting_key_pub.try_into()? |
| 88 | + } |
| 89 | + Some(key) => key.material.public, |
| 90 | + }; |
| 91 | + Ok(MakeCredParams { |
| 92 | + name: object_name.value().to_vec(), |
| 93 | + public: public.to_vec(), |
| 94 | + attesting_key_pub, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + /// Perform an ActivateCredential operation for the given object |
| 99 | + /// |
| 100 | + /// # Parameters |
| 101 | + /// |
| 102 | + /// * `object` - the object whose TPM name is included in the credential |
| 103 | + /// * `key` - the key used to encrypt the secret that wraps the credential |
| 104 | + /// * `credential_blob` - encrypted credential that will be returned by the |
| 105 | + /// TPM |
| 106 | + /// * `secret` - encrypted secret that was used to encrypt the credential |
| 107 | + /// |
| 108 | + /// **Note**: if no `key` is given, the default Endorsement Key |
| 109 | + /// will be used. You can find more information about the default Endorsement |
| 110 | + /// Key in the [ek] module. |
| 111 | + pub fn activate_credential( |
| 112 | + &mut self, |
| 113 | + object: ObjectWrapper, |
| 114 | + key: Option<ObjectWrapper>, |
| 115 | + credential_blob: Vec<u8>, |
| 116 | + secret: Vec<u8>, |
| 117 | + ) -> Result<Vec<u8>> { |
| 118 | + let credential_blob = IDObject::try_from(credential_blob)?; |
| 119 | + let secret = EncryptedSecret::try_from(secret)?; |
| 120 | + let object_handle = self.load_key(object.params, object.material, object.auth)?; |
| 121 | + let session_2; |
| 122 | + let key_handle = match key { |
| 123 | + None => { |
| 124 | + // No key was given, use the EK. This requires using a Policy session |
| 125 | + session_2 = self |
| 126 | + .context |
| 127 | + .start_auth_session( |
| 128 | + None, |
| 129 | + None, |
| 130 | + None, |
| 131 | + SessionType::Policy, |
| 132 | + SymmetricDefinition::AES_128_CFB, |
| 133 | + HashingAlgorithm::Sha256, |
| 134 | + ) |
| 135 | + .or_else(|e| { |
| 136 | + self.context.flush_context(object_handle.into())?; |
| 137 | + Err(e) |
| 138 | + })?; |
| 139 | + let _ = self.context.policy_secret( |
| 140 | + PolicySession::try_from(session_2.unwrap()) |
| 141 | + .expect("Failed to convert auth session to policy session"), |
| 142 | + AuthHandle::Endorsement, |
| 143 | + Default::default(), |
| 144 | + Default::default(), |
| 145 | + Default::default(), |
| 146 | + None, |
| 147 | + ); |
| 148 | + ek::create_ek_object(&mut self.context, AsymmetricAlgorithm::Rsa, None).or_else( |
| 149 | + |e| { |
| 150 | + self.context.flush_context(object_handle.into())?; |
| 151 | + self.context |
| 152 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 153 | + Err(e) |
| 154 | + }, |
| 155 | + )? |
| 156 | + } |
| 157 | + Some(key) => { |
| 158 | + // Load key and create a HMAC session for it |
| 159 | + session_2 = self |
| 160 | + .context |
| 161 | + .start_auth_session( |
| 162 | + None, |
| 163 | + None, |
| 164 | + None, |
| 165 | + SessionType::Hmac, |
| 166 | + SymmetricDefinition::AES_128_CFB, |
| 167 | + HashingAlgorithm::Sha256, |
| 168 | + ) |
| 169 | + .or_else(|e| { |
| 170 | + self.context.flush_context(object_handle.into())?; |
| 171 | + Err(e) |
| 172 | + })?; |
| 173 | + self.load_key(key.params, key.material, key.auth) |
| 174 | + .or_else(|e| { |
| 175 | + self.context.flush_context(object_handle.into())?; |
| 176 | + self.context |
| 177 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 178 | + Err(e) |
| 179 | + })? |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + let (session_1, _, _) = self.context.sessions(); |
| 184 | + let credential = self |
| 185 | + .context |
| 186 | + .execute_with_sessions((session_1, session_2, None), |ctx| { |
| 187 | + ctx.activate_credential(object_handle, key_handle, credential_blob, secret) |
| 188 | + }) |
| 189 | + .or_else(|e| { |
| 190 | + self.context.flush_context(object_handle.into())?; |
| 191 | + self.context.flush_context(key_handle.into())?; |
| 192 | + self.context |
| 193 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 194 | + Err(e) |
| 195 | + })?; |
| 196 | + |
| 197 | + self.context.flush_context(object_handle.into())?; |
| 198 | + self.context.flush_context(key_handle.into())?; |
| 199 | + self.context |
| 200 | + .flush_context(SessionHandle::from(session_2).into())?; |
| 201 | + Ok(credential.value().to_vec()) |
| 202 | + } |
| 203 | +} |
0 commit comments