diff --git a/utils/include/code_sign_block.h b/utils/include/code_sign_block.h index c56d6464b5fca96883c3412245e1bbc416f0191b..9796f1f20163fa06a815cdbf3c3e55cf09d1dff9 100644 --- a/utils/include/code_sign_block.h +++ b/utils/include/code_sign_block.h @@ -113,7 +113,7 @@ typedef struct { #pragma pack(pop) using SignMap = std::unordered_map; -using ReadBuffer = const char *; +using ReadBuffer = const char *const; #define CONST_STATIC_CAST(type, ptr) static_cast(static_cast(ptr)) class CodeSignBlock { @@ -140,7 +140,7 @@ private: int32_t ParseNativeLibSignInfo(const EntryMap &entryMap); int32_t ParseHapSignInfo(const std::string &path); int32_t ParseCodeSignBlockBaseInfo(ReadBuffer codeSignBlock, uint32_t &blockSize); - int32_t GetCodeSignBlockBuffer(const std::string &path, ReadBuffer &signBuffer, uint32_t &size); + int32_t GetCodeSignBlockBuffer(const std::string &path); static constexpr uint32_t CSB_HEADER_VERSION = 1; static constexpr uint32_t CSB_HEADER_FLAG_MERKLE_TREE = 0x1; @@ -242,7 +242,7 @@ private: return CS_SUCCESS; } - Verify::SignatureInfo signatureInfo_; + Verify::HapByteBuffer codeSignatureInfo_; const CodeSignBlockHeader *blockHeader_ = nullptr; const FsVerityInfo *fsVerityInfo_ = nullptr; const HapSignInfo *hapSignInfo_ = nullptr; diff --git a/utils/src/code_sign_block.cpp b/utils/src/code_sign_block.cpp index d1b8e242ed17f702af4da64fcd902e5ffe4ecf22..689f0120e81e80cc3261b332fafba962b8d63f10 100644 --- a/utils/src/code_sign_block.cpp +++ b/utils/src/code_sign_block.cpp @@ -31,16 +31,7 @@ namespace OHOS { namespace Security { namespace CodeSign { -constexpr uint32_t HAP_CODE_SIGN_BLOCK_ID = 0x30000001; -constexpr uint32_t CSB_PROPERTY_BLOB = 0x20000003; - -CodeSignBlock::CodeSignBlock() -{ - signatureInfo_.hapSigningBlockOffset = 0; - signatureInfo_.hapCentralDirOffset = 0; - signatureInfo_.hapEocdOffset = 0; - signatureInfo_.version = 0; -} +CodeSignBlock::CodeSignBlock() {} CodeSignBlock::~CodeSignBlock() { } @@ -233,50 +224,12 @@ int32_t CodeSignBlock::ParseCodeSignBlockBaseInfo(ReadBuffer codeSignBlock, uint return SetNativeLibSignInfo(CONST_STATIC_CAST(NativeLibSignInfo, codeSignBlock + segHeader->offset)); } -int32_t CodeSignBlock::GetCodeSignBlockBuffer(const std::string &path, ReadBuffer &signBuffer, uint32_t &size) +int32_t CodeSignBlock::GetCodeSignBlockBuffer(const std::string &path) { - ReadBuffer blobBuffer = nullptr; - uint32_t blobSize = 0; - ReadBuffer signBlockBuffer = nullptr; - uint32_t signBlockSize = 0; - - int32_t ret = Verify::ParseHapSignatureInfo(path, signatureInfo_); - if (ret != Verify::VERIFY_SUCCESS) { - LOG_ERROR("find code sign block buffer failed. errno = %{public}d ", ret); - return CS_ERR_FILE_INVALID; - } - - for (const auto &value : signatureInfo_.optionBlocks) { - if (value.optionalType != CSB_PROPERTY_BLOB) { - continue; - } - - blobBuffer = value.optionalBlockValue.GetBufferPtr(); - blobSize = static_cast(value.optionalBlockValue.GetCapacity()); - break; - } - - if ((blobBuffer == nullptr) || (blobSize <= sizeof(PropertyBlobHeader))) { + int32_t ret = Verify::ParseCodeSignatureInfo(path, codeSignatureInfo_); + if (ret == Verify::SIGNATURE_NOT_FOUND) { return CS_CODE_SIGN_NOT_EXISTS; } - - size_t length = 0; - do { - auto blobHeader = CONST_STATIC_CAST(PropertyBlobHeader, blobBuffer + length); - if (blobHeader->type == HAP_CODE_SIGN_BLOCK_ID) { - signBlockBuffer = CONST_STATIC_CAST(char, blobHeader) + sizeof(PropertyBlobHeader); - signBlockSize = blobHeader->size; - break; - } - length += blobHeader->size + sizeof(PropertyBlobHeader); - } while (length < blobSize); - - if ((signBlockBuffer == nullptr) || !signBlockSize) { - return CS_CODE_SIGN_NOT_EXISTS; - } - - signBuffer = signBlockBuffer; - size = signBlockSize; return CS_SUCCESS; } @@ -286,14 +239,14 @@ int32_t CodeSignBlock::ParseCodeSignBlock(const std::string &realPath, int32_t ret; ReadBuffer codeSignBlock = nullptr; uint32_t codeSignSize; - - ret = GetCodeSignBlockBuffer(realPath, codeSignBlock, codeSignSize); + ret = GetCodeSignBlockBuffer(realPath); if (ret != CS_SUCCESS) { - LOG_ERROR("Get code sign block buffer failed. errno = %{public}d ", ret); + LOG_ERROR("Get code sign block buffer failed. errno = %{public}d", ret); return ret; } - - ret = ParseCodeSignBlockBaseInfo(codeSignBlock, codeSignSize); + const auto signBuffer = reinterpret_cast(codeSignatureInfo_.GetBufferPtr()); + uint32_t size = static_cast(codeSignatureInfo_.GetCapacity()); + ret = ParseCodeSignBlockBaseInfo(signBuffer, size); if (ret != CS_SUCCESS) { return ret; }