1 /* 2 * Copyright (c) 2023 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 CODE_SIGN_ENABLE_MULTI_TASK_H 17 #define CODE_SIGN_ENABLE_MULTI_TASK_H 18 19 #include <condition_variable> 20 #include <cstdint> 21 #include <mutex> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 #include <linux/fsverity.h> 26 27 #include "thread_pool.h" 28 29 namespace OHOS { 30 namespace Security { 31 namespace CodeSign { 32 typedef int32_t CallbackFunc(const std::string &path, const struct code_sign_enable_arg &arg); 33 34 class CodeSignEnableMultiTask { 35 public: 36 CodeSignEnableMultiTask(); 37 ~CodeSignEnableMultiTask(); 38 /** 39 * @brief Add task data for code signing 40 * @param targetFile hap or so real path on disk 41 * @param code_sign_enable_arg arg 42 */ 43 void AddTaskData(const std::string &targetFile, const struct code_sign_enable_arg &arg); 44 /** 45 * @brief Execute code signature addition task 46 * @param taskRet Returned execution results 47 * @param ownerId app-identifier of the signature 48 * @param path hap real path on disk 49 * @param func Callback enable function 50 * @return Timed out or not 51 */ 52 bool ExecuteEnableCodeSignTask(int32_t &taskRet, const std::string &ownerId, 53 const std::string &path, CallbackFunc &func); 54 private: 55 void SortTaskData(); 56 void ExecuteEnableCodeSignTask(uint32_t &index, int32_t &taskRet, const std::string &ownerId, 57 const std::string &path, CallbackFunc &func); 58 int32_t CheckOwnerId(const std::string &path, const std::string &ownerId, 59 const uint8_t *sigPtr, uint32_t sigSize); 60 private: 61 std::mutex cvLock_; 62 std::condition_variable taskfinish_; 63 std::vector<std::pair<std::string, code_sign_enable_arg >> enableData_; 64 OHOS::ThreadPool enableCodeSignTaskWorker_; 65 uint32_t taskCallBack_; 66 }; 67 } 68 } 69 } 70 71 #endif