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 SIGNATURETOOLS_VERIFY_CODE_SIGNATURE_H
1654aa6d63Sopenharmony_ci#define SIGNATURETOOLS_VERIFY_CODE_SIGNATURE_H
1754aa6d63Sopenharmony_ci
1854aa6d63Sopenharmony_ci#include <vector>
1954aa6d63Sopenharmony_ci#include <string>
2054aa6d63Sopenharmony_ci#include <fstream>
2154aa6d63Sopenharmony_ci#include <contrib/minizip/unzip.h>
2254aa6d63Sopenharmony_ci
2354aa6d63Sopenharmony_ci#include "signature_tools_log.h"
2454aa6d63Sopenharmony_ci#include "code_sign_block.h"
2554aa6d63Sopenharmony_ci#include "hap_utils.h"
2654aa6d63Sopenharmony_ci#include "merkle_tree_extension.h"
2754aa6d63Sopenharmony_ci#include "native_lib_info_segment.h"
2854aa6d63Sopenharmony_ci#include "code_signing.h"
2954aa6d63Sopenharmony_ci#include "fs_verity_generator.h"
3054aa6d63Sopenharmony_ci#include "cms_utils.h"
3154aa6d63Sopenharmony_ci
3254aa6d63Sopenharmony_cinamespace OHOS {
3354aa6d63Sopenharmony_cinamespace SignatureTools {
3454aa6d63Sopenharmony_ciclass VerifyCodeSignature {
3554aa6d63Sopenharmony_cipublic:
3654aa6d63Sopenharmony_ci    static bool VerifyHap(std::string file, int64_t offset, int64_t length,
3754aa6d63Sopenharmony_ci                          std::string fileFormat, std::string profileContent);
3854aa6d63Sopenharmony_ci    static bool VerifyElf(std::string file, int64_t offset, int64_t length,
3954aa6d63Sopenharmony_ci                          std::string fileFormat, std::string profileContent);
4054aa6d63Sopenharmony_ci    static bool VerifyNativeLib(CodeSignBlock& csb, std::string& file, unzFile& zFile,
4154aa6d63Sopenharmony_ci                                std::pair<std::string, std::string>& pairResult);
4254aa6d63Sopenharmony_ci    static bool VerifyCodeSign(std::string file, std::pair<std::string,
4354aa6d63Sopenharmony_ci                               std::string>& pairResult, CodeSignBlock& csb);
4454aa6d63Sopenharmony_ci    static bool VerifySingleFile(std::istream& input, int64_t length, std::vector<int8_t> signature,
4554aa6d63Sopenharmony_ci                                 int64_t merkleTreeOffset, std::vector<int8_t> inMerkleTreeBytes);
4654aa6d63Sopenharmony_ci    static bool AreVectorsEqual(const std::vector<int8_t>& vec1, const std::vector<int8_t>& vec2);
4754aa6d63Sopenharmony_ci
4854aa6d63Sopenharmony_ciprivate:
4954aa6d63Sopenharmony_ci    static bool GenerateCodeSignBlock(const std::string& file, int64_t offset, int64_t length,
5054aa6d63Sopenharmony_ci                                      CodeSignBlock& csb);
5154aa6d63Sopenharmony_ci    static bool ParseSegmentHead(CodeSignBlock& csb, std::ifstream& signedHap,
5254aa6d63Sopenharmony_ci                                 std::vector<char>& merkleTreeBytes, int32_t& fileReadOffset);
5354aa6d63Sopenharmony_ci    static bool ParseMerkleTree(CodeSignBlock& csb, int32_t readOffset, std::ifstream& signedHap,
5454aa6d63Sopenharmony_ci                                int64_t computedTreeOffset);
5554aa6d63Sopenharmony_ci    static int64_t GetAlignmentAddr(int64_t alignment, int64_t input);
5654aa6d63Sopenharmony_ci    static std::pair<std::vector<int8_t>, std::vector<int8_t>> GenerateFsVerityDigest(std::istream& inputStream,
5754aa6d63Sopenharmony_ci                                                                                      int64_t size,
5854aa6d63Sopenharmony_ci                                                                                      int64_t merkleTreeOffset);
5954aa6d63Sopenharmony_ci};
6054aa6d63Sopenharmony_ci} // namespace SignatureTools
6154aa6d63Sopenharmony_ci} // namespace OHOS
6254aa6d63Sopenharmony_ci#endif // SIGNATURETOOLS_CMS_UTILS_H