Skip to content

Commit 300d5a9

Browse files
committed
Add new command: imgmod key show
show - Displays JSON describing one or more keys Usage: imgmod key show <key-file> [key-files...] [flags]
1 parent 02566d0 commit 300d5a9

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

cli/key_cmds.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package cli
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
10+
"github.com/apache/mynewt-artifact/errors"
11+
"github.com/spf13/cobra"
12+
13+
"mynewt.apache.org/imgmod/ikey"
14+
)
15+
16+
func keyDescToJson(path string, body []byte, desc ikey.Desc) (string, error) {
17+
type Key struct {
18+
Path string `json:"path"`
19+
Type string `json:"type"`
20+
Algorithm string `json:"algorithm"`
21+
Hash string `json:"hash"`
22+
FileSha256 string `json:"file_sha256"`
23+
}
24+
25+
var typ string
26+
if desc.Private {
27+
typ = "private"
28+
} else {
29+
typ = "public"
30+
}
31+
32+
h := sha256.Sum256(body)
33+
fileHash := h[:]
34+
35+
k := Key{
36+
Path: path,
37+
Type: typ,
38+
Algorithm: desc.Algorithm,
39+
Hash: hex.EncodeToString(desc.Hash),
40+
FileSha256: hex.EncodeToString(fileHash),
41+
}
42+
43+
j, err := json.MarshalIndent(k, "", " ")
44+
if err != nil {
45+
return "", errors.Wrapf(err,
46+
"internal error: failed to marshal key description")
47+
}
48+
49+
return string(j), nil
50+
}
51+
52+
func runKeyShowCmd(cmd *cobra.Command, args []string) {
53+
if len(args) < 1 {
54+
ImgmodUsage(cmd, nil)
55+
}
56+
57+
for i, arg := range args {
58+
bin, err := ioutil.ReadFile(arg)
59+
if err != nil {
60+
ImgmodUsage(nil, err)
61+
}
62+
63+
desc, err := ikey.KeyBytesToDesc(bin)
64+
if err != nil {
65+
ImgmodUsage(nil, errors.Wrapf(err, "file: \"%s\"", arg))
66+
}
67+
68+
j, err := keyDescToJson(arg, bin, desc)
69+
if err != nil {
70+
ImgmodUsage(nil, err)
71+
}
72+
73+
fmt.Printf("%s", j)
74+
if i < len(args)-1 {
75+
fmt.Printf(",")
76+
}
77+
fmt.Printf("\n")
78+
}
79+
}
80+
81+
func AddKeyCommands(cmd *cobra.Command) {
82+
keyCmd := &cobra.Command{
83+
Use: "key",
84+
Short: "Manipulates image keys",
85+
Run: func(cmd *cobra.Command, args []string) {
86+
cmd.Usage()
87+
},
88+
}
89+
cmd.AddCommand(keyCmd)
90+
91+
showCmd := &cobra.Command{
92+
Use: "show <key-file> [key-files...]",
93+
Short: "Displays JSON describing one or more keys",
94+
Run: runKeyShowCmd,
95+
}
96+
97+
keyCmd.AddCommand(showCmd)
98+
}

ikey/ikey.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ikey
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/apache/mynewt-artifact/sec"
7+
"github.com/pkg/errors"
8+
)
9+
10+
type Desc struct {
11+
Private bool
12+
Algorithm string
13+
PubBytes []byte
14+
Hash []byte
15+
}
16+
17+
func signKeyToDesc(key sec.PubSignKey, private bool) (Desc, error) {
18+
var alg string
19+
if key.Rsa != nil {
20+
alg = fmt.Sprintf("RSA-%d", key.Rsa.Size()*8)
21+
} else if key.Ec != nil {
22+
alg = fmt.Sprintf("ECDSA-%d", key.Ec.X.BitLen())
23+
} else {
24+
alg = "ED25519"
25+
}
26+
27+
pubBytes, err := key.Bytes()
28+
if err != nil {
29+
return Desc{}, err
30+
}
31+
32+
return Desc{
33+
Private: private,
34+
Algorithm: alg,
35+
PubBytes: pubBytes,
36+
Hash: sec.RawKeyHash(pubBytes),
37+
}, nil
38+
}
39+
40+
func KeyBytesToDesc(keyBytes []byte) (Desc, error) {
41+
pubsk, err := sec.ParsePubSignKey(keyBytes)
42+
if err == nil {
43+
return signKeyToDesc(pubsk, false)
44+
}
45+
46+
privsk, err := sec.ParsePrivSignKey(keyBytes)
47+
if err == nil {
48+
return signKeyToDesc(privsk.PubKey(), true)
49+
}
50+
51+
return Desc{}, errors.Errorf("unrecognized key type")
52+
}

imgmod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func main() {
7575

7676
cli.AddImageCommands(imgmodCmd)
7777
cli.AddMfgCommands(imgmodCmd)
78+
cli.AddKeyCommands(imgmodCmd)
7879

7980
imgmodCmd.Execute()
8081
}

0 commit comments

Comments
 (0)