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