@@ -6,12 +6,15 @@ import (
66 "crypto/rand"
77 "crypto/x509"
88 "crypto/x509/pkix"
9+ "encoding/base64"
10+ "encoding/json"
911 "encoding/pem"
1012 "math/big"
1113 "os"
1214 "path/filepath"
1315 "testing"
1416
17+ "github.com/sigstore/rekor/pkg/generated/models"
1518 "github.com/stretchr/testify/assert"
1619 "github.com/stretchr/testify/require"
1720 "gopkg.in/yaml.v3"
@@ -126,6 +129,92 @@ func TestExtractECDSAPublicKey_UnsupportedPEMType(t *testing.T) {
126129 assert .EqualError (t , err , "unsupported PEM block type: UNSUPPORTED" )
127130}
128131
132+ // Negative: Digest mismatch by passing wrong digest (no bundle mutation)
133+ func TestVerify_DigestMismatch (t * testing.T ) {
134+ descriptorYAML := loadTestData (t , "component-descriptor-signed.yaml" )
135+ realDigest , sigValue := getSignatureByAlgorithm (t , descriptorYAML , AlgorithmV2 )
136+
137+ wrongDigest := "deadbeef" + realDigest
138+
139+ handler := Handler {algorithm : AlgorithmV2 }
140+ err := handler .Verify (wrongDigest , & signing.Signature {
141+ Value : sigValue ,
142+ MediaType : MediaType ,
143+ Algorithm : AlgorithmV2 ,
144+ }, nil )
145+
146+ assert .EqualError (t , err , "rekor hash doesn't match provided digest" )
147+ }
148+
149+ // Negative: Invalid signature bytes
150+ func TestVerify_InvalidSignature (t * testing.T ) {
151+ descriptorYAML := loadTestData (t , "component-descriptor-signed.yaml" )
152+ digest , sigValue := getSignatureByAlgorithm (t , descriptorYAML , AlgorithmV2 )
153+
154+ // decode bundle
155+ var entries map [string ]any
156+ data , err := base64 .StdEncoding .DecodeString (sigValue )
157+ require .NoError (t , err )
158+ require .NoError (t , json .Unmarshal (data , & entries ))
159+
160+ // mutate signature content of first entry
161+ for k , v := range entries {
162+ entry := v .(map [string ]any )
163+ bodyB64 := entry ["body" ].(string )
164+ bodyJSONRaw , err := base64 .StdEncoding .DecodeString (bodyB64 )
165+ require .NoError (t , err )
166+
167+ var rekorEntry models.Hashedrekord
168+ require .NoError (t , json .Unmarshal (bodyJSONRaw , & rekorEntry ))
169+
170+ rekorSpec := rekorEntry .Spec .(map [string ]any )
171+ sigField := rekorSpec ["signature" ].(map [string ]any )
172+ content := sigField ["content" ].(string )
173+ sigBytes , err := base64 .StdEncoding .DecodeString (content )
174+ require .NoError (t , err )
175+ // flip one bit
176+ sigBytes [0 ] ^= 0x01
177+ sigField ["content" ] = base64 .StdEncoding .EncodeToString (sigBytes )
178+
179+ mutBody , err := json .Marshal (rekorEntry )
180+ require .NoError (t , err )
181+ entry ["body" ] = base64 .StdEncoding .EncodeToString (mutBody )
182+ entries [k ] = entry
183+ break
184+ }
185+
186+ // re-encode bundle
187+ mutData , err := json .Marshal (entries )
188+ require .NoError (t , err )
189+ mutated := base64 .StdEncoding .EncodeToString (mutData )
190+
191+ // verify mutated signature
192+ handler := Handler {algorithm : AlgorithmV2 }
193+ err = handler .Verify (digest , & signing.Signature {
194+ Value : mutated ,
195+ MediaType : MediaType ,
196+ Algorithm : AlgorithmV2 ,
197+ }, nil )
198+
199+ assert .EqualError (t , err , "could not verify signature using public key" )
200+ }
201+
202+ // Test handler for sigstore-v2 is registered and usable via signing registry
203+ func TestHandlerRegistry_RegisteredAndUsable (t * testing.T ) {
204+ verifier := signing .DefaultHandlerRegistry ().GetVerifier (AlgorithmV2 )
205+ require .NotNil (t , verifier , "v2 verifier should be registered" )
206+
207+ descriptorYAML := loadTestData (t , "component-descriptor-signed.yaml" )
208+ digest , sigValue := getSignatureByAlgorithm (t , descriptorYAML , AlgorithmV2 )
209+
210+ err := verifier .Verify (digest , & signing.Signature {
211+ Value : sigValue ,
212+ MediaType : MediaType ,
213+ Algorithm : AlgorithmV2 ,
214+ }, nil )
215+ assert .NoError (t , err , "verification via registry for v2 should succeed" )
216+ }
217+
129218// Verify signatures with both Sigstore algorithms (works offline,
130219// as Rekor public keys are embedded in Cosign library and
131220// all verification data contained in Sigstore bundle)
0 commit comments