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.openssl.libName 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 laoded.
- 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 porperty. 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 Nov 12, 2024
1 parent e251cff commit 51bbf34
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 388 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 @@ -64,12 +64,18 @@ public class NativeCrypto {

private static final Cleaner ECKeyCleaner = CleanerFactory.cleaner();

private static final String javaHome =
GetPropertyAction.privilegedGetProperty("java.home");

private static final boolean useNativeCrypto = Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("jdk.nativeCrypto", "true"));

private static final boolean traceEnabled = Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("jdk.nativeCryptoTrace", "false"));

private static String nativeLibName =
GetPropertyAction.privilegedGetProperty("jdk.native.openssl.lib");

private static final class InstanceHolder {
private static final NativeCrypto instance = new NativeCrypto();
}
Expand All @@ -85,20 +91,32 @@ private static long loadCryptoLibraries() {
long osslVersion;

try {
// load jncrypto JNI library
// Initialize null string before passing it to JNI.
if (nativeLibName == null){
nativeLibName = "";
}

// 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.");

// 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 @@ -253,14 +271,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 51bbf34

Please sign in to comment.