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