-
Notifications
You must be signed in to change notification settings - Fork 158
/
verify.go
227 lines (201 loc) · 6.56 KB
/
verify.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"math/bits"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/transparency-dev/merkle/proof"
"github.com/transparency-dev/merkle/rfc6962"
"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/sharding"
"github.com/sigstore/rekor/pkg/types"
"github.com/sigstore/rekor/pkg/util"
)
type verifyCmdOutput struct {
RootHash string
EntryUUID string
Index int64
Size int64
Hashes []string
Checkpoint string
}
func (v *verifyCmdOutput) String() string {
s := fmt.Sprintf("Current Root Hash: %v\n", v.RootHash)
s += fmt.Sprintf("Entry Hash: %v\n", v.EntryUUID)
s += fmt.Sprintf("Entry Index: %v\n", v.Index)
s += fmt.Sprintf("Current Tree Size: %v\n", v.Size)
if len(v.Checkpoint) > 0 {
s += fmt.Sprintf("Checkpoint:\n%v\n\n", v.Checkpoint)
} else {
s += "\n"
}
s += "Inclusion Proof:\n"
hasher := rfc6962.DefaultHasher
inner := bits.Len64(uint64(v.Index ^ (v.Size - 1)))
var left, right []byte
result, _ := hex.DecodeString(v.EntryUUID)
for i, h := range v.Hashes {
if i < inner && (v.Index>>uint(i))&1 == 0 {
left = result
right, _ = hex.DecodeString(h)
} else {
left, _ = hex.DecodeString(h)
right = result
}
result = hasher.HashChildren(left, right)
s += fmt.Sprintf("SHA256(0x01 | %v | %v) =\n\t%v\n\n",
hex.EncodeToString(left), hex.EncodeToString(right), hex.EncodeToString(result))
}
return s
}
// verifyCmd represents the get command
var verifyCmd = &cobra.Command{
Use: "verify",
Short: "Rekor verify command",
Long: `Verifies an entry exists in the transparency log through an inclusion proof`,
PreRunE: func(cmd *cobra.Command, args []string) error {
// these are bound here so that they are not overwritten by other commands
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return fmt.Errorf("error initializing cmd line args: %s", err)
}
if err := validateArtifactPFlags(true, true); err != nil {
return err
}
return nil
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"), client.WithUserAgent(UserAgent()))
if err != nil {
return nil, err
}
searchParams := entries.NewSearchLogQueryParams()
searchParams.SetTimeout(viper.GetDuration("timeout"))
searchLogQuery := models.SearchLogQuery{}
uuid := viper.GetString("uuid")
logIndex := viper.GetString("log-index")
if uuid != "" {
searchLogQuery.EntryUUIDs = append(searchLogQuery.EntryUUIDs, uuid)
} else if logIndex != "" {
logIndexInt, err := strconv.ParseInt(logIndex, 10, 0)
if err != nil {
return nil, fmt.Errorf("error parsing --log-index: %w", err)
}
searchLogQuery.LogIndexes = []*int64{&logIndexInt}
} else {
typeStr, versionStr, err := ParseTypeFlag(viper.GetString("type"))
if err != nil {
return nil, err
}
props := CreatePropsFromPflags()
entry, err := types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
}
entries := []models.ProposedEntry{entry}
searchLogQuery.SetEntries(entries)
}
searchParams.SetEntry(&searchLogQuery)
resp, err := rekorClient.Entries.SearchLogQuery(searchParams)
if err != nil {
return nil, err
}
if len(resp.Payload) == 0 {
return nil, fmt.Errorf("entry in log cannot be located")
} else if len(resp.Payload) > 1 {
return nil, fmt.Errorf("multiple entries returned; this should not happen")
}
logEntry := resp.Payload[0]
var o *verifyCmdOutput
var entryBytes []byte
for k, v := range logEntry {
o = &verifyCmdOutput{
RootHash: *v.Verification.InclusionProof.RootHash,
EntryUUID: k,
Index: *v.LogIndex,
Size: *v.Verification.InclusionProof.TreeSize,
Hashes: v.Verification.InclusionProof.Hashes,
}
if v.Verification.InclusionProof.Checkpoint != nil {
o.Checkpoint = *v.Verification.InclusionProof.Checkpoint
}
entryBytes, err = base64.StdEncoding.DecodeString(v.Body.(string))
if err != nil {
return nil, err
}
}
if viper.IsSet("uuid") {
uuid, err := sharding.GetUUIDFromIDString(viper.GetString("uuid"))
if err != nil {
return nil, err
}
if uuid != o.EntryUUID {
return nil, fmt.Errorf("unexpected entry returned from rekor server")
}
}
// Note: the returned entry UUID is the UUID (not include the Tree ID)
leafHash, _ := hex.DecodeString(o.EntryUUID)
if !bytes.Equal(rfc6962.DefaultHasher.HashLeaf(entryBytes), leafHash) {
return nil, fmt.Errorf("computed leaf hash did not match entry UUID")
}
hashes := [][]byte{}
for _, h := range o.Hashes {
hb, _ := hex.DecodeString(h)
hashes = append(hashes, hb)
}
rootHash, _ := hex.DecodeString(o.RootHash)
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, uint64(o.Index), uint64(o.Size), leafHash, hashes, rootHash); err != nil {
return nil, err
}
if len(o.Checkpoint) > 0 {
sth := &util.SignedCheckpoint{}
if err := sth.UnmarshalText([]byte(o.Checkpoint)); err != nil {
return nil, fmt.Errorf("unmarshalling o.Checkpoint to SignedCheckpoint: %w", err)
}
verifier, err := loadVerifier(rekorClient)
if err != nil {
return nil, err
}
if !sth.Verify(verifier) {
return nil, errors.New("error verifying signed checkpoint")
}
}
return o, err
}),
}
func init() {
initializePFlagMap()
if err := addArtifactPFlags(verifyCmd); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args:", err)
}
if err := addUUIDPFlags(verifyCmd, false); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args:", err)
}
if err := addLogIndexFlag(verifyCmd, false); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args:", err)
}
rootCmd.AddCommand(verifyCmd)
}