diff --git a/hapsigntool_cpp/api/src/cert_tools.cpp b/hapsigntool_cpp/api/src/cert_tools.cpp index 37826e945bafe816fc7caa4002d59ed89b9b3e2d..87b4815e8df1d79ac138abc8d5baba6b1bdbc04b 100644 --- a/hapsigntool_cpp/api/src/cert_tools.cpp +++ b/hapsigntool_cpp/api/src/cert_tools.cpp @@ -75,8 +75,44 @@ bool CertTools::SaveCertTofile(const std::string& filename, X509* cert) return true; } +static bool UpdateConstraint(Options* options) +{ + if (options->count(Options::BASIC_CONSTRAINTS)) { + std::string val = options->GetString(Options::BASIC_CONSTRAINTS); + if (!CmdUtil::String2Bool(options, Options::BASIC_CONSTRAINTS)) { + return false; + } + } else { + (*options)[Options::BASIC_CONSTRAINTS] = DEFAULT_BASIC_CONSTRAINTS; + } + + if (options->count(Options::BASIC_CONSTRAINTS_CRITICAL)) { + if (!CmdUtil::String2Bool(options, Options::BASIC_CONSTRAINTS_CRITICAL)) { + return false; + } + } else { + (*options)[Options::BASIC_CONSTRAINTS] = DEFAULT_BASIC_CONSTRAINTS_CRITICAL; + } + + if (options->count(Options::BASIC_CONSTRAINTS_CA)) { + if (!CmdUtil::String2Bool(options, Options::BASIC_CONSTRAINTS_CA)) { + return false; + } + } else { + (*options)[Options::BASIC_CONSTRAINTS] = DEFAULT_BASIC_CONSTRAINTS_CA; + } + return true; +} + bool CertTools::SetBisicConstraints(Options* options, X509* cert) { + /*Check here when the parameter is not entered through the command line */ + if (!(options->count(Options::BASIC_CONSTRAINTS) + && (*options)[Options::BASIC_CONSTRAINTS].index() == BASIC_NUMBER_TWO)) { + if (!UpdateConstraint(options)) { + return false; + } + } bool basicCon = options->GetBool(Options::BASIC_CONSTRAINTS); if (basicCon) { bool basicConstraintsCritical = options->GetBool(Options::BASIC_CONSTRAINTS_CRITICAL); diff --git a/hapsigntool_cpp/cmd/src/cmd_util.cpp b/hapsigntool_cpp/cmd/src/cmd_util.cpp index 1d87240d32e5b6728d6ffee723922fd61bb869c9..84d835cd13d4d9b35893cc90ed39e5e146601432 100644 --- a/hapsigntool_cpp/cmd/src/cmd_util.cpp +++ b/hapsigntool_cpp/cmd/src/cmd_util.cpp @@ -200,6 +200,9 @@ static bool outFilePath(Options* options) return false; } std::string parentPath = pat.parent_path(); + if (parentPath == "" || parentPath == "./") { + return true; + } if (!std::filesystem::exists(parentPath)) { PrintErrorNumberMsg("IO_ERROR", IO_ERROR, "output file parent directory'" + std::string(parentPath.c_str()) + "' not exist"); @@ -241,7 +244,7 @@ static bool UpdateParamForCheckSignAlg(ParamsSharedPtr param) if (options->count(Options::SIGN_ALG)) { std::string signAlg = options->GetString(Options::SIGN_ALG); if (signAlg != SIGN_ALG_SHA256 && signAlg != SIGN_ALG_SHA384) { - PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "'" + signAlg + "' parameter is incorrect"); + PrintErrorNumberMsg("NOT_SUPPORT_ERROR", NOT_SUPPORT_ERROR, "'" + signAlg + "' parameter is incorrect"); return false; } } @@ -257,8 +260,8 @@ static bool UpdateParamForInform(ParamsSharedPtr param) if (options->count(Options::INFORM)) { std::string inForm = options->GetString(Options::INFORM); if (!StringUtils::ContainsCase(ParamsRunTool::InformList, inForm)) { - PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "parameter '" + inForm - + "' format error, Inform only support zip/elf/bin"); + PrintErrorNumberMsg("NOT_SUPPORT_ERROR", NOT_SUPPORT_ERROR, "parameter '" + + inForm + "' format error, Inform only support zip/elf/bin"); return false; } } else { @@ -478,7 +481,7 @@ bool CmdUtil::VerifyTypes(const std::string& inputType) sets.insert("decipherOnly"); for (const auto& val : vecs) { if (sets.count(val) == 0) { - PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "Not support command param '" + val + "'"); return false; } diff --git a/hapsigntool_cpp/cmd/src/params_run_tool.cpp b/hapsigntool_cpp/cmd/src/params_run_tool.cpp index 2d161a61f6ef9da56f154046b42b1a3d906160f6..96d1fa6f2548578772fb8cb9f3f107375130dd39 100644 --- a/hapsigntool_cpp/cmd/src/params_run_tool.cpp +++ b/hapsigntool_cpp/cmd/src/params_run_tool.cpp @@ -121,7 +121,7 @@ bool ParamsRunTool::RunSignApp(Options* params, SignToolServiceImpl& api) } std::string inForm = params->GetString(Options::INFORM, ZIP); if (!StringUtils::IsEmpty(inForm) && !StringUtils::ContainsCase(InformList, inForm)) { - PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "parameter '" + inForm + PrintErrorNumberMsg("NOT_SUPPORT_ERROR", NOT_SUPPORT_ERROR, "parameter '" + inForm + "' format error, Inform only support zip/elf/bin"); return false; } @@ -385,7 +385,7 @@ bool ParamsRunTool::RunVerifyApp(Options* params, SignToolServiceImpl& api) } std::string inForm = params->GetString(Options::INFORM, ZIP); if (!StringUtils::ContainsCase(InformList, inForm)) { - PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "parameter '" + inForm + PrintErrorNumberMsg("NOT_SUPPORT_ERROR", NOT_SUPPORT_ERROR, "parameter '" + inForm + "' format error, Inform only support zip/elf/bin"); return false; } diff --git a/hapsigntool_cpp/codesigning/datastructure/src/code_sign_block.cpp b/hapsigntool_cpp/codesigning/datastructure/src/code_sign_block.cpp index 91fccfa35f7f2a4933cb2bb1c52ed7b84de59004..16b0e5eaf890eccc0cdf5587fa0db92d71bccf49 100644 --- a/hapsigntool_cpp/codesigning/datastructure/src/code_sign_block.cpp +++ b/hapsigntool_cpp/codesigning/datastructure/src/code_sign_block.cpp @@ -154,7 +154,7 @@ void CodeSignBlock::ComputeSegmentOffset() int segmentOffset = CodeSignBlockHeader::Size() + segmentHeaderList.size() * SegmentHeader::SEGMENT_HEADER_LENGTH + zeroPadding.size() + GetOneMerkleTreeByFileName("Hap").size(); - for (int i = 0; i < segmentHeaderList.size(); i++) { + for (std::vector::size_type i = 0; i < segmentHeaderList.size(); i++) { segmentHeaderList[i].SetSegmentOffset(static_cast(segmentOffset)); segmentOffset += segmentHeaderList[i].GetSegmentSize(); } diff --git a/hapsigntool_cpp/hap/provider/include/sign_provider.h b/hapsigntool_cpp/hap/provider/include/sign_provider.h index 685593925e632b0cf5af1a49dfba597581b0efab..2c67def498c7e2ec7f39d7abceffc57fbc838899 100644 --- a/hapsigntool_cpp/hap/provider/include/sign_provider.h +++ b/hapsigntool_cpp/hap/provider/include/sign_provider.h @@ -115,7 +115,6 @@ private: bool CheckSignatureAlg(); - static bool CheckStringToint(const std::string& in, int& out); int LoadOptionalBlock(const std::string& file, int type); bool CheckFile(const std::string& filePath); diff --git a/hapsigntool_cpp/hap/provider/src/sign_provider.cpp b/hapsigntool_cpp/hap/provider/src/sign_provider.cpp index de2d58ff1312468bc9a4b5a73f8e5eca10494c97..cdb3667e2ce6e252554be8506f5250b50c9b645d 100644 --- a/hapsigntool_cpp/hap/provider/src/sign_provider.cpp +++ b/hapsigntool_cpp/hap/provider/src/sign_provider.cpp @@ -55,7 +55,8 @@ bool SignProvider::InitSigerConfig(SignerConfig& signerConfig, STACK_OF(X509)* p return false; } int CompatibleVersion; - if (!CheckStringToint(signParams.at(ParamConstants::PARAM_BASIC_COMPATIBLE_VERSION), CompatibleVersion)) { + if (!StringUtils::CheckStringToint(signParams.at(ParamConstants::PARAM_BASIC_COMPATIBLE_VERSION), + CompatibleVersion)) { SIGNATURE_TOOLS_LOGE("[SignHap] CompatibleVersion String To int failed"); return false; } @@ -134,7 +135,7 @@ bool SignProvider::InitZipOutput(std::shared_ptr outputHap, std::string Path) { int alignment; - if (!CheckStringToint(signParams.at(ParamConstants::PARAM_BASIC_ALIGNMENT), alignment)) { + if (!StringUtils::CheckStringToint(signParams.at(ParamConstants::PARAM_BASIC_ALIGNMENT), alignment)) { SIGNATURE_TOOLS_LOGE("[signHap] alignment String To int failed"); inputStream->close(); tmpOutput->close(); @@ -647,17 +648,6 @@ void SignProvider::CheckSignAlignment() } } -bool SignProvider::CheckStringToint(const std::string& in, int& out) -{ - std::istringstream iss(in); - if ((iss >> out) && iss.eof()) { - return true; - } else { - SIGNATURE_TOOLS_LOGE("Invalid parameter: %s", in.c_str()); - return false; - } -} - bool SignProvider::CheckCompatibleVersion() { auto it = signParams.find(ParamConstants::PARAM_BASIC_COMPATIBLE_VERSION); @@ -667,7 +657,7 @@ bool SignProvider::CheckCompatibleVersion() } const std::string& compatibleApiVersionVal = it->second; int compatibleApiVersion; - if (!CheckStringToint(compatibleApiVersionVal, compatibleApiVersion)) { + if (!StringUtils::CheckStringToint(compatibleApiVersionVal, compatibleApiVersion)) { PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, "compatibleVersion Parameter is must integer"); return false; diff --git a/hapsigntool_cpp/utils/include/signature_tools_log.h b/hapsigntool_cpp/utils/include/signature_tools_log.h index f919423bded8dd58bedec4bd95c4f9ca353222c4..fc5af04a022984ef74309566471142e9400aa0e1 100644 --- a/hapsigntool_cpp/utils/include/signature_tools_log.h +++ b/hapsigntool_cpp/utils/include/signature_tools_log.h @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "signature_tools_errno.h" @@ -50,12 +53,10 @@ namespace SignatureTools { **/ inline void PrintErrorNumberMsg(const std::string& command, const int code, const std::string& details) { - time_t now = time(0); - if (!now) return; - char timebuffer[100] = { 0 }; - struct tm* time = localtime(&now); - if (!time && !strftime(timebuffer, sizeof(timebuffer), "%m-%d %H:%M:%S", time)) return; - std::cerr << timebuffer << " ERROR - " << command << ", code: " + auto now = std::chrono::system_clock::now(); + std::time_t nowTime = std::chrono::system_clock::to_time_t(now); + std::tm* localTime = std::localtime(&nowTime); + std::cerr << std::put_time(localTime, "%m-%d %H:%M:%S") << " ERROR - " << command << ", code: " << code << ". Details: " << details << std::endl; } @@ -66,18 +67,11 @@ inline void PrintErrorNumberMsg(const std::string& command, const int code, cons **/ inline void PrintMsg(const std::string& message) { - time_t now = time(0); - if (!now) { - return; - } - char timebuffer[100] = { 0 }; - struct tm* time = localtime(&now); - if (!time && !strftime(timebuffer, sizeof(timebuffer), "%m-%d %H:%M:%S", time)) { - return; - } - std::cout << timebuffer << " INFO - " << message << std::endl; + auto now = std::chrono::system_clock::now(); + std::time_t nowTime = std::chrono::system_clock::to_time_t(now); + std::tm* localTime = std::localtime(&nowTime); + std::cout << std::put_time(localTime, "%m-%d %H:%M:%S") << " INFO - " << message << std::endl; } - } // namespace SignatureTools } // namespace OHOS #endif // SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H \ No newline at end of file diff --git a/hapsigntool_cpp/utils/src/hash_utils.cpp b/hapsigntool_cpp/utils/src/hash_utils.cpp index 7d4f58d05b0311264021e9f117f2327e08ec9a44..9f7434f4e590e301e415fafad71410a5dc010847 100644 --- a/hapsigntool_cpp/utils/src/hash_utils.cpp +++ b/hapsigntool_cpp/utils/src/hash_utils.cpp @@ -98,7 +98,7 @@ std::vector HashUtils::GetFileDigest(const std::string& inputFile, const digestUtils.AddData(str); } std::string digest = digestUtils.Result(DigestUtils::Type::BINARY); - for (int i = 0; i < digest.size(); i++) { + for (std::string::size_type i = 0; i < digest.size(); i++) { result.push_back(digest[i]); } return result; @@ -133,7 +133,7 @@ std::vector HashUtils::GetDigestFromBytes(const std::vector& fil } std::string digest = digestUtils.Result(DigestUtils::Type::BINARY); std::vector result; - for (int i = 0; i < digest.size(); i++) { + for (std::string::size_type i = 0; i < digest.size(); i++) { result.push_back(digest[i]); } return result; @@ -145,7 +145,7 @@ std::vector HashUtils::GetByteDigest(const std::string& str, int count, DigestUtils digestUtils(HASH_SHA256); digestUtils.AddData(str); std::string digest = digestUtils.Result(DigestUtils::Type::BINARY); - for (int i = 0; i < digest.size(); i++) { + for (std::string::size_type i = 0; i < digest.size(); i++) { result.push_back(digest[i]); } return result; diff --git a/hapsigntool_cpp/zip/src/zip_signer.cpp b/hapsigntool_cpp/zip/src/zip_signer.cpp index 6524e4b0eb0583c447cf93ad86c6f8d0f68cbf86..cc1808e003a89b291262a7bb85c7548a27396076 100644 --- a/hapsigntool_cpp/zip/src/zip_signer.cpp +++ b/hapsigntool_cpp/zip/src/zip_signer.cpp @@ -101,7 +101,7 @@ EndOfCentralDirectory* ZipSigner::GetZipEndOfCentralDirectory(std::ifstream& inp return nullptr; } - for (int start = 0; start < eocdMaxLength; start++) { + for (uint64_t start = 0; start < eocdMaxLength; start++) { eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(retStr, start); if (eocdByBytes) { m_eOCDOffset += start; @@ -135,7 +135,7 @@ bool ZipSigner::GetZipCentralDirectory(std::ifstream& input) ByteBuffer bf(retStr.c_str(), retStr.size()); - int offset = 0; + std::string::size_type offset = 0; /* one by one format central directory */ while (offset < retStr.size()) { CentralDirectory* cd = new CentralDirectory();