aboutsummaryrefslogtreecommitdiff
path: root/cmd/sigsum-debug/head/head.go
blob: 22b857b14ca03bdb3c1f7dd6d977b291632984b3 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package head

import (
	"flag"
	"fmt"
	"log"

	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/head/consistency"
	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/head/sign"
	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/head/verify"
	"git.sigsum.org/sigsum-go/internal/options"
)

const usage = `
sigsum-debug head signs and verifies tree heads.

Usage:

  sigsum-debug head help
    Outputs a usage message

  sigsum-debug head sign -k PRIVATE_KEY -h KEY_HASH
    Reads an ascii signed tree head from stdin and outputs a new signature

  sigsum-debug head verify -k PUBLIC_KEY
    Reads an ascii signed tree head from stdin and verifies it

  sigsum-debug head consistency -n OLD_SIZE -N NEW_SIZE -r OLD_ROOT -R NEW_ROOT
    Reads an ascii consistency proof from stdin and verifies it

`

var (
	optPrivateKey, optPublicKey, optKeyHash, optOldRoot, optNewRoot string
	optOldSize, optNewSize                                          uint64
)

func Main(args []string) error {
	var err error

	opt := options.New(args, func() { log.Printf(usage[1:]) }, setOptions)
	err = checkOptions(opt.Name())
	if err == nil {
		switch opt.Name() {
		case "help", "":
			opt.Usage()
		case "sign":
			err = sign.Main(opt.Args(), optPrivateKey, optKeyHash)
		case "verify":
			err = verify.Main(opt.Args(), optPublicKey)
		case "consistency":
			err = consistency.Main(opt.Args(), optOldSize, optNewSize, optOldRoot, optNewRoot)
		default:
			err = fmt.Errorf("invalid command %q, try \"help\"", opt.Name())
		}
	}
	if err != nil {
		format := " %s: %w"
		if len(opt.Name()) == 0 {
			format = "%s: %w"
		}
		err = fmt.Errorf(format, opt.Name(), err)
	}

	return err
}

func setOptions(fs *flag.FlagSet) {
	switch cmd := fs.Name(); cmd {
	case "help":
	case "sign":
		options.AddString(fs, &optPrivateKey, "k", "private-key", options.DefaultString)
		options.AddString(fs, &optKeyHash, "h", "key-hash", options.DefaultString)
	case "verify":
		options.AddString(fs, &optPublicKey, "k", "public-key", options.DefaultString)
	case "consistency":
		options.AddUint64(fs, &optOldSize, "n", "old-size", options.DefaultUint64)
		options.AddUint64(fs, &optNewSize, "N", "new-size", options.DefaultUint64)
		options.AddString(fs, &optOldRoot, "r", "old-root", options.DefaultString)
		options.AddString(fs, &optNewRoot, "R", "new-root", options.DefaultString)
	}
}

// checkOptions checks that options with required arguments were set
func checkOptions(cmd string) error {
	var err error

	switch cmd {
	case "help":
	case "sign":
		err = options.CheckString("private key", optPrivateKey, err)
		err = options.CheckString("key hash", optKeyHash, err)
	case "verify":
		err = options.CheckString("public key", optPublicKey, err)
	case "consistency":
		err = options.CheckUint64("old size", optOldSize, err)
		err = options.CheckUint64("new size", optNewSize, err)
		err = options.CheckString("old root", optOldRoot, err)
		err = options.CheckString("new root", optNewRoot, err)
	}

	return err
}