• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ownerId app-identifier of the signature
47      * @param pluginId plugin-identifier of the signature
48      * @param path hap real path on disk
49      * @param func Callback enable function
50      * @return err code, see err_code.h
51      */
52     int32_t ExecuteEnableCodeSignTask(const std::string &ownerId, const std::string &pluginId,
53         const std::string &path, CallbackFunc &func);
54     /**
55      * @brief Check whether file is verity enabled by fd
56      * @param fd file descriptor
57      * @return err code, see err_code.h
58      */
59     static int32_t IsFsVerityEnabled(int fd);
60 private:
61     static int32_t IsFsVerityEnabled(const std::string &path);
62     void SortTaskData();
63     void ExecuteEnableCodeSignTask(uint32_t &index, int32_t &taskRet, const std::string &ownerId,
64         const std::string &pluginId, const std::string &path, CallbackFunc &func);
65     int32_t CheckOwnerId(const std::string &path, const std::string &ownerId,
66         const uint8_t *sigPtr, uint32_t sigSize);
67     int32_t CheckPluginId(const std::string &path, const std::string &pluginId,
68         const uint8_t *sigPtr, uint32_t sigSize);
69 private:
70     std::mutex cvLock_;
71     std::condition_variable taskfinish_;
72     std::vector<std::pair<std::string, code_sign_enable_arg >> enableData_;
73     OHOS::ThreadPool enableCodeSignTaskWorker_;
74     uint32_t taskCallBack_;
75 };
76 }
77 }
78 }
79 
80 #endif