• Home
Name Date Size #Lines LOC

..--

autosign/06-May-2025-893673

dist/06-May-2025-221211

figures/06-May-2025-

hapsigntool/06-May-2025-27,65115,732

hapsigntool_cpp/06-May-2025-25,75119,619

hapsigntool_cpp_test/06-May-2025-45,39930,635

tools/06-May-2025-1,2631,163

.gitignoreD06-May-202569 97

BUILD.gnD06-May-20251.1 KiB3229

LICENSED06-May-202511.1 KiB203169

NOTICED06-May-202537.2 KiB726601

OAT.xmlD06-May-20254.8 KiB8164

README.mdD06-May-202522.8 KiB330255

README_ZH.mdD06-May-202522.8 KiB362280

README.md

1# hapsigner
2
3## Introduction
4
5To ensure that all apps and binary tools (such as lldb-server) come from a known and approved source and have not been tampered with, OpenHarmony requires that all executable code be signed. Only signed apps and binary tools can be installed, run, and debugged on real devices.
6
7The repository provides the source code of the signing tool named hapsigner, which provides the functions such as generating a key pair, a certificate signing request (CSR), or a certificate, and signing a profile, a Harmony Ability Package (HAP), or a binary tool.
8The mandatory code signing mechanism provides validity check and integrity protection for apps in runtime, eliminating execution of malicious code on devices and malicious tampering of app code by attackers.
9
10Code signing is enabled by default for hapsigner. If you do not need the mandatory code signing feature, you can disable it as required. Currently, hapsigner supports code signing only for apps in hap format and binary tools.
11
12
13## Directory Structure
14
15    developtools_hapsigner
16
17    ├── autosign                # Script for one-click signing.
18    ├── dist                    # SDK preconfigured file.
19    ├── hapsigntool             # Code of the hapsigner tool.
20          ├──hap_sign_tool      # Entry of the hapsigner tool, used to verify input parameters.
21          ├──hap_sign_tool_lib  # Lib of the hapsigner tool, used to parse command words and parameter lists to implement logic of modules.
22    ├── tools                   # Auto-test script.
23
24
25
26## Constraints
27- The hapsigner tool is developed in Java and must run in JRE 8.0 or later.
28- The scripts, such as the one-click signing script, are developed in Python, and must run on Python 3.5 or later.
29
30## Build
31
32 1. Check that Maven 3 has been installed.
33
34        mvn -version
35
36 2. Download the code, open the directory **developtools_hapsigner/hapsigntool**, and run the following command to build the code:
37
38        mvn package
39
40 3. Check that **hap-sign-tool.jar** (binary files) is generated in the **./hap_sign_tool/target** directory.
41
42
43## Usage
44### Files Related to Signing
45
46When signing an app using the IDE, you will obtain the following files from the SDK:
47
48```
49KeyStore (KS) file: OpenHarmony.p12
50Profile signing certificates: OpenHarmonyProfileRelease.pem and OpenHarmonyProfileDebug.pem
51Profile templates: UnsgnedReleasedProfileTemplate.json and UnsgnedDebugProfileTemplate.json
52Signing tool: hap-sign-tool.jar
53```
54The figures below illustrate how these files are used.
55
56**Signing a Profile**
57
58![signprofile.png](figures/signprofile_en.png)
59
60**Signing an App**
61
62![signapp.png](figures/signapp_en.png)
63### Usage Guidelines
64
65In the following, the .jar package is the binary files built.
66
67#### Using Commands
68You can use commands to sign a profile and a HAP or binary tool.
69
701. Sign a profile.
71
72
73```shell
74java -jar hap-sign-tool.jar  sign-profile -keyAlias "oh-profile1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "result\profile1.pem" -inFile "app1-profile-release.json" -keystoreFile "result\ohtest.jks" -outFile "result\app1-profile.p7b" -keyPwd "123456" -keystorePwd "123456"
75```
76The parameters in the command are described as follows:
77
78    sign-profile: Sign a provisioning profile.
79         ├── -mode              # Signing mode, which can be localSign or remoteSign. It is mandatory.
80         ├── -keyAlias          # Key alias. It is mandatory.
81         ├── -keyPwd            # Key password. It is optional.
82         ├── -profileCertFile   # Profile signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
83         ├── -inFile            # Provisioning profile to be signed. It is mandatory.
84         ├── -signAlg           # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
85         ├── -keystoreFile      # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
86         ├── -keystorePwd       # KS password. It is optional.
87         ├── -outFile           # Signed provisioning profile to generate, in p7b format. It is mandatory.
88
89
90
912. Sign a HAP or binary tool.
92
93
94```shell
95java -jar hap-sign-tool.jar sign-app -keyAlias "oh-app1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "result\app1.pem" -profileFile "result\app1-profile.p7b" -inFile "app1-unsigned.zip" -keystoreFile "result\ohtest.jks" -outFile "result\app1-unsigned.hap" -keyPwd "123456" -keystorePwd "123456" -signCode "1"
96```
97The parameters in the command are described as follows:
98
99    sign-app: sign a HAP or binary tool
100         ├── -mode              # Signing mode, which can be localSign or remoteSign. It is mandatory.
101         ├── -keyAlias          # Key alias. It is mandatory.
102         ├── -keyPwd            # Key password. It is optional.
103         ├── -appCertFile       # App signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
104         ├── -profileFile       # Signed provisioning profile in p7b format. This parameter is mandatory for a HAP and optional for a binary tool.
105         ├── -profileSigned     # Whether the profile is signed. The value 1 means the profile is signed, and the value 0 means the opposite. The default value is 1. This parameter is optional.
106         ├── -inForm            # Format of the file to be signed. The value can be zip, elf, or bin. It is zip for a HAP, elf for a binary tool, and bin for a program running on the small system. In case of code signing, it can be zip or elf. The default value is zip. This parameter is optional.
107         ├── -inFile            # File to be signed, which can be a HAP or an ELF or bin file. This parameter is mandatory.
108         ├── -signAlg           # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
109         ├── -keystoreFile      # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
110         ├── -keystorePwd       # KS password. It is optional.
111         ├── -outFile           # Signed HAP to generate. It is mandatory.
112         ├── -signCode          # Whether to enable code signing. The value 1 means to enable code signing; the value 0 means the opposite. The default value is 1. This parameter is optional.
113
114#### Performing One-Click Signing
115
116
117To improve development efficiency, this project also provides scripts for one-click signing. You can use the scripts to easily generate a key pair or an end-entity certificate and sign a profile, HAP, or binary tool without entering complex commands.
118The following scripts and configuration files are located in the **autosign** directory:
119
120 - create_root.sh/create_root.bat
121 - create_appcert_sign_profile.sh/create_appcert_sign_profile.bat
122 - sign_hap.sh/sign_hap.bat
123 - sign_elf.sh/sign_elf.bat
124 - createAppCertAndProfile.config
125 - createRootAndSubCert.config
126 - signHap.config
127 - signElf.config
128
129**Procedure**
1301. Check that Python 3.5 or later is available.
1312. Obtain **hap-sign-tool.jar**. For details, see section **Build**.
1323. Check that the HAP, binary tool, or provisioning profile to be signed is available.
1334. Use the text editor to open **createAppCertAndProfile.config**, **signElf.config**, and **signHap.config** and change the values of **common.keyPwd** and **common.issuerKeyPwd** to match your case.
1345. Run **create_appcert_sign_profile.sh** on Linux or **create_appcert_sign_profile.bat** on Windows to generate the files required for signing.
1356. Run **sign_hap.sh** on Linux or **sign_hap.bat** on Windows to sign the HAP. Run **sign_elf.sh** on Linux or **sign_elf.bat** on Windows to sign the binary tool.
136
137 > **NOTE**
138 >
139 > To generate a KS file, root CA certificate, intermediate CA certificate, and profile signing certificate, perform the following steps:
140 1. Use the text editor to open the **createRootAndSubCert.config** file and change the values of **common.keyPwd** and **common.issuerKeyPwd** to match your case.
141 2. Run **create_root.sh** on Linux or run **create_root.bat** on Windows to generate the required KS file, root CA certificate, intermediate CA certificate, and profile signing certificate.
142
143****
144### Common Operations
1451. Generate a key pair.
146
147     ```
148     generate-keypair: Generate a key pair.
149         ├── -keyAlias          # Key alias. It is mandatory.
150         ├── -keyPwd            # Key password. It is optional.
151         ├── -keyAlg            # Key algorithm, which can be RSA or ECC. It is mandatory.
152         ├── -keySize           # Key length. It is mandatory. The key length is 2048, 3072, or 4096 bits if RSA is used and is NIST-P-256 or NIST-P-384 if ECC is used.
153         ├── -keystoreFile      # KS file, in JKS or P12 format. It is mandatory.
154         ├── -keystorePwd       # KS password. It is optional.
155     ```
156
157
1582. Generate a CSR.
159
160    ```
161    generate-csr: Generate a CSR.
162         ├── -keyAlias          # Key alias. It is mandatory.
163         ├── -keyPwd            # Key password. It is optional.
164         ├── -subject           # Certificate subject. It is mandatory.
165         ├── -signAlg           # Signing algorithm, which can be SHA256withRSA, SHA384withRSA, SHA256withECDSA, or SHA384withECDSA. It is mandatory.
166         ├── -keystoreFile      # KS file, in JKS or P12 format. It is mandatory.
167         ├── -keystorePwd       # KS password. It is optional.
168         ├── -outFile           # CSR to generate. It is optional. If you do not specify this parameter, the CSR is output to the console.
169    ```
170
171
1723. Generate a root CA or intermediate CA certificate.
173
174    ```
175    generate-ca: Generate a root CA or intermediate CA certificate. If the key does not exist, generate a key together with the certificate.
176         ├── -keyAlias                  # Key alias. It is mandatory.
177         ├── -keyPwd                    # Key password. It is optional.
178         ├── -keyAlg                    # Key algorithm, which can be RSA or ECC. It is mandatory.
179         ├── -keySize                   # Key length. It is mandatory. The key length is 2048, 3072, or 4096 bits if RSA is used and is NIST-P-256 or NIST-P-384 if ECC is used.
180         ├── -issuer                    # Issuer of the certificate. It is optional. It indicates a root CA certificate if not specified.
181         ├── -issuerKeyAlias            # Key alias of the issuer. It is optional. It indicates a root CA certificate if not specified.
182         ├── -issuerKeyPwd              # Key password of the issuer. It is optional.
183         ├── -subject                   # Certificate subject. It is mandatory.
184         ├── -validity                  # Validity period of the certificate. It is optional. The default value is 3650 days.
185         ├── -signAlg                   # Signing algorithm, which can be SHA256withRSA, SHA384withRSA,  SHA256withECDSA, or SHA384withECDSA. It is mandatory.
186         ├── -basicConstraintsPathLen   # Path length. It is optional. The default value is 0.
187         ├── -issuerKeystoreFile        # KS file of the issuer, in JKS or P12 format. It is optional.
188         ├── -issuerKeystorePwd         # KS password of the issuer. It is optional.
189         ├── -keystoreFile              # KS file, in JKS or P12 format. It is mandatory.
190         ├── -keystorePwd               # KS password. It is optional.
191         ├── -outFile                   # File to generate. It is optional. The file is output to the console if this parameter is not specified.
192    ```
193
194
1954. Generate an app debug or release certificate.
196
197    ```
198    generate-app-cert: Generate an app debug or release certificate.
199         ├── -keyAlias                        # Key alias. It is mandatory.
200         ├── -keyPwd                          # Key password. It is optional.
201         ├── -issuer                          # Issuer of the certificate. It is mandatory.
202         ├── -issuerKeyAlias                  # Key alias of the issuer. It is mandatory.
203         ├── -issuerKeyPwd                    # Key password of the issuer. It is optional.
204         ├── -subject                         # Certificate subject. It is mandatory.
205         ├── -validity                        # Validity period of the certificate. It is optional. The default value is 3650 days.
206         ├── -signAlg                         # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA.
207         ├── -keystoreFile                    # KS file, in JKS or P12 format. It is mandatory.
208         ├── -keystorePwd                     # KS password. It is optional.
209         ├── -issuerKeystoreFile              # KS file of the issuer, in JKS or P12 format. It is optional.
210         ├── -issuerKeystorePwd               # KS password of the issuer. It is optional.
211         ├── -outForm                         # Format of the certificate to generate. It is optional. The value can be cert or certChain. The default value is certChain.
212         ├── -rootCaCertFile                  # Root CA certificate, which is mandatory when outForm is certChain.
213         ├── -subCaCertFile                   # Intermediate CA certificate file, which is mandatory when outForm is certChain.
214         ├── -outFile                         # Certificate file (certificate or certificate chain) to generate. It is optional. The file is output to the console if this parameter is not specified.
215    ```
216
217
2185. Generate a profile debug or release certificate.
219
220    ```
221    generate-profile-cert: Generate a profile debug or release certificate.
222         ├── -keyAlias                        # Key alias. It is mandatory.
223         ├── -keyPwd                          # Key password. It is optional.
224         ├── -issuer                          # Issuer of the certificate. It is mandatory.
225         ├── -issuerKeyAlias                  # Key alias of the issuer. It is mandatory.
226         ├── -issuerKeyPwd                    # Key password of the issuer. It is optional.
227         ├── -subject                         # Certificate subject. It is mandatory.
228         ├── -validity                        # Validity period of the certificate. It is optional. The default value is 3650 days.
229         ├── -signAlg                         # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA.
230         ├── -keystoreFile                    # KS file, in JKS or P12 format. It is mandatory.
231         ├── -keystorePwd                     # KS password. It is optional.
232         ├── -issuerKeystoreFile              # KS file of the issuer, in JKS or P12 format. It is optional.
233         ├── -issuerKeystorePwd               # KS password of the issuer. It is optional.
234         ├── -outForm                         # Format of the certificate to generate. It is optional. The value can be cert or certChain. The default value is certChain.
235         ├── -rootCaCertFile                  # Root CA certificate, which is mandatory when outForm is certChain.
236         ├── -subCaCertFile                   # Intermediate CA certificate file, which is mandatory when outForm is certChain.
237         ├── -outFile                         # Certificate file (certificate or certificate chain) to generate. It is optional. The file is output to the console if this parameter is not specified.
238    ```
239
240
2416. Generate a common certificate, which can be used to generate a custom certificate.
242
243    ```
244    generate-cert: Generate a common certificate, which can be used to generate a custom certificate.
245          ├── -keyAlias                         # Key alias. It is mandatory.
246          ├── -keyPwd                           # Key password. It is optional.
247          ├── -issuer                           # Issuer of the certificate. It is mandatory.
248          ├── -issuerKeyAlias                   # Key alias of the issuer. It is mandatory.
249          ├── -issuerKeyPwd                     # Key password of the issuer. It is optional.
250          ├── -subject                          # Certificate subject. It is mandatory.
251          ├── -validity                         # Validity period of the certificate. It is optional. The default value is 1095 days.
252          ├── -keyUsage                         # Usages of the key. It is mandatory. The key usages include digitalSignature, nonRepudiation, keyEncipherment,
253          ├                                        dataEncipherment, keyAgreement, certificateSignature, crlSignature,
254          ├                                        encipherOnly, and decipherOnly. Use a comma (,) to separate multiple values.
255          ├── -keyUsageCritical                 # Whether keyUsage is a critical option. It is optional. The default value is true.
256          ├── -extKeyUsage                      # Extended key usages. It is optional. The extended key usages include clientAuthentication, serverAuthentication,
257          ├                                        codeSignature, emailProtection, smartCardLogin, timestamp, and ocspSignature.
258          ├── -extKeyUsageCritical              # Whether extKeyUsage is a critical option. It is optional. The default value is false.
259          ├── -signAlg                          # Signing algorithm, which can be SHA256withRSA, SHA384withRSA,  SHA256withECDSA, or SHA384withECDSA. It is mandatory.
260          ├── -basicConstraints                 # Whether basicConstraints is contained. It is optional. The default value is false.
261          ├── -basicConstraintsCritical         # Whether basicConstraints is a critical option. It is optional. The default value is false.
262          ├── -basicConstraintsCa               # Whether it is a CA. It is optional. The default value is false.
263          ├── -basicConstraintsPathLen          # Path length. It is optional. The default value is 0.
264          ├── -issuerKeystoreFile               # KS file of the issuer, in JKS or P12 format. It is optional.
265          ├── -issuerKeystorePwd                # KS password of the issuer. It is optional.
266          ├── -keystoreFile                     # KS file, in JKS or P12 format. It is mandatory.
267          ├── -keystorePwd                      # KS password. It is optional.
268          ├── -outFile                          # Certificate file to generate. It is optional. The file is output to the console if this parameter is not specified.
269    ```
270
271
2727. Sign a provisioning profile.
273
274    ```
275    sign-profile: Sign a provisioning profile.
276          ├── -mode            # Signing mode, which can be localSign or remoteSign. It is mandatory.
277          ├── -keyAlias        # Key alias. It is mandatory.
278          ├── -keyPwd          # Key password. It is optional.
279          ├── -profileCertFile # Profile signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
280          ├── -inFile          # Provisioning profile to be signed. It is mandatory.
281          ├── -signAlg         # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
282          ├── -keystoreFile    # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
283          ├── -keystorePwd     # KS password. It is optional.
284          ├── -outFile         # Signed provisioning profile to generate, in p7b format. It is mandatory.
285    ```
286
287
2888. Verify the provisioning profile signature.
289
290     ```
291     verify-profile: Verify the provisioning profile signature.
292           ├── -inFile       # Signed provisioning profile, in p7b format. It is mandatory.
293           ├── -outFile      # Verification result file (including the verification result and profile content), in json format. It is optional. The verification result is output to the console if this parameter is not specified.
294     ```
295
296
2979. Sign a HAP or binary tool
298
299     ```
300     sign-app: sign a HAP or binary tool
301          ├── -mode          # Signing mode, which can be localSign, remoteSign, or remoteResign. It is mandatory.
302          ├── -keyAlias      # Key alias. It is mandatory.
303          ├── -keyPwd        # Key password. It is optional.
304          ├── -appCertFile   # App signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
305          ├── -profileFile   # Name of the signed provisioning profile. When profileSigned is 1, the file is in p7b format. When profileSigned is 0, the file is in JSON format. This parameter is mandatory for a HAP and optional for a binary tool.
306          ├── -profileSigned # Whether the profile is signed. The value 1 means the profile is signed, and the value 0 means the opposite. The default value is 1. This parameter is optional.
307          ├── -inForm        # Format of the file to be signed. The value can be zip, elf, or bin. It is zip for a HAP, elf for a binary tool, and bin for a program running on the small system. In case of code signing, it can be zip or elf. The default value is zip. This parameter is optional.
308          ├── -inFile        # File to be signed, which can be a HAP or an ELF or bin file. This parameter is mandatory.
309          ├── -signAlg       # Signing algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
310          ├── -keystoreFile  # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
311          ├── -keystorePwd   # KS password. It is optional.
312          ├── -outFile       # Signed HAP to generate. It is mandatory.
313          ├── -signCode      # Whether to enable code signing. The value 1 means to enable code signing; the value 0 means the opposite. The default value is 1. This parameter is optional.
314     ```
315
316
31710. Verify the signature of a HAP or a binary tool.
318
319    ```
320      verify-app: verify the signature of a HAP or a binary tool.
321         ├── -inFile          # Signed file, which can be a HAP, an ELF file, or a bin file. This parameter is mandatory.
322         ├── -outCertchain    # Signed certificate chain file. It is mandatory.
323         ├── -outProfile      # Profile of the app. It is mandatory.
324         ├── -inForm          # Format of the file to be signed. The value can be zip, elf, or bin. It is zip for a HAP, elf for a binary tool, and bin for a program running on the small system. In case of code signing, it can be zip or elf. The default value is zip. This parameter is optional.
325    ```
326
327
328## Repositories Involved
329   N/A
330

