-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: isolate HSM interactions and crypto11 use in a more generic…
… interface (#993) The main driver behind this is to enable us to add a `GCPHSM` with its own MakeKey implementation to avoid cluttering up `Configuration.MakeKey` further. While I was doing this, I moved the other HSM and `crypto11`` related things elsewhere as well. Notably, two things are still present in `signer.Configuration` that would be part of a `signer.HSM` implementation in an ideal world: * The pub key casting in `Configuration.GetKeys` * `Configuration.CheckHSMConnection` entirely Both of these things depend on `Configuration.GetPrivateKey` which needs a `label` of a key to fetch. I don't think it's appropriate to move that label to the `HSM` interface (because it would tie an instance to a single key). I couldn't find a way to adjust the usage of these methods that didn't end up making things messier, or involved refactoring other things, so I'm punting on it for now.
- Loading branch information
Showing
3 changed files
with
91 additions
and
44 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package signer | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/rsa" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/miekg/pkcs11" | ||
"github.com/mozilla-services/autograph/crypto11" | ||
) | ||
|
||
type HSM interface { | ||
GetPrivateKey(label []byte) (crypto.PrivateKey, error) | ||
MakeKey(keyTpl interface{}, keyName string) (crypto.PrivateKey, crypto.PublicKey, error) | ||
GetRand() io.Reader | ||
} | ||
|
||
type GenericHSM struct { | ||
ctx *pkcs11.Ctx | ||
} | ||
|
||
// GetPrivateKey locates the keypair given by `label` in the HSM. | ||
func (hsm *GenericHSM) GetPrivateKey(label []byte) (crypto.PrivateKey, error) { | ||
return crypto11.FindKeyPair(nil, label) | ||
} | ||
|
||
func (hsm *GenericHSM) GetRand() io.Reader { | ||
return new(crypto11.PKCS11RandReader) | ||
} | ||
|
||
type AWSHSM struct { | ||
GenericHSM | ||
} | ||
|
||
// MakeKey generates a new keypair of type `keyTpl` and returns the new key structs. | ||
func (hsm *AWSHSM) MakeKey(keyTpl interface{}, keyName string) (crypto.PrivateKey, crypto.PublicKey, error) { | ||
var slots []uint | ||
slots, err := hsm.ctx.GetSlotList(true) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to list PKCS#11 Slots: %w", err) | ||
} | ||
if len(slots) < 1 { | ||
return nil, nil, fmt.Errorf("failed to find a usable slot in hsm context") | ||
} | ||
keyNameBytes := []byte(keyName) | ||
switch keyTplType := keyTpl.(type) { | ||
case *ecdsa.PublicKey: | ||
priv, err := crypto11.GenerateECDSAKeyPairOnSlot(slots[0], keyNameBytes, keyNameBytes, keyTplType) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to generate ecdsa key in hsm: %w", err) | ||
} | ||
pub := priv.PubKey.(*ecdsa.PublicKey) | ||
return priv, pub, nil | ||
case *rsa.PublicKey: | ||
keySize := keyTplType.Size() | ||
priv, err := crypto11.GenerateRSAKeyPairOnSlot(slots[0], keyNameBytes, keyNameBytes, keySize) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to generate rsa key in hsm: %w", err) | ||
} | ||
pub := priv.PubKey.(*rsa.PublicKey) | ||
return priv, pub, nil | ||
default: | ||
return nil, nil, fmt.Errorf("making key of type %T is not supported", keyTpl) | ||
} | ||
} | ||
|
||
func NewAWSHSM(ctx *pkcs11.Ctx) *AWSHSM { | ||
return &AWSHSM{ | ||
GenericHSM{ | ||
ctx, | ||
}, | ||
} | ||
} |
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