1 /*
2 * Copyright (c) 2022 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 #include "coauth_interface.h"
17
18 #include "securec.h"
19
20 extern "C" {
21 #include "coauth_funcs.h"
22 #include "defines.h"
23 #include "adaptor_log.h"
24 #include "lock.h"
25 }
26
27 namespace OHOS {
28 namespace UserIAM {
29 namespace CoAuth {
CopyExecutorInfoOut(const ExecutorInfoHal & executorInfoHal)30 static ExecutorInfo CopyExecutorInfoOut(const ExecutorInfoHal &executorInfoHal)
31 {
32 ExecutorInfo executorInfo;
33 executorInfo.authType = executorInfoHal.authType;
34 executorInfo.authAbility = executorInfoHal.authAbility;
35 executorInfo.esl = executorInfoHal.esl;
36 executorInfo.executorType = executorInfoHal.executorType;
37 if (memcpy_s(executorInfo.publicKey, PUBLIC_KEY_LEN, executorInfoHal.pubKey, PUBLIC_KEY_LEN) != EOK) {
38 LOG_ERROR("memcpy failed");
39 }
40 return executorInfo;
41 }
42
CopyExecutorInfoIn(const ExecutorInfo & executorInfo)43 static ExecutorInfoHal CopyExecutorInfoIn(const ExecutorInfo &executorInfo)
44 {
45 ExecutorInfoHal executorInfoHal;
46 executorInfoHal.authType = executorInfo.authType;
47 executorInfoHal.authAbility = executorInfo.authAbility;
48 executorInfoHal.esl = executorInfo.esl;
49 executorInfoHal.executorType = executorInfo.executorType;
50 if (memcpy_s(executorInfoHal.pubKey, PUBLIC_KEY_LEN, executorInfo.publicKey, PUBLIC_KEY_LEN) != EOK) {
51 LOG_ERROR("memcpy failed");
52 }
53 return executorInfoHal;
54 }
55
CopyScheduleInfoOut(ScheduleInfo & scheduleInfo,const ScheduleInfoHal & scheduleInfoHal)56 static void CopyScheduleInfoOut(ScheduleInfo &scheduleInfo, const ScheduleInfoHal &scheduleInfoHal)
57 {
58 LOG_INFO("start");
59 scheduleInfo.executors.clear();
60 scheduleInfo.authSubType = scheduleInfoHal.authSubType;
61 scheduleInfo.templateId = scheduleInfoHal.templateId;
62 scheduleInfo.scheduleMode = scheduleInfoHal.scheduleMode;
63 for (uint32_t i = 0; i < scheduleInfoHal.executorInfoNum; i++) {
64 ExecutorInfo executorInfo = CopyExecutorInfoOut(scheduleInfoHal.executorInfos[i]);
65 scheduleInfo.executors.push_back(executorInfo);
66 }
67 }
68
GetScheduleInfo(uint64_t scheduleId,ScheduleInfo & scheduleInfo)69 int32_t GetScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo)
70 {
71 LOG_INFO("start");
72 GlobalLock();
73 ScheduleInfoHal scheduleInfoHal;
74 int32_t ret = GetScheduleInfo(scheduleId, &scheduleInfoHal);
75 if (ret != RESULT_SUCCESS) {
76 LOG_ERROR("get schedule info failed");
77 GlobalUnLock();
78 return ret;
79 }
80 CopyScheduleInfoOut(scheduleInfo, scheduleInfoHal);
81 GlobalUnLock();
82 return RESULT_SUCCESS;
83 }
84
DeleteScheduleInfo(uint64_t scheduleId,ScheduleInfo & scheduleInfo)85 int32_t DeleteScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo)
86 {
87 LOG_INFO("start");
88 GlobalLock();
89 ScheduleInfoHal scheduleInfoHal;
90 int32_t ret = GetScheduleInfo(scheduleId, &scheduleInfoHal);
91 if (ret != RESULT_SUCCESS) {
92 LOG_ERROR("get schedule info failed");
93 (void)RemoveCoAuthSchedule(scheduleId);
94 GlobalUnLock();
95 return ret;
96 }
97 CopyScheduleInfoOut(scheduleInfo, scheduleInfoHal);
98 (void)RemoveCoAuthSchedule(scheduleId);
99 GlobalUnLock();
100 return RESULT_SUCCESS;
101 }
102
CreateBufferByVector(std::vector<uint8_t> & executorFinishMsg)103 static Buffer *CreateBufferByVector(std::vector<uint8_t> &executorFinishMsg)
104 {
105 LOG_INFO("executorFinishMsg size is %{public}zu", executorFinishMsg.size());
106 Buffer *data = CreateBufferByData(&executorFinishMsg[0], executorFinishMsg.size());
107 return data;
108 }
109
GetScheduleToken(std::vector<uint8_t> executorFinishMsg,ScheduleToken & scheduleToken)110 int32_t GetScheduleToken(std::vector<uint8_t> executorFinishMsg, ScheduleToken &scheduleToken)
111 {
112 LOG_INFO("start");
113 if (executorFinishMsg.empty()) {
114 LOG_ERROR("executorFinishMsg is empty");
115 ScheduleInfo scheduleInfo;
116 return DeleteScheduleInfo(scheduleToken.scheduleId, scheduleInfo);
117 }
118 GlobalLock();
119 Buffer *executorMsg = CreateBufferByVector(executorFinishMsg);
120 if (executorMsg == nullptr) {
121 LOG_ERROR("create msg failed");
122 GlobalUnLock();
123 return RESULT_NO_MEMORY;
124 }
125 ScheduleTokenHal scheduleTokenHal = {};
126 scheduleTokenHal.scheduleId = scheduleToken.scheduleId;
127 int32_t ret = ScheduleFinish(executorMsg, &scheduleTokenHal);
128 if (ret != RESULT_SUCCESS) {
129 DestoryBuffer(executorMsg);
130 GlobalUnLock();
131 return ret;
132 }
133 if (memcpy_s(&scheduleToken, sizeof(ScheduleToken), &scheduleTokenHal, sizeof(ScheduleTokenHal)) != EOK) {
134 LOG_ERROR("copy scheduleToken failed");
135 DestoryBuffer(executorMsg);
136 GlobalUnLock();
137 return RESULT_BAD_COPY;
138 }
139 DestoryBuffer(executorMsg);
140 GlobalUnLock();
141 return RESULT_SUCCESS;
142 }
143
ExecutorRegister(ExecutorInfo executorInfo,uint64_t & executorId)144 int32_t ExecutorRegister(ExecutorInfo executorInfo, uint64_t &executorId)
145 {
146 LOG_INFO("start");
147 GlobalLock();
148 ExecutorInfoHal executorInfoHal = CopyExecutorInfoIn(executorInfo);
149 int32_t ret = RegisterExecutor(&executorInfoHal, &executorId);
150 GlobalUnLock();
151 return ret;
152 }
153
ExecutorUnRegister(uint64_t executorId)154 int32_t ExecutorUnRegister(uint64_t executorId)
155 {
156 LOG_INFO("start");
157 GlobalLock();
158 int32_t ret = UnRegisterExecutor(executorId);
159 GlobalUnLock();
160 return ret;
161 }
162
IsExecutorExist(uint32_t authType)163 bool IsExecutorExist(uint32_t authType)
164 {
165 LOG_INFO("start");
166 GlobalLock();
167 bool ret = IsExecutorExistFunc(authType);
168 GlobalUnLock();
169 return ret;
170 }
171 } // CoAuth
172 } // UserIAM
173 } // OHOS