Skip to content

Commit 114b8d2

Browse files
committed
Upgrade to TUF v2 client with trusted root
Use sigstore-go's TUF client to fetch the trusted_root.json from the TUF mirror, if available. Where possible, use sigstore-go's verifiers which natively accept the trusted root as its trusted material. Where there is no trusted root available in TUF or sigstore-go doesn't support a use case, fall back to the sigstore/sigstore TUF v1 client and the existing verifiers in cosign. Signed-off-by: Colleen Murphy <[email protected]>
1 parent b438935 commit 114b8d2

26 files changed

+1363
-130
lines changed

cmd/conformance/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ func main() {
124124
args = append(args, os.Args[len(os.Args)-1])
125125

126126
dir := filepath.Dir(os.Args[0])
127+
initCmd := exec.Command(filepath.Join(dir, "cosign"), "initialize") // #nosec G204
128+
err := initCmd.Run()
129+
if err != nil {
130+
log.Fatal(err)
131+
}
127132
cmd := exec.Command(filepath.Join(dir, "cosign"), args...) // #nosec G204
128133
var out strings.Builder
129134
cmd.Stdout = &out
130135
cmd.Stderr = &out
131-
err := cmd.Run()
136+
err = cmd.Run()
132137

133138
fmt.Println(out.String())
134139

cmd/cosign/cli/attest.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package cli
1717

1818
import (
19+
"context"
1920
"fmt"
2021

2122
"github.com/spf13/cobra"
2223

2324
"github.com/sigstore/cosign/v2/cmd/cosign/cli/attest"
2425
"github.com/sigstore/cosign/v2/cmd/cosign/cli/generate"
2526
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
27+
"github.com/sigstore/cosign/v2/internal/ui"
28+
"github.com/sigstore/cosign/v2/pkg/cosign"
2629
)
2730

2831
func Attest() *cobra.Command {
@@ -70,6 +73,12 @@ func Attest() *cobra.Command {
7073
if err != nil {
7174
return err
7275
}
76+
77+
trustedMaterial, err := cosign.TrustedRoot()
78+
if err != nil {
79+
ui.Warnf(context.Background(), "Could not fetch trusted_root.json from the TUF repository. Continuing with individual targets. Error from TUF: %v", err)
80+
}
81+
7382
ko := options.KeyOpts{
7483
KeyRef: o.Key,
7584
PassFunc: generate.GetPass,
@@ -92,6 +101,7 @@ func Attest() *cobra.Command {
92101
TSAServerName: o.TSAServerName,
93102
TSAServerURL: o.TSAServerURL,
94103
NewBundleFormat: o.NewBundleFormat,
104+
TrustedMaterial: trustedMaterial,
95105
}
96106
attestCommand := attest.AttestCommand{
97107
KeyOpts: ko,

cmd/cosign/cli/attest_blob.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package cli
1616

1717
import (
18+
"context"
19+
1820
"github.com/sigstore/cosign/v2/cmd/cosign/cli/attest"
1921
"github.com/sigstore/cosign/v2/cmd/cosign/cli/generate"
2022
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
23+
"github.com/sigstore/cosign/v2/internal/ui"
24+
"github.com/sigstore/cosign/v2/pkg/cosign"
2125
"github.com/spf13/cobra"
2226
)
2327

@@ -54,6 +58,12 @@ func AttestBlob() *cobra.Command {
5458
if err != nil {
5559
return err
5660
}
61+
62+
trustedMaterial, err := cosign.TrustedRoot()
63+
if err != nil {
64+
ui.Warnf(context.Background(), "Could not fetch trusted_root.json from the TUF repository. Continuing with individual targets. Error from TUF: %v", err)
65+
}
66+
5767
ko := options.KeyOpts{
5868
KeyRef: o.Key,
5969
PassFunc: generate.GetPass,
@@ -78,6 +88,7 @@ func AttestBlob() *cobra.Command {
7888
RFC3161TimestampPath: o.RFC3161TimestampPath,
7989
BundlePath: o.BundlePath,
8090
NewBundleFormat: o.NewBundleFormat,
91+
TrustedMaterial: trustedMaterial,
8192
}
8293
v := attest.AttestBlobCommand{
8394
KeyOpts: ko,

cmd/cosign/cli/fulcio/fulcioverifier/fulcioverifier.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package fulcioverifier
1717

1818
import (
1919
"context"
20+
"crypto/x509"
2021
"fmt"
2122

2223
"github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio"
2324
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
2425
"github.com/sigstore/cosign/v2/internal/ui"
2526
"github.com/sigstore/cosign/v2/pkg/cosign"
27+
"github.com/sigstore/sigstore-go/pkg/verify"
28+
"github.com/sigstore/sigstore/pkg/cryptoutils"
2629
"github.com/sigstore/sigstore/pkg/signature"
2730
)
2831

@@ -32,12 +35,31 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
3235
return nil, err
3336
}
3437

35-
// Grab the PublicKeys for the CTFE, either from tuf or env.
38+
if ko.TrustedMaterial != nil && len(fs.SCT) == 0 {
39+
// Detached SCTs cannot be verified with this function.
40+
chain, err := cryptoutils.UnmarshalCertificatesFromPEM(fs.Chain)
41+
if err != nil {
42+
return nil, fmt.Errorf("unmarshalling cert chain from PEM for SCT verification: %w", err)
43+
}
44+
certs, err := cryptoutils.UnmarshalCertificatesFromPEM(fs.Cert)
45+
if err != nil || len(certs) < 1 {
46+
return nil, fmt.Errorf("unmarshalling cert from PEM for SCT verification: %w", err)
47+
}
48+
chain = append(certs, chain...)
49+
chains := make([][]*x509.Certificate, 1)
50+
chains[0] = chain
51+
if err := verify.VerifySignedCertificateTimestamp(chains, 1, ko.TrustedMaterial); err != nil {
52+
return nil, fmt.Errorf("verifying SCT using trusted root: %w", err)
53+
}
54+
ui.Infof(ctx, "Successfully verified SCT...")
55+
return fs, nil
56+
}
57+
58+
// There was no trusted_root.json or we need to verify a detached SCT, so grab the PublicKeys for the CTFE, either from tuf or env.
3659
pubKeys, err := cosign.GetCTLogPubs(ctx)
3760
if err != nil {
3861
return nil, fmt.Errorf("getting CTFE public keys: %w", err)
3962
}
40-
4163
// verify the sct
4264
if err := cosign.VerifySCT(ctx, fs.Cert, fs.Chain, fs.SCT, pubKeys); err != nil {
4365
return nil, fmt.Errorf("verifying SCT: %w", err)

0 commit comments

Comments
 (0)