diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 951ea169c..4fe5c7488 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -59,6 +59,11 @@ jobs: # - java: 22-ea # os: macos-latest # experimental: true + # We don't need to build all Java versions every time + # Try excluding 11, 17 on branch pushes + exclude: + - java: ${{ (github.ref != 'refs/heads/master' && github.event_name == 'push') && 11 || 99 }} + - java: ${{ (github.ref != 'refs/heads/master' && github.event_name == 'push') && 17 || 99 }} fail-fast: false steps: diff --git a/.github/workflows/maven_crossbuild.yml b/.github/workflows/maven_crossbuild.yml index 806bac204..076884494 100644 --- a/.github/workflows/maven_crossbuild.yml +++ b/.github/workflows/maven_crossbuild.yml @@ -21,7 +21,7 @@ on: # allow direct trigger workflow_dispatch: push: - include: + paths: - '**/native/**' - '**/maven_crossbuild.yml' diff --git a/src/main/native/org/apache/commons/crypto/DynamicLoader.c b/src/main/native/org/apache/commons/crypto/DynamicLoader.c index f64bb658a..b6a4fe035 100644 --- a/src/main/native/org/apache/commons/crypto/DynamicLoader.c +++ b/src/main/native/org/apache/commons/crypto/DynamicLoader.c @@ -22,15 +22,8 @@ Shared code to load and unload the library. #include "org_apache_commons_crypto.h" -#ifdef UNIX -static void *openssl; // the cached pointer -void *open_library(JNIEnv *env) -#endif - -#ifdef WINDOWS static HMODULE openssl; // the cached pointer HMODULE open_library(JNIEnv *env) -#endif { if (!openssl) { diff --git a/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c index d76550ea2..78a42c213 100644 --- a/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c +++ b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c @@ -54,23 +54,16 @@ static __dlsym_OpenSSL_version dlsym_OpenSSL_version; static char dynamicLibraryPath[80]; // where was the crypto library found? -#ifdef UNIX -static void get_methods(JNIEnv *env, void *openssl) -#endif -#ifdef WINDOWS static void get_methods(JNIEnv *env, HMODULE openssl) -#endif { + LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version_num, dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); + LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version, dlsym_OpenSSL_version, env, openssl, "OpenSSL_version", "SSLeay_version"); #ifdef UNIX - LOAD_DYNAMIC_SYMBOL_FALLBACK(dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); - LOAD_DYNAMIC_SYMBOL_FALLBACK(dlsym_OpenSSL_version, env, openssl, "OpenSSL_version", "SSLeay_version"); Dl_info info; (void) dladdr(dlsym_OpenSSL_version_num, &info); // ignore the return code strncpy(dynamicLibraryPath, info.dli_fname, sizeof(dynamicLibraryPath) - 1); // allow for null #endif #ifdef WINDOWS - LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version_num, dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); - LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version, dlsym_OpenSSL_version, env, openssl, "OpenSSL_version", "SSLeay_version"); LPWSTR lpFilename; WCHAR buffer[80]; lpFilename = buffer; @@ -81,24 +74,12 @@ static void get_methods(JNIEnv *env, HMODULE openssl) static int load_library(JNIEnv *env) { - char msg[1000]; -#ifdef UNIX - void *openssl = open_library(env); -#endif - -#ifdef WINDOWS HMODULE openssl = open_library(env); -#endif if (!openssl) { -#ifdef UNIX + char msg[1000]; snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ - dlerror()); -#endif -#ifdef WINDOWS - snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ - GetLastError()); -#endif + GET_LAST_ERROR); THROW(env, "java/lang/UnsatisfiedLinkError", msg); return 0; } diff --git a/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c b/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c index b38174eb4..6dc4347af 100644 --- a/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c +++ b/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c @@ -101,27 +101,8 @@ static __dlsym_EVP_aes_192_gcm dlsym_EVP_aes_192_gcm; static __dlsym_EVP_aes_128_gcm dlsym_EVP_aes_128_gcm; #endif -#ifdef UNIX -static void loadAes(JNIEnv *env, void *openssl) -#endif - -#ifdef WINDOWS static void loadAes(JNIEnv *env, HMODULE openssl) -#endif { -#ifdef UNIX - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_256_ctr, env, openssl, "EVP_aes_256_ctr"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_192_ctr, env, openssl, "EVP_aes_192_ctr"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_128_ctr, env, openssl, "EVP_aes_128_ctr"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_256_cbc, env, openssl, "EVP_aes_256_cbc"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_192_cbc, env, openssl, "EVP_aes_192_cbc"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_128_cbc, env, openssl, "EVP_aes_128_cbc"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_256_gcm, env, openssl, "EVP_aes_256_gcm"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_192_gcm, env, openssl, "EVP_aes_192_gcm"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_aes_128_gcm, env, openssl, "EVP_aes_128_gcm"); -#endif - -#ifdef WINDOWS LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_aes_256_ctr, dlsym_EVP_aes_256_ctr, \ env, openssl, "EVP_aes_256_ctr"); LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_aes_192_ctr, dlsym_EVP_aes_192_ctr, \ @@ -140,50 +121,24 @@ static void loadAes(JNIEnv *env, HMODULE openssl) env, openssl, "EVP_aes_192_gcm"); LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_aes_128_gcm, dlsym_EVP_aes_128_gcm, \ env, openssl, "EVP_aes_128_gcm"); -#endif } JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initIDs (JNIEnv *env, jclass clazz) { - char msg[1000]; -#ifdef UNIX - void *openssl = open_library(env); -#endif - -#ifdef WINDOWS HMODULE openssl = open_library(env); -#endif if (!openssl) { -#ifdef UNIX + char msg[1000]; snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ - dlerror()); -#endif -#ifdef WINDOWS - snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ - GetLastError()); -#endif + GET_LAST_ERROR); THROW(env, "java/lang/UnsatisfiedLinkError", msg); return; } #ifdef UNIX dlerror(); // Clear any existing error - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_new, env, openssl, "EVP_CIPHER_CTX_new"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_free, env, openssl, "EVP_CIPHER_CTX_free"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_set_padding, env, openssl, "EVP_CIPHER_CTX_set_padding"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_ctrl, env, openssl, "EVP_CIPHER_CTX_ctrl"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_block_size, env, openssl, "EVP_CIPHER_CTX_block_size"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_cipher, env, openssl, "EVP_CIPHER_CTX_cipher"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_flags, env, openssl, "EVP_CIPHER_flags"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CIPHER_CTX_test_flags, env, openssl, "EVP_CIPHER_CTX_test_flags"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherInit_ex, env, openssl, "EVP_CipherInit_ex"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherUpdate, env, openssl, "EVP_CipherUpdate"); - LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherFinal_ex, env, openssl, "EVP_CipherFinal_ex"); #endif - -#ifdef WINDOWS LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_CIPHER_CTX_new, dlsym_EVP_CIPHER_CTX_new, \ env, openssl, "EVP_CIPHER_CTX_new"); LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_CIPHER_CTX_free, dlsym_EVP_CIPHER_CTX_free, \ @@ -206,7 +161,6 @@ JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initI env, openssl, "EVP_CipherUpdate"); LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_CipherFinal_ex, dlsym_EVP_CipherFinal_ex, \ env, openssl, "EVP_CipherFinal_ex"); -#endif loadAes(env, openssl); jthrowable jthr = (*env)->ExceptionOccurred(env); diff --git a/src/main/native/org/apache/commons/crypto/org_apache_commons_crypto.h b/src/main/native/org/apache/commons/crypto/org_apache_commons_crypto.h index 1896bfbcb..fed2b036f 100644 --- a/src/main/native/org/apache/commons/crypto/org_apache_commons_crypto.h +++ b/src/main/native/org/apache/commons/crypto/org_apache_commons_crypto.h @@ -77,7 +77,9 @@ void close_library(); #include #include -void *open_library(JNIEnv *env); +typedef void * HMODULE; // to agree with Windows type name + +HMODULE open_library(JNIEnv *env); /** * A helper function to dlsym a 'symbol' from a given library-handle. @@ -133,16 +135,20 @@ void *do_dlsym_fallback(JNIEnv *env, void *handle, const char *symbol, const cha } /* A helper macro to dlsym the requisite dynamic symbol and bail-out on error. */ -#define LOAD_DYNAMIC_SYMBOL(func_ptr, env, handle, symbol) \ +// func_type is currently ignored, so can use same macro invocation as for Windows +#define LOAD_DYNAMIC_SYMBOL(_func_type, func_ptr, env, handle, symbol) \ if ((func_ptr = do_dlsym(env, handle, symbol)) == NULL) { \ return; \ } /* A macro to dlsym the requisite dynamic symbol (with fallback) and bail-out on error. */ -#define LOAD_DYNAMIC_SYMBOL_FALLBACK(func_ptr, env, handle, symbol, fallback) \ +// func_type is currently ignored, so can use same macro invocation as for Windows +#define LOAD_DYNAMIC_SYMBOL_FALLBACK(_func_type, func_ptr, env, handle, symbol, fallback) \ if ((func_ptr = do_dlsym_fallback(env, handle, symbol, fallback)) == NULL) { \ return; \ } +// Macro to hide different method names +#define GET_LAST_ERROR dlerror() #endif // Unix part end @@ -251,6 +257,8 @@ static FARPROC WINAPI do_dlsym_fallback(JNIEnv *env, HMODULE handle, LPCSTR symb } return func_ptr; } +// Macro to hide different method names +#define GET_LAST_ERROR GetLastError() #endif // Windows part end diff --git a/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c b/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c index 735ffeeb1..9a7fb8149 100644 --- a/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c +++ b/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c @@ -100,48 +100,18 @@ static int openssl_rand_bytes(unsigned char *buf, int num); JNIEXPORT void JNICALL Java_org_apache_commons_crypto_random_OpenSslCryptoRandomNative_initSR (JNIEnv *env, jclass clazz) { - char msg[1000]; -#ifdef UNIX - void *openssl = open_library(env); -#endif - -#ifdef WINDOWS HMODULE openssl = open_library(env); -#endif if (!openssl) { -#ifdef UNIX - snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, dlerror()); -#endif -#ifdef WINDOWS - snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, GetLastError()); -#endif + char msg[1000]; + snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, GET_LAST_ERROR); THROW(env, "java/lang/UnsatisfiedLinkError", msg); return; } + LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version_num, dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); #ifdef UNIX - LOAD_DYNAMIC_SYMBOL_FALLBACK(dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); dlerror(); // Clear any existing error - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_malloc, env, openssl, "CRYPTO_malloc"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_free, env, openssl, "CRYPTO_free"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_by_id, env, openssl, "ENGINE_by_id"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_init, env, openssl, "ENGINE_init"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_set_default, env, openssl, "ENGINE_set_default"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_finish, env, openssl, "ENGINE_finish"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_free, env, openssl, "ENGINE_free"); - LOAD_DYNAMIC_SYMBOL(dlsym_RAND_bytes, env, openssl, "RAND_bytes"); - LOAD_DYNAMIC_SYMBOL(dlsym_ERR_get_error, env, openssl, "ERR_get_error"); - if (dlsym_OpenSSL_version_num() < VERSION_1_1_X) { - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_num_locks, env, openssl, "CRYPTO_num_locks"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_id_callback, env, openssl, "CRYPTO_set_id_callback"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_locking_callback, env, openssl, "CRYPTO_set_locking_callback"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_load_rdrand, env, openssl, "ENGINE_load_rdrand"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_cleanup, env, openssl, "ENGINE_cleanup"); - } #endif - -#ifdef WINDOWS - LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version_num, dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay"); LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_malloc, dlsym_CRYPTO_malloc, env, openssl, "CRYPTO_malloc"); LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_free, dlsym_CRYPTO_free, env, openssl, "CRYPTO_free"); LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_by_id, dlsym_ENGINE_by_id, env, openssl, "ENGINE_by_id"); @@ -157,7 +127,6 @@ JNIEXPORT void JNICALL Java_org_apache_commons_crypto_random_OpenSslCryptoRandom LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_load_rdrand, dlsym_ENGINE_load_rdrand, env, openssl, "ENGINE_load_rdrand"); LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_cleanup, dlsym_ENGINE_cleanup, env, openssl, "ENGINE_cleanup"); } -#endif openssl_rand_init(); }