You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2022/07/01 15:45:24 UTC
[commons-crypto] branch master updated: Add DLLName and DLLPath methods
This is an automated email from the ASF dual-hosted git repository.
sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
The following commit(s) were added to refs/heads/master by this push:
new aa9c272 Add DLLName and DLLPath methods
aa9c272 is described below
commit aa9c272ba328f0d76b0c3de4d7023bd933a54b88
Author: Sebb <se...@apache.org>
AuthorDate: Fri Jul 1 16:45:13 2022 +0100
Add DLLName and DLLPath methods
---
.../java/org/apache/commons/crypto/Crypto.java | 2 ++
.../apache/commons/crypto/OpenSslInfoNative.java | 14 +++++++++
.../org/apache/commons/crypto/OpenSslInfoNative.c | 34 +++++++++++++++++++++-
.../commons/crypto/org_apache_commons_crypto.h | 6 ++++
4 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
index f664dd1..31cabae 100644
--- a/src/main/java/org/apache/commons/crypto/Crypto.java
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -158,6 +158,8 @@ public final class Crypto {
info("Native built: %s", OpenSslInfoNative.NativeTimeStamp());
info("OpenSSL library loaded OK, version: 0x%s", Long.toHexString(OpenSslInfoNative.OpenSSL()));
info("OpenSSL library info: %s", OpenSslInfoNative.OpenSSLVersion(0));
+ info("DLL name: %s", OpenSslInfoNative.DLLName());
+ info("DLL path: %s", OpenSslInfoNative.DLLPath());
{ // CryptoRandom
final Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY,
diff --git a/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java
index 6b4fc7e..a249ded 100644
--- a/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java
+++ b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java
@@ -65,4 +65,18 @@ class OpenSslInfoNative {
* @return The text variant of the version number and the release date.
*/
public static native String OpenSSLVersion(int type);
+
+ /**
+ * Return the name used to load the dynamic linked library.
+ *
+ * @return the name used to load the library (e.g. crypto.dll)
+ */
+ public static native String DLLName();
+
+ /**
+ * Return the path to the loaded dynamic linked library.
+ * [Currently not implemented on Windows]
+ * @return the path to the library that was loaded; may be null.
+ */
+ public static native String DLLPath();
}
diff --git a/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c
index e2bff49..7d30c18 100644
--- a/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c
+++ b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c
@@ -16,6 +16,8 @@
* limitations under the License.
*/
+// identify caller to the include file (to avoid unnecessary define of _GNU_SOURCE)
+#define ORG_APACHE_COMMONS_OPENSSLINFONATIVE_C
#include "org_apache_commons_crypto.h"
#include <stdio.h>
@@ -50,6 +52,8 @@ static __dlsym_OpenSSL_version_num dlsym_OpenSSL_version_num;
static __dlsym_OpenSSL_version dlsym_OpenSSL_version;
#endif
+static char dynamicLibraryPath[80]; // where was the crypto library found?
+
#ifdef UNIX
static void get_methods(JNIEnv *env, void *openssl)
#endif
@@ -60,11 +64,19 @@ static void get_methods(JNIEnv *env, HMODULE openssl)
#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");
-#endif
+ LPWSTR lpFilename;
+ WCHAR buffer[80];
+ lpFilename = buffer;
+ (void) GetModuleFileName(openssl, lpFilename, sizeof(buffer)); // ignore return code
+ wcstombs(dynamicLibraryPath, buffer, sizeof(dynamicLibraryPath));
+ #endif
}
static int load_library(JNIEnv *env)
@@ -131,3 +143,23 @@ JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_OpenS
jstring answer = (*env)->NewStringUTF(env,dlsym_OpenSSL_version(type));
return answer;
}
+
+JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_DLLName
+ (JNIEnv *env, jclass clazz)
+{
+ if (!load_library(env)) {
+ return NULL;
+ }
+ jstring answer = (*env)->NewStringUTF(env, COMMONS_CRYPTO_OPENSSL_LIBRARY);
+ return answer;
+}
+
+JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_DLLPath
+ (JNIEnv *env, jclass clazz)
+{
+ if (!load_library(env)) {
+ return NULL;
+ }
+ jstring answer = (*env)->NewStringUTF(env, dynamicLibraryPath);
+ return answer;
+}
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 9ee4671..83f592f 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
@@ -66,6 +66,12 @@
* Unix definitions
*/
#ifdef UNIX
+#ifdef ORG_APACHE_COMMONS_OPENSSLINFONATIVE_C
+#ifndef MAC_OS
+// needed for access to dladdr
+#define _GNU_SOURCE
+#endif
+#endif
#include <dlfcn.h>
#include <jni.h>