Blame SOURCES/pr1983-rh1565658-support_using_the_system_installation_of_nss_with_the_sunec_provider_jdk11.patch

660215
diff --git a/make/autoconf/jdk-options.m4 b/make/autoconf/jdk-options.m4
660215
--- a/make/autoconf/jdk-options.m4
660215
+++ b/make/autoconf/jdk-options.m4
660215
@@ -267,9 +267,10 @@
660215
 #
660215
 AC_DEFUN_ONCE([JDKOPT_DETECT_INTREE_EC],
660215
 [
660215
+  AC_REQUIRE([LIB_SETUP_MISC_LIBS])
660215
   AC_MSG_CHECKING([if elliptic curve crypto implementation is present])
660215
 
660215
-  if test -d "${TOPDIR}/src/jdk.crypto.ec/share/native/libsunec/impl"; then
660215
+  if test "x${system_nss}" = "xyes" -o -d "${TOPDIR}/src/jdk.crypto.ec/share/native/libsunec/impl"; then
660215
     ENABLE_INTREE_EC=true
660215
     AC_MSG_RESULT([yes])
660215
   else
660215
diff --git a/make/autoconf/libraries.m4 b/make/autoconf/libraries.m4
660215
--- a/make/autoconf/libraries.m4
660215
+++ b/make/autoconf/libraries.m4
660215
@@ -178,6 +178,48 @@
660215
   AC_SUBST(LIBDL)
660215
   LIBS="$save_LIBS"
660215
 
660215
+  ###############################################################################
660215
+  #
660215
+  # Check for the NSS libraries
660215
+  #
660215
+
660215
+  AC_MSG_CHECKING([whether to build the Sun EC provider against the system NSS libraries])
660215
+
660215
+  # default is bundled
660215
+  DEFAULT_SYSTEM_NSS=no
660215
+
660215
+  AC_ARG_ENABLE([system-nss], [AS_HELP_STRING([--enable-system-nss],
660215
+     [build the SunEC provider using the system NSS libraries @<:@disabled@:>@])],
660215
+  [
660215
+    case "${enableval}" in
660215
+      yes)
660215
+        system_nss=yes
660215
+        ;;
660215
+      *)
660215
+        system_nss=no
660215
+        ;;
660215
+    esac
660215
+  ],
660215
+  [
660215
+    system_nss=${DEFAULT_SYSTEM_NSS}
660215
+  ])
660215
+  AC_MSG_RESULT([$system_nss])
660215
+
660215
+  if test "x${system_nss}" = "xyes"; then
660215
+      PKG_CHECK_MODULES(NSS_SOFTTKN, nss-softokn >= 3.16.1, [NSS_SOFTOKN_FOUND=yes], [NSS_SOFTOKN_FOUND=no])
660215
+      PKG_CHECK_MODULES(NSS, nss >= 3.16.1, [NSS_FOUND=yes], [NSS_FOUND=no])
660215
+      if test "x${NSS_SOFTOKN_FOUND}" = "xyes" -a "x${NSS_FOUND}" = "xyes"; then
660215
+          NSS_LIBS="$NSS_SOFTOKN_LIBS $NSS_LIBS -lfreebl";
660215
+	  USE_EXTERNAL_NSS=true
660215
+      else
660215
+	  AC_MSG_ERROR([--enable-system-nss specified, but NSS not found.])
660215
+      fi
660215
+  else
660215
+      USE_EXTERNAL_NSS=false
660215
+  fi
660215
+  AC_SUBST(USE_EXTERNAL_NSS)
660215
+
660215
+
660215
   # Deprecated libraries, keep the flags for backwards compatibility
660215
   if test "x$OPENJDK_TARGET_OS" = "xwindows"; then
660215
     BASIC_DEPRECATED_ARG_WITH([dxsdk])
660215
diff --git a/make/autoconf/spec.gmk.in b/make/autoconf/spec.gmk.in
660215
--- a/make/autoconf/spec.gmk.in
660215
+++ b/make/autoconf/spec.gmk.in
660215
@@ -795,6 +795,10 @@
660215
 # Libraries
660215
 #
660215
 
