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