• 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 "compatible_auth_sub_session_util.h"
17 #include "hc_log.h"
18 #include "hc_types.h"
19 #include "os_account_adapter.h"
20 
21 static GetGroupAuthFunc g_groupAuthFunc;
22 
AuthFormToModuleType(int32_t authForm)23 static int32_t AuthFormToModuleType(int32_t authForm)
24 {
25     int moduleType = INVALID_MODULE_TYPE;
26     if (authForm == AUTH_FORM_ACCOUNT_UNRELATED) {
27         moduleType = DAS_MODULE;
28     } else if ((authForm == AUTH_FORM_IDENTICAL_ACCOUNT) || (authForm == AUTH_FORM_ACROSS_ACCOUNT)) {
29         moduleType = ACCOUNT_MODULE;
30     } else {
31         LOGE("Invalid auth form!");
32     }
33     return moduleType;
34 }
35 
GetAuthModuleType(const CJson * in)36 int32_t GetAuthModuleType(const CJson *in)
37 {
38     int32_t authForm = AUTH_FORM_INVALID_TYPE;
39     if (GetIntFromJson(in, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
40         LOGE("Failed to get auth form!");
41         return INVALID_MODULE_TYPE;
42     }
43     return AuthFormToModuleType(authForm);
44 }
45 
GetDuplicatePkgName(const CJson * params)46 char *GetDuplicatePkgName(const CJson *params)
47 {
48     const char *pkgName = GetStringFromJson(params, FIELD_SERVICE_PKG_NAME);
49     if (pkgName == NULL) {
50         LOGE("Failed to get servicePkgName from json!");
51         return NULL;
52     }
53     uint32_t pkgNameLen = HcStrlen(pkgName) + 1;
54     char *returnPkgName = (char *)HcMalloc(pkgNameLen, 0);
55     if (returnPkgName == NULL) {
56         LOGE("Failed to allocate returnPkgName memory!");
57         return NULL;
58     }
59     if (memcpy_s(returnPkgName, pkgNameLen, pkgName, pkgNameLen) != EOK) {
60         LOGE("Failed to copy pkgName!");
61         HcFree(returnPkgName);
62         return NULL;
63     }
64     return returnPkgName;
65 }
66 
CombineAuthConfirmData(const CJson * confirmationJson,CJson * dataFromClient)67 int32_t CombineAuthConfirmData(const CJson *confirmationJson, CJson *dataFromClient)
68 {
69     int32_t osAccountId = ANY_OS_ACCOUNT;
70     (void)GetIntFromJson(confirmationJson, FIELD_OS_ACCOUNT_ID, &osAccountId);
71     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
72     if (osAccountId == INVALID_OS_ACCOUNT) {
73         LOGE("Invalid os account!");
74         return HC_ERR_INVALID_PARAMS;
75     }
76     if (!CheckIsForegroundOsAccountId(osAccountId)) {
77         LOGE("This access is not from the foreground user, rejected it.");
78         return HC_ERR_CROSS_USER_ACCESS;
79     }
80     if (AddIntToJson(dataFromClient, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
81         LOGE("Failed to add os accountId!");
82         return HC_ERR_JSON_ADD;
83     }
84     int32_t authForm = AUTH_FORM_INVALID_TYPE;
85     if (GetIntFromJson(dataFromClient, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
86         LOGE("Failed to get auth form!");
87         return HC_ERR_JSON_GET;
88     }
89     int32_t groupAuthType = GetAuthType(authForm);
90     BaseGroupAuth *groupAuthHandle = GetGroupAuth(groupAuthType);
91     if (groupAuthHandle == NULL) {
92         LOGE("Failed to get group auth handle!");
93         return HC_ERR_NOT_SUPPORT;
94     }
95     int32_t res = groupAuthHandle->combineServerConfirmParams(confirmationJson, dataFromClient);
96     if (res != HC_SUCCESS) {
97         LOGE("Failed to combine server confirm params!");
98     }
99     return res;
100 }
101 
GetAuthType(int32_t authForm)102 int32_t GetAuthType(int32_t authForm)
103 {
104     switch (authForm) {
105         case AUTH_FORM_ACCOUNT_UNRELATED:
106             return ACCOUNT_UNRELATED_GROUP_AUTH_TYPE;
107         case AUTH_FORM_IDENTICAL_ACCOUNT:
108             return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
109         case AUTH_FORM_ACROSS_ACCOUNT:
110             return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
111         default:
112             LOGE("Invalid authForm!");
113             return INVALID_GROUP_AUTH_TYPE;
114     }
115 }
116 
GetGroupAuth(int32_t groupAuthType)117 BaseGroupAuth *GetGroupAuth(int32_t groupAuthType)
118 {
119     return g_groupAuthFunc(groupAuthType);
120 }
121 
RegisterGroupAuth(GetGroupAuthFunc getGroupAuthFunc)122 void RegisterGroupAuth(GetGroupAuthFunc getGroupAuthFunc)
123 {
124     g_groupAuthFunc = getGroupAuthFunc;
125 }