2015-04-16 00:08:12 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-16 00:08:12 +00:00
|
|
|
"encoding/base64"
|
2015-06-18 01:51:05 +00:00
|
|
|
"fmt"
|
2015-04-16 00:08:12 +00:00
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/errutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/keysutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-02-06 19:56:16 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-16 00:08:12 +00:00
|
|
|
)
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
// BatchRequestItem represents a request item for batch processing
|
|
|
|
type BatchRequestItem struct {
|
|
|
|
// Context for key derivation. This is required for derived keys.
|
2017-02-06 19:56:16 +00:00
|
|
|
Context string `json:"context" structs:"context" mapstructure:"context"`
|
|
|
|
|
|
|
|
// DecodedContext is the base64 decoded version of Context
|
|
|
|
DecodedContext []byte
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
// Plaintext for encryption
|
|
|
|
Plaintext string `json:"plaintext" structs:"plaintext" mapstructure:"plaintext"`
|
|
|
|
|
|
|
|
// Ciphertext for decryption
|
|
|
|
Ciphertext string `json:"ciphertext" structs:"ciphertext" mapstructure:"ciphertext"`
|
|
|
|
|
|
|
|
// Nonce to be used when v1 convergent encryption is used
|
2017-02-06 19:56:16 +00:00
|
|
|
Nonce string `json:"nonce" structs:"nonce" mapstructure:"nonce"`
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
// The key version to be used for encryption
|
|
|
|
KeyVersion int `json:"key_version" structs:"key_version" mapstructure:"key_version"`
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
// DecodedNonce is the base64 decoded version of Nonce
|
|
|
|
DecodedNonce []byte
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BatchResponseItem represents a response item for batch processing
|
|
|
|
type BatchResponseItem struct {
|
|
|
|
// Ciphertext for the plaintext present in the corresponding batch
|
|
|
|
// request item
|
|
|
|
Ciphertext string `json:"ciphertext,omitempty" structs:"ciphertext" mapstructure:"ciphertext"`
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Plaintext for the ciphertext present in the corresponding batch
|
2017-02-02 19:24:20 +00:00
|
|
|
// request item
|
|
|
|
Plaintext string `json:"plaintext,omitempty" structs:"plaintext" mapstructure:"plaintext"`
|
|
|
|
|
|
|
|
// Error, if set represents a failure encountered while encrypting a
|
|
|
|
// corresponding batch request item
|
|
|
|
Error string `json:"error,omitempty" structs:"error" mapstructure:"error"`
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:11 +00:00
|
|
|
func (b *backend) pathEncrypt() *framework.Path {
|
2015-04-16 00:08:12 +00:00
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "encrypt/" + framework.GenericNameRegex("name"),
|
2015-04-16 00:08:12 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the policy",
|
|
|
|
},
|
|
|
|
|
|
|
|
"plaintext": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2017-02-02 19:24:20 +00:00
|
|
|
Description: "Base64 encoded plaintext value to be encrypted",
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
2015-07-05 21:12:07 +00:00
|
|
|
|
|
|
|
"context": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2017-02-02 19:24:20 +00:00
|
|
|
Description: "Base64 encoded context for key derivation. Required if key derivation is enabled",
|
2015-07-05 21:12:07 +00:00
|
|
|
},
|
2016-08-05 21:52:44 +00:00
|
|
|
|
|
|
|
"nonce": &framework.FieldSchema{
|
2017-02-02 19:24:20 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `
|
|
|
|
Base64 encoded nonce value. Must be provided if convergent encryption is
|
|
|
|
enabled for this key and the key was generated with Vault 0.6.1. Not required
|
|
|
|
for keys created in 0.6.2+. The value must be exactly 96 bits (12 bytes) long
|
|
|
|
and the user must ensure that for any given context (and thus, any given
|
|
|
|
encryption key) this nonce value is **never reused**.
|
|
|
|
`,
|
2016-08-05 21:52:44 +00:00
|
|
|
},
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
"type": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "aes256-gcm96",
|
2017-02-02 19:24:20 +00:00
|
|
|
Description: `
|
|
|
|
This parameter is required when encryption key is expected to be created.
|
|
|
|
When performing an upsert operation, the type of key to create. Currently,
|
2019-10-03 20:11:43 +00:00
|
|
|
"aes128-gcm96" (symmetric) and "aes256-gcm96" (symmetric) are the only types supported. Defaults to "aes256-gcm96".`,
|
2016-09-21 14:29:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"convergent_encryption": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
2017-02-02 19:24:20 +00:00
|
|
|
Description: `
|
|
|
|
This parameter will only be used when a key is expected to be created. Whether
|
|
|
|
to support convergent encryption. This is only supported when using a key with
|
|
|
|
key derivation enabled and will require all requests to carry both a context
|
|
|
|
and 96-bit (12-byte) nonce. The given nonce will be used in place of a randomly
|
|
|
|
generated nonce. As a result, when the same context and nonce are supplied, the
|
|
|
|
same ciphertext is generated. It is *very important* when using this mode that
|
|
|
|
you ensure that all nonces are unique for a given context. Failing to do so
|
|
|
|
will severely impact the ciphertext's security.`,
|
|
|
|
},
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
"key_version": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeInt,
|
|
|
|
Description: `The version of the key to use for encryption.
|
|
|
|
Must be 0 (for latest) or a value greater than or equal
|
|
|
|
to the min_encryption_version configured on the key.`,
|
|
|
|
},
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-02-02 14:58:12 +00:00
|
|
|
logical.CreateOperation: b.pathEncryptWrite,
|
2016-01-27 21:24:11 +00:00
|
|
|
logical.UpdateOperation: b.pathEncryptWrite,
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
2015-04-27 19:47:09 +00:00
|
|
|
|
2016-02-02 14:58:12 +00:00
|
|
|
ExistenceCheck: b.pathEncryptExistenceCheck,
|
|
|
|
|
2015-04-27 19:47:09 +00:00
|
|
|
HelpSynopsis: pathEncryptHelpSyn,
|
|
|
|
HelpDescription: pathEncryptHelpDesc,
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathEncryptExistenceCheck(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
|
2016-02-02 14:58:12 +00:00
|
|
|
name := d.Get("name").(string)
|
2018-06-12 16:24:12 +00:00
|
|
|
p, _, err := b.lm.GetPolicy(ctx, keysutil.PolicyRequest{
|
|
|
|
Storage: req.Storage,
|
|
|
|
Name: name,
|
2019-10-17 17:33:00 +00:00
|
|
|
}, b.GetRandomReader())
|
2016-02-02 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-06-12 16:24:12 +00:00
|
|
|
if p != nil && b.System().CachingDisabled() {
|
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
return p != nil, nil
|
2016-02-02 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathEncryptWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-16 00:08:12 +00:00
|
|
|
name := d.Get("name").(string)
|
2016-09-13 16:00:04 +00:00
|
|
|
var err error
|
2017-02-02 19:24:20 +00:00
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInputRaw := d.Raw["batch_input"]
|
2017-02-02 19:24:20 +00:00
|
|
|
var batchInputItems []BatchRequestItem
|
2017-02-06 19:56:16 +00:00
|
|
|
if batchInputRaw != nil {
|
|
|
|
err = mapstructure.Decode(batchInputRaw, &batchInputItems)
|
2015-07-05 21:37:51 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to parse batch input: {{err}}", err)
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(batchInputItems) == 0 {
|
|
|
|
return logical.ErrorResponse("missing batch input to process"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueRaw, ok := d.GetOk("plaintext")
|
|
|
|
if !ok {
|
|
|
|
return logical.ErrorResponse("missing plaintext to encrypt"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
batchInputItems = make([]BatchRequestItem, 1)
|
|
|
|
batchInputItems[0] = BatchRequestItem{
|
2017-06-06 20:02:54 +00:00
|
|
|
Plaintext: valueRaw.(string),
|
|
|
|
Context: d.Get("context").(string),
|
|
|
|
Nonce: d.Get("nonce").(string),
|
|
|
|
KeyVersion: d.Get("key_version").(int),
|
2015-07-05 21:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchResponseItems := make([]BatchResponseItem, len(batchInputItems))
|
|
|
|
contextSet := len(batchInputItems[0].Context) != 0
|
|
|
|
|
|
|
|
// Before processing the batch request items, get the policy. If the
|
|
|
|
// policy is supposed to be upserted, then determine if 'derived' is to
|
|
|
|
// be set or not, based on the presence of 'context' field in all the
|
|
|
|
// input items.
|
|
|
|
for i, item := range batchInputItems {
|
|
|
|
if (len(item.Context) == 0 && contextSet) || (len(item.Context) != 0 && !contextSet) {
|
|
|
|
return logical.ErrorResponse("context should be set either in all the request blocks or in none"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := base64.StdEncoding.DecodeString(item.Plaintext)
|
2016-08-05 21:52:44 +00:00
|
|
|
if err != nil {
|
2017-10-20 15:21:45 +00:00
|
|
|
batchResponseItems[i].Error = err.Error()
|
2017-02-02 19:24:20 +00:00
|
|
|
continue
|
2016-08-05 21:52:44 +00:00
|
|
|
}
|
2017-02-06 19:56:16 +00:00
|
|
|
|
|
|
|
// Decode the context
|
|
|
|
if len(item.Context) != 0 {
|
|
|
|
batchInputItems[i].DecodedContext, err = base64.StdEncoding.DecodeString(item.Context)
|
|
|
|
if err != nil {
|
|
|
|
batchResponseItems[i].Error = err.Error()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the nonce
|
|
|
|
if len(item.Nonce) != 0 {
|
|
|
|
batchInputItems[i].DecodedNonce, err = base64.StdEncoding.DecodeString(item.Nonce)
|
|
|
|
if err != nil {
|
|
|
|
batchResponseItems[i].Error = err.Error()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 21:52:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:11 +00:00
|
|
|
// Get the policy
|
2016-10-26 23:52:31 +00:00
|
|
|
var p *keysutil.Policy
|
2016-04-26 15:39:19 +00:00
|
|
|
var upserted bool
|
2018-06-12 16:24:12 +00:00
|
|
|
var polReq keysutil.PolicyRequest
|
2016-04-26 15:39:19 +00:00
|
|
|
if req.Operation == logical.CreateOperation {
|
2016-09-21 14:29:42 +00:00
|
|
|
convergent := d.Get("convergent_encryption").(bool)
|
2017-02-02 19:24:20 +00:00
|
|
|
if convergent && !contextSet {
|
2016-09-21 14:29:42 +00:00
|
|
|
return logical.ErrorResponse("convergent encryption requires derivation to be enabled, so context is required"), nil
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:24:12 +00:00
|
|
|
polReq = keysutil.PolicyRequest{
|
|
|
|
Upsert: true,
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage: req.Storage,
|
|
|
|
Name: name,
|
2017-02-02 19:24:20 +00:00
|
|
|
Derived: contextSet,
|
2016-10-26 23:52:31 +00:00
|
|
|
Convergent: convergent,
|
2016-09-21 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyType := d.Get("type").(string)
|
|
|
|
switch keyType {
|
2019-10-03 20:11:43 +00:00
|
|
|
case "aes128-gcm96":
|
|
|
|
polReq.KeyType = keysutil.KeyType_AES128_GCM96
|
2016-09-21 14:29:42 +00:00
|
|
|
case "aes256-gcm96":
|
2016-10-26 23:52:31 +00:00
|
|
|
polReq.KeyType = keysutil.KeyType_AES256_GCM96
|
2018-02-14 16:59:46 +00:00
|
|
|
case "chacha20-poly1305":
|
|
|
|
polReq.KeyType = keysutil.KeyType_ChaCha20_Poly1305
|
2019-10-03 16:32:43 +00:00
|
|
|
case "ecdsa-p256", "ecdsa-p384", "ecdsa-p521":
|
2016-09-21 14:29:42 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("key type %v not supported for this operation", keyType)), logical.ErrInvalidRequest
|
|
|
|
default:
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unknown key type %v", keyType)), logical.ErrInvalidRequest
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
} else {
|
2018-06-12 16:24:12 +00:00
|
|
|
polReq = keysutil.PolicyRequest{
|
|
|
|
Storage: req.Storage,
|
|
|
|
Name: name,
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
2019-10-17 17:33:00 +00:00
|
|
|
p, upserted, err = b.lm.GetPolicy(ctx, polReq, b.GetRandomReader())
|
2016-01-27 21:24:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if p == nil {
|
2017-05-12 18:14:00 +00:00
|
|
|
return logical.ErrorResponse("encryption key not found"), logical.ErrInvalidRequest
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
2018-06-12 16:24:12 +00:00
|
|
|
if !b.System().CachingDisabled() {
|
|
|
|
p.Lock(false)
|
|
|
|
}
|
2015-04-16 00:08:12 +00:00
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
// Process batch request items. If encryption of any request
|
|
|
|
// item fails, respectively mark the error in the response
|
|
|
|
// collection and continue to process other items.
|
|
|
|
for i, item := range batchInputItems {
|
|
|
|
if batchResponseItems[i].Error != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
ciphertext, err := p.Encrypt(item.KeyVersion, item.DecodedContext, item.DecodedNonce, item.Plaintext)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case errutil.UserError:
|
|
|
|
batchResponseItems[i].Error = err.Error()
|
|
|
|
continue
|
|
|
|
default:
|
2018-06-12 16:24:12 +00:00
|
|
|
p.Unlock()
|
2017-02-02 19:24:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ciphertext == "" {
|
2018-06-12 16:24:12 +00:00
|
|
|
p.Unlock()
|
2017-02-02 19:24:20 +00:00
|
|
|
return nil, fmt.Errorf("empty ciphertext returned for input item %d", i)
|
2015-09-14 20:28:46 +00:00
|
|
|
}
|
2015-04-16 00:08:12 +00:00
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchResponseItems[i].Ciphertext = ciphertext
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
resp := &logical.Response{}
|
2017-02-06 19:56:16 +00:00
|
|
|
if batchInputRaw != nil {
|
2017-02-02 19:24:20 +00:00
|
|
|
resp.Data = map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_results": batchResponseItems,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if batchResponseItems[0].Error != "" {
|
2018-06-12 16:24:12 +00:00
|
|
|
p.Unlock()
|
2017-02-02 19:24:20 +00:00
|
|
|
return logical.ErrorResponse(batchResponseItems[0].Error), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
resp.Data = map[string]interface{}{
|
|
|
|
"ciphertext": batchResponseItems[0].Ciphertext,
|
|
|
|
}
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
|
|
|
|
if req.Operation == logical.CreateOperation && !upserted {
|
|
|
|
resp.AddWarning("Attempted creation of the key during the encrypt operation, but it was created beforehand")
|
|
|
|
}
|
2018-06-12 16:24:12 +00:00
|
|
|
|
|
|
|
p.Unlock()
|
2015-04-16 00:08:12 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2015-04-27 19:47:09 +00:00
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
const pathEncryptHelpSyn = `Encrypt a plaintext value or a batch of plaintext
|
|
|
|
blocks using a named key`
|
2015-04-27 19:47:09 +00:00
|
|
|
|
|
|
|
const pathEncryptHelpDesc = `
|
2017-02-02 19:24:20 +00:00
|
|
|
This path uses the named key from the request path to encrypt a user provided
|
|
|
|
plaintext or a batch of plaintext blocks. The plaintext must be base64 encoded.
|
2015-04-27 19:47:09 +00:00
|
|
|
`
|