/
keyls.go
53 lines (45 loc) · 1.07 KB
/
keyls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"github.com/folbricht/tpmk"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type keyLsOptions struct {
device string
}
func newKeyLsCommand() *cobra.Command {
var opt keyLsOptions
cmd := &cobra.Command{
Use: "ls",
Short: "List persistent keys",
Long: `List persistent key handles.`,
Example: ` tpmk key ls`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runKeyLs(opt, args)
},
SilenceUsage: true,
}
flags := cmd.Flags()
flags.StringVarP(&opt.device, "device", "d", "/dev/tpmrm0", "TPM device, 'sim' for simulator")
return cmd
}
func runKeyLs(opt keyLsOptions, args []string) error {
// Open device or simulator
dev, err := tpmk.OpenDevice(opt.device)
if err != nil {
return errors.Wrap(err, "opening device")
}
defer dev.Close()
// Get a list of keys
keys, err := tpmk.KeyList(dev)
if err != nil {
return errors.Wrap(err, "reading key list")
}
// Print the key handles in hex notation
for _, handle := range keys {
fmt.Printf("0x%x\n", handle)
}
return nil
}