From 9b13d8dc3715b89a1159f3d646e0c824bcf708a7 Mon Sep 17 00:00:00 2001
From: david_page <david_page@localhost>
Date: Mon, 17 Sep 2007 21:18:20 +0000
Subject: [PATCH] 1. Fix API type error. 2. Add testcase to check that cached key is reused for successive encryptions that request the same algorithm and key length (instead of generating another key).

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/CryptoManagerTestCase.java |   29 +++++++++++++++++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/types/CryptoManager.java                                 |    8 ++++----
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/types/CryptoManager.java b/opendj-sdk/opends/src/server/org/opends/server/types/CryptoManager.java
index 14b3e40..5df13f2 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/types/CryptoManager.java
+++ b/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);
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/CryptoManagerTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/CryptoManagerTestCase.java
index ef5b5d7..c139d10 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/CryptoManagerTestCase.java
+++ b/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
+    }
+  }
 }

--
Gitblit v1.10.0