crypto/openpgp: return error from userIdSignatureHash
Signature#SignUserId was ignoring the error returned by userIdSignatureHash. This error can happen, for example, when the Signature's Hash property is uninitialized. Not returning the error makes it hard for the user to detect the problem. Change-Id: I0ae0033e77bfb1ea1c06b0769f949e48c713adc6 Reviewed-on: https://go-review.googlesource.com/27997 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
9e590154d2
commit
6318898455
@@ -553,7 +553,7 @@ func (sig *Signature) Sign(h hash.Hash, priv *PrivateKey, config *Config) (err e
|
||||
func (sig *Signature) SignUserId(id string, pub *PublicKey, priv *PrivateKey, config *Config) error {
|
||||
h, err := userIdSignatureHash(id, pub, sig.Hash)
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
return sig.Sign(h, priv, config)
|
||||
}
|
||||
|
||||
@@ -39,4 +39,40 @@ func TestSignatureReserialize(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignUserId(t *testing.T) {
|
||||
sig := &Signature{
|
||||
SigType: SigTypeGenericCert,
|
||||
PubKeyAlgo: PubKeyAlgoRSA,
|
||||
Hash: 0, // invalid hash function
|
||||
}
|
||||
|
||||
packet, err := Read(readerFromHex(rsaPkDataHex))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to deserialize public key: %v", err)
|
||||
}
|
||||
pubKey := packet.(*PublicKey)
|
||||
|
||||
packet, err = Read(readerFromHex(privKeyRSAHex))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to deserialize private key: %v", err)
|
||||
}
|
||||
privKey := packet.(*PrivateKey)
|
||||
|
||||
err = sig.SignUserId("", pubKey, privKey, nil)
|
||||
if err == nil {
|
||||
t.Errorf("did not receive an error when expected")
|
||||
}
|
||||
|
||||
sig.Hash = crypto.SHA256
|
||||
err = privKey.Decrypt([]byte("testing"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to decrypt private key: %v", err)
|
||||
}
|
||||
|
||||
err = sig.SignUserId("", pubKey, privKey, nil)
|
||||
if err != nil {
|
||||
t.Errorf("failed to sign user id: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
const signatureDataHex = "c2c05c04000102000605024cb45112000a0910ab105c91af38fb158f8d07ff5596ea368c5efe015bed6e78348c0f033c931d5f2ce5db54ce7f2a7e4b4ad64db758d65a7a71773edeab7ba2a9e0908e6a94a1175edd86c1d843279f045b021a6971a72702fcbd650efc393c5474d5b59a15f96d2eaad4c4c426797e0dcca2803ef41c6ff234d403eec38f31d610c344c06f2401c262f0993b2e66cad8a81ebc4322c723e0d4ba09fe917e8777658307ad8329adacba821420741009dfe87f007759f0982275d028a392c6ed983a0d846f890b36148c7358bdb8a516007fac760261ecd06076813831a36d0459075d1befa245ae7f7fb103d92ca759e9498fe60ef8078a39a3beda510deea251ea9f0a7f0df6ef42060f20780360686f3e400e"
|
||||
|
||||
Reference in New Issue
Block a user