NDK开发 - 使用GMSSL库和OpenSSL库的注意点及编译
NDK开发 - 使用GMSSL库和OpenSSL库的注意点及编译

2019.08.05 11:17:00字数 591阅读 271
前言
- GmSSL是一个开源的密码工具箱,支持SM2/SM3/SM4/SM9/ZUC等国密(国家商用密码)算法、SM2国密数字证书及基于SM2证书的SSL/TLS安全通信协议,支持国密硬件密码设备,提供符合国密规范的编程接口与命令行工具,可以用于构建PKI/CA、安全通信、数据加密等符合国密标准的安全应用。GmSSL项目是OpenSSL项目的分支,并与OpenSSL保持接口兼容。因此GmSSL可以替代应用中的OpenSSL组件,并使应用自动具备基于国密的安全能力。GmSSL项目采用对商业应用友好的类BSD开源许可证,开源且可以用于闭源的商业应用。
- GmSSL项目由北京大学关志副研究员的密码学研究组开发维护,项目源码托管于GitHub。
编译
一、OpenSSL编译
1、参考https://github.com/leenjewel/openssl_for_ios_and_android。
2、使用NDK 14来编译。
3、32位的库可以使用Android 16来编译。
4、64位的库必须使用大于等于Android 21来编译。
5、不同OpenSSL版本,头文件可能不同。
PS:使用上面链接下载的脚本可在_shared.sh中修改Android API版本,在build-openssl4Android.sh中可以填写OpenSSL版本。
二、GMSSL编译
1、参考https://github.com/wangp8895/gmssl-for-android。
2、使用NDK 14来编译。
3、32位的库可以使用Android 16来编译。
4、64位的库必须使用大于等于Android 21来编译。
5、不同GMSSL版本,头文件可能不同。
PS:使用上面链接下载的脚本可在_shared.sh中修改Android API版本,在build-openssl4Android.sh中可以填写GMSSL版本。
使用
- 环境
1、Android Studio 3.1.2
2、NDK 16
3、minSDKVersion 16 编译32位。
4、minSDKVersion 21 编译64位。
5、使用CMake。
- 编写CMakeLists文件
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) #设置JNI变量名的值为${PROJECT_SOURCE_DIR}/src/main/jni #Mac set(JNI /${PROJECT_SOURCE_DIR}/src/main/jni) #windows #set(JNI ${PROJECT_SOURCE_DIR}/src/main/jni) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. nativeLib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/jni/nativeTest.cpp) #静态方式加载 add_library(crypto STATIC IMPORTED ) #配置加载头文件 include_directories( ${JNI}/include) #引入第三方.a库 set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${JNI}/libs/${ANDROID_ABI}/libcrypto.a) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. nativeLib crypto -lz # Links the target library to the log library # included in the NDK. ${log-lib})

image.png
如果遇到下面的错误,在CMakeLists中的target_link_libraries中加入 -lz 就可以解决。

image.png
注意OpenSSL的版本需要和OpenSSL的头文件版本一致,在OpenSSL头文件的opensslv.h中可以看到OpenSSL头文件的版本。

image.png
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?