README.md
1# hapsigner
2
3#### Introduction
4
5To ensure the integrity and secure source of OpenHarmony applications, the applications must be signed during the build process. Only signed applications can be installed, run, and debugged on real devices. This repository provides the source code of the HAP signing tool - hapsigner. This tool can be used to generate key pairs, certificate signing requests (CSRs), certificates, profile signatures, and HAP signatures.
6
7
8#### Directory Structure
9
10 developtools_hapsigner
11
12 ├── autosign # One-click signature script.
13 ├── dist # SDK preconfigured file.
14 ├── hapsigntool # Master code.
15 ├──hap_sign_tool # Application entry, used to verify input parameters.
16 ├──hap_sign_tool_lib # Signing tool lib, used to parse command words and parameter lists to implement logic of modules.
17 ├── tools # Auto-test script.
18
19
20
21#### Constraints
22hapsigner is developed in Java and must run in JRE 8.0 or later.
23The scripts, such as the one-click signature script, are developed in Python, and must run on Python 3.x.
24#### Build
25
26 1. Check that Gradle 7.1 has been installed.
27
28 gradle -v
29
30 2. Download the code, open the file directory **developtools_hapsigner/hapsigntool**, and run the following command to build the code:
31
32 gradle build or gradle jar
33
34 3. Check that **hap-sign-tool.jar** (binary files) is generated in the **./hap_sign_tool/build/libs** directory.
35
36****
37#### Usage
38##### Usage of Signature-related Files
39
40When signing an application using the IDE, you will obtain the following files from the SDK:
41
42```
43KeyStore (KS) file: OpenHarmony.p12
44Profile signing certificates: OpenHarmonyProfileRelease.pem and OpenHarmonyProfileDebug.pem
45Profile templates: UnsgnedReleasedProfileTemplate.json and UnsgnedDebugProfileTemplate.json
46Signature tool: hap-sign-tool.jar
47```
48The figures below illustrate how these files are used.
49
50**Signing a Profile**
51
52![signprofile.png](figures/signprofile_en.png)
53
54**Signing an App**
55
56![signapp.png](figures/signapp_en.png)
57##### Note
58
59In the following, the JAR package used is the binary files generated during the build process.
60
611. Command line signatures
62 Command line signatures include profile signatures and HAP signatures.
63
64 (1) Sign a profile.
65
66
67```shell
68java -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"
69```
70The parameters in the command are described as follows:
71
72 sign-profile: Sign a provisioning profile.
73 ├── -mode # Signing mode, which can be localSign or remoteSign. It is mandatory.
74 ├── -keyAlias # Key alias. It is mandatory.
75 ├── -keyPwd # Key password. It is optional.
76 ├── -profileCertFile # Profile signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
77 ├── -inFile # Raw provisioning profile. It is mandatory.
78 ├── -signAlg # Signature algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
79 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
80 ├── -keystorePwd # KS password. It is optional.
81 ├── -outFile # Signed provisioning profile to generate, in p7b format. It is mandatory.
82
83
84
85(2) Sign a HAP.
86
87
88```shell
89java -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"
90```
91The parameters in the command are described as follows:
92
93 sign-app: Sign a HAP.
94 ├── -mode # Signing mode, which can be localSign or remoteSign. It is mandatory.
95 ├── -keyAlias # Key alias. It is mandatory.
96 ├── -keyPwd # Key password. It is optional.
97 ├── -appCertFile # Application signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
98 ├── -profileFile # Singed provisioning profile, in p7b format. It is mandatory.
99 ├── -profileSigned # Whether the profile is signed. The value 1 means signed, and value 0 means unsigned. The default value is 1. It is optional.
100 ├── -inForm # Raw file, in .zip (default) or .bin format. It is optional.
101 ├── -inFile # Raw application package, in .zip or .bin format. It is mandatory.
102 ├── -signAlg # Signature algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
103 ├── -keystoreFile # KeyStore (KS) file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
104 ├── -keystorePwd # KS password. It is optional.
105 ├── -outFile # Signed HAP file to generate. It is mandatory.
106
107
1082. One-click signature
109
110
111To improve development efficiency, this project also provides one-click signature scripts based on the hapsigner tool. You can use these scripts to easily generate key pairs and end-entity certificates and sign profiles and HAPs, instead of entering complex commands.
112The scripts and configuration files are located in the **autosign** directory.
113
114 - create_root.sh/create_root.bat
115 - create_appcert_sign_profile.sh/create_appcert_sign_profile.bat
116 - sign_hap.sh/sign_hap.bat
117 - createAppCertAndProfile.config
118 - createRootAndSubCert.config
119 - signHap.config
120
121Procedure:
1221. Ensure that Python 3.5 or later has been installed.
1232. Prepare **hap-sign-tool.jar**. For details, see section **Build**.
1243. Prepare the HAP to be signed and the provisioning profile template file.
1254. Use the text editor to open the **createAppCertAndProfile.config** file and **signHap.config** file and change the values of **common.keyPwd** and **common.issuerKeyPwd** to match your case.
1265. Run **create_appcert_sign_profile.sh** in Linux or **create_appcert_sign_profile.bat** in Windows to generate files required for signature.
1276. Run **sign_hap.sh** in Linux or **sign_hap.bat** in Windows to sign the HAP.
128
129 > Note: To generate the KS file, root CA certificate, intermediate CA certificate, and profile signing certificate, perform the following steps:
130 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.
131 2. Run **create_root.sh** in Linux or run **create_root.bat** in Windows to generate the required KS file, root CA certificate, intermediate CA certificate, and profile signing certificate.
132
133
134****
135##### Common Operations
1361.Generate a key pair.
137
138 generate-keypair: Generate a key pair.
139 ├── -keyAlias # Key alias. It is mandatory.
140 ├── -keyPwd # Key password. It is optional.
141 ├── -keyAlg # Key algorithm, which can be RSA or ECC. It is mandatory.
142 ├── -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.
143 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
144 ├── -keystorePwd # KS password. It is optional.
145
1462.Generate a CSR.
147
148 generate-csr: Generate a CSR.
149 ├── -keyAlias # Key alias. It is mandatory.
150 ├── -keyPwd # Key password. It is optional.
151 ├── -subject # Certificate subject. It is mandatory.
152 ├── -signAlg # Signature algorithm, which can be SHA256withRSA, SHA384withRSA, SHA256withECDSA, or SHA384withECDSA. It is mandatory.
153 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
154 ├── -keystorePwd # KS password. It is optional.
155 ├── -outFile # CSR to generate. It is optional. If you do not specify this parameter, the CSR is output to the console.
156
1573.Generate a root CA or intermediate CA certificate.
158
159 generate-ca: Generate a root CA or intermediate CA certificate. If the key does not exist, generate a key together with the certificate.
160 ├── -keyAlias # Key alias. It is mandatory.
161 ├── -keyPwd # Key password. It is optional.
162 ├── -keyAlg # Key algorithm, which can be RSA or ECC. It is mandatory.
163 ├── -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.
164 ├── -issuer # Issuer of the certificate. It is optional. It indicates a root CA certificate if not specified.
165 ├── -issuerKeyAlias # Key alias of the issuer. It is optional. It indicates a root CA certificate if not specified.
166 ├── -issuerKeyPwd # Key password of the issuer. It is optional.
167 ├── -subject # Certificate subject. It is mandatory.
168 ├── -validity # Validity period of the certificate. It is optional. The default value is 3650 days.
169 ├── -signAlg # Signature algorithm, which can be SHA256withRSA, SHA384withRSA, SHA256withECDSA, or SHA384withECDSA. It is mandatory.
170 ├── -basicConstraintsPathLen # Path length. It is optional. The default value is 0.
171 ├── -issuerKeystoreFile # KS file of the issuer, in JKS or P12 format. It is optional.
172 ├── -issuerKeystorePwd # KS password of the issuer. It is optional.
173 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
174 ├── -keystorePwd # KS password. It is optional.
175 ├── -outFile # File to generate. It is optional. The file is output to the console if this parameter is not specified.
176
1774.Generate an application debug or release certificate.
178
179 generate-app-cert: Generate an application debug or release certificate.
180 ├── -keyAlias # Key alias. It is mandatory.
181 ├── -keyPwd # Key password. It is optional.
182 ├── -issuer # Issuer of the certificate. It is mandatory.
183 ├── -issuerKeyAlias # Key alias of the issuer. It is mandatory.
184 ├── -issuerKeyPwd # Key password of the issuer. It is optional.
185 ├── -subject # Certificate subject. It is mandatory.
186 ├── -validity # Validity period of the certificate. It is optional. The default value is 3650 days.
187 ├── -signAlg # Signature algoritym, which can be SHA256withECDSA or SHA384withECDSA.
188 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
189 ├── -keystorePwd # KS password. It is optional.
190 ├── -issuerKeystoreFile # KS file of the issuer, in JKS or P12 format. It is optional.
191 ├── -issuerKeystorePwd # KS password of the issuer. It is optional.
192 ├── -outForm # Format of the certificate to generate. It is optional. The value can be cert or certChain. The default value is certChain.
193 ├── -rootCaCertFile # Root CA certificate, which is mandatory when outForm is certChain.
194 ├── -subCaCertFile # Intermediate CA certificate file, which is mandatory when outForm is certChain.
195 ├── -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.
196
1975.Generate a profile debug or release certificate.
198
199 generate-profile-cert: Generate a profile debug or release certificate.
200 ├── -keyAlias # Key alias. It is mandatory.
201 ├── -keyPwd # Key password. It is optional.
202 ├── -issuer # Issuer of the certificate. It is mandatory.
203 ├── -issuerKeyAlias # Key alias of the issuer. It is mandatory.
204 ├── -issuerKeyPwd # Key password of the issuer. It is optional.
205 ├── -subject # Certificate subject. It is mandatory.
206 ├── -validity # Validity period of the certificate. It is optional. The default value is 3650 days.
207 ├── -signAlg # Signature algoritym, which can be SHA256withECDSA or SHA384withECDSA.
208 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
209 ├── -keystorePwd # KS password. It is optional.
210 ├── -issuerKeystoreFile # KS file of the issuer, in JKS or P12 format. It is optional.
211 ├── -issuerKeystorePwd # KS password of the issuer. It is optional.
212 ├── -outForm # Format of the certificate to generate. It is optional. The value can be cert or certChain. The default value is certChain.
213 ├── -rootCaCertFile # Root CA certificate, which is mandatory when outForm is certChain.
214 ├── -subCaCertFile # Intermediate CA certificate file, which is mandatory when outForm is certChain.
215 ├── -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.
216
2176.Generate a common certificate, which can be used to generate a custom certificate.
218
219 generate-cert: Generate a common certificate, which can be used to generate a custom certificate.
220 ├── -keyAlias # Key alias. It is mandatory.
221 ├── -keyPwd # Key password. It is optional.
222 ├── -issuer # Issuer of the certificate. It is mandatory.
223 ├── -issuerKeyAlias # Key alias of the issuer. It is mandatory.
224 ├── -issuerKeyPwd # Key password of the issuer. It is optional.
225 ├── -subject # Certificate subject. It is mandatory.
226 ├── -validity # Validity period of the certificate. It is optional. The default value is 1095 days.
227 ├── -keyUsage # Usages of the key. It is mandatory. The key usages include digitalSignature, nonRepudiation, keyEncipherment,
228 ├ dataEncipherment, keyAgreement, certificateSignature, crlSignature, encipherOnly, and decipherOnly.
229 ├ Use a comma (,) to separate multiple values.
230 ├── -keyUsageCritical # Whether keyUsage is a critical option. It is optional. The default value is true.
231 ├── -extKeyUsage # Extended key usages. It is optional. The extended key usages include clientAuthentication, serverAuthentication,
232 ├ codeSignature, emailProtection, smartCardLogin, timestamp, and ocspSignature.
233 ├── -extKeyUsageCritical # Whether extKeyUsage is a critical option. It is optional. The default value is false.
234 ├── -signAlg # Signature algorithm, which can be SHA256withRSA, SHA384withRSA, SHA256withECDSA, or SHA384withECDSA. It is mandatory.
235 ├── -basicConstraints # Whether basicConstraints is contained. It is optional. The default value is false.
236 ├── -basicConstraintsCritical # Whether basicConstraints is a critical option. It is optional. The default value is false.
237 ├── -basicConstraintsCa # Whether it is CA. It is optional. The default value is false.
238 ├── -basicConstraintsPathLen # Path length. It is optional. The default value is 0.
239 ├── -issuerKeystoreFile # KS file of the issuer, in JKS or P12 format. It is optional.
240 ├── -issuerKeystorePwd # KS password of the issuer. It is optional.
241 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory.
242 ├── -keystorePwd # KS password. It is optional.
243 ├── -outFile # Certificate file to generate. It is optional. The file is output to the console if this parameter is not specified.
244
2457.Sign a provisioning profile.
246
247 sign-profile: Sign a provisioning profile.
248 ├── -mode # Signing mode, which can be localSign or remoteSign. It is mandatory.
249 ├── -keyAlias # Key alias. It is mandatory.
250 ├── -keyPwd # Key password. It is optional.
251 ├── -profileCertFile # Profile signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
252 ├── -inFile # Raw provisioning profile. It is mandatory.
253 ├── -signAlg # Signature algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
254 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
255 ├── -keystorePwd # KS password. It is optional.
256 ├── -outFile # Signed provisioning profile to generate, in p7b format. It is mandatory.
257
2588.Verify the provisioning profile signature.
259
260 verify-profile: Verify the provisioning profile signature.
261 ├── -inFile # Signed provisioning profile, in p7b format. It is mandatory.
262 ├── -outFile # Verification result file (including the verification result and profile content), in json format. It is optional. The file is output to the console if this parameter is not specified.
263
2649.Sign a HAP.
265
266 sign-app: Sign a HAP
267 ├── -mode # Signing mode, which can be localSign, remoteSign, or remoteResign. It is mandatory.
268 ├── -keyAlias # Key alias. It is mandatory.
269 ├── -keyPwd # Key password. It is optional.
270 ├── -appCertFile # Application signing certificate (certificate chain, in the end-entity certificate, intermediate CA certificate, and root certificate order). It is mandatory.
271 ├── -profileFile # Name of the signed provisioning profile. The profile is in p7b format if profileSigned is 1 and in json format if profileSigned is 0. It is mandatory.
272 ├── -profileSigned # Whether the profile is signed. The value 1 means signed, and value 0 means unsigned. The default value is 1. It is optional.
273 ├── -inForm # Raw file, in .zip (default) or .bin format. It is optional.
274 ├── -inFile # Raw application package, in .zip or .bin format. It is mandatory.
275 ├── -signAlg # Signature algorithm, which can be SHA256withECDSA or SHA384withECDSA. It is mandatory.
276 ├── -keystoreFile # KS file, in JKS or P12 format. It is mandatory if the signing mode is localSign.
277 ├── -keystorePwd # KS password. It is optional.
278 ├── -outFile # Signed HAP file to generate. It is mandatory.
279
28010.Verify the HAP Signature.
281
282 verify-app: Verify the HAP signature.
283 ├── -inFile # Signed application file, in .zip or .bin format. It is mandatory.
284 ├── -outCertChain # Signed certificate chain file. It is mandatory.
285 ├── -outProfile # Profile of the application. It is mandatory.
286
287
288
289
290#### Repositories Involved
291 N/A
292