Skip to content

Commit 2ad1d62

Browse files
committed
Fixes to cosign sign / verify for the new bundle format (sigstore#4346)
* Fixes to cosign sign / verify for the new bundle format Signed-off-by: Zach Steindler <steiza@github.com> * Update function signature to pass crypto.PublicKey directly Signed-off-by: Zach Steindler <steiza@github.com> --------- Signed-off-by: Zach Steindler <steiza@github.com>
1 parent 5caf562 commit 2ad1d62

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed

cmd/cosign/cli/attest/attest.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package attest
1818
import (
1919
"bytes"
2020
"context"
21-
"crypto"
2221
_ "crypto/sha256" // for `crypto.SHA256`
2322
"encoding/json"
2423
"fmt"
@@ -252,10 +251,9 @@ func (c *AttestCommand) Exec(ctx context.Context, imageRef string) error {
252251
if err != nil {
253252
return err
254253
}
255-
var pubKey *crypto.PublicKey
256-
pk, err := sv.PublicKey()
257-
if err == nil {
258-
pubKey = &pk
254+
pubKey, err := sv.PublicKey()
255+
if err != nil {
256+
return err
259257
}
260258
bundleBytes, err := cbundle.MakeNewBundle(pubKey, rekorEntry, payload, signedPayload, signerBytes, timestampBytes)
261259
if err != nil {

cmd/cosign/cli/attest/attest_blob.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,9 @@ func (c *AttestBlobCommand) Exec(ctx context.Context, artifactPath string) error
290290
if c.BundlePath != "" {
291291
var contents []byte
292292
if c.NewBundleFormat {
293-
var pubKey *crypto.PublicKey
294-
pk, err := sv.PublicKey()
295-
if err == nil {
296-
pubKey = &pk
293+
pubKey, err := sv.PublicKey()
294+
if err != nil {
295+
return err
297296
}
298297

299298
contents, err = cbundle.MakeNewBundle(pubKey, rekorEntry, payload, sig, signer, timestampBytes)

cmd/cosign/cli/sign/sign.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,9 @@ func signDigestBundle(ctx context.Context, digest name.Digest, ko options.KeyOpt
305305
return fmt.Errorf("constructing client options: %w", err)
306306
}
307307

308-
var pubKey *crypto.PublicKey
309-
pk, err := sv.PublicKey()
310-
if err == nil {
311-
pubKey = &pk
308+
pubKey, err := sv.PublicKey()
309+
if err != nil {
310+
return err
312311
}
313312

314313
bundleBytes, err := cbundle.MakeNewBundle(pubKey, rekorEntry, payload, signedPayload, signerBytes, timestampBytes)

pkg/cosign/bundle/protobundle.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,20 @@ func MakeProtobufBundle(hint string, rawCert []byte, rekorEntry *models.LogEntry
7575
return bundle, nil
7676
}
7777

78-
func MakeNewBundle(pubKey *crypto.PublicKey, rekorEntry *models.LogEntryAnon, payload, sig, signer, timestampBytes []byte) ([]byte, error) {
78+
func MakeNewBundle(pubKey crypto.PublicKey, rekorEntry *models.LogEntryAnon, payload, sig, signer, timestampBytes []byte) ([]byte, error) {
7979
// Determine if the signer is a certificate or not
8080
var hint string
8181
var rawCert []byte
8282

83-
if pubKey != nil {
84-
pkixPubKey, err := x509.MarshalPKIXPublicKey(*pubKey)
83+
cert, err := cryptoutils.UnmarshalCertificatesFromPEM(signer)
84+
if err != nil || len(cert) == 0 {
85+
pkixPubKey, err := x509.MarshalPKIXPublicKey(pubKey)
8586
if err != nil {
8687
return nil, err
8788
}
8889
hashedBytes := sha256.Sum256(pkixPubKey)
8990
hint = base64.StdEncoding.EncodeToString(hashedBytes[:])
9091
} else {
91-
cert, err := cryptoutils.UnmarshalCertificatesFromPEM(signer)
92-
if err != nil {
93-
return nil, err
94-
}
9592
rawCert = cert[0].Raw
9693
}
9794

test/e2e_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ func TestSignVerifyBundle(t *testing.T) {
10431043
NewBundleFormat: true,
10441044
UseSignedTimestamps: false,
10451045
}
1046+
1047+
must(cmd.Exec(ctx, args), t)
10461048
}
10471049

10481050
func TestAttestVerify(t *testing.T) {

0 commit comments

Comments
 (0)