From 3e253102fbd75cf53375907c63084589b90485a5 Mon Sep 17 00:00:00 2001 From: Zhou Shihui Date: Tue, 22 Oct 2024 15:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E5=85=A5=E5=8F=82fd=E7=9A=84?= =?UTF-8?q?=E9=AA=8C=E7=AD=BE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Zhou Shihui --- .../include/common/random_access_file.h | 1 + .../appverify/include/interfaces/hap_verify.h | 2 ++ .../appverify/include/verify/hap_verify_v2.h | 1 + .../src/common/random_access_file.cpp | 25 ++++++++++++++++ .../appverify/src/interfaces/hap_verify.cpp | 29 +++++++++++++++++++ .../appverify/src/verify/hap_verify_v2.cpp | 12 ++++++++ 6 files changed, 70 insertions(+) diff --git a/interfaces/innerkits/appverify/include/common/random_access_file.h b/interfaces/innerkits/appverify/include/common/random_access_file.h index 8e67076..2fab5b3 100644 --- a/interfaces/innerkits/appverify/include/common/random_access_file.h +++ b/interfaces/innerkits/appverify/include/common/random_access_file.h @@ -34,6 +34,7 @@ public: DLL_EXPORT RandomAccessFile(); DLL_EXPORT ~RandomAccessFile(); DLL_EXPORT bool Init(const std::string& filePath); + DLL_EXPORT bool InitWithFd(const int32_t fileFd); DLL_EXPORT long long GetLength() const; DLL_EXPORT long long ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset); DLL_EXPORT long long ReadFileFullyFromOffset(char buf[], long long offset, int32_t bufCapacity); diff --git a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h index 4cc9461..2c85893 100644 --- a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h +++ b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h @@ -29,6 +29,8 @@ DLL_EXPORT void DisableDebugMode(); DLL_EXPORT int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult); DLL_EXPORT int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); DLL_EXPORT int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); +extern "C" DLL_EXPORT int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, + std::string &appIdentifier); DLL_EXPORT void SetDevMode(DevMode devMode); } // namespace Verify } // namespace Security diff --git a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h index f5983b6..c0ced01 100644 --- a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h +++ b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h @@ -30,6 +30,7 @@ namespace Verify { class HapVerifyV2 { public: int32_t Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); + int32_t Verify(const int32_t fileFd, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); diff --git a/interfaces/innerkits/appverify/src/common/random_access_file.cpp b/interfaces/innerkits/appverify/src/common/random_access_file.cpp index 7ad4008..a28e8b9 100644 --- a/interfaces/innerkits/appverify/src/common/random_access_file.cpp +++ b/interfaces/innerkits/appverify/src/common/random_access_file.cpp @@ -63,6 +63,31 @@ bool RandomAccessFile::Init(const std::string& filePath) return true; } +bool RandomAccessFile::InitWithFd(const int32_t fileFd) +{ + if (fileFd <= FILE_OPEN_FAIL_ERROR_NUM) { + HAPVERIFY_LOG_ERROR("invalid fd"); + return false; + } + fd = dup(fileFd); + if (fd <= FILE_OPEN_FAIL_ERROR_NUM) { + HAPVERIFY_LOG_ERROR("dup failed: %{public}d", errno); + return false; + } + + if (memoryPageSize <= 0) { + HAPVERIFY_LOG_ERROR("getting pagesize failed: %{public}d", memoryPageSize); + return false; + } + + fileLength = lseek(fd, 0, SEEK_END); + if (fileLength < 0) { + HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); + return false; + } + return true; +} + long long RandomAccessFile::GetLength() const { return fileLength; diff --git a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp index 316cfc5..39a78e7 100644 --- a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp +++ b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp @@ -17,6 +17,7 @@ #include +#include "common/hap_verify_log.h" #include "init/device_type_manager.h" #include "init/hap_crl_manager.h" #include "init/trusted_root_ca.h" @@ -104,6 +105,34 @@ int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSig return hapVerifyV2.ParseHapSignatureInfo(filePath, hapSignInfo); } +int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, + std::string &appIdentifier) +{ + if (fileFd <= -1) { + HAPVERIFY_LOG_ERROR("fd invalid"); + return OPEN_FILE_ERROR; + } + if (!g_isInit && !HapVerifyInit()) { + HAPVERIFY_LOG_ERROR("init failed"); + return VERIFY_SOURCE_INIT_FAIL; + } + HapVerifyV2 hapVerifyV2; + HapVerifyResult hapVerifyResult; + int32_t res = hapVerifyV2.Verify(fileFd, hapVerifyResult); + if (res != VERIFY_SUCCESS) { + HAPVERIFY_LOG_ERROR("verify failed"); + return res; + } + ProvisionInfo info = hapVerifyResult.GetProvisionInfo(); + if (info.distributionType == AppDistType::INTERNALTESTING) { + HAPVERIFY_LOG_ERROR("distTypt error"); + return GET_SIGNATURE_FAIL; + } + bundleName = info.bundleInfo.bundleName; + appIdentifier = info.bundleInfo.appIdentifier; + return VERIFY_SUCCESS; +} + } // namespace Verify } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp index 3d88fdd..32f01ac 100644 --- a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp +++ b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp @@ -59,6 +59,18 @@ int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVer return resultCode; } +int32_t HapVerifyV2::Verify(const int32_t fileFd, HapVerifyResult& hapVerifyV1Result) +{ + HAPVERIFY_LOG_INFO("Start Verify with fd"); + RandomAccessFile hapFile; + if (!hapFile.InitWithFd(fileFd)) { + HAPVERIFY_LOG_ERROR("init with fd failed"); + return OPEN_FILE_ERROR; + } + + return Verify(hapFile, hapVerifyV1Result); +} + bool HapVerifyV2::CheckFilePath(const std::string& filePath, std::string& standardFilePath) { char path[PATH_MAX + 1] = { 0x00 }; -- Gitee