1 /* 2 * Copyright (C) 2021-2022 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 HAP_SIGNING_BLOCK_UTILS_H 16 #define HAP_SIGNING_BLOCK_UTILS_H 17 18 #include <vector> 19 20 #include "common/data_source.h" 21 #include "common/export_define.h" 22 #include "common/hap_byte_buffer.h" 23 #include "common/random_access_file.h" 24 #include "interfaces/hap_verify_result.h" 25 #include "util/digest_parameter.h" 26 #include "util/pkcs7_context.h" 27 #include "util/signature_info.h" 28 #include "util/signature_info.h" 29 30 namespace OHOS { 31 namespace Security { 32 namespace Verify { 33 constexpr int32_t ZIP_CHUNK_DIGEST_PRIFIX_LEN = 5; 34 35 enum HapBlobType { 36 HAP_SIGN_BLOB = 0x20000000, 37 PROOF_ROTATION_BLOB = 0x20000001, 38 PROFILE_BLOB = 0x20000002, 39 PROPERTY_BLOB = 0x20000003, 40 }; 41 42 struct HapSignBlockHead { 43 int32_t version = 0; 44 int32_t blockCount = 0; 45 long long hapSignBlockSize; 46 long long hapSignBlockMagicLo; 47 long long hapSignBlockMagicHi; 48 }; 49 50 struct HapSubSignBlockHead { 51 uint32_t type = 0; 52 uint32_t length = 0; 53 uint32_t offset = 0; 54 }; 55 56 class HapSigningBlockUtils { 57 public: 58 DLL_EXPORT static bool FindHapSignature(RandomAccessFile& hapFile, SignatureInfo& signInfo); 59 DLL_EXPORT static bool GetOptionalBlockIndex(std::vector<OptionalBlock>& optionBlocks, int32_t type, int& index); 60 DLL_EXPORT static bool VerifyHapIntegrity(Pkcs7Context& digestInfo, RandomAccessFile& hapFile, 61 SignatureInfo& signInfo); 62 63 private: 64 DLL_EXPORT static const long long HAP_SIG_BLOCK_MAGIC_HIGH_OLD; 65 DLL_EXPORT static const long long HAP_SIG_BLOCK_MAGIC_LOW_OLD; 66 DLL_EXPORT static const long long HAP_SIG_BLOCK_MAGIC_HIGH; 67 DLL_EXPORT static const long long HAP_SIG_BLOCK_MAGIC_LOW; 68 DLL_EXPORT static const int32_t ZIP_HEAD_OF_SIGNING_BLOCK_LENGTH; 69 DLL_EXPORT static const int32_t ZIP_EOCD_SEGMENT_FLAG; 70 static const long long CHUNK_SIZE; 71 static const int32_t HAP_SIG_BLOCK_MIN_SIZE; 72 static const int32_t ZIP_EOCD_SEG_MIN_SIZE; 73 static const int32_t ZIP_EOCD_COMMENT_LENGTH_OFFSET; 74 static const int32_t ZIP_CD_OFFSET_IN_EOCD; 75 static const int32_t ZIP_CD_SIZE_OFFSET_IN_EOCD; 76 static const int32_t ZIP_BLOCKS_NUM_NEED_DIGEST; 77 static const char ZIP_FIRST_LEVEL_CHUNK_PREFIX; 78 static const char ZIP_SECOND_LEVEL_CHUNK_PREFIX; 79 /* the specifications of hap sign block */ 80 static constexpr long long MAX_HAP_SIGN_BLOCK_SIZE = 1024 * 1024 * 1024LL; // 1024MB 81 static constexpr int32_t MAX_BLOCK_COUNT = 10; 82 static constexpr int32_t VERSION_FOR_NEW_MAGIC_NUM = 3; 83 84 private: 85 DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, std::pair<HapByteBuffer, long long>& eocd); 86 DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, unsigned short maxCommentSize, 87 std::pair<HapByteBuffer, long long>& eocd); 88 DLL_EXPORT static bool FindEocdInSearchBuffer(HapByteBuffer& zipContents, int& offset); 89 DLL_EXPORT static bool GetCentralDirectoryOffset(HapByteBuffer& eocd, long long eocdOffset, 90 long long& centralDirectoryOffset); 91 static bool FindHapSigningBlock(RandomAccessFile& hapFile, long long centralDirOffset, 92 SignatureInfo& signInfo); 93 static bool FindHapSubSigningBlock(RandomAccessFile& hapFile, int32_t blockCount, 94 long long blockArrayLen, long long hapSignBlockOffset, SignatureInfo& signInfo); 95 DLL_EXPORT static bool ClassifyHapSubSigningBlock(SignatureInfo& signInfo, 96 const HapByteBuffer& subBlock, uint32_t type); 97 DLL_EXPORT static bool SetUnsignedInt32(HapByteBuffer& buffer, int32_t offset, long long value); 98 DLL_EXPORT static bool ComputeDigestsWithOptionalBlock(const DigestParameter& digestParam, 99 const std::vector<OptionalBlock>& optionalBlocks, const HapByteBuffer& chunkDigest, 100 HapByteBuffer& finalDigest); 101 static bool ComputeDigestsForEachChunk(const DigestParameter& digestParam, DataSource* contents[], 102 int32_t len, HapByteBuffer& result); 103 static int32_t GetChunkCount(long long inputSize, long long chunkSize); 104 static bool InitDigestPrefix(const DigestParameter& digestParam, 105 unsigned char (&chunkContentPrefix)[ZIP_CHUNK_DIGEST_PRIFIX_LEN], int32_t chunkLen); 106 DLL_EXPORT static DigestParameter GetDigestParameter(int32_t nId); 107 DLL_EXPORT static bool GetSumOfChunkDigestLen(DataSource* contents[], int32_t len, int32_t chunkDigestLen, 108 int& chunkCount, int& sumOfChunkDigestLen); 109 static bool ParseSignBlockHead(HapSignBlockHead& hapSignBlockHead, HapByteBuffer& hapBlockHead); 110 static bool ParseSubSignBlockHead(HapSubSignBlockHead& subSignBlockHead, HapByteBuffer& hapBlockHead); 111 static bool CheckSignBlockHead(const HapSignBlockHead& hapSignBlockHead); 112 }; 113 } // namespace Verify 114 } // namespace Security 115 } // namespace OHOS 116 #endif // HAP_SIGNING_BLOCK_UTILS_H 117