Skip to content

Commit

Permalink
Merge pull request ibmruntimes#668 from KostasTsiounis/ossl_version
Browse files Browse the repository at this point in the history
Change crypto loading method to return OpenSSL version
  • Loading branch information
keithc-ca authored May 25, 2023
2 parents a3f77a4 + 4d1b780 commit adbbc28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,44 @@ public class NativeCrypto {
private static final boolean traceEnabled = Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("jdk.nativeCryptoTrace", "false"));

//ossl_vers will be either:
//ossl_ver will be either:
// -1 : library load failed
// or one of the OPENSSL_VERSION_x_x_x constants
private static final boolean loaded = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) () -> {
Boolean isLoaded = Boolean.FALSE;

private static final long ossl_ver = AccessController.doPrivileged(
(PrivilegedAction<Long>) () -> {
long osslVersion;
try {
System.loadLibrary("jncrypto"); // check for native library
// load OpenSSL crypto library dynamically.
long ossl_ver = loadCrypto(traceEnabled);
if (ossl_ver != -1) {
isLoaded = Boolean.TRUE;
osslVersion = loadCrypto(traceEnabled);
if (traceEnabled && (osslVersion != -1)) {
System.err.println("Native crypto library load succeeded - using native crypto library.");
}
} catch (UnsatisfiedLinkError usle) {
} catch (UnsatisfiedLinkError usle) {
if (traceEnabled) {
System.err.println("UnsatisfiedLinkError: Failure attempting to load jncrypto JNI library");
System.err.println("UnsatisfiedLinkError: Failure attempting to load jncrypto JNI library");
System.err.println("Warning: Native crypto library load failed." +
" Using Java crypto implementation.");
}
// Return that isLoaded is false (default set above)
// signal load failure
osslVersion = -1;
}

return isLoaded;
}).booleanValue();


return osslVersion;
}).longValue();

/**
* Return the OpenSSL version. It will return -1 if the library isn't
* successfully loaded.
*
* @return the OpenSSL library version or -1, if not loaded.
*/
public static final long getVersion() {
return ossl_ver;
}

public static final boolean isLoaded() {
return loaded;
return getVersion() >= 0;
}

/**
Expand Down Expand Up @@ -132,7 +144,7 @@ public static final boolean isAlgorithmEnabled(String property, String name, boo
* native crypto library is loaded successfully. Otherwise, issue a warning
* message and fall back to the built-in java crypto implementation.
*/
if (loaded) {
if (isLoaded()) {
if (satisfied) {
if (traceEnabled) {
System.err.println(name + " - using native crypto library.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
return -1;
}
}
return 0;
return ossl_ver;
}
}

Expand Down

0 comments on commit adbbc28

Please sign in to comment.