• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "auth_message_processor.h"
17 
18 #include "dm_auth_manager.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr)24 AuthMessageProcessor::AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr) : authMgr_(authMgr)
25 {
26     LOGI("AuthMessageProcessor constructor");
27 }
28 
~AuthMessageProcessor()29 AuthMessageProcessor::~AuthMessageProcessor()
30 {
31     authMgr_.reset();
32 }
33 
CreateAuthRequestMessage()34 std::vector<std::string> AuthMessageProcessor::CreateAuthRequestMessage()
35 {
36     LOGI("AuthMessageProcessor::CreateAuthRequestMessage start.");
37     std::vector<std::string> jsonStrVec;
38     int32_t thumbnailSize = (int32_t)(authRequestContext_->appThumbnail.size());
39     int32_t thumbnailSlice = ((thumbnailSize / MSG_MAX_SIZE) + (thumbnailSize % MSG_MAX_SIZE) == 0 ? 0 : 1);
40     nlohmann::json jsonObj;
41     jsonObj[TAG_VER] = DM_ITF_VER;
42     jsonObj[TAG_TYPE] = MSG_TYPE_REQ_AUTH;
43     jsonObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
44     jsonObj[TAG_INDEX] = 0;
45     jsonObj[TAG_REQUESTER] = authRequestContext_->deviceName;
46     jsonObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
47     jsonObj[TAG_DEVICE_TYPE] = authRequestContext_->deviceTypeId;
48     jsonObj[TAG_AUTH_TYPE] = authRequestContext_->authType;
49     jsonObj[TAG_TOKEN] = authRequestContext_->token;
50     jsonObj[TAG_VISIBILITY] = authRequestContext_->groupVisibility;
51     if (authRequestContext_->groupVisibility == GROUP_VISIBILITY_IS_PRIVATE) {
52         jsonObj[TAG_TARGET] = authRequestContext_->targetPkgName;
53         jsonObj[TAG_HOST] = authRequestContext_->hostPkgName;
54     }
55     jsonObj[TAG_APP_NAME] = authRequestContext_->appName;
56     jsonObj[TAG_APP_DESCRIPTION] = authRequestContext_->appDesc;
57     jsonObj[TAG_APP_ICON] = authRequestContext_->appIcon;
58     jsonObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
59     jsonStrVec.push_back(jsonObj.dump());
60 
61     for (int32_t idx = 0; idx < thumbnailSlice; idx++) {
62         nlohmann::json jsonThumbnailObj;
63         jsonThumbnailObj[TAG_VER] = DM_ITF_VER;
64         jsonThumbnailObj[TAG_TYPE] = MSG_TYPE_REQ_AUTH;
65         jsonThumbnailObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
66         jsonThumbnailObj[TAG_INDEX] = idx + 1;
67         jsonThumbnailObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
68         jsonThumbnailObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
69 
70         int32_t leftLen = thumbnailSize - idx * MSG_MAX_SIZE;
71         int32_t sliceLen = (leftLen > MSG_MAX_SIZE) ? MSG_MAX_SIZE : leftLen;
72         LOGI("TAG_APP_THUMBNAIL encode, idx %d, sliceLen %d, thumbnailSize %d", idx, (uint32_t)sliceLen, thumbnailSize);
73         jsonObj[TAG_APP_THUMBNAIL] = authRequestContext_->appThumbnail.substr(idx * MSG_MAX_SIZE, sliceLen);
74         jsonStrVec.push_back(jsonThumbnailObj.dump());
75     }
76     return jsonStrVec;
77 }
78 
CreateSimpleMessage(int32_t msgType)79 std::string AuthMessageProcessor::CreateSimpleMessage(int32_t msgType)
80 {
81     LOGI("AuthMessageProcessor::CreateSimpleMessage start. msgType is %d", msgType);
82     nlohmann::json jsonObj;
83     jsonObj[TAG_VER] = DM_ITF_VER;
84     jsonObj[TAG_TYPE] = msgType;
85     switch (msgType) {
86         case MSG_TYPE_NEGOTIATE:
87         case MSG_TYPE_RESP_NEGOTIATE:
88             CreateNegotiateMessage(jsonObj);
89             break;
90         case MSG_TYPE_SYNC_GROUP:
91             CreateSyncGroupMessage(jsonObj);
92             break;
93         case MSG_TYPE_RESP_AUTH:
94             CreateResponseAuthMessage(jsonObj);
95             break;
96         case MSG_TYPE_REQ_AUTH_TERMINATE:
97             CreateResponseFinishMessage(jsonObj);
98             break;
99         default:
100             break;
101     }
102     return jsonObj.dump();
103 }
104 
CreateNegotiateMessage(nlohmann::json & json)105 void AuthMessageProcessor::CreateNegotiateMessage(nlohmann::json &json)
106 {
107     if (cryptoAdapter_ == nullptr) {
108         json[TAG_CRYPTO_SUPPORT] = false;
109     } else {
110         json[TAG_CRYPTO_SUPPORT] = true;
111         json[TAG_CRYPTO_NAME] = cryptoAdapter_->GetName();
112         json[TAG_CRYPTO_VERSION] = cryptoAdapter_->GetVersion();
113         json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
114     }
115     json[TAG_AUTH_TYPE] = authResponseContext_->authType;
116     json[TAG_REPLY] = authResponseContext_->reply;
117     json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
118 }
119 
CreateSyncGroupMessage(nlohmann::json & json)120 void AuthMessageProcessor::CreateSyncGroupMessage(nlohmann::json &json)
121 {
122     json[TAG_DEVICE_ID] = authRequestContext_->deviceId;
123     json[TAG_GROUPIDS] = authRequestContext_->syncGroupList;
124 }
125 
CreateResponseAuthMessage(nlohmann::json & json)126 void AuthMessageProcessor::CreateResponseAuthMessage(nlohmann::json &json)
127 {
128     json[TAG_REPLY] = authResponseContext_->reply;
129     json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
130     json[TAG_TOKEN] = authResponseContext_->token;
131     LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str());
132     if (authResponseContext_->reply == 0) {
133         std::string groupId = authResponseContext_->groupId;
134         LOGI("AuthMessageProcessor::CreateSimpleMessage groupId %d", groupId.c_str());
135         nlohmann::json jsonObject = nlohmann::json::parse(groupId, nullptr, false);
136         if (jsonObject.is_discarded()) {
137             LOGE("DecodeRequestAuth jsonStr error");
138             return;
139         }
140         groupId = jsonObject[TAG_GROUP_ID];
141         json[TAG_NET_ID] = authResponseContext_->networkId;
142         json[TAG_REQUEST_ID] = authResponseContext_->requestId;
143         json[TAG_GROUP_ID] = groupId;
144         json[TAG_GROUP_NAME] = authResponseContext_->groupName;
145         json[TAG_AUTH_TOKEN] = authResponseContext_->authToken;
146         LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s,%s", groupId.c_str(),
147              authResponseContext_->groupName.c_str());
148     }
149 }
150 
CreateResponseFinishMessage(nlohmann::json & json)151 void AuthMessageProcessor::CreateResponseFinishMessage(nlohmann::json &json)
152 {
153     json[TAG_REPLY] = authResponseContext_->reply;
154 }
155 
ParseMessage(const std::string & message)156 int32_t AuthMessageProcessor::ParseMessage(const std::string &message)
157 {
158     LOGE(" AuthMessageProcessor ParseMessage");
159     nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
160     if (jsonObject.is_discarded()) {
161         LOGE("DecodeRequestAuth jsonStr error");
162         return DM_FAILED;
163     }
164     if (!jsonObject.contains(TAG_TYPE)) {
165         LOGE("err json string, first time");
166         return DM_FAILED;
167     }
168     int32_t sliceNum = 0;
169     int32_t msgType = jsonObject[TAG_TYPE];
170     authResponseContext_->msgType = msgType;
171     LOGI("AuthMessageProcessor::ParseAuthRequestMessage======== %d", authResponseContext_->msgType);
172     switch (msgType) {
173         case MSG_TYPE_NEGOTIATE:
174             ParseNegotiateMessage(jsonObject);
175             break;
176         case MSG_TYPE_REQ_AUTH:
177             if (!jsonObject.contains(TAG_INDEX) || !jsonObject.contains(TAG_DEVICE_ID) ||
178                 !jsonObject.contains(TAG_SLICE_NUM)) {
179                 LOGE("err json string, first time");
180                 return DM_FAILED;
181             }
182             authResponseContext_->deviceId = jsonObject[TAG_DEVICE_ID];
183             authResponseContext_->authType = jsonObject[TAG_AUTH_TYPE];
184             authResponseContext_->appDesc = jsonObject[TAG_APP_DESCRIPTION];
185             authResponseContext_->token = jsonObject[TAG_TOKEN];
186             authResponseContext_->targetPkgName = jsonObject[TAG_TARGET];
187             authResponseContext_->appName = jsonObject[TAG_APP_NAME];
188             LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str());
189             sliceNum = jsonObject[TAG_SLICE_NUM];
190             if ((int32_t)authSplitJsonList_.size() < sliceNum) {
191                 authSplitJsonList_.push_back(message);
192             } else {
193                 ParseAuthRequestMessage();
194             }
195             break;
196         case MSG_TYPE_RESP_AUTH:
197             ParseAuthResponseMessage(jsonObject);
198             break;
199         case MSG_TYPE_REQ_AUTH_TERMINATE:
200             ParseResponseFinishMessage(jsonObject);
201             break;
202         default:
203             break;
204     }
205     return DM_OK;
206 }
207 
ParseResponseFinishMessage(nlohmann::json & json)208 void AuthMessageProcessor::ParseResponseFinishMessage(nlohmann::json &json)
209 {
210     authResponseContext_->reply = json[TAG_REPLY];
211 }
212 
ParseAuthResponseMessage(nlohmann::json & json)213 void AuthMessageProcessor::ParseAuthResponseMessage(nlohmann::json &json)
214 {
215     LOGI("AuthMessageProcessor::ParseAuthResponseMessage ");
216     authResponseContext_->reply = json[TAG_REPLY];
217     authResponseContext_->deviceId = json[TAG_DEVICE_ID];
218     authResponseContext_->token = json[TAG_TOKEN];
219     if (authResponseContext_->reply == 0) {
220         authResponseContext_->networkId = json[TAG_NET_ID];
221         authResponseContext_->requestId = json[TAG_REQUEST_ID];
222         authResponseContext_->groupId = json[TAG_GROUP_ID];
223         authResponseContext_->groupName = json[TAG_GROUP_NAME];
224         authResponseContext_->authToken = json[TAG_AUTH_TOKEN];
225         LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s,%s", authResponseContext_->groupId.c_str(),
226              authResponseContext_->groupName.c_str());
227     }
228     LOGI("AuthMessageProcessor::ParseAuthResponseMessage ");
229 }
230 
ParseAuthRequestMessage()231 int32_t AuthMessageProcessor::ParseAuthRequestMessage()
232 {
233     nlohmann::json jsonObject = authSplitJsonList_.front();
234     authResponseContext_->deviceId = jsonObject[TAG_DEVICE_ID];
235     authResponseContext_->reply = jsonObject[TAG_REPLY];
236     authResponseContext_->authType = jsonObject[TAG_AUTH_TYPE];
237     LOGI("AuthMessageProcessor::ParseAuthResponseMessage %d,%d", authResponseContext_->reply);
238     LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str());
239     if (authResponseContext_->reply == AUTH_REPLY_ACCEPT) {
240         authResponseContext_->networkId = jsonObject[TAG_NET_ID];
241         authResponseContext_->groupId = jsonObject[TAG_GROUP_ID];
242         authResponseContext_->groupName = jsonObject[TAG_GROUP_NAME];
243         authResponseContext_->requestId = jsonObject[TAG_REQUEST_ID];
244         return DM_FAILED;
245     }
246     authSplitJsonList_.clear();
247     return DM_OK;
248 }
249 
ParseNegotiateMessage(const nlohmann::json & json)250 void AuthMessageProcessor::ParseNegotiateMessage(const nlohmann::json &json)
251 {
252     if (json.contains(TAG_CRYPTO_SUPPORT)) {
253         authResponseContext_->cryptoSupport = json[TAG_CRYPTO_SUPPORT];
254     }
255     if (json.contains(TAG_CRYPTO_NAME)) {
256         authResponseContext_->cryptoSupport = json[TAG_CRYPTO_NAME];
257     }
258     if (json.contains(TAG_CRYPTO_VERSION)) {
259         authResponseContext_->cryptoSupport = json[TAG_CRYPTO_VERSION];
260     }
261     if (json.contains(TAG_DEVICE_ID)) {
262         authResponseContext_->deviceId = json[TAG_DEVICE_ID];
263     }
264     authResponseContext_->authType = json[TAG_AUTH_TYPE];
265     authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID];
266     authResponseContext_->reply = json[TAG_REPLY];
267 }
268 
SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)269 void AuthMessageProcessor::SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)
270 {
271     authRequestContext_ = authRequestContext;
272 }
273 
SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)274 void AuthMessageProcessor::SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)
275 {
276     authResponseContext_ = authResponseContext;
277 }
278 
GetResponseContext()279 std::shared_ptr<DmAuthResponseContext> AuthMessageProcessor::GetResponseContext()
280 {
281     return authResponseContext_;
282 }
283 
GetRequestContext()284 std::shared_ptr<DmAuthRequestContext> AuthMessageProcessor::GetRequestContext()
285 {
286     return authRequestContext_;
287 }
288 } // namespace DistributedHardware
289 } // namespace OHOS
290