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
16#ifndef SIGNATRUETOOLS_VERIFY_ELF_H
17#define SIGNATRUETOOLS_VERIFY_ELF_H
18
19#include <string>
20#include <vector>
21
22#include "options.h"
23#include "block_data.h"
24#include "signing_block.h"
25#include "pkcs7_context.h"
26#include "sign_block_info.h"
27
28namespace OHOS {
29namespace SignatureTools {
30class VerifyElf {
31public:
32    static const int8_t SIGNATURE_BLOCK;
33    static const int8_t PROFILE_NOSIGNED_BLOCK;
34    static const int8_t PROFILE_SIGNED_BLOCK;
35    static const int8_t KEY_ROTATION_BLOCK;
36    static const int8_t CODESIGNING_BLOCK_TYPE;
37
38public:
39    bool Verify(Options* options);
40    static bool CheckParams(Options* options);
41    static bool CheckSignFile(const std::string& signedFile);
42    static bool GetSignBlockData(std::vector<int8_t>& bytes, BlockData& blockData,
43        const std::string fileType);
44    static bool GetSignBlockInfo(const std::string& file, SignBlockInfo& signBlockInfo,
45        const std::string fileType);
46    static bool GetFileDigest(std::vector<int8_t>& fileBytes, const std::vector<int8_t>& signatrue,
47        SignBlockInfo& signBlockInfo);
48    static bool GetRawContent(const std::vector<int8_t>& contentVec, std::string& rawContent);
49    static bool VerifyP7b(std::unordered_map<int8_t, SigningBlock>& signBlockMap, Options* options,
50        Pkcs7Context& pkcs7Context, std::vector<int8_t>& profileVec, std::string& profileJson);
51
52private:
53    bool VerifyElfFile(const std::string& elfFile, std::vector<int8_t>& profileVec,
54        Options* options, Pkcs7Context& pkcs7Context);
55    static bool CheckMagicAndVersion(std::vector<int8_t>& bytes, int64_t& offset,
56        const std::string fileType);
57    static void GetElfSignBlock(std::vector<int8_t>& bytes, BlockData& blockData,
58        std::unordered_map<int8_t, SigningBlock>& signBlockMap);
59    static void GetBinSignBlock(std::vector<int8_t>& bytes, BlockData& blockData,
60        std::unordered_map<int8_t, SigningBlock>& signBlockMap);
61    static bool GenerateFileDigest(std::vector<int8_t>& fileBytes, SignBlockInfo& signBlockInfo);
62};
63} // namespace SignatureTools
64} // namespace OHOS
65#endif