2023-03-15 16:00:52 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2016-09-21 14:29:42 +00:00
package transit
import (
2018-01-08 18:31:38 +00:00
"context"
2022-08-31 16:27:03 +00:00
"crypto/rsa"
2016-09-21 14:29:42 +00:00
"encoding/base64"
2023-01-27 19:39:58 +00:00
"errors"
2016-09-21 14:29:42 +00:00
"fmt"
2022-08-31 16:27:03 +00:00
"strconv"
"strings"
2016-09-21 14:29:42 +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"
2019-03-04 20:26:20 +00:00
"github.com/mitchellh/mapstructure"
2016-09-21 14:29:42 +00:00
)
2019-03-04 20:26:20 +00:00
// BatchRequestSignItem represents a request item for batch processing.
// A map type allows us to distinguish between empty and missing values.
type batchRequestSignItem map [ string ] string
// BatchResponseSignItem represents a response item for batch processing
type batchResponseSignItem struct {
// signature for the input present in the corresponding batch
// request item
2019-03-04 22:49:29 +00:00
Signature string ` json:"signature,omitempty" mapstructure:"signature" `
2019-03-04 20:26:20 +00:00
2020-06-01 17:16:01 +00:00
// The key version to be used for encryption
KeyVersion int ` json:"key_version" mapstructure:"key_version" `
2019-03-04 22:49:29 +00:00
PublicKey [ ] byte ` json:"publickey,omitempty" mapstructure:"publickey" `
2019-03-04 20:26:20 +00:00
// Error, if set represents a failure encountered while encrypting a
// corresponding batch request item
2019-03-04 22:49:29 +00:00
Error string ` json:"error,omitempty" mapstructure:"error" `
2019-03-04 20:26:20 +00:00
// The return paths through WriteSign in some cases are (nil, err) and others
// (logical.ErrorResponse(..),nil), and others (logical.ErrorResponse(..),err).
// For batch processing to successfully mimic previous handling for simple 'input',
// both output values are needed - though 'err' should never be serialized.
err error
2022-12-13 18:03:40 +00:00
// Reference is an arbitrary caller supplied string value that will be placed on the
// batch response to ease correlation between inputs and outputs
Reference string ` json:"reference" mapstructure:"reference" `
2019-03-04 20:26:20 +00:00
}
// BatchRequestVerifyItem represents a request item for batch processing.
// A map type allows us to distinguish between empty and missing values.
type batchRequestVerifyItem map [ string ] string
// BatchResponseVerifyItem represents a response item for batch processing
type batchResponseVerifyItem struct {
// Valid indicates whether signature matches the signature derived from the input string
2019-03-04 22:49:29 +00:00
Valid bool ` json:"valid" mapstructure:"valid" `
2019-03-04 20:26:20 +00:00
// Error, if set represents a failure encountered while encrypting a
// corresponding batch request item
2019-03-04 22:49:29 +00:00
Error string ` json:"error,omitempty" mapstructure:"error" `
2019-03-04 20:26:20 +00:00
// The return paths through WriteSign in some cases are (nil, err) and others
// (logical.ErrorResponse(..),nil), and others (logical.ErrorResponse(..),err).
// For batch processing to successfully mimic previous handling for simple 'input',
// both output values are needed - though 'err' should never be serialized.
err error
2022-12-13 18:03:40 +00:00
// Reference is an arbitrary caller supplied string value that will be placed on the
// batch response to ease correlation between inputs and outputs
Reference string ` json:"reference" mapstructure:"reference" `
2019-03-04 20:26:20 +00:00
}
2022-10-27 12:26:20 +00:00
const defaultHashAlgorithm = "sha2-256"
2016-09-21 14:29:42 +00:00
func ( b * backend ) pathSign ( ) * framework . Path {
return & framework . Path {
Pattern : "sign/" + framework . GenericNameRegex ( "name" ) + framework . OptionalParamRegex ( "urlalgorithm" ) ,
2023-04-10 18:20:53 +00:00
DisplayAttrs : & framework . DisplayAttributes {
OperationPrefix : operationPrefixTransit ,
OperationVerb : "sign" ,
OperationSuffix : "|with-algorithm" ,
} ,
2016-09-21 14:29:42 +00:00
Fields : map [ string ] * framework . FieldSchema {
2019-01-23 17:31:34 +00:00
"name" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The key to use" ,
} ,
2019-01-23 17:31:34 +00:00
"input" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The base64-encoded input data" ,
} ,
2019-01-23 17:31:34 +00:00
"context" : {
2017-06-05 19:00:39 +00:00
Type : framework . TypeString ,
Description : ` Base64 encoded context for key derivation . Required if key
derivation is enabled ; currently only available with ed25519 keys . ` ,
} ,
2019-01-23 17:31:34 +00:00
"hash_algorithm" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
2022-10-27 12:26:20 +00:00
Default : defaultHashAlgorithm ,
2016-09-21 14:29:42 +00:00
Description : ` Hash algorithm to use ( POST body parameter ) . Valid values are :
2019-02-07 23:31:31 +00:00
* sha1
2016-09-21 14:29:42 +00:00
* sha2 - 224
* sha2 - 256
* sha2 - 384
* sha2 - 512
2021-12-08 18:29:33 +00:00
* sha3 - 224
* sha3 - 256
* sha3 - 384
* sha3 - 512
2022-10-27 12:26:20 +00:00
* none
2016-09-21 14:29:42 +00:00
2017-07-28 13:30:27 +00:00
Defaults to "sha2-256" . Not valid for all key types ,
2022-10-27 12:26:20 +00:00
including ed25519 . Using none requires setting prehashed = true and
signature_algorithm = pkcs1v15 , yielding a PKCSv1_5_NoOID instead of
the usual PKCSv1_5_DERnull signature . ` ,
2016-09-21 14:29:42 +00:00
} ,
2019-01-23 17:31:34 +00:00
"algorithm" : {
2018-03-15 16:17:02 +00:00
Type : framework . TypeString ,
2022-10-27 12:26:20 +00:00
Default : defaultHashAlgorithm ,
2018-03-15 16:17:02 +00:00
Description : ` Deprecated: use "hash_algorithm" instead. ` ,
} ,
2019-01-23 17:31:34 +00:00
"urlalgorithm" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : ` Hash algorithm to use (POST URL parameter) ` ,
} ,
2017-06-06 20:02:54 +00:00
2019-01-23 17:31:34 +00:00
"key_version" : {
2017-06-06 20:02:54 +00:00
Type : framework . TypeInt ,
Description : ` The version of the key to use for signing .
Must be 0 ( for latest ) or a value greater than or equal
to the min_encryption_version configured on the key . ` ,
} ,
2017-11-03 14:45:53 +00:00
2019-01-23 17:31:34 +00:00
"prehashed" : {
2017-11-03 14:45:53 +00:00
Type : framework . TypeBool ,
2020-02-15 22:40:50 +00:00
Description : ` Set to 'true' when the input is already hashed. If the key type is 'rsa-2048', 'rsa-3072' or 'rsa-4096', then the algorithm used to hash the input should be indicated by the 'algorithm' parameter. ` ,
2017-11-03 14:45:53 +00:00
} ,
2019-01-23 17:31:34 +00:00
"signature_algorithm" : {
2018-03-15 16:17:02 +00:00
Type : framework . TypeString ,
Description : ` The signature algorithm to use for signing . Currently only applies to RSA key types .
Options are ' pss ' or ' pkcs1v15 ' . Defaults to ' pss ' ` ,
} ,
2019-01-23 17:31:34 +00:00
"marshaling_algorithm" : {
Type : framework . TypeString ,
Default : "asn1" ,
Description : ` The method by which to marshal the signature. The default is 'asn1' which is used by openssl and X.509. It can also be set to 'jws' which is used for JWT signatures; setting it to this will also cause the encoding of the signature to be url-safe base64 instead of using standard base64 encoding. Currently only valid for ECDSA P-256 key types". ` ,
} ,
2022-08-31 16:27:03 +00:00
"salt_length" : {
Type : framework . TypeString ,
Default : "auto" ,
Description : ` The salt length used to sign . Currently only applies to the RSA PSS signature scheme .
Options are ' auto ' ( the default used by Golang , causing the salt to be as large as possible when signing ) , ' hash ' ( causes the salt length to equal the length of the hash used in the signature ) , or an integer between the minimum and the maximum permissible salt lengths for the given RSA key size . Defaults to ' auto ' . ` ,
} ,
2023-01-04 14:15:48 +00:00
"batch_input" : {
Type : framework . TypeSlice ,
Description : ` Specifies a list of items for processing . When this parameter is set ,
any supplied ' input ' or ' context ' parameters will be ignored . Responses are returned in the
' batch_results ' array component of the ' data ' element of the response . Any batch output will
preserve the order of the batch input ` ,
} ,
2016-09-21 14:29:42 +00:00
} ,
Callbacks : map [ logical . Operation ] framework . OperationFunc {
logical . UpdateOperation : b . pathSignWrite ,
} ,
HelpSynopsis : pathSignHelpSyn ,
HelpDescription : pathSignHelpDesc ,
}
}
func ( b * backend ) pathVerify ( ) * framework . Path {
return & framework . Path {
Pattern : "verify/" + framework . GenericNameRegex ( "name" ) + framework . OptionalParamRegex ( "urlalgorithm" ) ,
2023-04-10 18:20:53 +00:00
DisplayAttrs : & framework . DisplayAttributes {
OperationPrefix : operationPrefixTransit ,
OperationVerb : "verify" ,
OperationSuffix : "|with-algorithm" ,
} ,
2016-09-21 14:29:42 +00:00
Fields : map [ string ] * framework . FieldSchema {
2021-04-08 16:43:39 +00:00
"name" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The key to use" ,
} ,
2019-01-23 17:31:34 +00:00
"context" : {
2017-06-05 19:00:39 +00:00
Type : framework . TypeString ,
Description : ` Base64 encoded context for key derivation . Required if key
derivation is enabled ; currently only available with ed25519 keys . ` ,
} ,
2019-01-23 17:31:34 +00:00
"signature" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The signature, including vault header/key version" ,
} ,
2019-01-23 17:31:34 +00:00
"hmac" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The HMAC, including vault header/key version" ,
} ,
2019-01-23 17:31:34 +00:00
"input" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : "The base64-encoded input data to verify" ,
} ,
2019-01-23 17:31:34 +00:00
"urlalgorithm" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
Description : ` Hash algorithm to use (POST URL parameter) ` ,
} ,
2019-01-23 17:31:34 +00:00
"hash_algorithm" : {
2016-09-21 14:29:42 +00:00
Type : framework . TypeString ,
2022-10-27 12:26:20 +00:00
Default : defaultHashAlgorithm ,
2016-09-21 14:29:42 +00:00
Description : ` Hash algorithm to use ( POST body parameter ) . Valid values are :
2019-02-07 23:31:31 +00:00
* sha1
2016-09-21 14:29:42 +00:00
* sha2 - 224
* sha2 - 256
* sha2 - 384
* sha2 - 512
2021-12-08 18:29:33 +00:00
* sha3 - 224
* sha3 - 256
* sha3 - 384
* sha3 - 512
2022-10-27 12:26:20 +00:00
* none
2016-09-21 14:29:42 +00:00
2022-10-27 12:26:20 +00:00
Defaults to "sha2-256" . Not valid for all key types . See note about
none on signing path . ` ,
2016-09-21 14:29:42 +00:00
} ,
2019-01-23 17:31:34 +00:00
"algorithm" : {
2018-03-15 16:17:02 +00:00
Type : framework . TypeString ,
2022-10-27 12:26:20 +00:00
Default : defaultHashAlgorithm ,
2018-03-15 16:17:02 +00:00
Description : ` Deprecated: use "hash_algorithm" instead. ` ,
} ,
2017-11-03 14:45:53 +00:00
2019-01-23 17:31:34 +00:00
"prehashed" : {
2017-11-03 14:45:53 +00:00
Type : framework . TypeBool ,
2020-02-15 22:40:50 +00:00
Description : ` Set to 'true' when the input is already hashed. If the key type is 'rsa-2048', 'rsa-3072' or 'rsa-4096', then the algorithm used to hash the input should be indicated by the 'algorithm' parameter. ` ,
2017-11-03 14:45:53 +00:00
} ,
2019-01-23 17:31:34 +00:00
"signature_algorithm" : {
2018-03-15 16:17:02 +00:00
Type : framework . TypeString ,
Description : ` The signature algorithm to use for signature verification . Currently only applies to RSA key types .
Options are ' pss ' or ' pkcs1v15 ' . Defaults to ' pss ' ` ,
} ,
2019-01-23 17:31:34 +00:00
"marshaling_algorithm" : {
Type : framework . TypeString ,
Default : "asn1" ,
Description : ` The method by which to unmarshal the signature when verifying. The default is 'asn1' which is used by openssl and X.509; can also be set to 'jws' which is used for JWT signatures in which case the signature is also expected to be url-safe base64 encoding instead of standard base64 encoding. Currently only valid for ECDSA P-256 key types". ` ,
} ,
2022-08-31 16:27:03 +00:00
"salt_length" : {
Type : framework . TypeString ,
Default : "auto" ,
Description : ` The salt length used to sign . Currently only applies to the RSA PSS signature scheme .
Options are ' auto ' ( the default used by Golang , causing the salt to be as large as possible when signing ) , ' hash ' ( causes the salt length to equal the length of the hash used in the signature ) , or an integer between the minimum and the maximum permissible salt lengths for the given RSA key size . Defaults to ' auto ' . ` ,
} ,
2023-01-04 14:15:48 +00:00
"batch_input" : {
Type : framework . TypeSlice ,
Description : ` Specifies a list of items for processing . When this parameter is set ,
any supplied ' input ' , ' hmac ' or ' signature ' parameters will be ignored . Responses are returned in the
' batch_results ' array component of the ' data ' element of the response . Any batch output will
preserve the order of the batch input ` ,
} ,
2016-09-21 14:29:42 +00:00
} ,
Callbacks : map [ logical . Operation ] framework . OperationFunc {
logical . UpdateOperation : b . pathVerifyWrite ,
} ,
HelpSynopsis : pathVerifyHelpSyn ,
HelpDescription : pathVerifyHelpDesc ,
}
}
2022-08-31 16:27:03 +00:00
func ( b * backend ) getSaltLength ( d * framework . FieldData ) ( int , error ) {
rawSaltLength , ok := d . GetOk ( "salt_length" )
// This should only happen when something is wrong with the schema,
// so this is a reasonable default.
if ! ok {
return rsa . PSSSaltLengthAuto , nil
}
rawSaltLengthStr := rawSaltLength . ( string )
lowerSaltLengthStr := strings . ToLower ( rawSaltLengthStr )
switch lowerSaltLengthStr {
case "auto" :
return rsa . PSSSaltLengthAuto , nil
case "hash" :
return rsa . PSSSaltLengthEqualsHash , nil
default :
saltLengthInt , err := strconv . Atoi ( lowerSaltLengthStr )
if err != nil {
return rsa . PSSSaltLengthEqualsHash - 1 , fmt . Errorf ( "salt length neither 'auto', 'hash', nor an int: %s" , rawSaltLength )
}
if saltLengthInt < rsa . PSSSaltLengthEqualsHash {
return rsa . PSSSaltLengthEqualsHash - 1 , fmt . Errorf ( "salt length is invalid: %d" , saltLengthInt )
}
return saltLengthInt , nil
}
}
2018-01-08 18:31:38 +00:00
func ( b * backend ) pathSignWrite ( ctx context . Context , req * logical . Request , d * framework . FieldData ) ( * logical . Response , error ) {
2016-09-21 14:29:42 +00:00
name := d . Get ( "name" ) . ( string )
2017-06-06 20:02:54 +00:00
ver := d . Get ( "key_version" ) . ( int )
2019-01-23 17:31:34 +00:00
hashAlgorithmStr := d . Get ( "urlalgorithm" ) . ( string )
if hashAlgorithmStr == "" {
hashAlgorithmStr = d . Get ( "hash_algorithm" ) . ( string )
if hashAlgorithmStr == "" {
hashAlgorithmStr = d . Get ( "algorithm" ) . ( string )
2022-10-27 12:26:20 +00:00
if hashAlgorithmStr == "" {
hashAlgorithmStr = defaultHashAlgorithm
}
2018-03-15 16:17:02 +00:00
}
2016-09-21 14:29:42 +00:00
}
2019-01-23 17:31:34 +00:00
hashAlgorithm , ok := keysutil . HashTypeMap [ hashAlgorithmStr ]
if ! ok {
return logical . ErrorResponse ( fmt . Sprintf ( "invalid hash algorithm %q" , hashAlgorithmStr ) ) , logical . ErrInvalidRequest
}
marshalingStr := d . Get ( "marshaling_algorithm" ) . ( string )
marshaling , ok := keysutil . MarshalingTypeMap [ marshalingStr ]
if ! ok {
return logical . ErrorResponse ( fmt . Sprintf ( "invalid marshaling type %q" , marshalingStr ) ) , logical . ErrInvalidRequest
}
2017-11-03 14:45:53 +00:00
prehashed := d . Get ( "prehashed" ) . ( bool )
2018-03-15 16:17:02 +00:00
sigAlgorithm := d . Get ( "signature_algorithm" ) . ( string )
2022-08-31 16:27:03 +00:00
saltLength , err := b . getSaltLength ( d )
if err != nil {
return logical . ErrorResponse ( err . Error ( ) ) , logical . ErrInvalidRequest
}
2016-09-21 14:29:42 +00:00
2022-10-27 12:26:20 +00:00
if hashAlgorithm == keysutil . HashTypeNone && ( ! prehashed || sigAlgorithm != "pkcs1v15" ) {
return logical . ErrorResponse ( "hash_algorithm=none requires both prehashed=true and signature_algorithm=pkcs1v15" ) , logical . ErrInvalidRequest
}
2016-09-21 14:29:42 +00:00
// Get the policy
2021-09-13 21:44:56 +00:00
p , _ , err := b . GetPolicy ( ctx , keysutil . PolicyRequest {
2018-06-12 16:24:12 +00:00
Storage : req . Storage ,
Name : name ,
2019-10-17 17:33:00 +00:00
} , b . GetRandomReader ( ) )
2016-09-21 14:29:42 +00:00
if err != nil {
return nil , err
}
if p == nil {
2017-05-12 18:14:00 +00:00
return logical . ErrorResponse ( "encryption key not found" ) , logical . ErrInvalidRequest
2016-09-21 14:29:42 +00:00
}
2018-06-12 16:24:12 +00:00
if ! b . System ( ) . CachingDisabled ( ) {
p . Lock ( false )
}
2016-09-21 14:29:42 +00:00
if ! p . Type . SigningSupported ( ) {
2018-06-12 16:24:12 +00:00
p . Unlock ( )
2016-09-21 14:29:42 +00:00
return logical . ErrorResponse ( fmt . Sprintf ( "key type %v does not support signing" , p . Type ) ) , logical . ErrInvalidRequest
}
2019-03-04 20:26:20 +00:00
batchInputRaw := d . Raw [ "batch_input" ]
var batchInputItems [ ] batchRequestSignItem
if batchInputRaw != nil {
err = mapstructure . Decode ( batchInputRaw , & batchInputItems )
2017-06-05 19:00:39 +00:00
if err != nil {
2018-06-12 16:24:12 +00:00
p . Unlock ( )
2021-04-22 15:20:59 +00:00
return nil , fmt . Errorf ( "failed to parse batch input: %w" , err )
2017-06-05 19:00:39 +00:00
}
2019-03-04 20:26:20 +00:00
if len ( batchInputItems ) == 0 {
p . Unlock ( )
return logical . ErrorResponse ( "missing batch input to process" ) , logical . ErrInvalidRequest
}
} else {
// use empty string if input is missing - not an error
batchInputItems = make ( [ ] batchRequestSignItem , 1 )
batchInputItems [ 0 ] = batchRequestSignItem {
"input" : d . Get ( "input" ) . ( string ) ,
"context" : d . Get ( "context" ) . ( string ) ,
}
2017-06-05 19:00:39 +00:00
}
2019-03-04 20:26:20 +00:00
response := make ( [ ] batchResponseSignItem , len ( batchInputItems ) )
for i , item := range batchInputItems {
rawInput , ok := item [ "input" ]
if ! ok {
response [ i ] . Error = "missing input"
response [ i ] . err = logical . ErrInvalidRequest
continue
}
input , err := base64 . StdEncoding . DecodeString ( rawInput )
if err != nil {
response [ i ] . Error = fmt . Sprintf ( "unable to decode input as base64: %s" , err )
response [ i ] . err = logical . ErrInvalidRequest
continue
}
if p . Type . HashSignatureInput ( ) && ! prehashed {
2021-04-08 16:43:39 +00:00
hf := keysutil . HashFuncMap [ hashAlgorithm ] ( )
2019-03-04 20:26:20 +00:00
hf . Write ( input )
input = hf . Sum ( nil )
}
contextRaw := item [ "context" ]
var context [ ] byte
if len ( contextRaw ) != 0 {
context , err = base64 . StdEncoding . DecodeString ( contextRaw )
if err != nil {
response [ i ] . Error = "failed to base64-decode context"
response [ i ] . err = logical . ErrInvalidRequest
continue
}
}
2023-01-27 19:39:58 +00:00
var managedKeyParameters keysutil . ManagedKeyParameters
if p . Type == keysutil . KeyType_MANAGED_KEY {
managedKeySystemView , ok := b . System ( ) . ( logical . ManagedKeySystemView )
if ! ok {
return nil , errors . New ( "unsupported system view" )
}
managedKeyParameters = keysutil . ManagedKeyParameters {
ManagedKeySystemView : managedKeySystemView ,
BackendUUID : b . backendUUID ,
Context : ctx ,
}
}
2022-08-31 16:27:03 +00:00
sig , err := p . SignWithOptions ( ver , context , input , & keysutil . SigningOptions {
2023-01-27 19:39:58 +00:00
HashAlgorithm : hashAlgorithm ,
Marshaling : marshaling ,
SaltLength : saltLength ,
SigAlgorithm : sigAlgorithm ,
ManagedKeyParams : managedKeyParameters ,
2022-08-31 16:27:03 +00:00
} )
2019-03-04 20:26:20 +00:00
if err != nil {
if batchInputRaw != nil {
response [ i ] . Error = err . Error ( )
}
response [ i ] . err = err
} else if sig == nil {
response [ i ] . err = fmt . Errorf ( "signature could not be computed" )
} else {
2020-06-01 17:16:01 +00:00
keyVersion := ver
if keyVersion == 0 {
keyVersion = p . LatestVersion
}
2019-03-04 20:26:20 +00:00
response [ i ] . Signature = sig . Signature
response [ i ] . PublicKey = sig . PublicKey
2020-06-01 17:16:01 +00:00
response [ i ] . KeyVersion = keyVersion
2019-03-04 20:26:20 +00:00
}
2016-09-21 14:29:42 +00:00
}
// Generate the response
2019-03-04 20:26:20 +00:00
resp := & logical . Response { }
if batchInputRaw != nil {
2022-12-13 18:03:40 +00:00
// Copy the references
for i := range batchInputItems {
response [ i ] . Reference = batchInputItems [ i ] [ "reference" ]
}
2019-03-04 20:26:20 +00:00
resp . Data = map [ string ] interface { } {
"batch_results" : response ,
}
} else {
if response [ 0 ] . Error != "" || response [ 0 ] . err != nil {
p . Unlock ( )
if response [ 0 ] . Error != "" {
return logical . ErrorResponse ( response [ 0 ] . Error ) , response [ 0 ] . err
}
2020-06-01 17:16:01 +00:00
2019-03-04 20:26:20 +00:00
return nil , response [ 0 ] . err
}
2020-06-01 17:16:01 +00:00
2019-03-04 20:26:20 +00:00
resp . Data = map [ string ] interface { } {
2020-06-01 17:16:01 +00:00
"signature" : response [ 0 ] . Signature ,
"key_version" : response [ 0 ] . KeyVersion ,
2019-03-04 20:26:20 +00:00
}
2020-06-01 17:16:01 +00:00
2019-03-04 20:26:20 +00:00
if len ( response [ 0 ] . PublicKey ) > 0 {
resp . Data [ "public_key" ] = response [ 0 ] . PublicKey
}
2017-06-05 19:00:39 +00:00
}
2018-06-12 16:24:12 +00:00
p . Unlock ( )
2016-09-21 14:29:42 +00:00
return resp , nil
}
2018-01-08 18:31:38 +00:00
func ( b * backend ) pathVerifyWrite ( ctx context . Context , req * logical . Request , d * framework . FieldData ) ( * logical . Response , error ) {
2019-03-04 20:26:20 +00:00
batchInputRaw := d . Raw [ "batch_input" ]
var batchInputItems [ ] batchRequestVerifyItem
if batchInputRaw != nil {
err := mapstructure . Decode ( batchInputRaw , & batchInputItems )
if err != nil {
2021-04-22 15:20:59 +00:00
return nil , fmt . Errorf ( "failed to parse batch input: %w" , err )
2019-03-04 20:26:20 +00:00
}
if len ( batchInputItems ) == 0 {
return logical . ErrorResponse ( "missing batch input to process" ) , logical . ErrInvalidRequest
}
} else {
// use empty string if input is missing - not an error
inputB64 := d . Get ( "input" ) . ( string )
batchInputItems = make ( [ ] batchRequestVerifyItem , 1 )
batchInputItems [ 0 ] = batchRequestVerifyItem {
"input" : inputB64 ,
}
if sig , ok := d . GetOk ( "signature" ) ; ok {
batchInputItems [ 0 ] [ "signature" ] = sig . ( string )
}
if hmac , ok := d . GetOk ( "hmac" ) ; ok {
batchInputItems [ 0 ] [ "hmac" ] = hmac . ( string )
}
batchInputItems [ 0 ] [ "context" ] = d . Get ( "context" ) . ( string )
}
// For simplicity, 'signature' and 'hmac' cannot be mixed across batch_input elements.
// If one batch_input item is 'signature', they all must be 'signature'.
// If one batch_input item is 'hmac', they all must be 'hmac'.
sigFound := false
hmacFound := false
missing := false
for _ , v := range batchInputItems {
if _ , ok := v [ "signature" ] ; ok {
sigFound = true
} else if _ , ok := v [ "hmac" ] ; ok {
hmacFound = true
} else {
missing = true
}
}
2016-09-21 14:29:42 +00:00
switch {
2019-03-04 20:26:20 +00:00
case batchInputRaw == nil && sigFound && hmacFound :
2016-09-21 14:29:42 +00:00
return logical . ErrorResponse ( "provide one of 'signature' or 'hmac'" ) , logical . ErrInvalidRequest
2019-03-04 20:26:20 +00:00
case batchInputRaw == nil && ! sigFound && ! hmacFound :
2016-09-21 14:29:42 +00:00
return logical . ErrorResponse ( "neither a 'signature' nor an 'hmac' were given to verify" ) , logical . ErrInvalidRequest
2019-03-04 20:26:20 +00:00
case sigFound && hmacFound :
return logical . ErrorResponse ( "elements of batch_input must all provide 'signature' or all provide 'hmac'" ) , logical . ErrInvalidRequest
case missing && sigFound :
return logical . ErrorResponse ( "some elements of batch_input are missing 'signature'" ) , logical . ErrInvalidRequest
case missing && hmacFound :
return logical . ErrorResponse ( "some elements of batch_input are missing 'hmac'" ) , logical . ErrInvalidRequest
case missing :
return logical . ErrorResponse ( "no batch_input elements have 'signature' or 'hmac'" ) , logical . ErrInvalidRequest
case hmacFound :
return b . pathHMACVerify ( ctx , req , d )
2016-09-21 14:29:42 +00:00
}
name := d . Get ( "name" ) . ( string )
2019-01-23 17:31:34 +00:00
hashAlgorithmStr := d . Get ( "urlalgorithm" ) . ( string )
if hashAlgorithmStr == "" {
hashAlgorithmStr = d . Get ( "hash_algorithm" ) . ( string )
if hashAlgorithmStr == "" {
hashAlgorithmStr = d . Get ( "algorithm" ) . ( string )
2022-10-27 12:26:20 +00:00
if hashAlgorithmStr == "" {
hashAlgorithmStr = defaultHashAlgorithm
}
2018-03-15 16:17:02 +00:00
}
2016-09-21 14:29:42 +00:00
}
2019-01-23 17:31:34 +00:00
hashAlgorithm , ok := keysutil . HashTypeMap [ hashAlgorithmStr ]
if ! ok {
return logical . ErrorResponse ( fmt . Sprintf ( "invalid hash algorithm %q" , hashAlgorithmStr ) ) , logical . ErrInvalidRequest
}
marshalingStr := d . Get ( "marshaling_algorithm" ) . ( string )
marshaling , ok := keysutil . MarshalingTypeMap [ marshalingStr ]
if ! ok {
return logical . ErrorResponse ( fmt . Sprintf ( "invalid marshaling type %q" , marshalingStr ) ) , logical . ErrInvalidRequest
}
2017-11-03 14:45:53 +00:00
prehashed := d . Get ( "prehashed" ) . ( bool )
2018-03-15 16:17:02 +00:00
sigAlgorithm := d . Get ( "signature_algorithm" ) . ( string )
2022-08-31 16:27:03 +00:00
saltLength , err := b . getSaltLength ( d )
if err != nil {
return logical . ErrorResponse ( err . Error ( ) ) , logical . ErrInvalidRequest
}
2016-09-21 14:29:42 +00:00
2022-10-27 12:26:20 +00:00
if hashAlgorithm == keysutil . HashTypeNone && ( ! prehashed || sigAlgorithm != "pkcs1v15" ) {
return logical . ErrorResponse ( "hash_algorithm=none requires both prehashed=true and signature_algorithm=pkcs1v15" ) , logical . ErrInvalidRequest
}
2016-09-21 14:29:42 +00:00
// Get the policy
2021-09-13 21:44:56 +00:00
p , _ , err := b . GetPolicy ( ctx , keysutil . PolicyRequest {
2018-06-12 16:24:12 +00:00
Storage : req . Storage ,
Name : name ,
2019-10-17 17:33:00 +00:00
} , b . GetRandomReader ( ) )
2016-09-21 14:29:42 +00:00
if err != nil {
return nil , err
}
if p == nil {
2017-05-12 18:14:00 +00:00
return logical . ErrorResponse ( "encryption key not found" ) , logical . ErrInvalidRequest
2016-09-21 14:29:42 +00:00
}
2018-06-12 16:24:12 +00:00
if ! b . System ( ) . CachingDisabled ( ) {
p . Lock ( false )
}
2016-09-21 14:29:42 +00:00
2017-06-05 19:00:39 +00:00
if ! p . Type . SigningSupported ( ) {
2018-06-12 16:24:12 +00:00
p . Unlock ( )
2017-06-05 19:00:39 +00:00
return logical . ErrorResponse ( fmt . Sprintf ( "key type %v does not support verification" , p . Type ) ) , logical . ErrInvalidRequest
}
2019-03-04 20:26:20 +00:00
response := make ( [ ] batchResponseVerifyItem , len ( batchInputItems ) )
for i , item := range batchInputItems {
rawInput , ok := item [ "input" ]
if ! ok {
response [ i ] . Error = "missing input"
response [ i ] . err = logical . ErrInvalidRequest
continue
}
input , err := base64 . StdEncoding . DecodeString ( rawInput )
2017-06-05 19:00:39 +00:00
if err != nil {
2019-03-04 20:26:20 +00:00
response [ i ] . Error = fmt . Sprintf ( "unable to decode input as base64: %s" , err )
response [ i ] . err = logical . ErrInvalidRequest
continue
2017-06-05 19:00:39 +00:00
}
2019-03-04 20:26:20 +00:00
sig , ok := item [ "signature" ]
if ! ok {
response [ i ] . Error = "missing signature"
response [ i ] . err = logical . ErrInvalidRequest
continue
}
2017-06-05 19:00:39 +00:00
2019-03-04 20:26:20 +00:00
if p . Type . HashSignatureInput ( ) && ! prehashed {
hf := keysutil . HashFuncMap [ hashAlgorithm ] ( )
hf . Write ( input )
input = hf . Sum ( nil )
}
contextRaw := item [ "context" ]
var context [ ] byte
if len ( contextRaw ) != 0 {
context , err = base64 . StdEncoding . DecodeString ( contextRaw )
if err != nil {
response [ i ] . Error = "failed to base64-decode context"
response [ i ] . err = logical . ErrInvalidRequest
continue
}
}
2023-01-27 19:39:58 +00:00
var managedKeyParameters keysutil . ManagedKeyParameters
if p . Type == keysutil . KeyType_MANAGED_KEY {
managedKeySystemView , ok := b . System ( ) . ( logical . ManagedKeySystemView )
if ! ok {
return nil , errors . New ( "unsupported system view" )
}
managedKeyParameters = keysutil . ManagedKeyParameters {
ManagedKeySystemView : managedKeySystemView ,
BackendUUID : b . backendUUID ,
Context : ctx ,
}
}
2019-03-04 20:26:20 +00:00
2023-01-27 19:39:58 +00:00
signingOptions := & keysutil . SigningOptions {
HashAlgorithm : hashAlgorithm ,
Marshaling : marshaling ,
SaltLength : saltLength ,
SigAlgorithm : sigAlgorithm ,
ManagedKeyParams : managedKeyParameters ,
}
valid , err := p . VerifySignatureWithOptions ( context , input , sig , signingOptions )
2019-03-04 20:26:20 +00:00
if err != nil {
switch err . ( type ) {
case errutil . UserError :
response [ i ] . Error = err . Error ( )
response [ i ] . err = logical . ErrInvalidRequest
default :
if batchInputRaw != nil {
response [ i ] . Error = err . Error ( )
}
response [ i ] . err = err
}
} else {
response [ i ] . Valid = valid
2016-09-21 14:29:42 +00:00
}
}
// Generate the response
2019-03-04 20:26:20 +00:00
resp := & logical . Response { }
if batchInputRaw != nil {
2022-12-13 18:03:40 +00:00
// Copy the references
for i := range batchInputItems {
response [ i ] . Reference = batchInputItems [ i ] [ "reference" ]
}
2019-03-04 20:26:20 +00:00
resp . Data = map [ string ] interface { } {
"batch_results" : response ,
}
} else {
if response [ 0 ] . Error != "" || response [ 0 ] . err != nil {
p . Unlock ( )
if response [ 0 ] . Error != "" {
return logical . ErrorResponse ( response [ 0 ] . Error ) , response [ 0 ] . err
}
return nil , response [ 0 ] . err
}
resp . Data = map [ string ] interface { } {
"valid" : response [ 0 ] . Valid ,
}
2016-09-21 14:29:42 +00:00
}
2018-06-12 16:24:12 +00:00
p . Unlock ( )
2016-09-21 14:29:42 +00:00
return resp , nil
}
const pathSignHelpSyn = ` Generate a signature for input data using the named key `
const pathSignHelpDesc = `
Generates a signature of the input data using the named key and the given hash algorithm .
`
const pathVerifyHelpSyn = ` Verify a signature or HMAC for input data created using the named key `
const pathVerifyHelpDesc = `
Verifies a signature or HMAC of the input data using the named key and the given hash algorithm .
`