aboutsummaryrefslogtreecommitdiff
path: root/trillian.go
blob: f358d4d73cfe0022ea95608179cd8cbe57f93059 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package stfe

import (
	"fmt"

	"github.com/golang/glog"
	"github.com/google/trillian"
	"github.com/google/trillian/types"
	stfetypes "github.com/system-transparency/stfe/types"
	"google.golang.org/grpc/codes"
)

func checkQueueLeaf(rsp *trillian.QueueLeafResponse, err error) error {
	if err != nil {
		return fmt.Errorf("Trillian error: %v", err)
	}
	if rsp == nil {
		return fmt.Errorf("Trillian error: empty response")
	}
	if rsp.QueuedLeaf == nil {
		return fmt.Errorf("Trillian error: empty QueuedLeaf")
	}
	if codes.Code(rsp.QueuedLeaf.GetStatus().GetCode()) == codes.AlreadyExists {
		glog.V(3).Infof("queued leaf is a duplicate => %X", rsp.QueuedLeaf.Leaf.LeafValue)
	}
	return nil
}

func checkGetLeavesByRange(req *stfetypes.LeavesRequest, rsp *trillian.GetLeavesByRangeResponse, err error) error {
	if err != nil {
		return fmt.Errorf("Trillian Error: %v", err)
	}
	if rsp == nil {
		return fmt.Errorf("Trillian error: empty response")
	}
	if rsp.SignedLogRoot == nil {
		return fmt.Errorf("Trillian error: no signed log root")
	}
	if rsp.SignedLogRoot.LogRoot == nil {
		return fmt.Errorf("Trillian error: no log root")
	}
	if len(rsp.Leaves) == 0 {
		return fmt.Errorf("Trillian error: no leaves")
	}
	if len(rsp.Leaves) > int(req.EndSize-req.StartSize+1) {
		return fmt.Errorf("too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.StartSize, req.EndSize)
	}

	// Ensure that a bad start parameter results in an error
	var lr types.LogRootV1
	if err := lr.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil {
		return fmt.Errorf("cannot unmarshal log root: %v", err)
	}
	if uint64(req.StartSize) >= lr.TreeSize {
		return fmt.Errorf("invalid start(%d): tree size is %d", req.StartSize, lr.TreeSize)
	}

	// Ensure that we got and return expected leaf indices
	for i, leaf := range rsp.Leaves {
		if got, want := leaf.LeafIndex, int64(req.StartSize+uint64(i)); got != want {
			return fmt.Errorf("invalid leaf index(%d): wanted %d", got, want)
		}
	}
	return nil
}

func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) error {
	if err != nil {
		return fmt.Errorf("Trillian Error: %v", err)
	}
	if rsp == nil {
		return fmt.Errorf("Trillian error: empty response")
	}
	if len(rsp.Proof) == 0 {
		return fmt.Errorf("Trillian error: no proofs")
	}
	if rsp.Proof[0] == nil {
		return fmt.Errorf("Trillian error: no proof")
	}
	return checkHashPath(lp.HashType.Size(), rsp.Proof[0].Hashes)
}

func checkGetConsistencyProof(lp *LogParameters, rsp *trillian.GetConsistencyProofResponse, err error) error {
	if err != nil {
		return fmt.Errorf("Trillian Error: %v", err)
	}
	if rsp == nil {
		return fmt.Errorf("Trillian error: empty response")
	}
	if rsp.Proof == nil {
		return fmt.Errorf("Trillian error: no proof")
	}
	return checkHashPath(lp.HashType.Size(), rsp.Proof.Hashes)
}

func checkGetLatestSignedLogRoot(lp *LogParameters, rsp *trillian.GetLatestSignedLogRootResponse, err error, out *types.LogRootV1) error {
	if err != nil {
		return fmt.Errorf("Trillian Error: %v", err)
	}
	if rsp == nil {
		return fmt.Errorf("Trillian error: empty response")
	}
	if rsp.SignedLogRoot == nil {
		return fmt.Errorf("Trillian error: no signed log root")
	}
	if rsp.SignedLogRoot.LogRoot == nil {
		return fmt.Errorf("Trillian error: no log root")
	}
	if err := out.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil {
		return fmt.Errorf("cannot unmarshal log root: %v", err)
	}
	if len(out.RootHash) != lp.HashType.Size() {
		return fmt.Errorf("invalid root hash: %v", out.RootHash)
	}
	return nil
}

func checkHashPath(hashSize int, path [][]byte) error {
	for _, hash := range path {
		if len(hash) != hashSize {
			return fmt.Errorf("invalid proof: %v", path)
		}
	}
	return nil
}