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_CODE_SIGN_BLOCK_H 16 #define SIGNATRUETOOLS_CODE_SIGN_BLOCK_H 17 18 #include <map> 19 #include <string> 20 #include <vector> 21 #include <unordered_map> 22 23 #include "segment_header.h" 24 #include "hap_info_segment.h" 25 #include "code_sign_block_header.h" 26 #include "fs_verity_info_segment.h" 27 #include "native_lib_info_segment.h" 28 29 namespace OHOS { 30 namespace SignatureTools { 31 32 class CodeSignBlock { 33 public: 34 static constexpr long PAGE_SIZE_4K = 4096; 35 static constexpr int SEGMENT_HEADER_COUNT = 3; 36 CodeSignBlock(); 37 virtual ~CodeSignBlock(); 38 void AddOneMerkleTree(const std::string& key, const std::vector<int8_t>& merkleTree); 39 std::vector<int8_t> GetOneMerkleTreeByFileName(const std::string& key); 40 void SetCodeSignBlockFlag(); 41 void SetSegmentNum(); 42 void AddToSegmentList(const SegmentHeader& sh); 43 std::vector<SegmentHeader>& GetSegmentHeaderList(); 44 void SetSegmentHeaders(); 45 CodeSignBlockHeader& GetCodeSignBlockHeader(); 46 void SetCodeSignBlockHeader(const CodeSignBlockHeader& csbHeader); 47 void SetFsVerityInfoSegment(const FsVerityInfoSegment& fsVeritySeg); 48 HapInfoSegment& GetHapInfoSegment(); 49 void SetHapInfoSegment(const HapInfoSegment& hapSeg); 50 NativeLibInfoSegment& GetSoInfoSegment(); 51 void SetSoInfoSegment(const NativeLibInfoSegment& soSeg); 52 void ToByteArray(std::vector<int8_t>& ret); 53 void ComputeSegmentOffset(); 54 int64_t ComputeMerkleTreeOffset(int64_t codeSignBlockOffset); 55 void GenerateCodeSignBlockByte(int64_t fsvTreeOffset, std::vector<int8_t>& ret); 56 57 private: 58 CodeSignBlockHeader codeSignBlockHeader; 59 std::vector<SegmentHeader> segmentHeaderList; 60 FsVerityInfoSegment fsVerityInfoSegment; 61 HapInfoSegment hapInfoSegment; 62 NativeLibInfoSegment nativeLibInfoSegment; 63 std::vector<int8_t> zeroPadding; 64 std::map<std::string, std::vector<int8_t>> merkleTreeMap; 65 }; 66 } // namespace SignatureTools 67 } // namespace OHOS 68 #endif // SIGNATRUETOOLS_CODE_SIGN_BLOCK_H 69