Files
useriam_auth_executor_mgr/common/hal_sdk/coauth_interface.cpp
T
2022-03-19 21:39:45 +08:00

173 lines
5.4 KiB
C++

/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "coauth_interface.h"
#include "securec.h"
extern "C" {
#include "coauth_funcs.h"
#include "defines.h"
#include "adaptor_log.h"
#include "lock.h"
}
namespace OHOS {
namespace UserIAM {
namespace CoAuth {
static ExecutorInfo CopyExecutorInfoOut(const ExecutorInfoHal &executorInfoHal)
{
ExecutorInfo executorInfo;
executorInfo.authType = executorInfoHal.authType;
executorInfo.authAbility = executorInfoHal.authAbility;
executorInfo.esl = executorInfoHal.esl;
executorInfo.executorType = executorInfoHal.executorType;
if (memcpy_s(executorInfo.publicKey, PUBLIC_KEY_LEN, executorInfoHal.pubKey, PUBLIC_KEY_LEN) != EOK) {
LOG_ERROR("memcpy failed");
}
return executorInfo;
}
static ExecutorInfoHal CopyExecutorInfoIn(const ExecutorInfo &executorInfo)
{
ExecutorInfoHal executorInfoHal;
executorInfoHal.authType = executorInfo.authType;
executorInfoHal.authAbility = executorInfo.authAbility;
executorInfoHal.esl = executorInfo.esl;
executorInfoHal.executorType = executorInfo.executorType;
if (memcpy_s(executorInfoHal.pubKey, PUBLIC_KEY_LEN, executorInfo.publicKey, PUBLIC_KEY_LEN) != EOK) {
LOG_ERROR("memcpy failed");
}
return executorInfoHal;
}
static void CopyScheduleInfoOut(ScheduleInfo &scheduleInfo, const ScheduleInfoHal &scheduleInfoHal)
{
LOG_INFO("start");
scheduleInfo.executors.clear();
scheduleInfo.authSubType = scheduleInfoHal.authSubType;
scheduleInfo.templateId = scheduleInfoHal.templateId;
scheduleInfo.scheduleMode = scheduleInfoHal.scheduleMode;
for (uint32_t i = 0; i < scheduleInfoHal.executorInfoNum; i++) {
ExecutorInfo executorInfo = CopyExecutorInfoOut(scheduleInfoHal.executorInfos[i]);
scheduleInfo.executors.push_back(executorInfo);
}
}
int32_t GetScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo)
{
LOG_INFO("start");
GlobalLock();
ScheduleInfoHal scheduleInfoHal;
int32_t ret = GetScheduleInfo(scheduleId, &scheduleInfoHal);
if (ret != RESULT_SUCCESS) {
LOG_ERROR("get schedule info failed");
GlobalUnLock();
return ret;
}
CopyScheduleInfoOut(scheduleInfo, scheduleInfoHal);
GlobalUnLock();
return RESULT_SUCCESS;
}
int32_t DeleteScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo)
{
LOG_INFO("start");
GlobalLock();
ScheduleInfoHal scheduleInfoHal;
int32_t ret = GetScheduleInfo(scheduleId, &scheduleInfoHal);
if (ret != RESULT_SUCCESS) {
LOG_ERROR("get schedule info failed");
(void)RemoveCoAuthSchedule(scheduleId);
GlobalUnLock();
return ret;
}
CopyScheduleInfoOut(scheduleInfo, scheduleInfoHal);
(void)RemoveCoAuthSchedule(scheduleId);
GlobalUnLock();
return RESULT_SUCCESS;
}
static Buffer *CreateBufferByVector(std::vector<uint8_t> &executorFinishMsg)
{
LOG_INFO("executorFinishMsg size is %{public}zu", executorFinishMsg.size());
Buffer *data = CreateBufferByData(&executorFinishMsg[0], executorFinishMsg.size());
return data;
}
int32_t GetScheduleToken(std::vector<uint8_t> executorFinishMsg, ScheduleToken &scheduleToken)
{
LOG_INFO("start");
if (executorFinishMsg.empty()) {
LOG_ERROR("executorFinishMsg is empty");
ScheduleInfo scheduleInfo;
return DeleteScheduleInfo(scheduleToken.scheduleId, scheduleInfo);
}
GlobalLock();
Buffer *executorMsg = CreateBufferByVector(executorFinishMsg);
if (executorMsg == nullptr) {
LOG_ERROR("create msg failed");
GlobalUnLock();
return RESULT_NO_MEMORY;
}
ScheduleTokenHal scheduleTokenHal = {};
scheduleTokenHal.scheduleId = scheduleToken.scheduleId;
int32_t ret = ScheduleFinish(executorMsg, &scheduleTokenHal);
if (ret != RESULT_SUCCESS) {
DestoryBuffer(executorMsg);
GlobalUnLock();
return ret;
}
if (memcpy_s(&scheduleToken, sizeof(ScheduleToken), &scheduleTokenHal, sizeof(ScheduleTokenHal)) != EOK) {
LOG_ERROR("copy scheduleToken failed");
DestoryBuffer(executorMsg);
GlobalUnLock();
return RESULT_BAD_COPY;
}
DestoryBuffer(executorMsg);
GlobalUnLock();
return RESULT_SUCCESS;
}
int32_t ExecutorRegister(ExecutorInfo executorInfo, uint64_t &executorId)
{
LOG_INFO("start");
GlobalLock();
ExecutorInfoHal executorInfoHal = CopyExecutorInfoIn(executorInfo);
int32_t ret = RegisterExecutor(&executorInfoHal, &executorId);
GlobalUnLock();
return ret;
}
int32_t ExecutorUnRegister(uint64_t executorId)
{
LOG_INFO("start");
GlobalLock();
int32_t ret = UnRegisterExecutor(executorId);
GlobalUnLock();
return ret;
}
bool IsExecutorExist(uint32_t authType)
{
LOG_INFO("start");
GlobalLock();
bool ret = IsExecutorExistFunc(authType);
GlobalUnLock();
return ret;
}
} // CoAuth
} // UserIAM
} // OHOS