From 0f667b13d2fe3e49a042310d08fafdf0cdde100c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=B3=E6=A0=8B?= Date: Mon, 17 Feb 2025 15:32:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Signed-off-by:=20=E7=8E=8B=E4=BD=B3?= =?UTF-8?q?=E6=A0=8B=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit record hash --- .../package/pkg_verify/pkg_verify_util.cpp | 36 +++++++++++++++++-- services/package/pkg_verify/pkg_verify_util.h | 4 +++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/services/package/pkg_verify/pkg_verify_util.cpp b/services/package/pkg_verify/pkg_verify_util.cpp index 4725e1af..9ef6b1ce 100644 --- a/services/package/pkg_verify/pkg_verify_util.cpp +++ b/services/package/pkg_verify/pkg_verify_util.cpp @@ -195,10 +195,8 @@ int32_t PkgVerifyUtil::Pkcs7verify(std::vector &signature, std::vector< return pkcs7.GetHashFromSignBlock(signature.data(), signature.size(), hash); } -int32_t PkgVerifyUtil::HashCheck(const PkgStreamPtr srcData, const size_t dataLen, - const std::vector &hash, const std::string &path) const +std::string PkgVerifyUtil::GetPkgTime(const std::string &pkgPath) const { - Updater::UPDATER_INIT_RECORD; struct stat statInfo {}; std::string fileInfo = "valid info"; if (stat(path.c_str(), &statInfo) != 0) { @@ -208,6 +206,37 @@ int32_t PkgVerifyUtil::HashCheck(const PkgStreamPtr srcData, const size_t dataLe " , pkg last change time is " + ctime(&statInfo.st_mtime); PKG_LOGI(fileInfo.c_str()); } + return fileInfo; +} + +void PkgVerifyUtil::WriteHash(std::vector &sourceDigest, const std::string &pkgPath) const +{ + std::string path = "/data/updatar/hash_file"; + if (access(path.c_str(), F_OK) != 0) { + std::ofstream file(path, std::ios::out); + if (!file) { + PKG_LOGE("open file failed"); + return; + } + file.write(reinterpret_cast(hash.data()), sizeof(hash)); + return; + } + std::ifstream file(path, std::ios::in); + if (!file) { + PKG_LOGE("open file failed"); + return; + } + std::string lastHash {}; + if (getline(file, lastHash)) { + UPDATER_LAST_WORD(lastHash, hash.data(), GetPkgTime(pkgPath)); + } +} + +int32_t PkgVerifyUtil::HashCheck(const PkgStreamPtr srcData, const size_t dataLen, + const std::vector &hash, const std::string &path) const +{ + Updater::UPDATER_INIT_RECORD; + std::string fileInfo = GetPkgTime(path); if (srcData == nullptr || dataLen == 0) { UPDATER_LAST_WORD(PKG_INVALID_PARAM); return PKG_INVALID_PARAM; @@ -232,6 +261,7 @@ int32_t PkgVerifyUtil::HashCheck(const PkgStreamPtr srcData, const size_t dataLe UPDATER_LAST_WORD(PKG_INVALID_DIGEST, ConvertShaHex(hash).substr(0, INTERCEPT_HASH_LENGTH), ConvertShaHex(sourceDigest).substr(0, INTERCEPT_HASH_LENGTH), fileInfo); + WriteHash(sourceDigest, path); return PKG_INVALID_DIGEST; } diff --git a/services/package/pkg_verify/pkg_verify_util.h b/services/package/pkg_verify/pkg_verify_util.h index 006288f4..2e356d47 100644 --- a/services/package/pkg_verify/pkg_verify_util.h +++ b/services/package/pkg_verify/pkg_verify_util.h @@ -47,6 +47,10 @@ public: int32_t HashCheck(const PkgStreamPtr srcData, const size_t dataLen, const std::vector &hash, const std::string &path) const; + + std::string GetPkgTime(const std::string &pkgPath) const; + + void WriteHash(std::vector &sourceDigest, const std::string &pkgPath) const; }; } // namespace Hpackage #endif -- Gitee From 70da3fd041fb7f39969e00bd13ed89006e81d2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=B3=E6=A0=8B?= Date: Mon, 17 Feb 2025 07:52:03 +0000 Subject: [PATCH 2/4] update services/package/pkg_verify/pkg_verify_util.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王佳栋 --- services/package/pkg_verify/pkg_verify_util.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/package/pkg_verify/pkg_verify_util.cpp b/services/package/pkg_verify/pkg_verify_util.cpp index 9ef6b1ce..61fa63fb 100644 --- a/services/package/pkg_verify/pkg_verify_util.cpp +++ b/services/package/pkg_verify/pkg_verify_util.cpp @@ -211,6 +211,7 @@ std::string PkgVerifyUtil::GetPkgTime(const std::string &pkgPath) const void PkgVerifyUtil::WriteHash(std::vector &sourceDigest, const std::string &pkgPath) const { + Updater::UPDATER_INIT_RECORD; std::string path = "/data/updatar/hash_file"; if (access(path.c_str(), F_OK) != 0) { std::ofstream file(path, std::ios::out); -- Gitee From b1d1268a7d8c14f47c57bdbbdcfe248e6243ca61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=B3=E6=A0=8B?= Date: Mon, 17 Feb 2025 21:26:48 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Signed-off-by:=20=E7=8E=8B=E4=BD=B3?= =?UTF-8?q?=E6=A0=8B=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix --- services/package/pkg_verify/pkg_verify_util.cpp | 9 ++++++--- services/package/pkg_verify/pkg_verify_util.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/services/package/pkg_verify/pkg_verify_util.cpp b/services/package/pkg_verify/pkg_verify_util.cpp index 61fa63fb..be469d94 100644 --- a/services/package/pkg_verify/pkg_verify_util.cpp +++ b/services/package/pkg_verify/pkg_verify_util.cpp @@ -209,10 +209,10 @@ std::string PkgVerifyUtil::GetPkgTime(const std::string &pkgPath) const return fileInfo; } -void PkgVerifyUtil::WriteHash(std::vector &sourceDigest, const std::string &pkgPath) const +void PkgVerifyUtil::WriteHash(std::vector &hash, const std::string &pkgPath) const { Updater::UPDATER_INIT_RECORD; - std::string path = "/data/updatar/hash_file"; + std::string path = "/data/updater/hash_file"; if (access(path.c_str(), F_OK) != 0) { std::ofstream file(path, std::ios::out); if (!file) { @@ -229,7 +229,10 @@ void PkgVerifyUtil::WriteHash(std::vector &sourceDigest, const std::str } std::string lastHash {}; if (getline(file, lastHash)) { - UPDATER_LAST_WORD(lastHash, hash.data(), GetPkgTime(pkgPath)); + std::vector lastHashVector {}; + lastHashVector.assign(lastHash.begin(), lastHash.end()); + UPDATER_LAST_WORD(ConvertShaHex(static_cast>(lastHashVector)), + ConvertShaHex(hash), GetPkgTime(pkgPath)); } } diff --git a/services/package/pkg_verify/pkg_verify_util.h b/services/package/pkg_verify/pkg_verify_util.h index 2e356d47..5f546f76 100644 --- a/services/package/pkg_verify/pkg_verify_util.h +++ b/services/package/pkg_verify/pkg_verify_util.h @@ -50,7 +50,7 @@ public: std::string GetPkgTime(const std::string &pkgPath) const; - void WriteHash(std::vector &sourceDigest, const std::string &pkgPath) const; + void WriteHash(std::vector &hash, const std::string &pkgPath) const; }; } // namespace Hpackage #endif -- Gitee From 2cac2e8814aafd65014765a0687399ccc156b715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=B3=E6=A0=8B?= Date: Mon, 17 Feb 2025 22:07:02 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Signed-off-by:=20=E7=8E=8B=E4=BD=B3?= =?UTF-8?q?=E6=A0=8B=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix --- services/package/pkg_verify/pkg_verify_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/package/pkg_verify/pkg_verify_util.cpp b/services/package/pkg_verify/pkg_verify_util.cpp index be469d94..904c6f85 100644 --- a/services/package/pkg_verify/pkg_verify_util.cpp +++ b/services/package/pkg_verify/pkg_verify_util.cpp @@ -199,7 +199,7 @@ std::string PkgVerifyUtil::GetPkgTime(const std::string &pkgPath) const { struct stat statInfo {}; std::string fileInfo = "valid info"; - if (stat(path.c_str(), &statInfo) != 0) { + if (stat(pkgPath.c_str(), &statInfo) != 0) { PKG_LOGE("get file info error"); } else { fileInfo = "pkg size is " + std::to_string(statInfo.st_size) + -- Gitee