aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@mullvad.net>2022-03-27 15:26:37 +0200
committerRasmus Dahlberg <rasmus@mullvad.net>2022-04-13 15:11:08 +0200
commit30b9e5d5f5a29a3d83f9e16c9b606c903f750acc (patch)
tree6794d53eca7db8eb7938cb450ab99a7826e290f5
parentd86d2433e0da077063bb21dc7c29d491c6fe862f (diff)
add start on sigsum-debug utility
-rw-r--r--cmd/sigsum-debug/genkey.go19
-rw-r--r--cmd/sigsum-debug/hashkey.go24
-rw-r--r--cmd/sigsum-debug/main.go92
-rw-r--r--cmd/sigsum-debug/pubkey.go27
-rw-r--r--cmd/sigsum-debug/util.go24
5 files changed, 186 insertions, 0 deletions
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
+}