Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

use libcryto.so and libssl.so in an android project?

I'm beginer to Android NDK. I want to build a RSA example base on openssl libary. First, I built libssl.so and libcrypto.so librairies with ndk-build in the guardianproject. Next, I create a new sample project to integrate libary (libss.so & lybcryto.so). I follow the same in this post

My App directory

TrialApp
|
|-->Activity.java (includes System.LoadLibrary calls)
|
|-->jni
    |-->TestJNI2.cpp
    |
    |-->Android.mk
    |
    |-->includes
    |   |
    |   |-->openssl (dir containing *.h files)
    |
    |-->precompiled
       |-->libssl.so
       |-->libcrypto.so

My 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)

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();
}

Then when i build with ndk-build always have problems like this

/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

Can anyone help me? Or how to build an example RSA algorthim base on openssl lib?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should build static libraries for libssl and libcrypto in your script. If you can't, rename these libraries (you can do this after build, while copying to your precompiled directory). The reason is that the system comes with its own (probably different) version of these shared libraries, and the loader will use /system/lib/libssl.so and /system/lib/libcrypto.so instead of your private copies.

Regarding the Android.mk file, I slightly cleaned it up for you (note that I did not change the names of prebuilt LOCAL_MODULEs, but changed the name of LOCAL_MODULE you finally build, because security is, well, too generic and could also happen to match a system library on some device):

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)

Don't forget that your Java should load the dependencies first:

{
    System.loadLibrary("PrivateSsl");
    System.loadLibrary("PrivateCrypto");
    System.loadLibrary("PrivateSecurity");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...