Skip to content

Commit

Permalink
Consistently load OpenSSL libraries among different platforms
Browse files Browse the repository at this point in the history
As part of this pull request, these changes are performed:
- Allow loading specific user-defined OpenSSL library using the
  -Djdk.native.openssl.lib option.
- OpenSSL library loading is consolidated into a single file using
  ifdefs.
- The platform-specific MD files are no longer needed and are thus
  deleted.
- Additional traces are added to indicate the attempts and actual
  OpenSSL library loaded.
- The location of the library loaded is printed.

Moreover, the order of preference for loading a library is updated to
follow this order:

1. Explicitly load what was specified via JVM property. Fail if loading
   fails.
2. Search within the Semeru directories for a bundled version.
3. Search the system for existing libraries and attempt to find the
   higher version.
4. If all of the previous steps fail, revert to original Java
   implementation for crypto.

Co-authored by: Paritosh Kumar <[email protected]>
Co-authored by: Kostas Tsiounis <[email protected]>

Signed-off-by: Kostas Tsiounis <[email protected]>
  • Loading branch information
paritkum authored and KostasTsiounis committed Dec 6, 2024
1 parent a43a394 commit d786bf9
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 402 deletions.
84 changes: 0 additions & 84 deletions closed/src/java.base/aix/native/libjncrypto/NativeCrypto_md.c

This file was deleted.

75 changes: 0 additions & 75 deletions closed/src/java.base/macosx/native/libjncrypto/NativeCrypto_md.c

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jdk.internal.ref.CleanerFactory;
import jdk.internal.reflect.Reflection;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.util.StaticProperty;

/*[IF CRIU_SUPPORT]*/
import openj9.internal.criu.InternalCRIUSupport;
Expand Down Expand Up @@ -79,24 +80,38 @@ private static final class InstanceHolder {

private final boolean isOpenSSLFIPS;

@SuppressWarnings("restricted")
private static long loadCryptoLibraries() {
long osslVersion;

try {
// load jncrypto JNI library
// Load jncrypto JNI library.
System.loadLibrary("jncrypto");
// load OpenSSL crypto library dynamically
osslVersion = loadCrypto(traceEnabled);
if (traceEnabled && (osslVersion != -1)) {
System.err.println("Native crypto library load succeeded - using native crypto library.");

// Get user-specified OpenSSL library to use, if available.
String nativeLibName = System.getProperty("jdk.native.openssl.lib", "");

// Get the JDK location.
String javaHome = StaticProperty.javaHome();

// Load OpenSSL crypto library dynamically.
osslVersion = loadCrypto(traceEnabled, nativeLibName, javaHome);
if (osslVersion != -1) {
if (traceEnabled) {
System.err.println("Native crypto library load succeeded - using native crypto library.");
}
} else {
if (!nativeLibName.isEmpty()) {
throw new RuntimeException(nativeLibName + " is not available, crypto libraries are not loaded");
}
}
} catch (UnsatisfiedLinkError usle) {
if (traceEnabled) {
System.err.println("UnsatisfiedLinkError: Failure attempting to load jncrypto JNI library");
System.err.println("Warning: Native crypto library load failed." +
" Using Java crypto implementation.");
}
// signal load failure
// Signal load failure.
osslVersion = -1;
}
return osslVersion;
Expand Down Expand Up @@ -157,8 +172,7 @@ public static final long getVersionIfAvailable() {
public static final boolean isAlgorithmEnabled(String property, String name) {
boolean useNativeAlgorithm = false;
if (useNativeCrypto) {
useNativeAlgorithm = Boolean.parseBoolean(
System.getProperty(property, "true"));
useNativeAlgorithm = Boolean.parseBoolean(System.getProperty(property, "true"));
}
/*
* User wants to use the native crypto implementation. Ensure that the native crypto library is enabled.
Expand Down Expand Up @@ -251,14 +265,18 @@ public void run() {
});
}

/* Native digest interfaces */
/* OpenSSL utility interfaces */

private static final native long loadCrypto(boolean trace);
private static final native long loadCrypto(boolean trace,
String libName,
String javaHome);

public static final native boolean isMD5Available();

private static final native boolean isOpenSSLFIPS();

/* Native digest interfaces */

public final native long DigestCreateContext(long nativeBuffer,
int algoIndex);

Expand Down
Loading

0 comments on commit d786bf9

Please sign in to comment.