|
| 1 | +package org.bouncycastle.jce.provider.test; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.EOFException; |
| 5 | +import java.io.IOException; |
| 6 | +import java.security.KeyStore; |
| 7 | +import java.security.Security; |
| 8 | + |
| 9 | +import org.bouncycastle.jcajce.provider.keystore.util.AdaptingKeyStoreSpi; |
| 10 | +import org.bouncycastle.jcajce.provider.keystore.util.JKSKeyStoreSpi; |
| 11 | +import org.bouncycastle.jcajce.util.BCJcaJceHelper; |
| 12 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 13 | +import org.bouncycastle.util.Arrays; |
| 14 | +import org.bouncycastle.util.test.SimpleTest; |
| 15 | + |
| 16 | +public class JKSStoreTest |
| 17 | + extends SimpleTest |
| 18 | +{ |
| 19 | + public String getName() |
| 20 | + { |
| 21 | + return "JKSStore"; |
| 22 | + } |
| 23 | + |
| 24 | + public void performTest() |
| 25 | + throws Exception |
| 26 | + { |
| 27 | + testLoadTruncatedStore(); |
| 28 | + testLoadTruncatedStoreThroughKeyStore(); |
| 29 | + } |
| 30 | + |
| 31 | + private void testLoadTruncatedStore() |
| 32 | + throws Exception |
| 33 | + { |
| 34 | + // shorter than the checksum alone, and shorter than the header plus the checksum |
| 35 | + int[] lengths = new int[]{ 0, 4, 19, 31 }; |
| 36 | + |
| 37 | + for (int i = 0; i != lengths.length; i++) |
| 38 | + { |
| 39 | + byte[] store = Arrays.copyOfRange(PKCS12StoreTest.JKS_Store, 0, lengths[i]); |
| 40 | + |
| 41 | + implLoadTruncatedStore(store, null); |
| 42 | + implLoadTruncatedStore(store, PKCS12StoreTest.JKS_TEST_PWD); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void implLoadTruncatedStore(byte[] store, char[] password) |
| 47 | + throws Exception |
| 48 | + { |
| 49 | + JKSKeyStoreSpi jksStore = new JKSKeyStoreSpi(new BCJcaJceHelper()); |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + jksStore.engineLoad(new ByteArrayInputStream(store), password); |
| 54 | + |
| 55 | + fail("truncated store of " + store.length + " bytes accepted"); |
| 56 | + } |
| 57 | + catch (EOFException e) |
| 58 | + { |
| 59 | + // expected |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * The JKS store is reached through the compatibility probe in AdaptingKeyStoreSpi, so a |
| 65 | + * truncated store has to leave KeyStore.load() by way of its declared IOException rather |
| 66 | + * than an ArrayIndexOutOfBoundsException (github #2451). |
| 67 | + */ |
| 68 | + private void testLoadTruncatedStoreThroughKeyStore() |
| 69 | + throws Exception |
| 70 | + { |
| 71 | + String compatValue = System.getProperty(AdaptingKeyStoreSpi.COMPAT_OVERRIDE); |
| 72 | + |
| 73 | + System.setProperty(AdaptingKeyStoreSpi.COMPAT_OVERRIDE, "true"); |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + int[] lengths = new int[]{ 0, 4, 19, 31 }; |
| 78 | + |
| 79 | + for (int i = 0; i != lengths.length; i++) |
| 80 | + { |
| 81 | + byte[] store = Arrays.copyOfRange(PKCS12StoreTest.JKS_Store, 0, lengths[i]); |
| 82 | + |
| 83 | + KeyStore ks = KeyStore.getInstance("PKCS12", "BC"); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + ks.load(new ByteArrayInputStream(store), PKCS12StoreTest.JKS_TEST_PWD); |
| 88 | + |
| 89 | + fail("truncated store of " + store.length + " bytes accepted"); |
| 90 | + } |
| 91 | + catch (IOException e) |
| 92 | + { |
| 93 | + // expected |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + if (compatValue == null) |
| 100 | + { |
| 101 | + System.getProperties().remove(AdaptingKeyStoreSpi.COMPAT_OVERRIDE); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + System.setProperty(AdaptingKeyStoreSpi.COMPAT_OVERRIDE, compatValue); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static void main( |
| 111 | + String[] args) |
| 112 | + { |
| 113 | + Security.addProvider(new BouncyCastleProvider()); |
| 114 | + |
| 115 | + runTest(new JKSStoreTest()); |
| 116 | + } |
| 117 | +} |
0 commit comments