From 0822ab47c5257481e39fd7fd7757246052672cce Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Tue, 16 Jul 2013 14:12:20 +0000
Subject: [PATCH] CR-2005 Fix OPENDJ-1036 Cleanup passwords in memory?

---
 opendj-sdk/opends/src/server/org/opends/server/extensions/MD5PasswordStorageScheme.java |   30 +++++++++++++++++++++++++-----
 1 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/extensions/MD5PasswordStorageScheme.java b/opendj-sdk/opends/src/server/org/opends/server/extensions/MD5PasswordStorageScheme.java
index aae5497..0f37d1e 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/extensions/MD5PasswordStorageScheme.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/extensions/MD5PasswordStorageScheme.java
@@ -23,12 +23,14 @@
  *
  *
  *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Portions Copyright 2013 ForgeRock AS
  */
 package org.opends.server.extensions;
 
 
 
 import java.security.MessageDigest;
+import java.util.Arrays;
 
 import org.opends.messages.Message;
 import org.opends.server.admin.std.server.MD5PasswordStorageSchemeCfg;
@@ -141,13 +143,14 @@
          throws DirectoryException
   {
     byte[] digestBytes;
+    byte[] plaintextBytes = null;
 
     synchronized (digestLock)
     {
       try
       {
         // TODO: Can we avoid this copy?
-        byte[] plaintextBytes = plaintext.toByteArray();
+        plaintextBytes = plaintext.toByteArray();
         digestBytes = messageDigest.digest(plaintextBytes);
       }
       catch (Exception e)
@@ -162,6 +165,11 @@
         throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
                                      message, e);
       }
+      finally
+      {
+        if (plaintextBytes != null)
+          Arrays.fill(plaintextBytes, (byte) 0);
+      }
     }
 
     return ByteString.valueOf(Base64.encode(digestBytes));
@@ -181,14 +189,15 @@
     buffer.append(STORAGE_SCHEME_NAME_MD5);
     buffer.append('}');
 
-    // TODO: Can we avoid this copy?
-    byte[] plaintextBytes = plaintext.toByteArray();
+    byte[] plaintextBytes = null;
     byte[] digestBytes;
 
     synchronized (digestLock)
     {
       try
       {
+        // TODO: Can we avoid this copy?
+        plaintextBytes = plaintext.toByteArray();
         digestBytes = messageDigest.digest(plaintextBytes);
       }
       catch (Exception e)
@@ -203,6 +212,11 @@
         throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
                                      message, e);
       }
+      finally
+      {
+        if (plaintextBytes != null)
+          Arrays.fill(plaintextBytes, (byte) 0);
+      }
     }
 
     buffer.append(Base64.encode(digestBytes));
@@ -220,14 +234,15 @@
   public boolean passwordMatches(ByteSequence plaintextPassword,
                                  ByteSequence storedPassword)
   {
-    // TODO: Can we avoid this copy?
-    byte[] plaintextPasswordBytes = plaintextPassword.toByteArray();
+    byte[] plaintextPasswordBytes = null;
     ByteString userPWDigestBytes;
 
     synchronized (digestLock)
     {
       try
       {
+        // TODO: Can we avoid this copy?
+        plaintextPasswordBytes = plaintextPassword.toByteArray();
         userPWDigestBytes =
             ByteString.wrap(messageDigest.digest(plaintextPasswordBytes));
       }
@@ -240,6 +255,11 @@
 
         return false;
       }
+      finally
+      {
+        if (plaintextPasswordBytes != null)
+          Arrays.fill(plaintextPasswordBytes, (byte) 0);
+      }
     }
 
     ByteString storedPWDigestBytes;

--
Gitblit v1.10.0