154aa6d63Sopenharmony_ci/*
254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License.
554aa6d63Sopenharmony_ci * You may obtain a copy of the License at
654aa6d63Sopenharmony_ci *
754aa6d63Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
854aa6d63Sopenharmony_ci *
954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and
1354aa6d63Sopenharmony_ci * limitations under the License.
1454aa6d63Sopenharmony_ci */
1554aa6d63Sopenharmony_ci
1654aa6d63Sopenharmony_ci#ifndef SIGNATRUETOOLS_HAP_UTILS_H
1754aa6d63Sopenharmony_ci#define SIGNATRUETOOLS_HAP_UTILS_H
1854aa6d63Sopenharmony_ci
1954aa6d63Sopenharmony_ci#include <set>
2054aa6d63Sopenharmony_ci#include <vector>
2154aa6d63Sopenharmony_ci#include <string>
2254aa6d63Sopenharmony_ci#include <unordered_map>
2354aa6d63Sopenharmony_ci#include <utility>
2454aa6d63Sopenharmony_ci#include <memory>
2554aa6d63Sopenharmony_ci
2654aa6d63Sopenharmony_ci#include "content_digest_algorithm.h"
2754aa6d63Sopenharmony_ci#include "signing_block.h"
2854aa6d63Sopenharmony_ci#include "zip_data_input.h"
2954aa6d63Sopenharmony_ci#include "signature_tools_log.h"
3054aa6d63Sopenharmony_ci
3154aa6d63Sopenharmony_cinamespace OHOS {
3254aa6d63Sopenharmony_cinamespace SignatureTools {
3354aa6d63Sopenharmony_ciclass HapUtils {
3454aa6d63Sopenharmony_cipublic:
3554aa6d63Sopenharmony_ci    class HapSignBlockInfo {
3654aa6d63Sopenharmony_ci    public:
3754aa6d63Sopenharmony_ci        virtual ~HapSignBlockInfo()
3854aa6d63Sopenharmony_ci        {
3954aa6d63Sopenharmony_ci        }
4054aa6d63Sopenharmony_ci        HapSignBlockInfo(int64_t offset, int version, ByteBuffer contentByteBuffer);
4154aa6d63Sopenharmony_ci
4254aa6d63Sopenharmony_ci        virtual int GetVersion();
4354aa6d63Sopenharmony_ci        virtual ByteBuffer GetContent();
4454aa6d63Sopenharmony_ci        virtual int64_t GetOffset();
4554aa6d63Sopenharmony_ci
4654aa6d63Sopenharmony_ci    private:
4754aa6d63Sopenharmony_ci        const int64_t offset;
4854aa6d63Sopenharmony_ci        const int version;
4954aa6d63Sopenharmony_ci        ByteBuffer const content;
5054aa6d63Sopenharmony_ci    };
5154aa6d63Sopenharmony_ci
5254aa6d63Sopenharmony_cipublic:
5354aa6d63Sopenharmony_ci    static constexpr int HAP_SIGNATURE_SCHEME_V1_BLOCK_ID = 0x20000000;
5454aa6d63Sopenharmony_ci    static constexpr int HAP_PROOF_OF_ROTATION_BLOCK_ID = 0x20000001;
5554aa6d63Sopenharmony_ci    static constexpr int HAP_PROFILE_BLOCK_ID = 0x20000002;
5654aa6d63Sopenharmony_ci    static constexpr int HAP_PROPERTY_BLOCK_ID = 0x20000003;
5754aa6d63Sopenharmony_ci    static constexpr int HAP_CODE_SIGN_BLOCK_ID = 0x30000001;
5854aa6d63Sopenharmony_ci    static constexpr int CONTENT_DIGESTED_CHUNK_MAX_SIZE_BYTES = 1024 * 1024;
5954aa6d63Sopenharmony_ci    static constexpr int CONTENT_VERSION = 2;
6054aa6d63Sopenharmony_ci    static constexpr int BIT_SIZE = 8;
6154aa6d63Sopenharmony_ci    static constexpr int HALF_BIT_SIZE = 4;
6254aa6d63Sopenharmony_ci    static constexpr int INT_SIZE = 4;
6354aa6d63Sopenharmony_ci    static constexpr int BLOCK_NUMBER = 1;
6454aa6d63Sopenharmony_ci    static constexpr int HAP_SIGN_SCHEME_V2_BLOCK_VERSION = 2;
6554aa6d63Sopenharmony_ci    static constexpr int HAP_SIGN_SCHEME_V3_BLOCK_VERSION = 3;
6654aa6d63Sopenharmony_ci    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_LO_V2 = 0x2067695320504148LL;
6754aa6d63Sopenharmony_ci    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_HI_V2 = 0x3234206b636f6c42LL;
6854aa6d63Sopenharmony_ci    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_LO_V3 = 0x676973207061683cLL;
6954aa6d63Sopenharmony_ci    static constexpr int64_t HAP_SIG_BLOCK_MAGIC_HI_V3 = 0x3e6b636f6c62206eLL;
7054aa6d63Sopenharmony_ci    static constexpr int HAP_SIG_BLOCK_HEADER_SIZE = 32;
7154aa6d63Sopenharmony_ci    static constexpr int HAP_SIG_BLOCK_MIN_SIZE = HAP_SIG_BLOCK_HEADER_SIZE;
7254aa6d63Sopenharmony_ci    static constexpr int BLOCK_SIZE = 8;
7354aa6d63Sopenharmony_ci
7454aa6d63Sopenharmony_cipublic:
7554aa6d63Sopenharmony_ci    static std::string GetAppIdentifier(const std::string& profileContent);
7654aa6d63Sopenharmony_ci    static std::pair<std::string, std::string> ParseAppIdentifier(const std::string& profileContent);
7754aa6d63Sopenharmony_ci    static std::vector<int8_t> GetHapSigningBlockMagic(int compatibleVersion);
7854aa6d63Sopenharmony_ci    static int GetHapSigningBlockVersion(int compatibleVersion);
7954aa6d63Sopenharmony_ci    static bool ReadFileToByteBuffer(const std::string& file, ByteBuffer& buffer);
8054aa6d63Sopenharmony_ci
8154aa6d63Sopenharmony_ciprivate:
8254aa6d63Sopenharmony_ci    static const int32_t MAX_APP_ID_LEN = 32;
8354aa6d63Sopenharmony_ci    static const std::string HAP_DEBUG_OWNER_ID;
8454aa6d63Sopenharmony_ci    static std::set<int> HAP_SIGNATURE_OPTIONAL_BLOCK_IDS;
8554aa6d63Sopenharmony_ci    static constexpr int MIN_COMPATIBLE_VERSION_FOR_SCHEMA_V3 = 8;
8654aa6d63Sopenharmony_ci    static const std::vector<int8_t> HAP_SIGNING_BLOCK_MAGIC_V2;
8754aa6d63Sopenharmony_ci    static const std::vector<int8_t> HAP_SIGNING_BLOCK_MAGIC_V3;
8854aa6d63Sopenharmony_ci    static constexpr int8_t ZIP_FIRST_LEVEL_CHUNK_PREFIX = 0x5a;
8954aa6d63Sopenharmony_ci    static const int8_t ZIP_SECOND_LEVEL_CHUNK_PREFIX = static_cast<int8_t>(0xa5);
9054aa6d63Sopenharmony_ci    static const int DIGEST_PRIFIX_LENGTH = 5;
9154aa6d63Sopenharmony_ci    static constexpr int BUFFER_LENGTH = 4096;
9254aa6d63Sopenharmony_ci    static const std::string HEX_CHAR_ARRAY;
9354aa6d63Sopenharmony_ci
9454aa6d63Sopenharmony_ci    class StaticConstructor {
9554aa6d63Sopenharmony_ci    public:
9654aa6d63Sopenharmony_ci        StaticConstructor();
9754aa6d63Sopenharmony_ci    };
9854aa6d63Sopenharmony_ci
9954aa6d63Sopenharmony_ci    static HapUtils::StaticConstructor staticConstructor;
10054aa6d63Sopenharmony_ci};
10154aa6d63Sopenharmony_ci} // namespace SignatureTools
10254aa6d63Sopenharmony_ci} // namespace OHOS
10354aa6d63Sopenharmony_ci#endif
104