1 /*
2 * Copyright (C) 2021-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 "auth_session_util.h"
17 #include "account_related_group_auth.h"
18 #include "account_unrelated_group_auth.h"
19 #include "alg_loader.h"
20 #include "auth_session_common_defines.h"
21 #include "common_defs.h"
22 #include "device_auth_defines.h"
23 #include "hc_log.h"
24 #include "string_util.h"
25
AuthFormToModuleType(int32_t authForm)26 static int32_t AuthFormToModuleType(int32_t authForm)
27 {
28 int moduleType = INVALID_MODULE_TYPE;
29 if (authForm == AUTH_FORM_ACCOUNT_UNRELATED) {
30 moduleType = DAS_MODULE;
31 } else if ((authForm == AUTH_FORM_IDENTICAL_ACCOUNT) || (authForm == AUTH_FORM_ACROSS_ACCOUNT)) {
32 moduleType = ACCOUNT_MODULE;
33 } else {
34 LOGE("Invalid auth form!");
35 }
36 return moduleType;
37 }
38
GetGroupAuth(int32_t groupAuthType)39 BaseGroupAuth *GetGroupAuth(int32_t groupAuthType)
40 {
41 switch (groupAuthType) {
42 case ACCOUNT_UNRELATED_GROUP_AUTH_TYPE:
43 LOGI("Non-account auth type.");
44 return GetAccountUnrelatedGroupAuth();
45 case ACCOUNT_RELATED_GROUP_AUTH_TYPE:
46 LOGI("Account-related auth type.");
47 return GetAccountRelatedGroupAuth();
48 default:
49 LOGE("Invalid auth type!");
50 }
51 return NULL;
52 }
53
GetAuthModuleType(const CJson * in)54 int32_t GetAuthModuleType(const CJson *in)
55 {
56 int32_t authForm = AUTH_FORM_INVALID_TYPE;
57 if (GetIntFromJson(in, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
58 LOGE("Failed to get auth form!");
59 return INVALID_MODULE_TYPE;
60 }
61 return AuthFormToModuleType(authForm);
62 }
63
GetInfoHash(const uint8_t * info,uint32_t infoLen,char * str,uint32_t strLen)64 int32_t GetInfoHash(const uint8_t *info, uint32_t infoLen, char *str, uint32_t strLen)
65 {
66 Uint8Buff infoHash = {NULL, SHA256_LEN};
67 Uint8Buff message = {NULL, infoLen};
68 infoHash.val = (uint8_t *)HcMalloc(SHA256_LEN, 0);
69 if (infoHash.val == NULL) {
70 LOGE("Failed to malloc for info hash!");
71 return HC_ERR_ALLOC_MEMORY;
72 }
73 message.val = (uint8_t *)HcMalloc(infoLen, 0);
74 if (message.val == NULL) {
75 LOGE("Failed to malloc for message hash!");
76 HcFree(infoHash.val);
77 infoHash.val = NULL;
78 return HC_ERR_ALLOC_MEMORY;
79 }
80 int32_t res;
81 do {
82 if (memcpy_s(message.val, message.length, info, infoLen) != EOK) {
83 LOGE("Failed to copy data!");
84 res = HC_ERR_ALLOC_MEMORY;
85 break;
86 }
87 res = GetLoaderInstance()->sha256(&message, &infoHash);
88 if (res != HC_SUCCESS) {
89 LOGE("Failed to sha256 message!");
90 res = HC_ERROR;
91 break;
92 }
93 if (ByteToHexString(infoHash.val, infoHash.length, str, strLen) != HC_SUCCESS) {
94 LOGE("Failed to convert byte to string!");
95 res = HC_ERROR;
96 break;
97 }
98 } while (0);
99 HcFree(message.val);
100 message.val = NULL;
101 HcFree(infoHash.val);
102 infoHash.val = NULL;
103 return res;
104 }
105
GroupTypeToAuthForm(int32_t groupType)106 int32_t GroupTypeToAuthForm(int32_t groupType)
107 {
108 int32_t authForm;
109 switch (groupType) {
110 case PEER_TO_PEER_GROUP:
111 authForm = AUTH_FORM_ACCOUNT_UNRELATED;
112 break;
113 case COMPATIBLE_GROUP:
114 authForm = AUTH_FORM_ACCOUNT_UNRELATED;
115 break;
116 case IDENTICAL_ACCOUNT_GROUP:
117 authForm = AUTH_FORM_IDENTICAL_ACCOUNT;
118 break;
119 case ACROSS_ACCOUNT_AUTHORIZE_GROUP:
120 authForm = AUTH_FORM_ACROSS_ACCOUNT;
121 break;
122 default:
123 LOGE("Invalid group type!");
124 authForm = AUTH_FORM_INVALID_TYPE;
125 break;
126 }
127 return authForm;
128 }
129
AuthFormToGroupType(int32_t authForm)130 int32_t AuthFormToGroupType(int32_t authForm)
131 {
132 int32_t groupType;
133 switch (authForm) {
134 case AUTH_FORM_ACCOUNT_UNRELATED:
135 groupType = PEER_TO_PEER_GROUP;
136 break;
137 case AUTH_FORM_IDENTICAL_ACCOUNT:
138 groupType = IDENTICAL_ACCOUNT_GROUP;
139 break;
140 case AUTH_FORM_ACROSS_ACCOUNT:
141 groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
142 break;
143 default:
144 LOGE("Invalid auth form!");
145 groupType = GROUP_TYPE_INVALID;
146 break;
147 }
148 return groupType;
149 }