Skip to content

Commit 5edc36e

Browse files
authored
common: return standard CEL event log instead of encoded ones (#117)
1 parent 77048b4 commit 5edc36e

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

common/python/cctrusted_base/eventlog.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class TcgEventLog:
4040
"""
4141

4242
TCG_FORMAT_PCCLIENT = 0
43-
TCG_FORMAT_CEL_TLV = 1
44-
TCG_FORMAT_CEL_JSON = 2
45-
TCG_FORMAT_CEL_CBOR = 3
43+
TCG_FORMAT_CEL = 1
44+
TCG_FORMAT_CEL_TLV = 2
45+
TCG_FORMAT_CEL_JSON = 3
46+
TCG_FORMAT_CEL_CBOR = 4
4647

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

62-
if parse_format in (self.TCG_FORMAT_CEL_JSON, self.TCG_FORMAT_CEL_CBOR,
63-
self.TCG_FORMAT_CEL_TLV) :
64-
return self._to_tcg_canonical_format(parse_format)
63+
if parse_format == self.TCG_FORMAT_CEL :
64+
return self._to_tcg_canonical_format()
6565

6666
return None
6767

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

82-
def _to_tcg_canonical_format(self, encoding:str=None):
82+
def _to_tcg_canonical_format(self):
8383
"""The function to convert event log data into event log following
8484
Canonical Eventlog Spec.
8585
"""
@@ -101,8 +101,9 @@ def _to_tcg_canonical_format(self, encoding:str=None):
101101
None,
102102
content_data)
103103

104-
# switch encoding according to user input
105-
return TcgTpmsCelEvent.encode(event, encoding)
104+
# return basic CEL event
105+
# can switch encoding by calling the TcgTpmsCelEvent.encoding()
106+
return event
106107

107108
class EventLogs:
108109
"""EventLogs class.
@@ -251,7 +252,7 @@ def _parse(self) -> None:
251252
for event in self._runtime_data.splitlines():
252253
event_log = self._parse_ima_event_log(event)
253254
self._event_logs.append(
254-
event_log.format_event_log(TcgEventLog.TCG_FORMAT_CEL_TLV))
255+
event_log.format_event_log(TcgEventLog.TCG_FORMAT_CEL))
255256
self._count += 1
256257

257258
def _parse_spec_id_event_log(self, data:bytes) -> (TcgEventLog, int):
@@ -452,22 +453,21 @@ def replay(event_logs:list) -> dict:
452453
# TODO: consider CEL-JSON/CEL-CBOR encoding later
453454
# extract common attributes from different formats, only consider TLV encoding for now
454455
if isinstance(event, TcgTpmsCelEvent):
455-
content_type = event.content.type
456+
content_type = event.content_type
456457
# Align the Canonical types with TCG PCClient Event types
457458
match content_type:
458459
case TcgCelTypes.CEL_IMA_TEMPLATE:
459460
event_type = TcgEventType.IMA_MEASUREMENT_EVENT
460461
case TcgCelTypes.CEL_PCCLIENT_STD:
461462
# For PCClient_STD event,
462463
# the event type is store within the content attribute
463-
event_type = event.content.value[0].value
464+
# event_type = event.content.value[0].value
465+
event_type = event.content.event_type
464466

465467
# TODO: consider the NV_INDEX case later
466-
imr_index = event.index.value
468+
imr_index = event.index
467469

468-
digests = []
469-
for d in event.digests.value:
470-
digests.append(TcgDigest(d.type, d.value))
470+
digests = event.digests
471471
else:
472472
event_type = event.event_type
473473
# Skip EV_NO_ACTION event during replay as

common/python/cctrusted_base/tcgcel.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from abc import abstractmethod
66
from cctrusted_base.tcg import TcgDigest
77
from cctrusted_base.tcg import TcgAlgorithmRegistry
8+
from cctrusted_base.tcg import TcgEventType
9+
from cctrusted_base.eventlog import TcgImrEvent
810
from cctrusted_base.binaryblob import BinaryBlob
911

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

8486
@property
8587
def content(self):
86-
"""Content of the event.a"""
88+
"""Content of the event."""
8789
return self._content
8890

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

95+
@property
96+
def content_type(self):
97+
"""Content type of event."""
98+
return self._content_type
99+
93100
def encoding(self):
94101
"""Get the encoding format of the event"""
95102
return self._encoding
96103

104+
def to_pcclient_format(self):
105+
"""Convert CEL event log to PCClient format"""
106+
if self._content_type == TcgCelTypes.CEL_IMA_TEMPLATE:
107+
event = self.content.template_data
108+
return TcgImrEvent(self._imr, TcgEventType.IMA_MEASUREMENT_EVENT,
109+
self._digests, len(event), event)
110+
if self._content_type == TcgCelTypes.CEL_PCCLIENT_STD:
111+
return TcgImrEvent(self._imr, self.content.event_type, self._digests,
112+
len(self.content.event_data), self.content.event_data)
113+
LOG.error("Unsupported content to parse into TCG PCClient format.")
114+
return None
115+
97116
@staticmethod
98-
def encode(obj, encoding:int=1):
117+
def encode(obj, encoding:int=2):
99118
"""Encode the CEL record in certain format"""
100119
match encoding:
101-
# TCG_FORMAT_CEL_TLV = 1
102-
case 1:
120+
# TcgEventLog.TCG_FORMAT_CEL_TLV = 2
121+
case 2:
103122
# pylint: disable-next=w0212
104123
obj._encoding = "TLV"
105124
return TcgTpmsCelEvent._encoded_in_tlv(obj)
106-
# TCG_FORMAT_CEL_JSON = 2
107-
case 2:
125+
# TcgEventLog.TCG_FORMAT_CEL_JSON = 3
126+
case 3:
108127
# pylint: disable-next=w0212
109128
obj._encoding = "JSON"
110129
return TcgTpmsCelEvent._encoded_in_json(obj)
111-
# TCG_FORMAT_CEL_CBOR = 3
112-
case 3:
130+
# TcgEventLog.TCG_FORMAT_CEL_JSON = 4
131+
case 4:
113132
# pylint: disable-next=w0212
114133
obj._encoding = "CBOR"
115134
return TcgTpmsCelEvent._encoded_in_cbor(obj)

0 commit comments

Comments
 (0)