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"
|
2017-02-02 19:24:20 +00:00
|
|
|
"fmt"
|
2015-04-16 00:08:12 +00:00
|
|
|
|
2016-07-28 19:19:27 +00:00
|
|
|
"github.com/hashicorp/vault/helper/errutil"
|
2015-04-16 00:08:12 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2017-02-06 19:56:16 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-16 00:08:12 +00:00
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the policy",
|
|
|
|
},
|
|
|
|
|
|
|
|
"ciphertext": &framework.FieldSchema{
|
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
|
|
|
|
|
|
|
"context": &framework.FieldSchema{
|
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
|
|
|
|
|
|
|
"nonce": &framework.FieldSchema{
|
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 {
|
|
|
|
err = mapstructure.Decode(batchInputRaw, &batchInputItems)
|
2015-07-05 21:37:51 +00:00
|
|
|
if err != nil {
|
2017-02-06 19:56:16 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse batch input: %v", 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
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchResponseItems := make([]BatchResponseItem, len(batchInputItems))
|
|
|
|
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
|
2017-02-02 19:24:20 +00:00
|
|
|
p, lock, err := b.lm.GetPolicyShared(req.Storage, d.Get("name").(string))
|
2016-05-03 03:46:39 +00:00
|
|
|
if lock != nil {
|
|
|
|
defer lock.RUnlock()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
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 != "" {
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
`
|