8181670: Improve implementation of keystores
authorvinnie
Tue, 05 Sep 2017 15:53:33 +0100
changeset 48574 96bff87ea130
parent 48573 e6b173e04545
child 48575 2ce508de5c77
8181670: Improve implementation of keystores Reviewed-by: mullan
src/java.base/macosx/native/libosxsecurity/KeystoreImpl.m
--- a/src/java.base/macosx/native/libosxsecurity/KeystoreImpl.m	Mon Sep 04 19:33:44 2017 +0100
+++ b/src/java.base/macosx/native/libosxsecurity/KeystoreImpl.m	Tue Sep 05 15:53:33 2017 +0100
@@ -439,6 +439,11 @@
                 goto errOut;
             }
             passwordStrRef = CFStringCreateWithCharacters(kCFAllocatorDefault, passwordChars, passwordLen);
+
+            // clear the password and release
+            memset(passwordChars, 0, passwordLen);
+            (*env)->ReleaseCharArrayElements(env, passwordObj, passwordChars,
+                JNI_ABORT);
         }
     }
 
@@ -527,8 +532,19 @@
 
     if (passwordObj) {
         passwordLen = (*env)->GetArrayLength(env, passwordObj);
-        passwordChars = (*env)->GetCharArrayElements(env, passwordObj, NULL);
-        passwordStrRef = CFStringCreateWithCharacters(kCFAllocatorDefault, passwordChars, passwordLen);
+
+        if (passwordLen > 0) {
+            passwordChars = (*env)->GetCharArrayElements(env, passwordObj, NULL);
+            if (passwordChars == NULL) {
+                goto errOut;
+            }
+            passwordStrRef = CFStringCreateWithCharacters(kCFAllocatorDefault, passwordChars, passwordLen);
+
+            // clear the password and release
+            memset(passwordChars, 0, passwordLen);
+            (*env)->ReleaseCharArrayElements(env, passwordObj, passwordChars,
+                JNI_ABORT);
+        }
     }
 
     paramBlock.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;