Skip to content

Commit 64781b0

Browse files
Merge pull request #24 from mode51software/develop
Sign Self Issued and Delete
2 parents 4ba4f4e + ba26bf3 commit 64781b0

7 files changed

Lines changed: 138 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v0.3.4
2+
### 27/03/2021
3+
4+
* Sign Self Issued
5+
16
## v0.3.3
27
### 18/03/2021
38

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,30 @@ View the [TESTING](TESTING.md) README
216216

217217
### Troubleshooting
218218

219+
#### Vault
220+
221+
* api_addr must be configured in [Vault's server configuration file](https://www.vaultproject.io/docs/configuration#api_addr) otherwise plugins don't work properly.
222+
223+
219224
#### SafeNet DPoD [Troubleshooting](https://thalesdocs.com/dpod/services/hsmod_services/hsmod_troubleshooting/index.html)
220225

226+
##### Environment
227+
228+
The setenv script sets the following environment var. Please use the path to the root of your dpod files:
229+
230+
```
231+
declare -x ChrystokiConfigurationPath="/opt/safenet/dpod/current"
232+
```
233+
234+
This can be set in the service section of the systemd configuration file:
235+
236+
```
237+
Environment="ChrystokiConfigurationPath=/opt/safenet/dpod/current"
238+
```
239+
221240
##### HSM error code 0x80001604
222241

223-
This may indicate that the SafeNet DPoD partition is full
242+
* This may indicate that the SafeNet DPoD partition is full
224243

225244
## License
226245

conf/config-nshield.hcl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
lib = "/opt/apps/nfast/20201219/bin/libcknfast.so"
3+
# list slots using pkcs11-tool -L --module /opt/nfast/toolkits/pkcs11/libcknfast.so and use the decimal slot ID
4+
slot_id = 761406614
5+
pin = "1234"
6+
# be aware that the key_label can be overridden by dynamically providing it during Set Signed Intermediate
7+
#key_label = "ECTestCAInterKey0016"
8+
#key_label = "ECTestCARootKey0017"
9+
connect_timeout_s = 10
10+
read_timeout_s = 5

pkg/hsmpki/backend.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ import (
2020
type cachedCAConfig struct {
2121
caKeyAlias string
2222
hashAlgo crypto.Hash
23+
caKeyType uint
24+
caKeySize int
25+
}
26+
27+
func (c *cachedCAConfig) flushCAConfig() {
28+
c.caKeyAlias = ""
29+
c.caKeyType = 0
30+
c.caKeySize = 0
2331
}
2432

2533
type HsmPkiBackend struct {
@@ -96,7 +104,7 @@ func Backend(conf *logical.BackendConfig) (*HsmPkiBackend, error) {
96104
pki.PathRoles(&b.pkiBackend.Backend),
97105
pathGenerateRoot(b),
98106
pathSignIntermediate(b),
99-
//pathSignSelfIssued(b),
107+
pathSignSelfIssued(b),
100108
pathDeleteRoot(b),
101109
pathGenerateIntermediate(b),
102110
pathSetSignedIntermediate(b),
@@ -258,7 +266,15 @@ func (b *HsmPkiBackend) loadStorage() {
258266
b.cachedCAConfig.caKeyAlias = b.pkcs11client.HsmConfig.KeyLabel
259267
} else {
260268
b.cachedCAConfig.caKeyAlias = string(caKeyAlias.Value)
261-
msg := fmt.Sprintf("Found HSM key label in storage: %s alias: %s", caKeyAlias.Value, b.cachedCAConfig.caKeyAlias)
269+
270+
if keyType, err := b.loadCAKeyType(context.Background(), b.pkiBackend.GetStorage()); err != nil {
271+
b.pkiBackend.Backend.Logger().Error("Error retrieving key type")
272+
} else {
273+
b.cachedCAConfig.caKeyType = keyType
274+
}
275+
276+
msg := fmt.Sprintf("Found HSM key label in storage: %s alias: %s keyType: %d",
277+
caKeyAlias.Value, b.cachedCAConfig.caKeyAlias, b.cachedCAConfig.caKeyType)
262278
b.pkiBackend.Backend.Logger().Info(msg)
263279
}
264280

pkg/hsmpki/hsmhelpers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,10 @@ func CreateCertificate(b *HsmPkiBackend, data *certutil.CreationBundle) (*certut
329329
// gen a new key label based on the curr time
330330
keyLabel := "ROOTCA" + GenDateTimeKeyLabel()
331331
b.cachedCAConfig.caKeyAlias = keyLabel
332-
b.saveCAKeyData(context.Background(), b.pkiBackend.GetStorage(),
333-
&keyLabel, keyType, data.Params.KeyBits)
332+
if err = b.saveCAKeyData(context.Background(), b.pkiBackend.GetStorage(),
333+
&keyLabel, keyType, data.Params.KeyBits); err != nil {
334+
return nil, errutil.InternalError{err.Error()}
335+
}
334336
}
335337

336338
keyConfig := &pkcs11client.KeyConfig{Label: b.cachedCAConfig.caKeyAlias, Type: keyType, KeyBits: data.Params.KeyBits}

pkg/hsmpki/hsmpath_fetch.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,72 @@ func (b *HsmPkiBackend) loadCAKeyAlias(ctx context.Context, storage logical.Stor
4646
return b.loadCAKeyData(ctx, storage, PATH_CAKEYLABEL)
4747
}
4848

49-
/*func (b *HsmPkiBackend) loadCAKeyType(ctx context.Context, storage logical.Storage) (*logical.StorageEntry, error) {
50-
return b.loadCAKeyData(ctx, storage, PATH_CAKEYTYPE)
49+
func (b *HsmPkiBackend) loadCAKeyType(ctx context.Context, storage logical.Storage) (uint, error) {
50+
if data, err := b.loadCAKeyData(ctx, storage, PATH_CAKEYTYPE); err != nil {
51+
b.cachedCAConfig.caKeyType = 0
52+
return 0, err
53+
} else {
54+
val := uint(data.Value[0])
55+
b.cachedCAConfig.caKeyType = val
56+
return val, nil
57+
}
5158
}
5259

53-
func (b *HsmPkiBackend) loadCAKeySize(ctx context.Context, storage logical.Storage) (*logical.StorageEntry, error) {
54-
return b.loadCAKeyData(ctx, storage, PATH_CAKEYSIZE)
55-
}*/
60+
func (b *HsmPkiBackend) loadCAKeySize(ctx context.Context, storage logical.Storage) (int, error) {
61+
if data, err := b.loadCAKeyData(ctx, storage, PATH_CAKEYSIZE); err != nil {
62+
b.cachedCAConfig.caKeySize = 0
63+
return 0, err
64+
} else {
65+
val := (int(data.Value[0]) * 256) + (int(data.Value[1]))
66+
b.cachedCAConfig.caKeySize = val
67+
return val, nil
68+
}
69+
}
5670

5771
func (b *HsmPkiBackend) loadCAKeyData(ctx context.Context, storage logical.Storage, data string) (*logical.StorageEntry, error) {
58-
caKeyAlias, err := storage.Get(ctx, data)
72+
ret, err := storage.Get(ctx, data)
5973
if err != nil {
6074
return nil, err
6175
}
62-
return caKeyAlias, nil
76+
return ret, nil
6377
}
6478

6579
func (b *HsmPkiBackend) saveCAKeyData(ctx context.Context, storage logical.Storage,
66-
caKeyAlias *string, caKeyType uint, caKeySize int) error {
67-
return b.saveStoreData(ctx, storage, PATH_CAKEYLABEL, caKeyAlias)
80+
caKeyLabel *string, caKeyType uint, caKeySize int) (err error) {
81+
if err = b.saveCAKeyLabel(ctx, storage, caKeyLabel); err != nil {
82+
return
83+
}
84+
if err = b.saveCAKeyType(ctx, storage, caKeyType); err != nil {
85+
return
86+
}
87+
if err = b.saveCAKeySize(ctx, storage, caKeySize); err != nil {
88+
return
89+
}
90+
return
6891
}
6992

7093
func (b *HsmPkiBackend) saveCAKeyLabel(ctx context.Context, storage logical.Storage, caKeyAlias *string) error {
71-
return b.saveStoreData(ctx, storage, PATH_CAKEYLABEL, caKeyAlias)
94+
data := []byte(*caKeyAlias)
95+
b.cachedCAConfig.caKeyAlias = *caKeyAlias
96+
return b.saveStoreData(ctx, storage, PATH_CAKEYLABEL, &data)
7297
}
7398

74-
/*func (b *HsmPkiBackend) saveCAKeyType(ctx context.Context, storage logical.Storage, caKeyType uint) error {
75-
return b.saveStoreData(ctx, storage, PATH_CAKEYTYPE, caKeyType)
99+
func (b *HsmPkiBackend) saveCAKeyType(ctx context.Context, storage logical.Storage, caKeyType uint) error {
100+
data := []byte{byte(caKeyType)}
101+
b.cachedCAConfig.caKeyType = caKeyType
102+
return b.saveStoreData(ctx, storage, PATH_CAKEYTYPE, &data)
76103
}
77104

78105
func (b *HsmPkiBackend) saveCAKeySize(ctx context.Context, storage logical.Storage, caKeySize int) error {
79-
return b.saveStoreData(ctx, storage, PATH_CAKEYSIZE, caKeySize)
80-
}*/
106+
data := []byte{byte(caKeySize / 256), byte(caKeySize % 256)}
107+
b.cachedCAConfig.caKeySize = caKeySize
108+
return b.saveStoreData(ctx, storage, PATH_CAKEYSIZE, &data)
109+
}
81110

82-
func (b *HsmPkiBackend) saveStoreData(ctx context.Context, storage logical.Storage, path string, data *string) error {
111+
func (b *HsmPkiBackend) saveStoreData(ctx context.Context, storage logical.Storage, path string, data *[]byte) error {
83112
entry := logical.StorageEntry{
84113
Key: path,
85-
Value: []byte(*data),
114+
Value: *data,
86115
SealWrap: false,
87116
}
88117
if err := storage.Put(ctx, &entry); err != nil {

pkg/hsmpki/hsmpath_root.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package hsmpki
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"crypto/x509"
57
"encoding/base64"
8+
"encoding/pem"
69
"fmt"
10+
"github.com/hashicorp/vault/sdk/helper/certutil"
711
"github.com/mode51software/pkcs11helper/pkg/pkcs11client"
812
"github.com/mode51software/vaultplugin-hsmpki/pkg/pki"
13+
"reflect"
14+
"strings"
915
"time"
1016

1117
"github.com/hashicorp/errwrap"
@@ -86,7 +92,7 @@ the non-repudiation flag.`,
8692
return ret
8793
}
8894

89-
/*func pathSignSelfIssued(b *HsmPkiBackend) *framework.Path {
95+
func pathSignSelfIssued(b *HsmPkiBackend) *framework.Path {
9096
ret := &framework.Path{
9197
Pattern: "root/sign-self-issued",
9298

@@ -106,7 +112,7 @@ the non-repudiation flag.`,
106112
}
107113

108114
return ret
109-
}*/
115+
}
110116

111117
func (b *HsmPkiBackend) pathCADeleteRoot(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
112118

@@ -121,6 +127,8 @@ func (b *HsmPkiBackend) pathCADeleteRoot(ctx context.Context, req *logical.Reque
121127
if err := b.pkcs11client.DeleteKeyPair(&keyConfig); err != nil {
122128
return nil, errutil.UserError{"Unable to delete CA #{{err}}"}
123129
}
130+
b.cachedCAConfig.flushCAConfig()
131+
b.saveCAKeyData(ctx, req.Storage, &b.cachedCAConfig.caKeyAlias, 0, 0)
124132

125133
}
126134
return nil, req.Storage.Delete(ctx, CA_BUNDLE)
@@ -407,7 +415,7 @@ func (b *HsmPkiBackend) pathCASignIntermediate(ctx context.Context, req *logical
407415
return resp, nil
408416
}
409417

410-
/*func (b *HsmPkiBackend) pathCASignSelfIssued(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
418+
func (b *HsmPkiBackend) pathCASignSelfIssued(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
411419
var err error
412420

413421
if err = b.checkPkcs11ConnectionSync(); err != nil {
@@ -459,17 +467,36 @@ func (b *HsmPkiBackend) pathCASignIntermediate(ctx context.Context, req *logical
459467
cert.CRLDistributionPoints = urls.CRLDistributionPoints
460468
cert.OCSPServer = urls.OCSPServers
461469

462-
// msg := fmt.Sprintf("sign type=%s", signingCB.PrivateKeyType)
463-
// b.pkiBackend.Backend.Logger().Info(msg)
470+
if len(b.cachedCAConfig.caKeyAlias) == 0 {
471+
return nil, errutil.UserError{Err: "No HSM key label has been set"}
472+
}
473+
474+
msg := fmt.Sprintf("keyLabel=%s, keyType=%d, keySize=%d",
475+
b.cachedCAConfig.caKeyAlias, b.cachedCAConfig.caKeyType, b.cachedCAConfig.caKeySize)
476+
b.pkiBackend.Backend.Logger().Info(msg)
464477

465-
// publicKey, err := b.pkcs11client.ReadPublicKey(&keyConfig, keyConfig.Type)
478+
// msg := fmt.Sprintf("sign type=%s", signingCB.PrivateKeyType)
479+
// b.pkiBackend.Backend.Logger().Info(msg)
480+
481+
keyConfig := pkcs11client.KeyConfig{
482+
Label: b.cachedCAConfig.caKeyAlias,
483+
Type: b.cachedCAConfig.caKeyType,
484+
KeyBits: b.cachedCAConfig.caKeySize,
485+
}
486+
publicKey, err := b.pkcs11client.ReadPublicKey(&keyConfig, b.cachedCAConfig.caKeyType)
487+
488+
if err != nil {
489+
return nil, errutil.UserError{Err: "Unable to read CA public key for " + b.cachedCAConfig.caKeyAlias}
490+
}
466491

467492
var caSigner pkcs11client.HsmSigner
468493
caSigner.KeyConfig.Label = b.cachedCAConfig.caKeyAlias
494+
caSigner.KeyConfig.Type = b.cachedCAConfig.caKeyType
495+
caSigner.KeyConfig.KeyBits = b.cachedCAConfig.caKeySize
469496
caSigner.Pkcs11Client = &b.pkcs11client
470-
// caSigner.PublicKey = publicKey
497+
caSigner.PublicKey = publicKey
471498

472-
newCert, err := x509.CreateCertificate(rand.Reader, cert, signingBundle.Certificate, cert.PublicKey, signingBundle.PrivateKey)
499+
newCert, err := x509.CreateCertificate(rand.Reader, cert, signingBundle.Certificate, cert.PublicKey, caSigner) //signingBundle.PrivateKey)
473500
if err != nil {
474501
return nil, errwrap.Wrapf("error signing self-issued certificate: {{err}}", err)
475502
}
@@ -488,7 +515,7 @@ func (b *HsmPkiBackend) pathCASignIntermediate(ctx context.Context, req *logical
488515
},
489516
}, nil
490517
}
491-
*/
518+
492519
const pathGenerateRootHelpSyn = `
493520
Generate a new CA certificate and private key used for signing.
494521
`

0 commit comments

Comments
 (0)