Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

cel: Some ideas for ergonomics improvements #145

Merged
merged 2 commits into from
Dec 3, 2021
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
76 changes: 31 additions & 45 deletions cel/canonical_eventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type TLV struct {
}

// MarshalBinary marshals a TLV to a byte slice.
func (tlv *TLV) MarshalBinary() (data []byte, err error) {
func (tlv TLV) MarshalBinary() (data []byte, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems like:

So I think it's fine to use a value receiver here. The type is small, marshaling doesn't change the value, and if I change it to a pointer receiver the code doesn't compile.

This is because you then can't do something like:

createRecNumField(r.RecNum).MarshalBinary()

as temporary values do not have an address.

buf := make([]byte, len(tlv.Value)+tlvTypeFieldLength+tlvLengthFieldLength)

buf[0] = tlv.Type
Expand Down Expand Up @@ -98,9 +98,9 @@ func UnmarshalFirstTLV(buf *bytes.Buffer) (tlv TLV, err error) {

// Record represents a Canonical Eventlog Record.
type Record struct {
RECNUM TLV
PCR TLV
Digests TLV
RecNum uint64
PCR uint8
Digests map[crypto.Hash][]byte
Content TLV
}

Expand Down Expand Up @@ -132,9 +132,11 @@ func (c *CEL) AppendEvent(tpm io.ReadWriteCloser, pcr int, hashAlgos []crypto.Ha
// TODO: extend the digest to TPM PCR
}

celr, err := createCELR(uint64(len(c.Records)), uint8(pcr), digestsMap, event)
if err != nil {
return err
celr := Record{
RecNum: uint64(len(c.Records)),
PCR: uint8(pcr),
Digests: digestsMap,
Content: event.GetTLV(),
}

c.Records = append(c.Records, celr)
Expand All @@ -149,7 +151,7 @@ func createRecNumField(recNum uint64) TLV {

// UnmarshalRecNum takes in a TLV with its type equals to the recnum type value (0), and
// return its record number.
func UnmarshalRecNum(tlv TLV) (uint64, error) {
func unmarshalRecNum(tlv TLV) (uint64, error) {
if tlv.Type != recnumTypeValue {
return 0, fmt.Errorf("type of the TLV [%d] indicates it is not a recnum field [%d]",
tlv.Type, recnumTypeValue)
Expand All @@ -168,7 +170,7 @@ func createPCRField(pcrNum uint8) TLV {

// UnmarshalPCR takes in a TLV with its type equals to the PCR type value (1), and
// return its PCR number.
func UnmarshalPCR(tlv TLV) (pcrNum uint8, err error) {
func unmarshalPCR(tlv TLV) (pcrNum uint8, err error) {
if tlv.Type != pcrTypeValue {
return 0, fmt.Errorf("type of the TLV [%d] indicates it is not a PCR field [%d]",
tlv.Type, pcrTypeValue)
Expand Down Expand Up @@ -208,7 +210,7 @@ func createDigestField(digestMap map[crypto.Hash][]byte) (TLV, error) {

// UnmarshalDigests takes in a TLV with its type equals to the digests type value (3), and
// return its digests content in a map, the key is its TPM hash algorithm.
func UnmarshalDigests(tlv TLV) (digestsMap map[crypto.Hash][]byte, err error) {
func unmarshalDigests(tlv TLV) (digestsMap map[crypto.Hash][]byte, err error) {
if tlv.Type != digestsTypeValue {
return nil, fmt.Errorf("type of the TLV indicates it doesn't contain digests")
}
Expand All @@ -232,31 +234,22 @@ func UnmarshalDigests(tlv TLV) (digestsMap map[crypto.Hash][]byte, err error) {
return digestsMap, nil
}

func createCELR(recNum uint64, pcr uint8, digestsmap map[crypto.Hash][]byte, event Content) (celr Record, err error) {
recnumField := createRecNumField(recNum)
pcrField := createPCRField(pcr)

digestField, err := createDigestField(digestsmap)
if err != nil {
return Record{}, err
}
celr = Record{recnumField, pcrField, digestField, event.GetTLV()}

return celr, nil
}

// EncodeCELR encodes the CELR to bytes according to the CEL spec and write them
// to the bytes byffer.
func (r *Record) EncodeCELR(buf *bytes.Buffer) error {
recnumField, err := r.RECNUM.MarshalBinary()
recnumField, err := createRecNumField(r.RecNum).MarshalBinary()
if err != nil {
return err
}
pcrField, err := createPCRField(r.PCR).MarshalBinary()
if err != nil {
return err
}
pcrField, err := r.PCR.MarshalBinary()
digests, err := createDigestField(r.Digests)
if err != nil {
return err
}
digestsField, err := r.Digests.MarshalBinary()
digestsField, err := digests.MarshalBinary()
if err != nil {
return err
}
Expand Down Expand Up @@ -313,44 +306,37 @@ func DecodeToCEL(buf *bytes.Buffer) (CEL, error) {

// DecodeToCELR will read the buf for the next CELR, will return err if
// failed to unmarshal a correct CELR TLV from the buffer.
func DecodeToCELR(buf *bytes.Buffer) (Record, error) {
func DecodeToCELR(buf *bytes.Buffer) (r Record, err error) {
recnum, err := UnmarshalFirstTLV(buf)
if err != nil {
return Record{}, err
}
if recnum.Type != recnumTypeValue {
return Record{}, fmt.Errorf("recnum TLV doesn't have the correct type [%d], got [%d]",
recnumTypeValue, recnum.Type)
r.RecNum, err = unmarshalRecNum(recnum)
if err != nil {
return Record{}, err
}

pcr, err := UnmarshalFirstTLV(buf)
if err != nil {
return Record{}, err
}
if pcr.Type != pcrTypeValue {
return Record{}, fmt.Errorf("pcr TLV doesn't have the correct type [%d], got [%d]",
pcrTypeValue, pcr.Type)
r.PCR, err = unmarshalPCR(pcr)
if err != nil {
return Record{}, err
}

digests, err := UnmarshalFirstTLV(buf)
if err != nil {
return Record{}, err
}
if digests.Type != digestsTypeValue {
return Record{}, fmt.Errorf("digests TLV doesn't have the correct type [%d], got [%d]",
digestsTypeValue, digests.Type)
r.Digests, err = unmarshalDigests(digests)
if err != nil {
return Record{}, err
}

content, err := UnmarshalFirstTLV(buf)
r.Content, err = UnmarshalFirstTLV(buf)
if err != nil {
return Record{}, err
}
celr := Record{
recnum,
pcr,
digests,
content,
}

return celr, nil
return r, nil
}
13 changes: 5 additions & 8 deletions cel/canonical_eventlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,20 @@ func TestCELEncodingDecoding(t *testing.T) {
if len(decodedcel.Records) != 2 {
t.Errorf("should have two records")
}
if recnum, err := UnmarshalRecNum(decodedcel.Records[0].RECNUM); err != nil || recnum != uint64(0) {
if decodedcel.Records[0].RecNum != 0 {
t.Errorf("recnum mismatch")
}
if recnum, err := UnmarshalRecNum(decodedcel.Records[1].RECNUM); err != nil || recnum != uint64(1) {
if decodedcel.Records[1].RecNum != 1 {
t.Errorf("recnum mismatch")
}
if pcr, err := UnmarshalPCR(decodedcel.Records[0].PCR); err != nil || pcr != uint8(13) {
if decodedcel.Records[0].PCR != 13 {
t.Errorf("pcr value mismatch")
}
if pcr, err := UnmarshalPCR(decodedcel.Records[1].PCR); err != nil || pcr != uint8(14) {
if decodedcel.Records[1].PCR != 14 {
t.Errorf("pcr value mismatch")
}

digestsMap, err := UnmarshalDigests(decodedcel.Records[0].Digests)
if err != nil {
t.Error(err)
}
digestsMap := decodedcel.Records[0].Digests
if len(digestsMap[crypto.SHA256]) != 32 {
t.Errorf("SHA256 digest length doesn't match")
}
Expand Down