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 new file mode 100644 index 00000000..c5b036ad --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java @@ -0,0 +1,146 @@ +package hirs.utils.tpm.eventlog; + + +import java.io.IOException; +import java.io.InputStream; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.util.Arrays; + +import org.apache.commons.io.IOUtils; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +//import static org.junit.jupiter.api.Assertions.*; + + +public class TCGEventLogTest { +//class TCGEventLogTest { + + private static final String DEFAULT_EVENT_LOG = "/tcgeventlog/TpmLog.bin"; + private static final String DEFAULT_EXPECTED_PCRS = "/tcgeventlog/TpmLogExpectedPcrs.txt"; + private static final String SHA1_EVENT_LOG = "/tcgeventlog/TpmLogSHA1.bin"; + private static final String SHA1_EXPECTED_PCRS = "/tcgeventlog/TpmLogSHA1ExpectedPcrs.txt"; + private static final Logger LOGGER + = LogManager.getLogger(TCGEventLogTest.class); + + /** + * Initializes a SessionFactory. The factory is used for an in-memory database that + * is used for testing. + */ + @BeforeAll + public static final void setup() { + LOGGER.debug("retrieving session factory"); + + } + + /** + * Closes the SessionFactory from setup. + */ + @AfterAll + public static final void tearDown() { + LOGGER.debug("closing session factory"); + } + + /** + * Tests the processing of a crypto agile event log. + * @throws IOException when processing the test fails + * @throws NoSuchAlgorithmException if an unknown algorithm is encountered. + * @throws CertificateException if a certificate fails to parse. + */ + @Test + public final void testCryptoAgileTCGEventLog() throws IOException, CertificateException, + NoSuchAlgorithmException { + LOGGER.debug("Testing the parsing of a Crypto Agile formatted TCG Event Log"); + InputStream log, pcrs; + boolean testPass = true; + log = this.getClass().getResourceAsStream(DEFAULT_EVENT_LOG); + byte[] rawLogBytes = IOUtils.toByteArray(log); + 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(); + String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); + + // Test 1 get all PCRs + for (int i = 0; i < 24; i++) { + if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { + testPass = false; + LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); + } + } + //Assert.assertTrue(testPass); + assertTrue(testPass); + + // Test 2 get an individual PCR + String pcr3 = evlog.getExpectedPCRValue(3); + //Assert.assertEquals(pcr3, pcrFromLog[3]); + assertThat(pcr3, equalTo(pcrFromLog[3])); + + // Test 3 check the Algorithm Identifiers used in the log + String algStr = evlog.getEventLogHashAlgorithm(); + //Assert.assertEquals(algStr, "TPM_ALG_SHA256"); + assertThat(algStr, equalTo("TPM_ALG_SHA256")); + + int id = evlog.getEventLogHashAlgorithmID(); + //Assert.assertEquals(id, TcgTpmtHa.TPM_ALG_SHA256); + assertThat(id, equalTo(TcgTpmtHa.TPM_ALG_SHA256)); + + LOGGER.debug("OK. Parsing of a Crypto Agile Format Success"); + } + + /** + * Tests the processing of a SHA1 formatted Event log. + * @throws IOException when processing the test fails + * @throws NoSuchAlgorithmException if an unknown algorithm is encountered. + * @throws CertificateException if a certificate fails to parse. + */ + @Test + public final void testSHA1TCGEventLog() throws IOException, CertificateException, + NoSuchAlgorithmException { + LOGGER.debug("Testing the parsing of a SHA1 formated TCG Event Log"); + InputStream log, pcrs; + boolean testPass = true; + log = this.getClass().getResourceAsStream(SHA1_EVENT_LOG); + byte[] rawLogBytes = IOUtils.toByteArray(log); + 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(); + String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); + + // Test 1 get all PCRs + for (int i = 0; i < 24; i++) { + if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { + testPass = false; + LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); + } + } + //Assert.assertTrue(testPass); + assertTrue(testPass); + + // Test 2 get an individual PCR + String pcr0 = evlog.getExpectedPCRValue(0); + //Assert.assertEquals(pcr0, pcrFromLog[0]); + assertThat(pcr0, equalTo(pcrFromLog[0])); + + // Test 3 check the Algorithm Identifiers used in the log + String algStr = evlog.getEventLogHashAlgorithm(); + //Assert.assertEquals(algStr, "TPM_ALG_SHA1"); + assertThat(algStr, equalTo("TPM_ALG_SHA1")); + + int id = evlog.getEventLogHashAlgorithmID(); + //Assert.assertEquals(id, TcgTpmtHa.TPM_ALG_SHA1); + assertThat(id, equalTo(TcgTpmtHa.TPM_ALG_SHA1)); + + LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success"); + } + +} diff --git a/HIRS_Utils/src/test/resources/tcgeventlog/TpmLog.bin b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLog.bin new file mode 100644 index 00000000..0b8f1f39 Binary files /dev/null and b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLog.bin differ diff --git a/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogExpectedPcrs.txt b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogExpectedPcrs.txt new file mode 100644 index 00000000..c7828a7c --- /dev/null +++ b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogExpectedPcrs.txt @@ -0,0 +1,24 @@ +5ef6c69a589a96b5ade6a09e960eb341e6f68a8239df66be34e5e991ddde97a8 +0f16d93fe0cbe7114fd9fefeb1d98a0802b184b6077f05275269aa90ebb8a993 +966eb0b055e5b656f81c08ed1b2107cdea5740f321382d07a0eade7d014addee +3d458cfe55cc03ea1f443f1562beec8df51c75e14a9fcf9a7234a13f198e7969 +c919e77702cb066016b575c008659ba7d758b0b4c3f9df29658e1770699823d1 +45f6dd68feb493ec2f371f2fbd2f904181a20e9491102304f239745f6fd1eaf6 +3d458cfe55cc03ea1f443f1562beec8df51c75e14a9fcf9a7234a13f198e7969 +65caf8dd1e0ea7a6347b635d2b379c93b9a1351edc2afc3ecda700e534eb3068 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000 +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +0000000000000000000000000000000000000000000000000000000000000000 diff --git a/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1.bin b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1.bin new file mode 100644 index 00000000..95b74c38 Binary files /dev/null and b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1.bin differ diff --git a/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1ExpectedPcrs.txt b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1ExpectedPcrs.txt new file mode 100644 index 00000000..a6676a67 --- /dev/null +++ b/HIRS_Utils/src/test/resources/tcgeventlog/TpmLogSHA1ExpectedPcrs.txt @@ -0,0 +1,24 @@ +1f1e9bf7dea0be1c37c999c4233b0164ed577607 +46f041010f19e5e74aa33e04467c59759af3fca4 +b2a83b0ebf2f8374299a5b2bdfc31ea955ad7236 +b2a83b0ebf2f8374299a5b2bdfc31ea955ad7236 +f36f2acdb5134d2560e7784002f606573bac99d5 +ed6db334e4e0f3811c18b9e79601b0c16d5a5b2b +b2a83b0ebf2f8374299a5b2bdfc31ea955ad7236 +54f675801f2f654bf53fc61c36837198fddd7a85 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +0000000000000000000000000000000000000000 +ffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffff +ffffffffffffffffffffffffffffffffffffffff +0000000000000000000000000000000000000000