Skip to content

Commit

Permalink
common: return standard CEL event log instead of encoded ones (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruoyu-y authored Apr 2, 2024
1 parent 77048b4 commit 5edc36e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
32 changes: 16 additions & 16 deletions common/python/cctrusted_base/eventlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class TcgEventLog:
"""

TCG_FORMAT_PCCLIENT = 0
TCG_FORMAT_CEL_TLV = 1
TCG_FORMAT_CEL_JSON = 2
TCG_FORMAT_CEL_CBOR = 3
TCG_FORMAT_CEL = 1
TCG_FORMAT_CEL_TLV = 2
TCG_FORMAT_CEL_JSON = 3
TCG_FORMAT_CEL_CBOR = 4

def __init__(self, rec_num:int, imr_index:int, event_type:TcgEventType, digests:list[TcgDigest],
event_size:int, event:bytes, extra_info=None) -> None:
Expand All @@ -59,9 +60,8 @@ def format_event_log(self, parse_format:str):
if parse_format == self.TCG_FORMAT_PCCLIENT:
return self._to_tcg_pcclient_format()

if parse_format in (self.TCG_FORMAT_CEL_JSON, self.TCG_FORMAT_CEL_CBOR,
self.TCG_FORMAT_CEL_TLV) :
return self._to_tcg_canonical_format(parse_format)
if parse_format == self.TCG_FORMAT_CEL :
return self._to_tcg_canonical_format()

return None

Expand All @@ -79,7 +79,7 @@ def _to_tcg_pcclient_format(self):
return TcgImrEvent(self._imr_index, self._event_type, self._digests, self._event_size,
self._event)

def _to_tcg_canonical_format(self, encoding:str=None):
def _to_tcg_canonical_format(self):
"""The function to convert event log data into event log following
Canonical Eventlog Spec.
"""
Expand All @@ -101,8 +101,9 @@ def _to_tcg_canonical_format(self, encoding:str=None):
None,
content_data)

# switch encoding according to user input
return TcgTpmsCelEvent.encode(event, encoding)
# return basic CEL event
# can switch encoding by calling the TcgTpmsCelEvent.encoding()
return event

class EventLogs:
"""EventLogs class.
Expand Down Expand Up @@ -251,7 +252,7 @@ def _parse(self) -> None:
for event in self._runtime_data.splitlines():
event_log = self._parse_ima_event_log(event)
self._event_logs.append(
event_log.format_event_log(TcgEventLog.TCG_FORMAT_CEL_TLV))
event_log.format_event_log(TcgEventLog.TCG_FORMAT_CEL))
self._count += 1

def _parse_spec_id_event_log(self, data:bytes) -> (TcgEventLog, int):
Expand Down Expand Up @@ -452,22 +453,21 @@ def replay(event_logs:list) -> dict:
# TODO: consider CEL-JSON/CEL-CBOR encoding later
# extract common attributes from different formats, only consider TLV encoding for now
if isinstance(event, TcgTpmsCelEvent):
content_type = event.content.type
content_type = event.content_type
# Align the Canonical types with TCG PCClient Event types
match content_type:
case TcgCelTypes.CEL_IMA_TEMPLATE:
event_type = TcgEventType.IMA_MEASUREMENT_EVENT
case TcgCelTypes.CEL_PCCLIENT_STD:
# For PCClient_STD event,
# the event type is store within the content attribute
event_type = event.content.value[0].value
# event_type = event.content.value[0].value
event_type = event.content.event_type

# TODO: consider the NV_INDEX case later
imr_index = event.index.value
imr_index = event.index

digests = []
for d in event.digests.value:
digests.append(TcgDigest(d.type, d.value))
digests = event.digests
else:
event_type = event.event_type
# Skip EV_NO_ACTION event during replay as
Expand Down
35 changes: 27 additions & 8 deletions common/python/cctrusted_base/tcgcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from abc import abstractmethod
from cctrusted_base.tcg import TcgDigest
from cctrusted_base.tcg import TcgAlgorithmRegistry
from cctrusted_base.tcg import TcgEventType
from cctrusted_base.eventlog import TcgImrEvent
from cctrusted_base.binaryblob import BinaryBlob

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -83,33 +85,50 @@ def set_digests(self, digests):

@property
def content(self):
"""Content of the event.a"""
"""Content of the event."""
return self._content

def set_content(self, content):
"""Set formatted value for content."""
self._content = content

@property
def content_type(self):
"""Content type of event."""
return self._content_type

def encoding(self):
"""Get the encoding format of the event"""
return self._encoding

def to_pcclient_format(self):
"""Convert CEL event log to PCClient format"""
if self._content_type == TcgCelTypes.CEL_IMA_TEMPLATE:
event = self.content.template_data
return TcgImrEvent(self._imr, TcgEventType.IMA_MEASUREMENT_EVENT,
self._digests, len(event), event)
if self._content_type == TcgCelTypes.CEL_PCCLIENT_STD:
return TcgImrEvent(self._imr, self.content.event_type, self._digests,
len(self.content.event_data), self.content.event_data)
LOG.error("Unsupported content to parse into TCG PCClient format.")
return None

@staticmethod
def encode(obj, encoding:int=1):
def encode(obj, encoding:int=2):
"""Encode the CEL record in certain format"""
match encoding:
# TCG_FORMAT_CEL_TLV = 1
case 1:
# TcgEventLog.TCG_FORMAT_CEL_TLV = 2
case 2:
# pylint: disable-next=w0212
obj._encoding = "TLV"
return TcgTpmsCelEvent._encoded_in_tlv(obj)
# TCG_FORMAT_CEL_JSON = 2
case 2:
# TcgEventLog.TCG_FORMAT_CEL_JSON = 3
case 3:
# pylint: disable-next=w0212
obj._encoding = "JSON"
return TcgTpmsCelEvent._encoded_in_json(obj)
# TCG_FORMAT_CEL_CBOR = 3
case 3:
# TcgEventLog.TCG_FORMAT_CEL_JSON = 4
case 4:
# pylint: disable-next=w0212
obj._encoding = "CBOR"
return TcgTpmsCelEvent._encoded_in_cbor(obj)
Expand Down

0 comments on commit 5edc36e

Please sign in to comment.