Blame SOURCES/pr2815.patch

2b939e
# HG changeset patch
2b939e
# User andrew
2b939e
# Date 1453867347 0
2b939e
#      Wed Jan 27 04:02:27 2016 +0000
2b939e
# Node ID 26e2e029ee256e9815fdc324831a03d8582255e1
2b939e
# Parent  0ff7720931e8dbf7de25720bdc93b18527ab89e8
2b939e
PR2815: Race condition in SunEC provider with system NSS
2b939e
Summary: Perform initialisation and shutdown only when library is loaded or SunEC is finalized respectively
2b939e
2b939e
diff -r 0ff7720931e8 -r 26e2e029ee25 make/mapfiles/libsunec/mapfile-vers
2b939e
--- openjdk/jdk/make/mapfiles/libsunec/mapfile-vers	Wed Jan 27 03:45:06 2016 +0000
2b939e
+++ openjdk/jdk/make/mapfiles/libsunec/mapfile-vers	Wed Jan 27 04:02:27 2016 +0000
2b939e
@@ -31,6 +31,8 @@
2b939e
                 Java_sun_security_ec_ECDSASignature_signDigest;
2b939e
                 Java_sun_security_ec_ECDSASignature_verifySignedDigest;
2b939e
                 Java_sun_security_ec_ECDHKeyAgreement_deriveKey;
2b939e
+		Java_sun_security_ec_SunEC_initialize;
2b939e
+		Java_sun_security_ec_SunEC_cleanup;
2b939e
         local:
2b939e
                 *;
2b939e
 };
2b939e
diff -r 0ff7720931e8 -r 26e2e029ee25 src/share/classes/sun/security/ec/SunEC.java
2b939e
--- openjdk/jdk/src/share/classes/sun/security/ec/SunEC.java	Wed Jan 27 03:45:06 2016 +0000
2b939e
+++ openjdk/jdk/src/share/classes/sun/security/ec/SunEC.java	Wed Jan 27 04:02:27 2016 +0000
2b939e
@@ -58,6 +58,7 @@
2b939e
             AccessController.doPrivileged(new PrivilegedAction<Void>() {
2b939e
                 public Void run() {
2b939e
                     System.loadLibrary("sunec"); // check for native library
2b939e
+                    initialize();
2b939e
                     return null;
2b939e
                 }
2b939e
             });
2b939e
@@ -81,4 +82,22 @@
2b939e
         }
2b939e
     }
2b939e
 
2b939e
+    /**
2b939e
+     * Cleanup native resources during finalisation.
2b939e
+     */
2b939e
+    @Override
2b939e
+    protected void finalize() {
2b939e
+        cleanup();
2b939e
+    }
2b939e
+
2b939e
+    /**
2b939e
+     * Initialize the native code.
2b939e
+     */
2b939e
+    private static native void initialize();
2b939e
+
2b939e
+    /**
2b939e
+     * Cleanup in the native layer.
2b939e
+     */
2b939e
+    private static native void cleanup();
2b939e
+
2b939e
 }
2b939e
diff -r 0ff7720931e8 -r 26e2e029ee25 src/share/native/sun/security/ec/ECC_JNI.cpp
2b939e
--- openjdk/jdk/src/share/native/sun/security/ec/ECC_JNI.cpp	Wed Jan 27 03:45:06 2016 +0000
2b939e
+++ openjdk/jdk/src/share/native/sun/security/ec/ECC_JNI.cpp	Wed Jan 27 04:02:27 2016 +0000
2b939e
@@ -121,13 +121,6 @@
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
@@ -183,11 +176,6 @@
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
@@ -253,13 +241,6 @@
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
@@ -307,11 +288,6 @@
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
@@ -378,13 +354,6 @@
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
@@ -408,11 +377,6 @@
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
@@ -474,13 +438,6 @@
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
@@ -525,11 +482,6 @@
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
@@ -539,4 +491,26 @@
2b939e
     return jSecret;
2b939e
 }
2b939e
 
2b939e
+JNIEXPORT void
2b939e
+JNICALL Java_sun_security_ec_SunEC_initialize
2b939e
+  (JNIEnv *env, jclass UNUSED(clazz))
2b939e
+{
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Init() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+    }
2b939e
+#endif
2b939e
+}
2b939e
+
2b939e
+JNIEXPORT void
2b939e
+JNICALL Java_sun_security_ec_SunEC_cleanup
2b939e
+  (JNIEnv *env, jclass UNUSED(clazz))
2b939e
+{
2b939e
+#ifdef SYSTEM_NSS
2b939e
+    if (SECOID_Shutdown() != SECSuccess) {
2b939e
+	ThrowException(env, INTERNAL_ERROR);
2b939e
+    }
2b939e
+#endif
2b939e
+}
2b939e
+
2b939e
 } /* extern "C" */