660215
+USE_EXTERNAL_NSS:=@USE_EXTERNAL_NSS@
660215
+NSS_LIBS:=@NSS_LIBS@
660215
+NSS_CFLAGS:=@NSS_CFLAGS@
660215
+
660215
 USE_EXTERNAL_LCMS:=@USE_EXTERNAL_LCMS@
660215
 LCMS_CFLAGS:=@LCMS_CFLAGS@
660215
 LCMS_LIBS:=@LCMS_LIBS@
660215
diff --git a/make/lib/Lib-jdk.crypto.ec.gmk b/make/lib/Lib-jdk.crypto.ec.gmk
660215
--- a/make/lib/Lib-jdk.crypto.ec.gmk
660215
+++ b/make/lib/Lib-jdk.crypto.ec.gmk
660215
@@ -38,6 +38,11 @@
660215
     BUILD_LIBSUNEC_CXXFLAGS_JDKLIB := $(CXXFLAGS_JDKLIB)
660215
   endif
660215
 
660215
+  ifeq ($(USE_EXTERNAL_NSS), true)
660215
+    BUILD_LIBSUNEC_CFLAGS_JDKLIB += $(NSS_CFLAGS) -DSYSTEM_NSS -DNSS_ENABLE_ECC
660215
+    BUILD_LIBSUNEC_CXXFLAGS_JDKLIB += $(NSS_CFLAGS) -DSYSTEM_NSS -DNSS_ENABLE_ECC
660215
+  endif
660215
+
660215
   $(eval $(call SetupJdkLibrary, BUILD_LIBSUNEC, \
660215
       NAME := sunec, \
660215
       TOOLCHAIN := TOOLCHAIN_LINK_CXX, \
660215
@@ -47,9 +52,11 @@
660215
       CXXFLAGS := $(BUILD_LIBSUNEC_CXXFLAGS_JDKLIB), \
660215
       DISABLED_WARNINGS_gcc := sign-compare implicit-fallthrough, \
660215
       DISABLED_WARNINGS_microsoft := 4101 4244 4146 4018, \
660215
-      LDFLAGS := $(LDFLAGS_JDKLIB) $(LDFLAGS_CXX_JDK), \
660215
+      LDFLAGS := $(subst -Xlinker --as-needed,, \
660215
+                 $(subst -Wl$(COMMA)--as-needed,, $(LDFLAGS_JDKLIB))) $(LDFLAGS_CXX_JDK), \
660215
       LDFLAGS_macosx := $(call SET_SHARED_LIBRARY_ORIGIN), \
660215
       LIBS := $(LIBCXX), \
660215
+      LIBS_linux := -lc $(NSS_LIBS), \
660215
   ))
660215
 
660215
   TARGETS += $(BUILD_LIBSUNEC)
660215
diff --git a/src/java.base/unix/native/include/jni_md.h b/src/java.base/unix/native/include/jni_md.h
660215
--- a/src/java.base/unix/native/include/jni_md.h
660215
+++ b/src/java.base/unix/native/include/jni_md.h
660215
@@ -41,6 +41,11 @@
660215
   #define JNIEXPORT
660215
   #define JNIIMPORT
660215
 #endif
660215
+#if (defined(__GNUC__)) || __has_attribute(unused)
660215
+  #define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
660215
+#else
660215
+  #define UNUSED(x) UNUSED_ ## x
660215
+#endif
660215
 
660215
 #define JNICALL
660215
 
660215
diff --git a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
660215
--- a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
660215
+++ b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
660215
@@ -61,6 +61,7 @@
660215
             AccessController.doPrivileged(new PrivilegedAction<Void>() {
660215
                 public Void run() {
660215
                     System.loadLibrary("sunec"); // check for native library
660215
+                    initialize();
660215
                     return null;
660215
                 }
660215
             });
660215
@@ -293,6 +294,11 @@
660215
             "ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));
660215
     }
660215
 
