-
Notifications
You must be signed in to change notification settings - Fork 26
/
tlog.go
202 lines (170 loc) · 6.06 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
// Copyright 2023 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 verify
import (
"bytes"
"context"
"crypto"
"encoding/hex"
"errors"
"fmt"
rekorClient "github.com/sigstore/rekor/pkg/client"
rekorGeneratedClient "github.com/sigstore/rekor/pkg/generated/client"
rekorEntries "github.com/sigstore/rekor/pkg/generated/client/entries"
rekorVerify "github.com/sigstore/rekor/pkg/verify"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/tlog"
"github.com/sigstore/sigstore-go/pkg/util"
)
const maxAllowedTlogEntries = 32
var RekorClientGetter = getRekorClient
// VerifyArtifactTransparencyLog verifies that the given entity has been logged
// in the transparency log and that the log entry is valid.
//
// The threshold parameter is the number of unique transparency log entries
// that must be verified.
//
// If online is true, the log entry is verified against the Rekor server.
func VerifyArtifactTransparencyLog(entity SignedEntity, trustedMaterial root.TrustedMaterial, logThreshold int, trustIntegratedTime, online bool) ([]root.Timestamp, error) { //nolint:revive
entries, err := entity.TlogEntries()
if err != nil {
return nil, err
}
// limit the number of tlog entries to prevent DoS
if len(entries) > maxAllowedTlogEntries {
return nil, fmt.Errorf("too many tlog entries: %d > %d", len(entries), maxAllowedTlogEntries)
}
// disallow duplicate entries, as a malicious actor could use duplicates to bypass the threshold
for i := 0; i < len(entries); i++ {
for j := i + 1; j < len(entries); j++ {
if entries[i].LogKeyID() == entries[j].LogKeyID() && entries[i].LogIndex() == entries[j].LogIndex() {
return nil, errors.New("duplicate tlog entries found")
}
}
}
sigContent, err := entity.SignatureContent()
if err != nil {
return nil, err
}
entitySignature := sigContent.Signature()
verificationContent, err := entity.VerificationContent()
if err != nil {
return nil, err
}
verifiedTimestamps := []root.Timestamp{}
logEntriesVerified := 0
for _, entry := range entries {
err := tlog.ValidateEntry(entry)
if err != nil {
return nil, err
}
rekorLogs := trustedMaterial.RekorLogs()
keyID := entry.LogKeyID()
hex64Key := hex.EncodeToString([]byte(keyID))
tlogVerifier, ok := trustedMaterial.RekorLogs()[hex64Key]
if !ok {
// skip entries the trust root cannot verify
continue
}
if !online {
if !entry.HasInclusionPromise() && !entry.HasInclusionProof() {
return nil, fmt.Errorf("entry must contain an inclusion proof and/or promise")
}
if entry.HasInclusionPromise() {
err = tlog.VerifySET(entry, rekorLogs)
if err != nil {
// skip entries the trust root cannot verify
continue
}
if trustIntegratedTime {
verifiedTimestamps = append(verifiedTimestamps, root.Timestamp{Time: entry.IntegratedTime(), URI: tlogVerifier.BaseURL})
}
}
if entry.HasInclusionProof() {
verifier, err := getVerifier(tlogVerifier.PublicKey, tlogVerifier.SignatureHashFunc)
if err != nil {
return nil, err
}
err = tlog.VerifyInclusion(entry, *verifier)
if err != nil {
return nil, err
}
// DO NOT use timestamp with only an inclusion proof, because it is not signed metadata
}
} else {
client, err := RekorClientGetter(tlogVerifier.BaseURL)
if err != nil {
return nil, err
}
verifier, err := getVerifier(tlogVerifier.PublicKey, tlogVerifier.SignatureHashFunc)
if err != nil {
return nil, err
}
logIndex := entry.LogIndex()
searchParams := rekorEntries.NewGetLogEntryByIndexParams()
searchParams.LogIndex = logIndex
resp, err := client.Entries.GetLogEntryByIndex(searchParams)
if err != nil {
return nil, err
}
if len(resp.Payload) == 0 {
return nil, fmt.Errorf("unable to locate log entry %d", logIndex)
}
for _, v := range resp.Payload {
v := v
err = rekorVerify.VerifyLogEntry(context.TODO(), &v, *verifier)
if err != nil {
return nil, err
}
}
if trustIntegratedTime {
verifiedTimestamps = append(verifiedTimestamps, root.Timestamp{Time: entry.IntegratedTime(), URI: tlogVerifier.BaseURL})
}
}
// Ensure entry signature matches signature from bundle
if !bytes.Equal(entry.Signature(), entitySignature) {
return nil, errors.New("transparency log signature does not match")
}
// Ensure entry certificate matches bundle certificate
if !verificationContent.CompareKey(entry.PublicKey(), trustedMaterial) {
return nil, errors.New("transparency log certificate does not match")
}
// TODO: if you have access to artifact, check that it matches body subject
// Check tlog entry time against bundle certificates
if !verificationContent.ValidAtTime(entry.IntegratedTime(), trustedMaterial) {
return nil, errors.New("integrated time outside certificate validity")
}
// successful log entry verification
logEntriesVerified++
}
if logEntriesVerified < logThreshold {
return nil, fmt.Errorf("not enough verified log entries from transparency log: %d < %d", logEntriesVerified, logThreshold)
}
return verifiedTimestamps, nil
}
func getVerifier(publicKey crypto.PublicKey, hashFunc crypto.Hash) (*signature.Verifier, error) {
verifier, err := signature.LoadVerifier(publicKey, hashFunc)
if err != nil {
return nil, err
}
return &verifier, nil
}
func getRekorClient(baseURL string) (*rekorGeneratedClient.Rekor, error) {
client, err := rekorClient.GetRekorClient(baseURL, rekorClient.WithUserAgent(util.ConstructUserAgent()))
if err != nil {
return nil, err
}
return client, nil
}