Skip to content

Commit

Permalink
get_quote: refine output format
Browse files Browse the repository at this point in the history
Signed-off-by: zhongjie <[email protected]>
  • Loading branch information
intelzhongjie committed Dec 19, 2023
1 parent 60ef541 commit 7bc033b
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 48 deletions.
64 changes: 46 additions & 18 deletions common/python/cctrusted_base/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,39 @@
Quote data structures
"""

import logging
from abc import ABC, abstractmethod
from cctrusted_base.binaryblob import BinaryBlob



class QuoteHeader(ABC):
LOG = logging.getLogger(__name__)



class QuoteHeader(BinaryBlob):
"""
Quote Header abstract class (interface)
"""

@abstractmethod
def get_data(self) -> bytearray:
"""
Get raw data
"""
raise NotImplementedError("Should be implemented by inherited class")

class QuoteBody(ABC):
class QuoteBody(BinaryBlob):
"""
Quote Body abstract class (interface)
"""

@abstractmethod
def get_data(self) -> bytearray:
"""
Get raw data
"""
raise NotImplementedError("Should be implemented by inherited class")

QuoteSignature = bytearray
class QuoteSignature(BinaryBlob):
"""
Quote Signature
"""

class Quote(ABC):
"""
Quote abstract class (interface)
"""

QUOTE_OUT_FORMAT_RAW = "raw"
QUOTE_OUT_FORMAT_HUMAN = "human"

@abstractmethod
def get_header(self) -> QuoteHeader:
"""
Expand All @@ -48,7 +46,6 @@ def get_header(self) -> QuoteHeader:
def get_body(self) -> QuoteBody:
"""
Get quote body.
The body (excludes the header) correspongs to the data to be signed.
"""
raise NotImplementedError("Should be implemented by inherited class")

Expand All @@ -59,6 +56,37 @@ def get_sig(self) -> QuoteSignature:
"""
raise NotImplementedError("Should be implemented by inherited class")

def dump(self, out_format=QUOTE_OUT_FORMAT_RAW) -> None:
"""
Dump Quote Data.
out_format: QUOTE_OUT_FORMAT_RAW / QUOTE_OUT_FORMAT_HUMAN.
"""
print(f'{type(self).__name__}:')
h = self.get_header()
if h is not None:
h.dump(fmt=out_format)
b = self.get_body()
if b is not None:
b.dump(fmt=out_format)
s = self.get_sig()
if s is not None:
s.dump(fmt=out_format)

@staticmethod
def is_valid_format(fmt) -> bool:
"""
Check if format is supported valid option
"""
if fmt is None:
# When the format is not set. Use the default.
return True
if fmt == Quote.QUOTE_OUT_FORMAT_RAW:
return True
if fmt == Quote.QUOTE_OUT_FORMAT_HUMAN:
return True
return False


class Tpm2Quote(Quote):
"""
TPM Quote
Expand Down
Loading

0 comments on commit 7bc033b

Please sign in to comment.