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 #ifndef SIGNATURETOOLS_VERIFY_CODE_SIGNATURE_H
16 #define SIGNATURETOOLS_VERIFY_CODE_SIGNATURE_H
17 
18 #include <vector>
19 #include <string>
20 #include <fstream>
21 #include <contrib/minizip/unzip.h>
22 
23 #include "signature_tools_log.h"
24 #include "code_sign_block.h"
25 #include "hap_utils.h"
26 #include "merkle_tree_extension.h"
27 #include "native_lib_info_segment.h"
28 #include "code_signing.h"
29 #include "fs_verity_generator.h"
30 #include "cms_utils.h"
31 
32 namespace OHOS {
33 namespace SignatureTools {
34 class VerifyCodeSignature {
35 public:
36     static bool VerifyHap(std::string file, int64_t offset, int64_t length,
37                           std::string fileFormat, std::string profileContent);
38     static bool VerifyElf(std::string file, int64_t offset, int64_t length,
39                           std::string fileFormat, std::string profileContent);
40     static bool VerifyNativeLib(CodeSignBlock& csb, std::string& file, unzFile& zFile,
41                                 std::pair<std::string, std::string>& pairResult);
42     static bool VerifyCodeSign(std::string file, std::pair<std::string,
43                                std::string>& pairResult, CodeSignBlock& csb);
44     static bool VerifySingleFile(std::istream& input, int64_t length, std::vector<int8_t> signature,
45                                  int64_t merkleTreeOffset, std::vector<int8_t> inMerkleTreeBytes);
46     static bool AreVectorsEqual(const std::vector<int8_t>& vec1, const std::vector<int8_t>& vec2);
47 
48 private:
49     static bool GenerateCodeSignBlock(const std::string& file, int64_t offset, int64_t length,
50                                       CodeSignBlock& csb);
51     static bool ParseSegmentHead(CodeSignBlock& csb, std::ifstream& signedHap,
52                                  std::vector<char>& merkleTreeBytes, int32_t& fileReadOffset);
53     static bool ParseMerkleTree(CodeSignBlock& csb, int32_t readOffset, std::ifstream& signedHap,
54                                 int64_t computedTreeOffset);
55     static int64_t GetAlignmentAddr(int64_t alignment, int64_t input);
56     static std::pair<std::vector<int8_t>, std::vector<int8_t>> GenerateFsVerityDigest(std::istream& inputStream,
57                                                                                       int64_t size,
58                                                                                       int64_t merkleTreeOffset);
59 };
60 } // namespace SignatureTools
61 } // namespace OHOS
62 #endif // SIGNATURETOOLS_CMS_UTILS_H