diff --git a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/entity/RetMsg.java b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/entity/RetMsg.java index 2012f24f931ff7e1c38b1635b7eb1e3eb3dc3e05..1c5936a104c53cf73510c41cb2d43ed6fa424737 100644 --- a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/entity/RetMsg.java +++ b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/entity/RetMsg.java @@ -15,7 +15,7 @@ package com.ohos.entity; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; /** * RetMsg. @@ -23,20 +23,20 @@ import com.ohos.hapsigntool.error.Error; * @since 2024/04/06 */ public class RetMsg { - private Error errCode; + private ERROR errCode; private String errMessage; - public RetMsg(Error errCode, String errMessage) { + public RetMsg(ERROR errCode, String errMessage) { this.errCode = errCode; this.errMessage = errMessage; } - public Error getErrCode() { + public ERROR getErrCode() { return errCode; } - public void setErrCode(Error errCode) { + public void setErrCode(ERROR errCode) { this.errCode = errCode; } diff --git a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntool/HapSignTool.java b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntool/HapSignTool.java index cd19e936c2fdfc96b3c1cf58592e704d8c1c3608..531e96b3df118976cd02d9f6df012af11ec81e73 100644 --- a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntool/HapSignTool.java +++ b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntool/HapSignTool.java @@ -24,7 +24,7 @@ import com.ohos.hapsigntool.api.ServiceApi; import com.ohos.hapsigntool.api.SignToolServiceImpl; import com.ohos.hapsigntool.entity.Options; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.ParamException; import com.ohos.hapsigntool.utils.FileUtils; import com.ohos.hapsigntool.utils.StringUtils; @@ -156,7 +156,7 @@ public final class HapSignTool { isSuccess = runProfileCert(params.getOptions(), api); break; default: - CustomException.throwException(Error.COMMAND_ERROR, "Unsupported cmd"); + CustomException.throwException(ERROR.COMMAND_ERROR, "Unsupported cmd"); break; } return isSuccess; @@ -283,7 +283,7 @@ public final class HapSignTool { if (!LOCAL_SIGN.equalsIgnoreCase(mode) && !REMOTE_SIGN.equalsIgnoreCase(mode) && !"remoteResign".equalsIgnoreCase(mode)) { - CustomException.throwException(Error.COMMAND_ERROR, "mode params is incorrect"); + CustomException.throwException(ERROR.COMMAND_ERROR, "mode params is incorrect"); } if (LOCAL_SIGN.equalsIgnoreCase(mode)) { @@ -293,7 +293,7 @@ public final class HapSignTool { checkProfile(params); String inForm = params.getString(Options.IN_FORM, "zip"); if (!StringUtils.isEmpty(inForm) && !containsIgnoreCase(inForm)) { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, "inForm params is incorrect"); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "inForm params is incorrect"); } String signAlg = params.getString(Options.SIGN_ALG); CmdUtil.judgeEndSignAlgType(signAlg); @@ -310,7 +310,7 @@ public final class HapSignTool { return; } if (!SIGNED.equals(profileSigned) && !NOT_SIGNED.equals(profileSigned)) { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, "profileSigned params is incorrect"); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "profileSigned params is incorrect"); } if (SIGNED.equals(profileSigned)) { FileUtils.validFileType(profileFile, "p7b"); @@ -323,7 +323,7 @@ public final class HapSignTool { params.required(Options.MODE, Options.SIGN_ALG, Options.OUT_FILE, Options.IN_FILE); String mode = params.getString(Options.MODE); if (!LOCAL_SIGN.equalsIgnoreCase(mode) && !REMOTE_SIGN.equalsIgnoreCase(mode)) { - CustomException.throwException(Error.COMMAND_ERROR, "mode params is incorrect"); + CustomException.throwException(ERROR.COMMAND_ERROR, "mode params is incorrect"); } if (LOCAL_SIGN.equalsIgnoreCase(mode)) { params.required(Options.KEY_STORE_FILE, Options.KEY_ALIAS, Options.PROFILE_CERT_FILE); @@ -343,7 +343,7 @@ public final class HapSignTool { Options.OUT_PROFILE); String inForm = params.getString(Options.IN_FORM, "zip"); if (!containsIgnoreCase(inForm)) { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, "inForm params must is " + informList); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "inForm params must is " + informList); } FileUtils.validFileType(params.getString(Options.OUT_CERT_CHAIN), "cer"); FileUtils.validFileType(params.getString(Options.OUT_PROFILE), "p7b"); @@ -398,15 +398,15 @@ public final class HapSignTool { Options options = signAppParameters.toOptions(); ServiceApi api = new SignToolServiceImpl(); if (runSignApp(options, api)) { - return new RetMsg(Error.SUCCESS_CODE, "sign app success"); + return new RetMsg(ERROR.SUCCESS_CODE, "sign app success"); } - return new RetMsg(Error.SIGN_ERROR, "sign app failed"); + return new RetMsg(ERROR.SIGN_ERROR, "sign app failed"); } catch (CustomException e) { return new RetMsg(e.getError(), e.getMessage()); } catch (ParamException e) { - return new RetMsg(Error.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); + return new RetMsg(ERROR.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); } catch (Exception e) { - return new RetMsg(Error.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); + return new RetMsg(ERROR.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); } } @@ -424,15 +424,15 @@ public final class HapSignTool { Options options = verifyAppParameters.toOptions(); ServiceApi api = new SignToolServiceImpl(); if (runVerifyApp(options, api)) { - return new RetMsg(Error.SUCCESS_CODE, "verify app success"); + return new RetMsg(ERROR.SUCCESS_CODE, "verify app success"); } - return new RetMsg(Error.VERIFY_ERROR, "verify app failed"); + return new RetMsg(ERROR.VERIFY_ERROR, "verify app failed"); } catch (CustomException e) { return new RetMsg(e.getError(), e.getMessage()); } catch (ParamException e) { - return new RetMsg(Error.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); + return new RetMsg(ERROR.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); } catch (Exception e) { - return new RetMsg(Error.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); + return new RetMsg(ERROR.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); } } @@ -450,15 +450,15 @@ public final class HapSignTool { Options options = signProfileParameters.toOptions(); ServiceApi api = new SignToolServiceImpl(); if (runSignProfile(options, api)) { - return new RetMsg(Error.SUCCESS_CODE, "sign profile success"); + return new RetMsg(ERROR.SUCCESS_CODE, "sign profile success"); } - return new RetMsg(Error.SIGN_ERROR, "sign profile failed"); + return new RetMsg(ERROR.SIGN_ERROR, "sign profile failed"); } catch (CustomException e) { return new RetMsg(e.getError(), e.getMessage()); } catch (ParamException e) { - return new RetMsg(Error.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); + return new RetMsg(ERROR.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); } catch (Exception e) { - return new RetMsg(Error.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); + return new RetMsg(ERROR.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); } } @@ -476,15 +476,15 @@ public final class HapSignTool { Options options = verifyProfileParameters.toOptions(); ServiceApi api = new SignToolServiceImpl(); if (runVerifyProfile(options, api)) { - return new RetMsg(Error.SUCCESS_CODE, "verify profile success"); + return new RetMsg(ERROR.SUCCESS_CODE, "verify profile success"); } - return new RetMsg(Error.VERIFY_ERROR, "verify profile failed"); + return new RetMsg(ERROR.VERIFY_ERROR, "verify profile failed"); } catch (CustomException e) { return new RetMsg(e.getError(), e.getMessage()); } catch (ParamException e) { - return new RetMsg(Error.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); + return new RetMsg(ERROR.COMMAND_PARAM_ERROR, "paramException : " + e.getMessage()); } catch (Exception e) { - return new RetMsg(Error.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); + return new RetMsg(ERROR.UNKNOWN_ERROR, "unknownException : " + e.getMessage()); } } } diff --git a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/CmdUtil.java b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/CmdUtil.java index 1b84eef58347df82475a7bd5c6b42e8928f389f0..a43077b69d3fdce60227445de36c2f8c885770c4 100644 --- a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/CmdUtil.java +++ b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/CmdUtil.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntoolcmd; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.StringUtils; import com.ohos.hapsigntool.utils.ValidateUtils; @@ -51,7 +51,7 @@ public final class CmdUtil { * @return Params */ public static Params convert2Params(String[] args) { - ValidateUtils.throwIfNotMatches(args.length >= ARGS_MIN_LEN, Error.COMMAND_ERROR, ""); + ValidateUtils.throwIfNotMatches(args.length >= ARGS_MIN_LEN, ERROR.COMMAND_ERROR, ""); Params params = new Params(); params.setMethod(args[0]); @@ -59,12 +59,12 @@ public final class CmdUtil { boolean readKey = true; List trustList = ParamsTrustlist.getTrustList(args[0]); if (trustList.size() == 0) { - CustomException.throwException(Error.COMMAND_ERROR, "Unsupported cmd"); + CustomException.throwException(ERROR.COMMAND_ERROR, "Unsupported cmd"); } for (int i = 1; i < args.length; i++) { String value = args[i]; if (StringUtils.isEmpty(value)) { - CustomException.throwException(Error.COMMAND_ERROR, "param value could not be empty"); + CustomException.throwException(ERROR.COMMAND_ERROR, "param value could not be empty"); } if (readKey) { // prepare key @@ -72,12 +72,12 @@ public final class CmdUtil { value = value.equals("-signcode") ? "-signCode" : value; boolean isTrust = trustList.contains(value); ValidateUtils.throwIfNotMatches(isTrust, - Error.COMMAND_PARAM_ERROR, "Not support command param"); + ERROR.COMMAND_PARAM_ERROR, "Not support command param"); keyStandBy = value.substring(1); readKey = false; } else { ValidateUtils.throwIfNotMatches(false, - Error.COMMAND_PARAM_ERROR, "param key value must in pairs"); + ERROR.COMMAND_PARAM_ERROR, "param key value must in pairs"); } } else { // prepare value @@ -96,11 +96,11 @@ public final class CmdUtil { if (StringUtils.isEmpty(key)) { result = false; } else if (StringUtils.isEmpty(value)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("Command -%s could not be empty", key)); result = false; } else if (params.getOptions().containsKey(key)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("Duplicate param '%s'. Stop processing", key)); result = false; } else if (key.toLowerCase(Locale.ROOT).endsWith("pwd")) { @@ -120,7 +120,7 @@ public final class CmdUtil { */ public static void judgeAlgType(String alg) { if (!"RSA".equalsIgnoreCase(alg) && !"ECC".equalsIgnoreCase(alg)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, "KeyAlg params is incorrect"); } } @@ -135,17 +135,17 @@ public final class CmdUtil { String[] array = {"2048", "3072", "4096", "NIST-P-256", "NIST-P-384"}; List arrayList = Arrays.asList(array); if (!arrayList.contains(size)) { - CustomException.throwException(Error.COMMAND_ERROR, String.format("KeySize '%s' is incorrect", size)); + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("KeySize '%s' is incorrect", size)); } if ("RSA".equalsIgnoreCase(alg)) { if (!"2048".equals(size) && !"3072".equals(size) && !"4096".equals(size)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("KeySize of '%s' is incorrect", alg)); } } else { if (!"NIST-P-256".equalsIgnoreCase(size) && !"NIST-P-384".equalsIgnoreCase(size)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("KeySize of '%s' is incorrect", alg)); } } @@ -160,7 +160,7 @@ public final class CmdUtil { List arrayList = Arrays.asList("SHA256withRSA", "SHA384withRSA", "SHA256withECDSA", "SHA384withECDSA"); if (!arrayList.contains(signAlg)) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, "SignAlg params is incorrect"); } } @@ -173,7 +173,7 @@ public final class CmdUtil { public static void judgeEndSignAlgType(String signAlg) { List arrayList = Arrays.asList("SHA256withECDSA", "SHA384withECDSA"); if (!arrayList.contains(signAlg)) { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "SignAlg params is incorrect, signature algorithms include SHA256withECDSA,SHA384withECDSA"); } } @@ -192,7 +192,7 @@ public final class CmdUtil { continue; } if (!supportList.contains(type.trim())) { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, "'" + type + "' in params '" + inputType + "' is not support"); } } @@ -210,7 +210,7 @@ public final class CmdUtil { } else if (INTEGER_PATTERN.matcher(size).matches()) { return size; } else { - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("KeySize '%s' is incorrect", size)); return size; } diff --git a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/HelpDocument.java b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/HelpDocument.java index 26369cb5c3ff3eb26c78916aa2aea25affb6dcd7..7dbe3004238c297b5fb99c8d39dc77d97acae8af 100644 --- a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/HelpDocument.java +++ b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/HelpDocument.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntoolcmd; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.FileUtils; import org.apache.logging.log4j.Logger; @@ -55,7 +55,7 @@ public final class HelpDocument { logger.info(helpStr); } catch (IOException ioe) { logger.debug(ioe.getMessage(), ioe); - CustomException.throwException(Error.READ_FILE_ERROR, "Failed to read " + page + " resource"); + CustomException.throwException(ERROR.READ_FILE_ERROR, "Failed to read " + page + " resource"); } } } diff --git a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/ParamsTrustlist.java b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/ParamsTrustlist.java index a04b1dc46837fe7acf58d10cbdb0d4a545c35f43..56fb0ae65fd93324565c9216f6072daacdd5ad84 100644 --- a/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/ParamsTrustlist.java +++ b/hapsigntool/hap_sign_tool/src/main/java/com/ohos/hapsigntoolcmd/ParamsTrustlist.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntoolcmd; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import java.io.BufferedReader; import java.io.IOException; @@ -83,7 +83,7 @@ public final class ParamsTrustlist { readHelpParam(br); } } catch (IOException e) { - CustomException.throwException(Error.READ_FILE_ERROR, "Failed to read " + page + " resource"); + CustomException.throwException(ERROR.READ_FILE_ERROR, "Failed to read " + page + " resource"); } } diff --git a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/ConcurrencyTest.java b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/ConcurrencyTest.java index 80c343b69d906d6a93a0b603f1f328fad3ef985e..58d5951ea5caf6abf22215f06abdeed73aaef780 100644 --- a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/ConcurrencyTest.java +++ b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/ConcurrencyTest.java @@ -24,7 +24,7 @@ import com.ohos.entity.SignProfileParameters; import com.ohos.entity.VerifyAppParameters; import com.ohos.entity.VerifyProfileParameters; import com.ohos.hapsigntool.HapSignTool; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.FileUtils; import org.junit.jupiter.api.AfterAll; @@ -96,7 +96,7 @@ public class ConcurrencyTest { /** * test Sign Hap - * + * * @throws IOException IOException * @throws ExecutionException ExecutionException * @throws InterruptedException InterruptedException @@ -222,14 +222,14 @@ public class ConcurrencyTest { signProfileParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signProfileParameters.setKeystorePwd("123456".toCharArray()); signProfileParameters.setOutFile(profile.getCanonicalPath()); - return HapSignTool.signProfile(signProfileParameters).getErrCode() == Error.SUCCESS_CODE; + return HapSignTool.signProfile(signProfileParameters).getErrCode() == ERROR.SUCCESS_CODE; } private boolean verifyProfile() throws IOException { VerifyProfileParameters verifyProfileParameters = new VerifyProfileParameters(); verifyProfileParameters.setInFile(profile.getCanonicalPath()); verifyProfileParameters.setOutFile("out.json"); - return HapSignTool.verifyProfile(verifyProfileParameters).getErrCode() == Error.SUCCESS_CODE; + return HapSignTool.verifyProfile(verifyProfileParameters).getErrCode() == ERROR.SUCCESS_CODE; } private boolean signApp() throws IOException { @@ -247,7 +247,7 @@ public class ConcurrencyTest { signAppParameters.setProfileSigned(ProFileSigned.SIGNED); signAppParameters.setInForm(InForm.ZIP); signAppParameters.setSignCode(SignCode.OPEN); - return HapSignTool.signApp(signAppParameters).getErrCode() == Error.SUCCESS_CODE; + return HapSignTool.signApp(signAppParameters).getErrCode() == ERROR.SUCCESS_CODE; } private boolean verifyApp() throws IOException { @@ -255,7 +255,7 @@ public class ConcurrencyTest { verifyAppParameters.setInFile(outputFile.getCanonicalPath()); verifyAppParameters.setOutCertChain("out.cer"); verifyAppParameters.setOutProfile("out.p7b"); - return HapSignTool.verifyApp(verifyAppParameters).getErrCode() == Error.SUCCESS_CODE; + return HapSignTool.verifyApp(verifyAppParameters).getErrCode() == ERROR.SUCCESS_CODE; } } } diff --git a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSIgnToolTest.java b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSignToolTest.java similarity index 92% rename from hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSIgnToolTest.java rename to hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSignToolTest.java index f7e14cd092a8ea0b7819536ff910f40451af5f7e..32613fc0c70f5e031592c20991a0e6efe190017d 100644 --- a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSIgnToolTest.java +++ b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/HapSignToolTest.java @@ -20,7 +20,7 @@ import com.ohos.entity.Mode; import com.ohos.entity.SignAppParameters; import com.ohos.entity.VerifyAppParameters; import com.ohos.hapsigntool.HapSignTool; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.FileUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; @@ -37,7 +37,7 @@ import java.util.List; * * @since 2024/04/12 */ -public class HapSIgnToolTest { +public class HapSignToolTest { private static final String TMP_SIGNED_FILE = "signed-"; private static final String TMP_HAP_SUFFIX = ".hap"; @@ -60,6 +60,8 @@ public class HapSIgnToolTest { /** * test sign params default value + * + * @throws IOException IOException */ @Test public void testDefaultValue() throws IOException { @@ -77,17 +79,19 @@ public class HapSIgnToolTest { signAppParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); VerifyAppParameters verifyAppParameters = new VerifyAppParameters(); verifyAppParameters.setInFile(outputFile.getCanonicalPath()); verifyAppParameters.setOutCertChain("out.cer"); verifyAppParameters.setOutProfile("out.p7b"); - Assertions.assertSame(HapSignTool.verifyApp(verifyAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertSame(HapSignTool.verifyApp(verifyAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test sign elf + * + * @throws IOException IOException */ @Test public void testSignElf() throws IOException { @@ -106,11 +110,13 @@ public class HapSIgnToolTest { signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); signAppParameters.setInForm(InForm.ELF); - Assertions.assertSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test Param Without KeyAlias + * + * @throws IOException IOException */ @Test public void testParamWithoutKeyAlias() throws IOException { @@ -127,11 +133,13 @@ public class HapSIgnToolTest { signAppParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test Param Without SignAlg + * + * @throws IOException IOException */ @Test public void testParamWithoutSignAlg() throws IOException { @@ -148,12 +156,14 @@ public class HapSIgnToolTest { signAppParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test Param Without KeyPwd + * + * @throws IOException IOException */ @Test public void testParamWithoutKeyPwd() throws IOException { @@ -170,11 +180,13 @@ public class HapSIgnToolTest { signAppParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test Param Without AppCertFile + * + * @throws IOException IOException */ @Test public void testParamWithoutAppCertFile() throws IOException { @@ -191,11 +203,13 @@ public class HapSIgnToolTest { signAppParameters.setKeyStoreFile("../../tools/ohtest_pass.jks"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } /** * test Param Without KeyStoreFile + * + * @throws IOException IOException */ @Test public void testParamWithoutKeyStoreFile() throws IOException { @@ -212,7 +226,7 @@ public class HapSIgnToolTest { signAppParameters.setAppCertFile("../../tools/app1.pem"); signAppParameters.setKeystorePwd("123456".toCharArray()); signAppParameters.setOutFile(outputFile.getCanonicalPath()); - Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), Error.SUCCESS_CODE); + Assertions.assertNotSame(HapSignTool.signApp(signAppParameters).getErrCode(), ERROR.SUCCESS_CODE); } private static class Cleanable { diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java index 2aaaaa7cf83329e58e0f8ad1d9c29ff3d32be5b7..489e6bd4f09014d9b7d0d3d0f9291fcbd3f44f9d 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.adapter; import com.ohos.hapsigntool.entity.Options; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyCertificateChainException; import com.ohos.hapsigntool.utils.KeyPairTools; import com.ohos.hapsigntool.utils.KeyStoreHelper; @@ -186,7 +186,7 @@ public class LocalizationAdapter { if (keyStoreHelper == null) { initKeyStore(); } - ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(alias), Error.ACCESS_ERROR, "Alias could not be empty"); + ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(alias), ERROR.ACCESS_ERROR, "Alias could not be empty"); KeyPair keyPair = null; if (keyStoreHelper.hasAlias(alias)) { keyPair = keyStoreHelper.loadKeyPair(alias, keyPwd); @@ -198,7 +198,7 @@ public class LocalizationAdapter { keyStoreHelper.store(alias, keyPwd, keyPair, null); } } - ValidateUtils.throwIfNotMatches(keyPair != null, Error.PARAM_NOT_EXIST_ERROR, + ValidateUtils.throwIfNotMatches(keyPair != null, ERROR.PARAM_NOT_EXIST_ERROR, String.format("%s: '%s' is not exist in %s", Options.KEY_ALIAS, alias, keyStoreHelper.getKeyStorePath())); return keyPair; @@ -218,7 +218,7 @@ public class LocalizationAdapter { ValidateUtils.throwIfNotMatches( certificates.size() >= MIN_CERT_CHAIN_SIZE && certificates.size() <= MAX_CERT_CHAIN_SIZE, - Error.NOT_SUPPORT_ERROR, String.format("Profile cert '%s' must a cert chain", certPath) + ERROR.NOT_SUPPORT_ERROR, String.format("Profile cert '%s' must a cert chain", certPath) ); return certificates; } @@ -261,20 +261,20 @@ public class LocalizationAdapter { * @return certificates */ public List getCertsFromFile(String certPath, String logTitle) { - ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(certPath), Error.PARAM_NOT_EXIST_ERROR, + ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(certPath), ERROR.PARAM_NOT_EXIST_ERROR, String.format("Params '%s' not exist", logTitle)); File certFile = new File(certPath); - ValidateUtils.throwIfNotMatches(certFile.exists(), Error.FILE_NOT_FOUND, + ValidateUtils.throwIfNotMatches(certFile.exists(), ERROR.FILE_NOT_FOUND, String.format("%s: '%s' not exist", logTitle, certPath)); List certificates = null; try { certificates = CertUtils.generateCertificates(FileUtils.readFile(certFile)); } catch (IOException | CertificateException | VerifyCertificateChainException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); } - ValidateUtils.throwIfNotMatches(certificates != null && certificates.size() > 0, Error.READ_FILE_ERROR, + ValidateUtils.throwIfNotMatches(certificates != null && certificates.size() > 0, ERROR.READ_FILE_ERROR, String.format("Read fail from %s, bot found certificates", certPath)); return certificates; } @@ -387,7 +387,7 @@ public class LocalizationAdapter { */ public String getInFile() { String file = options.getString(Options.IN_FILE); - ValidateUtils.throwIfNotMatches(new File(file).exists(), Error.FILE_NOT_FOUND, + ValidateUtils.throwIfNotMatches(new File(file).exists(), ERROR.FILE_NOT_FOUND, String.format("Required %s: '%s' not exist", Options.IN_FILE, file)); return file; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/CertTools.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/CertTools.java index 790f863ec572055f3fc50dbb90419e9a4747f9b0..0d0ab3d56c2b7748f7fb236b4a3e94ba03164bc9 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/CertTools.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/CertTools.java @@ -20,7 +20,7 @@ import com.ohos.hapsigntool.cert.CertLevel; import com.ohos.hapsigntool.adapter.LocalizationAdapter; import com.ohos.hapsigntool.entity.Options; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.CertUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -83,7 +83,7 @@ public final class CertTools { .build(adapter.getSignAlg()); } catch (IOException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return null; } @@ -107,7 +107,7 @@ public final class CertTools { .build(adapter.getSignAlg()); } catch (IOException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return null; } @@ -135,7 +135,7 @@ public final class CertTools { .build(adapter.getSignAlg()); } catch (IOException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return null; } @@ -162,7 +162,7 @@ public final class CertTools { .build(adapter.getSignAlg()); } catch (IOException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return null; } @@ -184,7 +184,7 @@ public final class CertTools { return csr.getEncoded(); } catch (IOException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CSR_ERROR, "Not support " + subject); + CustomException.throwException(ERROR.IO_CSR_ERROR, "Not support " + subject); return NO_CSR; } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java index 81a4c4c337b84043f24c796fe4a0073d1abe5b15..7eee7c07a1d40ddb37be068a44e19c529340a1d5 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java @@ -19,7 +19,7 @@ import com.google.gson.JsonParser; import com.ohos.hapsigntool.entity.Options; import com.ohos.hapsigntool.adapter.LocalizationAdapter; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyException; import com.ohos.hapsigntool.hap.provider.LocalJKSSignProvider; import com.ohos.hapsigntool.hap.provider.RemoteSignProvider; @@ -152,14 +152,14 @@ public class SignToolServiceImpl implements ServiceApi { String iksFile = options.getString(Options.ISSUER_KEY_STORE_FILE); if (isEmpty) { if (!StringUtils.isEmpty(iksFile) && !ksFile.equals(iksFile)) { - CustomException.throwException(Error.WRITE_FILE_ERROR, + CustomException.throwException(ERROR.WRITE_FILE_ERROR, String.format("Parameter '%s' and parameter '%s' are inconsistent", ksFile, iksFile)); } if (options.containsKey(Options.ISSUER_KEY_STORE_RIGHTS)) { boolean isEqual = Arrays.equals(options.getChars(Options.KEY_STORE_RIGHTS), options.getChars(Options.ISSUER_KEY_STORE_RIGHTS)); if (!isEqual) { - CustomException.throwException(Error.WRITE_FILE_ERROR, + CustomException.throwException(ERROR.WRITE_FILE_ERROR, String.format("Parameter '%s' and parameter '%s' are inconsistent", Options.KEY_STORE_RIGHTS, Options.ISSUER_KEY_STORE_RIGHTS)); } @@ -288,7 +288,7 @@ public class SignToolServiceImpl implements ServiceApi { logger.error(exception.getMessage()); isSign = false; } catch (VerifyException e) { - CustomException.throwException(Error.VERIFY_ERROR, "Verify Profile Failed! " + e.getMessage()); + CustomException.throwException(ERROR.VERIFY_ERROR, "Verify Profile Failed! " + e.getMessage()); isSign = false; } return isSign; @@ -350,7 +350,7 @@ public class SignToolServiceImpl implements ServiceApi { FileUtils.write(content.getBytes(StandardCharsets.UTF_8), new File(file)); } catch (IOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.WRITE_FILE_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); } } } @@ -373,7 +373,7 @@ public class SignToolServiceImpl implements ServiceApi { return true; } catch (CertificateException | IOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.WRITE_FILE_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } } @@ -399,7 +399,7 @@ public class SignToolServiceImpl implements ServiceApi { return true; } catch (CertificateException | IOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.WRITE_FILE_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java index 2d569e15abbe2efe9280751b0d7a5602faae0dc1..a9cc7b61261957dad22eda96299231a0874a531b 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.cert; import com.ohos.hapsigntool.api.ServiceApi; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.CertUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -88,7 +88,7 @@ public class CertBuilder { request = new PKCS10CertificationRequest(csr); } catch (IOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CSR_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CSR_ERROR, exception.getMessage()); } x509v3CertificateBuilder = new X509v3CertificateBuilder( issuer, CertUtils.randomSerial(), Date.from(notBefore.atZone(ZoneId.systemDefault()).toInstant()), @@ -100,10 +100,10 @@ public class CertBuilder { extUtils.createSubjectKeyIdentifier(request.getSubjectPublicKeyInfo())); } catch (NoSuchAlgorithmException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.NOT_SUPPORT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } catch (CertIOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } } @@ -123,10 +123,10 @@ public class CertBuilder { } } catch (NoSuchAlgorithmException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.NOT_SUPPORT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } catch (CertIOException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return this; } @@ -217,16 +217,16 @@ public class CertBuilder { cert.verify(keyPair.getPublic()); } catch (InvalidKeyException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.KEY_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.KEY_ERROR, exception.getMessage()); } catch (SignatureException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.SIGN_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.SIGN_ERROR, exception.getMessage()); } catch (CertificateException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.CERTIFICATE_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.CERTIFICATE_ERROR, exception.getMessage()); } catch (NoSuchAlgorithmException | NoSuchProviderException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.NOT_SUPPORT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } return cert; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/entity/Options.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/entity/Options.java index e2457f84387dc4618d2fde9bbdbd35e347a15d09..e7335ead057d18cb8230190adcc0ba8ef2406fe9 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/entity/Options.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/entity/Options.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.entity; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -259,7 +259,7 @@ public class Options extends HashMap { public void required(String... keys) { for (String key : keys) { if (!isEmpty(key) && !this.containsKey(key)) { - CustomException.throwException(Error.COMMAND_ERROR, String.format("Params '%s' is required", key)); + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("Params '%s' is required", key)); } } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/CustomException.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/CustomException.java index 6c28cee43a979b92e69a9e4e59955d0882e816bc..e909e350f6d9598c8782850a8882bc9ec95c6d5b 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/CustomException.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/CustomException.java @@ -23,7 +23,7 @@ import java.util.Locale; * @since 2021/12/28 */ public class CustomException extends RuntimeException { - private Error error; + private ERROR error; private String message; @@ -33,7 +33,7 @@ public class CustomException extends RuntimeException { * @param error Error enum to throw * @param message Error msg to throw */ - CustomException(Error error, String message) { + CustomException(ERROR error, String message) { super(String.format(Locale.ROOT, "%s, code: %d. Details: %s", error.toString(), error.getErrorCode(), message)); this.error = error; @@ -46,15 +46,15 @@ public class CustomException extends RuntimeException { * @param error Error enum to throw * @param message Error msg to throw */ - public static void throwException(Error error, String message) { + public static void throwException(ERROR error, String message) { throw new CustomException(error, message); } - public Error getError() { + public ERROR getError() { return error; } - public void setError(Error error) { + public void setError(ERROR error) { this.error = error; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/Error.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ERROR.java similarity index 98% rename from hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/Error.java rename to hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ERROR.java index 0c4848fce552238eb0915da9530b3e82a4210ec3..e156c467994cc56caab4eb69ef97bb5065c278c6 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/Error.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ERROR.java @@ -20,7 +20,7 @@ package com.ohos.hapsigntool.error; * * @since 2021/12/28 */ -public enum Error { +public enum ERROR { /** * success return code */ @@ -117,7 +117,7 @@ public enum Error { return this.errorCode; } - Error(int code) { + ERROR(int code) { this.errorCode = code; } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ParamException.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ParamException.java index 4fb9490d2ba5b34ead41826b1622e088f27a79bb..0373d0be4fa76fc06dbf22d6489c4901ac4ffb89 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ParamException.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/error/ParamException.java @@ -22,7 +22,7 @@ package com.ohos.hapsigntool.error; */ public class ParamException extends Exception { /** - *ParamException + * ParamException * * @param key key */ diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/ProfileSignTool.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/ProfileSignTool.java index 96b9e8c1a6022454a93931249429f636f79d2e02..a78039834ca3d42ae0de7e3d9a0f88b229e2bd4b 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/ProfileSignTool.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/ProfileSignTool.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.profile; import com.ohos.hapsigntool.adapter.LocalizationAdapter; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyException; import com.ohos.hapsigntool.profile.model.VerificationResult; import com.ohos.hapsigntool.signer.ISigner; @@ -93,9 +93,9 @@ public final class ProfileSignTool { try { verificationResult = verifyHelper.verify(p7b); } catch (VerifyException e) { - CustomException.throwException(Error.VERIFY_ERROR, "Generate Profile Failed! " + e.getMessage()); + CustomException.throwException(ERROR.VERIFY_ERROR, "Generate Profile Failed! " + e.getMessage()); } - ValidateUtils.throwIfNotMatches(verificationResult.isVerifiedPassed(), Error.SIGN_ERROR, + ValidateUtils.throwIfNotMatches(verificationResult.isVerifiedPassed(), ERROR.SIGN_ERROR, verificationResult.getMessage()); return p7b; } @@ -135,7 +135,7 @@ public final class ProfileSignTool { return contentInfo.getEncoded("DER"); } catch (OperatorCreationException | IOException | CertificateEncodingException | CRLException e) { LOGGER.debug(e.getMessage(), e); - CustomException.throwException(Error.SIGN_ERROR, e.getMessage()); + CustomException.throwException(ERROR.SIGN_ERROR, e.getMessage()); } return NO_BYTE; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/VerifyHelper.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/VerifyHelper.java index f893cd6481dabb66794aa64e5833cca8559f9c30..ab36eefefa675c8953f404b4fda8b06730a11e26 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/VerifyHelper.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/VerifyHelper.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.profile; import com.google.gson.JsonObject; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyException; import com.ohos.hapsigntool.hap.verify.VerifyUtils; import com.ohos.hapsigntool.profile.model.VerificationResult; @@ -87,10 +87,10 @@ public class VerifyHelper implements IProvisionVerifier { Signature signature = Signature.getInstance(algorithm); signature.initVerify(cert); signature.update(unsignedData); - ValidateUtils.throwIfNotMatches(signature.verify(signedData), Error.SIGN_ERROR, "Signature not matched!"); + ValidateUtils.throwIfNotMatches(signature.verify(signedData), ERROR.SIGN_ERROR, "Signature not matched!"); } catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.SIGN_ERROR, "Failed to verify signature: " + exception.getMessage()); + CustomException.throwException(ERROR.SIGN_ERROR, "Failed to verify signature: " + exception.getMessage()); } } @@ -103,10 +103,10 @@ public class VerifyHelper implements IProvisionVerifier { public static List certStoreToCertList(Store certificates) { String errorMsg = "Verify failed, not found cert chain"; JcaX509CertificateConverter converter = new JcaX509CertificateConverter(); - ValidateUtils.throwIfMatches(certificates == null, Error.VERIFY_ERROR, errorMsg); + ValidateUtils.throwIfMatches(certificates == null, ERROR.VERIFY_ERROR, errorMsg); Collection matches = certificates.getMatches(null); ValidateUtils.throwIfMatches(matches == null || !matches.iterator().hasNext(), - Error.VERIFY_ERROR, errorMsg); + ERROR.VERIFY_ERROR, errorMsg); Iterator iterator = matches.iterator(); List certificateList = new ArrayList<>(); @@ -117,9 +117,9 @@ public class VerifyHelper implements IProvisionVerifier { } } catch (CertificateException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.VERIFY_ERROR, errorMsg); + CustomException.throwException(ERROR.VERIFY_ERROR, errorMsg); } - ValidateUtils.throwIfMatches(certificateList.size() == 0, Error.VERIFY_ERROR, errorMsg); + ValidateUtils.throwIfMatches(certificateList.size() == 0, ERROR.VERIFY_ERROR, errorMsg); return certificateList; } @@ -191,12 +191,12 @@ public class VerifyHelper implements IProvisionVerifier { try { cmsSignedData = new CMSSignedData(p7b); boolean verifyResult = VerifyUtils.verifyCmsSignedData(cmsSignedData); - ValidateUtils.throwIfNotMatches(verifyResult, Error.VERIFY_ERROR, + ValidateUtils.throwIfNotMatches(verifyResult, ERROR.VERIFY_ERROR, "Failed to verify BC signatures"); return cmsSignedData; } catch (CMSException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.VERIFY_ERROR, "Failed to verify BC signatures: " + CustomException.throwException(ERROR.VERIFY_ERROR, "Failed to verify BC signatures: " + exception.getMessage()); } return cmsSignedData; diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/BundleInfo.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/BundleInfo.java index 5cb4c64aa67dc681a037b897925f2ce0ffee779c..dcf012da5526de87f5af548701ec4ac026fc8c54 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/BundleInfo.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/BundleInfo.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.profile.model; import com.google.gson.annotations.SerializedName; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.ValidateUtils; /** @@ -122,10 +122,10 @@ public class BundleInfo { public void enforceValid(String buildType) { if (Provision.isBuildTypeRelease(buildType)) { ValidateUtils.throwIfMatches(this.distributionCertificate == null, - Error.SIGN_ERROR, "Require cert in bundleInfo!"); + ERROR.SIGN_ERROR, "Require cert in bundleInfo!"); } else { ValidateUtils.throwIfMatches(this.developmentCertificate == null, - Error.SIGN_ERROR, "Require cert in bundleInfo!"); + ERROR.SIGN_ERROR, "Require cert in bundleInfo!"); } } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/DebugInfo.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/DebugInfo.java index ac0fad672a3307f8cf1654dea696c3f5c3e35960..9fcd714c62f3116c617e24b14f07d37223e45eb6 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/DebugInfo.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/DebugInfo.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.profile.model; import com.google.gson.annotations.SerializedName; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.ValidateUtils; import java.util.List; @@ -72,9 +72,9 @@ public class DebugInfo { */ public void enforceValid() { if (this.deviceIds != null) { - ValidateUtils.throwIfMatches(this.deviceIds.size() > MAX_DEBUG_DEVICE_NUM, Error.SIGN_ERROR, + ValidateUtils.throwIfMatches(this.deviceIds.size() > MAX_DEBUG_DEVICE_NUM, ERROR.SIGN_ERROR, "Support at most: 100 devices!"); - ValidateUtils.throwIfMatches(!this.isDeviceIdTypeValid(), Error.SIGN_ERROR, + ValidateUtils.throwIfMatches(!this.isDeviceIdTypeValid(), ERROR.SIGN_ERROR, "Device id type must be sn or udid, current is " + this.deviceIdType); } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Provision.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Provision.java index 725251f7fc8497a485bcb5fbc93a308e20bdc486..50c19b5ea5227de44c6e81a426edf5231704f0e3 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Provision.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Provision.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.profile.model; import com.google.gson.annotations.SerializedName; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.ValidateUtils; /** @@ -195,9 +195,9 @@ public class Provision { */ public static void enforceValid(Provision provision) { ValidateUtils.throwIfMatches(provision.type == null || !isBuildTypeValid(provision.type), - Error.SIGN_ERROR, "Require build type must be debug or release, current is :" + provision.type); + ERROR.SIGN_ERROR, "Require build type must be debug or release, current is :" + provision.type); - ValidateUtils.throwIfMatches(provision.bundleInfo == null, Error.SIGN_ERROR, + ValidateUtils.throwIfMatches(provision.bundleInfo == null, ERROR.SIGN_ERROR, "Require bundleInfo in provision!"); provision.bundleInfo.enforceValid(provision.type); } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Validity.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Validity.java index 9bb3585eab2cfac065a8a0d5069882112f818faa..021519730510fb883149a8fb4de862ba2593a736 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Validity.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/profile/model/Validity.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.profile.model; import com.google.gson.annotations.SerializedName; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.ValidateUtils; /** @@ -46,9 +46,9 @@ public class Validity { * Validate attribute. */ public void enforceValid() { - ValidateUtils.throwIfMatches(this.notBefore == 0L, Error.SIGN_ERROR, "Require notBefore in validity!"); - ValidateUtils.throwIfMatches(this.notAfter == 0L, Error.SIGN_ERROR, "Require notAfter in validity!"); - ValidateUtils.throwIfMatches(this.notBefore >= this.notAfter, Error.SIGN_ERROR, + ValidateUtils.throwIfMatches(this.notBefore == 0L, ERROR.SIGN_ERROR, "Require notBefore in validity!"); + ValidateUtils.throwIfMatches(this.notAfter == 0L, ERROR.SIGN_ERROR, "Require notAfter in validity!"); + ValidateUtils.throwIfMatches(this.notBefore >= this.notAfter, ERROR.SIGN_ERROR, "Require notBefore less than notAfter in validity!"); } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java index 068f08d06c90ce8fd0b44a7872721f692d51d495..d71d77480fdab33d1fb1b3476a02dce5e0442374 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.signer; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -86,7 +86,7 @@ public class LocalSigner implements ISigner { } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | SignatureException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.SIGN_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.SIGN_ERROR, exception.getMessage()); } return signData; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/SignerFactory.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/SignerFactory.java index 694dd61f07eb0a588f1e0e16851c9421b2220c6b..8649eee636234db7ad8e4e7e19c9abb49d43a522 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/SignerFactory.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/SignerFactory.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.signer; import com.ohos.hapsigntool.adapter.LocalizationAdapter; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.utils.StringUtils; import org.apache.logging.log4j.LogManager; @@ -137,7 +137,7 @@ public class SignerFactory { private File getClassLocation() { String jarPath = SignerFactory.class.getProtectionDomain().getCodeSource().getLocation().getFile(); if (StringUtils.isEmpty(jarPath)) { - CustomException.throwException(Error.COMMAND_ERROR, "class path is empty"); + CustomException.throwException(ERROR.COMMAND_ERROR, "class path is empty"); } try { jarPath = URLDecoder.decode(URLEncoder.encode(jarPath, "utf-8"), "utf-8"); @@ -146,7 +146,7 @@ public class SignerFactory { } File jarFile = new File(jarPath); if (!jarFile.exists()) { - CustomException.throwException(Error.COMMAND_ERROR, "class path" + jarFile + "is not exists"); + CustomException.throwException(ERROR.COMMAND_ERROR, "class path" + jarFile + "is not exists"); } if (jarFile.isFile()) { return jarFile.getParentFile(); diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertChainUtils.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertChainUtils.java index d9f3f4d995141a4277d7c3065a0a6929e9ff5935..3ab6ca970bea2568cfab8fb61d52ec6ef25fb928 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertChainUtils.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertChainUtils.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.utils; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -106,14 +106,14 @@ public class CertChainUtils { CertPathValidator validator = CertPathValidator.getInstance("PKIX"); CertPathValidatorResult validatorResult = validator.validate(certPath, params); ValidateUtils.throwIfNotMatches(validatorResult instanceof PKIXCertPathValidatorResult, - Error.VERIFY_ERROR, "Validator result not target type"); + ERROR.VERIFY_ERROR, "Validator result not target type"); if (validatorResult instanceof PKIXCertPathValidatorResult) { PKIXCertPathValidatorResult pkixValidatorResult = (PKIXCertPathValidatorResult) validatorResult; ValidateUtils.throwIfNotMatches(params.getTrustAnchors().contains(pkixValidatorResult.getTrustAnchor()), - Error.VERIFY_ERROR, "Anchor is not trusted: " + Base64.getEncoder().encodeToString( + ERROR.VERIFY_ERROR, "Anchor is not trusted: " + Base64.getEncoder().encodeToString( pkixValidatorResult.getTrustAnchor().getTrustedCert().getEncoded())); } else { - CustomException.throwException(Error.VERIFY_ERROR, "Validator result not target type"); + CustomException.throwException(ERROR.VERIFY_ERROR, "Validator result not target type"); } } catch (IOException | GeneralSecurityException exception) { throw new VerifyException("Cert chain verify failed! " + exception.getMessage()); diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertUtils.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertUtils.java index a2deeb8defdf8d6f36f348c8b8c86e9ff2fb02af..714ad74beef82f2b6e3eece4e16a34ce045fcca4 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertUtils.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/CertUtils.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.utils; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyCertificateChainException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -167,7 +167,7 @@ public final class CertUtils { dn = new X500Name(nameString); } catch (IllegalArgumentException | IndexOutOfBoundsException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.COMMAND_ERROR, + CustomException.throwException(ERROR.COMMAND_ERROR, String.format("Error params near: %s. Reason: %s", nameString, exception.getMessage())); } return dn; @@ -181,14 +181,14 @@ public final class CertUtils { */ private static void checkDN(String nameString) { String errorMsg = String.format("Format error, must be \"X=xx,XX=xxx,...\", please check: \"%s\"", nameString); - ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(nameString), Error.COMMAND_ERROR, errorMsg); + ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(nameString), ERROR.COMMAND_ERROR, errorMsg); String[] pairs = nameString.split(","); for (String pair : pairs) { - ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(nameString.trim()), Error.COMMAND_ERROR, errorMsg); + ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(nameString.trim()), ERROR.COMMAND_ERROR, errorMsg); String[] kvPair = pair.split("="); - ValidateUtils.throwIfNotMatches(kvPair.length == SECOND_INDEX, Error.COMMAND_ERROR, errorMsg); + ValidateUtils.throwIfNotMatches(kvPair.length == SECOND_INDEX, ERROR.COMMAND_ERROR, errorMsg); // Key will be checked in X500NameStyle.attrNameToOID - ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(kvPair[1].trim()), Error.COMMAND_ERROR, errorMsg); + ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(kvPair[1].trim()), ERROR.COMMAND_ERROR, errorMsg); } } @@ -273,7 +273,7 @@ public final class CertUtils { */ public static ContentSigner createFixedContentSigner(PrivateKey privateKey, String signAlgorithm) { Matcher matcher = SIGN_ALGORITHM_PATTERN.matcher(signAlgorithm); - ValidateUtils.throwIfNotMatches(matcher.matches(), Error.NOT_SUPPORT_ERROR, "Not Support " + signAlgorithm); + ValidateUtils.throwIfNotMatches(matcher.matches(), ERROR.NOT_SUPPORT_ERROR, "Not Support " + signAlgorithm); String signAlg = signAlgorithm; // Auto fix signAlgorithm error if (privateKey instanceof ECPrivateKey && signAlgorithm.contains("RSA")) { @@ -290,7 +290,7 @@ public final class CertUtils { return jcaContentSignerBuilder.build(privateKey); } catch (OperatorCreationException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.OPERATOR_CREATION_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.OPERATOR_CREATION_ERROR, exception.getMessage()); } return null; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/FileUtils.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/FileUtils.java index 34242e188ec51def0d7fbe761e8d95b8c553595b..b61266aeeb79be65d51c146f8f46d65c6e84570f 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/FileUtils.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/FileUtils.java @@ -17,7 +17,7 @@ package com.ohos.hapsigntool.utils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -256,7 +256,7 @@ public final class FileUtils { public static void validFileType(String filePath, String... types) { String suffix = getSuffix(filePath); ValidateUtils.throwIfNotMatches(!StringUtils.isEmpty(suffix), - Error.NOT_SUPPORT_ERROR, "Not support file: " + filePath); + ERROR.NOT_SUPPORT_ERROR, "Not support file: " + filePath); boolean isMatches = false; for (String type : types) { if (StringUtils.isEmpty(type)) { @@ -267,7 +267,7 @@ public final class FileUtils { break; } } - ValidateUtils.throwIfNotMatches(isMatches, Error.NOT_SUPPORT_ERROR, "Not support file: " + filePath); + ValidateUtils.throwIfNotMatches(isMatches, ERROR.NOT_SUPPORT_ERROR, "Not support file: " + filePath); } /** diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyPairTools.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyPairTools.java index d6c9066e01b33131a683fcf7a6fdeaea09bb3e96..7fd288ea40e267d076f3eb9529bb03cc77034f90 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyPairTools.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyPairTools.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.utils; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bouncycastle.util.encoders.Base64; @@ -94,7 +94,7 @@ public final class KeyPairTools { */ public static KeyPair generateKeyPair(String algorithm, int keySize) { if (algorithm == null) { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, "Not support algorithm: null"); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "Not support algorithm: null"); } String alg = algorithm; if (ECC_INPUT.equalsIgnoreCase(alg)) { @@ -102,12 +102,12 @@ public final class KeyPairTools { } if (RSA.equalsIgnoreCase(alg)) { ValidateUtils.throwIfNotMatches((keySize == RSA_2048 || keySize == RSA_3072 || keySize == RSA_4096), - Error.NOT_SUPPORT_ERROR, "Algorithm 'RSA' not support size: " + keySize); + ERROR.NOT_SUPPORT_ERROR, "Algorithm 'RSA' not support size: " + keySize); } else if (ECC.equalsIgnoreCase(alg)) { ValidateUtils.throwIfNotMatches((keySize == NIST_P_256 || keySize == NIST_P_384), - Error.NOT_SUPPORT_ERROR, "Algorithm 'ECC' not support size: " + keySize); + ERROR.NOT_SUPPORT_ERROR, "Algorithm 'ECC' not support size: " + keySize); } else { - CustomException.throwException(Error.NOT_SUPPORT_ERROR, "Not support algorithm: " + alg); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, "Not support algorithm: " + alg); } try { @@ -116,7 +116,7 @@ public final class KeyPairTools { return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { LOGGER.debug(e.getMessage(), e); - CustomException.throwException(Error.NOT_SUPPORT_ERROR, e.getMessage()); + CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, e.getMessage()); return null; } } @@ -145,7 +145,7 @@ public final class KeyPairTools { result = KeyFactory.getInstance(algorithm).generatePublic(spec); } catch (InvalidKeySpecException | NoSuchAlgorithmException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); } return result; } @@ -164,7 +164,7 @@ public final class KeyPairTools { result = KeyFactory.getInstance(algorithm).generatePrivate(spec); } catch (InvalidKeySpecException | NoSuchAlgorithmException exception) { LOGGER.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); } return result; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java index 2b0b8297b20ed096c1dcfa09fc04465b4c5879bd..9aa54544c744074003fb227b552d59034211c46f 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.utils; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bouncycastle.asn1.x500.X500NameBuilder; @@ -106,7 +106,7 @@ public class KeyStoreHelper { */ public KeyStoreHelper(String keyStorePath, char[] storePwd) { char[] pwd = storePwd; - ValidateUtils.throwIfMatches(StringUtils.isEmpty(keyStorePath), Error.COMMAND_ERROR, + ValidateUtils.throwIfMatches(StringUtils.isEmpty(keyStorePath), ERROR.COMMAND_ERROR, "Missed params: 'keyStorePath'"); if (pwd == null) { pwd = new char[0]; @@ -125,7 +125,7 @@ public class KeyStoreHelper { } } catch (IOException | NoSuchAlgorithmException | CertificateException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, "Init keystore failed: " + exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, "Init keystore failed: " + exception.getMessage()); } finally { FileUtils.close(fis); } @@ -141,7 +141,7 @@ public class KeyStoreHelper { * @param alias alias of key */ public void errorOnExist(String alias) { - ValidateUtils.throwIfMatches(this.hasAlias(alias), Error.ACCESS_ERROR, + ValidateUtils.throwIfMatches(this.hasAlias(alias), ERROR.ACCESS_ERROR, String.format("Could not overwrite! Already exist '%s' in %s", alias, this.keyStorePath)); } @@ -151,7 +151,7 @@ public class KeyStoreHelper { * @param alias alias of key */ public void errorIfNotExist(String alias) { - ValidateUtils.throwIfNotMatches(this.hasAlias(alias), Error.FILE_NOT_FOUND, + ValidateUtils.throwIfNotMatches(this.hasAlias(alias), ERROR.FILE_NOT_FOUND, String.format("Not exist '%s' in %s", alias, this.keyStorePath)); } @@ -166,7 +166,7 @@ public class KeyStoreHelper { return keyStore.containsAlias(alias); } catch (KeyStoreException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); return false; } } @@ -203,10 +203,10 @@ public class KeyStoreHelper { } } catch (KeyStoreException | NoSuchAlgorithmException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); } catch (UnrecoverableKeyException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.ACCESS_ERROR, "Password error of '" + alias + "'"); + CustomException.throwException(ERROR.ACCESS_ERROR, "Password error of '" + alias + "'"); } return null; } @@ -237,7 +237,7 @@ public class KeyStoreHelper { * @return certificates of alias */ public List loadCertificates(String alias) { - ValidateUtils.throwIfNotMatches(this.hasAlias(alias), Error.FILE_NOT_FOUND, + ValidateUtils.throwIfNotMatches(this.hasAlias(alias), ERROR.FILE_NOT_FOUND, String.format("Not found '%s' in %s", alias, this.keyStorePath)); List certificates = new ArrayList<>(); @@ -255,10 +255,10 @@ public class KeyStoreHelper { } } catch (KeyStoreException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.KEYSTORE_OPERATION_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.KEYSTORE_OPERATION_ERROR, exception.getMessage()); } - ValidateUtils.throwIfNotMatches(certificates.size() > 0, Error.ACCESS_ERROR, + ValidateUtils.throwIfNotMatches(certificates.size() > 0, ERROR.ACCESS_ERROR, "No usable cert found in " + this.keyStorePath); return certificates; } @@ -290,7 +290,7 @@ public class KeyStoreHelper { fos.flush(); } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.WRITE_FILE_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } return true; @@ -317,7 +317,7 @@ public class KeyStoreHelper { .getCertificate(certificateBuilder.build(contentSigner)); } catch (CertificateException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.IO_CERT_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); return null; } } @@ -341,7 +341,7 @@ public class KeyStoreHelper { } } catch (KeyStoreException exception) { logger.debug(exception.getMessage(), exception); - CustomException.throwException(Error.KEYSTORE_OPERATION_ERROR, exception.getMessage()); + CustomException.throwException(ERROR.KEYSTORE_OPERATION_ERROR, exception.getMessage()); } return typeKeyStore; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/ValidateUtils.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/ValidateUtils.java index 8ab384edb391c285107f6103448e8259674f1d75..b87df06713b5f6aac64ba44edf411717631440f2 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/ValidateUtils.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/ValidateUtils.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.utils; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; /** * ValidateUtils. @@ -35,7 +35,7 @@ public final class ValidateUtils { * @param errorMsg Error msg to throw * @throws CustomException CustomException */ - public static void throwIfNotMatches(boolean isMatch, Error error, String errorMsg) throws CustomException { + public static void throwIfNotMatches(boolean isMatch, ERROR error, String errorMsg) throws CustomException { if (!isMatch) { CustomException.throwException(error, errorMsg); } @@ -49,7 +49,7 @@ public final class ValidateUtils { * @param errorMsg Error msg to throw * @throws CustomException CustomException */ - public static void throwIfMatches(boolean isMatch, Error error, String errorMsg) throws CustomException { + public static void throwIfMatches(boolean isMatch, ERROR error, String errorMsg) throws CustomException { throwIfNotMatches(!isMatch, error, errorMsg); } } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/zip/Zip.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/zip/Zip.java index c15d1ac00b7ce8e1a8ad82514c0b46fa6fd5434c..7ab9bdc2ed2b9a31894b6837620bd6b1d93411f9 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/zip/Zip.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/zip/Zip.java @@ -16,7 +16,7 @@ package com.ohos.hapsigntool.zip; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.ZipException; import com.ohos.hapsigntool.utils.FileUtils; @@ -96,7 +96,7 @@ public class Zip { // 4. file all data - eocd - cd - entry = sign block signingBlock = getSigningBlock(inputFile); } catch (IOException e) { - CustomException.throwException(Error.ZIP_ERROR, e.getMessage()); + CustomException.throwException(ERROR.ZIP_ERROR, e.getMessage()); } } @@ -207,7 +207,7 @@ public class Zip { } FileUtils.writeByteToOutFile(endOfCentralDirectory.toBytes(), fos); } catch (IOException e) { - CustomException.throwException(Error.ZIP_ERROR, e.getMessage()); + CustomException.throwException(ERROR.ZIP_ERROR, e.getMessage()); } } @@ -246,7 +246,7 @@ public class Zip { } } } catch (ZipException e) { - CustomException.throwException(Error.ZIP_ERROR, e.getMessage()); + CustomException.throwException(ERROR.ZIP_ERROR, e.getMessage()); } } diff --git a/hapsigntool/hap_sign_tool_lib/src/test/java/com/ohos/hapsigntool/ProfileTest.java b/hapsigntool/hap_sign_tool_lib/src/test/java/com/ohos/hapsigntool/ProfileTest.java index d999f52d9d5f59280968513b91b627b0c5e1996a..a6366dd1b037364c2092173fff2c7e2936065b58 100644 --- a/hapsigntool/hap_sign_tool_lib/src/test/java/com/ohos/hapsigntool/ProfileTest.java +++ b/hapsigntool/hap_sign_tool_lib/src/test/java/com/ohos/hapsigntool/ProfileTest.java @@ -19,7 +19,7 @@ import com.ohos.hapsigntool.adapter.LocalizationAdapter; import com.ohos.hapsigntool.api.SignToolServiceImpl; import com.ohos.hapsigntool.entity.Options; import com.ohos.hapsigntool.error.CustomException; -import com.ohos.hapsigntool.error.Error; +import com.ohos.hapsigntool.error.ERROR; import com.ohos.hapsigntool.error.VerifyException; import com.ohos.hapsigntool.utils.KeyPairTools; import com.ohos.hapsigntool.profile.ProfileSignTool; @@ -148,7 +148,7 @@ public class ProfileTest { try { verificationResult = verifyHelper.verify(p7b); } catch (VerifyException e) { - CustomException.throwException(Error.VERIFY_ERROR, e.getMessage()); + CustomException.throwException(ERROR.VERIFY_ERROR, e.getMessage()); } assertTrue(verificationResult.isVerifiedPassed());