Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Commit a3d8ed7

Browse files
authored
Merge pull request ProtonMail#139 from T4cC0re/master
Disregard GNU dummy subkeys when checking (un)locked state and unlocking a crypto.Key
2 parents a5fa9e2 + abf7e6f commit a3d8ed7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

crypto/key.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (key *Key) Unlock(passphrase []byte) (*Key, error) {
156156
}
157157

158158
for _, sub := range unlockedKey.entity.Subkeys {
159-
if sub.PrivateKey != nil {
159+
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() {
160160
if err := sub.PrivateKey.Decrypt(passphrase); err != nil {
161161
return nil, errors.Wrap(err, "gopenpgp: error in unlocking sub key")
162162
}
@@ -280,13 +280,19 @@ func (key *Key) IsLocked() (bool, error) {
280280
return true, errors.New("gopenpgp: a public key cannot be locked")
281281
}
282282

283+
encryptedKeys := 0
284+
283285
for _, sub := range key.entity.Subkeys {
284-
if sub.PrivateKey != nil && !sub.PrivateKey.Encrypted {
285-
return false, nil
286+
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() && sub.PrivateKey.Encrypted {
287+
encryptedKeys++
286288
}
287289
}
288290

289-
return key.entity.PrivateKey.Encrypted, nil
291+
if key.entity.PrivateKey.Encrypted {
292+
encryptedKeys++
293+
}
294+
295+
return encryptedKeys > 0, nil
290296
}
291297

292298
// IsUnlocked checks if a private key is unlocked.
@@ -295,13 +301,19 @@ func (key *Key) IsUnlocked() (bool, error) {
295301
return true, errors.New("gopenpgp: a public key cannot be unlocked")
296302
}
297303

304+
encryptedKeys := 0
305+
298306
for _, sub := range key.entity.Subkeys {
299-
if sub.PrivateKey != nil && sub.PrivateKey.Encrypted {
300-
return false, nil
307+
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() && sub.PrivateKey.Encrypted {
308+
encryptedKeys++
301309
}
302310
}
303311

304-
return !key.entity.PrivateKey.Encrypted, nil
312+
if key.entity.PrivateKey.Encrypted {
313+
encryptedKeys++
314+
}
315+
316+
return encryptedKeys == 0, nil
305317
}
306318

307319
// Check verifies if the public keys match the private key parameters by

0 commit comments

Comments
 (0)