/
fulcio.go
182 lines (151 loc) · 4.82 KB
/
fulcio.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
package fulcio
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/chainguard-dev/terraform-provider-cosign/internal/secant/types"
"github.com/google/certificate-transparency-go/x509"
"github.com/google/certificate-transparency-go/x509util"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/cosign/v2/pkg/oci"
"github.com/sigstore/cosign/v2/pkg/oci/static"
"github.com/sigstore/fulcio/pkg/api"
"github.com/sigstore/sigstore/pkg/cryptoutils"
"github.com/sigstore/sigstore/pkg/oauthflow"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/options"
"golang.org/x/oauth2"
)
var _ types.CosignerSignerVerifier = (*SignerVerifier)(nil)
// OIDCProvider is what providers need to implement to participate in furnishing OIDC tokens.
type OIDCProvider interface {
// Enabled returns true if the provider is enabled.
Enabled(ctx context.Context) bool
// Provide returns an OIDC token scoped to the provided audience.
Provide(ctx context.Context, audience string) (string, error)
}
// SignerVerifier implements types.CosignerSignerVerifier using "keyless" signatures.
// If its signing certificate expires, it will refresh it by requesting a new cert from fulcio
// using the same key.
type SignerVerifier struct {
inner signature.SignerVerifier
provider OIDCProvider
fulcioClient api.LegacyClient
// Protects these fields from mutating from refresh().
sync.Mutex
cert *x509.Certificate
certPEM []byte
chain []byte
sct []byte
}
// NewSigner returns a "keyless" fulcio signer.
func NewSigner(provider OIDCProvider, fulcioClient api.LegacyClient) (*SignerVerifier, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("generating cert: %w", err)
}
inner, err := signature.LoadECDSASignerVerifier(privKey, crypto.SHA256)
if err != nil {
return nil, err
}
sv := &SignerVerifier{
inner: inner,
provider: provider,
fulcioClient: fulcioClient,
}
return sv, nil
}
func (sv *SignerVerifier) refresh(ctx context.Context) error {
idToken, err := sv.provider.Provide(ctx, "sigstore")
if err != nil {
return err
}
flow := &oauthflow.StaticTokenGetter{RawToken: idToken}
tok, err := flow.GetIDToken(nil, oauth2.Config{})
if err != nil {
return err
}
publicKey, err := sv.inner.PublicKey()
if err != nil {
return err
}
pubBytes, err := cryptoutils.MarshalPublicKeyToPEM(publicKey)
if err != nil {
return err
}
// Sign the email address as part of the request
proof, err := sv.inner.SignMessage(strings.NewReader(tok.Subject))
if err != nil {
return err
}
cr := api.CertificateRequest{
PublicKey: api.Key{
Content: pubBytes,
},
SignedEmailAddress: proof,
}
resp, err := sv.fulcioClient.SigningCert(cr, tok.RawString)
if err != nil {
return fmt.Errorf("retrieving cert: %w", err)
}
// Grab the PublicKeys for the CTFE, either from tuf or env.
pubKeys, err := cosign.GetCTLogPubs(ctx)
if err != nil {
return fmt.Errorf("getting CTFE public keys: %w", err)
}
// verify the sct
if err := cosign.VerifySCT(ctx, resp.CertPEM, resp.ChainPEM, resp.SCT, pubKeys); err != nil {
return fmt.Errorf("verifying SCT: %w", err)
}
cert, err := x509util.CertificateFromPEM(resp.CertPEM)
if err != nil {
return fmt.Errorf("parsing cert PEM: %w", err)
}
sv.cert = cert
sv.certPEM = resp.CertPEM
sv.chain = resp.ChainPEM
sv.sct = resp.SCT
return nil
}
func (sv *SignerVerifier) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) {
return sv.inner.SignMessage(message, opts...)
}
func (sv *SignerVerifier) VerifySignature(signature, message io.Reader, opts ...signature.VerifyOption) error {
return sv.inner.VerifySignature(signature, message, opts...)
}
func (sv *SignerVerifier) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) {
return sv.inner.PublicKey(opts...)
}
func (sv *SignerVerifier) Cosign(ctx context.Context, payload io.Reader) (oci.Signature, error) {
payloadBytes, err := io.ReadAll(payload)
if err != nil {
return nil, err
}
signed, err := sv.inner.SignMessage(bytes.NewReader(payloadBytes), options.WithContext(ctx))
if err != nil {
return nil, err
}
sv.Lock()
defer sv.Unlock()
// If this is our first cert or the cert is expired, we need to hit fulcio.
// Add 30 seconds of buffer to account for clock skew (gitsign does the same).
if sv.cert == nil || time.Now().Add(time.Second*30).After(sv.cert.NotAfter) {
if err := sv.refresh(ctx); err != nil {
return nil, fmt.Errorf("refreshing fulcio cert: %w", err)
}
}
sig, err := static.NewSignature(payloadBytes, base64.StdEncoding.EncodeToString(signed), static.WithCertChain(sv.certPEM, sv.chain))
if err != nil {
return nil, err
}
return sig, nil
}