2020-05-20 00:35:36 +00:00
|
|
|
package awskms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rsa"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/kms"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/cli/crypto/pemutil"
|
|
|
|
)
|
|
|
|
|
2020-05-21 00:04:01 +00:00
|
|
|
// Signer implements a crypto.Signer using the AWS KMS.
|
2020-05-20 00:35:36 +00:00
|
|
|
type Signer struct {
|
2020-05-20 19:43:14 +00:00
|
|
|
service KeyManagementClient
|
2020-05-20 00:35:36 +00:00
|
|
|
keyID string
|
|
|
|
publicKey crypto.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSigner creates a new signer using a key in the AWS KMS.
|
2020-05-20 19:43:14 +00:00
|
|
|
func NewSigner(svc KeyManagementClient, signingKey string) (*Signer, error) {
|
2020-05-20 00:35:36 +00:00
|
|
|
keyID, err := parseKeyID(signingKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the key exists.
|
|
|
|
signer := &Signer{
|
|
|
|
service: svc,
|
|
|
|
keyID: keyID,
|
|
|
|
}
|
|
|
|
if err := signer.preloadKey(keyID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return signer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Signer) preloadKey(keyID string) error {
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := s.service.GetPublicKeyWithContext(ctx, &kms.GetPublicKeyInput{
|
|
|
|
KeyId: &keyID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "awskms GetPublicKeyWithContext failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.publicKey, err = pemutil.ParseDER(resp.PublicKey)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public returns the public key of this signer or an error.
|
|
|
|
func (s *Signer) Public() crypto.PublicKey {
|
|
|
|
return s.publicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs digest with the private key stored in the AWS KMS.
|
|
|
|
func (s *Signer) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
|
|
|
|
alg, err := getSigningAlgorithm(s.Public(), opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &kms.SignInput{
|
|
|
|
KeyId: &s.keyID,
|
|
|
|
SigningAlgorithm: &alg,
|
|
|
|
Message: digest,
|
|
|
|
}
|
|
|
|
req.SetMessageType("DIGEST")
|
|
|
|
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := s.service.SignWithContext(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "awsKMS SignWithContext failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSigningAlgorithm(key crypto.PublicKey, opts crypto.SignerOpts) (string, error) {
|
|
|
|
switch key.(type) {
|
|
|
|
case *rsa.PublicKey:
|
|
|
|
_, isPSS := opts.(*rsa.PSSOptions)
|
|
|
|
switch h := opts.HashFunc(); h {
|
|
|
|
case crypto.SHA256:
|
|
|
|
if isPSS {
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPssSha256, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
}
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPkcs1V15Sha256, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
case crypto.SHA384:
|
|
|
|
if isPSS {
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPssSha384, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
}
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPkcs1V15Sha384, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
case crypto.SHA512:
|
|
|
|
if isPSS {
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPssSha512, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
}
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecRsassaPkcs1V15Sha512, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
default:
|
|
|
|
return "", errors.Errorf("unsupported hash function %v", h)
|
|
|
|
}
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
switch h := opts.HashFunc(); h {
|
|
|
|
case crypto.SHA256:
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecEcdsaSha256, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
case crypto.SHA384:
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecEcdsaSha384, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
case crypto.SHA512:
|
2020-05-21 00:04:01 +00:00
|
|
|
return kms.SigningAlgorithmSpecEcdsaSha512, nil
|
2020-05-20 00:35:36 +00:00
|
|
|
default:
|
|
|
|
return "", errors.Errorf("unsupported hash function %v", h)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", errors.Errorf("unsupported key type %T", key)
|
|
|
|
}
|
|
|
|
}
|