Skip to content

Commit bfcca27

Browse files
authored
Merge pull request #402 from Superhepper/tpm-format-zero-display-impl
Cleans up TPM format zero error messages.
2 parents 5e245b8 + 0586c4d commit bfcca27

File tree

4 files changed

+191
-42
lines changed

4 files changed

+191
-42
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,28 @@ impl Context {
3333

3434
/// Get the TPM self test result
3535
///
36-
/// The returned buffer data is manufacturer-specific information.
36+
/// # Details
37+
/// The first parameter returned is a buffer with manufacturer-specific information.
38+
///
39+
/// The second parameter returned by the method is an indicator of how the
40+
/// test went in the form a [Result].
41+
///
42+
/// If testing of all functions is complete without functional failures then Ok(())
43+
/// or else a `TssError` (see [Error](crate::error::Error)) is returned.
44+
///
45+
/// - A [TpmFormatZeroWarningResponseCode](crate::error::TpmFormatZeroWarningResponseCode) with a `Testing`
46+
/// [TpmFormatZeroWarning](crate::constants::return_code::TpmFormatZeroWarning) indicates that the test
47+
/// are not complete.
48+
///
49+
/// - A [TpmFormatZeroErrorResponseCode](crate::error::TpmFormatZeroErrorResponseCode) with a `NeedsTest`
50+
/// [TpmFormatZeroError](crate::constants::return_code::TpmFormatZeroError) indicates that no self test
51+
/// has been performed and testable function has not been tested.
52+
///
53+
/// - A [TpmFormatZeroErrorResponseCode](crate::error::TpmFormatZeroErrorResponseCode) with a `Failure`
54+
/// [TpmFormatZeroError](crate::constants::return_code::TpmFormatZeroError) indicates that there was
55+
/// a failure.
56+
///
57+
/// See [Part 3, Commands](https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part3_Commands_pub.pdf).
3758
pub fn get_test_result(&mut self) -> Result<(MaxBuffer, Result<()>)> {
3859
let mut out_data_ptr = null_mut();
3960
let mut test_result: u32 = 0;

tss-esapi/src/error/return_code/tpm/format_zero/error.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ use std::convert::TryFrom;
55

66
/// Type representing the TPM format zero error
77
/// response code.
8+
///
9+
/// # Details
10+
/// The error messages are short forms of the descriptions given in the specification
11+
/// that describes return codes (see the
12+
/// [Part 2, Structures](https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part2_Structures_pub.pdf)).
13+
/// Sometimes these descriptions refers to fields within the structures described in the specification. When
14+
/// a message contains such a description then the name of the of the field is surrounded with backticks
15+
/// (e.g. `authValue`).
816
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
917
pub struct TpmFormatZeroErrorResponseCode {
1018
error_number: TpmFormatZeroError,
@@ -50,40 +58,40 @@ impl std::error::Error for TpmFormatZeroErrorResponseCode {}
5058
impl std::fmt::Display for TpmFormatZeroErrorResponseCode {
5159
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5260
match self.error_number {
53-
TpmFormatZeroError::Initialize => write!(f, "TPM not initialized by TPM2_Startup or already initialized"),
54-
TpmFormatZeroError::Failure => write!(f, "commands not being accepted because of a TPM failure. NOTE: This may be returned by TPM2_GetTestResult() as the testResultparameter"),
55-
TpmFormatZeroError::Sequence => write!(f, "improper use of a sequence handle"),
56-
TpmFormatZeroError::Private => write!(f, "not currently used"),
57-
TpmFormatZeroError::Hmac => write!(f, "not currently used"),
58-
TpmFormatZeroError::Disabled => write!(f, "the command is disabled"),
59-
TpmFormatZeroError::Exclusive => write!(f, "command failed because audit sequence required exclusivity"),
60-
TpmFormatZeroError::AuthType => write!(f, "authorization handle is not correct for command"),
61-
TpmFormatZeroError::AuthMissing => write!(f, "command requires an authorization session for handle and it is not present"),
62-
TpmFormatZeroError::Policy => write!(f, "policy failure in math operation or an invalid authPolicy value"),
63-
TpmFormatZeroError::Pcr => write!(f, "PCR check fail"),
64-
TpmFormatZeroError::PcrChanged => write!(f, "PCR have changed since checked"),
65-
TpmFormatZeroError::Upgrade => write!(f, "for all commands other than TPM2_FieldUpgradeData(), this code indicates that the TPM is in field upgrade mode; for TPM2_FieldUpgradeData(), this code indicates that the TPM is not in field upgrade mode"),
66-
TpmFormatZeroError::TooManyContexts => write!(f, "context ID counter is at maximum"),
67-
TpmFormatZeroError::AuthUnavailable => write!(f, "authValue or authPolicy is not available for selected entity"),
68-
TpmFormatZeroError::Reboot => write!(f, "a _TPM_Init and Startup(CLEAR) is required before the TPM can resume operation"),
69-
TpmFormatZeroError::Unbalanced => write!(f, "the protection algorithms (hash and symmetric) are not reasonably balanced. The digest size of the hash must be larger than the key size of the symmetric algorithm"),
70-
TpmFormatZeroError::CommandSize => write!(f, "command commandSizevalue is inconsistent with contents of the command buffer; either the size is not the same as the octets loaded by the hardware interface layer or the value is not large enough to hold a command header"),
71-
TpmFormatZeroError::CommandCode => write!(f, "command code not supported"),
72-
TpmFormatZeroError::AuthSize => write!(f, "the value of authorizationSizeis out of range or the number of octets in the Authorization Area is greater than required"),
73-
TpmFormatZeroError::AuthContext => write!(f, "use of an authorization session with a context command or another command that cannot have an authorization session"),
74-
TpmFormatZeroError::NvRange => write!(f, "NV offset+size is out of range"),
75-
TpmFormatZeroError::NvSize => write!(f, "Requested allocation size is larger than allowed"),
76-
TpmFormatZeroError::NvLocked => write!(f, "NV access locked"),
77-
TpmFormatZeroError::NvAuthorization => write!(f, "NV access authorization fails in command actions (this failure does not affect lockout.action)"),
78-
TpmFormatZeroError::NvUninitialized => write!(f, "an NV Index is used before being initialized or the state saved by TPM2_Shutdown(STATE) could not be restored"),
79-
TpmFormatZeroError::NvSpace => write!(f, "insufficient space for NV allocation"),
80-
TpmFormatZeroError::NvDefined => write!(f, "NV Index or persistent object already defined"),
81-
TpmFormatZeroError::BadContext => write!(f, "context in TPM2_ContextLoad() is not valid"),
82-
TpmFormatZeroError::CpHash => write!(f, "cpHash value already set or not correct for use"),
83-
TpmFormatZeroError::Parent => write!(f, "handle for parent is not a valid parent"),
84-
TpmFormatZeroError::NeedsTest => write!(f, "some function needs testing."),
85-
TpmFormatZeroError::NoResult => write!(f, "returned when an internal function cannot process a request due to an unspecified problem. This code is usually related to invalid parameters that are not properly filtered by the input unmarshaling code."),
86-
TpmFormatZeroError::Sensitive => write!(f, "the sensitive area did not unmarshal correctly after decryption - this code is used in lieu of the other unmarshaling errors so that an attacker cannot determine where the unmarshaling error occurred"),
61+
TpmFormatZeroError::Initialize => write!(f, "TPM not initialized by TPM2_Startup or already initialized."),
62+
TpmFormatZeroError::Failure => write!(f, "Commands not accepted because of a TPM failure."),
63+
TpmFormatZeroError::Sequence => write!(f, "Improper use of a sequence handle."),
64+
TpmFormatZeroError::Private => write!(f, "Not currently used."),
65+
TpmFormatZeroError::Hmac => write!(f, "Not currently used."),
66+
TpmFormatZeroError::Disabled => write!(f, "The command is disabled."),
67+
TpmFormatZeroError::Exclusive => write!(f, "Command failed because audit sequence required exclusivity."),
68+
TpmFormatZeroError::AuthType => write!(f, "Authorization handle is not correct for command."),
69+
TpmFormatZeroError::AuthMissing => write!(f, "Command requires an authorization session for handle and it is not present."),
70+
TpmFormatZeroError::Policy => write!(f, "Policy failure in math operation or an invalid `authPolicy` value."),
71+
TpmFormatZeroError::Pcr => write!(f, "PCR check fail."),
72+
TpmFormatZeroError::PcrChanged => write!(f, "PCR have changed since checked."),
73+
TpmFormatZeroError::Upgrade => write!(f, "For all commands other than TPM2_FieldUpgradeData(), this code indicates that the TPM is in field upgrade mode; for TPM2_FieldUpgradeData(), this code indicates that the TPM is not in field upgrade mode."),
74+
TpmFormatZeroError::TooManyContexts => write!(f, "Context ID counter is at maximum."),
75+
TpmFormatZeroError::AuthUnavailable => write!(f, "`authValue` or `authPolicy` is not available for selected entity."),
76+
TpmFormatZeroError::Reboot => write!(f, "A _TPM_Init and Startup(CLEAR) is required before the TPM can resume operation."),
77+
TpmFormatZeroError::Unbalanced => write!(f, "The protection algorithms (hash and symmetric) are not reasonably balanced. The digest size of the hash must be larger than the key size of the symmetric algorithm."),
78+
TpmFormatZeroError::CommandSize => write!(f, "Command `commandSize` value is inconsistent with contents of the command buffer; either the size is not the same as the octets loaded by the hardware interface layer or the value is not large enough to hold a command header."),
79+
TpmFormatZeroError::CommandCode => write!(f, "Command code not supported."),
80+
TpmFormatZeroError::AuthSize => write!(f, "The value of `authorizationSize` is out of range or the number of octets in the authorization area is greater than required."),
81+
TpmFormatZeroError::AuthContext => write!(f, "Use of an authorization session with a context command or another command that cannot have an authorization session."),
82+
TpmFormatZeroError::NvRange => write!(f, "NV offset+size is out of range."),
83+
TpmFormatZeroError::NvSize => write!(f, "Requested allocation size is larger than allowed."),
84+
TpmFormatZeroError::NvLocked => write!(f, "NV access locked."),
85+
TpmFormatZeroError::NvAuthorization => write!(f, "NV access authorization fails in command actions."),
86+
TpmFormatZeroError::NvUninitialized => write!(f, "An NV Index is used before being initialized or the state saved by TPM2_Shutdown(STATE) could not be restored."),
87+
TpmFormatZeroError::NvSpace => write!(f, "Insufficient space for NV allocation."),
88+
TpmFormatZeroError::NvDefined => write!(f, "NV Index or persistent object already defined."),
89+
TpmFormatZeroError::BadContext => write!(f, "Context in TPM2_ContextLoad() is not valid."),
90+
TpmFormatZeroError::CpHash => write!(f, "`cpHash` value already set or not correct for use."),
91+
TpmFormatZeroError::Parent => write!(f, "Handle for parent is not a valid parent."),
92+
TpmFormatZeroError::NeedsTest => write!(f, "Function needs testing."),
93+
TpmFormatZeroError::NoResult => write!(f, "Function cannot process a request due to an unspecified problem. This code is usually related to invalid parameters that are not properly filtered by the input unmarshaling code."),
94+
TpmFormatZeroError::Sensitive => write!(f, "The sensitive area did not unmarshal correctly after decryption."),
8795
}
8896
}
8997
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_error_tests.rs

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ macro_rules! test_valid_conversion {
3232
expected_tpm_format_zero_error_rc,
3333
TpmFormatZeroErrorResponseCode::try_from(($tpm_rc - TPM2_RC_VER1) as u8).expect(
3434
&format!(
35-
"{} did not convert into a TpmFormatZeroErrorResponseCode",
36-
std::stringify!($tpm_rc - TPM2_RC_VER1)
35+
"{} did not convert into a {}",
36+
std::stringify!($tpm_rc - TPM2_RC_VER1),
37+
std::any::type_name::<TpmFormatZeroErrorResponseCode>(),
3738
)
3839
),
39-
"{} did not convert into the expected TpmFormatZeroErrorResponseCode",
40+
"{} did not convert into the expected {}",
4041
std::stringify!($tpm_rc - TPM2_RC_VER1),
42+
std::any::type_name::<TpmFormatZeroErrorResponseCode>()
4143
);
4244

4345
let actual_rc = ReturnCode::try_from(expected_tss_rc)
@@ -50,8 +52,9 @@ macro_rules! test_valid_conversion {
5052
assert_eq!(
5153
expected_tpm_format_zero_error_rc,
5254
actual_tpm_format_zero_error_rc,
53-
"{} in the TPM layer did not convert into the expected TpmFormatZeroResponseCode",
54-
std::stringify!($tpm_rc)
55+
"{} in the TPM layer did not convert into the expected {}",
56+
std::stringify!($tpm_rc),
57+
std::any::type_name::<TpmFormatZeroResponseCode>(),
5558
);
5659
} else {
5760
panic!("TPM TSS2_RC layer did no convert into ReturnCode::Tpm");
@@ -60,13 +63,29 @@ macro_rules! test_valid_conversion {
6063
assert_eq!(
6164
expected_tss_rc,
6265
actual_rc.into(),
63-
"TpmFormatZeroResponseCode with {} did not convert into expected {} TSS2_RC in the TPM layer.",
66+
"{} with {} did not convert into expected {} TSS2_RC in the TPM layer.",
67+
std::any::type_name::<TpmFormatZeroResponseCode>(),
6468
std::stringify!(TpmFormatZeroError::$item),
6569
std::stringify!($tpm_rc),
6670
);
6771
};
6872
}
6973

74+
macro_rules! test_display_trait_impl {
75+
($expected_error_message:tt, TpmFormatZeroError::$zero_error:ident) => {
76+
assert_eq!(
77+
format!(
78+
"{}",
79+
TpmFormatZeroErrorResponseCode::from(TpmFormatZeroError::$zero_error)
80+
),
81+
$expected_error_message,
82+
"{} with {} did not produce the expected error message",
83+
std::any::type_name::<TpmFormatZeroErrorResponseCode>(),
84+
std::stringify!(TpmFormatZeroError::$zero_error),
85+
);
86+
};
87+
}
88+
7089
#[test]
7190
fn test_valid_conversions() {
7291
test_valid_conversion!(TPM2_RC_INITIALIZE, TpmFormatZeroError::Initialize);
@@ -125,3 +144,104 @@ fn test_invalid_conversions() {
125144
"Converting invalid TPM layer response code did not produce the expected error"
126145
);
127146
}
147+
148+
#[test]
149+
fn test_display_implementation() {
150+
test_display_trait_impl!(
151+
"TPM not initialized by TPM2_Startup or already initialized.",
152+
TpmFormatZeroError::Initialize
153+
);
154+
test_display_trait_impl!(
155+
"Commands not accepted because of a TPM failure.",
156+
TpmFormatZeroError::Failure
157+
);
158+
test_display_trait_impl!(
159+
"Improper use of a sequence handle.",
160+
TpmFormatZeroError::Sequence
161+
);
162+
test_display_trait_impl!("Not currently used.", TpmFormatZeroError::Private);
163+
test_display_trait_impl!("Not currently used.", TpmFormatZeroError::Hmac);
164+
test_display_trait_impl!("The command is disabled.", TpmFormatZeroError::Disabled);
165+
test_display_trait_impl!(
166+
"Command failed because audit sequence required exclusivity.",
167+
TpmFormatZeroError::Exclusive
168+
);
169+
test_display_trait_impl!(
170+
"Authorization handle is not correct for command.",
171+
TpmFormatZeroError::AuthType
172+
);
173+
test_display_trait_impl!(
174+
"Command requires an authorization session for handle and it is not present.",
175+
TpmFormatZeroError::AuthMissing
176+
);
177+
test_display_trait_impl!(
178+
"Policy failure in math operation or an invalid `authPolicy` value.",
179+
TpmFormatZeroError::Policy
180+
);
181+
test_display_trait_impl!("PCR check fail.", TpmFormatZeroError::Pcr);
182+
test_display_trait_impl!(
183+
"PCR have changed since checked.",
184+
TpmFormatZeroError::PcrChanged
185+
);
186+
test_display_trait_impl!("For all commands other than TPM2_FieldUpgradeData(), this code indicates that the TPM is in field upgrade mode; for TPM2_FieldUpgradeData(), this code indicates that the TPM is not in field upgrade mode.", TpmFormatZeroError::Upgrade);
187+
test_display_trait_impl!(
188+
"Context ID counter is at maximum.",
189+
TpmFormatZeroError::TooManyContexts
190+
);
191+
test_display_trait_impl!(
192+
"`authValue` or `authPolicy` is not available for selected entity.",
193+
TpmFormatZeroError::AuthUnavailable
194+
);
195+
test_display_trait_impl!(
196+
"A _TPM_Init and Startup(CLEAR) is required before the TPM can resume operation.",
197+
TpmFormatZeroError::Reboot
198+
);
199+
test_display_trait_impl!("The protection algorithms (hash and symmetric) are not reasonably balanced. The digest size of the hash must be larger than the key size of the symmetric algorithm.", TpmFormatZeroError::Unbalanced);
200+
test_display_trait_impl!("Command `commandSize` value is inconsistent with contents of the command buffer; either the size is not the same as the octets loaded by the hardware interface layer or the value is not large enough to hold a command header.", TpmFormatZeroError::CommandSize);
201+
test_display_trait_impl!(
202+
"Command code not supported.",
203+
TpmFormatZeroError::CommandCode
204+
);
205+
test_display_trait_impl!("The value of `authorizationSize` is out of range or the number of octets in the authorization area is greater than required.", TpmFormatZeroError::AuthSize);
206+
test_display_trait_impl!("Use of an authorization session with a context command or another command that cannot have an authorization session.", TpmFormatZeroError::AuthContext);
207+
test_display_trait_impl!(
208+
"NV offset+size is out of range.",
209+
TpmFormatZeroError::NvRange
210+
);
211+
test_display_trait_impl!(
212+
"Requested allocation size is larger than allowed.",
213+
TpmFormatZeroError::NvSize
214+
);
215+
test_display_trait_impl!("NV access locked.", TpmFormatZeroError::NvLocked);
216+
test_display_trait_impl!(
217+
"NV access authorization fails in command actions.",
218+
TpmFormatZeroError::NvAuthorization
219+
);
220+
test_display_trait_impl!("An NV Index is used before being initialized or the state saved by TPM2_Shutdown(STATE) could not be restored.", TpmFormatZeroError::NvUninitialized);
221+
test_display_trait_impl!(
222+
"Insufficient space for NV allocation.",
223+
TpmFormatZeroError::NvSpace
224+
);
225+
test_display_trait_impl!(
226+
"NV Index or persistent object already defined.",
227+
TpmFormatZeroError::NvDefined
228+
);
229+
test_display_trait_impl!(
230+
"Context in TPM2_ContextLoad() is not valid.",
231+
TpmFormatZeroError::BadContext
232+
);
233+
test_display_trait_impl!(
234+
"`cpHash` value already set or not correct for use.",
235+
TpmFormatZeroError::CpHash
236+
);
237+
test_display_trait_impl!(
238+
"Handle for parent is not a valid parent.",
239+
TpmFormatZeroError::Parent
240+
);
241+
test_display_trait_impl!("Function needs testing.", TpmFormatZeroError::NeedsTest);
242+
test_display_trait_impl!("Function cannot process a request due to an unspecified problem. This code is usually related to invalid parameters that are not properly filtered by the input unmarshaling code.", TpmFormatZeroError::NoResult);
243+
test_display_trait_impl!(
244+
"The sensitive area did not unmarshal correctly after decryption.",
245+
TpmFormatZeroError::Sensitive
246+
);
247+
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_warning_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! test_display_trait_impl {
7474
TpmFormatZeroWarningResponseCode::from(TpmFormatZeroWarning::$zero_warning)
7575
),
7676
$expected_error_message,
77-
"BaseReturnCode with {} did not produce the expected error message",
77+
"TpmFormatZeroWarningResponseCode with {} did not produce the expected error message",
7878
std::stringify!(TpmFormatZeroWarning::$zero_warning),
7979
);
8080
};

0 commit comments

Comments
 (0)