@@ -731,6 +731,84 @@ func TestVerifyImageSignatureWithSigVerifierAndRekorTSA(t *testing.T) {
731731 }
732732}
733733
734+ func TestVerifyImageSignatureWithMismatchedBundleAndTrustedRoot (t * testing.T ) {
735+ ctx := context .Background ()
736+ var ca root.FulcioCertificateAuthority
737+ rootCert , rootKey , _ := test .GenerateRootCa ()
738+ ca .Root = rootCert
739+ sv , _ , err := signature .NewECDSASignerVerifier (elliptic .P256 (), rand .Reader , crypto .SHA256 )
740+ if err != nil {
741+ t .Fatalf ("creating signer: %v" , err )
742+ }
743+
744+ leafCert , privKey , _ := test .GenerateLeafCert ("subject@mail.com" , "oidc-issuer" , rootCert , rootKey )
745+ pemLeaf := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : leafCert .Raw })
746+
747+ rootPool := x509 .NewCertPool ()
748+ rootPool .AddCert (rootCert )
749+
750+ payload := []byte {1 , 2 , 3 , 4 }
751+ h := sha256 .Sum256 (payload )
752+ signature1 , _ := privKey .Sign (rand .Reader , h [:], crypto .SHA256 )
753+
754+ // Create a fake bundle
755+ pe , _ := proposedEntries (base64 .StdEncoding .EncodeToString (signature1 ), payload , pemLeaf )
756+ entry , _ := rtypes .UnmarshalEntry (pe [0 ])
757+ leaf , _ := entry .Canonicalize (ctx )
758+ rekorBundle := CreateTestBundle (ctx , t , sv , leaf )
759+ pemBytes , _ := cryptoutils .MarshalPublicKeyToPEM (sv .Public ())
760+ rekorPubKeys := NewTrustedTransparencyLogPubKeys ()
761+ rekorPubKeys .AddTransparencyLogPubKey (pemBytes , tuf .Active )
762+
763+ tlogs := make (map [string ]* root.TransparencyLog )
764+ for k , v := range rekorPubKeys .Keys {
765+ tlogs [k ] = & root.TransparencyLog {PublicKey : v .PubKey , HashFunc : crypto .SHA256 , ValidityPeriodStart : time .Now ().Add (- 1 * time .Minute )}
766+ }
767+
768+ trustedRoot , err := root .NewTrustedRoot (root .TrustedRootMediaType01 , []root.CertificateAuthority {& ca }, nil , nil , tlogs )
769+ if err != nil {
770+ t .Fatal (err )
771+ }
772+
773+ // Create a different bundle for a different signature
774+ signature2 , _ := privKey .Sign (rand .Reader , h [:], crypto .SHA256 )
775+ pe2 , _ := proposedEntries (base64 .StdEncoding .EncodeToString (signature2 ), payload , pemLeaf )
776+ entry2 , _ := rtypes .UnmarshalEntry (pe2 [0 ])
777+ leaf2 , _ := entry2 .Canonicalize (ctx )
778+ rekorBundle2 := CreateTestBundle (ctx , t , sv , leaf2 )
779+
780+ opts := []static.Option {static .WithCertChain (pemLeaf , []byte {}), static .WithBundle (rekorBundle2 )}
781+ // Create a signed entity for the original signature but with the wrong bundle for that signature
782+ ociSig , _ := static .NewSignature (payload , base64 .StdEncoding .EncodeToString (signature1 ), opts ... )
783+
784+ _ , err = VerifyImageSignature (context .TODO (), ociSig , v1.Hash {},
785+ & CheckOpts {
786+ RootCerts : rootPool ,
787+ IgnoreSCT : true ,
788+ Identities : []Identity {{Subject : "subject@mail.com" , Issuer : "oidc-issuer" }},
789+ TrustedMaterial : trustedRoot })
790+ if err == nil || ! strings .Contains (err .Error (), "signature in bundle does not match signature being verified" ) {
791+ t .Fatalf ("expected error for mismatched signature and bundle, got %v" , err )
792+ }
793+
794+ // Create a signed entity with a different key from the bundle
795+ leafCert2 , _ , _ := test .GenerateLeafCert ("subject@mail.com" , "oidc-issuer" , rootCert , rootKey )
796+ pemLeaf2 := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : leafCert2 .Raw })
797+
798+ opts = []static.Option {static .WithCertChain (pemLeaf2 , []byte {}), static .WithBundle (rekorBundle )}
799+ ociSig , _ = static .NewSignature (payload , base64 .StdEncoding .EncodeToString (signature1 ), opts ... )
800+
801+ _ , err = VerifyImageSignature (context .TODO (), ociSig , v1.Hash {},
802+ & CheckOpts {
803+ RootCerts : rootPool ,
804+ IgnoreSCT : true ,
805+ Identities : []Identity {{Subject : "subject@mail.com" , Issuer : "oidc-issuer" }},
806+ TrustedMaterial : trustedRoot })
807+ if err == nil || ! strings .Contains (err .Error (), "error verifying bundle: comparing public key PEMs" ) {
808+ t .Fatal (err )
809+ }
810+ }
811+
734812func TestValidateAndUnpackCertSuccess (t * testing.T ) {
735813 subject := "email@email"
736814 oidcIssuer := "https://accounts.google.com"
0 commit comments