1 /*
2 * Copyright (C) 2021 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 "group_auth_manager.h"
17 #include "common_defs.h"
18 #include "dev_auth_module_manager.h"
19 #include "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "session_manager.h"
22
GetModuleTypeFromPayload(const CJson * authParams)23 static int32_t GetModuleTypeFromPayload(const CJson *authParams)
24 {
25 int32_t authForm = AUTH_FORM_INVALID_TYPE;
26 if (GetIntFromJson(authParams, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
27 LOGE("[GetModuleTypeFromPayload], Failed to get authForm!");
28 return INVALID_MODULE_TYPE;
29 }
30 if (authForm == AUTH_FORM_ACCOUNT_UNRELATED) {
31 return DAS_MODULE;
32 } else if ((authForm == AUTH_FORM_IDENTICAL_ACCOUNT) || (authForm == AUTH_FORM_ACROSS_ACCOUNT)) {
33 return ACCOUNT_MODULE;
34 }
35 LOGE("Invalid authForm for repeated payload check!");
36 return INVALID_MODULE_TYPE;
37 }
38
GetAuthState(int64_t authReqId,const char * groupId,const char * peerUdid,uint8_t * out,uint32_t * outLen)39 int32_t GetAuthState(int64_t authReqId, const char *groupId, const char *peerUdid, uint8_t *out, uint32_t *outLen)
40 {
41 (void)authReqId;
42 (void)groupId;
43 (void)peerUdid;
44 (void)out;
45 (void)outLen;
46 return HC_ERROR;
47 }
48
InformDeviceDisconnection(const char * udid)49 void InformDeviceDisconnection(const char *udid)
50 {
51 (void)udid;
52 return;
53 }
54
IsTrustedDevice(const char * udid)55 bool IsTrustedDevice(const char *udid)
56 {
57 if (udid == NULL) {
58 LOGE("Invalid params in IsTrustedDevice");
59 return false;
60 }
61 LOGE("Group auth do not support func currently: %s!", __func__);
62 return false;
63 }
64
QueryTrustedDeviceNum(void)65 int32_t QueryTrustedDeviceNum(void)
66 {
67 LOGE("Group auth do not support func currently: %s!", __func__);
68 return HC_ERR_NOT_SUPPORT;
69 }
70
DoAuthDevice(HcTaskBase * task)71 void DoAuthDevice(HcTaskBase *task)
72 {
73 if (task == NULL) {
74 LOGE("The input task is NULL, can't start auth device!");
75 return;
76 }
77 AuthDeviceTask *realTask = (AuthDeviceTask *)task;
78 bool isClient = true;
79 if (GetBoolFromJson(realTask->authParams, FIELD_IS_CLIENT, &isClient)) {
80 LOGE("Failed to get role of client or server when start auth!");
81 return;
82 }
83 if (!isClient) {
84 LOGI("Server invokes authDevice, return directly.");
85 return;
86 }
87 int32_t result = CreateSession(realTask->authReqId, TYPE_CLIENT_AUTH_SESSION, realTask->authParams,
88 realTask->callback);
89 if (result != HC_SUCCESS) {
90 LOGE("Failed to create session for authDevice!");
91 if ((result != HC_ERR_CREATE_SESSION_FAIL) && (realTask->callback != NULL) &&
92 (realTask->callback->onError != NULL)) {
93 LOGE("[DoAuthDevice] Begin invoke onError by group auth manager!");
94 realTask->callback->onError(realTask->authReqId, AUTH_FORM_INVALID_TYPE, result, NULL);
95 LOGE("[DoAuthDevice] End invoke onError by group auth manager!");
96 }
97 }
98 }
99
DoProcessAuthData(HcTaskBase * task)100 void DoProcessAuthData(HcTaskBase *task)
101 {
102 if (task == NULL) {
103 LOGE("The input task is NULL, can't process auth data!");
104 return;
105 }
106 AuthDeviceTask *realTask = (AuthDeviceTask *)task;
107 int32_t res;
108 if (IsRequestExist(realTask->authReqId)) {
109 res = ProcessSession(realTask->authReqId, AUTH_TYPE, realTask->authParams);
110 if (res != HC_SUCCESS) {
111 DestroySession(realTask->authReqId);
112 }
113 return;
114 }
115 res = CheckMsgRepeatability(realTask->authParams, GetModuleTypeFromPayload(realTask->authParams));
116 if (res != HC_SUCCESS) {
117 LOGD("Caller inputs repeated payload, so we will ignore it.");
118 return;
119 }
120 res = CreateSession(realTask->authReqId, TYPE_SERVER_AUTH_SESSION, realTask->authParams,
121 realTask->callback);
122 if (res != HC_SUCCESS) {
123 LOGE("Failed to create session for process auth data!");
124 if ((res != HC_ERR_CREATE_SESSION_FAIL) && (realTask->callback != NULL) &&
125 (realTask->callback->onError != NULL)) {
126 LOGE("Begin invoke onError by group auth manager!");
127 realTask->callback->onError(realTask->authReqId, AUTH_FORM_INVALID_TYPE, res, NULL);
128 LOGE("End invoke onError by group auth manager!");
129 }
130 }
131 }
132