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_SIGH_ELF_H 17 #define SIGNATRUETOOLS_SIGH_ELF_H 18 19 #include <list> 20 #include <ctime> 21 #include <string> 22 #include <chrono> 23 #include <vector> 24 25 #include "signer_config.h" 26 #include "sign_block_data.h" 27 #include "hap_signer_block_utils.h" 28 #include "signature_tools_log.h" 29 30 namespace OHOS { 31 namespace SignatureTools { 32 class SignElf { 33 public: 34 static const char CODESIGN_BLOCK_TYPE = 3; 35 static bool Sign(SignerConfig& signerConfig, std::map<std::string, std::string> &signParams); 36 37 private: 38 static int blockNum; 39 static constexpr int FILE_PATH_LENGTH = 256; 40 static constexpr int PAGE_SIZE = 4096; 41 static constexpr int FILE_BUFFER_BLOCK = 16384; 42 static const std::string CODESIGN_OFF; 43 44 static bool AlignFileBy4kBytes(const std::string& inputFile, std::string& tmp); 45 static bool WriteBlockDataToFile(SignerConfig& signerConfig, 46 const std::string& inputFile, std::string& outputFile, 47 const std::string &profileSigned, 48 const std::map<std::string, std::string>& signParams); 49 static bool WriteSignedElf(const std::string &inputFile, 50 std::list<SignBlockData>& signBlockList, std::string &outputFile); 51 static bool WriteSignBlockData(std::list<SignBlockData>& signBlockList, std::ofstream& fileOutputStream); 52 static bool GenerateSignBlockHead(std::list<SignBlockData>& signDataList); 53 static SignBlockData GenerateProfileSignByte(std::string profileFile, std::string profileSigned); 54 static bool GenerateCodeSignByte(SignerConfig& signerConfig, const std::map<std::string, std::string> &signParams, 55 const std::string &inputFile, const int blockNum, 56 const long binFileLen, SignBlockData** codeSign); 57 static bool WriteSignHeadDataToOutputFile(const std::string &inputFile, const std::string &outputFile, 58 const int blockNum); 59 static bool IsLongOverflowInteger(const int64_t num); 60 static bool IsLongOverflowShort(const int64_t num); 61 }; 62 } // namespace SignatureTools 63 } // namespace OHOS 64 #endif 65