1/* 2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#ifndef SIGNATRUETOOLS_SIGNING_BLOCK_UTILS_H 16#define SIGNATRUETOOLS_SIGNING_BLOCK_UTILS_H 17 18#include <vector> 19 20#include "data_source.h" 21#include "export_define.h" 22#include "byte_buffer.h" 23#include "random_access_file.h" 24#include "digest_parameter.h" 25#include "pkcs7_context.h" 26#include "signature_info.h" 27 28namespace OHOS { 29namespace SignatureTools { 30 31constexpr int32_t ZIP_CHUNK_DIGEST_PRIFIX_LEN = 5; 32 33struct HapSignBlockHead { 34 int32_t version = 0; 35 int32_t blockCount = 0; 36 int64_t hapSignBlockSize; 37 int64_t hapSignBlockMagicLo; 38 int64_t hapSignBlockMagicHi; 39}; 40 41enum HapBlobType { 42 HAP_SIGN_BLOB = 0x20000000, 43 PROOF_ROTATION_BLOB = 0x20000001, 44 PROFILE_BLOB = 0x20000002, 45 PROPERTY_BLOB = 0x20000003, 46}; 47 48struct HapSubSignBlockHead { 49 uint32_t type = 0; 50 uint32_t length = 0; 51 uint32_t offset = 0; 52}; 53 54class HapSignerBlockUtils { 55public: 56 DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_HIGH_OLD; 57 DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_LOW_OLD; 58 DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_HIGH; 59 DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_LOW; 60 DLL_EXPORT static const int32_t ZIP_HEAD_OF_SIGNING_BLOCK_LENGTH; 61 DLL_EXPORT static const int32_t ZIP_EOCD_SEGMENT_FLAG; 62 static const int64_t CHUNK_SIZE; 63 static const int32_t HAP_SIG_BLOCK_MIN_SIZE; 64 static const int32_t ZIP_EOCD_SEG_MIN_SIZE; 65 static const int32_t ZIP_EOCD_COMMENT_LENGTH_OFFSET; 66 static const int32_t ZIP_CD_OFFSET_IN_EOCD; 67 static const int32_t ZIP_CD_SIZE_OFFSET_IN_EOCD; 68 static const int32_t ZIP_BLOCKS_NUM_NEED_DIGEST; 69 static const char ZIP_FIRST_LEVEL_CHUNK_PREFIX; 70 static const char ZIP_SECOND_LEVEL_CHUNK_PREFIX; 71 /* the specifications of hap sign block */ 72 static constexpr int64_t MAX_HAP_SIGN_BLOCK_SIZE = 1024 * 1024 * 1024LL; // 1024MB 73 static constexpr int32_t MAX_BLOCK_COUNT = 10; 74 static constexpr int32_t VERSION_FOR_NEW_MAGIC_NUM = 3; 75 static constexpr int32_t TEST_FILE_BLOCK_LENGTH = 50; 76 static constexpr int32_t TEST_FILE_BLOCK_COUNT = 3; 77 78 HapSignerBlockUtils() = delete; 79 DLL_EXPORT static bool FindHapSignature(RandomAccessFile& hapFile, SignatureInfo& signInfo); 80 DLL_EXPORT static bool GetOptionalBlockIndex(std::vector<OptionalBlock>& optionBlocks, 81 int32_t type, int& index); 82 DLL_EXPORT static bool VerifyHapIntegrity(Pkcs7Context& digestInfo, RandomAccessFile& hapFile, 83 SignatureInfo& signInfo); 84 DLL_EXPORT static int64_t CreatTestZipFile(const std::string& pathFile, SignatureInfo& signInfo); 85 86 DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, std::pair<ByteBuffer, int64_t>& eocd); 87 DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, unsigned short maxCommentSize, 88 std::pair<ByteBuffer, int64_t>& eocd); 89 DLL_EXPORT static bool FindEocdInSearchBuffer(ByteBuffer& zipContents, int& offset); 90 DLL_EXPORT static bool GetCentralDirectoryOffset(ByteBuffer& eocd, int64_t eocdOffset, 91 int64_t& centralDirectoryOffset); 92 DLL_EXPORT static bool GetCentralDirectorySize(ByteBuffer& eocd, long& centralDirectorySize); 93 static bool FindHapSigningBlock(RandomAccessFile& hapFile, int64_t centralDirOffset, 94 SignatureInfo& signInfo); 95 static bool FindHapSubSigningBlock(RandomAccessFile& hapFile, 96 int32_t blockCount, 97 int64_t blockArrayLen, 98 int64_t hapSignBlockOffset, 99 SignatureInfo& signInfo); 100 DLL_EXPORT static bool ClassifyHapSubSigningBlock(SignatureInfo& signInfo, 101 const ByteBuffer& subBlock, uint32_t type); 102 DLL_EXPORT static bool SetUnsignedInt32(ByteBuffer& buffer, int32_t offset, int64_t value); 103 DLL_EXPORT static bool ComputeDigestsWithOptionalBlock(const DigestParameter& digestParam, 104 const std::vector<OptionalBlock>& optionalBlocks, 105 const ByteBuffer& chunkDigest, 106 ByteBuffer& finalDigest); 107 static bool ComputeDigestsForEachChunk(const DigestParameter& digestParam, DataSource* contents[], 108 int32_t len, ByteBuffer& result); 109 static int32_t GetChunkCount(int64_t inputSize, int64_t chunkSize); 110 static bool InitDigestPrefix(const DigestParameter& digestParam, 111 unsigned char(&chunkContentPrefix)[ZIP_CHUNK_DIGEST_PRIFIX_LEN], 112 int32_t chunkLen); 113 DLL_EXPORT static DigestParameter GetDigestParameter(int32_t nId); 114 DLL_EXPORT static bool GetSumOfChunkDigestLen(DataSource* contents[], int32_t len, int32_t chunkDigestLen, 115 int& chunkCount, int& sumOfChunkDigestLen); 116 static bool ParseSignBlockHead(HapSignBlockHead& hapSignBlockHead, ByteBuffer& hapBlockHead); 117 static bool ParseSubSignBlockHead(HapSubSignBlockHead& subSignBlockHead, ByteBuffer& hapBlockHead); 118 static bool CheckSignBlockHead(const HapSignBlockHead& hapSignBlockHead); 119 static void CreateHapSubSignBlockHead(HapSubSignBlockHead& signBlob, HapSubSignBlockHead& profileBlob, 120 HapSubSignBlockHead& propertyBlob); 121}; 122} // namespace SignatureTools 123} // namespace OHOS 124#endif // HAP_SIGNING_BLOCK_UTILS_H 125