-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
180 lines (155 loc) · 5.6 KB
/
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
package transit
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"strconv"
"strings"
"github.com/PumpkinSeed/heimdall/internal/errors"
"github.com/hashicorp/vault/sdk/helper/keysutil"
)
const (
ExportTypeEncryptionKey = "encryption-key"
ExportTypeSigningKey = "signing-key"
ExportTypeHMACKey = "hmac-key"
)
func (t Transit) Export(ctx context.Context, keyName, engineName, exportType, version string) (map[string]string, error) {
switch exportType {
case ExportTypeEncryptionKey:
case ExportTypeSigningKey:
case ExportTypeHMACKey:
default:
return nil, errors.Newf(errors.CodePkgCryptoTransitExportType, "invalid export type: [%s]", exportType)
}
p, err := t.GetKey(ctx, keyName, engineName)
if err != nil {
return nil, errors.Wrap(err, "transit export get key error", errors.CodePkgCryptoTransitExportGetKey)
}
if !p.Exportable {
return nil, errors.New("key is not exportable", errors.CodePkgCryptoTransitExportNonExportable)
}
switch exportType {
case ExportTypeEncryptionKey:
if !p.Type.EncryptionSupported() {
return nil, errors.New("encryption not supported for the key", errors.CodePkgCryptoTransitExportTypeEncryptKeyNotSupported)
}
case ExportTypeSigningKey:
if !p.Type.SigningSupported() {
return nil, errors.New("signing not supported for the key", errors.CodePkgCryptoTransitExportTypeSigningKeyNotSupported)
}
}
retKeys := map[string]string{}
switch version {
case "":
for k, v := range p.Keys {
exportKey, err := getExportKey(p, &v, exportType)
if err != nil {
return nil, errors.Wrap(err, "get export key error", errors.CodePkgCryptoTransitExportGetExportKey)
}
retKeys[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 nil, errors.Newf(errors.CodePkgCryptoTransitExportInvalidVersionFormat, "invalid key version [%s]", version)
}
}
if versionValue < p.MinDecryptionVersion {
return nil, errors.New("version for export is below minimum decryption version", errors.CodePkgCryptoTransitExportInvalidVersionLessThanMin)
}
key, ok := p.Keys[strconv.Itoa(versionValue)]
if !ok {
return nil, errors.New("version does not exist or cannot be found", errors.CodePkgCryptoTransitExportVersionNotFound)
}
exportKey, err := getExportKey(p, &key, exportType)
if err != nil {
return nil, errors.Wrap(err, "get export key error for specified version", errors.CodePkgCryptoTransitExportGetExportKey)
}
retKeys[strconv.Itoa(versionValue)] = exportKey
}
return retKeys, nil
}
func getExportKey(policy *keysutil.Policy, key *keysutil.KeyEntry, exportType string) (string, error) {
if policy == nil {
return "", errors.New("nil policy provided", errors.CodePkgCryptoTransitExportGetExportKeyPolicyMissing)
}
switch exportType {
case ExportTypeHMACKey:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.HMACKey)), nil
case ExportTypeEncryptionKey:
switch policy.Type {
case keysutil.KeyType_AES128_GCM96, keysutil.KeyType_AES256_GCM96, keysutil.KeyType_ChaCha20_Poly1305:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
case keysutil.KeyType_RSA2048, keysutil.KeyType_RSA3072, keysutil.KeyType_RSA4096:
return encodeRSAPrivateKey(key.RSAKey), nil
}
case ExportTypeSigningKey:
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 := keyEntryToECPrivateKey(key, curve)
if err != nil {
return "", errors.Wrap(err, "key entry to EC private key error", errors.CodePkgCryptoTransitExportGetExportKeyToPrivateKey)
}
return ecKey, nil
case keysutil.KeyType_ED25519:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.Key)), nil
case keysutil.KeyType_RSA2048, keysutil.KeyType_RSA3072, keysutil.KeyType_RSA4096:
return encodeRSAPrivateKey(key.RSAKey), nil
}
}
return "", errors.Newf(errors.CodePkgCryptoTransitExportGetExportKeyUnknownType, "unknown key type [%v]", policy.Type)
}
func encodeRSAPrivateKey(key *rsa.PrivateKey) string {
// When encoding PKCS1, the PEM header should be `RSA PRIVATE KEY`. When Go
// has PKCS8 encoding support, we may want to change this.
derBytes := x509.MarshalPKCS1PrivateKey(key)
pemBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derBytes,
}
pemBytes := pem.EncodeToMemory(pemBlock)
return string(pemBytes)
}
func keyEntryToECPrivateKey(k *keysutil.KeyEntry, curve elliptic.Curve) (string, error) {
if k == nil {
return "", errors.New("nil KeyEntry provided", errors.CodePkgCryptoTransitExportGetExportKeyToPrivateKeyMissingEntry)
}
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 "", errors.Wrap(err, "keyEntryToECPrivateKey x509 Marshal EC error", errors.CodePkgCryptoTransitExportGetExportKeyToPrivateKeyMarshal)
}
if ecder == nil {
return "", errors.New("no data returned when marshalling to private key", errors.CodePkgCryptoTransitExportGetExportKeyToPrivateKeyMarshalResult)
}
block := pem.Block{
Type: "EC PRIVATE KEY",
Bytes: ecder,
}
return strings.TrimSpace(string(pem.EncodeToMemory(&block))), nil
}