660215
+    /**
660215
+     * Initialize the native code.
660215
+     */
660215
+    private static native void initialize();
660215
+
660215
     private void putXDHEntries() {
660215
 
660215
         HashMap<String, String> ATTRS = new HashMap<>(1);
660215
diff --git a/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp b/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
660215
--- a/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
660215
+++ b/src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
660215
@@ -25,7 +25,11 @@
660215
 
660215
 #include <jni.h>
660215
 #include "jni_util.h"
660215
+#ifdef SYSTEM_NSS
660215
+#include "ecc_impl.h"
660215
+#else
660215
 #include "impl/ecc_impl.h"
660215
+#endif
660215
 #include "sun_security_ec_ECDHKeyAgreement.h"
660215
 #include "sun_security_ec_ECKeyPairGenerator.h"
660215
 #include "sun_security_ec_ECDSASignature.h"
660215
@@ -33,6 +37,13 @@
660215
 #define INVALID_PARAMETER_EXCEPTION \
660215
         "java/security/InvalidParameterException"
660215
 #define KEY_EXCEPTION   "java/security/KeyException"
660215
+#define INTERNAL_ERROR "java/lang/InternalError"
660215
+
660215
+#ifdef SYSTEM_NSS
660215
+#define SYSTEM_UNUSED(x) UNUSED(x)
660215
+#else
660215
+#define SYSTEM_UNUSED(x) x
660215
+#endif
660215
 
660215
 extern "C" {
660215
 
660215
@@ -55,8 +66,13 @@
660215
 /*
660215
  * Deep free of the ECParams struct
660215
  */
660215
-void FreeECParams(ECParams *ecparams, jboolean freeStruct)
660215
+void FreeECParams(ECParams *ecparams, jboolean SYSTEM_UNUSED(freeStruct))
660215
 {
660215
+#ifdef SYSTEM_NSS
660215
+    // Needs to be freed using the matching method to the one
660215
+    // that allocated it. PR_TRUE means the memory is zeroed.
660215
+    PORT_FreeArena(ecparams->arena, PR_TRUE);
660215
+#else
660215
     // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
660215
     // Use B_TRUE to free both
660215
 
660215
@@ -70,6 +86,7 @@
660215
     SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
660215
     if (freeStruct)
660215
         free(ecparams);
660215
+#endif
660215
 }
660215
 
660215
 jbyteArray getEncodedBytes(JNIEnv *env, SECItem *hSECItem)
660215
@@ -139,7 +156,7 @@
660215
  */
660215
 JNIEXPORT jobjectArray
660215
 JNICALL Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
660215
-  (JNIEnv *env, jclass clazz, jint keySize, jbyteArray encodedParams, jbyteArray seed)
660215
+  (JNIEnv *env, jclass UNUSED(clazz), jint UNUSED(keySize), jbyteArray encodedParams, jbyteArray seed)
660215
 {
660215
     ECPrivateKey *privKey = NULL; // contains both public and private values
660215
     ECParams *ecparams = NULL;
660215
@@ -171,8 +188,17 @@
660215
     env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
660215
 
660215
     // Generate the new keypair (using the supplied seed)
660215
+#ifdef SYSTEM_NSS
660215
+    if (RNG_RandomUpdate((unsigned char *) pSeedBuffer, jSeedLength)
660215
+        != SECSuccess) {
660215
+        ThrowException(env, KEY_EXCEPTION);
660215
+        goto cleanup;
660215
+    }
660215
+    if (EC_NewKey(ecparams, &privKey) != SECSuccess) {    
660215
+#else    
660215
     if (EC_NewKey(ecparams, &privKey, (unsigned char *) pSeedBuffer,
660215
         jSeedLength, 0) != SECSuccess) {
660215
+#endif
660215
         ThrowException(env, KEY_EXCEPTION);
660215
         goto cleanup;
660215
     }
660215
@@ -219,10 +245,15 @@
660215
         }
660215
         if (privKey) {
660215
             FreeECParams(&privKey->ecParams, false);
660215
+#ifndef SYSTEM_NSS
660215
+	    // The entire ECPrivateKey is allocated in the arena
660215
+	    // when using system NSS, so only the in-tree version
660215
+	    // needs to clear these manually.
660215
             SECITEM_FreeItem(&privKey->version, B_FALSE);
660215
             SECITEM_FreeItem(&privKey->privateValue, B_FALSE);
660215
             SECITEM_FreeItem(&privKey->publicValue, B_FALSE);
660215
             free(privKey);
660215
+#endif
660215
         }
660215
 
660215
         if (pSeedBuffer) {
660215
@@ -240,7 +271,7 @@
660215
  */
660215
 JNIEXPORT jbyteArray
660215
 JNICALL Java_sun_security_ec_ECDSASignature_signDigest
660215
-  (JNIEnv *env, jclass clazz, jbyteArray digest, jbyteArray privateKey, jbyteArray encodedParams, jbyteArray seed, jint timing)
660215
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray digest, jbyteArray privateKey, jbyteArray encodedParams, jbyteArray seed, jint timing)
660215
 {
660215
     jbyte* pDigestBuffer = NULL;
660215
     jint jDigestLength = env->GetArrayLength(digest);
660215
@@ -299,8 +330,18 @@
660215
     env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
660215
 
660215
     // Sign the digest (using the supplied seed)
660215
+#ifdef SYSTEM_NSS
660215
+    if (RNG_RandomUpdate((unsigned char *) pSeedBuffer, jSeedLength)
660215
+        != SECSuccess) {
660215
+        ThrowException(env, KEY_EXCEPTION);
660215
+        goto cleanup;
660215
+    }
660215
+    if (ECDSA_SignDigest(&privKey, &signature_item, &digest_item)
660215
+        != SECSuccess) {    
660215
+#else    
660215
     if (ECDSA_SignDigest(&privKey, &signature_item, &digest_item,
660215
         (unsigned char *) pSeedBuffer, jSeedLength, 0, timing) != SECSuccess) {
660215
+#endif
660215
         ThrowException(env, KEY_EXCEPTION);
660215
         goto cleanup;
660215
     }
660215
@@ -349,7 +390,7 @@
660215
  */
660215
 JNIEXPORT jboolean
660215
 JNICALL Java_sun_security_ec_ECDSASignature_verifySignedDigest
660215
-  (JNIEnv *env, jclass clazz, jbyteArray signedDigest, jbyteArray digest, jbyteArray publicKey, jbyteArray encodedParams)
660215
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray signedDigest, jbyteArray digest, jbyteArray publicKey, jbyteArray encodedParams)
660215
 {
660215
     jboolean isValid = false;
660215
 
660215
@@ -406,9 +447,10 @@
660215
 
660215
 cleanup:
660215
     {
660215
-        if (params_item.data)
660215
+        if (params_item.data) {
660215
             env->ReleaseByteArrayElements(encodedParams,
660215
                 (jbyte *) params_item.data, JNI_ABORT);
660215
+	}
660215
 
660215
         if (pubKey.publicValue.data)
660215
             env->ReleaseByteArrayElements(publicKey,
660215
@@ -434,7 +476,7 @@
660215
  */
660215
 JNIEXPORT jbyteArray
660215
 JNICALL Java_sun_security_ec_ECDHKeyAgreement_deriveKey
660215
-  (JNIEnv *env, jclass clazz, jbyteArray privateKey, jbyteArray publicKey, jbyteArray encodedParams)
660215
+  (JNIEnv *env, jclass UNUSED(clazz), jbyteArray privateKey, jbyteArray publicKey, jbyteArray encodedParams)
660215
 {
660215
     jbyteArray jSecret = NULL;
660215
     ECParams *ecparams = NULL;
660215
@@ -510,9 +552,10 @@
660215
             env->ReleaseByteArrayElements(publicKey,
660215
                 (jbyte *) publicValue_item.data, JNI_ABORT);
660215
 
660215
-        if (params_item.data)
660215
+        if (params_item.data) {
660215
             env->ReleaseByteArrayElements(encodedParams,
660215
                 (jbyte *) params_item.data, JNI_ABORT);
660215
+	}
660215
 
660215
         if (ecparams)
660215
             FreeECParams(ecparams, true);
660215
@@ -521,4 +564,28 @@
660215
     return jSecret;
660215
 }
660215
 
660215
+JNIEXPORT void
660215
+JNICALL Java_sun_security_ec_SunEC_initialize
660215
+  (JNIEnv *env, jclass UNUSED(clazz))
660215
+{
660215
+#ifdef SYSTEM_NSS
660215
+    if (SECOID_Init() != SECSuccess) {
660215
+        ThrowException(env, INTERNAL_ERROR);
660215
+    }
660215
+    if (RNG_RNGInit() != SECSuccess) {
660215
+        ThrowException(env, INTERNAL_ERROR);
660215
+    }
660215
+#endif
660215
+}
660215
+
660215
+JNIEXPORT void
660215
+JNICALL JNI_OnUnload
660215
+  (JavaVM *vm, void *reserved)
660215
+{
660215
+#ifdef SYSTEM_NSS
660215
+    RNG_RNGShutdown();
660215
+    SECOID_Shutdown();
660215
+#endif
660215
+}
660215
+
660215
 } /* extern "C" */
660215
--- a/src/jdk.crypto.ec/share/native/libsunec/ecc_impl.h	2019-01-11 00:01:25.000000000 -0500
660215
+++ b/src/jdk.crypto.ec/share/native/libsunec/ecc_impl.h	2019-01-14 03:52:54.145695946 -0500
660215
@@ -45,7 +45,19 @@
660215
 #endif
660215
 
660215
 #include <sys/types.h>
660215
+
660215
+#ifdef SYSTEM_NSS
660215
+#include <secitem.h>
660215
+#include <secerr.h>
660215
+#include <keythi.h>
660215
+#ifdef LEGACY_NSS
660215
+#include <softoken.h>
660215
+#else
660215
+#include <blapi.h>
660215
+#endif
660215
+#else
660215
 #include "ecl-exp.h"
660215
+#endif
660215
 
660215
 /*
660215
  * Multi-platform definitions
660215
@@ -96,6 +108,7 @@
660215
  * Various structures and definitions from NSS are here.
660215
  */
660215
 
660215
+#ifndef SYSTEM_NSS
660215
 #ifdef _KERNEL
660215
 #define PORT_ArenaAlloc(a, n, f)        kmem_alloc((n), (f))
660215
 #define PORT_ArenaZAlloc(a, n, f)       kmem_zalloc((n), (f))
660215
@@ -130,9 +143,12 @@
660215
 #define PORT_Memcpy(t, f, l)            memcpy((t), (f), (l))
660215
 #endif
660215
 
660215
+#endif
660215
+
660215
 #define CHECK_OK(func) if (func == NULL) goto cleanup
660215
 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
660215
 
660215
+#ifndef SYSTEM_NSS
660215
 typedef enum {
660215
         siBuffer = 0,
660215
         siClearDataBuffer = 1,
660215
@@ -229,6 +245,7 @@
660215
         SECFailure = -1,
660215
         SECSuccess = 0
660215
 } SECStatus;
660215
+#endif
660215
 
660215
 #ifdef _KERNEL
660215
 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l))
660215
@@ -237,8 +254,10 @@
660215
  This function is no longer required because the random bytes are now
660215
  supplied by the caller. Force a failure.
660215
 */
660215
+#ifndef SYSTEM_NSS
660215
 #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure
660215
 #endif
660215
+#endif
660215
 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup
660215
 #define MP_TO_SEC_ERROR(err)
660215
 
660215
@@ -248,11 +267,18 @@
660215
 extern int ecc_knzero_random_generator(uint8_t *, size_t);
660215
 extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t);
660215
 
660215
+#ifdef SYSTEM_NSS
660215
+#define EC_DecodeParams(a,b,c) EC_DecodeParams(a,b)
660215
+#define ECDSA_VerifyDigest(a,b,c,d) ECDSA_VerifyDigest(a,b,c)
660215
+#define ECDH_Derive(a,b,c,d,e,f) ECDH_Derive(a,b,c,d,e)
660215
+#else
660215
 extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int);
660215
+
660215
 extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int);
660215
 extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *,
660215
     int);
660215
 extern void SECITEM_FreeItem(SECItem *, boolean_t);
660215
+
660215
 /* This function has been modified to accept an array of random bytes */
660215
 extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
660215
     const unsigned char* random, int randomlen, int);
660215
@@ -263,9 +289,10 @@
660215
     const SECItem *, int);
660215
 extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t,
660215
     SECItem *, int);
660215
+#endif
660215
 
660215
 #ifdef  __cplusplus
660215
 }
660215
 #endif
660215
 
660215
-#endif /* _ECC_IMPL_H */
660215
+#endif /* _ECC_IMPL_H */