certificates/kms/cloudkms/signer.go

87 lines
2 KiB
Go
Raw Normal View History

2020-01-10 02:41:13 +00:00
package cloudkms
import (
"crypto"
"io"
"github.com/pkg/errors"
2020-08-14 22:33:50 +00:00
"go.step.sm/crypto/pemutil"
2020-01-10 02:41:13 +00:00
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
// Signer implements a crypto.Signer using Google's Cloud KMS.
type Signer struct {
client KeyManagementClient
2020-01-10 02:41:13 +00:00
signingKey string
publicKey crypto.PublicKey
2020-01-10 02:41:13 +00:00
}
// NewSigner creates a new crypto.Signer the given CloudKMS signing key.
func NewSigner(c KeyManagementClient, signingKey string) (*Signer, error) {
// Make sure that the key exists.
signer := &Signer{
2020-01-10 02:41:13 +00:00
client: c,
signingKey: signingKey,
}
if err := signer.preloadKey(signingKey); err != nil {
return nil, err
}
return signer, nil
2020-01-10 02:41:13 +00:00
}
func (s *Signer) preloadKey(signingKey string) error {
2020-01-10 02:41:13 +00:00
ctx, cancel := defaultContext()
defer cancel()
response, err := s.client.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{
Name: signingKey,
2020-01-10 02:41:13 +00:00
})
if err != nil {
return errors.Wrap(err, "cloudKMS GetPublicKey failed")
}
s.publicKey, err = pemutil.ParseKey([]byte(response.Pem))
return err
}
2020-01-10 02:41:13 +00:00
// Public returns the public key of this signer or an error.
func (s *Signer) Public() crypto.PublicKey {
return s.publicKey
2020-01-10 02:41:13 +00:00
}
// Sign signs digest with the private key stored in Google's Cloud KMS.
func (s *Signer) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
2020-01-10 02:41:13 +00:00
req := &kmspb.AsymmetricSignRequest{
Name: s.signingKey,
Digest: &kmspb.Digest{},
}
switch h := opts.HashFunc(); h {
case crypto.SHA256:
req.Digest.Digest = &kmspb.Digest_Sha256{
Sha256: digest,
}
case crypto.SHA384:
req.Digest.Digest = &kmspb.Digest_Sha384{
Sha384: digest,
}
case crypto.SHA512:
req.Digest.Digest = &kmspb.Digest_Sha512{
Sha512: digest,
}
default:
return nil, errors.Errorf("unsupported hash function %v", h)
}
ctx, cancel := defaultContext()
defer cancel()
response, err := s.client.AsymmetricSign(ctx, req)
if err != nil {
return nil, errors.Wrap(err, "cloudKMS AsymmetricSign failed")
}
return response.Signature, nil
}