mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

david_page
17.18.2007 9b13d8dc3715b89a1159f3d646e0c824bcf708a7
opendj-sdk/opends/src/server/org/opends/server/types/CryptoManager.java
@@ -1442,7 +1442,7 @@
     *
     * @param cryptoManager  The CryptoManager instance.
     *
     * @param keyIDBytes  The key identifier.
     * @param keyIDString  The key identifier.
     *
     * @param transformation  The cipher transformation for which the
     * key entry was produced.
@@ -1468,18 +1468,18 @@
     */
    public static CipherKeyEntry importCipherKeyEntry(
            final CryptoManager cryptoManager,
            final byte[] keyIDBytes,
            final String keyIDString,
            final String transformation,
            final String keyAlgorithm,
            final byte[] key,
            final int ivLengthBits,
            final boolean isCompromised)
            throws CryptoManagerException {
      Validator.ensureNotNull(keyIDBytes, transformation,
      Validator.ensureNotNull(keyIDString, transformation,
              keyAlgorithm, key);
      Validator.ensureTrue(0 <= ivLengthBits);
      final KeyEntryID keyID = new KeyEntryID(keyIDBytes);
      final KeyEntryID keyID = new KeyEntryID(keyIDString);
      // Check map for existing key entry with the supplied keyID.
      CipherKeyEntry keyEntry = getKeyEntry(cryptoManager, keyID);
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/CryptoManagerTestCase.java
@@ -43,6 +43,7 @@
import java.util.List;
import java.util.LinkedList;
import java.util.Arrays;
import java.lang.reflect.Method;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
@@ -144,6 +145,7 @@
    // default (preferred) AES/CBC/PKCS5Padding 128bit key.
    paramList.add(new CipherParameters(null, null, null, 128, 128));
    // custom
//    paramList.add(new CipherParameters("Blowfish", "CFB", "NoPadding", 192, 64));
    paramList.add(new CipherParameters("Blowfish", "CFB", "NoPadding", 128, 64));
    paramList.add(new CipherParameters("RC4", null, null, 104, 0));
    paramList.add(new CipherParameters("DES", "CFB", "NoPadding", 56, 56));
@@ -220,4 +222,31 @@
    is.close();
    assertEquals(new String(plainText), secretMessage);
  }
  /**
   Tests to ensure the same key identifier (and hence, key) is used for
   successive encryptions specifying the same algorithm and key length.
   @throws Exception  In case an error occurs in the encryption routine.
   */
  @Test
  public void testKeyEntryReuse()
          throws Exception {
    final CryptoManager cm = DirectoryServer.getCryptoManager();
    final String secretMessage = "1234";
    try {
      Method m = Arrays.class.getMethod("copyOfRange", (new byte[16]).getClass(),
              Integer.TYPE, Integer.TYPE);
      final byte[] cipherText = cm.encrypt(secretMessage.getBytes());
      final byte[] keyID = (byte[])m.invoke(null, cipherText, 0, 16);
      final byte[] cipherText2 = cm.encrypt(secretMessage.getBytes());
      final byte[] keyID2 = (byte[])m.invoke(null, cipherText2, 0, 16);
      assertTrue(Arrays.equals(keyID, keyID2));
    }
    catch (NoSuchMethodException ex) {
      // ignore - requires Java 6
    }
  }
}