Debian Codegraph @

integration/hcvault/hcvault_aead.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package hcvault
16
17 import (
18 "errors"
19 "fmt"
20 "strings"
21
22 "github.com/tink-crypto/tink-go/v2/tink"
23 )
24
25 // vaultAEAD represents a HashiCorp Vault service to a particular URI.
26 type vaultAEAD struct {
27 encKeyPath string
28 decKeyPath string
29 associatedDataName string
30 }
31
32 var _ tink.AEAD = (*vaultAEAD)(nil)
33
34 const (
35 encryptSegment = "encrypt"
36 decryptSegment = "decrypt"
37 defaultAssociatedDataName = "associated_data"
38 legacyAssociatedDataName = "context"
39 )
40
41 type aeadParams struct {
42 associatedDataName string
43 }
44
45 // AEADOption is an interface for defining options that are passed to [NewAEAD].
46 type AEADOption interface{ set(*aeadParams) error }
47
48 type option func(*aeadParams) error
49
50 func (o option) set(a *aeadParams) error { return o(a) }
51
52 // WithLegacyContextParamater lets the remote AEAD populate the "context" parameter
53 // in encrypt and decrypt requests instead of the "associated_data".
54 //
55 // Using this option makes the AEAD compatible with the instance returned by GetAEAD
56 // from the KMSClient returned by NewClient. For new keys, this option should not be used.
57 //
58 // ## Warning
59 //
60 // Vault only uses the "context" parameter for keys which have derivation enabled
61 // (with "derived=true") and ignores it otherwise. For such keys, the "context"
62 // parameter is required to be non-empty.
63 //
64 // Therefore:
65 //
66 // - for keys with "derived=false", you should only use empty associated data.
67 //
68 // - for keys with "derived=true", you should only use non-empty associated data.
69 //
70 // With Tink's "KMS envelope AEAD", always use a key with "derived=false".
71 //
72 // For reference, see https://developer.hashicorp.com/vault/api-docs/secret/transit.
73 func WithLegacyContextParamater() AEADOption {
74 return option(func(a *aeadParams) error {
75 a.associatedDataName = legacyAssociatedDataName
76 return nil
77 })
78 }
79
80
81 // Encrypt encrypts the plaintext data using a key stored in HashiCorp Vault.
82 func (a *vaultAEAD) Encrypt(plaintext, associatedData []byte) ([]byte, error) {
•83 return nil, fmt.Errorf("Debian Vault support incomplete")
84 }
85
86
87 // Decrypt decrypts the ciphertext using a key stored in HashiCorp Vault.
88 func (a *vaultAEAD) Decrypt(ciphertext, associatedData []byte) ([]byte, error) {
•89 return nil, fmt.Errorf("Debian Vault support incomplete")
90 }
91
92 // getEndpointPaths transforms keyPath into the Vault transit encrypt and decrypt
93 // paths. The keyPath is expected to have the form "/{mount-path}/keys/{keyName}", which will
94 // be transformed to
95 // "{mount-path}/encrypt/{keyName}" and "{mount-path}/decrypt/{keyName}".
96 func getEndpointPaths(keyPath string) (encryptPath, decryptPath string, err error) {
97 parts := strings.Split(keyPath, "/")
98 length := len(parts)
99 if length < 4 || parts[0] != "" || parts[length-2] != "keys" {
100 return "", "", errors.New("malformed keyPath")
101 }
102
103 parts[length-2] = encryptSegment
104 encryptPath = strings.Join(parts[1:], "/")
105 parts[length-2] = decryptSegment
106 decryptPath = strings.Join(parts[1:], "/")
107 return encryptPath, decryptPath, nil
108 }