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"
|
2021-04-22 15:20:59 +00:00
|
|
|
"fmt"
|
2015-04-16 00:08:12 +00:00
|
|
|
|
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"
|
2015-04-16 00:08:12 +00:00
|
|
|
)
|
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
type DecryptBatchResponseItem struct {
|
|
|
|
// Plaintext for the ciphertext present in the corresponding batch
|
|
|
|
// request item
|
|
|
|
Plaintext string `json:"plaintext" 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) pathDecrypt() *framework.Path {
|
2015-04-16 00:08:12 +00:00
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "decrypt/" + framework.GenericNameRegex("name"),
|
2015-04-16 00:08:12 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"name": {
|
2015-04-16 00:08:12 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the policy",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"ciphertext": {
|
2017-02-02 19:24:20 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `
|
|
|
|
The ciphertext to decrypt, provided as returned by encrypt.`,
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
2015-07-05 21:12:07 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"context": {
|
2017-02-02 19:24:20 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
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
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"nonce": {
|
2017-02-02 19:24:20 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `
|
|
|
|
Base64 encoded nonce value used during encryption. 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+.`,
|
|
|
|
},
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-27 21:24:11 +00:00
|
|
|
logical.UpdateOperation: b.pathDecryptWrite,
|
2015-04-16 00:08:12 +00:00
|
|
|
},
|
2015-04-27 19:47:09 +00:00
|
|
|
|
|
|
|
HelpSynopsis: pathDecryptHelpSyn,
|
|
|
|
HelpDescription: pathDecryptHelpDesc,
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathDecryptWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInputRaw := d.Raw["batch_input"]
|
2017-02-02 19:24:20 +00:00
|
|
|
var batchInputItems []BatchRequestItem
|
2016-08-05 21:52:44 +00:00
|
|
|
var err error
|
2017-02-06 19:56:16 +00:00
|
|
|
if batchInputRaw != nil {
|
2020-06-10 17:31:46 +00:00
|
|
|
err = decodeBatchRequestItems(batchInputRaw, &batchInputItems)
|
2015-07-05 21:37:51 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse batch input: %w", err)
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(batchInputItems) == 0 {
|
|
|
|
return logical.ErrorResponse("missing batch input to process"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ciphertext := d.Get("ciphertext").(string)
|
|
|
|
if len(ciphertext) == 0 {
|
|
|
|
return logical.ErrorResponse("missing ciphertext to decrypt"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
batchInputItems = make([]BatchRequestItem, 1)
|
|
|
|
batchInputItems[0] = BatchRequestItem{
|
|
|
|
Ciphertext: ciphertext,
|
2017-02-06 19:56:16 +00:00
|
|
|
Context: d.Get("context").(string),
|
|
|
|
Nonce: d.Get("nonce").(string),
|
2015-07-05 21:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-16 00:08:12 +00:00
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
batchResponseItems := make([]DecryptBatchResponseItem, len(batchInputItems))
|
2017-02-02 19:24:20 +00:00
|
|
|
contextSet := len(batchInputItems[0].Context) != 0
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Ciphertext == "" {
|
|
|
|
batchResponseItems[i].Error = "missing ciphertext to decrypt"
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-16 00:08:12 +00:00
|
|
|
// Get the policy
|
2018-06-12 16:24:12 +00:00
|
|
|
p, _, err := b.lm.GetPolicy(ctx, keysutil.PolicyRequest{
|
|
|
|
Storage: req.Storage,
|
|
|
|
Name: d.Get("name").(string),
|
2019-10-17 17:33:00 +00:00
|
|
|
}, b.GetRandomReader())
|
2015-04-16 00:08:12 +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
|
|
|
for i, item := range batchInputItems {
|
|
|
|
if batchResponseItems[i].Error != "" {
|
|
|
|
continue
|
2015-09-14 20:28:46 +00:00
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
plaintext, err := p.Decrypt(item.DecodedContext, item.DecodedNonce, item.Ciphertext)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batchResponseItems[i].Plaintext = plaintext
|
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{}{
|
|
|
|
"plaintext": batchResponseItems[0].Plaintext,
|
|
|
|
}
|
2015-04-16 00:08:12 +00:00
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
const pathDecryptHelpSyn = `Decrypt a ciphertext value using a named key`
|
|
|
|
|
|
|
|
const pathDecryptHelpDesc = `
|
|
|
|
This path uses the named key from the request path to decrypt a user
|
|
|
|
provided ciphertext. The plaintext is returned base64 encoded.
|
|
|
|
`
|