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 "hks_useridm_api_wrap.h"
17 #include "user_idm_client.h"
18 
19 #include "hks_log.h"
20 #include "hks_mem.h"
21 #include "hks_template.h"
22 
23 #define USER_IAM OHOS::UserIam::UserAuth
24 
25 static constexpr const uint32_t g_paramSidMax = 2;
26 
27 static const struct SecInfoWrap SecInfoParams[g_paramSidMax] = {
28     {
29         .secureUid = 1,
30         .enrolledInfoLen = 3
31     }, {
32         .secureUid = 2,
33         .enrolledInfoLen = 1
34     }
35 };
36 
ConvertToHksAuthType(enum USER_IAM::AuthType authType,enum HksUserAuthType * hksAuthType)37 static int32_t ConvertToHksAuthType(enum USER_IAM::AuthType authType, enum HksUserAuthType *hksAuthType)
38 {
39     switch (authType) {
40         case USER_IAM::AuthType::FACE:
41             *hksAuthType = HKS_USER_AUTH_TYPE_FACE;
42             break;
43         case USER_IAM::AuthType::FINGERPRINT:
44             *hksAuthType =  HKS_USER_AUTH_TYPE_FINGERPRINT;
45             break;
46         case USER_IAM::AuthType::PIN:
47             *hksAuthType =  HKS_USER_AUTH_TYPE_PIN;
48             break;
49         default:
50             HKS_LOG_E("Invalid authType!");
51             return HKS_FAILURE;
52     }
53     return HKS_SUCCESS;
54 }
55 
56 static struct EnrolledInfoWrap EnrolledInfoParams[g_paramSidMax][3] = {
57     {
58         {
59             .authType = HKS_USER_AUTH_TYPE_PIN,
60             .enrolledId = 1
61         }, {
62             .authType = HKS_USER_AUTH_TYPE_FINGERPRINT,
63             .enrolledId = 2
64         }, {
65             .authType = HKS_USER_AUTH_TYPE_FACE,
66             .enrolledId = 3
67         }
68     }, {
69         {
70             .authType = HKS_USER_AUTH_TYPE_PIN,
71             .enrolledId = 0
72         }
73     }
74 };
75 
76 static int32_t g_ParamsId = 0;
77 
HksUserIdmGetSecInfo(int32_t userId,struct SecInfoWrap ** outSecInfo)78 int32_t HksUserIdmGetSecInfo(int32_t userId, struct SecInfoWrap **outSecInfo)
79 {
80     (void)userId;
81     if (outSecInfo == nullptr)
82         return HKS_ERROR_INVALID_ARGUMENT;
83     *outSecInfo = static_cast<struct SecInfoWrap *>(HksMalloc(sizeof(struct SecInfoWrap)));
84     HKS_IF_NULL_RETURN(*outSecInfo, HKS_ERROR_MALLOC_FAIL)
85     (*outSecInfo)->enrolledInfo = static_cast<struct EnrolledInfoWrap *>(
86         HksMalloc(sizeof(struct EnrolledInfoWrap) * SecInfoParams[g_ParamsId].enrolledInfoLen));
87     if ((*outSecInfo)->enrolledInfo == NULL) {
88         HKS_FREE(*outSecInfo);
89         return HKS_ERROR_MALLOC_FAIL;
90     }
91     (*outSecInfo)->secureUid = SecInfoParams[g_ParamsId].secureUid;
92     (*outSecInfo)->enrolledInfoLen = SecInfoParams[g_ParamsId].enrolledInfoLen;
93     for (uint32_t i = 0; i < SecInfoParams[g_ParamsId].enrolledInfoLen; i++) {
94         (*outSecInfo)->enrolledInfo[i] = EnrolledInfoParams[g_ParamsId][i];
95     }
96     return HKS_SUCCESS;
97 }
98 
HksUserIdmGetAuthInfoNum(int32_t userId,enum HksUserAuthType hksAuthType,uint32_t * numOfAuthInfo)99 int32_t HksUserIdmGetAuthInfoNum(int32_t userId, enum HksUserAuthType hksAuthType, uint32_t *numOfAuthInfo)
100 {
101     *numOfAuthInfo = 1;
102     return HKS_SUCCESS;
103 }
104 
HksConvertUserIamTypeToHksType(enum HksUserIamType userIamType,uint32_t userIamValue,uint32_t * hksValue)105 int32_t HksConvertUserIamTypeToHksType(enum HksUserIamType userIamType, uint32_t userIamValue, uint32_t *hksValue)
106 {
107     HKS_IF_NULL_RETURN(hksValue, HKS_ERROR_NULL_POINTER)
108 
109     switch (userIamType) {
110         case HKS_AUTH_TYPE:
111             return ConvertToHksAuthType((enum USER_IAM::AuthType)userIamValue, (enum HksUserAuthType *)hksValue);
112         default:
113             break;
114     }
115     return HKS_ERROR_NOT_SUPPORTED;
116 }
117