-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathsigner.go
More file actions
321 lines (267 loc) · 8.68 KB
/
signer.go
File metadata and controls
321 lines (267 loc) · 8.68 KB
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
// Package gcp implements a signer.Signer backed by Google Cloud KMS.
// It delegates signing to a remote KMS key and caches the public key locally.
package gcp
import (
"context"
"crypto/ed25519"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"hash/crc32"
"net"
"sync"
"time"
kms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
"github.com/libp2p/go-libp2p/core/crypto"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
const (
baseRetryBackoff = 100 * time.Millisecond
maxRetryBackoff = 5 * time.Second
)
// KMSClient is the subset of the Google Cloud KMS client API that KmsSigner needs.
// This allows mocking in tests.
type KMSClient interface {
AsymmetricSign(ctx context.Context, req *kmspb.AsymmetricSignRequest) (*kmspb.AsymmetricSignResponse, error)
GetPublicKey(ctx context.Context, req *kmspb.GetPublicKeyRequest) (*kmspb.PublicKey, error)
}
type cloudKMSClient struct {
client *kms.KeyManagementClient
}
func (c *cloudKMSClient) AsymmetricSign(ctx context.Context, req *kmspb.AsymmetricSignRequest) (*kmspb.AsymmetricSignResponse, error) {
return c.client.AsymmetricSign(ctx, req)
}
func (c *cloudKMSClient) GetPublicKey(ctx context.Context, req *kmspb.GetPublicKeyRequest) (*kmspb.PublicKey, error) {
return c.client.GetPublicKey(ctx, req)
}
// Options configures optional KmsSigner behavior.
type Options struct {
// CredentialsFile is an optional path to a Google credentials JSON file.
// If empty, Application Default Credentials are used.
CredentialsFile string
// Timeout for individual KMS Sign API calls. Default: 1s.
Timeout time.Duration
// MaxRetries for transient KMS failures during Sign. Default: 3.
MaxRetries int
}
func (o *Options) timeout() time.Duration { return o.Timeout }
func (o *Options) maxRetries() int { return o.MaxRetries }
// KmsSigner implements the signer.Signer interface using Google Cloud KMS.
type KmsSigner struct {
client KMSClient
keyName string
opts Options
mu sync.RWMutex
pubKey crypto.PubKey
address []byte
}
// NewKmsSigner creates a new Signer backed by a Google Cloud KMS Ed25519 key version.
// It uses Application Default Credentials unless opts.CredentialsFile is provided.
func NewKmsSigner(ctx context.Context, keyName string, opts *Options) (*KmsSigner, error) {
if keyName == "" {
return nil, fmt.Errorf("gcp kms key name is required")
}
var clientOpts []option.ClientOption
if opts != nil && opts.CredentialsFile != "" {
clientOpts = append(clientOpts, option.WithAuthCredentialsFile(option.ServiceAccount, opts.CredentialsFile))
}
client, err := kms.NewKeyManagementClient(ctx, clientOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create Google Cloud KMS client: %w", err)
}
return kmsSignerFromClient(ctx, &cloudKMSClient{client: client}, keyName, opts)
}
// kmsSignerFromClient creates a KmsSigner from an existing KMS client.
// Useful for testing with a mock client.
func kmsSignerFromClient(ctx context.Context, client KMSClient, keyName string, opts *Options) (*KmsSigner, error) {
if keyName == "" {
return nil, fmt.Errorf("gcp kms key name is required")
}
if client == nil {
return nil, fmt.Errorf("gcp kms client is required")
}
o := Options{Timeout: 1 * time.Second, MaxRetries: 3}
if opts != nil {
if opts.Timeout > 0 {
o.Timeout = opts.Timeout
}
if opts.MaxRetries >= 0 {
o.MaxRetries = opts.MaxRetries
}
}
s := &KmsSigner{
client: client,
keyName: keyName,
opts: o,
}
// Fetch and cache the public key eagerly so we fail fast on misconfiguration.
if err := s.fetchPublicKey(ctx); err != nil {
return nil, fmt.Errorf("failed to fetch public key from Google Cloud KMS: %w", err)
}
return s, nil
}
// fetchPublicKey retrieves the public key from KMS and caches it.
func (s *KmsSigner) fetchPublicKey(ctx context.Context) error {
out, err := s.client.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{Name: s.keyName})
if err != nil {
return fmt.Errorf("KMS GetPublicKey failed: %w", err)
}
if out.GetName() != s.keyName {
return fmt.Errorf("KMS GetPublicKey integrity check failed: unexpected key name %q", out.GetName())
}
if out.GetPemCrc32C() == nil {
return fmt.Errorf("KMS GetPublicKey integrity check failed: pem_crc32c is missing")
}
if got, want := out.GetPemCrc32C().GetValue(), int64(crc32.Checksum([]byte(out.GetPem()), castagnoliTable)); got != want {
return fmt.Errorf("KMS GetPublicKey integrity check failed: pem_crc32c mismatch")
}
block, _ := pem.Decode([]byte(out.GetPem()))
if block == nil {
return fmt.Errorf("failed to decode PEM public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse KMS public key: %w", err)
}
edPubKey, ok := pub.(ed25519.PublicKey)
if !ok {
return fmt.Errorf("unsupported key type from KMS: expected ed25519, got %T", pub)
}
cryptoPubKey, err := crypto.UnmarshalEd25519PublicKey(edPubKey)
if err != nil {
return fmt.Errorf("failed to convert to libp2p pubkey: %w", err)
}
bz, err := cryptoPubKey.Raw()
if err != nil {
return fmt.Errorf("failed to get raw pubkey bytes: %w", err)
}
address := sha256.Sum256(bz)
s.mu.Lock()
defer s.mu.Unlock()
s.pubKey = cryptoPubKey
s.address = address[:]
return nil
}
// Sign signs a message using the remote KMS key with configurable timeout
// and retry with exponential backoff.
func (s *KmsSigner) Sign(ctx context.Context, message []byte) ([]byte, error) {
var lastErr error
maxRetries := s.opts.maxRetries()
timeout := s.opts.timeout()
maxAttempts := maxRetries + 1
for attempt := range maxAttempts {
if err := ctx.Err(); err != nil {
return nil, err
}
if attempt > 0 {
// Exponential backoff with cap: 100ms, 200ms, 400ms, ... up to 5s.
backoff := retryBackoff(attempt)
select {
case <-ctx.Done():
return nil, fmt.Errorf("KMS Sign canceled: %w", ctx.Err())
case <-time.After(backoff):
}
}
callCtx, cancel := context.WithTimeout(ctx, timeout)
dataCRC32C := int64(crc32.Checksum(message, castagnoliTable))
out, err := s.client.AsymmetricSign(callCtx, &kmspb.AsymmetricSignRequest{
Name: s.keyName,
Data: message,
DataCrc32C: wrapperspb.Int64(dataCRC32C),
})
cancel()
if err != nil {
lastErr = err
if !isRetryableKMSError(err) {
return nil, fmt.Errorf("GCP KMS sign failed with non-retryable error: %w", err)
}
continue
}
if err := verifySignResponse(out, s.keyName); err != nil {
lastErr = err
continue
}
return out.GetSignature(), nil
}
return nil, fmt.Errorf("KMS Sign failed after %d attempts: %w", maxAttempts, lastErr)
}
func retryBackoff(attempt int) time.Duration {
if attempt <= 1 {
return baseRetryBackoff
}
backoff := baseRetryBackoff
for i := 1; i < attempt; i++ {
if backoff >= maxRetryBackoff/2 {
return maxRetryBackoff
}
backoff *= 2
}
if backoff > maxRetryBackoff {
return maxRetryBackoff
}
return backoff
}
func verifySignResponse(out *kmspb.AsymmetricSignResponse, expectedName string) error {
if !out.GetVerifiedDataCrc32C() {
return fmt.Errorf("KMS Sign integrity check failed: verified_data_crc32c is false")
}
if out.GetName() != expectedName {
return fmt.Errorf("KMS Sign integrity check failed: unexpected key name %q", out.GetName())
}
signatureCRC32C := out.GetSignatureCrc32C()
if signatureCRC32C == nil {
return fmt.Errorf("KMS Sign integrity check failed: signature_crc32c is missing")
}
signature := out.GetSignature()
expectedCRC32C := int64(crc32.Checksum(signature, castagnoliTable))
if signatureCRC32C.GetValue() != expectedCRC32C {
return fmt.Errorf("KMS Sign integrity check failed: signature_crc32c mismatch")
}
return nil
}
// GetPublic returns the cached public key.
func (s *KmsSigner) GetPublic() (crypto.PubKey, error) {
s.mu.RLock()
pubKey := s.pubKey
s.mu.RUnlock()
if pubKey == nil {
return nil, fmt.Errorf("public key not loaded")
}
return pubKey, nil
}
// GetAddress returns the cached address derived from the public key.
func (s *KmsSigner) GetAddress() ([]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.address == nil {
return nil, fmt.Errorf("address not loaded")
}
r := make([]byte, len(s.address))
copy(r, s.address)
return r, nil
}
func isRetryableKMSError(err error) bool {
if errors.Is(err, context.Canceled) {
return false
}
if errors.Is(err, context.DeadlineExceeded) {
return true
}
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
switch status.Code(err) {
case codes.DeadlineExceeded, codes.Unavailable, codes.ResourceExhausted, codes.Aborted, codes.Internal:
return true
default:
return false
}
}