-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsigstore.go
125 lines (111 loc) · 3.42 KB
/
sigstore.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
package sigstore
import (
_ "embed"
"encoding/hex"
"fmt"
"strings"
protobundle "github.com/sigstore/protobuf-specs/gen/pb-go/bundle/v1"
"github.com/sigstore/sigstore-go/pkg/bundle"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/tuf"
"github.com/sigstore/sigstore-go/pkg/verify"
"github.com/tinfoilsh/verifier/attestation"
)
const (
oidcIssuer = "https://token.actions.githubusercontent.com"
)
// FetchTrustRoot fetches the trust root from the Sigstore TUF repo
func FetchTrustRoot() ([]byte, error) {
tufOpts := tuf.
DefaultOptions().
WithDisableLocalCache()
//WithFetcher(util.NewFetcher())
client, err := tuf.New(tufOpts)
if err != nil {
return nil, err
}
return client.GetTarget("trusted_root.json")
}
// VerifyAttestation verifies the attested measurements of an enclave image
// against a trusted root (Sigstore) and returns the measurement payload contained in the DSSE.
func VerifyAttestation(
trustRootJSON, bundleJSON []byte,
hexDigest, repo string,
) (*attestation.Measurement, error) {
trustRoot, err := root.NewTrustedRootFromJSON(trustRootJSON)
if err != nil {
return nil, fmt.Errorf("parsing trust root: %w", err)
}
var b bundle.Bundle
b.Bundle = new(protobundle.Bundle)
if err := b.UnmarshalJSON(bundleJSON); err != nil {
return nil, fmt.Errorf("parsing bundle: %w", err)
}
verifier, err := verify.NewSignedEntityVerifier(
trustRoot,
verify.WithSignedCertificateTimestamps(1),
verify.WithTransparencyLog(1),
verify.WithObserverTimestamps(1),
)
if err != nil {
return nil, fmt.Errorf("creating signed entity verifier: %w", err)
}
certID, err := verify.NewShortCertificateIdentity(
oidcIssuer,
"",
"",
// TODO: Can we pin this to latest without fetching the latest release?
"^https://github.com/"+repo+"/.github/workflows/.*@refs/tags/*",
)
if err != nil {
return nil, fmt.Errorf("creating certificate identity: %w", err)
}
//
// WARNING: This is a temporary hack to get around our GitHub repo migration from tinfoilanalytics to tinfoilsh.
//
fallbackCertID, err := verify.NewShortCertificateIdentity(
oidcIssuer,
"",
"",
"^https://github.com/"+strings.ReplaceAll(repo, "tinfoilsh", "tinfoilanalytics")+"/.github/workflows/.*@refs/tags/*",
)
if err != nil {
return nil, fmt.Errorf("creating certificate identity: %w", err)
}
digest, err := hex.DecodeString(hexDigest)
if err != nil {
return nil, fmt.Errorf("decoding hex digest: %w", err)
}
result, err := verifier.Verify(
&b,
verify.NewPolicy(
verify.WithArtifactDigest("sha256", digest),
verify.WithCertificateIdentity(certID),
verify.WithCertificateIdentity(fallbackCertID),
),
)
if err != nil {
return nil, fmt.Errorf("verifying: %w", err)
}
predicate := result.Statement.Predicate
predicateFields := predicate.Fields
measurementType := attestation.PredicateType(result.Statement.PredicateType)
switch measurementType {
case attestation.AWSNitroEnclaveV1:
return &attestation.Measurement{
Type: measurementType,
Registers: []string{
predicateFields["PCR0"].GetStringValue(),
predicateFields["PCR1"].GetStringValue(),
predicateFields["PCR2"].GetStringValue(),
},
}, nil
case attestation.SevGuestV1:
return &attestation.Measurement{
Type: measurementType,
Registers: []string{predicateFields["measurement"].GetStringValue()},
}, nil
default:
return nil, fmt.Errorf("unsupported predicate type: %s", result.Statement.PredicateType)
}
}