1 /*
2 * Copyright (c) 2022-2024 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_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 const char* TAG_NET_ID = "NETID";
26 const char* TAG_TARGET = "TARGET";
27 const char* TAG_APP_OPERATION = "APPOPERATION";
28 const char* TAG_APP_NAME = "APPNAME";
29 const char* TAG_APP_DESCRIPTION = "APPDESC";
30 const char* TAG_GROUPIDS = "GROUPIDLIST";
31 const char* TAG_CUSTOM_DESCRIPTION = "CUSTOMDESC";
32 const char* TAG_DEVICE_TYPE = "DEVICETYPE";
33 const char* TAG_REQUESTER = "REQUESTER";
34 const char* TAG_LOCAL_DEVICE_TYPE = "LOCALDEVICETYPE";
35 const char* TAG_INDEX = "INDEX";
36 const char* TAG_SLICE_NUM = "SLICE";
37 const char* TAG_IS_AUTH_CODE_READY = "IS_AUTH_CODE_READY";
38 const char* TAG_IS_SHOW_DIALOG = "IS_SHOW_DIALOG";
39 const char* TAG_TOKEN = "TOKEN";
40 const char* TAG_CRYPTO_NAME = "CRYPTONAME";
41 const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION";
42 const char* QR_CODE_KEY = "qrCode";
43 const char* TAG_AUTH_TOKEN = "authToken";
44 const char* NFC_CODE_KEY = "nfcCode";
45 const char* OLD_VERSION_ACCOUNT = "oldVersionAccount";
46
47 const char* TAG_PUBLICKEY = "publicKey";
48 const char* TAG_SESSIONKEY = "sessionKey";
49 const char* TAG_BIND_TYPE_SIZE = "bindTypeSize";
50 const char* TAG_HOST_PKGNAME = "hostPkgname";
51 const char* TAG_HAVECREDENTIAL = "haveCredential";
52 const char* TAG_CONFIRM_OPERATION = "confirmOperation";
53 const char* TAG_IMPORT_AUTH_CODE = "IMPORT_AUTH_CODE";
54 const char* TAG_CRYPTIC_MSG = "encryptMsg";
55 const char* TAG_SESSIONKEY_ID = "sessionKeyId";
56
57 const int32_t MSG_MAX_SIZE = 45 * 1024;
58 const int32_t GROUP_VISIBILITY_IS_PRIVATE = 0;
59 const int32_t MAX_BINDTYPE_SIZE = 1000;
60 constexpr const char* TAG_VISIBILITY = "VISIBILITY";
61 constexpr const char* TAG_APP_THUMBNAIL = "APPTHUM";
62 constexpr const char* TAG_THUMBNAIL_SIZE = "THUMSIZE";
63
AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr)64 AuthMessageProcessor::AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr) : authMgr_(authMgr)
65 {
66 LOGI("AuthMessageProcessor constructor");
67 cryptoMgr_ = std::make_shared<CryptoMgr>();
68 }
69
~AuthMessageProcessor()70 AuthMessageProcessor::~AuthMessageProcessor()
71 {
72 authMgr_.reset();
73 if (cryptoMgr_ != nullptr) {
74 cryptoMgr_->ClearSessionKey();
75 cryptoMgr_ = nullptr;
76 }
77 }
78
GetJsonObj(JsonObject & jsonObj)79 void AuthMessageProcessor::GetJsonObj(JsonObject &jsonObj)
80 {
81 if (authResponseContext_ == nullptr || authResponseContext_->bindType.size() > MAX_BINDTYPE_SIZE) {
82 LOGE("GetJsonObj invalid bindType size.");
83 return;
84 }
85 jsonObj[TAG_VER] = DM_ITF_VER;
86 jsonObj[TAG_MSG_TYPE] = MSG_TYPE_REQ_AUTH;
87 jsonObj[TAG_INDEX] = 0;
88 jsonObj[TAG_REQUESTER] = authRequestContext_->localDeviceName;
89 jsonObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
90 jsonObj[TAG_LOCAL_DEVICE_ID] = authRequestContext_->localDeviceId;
91 jsonObj[TAG_LOCAL_DEVICE_TYPE] = authRequestContext_->localDeviceTypeId;
92 jsonObj[TAG_DEVICE_TYPE] = authRequestContext_->deviceTypeId;
93 jsonObj[TAG_AUTH_TYPE] = authRequestContext_->authType;
94 jsonObj[TAG_TOKEN] = authRequestContext_->token;
95 jsonObj[TAG_VISIBILITY] = authRequestContext_->groupVisibility;
96 if (authRequestContext_->groupVisibility == GROUP_VISIBILITY_IS_PRIVATE) {
97 jsonObj[TAG_TARGET] = authRequestContext_->targetPkgName;
98 jsonObj[TAG_HOST] = authRequestContext_->hostPkgName;
99 }
100 if (authRequestContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) {
101 jsonObj[TAG_IS_SHOW_DIALOG] = false;
102 } else {
103 jsonObj[TAG_IS_SHOW_DIALOG] = true;
104 }
105 jsonObj[TAG_APP_OPERATION] = authRequestContext_->appOperation;
106 jsonObj[TAG_CUSTOM_DESCRIPTION] = authRequestContext_->customDesc;
107 jsonObj[TAG_APP_NAME] = authRequestContext_->appName;
108 jsonObj[TAG_APP_DESCRIPTION] = authRequestContext_->appDesc;
109 jsonObj[TAG_BIND_TYPE_SIZE] = authResponseContext_->bindType.size();
110 for (uint32_t item = 0; item < authResponseContext_->bindType.size(); item++) {
111 std::string itemStr = std::to_string(item);
112 jsonObj[itemStr] = authResponseContext_->bindType[item];
113 }
114 }
115
IsPincodeImported()116 bool AuthMessageProcessor::IsPincodeImported()
117 {
118 auto sptrAuthMgr = authMgr_.lock();
119 if (sptrAuthMgr == nullptr) {
120 return false;
121 }
122 return sptrAuthMgr->IsImportedAuthCodeValid();
123 }
124
CreateAuthRequestMessage()125 std::vector<std::string> AuthMessageProcessor::CreateAuthRequestMessage()
126 {
127 LOGI("start.");
128 std::vector<std::string> jsonStrVec;
129 if (authRequestContext_ == nullptr) {
130 LOGE("AuthMessageProcessor::CreateAuthRequestMessage authRequestContext_ is nullptr.");
131 return jsonStrVec;
132 }
133 int32_t thumbnailSize = (int32_t)(authRequestContext_->appThumbnail.size());
134 int32_t thumbnailSlice = ((thumbnailSize / MSG_MAX_SIZE) + (thumbnailSize % MSG_MAX_SIZE) == 0 ? 0 : 1);
135 JsonObject jsonObj;
136 jsonObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
137 jsonObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
138 GetJsonObj(jsonObj);
139 jsonStrVec.push_back(jsonObj.Dump());
140 for (int32_t idx = 0; idx < thumbnailSlice; idx++) {
141 JsonObject jsonThumbnailObj;
142 jsonThumbnailObj[TAG_VER] = DM_ITF_VER;
143 jsonThumbnailObj[TAG_MSG_TYPE] = MSG_TYPE_REQ_AUTH;
144 jsonThumbnailObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
145 jsonThumbnailObj[TAG_INDEX] = idx + 1;
146 jsonThumbnailObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
147 jsonThumbnailObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
148 int32_t leftLen = thumbnailSize - idx * MSG_MAX_SIZE;
149 int32_t sliceLen = (leftLen > MSG_MAX_SIZE) ? MSG_MAX_SIZE : leftLen;
150 jsonObj[TAG_APP_THUMBNAIL] = authRequestContext_->appThumbnail.substr(idx * MSG_MAX_SIZE, sliceLen);
151 jsonStrVec.push_back(jsonThumbnailObj.Dump());
152 }
153 return jsonStrVec;
154 }
155
CreateSimpleMessage(int32_t msgType)156 std::string AuthMessageProcessor::CreateSimpleMessage(int32_t msgType)
157 {
158 LOGI("start. msgType is %{public}d", msgType);
159 JsonObject jsonObj;
160 jsonObj[TAG_VER] = DM_ITF_VER;
161 jsonObj[TAG_MSG_TYPE] = msgType;
162 switch (msgType) {
163 case MSG_TYPE_NEGOTIATE:
164 CreateNegotiateMessage(jsonObj);
165 break;
166 case MSG_TYPE_RESP_NEGOTIATE:
167 CreateRespNegotiateMessage(jsonObj);
168 break;
169 case MSG_TYPE_SYNC_GROUP:
170 CreateSyncGroupMessage(jsonObj);
171 break;
172 case MSG_TYPE_RESP_AUTH:
173 CreateResponseAuthMessage(jsonObj);
174 break;
175 case MSG_TYPE_RESP_AUTH_EXT:
176 CreateResponseAuthMessageExt(jsonObj);
177 break;
178 case MSG_TYPE_REQ_AUTH_TERMINATE:
179 CreateResponseFinishMessage(jsonObj);
180 break;
181 case MSG_TYPE_REQ_PUBLICKEY:
182 case MSG_TYPE_RESP_PUBLICKEY:
183 CreatePublicKeyMessageExt(jsonObj);
184 break;
185 case MSG_TYPE_REQ_RECHECK_MSG:
186 case MSG_TYPE_RESP_RECHECK_MSG:
187 CreateReqReCheckMessage(jsonObj);
188 break;
189 default:
190 break;
191 }
192 return jsonObj.Dump();
193 }
194
CreatePublicKeyMessageExt(JsonObject & json)195 void AuthMessageProcessor::CreatePublicKeyMessageExt(JsonObject &json)
196 {
197 bool encryptFlag = false;
198 {
199 std::lock_guard<std::mutex> mutexLock(encryptFlagMutex_);
200 encryptFlag = encryptFlag_;
201 }
202 if (!encryptFlag) {
203 LOGI("not encrypt publickey.");
204 json[TAG_PUBLICKEY] = authResponseContext_->publicKey;
205 return;
206 } else {
207 JsonObject jsonTemp;
208 auto sptr = authMgr_.lock();
209 if (sptr != nullptr && !sptr->IsSrc()) {
210 authResponseContext_->localSessionKeyId = sptr->GetSessionKeyIdSync(authResponseContext_->requestId);
211 jsonTemp[TAG_SESSIONKEY_ID] = authResponseContext_->localSessionKeyId;
212 }
213 jsonTemp[TAG_PUBLICKEY] = authResponseContext_->publicKey;
214 std::string strTemp = jsonTemp.Dump();
215 std::string encryptStr = "";
216 CHECK_NULL_VOID(cryptoMgr_);
217 if (cryptoMgr_->EncryptMessage(strTemp, encryptStr) != DM_OK) {
218 LOGE("EncryptMessage failed.");
219 return;
220 }
221 json[TAG_CRYPTIC_MSG] = encryptStr;
222 return;
223 }
224 }
225
CreateResponseAuthMessageExt(JsonObject & json)226 void AuthMessageProcessor::CreateResponseAuthMessageExt(JsonObject &json)
227 {
228 json[TAG_REPLY] = authResponseContext_->reply;
229 json[TAG_TOKEN] = authResponseContext_->token;
230 json[TAG_CONFIRM_OPERATION] = authResponseContext_->confirmOperation;
231 json[TAG_REQUEST_ID] = authResponseContext_->requestId;
232 }
233
CreateNegotiateMessage(JsonObject & json)234 void AuthMessageProcessor::CreateNegotiateMessage(JsonObject &json)
235 {
236 if (cryptoAdapter_ == nullptr) {
237 json[TAG_CRYPTO_SUPPORT] = false;
238 } else {
239 json[TAG_CRYPTO_SUPPORT] = true;
240 json[TAG_CRYPTO_NAME] = cryptoAdapter_->GetName();
241 json[TAG_CRYPTO_VERSION] = cryptoAdapter_->GetVersion();
242 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
243 }
244 json[TAG_AUTH_TYPE] = authResponseContext_->authType;
245 json[TAG_REPLY] = authResponseContext_->reply;
246 json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
247 json[TAG_ACCOUNT_GROUPID] = authResponseContext_->accountGroupIdHash;
248
249 json[TAG_BIND_LEVEL] = authResponseContext_->bindLevel;
250 json[TAG_LOCAL_ACCOUNTID] = authResponseContext_->localAccountId;
251 json[TAG_LOCAL_USERID] = authResponseContext_->localUserId;
252 json[TAG_ISONLINE] = authResponseContext_->isOnline;
253 json[TAG_AUTHED] = authResponseContext_->authed;
254 json[TAG_DMVERSION] = authResponseContext_->dmVersion;
255 json[TAG_HOST] = authResponseContext_->hostPkgName;
256 json[TAG_BUNDLE_NAME] = authResponseContext_->bundleName;
257 json[TAG_PEER_BUNDLE_NAME] = authResponseContext_->peerBundleName;
258 json[TAG_TOKENID] = authResponseContext_->tokenId;
259 json[TAG_IDENTICAL_ACCOUNT] = authResponseContext_->isIdenticalAccount;
260 json[TAG_HAVE_CREDENTIAL] = authResponseContext_->haveCredential;
261 json[TAG_HOST_PKGLABEL] = authResponseContext_->hostPkgLabel;
262 json[TAG_EDITION] = authResponseContext_->edition;
263 json[TAG_REMOTE_DEVICE_NAME] = authResponseContext_->remoteDeviceName;
264 }
265
CreateRespNegotiateMessage(JsonObject & json)266 void AuthMessageProcessor::CreateRespNegotiateMessage(JsonObject &json)
267 {
268 if (cryptoAdapter_ == nullptr) {
269 json[TAG_CRYPTO_SUPPORT] = false;
270 } else {
271 json[TAG_CRYPTO_SUPPORT] = true;
272 json[TAG_CRYPTO_NAME] = cryptoAdapter_->GetName();
273 json[TAG_CRYPTO_VERSION] = cryptoAdapter_->GetVersion();
274 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
275 }
276 json[TAG_AUTH_TYPE] = authResponseContext_->authType;
277 json[TAG_REPLY] = authResponseContext_->reply;
278 json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
279 json[TAG_IS_AUTH_CODE_READY] = authResponseContext_->isAuthCodeReady;
280 json[TAG_NET_ID] = authResponseContext_->networkId;
281
282 json[TAG_LOCAL_ACCOUNTID] = authResponseContext_->localAccountId;
283 json[TAG_LOCAL_USERID] = authResponseContext_->localUserId;
284 json[TAG_TOKENID] = authResponseContext_->tokenId;
285 json[TAG_ISONLINE] = authResponseContext_->isOnline;
286 json[TAG_AUTHED] = authResponseContext_->authed;
287 json[TAG_DMVERSION] = authResponseContext_->dmVersion;
288 json[TAG_BIND_LEVEL] = authResponseContext_->bindLevel;
289 json[TAG_IDENTICAL_ACCOUNT] = authResponseContext_->isIdenticalAccount;
290 json[TAG_HAVE_CREDENTIAL] = authResponseContext_->haveCredential;
291 json[TAG_BIND_TYPE_SIZE] = authResponseContext_->bindType.size();
292 json[TAG_TARGET_DEVICE_NAME] = authResponseContext_->targetDeviceName;
293 json[TAG_IMPORT_AUTH_CODE] = authResponseContext_->importAuthCode;
294 for (uint32_t item = 0; item < authResponseContext_->bindType.size(); item++) {
295 auto itemStr = std::to_string(item);
296 json[itemStr] = authResponseContext_->bindType[item];
297 }
298 }
299
CreateSyncGroupMessage(JsonObject & json)300 void AuthMessageProcessor::CreateSyncGroupMessage(JsonObject &json)
301 {
302 json[TAG_DEVICE_ID] = authRequestContext_->deviceId;
303 json[TAG_GROUPIDS] = authRequestContext_->syncGroupList;
304 }
305
CreateResponseAuthMessage(JsonObject & json)306 void AuthMessageProcessor::CreateResponseAuthMessage(JsonObject &json)
307 {
308 json[TAG_REPLY] = authResponseContext_->reply;
309 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
310 json[TAG_TOKEN] = authResponseContext_->token;
311 if (authResponseContext_->reply == 0) {
312 std::string groupId = authResponseContext_->groupId;
313 LOGI("groupId %{public}s", GetAnonyString(groupId).c_str());
314 JsonObject jsonObject(groupId);
315 if (jsonObject.IsDiscarded()) {
316 LOGE("DecodeRequestAuth jsonStr error");
317 return;
318 }
319 if (!IsString(jsonObject, TAG_GROUP_ID)) {
320 LOGE("err json string.");
321 return;
322 }
323 groupId = jsonObject[TAG_GROUP_ID].Get<std::string>();
324 json[TAG_NET_ID] = authResponseContext_->networkId;
325 json[TAG_REQUEST_ID] = authResponseContext_->requestId;
326 json[TAG_GROUP_ID] = groupId;
327 json[TAG_GROUP_NAME] = authResponseContext_->groupName;
328 json[TAG_AUTH_TOKEN] = authResponseContext_->authToken;
329 LOGI("AuthMessageProcessor::CreateResponseAuthMessage %{public}s, %{public}s", GetAnonyString(groupId).c_str(),
330 GetAnonyString(authResponseContext_->groupName).c_str());
331 }
332 }
333
CreateResponseFinishMessage(JsonObject & json)334 void AuthMessageProcessor::CreateResponseFinishMessage(JsonObject &json)
335 {
336 json[TAG_REPLY] = authResponseContext_->reply;
337 json[TAG_AUTH_FINISH] = authResponseContext_->isFinish;
338 }
339
ParseMessage(const std::string & message)340 int32_t AuthMessageProcessor::ParseMessage(const std::string &message)
341 {
342 JsonObject jsonObject(message);
343 if (jsonObject.IsDiscarded()) {
344 LOGE("DecodeRequestAuth jsonStr error");
345 return ERR_DM_FAILED;
346 }
347 if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
348 LOGE("err json string, first time");
349 return ERR_DM_FAILED;
350 }
351 int32_t msgType = jsonObject[TAG_MSG_TYPE].Get<int32_t>();
352 authResponseContext_->msgType = msgType;
353 LOGI("message type %{public}d", authResponseContext_->msgType);
354 switch (msgType) {
355 case MSG_TYPE_NEGOTIATE:
356 ParseNegotiateMessage(jsonObject);
357 break;
358 case MSG_TYPE_RESP_NEGOTIATE:
359 ParseRespNegotiateMessage(jsonObject);
360 break;
361 case MSG_TYPE_REQ_AUTH:
362 return ParseAuthRequestMessage(jsonObject);
363 break;
364 case MSG_TYPE_RESP_AUTH:
365 ParseAuthResponseMessage(jsonObject);
366 break;
367 case MSG_TYPE_RESP_AUTH_EXT:
368 ParseAuthResponseMessageExt(jsonObject);
369 break;
370 case MSG_TYPE_REQ_AUTH_TERMINATE:
371 ParseResponseFinishMessage(jsonObject);
372 break;
373 case MSG_TYPE_REQ_PUBLICKEY:
374 case MSG_TYPE_RESP_PUBLICKEY:
375 ParsePublicKeyMessageExt(jsonObject);
376 break;
377 case MSG_TYPE_REQ_RECHECK_MSG:
378 case MSG_TYPE_RESP_RECHECK_MSG:
379 ParseReqReCheckMessage(jsonObject);
380 break;
381 default:
382 break;
383 }
384 return DM_OK;
385 }
386
ParsePublicKeyMessageExt(JsonObject & json)387 void AuthMessageProcessor::ParsePublicKeyMessageExt(JsonObject &json)
388 {
389 bool encryptFlag = false;
390 {
391 std::lock_guard<std::mutex> mutexLock(encryptFlagMutex_);
392 encryptFlag = encryptFlag_;
393 }
394 if (!encryptFlag && IsString(json, TAG_PUBLICKEY)) {
395 authResponseContext_->publicKey = json[TAG_PUBLICKEY].Get<std::string>();
396 return;
397 }
398 if (encryptFlag && IsString(json, TAG_CRYPTIC_MSG)) {
399 std::string encryptStr = json[TAG_CRYPTIC_MSG].Get<std::string>();
400 std::string decryptStr = "";
401 authResponseContext_->publicKey = "";
402 CHECK_NULL_VOID(cryptoMgr_);
403 if (cryptoMgr_->DecryptMessage(encryptStr, decryptStr) != DM_OK) {
404 LOGE("DecryptMessage failed.");
405 return;
406 }
407 JsonObject jsonObject(decryptStr);
408 if (jsonObject.IsDiscarded()) {
409 LOGE("DecodeRequestAuth jsonStr error");
410 return;
411 }
412 if (IsString(jsonObject, TAG_PUBLICKEY)) {
413 authResponseContext_->publicKey = jsonObject[TAG_PUBLICKEY].Get<std::string>();
414 }
415 if (IsInt32(jsonObject, TAG_SESSIONKEY_ID)) {
416 authResponseContext_->remoteSessionKeyId = jsonObject[TAG_SESSIONKEY_ID].Get<int32_t>();
417 }
418 return;
419 }
420 }
421
ParseAuthResponseMessageExt(JsonObject & json)422 void AuthMessageProcessor::ParseAuthResponseMessageExt(JsonObject &json)
423 {
424 LOGI("start");
425 if (IsInt32(json, TAG_REPLY)) {
426 authResponseContext_->reply = json[TAG_REPLY].Get<int32_t>();
427 }
428 if (IsString(json, TAG_TOKEN)) {
429 authResponseContext_->token = json[TAG_TOKEN].Get<std::string>();
430 }
431 if (IsInt32(json, TAG_CONFIRM_OPERATION)) {
432 authResponseContext_->confirmOperation = json[TAG_CONFIRM_OPERATION].Get<int32_t>();
433 }
434 if (IsInt64(json, TAG_REQUEST_ID)) {
435 authResponseContext_->requestId = json[TAG_REQUEST_ID].Get<int64_t>();
436 }
437 }
438
ParseResponseFinishMessage(JsonObject & json)439 void AuthMessageProcessor::ParseResponseFinishMessage(JsonObject &json)
440 {
441 if (IsInt32(json, TAG_REPLY)) {
442 authResponseContext_->reply = json[TAG_REPLY].Get<int32_t>();
443 }
444 if (IsBool(json, TAG_AUTH_FINISH)) {
445 authResponseContext_->isFinish = json[TAG_AUTH_FINISH].Get<bool>();
446 }
447 }
448
GetAuthReqMessage(JsonObject & json)449 void AuthMessageProcessor::GetAuthReqMessage(JsonObject &json)
450 {
451 authResponseContext_->localDeviceId = "";
452 authResponseContext_->deviceId = "";
453 if (IsInt32(json, TAG_AUTH_TYPE)) {
454 authResponseContext_->authType = json[TAG_AUTH_TYPE].Get<int32_t>();
455 }
456 if (IsString(json, TAG_TOKEN)) {
457 authResponseContext_->token = json[TAG_TOKEN].Get<std::string>();
458 }
459 if (IsString(json, TAG_DEVICE_ID)) {
460 authResponseContext_->deviceId = json[TAG_DEVICE_ID].Get<std::string>();
461 }
462 if (IsString(json, TAG_TARGET)) {
463 authResponseContext_->targetPkgName = json[TAG_TARGET].Get<std::string>();
464 }
465 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
466 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].Get<std::string>();
467 }
468 if (IsString(json, TAG_APP_OPERATION)) {
469 authResponseContext_->appOperation = json[TAG_APP_OPERATION].Get<std::string>();
470 }
471 if (IsString(json, TAG_CUSTOM_DESCRIPTION)) {
472 authResponseContext_->customDesc = json[TAG_CUSTOM_DESCRIPTION].Get<std::string>();
473 }
474 if (IsString(json, TAG_REQUESTER)) {
475 authResponseContext_->deviceName = json[TAG_REQUESTER].Get<std::string>();
476 }
477 if (IsInt32(json, TAG_LOCAL_DEVICE_TYPE)) {
478 authResponseContext_->deviceTypeId = json[TAG_LOCAL_DEVICE_TYPE].Get<int32_t>();
479 } else {
480 authResponseContext_->deviceTypeId = DmDeviceType::DEVICE_TYPE_UNKNOWN;
481 }
482 }
483
ParseAuthRequestMessage(JsonObject & json)484 int32_t AuthMessageProcessor::ParseAuthRequestMessage(JsonObject &json)
485 {
486 LOGI("start");
487 int32_t sliceNum = 0;
488 int32_t idx = 0;
489 if (!IsInt32(json, TAG_INDEX) || !IsInt32(json, TAG_SLICE_NUM)) {
490 LOGE("AuthMessageProcessor::ParseAuthRequestMessage err json string, first.");
491 return ERR_DM_FAILED;
492 }
493 idx = json[TAG_INDEX].Get<int32_t>();
494 sliceNum = json[TAG_SLICE_NUM].Get<int32_t>();
495 if (idx == 0) {
496 GetAuthReqMessage(json);
497 authResponseContext_->appThumbnail = "";
498 }
499 if (idx < sliceNum && IsString(json, TAG_APP_THUMBNAIL)) {
500 std::string appSliceThumbnail = json[TAG_APP_THUMBNAIL].Get<std::string>();
501 authResponseContext_->appThumbnail = authResponseContext_->appThumbnail + appSliceThumbnail;
502 return ERR_DM_AUTH_MESSAGE_INCOMPLETE;
503 }
504 if (IsBool(json, TAG_IS_SHOW_DIALOG)) {
505 authResponseContext_->isShowDialog = json[TAG_IS_SHOW_DIALOG].Get<bool>();
506 } else {
507 authResponseContext_->isShowDialog = true;
508 }
509 if (IsInt32(json, TAG_BIND_TYPE_SIZE)) {
510 int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].Get<int32_t>();
511 if (bindTypeSize > MAX_BINDTYPE_SIZE) {
512 LOGE("ParseAuthRequestMessage bindTypeSize is over size.");
513 return ERR_DM_FAILED;
514 }
515 authResponseContext_->bindType.clear();
516 for (int32_t item = 0; item < bindTypeSize; item++) {
517 std::string itemStr = std::to_string(item);
518 if (IsInt32(json, itemStr)) {
519 authResponseContext_->bindType.push_back(json[itemStr].Get<int32_t>());
520 }
521 }
522 }
523 return DM_OK;
524 }
525
ParseAuthResponseMessage(JsonObject & json)526 void AuthMessageProcessor::ParseAuthResponseMessage(JsonObject &json)
527 {
528 LOGI("start");
529 if (!IsInt32(json, TAG_REPLY)) {
530 LOGE("AuthMessageProcessor::ParseAuthResponseMessage err json string, first time.");
531 return;
532 }
533 authResponseContext_->reply = json[TAG_REPLY].Get<int32_t>();
534 if (IsString(json, TAG_DEVICE_ID)) {
535 authResponseContext_->deviceId = json[TAG_DEVICE_ID].Get<std::string>();
536 }
537 if (IsString(json, TAG_TOKEN)) {
538 authResponseContext_->token = json[TAG_TOKEN].Get<std::string>();
539 }
540 if (authResponseContext_->reply == 0) {
541 if (!IsInt64(json, TAG_REQUEST_ID) || !IsString(json, TAG_GROUP_ID) ||
542 !IsString(json, TAG_GROUP_NAME) || !IsString(json, TAG_AUTH_TOKEN)) {
543 LOGE("AuthMessageProcessor::ParseAuthResponseMessage err json string, second time.");
544 return;
545 }
546 authResponseContext_->requestId = json[TAG_REQUEST_ID].Get<int64_t>();
547 authResponseContext_->groupId = json[TAG_GROUP_ID].Get<std::string>();
548 authResponseContext_->groupName = json[TAG_GROUP_NAME].Get<std::string>();
549 authResponseContext_->authToken = json[TAG_AUTH_TOKEN].Get<std::string>();
550 if (IsString(json, TAG_NET_ID)) {
551 authResponseContext_->networkId = json[TAG_NET_ID].Get<std::string>();
552 }
553 LOGI("AuthMessageProcessor::ParseAuthResponseMessage groupId = %{public}s, groupName = %{public}s",
554 GetAnonyString(authResponseContext_->groupId).c_str(),
555 GetAnonyString(authResponseContext_->groupName).c_str());
556 }
557 }
558
ParsePkgNegotiateMessage(const JsonObject & json)559 void AuthMessageProcessor::ParsePkgNegotiateMessage(const JsonObject &json)
560 {
561 if (IsString(json, TAG_LOCAL_ACCOUNTID)) {
562 authResponseContext_->localAccountId = json[TAG_LOCAL_ACCOUNTID].Get<std::string>();
563 }
564 if (IsInt32(json, TAG_LOCAL_USERID)) {
565 authResponseContext_->localUserId = json[TAG_LOCAL_USERID].Get<int32_t>();
566 }
567 if (IsInt32(json, TAG_BIND_LEVEL)) {
568 authResponseContext_->bindLevel = json[TAG_BIND_LEVEL].Get<int32_t>();
569 }
570 if (IsBool(json, TAG_ISONLINE)) {
571 authResponseContext_->isOnline = json[TAG_ISONLINE].Get<bool>();
572 }
573 if (IsBool(json, TAG_IDENTICAL_ACCOUNT)) {
574 authResponseContext_->isIdenticalAccount = json[TAG_IDENTICAL_ACCOUNT].Get<bool>();
575 }
576 if (IsBool(json, TAG_AUTHED)) {
577 authResponseContext_->authed = json[TAG_AUTHED].Get<bool>();
578 }
579 if (IsInt64(json, TAG_TOKENID)) {
580 authResponseContext_->remoteTokenId = json[TAG_TOKENID].Get<int64_t>();
581 }
582 if (IsString(json, TAG_DMVERSION)) {
583 authResponseContext_->dmVersion = json[TAG_DMVERSION].Get<std::string>();
584 } else {
585 authResponseContext_->dmVersion = "3.2";
586 }
587 if (IsBool(json, TAG_HAVECREDENTIAL)) {
588 authResponseContext_->haveCredential = json[TAG_HAVECREDENTIAL].Get<bool>();
589 }
590 if (IsInt32(json, TAG_BIND_TYPE_SIZE)) {
591 int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].Get<int32_t>();
592 if (bindTypeSize > MAX_BINDTYPE_SIZE) {
593 LOGE("ParsePkgNegotiateMessage bindTypeSize is over size.");
594 return;
595 }
596 authResponseContext_->bindType.clear();
597 for (int32_t item = 0; item < bindTypeSize; item++) {
598 std::string itemStr = std::to_string(item);
599 if (IsInt32(json, itemStr)) {
600 authResponseContext_->bindType.push_back(json[itemStr].Get<int32_t>());
601 }
602 }
603 }
604 if (IsString(json, TAG_HOST_PKGLABEL)) {
605 authResponseContext_->hostPkgLabel = json[TAG_HOST_PKGLABEL].Get<std::string>();
606 }
607 }
608
ParseNegotiateMessage(const JsonObject & json)609 void AuthMessageProcessor::ParseNegotiateMessage(const JsonObject &json)
610 {
611 if (IsBool(json, TAG_CRYPTO_SUPPORT)) {
612 authResponseContext_->cryptoSupport = json[TAG_CRYPTO_SUPPORT].Get<bool>();
613 }
614 if (IsString(json, TAG_CRYPTO_NAME)) {
615 authResponseContext_->cryptoName = json[TAG_CRYPTO_NAME].Get<std::string>();
616 }
617 if (IsString(json, TAG_CRYPTO_VERSION)) {
618 authResponseContext_->cryptoVer = json[TAG_CRYPTO_VERSION].Get<std::string>();
619 }
620 if (IsString(json, TAG_DEVICE_ID)) {
621 authResponseContext_->deviceId = json[TAG_DEVICE_ID].Get<std::string>();
622 }
623 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
624 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].Get<std::string>();
625 }
626 if (IsInt32(json, TAG_AUTH_TYPE)) {
627 authResponseContext_->authType = json[TAG_AUTH_TYPE].Get<int32_t>();
628 }
629 if (IsInt32(json, TAG_REPLY)) {
630 authResponseContext_->reply = json[TAG_REPLY].Get<int32_t>();
631 }
632 if (IsString(json, TAG_ACCOUNT_GROUPID)) {
633 authResponseContext_->accountGroupIdHash = json[TAG_ACCOUNT_GROUPID].Get<std::string>();
634 } else {
635 authResponseContext_->accountGroupIdHash = OLD_VERSION_ACCOUNT;
636 }
637 if (IsString(json, TAG_HOST)) {
638 authResponseContext_->hostPkgName = json[TAG_HOST].Get<std::string>();
639 }
640 if (IsString(json, TAG_EDITION)) {
641 authResponseContext_->edition = json[TAG_EDITION].Get<std::string>();
642 }
643 if (IsString(json, TAG_BUNDLE_NAME)) {
644 authResponseContext_->bundleName = json[TAG_BUNDLE_NAME].Get<std::string>();
645 }
646 if (IsString(json, TAG_PEER_BUNDLE_NAME)) {
647 authResponseContext_->peerBundleName = json[TAG_PEER_BUNDLE_NAME].Get<std::string>();
648 } else {
649 authResponseContext_->peerBundleName = authResponseContext_->hostPkgName;
650 }
651 if (IsString(json, TAG_REMOTE_DEVICE_NAME)) {
652 authResponseContext_->remoteDeviceName = json[TAG_REMOTE_DEVICE_NAME].Get<std::string>();
653 }
654 ParsePkgNegotiateMessage(json);
655 }
656
ParseRespNegotiateMessage(const JsonObject & json)657 void AuthMessageProcessor::ParseRespNegotiateMessage(const JsonObject &json)
658 {
659 if (IsBool(json, TAG_IDENTICAL_ACCOUNT)) {
660 authResponseContext_->isIdenticalAccount = json[TAG_IDENTICAL_ACCOUNT].Get<bool>();
661 }
662 if (IsInt32(json, TAG_REPLY)) {
663 authResponseContext_->reply = json[TAG_REPLY].Get<int32_t>();
664 }
665 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
666 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].Get<std::string>();
667 }
668 if (IsBool(json, TAG_IS_AUTH_CODE_READY)) {
669 authResponseContext_->isAuthCodeReady = json[TAG_IS_AUTH_CODE_READY].Get<bool>();
670 }
671 if (IsString(json, TAG_ACCOUNT_GROUPID)) {
672 authResponseContext_->accountGroupIdHash = json[TAG_ACCOUNT_GROUPID].Get<std::string>();
673 } else {
674 authResponseContext_->accountGroupIdHash = OLD_VERSION_ACCOUNT;
675 }
676 if (IsString(json, TAG_NET_ID)) {
677 authResponseContext_->networkId = json[TAG_NET_ID].Get<std::string>();
678 }
679 if (IsString(json, TAG_TARGET_DEVICE_NAME)) {
680 authResponseContext_->targetDeviceName = json[TAG_TARGET_DEVICE_NAME].Get<std::string>();
681 }
682 if (IsString(json, TAG_IMPORT_AUTH_CODE)) {
683 authResponseContext_->importAuthCode = json[TAG_IMPORT_AUTH_CODE].Get<std::string>();
684 }
685
686 ParsePkgNegotiateMessage(json);
687 }
688
SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)689 void AuthMessageProcessor::SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)
690 {
691 authRequestContext_ = authRequestContext;
692 }
693
SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)694 void AuthMessageProcessor::SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)
695 {
696 authResponseContext_ = authResponseContext;
697 }
698
GetResponseContext()699 std::shared_ptr<DmAuthResponseContext> AuthMessageProcessor::GetResponseContext()
700 {
701 return authResponseContext_;
702 }
703
GetRequestContext()704 std::shared_ptr<DmAuthRequestContext> AuthMessageProcessor::GetRequestContext()
705 {
706 return authRequestContext_;
707 }
708
CreateDeviceAuthMessage(int32_t msgType,const uint8_t * data,uint32_t dataLen)709 std::string AuthMessageProcessor::CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen)
710 {
711 LOGI("start, msgType %{public}d.", msgType);
712 JsonObject jsonObj;
713 jsonObj[TAG_MSG_TYPE] = msgType;
714 std::string authDataStr = std::string(reinterpret_cast<const char *>(data), dataLen);
715 jsonObj[TAG_DATA] = authDataStr;
716 jsonObj[TAG_DATA_LEN] = dataLen;
717 return jsonObj.Dump();
718 }
719
CreateReqReCheckMessage(JsonObject & jsonObj)720 void AuthMessageProcessor::CreateReqReCheckMessage(JsonObject &jsonObj)
721 {
722 JsonObject jsonTemp;
723 jsonTemp[TAG_EDITION] = authResponseContext_->edition;
724 jsonTemp[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
725 jsonTemp[TAG_LOCAL_USERID] = authResponseContext_->localUserId;
726 jsonTemp[TAG_BUNDLE_NAME] = authResponseContext_->bundleName;
727 jsonTemp[TAG_BIND_LEVEL] = authResponseContext_->bindLevel;
728 jsonTemp[TAG_LOCAL_ACCOUNTID] = authResponseContext_->localAccountId;
729 jsonTemp[TAG_TOKENID] = authResponseContext_->tokenId;
730 std::string strTemp = jsonTemp.Dump();
731 std::string encryptStr = "";
732 CHECK_NULL_VOID(cryptoMgr_);
733 if (cryptoMgr_->EncryptMessage(strTemp, encryptStr) != DM_OK) {
734 LOGE("EncryptMessage failed.");
735 return;
736 }
737 jsonObj[TAG_CRYPTIC_MSG] = encryptStr;
738 }
739
ParseReqReCheckMessage(JsonObject & json)740 void AuthMessageProcessor::ParseReqReCheckMessage(JsonObject &json)
741 {
742 std::string encryptStr = "";
743 if (IsString(json, TAG_CRYPTIC_MSG)) {
744 encryptStr = json[TAG_CRYPTIC_MSG].Get<std::string>();
745 }
746 std::string decryptStr = "";
747 authResponseContext_->edition = "";
748 authResponseContext_->localDeviceId = "";
749 authResponseContext_->localUserId = 0;
750 authResponseContext_->bundleName = "";
751 authResponseContext_->localBindLevel = -1;
752 authResponseContext_->localAccountId = "";
753 authResponseContext_->tokenId = 0;
754 CHECK_NULL_VOID(cryptoMgr_);
755 if (cryptoMgr_->DecryptMessage(encryptStr, decryptStr) != DM_OK) {
756 LOGE("DecryptMessage failed.");
757 return;
758 }
759 JsonObject jsonObject(decryptStr);
760 if (jsonObject.IsDiscarded()) {
761 LOGE("DecodeRequestAuth jsonStr error");
762 return;
763 }
764 if (IsString(jsonObject, TAG_EDITION)) {
765 authResponseContext_->edition = jsonObject[TAG_EDITION].Get<std::string>();
766 }
767 if (IsString(jsonObject, TAG_LOCAL_DEVICE_ID)) {
768 authResponseContext_->localDeviceId = jsonObject[TAG_LOCAL_DEVICE_ID].Get<std::string>();
769 }
770 if (IsInt32(jsonObject, TAG_LOCAL_USERID)) {
771 authResponseContext_->localUserId = jsonObject[TAG_LOCAL_USERID].Get<int32_t>();
772 }
773 if (IsString(jsonObject, TAG_BUNDLE_NAME)) {
774 authResponseContext_->bundleName = jsonObject[TAG_BUNDLE_NAME].Get<std::string>();
775 }
776 if (IsInt32(jsonObject, TAG_BIND_LEVEL)) {
777 authResponseContext_->localBindLevel = jsonObject[TAG_BIND_LEVEL].Get<int32_t>();
778 }
779 if (IsString(jsonObject, TAG_LOCAL_ACCOUNTID)) {
780 authResponseContext_->localAccountId = jsonObject[TAG_LOCAL_ACCOUNTID].Get<std::string>();
781 }
782 if (IsInt64(jsonObject, TAG_TOKENID)) {
783 authResponseContext_->tokenId = jsonObject[TAG_TOKENID].Get<int64_t>();
784 }
785 }
786
SaveSessionKey(const uint8_t * sessionKey,const uint32_t keyLen)787 int32_t AuthMessageProcessor::SaveSessionKey(const uint8_t *sessionKey, const uint32_t keyLen)
788 {
789 CHECK_NULL_RETURN(cryptoMgr_, ERR_DM_POINT_NULL);
790 return cryptoMgr_->SaveSessionKey(sessionKey, keyLen);
791 }
792
SetEncryptFlag(bool flag)793 void AuthMessageProcessor::SetEncryptFlag(bool flag)
794 {
795 std::lock_guard<std::mutex> mutexLock(encryptFlagMutex_);
796 encryptFlag_ = flag;
797 }
798
ProcessSessionKey(const uint8_t * sessionKey,const uint32_t keyLen)799 int32_t AuthMessageProcessor::ProcessSessionKey(const uint8_t *sessionKey, const uint32_t keyLen)
800 {
801 CHECK_NULL_RETURN(cryptoMgr_, ERR_DM_POINT_NULL);
802 return cryptoMgr_->ProcessSessionKey(sessionKey, keyLen);
803 }
804 } // namespace DistributedHardware
805 } // namespace OHOS