Skip to content

Commit e363607

Browse files
MeroviusFiloSottile
authored andcommitted
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 <[email protected]>
1 parent f7f5466 commit e363607

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

openpgp/packet/private_key.go

+7
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ func NewECDSAPrivateKey(currentTime time.Time, priv *ecdsa.PrivateKey) *PrivateK
6868
// implements RSA or ECDSA.
6969
func NewSignerPrivateKey(currentTime time.Time, signer crypto.Signer) *PrivateKey {
7070
pk := new(PrivateKey)
71+
// In general, the public Keys should be used as pointers. We still
72+
// type-switch on the values, for backwards-compatibility.
7173
switch pubkey := signer.Public().(type) {
74+
case *rsa.PublicKey:
75+
pk.PublicKey = *NewRSAPublicKey(currentTime, pubkey)
76+
pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
7277
case rsa.PublicKey:
7378
pk.PublicKey = *NewRSAPublicKey(currentTime, &pubkey)
7479
pk.PubKeyAlgo = PubKeyAlgoRSASignOnly
80+
case *ecdsa.PublicKey:
81+
pk.PublicKey = *NewECDSAPublicKey(currentTime, pubkey)
7582
case ecdsa.PublicKey:
7683
pk.PublicKey = *NewECDSAPublicKey(currentTime, &pubkey)
7784
default:

openpgp/packet/private_key_test.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"crypto/x509"
1515
"encoding/hex"
1616
"hash"
17-
"io"
1817
"testing"
1918
"time"
2019
)
@@ -162,15 +161,7 @@ func TestECDSAPrivateKey(t *testing.T) {
162161
}
163162

164163
type rsaSigner struct {
165-
priv *rsa.PrivateKey
166-
}
167-
168-
func (s *rsaSigner) Public() crypto.PublicKey {
169-
return s.priv.PublicKey
170-
}
171-
172-
func (s *rsaSigner) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
173-
return s.priv.Sign(rand, msg, opts)
164+
*rsa.PrivateKey
174165
}
175166

176167
func TestRSASignerPrivateKey(t *testing.T) {
@@ -208,15 +199,7 @@ func TestRSASignerPrivateKey(t *testing.T) {
208199
}
209200

210201
type ecdsaSigner struct {
211-
priv *ecdsa.PrivateKey
212-
}
213-
214-
func (s *ecdsaSigner) Public() crypto.PublicKey {
215-
return s.priv.PublicKey
216-
}
217-
218-
func (s *ecdsaSigner) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
219-
return s.priv.Sign(rand, msg, opts)
202+
*ecdsa.PrivateKey
220203
}
221204

222205
func TestECDSASignerPrivateKey(t *testing.T) {

0 commit comments

Comments
 (0)