From 30b9e5d5f5a29a3d83f9e16c9b606c903f750acc Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Sun, 27 Mar 2022 15:26:37 +0200 Subject: add start on sigsum-debug utility --- cmd/sigsum-debug/genkey.go | 19 ++++++++++ cmd/sigsum-debug/hashkey.go | 24 ++++++++++++ cmd/sigsum-debug/main.go | 92 +++++++++++++++++++++++++++++++++++++++++++++ cmd/sigsum-debug/pubkey.go | 27 +++++++++++++ cmd/sigsum-debug/util.go | 24 ++++++++++++ 5 files changed, 186 insertions(+) create mode 100644 cmd/sigsum-debug/genkey.go create mode 100644 cmd/sigsum-debug/hashkey.go create mode 100644 cmd/sigsum-debug/main.go create mode 100644 cmd/sigsum-debug/pubkey.go create mode 100644 cmd/sigsum-debug/util.go (limited to 'cmd') diff --git a/cmd/sigsum-debug/genkey.go b/cmd/sigsum-debug/genkey.go new file mode 100644 index 0000000..87175a5 --- /dev/null +++ b/cmd/sigsum-debug/genkey.go @@ -0,0 +1,19 @@ +package main + +import ( + "crypto/ed25519" + "crypto/rand" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func CmdGenKey() error { + _, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + return fmt.Errorf("failed generating key-pair: %v", err) + } + + fmt.Printf("%s\n", hex.Serialize(priv[:])) + return nil +} diff --git a/cmd/sigsum-debug/hashkey.go b/cmd/sigsum-debug/hashkey.go new file mode 100644 index 0000000..d9b37dd --- /dev/null +++ b/cmd/sigsum-debug/hashkey.go @@ -0,0 +1,24 @@ +package main + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" + "git.sigsum.org/sigsum-lib-go/pkg/types" +) + +func CmdHashKey() error { + b, err := decodeHexFromStdin() + if err != nil { + return err + } + if len(b) != ed25519.PublicKeySize { + return fmt.Errorf("invalid private key: size") + } + pub := ed25519.PublicKey(b) + keyHash := types.HashFn(pub) + + fmt.Printf("%s\n", hex.Serialize(keyHash[:])) + return nil +} diff --git a/cmd/sigsum-debug/main.go b/cmd/sigsum-debug/main.go new file mode 100644 index 0000000..1d9e769 --- /dev/null +++ b/cmd/sigsum-debug/main.go @@ -0,0 +1,92 @@ +// package main provides a tool named sigsum-debug. +// +// Install as follows: +// +// $ go install -ldflags="-X 'main.someVersion=git commit $(git rev-list -1 HEAD)'" +// +// Usage: +// +// $ sigsum-debug help +// +package main + +import ( + "flag" + "fmt" + "log" + "os" +) + +const usage = `sigsum-debug version %s + +Usage: + sigsum-debug help + Outputs a usage message. + + sigsum-debug genkey + Outputs a new private key. + + sigsum-debug pubkey + Reads a private key from stdin and output its public key. + + sigsum-debug hashkey + Reads a public key from stdin and output its key hash. +` + +var ( + someVersion = "unknown" +) + +func main() { + log.SetFlags(0) + + var err error + switch cmd := parseCommand(); cmd.Name() { + case "help": + cmd.Usage() + case "genkey": + err = CmdGenKey() + case "pubkey": + err = CmdPubKey() + case "hashkey": + err = CmdHashKey() + default: + err = fmt.Errorf("invalid command %q, try %q", cmd.Name(), "sigsum help") + } + + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } +} + +func parseCommand() *flag.FlagSet { + args := os.Args + if len(args) < 2 { + args = append(args, "") + } + + fs := flag.NewFlagSet(args[1], flag.ExitOnError) + fs.Usage = func() { + log.Printf(usage, someVersion) + } + registerOptions(fs) + fs.Parse(args[2:]) + return fs +} + +func registerOptions(fs *flag.FlagSet) { + switch cmd := fs.Name(); cmd { + default: + } +} + +func registerStringOption(fs *flag.FlagSet, opt *string, short, long, value string) { + fs.StringVar(opt, short, value, "") + fs.StringVar(opt, long, value, "") +} + +func registerUint64Option(fs *flag.FlagSet, opt *uint64, short, long string, value uint64) { + fs.Uint64Var(opt, short, value, "") + fs.Uint64Var(opt, long, value, "") +} diff --git a/cmd/sigsum-debug/pubkey.go b/cmd/sigsum-debug/pubkey.go new file mode 100644 index 0000000..586d19a --- /dev/null +++ b/cmd/sigsum-debug/pubkey.go @@ -0,0 +1,27 @@ +package main + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func CmdPubKey() error { + b, err := decodeHexFromStdin() + if err != nil { + return err + } + if len(b) != ed25519.PrivateKeySize { + return fmt.Errorf("invalid private key: size") + } + + priv := ed25519.PrivateKey(b) + pub, ok := priv.Public().(ed25519.PublicKey) + if !ok { + return fmt.Errorf("failed converting to public key") + } + + fmt.Printf("%s\n", hex.Serialize(pub[:])) + return nil +} diff --git a/cmd/sigsum-debug/util.go b/cmd/sigsum-debug/util.go new file mode 100644 index 0000000..d7ba6a8 --- /dev/null +++ b/cmd/sigsum-debug/util.go @@ -0,0 +1,24 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func decodeHexFromStdin() ([]byte, error) { + b, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return nil, fmt.Errorf("failed reading stdin: %v", err) + } + + b, err = hex.Deserialize(string(bytes.TrimSpace(b))) + if err != nil { + return nil, fmt.Errorf("invalid private key: %v", err) + } + + return b, nil +} -- cgit v1.2.3