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_NET_ID; 31 extern const char* TAG_TARGET; 32 extern const char* TAG_APP_OPERATION; 33 extern const char* TAG_APP_NAME; 34 extern const char* TAG_APP_DESCRIPTION; 35 extern const char* TAG_GROUPIDS; 36 extern const char* TAG_CUSTOM_DESCRIPTION; 37 extern const char* TAG_DEVICE_TYPE; 38 extern const char* TAG_REQUESTER; 39 extern const char* TAG_LOCAL_DEVICE_TYPE; 40 extern const char* TAG_INDEX; 41 extern const char* TAG_SLICE_NUM; 42 extern const char* TAG_IS_AUTH_CODE_READY; 43 extern const char* TAG_IS_SHOW_DIALOG; 44 extern const char* TAG_TOKEN; 45 extern const char* TAG_CRYPTO_NAME; 46 extern const char* TAG_CRYPTO_VERSION; 47 extern const char* QR_CODE_KEY; 48 extern const char* TAG_AUTH_TOKEN; 49 extern const char* NFC_CODE_KEY; 50 extern const char* OLD_VERSION_ACCOUNT; 51 52 extern const char* TAG_PUBLICKEY; 53 extern const char* TAG_SESSIONKEY; 54 extern const char* TAG_BIND_TYPE_SIZE; 55 extern const char* TAG_HOST_PKGNAME; 56 extern const char* TAG_HAVECREDENTIAL; 57 extern const char* TAG_CONFIRM_OPERATION; 58 extern const char* TAG_IMPORT_AUTH_CODE; 59 extern const char* TAG_CRYPTIC_MSG; 60 extern const char* TAG_SESSIONKEY_ID; 61 62 class DmAuthManager; 63 struct DmAuthRequestContext; 64 struct DmAuthResponseContext; 65 class ICryptoAdapter; 66 class AuthMessageProcessor { 67 public: 68 explicit AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr); 69 ~AuthMessageProcessor(); 70 std::vector<std::string> CreateAuthRequestMessage(); 71 std::string CreateSimpleMessage(int32_t msgType); 72 int32_t ParseMessage(const std::string &message); 73 void SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext); 74 void SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext); 75 std::shared_ptr<DmAuthResponseContext> GetResponseContext(); 76 std::shared_ptr<DmAuthRequestContext> GetRequestContext(); 77 std::string CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen); 78 void CreateResponseAuthMessageExt(JsonObject &json); 79 void ParseAuthResponseMessageExt(JsonObject &json); 80 void SetEncryptFlag(bool flag); 81 int32_t SaveSessionKey(const uint8_t *sessionKey, const uint32_t keyLen); 82 int32_t ProcessSessionKey(const uint8_t *sessionKey, const uint32_t keyLen); 83 84 private: 85 std::string CreateRequestAuthMessage(JsonObject &json); 86 void CreateNegotiateMessage(JsonObject &json); 87 void CreateRespNegotiateMessage(JsonObject &json); 88 void CreateSyncGroupMessage(JsonObject &json); 89 void CreateResponseAuthMessage(JsonObject &json); 90 void ParseAuthResponseMessage(JsonObject &json); 91 int32_t ParseAuthRequestMessage(JsonObject &json); 92 void ParseNegotiateMessage(const JsonObject &json); 93 void ParseRespNegotiateMessage(const JsonObject &json); 94 void CreateResponseFinishMessage(JsonObject &json); 95 void ParseResponseFinishMessage(JsonObject &json); 96 void GetAuthReqMessage(JsonObject &json); 97 void ParsePkgNegotiateMessage(const JsonObject &json); 98 void CreatePublicKeyMessageExt(JsonObject &json); 99 void ParsePublicKeyMessageExt(JsonObject &json); 100 void GetJsonObj(JsonObject &jsonObj); 101 void CreateReqReCheckMessage(JsonObject &jsonObj); 102 void ParseReqReCheckMessage(JsonObject &json); 103 bool IsPincodeImported(); 104 105 private: 106 std::weak_ptr<DmAuthManager> authMgr_; 107 std::shared_ptr<ICryptoAdapter> cryptoAdapter_; 108 std::shared_ptr<DmAuthRequestContext> authRequestContext_; 109 std::shared_ptr<DmAuthResponseContext> authResponseContext_; 110 std::mutex encryptFlagMutex_; 111 bool encryptFlag_ = false; 112 std::shared_ptr<CryptoMgr> cryptoMgr_ = nullptr; 113 }; 114 } // namespace DistributedHardware 115 } // namespace OHOS 116 #endif // OHOS_DM_AUTH_MESSAGE_PROCESSOR_H 117