-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d63262b
commit aefab11
Showing
9 changed files
with
307 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2021 Contributors to the Veraison project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/veraison/corim/corim" | ||
) | ||
|
||
var ( | ||
corimDisplayCorimFile *string | ||
corimDisplayShowTags *bool | ||
) | ||
|
||
var corimDisplayCmd = NewCorimDisplayCmd() | ||
|
||
func NewCorimDisplayCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "display", | ||
Short: "display the content of a CoRIM as JSON", | ||
Long: `display the content of a CoRIM as JSON | ||
Display the contents of the signed CoRIM signed-corim.cbor | ||
cli corim display --corim-file signed-corim.cbor | ||
Display the contents of the signed CoRIM yet-another-signed-corim.cbor and | ||
also unpack any embedded CoRIM and CoSWID | ||
cli corim display --corim-file yet-another-signed-corim.cbor --show-tags | ||
`, | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := checkCorimDisplayArgs(); err != nil { | ||
return err | ||
} | ||
|
||
if err := display(*corimDisplayCorimFile, *corimDisplayShowTags); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
corimDisplayCorimFile = cmd.Flags().StringP("corim-file", "f", "", "a signed CoRIM file (in CBOR format)") | ||
corimDisplayShowTags = cmd.Flags().BoolP("show-tags", "v", false, "display embedded tags") | ||
|
||
return cmd | ||
} | ||
|
||
func checkCorimDisplayArgs() error { | ||
if corimDisplayCorimFile == nil || *corimDisplayCorimFile == "" { | ||
return errors.New("no CoRIM supplied") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func display(signedCorimFile string, showTags bool) error { | ||
var ( | ||
signedCorimCBOR []byte | ||
metaJSON []byte | ||
corimJSON []byte | ||
err error | ||
s corim.SignedCorim | ||
) | ||
|
||
if signedCorimCBOR, err = afero.ReadFile(fs, signedCorimFile); err != nil { | ||
return fmt.Errorf("error loading signed CoRIM from %s: %w", signedCorimFile, err) | ||
} | ||
|
||
if err = s.FromCOSE(signedCorimCBOR); err != nil { | ||
return fmt.Errorf("error decoding signed CoRIM from %s: %w", signedCorimFile, err) | ||
} | ||
|
||
if metaJSON, err = json.MarshalIndent(&s.Meta, "", " "); err != nil { | ||
return fmt.Errorf("error decoding CoRIM Meta from %s: %w", signedCorimFile, err) | ||
} | ||
|
||
fmt.Println("Meta:") | ||
fmt.Println(string(metaJSON)) | ||
|
||
if corimJSON, err = json.MarshalIndent(&s.UnsignedCorim, "", " "); err != nil { | ||
return fmt.Errorf("error decoding unsigned CoRIM from %s: %w", signedCorimFile, err) | ||
} | ||
|
||
fmt.Println("Corim:") | ||
fmt.Println(string(corimJSON)) | ||
|
||
if showTags { | ||
fmt.Println("Tags:") | ||
for i, e := range s.UnsignedCorim.Tags { | ||
var ( | ||
coswidTag = []byte{0xd9, 0x01, 0xf9} // 505() | ||
comidTag = []byte{0xd9, 0x01, 0xfa} // 506() | ||
) | ||
|
||
// need at least 3 bytes for the tag and 1 for the smallest bstr | ||
if len(e) < len(comidTag)+1 { | ||
fmt.Printf(">> skipping malformed tag at index %d\n", i) | ||
continue | ||
} | ||
|
||
// split tag from data | ||
cborTag, cborData := e[:3], e[3:] | ||
|
||
hdr := fmt.Sprintf(">> [ %d ]", i) | ||
|
||
if bytes.Equal(cborTag, comidTag) { | ||
if err = printComid(cborData, hdr); err != nil { | ||
fmt.Printf(">> skipping malformed CoMID tag at index %d: %v\n", i, err) | ||
} | ||
} else if bytes.Equal(cborTag, coswidTag) { | ||
if err = printCoswid(cborData, hdr); err != nil { | ||
fmt.Printf(">> skipping malformed CoSWID tag at index %d: %v\n", i, err) | ||
} | ||
} else { | ||
fmt.Printf("unmatched CBOR tag: %x\n", e[:2]) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
corimCmd.AddCommand(corimDisplayCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2021 Contributors to the Veraison project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_CorimDisplayCmd_unknown_argument(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{"--unknown-argument=val"} | ||
cmd.SetArgs(args) | ||
|
||
err := cmd.Execute() | ||
assert.EqualError(t, err, "unknown flag: --unknown-argument") | ||
} | ||
|
||
func Test_CorimDisplayCmd_mandatory_args_missing_corim_file(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--show-tags", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
err := cmd.Execute() | ||
assert.EqualError(t, err, "no CoRIM supplied") | ||
} | ||
|
||
func Test_CorimDisplayCmd_non_existent_corim_file(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--corim-file=nonexistent.cbor", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
fs = afero.NewMemMapFs() | ||
|
||
err := cmd.Execute() | ||
assert.EqualError(t, err, "error loading signed CoRIM from nonexistent.cbor: open nonexistent.cbor: file does not exist") | ||
} | ||
|
||
func Test_CorimDisplayCmd_bad_signed_corim(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--corim-file=bad.txt", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
fs = afero.NewMemMapFs() | ||
err := afero.WriteFile(fs, "bad.txt", []byte("hello!"), 0644) | ||
require.NoError(t, err) | ||
|
||
err = cmd.Execute() | ||
assert.EqualError(t, err, "error decoding signed CoRIM from bad.txt: failed CBOR decoding for COSE-Sign1 signed CoRIM: unexpected EOF") | ||
} | ||
|
||
func Test_CorimDisplayCmd_invalid_signed_corim(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--corim-file=invalid.cbor", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
fs = afero.NewMemMapFs() | ||
err := afero.WriteFile(fs, "invalid.cbor", testSignedCorimInvalid, 0644) | ||
require.NoError(t, err) | ||
|
||
err = cmd.Execute() | ||
assert.EqualError(t, err, "error decoding signed CoRIM from invalid.cbor: failed validation of unsigned CoRIM: empty id") | ||
} | ||
|
||
func Test_CorimDisplayCmd_ok_top_level_view(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--corim-file=ok.cbor", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
fs = afero.NewMemMapFs() | ||
err := afero.WriteFile(fs, "ok.cbor", testSignedCorimValid, 0644) | ||
require.NoError(t, err) | ||
|
||
err = cmd.Execute() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func Test_CorimDisplayCmd_ok_nested_view(t *testing.T) { | ||
cmd := NewCorimDisplayCmd() | ||
|
||
args := []string{ | ||
"--corim-file=ok.cbor", | ||
"--show-tags", | ||
} | ||
cmd.SetArgs(args) | ||
|
||
fs = afero.NewMemMapFs() | ||
err := afero.WriteFile(fs, "ok.cbor", testSignedCorimValid, 0644) | ||
require.NoError(t, err) | ||
|
||
err = cmd.Execute() | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.