2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-01-23 16:04:43 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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/keysutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-01-23 16:04:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
exportTypeEncryptionKey = "encryption-key"
|
|
|
|
exportTypeSigningKey = "signing-key"
|
|
|
|
exportTypeHMACKey = "hmac-key"
|
2023-05-24 15:26:35 +00:00
|
|
|
exportTypePublicKey = "public-key"
|
2017-01-23 16:04:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *backend) pathExportKeys() *framework.Path {
|
|
|
|
return &framework.Path{
|
2017-01-28 15:37:35 +00:00
|
|
|
Pattern: "export/" + framework.GenericNameRegex("type") + "/" + framework.GenericNameRegex("name") + framework.OptionalParamRegex("version"),
|
2023-04-10 18:20:53 +00:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixTransit,
|
|
|
|
OperationVerb: "export",
|
|
|
|
OperationSuffix: "key|key-version",
|
|
|
|
},
|
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"type": {
|
2017-01-23 16:04:43 +00:00
|
|
|
Type: framework.TypeString,
|
2023-05-24 15:26:35 +00:00
|
|
|
Description: "Type of key to export (encryption-key, signing-key, hmac-key, public-key)",
|
2017-01-23 16:04:43 +00:00
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"name": {
|
2017-01-23 16:04:43 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the key",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"version": {
|
2017-01-23 16:04:43 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Version of the key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathPolicyExportRead,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathExportHelpSyn,
|
|
|
|
HelpDescription: pathExportHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathPolicyExportRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2017-01-28 15:37:35 +00:00
|
|
|
exportType := d.Get("type").(string)
|
2017-01-23 16:04:43 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
version := d.Get("version").(string)
|
|
|
|
|
|
|
|
switch exportType {
|
|
|
|
case exportTypeEncryptionKey:
|
|
|
|
case exportTypeSigningKey:
|
|
|
|
case exportTypeHMACKey:
|
2023-05-24 15:26:35 +00:00
|
|
|
case exportTypePublicKey:
|
2017-01-23 16:04:43 +00:00
|
|
|
default:
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("invalid export type: %s", exportType)), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
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())
|
2017-01-23 16:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-06-12 16:24:12 +00:00
|
|
|
if !b.System().CachingDisabled() {
|
|
|
|
p.Lock(false)
|
|
|
|
}
|
|
|
|
defer p.Unlock()
|
2017-01-23 16:04:43 +00:00
|
|
|
|
|
|
|
if !p.Exportable {
|
|
|
|
return logical.ErrorResponse("key is not exportable"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch exportType {
|
|
|
|
case exportTypeEncryptionKey:
|
|
|
|
if !p.Type.EncryptionSupported() {
|
|
|
|
return logical.ErrorResponse("encryption not supported for the key"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
case exportTypeSigningKey:
|
|
|
|
if !p.Type.SigningSupported() {
|
|
|
|
return logical.ErrorResponse("signing not supported for the key"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 15:37:35 +00:00
|
|
|
retKeys := map[string]string{}
|
|
|
|
switch version {
|
|
|
|
case "":
|
|
|
|
for k, v := range p.Keys {
|
|
|
|
exportKey, err := getExportKey(p, &v, exportType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-06 23:24:00 +00:00
|
|
|
retKeys[k] = exportKey
|
2017-01-28 15:37:35 +00:00
|
|
|
}
|
2017-01-23 16:04:43 +00:00
|
|
|
|
2017-01-28 15:37:35 +00:00
|
|
|
default:
|
2017-01-23 16:04:43 +00:00
|
|
|
var versionValue int
|
|
|
|
if version == "latest" {
|
|
|
|
versionValue = p.LatestVersion
|
|
|
|
} else {
|
|
|
|
version = strings.TrimPrefix(version, "v")
|
|
|
|
versionValue, err = strconv.Atoi(version)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse("invalid key version"), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 14:25:50 +00:00
|
|
|
if versionValue < p.MinDecryptionVersion {
|
2018-03-20 18:54:10 +00:00
|
|
|
return logical.ErrorResponse("version for export is below minimum decryption version"), logical.ErrInvalidRequest
|
2017-01-30 14:25:50 +00:00
|
|
|
}
|
2017-12-06 23:24:00 +00:00
|
|
|
key, ok := p.Keys[strconv.Itoa(versionValue)]
|
2017-01-23 16:04:43 +00:00
|
|
|
if !ok {
|
2017-01-30 14:25:50 +00:00
|
|
|
return logical.ErrorResponse("version does not exist or cannot be found"), logical.ErrInvalidRequest
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exportKey, err := getExportKey(p, &key, exportType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-28 15:37:35 +00:00
|
|
|
retKeys[strconv.Itoa(versionValue)] = exportKey
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-28 15:37:35 +00:00
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"name": p.Name,
|
|
|
|
"type": p.Type.String(),
|
|
|
|
"keys": retKeys,
|
|
|
|
},
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExportKey(policy *keysutil.Policy, key *keysutil.KeyEntry, exportType string) (string, error) {
|
|
|
|
if policy == nil {
|
|
|
|
return "", errors.New("nil policy provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch exportType {
|
|
|
|
case exportTypeHMACKey:
|
|
|
|
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.HMACKey)), nil
|
2017-01-28 15:37:35 +00:00
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
case exportTypeEncryptionKey:
|
|
|
|
switch policy.Type {
|
2019-10-03 20:11:43 +00:00
|
|
|
case keysutil.KeyType_AES128_GCM96, keysutil.KeyType_AES256_GCM96, keysutil.KeyType_ChaCha20_Poly1305:
|
2017-06-05 19:00:39 +00:00
|
|
|
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
|
2017-11-03 14:45:53 +00:00
|
|
|
|
2020-02-15 22:40:50 +00:00
|
|
|
case keysutil.KeyType_RSA2048, keysutil.KeyType_RSA3072, keysutil.KeyType_RSA4096:
|
2023-05-11 11:56:46 +00:00
|
|
|
rsaKey, err := encodeRSAPrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rsaKey, nil
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
2017-01-28 15:37:35 +00:00
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
case exportTypeSigningKey:
|
|
|
|
switch policy.Type {
|
2019-10-03 16:32:43 +00:00
|
|
|
case keysutil.KeyType_ECDSA_P256, keysutil.KeyType_ECDSA_P384, keysutil.KeyType_ECDSA_P521:
|
|
|
|
var curve elliptic.Curve
|
|
|
|
switch policy.Type {
|
|
|
|
case keysutil.KeyType_ECDSA_P384:
|
|
|
|
curve = elliptic.P384()
|
|
|
|
case keysutil.KeyType_ECDSA_P521:
|
|
|
|
curve = elliptic.P521()
|
|
|
|
default:
|
|
|
|
curve = elliptic.P256()
|
|
|
|
}
|
|
|
|
ecKey, err := keyEntryToECPrivateKey(key, curve)
|
2017-01-23 16:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ecKey, nil
|
2017-06-05 19:00:39 +00:00
|
|
|
|
|
|
|
case keysutil.KeyType_ED25519:
|
2023-05-24 15:26:35 +00:00
|
|
|
if len(key.Key) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-06-05 19:00:39 +00:00
|
|
|
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
|
2017-11-03 14:45:53 +00:00
|
|
|
|
2020-02-15 22:40:50 +00:00
|
|
|
case keysutil.KeyType_RSA2048, keysutil.KeyType_RSA3072, keysutil.KeyType_RSA4096:
|
2023-05-11 11:56:46 +00:00
|
|
|
rsaKey, err := encodeRSAPrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rsaKey, nil
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
2023-05-24 15:26:35 +00:00
|
|
|
case exportTypePublicKey:
|
|
|
|
switch policy.Type {
|
|
|
|
case keysutil.KeyType_ECDSA_P256, keysutil.KeyType_ECDSA_P384, keysutil.KeyType_ECDSA_P521:
|
|
|
|
var curve elliptic.Curve
|
|
|
|
switch policy.Type {
|
|
|
|
case keysutil.KeyType_ECDSA_P384:
|
|
|
|
curve = elliptic.P384()
|
|
|
|
case keysutil.KeyType_ECDSA_P521:
|
|
|
|
curve = elliptic.P521()
|
|
|
|
default:
|
|
|
|
curve = elliptic.P256()
|
|
|
|
}
|
|
|
|
ecKey, err := keyEntryToECPublicKey(key, curve)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ecKey, nil
|
|
|
|
|
|
|
|
case keysutil.KeyType_ED25519:
|
|
|
|
return strings.TrimSpace(key.FormattedPublicKey), nil
|
|
|
|
|
|
|
|
case keysutil.KeyType_RSA2048, keysutil.KeyType_RSA3072, keysutil.KeyType_RSA4096:
|
|
|
|
rsaKey, err := encodeRSAPublicKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rsaKey, nil
|
|
|
|
}
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:26:35 +00:00
|
|
|
return "", fmt.Errorf("unknown key type %v for export type %v", policy.Type, exportType)
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 11:56:46 +00:00
|
|
|
func encodeRSAPrivateKey(key *keysutil.KeyEntry) (string, error) {
|
2023-05-24 15:26:35 +00:00
|
|
|
if key == nil {
|
|
|
|
return "", errors.New("nil KeyEntry provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.IsPrivateKeyMissing() {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-11-03 14:45:53 +00:00
|
|
|
// When encoding PKCS1, the PEM header should be `RSA PRIVATE KEY`. When Go
|
|
|
|
// has PKCS8 encoding support, we may want to change this.
|
2023-05-24 15:26:35 +00:00
|
|
|
blockType := "RSA PRIVATE KEY"
|
|
|
|
derBytes := x509.MarshalPKCS1PrivateKey(key.RSAKey)
|
|
|
|
pemBlock := pem.Block{
|
|
|
|
Type: blockType,
|
|
|
|
Bytes: derBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
pemBytes := pem.EncodeToMemory(&pemBlock)
|
|
|
|
return string(pemBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeRSAPublicKey(key *keysutil.KeyEntry) (string, error) {
|
|
|
|
if key == nil {
|
|
|
|
return "", errors.New("nil KeyEntry provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
blockType := "RSA PUBLIC KEY"
|
|
|
|
derBytes, err := x509.MarshalPKIXPublicKey(key.RSAPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-05-11 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pemBlock := pem.Block{
|
|
|
|
Type: blockType,
|
2017-11-03 14:45:53 +00:00
|
|
|
Bytes: derBytes,
|
|
|
|
}
|
2023-05-11 11:56:46 +00:00
|
|
|
|
|
|
|
pemBytes := pem.EncodeToMemory(&pemBlock)
|
|
|
|
return string(pemBytes), nil
|
2017-11-03 14:45:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
func keyEntryToECPrivateKey(k *keysutil.KeyEntry, curve elliptic.Curve) (string, error) {
|
|
|
|
if k == nil {
|
|
|
|
return "", errors.New("nil KeyEntry provided")
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:26:35 +00:00
|
|
|
if k.IsPrivateKeyMissing() {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 11:56:46 +00:00
|
|
|
pubKey := ecdsa.PublicKey{
|
|
|
|
Curve: curve,
|
|
|
|
X: k.EC_X,
|
|
|
|
Y: k.EC_Y,
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
2023-05-11 11:56:46 +00:00
|
|
|
|
2023-05-24 15:26:35 +00:00
|
|
|
blockType := "EC PRIVATE KEY"
|
|
|
|
privKey := &ecdsa.PrivateKey{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
D: k.EC_D,
|
|
|
|
}
|
|
|
|
derBytes, err := x509.MarshalECPrivateKey(privKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-05-11 11:56:46 +00:00
|
|
|
|
2023-05-24 15:26:35 +00:00
|
|
|
pemBlock := pem.Block{
|
|
|
|
Type: blockType,
|
|
|
|
Bytes: derBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(pem.EncodeToMemory(&pemBlock))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyEntryToECPublicKey(k *keysutil.KeyEntry, curve elliptic.Curve) (string, error) {
|
|
|
|
if k == nil {
|
|
|
|
return "", errors.New("nil KeyEntry provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey := ecdsa.PublicKey{
|
|
|
|
Curve: curve,
|
|
|
|
X: k.EC_X,
|
|
|
|
Y: k.EC_Y,
|
|
|
|
}
|
|
|
|
|
|
|
|
blockType := "PUBLIC KEY"
|
|
|
|
derBytes, err := x509.MarshalPKIXPublicKey(&pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 11:56:46 +00:00
|
|
|
pemBlock := pem.Block{
|
|
|
|
Type: blockType,
|
|
|
|
Bytes: derBytes,
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
2023-05-11 11:56:46 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(string(pem.EncodeToMemory(&pemBlock))), nil
|
2017-01-23 16:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathExportHelpSyn = `Export named encryption or signing key`
|
|
|
|
|
|
|
|
const pathExportHelpDesc = `
|
|
|
|
This path is used to export the named keys that are configured as
|
|
|
|
exportable.
|
|
|
|
`
|