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