From a0238abe8bfb356f8e8a6d70e6adebdaac4cb5ef Mon Sep 17 00:00:00 2001 From: iadgovuser58 <124906646+iadgovuser58@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:21:16 -0400 Subject: [PATCH] minor updates to replace deprecated function, swap actual/expected in assertthat, and use lombok Getter --- .../hirs/utils/tpm/eventlog/TCGEventLog.java | 21 ++++++------------- .../utils/tpm/eventlog/TCGEventLogTest.java | 16 +++++++------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/TCGEventLog.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/TCGEventLog.java index aa65b313..87405a5a 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/TCGEventLog.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/TCGEventLog.java @@ -29,7 +29,7 @@ public final class TCGEventLog { private static final Logger LOGGER = LogManager.getLogger(TCGEventLog.class); /** Name of the hash algorithm used to process the Event Log, default is SHA256. */ @Getter - private String algorithm = "TPM_ALG_SHA256"; + private String eventLogHashAlgorithm = "TPM_ALG_SHA256"; /** Parsed event log array. */ private static final int SIG_OFFSET = 32; /** TEV_NO_ACTION signature size. */ @@ -87,7 +87,7 @@ public final class TCGEventLog { initLocalityFourValue = LOCALITY4_SHA1_LIST; pcrLength = EvConstants.SHA1_LENGTH; hashType = HASH_STRING; - algorithm = "TPM_ALG_SHA1"; + eventLogHashAlgorithm = "TPM_ALG_SHA1"; initPcrList(); } @@ -121,14 +121,14 @@ public final class TCGEventLog { if (bCryptoAgile) { initValue = INIT_SHA256_LIST; initLocalityFourValue = LOCALITY4_SHA256_LIST; - algorithm = "TPM_ALG_SHA256"; + eventLogHashAlgorithm = "TPM_ALG_SHA256"; hashType = HASH256_STRING; pcrLength = EvConstants.SHA256_LENGTH; } else { initValue = INIT_SHA1_LIST; initLocalityFourValue = LOCALITY4_SHA1_LIST; hashType = HASH_STRING; - algorithm = "TPM_ALG_SHA1"; + eventLogHashAlgorithm = "TPM_ALG_SHA1"; pcrLength = EvConstants.SHA1_LENGTH; } this.pcrList = new byte[PCR_COUNT][pcrLength]; @@ -181,7 +181,7 @@ public final class TCGEventLog { // TPMMeasurementRecord record; // String pcrValue; // for (int i = 0; i < PCR_COUNT; i++) { -// if (algorithm.compareToIgnoreCase("TPM_ALG_SHA1") == 0) { // Log Was SHA1 Format +// if (eventLogHashAlgorithm.compareToIgnoreCase("TPM_ALG_SHA1") == 0) { // Log Was SHA1 Format // pcrValue = getExpectedPCRValue(i); // byte[] hexValue = HexUtils.hexStringToByteArray(pcrValue); // final Digest hash = new Digest(DigestAlgorithm.SHA1, hexValue); @@ -314,15 +314,6 @@ public final class TCGEventLog { return this.toString(); } - - /** - * Returns the TCG Algorithm Registry defined string for the Digest Algorithm - * used in the event log. - * @return TCG Defined Algorithm name - */ - public String getEventLogHashAlgorithm() { - return algorithm; - } /** * Returns the TCG Algorithm Registry defined ID for the Digest Algorithm @@ -330,7 +321,7 @@ public final class TCGEventLog { * @return TCG Defined Algorithm name */ public int getEventLogHashAlgorithmID() { - return TcgTpmtHa.tcgAlgStringToId(algorithm); + return TcgTpmtHa.tcgAlgStringToId(eventLogHashAlgorithm); } /** diff --git a/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java index a4725f8b..7fe0aefd 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java @@ -64,7 +64,7 @@ public class TCGEventLogTest { TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); String[] pcrFromLog = evlog.getExpectedPCRValues(); pcrs = this.getClass().getResourceAsStream(DEFAULT_EXPECTED_PCRS); - Object[] pcrObj = IOUtils.readLines(pcrs).toArray(); + Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); // Test 1 get all PCRs @@ -78,15 +78,15 @@ public class TCGEventLogTest { // Test 2 get an individual PCR String pcr3 = evlog.getExpectedPCRValue(3); - assertThat(pcr3, equalTo(pcrFromLog[3])); + assertThat(pcrFromLog[3], equalTo(pcr3)); // Test 3 check the Algorithm String Identifier used in the log String algStr = evlog.getEventLogHashAlgorithm(); - assertThat(algStr, equalTo("TPM_ALG_SHA256")); + assertThat("TPM_ALG_SHA256", equalTo(algStr)); // Test 4 check the Algorithm # Identifier used in the log int id = evlog.getEventLogHashAlgorithmID(); - assertThat(id, equalTo(TcgTpmtHa.TPM_ALG_SHA256)); + assertThat(TcgTpmtHa.TPM_ALG_SHA256, equalTo(id)); LOGGER.debug("OK. Parsing of a Crypto Agile Format Success"); } @@ -108,7 +108,7 @@ public class TCGEventLogTest { TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); String[] pcrFromLog = evlog.getExpectedPCRValues(); pcrs = this.getClass().getResourceAsStream(SHA1_EXPECTED_PCRS); - Object[] pcrObj = IOUtils.readLines(pcrs).toArray(); + Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); // Test 1 get all PCRs @@ -122,15 +122,15 @@ public class TCGEventLogTest { // Test 2 get an individual PCR String pcr0 = evlog.getExpectedPCRValue(0); - assertThat(pcr0, equalTo(pcrFromLog[0])); + assertThat(pcrFromLog[0], equalTo(pcr0)); // Test 3 check the Algorithm String Identifier used in the log String algStr = evlog.getEventLogHashAlgorithm(); - assertThat(algStr, equalTo("TPM_ALG_SHA1")); + assertThat("TPM_ALG_SHA1", equalTo(algStr)); // Test 4 check the Algorithm # Identifier used in the log int id = evlog.getEventLogHashAlgorithmID(); - assertThat(id, equalTo(TcgTpmtHa.TPM_ALG_SHA1)); + assertThat(TcgTpmtHa.TPM_ALG_SHA1, equalTo(id)); LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success"); }