go.crypto/openpgp: don't corrupt encrypted private key when decryption fails.
Since the decryption was done in place, if an incorrect key was given, the encrypted data would be corrupted. Fixes golang/go#8439. R=bradfitz CC=golang-codereviews https://golang.org/cl/115550043
This commit is contained in:
@@ -196,8 +196,8 @@ func (pk *PrivateKey) Decrypt(passphrase []byte) error {
|
|||||||
block := pk.cipher.new(key)
|
block := pk.cipher.new(key)
|
||||||
cfb := cipher.NewCFBDecrypter(block, pk.iv)
|
cfb := cipher.NewCFBDecrypter(block, pk.iv)
|
||||||
|
|
||||||
data := pk.encryptedData
|
data := make([]byte, len(pk.encryptedData))
|
||||||
cfb.XORKeyStream(data, data)
|
cfb.XORKeyStream(data, pk.encryptedData)
|
||||||
|
|
||||||
if pk.sha1Checksum {
|
if pk.sha1Checksum {
|
||||||
if len(data) < sha1.Size {
|
if len(data) < sha1.Size {
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ func TestPrivateKeyRead(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = privKey.Decrypt([]byte("wrong password"))
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("#%d: decrypted with incorrect key", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
err = privKey.Decrypt([]byte("testing"))
|
err = privKey.Decrypt([]byte("testing"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("#%d: failed to decrypt: %s", i, err)
|
t.Errorf("#%d: failed to decrypt: %s", i, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user