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 (AddIntToJson(dataFromClient, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
77 LOGE("Failed to add os accountId!");
78 return HC_ERR_JSON_ADD;
79 }
80 int32_t authForm = AUTH_FORM_INVALID_TYPE;
81 if (GetIntFromJson(dataFromClient, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
82 LOGE("Failed to get auth form!");
83 return HC_ERR_JSON_GET;
84 }
85 int32_t groupAuthType = GetAuthType(authForm);
86 BaseGroupAuth *groupAuthHandle = GetGroupAuth(groupAuthType);
87 if (groupAuthHandle == NULL) {
88 LOGE("Failed to get group auth handle!");
89 return HC_ERR_NOT_SUPPORT;
90 }
91 int32_t res = groupAuthHandle->combineServerConfirmParams(confirmationJson, dataFromClient);
92 if (res != HC_SUCCESS) {
93 LOGE("Failed to combine server confirm params!");
94 }
95 return res;
96 }
97
GetAuthType(int32_t authForm)98 int32_t GetAuthType(int32_t authForm)
99 {
100 switch (authForm) {
101 case AUTH_FORM_ACCOUNT_UNRELATED:
102 return ACCOUNT_UNRELATED_GROUP_AUTH_TYPE;
103 case AUTH_FORM_IDENTICAL_ACCOUNT:
104 return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
105 case AUTH_FORM_ACROSS_ACCOUNT:
106 return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
107 default:
108 LOGE("Invalid authForm!");
109 return INVALID_GROUP_AUTH_TYPE;
110 }
111 }
112
GetGroupAuth(int32_t groupAuthType)113 BaseGroupAuth *GetGroupAuth(int32_t groupAuthType)
114 {
115 return g_groupAuthFunc(groupAuthType);
116 }
117
RegisterGroupAuth(GetGroupAuthFunc getGroupAuthFunc)118 void RegisterGroupAuth(GetGroupAuthFunc getGroupAuthFunc)
119 {
120 g_groupAuthFunc = getGroupAuthFunc;
121 }