1 /* 2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.entity; 17 18 import com.ohos.hapsigntool.entity.Options; 19 import com.ohos.hapsigntool.entity.ParamConstants; 20 import com.ohos.hapsigntool.error.ParamException; 21 22 /** 23 * SignAppParameters. 24 * 25 * @since 2024/04/06 26 */ 27 public class SignAppParameters implements Parameters { 28 /** 29 * signature mode, required fields, including localSign/remoteSign 30 */ 31 private Mode mode; 32 33 /** 34 * key alias, required fields 35 */ 36 private String keyAlias; 37 38 /** 39 * key password, optional fields on localSign mode 40 */ 41 private char[] keyPwd; 42 43 /** 44 * application signature certificate file, required fields on localSign mode, optional fields on remoteSign mode 45 */ 46 private String appCertFile; 47 48 /** 49 * signed Provision Profile file, p7b format, required fields 50 */ 51 private String profileFile; 52 53 /** 54 * indicates whether the profile file has a signature.The options are as follows: SIGNED, UNSIGNED; default :SIGNED 55 */ 56 private ProFileSigned profileSigned = ProFileSigned.SIGNED; 57 58 /** 59 * Enter the format of the original file, ZIP, BIN, and ELF format, default:ZIP 60 */ 61 private InForm inForm = InForm.ZIP; 62 63 /** 64 * input original application package file, required fields 65 */ 66 private String inFile; 67 68 /** 69 * signature algorithm, required fields 70 */ 71 private String signAlg; 72 73 /** 74 * keystore file, if signature mode is localSign, required fields on localSign mode, JKS or P12 format 75 */ 76 private String keyStoreFile; 77 78 /** 79 * keystore password, optional fields on localSign mode 80 */ 81 private char[] keystorePwd; 82 83 /** 84 * output the signed Provision Profile file, required fields 85 */ 86 private String outFile; 87 88 /** 89 * Whether the infile is signed code, optional fields, default signed code 90 */ 91 private SignCode signCode; 92 93 /** 94 * user account for online authentication, required fields on remoteSign mode with account auth mode 95 */ 96 private String userName; 97 98 /** 99 * user password for online authentication, required fields on remoteSign mode with account auth mode 100 */ 101 private String userPwd; 102 103 /** 104 * remote sign service url, required fields on remoteSign mode 105 */ 106 private String signServer; 107 108 /** 109 * remote signer plug-in component, required fields on remoteSign mode 110 */ 111 private String signerPlugin; 112 113 /** 114 * min compatible api version for running app 115 */ 116 private String compatibleVersion; 117 118 /** 119 * remote sign auth mode, required fields on remoteSign mode, including account 120 */ 121 private String onlineAuthMode; 122 123 @Override toOptions()124 public Options toOptions() throws ParamException { 125 Options options = new Options(); 126 if (mode == null) { 127 throw new ParamException(Options.MODE); 128 } 129 options.put(Options.MODE, mode.getValue()); 130 131 if (keyAlias == null) { 132 throw new ParamException(Options.KEY_ALIAS); 133 } 134 options.put(Options.KEY_ALIAS, keyAlias); 135 136 if (keyPwd != null) { 137 options.put(Options.KEY_RIGHTS, keyPwd); 138 } 139 140 if (profileFile == null) { 141 throw new ParamException(Options.PROFILE_FILE); 142 } 143 options.put(Options.PROFILE_FILE, profileFile); 144 145 if (profileSigned != null) { 146 options.put(Options.PROFILE_SIGNED, profileSigned.getValue()); 147 } 148 149 if (inForm != null) { 150 options.put(Options.IN_FORM, inForm.getValue()); 151 } 152 153 if (inFile == null) { 154 throw new ParamException(Options.IN_FILE); 155 } 156 options.put(Options.IN_FILE, inFile); 157 158 if (signAlg == null) { 159 throw new ParamException(Options.SIGN_ALG); 160 } 161 options.put(Options.SIGN_ALG, signAlg); 162 163 if (keystorePwd != null) { 164 options.put(Options.KEY_STORE_RIGHTS, keystorePwd); 165 } 166 167 if (outFile == null) { 168 throw new ParamException(Options.OUT_FILE); 169 } 170 options.put(Options.OUT_FILE, outFile); 171 172 if (signCode != null) { 173 options.put(ParamConstants.PARAM_SIGN_CODE, signCode.getValue()); 174 } 175 176 if (compatibleVersion != null) { 177 options.put("compatibleVersion", compatibleVersion); 178 } 179 180 keyStoreFileToOptions(options); 181 appCertFileToOptions(options); 182 remoteSignParamToOptions(options); 183 return options; 184 } 185 keyStoreFileToOptions(Options options)186 private void keyStoreFileToOptions(Options options) throws ParamException { 187 if (mode == Mode.LOCAL_SIGN) { 188 if (keyStoreFile == null) { 189 throw new ParamException(Options.KEY_STORE_FILE); 190 } 191 options.put(Options.KEY_STORE_FILE, keyStoreFile); 192 } else { 193 if (keyStoreFile != null) { 194 throw new ParamException(Options.KEY_STORE_FILE, "remote sign do not use this param"); 195 } 196 } 197 } 198 appCertFileToOptions(Options options)199 private void appCertFileToOptions(Options options) throws ParamException { 200 if (mode == Mode.LOCAL_SIGN) { 201 if (appCertFile == null) { 202 throw new ParamException(Options.APP_CERT_FILE); 203 } 204 options.put(Options.APP_CERT_FILE, appCertFile); 205 } else { 206 if (appCertFile != null) { 207 options.put(Options.APP_CERT_FILE, appCertFile); 208 } 209 } 210 } 211 remoteSignParamToOptions(Options options)212 private void remoteSignParamToOptions(Options options) throws ParamException { 213 if (mode == Mode.LOCAL_SIGN) { 214 return; 215 } 216 if (signServer == null) { 217 throw new ParamException("signServer"); 218 } 219 if (userPwd == null) { 220 throw new ParamException("userPwd"); 221 } 222 if (userName == null) { 223 throw new ParamException("username"); 224 } 225 if (signerPlugin == null) { 226 throw new ParamException("signerPlugin"); 227 } 228 if (onlineAuthMode == null) { 229 throw new ParamException("onlineAuthMode"); 230 } 231 options.put("signServer", signServer); 232 options.put("userPwd", userPwd); 233 options.put("username", userName); 234 options.put("signerPlugin", signerPlugin); 235 options.put("onlineAuthMode", onlineAuthMode); 236 } 237 getMode()238 public Mode getMode() { 239 return mode; 240 } 241 setMode(Mode mode)242 public void setMode(Mode mode) { 243 this.mode = mode; 244 } 245 getKeyAlias()246 public String getKeyAlias() { 247 return keyAlias; 248 } 249 setKeyAlias(String keyAlias)250 public void setKeyAlias(String keyAlias) { 251 this.keyAlias = keyAlias; 252 } 253 getKeyPwd()254 public char[] getKeyPwd() { 255 return keyPwd; 256 } 257 setKeyPwd(char[] keyPwd)258 public void setKeyPwd(char[] keyPwd) { 259 this.keyPwd = keyPwd; 260 } 261 getAppCertFile()262 public String getAppCertFile() { 263 return appCertFile; 264 } 265 setAppCertFile(String appCertFile)266 public void setAppCertFile(String appCertFile) { 267 this.appCertFile = appCertFile; 268 } 269 getProfileFile()270 public String getProfileFile() { 271 return profileFile; 272 } 273 setProfileFile(String profileFile)274 public void setProfileFile(String profileFile) { 275 this.profileFile = profileFile; 276 } 277 getProfileSigned()278 public ProFileSigned getProfileSigned() { 279 return profileSigned; 280 } 281 setProfileSigned(ProFileSigned profileSigned)282 public void setProfileSigned(ProFileSigned profileSigned) { 283 this.profileSigned = profileSigned; 284 } 285 getInForm()286 public InForm getInForm() { 287 return inForm; 288 } 289 setInForm(InForm inForm)290 public void setInForm(InForm inForm) { 291 this.inForm = inForm; 292 } 293 getInFile()294 public String getInFile() { 295 return inFile; 296 } 297 setInFile(String inFile)298 public void setInFile(String inFile) { 299 this.inFile = inFile; 300 } 301 getSignAlg()302 public String getSignAlg() { 303 return signAlg; 304 } 305 setSignAlg(String signAlg)306 public void setSignAlg(String signAlg) { 307 this.signAlg = signAlg; 308 } 309 getKeyStoreFile()310 public String getKeyStoreFile() { 311 return keyStoreFile; 312 } 313 setKeyStoreFile(String keyStoreFile)314 public void setKeyStoreFile(String keyStoreFile) { 315 this.keyStoreFile = keyStoreFile; 316 } 317 getKeystorePwd()318 public char[] getKeystorePwd() { 319 return keystorePwd; 320 } 321 setKeystorePwd(char[] keystorePwd)322 public void setKeystorePwd(char[] keystorePwd) { 323 this.keystorePwd = keystorePwd; 324 } 325 getOutFile()326 public String getOutFile() { 327 return outFile; 328 } 329 setOutFile(String outFile)330 public void setOutFile(String outFile) { 331 this.outFile = outFile; 332 } 333 getSignCode()334 public SignCode getSignCode() { 335 return signCode; 336 } 337 setSignCode(SignCode signCode)338 public void setSignCode(SignCode signCode) { 339 this.signCode = signCode; 340 } 341 getUserName()342 public String getUserName() { 343 return userName; 344 } 345 setUserName(String userName)346 public void setUserName(String userName) { 347 this.userName = userName; 348 } 349 getUserPwd()350 public String getUserPwd() { 351 return userPwd; 352 } 353 setUserPwd(String userPwd)354 public void setUserPwd(String userPwd) { 355 this.userPwd = userPwd; 356 } 357 getSignServer()358 public String getSignServer() { 359 return signServer; 360 } 361 setSignServer(String signServer)362 public void setSignServer(String signServer) { 363 this.signServer = signServer; 364 } 365 getSignerPlugin()366 public String getSignerPlugin() { 367 return signerPlugin; 368 } 369 setSignerPlugin(String signerPlugin)370 public void setSignerPlugin(String signerPlugin) { 371 this.signerPlugin = signerPlugin; 372 } 373 getCompatibleVersion()374 public String getCompatibleVersion() { 375 return compatibleVersion; 376 } 377 setCompatibleVersion(String compatibleVersion)378 public void setCompatibleVersion(String compatibleVersion) { 379 this.compatibleVersion = compatibleVersion; 380 } 381 getOnlineAuthMode()382 public String getOnlineAuthMode() { 383 return onlineAuthMode; 384 } 385 setOnlineAuthMode(String onlineAuthMode)386 public void setOnlineAuthMode(String onlineAuthMode) { 387 this.onlineAuthMode = onlineAuthMode; 388 } 389 } 390