forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_export.go
207 lines (178 loc) · 5.07 KB
/
path_export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package transit
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"strconv"
"strings"
"github.com/hashicorp/vault/helper/keysutil"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
const (
exportTypeEncryptionKey = "encryption-key"
exportTypeSigningKey = "signing-key"
exportTypeHMACKey = "hmac-key"
)
func (b *backend) pathExportKeys() *framework.Path {
return &framework.Path{
Pattern: "export/" + framework.GenericNameRegex("type") + "/" + framework.GenericNameRegex("name") + framework.OptionalParamRegex("version"),
Fields: map[string]*framework.FieldSchema{
"type": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Type of key to export (encryption-key, signing-key, hmac-key)",
},
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the key",
},
"version": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Version of the key",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathPolicyExportRead,
},
HelpSynopsis: pathExportHelpSyn,
HelpDescription: pathExportHelpDesc,
}
}
func (b *backend) pathPolicyExportRead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
exportType := d.Get("type").(string)
name := d.Get("name").(string)
version := d.Get("version").(string)
switch exportType {
case exportTypeEncryptionKey:
case exportTypeSigningKey:
case exportTypeHMACKey:
default:
return logical.ErrorResponse(fmt.Sprintf("invalid export type: %s", exportType)), logical.ErrInvalidRequest
}
p, lock, err := b.lm.GetPolicyShared(req.Storage, name)
if lock != nil {
defer lock.RUnlock()
}
if err != nil {
return nil, err
}
if p == nil {
return nil, nil
}
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
}
}
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
}
retKeys[strconv.Itoa(k)] = exportKey
}
default:
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
}
}
if versionValue < p.MinDecryptionVersion {
return logical.ErrorResponse("version for export is below minimun decryption version"), logical.ErrInvalidRequest
}
key, ok := p.Keys[versionValue]
if !ok {
return logical.ErrorResponse("version does not exist or cannot be found"), logical.ErrInvalidRequest
}
exportKey, err := getExportKey(p, &key, exportType)
if err != nil {
return nil, err
}
retKeys[strconv.Itoa(versionValue)] = exportKey
}
resp := &logical.Response{
Data: map[string]interface{}{
"name": p.Name,
"type": p.Type.String(),
"keys": retKeys,
},
}
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
case exportTypeEncryptionKey:
switch policy.Type {
case keysutil.KeyType_AES256_GCM96:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
}
case exportTypeSigningKey:
switch policy.Type {
case keysutil.KeyType_ECDSA_P256:
ecKey, err := keyEntryToECPrivateKey(key, elliptic.P256())
if err != nil {
return "", err
}
return ecKey, nil
case keysutil.KeyType_ED25519:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
}
}
return "", fmt.Errorf("unknown key type %v", policy.Type)
}
func keyEntryToECPrivateKey(k *keysutil.KeyEntry, curve elliptic.Curve) (string, error) {
if k == nil {
return "", errors.New("nil KeyEntry provided")
}
privKey := &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: k.EC_X,
Y: k.EC_Y,
},
D: k.EC_D,
}
ecder, err := x509.MarshalECPrivateKey(privKey)
if err != nil {
return "", err
}
if ecder == nil {
return "", errors.New("No data returned when marshalling to private key")
}
block := pem.Block{
Type: "EC PRIVATE KEY",
Bytes: ecder,
}
return strings.TrimSpace(string(pem.EncodeToMemory(&block))), nil
}
const pathExportHelpSyn = `Export named encryption or signing key`
const pathExportHelpDesc = `
This path is used to export the named keys that are configured as
exportable.
`