1 /*
2  * Copyright (c) 2022-2023 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 #ifndef OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
17 #define OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "crypto_adapter.h"
23 #include "dm_auth_manager.h"
24 #include "nlohmann/json.hpp"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 constexpr const char* TAG_REPLY = "REPLY";
29 constexpr const char* TAG_NET_ID = "NETID";
30 constexpr const char* TAG_TARGET = "TARGET";
31 constexpr const char* TAG_APP_OPERATION = "APPOPERATION";
32 constexpr const char* TAG_APP_NAME = "APPNAME";
33 constexpr const char* TAG_APP_DESCRIPTION = "APPDESC";
34 constexpr const char* TAG_GROUPIDS = "GROUPIDLIST";
35 constexpr const char* TAG_CUSTOM_DESCRIPTION = "CUSTOMDESC";
36 constexpr const char* TAG_DEVICE_TYPE = "DEVICETYPE";
37 constexpr const char* TAG_REQUESTER = "REQUESTER";
38 constexpr const char* TAG_LOCAL_DEVICE_ID = "LOCALDEVICEID";
39 constexpr const char* TAG_LOCAL_DEVICE_TYPE = "LOCALDEVICETYPE";
40 constexpr const char* TAG_INDEX = "INDEX";
41 constexpr const char* TAG_SLICE_NUM = "SLICE";
42 constexpr const char* TAG_IS_AUTH_CODE_READY = "IS_AUTH_CODE_READY";
43 constexpr const char* TAG_IS_SHOW_DIALOG = "IS_SHOW_DIALOG";
44 constexpr const char* TAG_TOKEN = "TOKEN";
45 constexpr const char* TAG_CRYPTO_NAME = "CRYPTONAME";
46 constexpr const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION";
47 constexpr const char* TAG_IDENTICAL_ACCOUNT = "IDENTICALACCOUNT";
48 constexpr const char* TAG_ACCOUNT_GROUPID = "ACCOUNTGROUPID";
49 constexpr const char* APP_THUMBNAIL = "appThumbnail";
50 constexpr const char* QR_CODE_KEY = "qrCode";
51 constexpr const char* TAG_AUTH_TOKEN = "authToken";
52 constexpr const char* NFC_CODE_KEY = "nfcCode";
53 constexpr const char* OLD_VERSION_ACCOUNT = "oldVersionAccount";
54 constexpr const char* TAG_AUTH_FINISH = "isFinish";
55 
56 constexpr const char* TAG_HAVE_CREDENTIAL = "haveCredential";
57 constexpr const char* TAG_PUBLICKEY = "publicKey";
58 constexpr const char* TAG_SESSIONKEY = "sessionKey";
59 constexpr const char* TAG_BIND_LEVEL = "bindLevel";
60 constexpr const char* TAG_LOCAL_USERID = "localUserId";
61 constexpr const char* TAG_BIND_TYPE_SIZE = "bindTypeSize";
62 constexpr const char* TAG_ISONLINE = "isOnline";
63 constexpr const char* TAG_AUTHED = "authed";
64 constexpr const char* TAG_LOCAL_ACCOUNTID = "localAccountId";
65 constexpr const char* TAG_DMVERSION = "dmVersion";
66 constexpr const char* TAG_HOST_PKGNAME = "hostPkgname";
67 constexpr const char* TAG_TOKENID = "tokenId";
68 constexpr const char* TAG_HAVECREDENTIAL = "haveCredential";
69 constexpr const char* TAG_CONFIRM_OPERATION = "confirmOperation";
70 constexpr const char* TAG_DATA = "data";
71 constexpr const char* TAG_DATA_LEN = "dataLen";
72 constexpr const char* TAG_IMPORT_AUTH_CODE = "IMPORT_AUTH_CODE";
73 constexpr const char* TAG_HOST_PKGLABEL = "hostPkgLabel";
74 constexpr const char* TAG_EDITION = "edition";
75 
76 class DmAuthManager;
77 struct DmAuthRequestContext;
78 struct DmAuthResponseContext;
79 class ICryptoAdapter;
80 class AuthMessageProcessor {
81 public:
82     explicit AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr);
83     ~AuthMessageProcessor();
84     std::vector<std::string> CreateAuthRequestMessage();
85     std::string CreateSimpleMessage(int32_t msgType);
86     int32_t ParseMessage(const std::string &message);
87     void SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext);
88     void SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext);
89     std::shared_ptr<DmAuthResponseContext> GetResponseContext();
90     std::shared_ptr<DmAuthRequestContext> GetRequestContext();
91     std::string CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen);
92     void CreateResponseAuthMessageExt(nlohmann::json &json);
93     void ParseAuthResponseMessageExt(nlohmann::json &json);
94 
95 private:
96     std::string CreateRequestAuthMessage(nlohmann::json &json);
97     void CreateNegotiateMessage(nlohmann::json &json);
98     void CreateRespNegotiateMessage(nlohmann::json &json);
99     void CreateSyncGroupMessage(nlohmann::json &json);
100     void CreateResponseAuthMessage(nlohmann::json &json);
101     void ParseAuthResponseMessage(nlohmann::json &json);
102     int32_t ParseAuthRequestMessage(nlohmann::json &json);
103     void ParseNegotiateMessage(const nlohmann::json &json);
104     void ParseRespNegotiateMessage(const nlohmann::json &json);
105     void CreateResponseFinishMessage(nlohmann::json &json);
106     void ParseResponseFinishMessage(nlohmann::json &json);
107     void GetAuthReqMessage(nlohmann::json &json);
108     void ParsePkgNegotiateMessage(const nlohmann::json &json);
109     void CreatePublicKeyMessageExt(nlohmann::json &json);
110     void ParsePublicKeyMessageExt(nlohmann::json &json);
111     void CreateSyncDeleteMessageExt(nlohmann::json &json);
112     void ParseSyncDeleteMessageExt(nlohmann::json &json);
113     void GetJsonObj(nlohmann::json &jsonObj);
114 
115 private:
116     std::weak_ptr<DmAuthManager> authMgr_;
117     std::shared_ptr<ICryptoAdapter> cryptoAdapter_;
118     std::shared_ptr<DmAuthRequestContext> authRequestContext_;
119     std::shared_ptr<DmAuthResponseContext> authResponseContext_;
120     std::vector<nlohmann::json> authSplitJsonList_;
121 };
122 } // namespace DistributedHardware
123 } // namespace OHOS
124 #endif // OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
125