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 "enroll_command.h"
17
18 #include "iam_executor_framework_types.h"
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_mem.h"
22 #include "iam_para2str.h"
23 #include "iam_ptr.h"
24 #include "iam_defines.h"
25 #include "hisysevent_adapter.h"
26
27 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
EnrollCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)32 EnrollCommand::EnrollCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
33 const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
34 : AsyncCommandBase("ENROLL", scheduleId, executor, executorMessenger),
35 attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
36 iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("EnrollCommand"))
37 {
38 }
39
SendRequest()40 ResultCode EnrollCommand::SendRequest()
41 {
42 IAM_LOGI("%{public}s send request start", GetDescription());
43 IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
44
45 auto hdi = GetExecutorHdi();
46 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
47
48 uint32_t tokenId = 0;
49 bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
50 IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
51
52 std::vector<uint8_t> extraInfo;
53 bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
54 IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
55
56 IamHitraceHelper traceHelper("hdi Enroll");
57 ResultCode ret = hdi->Enroll(scheduleId_, (EnrollParam) { tokenId, extraInfo }, shared_from_this());
58 IAM_LOGI("%{public}s enroll result %{public}d", GetDescription(), ret);
59 return ret;
60 }
61
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)62 void EnrollCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
63 {
64 IAM_LOGI("%{public}s on result start", GetDescription());
65 TemplateChangeTrace info = {};
66 info.scheduleId = scheduleId_;
67 info.executorType = GetAuthType();
68 info.changeType = TRACE_ADD_CREDENTIAL;
69 info.reason = "AddTemplate";
70 UserIam::UserAuth::ReportSecurityTemplateChange(info);
71 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
72 auto authAttributes = Common::MakeShared<Attributes>();
73 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
74 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
75 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
76 bool setAuthResultRet =
77 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
78 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
79 iamHitraceHelper_ = nullptr;
80 int32_t ret = MessengerFinish(scheduleId_, ALL_IN_ONE, result, authAttributes);
81 if (ret != USERAUTH_SUCCESS) {
82 IAM_LOGE("%{public}s call finish fail", GetDescription());
83 return;
84 }
85 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
86 }
87
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)88 void EnrollCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
89 {
90 IAM_LOGI("%{public}s on acquire info start", GetDescription());
91
92 Attributes attr;
93 bool setAcquireRet = attr.SetInt32Value(Attributes::ATTR_TIP_INFO, acquire);
94 IF_FALSE_LOGE_AND_RETURN(setAcquireRet);
95 bool setExtraInfoRet = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
96 IF_FALSE_LOGE_AND_RETURN(setExtraInfoRet);
97
98 auto msg = AuthMessage::As(attr.Serialize());
99 IF_FALSE_LOGE_AND_RETURN(msg != nullptr);
100 int32_t ret = MessengerSendData(scheduleId_, transNum_, ALL_IN_ONE, SCHEDULER, msg);
101 ++transNum_;
102 if (ret != USERAUTH_SUCCESS) {
103 IAM_LOGE("%{public}s call SendData fail", GetDescription());
104 return;
105 }
106 IAM_LOGI("%{public}s call SendData success acquire %{public}d", GetDescription(), acquire);
107 }
108 } // namespace UserAuth
109 } // namespace UserIam
110 } // namespace OHOS
111