| 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 hcvaultscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/ |
| 16 |
|
| 17 |
import ( |
| 18 |
"errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 19 |
"fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 20 |
"stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 21 |
|
| 22 |
"github.com/tink-crypto/tink-go/v2/tinkscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/" |
| 23 |
) |
| 24 |
|
| 25 |
// vaultAEAD represents a HashiCorp Vault service to a particular URI. |
| 26 |
type vaultAEADscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD# struct { |
| 27 |
encKeyPathscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#encKeyPath. string |
| 28 |
decKeyPathscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#decKeyPath. string |
| 29 |
associatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#associatedDataName. string |
| 30 |
} |
| 31 |
|
| 32 |
var _ tinkscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/AEAD# = (*vaultAEADscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#)(nil) |
| 33 |
|
| 34 |
const ( |
| 35 |
encryptSegmentscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/encryptSegment. = "encrypt" |
| 36 |
decryptSegmentscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/decryptSegment. = "decrypt" |
| 37 |
defaultAssociatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/defaultAssociatedDataName. = "associated_data" |
| 38 |
legacyAssociatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/legacyAssociatedDataName. = "context" |
| 39 |
) |
| 40 |
|
| 41 |
type aeadParamsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams# struct { |
| 42 |
associatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams#associatedDataName. string |
| 43 |
} |
| 44 |
|
| 45 |
// AEADOption is an interface for defining options that are passed to [NewAEAD]. |
| 46 |
type AEADOptionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/AEADOption# interface{ setscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/AEADOption#set.(*aeadParamsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams#) error } |
| 47 |
|
| 48 |
type optionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/option# func(*aeadParamsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams#) error |
| 49 |
|
| 50 |
func (o optionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/option#) setscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/option#set().(a *aeadParamsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/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 WithLegacyContextParamaterscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/WithLegacyContextParamater().() AEADOptionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/AEADOption# { |
| 74 |
return optionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/option#(func(a *aeadParamsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams#) error { |
| 75 |
a.associatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/aeadParams#associatedDataName. = legacyAssociatedDataNamescip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/legacyAssociatedDataName. |
| 76 |
return nil |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
// Encrypt encrypts the plaintext data using a key stored in HashiCorp Vault. |
| 82 |
func (a *vaultAEADscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#) Encryptscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#Encrypt().(plaintext, associatedData []byte) ([]byte, error) { |
| •83 |
return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("Debian Vault support incomplete") |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
// Decrypt decrypts the ciphertext using a key stored in HashiCorp Vault. |
| 88 |
func (a *vaultAEADscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#) Decryptscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultAEAD#Decrypt().(ciphertext, associatedData []byte) ([]byte, error) { |
| •89 |
return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 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 getEndpointPathsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/getEndpointPaths().(keyPath string) (encryptPath, decryptPath string, err error) { |
| 97 |
parts := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.24.0 strings/Split().(keyPath, "/") |
| 98 |
length := len(parts) |
| 99 |
if length < 4 || parts[0] != "" || parts[length-2] != "keys" { |
| 100 |
return "", "", errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("malformed keyPath") |
| 101 |
} |
| 102 |
|
| 103 |
parts[length-2] = encryptSegmentscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/encryptSegment. |
| 104 |
encryptPath = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.0 strings/Join().(parts[1:], "/") |
| 105 |
parts[length-2] = decryptSegmentscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/decryptSegment. |
| 106 |
decryptPath = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.0 strings/Join().(parts[1:], "/") |
| 107 |
return encryptPath, decryptPath, nil |
| 108 |
} |