Blame SOURCES/pr2127.patch

2b939e
# HG changeset patch
2b939e
# User andrew
2b939e
# Date 1453866306 0
2b939e
#      Wed Jan 27 03:45:06 2016 +0000
2b939e
# Node ID 0ff7720931e8dbf7de25720bdc93b18527ab89e8
2b939e
# Parent  48c15869ecd568263249af4b9a4e98d4e57f9a8f
2b939e
PR2127: SunEC provider crashes when built using system NSS
2b939e
Summary: Use NSS memory management functions
2b939e
2b939e
diff -r 48c15869ecd5 -r 0ff7720931e8 src/share/native/sun/security/ec/ECC_JNI.cpp
2b939e
--- openjdk/jdk/src/share/native/sun/security/ec/ECC_JNI.cpp	Wed Jan 27 02:54:06 2016 +0000
2b939e
+++ openjdk/jdk/src/share/native/sun/security/ec/ECC_JNI.cpp	Wed Jan 27 03:45:06 2016 +0000
2b939e
@@ -32,6 +32,13 @@
2b939e
 #define INVALID_PARAMETER_EXCEPTION \
2b939e
         "java/security/InvalidParameterException"
2b939e
 #define KEY_EXCEPTION   "java/security/KeyException"
2b939e
+#define INTERNAL_ERROR "java/lang/InternalError"
2b939e
+
2b939e
+#ifdef SYSTEM_NSS
2b939e
+#define SYSTEM_UNUSED(x) UNUSED(x)
2b939e
+#else
2b939e
+#define SYSTEM_UNUSED(x) x
2b939e
+#endif
2b939e
 
2b939e
 extern "C" {
2b939e
 
2b939e
@@ -49,8 +56,13 @@
2b939e
 /*
2b939e
  * Deep free of the ECParams struct
2b939e
  */
2b939e
-void FreeECParams(ECParams *ecparams, jboolean freeStruct)
2b939e
+void FreeECParams(ECParams *ecparams, jboolean SYSTEM_UNUSED(freeStruct))
2b939e
 {
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    // Needs to be freed using the matching method to the one
2b939e
+    // that allocated it. PR_TRUE means the memory is zeroed.
2b939e
+    PORT_FreeArena(ecparams->arena, PR_TRUE);
2b939e
+#else
2b939e
     // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
2b939e
     // Use B_TRUE to free both
2b939e
 
2b939e
@@ -64,6 +76,7 @@
2b939e
     SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
2b939e
     if (freeStruct)
2b939e
         free(ecparams);
2b939e
+#endif
2b939e
 }
2b939e
 
2b939e
 jbyteArray getEncodedBytes(JNIEnv *env, SECItem *hSECItem)
2b939e
@@ -108,6 +121,13 @@
2b939e
         goto cleanup;
2b939e
     }
2b939e
 
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Init() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+	goto cleanup;
2b939e
+    }
2b939e
+#endif
2b939e
+
2b939e
     // Fill a new ECParams using the supplied OID
2b939e
     if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
2b939e
         /* bad curve OID */
2b939e
@@ -163,16 +183,26 @@
2b939e
         if (params_item.data) {
2b939e
             env->ReleaseByteArrayElements(encodedParams,
2b939e
                 (jbyte *) params_item.data, JNI_ABORT);
2b939e
+#ifdef SYSTEM_NSS
2b939e
+	    if (SECOID_Shutdown() != SECSuccess) {
2b939e
+		ThrowException(env, INTERNAL_ERROR);
2b939e
+	    }
2b939e
+#endif
2b939e
         }
2b939e
         if (ecparams) {
2b939e
             FreeECParams(ecparams, true);
2b939e
         }
2b939e
         if (privKey) {
2b939e
             FreeECParams(&privKey->ecParams, false);
2b939e
+#ifndef SYSTEM_NSS
2b939e
+	    // The entire ECPrivateKey is allocated in the arena
2b939e
+	    // when using system NSS, so only the in-tree version
2b939e
+	    // needs to clear these manually.
2b939e
             SECITEM_FreeItem(&privKey->version, B_FALSE);
2b939e
             SECITEM_FreeItem(&privKey->privateValue, B_FALSE);
2b939e
             SECITEM_FreeItem(&privKey->publicValue, B_FALSE);
2b939e
             free(privKey);
2b939e
+#endif
2b939e
         }
2b939e
 
2b939e
         if (pSeedBuffer) {
2b939e
@@ -223,6 +253,13 @@
2b939e
         goto cleanup;
2b939e
     }
2b939e
 
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Init() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+	goto cleanup;
2b939e
+    }
2b939e
+#endif
2b939e
+
2b939e
     // Fill a new ECParams using the supplied OID
2b939e
     if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
2b939e
         /* bad curve OID */
2b939e
@@ -270,6 +307,11 @@
2b939e
         if (params_item.data) {
2b939e
             env->ReleaseByteArrayElements(encodedParams,
2b939e
                 (jbyte *) params_item.data, JNI_ABORT);
2b939e
+#ifdef SYSTEM_NSS
2b939e
+	    if (SECOID_Shutdown() != SECSuccess) {
2b939e
+		ThrowException(env, INTERNAL_ERROR);
2b939e
+	    }
2b939e
+#endif
2b939e
         }
2b939e
         if (privKey.privateValue.data) {
2b939e
             env->ReleaseByteArrayElements(privateKey,
2b939e
@@ -336,6 +378,13 @@
2b939e
         goto cleanup;
2b939e
     }
2b939e
 
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Init() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+	goto cleanup;
2b939e
+    }
2b939e
+#endif
2b939e
+
2b939e
     // Fill a new ECParams using the supplied OID
2b939e
     if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
2b939e
         /* bad curve OID */
2b939e
@@ -356,9 +405,15 @@
2b939e
 
2b939e
 cleanup:
2b939e
     {
2b939e
-        if (params_item.data)
2b939e
+        if (params_item.data) {
2b939e
             env->ReleaseByteArrayElements(encodedParams,
2b939e
                 (jbyte *) params_item.data, JNI_ABORT);
2b939e
+#ifdef SYSTEM_NSS
2b939e
+	    if (SECOID_Shutdown() != SECSuccess) {
2b939e
+		ThrowException(env, INTERNAL_ERROR);
2b939e
+	    }
2b939e
+#endif
2b939e
+	}
2b939e
 
2b939e
         if (pubKey.publicValue.data)
2b939e
             env->ReleaseByteArrayElements(publicKey,
2b939e
@@ -419,6 +474,13 @@
2b939e
         goto cleanup;
2b939e
     }
2b939e
 
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Init() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+	goto cleanup;
2b939e
+    }
2b939e
+#endif
2b939e
+
2b939e
     // Fill a new ECParams using the supplied OID
2b939e
     if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
2b939e
         /* bad curve OID */
2b939e
@@ -460,9 +522,15 @@
2b939e
             env->ReleaseByteArrayElements(publicKey,
2b939e
                 (jbyte *) publicValue_item.data, JNI_ABORT);
2b939e
 
2b939e
-        if (params_item.data)
2b939e
+        if (params_item.data) {
2b939e
             env->ReleaseByteArrayElements(encodedParams,
2b939e
                 (jbyte *) params_item.data, JNI_ABORT);
2b939e
+#ifdef SYSTEM_NSS
2b939e
+	    if (SECOID_Shutdown() != SECSuccess) {
2b939e
+		ThrowException(env, INTERNAL_ERROR);
2b939e
+	    }
2b939e
+#endif
2b939e
+	}
2b939e
 
2b939e
         if (ecparams)
2b939e
             FreeECParams(ecparams, true);