README_ZH.md

1# 应用包签名工具
2
3* 简介
4* 目录
5* 约束
6* 编译构建
7* 说明
8  * 签名相关文件用法说明
9  * 使用说明
10  * 接口说明
11* 相关仓
12
13#### 简介
14
15为了保证OpenHarmony应用和二进制工具(如:lldb-server)的完整性和来源可靠,需要对应用和二进制工具进行签名。经过签名的应用和二进制工具才能在真机设备上安装、运行和调试。本仓提供了签名工具的源码,包含密钥对生成、CSR文件生成、证书生成、Profile文件签名、应用包签名、二进制工具签名等功能。
16在支持强制代码签名机制的设备上,该机制可以为应用提供运行时的合法性校验以及完整性保护,杜绝未经审核的恶意代码在端侧任意执行,或应用代码被攻击者恶意篡改。
17签名工具默认开启代码签名,若用户确定不需要强制执行代码签名,可参考以下说明,关闭代码签名功能。签名工具当前仅支持对hap、hsp、hqf格式的应用包和二进制工具执行代码签名。
18
19
20#### 目录
21
22    developtools_hapsigner
23
24    ├── autosign                 # 一键签名脚本
25    ├── dist                     # SDK预置文件
26    ├── hapsigntool              # java版工具根目录
27          ├──hap_sign_tool       # 主程序入口,完成输入参数的基础校验
28          ├──hap_sign_tool_lib   # 签名工具库,解析命令字和参数列表,实现各模块逻辑功能
29    ├── tools                    # 自动化测试脚本
30    ├── hapsigntool_cpp          # c++签名工具根目录
31          ├── api                # 签名接口api
32          ├── cmd                # 命令行输入
33          ├── codesigning        # 代码签名模块
34          ├── common             # 公共类
35          ├── hap                # hap包签名验签
36          ├── main.cpp           # 程序执行入口
37          ├── profile            # profile签名验签
38          ├── signer             # 签名工厂,支持本地和远程签名
39          ├── utils              # 工具类
40          ├── zip                # hap包解压缩,获取entry
41          ├── service            # 代码签名模块
42    ├── hapsigntool_cpp_test     # 存放C++版工具的测试文件
43          ├── fuzztest           # fuzz测试用例
44          ├── unittest           # unit测试用例
45
46
47
48#### 约束
49
50**java版本约束**
51
52- 签名工具基于Java语言开发,需要在Java8以上Java环境运行。
53- 一键签名等脚本文件基于Python语言开发,使用需配置环境python3.5及以上。
54
55**c++版本约束**
56
57+ 该工具基于openharmony标准系统编译构建ohos-sdk形态,使用前先配置openharmony开发环境,并使用C++17及以上语言标准。
58
59#### 编译构建
60
61**java版本编译流程**
62
63 1. 该工具基于Maven3编译构建,请确认环境已安装配置Maven3环境,并且版本正确
64
65        mvn -version
66
67 2. 下载代码,命令行打开文件目录至developtools_hapsigner/hapsigntool,执行命令进行编译打包
68
69        mvn package
70
71 3. 编译后得到二进制文件,目录为: ./hap_sign_tool/target
72
73**c++版本编译流程**
74
751. 编译ohos-sdk形态签名工具
76   + 编译**release**版本:默认是release版本,直接编译即可。
77   + 编译**debug**版本(增加了调试日志):在 hapsigntool_cpp/BUILD.gn 中 添加 defines = [ "SIGNATURE_LOG_DEBUG" ] 即可。
78
792. 编译命令:./build.sh --product-name ohos-sdk
80
813. 编译产物路径:/openharmony_master/out/sdk/packages/ohos-sdk/ohos
82
83**特别说明**:
84
851.使用c++版本签名工具时,仅支持**ECC**密钥算法,不支持RSA。
862.在java版本签名工具中支持PKCS#12和JKS两种密钥库格式,C++工具仅支持PKCS#12密钥库格式。支持的密钥库文件后缀为.p12或.jks。
87
88****
89#### 说明
90##### 签名相关文件用法说明
91
92开发者通过IDE进行应用签名时,可在SDK中会获得如下签名相关文件:
93
94```
95签名密钥库文件:OpenHarmony.p12
96Profile签名证书:OpenHarmonyProfileRelease.pemOpenHarmonyProfileDebug.pem
97Profile模板文件:UnsgnedReleasedProfileTemplate.jsonUnsgnedDebugProfileTemplate.json
98签名工具:hap-sign-tool.jar
99```
100上述文件的使用场景如下图所示。
101
102**Profile签名场景:**
103
104![signprofile.png](figures/signprofile_zh.png)
105
106**应用签名场景:**
107
108![signapp.png](figures/signapp_zh.png)
109##### 使用说明
110
111以下说明中使用jar包为java编译构建中生成的二进制文件,可执行程序hap-sign-tool为c++编译构建中生成的二进制文件
112
1131.命令行签名
114   命令行签名分为profile文件签名和应用包或二进制工具签名。
115
116   (1)签名profile文件的命令实例如下:
117
118​      **java:**
119
120
121```shell
122java -jar hap-sign-tool.jar  sign-profile -keyAlias "oh-profile1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "result\profile1.pem" -inFile "app1-profile-release.json" -keystoreFile "result\ohtest.jks" -outFile "result\app1-profile.p7b" -keyPwd "123456" -keystorePwd "123456"
123```
124
125​      **c++:**
126
127```shell
128hap-sign-tool sign-profile -keyAlias "oh-profile1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "result\profile1.pem" -inFile "app1-profile-release.json" -keystoreFile "result\ohtest.p12" -outFile "result\app1-profile.p7b" -keyPwd "123456" -keystorePwd "123456"
129```
130
131上述命令的参数说明如下:
132
133    sign-profile : ProvisionProfile文件签名
134         ├── -mode              #签名模式,必填项,包括localSign,remoteSign
135         ├── -keyAlias          #密钥别名,必填项, 不区分大小写
136         ├── -keyPwd            #密钥口令,可选项
137         ├── -profileCertFile   #Profile签名证书(证书链,顺序为实体证书-中间CA证书-根证书),必填项
138         ├── -inFile            #输入的原始Provision Profile文件,必填项
139         ├── -signAlg           #签名算法,必填项,包括 SHA256withECDSA / SHA384withECDSA
140         ├── -keystoreFile      #密钥库文件,localSign模式时为必填项
141         ├── -keystorePwd       #密钥库口令,可选项
142         ├── -outFile           #输出签名后的Provision Profile文件,p7b格式,必填项
143
144
145
146(2)签名应用包或二进制工具的命令实例如下:
147
148​      **java:**
149
150```shell
151java -jar hap-sign-tool.jar sign-app -keyAlias "oh-app1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "result\app1.pem" -profileFile "result\app1-profile.p7b" -inFile "app1-unsigned.zip" -keystoreFile "result\ohtest.jks" -outFile "result\app1-unsigned.hap" -keyPwd "123456" -keystorePwd "123456" -signCode "1"
152```
153
154​      **c++:**
155
156```shell
157hap-sign-tool sign-app -keyAlias "oh-app1-key-v1" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "result\app1.pem" -profileFile "result\app1-profile.p7b" -inFile "app1-unsigned.zip" -keystoreFile "result\ohtest.p12" -outFile "result\app1-unsigned.hap" -keyPwd "123456" -keystorePwd "123456" -signCode "1"
158```
159
160上述命令的参数说明如下:
161
162    sign-app : 应用包和二进制工具签名
163         ├── -mode              #签名模式,必填项,包括localSign,remoteSign
164         ├── -keyAlias          #密钥别名,必填项,不区分大小写
165         ├── -keyPwd            #密钥口令,可选项
166         ├── -appCertFile       #应用签名证书文件(证书链,顺序为实体证书-中间CA证书-根证书),必填项
167         ├── -profileFile       #签名后的Provision Profile文件名,p7b格式,应用包签名必填项,二进制工具签名选填
168         ├── -profileSigned     #指示profile文件是否带有签名,1表示有签名,0表示没有签名,默认为1。可选项
169         ├── -inForm            #输入的原始文件的格式,枚举值:zip、elf或bin;zip应用包对应zip,二进制工具对应elf,bin应用包为bin,默认zip;可选项
170         ├── -inFile            #输入的原始文件,应用包、elf或bin文件,必填项
171         ├── -signAlg           #签名算法,必填项,包括SHA256withECDSA / SHA384withECDSA
172         ├── -keystoreFile      #密钥库文件,localSign模式时为必填项
173         ├── -keystorePwd       #密钥库口令,可选项
174         ├── -outFile           #输出签名后的包文件,必填项
175         ├── -signCode          #是否启用代码签名,1表示开启代码签名,0表示关闭代码签名,可选项。默认对hap、hsp、hqf、elf开启代码签名,通过参数配置为0关闭。
176
1772.一键签名
178
179
180为降低学习成本,提高开发效率,本项目还将基于应用签名工具提供一键签名脚本,免于输入繁杂的参数命令,脚本内容包括生成密钥对、实体证书、签名profile、签名应用包和二进制工具的命令。
181脚本以及配置文件位于目录 autosign 下:
182
183 - create_root.sh/create_root.bat
184 - create_appcert_sign_profile.sh/create_appcert_sign_profile.bat
185 - sign_hap.sh/sign_hap.bat
186 - sign_elf.sh/sign_elf.bat
187 - createAppCertAndProfile.config
188 - createRootAndSubCert.config
189 - signHap.config
190 - signElf.config
191
192使用指导:
1931. 准备依赖环境 python3.5 以上
1942. 准备签名工具jar包:hap-sign-tool.jar(参照上文编译生成的产物)
1953. 准备待签名的应用应用包、二进制工具和 Provision profile 模板文件
1964. 使用文本编辑器编辑 createAppCertAndProfile.configsignElf.configsignHap.config,修改配置文件中的配置信息:common.keyPwdcommon.issuerKeyPwd 参数值改成自己定义的口令信息
1975. Linux运行 create_appcert_sign_profile.sh、Windows运行 create_appcert_sign_profile.bat 生成签名所需文件
1986. Linux运行 sign_hap.sh、Windows运行 sign_hap.bat 对应用包进行签名;Linux运行 sign_elf.sh、Windows运行 sign_elf.bat 对二进制工具进行签名
199
200 > 说明:如需自定义生成密钥库文件,根CA,中间CA证书,profile签名证书,可执行以下步骤:
201 1.使用文本编辑器编辑 createRootAndSubCert.config 修改配置文件中的配置信息:common.keyPwdcommon.issuerKeyPwd 参数值改成自己定义的口令信息
202 2.Linux运行 create_root.sh、Windows运行 create_root.bat 生成所需密钥库文件,根CA,中间CA证书,profile签名证书
203
204
205****
206##### 接口说明
2071.生成密钥对
208
209     generate-keypair : 生成密钥对
210         ├── -keyAlias          # 密钥别名,必填项,不区分大小写
211         ├── -keyPwd            # 密钥口令,可选项
212         ├── -keyAlg            # 密钥算法,必填项,包括RSA/ECC
213         ├── -keySize           # 密钥长度,必填项,RSA算法的长度为2048/3072/4096,ECC算法的长度NIST-P-256/NIST-P-384
214         ├── -keystoreFile      # 密钥库文件,必填项
215         ├── -keystorePwd       # 密钥库口令,可选项
216
2172.生成证书签名请求
218
219    generate-csr : 生成证书签名请求
220         ├── -keyAlias          # 密钥别名,必填项,不区分大小写
221         ├── -keyPwd            # 密钥口令,可选项
222         ├── -subject           # 证书主题,必填项
223         ├── -signAlg           # 签名算法,必填项,包括SHA256withRSA / SHA384withRSA / SHA256withECDSA / SHA384withECDSA
224         ├── -keystoreFile      # 密钥库文件,必填项
225         ├── -keystorePwd       # 密钥库口令,可选项
226         ├── -outFile           # 输出文件,可选项,如果不填,则直接输出到控制台
227
2283.生成根CA/中间CA证书
229
230    generate-ca : 生成根CA/中间CA证书,如果密钥不存在,一起生成密钥
231         ├── -keyAlias                        # 密钥别名,必填项,不区分大小写
232         ├── -keyPwd                          # 密钥口令,可选项
233         ├── -keyAlg                          # 密钥算法,必填项,包括RSA/ECC
234         ├── -keySize                         # 密钥长度,必填项,RSA算法的长度为2048/3072/4096,ECC算法的长度NIST-P-256/NIST-P-384
235         ├── -issuer                          # 颁发者的主题,可选项,如果不填,表示根CA
236         ├── -issuerKeyAlias                  # 颁发者的密钥别名,可选项,如果不填,表示根CA,不区分大小写
237         ├── -issuerKeyPwd                    # 颁发者的密钥口令,可选项
238         ├── -subject                         # 证书主题,必填项
239         ├── -validity                        # 证书有效期,可选项,默认为3650天
240         ├── -signAlg                         # 签名算法,必填项,包括SHA256withRSA / SHA384withRSA / SHA256withECDSA / SHA384withECDSA
241         ├── -basicConstraintsPathLen         # 路径长度,可选项,默认为0
242         ├── -issuerKeystoreFile              # 签发者密钥库文件,可选项
243         ├── -issuerKeystorePwd               # 签发者密钥库口令,可选项
244         ├── -keystoreFile                    # 密钥库文件,必填项
245         ├── -keystorePwd                     # 密钥库口令,可选项
246         ├── -outFile                         # 输出文件,可选项,如果不填,则直接输出到控制台
247
2484.生成应用调试/发布证书
249
250    generate-app-cert : 生成应用调试/发布证书
251         ├── -keyAlias                        # 密钥别名,必填项,不区分大小写
252         ├── -keyPwd                          # 密钥口令,可选项
253         ├── -issuer                          # 颁发者的主题,必填项
254         ├── -issuerKeyAlias                  # 颁发者的密钥别名,必填项,不区分大小写
255         ├── -issuerKeyPwd                    # 颁发者的密钥口令,可选项
256         ├── -subject                         # 证书主题,必填项
257         ├── -validity                        # 证书有效期,可选项,默认为3650天
258         ├── -signAlg                         # 签名算法,必填项,包括SHA256withECDSA / SHA384withECDSA;
259         ├── -keystoreFile                    # 密钥库文件,必填项
260         ├── -keystorePwd                     # 密钥库口令,可选项
261         ├── -issuerKeystoreFile              # 签发者密钥库文件,可选项
262         ├── -issuerKeystorePwd               # 签发者密钥库口令,可选项
263         ├── -outForm                         # 输出证书文件的格式,包括 cert / certChain,可选项,默认为certChain
264         ├── -rootCaCertFile                  #  outForm为certChain时必填,根CA证书文件
265         ├── -subCaCertFile                   #  outForm为certChain时必填,中间CA证书文件
266         ├── -outFile                         #  输出证书文件(证书或证书链),可选项,如果不填,则直接输出到控制台
267
2685.生成profile调试/发布证书
269
270    generate-profile-cert : 生成profile调试/发布证书
271         ├── -keyAlias                        # 密钥别名,必填项,不区分大小写
272         ├── -keyPwd                          # 密钥口令,可选项
273         ├── -issuer                          # 颁发者的主题,必填项
274         ├── -issuerKeyAlias                  # 颁发者的密钥别名,必填项,不区分大小写
275         ├── -issuerKeyPwd                    # 颁发者的密钥口令,可选项
276         ├── -subject                         # 证书主题,必填项
277         ├── -validity                        # 证书有效期,可选项,默认为3650天
278         ├── -signAlg                         # 签名算法,必填项,包括SHA256withECDSA / SHA384withECDSA;
279         ├── -keystoreFile                    # 密钥库文件,必填项
280         ├── -keystorePwd                     # 密钥库口令,可选项
281         ├── -issuerKeystoreFile              # 签发者密钥库文件,可选项
282         ├── -issuerKeystorePwd               # 签发者密钥库口令,可选项
283         ├── -outForm                         # 输出证书文件的格式,包括 cert / certChain,可选项,默认为certChain
284         ├── -rootCaCertFile                  #  outForm为certChain时必填,根CA证书文件
285         ├── -subCaCertFile                   #  outForm为certChain时必填,中间CA证书文件
286         ├── -outFile                         #  输出证书文件(证书或证书链),可选项,如果不填,则直接输出到控制台
287
2886.通用证书生成,可以生成自定义证书
289
290    generate-cert : 通用证书生成,可以生成自定义证书
291          ├── -keyAlias                          # 密钥别名,必填项,不区分大小写
292          ├── -keyPwd                            # 密钥口令,可选项
293          ├── -issuer                            # 颁发者的主题,必填项
294          ├── -issuerKeyAlias                    # 颁发者的密钥别名,必填项,不区分大小写
295          ├── -issuerKeyPwd                      # 颁发者的密钥口令,可选项
296          ├── -subject                           # 证书主题,必填项
297          ├── -validity                          # 证书有效期,可选项,默认为1095天
298          ├── -keyUsage                          # 密钥用法,必选项,包括digitalSignature, nonRepudiation, keyEncipherment,
299          ├                                        dataEncipherment, keyAgreement, certificateSignature, crlSignature,
300          ├                                        encipherOnly和decipherOnly,如果证书包括多个密钥用法,用逗号分隔
301          ├── -keyUsageCritical                  # keyUsage是否为关键项,可选项,默认为是
302          ├── -extKeyUsage                       # 扩展密钥用法,可选项,包括clientAuthentication,serverAuthentication,
303          ├                                        codeSignature,emailProtection,smartCardLogin,timestamp,ocspSignature
304          ├── -extKeyUsageCritical               # extKeyUsage是否为关键项,可选项,默认为否
305          ├── -signAlg                           # 签名算法,必填项,包括SHA256withRSA/SHA384withRSA/SHA256withECDSA/SHA384withECDSA
306          ├── -basicConstraints                  # 是否包含basicConstraints,可选项,默认为否
307          ├── -basicConstraintsCritical          # basicConstraints是否包含为关键项,可选项,默认为否
308          ├── -basicConstraintsCa                # 是否为CA,可选项,默认为否
309          ├── -basicConstraintsPathLen           # 路径长度,可选项,默认为0
310          ├── -issuerKeystoreFile                # 签发者密钥库文件,可选项
311          ├── -issuerKeystorePwd                 # 签发者密钥库口令,可选项
312          ├── -keystoreFile                      # 密钥库文件,必填项
313          ├── -keystorePwd                       # 密钥库口令,可选项
314          ├── -outFile                           # 输出证书文件,可选项,如果不填,则直接输出到控制台
315
3167.ProvisionProfile文件签名
317
318    sign-profile : ProvisionProfile文件签名
319          ├── -mode            # 签名模式,必填项,包括localSign,remoteSign
320          ├── -keyAlias        # 密钥别名,必填项,不区分大小写
321          ├── -keyPwd          # 密钥口令,可选项
322          ├── -profileCertFile # Profile签名证书(证书链,顺序为实体证书-中间CA证书-根证书),必填项
323          ├── -inFile          # 输入的原始Provision Profile文件,必填项
324          ├── -signAlg         # 签名算法,必填项,包括SHA256withECDSA / SHA384withECDSA
325          ├── -keystoreFile    # 密钥库文件,localSign模式时为必填项
326          ├── -keystorePwd     # 密钥库口令,可选项
327          ├── -outFile         # 输出签名后的Provision Profile文件,p7b格式,必填项
328
3298.ProvisionProfile文件验签
330
331     verify-profile : ProvisionProfile文件验签
332           ├── -inFile       # 已签名的Provision Profile文件,p7b格式,必填项
333           ├── -outFile      # 验证结果文件(包含验证结果和profile内容),json格式,可选项;如果不填,则直接输出到控制台
334
3359.应用包和二进制工具签名
336
337     sign-app : 应用包和二进制工具签名
338          ├── -mode          # 签名模式,必填项,包括localSign,remoteSign
339          ├── -keyAlias      # 密钥别名,必填项,不区分大小写
340          ├── -keyPwd        # 密钥口令,可选项
341          ├── -appCertFile   # 应用签名证书文件(证书链,顺序为实体证书-中间CA证书-根证书),必填项
342          ├── -profileFile   # 签名后的Provision Profile文件名,profileSigned为1时为p7b格式,profileSigned为0时为json格式,应用包签名必填项,二进制工具签名选填
343          ├── -profileSigned # 指示profile文件是否带有签名,1表示有签名,0表示没有签名,默认为1。可选项
344          ├── -inForm        # 输入的原始文件的格式,枚举值:zip、elf或bin;zip应用包对应zip,二进制工具对应elf,bin应用包为bin,默认zip;可选项
345          ├── -inFile        # 输入的原始文件,应用包、elf或bin文件,必填项
346          ├── -signAlg       # 签名算法,必填项,包括SHA256withECDSA / SHA384withECDSA
347          ├── -keystoreFile  # 密钥库文件,localSign模式时为必填项
348          ├── -keystorePwd   # 密钥库口令,可选项
349          ├── -outFile       # 输出签名后的包文件,必填项
350          ├── -signCode      # 是否启用代码签名,1表示开启代码签名,0表示关闭代码签名。可选项。默认对hap、hsp、hqf、elf开启代码签名,通过参数配置为0关闭。
351
35210.应用包和二进制工具文件验签
353
354      verify-app : 应用包和二进制工具文件验签
355         ├── -inFile          # 已签名的文件,应用包、elf或bin文件,必填项
356         ├── -outCertChain    # 签名的证书链文件,必填项
357         ├── -outProfile      # 应用包中的profile文件,必填项
358         ├── -inForm          # 输入的原始文件的格式,枚举值:zip、elf或bin;zip应用包对应zip,二进制工具对应elf,bin应用包为bin,默认zip;可选项
359
360
361#### 相关仓
362   不涉及