-
Notifications
You must be signed in to change notification settings - Fork 549
/
tlog.go
444 lines (402 loc) · 13.9 KB
/
tlog.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// 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 cosign
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"hash"
"os"
"strconv"
"strings"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/transparency-dev/merkle/proof"
"github.com/transparency-dev/merkle/rfc6962"
"github.com/sigstore/cosign/pkg/cosign/bundle"
"github.com/sigstore/cosign/pkg/cosign/env"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/types"
hashedrekord_v001 "github.com/sigstore/rekor/pkg/types/hashedrekord/v0.0.1"
"github.com/sigstore/rekor/pkg/types/intoto"
intoto_v001 "github.com/sigstore/rekor/pkg/types/intoto/v0.0.1"
"github.com/sigstore/sigstore/pkg/tuf"
)
// This is the rekor public key target name
var rekorTargetStr = `rekor.pub`
// RekorPubKey contains the ECDSA verification key and the current status
// of the key according to TUF metadata, whether it's active or expired.
type RekorPubKey struct {
PubKey *ecdsa.PublicKey
Status tuf.StatusKind
}
const treeIDHexStringLen = 16
const uuidHexStringLen = 64
const entryIDHexStringLen = treeIDHexStringLen + uuidHexStringLen
// getLogID generates a SHA256 hash of a DER-encoded public key.
func getLogID(pub crypto.PublicKey) (string, error) {
pubBytes, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return "", err
}
digest := sha256.Sum256(pubBytes)
return hex.EncodeToString(digest[:]), nil
}
func intotoEntry(ctx context.Context, signature, pubKey []byte) (models.ProposedEntry, error) {
var pubKeyBytes [][]byte
if len(pubKey) == 0 {
return nil, errors.New("none of the Rekor public keys have been found")
}
pubKeyBytes = append(pubKeyBytes, pubKey)
return types.NewProposedEntry(ctx, intoto.KIND, intoto_v001.APIVERSION, types.ArtifactProperties{
ArtifactBytes: signature,
PublicKeyBytes: pubKeyBytes,
})
}
// GetRekorPubs retrieves trusted Rekor public keys from the embedded or cached
// TUF root. If expired, makes a network call to retrieve the updated targets.
// There are two Env variable that can be used to override this behaviour:
// SIGSTORE_REKOR_PUBLIC_KEY - If specified, location of the file that contains
// the Rekor Public Key on local filesystem
func GetRekorPubs(ctx context.Context, _ *client.Rekor) (map[string]RekorPubKey, error) {
publicKeys := make(map[string]RekorPubKey)
altRekorPub := env.Getenv(env.VariableSigstoreRekorPublicKey)
if altRekorPub != "" {
raw, err := os.ReadFile(altRekorPub)
if err != nil {
return nil, fmt.Errorf("error reading alternate Rekor public key file: %w", err)
}
extra, err := PemToECDSAKey(raw)
if err != nil {
return nil, fmt.Errorf("error converting PEM to ECDSAKey: %w", err)
}
keyID, err := getLogID(extra)
if err != nil {
return nil, fmt.Errorf("error generating log ID: %w", err)
}
publicKeys[keyID] = RekorPubKey{PubKey: extra, Status: tuf.Active}
} else {
tufClient, err := tuf.NewFromEnv(ctx)
if err != nil {
return nil, err
}
targets, err := tufClient.GetTargetsByMeta(tuf.Rekor, []string{rekorTargetStr})
if err != nil {
return nil, err
}
for _, t := range targets {
rekorPubKey, err := PemToECDSAKey(t.Target)
if err != nil {
return nil, fmt.Errorf("pem to ecdsa: %w", err)
}
keyID, err := getLogID(rekorPubKey)
if err != nil {
return nil, fmt.Errorf("error generating log ID: %w", err)
}
publicKeys[keyID] = RekorPubKey{PubKey: rekorPubKey, Status: t.Status}
}
}
if len(publicKeys) == 0 {
return nil, errors.New("none of the Rekor public keys have been found")
}
return publicKeys, nil
}
// TLogUpload will upload the signature, public key and payload to the transparency log.
func TLogUpload(ctx context.Context, rekorClient *client.Rekor, signature []byte, sha256CheckSum hash.Hash, pemBytes []byte) (*models.LogEntryAnon, error) {
re := rekorEntry(sha256CheckSum, signature, pemBytes)
returnVal := models.Hashedrekord{
APIVersion: swag.String(re.APIVersion()),
Spec: re.HashedRekordObj,
}
return doUpload(ctx, rekorClient, &returnVal)
}
// TLogUploadInTotoAttestation will upload and in-toto entry for the signature and public key to the transparency log.
func TLogUploadInTotoAttestation(ctx context.Context, rekorClient *client.Rekor, signature, pemBytes []byte) (*models.LogEntryAnon, error) {
e, err := intotoEntry(ctx, signature, pemBytes)
if err != nil {
return nil, err
}
return doUpload(ctx, rekorClient, e)
}
func doUpload(ctx context.Context, rekorClient *client.Rekor, pe models.ProposedEntry) (*models.LogEntryAnon, error) {
params := entries.NewCreateLogEntryParamsWithContext(ctx)
params.SetProposedEntry(pe)
resp, err := rekorClient.Entries.CreateLogEntry(params)
if err != nil {
// If the entry already exists, we get a specific error.
// Here, we display the proof and succeed.
var existsErr *entries.CreateLogEntryConflict
if errors.As(err, &existsErr) {
fmt.Println("Signature already exists. Displaying proof")
uriSplit := strings.Split(existsErr.Location.String(), "/")
uuid := uriSplit[len(uriSplit)-1]
e, err := GetTlogEntry(ctx, rekorClient, uuid)
if err != nil {
return nil, err
}
return e, VerifyTLogEntry(ctx, nil, e)
}
return nil, err
}
// UUID is at the end of location
for _, p := range resp.Payload {
return &p, nil
}
return nil, errors.New("bad response from server")
}
func rekorEntry(sha256CheckSum hash.Hash, signature, pubKey []byte) hashedrekord_v001.V001Entry {
// TODO: Signatures created on a digest using a hash algorithm other than SHA256 will fail
// upload right now. Plumb information on the hash algorithm used when signing from the
// SignerVerifier to use for the HashedRekordObj.Data.Hash.Algorithm.
return hashedrekord_v001.V001Entry{
HashedRekordObj: models.HashedrekordV001Schema{
Data: &models.HashedrekordV001SchemaData{
Hash: &models.HashedrekordV001SchemaDataHash{
Algorithm: swag.String(models.HashedrekordV001SchemaDataHashAlgorithmSha256),
Value: swag.String(hex.EncodeToString(sha256CheckSum.Sum(nil))),
},
},
Signature: &models.HashedrekordV001SchemaSignature{
Content: strfmt.Base64(signature),
PublicKey: &models.HashedrekordV001SchemaSignaturePublicKey{
Content: strfmt.Base64(pubKey),
},
},
},
}
}
func ComputeLeafHash(e *models.LogEntryAnon) ([]byte, error) {
entryBytes, err := base64.StdEncoding.DecodeString(e.Body.(string))
if err != nil {
return nil, err
}
return rfc6962.DefaultHasher.HashLeaf(entryBytes), nil
}
func getUUID(entryUUID string) (string, error) {
switch len(entryUUID) {
case uuidHexStringLen:
if _, err := hex.DecodeString(entryUUID); err != nil {
return "", fmt.Errorf("uuid %v is not a valid hex string: %w", entryUUID, err)
}
return entryUUID, nil
case entryIDHexStringLen:
uid := entryUUID[len(entryUUID)-uuidHexStringLen:]
return getUUID(uid)
default:
return "", fmt.Errorf("invalid ID len %v for %v", len(entryUUID), entryUUID)
}
}
func getTreeUUID(entryUUID string) (string, error) {
switch len(entryUUID) {
case uuidHexStringLen:
// No Tree ID provided
return "", nil
case entryIDHexStringLen:
tid := entryUUID[:treeIDHexStringLen]
return getTreeUUID(tid)
case treeIDHexStringLen:
// Check that it's a valid int64 in hex (base 16)
i, err := strconv.ParseInt(entryUUID, 16, 64)
if err != nil {
return "", fmt.Errorf("could not convert treeID %v to int64: %w", entryUUID, err)
}
// Check for invalid TreeID values
if i == 0 {
return "", fmt.Errorf("0 is not a valid TreeID")
}
return entryUUID, nil
default:
return "", fmt.Errorf("invalid ID len %v for %v", len(entryUUID), entryUUID)
}
}
// Validates UUID and also TreeID if present.
func isExpectedResponseUUID(requestEntryUUID string, responseEntryUUID string, treeid string) error {
// Comparare UUIDs
requestUUID, err := getUUID(requestEntryUUID)
if err != nil {
return err
}
responseUUID, err := getUUID(responseEntryUUID)
if err != nil {
return err
}
if requestUUID != responseUUID {
return fmt.Errorf("expected EntryUUID %s got UUID %s", requestEntryUUID, responseEntryUUID)
}
// Compare tree ID if it is in the request.
requestTreeID, err := getTreeUUID(requestEntryUUID)
if err != nil {
return err
}
if requestTreeID != "" {
tid, err := getTreeUUID(treeid)
if err != nil {
return err
}
if requestTreeID != tid {
return fmt.Errorf("expected EntryUUID %s got UUID %s from Tree %s", requestEntryUUID, responseEntryUUID, treeid)
}
}
return nil
}
func verifyUUID(entryUUID string, e models.LogEntryAnon) error {
// Verify and get the UUID.
uid, err := getUUID(entryUUID)
if err != nil {
return err
}
uuid, _ := hex.DecodeString(uid)
// Verify leaf hash matches hash of the entry body.
computedLeafHash, err := ComputeLeafHash(&e)
if err != nil {
return err
}
if !bytes.Equal(computedLeafHash, uuid) {
return fmt.Errorf("computed leaf hash did not match UUID")
}
return nil
}
func GetTlogEntry(ctx context.Context, rekorClient *client.Rekor, entryUUID string) (*models.LogEntryAnon, error) {
params := entries.NewGetLogEntryByUUIDParamsWithContext(ctx)
params.SetEntryUUID(entryUUID)
resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
if err != nil {
return nil, err
}
for k, e := range resp.Payload {
// Validate that request EntryUUID matches the response UUID and response Tree ID
if err := isExpectedResponseUUID(entryUUID, k, *e.LogID); err != nil {
return nil, fmt.Errorf("unexpected entry returned from rekor server: %w", err)
}
// Check that body hash matches UUID
if err := verifyUUID(k, e); err != nil {
return nil, err
}
return &e, nil
}
return nil, errors.New("empty response")
}
func proposedEntry(b64Sig string, payload, pubKey []byte) ([]models.ProposedEntry, error) {
var proposedEntry []models.ProposedEntry
signature, err := base64.StdEncoding.DecodeString(b64Sig)
if err != nil {
return nil, fmt.Errorf("decoding base64 signature: %w", err)
}
// The fact that there's no signature (or empty rather), implies
// that this is an Attestation that we're verifying.
if len(signature) == 0 {
e, err := intotoEntry(context.Background(), payload, pubKey)
if err != nil {
return nil, err
}
proposedEntry = []models.ProposedEntry{e}
} else {
sha256CheckSum := sha256.New()
if _, err := sha256CheckSum.Write(payload); err != nil {
return nil, err
}
re := rekorEntry(sha256CheckSum, signature, pubKey)
entry := &models.Hashedrekord{
APIVersion: swag.String(re.APIVersion()),
Spec: re.HashedRekordObj,
}
proposedEntry = []models.ProposedEntry{entry}
}
return proposedEntry, nil
}
func FindTlogEntry(ctx context.Context, rekorClient *client.Rekor,
b64Sig string, payload, pubKey []byte) ([]models.LogEntryAnon, error) {
searchParams := entries.NewSearchLogQueryParamsWithContext(ctx)
searchLogQuery := models.SearchLogQuery{}
proposedEntry, err := proposedEntry(b64Sig, payload, pubKey)
if err != nil {
return nil, err
}
searchLogQuery.SetEntries(proposedEntry)
searchParams.SetEntry(&searchLogQuery)
resp, err := rekorClient.Entries.SearchLogQuery(searchParams)
if err != nil {
return nil, fmt.Errorf("searching log query: %w", err)
}
if len(resp.Payload) == 0 {
return nil, errors.New("signature not found in transparency log")
}
// This may accumulate multiple entries on multiple tree IDs.
results := make([]models.LogEntryAnon, 0)
for _, logEntry := range resp.GetPayload() {
for k, e := range logEntry {
// Check body hash matches uuid
if err := verifyUUID(k, e); err != nil {
continue
}
results = append(results, e)
}
}
return results, nil
}
// VerityTLogEntry verifies a TLog entry.
// The argument *client.Rekor is unused and may be nil.
func VerifyTLogEntry(ctx context.Context, _ *client.Rekor, e *models.LogEntryAnon) error {
if e.Verification == nil || e.Verification.InclusionProof == nil {
return errors.New("inclusion proof not provided")
}
hashes := [][]byte{}
for _, h := range e.Verification.InclusionProof.Hashes {
hb, _ := hex.DecodeString(h)
hashes = append(hashes, hb)
}
rootHash, _ := hex.DecodeString(*e.Verification.InclusionProof.RootHash)
entryBytes, err := base64.StdEncoding.DecodeString(e.Body.(string))
if err != nil {
return err
}
leafHash := rfc6962.DefaultHasher.HashLeaf(entryBytes)
// Verify the inclusion proof.
if err := proof.VerifyInclusion(rfc6962.DefaultHasher, uint64(*e.Verification.InclusionProof.LogIndex), uint64(*e.Verification.InclusionProof.TreeSize),
leafHash, hashes, rootHash); err != nil {
return fmt.Errorf("verifying inclusion proof: %w", err)
}
// Verify rekor's signature over the SET.
payload := bundle.RekorPayload{
Body: e.Body,
IntegratedTime: *e.IntegratedTime,
LogIndex: *e.LogIndex,
LogID: *e.LogID,
}
rekorPubKeys, err := GetRekorPubs(ctx, nil)
if err != nil {
return fmt.Errorf("unable to fetch Rekor public keys: %w", err)
}
pubKey, ok := rekorPubKeys[payload.LogID]
if !ok {
return errors.New("rekor log public key not found for payload")
}
err = VerifySET(payload, []byte(e.Verification.SignedEntryTimestamp), pubKey.PubKey)
if err != nil {
return fmt.Errorf("verifying signedEntryTimestamp: %w", err)
}
if pubKey.Status != tuf.Active {
fmt.Fprintf(os.Stderr, "**Info** Successfully verified Rekor entry using an expired verification key\n")
}
return nil
}