Skip to content

Commit

Permalink
openpgp: allow RSA/ECDSA signers to return a pointer
Browse files Browse the repository at this point in the history
Fixes golang/go#27606

Change-Id: I88b2f7c7796b43449a17a6be963c05f741dbf904
Reviewed-on: https://go-review.googlesource.com/137895
Reviewed-by: Filippo Valsorda <filippo@golang.org>
  • Loading branch information
Merovius authored and FiloSottile committed Oct 1, 2018
1 parent 5d5af2a commit 3e5366a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
7 changes: 7 additions & 0 deletions openpgp/packet/private_key.go
Expand Up @@ -68,10 +68,17 @@ func NewECDSAPrivateKey(currentTime time.Time, priv *ecdsa.PrivateKey) *PrivateK
// 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:
Expand Down
21 changes: 2 additions & 19 deletions openpgp/packet/private_key_test.go
Expand Up @@ -14,7 +14,6 @@ import (
"crypto/x509"
"encoding/hex"
"hash"
"io"
"testing"
"time"
)
Expand Down Expand Up @@ -162,15 +161,7 @@ func TestECDSAPrivateKey(t *testing.T) {
}

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) {
Expand Down Expand Up @@ -208,15 +199,7 @@ func TestRSASignerPrivateKey(t *testing.T) {
}

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) {
Expand Down

0 comments on commit 3e5366a

Please sign in to comment.