Skip to content

Commit da2e764

Browse files
committed
added UnrecoverableKeyException cause for "wrong password" on PKCS12 KeyStore - relates to github #2082
1 parent e5d15d8 commit da2e764

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

prov/src/main/java/org/bouncycastle/jcajce/provider/keystore/pkcs12/PKCS12KeyStoreSpi.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
import org.bouncycastle.jce.provider.JDKPKCS12StoreParameter;
124124
import org.bouncycastle.util.Arrays;
125125
import org.bouncycastle.util.BigIntegers;
126+
import org.bouncycastle.util.Exceptions;
126127
import org.bouncycastle.util.Integers;
127128
import org.bouncycastle.util.Properties;
128129
import org.bouncycastle.util.Strings;
@@ -945,15 +946,16 @@ public void engineLoad(
945946
{
946947
if (password.length > 0)
947948
{
948-
throw new IOException("PKCS12 key store mac invalid - wrong password or corrupted file.");
949+
throw Exceptions.ioException("PKCS12 key store mac invalid - wrong password or corrupted file",
950+
new UnrecoverableKeyException("PKCS12 key store mac invalid"));
949951
}
950952

951953
// Try with incorrect zero length password
952954
res = calculatePbeMac(macAlgorithm.getAlgorithm(), salt, itCount, password, true, data);
953955

954956
if (!Arrays.constantTimeAreEqual(res, dig))
955957
{
956-
throw new IOException("PKCS12 key store mac invalid - wrong password or corrupted file.");
958+
throw Exceptions.ioException("PKCS12 key store mac invalid - wrong password or corrupted file", new UnrecoverableKeyException("PKCS12 key store mac invalid"));
957959
}
958960

959961
wrongPKCS12Zero = true;

prov/src/test/java/org/bouncycastle/jce/provider/test/PKCS12StoreTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.security.PublicKey;
1414
import java.security.Security;
1515
import java.security.Signature;
16+
import java.security.UnrecoverableKeyException;
1617
import java.security.cert.Certificate;
1718
import java.security.cert.X509Certificate;
1819
import java.security.interfaces.RSAPrivateKey;
@@ -2361,6 +2362,26 @@ private void testNoExtraLocalKeyID(byte[] store1data)
23612362
}
23622363
}
23632364

2365+
public void testPKCS12StoreWrongPassword()
2366+
throws Exception
2367+
{
2368+
KeyStore store = KeyStore.getInstance("PKCS12", BC);
2369+
ByteArrayInputStream stream = new ByteArrayInputStream(pkcs12);
2370+
2371+
try
2372+
{
2373+
store.load(stream, "Goodbye World!".toCharArray());
2374+
fail("no exception");
2375+
}
2376+
catch (IOException e)
2377+
{ e.printStackTrace();
2378+
if (!(e.getCause() instanceof UnrecoverableKeyException))
2379+
{
2380+
fail("no exception cause found for wrong password");
2381+
}
2382+
}
2383+
}
2384+
23642385
private void testChainCycle()
23652386
throws Exception
23662387
{
@@ -2551,7 +2572,7 @@ private void testPBMac1PBKdf2()
25512572
}
25522573
catch (IOException e)
25532574
{
2554-
isTrue(e.getMessage().contains("PKCS12 key store mac invalid - wrong password or corrupted file."));
2575+
isTrue(e.getMessage().contains("PKCS12 key store mac invalid - wrong password or corrupted file"));
25552576
}
25562577
}
25572578
// invalid test vector that throws exception
@@ -2764,6 +2785,7 @@ public void performTest()
27642785
testRawKeyBagStore();
27652786
testAES256_AES128();
27662787
testAES256GCM_AES128_GCM();
2788+
// testPKCS12StoreWrongPassword();
27672789

27682790
// converter tests
27692791

0 commit comments

Comments
 (0)