-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add HSM support via PKCS11 dlopen
This requires dynamic library linking of Libc, hence, not compatible with our static releases. But, it was fairly simple to implement, so can be interesting for some users. Signed-off-by: Volodymyr Khoroz <[email protected]>
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build !bashpki && cgopki | ||
|
||
package x509 | ||
|
||
import ( | ||
"crypto" | ||
"crypto/elliptic" | ||
"fmt" | ||
|
||
"github.com/ThalesIgnite/crypto11" | ||
|
||
"github.com/foundriesio/fioctl/subcommands" | ||
) | ||
|
||
const hsmObjectId = "1" | ||
|
||
func newPkcs11Session(hsm HsmInfo) *crypto11.Context { | ||
cfg := crypto11.Config{ | ||
Path: hsm.Module, | ||
TokenLabel: hsm.TokenLabel, | ||
Pin: hsm.Pin, | ||
MaxSessions: 0, | ||
} | ||
|
||
ctx, err := crypto11.Configure(&cfg) | ||
subcommands.DieNotNil(err) | ||
return ctx | ||
} | ||
|
||
func genAndSaveKeyToHsm(hsm HsmInfo, id, label string) crypto.Signer { | ||
// See storage_pkcs11_tool.go why we need to first check for the key existance. | ||
ctx := newPkcs11Session(hsm) | ||
key, err := ctx.FindKeyPair([]byte(id), []byte(label)) | ||
subcommands.DieNotNil(err) | ||
if key != nil { | ||
subcommands.DieNotNil(fmt.Errorf("Key %s already exists on the HSM device", label)) | ||
} | ||
|
||
key, err = ctx.GenerateECDSAKeyPairWithLabel([]byte(id), []byte(label), elliptic.P256()) | ||
subcommands.DieNotNil(err) | ||
return key | ||
} | ||
|
||
func loadKeyFromHsm(hsm HsmInfo, id, label string) crypto.Signer { | ||
ctx := newPkcs11Session(hsm) | ||
key, err := ctx.FindKeyPair([]byte(id), []byte(label)) | ||
subcommands.DieNotNil(err) | ||
if key == nil { | ||
subcommands.DieNotNil(fmt.Errorf("Key %s not found on the HSM device", label)) | ||
} | ||
return key | ||
} | ||
|
||
func (s *hsmStorage) genAndSaveKey() crypto.Signer { | ||
return genAndSaveKeyToHsm(s.HsmInfo, hsmObjectId, s.Label) | ||
} | ||
|
||
func (s *hsmStorage) loadKey() crypto.Signer { | ||
return loadKeyFromHsm(s.HsmInfo, hsmObjectId, s.Label) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !bashpki | ||
//go:build !bashpki && !cgopki | ||
|
||
package x509 | ||
|
||
|