mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
Refactor convergent encryption to make specifying a nonce in addition to context possible
This commit is contained in:
@@ -28,6 +28,11 @@ func (b *backend) pathEncrypt() *framework.Path {
|
||||
Type: framework.TypeString,
|
||||
Description: "Context for key derivation. Required for derived keys.",
|
||||
},
|
||||
|
||||
"nonce": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "Nonce for when convergent encryption is used and the context is not used as the nonce",
|
||||
},
|
||||
},
|
||||
|
||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
||||
@@ -63,10 +68,11 @@ func (b *backend) pathEncryptWrite(
|
||||
return logical.ErrorResponse("missing plaintext to encrypt"), logical.ErrInvalidRequest
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
// Decode the context if any
|
||||
contextRaw := d.Get("context").(string)
|
||||
var context []byte
|
||||
var err error
|
||||
if len(contextRaw) != 0 {
|
||||
context, err = base64.StdEncoding.DecodeString(contextRaw)
|
||||
if err != nil {
|
||||
@@ -74,12 +80,22 @@ func (b *backend) pathEncryptWrite(
|
||||
}
|
||||
}
|
||||
|
||||
// Decode the nonce if any
|
||||
nonceRaw := d.Get("nonce").(string)
|
||||
var nonce []byte
|
||||
if len(nonceRaw) != 0 {
|
||||
nonce, err = base64.StdEncoding.DecodeString(nonceRaw)
|
||||
if err != nil {
|
||||
return logical.ErrorResponse("failed to decode nonce as base64"), logical.ErrInvalidRequest
|
||||
}
|
||||
}
|
||||
|
||||
// Get the policy
|
||||
var p *Policy
|
||||
var lock *sync.RWMutex
|
||||
var upserted bool
|
||||
if req.Operation == logical.CreateOperation {
|
||||
p, lock, upserted, err = b.lm.GetPolicyUpsert(req.Storage, name, len(context) != 0, false)
|
||||
p, lock, upserted, err = b.lm.GetPolicyUpsert(req.Storage, name, len(context) != 0, false, false)
|
||||
} else {
|
||||
p, lock, err = b.lm.GetPolicyShared(req.Storage, name)
|
||||
}
|
||||
@@ -93,7 +109,7 @@ func (b *backend) pathEncryptWrite(
|
||||
return logical.ErrorResponse("policy not found"), logical.ErrInvalidRequest
|
||||
}
|
||||
|
||||
ciphertext, err := p.Encrypt(context, value)
|
||||
ciphertext, err := p.Encrypt(context, nonce, value)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case errutil.UserError:
|
||||
|
||||
Reference in New Issue
Block a user