From 218d939f135aae362f146986ae968e234c423cc4 Mon Sep 17 00:00:00 2001 From: Ruoyu Ying Date: Tue, 2 Apr 2024 09:36:10 +0800 Subject: [PATCH] common: return standard CEL event log instead of encoded ones Signed-off-by: Ruoyu Ying --- common/python/cctrusted_base/eventlog.py | 32 +++++++++++----------- common/python/cctrusted_base/tcgcel.py | 35 ++++++++++++++++++------ 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/common/python/cctrusted_base/eventlog.py b/common/python/cctrusted_base/eventlog.py index f8ca74ce..daadf33a 100644 --- a/common/python/cctrusted_base/eventlog.py +++ b/common/python/cctrusted_base/eventlog.py @@ -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: @@ -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 @@ -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. """ @@ -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. @@ -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): @@ -452,7 +453,7 @@ 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: @@ -460,14 +461,13 @@ def replay(event_logs:list) -> dict: 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 diff --git a/common/python/cctrusted_base/tcgcel.py b/common/python/cctrusted_base/tcgcel.py index c0fbd5ac..fce250d9 100644 --- a/common/python/cctrusted_base/tcgcel.py +++ b/common/python/cctrusted_base/tcgcel.py @@ -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__) @@ -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 + @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)