openpgp: allow RSA/ECDSA signers to return a pointer
Fixes golang/go#27606
Change-Id: I88b2f7c7796b43449a17a6be963c05f741dbf904
Reviewed-on: https://go-review.googlesource.com/137895
Reviewed-by: Filippo Valsorda <filippo@golang.org>
diff --git a/openpgp/packet/private_key.go b/openpgp/packet/private_key.go
index 34734cc..87fc461 100644
--- a/openpgp/packet/private_key.go
+++ b/openpgp/packet/private_key.go
@@ -68,10 +68,17 @@
// implements RSA or ECDSA.
func NewSignerPrivateKey(currentTime time.Time, signer crypto.Signer) *PrivateKey {
pk := new(PrivateKey)
+ // In general, the public Keys should be used as pointers. We still
+ // type-switch on the values, for backwards-compatibility.
switch pubkey := signer.Public().(type) {
+ case *rsa.PublicKey:
+ pk.PublicKey = *NewRSAPublicKey(currentTime, pubkey)
+ pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
case rsa.PublicKey:
pk.PublicKey = *NewRSAPublicKey(currentTime, &pubkey)
pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
+ case *ecdsa.PublicKey:
+ pk.PublicKey = *NewECDSAPublicKey(currentTime, pubkey)
case ecdsa.PublicKey:
pk.PublicKey = *NewECDSAPublicKey(currentTime, &pubkey)
default:
diff --git a/openpgp/packet/private_key_test.go b/openpgp/packet/private_key_test.go
index ac651d9..794d746 100644
--- a/openpgp/packet/private_key_test.go
+++ b/openpgp/packet/private_key_test.go
@@ -14,7 +14,6 @@
"crypto/x509"
"encoding/hex"
"hash"
- "io"
"testing"
"time"
)
@@ -162,15 +161,7 @@
}
type rsaSigner struct {
- priv *rsa.PrivateKey
-}
-
-func (s *rsaSigner) Public() crypto.PublicKey {
- return s.priv.PublicKey
-}
-
-func (s *rsaSigner) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
- return s.priv.Sign(rand, msg, opts)
+ *rsa.PrivateKey
}
func TestRSASignerPrivateKey(t *testing.T) {
@@ -208,15 +199,7 @@
}
type ecdsaSigner struct {
- priv *ecdsa.PrivateKey
-}
-
-func (s *ecdsaSigner) Public() crypto.PublicKey {
- return s.priv.PublicKey
-}
-
-func (s *ecdsaSigner) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
- return s.priv.Sign(rand, msg, opts)
+ *ecdsa.PrivateKey
}
func TestECDSASignerPrivateKey(t *testing.T) {