1515package cosign
1616
1717import (
18+ "bytes"
1819 "context"
1920 "crypto"
21+ "crypto/elliptic"
2022 "crypto/rand"
2123 "crypto/sha256"
2224 "crypto/x509"
@@ -26,15 +28,22 @@ import (
2628 "io"
2729 "strings"
2830 "testing"
31+ "time"
2932
33+ "github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
34+ "github.com/go-openapi/strfmt"
3035 v1 "github.com/google/go-containerregistry/pkg/v1"
3136 "github.com/in-toto/in-toto-golang/in_toto"
3237 "github.com/pkg/errors"
3338 "github.com/secure-systems-lab/go-securesystemslib/dsse"
39+ "github.com/sigstore/cosign/pkg/cosign/bundle"
40+ ctuf "github.com/sigstore/cosign/pkg/cosign/tuf"
3441 "github.com/sigstore/cosign/pkg/oci/static"
3542 "github.com/sigstore/cosign/pkg/types"
3643 "github.com/sigstore/cosign/test"
44+ rtypes "github.com/sigstore/rekor/pkg/types"
3745 "github.com/sigstore/sigstore/pkg/signature"
46+ "github.com/sigstore/sigstore/pkg/signature/options"
3847 "github.com/stretchr/testify/require"
3948)
4049
@@ -167,8 +176,54 @@ func TestVerifyImageSignatureMultipleSubs(t *testing.T) {
167176 }
168177}
169178
179+ func signEntry (ctx context.Context , t * testing.T , signer signature.Signer , entry bundle.RekorPayload ) []byte {
180+ payload , err := json .Marshal (entry )
181+ if err != nil {
182+ t .Fatalf ("marshalling error: %v" , err )
183+ }
184+ canonicalized , err := jsoncanonicalizer .Transform (payload )
185+ if err != nil {
186+ t .Fatalf ("canonicalizing error: %v" , err )
187+ }
188+ signature , err := signer .SignMessage (bytes .NewReader (canonicalized ), options .WithContext (ctx ))
189+ if err != nil {
190+ t .Fatalf ("signing error: %v" , err )
191+ }
192+ return signature
193+ }
194+
195+ func CreateTestBundle (ctx context.Context , t * testing.T , rekor signature.Signer , leaf []byte ) * bundle.RekorBundle {
196+ // generate log ID according to rekor public key
197+ pk , _ := rekor .PublicKey (nil )
198+ keyID , _ := getLogID (pk )
199+ pyld := bundle.RekorPayload {
200+ Body : base64 .StdEncoding .EncodeToString (leaf ),
201+ IntegratedTime : time .Now ().Unix (),
202+ LogIndex : 693591 ,
203+ LogID : keyID ,
204+ }
205+ // Sign with root.
206+ signature := signEntry (ctx , t , rekor , pyld )
207+ b := & bundle.RekorBundle {
208+ SignedEntryTimestamp : strfmt .Base64 (signature ),
209+ Payload : pyld ,
210+ }
211+ return b
212+ }
213+
170214func TestVerifyImageSignatureWithNoChain (t * testing.T ) {
215+ ctx := context .Background ()
171216 rootCert , rootKey , _ := test .GenerateRootCa ()
217+ sv , _ , err := signature .NewECDSASignerVerifier (elliptic .P256 (), rand .Reader , crypto .SHA256 )
218+ if err != nil {
219+ t .Fatalf ("creating signer: %v" , err )
220+ }
221+ testSigstoreRoot := ctuf.TestSigstoreRoot {
222+ Rekor : sv ,
223+ FulcioCertificate : rootCert ,
224+ }
225+ _ , _ = ctuf .NewSigstoreTufRepo (t , testSigstoreRoot )
226+
172227 leafCert , privKey , _ := test .GenerateLeafCert ("subject" , "oidc-issuer" , rootCert , rootKey )
173228 pemLeaf := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : leafCert .Raw })
174229
@@ -179,14 +234,21 @@ func TestVerifyImageSignatureWithNoChain(t *testing.T) {
179234 h := sha256 .Sum256 (payload )
180235 signature , _ := privKey .Sign (rand .Reader , h [:], crypto .SHA256 )
181236
182- ociSig , _ := static .NewSignature (payload , base64 .StdEncoding .EncodeToString (signature ), static .WithCertChain (pemLeaf , []byte {}))
237+ // Create a fake bundle
238+ pe , _ := proposedEntry (base64 .StdEncoding .EncodeToString (signature ), payload , pemLeaf )
239+ entry , _ := rtypes .NewEntry (pe [0 ])
240+ leaf , _ := entry .Canonicalize (ctx )
241+ rekorBundle := CreateTestBundle (ctx , t , sv , leaf )
242+
243+ opts := []static.Option {static .WithCertChain (pemLeaf , []byte {}), static .WithBundle (rekorBundle )}
244+ ociSig , _ := static .NewSignature (payload , base64 .StdEncoding .EncodeToString (signature ), opts ... )
245+
183246 verified , err := VerifyImageSignature (context .TODO (), ociSig , v1.Hash {}, & CheckOpts {RootCerts : rootPool })
184247 if err != nil {
185248 t .Fatalf ("unexpected error while verifying signature, expected no error, got %v" , err )
186249 }
187- // TODO: Create fake bundle and test verification
188- if verified == true {
189- t .Fatalf ("expected verified=false, got verified=true" )
250+ if verified == false {
251+ t .Fatalf ("expected verified=true, got verified=false" )
190252 }
191253}
192254
0 commit comments