1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cmd
16
17 import (
18 "context"
19 "crypto/sha256"
20 "encoding/base64"
21 "encoding/hex"
22 "fmt"
23
24 "github.com/google/certificate-transparency-go/client"
25 "github.com/spf13/cobra"
26 "github.com/transparency-dev/merkle/proof"
27 "github.com/transparency-dev/merkle/rfc6962"
28 "k8s.io/klog/v2"
29 )
30
31 var (
32 treeHash string
33 prevSize uint64
34 prevHash string
35 )
36
37 func init() {
38 cmd := cobra.Command{
39 Use: fmt.Sprintf("get-consistency-proof %s --size=N --tree_hash=hash --prev_size=N --prev_hash=hash", connectionFlags),
40 Aliases: []string{"getconsistencyproof", "consistency-proof", "consistency"},
41 Short: "Fetch and verify a consistency proof between two tree states",
42 Args: cobra.MaximumNArgs(0),
43 Run: func(cmd *cobra.Command, _ []string) {
44 runGetConsistencyProof(cmd.Context())
45 },
46 }
47
48 cmd.Flags().Uint64Var(&treeSize, "size", 0, "Tree size to query at")
49 cmd.Flags().StringVar(&treeHash, "tree_hash", "", "Tree hash to check against (as hex string or base64)")
50 cmd.Flags().Uint64Var(&prevSize, "prev_size", 0, "Previous tree size to get consistency against")
51 cmd.Flags().StringVar(&prevHash, "prev_hash", "", "Previous tree hash to check against (as hex string or base64)")
52 rootCmd.AddCommand(&cmd)
53 }
54
55
56 func runGetConsistencyProof(ctx context.Context) {
57 logClient := connect(ctx)
58 if treeSize <= 0 {
59 klog.Exit("No valid --size supplied")
60 }
61 if prevSize <= 0 {
62 klog.Exit("No valid --prev_size supplied")
63 }
64 var hash1, hash2 []byte
65 if prevHash != "" {
66 var err error
67 hash1, err = hashFromString(prevHash)
68 if err != nil {
69 klog.Exitf("Invalid --prev_hash: %v", err)
70 }
71 }
72 if treeHash != "" {
73 var err error
74 hash2, err = hashFromString(treeHash)
75 if err != nil {
76 klog.Exitf("Invalid --tree_hash: %v", err)
77 }
78 }
79 if (hash1 != nil) != (hash2 != nil) {
80 klog.Exitf("Need both --prev_hash and --tree_hash or neither")
81 }
82 getConsistencyProofBetween(ctx, logClient, prevSize, treeSize, hash1, hash2)
83 }
84
85 func getConsistencyProofBetween(ctx context.Context, logClient client.CheckLogClient, first, second uint64, prevHash, treeHash []byte) {
86 pf, err := logClient.GetSTHConsistency(ctx, uint64(first), uint64(second))
87 if err != nil {
88 exitWithDetails(err)
89 }
90 fmt.Printf("Consistency proof from size %d to size %d:\n", first, second)
91 for _, e := range pf {
92 fmt.Printf(" %x\n", e)
93 }
94 if prevHash == nil || treeHash == nil {
95 return
96 }
97
98 if err := proof.VerifyConsistency(rfc6962.DefaultHasher, first, second, pf, prevHash, treeHash); err != nil {
99 klog.Exitf("Failed to VerifyConsistency(%x @size=%d, %x @size=%d): %v", prevHash, first, treeHash, second, err)
100 }
101 fmt.Printf("Verified that hash %x @%d + proof = hash %x @%d\n", prevHash, first, treeHash, second)
102 }
103
104 func hashFromString(input string) ([]byte, error) {
105 hash, err := hex.DecodeString(input)
106 if err == nil && len(hash) == sha256.Size {
107 return hash, nil
108 }
109 hash, err = base64.StdEncoding.DecodeString(input)
110 if err == nil && len(hash) == sha256.Size {
111 return hash, nil
112 }
113 return nil, fmt.Errorf("hash value %q failed to parse as 32-byte hex or base64", input)
114 }
115
View as plain text