@@ -21,6 +21,7 @@ import (
21
21
"crypto/ecdsa"
22
22
"crypto/elliptic"
23
23
"crypto/rand"
24
+ "crypto/rsa"
24
25
"crypto/sha256"
25
26
"crypto/x509"
26
27
"crypto/x509/pkix"
@@ -38,6 +39,7 @@ import (
38
39
"github.com/go-openapi/runtime"
39
40
"github.com/go-openapi/swag"
40
41
"github.com/secure-systems-lab/go-securesystemslib/dsse"
42
+ v1 "github.com/sigstore/protobuf-specs/gen/pb-go/common/v1"
41
43
"github.com/sigstore/rekor/pkg/generated/models"
42
44
"github.com/sigstore/rekor/pkg/pki"
43
45
"github.com/sigstore/rekor/pkg/types"
@@ -56,16 +58,17 @@ import (
56
58
)
57
59
58
60
type VirtualSigstore struct {
59
- fulcioCA * root.FulcioCertificateAuthority
60
- fulcioIntermediateKey * ecdsa.PrivateKey
61
- tsaCA * root.SigstoreTimestampingAuthority
62
- tsaLeafKey * ecdsa.PrivateKey
63
- rekorKey * ecdsa.PrivateKey
64
- ctlogKey * ecdsa.PrivateKey
65
- publicKeyVerifier map [string ]root.TimeConstrainedVerifier
61
+ fulcioCA * root.FulcioCertificateAuthority
62
+ fulcioIntermediateKey * ecdsa.PrivateKey
63
+ tsaCA * root.SigstoreTimestampingAuthority
64
+ tsaLeafKey * ecdsa.PrivateKey
65
+ rekorKey * ecdsa.PrivateKey
66
+ ctlogKey * ecdsa.PrivateKey
67
+ publicKeyVerifier map [string ]root.TimeConstrainedVerifier
68
+ signingAlgorithmDetails signature.AlgorithmDetails
66
69
}
67
70
68
- func NewVirtualSigstore ( ) (* VirtualSigstore , error ) {
71
+ func NewVirtualSigstoreWithSigningAlg ( signingKeyDetails v1. PublicKeyDetails ) (* VirtualSigstore , error ) {
69
72
ss := & VirtualSigstore {fulcioCA : & root.FulcioCertificateAuthority {}, tsaCA : & root.SigstoreTimestampingAuthority {}}
70
73
71
74
rootCert , rootKey , err := GenerateRootCa ()
@@ -109,10 +112,18 @@ func NewVirtualSigstore() (*VirtualSigstore, error) {
109
112
if err != nil {
110
113
return nil , err
111
114
}
115
+ ss .signingAlgorithmDetails , err = signature .GetAlgorithmDetails (signingKeyDetails )
116
+ if err != nil {
117
+ return nil , err
118
+ }
112
119
113
120
return ss , nil
114
121
}
115
122
123
+ func NewVirtualSigstore () (* VirtualSigstore , error ) {
124
+ return NewVirtualSigstoreWithSigningAlg (v1 .PublicKeyDetails_PKIX_ECDSA_P256_SHA_256 )
125
+ }
126
+
116
127
// getLogID calculates the digest of a PKIX-encoded public key
117
128
func getLogID (pub crypto.PublicKey ) (string , error ) {
118
129
pubBytes , err := x509 .MarshalPKIXPublicKey (pub )
@@ -147,8 +158,25 @@ func (ca *VirtualSigstore) RekorSignPayload(payload tlog.RekorPayload) ([]byte,
147
158
return bundleSig , nil
148
159
}
149
160
150
- func (ca * VirtualSigstore ) GenerateLeafCert (identity , issuer string ) (* x509.Certificate , * ecdsa.PrivateKey , error ) {
151
- privKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
161
+ func (ca * VirtualSigstore ) GenerateLeafCert (identity , issuer string ) (* x509.Certificate , crypto.PrivateKey , error ) {
162
+ var privKey crypto.PrivateKey
163
+ var err error
164
+ switch ca .signingAlgorithmDetails .GetKeyType () {
165
+ case signature .ECDSA :
166
+ var curve * elliptic.Curve
167
+ curve , err = ca .signingAlgorithmDetails .GetECDSACurve ()
168
+ if err != nil {
169
+ return nil , nil , err
170
+ }
171
+ privKey , err = ecdsa .GenerateKey (* curve , rand .Reader )
172
+ case signature .RSA :
173
+ var keySize signature.RSAKeySize
174
+ keySize , err = ca .signingAlgorithmDetails .GetRSAKeySize ()
175
+ if err != nil {
176
+ return nil , nil , err
177
+ }
178
+ privKey , err = rsa .GenerateKey (rand .Reader , int (keySize ))
179
+ }
152
180
if err != nil {
153
181
return nil , nil , err
154
182
}
@@ -171,7 +199,7 @@ func (ca *VirtualSigstore) AttestAtTime(identity, issuer string, envelopeBody []
171
199
return nil , err
172
200
}
173
201
174
- signer , err := signature .LoadECDSASignerVerifier (leafPrivKey , crypto . SHA256 )
202
+ signer , err := signature .LoadSignerFromAlgorithmDetails (leafPrivKey , ca . signingAlgorithmDetails )
175
203
if err != nil {
176
204
return nil , err
177
205
}
@@ -213,21 +241,42 @@ func (ca *VirtualSigstore) AttestAtTime(identity, issuer string, envelopeBody []
213
241
}
214
242
215
243
func (ca * VirtualSigstore ) Sign (identity , issuer string , artifact []byte ) (* TestEntity , error ) {
216
- return ca .SignAtTime (identity , issuer , artifact , time .Now ().Add (5 * time .Minute ))
244
+ return ca .SignAtTimeWithVersion (identity , issuer , artifact , time .Now ().Add (5 * time .Minute ), "v0.3" )
245
+ }
246
+
247
+ func (ca * VirtualSigstore ) SignWithVersion (identity , issuer string , artifact []byte , version string ) (* TestEntity , error ) {
248
+ return ca .SignAtTimeWithVersion (identity , issuer , artifact , time .Now ().Add (5 * time .Minute ), version )
217
249
}
218
250
219
251
func (ca * VirtualSigstore ) SignAtTime (identity , issuer string , artifact []byte , integratedTime time.Time ) (* TestEntity , error ) {
252
+ return ca .SignAtTimeWithVersion (identity , issuer , artifact , integratedTime , "v0.3" )
253
+ }
254
+
255
+ func (ca * VirtualSigstore ) SignAtTimeWithVersion (identity , issuer string , artifact []byte , integratedTime time.Time , version string ) (* TestEntity , error ) {
220
256
leafCert , leafPrivKey , err := ca .GenerateLeafCert (identity , issuer )
221
257
if err != nil {
222
258
return nil , err
223
259
}
224
260
225
- signer , err := signature .LoadECDSASignerVerifier (leafPrivKey , crypto . SHA256 )
261
+ signer , err := signature .LoadSignerFromAlgorithmDetails (leafPrivKey , ca . signingAlgorithmDetails )
226
262
if err != nil {
227
263
return nil , err
228
264
}
229
265
230
- digest := sha256 .Sum256 (artifact )
266
+ hashType := ca .signingAlgorithmDetails .GetHashType ()
267
+ hasher := hashType .New ()
268
+ hasher .Write (artifact )
269
+ digest := hasher .Sum (nil )
270
+
271
+ var digestString string
272
+ switch hashType {
273
+ case crypto .SHA256 :
274
+ digestString = "SHA2_256"
275
+ case crypto .SHA384 :
276
+ digestString = "SHA2_384"
277
+ case crypto .SHA512 :
278
+ digestString = "SHA2_512"
279
+ }
231
280
sig , err := signer .SignMessage (bytes .NewReader (artifact ))
232
281
if err != nil {
233
282
return nil , err
@@ -246,8 +295,9 @@ func (ca *VirtualSigstore) SignAtTime(identity, issuer string, artifact []byte,
246
295
return & TestEntity {
247
296
certChain : []* x509.Certificate {leafCert , ca .fulcioCA .Intermediates [0 ], ca .fulcioCA .Root },
248
297
timestamps : [][]byte {tsr },
249
- messageSignature : bundle .NewMessageSignature (digest [:], "SHA2_256" , sig ),
298
+ messageSignature : bundle .NewMessageSignature (digest , digestString , sig ),
250
299
tlogEntries : []* tlog.Entry {entry },
300
+ version : version ,
251
301
}, nil
252
302
}
253
303
@@ -262,7 +312,7 @@ func (ca *VirtualSigstore) GenerateTlogEntry(leafCert *x509.Certificate, envelop
262
312
return nil , err
263
313
}
264
314
265
- rekorBody , err := generateRekorEntry (intoto .KIND , intoto .New ().DefaultVersion (), envelopeBytes , leafCertPem , sig )
315
+ rekorBody , err := generateRekorEntry (intoto .KIND , intoto .New ().DefaultVersion (), envelopeBytes , leafCertPem , sig , ca . signingAlgorithmDetails )
266
316
if err != nil {
267
317
return nil , err
268
318
}
@@ -327,7 +377,7 @@ func (ca *VirtualSigstore) generateTlogEntryHashedRekord(leafCert *x509.Certific
327
377
return nil , err
328
378
}
329
379
330
- rekorBody , err := generateRekorEntry (hashedrekord .KIND , hashedrekord .New ().DefaultVersion (), artifact , leafCertPem , sig )
380
+ rekorBody , err := generateRekorEntry (hashedrekord .KIND , hashedrekord .New ().DefaultVersion (), artifact , leafCertPem , sig , ca . signingAlgorithmDetails )
331
381
if err != nil {
332
382
return nil , err
333
383
}
@@ -366,9 +416,9 @@ func (ca *VirtualSigstore) PublicKeyVerifier(keyID string) (root.TimeConstrained
366
416
return v , nil
367
417
}
368
418
369
- func generateRekorEntry (kind , version string , artifact []byte , cert []byte , sig []byte ) (string , error ) {
419
+ func generateRekorEntry (kind , version string , artifact []byte , cert []byte , sig []byte , algorithmDetails signature. AlgorithmDetails ) (string , error ) {
370
420
// Generate the Rekor Entry
371
- entryImpl , err := createEntry (context .Background (), kind , version , artifact , cert , sig )
421
+ entryImpl , err := createEntry (context .Background (), kind , version , artifact , cert , sig , algorithmDetails )
372
422
if err != nil {
373
423
return "" , err
374
424
}
@@ -379,7 +429,7 @@ func generateRekorEntry(kind, version string, artifact []byte, cert []byte, sig
379
429
return base64 .StdEncoding .EncodeToString (entryBytes ), nil
380
430
}
381
431
382
- func createEntry (ctx context.Context , kind , apiVersion string , blobBytes , certBytes , sigBytes []byte ) (types.EntryImpl , error ) {
432
+ func createEntry (ctx context.Context , kind , apiVersion string , blobBytes , certBytes , sigBytes []byte , algorithmDetails signature. AlgorithmDetails ) (types.EntryImpl , error ) {
383
433
props := types.ArtifactProperties {
384
434
PublicKeyBytes : [][]byte {certBytes },
385
435
PKIFormat : string (pki .X509 ),
@@ -389,8 +439,12 @@ func createEntry(ctx context.Context, kind, apiVersion string, blobBytes, certBy
389
439
props .ArtifactBytes = blobBytes
390
440
props .SignatureBytes = sigBytes
391
441
case hashedrekord .KIND :
392
- blobHash := sha256 .Sum256 (blobBytes )
393
- props .ArtifactHash = strings .ToLower (hex .EncodeToString (blobHash [:]))
442
+ hashType := algorithmDetails .GetHashType ()
443
+ hasher := hashType .New ()
444
+ hasher .Write (blobBytes )
445
+ blobHash := hasher .Sum (nil )
446
+
447
+ props .ArtifactHash = strings .ToLower (hex .EncodeToString (blobHash ))
394
448
props .SignatureBytes = sigBytes
395
449
default :
396
450
return nil , fmt .Errorf ("unexpected entry kind: %s" , kind )
@@ -513,6 +567,7 @@ type TestEntity struct {
513
567
messageSignature * bundle.MessageSignature
514
568
timestamps [][]byte
515
569
tlogEntries []* tlog.Entry
570
+ version string
516
571
}
517
572
518
573
func (e * TestEntity ) VerificationContent () (verify.VerificationContent , error ) {
@@ -523,6 +578,10 @@ func (e *TestEntity) HasInclusionPromise() bool {
523
578
return true
524
579
}
525
580
581
+ func (e * TestEntity ) Version () (string , error ) {
582
+ return e .version , nil
583
+ }
584
+
526
585
func (e * TestEntity ) HasInclusionProof () bool {
527
586
for _ , tlog := range e .tlogEntries {
528
587
if tlog .HasInclusionProof () {
@@ -645,7 +704,7 @@ func GenerateTSAIntermediate(rootTemplate *x509.Certificate, rootPriv crypto.Sig
645
704
return cert , priv , nil
646
705
}
647
706
648
- func GenerateLeafCert (subject string , oidcIssuer string , expiration time.Time , priv * ecdsa .PrivateKey ,
707
+ func GenerateLeafCert (subject string , oidcIssuer string , expiration time.Time , priv crypto .PrivateKey ,
649
708
parentTemplate * x509.Certificate , parentPriv crypto.Signer ) (* x509.Certificate , error ) {
650
709
certTemplate := & x509.Certificate {
651
710
SerialNumber : big .NewInt (1 ),
@@ -664,7 +723,12 @@ func GenerateLeafCert(subject string, oidcIssuer string, expiration time.Time, p
664
723
},
665
724
}
666
725
667
- cert , err := createCertificate (certTemplate , parentTemplate , & priv .PublicKey , parentPriv )
726
+ signer , ok := priv .(crypto.Signer )
727
+ if ! ok {
728
+ return nil , fmt .Errorf ("private key does not implement crypto.Signer" )
729
+ }
730
+
731
+ cert , err := createCertificate (certTemplate , parentTemplate , signer .Public (), parentPriv )
668
732
if err != nil {
669
733
return nil , err
670
734
}
0 commit comments