Skip to content

Commit 1f170fc

Browse files
committed
A few fixes
* Changed the serialisation of the TPM2B_Public to use the marshaling command * Changed the MakeCredParams struct to be fully public to avoid needless clones * Factored out the code for obtaining the EK public key Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 9d11249 commit 1f170fc

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

tss-esapi/src/abstraction/transient/key_attestation.rs

+34-36
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,23 @@ use crate::{
1010
session_handles::{AuthSession, PolicySession},
1111
},
1212
structures::{EncryptedSecret, IDObject, SymmetricDefinition},
13-
tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC},
13+
tss2_esys::{Tss2_MU_TPM2B_PUBLIC_Marshal, TPM2B_PUBLIC},
1414
utils::PublicKey,
15-
Result,
15+
Error, Result,
1616
};
17-
use std::convert::{TryFrom, TryInto};
17+
use log::error;
18+
use std::convert::TryFrom;
1819

1920
#[derive(Debug)]
2021
/// Wrapper for the parameters needed by MakeCredential
2122
pub struct MakeCredParams {
2223
/// TPM name of the object
23-
name: Vec<u8>,
24+
pub name: Vec<u8>,
2425
/// Encoding of the public parameters of the object whose name
2526
/// will be included in the credential computations
26-
public: Vec<u8>,
27+
pub public: Vec<u8>,
2728
/// 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-
}
29+
pub attesting_key_pub: PublicKey,
4330
}
4431

4532
impl TransientKeyContext {
@@ -68,29 +55,29 @@ impl TransientKeyContext {
6855
self.context.flush_context(object_handle.into())?;
6956

7057
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,
58+
let mut pub_buf = [0u8; std::mem::size_of::<TPM2B_PUBLIC>()];
59+
let mut offset = 0;
60+
let result = unsafe {
61+
Tss2_MU_TPM2B_PUBLIC_Marshal(
62+
&public,
63+
&mut pub_buf as *mut u8,
64+
pub_buf.len() as u64,
65+
&mut offset,
7466
)
7567
};
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())?;
68+
let result = Error::from_tss_rc(result);
69+
if !result.is_success() {
70+
error!("Error in marshalling TPM2B");
71+
return Err(result);
72+
}
8673

87-
attesting_key_pub.try_into()?
88-
}
74+
let attesting_key_pub = match key {
75+
None => get_ek_object_public(&mut self.context)?,
8976
Some(key) => key.material.public,
9077
};
9178
Ok(MakeCredParams {
9279
name: object_name.value().to_vec(),
93-
public: public.to_vec(),
80+
public: pub_buf.to_vec(),
9481
attesting_key_pub,
9582
})
9683
}
@@ -203,3 +190,14 @@ impl TransientKeyContext {
203190
))
204191
}
205192
}
193+
194+
fn get_ek_object_public(context: &mut crate::Context) -> Result<PublicKey> {
195+
let key_handle = ek::create_ek_object(context, AsymmetricAlgorithm::Rsa, None)?;
196+
let (attesting_key_pub, _, _) = context.read_public(key_handle).or_else(|e| {
197+
context.flush_context(key_handle.into())?;
198+
Err(e)
199+
})?;
200+
context.flush_context(key_handle.into())?;
201+
202+
PublicKey::try_from(attesting_key_pub)
203+
}

tss-esapi/src/abstraction/transient/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ use zeroize::Zeroize;
4242

4343
mod key_attestation;
4444

45+
pub use key_attestation::MakeCredParams;
46+
4547
/// Parameters for the kinds of keys supported by the context
4648
#[derive(Debug, Clone, Copy)]
4749
pub enum KeyParams {

tss-esapi/tests/integration_tests/abstraction_tests/transient_key_context_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ fn activate_credential() {
644644
name_hashing_algorithm,
645645
auth_policy,
646646
parameters,
647-
unique: if let PublicKey::Rsa(val) = make_cred_params.attesting_key_pub().clone() {
647+
unique: if let PublicKey::Rsa(val) = make_cred_params.attesting_key_pub {
648648
PublicKeyRsa::try_from(val).unwrap()
649649
} else {
650650
panic!("Wrong public key type");
@@ -664,7 +664,7 @@ fn activate_credential() {
664664
.make_credential(
665665
pub_handle,
666666
credential.clone().try_into().unwrap(),
667-
make_cred_params.name().to_vec().try_into().unwrap(),
667+
make_cred_params.name.try_into().unwrap(),
668668
)
669669
.unwrap();
670670

0 commit comments

Comments
 (0)