2016-09-21 14:29:42 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-09-21 14:29:42 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
|
2021-12-08 18:29:33 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-09-21 14:29:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *backend) pathHash() *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "hash" + framework.OptionalParamRegex("urlalgorithm"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"input": {
|
2016-09-21 14:29:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "The base64-encoded input data",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"algorithm": {
|
2016-09-21 14:29:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "sha2-256",
|
|
|
|
Description: `Algorithm to use (POST body parameter). Valid values are:
|
|
|
|
|
|
|
|
* sha2-224
|
|
|
|
* sha2-256
|
|
|
|
* sha2-384
|
|
|
|
* sha2-512
|
2021-12-08 18:29:33 +00:00
|
|
|
* sha3-224
|
|
|
|
* sha3-256
|
|
|
|
* sha3-384
|
|
|
|
* sha3-512
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
Defaults to "sha2-256".`,
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"urlalgorithm": {
|
2016-09-21 14:29:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Algorithm to use (POST URL parameter)`,
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"format": {
|
2016-09-21 14:29:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "hex",
|
|
|
|
Description: `Encoding format to use. Can be "hex" or "base64". Defaults to "hex".`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.UpdateOperation: b.pathHashWrite,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathHashHelpSyn,
|
|
|
|
HelpDescription: pathHashHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathHashWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-09-21 14:29:42 +00:00
|
|
|
inputB64 := d.Get("input").(string)
|
|
|
|
format := d.Get("format").(string)
|
|
|
|
algorithm := d.Get("urlalgorithm").(string)
|
|
|
|
if algorithm == "" {
|
|
|
|
algorithm = d.Get("algorithm").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
input, err := base64.StdEncoding.DecodeString(inputB64)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unable to decode input as base64: %s", err)), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case "hex":
|
|
|
|
case "base64":
|
|
|
|
default:
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unsupported encoding format %s; must be \"hex\" or \"base64\"", format)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var hf hash.Hash
|
|
|
|
switch algorithm {
|
|
|
|
case "sha2-224":
|
|
|
|
hf = sha256.New224()
|
|
|
|
case "sha2-256":
|
|
|
|
hf = sha256.New()
|
|
|
|
case "sha2-384":
|
|
|
|
hf = sha512.New384()
|
|
|
|
case "sha2-512":
|
|
|
|
hf = sha512.New()
|
2021-12-08 18:29:33 +00:00
|
|
|
case "sha3-224":
|
|
|
|
hf = sha3.New224()
|
|
|
|
case "sha3-256":
|
|
|
|
hf = sha3.New256()
|
|
|
|
case "sha3-384":
|
|
|
|
hf = sha3.New384()
|
|
|
|
case "sha3-512":
|
|
|
|
hf = sha3.New512()
|
2016-09-21 14:29:42 +00:00
|
|
|
default:
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unsupported algorithm %s", algorithm)), nil
|
|
|
|
}
|
|
|
|
hf.Write(input)
|
|
|
|
retBytes := hf.Sum(nil)
|
|
|
|
|
|
|
|
var retStr string
|
|
|
|
switch format {
|
|
|
|
case "hex":
|
|
|
|
retStr = hex.EncodeToString(retBytes)
|
|
|
|
case "base64":
|
|
|
|
retStr = base64.StdEncoding.EncodeToString(retBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"sum": retStr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathHashHelpSyn = `Generate a hash sum for input data`
|
|
|
|
|
|
|
|
const pathHashHelpDesc = `
|
|
|
|
Generates a hash sum of the given algorithm against the given input data.
|
|
|
|
`
|