Skip to content
Open
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
16 changes: 7 additions & 9 deletions tcglog-dump/block.go → block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package main
package tcglog

import (
"encoding/hex"
Expand All @@ -12,8 +12,6 @@ import (
"strings"

"github.com/canonical/go-tpm2"

"github.com/canonical/tcglog-parser"
)

type blockFormatter struct {
Expand All @@ -24,9 +22,9 @@ type blockFormatter struct {
varHexdump bool
}

func (*blockFormatter) printHeader() {}
func (*blockFormatter) PrintHeader() {}

func (f *blockFormatter) printEvent(event *tcglog.Event) {
func (f *blockFormatter) PrintEvent(event *Event) {
fmt.Fprintf(f.dst, "\nPCR: %d\n", event.PCRIndex)
fmt.Fprintf(f.dst, "TYPE: %s\n", event.EventType)
for _, alg := range []tpm2.HashAlgorithmId{
Expand All @@ -50,22 +48,22 @@ func (f *blockFormatter) printEvent(event *tcglog.Event) {
verbose = true
}
if f.verbosity > 0 {
fmt.Fprintf(f.dst, "DETAILS: %s\n", eventDetailsStringer(event, verbose))
fmt.Fprintf(f.dst, "DETAILS: %s\n", EventDetailsStringer(event, verbose))
}
if f.hexdump {
fmt.Fprintf(f.dst, "EVENT DATA:\n\t%s", strings.Replace(hex.Dump(event.Data.Bytes()), "\n", "\n\t", -1))
}
if f.varHexdump {
varData, ok := event.Data.(*tcglog.EFIVariableData)
varData, ok := event.Data.(*EFIVariableData)
if ok {
fmt.Fprintf(f.dst, "EFI VARIABLE PAYLOAD:\n\t%s", strings.Replace(hex.Dump(varData.VariableData), "\n", "\n\t", -1))
}
}
}

func (*blockFormatter) flush() {}
func (*blockFormatter) Flush() {}

func newBlockFormatter(f *os.File, verbosity int, hexdump, varHexdump bool) formatter {
func NewBlockFormatter(f *os.File, verbosity int, hexdump, varHexdump bool) Formatter {
return &blockFormatter{
dst: f,
verbosity: verbosity,
Expand Down
44 changes: 21 additions & 23 deletions tcglog-dump/details.go → details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package main
package tcglog

import (
"bytes"
Expand All @@ -12,9 +12,7 @@ import (
"fmt"
"strings"

"github.com/canonical/go-efilib"

"github.com/canonical/tcglog-parser"
efi "github.com/canonical/go-efilib"
)

var shimLockGuid = efi.MakeGUID(0x605dab50, 0xe046, 0x4300, 0xabb6, [...]uint8{0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23})
Expand Down Expand Up @@ -161,18 +159,18 @@ func (s stringVariableStringer) String() string {
}

type simpleGptEventStringer struct {
data *tcglog.EFIGPTData
data *EFIGPTData
}

func (s *simpleGptEventStringer) String() string {
return fmt.Sprint("DiskGUID: ", s.data.Hdr.DiskGUID)
}

func customEventDetailsStringer(event *tcglog.Event, verbose bool) fmt.Stringer {
func customEventDetailsStringer(event *Event, verbose bool) fmt.Stringer {
switch {
//case event.EventType == tcglog.EventTypeNoAction && !verbose:
case event.EventType == tcglog.EventTypeEFIVariableBoot, event.EventType == tcglog.EventTypeEFIVariableBoot2:
varData, ok := event.Data.(*tcglog.EFIVariableData)
//case event.EventType == EventTypeNoAction && !verbose:
case event.EventType == EventTypeEFIVariableBoot, event.EventType == EventTypeEFIVariableBoot2:
varData, ok := event.Data.(*EFIVariableData)
if !ok {
return event.Data
}
Expand All @@ -186,8 +184,8 @@ func customEventDetailsStringer(event *tcglog.Event, verbose bool) fmt.Stringer
}

return &bootOptionVariableStringer{verbose, varData.UnicodeName, varData.VariableData}
case event.EventType == tcglog.EventTypeEFIVariableDriverConfig:
varData, ok := event.Data.(*tcglog.EFIVariableData)
case event.EventType == EventTypeEFIVariableDriverConfig:
varData, ok := event.Data.(*EFIVariableData)
if !ok {
return event.Data
}
Expand All @@ -198,8 +196,8 @@ func customEventDetailsStringer(event *tcglog.Event, verbose bool) fmt.Stringer
}
}
return &dbVariableStringer{varDescriptor{Name: varData.UnicodeName, GUID: varData.VariableName}, varData.VariableData, verbose}
case event.EventType == tcglog.EventTypeEFIVariableAuthority:
varData, ok := event.Data.(*tcglog.EFIVariableData)
case event.EventType == EventTypeEFIVariableAuthority:
varData, ok := event.Data.(*EFIVariableData)
if !ok {
return event.Data
}
Expand All @@ -214,17 +212,17 @@ func customEventDetailsStringer(event *tcglog.Event, verbose bool) fmt.Stringer
}

return &variableAuthorityStringer{varDescriptor{Name: varData.UnicodeName, GUID: varData.VariableName}, varData.VariableData, verbose}
case event.EventType == tcglog.EventTypeEFIGPTEvent && !verbose:
data, ok := event.Data.(*tcglog.EFIGPTData)
case event.EventType == EventTypeEFIGPTEvent && !verbose:
data, ok := event.Data.(*EFIGPTData)
if !ok {
return event.Data
}

return &simpleGptEventStringer{data}
case event.EventType == tcglog.EventTypeEFIBootServicesApplication, event.EventType == tcglog.EventTypeEFIBootServicesDriver,
event.EventType == tcglog.EventTypeEFIRuntimeServicesDriver:
case event.EventType == EventTypeEFIBootServicesApplication, event.EventType == EventTypeEFIBootServicesDriver,
event.EventType == EventTypeEFIRuntimeServicesDriver:
if !verbose {
data, ok := event.Data.(*tcglog.EFIImageLoadEvent)
data, ok := event.Data.(*EFIImageLoadEvent)
if !ok {
return event.Data
}
Expand All @@ -239,18 +237,18 @@ type nullStringer struct{}

func (s nullStringer) String() string { return "" }

func eventDetailsStringer(event *tcglog.Event, verbose bool) fmt.Stringer {
func EventDetailsStringer(event *Event, verbose bool) fmt.Stringer {
if out := customEventDetailsStringer(event, verbose); out != nil {
return out
}
switch d := event.Data.(type) {
case *tcglog.GrubStringEventData:
case *GrubStringEventData:
return d
case tcglog.OpaqueEventData:
case OpaqueEventData:
return d
case tcglog.StringEventData:
case StringEventData:
return d
case *tcglog.SystemdEFIStubCommandline:
case *SystemdEFIStubCommandline:
return d
default:
if verbose {
Expand Down
11 changes: 11 additions & 0 deletions formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package tcglog

type Formatter interface {
PrintHeader()
PrintEvent(event *Event)
Flush()
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/canonical/go-sp800.108-kdf v0.0.0-20210315104021-ead800bbf9a0 // indirect
github.com/canonical/go-sp800.90a-drbg v0.0.0-20210314144037-6eeb1040d6c3 // indirect
github.com/canonical/go-tpm2 v0.1.0
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2
github.com/gorilla/mux v1.8.0 // indirect
github.com/jessevdk/go-flags v1.5.0
github.com/mvo5/goconfigparser v0.0.0-20201015074339-50f22f44deb5 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ github.com/canonical/go-tpm2 v0.1.0 h1:2mXU+Hy+zYSxmuYys2NtPEO6NwT3Qr9Sygwtops7N
github.com/canonical/go-tpm2 v0.1.0/go.mod h1:vG41hdbBjV4+/fkubTT1ENBBqSkLwLr7mCeW9Y6kpZY=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand Down
14 changes: 6 additions & 8 deletions tcglog-dump/table.go → table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package main
package tcglog

import (
"bytes"
Expand All @@ -15,8 +15,6 @@ import (

"github.com/canonical/go-tpm2"
"golang.org/x/sys/unix"

"github.com/canonical/tcglog-parser"
)

type terminalWriter struct {
Expand Down Expand Up @@ -80,27 +78,27 @@ type tableFormatter struct {
verbose bool
}

func (f *tableFormatter) printHeader() {
func (f *tableFormatter) PrintHeader() {
fmt.Fprint(f.dst, "PCR\tDIGEST\tTYPE")
if f.verbose {
fmt.Fprint(f.dst, "\tDETAILS")
}
fmt.Fprint(f.dst, "\n")
}

func (f *tableFormatter) printEvent(event *tcglog.Event) {
func (f *tableFormatter) PrintEvent(event *Event) {
fmt.Fprintf(f.dst, "%d\t%x\t%s", event.PCRIndex, event.Digests[f.alg], event.EventType)
if f.verbose {
fmt.Fprintf(f.dst, "\t%s", &tableStringer{eventDetailsStringer(event, false)})
fmt.Fprintf(f.dst, "\t%s", &tableStringer{EventDetailsStringer(event, false)})
}
fmt.Fprint(f.dst, "\n")
}

func (f *tableFormatter) flush() {
func (f *tableFormatter) Flush() {
f.dst.Flush()
}

func newTableFormatter(f *os.File, alg tpm2.HashAlgorithmId, verbose bool) (formatter, error) {
func NewTableFormatter(f *os.File, alg tpm2.HashAlgorithmId, verbose bool) (Formatter, error) {
var w io.Writer = f

sz, err := unix.IoctlGetWinsize(int(f.Fd()), unix.TIOCGWINSZ)
Expand Down
15 changes: 0 additions & 15 deletions tcglog-dump/formatter.go

This file was deleted.

12 changes: 6 additions & 6 deletions tcglog-dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,25 @@ func run() error {
}
}

var formatter formatter
var formatter tcglog.Formatter
if len(opts.Verbose) < 2 && !opts.Hexdump && !opts.VarHexdump {
var err error
formatter, err = newTableFormatter(os.Stdout, alg, len(opts.Verbose) > 0)
formatter, err = tcglog.NewTableFormatter(os.Stdout, alg, len(opts.Verbose) > 0)
if err != nil {
return err
}
} else {
formatter = newBlockFormatter(os.Stdout, len(opts.Verbose), opts.Hexdump, opts.VarHexdump)
formatter = tcglog.NewBlockFormatter(os.Stdout, len(opts.Verbose), opts.Hexdump, opts.VarHexdump)
}

formatter.printHeader()
formatter.PrintHeader()

for i, event := range log.Events {
if !shouldDisplayEvent(event) {
continue
}

formatter.printEvent(event)
formatter.PrintEvent(event)

if opts.ExtractData != "" {
ioutil.WriteFile(fmt.Sprintf("%s-%d", opts.ExtractData, i), event.Data.Bytes(), 0644)
Expand All @@ -114,7 +114,7 @@ func run() error {
}
}

formatter.flush()
formatter.Flush()

return nil
}
Expand Down