Skip to content

Commit ddd6bc1

Browse files
authored
Merge pull request #457 from smallstep/pkcs11
Add support for PKCS #11 KMS.
2 parents c1a2697 + e446e22 commit ddd6bc1

24 files changed

+2402
-21
lines changed

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ AWSKMS_BINNAME?=step-awskms-init
66
AWSKMS_PKG?=github.com/smallstep/certificates/cmd/step-awskms-init
77
YUBIKEY_BINNAME?=step-yubikey-init
88
YUBIKEY_PKG?=github.com/smallstep/certificates/cmd/step-yubikey-init
9+
PKCS11_BINNAME?=step-pkcs11-init
10+
PKCS11_PKG?=github.com/smallstep/certificates/cmd/step-pkcs11-init
911

1012
# Set V to 1 for verbose output from the Makefile
1113
Q=$(if $V,,@)
@@ -76,7 +78,7 @@ GOFLAGS := CGO_ENABLED=0
7678
download:
7779
$Q go mod download
7880

79-
build: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME) $(PREFIX)bin/$(YUBIKEY_BINNAME)
81+
build: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME) $(PREFIX)bin/$(YUBIKEY_BINNAME) $(PREFIX)bin/$(PKCS11_BINNAME)
8082
@echo "Build Complete!"
8183

8284
$(PREFIX)bin/$(BINNAME): download $(call rwildcard,*.go)
@@ -95,6 +97,10 @@ $(PREFIX)bin/$(YUBIKEY_BINNAME): download $(call rwildcard,*.go)
9597
$Q mkdir -p $(@D)
9698
$Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(YUBIKEY_BINNAME) $(LDFLAGS) $(YUBIKEY_PKG)
9799

100+
$(PREFIX)bin/$(PKCS11_BINNAME): download $(call rwildcard,*.go)
101+
$Q mkdir -p $(@D)
102+
$Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(PKCS11_BINNAME) $(LDFLAGS) $(PKCS11_PKG)
103+
98104
# Target to force a build of step-ca without running tests
99105
simple: build
100106

@@ -113,7 +119,7 @@ generate:
113119
# Test
114120
#########################################
115121
test:
116-
$Q $(GOFLAGS) go test -short -coverprofile=coverage.out ./...
122+
$Q go test -short -coverprofile=coverage.out ./...
117123

118124
.PHONY: test
119125

@@ -171,6 +177,9 @@ endif
171177
ifneq ($(YUBIKEY_BINNAME),"")
172178
$Q rm -f bin/$(YUBIKEY_BINNAME)
173179
endif
180+
ifneq ($(PKCS11_BINNAME),"")
181+
$Q rm -f bin/$(PKCS11_BINNAME)
182+
endif
174183

175184
.PHONY: clean
176185

authority/authority.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,10 @@ func (a *Authority) Shutdown() error {
382382
}
383383
return a.db.Shutdown()
384384
}
385+
386+
// CloseForReload closes internal services, to allow a safe reload.
387+
func (a *Authority) CloseForReload() {
388+
if err := a.keyManager.Close(); err != nil {
389+
log.Printf("error closing the key manager: %v", err)
390+
}
391+
}

authority/authority_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,17 @@ func TestNewEmbedded_GetTLSCertificate(t *testing.T) {
306306
assert.True(t, cert.Leaf.IPAddresses[0].Equal(net.ParseIP("127.0.0.1")))
307307
assert.True(t, cert.Leaf.IPAddresses[1].Equal(net.ParseIP("::1")))
308308
}
309+
310+
func TestAuthority_CloseForReload(t *testing.T) {
311+
tests := []struct {
312+
name string
313+
auth *Authority
314+
}{
315+
{"ok", testAuthority(t)},
316+
}
317+
for _, tt := range tests {
318+
t.Run(tt.name, func(t *testing.T) {
319+
tt.auth.CloseForReload()
320+
})
321+
}
322+
}

ca/ca.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,11 @@ func (ca *CA) Reload() error {
227227
}
228228

229229
// 1. Stop previous renewer
230-
// 2. Replace ca properties
230+
// 2. Safely shutdown any internal resources (e.g. key manager)
231+
// 3. Replace ca properties
231232
// Do not replace ca.srv
232233
ca.renewer.Stop()
234+
ca.auth.CloseForReload()
233235
ca.auth = newCA.auth
234236
ca.config = newCA.config
235237
ca.opts = newCA.opts

cmd/step-ca/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
_ "github.com/smallstep/certificates/kms/sshagentkms"
3232

3333
// Experimental kms interfaces.
34+
_ "github.com/smallstep/certificates/kms/pkcs11"
3435
_ "github.com/smallstep/certificates/kms/yubikey"
3536

3637
// Enabled cas interfaces.

cmd/step-cloudkms-init/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func createSSH(c *cloudkms.CloudKMS, project, location, keyRing string, protecti
234234
resp, err = c.CreateKey(&apiv1.CreateKeyRequest{
235235
Name: parent + "/ssh-host-key",
236236
SignatureAlgorithm: apiv1.ECDSAWithSHA256,
237-
ProtectionLevel: apiv1.Software,
237+
ProtectionLevel: protectionLevel,
238238
})
239239
if err != nil {
240240
return err

0 commit comments

Comments
 (0)