@@ -2,10 +2,16 @@ package hsmpki
22
33import (
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
111117func (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+
492519const pathGenerateRootHelpSyn = `
493520Generate a new CA certificate and private key used for signing.
494521`
0 commit comments