diff --git a/tcglog-dump/block.go b/block.go similarity index 77% rename from tcglog-dump/block.go rename to block.go index 7c428df..eec495f 100644 --- a/tcglog-dump/block.go +++ b/block.go @@ -2,7 +2,7 @@ // Licensed under the LGPLv3 with static-linking exception. // See LICENCE file for details. -package main +package tcglog import ( "encoding/hex" @@ -12,8 +12,6 @@ import ( "strings" "github.com/canonical/go-tpm2" - - "github.com/canonical/tcglog-parser" ) type blockFormatter struct { @@ -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{ @@ -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, diff --git a/tcglog-dump/details.go b/details.go similarity index 82% rename from tcglog-dump/details.go rename to details.go index 22a3662..3980f22 100644 --- a/tcglog-dump/details.go +++ b/details.go @@ -2,7 +2,7 @@ // Licensed under the LGPLv3 with static-linking exception. // See LICENCE file for details. -package main +package tcglog import ( "bytes" @@ -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}) @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 { diff --git a/formatter.go b/formatter.go new file mode 100644 index 0000000..2b3e137 --- /dev/null +++ b/formatter.go @@ -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() +} diff --git a/go.mod b/go.mod index b3ad72d..799eb93 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c2741b1..3e681de 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/tcglog-dump/table.go b/table.go similarity index 85% rename from tcglog-dump/table.go rename to table.go index 7519fc6..6c82b1d 100644 --- a/tcglog-dump/table.go +++ b/table.go @@ -2,7 +2,7 @@ // Licensed under the LGPLv3 with static-linking exception. // See LICENCE file for details. -package main +package tcglog import ( "bytes" @@ -15,8 +15,6 @@ import ( "github.com/canonical/go-tpm2" "golang.org/x/sys/unix" - - "github.com/canonical/tcglog-parser" ) type terminalWriter struct { @@ -80,7 +78,7 @@ 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") @@ -88,19 +86,19 @@ func (f *tableFormatter) printHeader() { 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) diff --git a/tcglog-dump/formatter.go b/tcglog-dump/formatter.go deleted file mode 100644 index 1efee60..0000000 --- a/tcglog-dump/formatter.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2022 Canonical Ltd. -// Licensed under the LGPLv3 with static-linking exception. -// See LICENCE file for details. - -package main - -import ( - "github.com/canonical/tcglog-parser" -) - -type formatter interface { - printHeader() - printEvent(event *tcglog.Event) - flush() -} diff --git a/tcglog-dump/main.go b/tcglog-dump/main.go index afe95e9..6a20a8d 100644 --- a/tcglog-dump/main.go +++ b/tcglog-dump/main.go @@ -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) @@ -114,7 +114,7 @@ func run() error { } } - formatter.flush() + formatter.Flush() return nil }