Skip to content

Commit b7718a0

Browse files
committed
Improves 'use' and naming in context methods.
Removes all 'use crate::tss2_esys::*' and replaces it with more specific use declarations that only imports with module needs. Makes the naming of variables in the context methods more consistent by allways using the name that are snake case versions of the names used in the declaration of the Tss_Esys-functions. A '_ptr' suffix is used when it is a variable that will contain a pointer to a memory that is not freed by the tpm2-tss library. Makes use of the YesNo interface for handling the conversions from bool to TPMI_YES_NO. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent a897f3d commit b7718a0

17 files changed

+247
-271
lines changed

tss-esapi/src/context/general_esys_tr.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use crate::{
66
handles::{handle_conversion::TryIntoNotNone, TpmHandle},
77
structures::Auth,
88
structures::Name,
9-
tss2_esys::{
10-
Esys_TR_Close, Esys_TR_FromTPMPublic, Esys_TR_GetName, Esys_TR_SetAuth, ESYS_TR,
11-
ESYS_TR_NONE, TPM2B_NAME,
12-
},
9+
tss2_esys::{Esys_TR_Close, Esys_TR_FromTPMPublic, Esys_TR_GetName, Esys_TR_SetAuth},
1310
Context, Error, Result,
1411
};
1512
use log::error;
@@ -21,9 +18,9 @@ use zeroize::Zeroize;
2118
impl Context {
2219
/// Set the authentication value for a given object handle in the ESYS context.
2320
pub fn tr_set_auth(&mut self, object_handle: ObjectHandle, auth: Auth) -> Result<()> {
24-
let mut tss_auth = auth.into();
25-
let ret = unsafe { Esys_TR_SetAuth(self.mut_context(), object_handle.into(), &tss_auth) };
26-
tss_auth.buffer.zeroize();
21+
let mut auth_value = auth.into();
22+
let ret = unsafe { Esys_TR_SetAuth(self.mut_context(), object_handle.into(), &auth_value) };
23+
auth_value.buffer.zeroize();
2724
let ret = Error::from_tss_rc(ret);
2825
if ret.is_success() {
2926
Ok(())
@@ -35,12 +32,13 @@ impl Context {
3532

3633
/// Retrieve the name of an object from the object handle
3734
pub fn tr_get_name(&mut self, object_handle: ObjectHandle) -> Result<Name> {
38-
let mut name = null_mut();
39-
let ret = unsafe { Esys_TR_GetName(self.mut_context(), object_handle.into(), &mut name) };
35+
let mut name_ptr = null_mut();
36+
let ret =
37+
unsafe { Esys_TR_GetName(self.mut_context(), object_handle.into(), &mut name_ptr) };
4038
let ret = Error::from_tss_rc(ret);
4139
if ret.is_success() {
42-
let tss_name = unsafe { MBox::<TPM2B_NAME>::from_raw(name) };
43-
Ok(Name::try_from(*tss_name)?)
40+
let name = unsafe { MBox::from_raw(name_ptr) };
41+
Ok(Name::try_from(*name)?)
4442
} else {
4543
error!("Error in getting name: {}", ret);
4644
Err(ret)
@@ -49,29 +47,28 @@ impl Context {
4947

5048
/// Used to construct an esys object from the resources inside the TPM.
5149
pub fn tr_from_tpm_public(&mut self, tpm_handle: TpmHandle) -> Result<ObjectHandle> {
52-
let mut esys_object_handle: ESYS_TR = ESYS_TR_NONE;
50+
let mut object = ObjectHandle::None.into();
5351
let ret = unsafe {
5452
Esys_TR_FromTPMPublic(
5553
self.mut_context(),
5654
tpm_handle.into(),
5755
self.optional_session_1(),
5856
self.optional_session_2(),
5957
self.optional_session_3(),
60-
&mut esys_object_handle,
58+
&mut object,
6159
)
6260
};
6361
let ret = Error::from_tss_rc(ret);
6462
if ret.is_success() {
65-
let object_handle = ObjectHandle::from(esys_object_handle);
6663
self.handle_manager.add_handle(
67-
object_handle,
64+
object.into(),
6865
if tpm_handle.may_be_flushed() {
6966
HandleDropAction::Flush
7067
} else {
7168
HandleDropAction::Close
7269
},
7370
)?;
74-
Ok(object_handle)
71+
Ok(object.into())
7572
} else {
7673
error!("Error when getting ESYS handle from TPM handle: {}", ret);
7774
Err(ret)
@@ -82,12 +79,12 @@ impl Context {
8279
///
8380
/// This is useful for cleaning up handles for which the context cannot be flushed.
8481
pub fn tr_close(&mut self, object_handle: &mut ObjectHandle) -> Result<()> {
85-
let mut tss_esys_object_handle = object_handle.try_into_not_none()?;
86-
let ret = unsafe { Esys_TR_Close(self.mut_context(), &mut tss_esys_object_handle) };
82+
let mut rsrc_handle = object_handle.try_into_not_none()?;
83+
let ret = unsafe { Esys_TR_Close(self.mut_context(), &mut rsrc_handle) };
8784
let ret = Error::from_tss_rc(ret);
8885
if ret.is_success() {
8986
self.handle_manager.set_as_closed(*object_handle)?;
90-
*object_handle = ObjectHandle::from(tss_esys_object_handle);
87+
*object_handle = ObjectHandle::from(rsrc_handle);
9188
Ok(())
9289
} else {
9390
error!("Error when closing an ESYS handle: {}", ret);

tss-esapi/src/context/session_administration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
attributes::{SessionAttributes, SessionAttributesMask},
55
handles::SessionHandle,
66
interface_types::session_handles::AuthSession,
7-
tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_SetAttributes, TPMA_SESSION},
7+
tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_SetAttributes},
88
Context, Error, Result,
99
};
1010
use log::error;
@@ -36,7 +36,7 @@ impl Context {
3636

3737
/// Get session attribute flags.
3838
pub fn tr_sess_get_attributes(&mut self, session: AuthSession) -> Result<SessionAttributes> {
39-
let mut flags: TPMA_SESSION = 0;
39+
let mut flags = 0;
4040
let ret = unsafe {
4141
Esys_TRSess_GetAttributes(
4242
self.mut_context(),

tss-esapi/src/context/tpm_commands/asymmetric_primitives.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ use crate::{
44
handles::KeyHandle,
55
structures::Data,
66
structures::{EccPoint, PublicKeyRsa, RsaDecryptionScheme},
7-
tss2_esys::*,
7+
tss2_esys::{Esys_ECDH_KeyGen, Esys_ECDH_ZGen, Esys_RSA_Decrypt, Esys_RSA_Encrypt},
88
Context, Error, Result,
99
};
1010
use log::error;
11-
use std::convert::TryFrom;
12-
use std::convert::TryInto;
11+
use std::convert::{TryFrom, TryInto};
1312
use std::ptr::null_mut;
1413

1514
impl Context {
@@ -21,7 +20,7 @@ impl Context {
2120
in_scheme: RsaDecryptionScheme,
2221
label: Data,
2322
) -> Result<PublicKeyRsa> {
24-
let mut out_data = null_mut();
23+
let mut out_data_ptr = null_mut();
2524
let ret = unsafe {
2625
Esys_RSA_Encrypt(
2726
self.mut_context(),
@@ -32,13 +31,13 @@ impl Context {
3231
&message.into(),
3332
&in_scheme.into(),
3433
&label.into(),
35-
&mut out_data,
34+
&mut out_data_ptr,
3635
)
3736
};
3837
let ret = Error::from_tss_rc(ret);
3938

4039
if ret.is_success() {
41-
let data = unsafe { PublicKeyRsa::try_from(*out_data)? };
40+
let data = unsafe { PublicKeyRsa::try_from(*out_data_ptr)? };
4241
Ok(data)
4342
} else {
4443
error!("Error when performing RSA encryption: {}", ret);
@@ -54,7 +53,7 @@ impl Context {
5453
in_scheme: RsaDecryptionScheme,
5554
label: Data,
5655
) -> Result<PublicKeyRsa> {
57-
let mut message = null_mut();
56+
let mut message_ptr = null_mut();
5857
let ret = unsafe {
5958
Esys_RSA_Decrypt(
6059
self.mut_context(),
@@ -65,13 +64,13 @@ impl Context {
6564
&cipher_text.into(),
6665
&in_scheme.into(),
6766
&label.into(),
68-
&mut message,
67+
&mut message_ptr,
6968
)
7069
};
7170
let ret = Error::from_tss_rc(ret);
7271

7372
if ret.is_success() {
74-
let data = unsafe { PublicKeyRsa::try_from(*message)? };
73+
let data = unsafe { PublicKeyRsa::try_from(*message_ptr)? };
7574
Ok(data)
7675
} else {
7776
error!("Error when performing RSA decryption: {}", ret);
@@ -186,24 +185,29 @@ impl Context {
186185
/// let (z_point, pub_point) = context.ecdh_key_gen(key_handle).unwrap();
187186
/// ```
188187
pub fn ecdh_key_gen(&mut self, key_handle: KeyHandle) -> Result<(EccPoint, EccPoint)> {
189-
let mut z_point = null_mut();
190-
let mut pub_point = null_mut();
188+
let mut z_point_ptr = null_mut();
189+
let mut pub_point_ptr = null_mut();
191190
let ret = unsafe {
192191
Esys_ECDH_KeyGen(
193192
self.mut_context(),
194193
key_handle.into(),
195194
self.optional_session_1(),
196195
self.optional_session_2(),
197196
self.optional_session_3(),
198-
&mut z_point,
199-
&mut pub_point,
197+
&mut z_point_ptr,
198+
&mut pub_point_ptr,
200199
)
201200
};
202201

203202
let ret = Error::from_tss_rc(ret);
204203

205204
if ret.is_success() {
206-
Ok(unsafe { ((*z_point).point.try_into()?, (*pub_point).point.try_into()?) })
205+
Ok(unsafe {
206+
(
207+
(*z_point_ptr).point.try_into()?,
208+
(*pub_point_ptr).point.try_into()?,
209+
)
210+
})
207211
} else {
208212
error!("Error when generating ECDH keypair: {}", ret);
209213
Err(ret)
@@ -320,7 +324,7 @@ impl Context {
320324
/// assert_eq!(z_point.x().value(), z_point_gen.x().value());
321325
/// ```
322326
pub fn ecdh_z_gen(&mut self, key_handle: KeyHandle, in_point: EccPoint) -> Result<EccPoint> {
323-
let mut point = null_mut();
327+
let mut out_point_ptr = null_mut();
324328
let ret = unsafe {
325329
Esys_ECDH_ZGen(
326330
self.mut_context(),
@@ -329,13 +333,13 @@ impl Context {
329333
self.optional_session_2(),
330334
self.optional_session_3(),
331335
&in_point.into(),
332-
&mut point,
336+
&mut out_point_ptr,
333337
)
334338
};
335339
let ret = Error::from_tss_rc(ret);
336340

337341
if ret.is_success() {
338-
Ok(unsafe { (*point).point.try_into()? })
342+
Ok(unsafe { (*out_point_ptr).point.try_into()? })
339343
} else {
340344
error!("Error when performing ECDH ZGen: {}", ret);
341345
Err(ret)

tss-esapi/src/context/tpm_commands/attestation_commands.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
handles::{KeyHandle, ObjectHandle},
55
structures::{Attest, AttestBuffer, Data, PcrSelectionList, Signature, SignatureScheme},
6-
tss2_esys::*,
6+
tss2_esys::{Esys_Certify, Esys_Quote},
77
Context, Error, Result,
88
};
99
use log::error;
@@ -123,8 +123,8 @@ impl Context {
123123
qualifying_data: Data,
124124
signing_scheme: SignatureScheme,
125125
) -> Result<(Attest, Signature)> {
126-
let mut certify_info = null_mut();
127-
let mut signature = null_mut();
126+
let mut certify_info_ptr = null_mut();
127+
let mut signature_ptr = null_mut();
128128
let ret = unsafe {
129129
Esys_Certify(
130130
self.mut_context(),
@@ -135,15 +135,15 @@ impl Context {
135135
self.optional_session_3(),
136136
&qualifying_data.into(),
137137
&signing_scheme.into(),
138-
&mut certify_info,
139-
&mut signature,
138+
&mut certify_info_ptr,
139+
&mut signature_ptr,
140140
)
141141
};
142142
let ret = Error::from_tss_rc(ret);
143143

144144
if ret.is_success() {
145-
let certify_info = unsafe { MBox::<TPM2B_ATTEST>::from_raw(certify_info) };
146-
let signature = unsafe { MBox::from_raw(signature) };
145+
let certify_info = unsafe { MBox::from_raw(certify_info_ptr) };
146+
let signature = unsafe { MBox::from_raw(signature_ptr) };
147147
Ok((
148148
Attest::try_from(AttestBuffer::try_from(*certify_info)?)?,
149149
Signature::try_from(*signature)?,
@@ -167,8 +167,8 @@ impl Context {
167167
signing_scheme: SignatureScheme,
168168
pcr_selection_list: PcrSelectionList,
169169
) -> Result<(Attest, Signature)> {
170-
let mut quoted = null_mut();
171-
let mut signature = null_mut();
170+
let mut quoted_ptr = null_mut();
171+
let mut signature_ptr = null_mut();
172172
let ret = unsafe {
173173
Esys_Quote(
174174
self.mut_context(),
@@ -179,15 +179,15 @@ impl Context {
179179
&qualifying_data.into(),
180180
&signing_scheme.into(),
181181
&pcr_selection_list.into(),
182-
&mut quoted,
183-
&mut signature,
182+
&mut quoted_ptr,
183+
&mut signature_ptr,
184184
)
185185
};
186186
let ret = Error::from_tss_rc(ret);
187187

188188
if ret.is_success() {
189-
let quoted = unsafe { MBox::from_raw(quoted) };
190-
let signature = unsafe { MBox::from_raw(signature) };
189+
let quoted = unsafe { MBox::from_raw(quoted_ptr) };
190+
let signature = unsafe { MBox::from_raw(signature_ptr) };
191191
Ok((
192192
Attest::try_from(AttestBuffer::try_from(*quoted)?)?,
193193
Signature::try_from(*signature)?,

tss-esapi/src/context/tpm_commands/capability_commands.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
44
constants::CapabilityType,
5+
interface_types::YesNo,
56
structures::{CapabilityData, PublicParameters},
6-
tss2_esys::*,
7-
Context, Error, Result, WrapperErrorKind as ErrorKind,
7+
tss2_esys::{Esys_GetCapability, Esys_TestParms},
8+
Context, Error, Result,
89
};
910
use log::{error, warn};
1011
use mbox::MBox;
@@ -46,8 +47,8 @@ impl Context {
4647
property: u32,
4748
property_count: u32,
4849
) -> Result<(CapabilityData, bool)> {
49-
let mut outcapabilitydata = null_mut();
50-
let mut outmoredata: u8 = 0;
50+
let mut capability_data_ptr = null_mut();
51+
let mut more_data = YesNo::No.into();
5152

5253
let ret = unsafe {
5354
Esys_GetCapability(
@@ -58,23 +59,16 @@ impl Context {
5859
capability.into(),
5960
property,
6061
property_count,
61-
&mut outmoredata,
62-
&mut outcapabilitydata,
62+
&mut more_data,
63+
&mut capability_data_ptr,
6364
)
6465
};
65-
let moredata = if outmoredata == 0 {
66-
false
67-
} else if outmoredata == 1 {
68-
true
69-
} else {
70-
return Err(Error::WrapperError(ErrorKind::WrongValueFromTpm));
71-
};
7266
let ret = Error::from_tss_rc(ret);
7367

7468
if ret.is_success() {
75-
let capabilitydata = unsafe { MBox::from_raw(outcapabilitydata) };
76-
let capabilities = CapabilityData::try_from(*capabilitydata)?;
77-
Ok((capabilities, moredata))
69+
let capability_data = unsafe { MBox::from_raw(capability_data_ptr) };
70+
let capabilities = CapabilityData::try_from(*capability_data)?;
71+
Ok((capabilities, YesNo::try_from(more_data)?.into()))
7872
} else {
7973
error!("Error when getting capabilities: {}", ret);
8074
Err(ret)

0 commit comments

Comments
 (0)