Skip to content

Commit

Permalink
openssl version 3.0+ supported
Browse files Browse the repository at this point in the history
Signed-off-by: Jinhang Zhang <[email protected]>
  • Loading branch information
JinhangZhang committed Apr 12, 2022
1 parent aff77b6 commit a2ce126
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
4 changes: 2 additions & 2 deletions closed/src/java.base/aix/native/libjncrypto/NativeCrypto_md.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,7 +30,7 @@
#include "NativeCrypto_md.h"

/* Load the crypto library (return NULL on error) */
void * load_crypto_library() {
void * load_crypto_library(jboolean traceEnabled) {
void * result = NULL;
const char *libname111 = "libcrypto.a(libcrypto64.so.1.1)";
const char *libname110 = "libcrypto.so.1.1";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,7 +30,7 @@
#include "NativeCrypto_md.h"

/* Load the crypto library (return NULL on error) */
void * load_crypto_library() {
void * load_crypto_library(jboolean traceEnabled) {
void * result = NULL;

const char *libname = "libcrypto.1.1.dylib";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2018, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2018, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,12 +40,16 @@ public class NativeCrypto {
private static final int ossl_ver = AccessController.doPrivileged(
(PrivilegedAction<Integer>) () -> {
int ossl_ver;
String nativeCryptTrace = System.getProperty("jdk.nativeCryptoTrace");

try {
System.loadLibrary("jncrypto"); // check for native library
// load OpenSSL crypto library dynamically
ossl_ver = loadCrypto(Boolean.getBoolean("jdk.nativeCryptoTrace"));
ossl_ver = loadCrypto(nativeCryptTrace != null);
} catch (UnsatisfiedLinkError e) {
// signal load failure
if (nativeCryptTrace != null) {
System.err.println("UnsatisfiedLinkError: Failure attempting to load jncrypto JNI library");
}
ossl_ver = -1;
}

Expand Down
37 changes: 25 additions & 12 deletions closed/src/java.base/share/native/libjncrypto/NativeCrypto.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2018, 2021 All Rights Reserved
* (c) Copyright IBM Corp. 2018, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -38,7 +38,10 @@

#define OPENSSL_VERSION_1_0 "OpenSSL 1.0."
#define OPENSSL_VERSION_1_1 "OpenSSL 1.1."
/* needed for OpenSSL 1.0.2 Thread handling routines */
/* Per new OpenSSL naming convention starting from OpenSSL3, all major versions are ABI and API compatible. */
#define OPENSSL_VERSION_3_0 "OpenSSL 3."

/* needed for OpenSSL 1.0.2 Thread handling routines. */
#define CRYPTO_LOCK 1

#if defined(WINDOWS)
Expand All @@ -47,12 +50,12 @@
# include <pthread.h>
#endif /* defined(WINDOWS) */

/* Header for RSA algorithm using 1.0.2 OpenSSL */
/* Header for RSA algorithm using 1.0.2 OpenSSL. */
int OSSL102_RSA_set0_key(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
int OSSL102_RSA_set0_factors(RSA *, BIGNUM *, BIGNUM *);
int OSSL102_RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);

/* Define literals from OpenSSL 1.1.x so that it compiles with OpenSSL 1.0.x */
/* Define literals from OpenSSL 1.1.x so that it compiles with OpenSSL 1.0.x. */
#ifndef EVP_CTRL_AEAD_GET_TAG
#define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
#endif
Expand All @@ -65,7 +68,7 @@ int OSSL102_RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
#endif

/* Type definitions of function pointers */
/* Type definitions of function pointers. */
typedef char * OSSL_error_string_n_t(unsigned long, char *, size_t);
typedef char * OSSL_error_string_t(unsigned long, char *);
typedef unsigned long OSSL_get_error_t();
Expand Down Expand Up @@ -123,7 +126,7 @@ OSSL_error_string_n_t* OSSL_error_string_n;
OSSL_error_string_t* OSSL_error_string;
OSSL_get_error_t* OSSL_get_error;

/* Define pointers for OpenSSL 1.0.2 threading routines */
/* Define pointers for OpenSSL 1.0.2 threading routines. */
static OSSL_CRYPTO_num_locks_t* OSSL_CRYPTO_num_locks = NULL;
static OSSL_CRYPTO_THREADID_set_numeric_t* OSSL_CRYPTO_THREADID_set_numeric = NULL;
static OSSL_OPENSSL_malloc_t* OSSL_OPENSSL_malloc = NULL;
Expand Down Expand Up @@ -219,19 +222,20 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
int ossl_ver;

/* Load OpenSSL Crypto library */
crypto_library = load_crypto_library();
crypto_library = load_crypto_library(trace);
if (NULL == crypto_library) {
if (trace) {
fprintf(stderr, "Error loading OpenSSL: FAILED TO LOAD OPENSSL CRYPTO LIBRARY\n");
fflush(stderr);
}
return -1;
}

/*
* Different symbols are used by OpenSSL with 1.0 and 1.1.
* The symbol 'OpenSSL_version' is used by OpenSSL 1.1 where as
* Different symbols are used by OpenSSL with 1.0 and 1.1 (and later).
* The symbol 'OpenSSL_version' is used by OpenSSL 1.1 and later where as
* the symbol "SSLeay_version" is used by OpenSSL 1.0.
* Currently only openssl 1.0.x and 1.1.x are supported.
* Currently only openssl 1.0.x, 1.1.x, and 3.x.x are supported.
*/
OSSL_version = (OSSL_version_t*)find_crypto_symbol(crypto_library, "OpenSSL_version");

Expand All @@ -241,6 +245,7 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
if (NULL == OSSL_version) {
if (trace) {
fprintf(stderr, "Error loading OpenSSL: Error finding the OpenSSL version symbol in the crypto library\n");
fflush(stderr);
}
unload_crypto_library(crypto_library);
crypto_library = NULL;
Expand All @@ -251,6 +256,7 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
if (0 != strncmp(openssl_version, OPENSSL_VERSION_1_0, strlen(OPENSSL_VERSION_1_0))) {
if (trace) {
fprintf(stderr, "Error loading OpenSSL: Incompatible OpenSSL version found: %s\n", openssl_version);
fflush(stderr);
}
unload_crypto_library(crypto_library);
crypto_library = NULL;
Expand All @@ -260,10 +266,12 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
}
} else {
openssl_version = (*OSSL_version)(0); /* get OPENSSL_VERSION */
/* Ensure the OpenSSL version is "OpenSSL 1.1.x". */
if (0 != strncmp(openssl_version, OPENSSL_VERSION_1_1, strlen(OPENSSL_VERSION_1_1))) {
/* Ensure the OpenSSL version is "OpenSSL 1.1.x" or "OpenSSL 3.0.x". */
if ((0 != strncmp(openssl_version, OPENSSL_VERSION_1_1, strlen(OPENSSL_VERSION_1_1)))
&& (0 != strncmp(openssl_version, OPENSSL_VERSION_3_0, strlen(OPENSSL_VERSION_3_0)))) {
if (trace) {
fprintf(stderr, "Error loading OpenSSL: Incompatible OpenSSL version found: %s\n", openssl_version);
fflush(stderr);
}
unload_crypto_library(crypto_library);
crypto_library = NULL;
Expand All @@ -272,6 +280,11 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
ossl_ver = 1;
}

if (trace) {
fprintf(stderr, "Error loading OpenSSL: Incompatible OpenSSL version found: %s\n", openssl_version);
fflush(stderr);
}

/* Load the function symbols for OpenSSL errors. */
OSSL_error_string_n = (OSSL_error_string_n_t*)find_crypto_symbol(crypto_library, "ERR_error_string_n");
OSSL_error_string = (OSSL_error_string_t*)find_crypto_symbol(crypto_library, "ERR_error_string");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,11 +21,11 @@
*
* ===========================================================================
*/

#include <jni.h>
#ifndef NATIVECRYPTO_MD_H
#define NATIVECRYPTO_MD_H

void * load_crypto_library();
void * load_crypto_library(jboolean traceEnabled);
void unload_crypto_library(void *handle);
void * find_crypto_symbol(void *handle, const char *symname);
void get_library_path(void * handle, char * library_path);
Expand Down
14 changes: 11 additions & 3 deletions closed/src/java.base/unix/native/libjncrypto/NativeCrypto_md.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,6 +22,7 @@
* ===========================================================================
*/

#include <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -30,12 +31,13 @@
#include "NativeCrypto_md.h"

/* Load the crypto library (return NULL on error) */
void * load_crypto_library() {
void * load_crypto_library(jboolean traceEnabled) {
void * result = NULL;
size_t i = 0;

// Library names for OpenSSL 1.1.1, 1.1.0, 1.0.2 and symbolic links
// Library names for OpenSSL 3.x, 1.1.1, 1.1.0, 1.0.2 and symbolic links
static const char * const libNames[] = {
"libcrypto.so.3", // 3.x library name
"libcrypto.so.1.1", // 1.1.x library name
"libcrypto.so.1.0.0", // 1.0.x library name
"libcrypto.so.10", // 1.0.x library name on RHEL
Expand All @@ -50,6 +52,12 @@ void * load_crypto_library() {
result = dlopen (libName, RTLD_NOW);
}

if (traceEnabled && (NULL != result)) {
struct link_map *map;
dlinfo(result, RTLD_DI_LINKMAP, &map);
fprintf(stderr, "Attempt to load OpenSSL %s\n",map->l_name);
fflush(stderr);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
* ===========================================================================
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -27,7 +27,7 @@
#include "NativeCrypto_md.h"

/* Load the crypto library (return NULL on error) */
void * load_crypto_library() {
void * load_crypto_library(jboolean traceEnabled) {
void * result = NULL;
const char *libname = "libcrypto-1_1-x64.dll";
const char *oldname = "libeay32.dll";
Expand Down

0 comments on commit a2ce126

Please sign in to comment.