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 <mutex> 21 #include <vector> 22 23 #include "crypto_adapter.h" 24 #include "crypto_mgr.h" 25 #include "dm_auth_manager.h" 26 #include "json_object.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 extern const char* TAG_REPLY; 31 extern const char* TAG_NET_ID; 32 extern const char* TAG_TARGET; 33 extern const char* TAG_APP_OPERATION; 34 extern const char* TAG_APP_NAME; 35 extern const char* TAG_APP_DESCRIPTION; 36 extern const char* TAG_GROUPIDS; 37 extern const char* TAG_CUSTOM_DESCRIPTION; 38 extern const char* TAG_DEVICE_TYPE; 39 extern const char* TAG_REQUESTER; 40 extern const char* TAG_LOCAL_DEVICE_ID; 41 extern const char* TAG_LOCAL_DEVICE_TYPE; 42 extern const char* TAG_INDEX; 43 extern const char* TAG_SLICE_NUM; 44 extern const char* TAG_IS_AUTH_CODE_READY; 45 extern const char* TAG_IS_SHOW_DIALOG; 46 extern const char* TAG_TOKEN; 47 extern const char* TAG_CRYPTO_NAME; 48 extern const char* TAG_CRYPTO_VERSION; 49 extern const char* TAG_IDENTICAL_ACCOUNT; 50 extern const char* TAG_ACCOUNT_GROUPID; 51 extern const char* APP_THUMBNAIL; 52 extern const char* QR_CODE_KEY; 53 extern const char* TAG_AUTH_TOKEN; 54 extern const char* NFC_CODE_KEY; 55 extern const char* OLD_VERSION_ACCOUNT; 56 extern const char* TAG_AUTH_FINISH; 57 58 extern const char* TAG_HAVE_CREDENTIAL; 59 extern const char* TAG_PUBLICKEY; 60 extern const char* TAG_SESSIONKEY; 61 extern const char* TAG_BIND_LEVEL; 62 extern const char* TAG_LOCAL_USERID; 63 extern const char* TAG_BIND_TYPE_SIZE; 64 extern const char* TAG_ISONLINE; 65 extern const char* TAG_AUTHED; 66 extern const char* TAG_LOCAL_ACCOUNTID; 67 extern const char* TAG_DMVERSION; 68 extern const char* TAG_HOST_PKGNAME; 69 extern const char* TAG_TOKENID; 70 extern const char* TAG_HAVECREDENTIAL; 71 extern const char* TAG_CONFIRM_OPERATION; 72 extern const char* TAG_DATA; 73 extern const char* TAG_DATA_LEN; 74 extern const char* TAG_IMPORT_AUTH_CODE; 75 extern const char* TAG_HOST_PKGLABEL; 76 extern const char* TAG_EDITION; 77 extern const char* TAG_BUNDLE_NAME; 78 extern const char* TAG_CRYPTIC_MSG; 79 extern const char* TAG_PEER_BUNDLE_NAME; 80 extern const char* TAG_REMOTE_DEVICE_NAME; 81 extern const char* TAG_SESSIONKEY_ID; 82 83 class DmAuthManager; 84 struct DmAuthRequestContext; 85 struct DmAuthResponseContext; 86 class ICryptoAdapter; 87 class AuthMessageProcessor { 88 public: 89 explicit AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr); 90 ~AuthMessageProcessor(); 91 std::vector<std::string> CreateAuthRequestMessage(); 92 std::string CreateSimpleMessage(int32_t msgType); 93 int32_t ParseMessage(const std::string &message); 94 void SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext); 95 void SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext); 96 std::shared_ptr<DmAuthResponseContext> GetResponseContext(); 97 std::shared_ptr<DmAuthRequestContext> GetRequestContext(); 98 std::string CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen); 99 void CreateResponseAuthMessageExt(JsonObject &json); 100 void ParseAuthResponseMessageExt(JsonObject &json); 101 void SetEncryptFlag(bool flag); 102 int32_t SaveSessionKey(const uint8_t *sessionKey, const uint32_t keyLen); 103 int32_t ProcessSessionKey(const uint8_t *sessionKey, const uint32_t keyLen); 104 105 private: 106 std::string CreateRequestAuthMessage(JsonObject &json); 107 void CreateNegotiateMessage(JsonObject &json); 108 void CreateRespNegotiateMessage(JsonObject &json); 109 void CreateSyncGroupMessage(JsonObject &json); 110 void CreateResponseAuthMessage(JsonObject &json); 111 void ParseAuthResponseMessage(JsonObject &json); 112 int32_t ParseAuthRequestMessage(JsonObject &json); 113 void ParseNegotiateMessage(const JsonObject &json); 114 void ParseRespNegotiateMessage(const JsonObject &json); 115 void CreateResponseFinishMessage(JsonObject &json); 116 void ParseResponseFinishMessage(JsonObject &json); 117 void GetAuthReqMessage(JsonObject &json); 118 void ParsePkgNegotiateMessage(const JsonObject &json); 119 void CreatePublicKeyMessageExt(JsonObject &json); 120 void ParsePublicKeyMessageExt(JsonObject &json); 121 void GetJsonObj(JsonObject &jsonObj); 122 void CreateReqReCheckMessage(JsonObject &jsonObj); 123 void ParseReqReCheckMessage(JsonObject &json); 124 bool IsPincodeImported(); 125 126 private: 127 std::weak_ptr<DmAuthManager> authMgr_; 128 std::shared_ptr<ICryptoAdapter> cryptoAdapter_; 129 std::shared_ptr<DmAuthRequestContext> authRequestContext_; 130 std::shared_ptr<DmAuthResponseContext> authResponseContext_; 131 std::mutex encryptFlagMutex_; 132 bool encryptFlag_ = false; 133 std::shared_ptr<CryptoMgr> cryptoMgr_ = nullptr; 134 }; 135 } // namespace DistributedHardware 136 } // namespace OHOS 137 #endif // OHOS_DM_AUTH_MESSAGE_PROCESSOR_H 138