CRYPTO-104 Native code should provide getVersion() methods Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/eaed08d0 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/eaed08d0 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/eaed08d0
Branch: refs/heads/CRYPTO-1.0.0 Commit: eaed08d07d0d7800c48e428061d919130fba0733 Parents: dba8b4a Author: Sebb <s...@apache.org> Authored: Mon Jul 11 00:45:55 2016 +0100 Committer: Sebb <s...@apache.org> Committed: Mon Jul 11 00:45:55 2016 +0100 ---------------------------------------------------------------------- Makefile | 9 +- pom.xml | 1 + .../java/org/apache/commons/crypto/Crypto.java | 11 +- .../commons/crypto/OpenSslInfoNative.java | 41 ++++++ .../apache/commons/crypto/OpenSslInfoNative.c | 137 +++++++++++++++++++ 5 files changed, 196 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/eaed08d0/Makefile ---------------------------------------------------------------------- diff --git a/Makefile b/Makefile index e579664..b6e8648 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ include Makefile.common COMMONS_CRYPTO_OUT:=$(TARGET)/$(commons-crypto)-$(os_arch) -COMMONS_CRYPTO_OBJ:=$(addprefix $(COMMONS_CRYPTO_OUT)/,OpenSslCryptoRandomNative.o OpenSslNative.o) +COMMONS_CRYPTO_OBJ:=$(addprefix $(COMMONS_CRYPTO_OUT)/,OpenSslCryptoRandomNative.o OpenSslNative.o OpenSslInfoNative.o) # Windows uses different path separators ifeq ($(OS_NAME),Windows) @@ -43,6 +43,9 @@ $(TARGET)/jni-classes/org/apache/commons/crypto/cipher/OpenSslNative.h: $(TARGET $(TARGET)/jni-classes/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.h: $(TARGET)/classes/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.class $(JAVAH) -force -classpath $(TARGET)/classes -o $@ org.apache.commons.crypto.random.OpenSslCryptoRandomNative +$(TARGET)/jni-classes/org/apache/commons/crypto/OpenSslInfoNative.h: $(TARGET)/classes/org/apache/commons/crypto/OpenSslInfoNative.class + $(JAVAH) -force -classpath $(TARGET)/classes -o $@ org.apache.commons.crypto.OpenSslInfoNative + $(COMMONS_CRYPTO_OUT)/OpenSslNative.o : $(SRC_NATIVE)/org/apache/commons/crypto/cipher/OpenSslNative.c $(TARGET)/jni-classes/org/apache/commons/crypto/cipher/OpenSslNative.h @mkdir -p $(@D) $(CC) $(CFLAGS) -c $< -o $@ @@ -51,6 +54,10 @@ $(COMMONS_CRYPTO_OUT)/OpenSslCryptoRandomNative.o : $(SRC_NATIVE)/org/apache/com @mkdir -p $(@D) $(CC) $(CFLAGS) -c $< -o $@ +$(COMMONS_CRYPTO_OUT)/OpenSslInfoNative.o : $(SRC_NATIVE)/org/apache/commons/crypto/OpenSslInfoNative.c $(TARGET)/jni-classes/org/apache/commons/crypto/OpenSslInfoNative.h + @mkdir -p $(@D) + $(CC) $(CFLAGS) -DVERSION='"$(VERSION)"' -DPROJECT_NAME='"$(PROJECT_NAME)"' -I"$(TARGET)/jni-classes/org/apache/commons/crypto" -c $< -o $@ + $(COMMONS_CRYPTO_OUT)/$(LIBNAME): $(COMMONS_CRYPTO_OBJ) $(CXX) $(CXXFLAGS) -o $@ $+ $(LINKFLAGS) $(STRIP) $@ http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/eaed08d0/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index fc27e2c..45a2a3f 100644 --- a/pom.xml +++ b/pom.xml @@ -480,6 +480,7 @@ The following provides more details on the included cryptographic software: classpathref="maven.plugin.classpath"/> <exec executable="make" failonerror="true" dir="${project.basedir}"> <env key="VERSION" value="${project.version}"/> + <env key="PROJECT_NAME" value="${project.name}"/> <!-- For debugging: --> <!-- <arg value="-d"/> --> <arg value="${target.name}"/> http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/eaed08d0/src/main/java/org/apache/commons/crypto/Crypto.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java index 12e55c3..7c86ee8 100644 --- a/src/main/java/org/apache/commons/crypto/Crypto.java +++ b/src/main/java/org/apache/commons/crypto/Crypto.java @@ -119,20 +119,27 @@ public final class Crypto { public static void main(String args[]) throws Exception { System.out.println(getComponentName() + " " + getComponentVersion()); if (isNativeCodeLoaded()) { - System.out.println("Native code loaded OK, version: TBA"); // TODO get VERSION from native code + System.out.println("Native code loaded OK " + OpenSslInfoNative.NativeVersion()); + System.out.println("Native Name " + OpenSslInfoNative.NativeName()); + System.out.println("Native Built " + OpenSslInfoNative.NativeTimeStamp()); + System.out.println("OpenSSL library loaded OK, version: 0x" + Long.toHexString(OpenSslInfoNative.SSLeay())); + System.out.println(OpenSslInfoNative.SSLeayVersion(0)); { Properties props = new Properties(); props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName()); CryptoRandomFactory.getCryptoRandom(props); System.out.println("Random instance created OK"); } - System.out.println("OpenSSL library loaded OK, version: TBA"); // TODO get SSLeay() etc. from library { Properties props = new Properties(); props.setProperty(CryptoCipherFactory.CLASSES_KEY, CryptoCipherFactory.CipherProvider.OPENSSL.getClassName()); CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", props); System.out.println("Cipher instance created OK"); } + System.out.println("Additional SSLeay_version(n) details:"); + for(int j=1;j<6;j++) { + System.out.println(j+": "+ OpenSslInfoNative.SSLeayVersion(j)); + } } else { System.out.println("Native load failed: " + getLoadingError()); } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/eaed08d0/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java new file mode 100644 index 0000000..3aa394f --- /dev/null +++ b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.crypto; + +/** + * JNI interface of {@link CryptoRandom} implementation for OpenSSL. + * The native method in this class is defined in + * OpenSslCryptoRandomNative.h (generated at build time by javah) + * and implemented in the file + * src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c + */ +class OpenSslInfoNative { + + private OpenSslInfoNative() { + } + + public static native String NativeVersion(); + + public static native String NativeName(); + + public static native String NativeTimeStamp(); + + public static native long SSLeay(); + + public static native String SSLeayVersion(int type); +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/eaed08d0/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c ---------------------------------------------------------------------- diff --git a/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c new file mode 100644 index 0000000..85d1407 --- /dev/null +++ b/src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "org_apache_commons_crypto.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef UNIX +#include <unistd.h> +#include <sys/types.h> +#endif + +#ifdef WINDOWS +#include <windows.h> +#endif + +// export the native interfaces +#ifdef JNIEXPORT +#undef JNIEXPORT +#endif +#define JNIEXPORT __attribute__((__visibility__("default"))) +#include "OpenSslInfoNative.h" + +#ifdef UNIX +static unsigned long (*dlsym_SSLeay) (void); +static char * (*dlsym_SSLeay_version) (int); +static void *openssl; +#endif + +#ifdef WINDOWS +typedef unsigned long (__cdecl *__dlsym_SSLeay) (void); +static __dlsym_SSLeay dlsym_SSLeay; +typedef char * (__cdecl *__dlsym_SSLeay_version) (int); +static __dlsym_SSLeay dlsym_SSLeay; +static __dlsym_SSLeay_version dlsym_SSLeay_version; +HMODULE openssl; +#endif + +#ifdef UNIX +static void get_methods(JNIEnv *env, void *openssl) +#endif +#ifdef WINDOWS +static void get_methods(JNIEnv *env, HMODULE openssl) +#endif +{ +#ifdef UNIX + dlerror(); // Clear any existing error + LOAD_DYNAMIC_SYMBOL(dlsym_SSLeay, env, openssl, "SSLeay"); + LOAD_DYNAMIC_SYMBOL(dlsym_SSLeay_version, env, openssl, "SSLeay_version"); +#endif + +#ifdef WINDOWS + LOAD_DYNAMIC_SYMBOL(__dlsym_SSLeay, dlsym_SSLeay, env, openssl, "SSLeay"); + LOAD_DYNAMIC_SYMBOL(__dlsym_SSLeay_version, dlsym_SSLeay_version, env, openssl, "SSLeay_version"); +#endif +} +static int load_library(JNIEnv *env) +{ + char msg[100]; +#ifdef UNIX + openssl = dlopen(COMMONS_CRYPTO_OPENSSL_LIBRARY, RTLD_LAZY | RTLD_GLOBAL); +#endif + +#ifdef WINDOWS + openssl = LoadLibrary(TEXT(COMMONS_CRYPTO_OPENSSL_LIBRARY)); +#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 + THROW(env, "java/lang/UnsatisfiedLinkError", msg); + return 0; + } + get_methods(env, openssl); + return 1; +} + +JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_SSLeayVersion + (JNIEnv *env, jclass clazz, jint type) +{ + if (!load_library(env)) { + return NULL; + } + + jstring answer = (*env)->NewStringUTF(env,dlsym_SSLeay_version(type)); + return answer; +} + +JNIEXPORT jlong JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_SSLeay + (JNIEnv *env, jobject object) +{ + if (!load_library(env)) { + return 0; + } + return dlsym_SSLeay(); +} + +JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_NativeVersion + (JNIEnv *env, jobject object) +{ + return (*env)->NewStringUTF(env, VERSION); +} + +JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_NativeTimeStamp + (JNIEnv *env, jobject object) +{ + return (*env)->NewStringUTF(env, __DATE__); +} + +JNIEXPORT jstring JNICALL Java_org_apache_commons_crypto_OpenSslInfoNative_NativeName + (JNIEnv *env, jobject object) +{ + return (*env)->NewStringUTF(env, PROJECT_NAME); +}