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_CODE_SIGN_BLOCK_H
1654aa6d63Sopenharmony_ci#define SIGNATRUETOOLS_CODE_SIGN_BLOCK_H
1754aa6d63Sopenharmony_ci
1854aa6d63Sopenharmony_ci#include <map>
1954aa6d63Sopenharmony_ci#include <string>
2054aa6d63Sopenharmony_ci#include <vector>
2154aa6d63Sopenharmony_ci#include <unordered_map>
2254aa6d63Sopenharmony_ci
2354aa6d63Sopenharmony_ci#include "segment_header.h"
2454aa6d63Sopenharmony_ci#include "hap_info_segment.h"
2554aa6d63Sopenharmony_ci#include "code_sign_block_header.h"
2654aa6d63Sopenharmony_ci#include "fs_verity_info_segment.h"
2754aa6d63Sopenharmony_ci#include "native_lib_info_segment.h"
2854aa6d63Sopenharmony_ci
2954aa6d63Sopenharmony_cinamespace OHOS {
3054aa6d63Sopenharmony_cinamespace SignatureTools {
3154aa6d63Sopenharmony_ci
3254aa6d63Sopenharmony_ciclass CodeSignBlock {
3354aa6d63Sopenharmony_cipublic:
3454aa6d63Sopenharmony_ci    static constexpr long PAGE_SIZE_4K = 4096;
3554aa6d63Sopenharmony_ci    static constexpr int SEGMENT_HEADER_COUNT = 3;
3654aa6d63Sopenharmony_ci    CodeSignBlock();
3754aa6d63Sopenharmony_ci    virtual ~CodeSignBlock();
3854aa6d63Sopenharmony_ci    void AddOneMerkleTree(const std::string& key, const std::vector<int8_t>& merkleTree);
3954aa6d63Sopenharmony_ci    std::vector<int8_t> GetOneMerkleTreeByFileName(const std::string& key);
4054aa6d63Sopenharmony_ci    void SetCodeSignBlockFlag();
4154aa6d63Sopenharmony_ci    void SetSegmentNum();
4254aa6d63Sopenharmony_ci    void AddToSegmentList(const SegmentHeader& sh);
4354aa6d63Sopenharmony_ci    std::vector<SegmentHeader>& GetSegmentHeaderList();
4454aa6d63Sopenharmony_ci    void SetSegmentHeaders();
4554aa6d63Sopenharmony_ci    CodeSignBlockHeader& GetCodeSignBlockHeader();
4654aa6d63Sopenharmony_ci    void SetCodeSignBlockHeader(const CodeSignBlockHeader& csbHeader);
4754aa6d63Sopenharmony_ci    void SetFsVerityInfoSegment(const FsVerityInfoSegment& fsVeritySeg);
4854aa6d63Sopenharmony_ci    HapInfoSegment& GetHapInfoSegment();
4954aa6d63Sopenharmony_ci    void SetHapInfoSegment(const HapInfoSegment& hapSeg);
5054aa6d63Sopenharmony_ci    NativeLibInfoSegment& GetSoInfoSegment();
5154aa6d63Sopenharmony_ci    void SetSoInfoSegment(const NativeLibInfoSegment& soSeg);
5254aa6d63Sopenharmony_ci    void ToByteArray(std::vector<int8_t>& ret);
5354aa6d63Sopenharmony_ci    void ComputeSegmentOffset();
5454aa6d63Sopenharmony_ci    int64_t ComputeMerkleTreeOffset(int64_t codeSignBlockOffset);
5554aa6d63Sopenharmony_ci    void GenerateCodeSignBlockByte(int64_t fsvTreeOffset, std::vector<int8_t>& ret);
5654aa6d63Sopenharmony_ci
5754aa6d63Sopenharmony_ciprivate:
5854aa6d63Sopenharmony_ci    CodeSignBlockHeader codeSignBlockHeader;
5954aa6d63Sopenharmony_ci    std::vector<SegmentHeader> segmentHeaderList;
6054aa6d63Sopenharmony_ci    FsVerityInfoSegment fsVerityInfoSegment;
6154aa6d63Sopenharmony_ci    HapInfoSegment hapInfoSegment;
6254aa6d63Sopenharmony_ci    NativeLibInfoSegment nativeLibInfoSegment;
6354aa6d63Sopenharmony_ci    std::vector<int8_t> zeroPadding;
6454aa6d63Sopenharmony_ci    std::map<std::string, std::vector<int8_t>> merkleTreeMap;
6554aa6d63Sopenharmony_ci};
6654aa6d63Sopenharmony_ci} // namespace SignatureTools
6754aa6d63Sopenharmony_ci} // namespace OHOS
6854aa6d63Sopenharmony_ci#endif // SIGNATRUETOOLS_CODE_SIGN_BLOCK_H
69