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_HEADER_H 16 #define SIGNATRUETOOLS_CODE_SIGN_BLOCK_HEADER_H 17 18 #include <vector> 19 #include <string> 20 21 #include "byte_buffer.h" 22 #include "signature_tools_log.h" 23 24 namespace OHOS { 25 namespace SignatureTools { 26 27 class CodeSignBlockHeader { 28 public: 29 class Builder { 30 public: 31 virtual CodeSignBlockHeader* Build(); 32 virtual ~Builder(); 33 34 virtual Builder* SetMagic(int64_t magic); 35 virtual Builder* SetVersion(int version); 36 virtual Builder* SetBlockSize(int blockSize); 37 virtual Builder* SetSegmentNum(int segmentNum); 38 virtual Builder* SetFlags(int flags); 39 virtual Builder* SetReserved(const std::vector<int8_t>& reserved); 40 41 int64_t magic = MAGIC_NUM; 42 int version = CODE_SIGNING_VERSION; 43 int blockSize = 0; 44 int segmentNum = 0; 45 int flags = 0; 46 std::vector<int8_t> reserved = std::vector<int8_t>(RESERVED_BYTE_ARRAY_LENGTH); 47 }; 48 49 public: 50 static const int FLAG_MERKLE_TREE_INLINED = 0x1; 51 static const int FLAG_NATIVE_LIB_INCLUDED = 0x2; 52 CodeSignBlockHeader(); 53 CodeSignBlockHeader(Builder* builder); 54 virtual ~CodeSignBlockHeader(); 55 static CodeSignBlockHeader* FromByteArray(const std::vector<int8_t>& bytes); 56 static int Size(); 57 virtual void SetSegmentNum(int num); 58 virtual int GetSegmentNum(); 59 virtual void SetBlockSize(int64_t size); 60 virtual int GetBlockSize(); 61 virtual void SetFlags(int flags); 62 virtual void ToByteArray(std::vector<int8_t>& ret); 63 64 private: 65 static const signed int MAGIC_BYTE_LENGTH = 4; 66 static constexpr int CODE_SIGNING_VERSION = 1; 67 // byte size of magic number 68 static const int8_t MAGIC_BYTE_ARRAY_LENGTH = 8; 69 // lower 8 bytes of MD5 result of string "hap code sign block" (E046 C8C6 5389 FCCD) 70 static const int64_t MAGIC_NUM = ((0xE046C8C6LL << 32) + 0x5389FCCDLL); 71 // size of byte[8] reserved 72 static const int8_t RESERVED_BYTE_ARRAY_LENGTH = 8; 73 // At all times three segment are always included in code sign block 74 // update this if new segments are created. 75 static const int SEGMENT_NUM = 3; 76 int64_t magic = 0; 77 int version = 0; 78 int blockSize = 0; 79 int segmentNum = 0; 80 // FLAG_MERKLE_TREE_INLINED + FLAG_NATIVE_LIB_INCLUDED 81 int flags = 0; 82 std::vector<int8_t> reserved; 83 }; 84 } // namespace SignatureTools 85 } // namespace OHOS 86 #endif // SIGNATRUETOOLS_CODE_SIGN_BLOCK_HEADER_H 87