Merge pull request from nsacyber/issue-240

[] Adds ability to get the DigestAlgorithm from an Event Log
This commit is contained in:
iadgovuser26 2020-03-27 11:00:17 -04:00 committed by GitHub
commit d7f075d70c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 18 deletions
HIRS_Utils/src

@ -10,7 +10,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Interface for handling different formats of TCG Event logs.
* Class for handling different formats of TCG Event logs.
*/
public class TCGEventLog {
@ -90,9 +90,15 @@ public class TCGEventLog {
this.hashType = hashType;
this.initValue = initValue;
ByteArrayInputStream is = new ByteArrayInputStream(rawlog);
// Process the 1st entry as a SHA1 format (per the spec)
eventList.add(new TpmPcrEvent1(is));
// put all events into an event list for further processing
while (is.available() > 0) {
eventList.add(new TpmPcrEvent1(is));
if (hashType.compareToIgnoreCase(HASH_STRING) == 0) {
eventList.add(new TpmPcrEvent1(is));
} else {
eventList.add(new TpmPcrEvent2(is));
}
}
calculatePcrValues();
}

@ -19,7 +19,7 @@ public class TCGEventLogProcessor {
/**
* Name of the hash algorithm used to process the Event Log, default is SHA256.
*/
private String algorithm = "SHA256";
private String algorithm = "TPM_ALG_SHA256";
/**
* Parsed event log array.
*/
@ -52,7 +52,7 @@ public class TCGEventLogProcessor {
TCGEventLog.HASH256_STRING, TCGEventLog.INIT_SHA256_LIST);
} else {
tcgLog = new TCGEventLog(rawLog);
algorithm = "SHA";
algorithm = "TPM_ALG_SHA1";
}
}
@ -75,6 +75,24 @@ public class TCGEventLogProcessor {
return tcgLog.getExpectedPCRValue(index);
}
/**
* 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
* used in the event log.
* @return TCG Defined Algorithm name
*/
public int getEventLogHashAlgorithmID() {
return TcgTpmtHa.tcgAlgStringtoId(algorithm);
}
/**
* Creates a TPM baseline using the expected PCR Values.
* Expected PCR Values were Calculated from the EventLog (RIM Support file).
@ -87,7 +105,7 @@ public class TCGEventLogProcessor {
TPMMeasurementRecord record;
String pcrValue;
for (int i = 0; i < TpmPcrEvent.PCR_COUNT; i++) {
if (algorithm.compareToIgnoreCase("SHA1") == 0) { // Log Was SHA1 Format
if (algorithm.compareToIgnoreCase("TPM_ALG_SHA1") == 0) { // Log Was SHA1 Format
pcrValue = tcgLog.getExpectedPCRValue(i);
byte[] hexValue = HexUtils.hexStringToByteArray(pcrValue);
final Digest hash = new Digest(DigestAlgorithm.SHA1, hexValue);

@ -34,43 +34,43 @@ public class TcgTpmtHa {
/**
* TCG ID for SHA1.
*/
private static final int TPM_ALG_SHA1 = 0x04;
public static final int TPM_ALG_SHA1 = 0x04;
/**
* TCG ID for SHA1.
*/
private static final int TPM_ALG_SHA256 = 0x0B;
public static final int TPM_ALG_SHA256 = 0x0B;
/**
* TCG ID for SHA 384.
*/
private static final int TPM_ALG_SHA384 = 0x0C;
public static final int TPM_ALG_SHA384 = 0x0C;
/**
* TCG ID for SHA512.
*/
private static final int TPM_ALG_SHA_512 = 0x0D;
public static final int TPM_ALG_SHA_512 = 0x0D;
/**
* TCG ID for Null algorithm.
*/
private static final int TPM_ALG_NULL = 0x10;
public static final int TPM_ALG_NULL = 0x10;
/**
* TCG ID for SHA1.
*/
private static final int TPM_ALG_SHA1_LENGTH = 20;
public static final int TPM_ALG_SHA1_LENGTH = 20;
/**
* TCG ID for SHA1.
*/
private static final int TPM_ALG_SHA256_LENGH = 32;
public static final int TPM_ALG_SHA256_LENGTH = 32;
/**
* TCG ID for SHA 384.
*/
private static final int TPM_ALG_SHA384_LENGTH = 48;
public static final int TPM_ALG_SHA384_LENGTH = 48;
/**
* TCG ID for SHA512.
*/
private static final int TPM_ALG_SHA512_LENGTH = 64;
public static final int TPM_ALG_SHA512_LENGTH = 64;
/**
* TCG ID for Null algorithm.
*/
private static final int TPM_ALG_NULL_LENGTH = 0;
public static final int TPM_ALG_NULL_LENGTH = 0;
/**
* Constructor.
@ -139,8 +139,9 @@ public class TcgTpmtHa {
* Only hash algorithms found in Table 7 are used.
*
* @param algid int to convert to string
* @return name of the algorithm
*/
private String tcgAlgIdtoString(final int algid) {
public static String tcgAlgIdtoString(final int algid) {
String alg;
switch (algid) {
case TPM_ALG_SHA1:
@ -164,6 +165,38 @@ public class TcgTpmtHa {
return alg;
}
/**
* Returns the TCG defined ID via a lookup o the TCG Defined Algorithm String.
* Lookup based upon section 6.3 for the TPM-Rev-2.0-Part-2-Structures.pdf document.
* Only hash algorithms found in Table 7 are used.
*
* @param algorithm String to convert to an id
* @return id of hash algorithm
*/
public static int tcgAlgStringtoId(final String algorithm) {
int alg;
switch (algorithm) {
case "TPM_ALG_SHA1":
alg = TPM_ALG_SHA1;
break;
case "TPM_ALG_SHA256":
alg = TPM_ALG_SHA256;
break;
case "TPM_ALG_SHA384":
alg = TPM_ALG_SHA384;
break;
case "TPM_ALG_SHA512":
alg = TPM_ALG_SHA_512;
break;
case "TPM_ALG_NULL":
alg = TPM_ALG_NULL;
break;
default:
alg = TPM_ALG_NULL;
}
return alg;
}
/**
* Sets the length of a given TPM ALG Identifier.
* (lookup based upon section 6.3 for the TPM-Rev-2.0-Part-2-Structures.pdf document)
@ -172,14 +205,14 @@ public class TcgTpmtHa {
* @param algId TCG defined Algorithm identifier
* @return length of hash data in bytes
*/
private int tcgAlgLength(final int algId) {
public static int tcgAlgLength(final int algId) {
int length;
switch (algId) {
case TPM_ALG_SHA1:
length = TPM_ALG_SHA1_LENGTH;
break;
case TPM_ALG_SHA256:
length = TPM_ALG_SHA256_LENGH;
length = TPM_ALG_SHA256_LENGTH;
break;
case TPM_ALG_SHA384:
length = TPM_ALG_SHA384_LENGTH;

@ -105,6 +105,11 @@ public class TCGEventLogProcessorTest extends SpringPersistenceTest {
// Test 2 get an individual PCR
String pcr3 = tlp.getExpectedPCRValue(3);
Assert.assertEquals(pcr3, pcrFromLog[3]);
// Test 3 check the Algorithm Identifiers used in the log
String algStr = tlp.getEventLogHashAlgorithm();
Assert.assertEquals(algStr, "TPM_ALG_SHA256");
int id = tlp.getEventLogHashAlgorithmID();
Assert.assertEquals(id, TcgTpmtHa.TPM_ALG_SHA256);
LOGGER.debug("OK. Parsing of a Crypto Agile Format Success");
}
@ -135,6 +140,11 @@ public class TCGEventLogProcessorTest extends SpringPersistenceTest {
// Test 2 get an individual PCR
String pcr0 = tlp.getExpectedPCRValue(0);
Assert.assertEquals(pcr0, pcrFromLog[0]);
// Test 3 check the Algorithm Identifiers used in the log
String algStr = tlp.getEventLogHashAlgorithm();
Assert.assertEquals(algStr, "TPM_ALG_SHA1");
int id = tlp.getEventLogHashAlgorithmID();
Assert.assertEquals(id, TcgTpmtHa.TPM_ALG_SHA1);
LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success");
}