Debian Codegraph @

pkg/signature/kms/hashivault/client.go

raw
1 //
2 // Copyright 2021 The Sigstore Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 // Package hashivault implement the interface with hashivault kms service
17 package hashivault
18
19 import (
20 "context"
21 "crypto"
22 "crypto/ed25519"
23 "encoding/base64"
24 "encoding/json"
25 "errors"
26 "fmt"
27 "log"
28 "os"
29 "regexp"
30 "strconv"
31 "strings"
32 "time"
33
34 vault "github.com/hashicorp/vault/api"
35 config "github.com/hashicorp/vault/api/cliconfig"
36 "github.com/jellydator/ttlcache/v3"
37 "github.com/sigstore/sigstore/pkg/cryptoutils"
38 "github.com/sigstore/sigstore/pkg/signature"
39 sigkms "github.com/sigstore/sigstore/pkg/signature/kms"
40 )
41
42 func init() {
43 sigkms.AddProvider(ReferenceScheme, func(_ context.Context, keyResourceID string, hashFunc crypto.Hash, opts ...signature.RPCOption) (sigkms.SignerVerifier, error) {
44 return LoadSignerVerifier(keyResourceID, hashFunc, opts...)
45 })
46 sigkms.AddProvider(AlternativeScheme, func(_ context.Context, keyResourceID string, hashFunc crypto.Hash, opts ...signature.RPCOption) (sigkms.SignerVerifier, error) {
47 return LoadSignerVerifier(keyResourceID, hashFunc, opts...)
48 })
49 }
50
51 type hashivaultClient struct {
52 client *vault.Client
53 keyPath string
54 transitSecretEnginePath string
55 keyCache *ttlcache.Cache[string, crypto.PublicKey]
56 keyVersion uint64
57 }
58
59 var (
60 errReference = errors.New("kms specification should be in the format hashivault://<key>")
61 referenceRegex = regexp.MustCompile(`^(?:hashivault|openbao)://(?P<path>\w(([\w-.]+)?\w)?)$`)
62 prefixRegex = regexp.MustCompile("^vault:v[0-9]+:")
63 )
64
65 const (
66 vaultV1DataPrefix = "vault:v1:"
67
68 // use a consistent key for cache lookups
69 cacheKey = "signer"
70
71 // ReferenceScheme schemes for various KMS services are copied from https://github.com/google/go-cloud/tree/master/secrets
72 ReferenceScheme = "hashivault://"
73
74 // AlternativeScheme scheme for OpenBao KMS.
75 AlternativeScheme = "openbao://"
76 )
77
78 // ValidReference returns a non-nil error if the reference string is invalid
79 func ValidReference(ref string) error {
80 if !referenceRegex.MatchString(ref) {
81 return errReference
82 }
83 return nil
84 }
85
86 func parseReference(resourceID string) (keyPath string, err error) {
87 i := referenceRegex.SubexpIndex("path")
88 v := referenceRegex.FindStringSubmatch(resourceID)
89 if len(v) < i+1 {
90 err = fmt.Errorf("invalid vault format %q: %w", resourceID, err)
91 return keyPath, err
92 }
93 keyPath = v[i]
94 return keyPath, err
95 }
96
97 func newHashivaultClient(address, token, transitSecretEnginePath, keyResourceID string, keyVersion uint64) (*hashivaultClient, error) {
98 if err := ValidReference(keyResourceID); err != nil {
99 return nil, err
100 }
101
102 keyPath, err := parseReference(keyResourceID)
103 if err != nil {
104 return nil, err
105 }
106
107 if address == "" {
108 address = os.Getenv("VAULT_ADDR")
109 }
110
111 if address == "" {
112 address = os.Getenv("BAO_ADDR")
113 }
114
115 if address == "" {
116 return nil, errors.New("VAULT_ADDR or BAO_ADDR is not set")
117 }
118
119 client, err := vault.NewClient(&vault.Config{
120 Address: address,
121 })
122 if err != nil {
123 return nil, fmt.Errorf("new vault client: %w", err)
124 }
125
126 if token == "" {
127 token = os.Getenv("VAULT_TOKEN")
128 }
129
130 if token == "" {
131 token = os.Getenv("BAO_TOKEN")
132 }
133
134 if token == "" {
135 // token helper will use the configured token helper in ~/.vault
136 // if no token helper is defined the default token helper will look for the ~/.vault-token file
137 log.Printf("VAULT_TOKEN or BAO_TOKEN not set, trying to find token helper")
138
139 tokenHelper, err := config.DefaultTokenHelper()
140 if tokenHelper == nil {
141 return nil, fmt.Errorf("no token helper configured and VAULT_TOKEN not set")
142 }
143 if tokenHelper.Path() != "" {
144 log.Printf("Using custom token helper: %s", tokenHelper.Path())
145 }
146 if err == nil {
147 if t, err := tokenHelper.Get(); err == nil && t != "" {
148 token = t
149 } else {
150 if strings.HasSuffix(tokenHelper.Path(), "/.vault-token") {
151 return nil, fmt.Errorf("no token helper configured and ~/.vault-token file not found or empty")
152 }
153 return nil, fmt.Errorf("token helper failed to get token")
154 }
155 } else {
156 return nil, fmt.Errorf("token helper returned an error: %w", err)
157 }
158 }
159
160 if token == "" {
161 return nil, fmt.Errorf("no Vault token found in env, helper, or ~/.vault-token")
162 }
163
164 client.SetToken(token)
165
166 if transitSecretEnginePath == "" {
167 transitSecretEnginePath = os.Getenv("TRANSIT_SECRET_ENGINE_PATH")
168 }
169 if transitSecretEnginePath == "" {
170 transitSecretEnginePath = "transit"
171 }
172
173 hvClient := &hashivaultClient{
174 client: client,
175 keyPath: keyPath,
176 transitSecretEnginePath: transitSecretEnginePath,
177 keyCache: ttlcache.New[string, crypto.PublicKey](
178 ttlcache.WithDisableTouchOnHit[string, crypto.PublicKey](),
179 ),
180 keyVersion: keyVersion,
181 }
182
183 return hvClient, nil
184 }
185
186 func oidcLogin(_ context.Context, address, path, role, token string) (string, error) {
187 if address == "" {
188 address = os.Getenv("VAULT_ADDR")
189 }
190 if address == "" {
191 address = os.Getenv("BAO_ADDR")
192 }
193 if address == "" {
194 return "", errors.New("VAULT_ADDR or BAO_ADDR is not set")
195 }
196 if path == "" {
197 path = "jwt"
198 }
199
200 client, err := vault.NewClient(&vault.Config{
201 Address: address,
202 })
203 if err != nil {
204 return "", fmt.Errorf("new vault client: %w", err)
205 }
206
207 loginData := map[string]any{
208 "role": role,
209 "jwt": token,
210 }
211 fullpath := fmt.Sprintf("auth/%s/login", path)
212 resp, err := client.Logical().Write(fullpath, loginData)
213 if err != nil {
214 return "", fmt.Errorf("vault oidc login: %w", err)
215 }
216 return resp.TokenID()
217 }
218
219 func (h *hashivaultClient) fetchPublicKey(_ context.Context) (crypto.PublicKey, error) {
220 client := h.client.Logical()
221
222 path := fmt.Sprintf("/%s/keys/%s", h.transitSecretEnginePath, h.keyPath)
223
224 keyResult, err := client.Read(path)
225 if err != nil {
226 return nil, fmt.Errorf("public key: %w", err)
227 }
228
229 if keyResult == nil {
230 return nil, fmt.Errorf("could not read data from transit key path: %s", path)
231 }
232
233 keysData, hasKeys := keyResult.Data["keys"]
234 latestVersion, hasVersion := keyResult.Data["latest_version"]
235 if !hasKeys || !hasVersion {
236 return nil, errors.New("failed to read transit key keys: corrupted response")
237 }
238
239 keys, ok := keysData.(map[string]any)
240 if !ok {
241 return nil, errors.New("failed to read transit key keys: Invalid keys map")
242 }
243
244 keyVersion, ok := latestVersion.(json.Number)
245 if !ok {
246 return nil, fmt.Errorf("format of 'latest_version' is not json.Number")
247 }
248
249 keyData, ok := keys[string(keyVersion)]
250 if !ok {
251 return nil, errors.New("failed to read transit key keys: corrupted response")
252 }
253
254 keyMap, ok := keyData.(map[string]any)
255 if !ok {
256 return nil, fmt.Errorf("could not parse transit key keys data as map[string]interface{}")
257 }
258
259 publicKey, ok := keyMap["public_key"]
260 if !ok {
261 return nil, errors.New("failed to read transit key keys: corrupted response")
262 }
263
264 strPublicKey, ok := publicKey.(string)
265 if !ok {
266 return nil, fmt.Errorf("could not parse public key pem as string")
267 }
268 // vault returns the key type in the "name" field
269 if keyType := keyMap["name"]; keyType == "ed25519" {
270 // vault returns ed25519 public keys as base64 encoding of
271 // the raw bytes of the key
272 decodedPublicKey, err := base64.StdEncoding.DecodeString(strPublicKey)
273 if err != nil {
274 return nil, fmt.Errorf("failed to base64 decode ed25519 public key: %w", err)
275 }
276 if keyLen := len(decodedPublicKey); keyLen != ed25519.PublicKeySize {
277 return nil, fmt.Errorf("decoded ed25519 public key length is %d, should be %d", keyLen, ed25519.PublicKeySize)
278 }
279 return ed25519.PublicKey(decodedPublicKey), nil
280 }
281
282 return cryptoutils.UnmarshalPEMToPublicKey([]byte(strPublicKey))
283 }
284
285 func (h *hashivaultClient) public() (crypto.PublicKey, error) {
286 var lerr error
287 loader := ttlcache.LoaderFunc[string, crypto.PublicKey](
288 func(c *ttlcache.Cache[string, crypto.PublicKey], key string) *ttlcache.Item[string, crypto.PublicKey] {
289 var pubkey crypto.PublicKey
290 pubkey, lerr = h.fetchPublicKey(context.Background())
291 if lerr == nil {
292 item := c.Set(key, pubkey, 300*time.Second)
293 return item
294 }
295 return nil
296 },
297 )
298
299 item := h.keyCache.Get(cacheKey, ttlcache.WithLoader[string, crypto.PublicKey](loader))
300 if lerr != nil {
301 return nil, lerr
302 }
303
304 if item == nil {
305 return nil, fmt.Errorf("unable to retrieve an item from the cache by the provided key")
306 }
307
308 return item.Value(), nil
309 }
310
311 func (h hashivaultClient) sign(digest []byte, alg crypto.Hash, opts ...signature.SignOption) ([]byte, error) {
312 client := h.client.Logical()
313
314 keyVersion := fmt.Sprintf("%d", h.keyVersion)
315 var keyVersionUsedPtr *string
316 for _, opt := range opts {
317 opt.ApplyKeyVersion(&keyVersion)
318 opt.ApplyKeyVersionUsed(&keyVersionUsedPtr)
319 }
320
321 if keyVersion != "" {
322 if _, err := strconv.ParseUint(keyVersion, 10, 64); err != nil {
323 return nil, fmt.Errorf("parsing requested key version: %w", err)
324 }
325 }
326
327 prehashed := alg != crypto.Hash(0)
328
329 pub, err := h.public()
330 if err != nil {
331 return nil, fmt.Errorf("determining key type: %w", err)
332 }
333 switch pub.(type) {
334 case ed25519.PublicKey:
335 if alg == crypto.SHA512 {
336 prehashed = true
337 break
338 }
339 prehashed = false
340 }
341
342 signResult, err := client.Write(fmt.Sprintf("/%s/sign/%s%s", h.transitSecretEnginePath, h.keyPath, hashString(alg)), map[string]any{
343 "input": base64.StdEncoding.Strict().EncodeToString(digest),
344 "prehashed": prehashed,
345 "key_version": keyVersion,
346 "signature_algorithm": "pkcs1v15",
347 })
348 if err != nil {
349 return nil, fmt.Errorf("transit: failed to sign payload: %w", err)
350 }
351
352 encodedSignature, ok := signResult.Data["signature"]
353 if !ok {
354 return nil, errors.New("transit: response corrupted in-transit")
355 }
356
357 return vaultDecode(encodedSignature, keyVersionUsedPtr)
358 }
359
360 func (h hashivaultClient) verify(sig, digest []byte, alg crypto.Hash, opts ...signature.VerifyOption) error {
361 client := h.client.Logical()
362 encodedSig := base64.StdEncoding.EncodeToString(sig)
363
364 keyVersion := ""
365 for _, opt := range opts {
366 opt.ApplyKeyVersion(&keyVersion)
367 }
368
369 var vaultDataPrefix string
370 if keyVersion != "" {
371 // keyVersion >= 1 on verification but can be set to 0 on signing
372 kvUint, err := strconv.ParseUint(keyVersion, 10, 64)
373 if err != nil {
374 return fmt.Errorf("parsing requested key version: %w", err)
375 } else if kvUint == 0 {
376 return errors.New("key version must be >= 1")
377 }
378
379 vaultDataPrefix = fmt.Sprintf("vault:v%d:", kvUint)
380 } else {
381 vaultDataPrefix = os.Getenv("VAULT_KEY_PREFIX")
382 if vaultDataPrefix == "" {
383 if h.keyVersion > 0 {
384 vaultDataPrefix = fmt.Sprintf("vault:v%d:", h.keyVersion)
385 } else {
386 vaultDataPrefix = vaultV1DataPrefix
387 }
388 }
389 }
390 prehashed := alg != crypto.Hash(0)
391
392 pub, err := h.public()
393 if err != nil {
394 return fmt.Errorf("determining key type: %w", err)
395 }
396 switch pub.(type) {
397 case ed25519.PublicKey:
398 if alg == crypto.SHA512 {
399 prehashed = true
400 break
401 }
402 prehashed = false
403 }
404
405 result, err := client.Write(fmt.Sprintf("/%s/verify/%s/%s", h.transitSecretEnginePath, h.keyPath, hashString(alg)), map[string]any{
406 "input": base64.StdEncoding.EncodeToString(digest),
407 "prehashed": prehashed,
408 "signature": fmt.Sprintf("%s%s", vaultDataPrefix, encodedSig),
409 })
410 if err != nil {
411 return fmt.Errorf("verify: %w", err)
412 }
413
414 valid, ok := result.Data["valid"]
415 if !ok {
416 return errors.New("corrupted response")
417 }
418
419 isValid, ok := valid.(bool)
420 if !ok {
421 return fmt.Errorf("received non-bool value from 'valid' key")
422 }
423
424 if !isValid {
425 return errors.New("failed vault verification")
426 }
427
428 return nil
429 }
430
431 // Vault likes to prefix base64 data with a version prefix
432 func vaultDecode(data any, keyVersionUsed *string) ([]byte, error) {
433 encoded, ok := data.(string)
434 if !ok {
435 return nil, errors.New("received non-string data")
436 }
437
438 if keyVersionUsed != nil {
439 *keyVersionUsed = prefixRegex.FindString(encoded)
440 }
441 return base64.StdEncoding.DecodeString(prefixRegex.ReplaceAllString(encoded, ""))
442 }
443
444 func hashString(h crypto.Hash) string {
445 var hashStr string
446 switch h {
447 case crypto.SHA224:
448 hashStr = "/sha2-224"
449 case crypto.SHA256:
450 hashStr = "/sha2-256"
451 case crypto.SHA384:
452 hashStr = "/sha2-384"
453 case crypto.SHA512:
454 hashStr = "/sha2-512"
455 default:
456 hashStr = ""
457 }
458 return hashStr
459 }
460
461 func (h hashivaultClient) createKey(typeStr string) (crypto.PublicKey, error) {
462 client := h.client.Logical()
463
464 if _, err := client.Write(fmt.Sprintf("/%s/keys/%s", h.transitSecretEnginePath, h.keyPath), map[string]any{
465 "type": typeStr,
466 }); err != nil {
467 return nil, fmt.Errorf("failed to create transit key: %w", err)
468 }
469 return h.public()
470 }