Skip to content

Commit 0f33f19

Browse files
committed
Merge branch 'fixStrippedKeyUnlockNPE' of https://github.com/pgpainless/bc-java into pgpainless-fixStrippedKeyUnlockNPE
2 parents 63d00e4 + ed2bcc4 commit 0f33f19

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ private void sanitizeProtectionMode()
404404

405405
/**
406406
* Return true if the provided passphrase is correct.
407+
* Note: This method will always return false for stripped secret keys.
407408
*
408409
* @param passphrase passphrase
409410
* @return true if the passphrase is correct
@@ -418,7 +419,7 @@ public boolean isPassphraseCorrect(char[] passphrase)
418419
try
419420
{
420421
OpenPGPPrivateKey privateKey = unlock(passphrase);
421-
return privateKey.unlockedKey != null;
422+
return privateKey != null && privateKey.unlockedKey != null;
422423
}
423424
catch (PGPException e)
424425
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.bouncycastle.openpgp.api.test;
2+
3+
import org.bouncycastle.openpgp.PGPException;
4+
import org.bouncycastle.openpgp.api.OpenPGPApi;
5+
import org.bouncycastle.openpgp.api.OpenPGPKey;
6+
import org.bouncycastle.openpgp.api.OpenPGPKeyReader;
7+
8+
import java.io.IOException;
9+
10+
public class StrippedOpenPGPKeyTest
11+
extends APITest
12+
{
13+
14+
@Override
15+
protected void performTestWith(OpenPGPApi api)
16+
throws PGPException, IOException
17+
{
18+
// Adapted test case from https://github.com/bcgit/bc-java/issues/2173
19+
// Credit for test vectors to @agrahn
20+
OpenPGPKeyReader reader = api.readKeyOrCertificate();
21+
OpenPGPKey strippedKey = reader.parseKey(
22+
"-----BEGIN PGP PRIVATE KEY BLOCK-----\n\n" +
23+
"lDsEaNz9VhYJKwYBBAHaRw8BAQdANvkQp6G9vVPUtxHplmw44lclTAm2vSqREnfi\n" +
24+
"bsqmDDP/AGUAR05VAbQfQm9iIFVzZXIgPGJvYi51c2VyQGV4YW1wbGUub3JnPoiT\n" +
25+
"BBMWCgA7FiEE81kLNGDerGMA7okHMcFP0Qqg/SwFAmjc/VYCGwEFCwkIBwICIgIG\n" +
26+
"FQoJCAsCBBYCAwECHgcCF4AACgkQMcFP0Qqg/Szv3AEA5Q0S6UrHI6YC9IqCV86Z\n" +
27+
"xF7zegeUJiTGfbIMmp+7qk4BAIJBZyfpsutfdnLBmXMQmPPvdlfNZ0H781sm4vq4\n" +
28+
"1KkFnIsEaNz9pRIKKwYBBAGXVQEFAQEHQLilfhrcbzI6XI7a+HbOfqNj/9cwZk8s\n" +
29+
"O4H/4IMhY7ZZAwEIB/4HAwIpPDPOpRpcw//ZZTsMuT5ZRDGnSA+3i34NWnhv50ex\n" +
30+
"yf51MgrvY+E3NaE9ObFfvEJILF8kub206yaQRbHWPrj7fU1C+DKJ9AbDcXZmzu/U\n" +
31+
"iHgEGBYKACAWIQTzWQs0YN6sYwDuiQcxwU/RCqD9LAUCaNz9pQIbDAAKCRAxwU/R\n" +
32+
"CqD9LCNSAP9v7GminBOFV8XkMsL4T+0P0woGjTZxUrYKKVR98NhXswEAhDfkQh0n\n" +
33+
"IyhOyHwzLuoGJ31M7a1rtB44tcJNtnP6XQQ=\n" +
34+
"=jquc\n" +
35+
"-----END PGP PRIVATE KEY BLOCK-----\n");
36+
37+
OpenPGPKey.OpenPGPSecretKey secKey = strippedKey.getPrimarySecretKey();
38+
39+
boolean isCorrect = secKey.isPassphraseCorrect(("12345678").toCharArray());
40+
isFalse("Expected false when checking passphrase of stripped secret key", isCorrect);
41+
}
42+
43+
@Override
44+
public String getName()
45+
{
46+
return "StrippedOpenPGPKeyTest";
47+
}
48+
49+
public static void main(String[] args)
50+
{
51+
runTest(new StrippedOpenPGPKeyTest());
52+
}
53+
}

0 commit comments

Comments
 (0)