• 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 #include "auth_widget_helper.h"
17 
18 #include "iam_logger.h"
19 #include "resource_node_pool.h"
20 #include "user_idm_database.h"
21 #include "widget_client.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 
InitWidgetContextParam(int32_t userId,const AuthParam & authParam,const WidgetParam & widgetParam,ContextFactory::AuthWidgetContextPara & para)29 bool AuthWidgetHelper::InitWidgetContextParam(int32_t userId, const AuthParam &authParam,
30     const WidgetParam &widgetParam, ContextFactory::AuthWidgetContextPara &para)
31 {
32     for (auto &authType : authParam.authType) {
33         ContextFactory::AuthWidgetContextPara::AuthProfile profile;
34         if (!GetUserAuthProfile(userId, authType, profile, para.callingUid)) {
35             IAM_LOGE("get user auth profile failed");
36             return false;
37         }
38         para.authProfileMap[authType] = profile;
39         if (authType == AuthType::PIN) {
40             WidgetClient::Instance().SetPinSubType(static_cast<PinSubType>(profile.pinSubType));
41         } else if (authType == AuthType::FINGERPRINT) {
42             WidgetClient::Instance().SetSensorInfo(profile.sensorInfo);
43         }
44     }
45     para.challenge = std::move(authParam.challenge);
46     para.authTypeList = authParam.authType;
47     para.atl = authParam.authTrustLevel;
48     para.widgetParam = widgetParam;
49     if (widgetParam.windowMode == WindowModeType::UNKNOWN_WINDOW_MODE) {
50         para.widgetParam.windowMode = WindowModeType::DIALOG_BOX;
51     }
52     return true;
53 }
54 
GetUserAuthProfile(int32_t userId,const AuthType & authType,ContextFactory::AuthWidgetContextPara::AuthProfile & profile,uint32_t callingUid)55 bool AuthWidgetHelper::GetUserAuthProfile(int32_t userId, const AuthType &authType,
56     ContextFactory::AuthWidgetContextPara::AuthProfile &profile, uint32_t callingUid)
57 {
58     Attributes values;
59     auto credentialInfos = UserIdmDatabase::Instance().GetCredentialInfo(userId, authType);
60     if (credentialInfos.empty() || credentialInfos[0] == nullptr) {
61         IAM_LOGE("user %{public}d has no credential type %{public}d", userId, authType);
62         return false;
63     }
64     uint64_t executorIndex = credentialInfos[0]->GetExecutorIndex();
65     auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
66     if (resourceNode == nullptr) {
67         IAM_LOGE("resourceNode is nullptr");
68         return false;
69     }
70 
71     std::vector<uint64_t> templateIds;
72     templateIds.reserve(credentialInfos.size());
73     for (auto &info : credentialInfos) {
74         templateIds.push_back(info->GetTemplateId());
75     }
76     std::vector<uint32_t> uint32Keys = {
77         Attributes::ATTR_SENSOR_INFO,
78         Attributes::ATTR_REMAIN_TIMES,
79         Attributes::ATTR_FREEZING_TIME
80     };
81     if (authType == AuthType::PIN) {
82         uint32Keys.push_back(Attributes::ATTR_PIN_SUB_TYPE);
83     }
84 
85     Attributes attr;
86     attr.SetInt32Value(Attributes::ATTR_AUTH_TYPE, authType);
87     attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_GET);
88     attr.SetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds);
89     attr.SetUint64Value(Attributes::ATTR_CALLER_UID, static_cast<uint64_t>(callingUid));
90     attr.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, uint32Keys);
91     int32_t result = resourceNode->GetProperty(attr, values);
92     if (result != SUCCESS) {
93         IAM_LOGE("failed to get property, result = %{public}d", result);
94         return false;
95     }
96     return ParseAttributes(values, authType, profile);
97 }
98 
ParseAttributes(const Attributes & values,const AuthType & authType,ContextFactory::AuthWidgetContextPara::AuthProfile & profile)99 bool AuthWidgetHelper::ParseAttributes(const Attributes &values, const AuthType &authType,
100     ContextFactory::AuthWidgetContextPara::AuthProfile &profile)
101 {
102     if (authType == AuthType::PIN) {
103         if (!values.GetInt32Value(Attributes::ATTR_PIN_SUB_TYPE, profile.pinSubType)) {
104             IAM_LOGE("get ATTR_PIN_SUB_TYPE failed");
105             return false;
106         }
107     }
108     if (!values.GetStringValue(Attributes::ATTR_SENSOR_INFO, profile.sensorInfo)) {
109         IAM_LOGE("get ATTR_SENSOR_INFO failed");
110         return false;
111     }
112     if (!values.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, profile.remainTimes)) {
113         IAM_LOGE("get ATTR_REMAIN_TIMES failed");
114         return false;
115     }
116     if (!values.GetInt32Value(Attributes::ATTR_FREEZING_TIME, profile.freezingTime)) {
117         IAM_LOGE("get ATTR_FREEZING_TIME failed");
118         return false;
119     }
120     return true;
121 }
122 
CheckValidSolution(int32_t userId,const std::vector<AuthType> & authTypeList,const AuthTrustLevel & atl)123 int32_t AuthWidgetHelper::CheckValidSolution(int32_t userId,
124     const std::vector<AuthType> &authTypeList, const AuthTrustLevel &atl)
125 {
126     auto hdi = HdiWrapper::GetHdiInstance();
127     IAM_LOGE("hdi interface authTypeList start");
128     if (hdi == nullptr) {
129         IAM_LOGE("hdi interface is nullptr");
130         return GENERAL_ERROR;
131     }
132     std::vector<HdiAuthType> inputAuthType;
133     std::vector<HdiAuthType> validTypes;
134     uint32_t inputAtl = atl;
135     IAM_LOGE("hdi interface authTypeList size is:%{public}zu", authTypeList.size());
136     for (size_t index = 0; index < authTypeList.size(); index++) {
137         inputAuthType.emplace_back(static_cast<HdiAuthType>(authTypeList[index]));
138         IAM_LOGE("hdi interface authTypeList now index is:%{public}zu", index);
139     }
140     int32_t result = hdi->GetValidSolution(userId, inputAuthType, inputAtl, validTypes);
141     if (result != SUCCESS) {
142         IAM_LOGE("failed to get current supported authTrustLevel from hdi result:%{public}d userId:%{public}d",
143             result, userId);
144     }
145     return result;
146 }
147 } // namespace UserAuth
148 } // namespace UserIam
149 } // namespace OHOS