Skip to content

Commit

Permalink
Fix bug with detached signature
Browse files Browse the repository at this point in the history
Also add test for Fulcio certificate and old bundle format

Signed-off-by: Zach Steindler <[email protected]>
  • Loading branch information
steiza committed Nov 1, 2024
1 parent 0fff093 commit 0d55987
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cmd/cosign/cli/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ func (c *CreateCmd) Exec(ctx context.Context) (err error) {
}

if c.SignaturePath != "" {
sigBytes, err = os.ReadFile(c.SignaturePath)
signatureB64, err := os.ReadFile(c.SignaturePath)
if err != nil {
return err
}

sigBytes, err = base64.StdEncoding.DecodeString(string(signatureB64))
if err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/cosign/cli/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"os"
"path/filepath"
"testing"

"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/cosign/v2/test"
sgBundle "github.com/sigstore/sigstore-go/pkg/bundle"
"github.com/sigstore/sigstore/pkg/cryptoutils"
)

func TestCreateCmd(t *testing.T) {
Expand All @@ -43,6 +47,7 @@ func TestCreateCmd(t *testing.T) {
err := os.WriteFile(artifactPath, []byte(artifact), 0600)
checkErr(t, err)

// Test signing with a key
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
checkErr(t, err)
sigBytes, err := privateKey.Sign(rand.Reader, digest[:], crypto.SHA256)
Expand Down Expand Up @@ -90,6 +95,52 @@ func TestCreateCmd(t *testing.T) {
if b.Bundle.GetMessageSignature() == nil {
t.Fatal("bundle does not have message signature")
}

// Test using an identity certificate in an old bundle format
rootCert, rootKey, _ := test.GenerateRootCa()
leafCert, privKey, _ := test.GenerateLeafCert("subject", "oidc-issuer", rootCert, rootKey)

sigBytes, err = privKey.Sign(rand.Reader, digest[:], crypto.SHA256)
checkErr(t, err)

signedPayload := cosign.LocalSignedPayload{}
signedPayload.Base64Signature = base64.StdEncoding.EncodeToString(sigBytes)

certBytes, err := cryptoutils.MarshalCertificateToPEM(leafCert)
checkErr(t, err)

signedPayload.Cert = base64.StdEncoding.EncodeToString(certBytes)
bundleContents, err := json.Marshal(signedPayload)
checkErr(t, err)

bundlePath := filepath.Join(td, "old-bundle.json")
err = os.WriteFile(bundlePath, bundleContents, 0600)
checkErr(t, err)

bundleCreate = CreateCmd{
Artifact: artifactPath,
BundlePath: bundlePath,
IgnoreTlog: true,
Out: outPath,
}

err = bundleCreate.Exec(ctx)
checkErr(t, err)

b, err = sgBundle.LoadJSONFromPath(outPath)
checkErr(t, err)

if b.Bundle.VerificationMaterial == nil {
t.Fatal("bundle does not have verification material")
}

if b.Bundle.VerificationMaterial.GetCertificate() == nil {
t.Fatal("bundle verification material does not have certificate")
}

if b.Bundle.GetMessageSignature() == nil {
t.Fatal("bundle does not have message signature")
}
}

func checkErr(t *testing.T, err error) {
Expand Down
14 changes: 3 additions & 11 deletions cmd/cosign/cli/verify/verify_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
protodsse "github.com/sigstore/protobuf-specs/gen/pb-go/dsse"
protorekor "github.com/sigstore/protobuf-specs/gen/pb-go/rekor/v1"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/tle"
sgbundle "github.com/sigstore/sigstore-go/pkg/bundle"
"github.com/sigstore/sigstore-go/pkg/fulcio/certificate"
Expand Down Expand Up @@ -294,18 +293,11 @@ func AssembleNewBundle(ctx context.Context, sigBytes, signedTimestamp []byte, en
if len(tlogEntries) == 0 {
return nil, fmt.Errorf("unable to find tlog entry")
}
// Attempt to verify with the earliest integrated entry
var earliestLogEntry models.LogEntryAnon
var earliestLogEntryTime *time.Time
for _, e := range tlogEntries {
entryTime := time.Unix(*e.IntegratedTime, 0)
if earliestLogEntryTime == nil || entryTime.Before(*earliestLogEntryTime) {
earliestLogEntryTime = &entryTime
earliestLogEntry = e
}
if len(tlogEntries) > 1 {
return nil, fmt.Errorf("too many tlog entries; should only have 1")
}

tlogEntry, err := tle.GenerateTransparencyLogEntry(earliestLogEntry)
tlogEntry, err := tle.GenerateTransparencyLogEntry(tlogEntries[0])
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0d55987

Please sign in to comment.