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#ifndef SIGNATRUETOOLS_SIGNING_BLOCK_UTILS_H
1654aa6d63Sopenharmony_ci#define SIGNATRUETOOLS_SIGNING_BLOCK_UTILS_H
1754aa6d63Sopenharmony_ci
1854aa6d63Sopenharmony_ci#include <vector>
1954aa6d63Sopenharmony_ci
2054aa6d63Sopenharmony_ci#include "data_source.h"
2154aa6d63Sopenharmony_ci#include "export_define.h"
2254aa6d63Sopenharmony_ci#include "byte_buffer.h"
2354aa6d63Sopenharmony_ci#include "random_access_file.h"
2454aa6d63Sopenharmony_ci#include "digest_parameter.h"
2554aa6d63Sopenharmony_ci#include "pkcs7_context.h"
2654aa6d63Sopenharmony_ci#include "signature_info.h"
2754aa6d63Sopenharmony_ci
2854aa6d63Sopenharmony_cinamespace OHOS {
2954aa6d63Sopenharmony_cinamespace SignatureTools {
3054aa6d63Sopenharmony_ci
3154aa6d63Sopenharmony_ciconstexpr int32_t ZIP_CHUNK_DIGEST_PRIFIX_LEN = 5;
3254aa6d63Sopenharmony_ci
3354aa6d63Sopenharmony_cistruct HapSignBlockHead {
3454aa6d63Sopenharmony_ci    int32_t version = 0;
3554aa6d63Sopenharmony_ci    int32_t blockCount = 0;
3654aa6d63Sopenharmony_ci    int64_t hapSignBlockSize;
3754aa6d63Sopenharmony_ci    int64_t hapSignBlockMagicLo;
3854aa6d63Sopenharmony_ci    int64_t hapSignBlockMagicHi;
3954aa6d63Sopenharmony_ci};
4054aa6d63Sopenharmony_ci
4154aa6d63Sopenharmony_cienum HapBlobType {
4254aa6d63Sopenharmony_ci    HAP_SIGN_BLOB = 0x20000000,
4354aa6d63Sopenharmony_ci    PROOF_ROTATION_BLOB = 0x20000001,
4454aa6d63Sopenharmony_ci    PROFILE_BLOB = 0x20000002,
4554aa6d63Sopenharmony_ci    PROPERTY_BLOB = 0x20000003,
4654aa6d63Sopenharmony_ci};
4754aa6d63Sopenharmony_ci
4854aa6d63Sopenharmony_cistruct HapSubSignBlockHead {
4954aa6d63Sopenharmony_ci    uint32_t type = 0;
5054aa6d63Sopenharmony_ci    uint32_t length = 0;
5154aa6d63Sopenharmony_ci    uint32_t offset = 0;
5254aa6d63Sopenharmony_ci};
5354aa6d63Sopenharmony_ci
5454aa6d63Sopenharmony_ciclass HapSignerBlockUtils {
5554aa6d63Sopenharmony_cipublic:
5654aa6d63Sopenharmony_ci    DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_HIGH_OLD;
5754aa6d63Sopenharmony_ci    DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_LOW_OLD;
5854aa6d63Sopenharmony_ci    DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_HIGH;
5954aa6d63Sopenharmony_ci    DLL_EXPORT static const int64_t HAP_SIG_BLOCK_MAGIC_LOW;
6054aa6d63Sopenharmony_ci    DLL_EXPORT static const int32_t ZIP_HEAD_OF_SIGNING_BLOCK_LENGTH;
6154aa6d63Sopenharmony_ci    DLL_EXPORT static const int32_t ZIP_EOCD_SEGMENT_FLAG;
6254aa6d63Sopenharmony_ci    static const int64_t CHUNK_SIZE;
6354aa6d63Sopenharmony_ci    static const int32_t HAP_SIG_BLOCK_MIN_SIZE;
6454aa6d63Sopenharmony_ci    static const int32_t ZIP_EOCD_SEG_MIN_SIZE;
6554aa6d63Sopenharmony_ci    static const int32_t ZIP_EOCD_COMMENT_LENGTH_OFFSET;
6654aa6d63Sopenharmony_ci    static const int32_t ZIP_CD_OFFSET_IN_EOCD;
6754aa6d63Sopenharmony_ci    static const int32_t ZIP_CD_SIZE_OFFSET_IN_EOCD;
6854aa6d63Sopenharmony_ci    static const int32_t ZIP_BLOCKS_NUM_NEED_DIGEST;
6954aa6d63Sopenharmony_ci    static const char ZIP_FIRST_LEVEL_CHUNK_PREFIX;
7054aa6d63Sopenharmony_ci    static const char ZIP_SECOND_LEVEL_CHUNK_PREFIX;
7154aa6d63Sopenharmony_ci    /* the specifications of hap sign block */
7254aa6d63Sopenharmony_ci    static constexpr int64_t MAX_HAP_SIGN_BLOCK_SIZE = 1024 * 1024 * 1024LL; // 1024MB
7354aa6d63Sopenharmony_ci    static constexpr int32_t MAX_BLOCK_COUNT = 10;
7454aa6d63Sopenharmony_ci    static constexpr int32_t VERSION_FOR_NEW_MAGIC_NUM = 3;
7554aa6d63Sopenharmony_ci    static constexpr int32_t TEST_FILE_BLOCK_LENGTH = 50;
7654aa6d63Sopenharmony_ci    static constexpr int32_t TEST_FILE_BLOCK_COUNT = 3;
7754aa6d63Sopenharmony_ci
7854aa6d63Sopenharmony_ci    HapSignerBlockUtils() = delete;
7954aa6d63Sopenharmony_ci    DLL_EXPORT static bool FindHapSignature(RandomAccessFile& hapFile, SignatureInfo& signInfo);
8054aa6d63Sopenharmony_ci    DLL_EXPORT static bool GetOptionalBlockIndex(std::vector<OptionalBlock>& optionBlocks,
8154aa6d63Sopenharmony_ci                                                 int32_t type, int& index);
8254aa6d63Sopenharmony_ci    DLL_EXPORT static bool VerifyHapIntegrity(Pkcs7Context& digestInfo, RandomAccessFile& hapFile,
8354aa6d63Sopenharmony_ci                                              SignatureInfo& signInfo);
8454aa6d63Sopenharmony_ci    DLL_EXPORT static int64_t CreatTestZipFile(const std::string& pathFile, SignatureInfo& signInfo);
8554aa6d63Sopenharmony_ci
8654aa6d63Sopenharmony_ci    DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, std::pair<ByteBuffer, int64_t>& eocd);
8754aa6d63Sopenharmony_ci    DLL_EXPORT static bool FindEocdInHap(RandomAccessFile& hapFile, unsigned short maxCommentSize,
8854aa6d63Sopenharmony_ci                                         std::pair<ByteBuffer, int64_t>& eocd);
8954aa6d63Sopenharmony_ci    DLL_EXPORT static bool FindEocdInSearchBuffer(ByteBuffer& zipContents, int& offset);
9054aa6d63Sopenharmony_ci    DLL_EXPORT static bool GetCentralDirectoryOffset(ByteBuffer& eocd, int64_t eocdOffset,
9154aa6d63Sopenharmony_ci                                                     int64_t& centralDirectoryOffset);
9254aa6d63Sopenharmony_ci    DLL_EXPORT static bool GetCentralDirectorySize(ByteBuffer& eocd, long& centralDirectorySize);
9354aa6d63Sopenharmony_ci    static bool FindHapSigningBlock(RandomAccessFile& hapFile, int64_t centralDirOffset,
9454aa6d63Sopenharmony_ci                                    SignatureInfo& signInfo);
9554aa6d63Sopenharmony_ci    static bool FindHapSubSigningBlock(RandomAccessFile& hapFile,
9654aa6d63Sopenharmony_ci                                       int32_t blockCount,
9754aa6d63Sopenharmony_ci                                       int64_t blockArrayLen,
9854aa6d63Sopenharmony_ci                                       int64_t hapSignBlockOffset,
9954aa6d63Sopenharmony_ci                                       SignatureInfo& signInfo);
10054aa6d63Sopenharmony_ci    DLL_EXPORT static bool ClassifyHapSubSigningBlock(SignatureInfo& signInfo,
10154aa6d63Sopenharmony_ci                                                      const ByteBuffer& subBlock, uint32_t type);
10254aa6d63Sopenharmony_ci    DLL_EXPORT static bool SetUnsignedInt32(ByteBuffer& buffer, int32_t offset, int64_t value);
10354aa6d63Sopenharmony_ci    DLL_EXPORT static bool ComputeDigestsWithOptionalBlock(const DigestParameter& digestParam,
10454aa6d63Sopenharmony_ci                                                           const std::vector<OptionalBlock>& optionalBlocks,
10554aa6d63Sopenharmony_ci                                                           const ByteBuffer& chunkDigest,
10654aa6d63Sopenharmony_ci                                                           ByteBuffer& finalDigest);
10754aa6d63Sopenharmony_ci    static bool ComputeDigestsForEachChunk(const DigestParameter& digestParam, DataSource* contents[],
10854aa6d63Sopenharmony_ci                                           int32_t len, ByteBuffer& result);
10954aa6d63Sopenharmony_ci    static int32_t GetChunkCount(int64_t inputSize, int64_t chunkSize);
11054aa6d63Sopenharmony_ci    static bool InitDigestPrefix(const DigestParameter& digestParam,
11154aa6d63Sopenharmony_ci                                 unsigned char(&chunkContentPrefix)[ZIP_CHUNK_DIGEST_PRIFIX_LEN],
11254aa6d63Sopenharmony_ci                                 int32_t chunkLen);
11354aa6d63Sopenharmony_ci    DLL_EXPORT static DigestParameter GetDigestParameter(int32_t nId);
11454aa6d63Sopenharmony_ci    DLL_EXPORT static bool GetSumOfChunkDigestLen(DataSource* contents[], int32_t len, int32_t chunkDigestLen,
11554aa6d63Sopenharmony_ci                                                  int& chunkCount, int& sumOfChunkDigestLen);
11654aa6d63Sopenharmony_ci    static bool ParseSignBlockHead(HapSignBlockHead& hapSignBlockHead, ByteBuffer& hapBlockHead);
11754aa6d63Sopenharmony_ci    static bool ParseSubSignBlockHead(HapSubSignBlockHead& subSignBlockHead, ByteBuffer& hapBlockHead);
11854aa6d63Sopenharmony_ci    static bool CheckSignBlockHead(const HapSignBlockHead& hapSignBlockHead);
11954aa6d63Sopenharmony_ci    static void CreateHapSubSignBlockHead(HapSubSignBlockHead& signBlob, HapSubSignBlockHead& profileBlob,
12054aa6d63Sopenharmony_ci                                          HapSubSignBlockHead& propertyBlob);
12154aa6d63Sopenharmony_ci};
12254aa6d63Sopenharmony_ci} // namespace SignatureTools
12354aa6d63Sopenharmony_ci} // namespace OHOS
12454aa6d63Sopenharmony_ci#endif // HAP_SIGNING_BLOCK_UTILS_H
125