tua*_*iao 5 java-native-interface android openssl android-ndk
我是Android NDK的初学者.我想基于openssl libary构建一个RSA示例.首先,我在guardianproject中使用ndk-build构建了libssl.so和libcrypto.so库.接下来,我创建了一个新的示例项目来集成libary(libss.so和lybcryto.so).我在这篇文章中也是如此
我的App目录
TrialApp
|
|-->Activity.java (includes System.LoadLibrary calls)
|
|-->jni
|-->TestJNI2.cpp
|
|-->Android.mk
|
|-->includes
| |
| |-->openssl (dir containing *.h files)
|
|-->precompiled
|-->libssl.so
|-->libcrypto.so
Run Code Online (Sandbox Code Playgroud)
我的android.mk:
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libssl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
c_includes := $(LOCAL_PATH)
cf_includes := includes/openssl
cf_includes := $(addprefix -Ijni/,$(cf_includes))
export_c_includes := $(c_includes)
LOCAL_MODULE := security
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_CFLAGS += $(cf_includes)
LOCAL_EXPORT_C_INCLUDES := $(export_c_includes)
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libssl.so
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libcrypto.so
include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)
TestJNI2.cpp
/* OpenSSL headers */
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
/* Initializing OpenSSL */
void init_openssl(void){
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
}
Run Code Online (Sandbox Code Playgroud)
然后当我使用ndk-build构建时总是会遇到这样的问题
/data/workspace/TestJNI2/jni/TestJNI2.cpp:3:25: error: openssl/bio.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:4:25: error: openssl/ssl.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:5:25: error: openssl/err.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp: In function 'void init_openssl()':
/data/workspace/TestJNI2/jni/TestJNI2.cpp:10: error: 'SSL_load_error_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:11: error: 'ERR_load_BIO_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:12: error: 'OpenSSL_add_all_algorithms' was not declared in this scope
make: *** [/data/workspace/TestJNI2/obj/local/armeabi/objs/security/TestJNI2.o] Error 1
Run Code Online (Sandbox Code Playgroud)
谁能帮我?或者如何在openssl lib上构建一个示例RSA algorthim?
您应该在脚本中为libssl和libcrypto构建静态库.如果不能,请重命名这些库(您可以在构建之后执行此操作,同时复制到您的precompiled目录).原因是系统自带(可能是不同的)这些共享库的版本,加载器将使用/system/lib/libssl.so而/system/lib/libcrypto.so不是您的私有副本.
关于Android.mk文件,我稍微清理了一下(请注意,我没有更改预先构建的LOCAL_MODULE的名称,但更改了最终构建的LOCAL_MODULE的名称,因为它security是,太通用了,也可能发生在在某些设备上匹配系统库):
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libPrivateSsl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libPrivateCrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := PrivateSecurity
LOCAL_C_INCLUDES := includes
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := ssl crypto
include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)
不要忘记Java应该首先加载依赖项:
{
System.loadLibrary("PrivateSsl");
System.loadLibrary("PrivateCrypto");
System.loadLibrary("PrivateSecurity");
}
Run Code Online (Sandbox Code Playgroud)
我是 Android NDK 的初学者。我想基于 openssl 库构建一个 RSA 示例。
可以,但你必须小心。这就是 Alex 告诉您在脚本中libssl使用静态库的原因。libcrypto
Android的主进程是zygote. 就像init在 Linux 中一样。Zygote 在启动时加载 OpenSSL,并且加载版本 0.9.8。如果您链接到 OpenSSL 1.0.1,那么您将遇到神秘的运行时崩溃。崩溃是由于 Android 加载程序使用 0.9.8 版本的库(已从 Zygote 映射),而不是您的 OpenSSL 版本。
您可以libssl使用共享对象,但您的共享对象必须是和的静态版本的包装器libcrypto。
如果您不使用 Android 的构建系统,那么您可以在 OpenSSL 的 wiki 中找到纯粹从命令行构建的附加说明。wiki页面包括设置环境和交叉编译。请参阅FIPS 库和 Android。
另一件需要注意的事情是:一定要使用-mfloat-abi=softfp. 交叉编译时,普通的 OpenSSL 会忽略这一点。您需要它来确保浮点数在堆栈上传递,而不是通过浮点寄存器传递。否则,所有浮点数都会神秘地具有 0.0f 值(就像用于估计熵的浮点数RAND_add)。
| 归档时间: |
|
| 查看次数: |
13404 次 |
| 最近记录: |