• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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_SIGH_ELF_H
17 #define SIGNATRUETOOLS_SIGH_ELF_H
18 
19 #include <list>
20 #include <string>
21 #include <vector>
22 #include <elfio.hpp>
23 
24 #include "signer_config.h"
25 #include "signature_tools_log.h"
26 
27 namespace OHOS {
28 namespace SignatureTools {
29 class SignElf {
30 public:
31     static bool Sign(SignerConfig& signerConfig, std::map<std::string, std::string> &signParams);
32 
33 private:
34     static constexpr int PAGE_SIZE = 4096;
35     static const std::string profileSec;
36     static const std::string permissionSec;
37     static const std::string codesignSec;
38 
39     static bool loadModule(std::map<std::string, std::string>& signParams, std::string& moduleContent);
40     static bool loadProfileAndSign(SignerConfig& signerConfig, std::map<std::string, std::string>& signParams,
41                             std::string& p7b);
42     static bool isExecElf(ELFIO::elfio& reader);
43     static bool WriteCodeSignBlock(ELFIO::elfio& reader, std::string& tmpOutputFilePath, uint64_t& csOffset);
44     static bool WriteSection(ELFIO::elfio& reader, const std::string& content, const std::string& secName);
45     static bool WriteSecDataToFile(ELFIO::elfio& reader, SignerConfig& signerConfig,
46                                    std::map<std::string, std::string>& signParams);
47     static bool GenerateCodeSignByte(SignerConfig& signerConfig, const std::string& inputFile, uint64_t& csOffset,
48                                      const std::string& selfSign);
49     static bool ReplaceDataOffset(const std::string& filePath, uint64_t& csOffset, const std::vector<int8_t>& csData);
50 };
51 } // namespace SignatureTools
52 } // namespace OHOS
53 #endif
54