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>
This commit is contained in:
Axel Wagner
2018-10-01 20:31:47 +00:00
committed by Filippo Valsorda
parent f7f546618e
commit e3636079e1
2 changed files with 9 additions and 19 deletions
+7
View File
@@ -68,10 +68,17 @@ func NewECDSAPrivateKey(currentTime time.Time, priv *ecdsa.PrivateKey) *PrivateK
// implements RSA or ECDSA. // implements RSA or ECDSA.
func NewSignerPrivateKey(currentTime time.Time, signer crypto.Signer) *PrivateKey { func NewSignerPrivateKey(currentTime time.Time, signer crypto.Signer) *PrivateKey {
pk := new(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) { switch pubkey := signer.Public().(type) {
case *rsa.PublicKey:
pk.PublicKey = *NewRSAPublicKey(currentTime, pubkey)
pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
case rsa.PublicKey: case rsa.PublicKey:
pk.PublicKey = *NewRSAPublicKey(currentTime, &pubkey) pk.PublicKey = *NewRSAPublicKey(currentTime, &pubkey)
pk.PubKeyAlgo = PubKeyAlgoRSASignOnly pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
case *ecdsa.PublicKey:
pk.PublicKey = *NewECDSAPublicKey(currentTime, pubkey)
case ecdsa.PublicKey: case ecdsa.PublicKey:
pk.PublicKey = *NewECDSAPublicKey(currentTime, &pubkey) pk.PublicKey = *NewECDSAPublicKey(currentTime, &pubkey)
default: default:
+2 -19
View File
@@ -14,7 +14,6 @@ import (
"crypto/x509" "crypto/x509"
"encoding/hex" "encoding/hex"
"hash" "hash"
"io"
"testing" "testing"
"time" "time"
) )
@@ -162,15 +161,7 @@ func TestECDSAPrivateKey(t *testing.T) {
} }
type rsaSigner struct { type rsaSigner struct {
priv *rsa.PrivateKey *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)
} }
func TestRSASignerPrivateKey(t *testing.T) { func TestRSASignerPrivateKey(t *testing.T) {
@@ -208,15 +199,7 @@ func TestRSASignerPrivateKey(t *testing.T) {
} }
type ecdsaSigner struct { type ecdsaSigner struct {
priv *ecdsa.PrivateKey *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)
} }
func TestECDSASignerPrivateKey(t *testing.T) { func TestECDSASignerPrivateKey(t *testing.T) {