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
16#ifndef SIGNATRUETOOLS_HAP_UTILS_H
17#define SIGNATRUETOOLS_HAP_UTILS_H
18
19#include <set>
20#include <vector>
21#include <string>
22#include <unordered_map>
23#include <utility>
24#include <memory>
25
26#include "content_digest_algorithm.h"
27#include "signing_block.h"
28#include "zip_data_input.h"
29#include "signature_tools_log.h"
30
31namespace OHOS {
32namespace SignatureTools {
33class HapUtils {
34public:
35    class HapSignBlockInfo {
36    public:
37        virtual ~HapSignBlockInfo()
38        {
39        }
40        HapSignBlockInfo(int64_t offset, int version, ByteBuffer contentByteBuffer);
41
42        virtual int GetVersion();
43        virtual ByteBuffer GetContent();
44        virtual int64_t GetOffset();
45
46    private:
47        const int64_t offset;
48        const int version;
49        ByteBuffer const content;
50    };
51
52public:
53    static constexpr int HAP_SIGNATURE_SCHEME_V1_BLOCK_ID = 0x20000000;
54    static constexpr int HAP_PROOF_OF_ROTATION_BLOCK_ID = 0x20000001;
55    static constexpr int HAP_PROFILE_BLOCK_ID = 0x20000002;
56    static constexpr int HAP_PROPERTY_BLOCK_ID = 0x20000003;
57    static constexpr int HAP_CODE_SIGN_BLOCK_ID = 0x30000001;
58    static constexpr int CONTENT_DIGESTED_CHUNK_MAX_SIZE_BYTES = 1024 * 1024;
59    static constexpr int CONTENT_VERSION = 2;
60    static constexpr int BIT_SIZE = 8;
61    static constexpr int HALF_BIT_SIZE = 4;
62    static constexpr int INT_SIZE = 4;
63    static constexpr int BLOCK_NUMBER = 1;
64    static constexpr int HAP_SIGN_SCHEME_V2_BLOCK_VERSION = 2;
65    static constexpr int HAP_SIGN_SCHEME_V3_BLOCK_VERSION = 3;
66    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_LO_V2 = 0x2067695320504148LL;
67    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_HI_V2 = 0x3234206b636f6c42LL;
68    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_LO_V3 = 0x676973207061683cLL;
69    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_HI_V3 = 0x3e6b636f6c62206eLL;
70    static constexpr int HAP_SIG_BLOCK_HEADER_SIZE = 32;
71    static constexpr int HAP_SIG_BLOCK_MIN_SIZE = HAP_SIG_BLOCK_HEADER_SIZE;
72    static constexpr int BLOCK_SIZE = 8;
73
74public:
75    static std::string GetAppIdentifier(const std::string& profileContent);
76    static std::pair<std::string, std::string> ParseAppIdentifier(const std::string& profileContent);
77    static std::vector<int8_t> GetHapSigningBlockMagic(int compatibleVersion);
78    static int GetHapSigningBlockVersion(int compatibleVersion);
79    static bool ReadFileToByteBuffer(const std::string& file, ByteBuffer& buffer);
80
81private:
82    static const int32_t MAX_APP_ID_LEN = 32;
83    static const std::string HAP_DEBUG_OWNER_ID;
84    static std::set<int> HAP_SIGNATURE_OPTIONAL_BLOCK_IDS;
85    static constexpr int MIN_COMPATIBLE_VERSION_FOR_SCHEMA_V3 = 8;
86    static const std::vector<int8_t> HAP_SIGNING_BLOCK_MAGIC_V2;
87    static const std::vector<int8_t> HAP_SIGNING_BLOCK_MAGIC_V3;
88    static constexpr int8_t ZIP_FIRST_LEVEL_CHUNK_PREFIX = 0x5a;
89    static const int8_t ZIP_SECOND_LEVEL_CHUNK_PREFIX = static_cast<int8_t>(0xa5);
90    static const int DIGEST_PRIFIX_LENGTH = 5;
91    static constexpr int BUFFER_LENGTH = 4096;
92    static const std::string HEX_CHAR_ARRAY;
93
94    class StaticConstructor {
95    public:
96        StaticConstructor();
97    };
98
99    static HapUtils::StaticConstructor staticConstructor;
100};
101} // namespace SignatureTools
102} // namespace OHOS
103#endif
104