From 7f93565a6e841e45f080321bfc0b6c8bd8feadc0 Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Tue, 16 May 2023 16:23:07 +0800 Subject: [PATCH] #I73JKJ Signed-off-by: jiangminsen --- .../include/common/hap_byte_buffer.h | 49 ++++++------ .../include/common/random_access_file.h | 2 +- .../include/util/hap_signing_block_utils.h | 1 + .../appverify/src/common/hap_byte_buffer.cpp | 78 +++++++++++-------- .../src/common/random_access_file.cpp | 14 ++++ .../src/util/hap_signing_block_utils.cpp | 19 ++++- 6 files changed, 100 insertions(+), 63 deletions(-) diff --git a/interfaces/innerkits/appverify/include/common/hap_byte_buffer.h b/interfaces/innerkits/appverify/include/common/hap_byte_buffer.h index fc8ea13..44ec9a5 100644 --- a/interfaces/innerkits/appverify/include/common/hap_byte_buffer.h +++ b/interfaces/innerkits/appverify/include/common/hap_byte_buffer.h @@ -35,45 +35,46 @@ enum ReadFileErrorCode { class HapByteBuffer { public: DLL_EXPORT HapByteBuffer(); - DLL_EXPORT explicit HapByteBuffer(int bufferCapacity); + DLL_EXPORT explicit HapByteBuffer(int32_t bufferCapacity); DLL_EXPORT HapByteBuffer(const HapByteBuffer& other); DLL_EXPORT ~HapByteBuffer(); DLL_EXPORT HapByteBuffer& operator=(const HapByteBuffer& other); DLL_EXPORT bool GetInt64(long long& value); - DLL_EXPORT bool GetInt64(int index, long long& value); - DLL_EXPORT bool GetUInt32(int index, unsigned int& value); - DLL_EXPORT bool GetInt32(int& value); - DLL_EXPORT bool GetInt32(int index, int& value); - DLL_EXPORT bool GetUInt16(int index, unsigned short& value); - DLL_EXPORT void PutInt32(int offset, int value); - DLL_EXPORT void PutByte(int offset, char value); - DLL_EXPORT void PutData(int offset, const char data[], int len); - DLL_EXPORT int GetCapacity() const; - DLL_EXPORT int GetPosition() const; - DLL_EXPORT int GetLimit() const; + DLL_EXPORT bool GetInt64(int32_t index, long long& value); + DLL_EXPORT bool GetUInt32(uint32_t& value); + DLL_EXPORT bool GetUInt32(int32_t index, uint32_t& value); + DLL_EXPORT bool GetInt32(int32_t& value); + DLL_EXPORT bool GetInt32(int32_t index, int32_t& value); + DLL_EXPORT bool GetUInt16(int32_t index, uint16_t& value); + DLL_EXPORT void PutInt32(int32_t offset, int32_t value); + DLL_EXPORT void PutByte(int32_t offset, char value); + DLL_EXPORT void PutData(int32_t offset, const char data[], int32_t len); + DLL_EXPORT int32_t GetCapacity() const; + DLL_EXPORT int32_t GetPosition() const; + DLL_EXPORT int32_t GetLimit() const; DLL_EXPORT const char* GetBufferPtr() const; - DLL_EXPORT void SetPosition(int pos); - DLL_EXPORT void SetLimit(int lim); - DLL_EXPORT void SetCapacity(int cap); + DLL_EXPORT void SetPosition(int32_t pos); + DLL_EXPORT void SetLimit(int32_t lim); + DLL_EXPORT void SetCapacity(int32_t cap); DLL_EXPORT void Slice(); - DLL_EXPORT int Remaining() const; + DLL_EXPORT int32_t Remaining() const; DLL_EXPORT bool HasRemaining() const; - DLL_EXPORT bool CopyPartialBuffer(const HapByteBuffer& other, int len); + DLL_EXPORT bool CopyPartialBuffer(const HapByteBuffer& other, int32_t len); DLL_EXPORT void Clear(); DLL_EXPORT bool IsEqual(const HapByteBuffer& other); DLL_EXPORT bool IsEqual(const std::string& other); private: - void Init(int bufferCapacity); - bool CheckInputForGettingData(int index, int dataLen); + void Init(int32_t bufferCapacity); + bool CheckInputForGettingData(int32_t index, int32_t dataLen); private: - static const int MAX_PRINT_LENGTH; - static const int HEX_PRINT_LENGTH; + static const int32_t MAX_PRINT_LENGTH; + static const int32_t HEX_PRINT_LENGTH; std::unique_ptr buffer; - int position; - int limit; - int capacity; + int32_t position; + int32_t limit; + int32_t capacity; }; } // namespace Verify } // namespace Security diff --git a/interfaces/innerkits/appverify/include/common/random_access_file.h b/interfaces/innerkits/appverify/include/common/random_access_file.h index 62abfb8..ce48c8e 100644 --- a/interfaces/innerkits/appverify/include/common/random_access_file.h +++ b/interfaces/innerkits/appverify/include/common/random_access_file.h @@ -41,7 +41,7 @@ public: private: long long DoMMap(int bufCapacity, long long offset, MmapInfo& mmapInfo); - + bool CheckLittleEndian(); static const int FILE_OPEN_FAIL_ERROR_NUM; static int memoryPageSize; int fd; diff --git a/interfaces/innerkits/appverify/include/util/hap_signing_block_utils.h b/interfaces/innerkits/appverify/include/util/hap_signing_block_utils.h index 1c76136..05632a5 100644 --- a/interfaces/innerkits/appverify/include/util/hap_signing_block_utils.h +++ b/interfaces/innerkits/appverify/include/util/hap_signing_block_utils.h @@ -107,6 +107,7 @@ private: DLL_EXPORT static bool GetSumOfChunkDigestLen(DataSource* contents[], int len, int chunkDigestLen, int& chunkCount, int& sumOfChunkDigestLen); static bool ParseSignBlockHead(HapSignBlockHead& hapSignBlockHead, HapByteBuffer& hapBlockHead); + static bool ParseSubSignBlockHead(HapSubSignBlockHead& subSignBlockHead, HapByteBuffer& hapBlockHead); static inline bool CheckSignBlockHead(const HapSignBlockHead& hapSignBlockHead); }; } // namespace Verify diff --git a/interfaces/innerkits/appverify/src/common/hap_byte_buffer.cpp b/interfaces/innerkits/appverify/src/common/hap_byte_buffer.cpp index ed57e63..67d431d 100644 --- a/interfaces/innerkits/appverify/src/common/hap_byte_buffer.cpp +++ b/interfaces/innerkits/appverify/src/common/hap_byte_buffer.cpp @@ -21,14 +21,14 @@ namespace OHOS { namespace Security { namespace Verify { -const int HapByteBuffer::MAX_PRINT_LENGTH = 200; -const int HapByteBuffer::HEX_PRINT_LENGTH = 3; +const int32_t HapByteBuffer::MAX_PRINT_LENGTH = 200; +const int32_t HapByteBuffer::HEX_PRINT_LENGTH = 3; HapByteBuffer::HapByteBuffer() : buffer(nullptr), position(0), limit(0), capacity(0) { } -HapByteBuffer::HapByteBuffer(int bufferCapacity) : buffer(nullptr), position(0), limit(0), capacity(0) +HapByteBuffer::HapByteBuffer(int32_t bufferCapacity) : buffer(nullptr), position(0), limit(0), capacity(0) { Init(bufferCapacity); } @@ -51,7 +51,7 @@ HapByteBuffer::~HapByteBuffer() buffer.reset(nullptr); } -void HapByteBuffer::Init(int bufferCapacity) +void HapByteBuffer::Init(int32_t bufferCapacity) { if (bufferCapacity > 0) { buffer = std::make_unique(bufferCapacity); @@ -83,7 +83,7 @@ HapByteBuffer& HapByteBuffer::operator=(const HapByteBuffer& other) return *this; } -bool HapByteBuffer::CheckInputForGettingData(int index, int dataLen) +bool HapByteBuffer::CheckInputForGettingData(int32_t index, int32_t dataLen) { if (buffer == nullptr) { HAPVERIFY_LOG_ERROR(LABEL, "buffer is nullptr"); @@ -113,7 +113,7 @@ bool HapByteBuffer::GetInt64(long long& value) return true; } -bool HapByteBuffer::GetInt64(int index, long long& value) +bool HapByteBuffer::GetInt64(int32_t index, long long& value) { if (!CheckInputForGettingData(index, sizeof(long long))) { HAPVERIFY_LOG_ERROR(LABEL, "Failed to get Int64"); @@ -137,77 +137,87 @@ const char* HapByteBuffer::GetBufferPtr() const return buffer.get(); } -bool HapByteBuffer::GetInt32(int& value) +bool HapByteBuffer::GetInt32(int32_t& value) { if (!GetInt32(0, value)) { HAPVERIFY_LOG_ERROR(LABEL, "GetInt32 failed"); return false; } - position += sizeof(int); + position += sizeof(int32_t); return true; } -bool HapByteBuffer::GetInt32(int index, int& value) +bool HapByteBuffer::GetInt32(int32_t index, int32_t& value) { - if (!CheckInputForGettingData(index, sizeof(int))) { + if (!CheckInputForGettingData(index, sizeof(int32_t))) { HAPVERIFY_LOG_ERROR(LABEL, "Failed to get Int32"); return false; } - if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(int)) != EOK) { + if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(int32_t)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); return false; } return true; } -bool HapByteBuffer::GetUInt32(int index, unsigned int& value) +bool HapByteBuffer::GetUInt32(int32_t index, uint32_t& value) { - if (!CheckInputForGettingData(index, sizeof(unsigned int))) { + if (!CheckInputForGettingData(index, sizeof(uint32_t))) { HAPVERIFY_LOG_ERROR(LABEL, "Failed to get UInt32"); return false; } - if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(unsigned int)) != EOK) { + if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(uint32_t)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); return false; } return true; } -bool HapByteBuffer::GetUInt16(int index, unsigned short& value) +bool HapByteBuffer::GetUInt32(uint32_t& value) { - if (!CheckInputForGettingData(index, sizeof(unsigned short))) { + if (!GetUInt32(0, value)) { + HAPVERIFY_LOG_ERROR(LABEL, "GetUInt32 failed"); + return false; + } + position += sizeof(uint32_t); + return true; +} + +bool HapByteBuffer::GetUInt16(int32_t index, uint16_t& value) +{ + if (!CheckInputForGettingData(index, sizeof(uint16_t))) { HAPVERIFY_LOG_ERROR(LABEL, "Failed to get UInt16"); return false; } - if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(unsigned short)) != EOK) { + if (memcpy_s(&value, sizeof(value), (buffer.get() + position + index), sizeof(uint16_t)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); return false; } return true; } -void HapByteBuffer::PutInt32(int offset, int value) +void HapByteBuffer::PutInt32(int32_t offset, int32_t value) { - if (buffer != nullptr && offset >= 0 && limit - offset >= static_cast(sizeof(value))) { + if (buffer != nullptr && offset >= 0 && limit - offset >= static_cast(sizeof(value))) { if (memcpy_s((buffer.get() + offset), (limit - offset), &value, sizeof(value)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); } } } -void HapByteBuffer::PutByte(int offset, char value) +void HapByteBuffer::PutByte(int32_t offset, char value) { - if (buffer != nullptr && offset >= 0 && limit - offset >= static_cast(sizeof(value))) { + if (buffer != nullptr && offset >= 0 && limit - offset >= static_cast(sizeof(value))) { if (memcpy_s((buffer.get() + offset), (limit - offset), (&value), sizeof(value)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); } } } -void HapByteBuffer::PutData(int offset, const char data[], int len) +void HapByteBuffer::PutData(int32_t offset, const char data[], int32_t len) { if (buffer != nullptr && data != nullptr && offset >= 0 && len > 0 && (limit - offset) >= len) { if (memcpy_s((buffer.get() + offset), (limit - offset), data, len) != EOK) { @@ -216,7 +226,7 @@ void HapByteBuffer::PutData(int offset, const char data[], int len) } } -void HapByteBuffer::SetPosition(int pos) +void HapByteBuffer::SetPosition(int32_t pos) { if (pos >= 0 && pos <= limit) { position = pos; @@ -230,7 +240,7 @@ void HapByteBuffer::Slice() position, capacity, limit); return; } - int newCapacity = limit - position; + int32_t newCapacity = limit - position; std::unique_ptr newBuffer = std::make_unique(newCapacity); if (memcpy_s(newBuffer.get(), newCapacity, (buffer.get() + position), (limit - position)) != EOK) { HAPVERIFY_LOG_ERROR(LABEL, "memcpy_s failed"); @@ -242,24 +252,24 @@ void HapByteBuffer::Slice() limit = capacity; } -int HapByteBuffer::GetPosition() const +int32_t HapByteBuffer::GetPosition() const { return position; } -int HapByteBuffer::GetLimit() const +int32_t HapByteBuffer::GetLimit() const { return limit; } -void HapByteBuffer::SetLimit(int lim) +void HapByteBuffer::SetLimit(int32_t lim) { if (lim <= capacity && lim >= position) { limit = lim; } } -int HapByteBuffer::Remaining() const +int32_t HapByteBuffer::Remaining() const { return limit - position; } @@ -269,9 +279,9 @@ bool HapByteBuffer::HasRemaining() const return position < limit; } -bool HapByteBuffer::CopyPartialBuffer(const HapByteBuffer& other, int len) +bool HapByteBuffer::CopyPartialBuffer(const HapByteBuffer& other, int32_t len) { - int readableDataLen = other.Remaining(); + int32_t readableDataLen = other.Remaining(); if (readableDataLen < len) { HAPVERIFY_LOG_ERROR(LABEL, "readableDataLen %{public}d less than len %{public}d", readableDataLen, len); return false; @@ -307,7 +317,7 @@ bool HapByteBuffer::IsEqual(const HapByteBuffer& other) return false; } const char* otherBuffer = other.GetBufferPtr(); - for (int i = 0; i < capacity; i++) { + for (int32_t i = 0; i < capacity; i++) { if (buffer[i] != otherBuffer[i]) { HAPVERIFY_LOG_ERROR(LABEL, "diff value[%{public}d]: %{public}x %{public}x", i, buffer[i], otherBuffer[i]); @@ -319,11 +329,11 @@ bool HapByteBuffer::IsEqual(const HapByteBuffer& other) bool HapByteBuffer::IsEqual(const std::string& other) { - if (capacity != static_cast(other.size()) || buffer == nullptr) { + if (capacity != static_cast(other.size()) || buffer == nullptr) { HAPVERIFY_LOG_ERROR(LABEL, "invalid input"); return false; } - for (int i = 0; i < capacity; i++) { + for (int32_t i = 0; i < capacity; i++) { if (buffer[i] != other[i]) { HAPVERIFY_LOG_ERROR(LABEL, "diff value[%{public}d]: %{public}x %{public}x", i, buffer[i], other[i]); @@ -333,7 +343,7 @@ bool HapByteBuffer::IsEqual(const std::string& other) return true; } -void HapByteBuffer::SetCapacity(int cap) +void HapByteBuffer::SetCapacity(int32_t cap) { if (buffer != nullptr) { buffer.reset(nullptr); diff --git a/interfaces/innerkits/appverify/src/common/random_access_file.cpp b/interfaces/innerkits/appverify/src/common/random_access_file.cpp index 70e4779..49a8434 100644 --- a/interfaces/innerkits/appverify/src/common/random_access_file.cpp +++ b/interfaces/innerkits/appverify/src/common/random_access_file.cpp @@ -68,8 +68,22 @@ long long RandomAccessFile::GetLength() const return fileLength; } +bool RandomAccessFile::CheckLittleEndian() +{ + union LittleEndian { + int num; + char ch; + } t; + t.num = 1; + return (t.ch == 1); +} + long long RandomAccessFile::DoMMap(int bufCapacity, long long offset, MmapInfo& mmapInfo) { + if (!CheckLittleEndian()) { + HAPVERIFY_LOG_ERROR(LABEL, "CheckLittleEndian: failed"); + return MMAP_FAILED; + } mmapInfo.mapAddr = reinterpret_cast(MAP_FAILED); if (fd == FILE_OPEN_FAIL_ERROR_NUM) { return FILE_IS_CLOSE; diff --git a/interfaces/innerkits/appverify/src/util/hap_signing_block_utils.cpp b/interfaces/innerkits/appverify/src/util/hap_signing_block_utils.cpp index 9f06811..d302fef 100644 --- a/interfaces/innerkits/appverify/src/util/hap_signing_block_utils.cpp +++ b/interfaces/innerkits/appverify/src/util/hap_signing_block_utils.cpp @@ -291,6 +291,13 @@ bool HapSigningBlockUtils::ParseSignBlockHead(HapSignBlockHead& hapSignBlockHead hapBlockHead.GetInt32(hapSignBlockHead.version); } +bool HapSigningBlockUtils::ParseSubSignBlockHead(HapSubSignBlockHead& subSignBlockHead, HapByteBuffer& hapBlockHead) +{ + return hapBlockHead.GetUInt32(subSignBlockHead.type) && + hapBlockHead.GetUInt32(subSignBlockHead.length) && + hapBlockHead.GetUInt32(subSignBlockHead.offset); +} + /* * Hap Sign Block Format: * HapSubSignBlock1_Head @@ -315,11 +322,15 @@ bool HapSigningBlockUtils::FindHapSubSigningBlock(RandomAccessFile& hapFile, int HAPVERIFY_LOG_DEBUG(LABEL, "hapSignBlockOffset %{public}lld blockArrayLen: %{public}lld blockCount: %{public}d", hapSignBlockOffset, blockArrayLen, blockCount); for (int i = 0; i < blockCount; i++) { - HapSubSignBlockHead subSignBlockHead; - long long ret = hapFile.ReadFileFullyFromOffset(reinterpret_cast(&subSignBlockHead), - readHeadOffset, sizeof(HapSubSignBlockHead)); + HapByteBuffer hapBlockHead(ZIP_CD_SIZE_OFFSET_IN_EOCD); + long long ret = hapFile.ReadFileFullyFromOffset(hapBlockHead, readHeadOffset); if (ret < 0) { - HAPVERIFY_LOG_ERROR(LABEL, "read %{public}dst subblock head error: %{public}lld", i, ret); + HAPVERIFY_LOG_ERROR(LABEL, "read hapBlockHead error: %{public}lld", ret); + return false; + } + HapSubSignBlockHead subSignBlockHead; + if (!ParseSubSignBlockHead(subSignBlockHead, hapBlockHead)) { + HAPVERIFY_LOG_ERROR(LABEL, "ParseSubSignBlockHead failed"); return false; } readLen += sizeof(HapSubSignBlockHead); -- Gitee