Skip to content

Commit 9c7eb46

Browse files
committed
Fixes problem with sign requiring a HashcheckTicket.
- This fixes #475 by making the HashcheckTicket that was previously required in the ```sign``` context method optional instead. If it is ```None``` it is then internally converted into the HashcheckTicket version of the `Null ticket` before being converted to the corresponding TSS type. This has the benefit of removing the need to use the TSS type in order to create a `Null ticket`. Signed-off-by: Jesper Brynolf <jesper.brynolf@gmail.com>
1 parent 2dfc315 commit 9c7eb46

File tree

5 files changed

+30
-66
lines changed

5 files changed

+30
-66
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! client.
99
use crate::{
1010
attributes::{ObjectAttributesBuilder, SessionAttributesBuilder},
11-
constants::{tss::*, SessionType, TpmFormatZeroError},
11+
constants::{SessionType, TpmFormatZeroError},
1212
error::{TpmFormatZeroResponseCode, TpmResponseCode},
1313
handles::{KeyHandle, SessionHandle},
1414
interface_types::{
@@ -23,7 +23,6 @@ use crate::{
2323
RsaScheme, Signature, SignatureScheme, SymmetricDefinitionObject, VerifiedTicket,
2424
},
2525
tcti_ldr::TctiNameConf,
26-
tss2_esys::*,
2726
utils::{create_restricted_decryption_rsa_public, PublicKey, TpmsContext},
2827
Context, Error, Result, ReturnCode, WrapperErrorKind as ErrorKind,
2928
};
@@ -287,20 +286,10 @@ impl TransientKeyContext {
287286
) -> Result<Signature> {
288287
let key_handle = self.load_key(key_params, key_material, key_auth)?;
289288

290-
let validation = TPMT_TK_HASHCHECK {
291-
tag: TPM2_ST_HASHCHECK,
292-
hierarchy: TPM2_RH_NULL,
293-
digest: Default::default(),
294-
};
295289
self.set_session_attrs()?;
296290
let signature = self
297291
.context
298-
.sign(
299-
key_handle,
300-
digest,
301-
SignatureScheme::Null,
302-
validation.try_into()?,
303-
)
292+
.sign(key_handle, digest, SignatureScheme::Null, None)
304293
.or_else(|e| {
305294
self.context.flush_context(key_handle.into())?;
306295
Err(e)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ impl Context {
4545
key_handle: KeyHandle,
4646
digest: Digest,
4747
scheme: SignatureScheme,
48-
validation: HashcheckTicket,
48+
validation: Option<HashcheckTicket>,
4949
) -> Result<Signature> {
5050
let mut signature_ptr = null_mut();
51+
let validation_ticket = validation.unwrap_or_default().try_into()?;
5152
ReturnCode::ensure_success(
5253
unsafe {
5354
Esys_Sign(
@@ -58,7 +59,7 @@ impl Context {
5859
self.optional_session_3(),
5960
&digest.into(),
6061
&scheme.into(),
61-
&validation.try_into()?,
62+
&validation_ticket,
6263
&mut signature_ptr,
6364
)
6465
},

tss-esapi/src/structures/tickets.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use crate::{
1111
};
1212

1313
use log::error;
14-
use std::convert::{TryFrom, TryInto};
14+
use std::{
15+
convert::{TryFrom, TryInto},
16+
default::Default,
17+
};
1518

1619
/// Macro used for implementing try_from
1720
/// TssTicketType -> TicketType
@@ -124,6 +127,17 @@ pub struct HashcheckTicket {
124127
digest: Vec<u8>,
125128
}
126129

130+
impl Default for HashcheckTicket {
131+
/// The default for the Hashcheck ticket is the Null ticket.
132+
fn default() -> Self {
133+
return Self {
134+
tag: StructureTag::Hashcheck,
135+
hierarchy: Hierarchy::Null,
136+
digest: Vec::<u8>::new(),
137+
};
138+
}
139+
}
140+
127141
impl Ticket for HashcheckTicket {
128142
/// The tag of the verified ticket.
129143
const POSSIBLE_TAGS: &'static [StructureTag] = &[StructureTag::Hashcheck];

tss-esapi/tests/integration_tests/context_tests/tpm_commands/enhanced_authorization_ea_commands_tests.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,11 @@ mod test_policy_name_hash {
530530

531531
mod test_policy_authorize {
532532
use crate::common::{create_ctx_with_session, get_pcr_policy_digest, signing_key_pub};
533-
use std::convert::{TryFrom, TryInto};
533+
use std::convert::TryFrom;
534534
use tss_esapi::{
535-
constants::tss::{TPM2_RH_NULL, TPM2_ST_HASHCHECK},
536535
interface_types::{algorithm::HashingAlgorithm, resource_handles::Hierarchy},
537536
structures::{Auth, MaxBuffer, Nonce, SignatureScheme},
538-
tss2_esys::{TPM2B_NONCE, TPMT_TK_HASHCHECK},
537+
tss2_esys::TPM2B_NONCE,
539538
};
540539
#[test]
541540
fn test_policy_authorize() {
@@ -569,19 +568,9 @@ mod test_policy_authorize {
569568
.unwrap()
570569
.0;
571570

572-
let validation = TPMT_TK_HASHCHECK {
573-
tag: TPM2_ST_HASHCHECK,
574-
hierarchy: TPM2_RH_NULL,
575-
digest: Default::default(),
576-
};
577571
// A signature over just the policy_digest, since the policy_ref is empty
578572
let signature = context
579-
.sign(
580-
key_handle,
581-
ahash.clone(),
582-
SignatureScheme::Null,
583-
validation.try_into().unwrap(),
584-
)
573+
.sign(key_handle, ahash.clone(), SignatureScheme::Null, None)
585574
.unwrap();
586575
let tkt = context
587576
.verify_signature(key_handle, ahash, signature)

tss-esapi/tests/integration_tests/context_tests/tpm_commands/signing_and_signature_verification_tests.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33
mod test_verify_signature {
44
use crate::common::{create_ctx_with_session, signing_key_pub, HASH};
5-
use std::convert::{TryFrom, TryInto};
5+
use std::convert::TryFrom;
66
use tss_esapi::{
7-
constants::tss::{TPM2_RH_NULL, TPM2_ST_HASHCHECK},
87
interface_types::{algorithm::HashingAlgorithm, resource_handles::Hierarchy},
98
structures::{Auth, Digest, PublicKeyRsa, RsaSignature, Signature, SignatureScheme},
10-
tss2_esys::TPMT_TK_HASHCHECK,
119
};
1210

1311
#[test]
@@ -28,17 +26,12 @@ mod test_verify_signature {
2826
.unwrap()
2927
.key_handle;
3028

31-
let validation = TPMT_TK_HASHCHECK {
32-
tag: TPM2_ST_HASHCHECK,
33-
hierarchy: TPM2_RH_NULL,
34-
digest: Default::default(),
35-
};
3629
let signature = context
3730
.sign(
3831
key_handle,
3932
Digest::try_from(HASH[..32].to_vec()).unwrap(),
4033
SignatureScheme::Null,
41-
validation.try_into().unwrap(),
34+
None,
4235
)
4336
.unwrap();
4437

@@ -69,17 +62,12 @@ mod test_verify_signature {
6962
.unwrap()
7063
.key_handle;
7164

72-
let validation = TPMT_TK_HASHCHECK {
73-
tag: TPM2_ST_HASHCHECK,
74-
hierarchy: TPM2_RH_NULL,
75-
digest: Default::default(),
76-
};
7765
let mut signature = context
7866
.sign(
7967
key_handle,
8068
Digest::try_from(HASH[..32].to_vec()).unwrap(),
8169
SignatureScheme::Null,
82-
validation.try_into().unwrap(),
70+
None,
8371
)
8472
.unwrap();
8573

@@ -176,12 +164,10 @@ mod test_verify_signature {
176164

177165
mod test_sign {
178166
use crate::common::{create_ctx_with_session, signing_key_pub, HASH};
179-
use std::convert::{TryFrom, TryInto};
167+
use std::convert::TryFrom;
180168
use tss_esapi::{
181-
constants::tss::{TPM2_RH_NULL, TPM2_ST_HASHCHECK},
182169
interface_types::resource_handles::Hierarchy,
183170
structures::{Auth, Digest, SignatureScheme},
184-
tss2_esys::TPMT_TK_HASHCHECK,
185171
};
186172

187173
#[test]
@@ -202,17 +188,12 @@ mod test_sign {
202188
.unwrap()
203189
.key_handle;
204190

205-
let validation = TPMT_TK_HASHCHECK {
206-
tag: TPM2_ST_HASHCHECK,
207-
hierarchy: TPM2_RH_NULL,
208-
digest: Default::default(),
209-
};
210191
context
211192
.sign(
212193
key_handle,
213194
Digest::try_from(HASH[..32].to_vec()).unwrap(),
214195
SignatureScheme::Null,
215-
validation.try_into().unwrap(),
196+
None,
216197
)
217198
.unwrap();
218199
}
@@ -235,17 +216,12 @@ mod test_sign {
235216
.unwrap()
236217
.key_handle;
237218

238-
let validation = TPMT_TK_HASHCHECK {
239-
tag: TPM2_ST_HASHCHECK,
240-
hierarchy: TPM2_RH_NULL,
241-
digest: Default::default(),
242-
};
243219
context
244220
.sign(
245221
key_handle,
246222
Digest::try_from(Vec::<u8>::new()).unwrap(),
247223
SignatureScheme::Null,
248-
validation.try_into().unwrap(),
224+
None,
249225
)
250226
.unwrap_err();
251227
}
@@ -268,17 +244,12 @@ mod test_sign {
268244
.unwrap()
269245
.key_handle;
270246

271-
let validation = TPMT_TK_HASHCHECK {
272-
tag: TPM2_ST_HASHCHECK,
273-
hierarchy: TPM2_RH_NULL,
274-
digest: Default::default(),
275-
};
276247
context
277248
.sign(
278249
key_handle,
279250
Digest::try_from([0xbb; 40].to_vec()).unwrap(),
280251
SignatureScheme::Null,
281-
validation.try_into().unwrap(),
252+
None,
282253
)
283254
.unwrap_err();
284255
}

0 commit comments

Comments
 (0)