Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add SPDM Events Part 3 #795

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,7 @@ public String getEventContentStr() {
break;
case EvConstants.EV_EFI_SPDM_FIRMWARE_BLOB:
case EvConstants.EV_EFI_SPDM_FIRMWARE_CONFIG:
try {
sb.append(new EvEfiSpdmDeviceSecurityEvent(eventContent).toString());
} catch (UnsupportedEncodingException ueEx) {
log.error(ueEx);
sb.append(ueEx.toString());
}
sb.append(new EvEfiSpdmDeviceSecurityEvent(eventContent).toString());
break;
default:
sb.append("Unknown Event found\n");
Expand Down Expand Up @@ -560,7 +555,9 @@ public String processEvent(final byte[] eventData, final byte[] content,
case EvConstants.EV_EFI_HCRTM_EVENT:
break;
case EvConstants.EV_EFI_SPDM_FIRMWARE_BLOB:
description += "Event Content:\n" + new EvEfiSpdmDeviceSecurityEvent(content).toString();
EvEfiSpdmDeviceSecurityEvent tempp = new EvEfiSpdmDeviceSecurityEvent(content);
description += "Event Content:\n" + tempp.toString();
// description += "Event Content:\n" + new EvEfiSpdmDeviceSecurityEvent(content).toString();
break;
case EvConstants.EV_EFI_SPDM_FIRMWARE_CONFIG:
description += "Event Content:\n" + new EvEfiSpdmDeviceSecurityEvent(content).toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package hirs.utils.tpm.eventlog.events;

import lombok.Getter;
import lombok.Setter;

import static hirs.utils.tpm.eventlog.events.DeviceSecurityEventHeader.DEVICE_TYPE_NONE;
import static hirs.utils.tpm.eventlog.events.DeviceSecurityEventHeader.DEVICE_TYPE_PCI;
import static hirs.utils.tpm.eventlog.events.DeviceSecurityEventHeader.DEVICE_TYPE_USB;


/**
Expand Down Expand Up @@ -51,7 +56,20 @@
public abstract class DeviceSecurityEvent {

/**
* Human readable description of the data within the
* DeviceSecurityEventDataContext Object.
*/
@Getter
private DeviceSecurityEventDataDeviceContext dsedDevContext = null;

/**
* Device type.
*/
@Getter
@Setter
private int deviceType = -1;

/**
* Human-readable description of the data within the
* DEVICE_SECURITY_EVENT_DATA_DEVICE_CONTEXT. DEVICE can be either PCI or USB.
*/
@Getter
Expand All @@ -68,34 +86,25 @@ public DeviceSecurityEvent() {
/**
* Parse the Device Context structure, can be PCI or USB based on device type field.
*
* @param dSEDbytes byte array holding the DeviceSecurityEventData.
* @param startByte starting byte of the device structure (depends on length of header).
* @param deviceType device type either PCI or USB.
* @param dsedDeviceContextBytes byte array holding the DeviceSecurityEventData.
*
*/
public void parseDeviceContext(final byte[] dSEDbytes, int startByte, int deviceType) {
public void instantiateDeviceContext(final byte[] dsedDeviceContextBytes) {

int deviceContextLength = dSEDbytes.length - startByte;

// get the device context bytes
byte[] deviceContextBytes = new byte[deviceContextLength];
System.arraycopy(dSEDbytes, startByte, deviceContextBytes, 0,
deviceContextLength);

if (deviceType == 0) {
deviceContextInfo = "No Device Context (indicated by device type value of 0";
if (deviceType == DEVICE_TYPE_NONE) {
deviceContextInfo = "\n No Device Context (indicated by device type value of 0";
}
else if (deviceType == DEVICE_TYPE_PCI) {
dsedDevContext
= new DeviceSecurityEventDataPciContext(dsedDeviceContextBytes);
deviceContextInfo = dsedDevContext.toString();
}
else if (deviceType == 1) {
DeviceSecurityEventDataPciContext dSEDpciContext
= new DeviceSecurityEventDataPciContext(deviceContextBytes);
deviceContextInfo = dSEDpciContext.toString();
else if (deviceType == DEVICE_TYPE_USB) {
// dsedDevContext
// = new DeviceSecurityEventDataUsbContext(dsedDeviceContextBytes);
// deviceContextInfo = dsedDevContext.toString();
deviceContextInfo = " Device Type: USB - To be implemented";
}
//else if (deviceType == 2) {
//DeviceSecurityEventDataUsbContext dSEDusbContext
// = new DeviceSecurityEventDataUsbContext(deviceContextBytes);
//deviceContextInfo = dSEDusbContext.toString();
//deviceContextInfo = "Device type is USB - to be implemented in future";
//}
else {
deviceContextInfo = " Unknown device type; cannot process device context";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hirs.utils.tpm.eventlog.events;

import lombok.Getter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
Expand All @@ -24,15 +26,23 @@ public class DeviceSecurityEventData extends DeviceSecurityEvent {
/**
* DeviceSecurityEventData Constructor.
*
* @param dSEDbytes byte array holding the DeviceSecurityEventData.
* @param dsedBytes byte array holding the DeviceSecurityEventData.
*/
public DeviceSecurityEventData(final byte[] dSEDbytes) throws UnsupportedEncodingException {
dsedHeader = new DeviceSecurityEventDataHeader(dSEDbytes);
parseDeviceContext(dSEDbytes, dsedHeader.getDSEDheaderByteSize(), dsedHeader.getDeviceType());
public DeviceSecurityEventData(final byte[] dsedBytes) {
dsedHeader = new DeviceSecurityEventDataHeader(dsedBytes);
setDeviceType(dsedHeader.getDeviceType());
int dsedHeaderLength = dsedHeader.getDsedHeaderLength();

int dsedDevContextLength = dsedBytes.length - dsedHeaderLength;
byte[] dsedDevContextBytes = new byte[dsedDevContextLength];
System.arraycopy(dsedBytes, dsedHeaderLength, dsedDevContextBytes, 0,
dsedDevContextLength);

instantiateDeviceContext(dsedDevContextBytes);
}

/**
* Returns a human readable description of the data within this structure.
* Returns a human-readable description of the data within this structure.
*
* @return a description of this structure.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import lombok.Getter;

// TODO Placeholder class to be implemented upon getting test pattern
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static hirs.utils.tpm.eventlog.events.DeviceSecurityEventDataHeader2.SUBHEADERTYPE_CERT_CHAIN;
import static hirs.utils.tpm.eventlog.events.DeviceSecurityEventDataHeader2.SUBHEADERTYPE_MEAS_BLOCK;

/**
* Class to process DEVICE_SECURITY_EVENT_DATA2.
* Parses event data per PFP v1.06 Rev52 Table 26.
Expand All @@ -22,25 +27,70 @@ public class DeviceSecurityEventData2 extends DeviceSecurityEvent {
@Getter
private DeviceSecurityEventDataHeader2 dsedHeader2 = null;

/**
* DeviceSecurityEventDataSubHeader Object.
*/
@Getter
private DeviceSecurityEventDataSubHeader dsedSubHeader = null;

/**
* Human readable description of the data within the
* DEVICE_SECURITY_EVENT_DATA_SUB_HEADER. SUB_HEADER can be either
* DEVICE_SECURITY_EVENT_DATA_SUB_HEADER_SPDM_MEASUREMENT_BLOCK or
* DEVICE_SECURITY_EVENT_DATA_SUB_HEADER_SPDM_CERT_CHAIN
*/
@Getter
String subHeaderInfo = "";

/**
* DeviceSecurityEventData2 Constructor.
*
* @param dSEDbytes byte array holding the DeviceSecurityEventData2.
* @param dsedBytes byte array holding the DeviceSecurityEventData2.
*/
public DeviceSecurityEventData2(final byte[] dSEDbytes) {
public DeviceSecurityEventData2(final byte[] dsedBytes) {

dsedHeader2 = new DeviceSecurityEventDataHeader2(dsedBytes);
setDeviceType(dsedHeader2.getDeviceType());
int dsedHeaderLength = dsedHeader2.getDsedHeaderLength();
int subHeaderType = dsedHeader2.getSubHeaderType();
int subHeaderLength = dsedHeader2.getSubHeaderLength();

subHeaderInfo = "\nSub header type: " + subHeaderType;

byte[] dsedSubHeaderBytes = new byte[subHeaderLength];
System.arraycopy(dsedBytes, dsedHeaderLength, dsedSubHeaderBytes, 0, subHeaderLength);

if (subHeaderType == SUBHEADERTYPE_MEAS_BLOCK) {
dsedSubHeader = new DeviceSecurityEventDataSubHeaderSpdmMeasurementBlock(dsedSubHeaderBytes);
subHeaderInfo += dsedSubHeader.toString();
}
else if (subHeaderType == SUBHEADERTYPE_CERT_CHAIN) {
// dsedSubHeader = new DeviceSecurityEventDataSubHeaderCertChain();
subHeaderInfo += " Cert chain to be implemented ";
}
else {
subHeaderInfo += "Sub header type unknown";
}

int dsedDevContextStartByte = dsedHeaderLength + subHeaderLength;
int dsedDevContextLength = dsedBytes.length - dsedDevContextStartByte;
byte[] dsedDevContextBytes = new byte[dsedDevContextLength];
System.arraycopy(dsedBytes, dsedDevContextStartByte, dsedDevContextBytes, 0,
dsedDevContextLength);

dsedHeader2 = new DeviceSecurityEventDataHeader2(dSEDbytes);
// get subheader
parseDeviceContext(dSEDbytes, dsedHeader2.getDSEDheaderByteSize(), dsedHeader2.getDeviceType());
instantiateDeviceContext(dsedDevContextBytes);
}

/**
* Returns a human readable description of the data within this structure.
* Returns a human-readable description of the data within this structure.
*
* @return a description of this structure.
*/
public String toString() {
String dsedInfo = "";
dsedInfo += dsedHeader2.toString();
dsedInfo += dsedSubHeader.toString();
dsedInfo += getDeviceContextInfo();
return dsedInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public abstract class DeviceSecurityEventDataDeviceContext {
/**
* DeviceSecurityEventDataDeviceContext Constructor.
*
* @param dSEDdeviceContextBytes byte array holding the DeviceSecurityEventData.
* @param dsedDeviceContextBytes byte array holding the DeviceSecurityEventData.
*/
public DeviceSecurityEventDataDeviceContext(final byte[] dSEDdeviceContextBytes) {
public DeviceSecurityEventDataDeviceContext(final byte[] dsedDeviceContextBytes) {

byte[] pciVersionBytes = new byte[2];
System.arraycopy(dSEDdeviceContextBytes, 0, pciVersionBytes, 0, 2);
version = HexUtils.leReverseInt(pciVersionBytes);
byte[] versionBytes = new byte[2];
System.arraycopy(dsedDeviceContextBytes, 0, versionBytes, 0, 2);
version = HexUtils.leReverseInt(versionBytes);

byte[] pciLengthBytes = new byte[2];
System.arraycopy(dSEDdeviceContextBytes, 2, pciLengthBytes, 0, 2);
length = HexUtils.leReverseInt(pciLengthBytes);
byte[] lengthBytes = new byte[2];
System.arraycopy(dsedDeviceContextBytes, 2, lengthBytes, 0, 2);
length = HexUtils.leReverseInt(lengthBytes);
}

/**
Expand All @@ -52,8 +52,7 @@ public DeviceSecurityEventDataDeviceContext(final byte[] dSEDdeviceContextBytes)
public String toString() {
String dSEDdeviceContextCommonInfo = "";

dSEDdeviceContextCommonInfo += "\n DeviceSecurityEventData Device Info:";
dSEDdeviceContextCommonInfo += "\n Device Structure Version = " + version;
dSEDdeviceContextCommonInfo += "\n DeviceSecurityEventData Device Context:";

return dSEDdeviceContextCommonInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import hirs.utils.tpm.eventlog.uefi.UefiConstants;
import lombok.Getter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
Expand Down Expand Up @@ -49,43 +51,46 @@ public class DeviceSecurityEventDataHeader extends DeviceSecurityEventHeader {
/**
* DeviceSecurityEventDataHeader Constructor.
*
* @param dSEDbytes byte array holding the DeviceSecurityEventData.
* @param dsedBytes byte array holding the DeviceSecurityEventData.
*/
public DeviceSecurityEventDataHeader(final byte[] dSEDbytes) throws UnsupportedEncodingException {
public DeviceSecurityEventDataHeader(final byte[] dsedBytes) {

super(dSEDbytes);
super(dsedBytes);

byte[] lengthBytes = new byte[UefiConstants.SIZE_2];
System.arraycopy(dSEDbytes, 18, lengthBytes, 0,
System.arraycopy(dsedBytes, 18, lengthBytes, 0,
UefiConstants.SIZE_2);
length = HexUtils.leReverseInt(lengthBytes);

byte[] spdmHashAlgoBytes = new byte[UefiConstants.SIZE_4];
System.arraycopy(dSEDbytes, UefiConstants.OFFSET_20, spdmHashAlgoBytes, 0,
System.arraycopy(dsedBytes, UefiConstants.OFFSET_20, spdmHashAlgoBytes, 0,
UefiConstants.SIZE_4);
spdmHashAlgo = HexUtils.leReverseInt(spdmHashAlgoBytes);

extractDeviceType(dSEDbytes, 24);
extractDeviceType(dsedBytes, 24);

// get the size of the SPDM Measurement Block
byte[] sizeOfSpdmMeasBlockBytes = new byte[UefiConstants.SIZE_2];
System.arraycopy(dSEDbytes, 30, sizeOfSpdmMeasBlockBytes, 0,
System.arraycopy(dsedBytes, 30, sizeOfSpdmMeasBlockBytes, 0,
UefiConstants.SIZE_2);
int sizeOfSpdmMeas = HexUtils.leReverseInt(sizeOfSpdmMeasBlockBytes);
int sizeOfSpdmMeasBlock = sizeOfSpdmMeas + 4; // header is 4 bytes

// extract the bytes from the SPDM Measurement Block
// extract the bytes that comprise the SPDM Measurement Block
byte[] spdmMeasBlockBytes = new byte[sizeOfSpdmMeasBlock];
System.arraycopy(dSEDbytes, 28, spdmMeasBlockBytes, 0,
System.arraycopy(dsedBytes, 28, spdmMeasBlockBytes, 0,
sizeOfSpdmMeasBlock);
spdmMeasurementBlock = new SpdmMeasurementBlock(spdmMeasBlockBytes);

ByteArrayInputStream spdmMeasurementBlockData =
new ByteArrayInputStream(spdmMeasBlockBytes);
spdmMeasurementBlock = new SpdmMeasurementBlock(spdmMeasurementBlockData);

int devPathLenStartByte = 28 + sizeOfSpdmMeasBlock;
extractDevicePathAndFinalSize(dSEDbytes, devPathLenStartByte);
extractDevicePathAndFinalSize(dsedBytes, devPathLenStartByte);
}

/**
* Returns a human readable description of the data within this structure.
* Returns a human-readable description of the data within this structure.
*
* @return a description of this structure.
*/
Expand Down
Loading
Loading