Skip to content

Commit

Permalink
Simplify native code by moving differences into macros (#266)
Browse files Browse the repository at this point in the history
* Hide OS variable behaviour in macro define

The Windows macro invocations have an extra type argument,
but the other args are identical.
The Unix macro now ignores the type parameter
This allows the same invocation parameters on Unix and Windows

* Define HMODULE for Unix to simply conditional code

* Define HMODULE for Unix to simply conditional code

* Use macro to hide last error message names
  • Loading branch information
sebbASF authored Nov 4, 2023
1 parent 9e2fd17 commit 3a09ee3
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maven_crossbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on:
# allow direct trigger
workflow_dispatch:
push:
include:
paths:
- '**/native/**'
- '**/maven_crossbuild.yml'

Expand Down
7 changes: 0 additions & 7 deletions src/main/native/org/apache/commons/crypto/DynamicLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 4 additions & 23 deletions src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
50 changes: 2 additions & 48 deletions src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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, \
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ void close_library();
#include <dlfcn.h>
#include <jni.h>

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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
}
Expand Down

0 comments on commit 3a09ee3

Please sign in to comment.