Skip to content

Commit

Permalink
Merge branch 'master' into crypto-174
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbASF committed Nov 6, 2023
2 parents c17c77e + 950bf17 commit d0e049a
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* CryptoRandomFactory.RandomProvider
* </p>
*/
class JceCipher implements CryptoCipher {
final class JceCipher implements CryptoCipher {
private final Cipher cipher;

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ public String getAlgorithm() {
* not a block cipher
*/
@Override
public final int getBlockSize() {
public int getBlockSize() {
return cipher.getBlockSize();
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public static boolean isEnabled() {
* @throws Throwable Throws value from {@link #initialisationError()}.
*/
public static void main(final String[] args) throws Throwable {
// These are used by JNA code if defined:
info("jna.library.path=%s", System.getProperty("jna.library.path"));
info("jna.platform.library.path=%s", System.getProperty("jna.platform.library.path"));
// can set jna.debug_load=true for loading info
info(Crypto.getComponentName() + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION);
final Throwable initialisationError = initialisationError();
if (initialisationError != null) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/native/org/apache/commons/crypto/DynamicLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ HMODULE open_library(JNIEnv *env)
#endif

}
// Did we succeed?
if (!openssl)
{
char msg[1000];
#ifdef UNIX
snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
dlerror()); // returns char*
#endif
#ifdef WINDOWS
// TODO: convert to string
snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
GetLastError()); // returns DWORD
#endif
THROW(env, "java/lang/UnsatisfiedLinkError", msg);
return 0;
}
return openssl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ static void get_methods(JNIEnv *env, HMODULE openssl)

static int load_library(JNIEnv *env)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
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 0;
}
get_methods(env, openssl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ static void loadAes(JNIEnv *env, HMODULE openssl)
JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initIDs
(JNIEnv *env, jclass clazz)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ void *do_dlsym_fallback(JNIEnv *env, void *handle, const char *symbol, const cha
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 @@ -257,8 +256,7 @@ 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,14 +100,12 @@ 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)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
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
dlerror(); // Clear any existing error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Properties;

class FailingRandom implements CryptoRandom {
final class FailingRandom implements CryptoRandom {

public static native void NoSuchMethod();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.apache.commons.crypto.random;


class NoopRandom implements CryptoRandom {
final class NoopRandom implements CryptoRandom {

/** Should fail with NoSuchMethodException. */
NoopRandom() {
Expand Down

0 comments on commit d0e049a

Please sign in to comment.