-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_ca.go
More file actions
451 lines (403 loc) · 11.4 KB
/
path_ca.go
File metadata and controls
451 lines (403 loc) · 11.4 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package kmipengine
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/hashicorp/vault/helper/namespace"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"math/big"
"strings"
"sync"
)
const (
caPath = "ca"
serialNumberPath = "serial_number"
)
// RSAKey and ECDSAKey represent the private keys of RSA and ECDSA, respectively
type RSAKey struct {
PrivateKey *rsa.PrivateKey `json:"private_key_rsa"`
}
type ECDSAKey struct {
PrivateKey *ecdsa.PrivateKey `json:"private_key_ecdsa"`
}
// PrivateKeyType is an interface for private key types
type PrivateKeyType interface {
isPrivateKeyType() tlsKeyType
}
func (r *RSAKey) isPrivateKeyType() tlsKeyType { return rsaKeyType }
func (e *ECDSAKey) isPrivateKeyType() tlsKeyType { return ecKeyType }
// hashiCupsConfig includes the minimum configuration
// required to instantiate a new HashiCups client.
type CA struct {
lock *sync.RWMutex `json:"-"`
ParentCaSn string `json:"parent_ca_sn"`
CertPEM string `json:"cert_pem"`
CertBytes []byte `json:"cert_bytes"`
Cert *x509.Certificate `json:"cert"`
//PrivateKey interface{} `json:"private_key"`
PrivateKey PrivateKeyType `json:"private_key"`
sn string
}
func (kb *KmipBackend) newCA(sn string) *CA {
kb.lock.Lock()
defer kb.lock.Unlock()
if lock, ok := kb.certLock[sn]; ok {
return &CA{
lock: lock,
sn: sn,
}
} else {
kb.certLock[sn] = new(sync.RWMutex)
return &CA{
lock: kb.certLock[sn],
sn: sn,
}
}
}
func (c *CA) readStorage(ctx context.Context, storage logical.Storage, key string) error {
c.lock.RLock()
defer c.lock.RUnlock()
path := key
if !strings.HasSuffix(key, "/") {
path = path + "/"
}
path = path + c.sn
data, err := readStorage(ctx, storage, path)
if err != nil {
return err
}
privateKeyData := data["private_key"].(map[string]interface{})
delete(data, "private_key")
if _, ok := privateKeyData["private_key_rsa"]; ok {
key := new(RSAKey)
if err := MapToStruct(privateKeyData, key); err != nil {
return err
}
c.PrivateKey = key
} else if _, ok := privateKeyData["private_key_ecdsa"]; ok {
key := new(ECDSAKey)
if err := MapToStruct(privateKeyData, key); err != nil {
return err
}
c.PrivateKey = key
}
if err := MapToStruct(data, c); err != nil {
return err
}
return nil
}
func (c *CA) writeStorage(ctx context.Context, storage logical.Storage, key string) error {
c.lock.Lock()
defer c.lock.Unlock()
buf, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("json encoding failed: %w", err)
}
if !strings.HasSuffix(key, "/") {
key = key + "/"
}
// Write out a new key
entry := &logical.StorageEntry{
Key: key + c.Cert.SerialNumber.String(),
Value: buf,
}
if err := storage.Put(ctx, entry); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
return nil
}
func pathCa(b *KmipBackend) *framework.Path {
return &framework.Path{
Pattern: caPath,
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: "kmip",
},
TakesArbitraryInput: true,
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handleCARead(),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "read",
},
},
},
ExistenceCheck: b.handleCAExistenceCheck(),
HelpSynopsis: strings.TrimSpace(KmipHelpSynopsis),
HelpDescription: strings.TrimSpace(KmipHelpDescription),
}
}
func (kb *KmipBackend) handleCAExistenceCheck() framework.ExistenceFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
key := caPath
kb.tokenAccessor = req.ClientTokenAccessor
out, err := req.Storage.Get(ctx, key)
if err != nil {
return false, fmt.Errorf("existence check failed: %w", err)
}
return out != nil, nil
}
}
func (kb *KmipBackend) handleCARead() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
sn, err := listStorage(ctx, req.Storage, caPath)
if err != nil {
return nil, err
}
// read cert pem
resData := map[string]interface{}{}
pem := ""
for _, k := range sn {
ca := kb.newCA(k)
ca.readStorage(ctx, req.Storage, caPath)
if err != nil {
return nil, err
}
Data := map[string]interface{}{
"ca_pem": ca.CertPEM,
"privateKey": ca.PrivateKeyPEM(),
}
resData[k] = Data
pem = pem + ca.CertPEM
}
resData["pem"] = pem
resp := &logical.Response{
Secret: &logical.Secret{},
Data: resData,
}
return resp, nil
}
}
func (c *CA) setCA(certPEM string, certBytes []byte, cert *x509.Certificate, privateKey PrivateKeyType, sn *big.Int) {
c.Cert = cert
c.CertPEM = certPEM
c.PrivateKey = privateKey
c.CertBytes = certBytes
if sn == nil {
c.ParentCaSn = ""
} else {
c.ParentCaSn = sn.String()
}
}
func (c *CA) SetCACert(role, scope, ttl string, ns *namespace.Namespace, sn *SerialNumber) {
duration, _ := time.ParseDuration(ttl)
cert := &x509.Certificate{
SerialNumber: sn.SN,
Subject: pkix.Name{
Province: []string{ns.Path}, // namespacePath
Locality: []string{scope}, // scope
StreetAddress: []string{role}, // role
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(duration), // Validity period
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
BasicConstraintsValid: true,
IsCA: true,
}
c.Cert = cert
}
func (c *CA) setToken(auth *logical.Auth) {
c.Cert.Subject.PostalCode = []string{auth.ClientToken}
c.Cert.Subject.CommonName = auth.Accessor
}
func (c *CA) getTokenAccessor() string {
return c.Cert.Subject.CommonName
}
func (c *CA) PrivateKeyPEM() []byte {
var keyBytes []byte
var _type string
switch c.PrivateKey.isPrivateKeyType() {
case rsaKeyType:
privateKey := c.PrivateKey.(*RSAKey)
keyBytes = x509.MarshalPKCS1PrivateKey(privateKey.PrivateKey)
_type = "RSA PRIVATE KEY"
case ecKeyType:
privateKey := c.PrivateKey.(*ECDSAKey)
keyBytes, _ = x509.MarshalECPrivateKey(privateKey.PrivateKey)
_type = "EC PRIVATE KEY"
}
//Convert private key to PEM format
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: _type,
Bytes: keyBytes,
})
return privateKeyPEM
}
func (c *CA) PublicKeyPEM() []byte {
var publicKey any
keyType := ""
switch c.PrivateKey.isPrivateKeyType() {
case rsaKeyType:
privateKey := c.PrivateKey.(*RSAKey)
publicKey = privateKey.PrivateKey.PublicKey
keyType = "RSA PUBLIC KEY"
case ecKeyType:
privateKey := c.PrivateKey.(*ECDSAKey)
publicKey = privateKey.PrivateKey.PublicKey
keyType = "EC PUBLIC KEY"
}
derBytes, _ := x509.MarshalPKIXPublicKey(publicKey)
//if err != nil {
// return nil, err
//}
//Convert private key to PEM format
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: keyType,
Bytes: derBytes,
})
return privateKeyPEM
}
func (c *CA) CaGenerate(tlsCAKeyType tlsKeyType, tlsCAKeyBits int, parentCA *CA) error {
// Set certificate information
var CertBytes []byte
// Generate random private key
switch tlsCAKeyType {
case rsaKeyType:
var privateKey *rsa.PrivateKey
var err error
switch tlsCAKeyBits {
case 2048:
privateKey, err = rsa.GenerateKey(rand.Reader, tlsCAKeyBits)
case 3072:
privateKey, err = rsa.GenerateKey(rand.Reader, tlsCAKeyBits)
case 4096:
privateKey, err = rsa.GenerateKey(rand.Reader, tlsCAKeyBits)
default:
return fmt.Errorf("unsupport rsa key bits size")
}
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
// Generate certificate
if parentCA == nil {
CertBytes, err = x509.CreateCertificate(rand.Reader, c.Cert, c.Cert, &privateKey.PublicKey, privateKey)
c.ParentCaSn = ""
} else {
rootPrivateKey := parentCA.PrivateKey.(*RSAKey)
CertBytes, err = x509.CreateCertificate(rand.Reader, c.Cert, parentCA.Cert, &privateKey.PublicKey, rootPrivateKey.PrivateKey)
c.ParentCaSn = parentCA.Cert.SerialNumber.String()
}
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
c.CertPEM, err = CertPEM(CertBytes)
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
c.CertBytes = CertBytes
c.PrivateKey = &RSAKey{PrivateKey: privateKey}
return nil
case ecKeyType:
var privateKey *ecdsa.PrivateKey
var err error
switch tlsCAKeyBits {
case 256:
privateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
case 384:
privateKey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
case 521:
privateKey, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
default:
return fmt.Errorf("unsupport ec key bits size")
}
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
if parentCA == nil {
CertBytes, err = x509.CreateCertificate(rand.Reader, c.Cert, c.Cert, &privateKey.PublicKey, privateKey)
c.ParentCaSn = ""
} else {
rootPrivateKey := parentCA.PrivateKey.(*ECDSAKey)
CertBytes, err = x509.CreateCertificate(rand.Reader, c.Cert, parentCA.Cert, &privateKey.PublicKey, rootPrivateKey.PrivateKey)
c.ParentCaSn = parentCA.Cert.SerialNumber.String()
}
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
c.CertPEM, err = CertPEM(CertBytes)
if err != nil {
fmt.Println("Failed to create certificate:", err)
return err
}
c.CertBytes = CertBytes
c.PrivateKey = &ECDSAKey{PrivateKey: privateKey}
return nil
}
return fmt.Errorf("This type of certificate type is not supported")
}
type SerialNumber struct {
SN *big.Int
lock *sync.Mutex
}
func (kb *KmipBackend) newSerialNumber() *SerialNumber {
kb.lock.Lock()
defer kb.lock.Unlock()
return &SerialNumber{lock: kb.snLock}
}
func (sn *SerialNumber) readStorage(ctx context.Context, storage logical.Storage) error {
sn.lock.Lock()
data, err := readStorage(ctx, storage, serialNumberPath)
sn.lock.Unlock()
var snOld SerialNumber
if err != nil {
// SerialNumber not initialized
if err.Error() == errPathDataIsEmpty {
snOld = SerialNumber{
lock: sn.lock,
SN: big.NewInt(0),
}
} else {
return err
}
} else {
MapToStruct(data, &snOld)
snOld.lock = sn.lock
}
// update SN, Write SN+1 back to Storage
snNew := snOld
snNew.SN.Add(snNew.SN, big.NewInt(1))
snNew.writeStorage(ctx, storage)
// return sn
sn.SN = snOld.SN
return nil
}
func (sn *SerialNumber) writeStorage(ctx context.Context, storage logical.Storage) error {
sn.lock.Lock()
defer sn.lock.Unlock()
buf, err := json.Marshal(sn)
if err != nil {
return fmt.Errorf("json encoding failed: %w", err)
}
// Write out a new key
entry := &logical.StorageEntry{
Key: serialNumberPath,
Value: buf,
}
if err := storage.Put(ctx, entry); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
return nil
}
// pathConfigHelpSynopsis summarizes the help text for the configuration
const pathConfigHelpSynopsis = `Configure the HashiCups backend.`
// pathConfigHelpDescription describes the help text for the configuration
const pathConfigHelpDescription = `
The HashiCups secret backend requires credentials for managing
JWTs issued to users working with the products API.
You must sign up with a username and password and
specify the HashiCups address for the products API
before using this secrets backend.
`