-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathattest_blob.go
More file actions
397 lines (356 loc) · 11.4 KB
/
attest_blob.go
File metadata and controls
397 lines (356 loc) · 11.4 KB
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
// Copyright 2022 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 attest
import (
"bytes"
"context"
"crypto"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
intotov1 "github.com/in-toto/attestation/go/v1"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/rekor"
cosign_sign "github.com/sigstore/cosign/v2/cmd/cosign/cli/sign"
"github.com/sigstore/cosign/v2/internal/auth"
"github.com/sigstore/cosign/v2/internal/key"
"github.com/sigstore/cosign/v2/internal/pkg/cosign/tsa"
tsaclient "github.com/sigstore/cosign/v2/internal/pkg/cosign/tsa/client"
"github.com/sigstore/cosign/v2/internal/ui"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/cosign/v2/pkg/cosign/attestation"
cbundle "github.com/sigstore/cosign/v2/pkg/cosign/bundle"
"github.com/sigstore/cosign/v2/pkg/types"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/sigstore-go/pkg/sign"
"github.com/sigstore/sigstore/pkg/cryptoutils"
"github.com/sigstore/sigstore/pkg/signature"
sigstoredsse "github.com/sigstore/sigstore/pkg/signature/dsse"
signatureoptions "github.com/sigstore/sigstore/pkg/signature/options"
)
// nolint
type AttestBlobCommand struct {
options.KeyOpts
CertPath string
CertChainPath string
ArtifactHash string
StatementPath string
PredicatePath string
PredicateType string
TlogUpload bool
Timeout time.Duration
OutputSignature string
OutputAttestation string
OutputCertificate string
RekorEntryType string
}
// nolint
func (c *AttestBlobCommand) Exec(ctx context.Context, artifactPath string) error {
// We can't have both a key and a security key
if options.NOf(c.KeyRef, c.Sk) > 1 {
return &options.KeyParseError{}
}
if options.NOf(c.PredicatePath, c.StatementPath) != 1 {
return fmt.Errorf("one of --predicate or --statement must be set")
}
if c.RekorEntryType != "dsse" && c.RekorEntryType != "intoto" {
return fmt.Errorf("unknown value for rekor-entry-type")
}
if c.Timeout != 0 {
var cancelFn context.CancelFunc
ctx, cancelFn = context.WithTimeout(ctx, c.Timeout)
defer cancelFn()
}
if c.TSAServerURL != "" && c.RFC3161TimestampPath == "" && !c.NewBundleFormat {
return errors.New("expected either new bundle or an rfc3161-timestamp path when using a TSA server")
}
base := path.Base(artifactPath)
var payload []byte
var err error
if c.StatementPath != "" {
fmt.Fprintln(os.Stderr, "Using statement from:", c.StatementPath)
payload, err = os.ReadFile(filepath.Clean(c.StatementPath))
if err != nil {
return fmt.Errorf("could not read statement: %w", err)
}
if _, err := validateStatement(payload); err != nil {
return fmt.Errorf("invalid statement: %w", err)
}
} else {
var artifact []byte
var hexDigest string
if c.ArtifactHash == "" {
if artifactPath == "-" {
artifact, err = io.ReadAll(os.Stdin)
} else {
fmt.Fprintln(os.Stderr, "Using payload from:", artifactPath)
artifact, err = os.ReadFile(filepath.Clean(artifactPath))
}
if err != nil {
return err
}
}
if c.ArtifactHash == "" {
digest, _, err := signature.ComputeDigestForSigning(bytes.NewReader(artifact), crypto.SHA256, []crypto.Hash{crypto.SHA256, crypto.SHA384})
if err != nil {
return err
}
hexDigest = strings.ToLower(hex.EncodeToString(digest))
} else {
hexDigest = c.ArtifactHash
}
predicate, err := predicateReader(c.PredicatePath)
if err != nil {
return fmt.Errorf("getting predicate reader: %w", err)
}
defer predicate.Close()
sh, err := attestation.GenerateStatement(attestation.GenerateOpts{
Predicate: predicate,
Type: c.PredicateType,
Digest: hexDigest,
Repo: base,
})
if err != nil {
return err
}
payload, err = json.Marshal(sh)
if err != nil {
return err
}
}
if c.SigningConfig != nil {
var keypair sign.Keypair
var ephemeralKeypair bool
var idToken string
var sv *cosign_sign.SignerVerifier
var err error
if c.Sk || c.Slot != "" || c.KeyRef != "" || c.CertPath != "" {
// Set no load options so that Ed25519 is preferred over Ed25519ph, required for signing DSSEs
var signOpts []signature.LoadOption
c.KeyOpts.DefaultLoadOptions = &signOpts
sv, _, err = cosign_sign.SignerFromKeyOpts(ctx, c.CertPath, c.CertChainPath, c.KeyOpts)
if err != nil {
return fmt.Errorf("getting signer: %w", err)
}
keypair, err = key.NewSignerVerifierKeypair(sv, &signOpts)
if err != nil {
return fmt.Errorf("creating signerverifier keypair: %w", err)
}
} else {
keypair, err = sign.NewEphemeralKeypair(nil)
if err != nil {
return fmt.Errorf("generating keypair: %w", err)
}
ephemeralKeypair = true
}
defer func() {
if sv != nil {
sv.Close()
}
}()
if ephemeralKeypair || c.IssueCertificateForExistingKey {
idToken, err = auth.RetrieveIDToken(ctx, auth.IDTokenConfig{
TokenOrPath: c.IDToken,
DisableProviders: c.OIDCDisableProviders,
Provider: c.OIDCProvider,
AuthFlow: c.FulcioAuthFlow,
SkipConfirm: c.SkipConfirmation,
OIDCServices: c.SigningConfig.OIDCProviderURLs(),
ClientID: c.OIDCClientID,
ClientSecret: c.OIDCClientSecret,
RedirectURL: c.OIDCRedirectURL,
})
if err != nil {
return fmt.Errorf("retrieving ID token: %w", err)
}
}
content := &sign.DSSEData{
Data: payload,
PayloadType: "application/vnd.in-toto+json",
}
bundle, err := cbundle.SignData(ctx, content, keypair, idToken, c.SigningConfig, c.TrustedMaterial)
if err != nil {
return fmt.Errorf("signing bundle: %w", err)
}
if err := os.WriteFile(c.BundlePath, bundle, 0600); err != nil {
return fmt.Errorf("create bundle file: %w", err)
}
ui.Infof(ctx, "Wrote bundle to file %s", c.BundlePath)
return nil
}
sv, genKey, err := cosign_sign.SignerFromKeyOpts(ctx, c.CertPath, c.CertChainPath, c.KeyOpts)
if err != nil {
return fmt.Errorf("getting signer: %w", err)
}
if genKey || c.IssueCertificateForExistingKey {
sv, err = cosign_sign.KeylessSigner(ctx, c.KeyOpts, sv)
if err != nil {
return fmt.Errorf("getting Fulcio signer: %w", err)
}
}
defer sv.Close()
wrapped := sigstoredsse.WrapSigner(sv, types.IntotoPayloadType)
sig, err := wrapped.SignMessage(bytes.NewReader(payload), signatureoptions.WithContext(ctx))
if err != nil {
return fmt.Errorf("signing: %w", err)
}
var rfc3161Timestamp *cbundle.RFC3161Timestamp
var timestampBytes []byte
var tsaPayload []byte
var rekorEntry *models.LogEntryAnon
if c.KeyOpts.TSAServerURL != "" {
tc := tsaclient.NewTSAClient(c.KeyOpts.TSAServerURL)
if c.TSAClientCert != "" {
tc = tsaclient.NewTSAClientMTLS(c.KeyOpts.TSAServerURL,
c.KeyOpts.TSAClientCACert,
c.KeyOpts.TSAClientCert,
c.KeyOpts.TSAClientKey,
c.KeyOpts.TSAServerName,
)
}
// We need to decide what signature to send to the timestamp authority.
//
// Historically, cosign sent `sig`, which is the entire JSON DSSE
// Envelope. However, when sigstore clients are verifying a bundle they
// will use the DSSE Sig field, so we choose what signature to send to
// the timestamp authority based on our output format.
if c.NewBundleFormat {
tsaPayload, err = cosign.GetDSSESigBytes(sig)
if err != nil {
return err
}
} else {
tsaPayload = sig
}
timestampBytes, err = tsa.GetTimestampedSignature(tsaPayload, tc)
if err != nil {
return err
}
rfc3161Timestamp = cbundle.TimestampToRFC3161Timestamp(timestampBytes)
// TODO: Consider uploading RFC3161 TS to Rekor
if rfc3161Timestamp == nil {
return fmt.Errorf("rfc3161 timestamp is nil")
}
if c.RFC3161TimestampPath != "" {
ts, err := json.Marshal(rfc3161Timestamp)
if err != nil {
return err
}
if err := os.WriteFile(c.RFC3161TimestampPath, ts, 0600); err != nil {
return fmt.Errorf("create RFC3161 timestamp file: %w", err)
}
fmt.Fprintln(os.Stderr, "RFC3161 timestamp bundle written to file ", c.RFC3161TimestampPath)
}
}
signer, err := sv.Bytes(ctx)
if err != nil {
return err
}
shouldUpload, err := cosign_sign.ShouldUploadToTlog(ctx, c.KeyOpts, nil, c.TlogUpload)
if err != nil {
return fmt.Errorf("upload to tlog: %w", err)
}
signedPayload := cosign.LocalSignedPayload{}
if shouldUpload {
rekorClient, err := rekor.NewClient(c.RekorURL)
if err != nil {
return err
}
if c.RekorEntryType == "intoto" {
rekorEntry, err = cosign.TLogUploadInTotoAttestation(ctx, rekorClient, sig, signer)
} else {
rekorEntry, err = cosign.TLogUploadDSSEEnvelope(ctx, rekorClient, sig, signer)
}
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, "tlog entry created with index:", *rekorEntry.LogIndex)
signedPayload.Bundle = cbundle.EntryToBundle(rekorEntry)
}
if c.BundlePath != "" {
var contents []byte
if c.NewBundleFormat {
pubKey, err := sv.PublicKey()
if err != nil {
return err
}
contents, err = cbundle.MakeNewBundle(pubKey, rekorEntry, payload, sig, signer, timestampBytes)
if err != nil {
return err
}
} else {
signedPayload.Base64Signature = base64.StdEncoding.EncodeToString(sig)
signedPayload.Cert = base64.StdEncoding.EncodeToString(signer)
contents, err = json.Marshal(signedPayload)
if err != nil {
return err
}
}
if err := os.WriteFile(c.BundlePath, contents, 0600); err != nil {
return fmt.Errorf("create bundle file: %w", err)
}
fmt.Fprintln(os.Stderr, "Bundle wrote in the file ", c.BundlePath)
}
if c.OutputSignature != "" {
if err := os.WriteFile(c.OutputSignature, sig, 0600); err != nil {
return fmt.Errorf("create signature file: %w", err)
}
fmt.Fprintf(os.Stderr, "Signature written in %s\n", c.OutputSignature)
} else {
fmt.Fprintln(os.Stdout, string(sig))
}
if c.OutputAttestation != "" {
if err := os.WriteFile(c.OutputAttestation, payload, 0600); err != nil {
return fmt.Errorf("create signature file: %w", err)
}
fmt.Fprintf(os.Stderr, "Attestation written in %s\n", c.OutputAttestation)
}
if c.OutputCertificate != "" {
signer, err := sv.Bytes(ctx)
if err != nil {
return fmt.Errorf("error getting signer: %w", err)
}
cert, err := cryptoutils.UnmarshalCertificatesFromPEM(signer)
// signer is a certificate
if err != nil {
fmt.Fprintln(os.Stderr, "Could not output signer certificate. Was a certificate used? ", err)
return nil
}
if len(cert) != 1 {
fmt.Fprintln(os.Stderr, "Could not output signer certificate. Expected a single certificate")
return nil
}
bts := signer
if err := os.WriteFile(c.OutputCertificate, bts, 0600); err != nil {
return fmt.Errorf("create certificate file: %w", err)
}
fmt.Fprintln(os.Stderr, "Certificate written to file ", c.OutputCertificate)
}
return nil
}
func validateStatement(payload []byte) (string, error) {
var statement *intotov1.Statement
if err := json.Unmarshal(payload, &statement); err != nil {
return "", fmt.Errorf("invalid statement: %w", err)
}
return statement.PredicateType, nil
}