forked from sigstore/cosign
/
verify.go
802 lines (708 loc) · 22.4 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cosign
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strings"
"time"
cbundle "github.com/sigstore/cosign/pkg/cosign/bundle"
"github.com/sigstore/cosign/pkg/blob"
"github.com/sigstore/cosign/pkg/oci/static"
"github.com/sigstore/cosign/pkg/types"
"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
ssldsse "github.com/secure-systems-lab/go-securesystemslib/dsse"
"github.com/sigstore/cosign/pkg/oci"
"github.com/sigstore/cosign/pkg/oci/layout"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/sigstore/pkg/cryptoutils"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/dsse"
"github.com/sigstore/sigstore/pkg/signature/options"
sigPayload "github.com/sigstore/sigstore/pkg/signature/payload"
)
// CheckOpts are the options for checking signatures.
type CheckOpts struct {
// RegistryClientOpts are the options for interacting with the container registry.
RegistryClientOpts []ociremote.Option
// Annotations optionally specifies image signature annotations to verify.
Annotations map[string]interface{}
// ClaimVerifier, if provided, verifies claims present in the oci.Signature.
ClaimVerifier func(sig oci.Signature, imageDigest v1.Hash, annotations map[string]interface{}) error
// RekorClient, if set, is used to use to verify signatures and public keys.
RekorClient *client.Rekor
// SigVerifier is used to verify signatures.
SigVerifier signature.Verifier
// PKOpts are the options provided to `SigVerifier.PublicKey()`.
PKOpts []signature.PublicKeyOption
// RootCerts are the root CA certs used to verify a signature's chained certificate.
RootCerts *x509.CertPool
// CertEmail is the email expected for a certificate to be valid. The empty string means any certificate can be valid.
CertEmail string
// CertOidcIssuer is the OIDC issuer expected for a certificate to be valid. The empty string means any certificate can be valid.
CertOidcIssuer string
// SignatureRef is the reference to the signature file
SignatureRef string
}
func getSignedEntity(signedImgRef name.Reference, regClientOpts []ociremote.Option) (oci.SignedEntity, v1.Hash, error) {
se, err := ociremote.SignedEntity(signedImgRef, regClientOpts...)
if err != nil {
return nil, v1.Hash{}, err
}
// Both of the SignedEntity types implement Digest()
h, err := se.(interface{ Digest() (v1.Hash, error) }).Digest()
if err != nil {
return nil, v1.Hash{}, err
}
return se, h, nil
}
func verifyOCISignature(ctx context.Context, verifier signature.Verifier, sig oci.Signature) error {
b64sig, err := sig.Base64Signature()
if err != nil {
return err
}
signature, err := base64.StdEncoding.DecodeString(b64sig)
if err != nil {
return err
}
payload, err := sig.Payload()
if err != nil {
return err
}
return verifier.VerifySignature(bytes.NewReader(signature), bytes.NewReader(payload), options.WithContext(ctx))
}
// For unit testing
type payloader interface {
Payload() ([]byte, error)
}
func verifyOCIAttestation(_ context.Context, verifier signature.Verifier, att payloader) error {
payload, err := att.Payload()
if err != nil {
return err
}
env := ssldsse.Envelope{}
if err := json.Unmarshal(payload, &env); err != nil {
return err
}
if env.PayloadType != types.IntotoPayloadType {
return fmt.Errorf("invalid payloadType %s on envelope. Expected %s", env.PayloadType, types.IntotoPayloadType)
}
dssev, err := ssldsse.NewEnvelopeVerifier(&dsse.VerifierAdapter{SignatureVerifier: verifier})
if err != nil {
return err
}
_, err = dssev.Verify(&env)
return err
}
// ValidateAndUnpackCert creates a Verifier from a certificate. Veries that the certificate
// chains up to a trusted root. Optionally verifies the subject of the certificate.
func ValidateAndUnpackCert(cert *x509.Certificate, co *CheckOpts) (signature.Verifier, error) {
verifier, err := signature.LoadECDSAVerifier(cert.PublicKey.(*ecdsa.PublicKey), crypto.SHA256)
if err != nil {
return nil, errors.Wrap(err, "invalid certificate found on signature")
}
// Now verify the cert, then the signature.
if err := TrustedCert(cert, co.RootCerts); err != nil {
return nil, err
}
if co.CertEmail != "" {
emailVerified := false
for _, em := range cert.EmailAddresses {
if co.CertEmail == em {
emailVerified = true
break
}
}
if !emailVerified {
return nil, errors.New("expected email not found in certificate")
}
}
if co.CertOidcIssuer != "" {
issuer := ""
for _, ext := range cert.Extensions {
if ext.Id.Equal(asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 57264, 1, 1}) {
issuer = string(ext.Value)
break
}
}
if issuer != co.CertOidcIssuer {
return nil, errors.New("expected oidc issuer not found in certificate")
}
}
return verifier, nil
}
func tlogValidatePublicKey(ctx context.Context, rekorClient *client.Rekor, pub crypto.PublicKey, sig oci.Signature) error {
pemBytes, err := cryptoutils.MarshalPublicKeyToPEM(pub)
if err != nil {
return err
}
b64sig, err := sig.Base64Signature()
if err != nil {
return err
}
payload, err := sig.Payload()
if err != nil {
return err
}
_, _, err = FindTlogEntry(ctx, rekorClient, b64sig, payload, pemBytes)
return err
}
func tlogValidateCertificate(ctx context.Context, rekorClient *client.Rekor, sig oci.Signature) error {
cert, err := sig.Cert()
if err != nil {
return err
}
pemBytes, err := cryptoutils.MarshalCertificateToPEM(cert)
if err != nil {
return err
}
b64sig, err := sig.Base64Signature()
if err != nil {
return err
}
payload, err := sig.Payload()
if err != nil {
return err
}
uuid, _, err := FindTlogEntry(ctx, rekorClient, b64sig, payload, pemBytes)
if err != nil {
return err
}
// if we have a cert, we should check expiry
// The IntegratedTime verified in VerifyTlog
e, err := GetTlogEntry(ctx, rekorClient, uuid)
if err != nil {
return err
}
return CheckExpiry(cert, time.Unix(*e.IntegratedTime, 0))
}
type fakeOCISignatures struct {
oci.Signatures
signatures []oci.Signature
}
func (fos *fakeOCISignatures) Get() ([]oci.Signature, error) {
return fos.signatures, nil
}
// VerifyImageSignatures does all the main cosign checks in a loop, returning the verified signatures.
// If there were no valid signatures, we return an error.
func VerifyImageSignatures(ctx context.Context, signedImgRef name.Reference, co *CheckOpts) (checkedSignatures []oci.Signature, bundleVerified bool, err error) {
// Enforce this up front.
if co.RootCerts == nil && co.SigVerifier == nil {
return nil, false, errors.New("one of verifier or root certs is required")
}
// TODO(mattmoor): We could implement recursive verification if we just wrapped
// most of the logic below here in a call to mutate.Map
se, h, err := getSignedEntity(signedImgRef, co.RegistryClientOpts)
if err != nil {
return nil, false, err
}
var sigs oci.Signatures
sigRef := co.SignatureRef
if sigRef == "" {
sigs, err = se.Signatures()
if err != nil {
return nil, false, err
}
} else {
sigs, err = loadSignatureFromFile(sigRef, signedImgRef, co)
if err != nil {
return nil, false, err
}
}
return verifySignatures(ctx, sigs, h, co)
}
// VerifyLocalImageSignatures verifies signatures from a saved, local image, without any network calls, returning the verified signatures.
// If there were no valid signatures, we return an error.
func VerifyLocalImageSignatures(ctx context.Context, path string, co *CheckOpts) (checkedSignatures []oci.Signature, bundleVerified bool, err error) {
// Enforce this up front.
if co.RootCerts == nil && co.SigVerifier == nil {
return nil, false, errors.New("one of verifier or root certs is required")
}
se, err := layout.SignedImageIndex(path)
if err != nil {
return nil, false, err
}
var h v1.Hash
// Verify either an image index or image.
ii, err := se.SignedImageIndex(v1.Hash{})
if err != nil {
return nil, false, err
}
i, err := se.SignedImage(v1.Hash{})
if err != nil {
return nil, false, err
}
switch {
case ii != nil:
h, err = ii.Digest()
if err != nil {
return nil, false, err
}
case i != nil:
h, err = i.Digest()
if err != nil {
return nil, false, err
}
default:
return nil, false, errors.New("must verify either an image index or image")
}
sigs, err := se.Signatures()
if err != nil {
return nil, false, err
}
return verifySignatures(ctx, sigs, h, co)
}
func verifySignatures(ctx context.Context, sigs oci.Signatures, h v1.Hash, co *CheckOpts) (checkedSignatures []oci.Signature, bundleVerified bool, err error) {
sl, err := sigs.Get()
if err != nil {
return nil, false, err
}
validationErrs := []string{}
for _, sig := range sl {
verified, err := VerifyImageSignature(ctx, sig, h, co)
bundleVerified = bundleVerified || verified
if err != nil {
validationErrs = append(validationErrs, err.Error())
continue
}
// Phew, we made it.
checkedSignatures = append(checkedSignatures, sig)
}
if len(checkedSignatures) == 0 {
return nil, false, fmt.Errorf("no matching signatures:\n%s", strings.Join(validationErrs, "\n "))
}
return checkedSignatures, bundleVerified, nil
}
// VerifyImageSignature verifies a signature
func VerifyImageSignature(ctx context.Context, sig oci.Signature, h v1.Hash, co *CheckOpts) (bundleVerified bool, err error) {
verifier := co.SigVerifier
if verifier == nil {
// If we don't have a public key to check against, we can try a root cert.
cert, err := sig.Cert()
if err != nil {
return bundleVerified, err
}
if cert == nil {
return bundleVerified, errors.New("no certificate found on signature")
}
verifier, err = ValidateAndUnpackCert(cert, co)
if err != nil {
return bundleVerified, err
}
}
if err := verifyOCISignature(ctx, verifier, sig); err != nil {
return bundleVerified, err
}
// We can't check annotations without claims, both require unmarshalling the payload.
if co.ClaimVerifier != nil {
if err := co.ClaimVerifier(sig, h, co.Annotations); err != nil {
return bundleVerified, err
}
}
bundleVerified, err = VerifyBundle(ctx, sig)
if err != nil && co.RekorClient == nil {
return false, errors.Wrap(err, "unable to verify bundle")
}
if !bundleVerified && co.RekorClient != nil {
if co.SigVerifier != nil {
pub, err := co.SigVerifier.PublicKey(co.PKOpts...)
if err != nil {
return bundleVerified, err
}
return bundleVerified, tlogValidatePublicKey(ctx, co.RekorClient, pub, sig)
}
return bundleVerified, tlogValidateCertificate(ctx, co.RekorClient, sig)
}
return bundleVerified, nil
}
func loadSignatureFromFile(sigRef string, signedImgRef name.Reference, co *CheckOpts) (oci.Signatures, error) {
var b64sig string
targetSig, err := blob.LoadFileOrURL(sigRef)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
targetSig = []byte(sigRef)
}
_, err = base64.StdEncoding.DecodeString(string(targetSig))
if err == nil {
b64sig = string(targetSig)
} else {
b64sig = base64.StdEncoding.EncodeToString(targetSig)
}
digest, err := ociremote.ResolveDigest(signedImgRef, co.RegistryClientOpts...)
if err != nil {
return nil, err
}
payload, err := (&sigPayload.Cosign{Image: digest}).MarshalJSON()
if err != nil {
return nil, err
}
sig, err := static.NewSignature(payload, b64sig)
if err != nil {
return nil, err
}
return &fakeOCISignatures{
signatures: []oci.Signature{sig},
}, nil
}
// VerifyAttestations does all the main cosign checks in a loop, returning the verified attestations.
// If there were no valid attestations, we return an error.
func VerifyImageAttestations(ctx context.Context, signedImgRef name.Reference, co *CheckOpts) (checkedAttestations []oci.Signature, bundleVerified bool, err error) {
// Enforce this up front.
if co.RootCerts == nil && co.SigVerifier == nil {
return nil, false, errors.New("one of verifier or root certs is required")
}
// TODO(mattmoor): We could implement recursive verification if we just wrapped
// most of the logic below here in a call to mutate.Map
se, h, err := getSignedEntity(signedImgRef, co.RegistryClientOpts)
if err != nil {
return nil, false, err
}
atts, err := se.Attestations()
if err != nil {
return nil, false, err
}
return verifyImageAttestations(ctx, atts, h, co)
}
// VerifyLocalImageAttestations verifies attestations from a saved, local image, without any network calls,
// returning the verified attestations.
// If there were no valid signatures, we return an error.
func VerifyLocalImageAttestations(ctx context.Context, path string, co *CheckOpts) (checkedAttestations []oci.Signature, bundleVerified bool, err error) {
// Enforce this up front.
if co.RootCerts == nil && co.SigVerifier == nil {
return nil, false, errors.New("one of verifier or root certs is required")
}
se, err := layout.SignedImageIndex(path)
if err != nil {
return nil, false, err
}
var h v1.Hash
// Verify either an image index or image.
ii, err := se.SignedImageIndex(v1.Hash{})
if err != nil {
return nil, false, err
}
i, err := se.SignedImage(v1.Hash{})
if err != nil {
return nil, false, err
}
switch {
case ii != nil:
h, err = ii.Digest()
if err != nil {
return nil, false, err
}
case i != nil:
h, err = i.Digest()
if err != nil {
return nil, false, err
}
default:
return nil, false, errors.New("must verify either an image index or image")
}
atts, err := se.Attestations()
if err != nil {
return nil, false, err
}
return verifyImageAttestations(ctx, atts, h, co)
}
func verifyImageAttestations(ctx context.Context, atts oci.Signatures, h v1.Hash, co *CheckOpts) (checkedAttestations []oci.Signature, bundleVerified bool, err error) {
sl, err := atts.Get()
if err != nil {
return nil, false, err
}
validationErrs := []string{}
for _, att := range sl {
if err := func(att oci.Signature) error {
verifier := co.SigVerifier
if verifier == nil {
// If we don't have a public key to check against, we can try a root cert.
cert, err := att.Cert()
if err != nil {
return err
}
if cert == nil {
return errors.New("no certificate found on attestation")
}
verifier, err = ValidateAndUnpackCert(cert, co)
if err != nil {
return err
}
}
if err := verifyOCIAttestation(ctx, verifier, att); err != nil {
return err
}
// We can't check annotations without claims, both require unmarshalling the payload.
if co.ClaimVerifier != nil {
if err := co.ClaimVerifier(att, h, co.Annotations); err != nil {
return err
}
}
verified, err := VerifyBundle(ctx, att)
if err != nil && co.RekorClient == nil {
return errors.Wrap(err, "unable to verify bundle")
}
bundleVerified = bundleVerified || verified
if !verified && co.RekorClient != nil {
if co.SigVerifier != nil {
pub, err := co.SigVerifier.PublicKey(co.PKOpts...)
if err != nil {
return err
}
return tlogValidatePublicKey(ctx, co.RekorClient, pub, att)
}
return tlogValidateCertificate(ctx, co.RekorClient, att)
}
return nil
}(att); err != nil {
validationErrs = append(validationErrs, err.Error())
continue
}
// Phew, we made it.
checkedAttestations = append(checkedAttestations, att)
}
if len(checkedAttestations) == 0 {
return nil, false, fmt.Errorf("no matching attestations:\n%s", strings.Join(validationErrs, "\n "))
}
return checkedAttestations, bundleVerified, nil
}
// CheckExpiry confirms the time provided is within the valid period of the cert
func CheckExpiry(cert *x509.Certificate, it time.Time) error {
ft := func(t time.Time) string {
return t.Format(time.RFC3339)
}
if cert.NotAfter.Before(it) {
return fmt.Errorf("certificate expired before signatures were entered in log: %s is before %s",
ft(cert.NotAfter), ft(it))
}
if cert.NotBefore.After(it) {
return fmt.Errorf("certificate was issued after signatures were entered in log: %s is after %s",
ft(cert.NotAfter), ft(it))
}
return nil
}
func VerifyBundle(ctx context.Context, sig oci.Signature) (bool, error) {
bundle, err := sig.Bundle()
if err != nil {
return false, err
} else if bundle == nil {
return false, nil
}
if err := compareSigs(bundle.Payload.Body.(string), sig); err != nil {
return false, err
}
pub, err := GetRekorPub(ctx)
if err != nil {
return false, errors.Wrap(err, "retrieving rekor public key")
}
rekorPubKey, err := PemToECDSAKey(pub)
if err != nil {
return false, errors.Wrap(err, "pem to ecdsa")
}
if err := VerifySET(bundle.Payload, bundle.SignedEntryTimestamp, rekorPubKey); err != nil {
return false, err
}
cert, err := sig.Cert()
if err != nil {
return false, err
} else if cert == nil {
return true, nil
}
// verify the cert against the integrated time
if err := CheckExpiry(cert, time.Unix(bundle.Payload.IntegratedTime, 0)); err != nil {
return false, errors.Wrap(err, "checking expiry on cert")
}
payload, err := sig.Payload()
if err != nil {
return false, errors.Wrap(err, "reading payload")
}
signature, err := sig.Base64Signature()
if err != nil {
return false, errors.Wrap(err, "reading base64signature")
}
alg, bundlehash, err := bundleHash(bundle.Payload.Body.(string), signature)
h := sha256.Sum256(payload)
payloadHash := hex.EncodeToString(h[:])
if alg != "sha256" || bundlehash != payloadHash {
return false, errors.Wrap(err, "matching bundle to payload")
}
return true, nil
}
// compare bundle signature to the signature we are verifying
func compareSigs(bundleBody string, sig oci.Signature) error {
// TODO(nsmith5): modify function signature to make it more clear _why_
// we've returned nil (there are several reasons possible here).
actualSig, err := sig.Base64Signature()
if err != nil {
return errors.Wrap(err, "base64 signature")
}
if actualSig == "" {
// NB: empty sig means this is an attestation
return nil
}
bundleSignature, err := bundleSig(bundleBody)
if err != nil {
return errors.Wrap(err, "failed to extract signature from bundle")
}
if bundleSignature == "" {
return nil
}
if bundleSignature != actualSig {
return fmt.Errorf("signature in bundle does not match signature being verified")
}
return nil
}
func bundleHash(bundleBody, signature string) (string, string, error) {
var toto models.Intoto
var rekord models.Rekord
var hrekord models.Hashedrekord
var intotoObj models.IntotoV001Schema
var rekordObj models.RekordV001Schema
var hrekordObj models.HashedrekordV001Schema
bodyDecoded, err := base64.StdEncoding.DecodeString(bundleBody)
if err != nil {
return "", "", err
}
// The fact that there's no signature (or empty rather), implies
// that this is an Attestation that we're verifying.
if len(signature) == 0 {
err = json.Unmarshal(bodyDecoded, &toto)
if err != nil {
return "", "", err
}
specMarshal, err := json.Marshal(toto.Spec)
if err != nil {
return "", "", err
}
err = json.Unmarshal(specMarshal, &intotoObj)
if err != nil {
return "", "", err
}
return *intotoObj.Content.Hash.Algorithm, *intotoObj.Content.Hash.Value, nil
}
if err := json.Unmarshal(bodyDecoded, &rekord); err == nil {
specMarshal, err := json.Marshal(rekord.Spec)
if err != nil {
return "", "", err
}
err = json.Unmarshal(specMarshal, &rekordObj)
if err != nil {
return "", "", err
}
return *rekordObj.Data.Hash.Algorithm, *rekordObj.Data.Hash.Value, nil
}
// Try hashedRekordObj
err = json.Unmarshal(bodyDecoded, &hrekord)
if err != nil {
return "", "", err
}
specMarshal, err := json.Marshal(hrekord.Spec)
if err != nil {
return "", "", err
}
err = json.Unmarshal(specMarshal, &hrekordObj)
if err != nil {
return "", "", err
}
return *hrekordObj.Data.Hash.Algorithm, *hrekordObj.Data.Hash.Value, nil
}
// bundleSig extracts the signature from the rekor bundle body
func bundleSig(bundleBody string) (string, error) {
var rekord models.Rekord
var hrekord models.Hashedrekord
var rekordObj models.RekordV001Schema
var hrekordObj models.HashedrekordV001Schema
bodyDecoded, err := base64.StdEncoding.DecodeString(bundleBody)
if err != nil {
return "", errors.Wrap(err, "decoding bundleBody")
}
// Try Rekord
if err := json.Unmarshal(bodyDecoded, &rekord); err == nil {
specMarshal, err := json.Marshal(rekord.Spec)
if err != nil {
return "", err
}
if err := json.Unmarshal(specMarshal, &rekordObj); err != nil {
return "", err
}
return rekordObj.Signature.Content.String(), nil
}
// Try hashedRekordObj
if err := json.Unmarshal(bodyDecoded, &hrekord); err != nil {
return "", err
}
specMarshal, err := json.Marshal(hrekord.Spec)
if err != nil {
return "", err
}
if err := json.Unmarshal(specMarshal, &hrekordObj); err != nil {
return "", err
}
return hrekordObj.Signature.Content.String(), nil
}
func VerifySET(bundlePayload cbundle.RekorPayload, signature []byte, pub *ecdsa.PublicKey) error {
contents, err := json.Marshal(bundlePayload)
if err != nil {
return errors.Wrap(err, "marshaling")
}
canonicalized, err := jsoncanonicalizer.Transform(contents)
if err != nil {
return errors.Wrap(err, "canonicalizing")
}
// verify the SET against the public key
hash := sha256.Sum256(canonicalized)
if !ecdsa.VerifyASN1(pub, hash[:], signature) {
return errors.New("unable to verify")
}
return nil
}
func TrustedCert(cert *x509.Certificate, roots *x509.CertPool) error {
if _, err := cert.Verify(x509.VerifyOptions{
// THIS IS IMPORTANT: WE DO NOT CHECK TIMES HERE
// THE CERTIFICATE IS TREATED AS TRUSTED FOREVER
// WE CHECK THAT THE SIGNATURES WERE CREATED DURING THIS WINDOW
CurrentTime: cert.NotBefore,
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{
x509.ExtKeyUsageCodeSigning,
},
}); err != nil {
return err
}
return nil
}
func correctAnnotations(wanted, have map[string]interface{}) bool {
for k, v := range wanted {
if have[k] != v {
return false
}
}
return true
}