1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_DAEMON_H 16 #define HDC_DAEMON_H 17 #include "daemon_common.h" 18 #include "openssl/pem.h" 19 20 namespace Hdc { 21 enum UserPermit { 22 REFUSE = 0, 23 ALLOWONCE = 1, 24 ALLOWFORVER = 2, 25 }; 26 struct HdcDaemonAuthInfo { 27 HdcSessionBase::AuthType authtype; 28 string token; 29 string pubkey; 30 string authmsg; 31 }; 32 class HdcDaemon : public HdcSessionBase { 33 public: 34 #ifdef USE_CONFIG_UV_THREADS 35 HdcDaemon(bool serverOrDaemonIn, size_t uvThreadSize = SIZE_THREAD_POOL); 36 #else 37 HdcDaemon(bool serverOrDaemonIn); 38 #endif 39 virtual ~HdcDaemon(); 40 #ifdef HDC_SUPPORT_UART 41 #ifdef HDC_EMULATOR 42 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableBridge, bool bEnableUART); 43 #endif 44 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableUART); 45 #else 46 #ifdef HDC_EMULATOR 47 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableBridge); 48 #endif 49 void InitMod(bool bEnableTCP, bool bEnableUSB); 50 #endif 51 bool FetchCommand(HSession hSession, const uint32_t channelId, const uint16_t command, uint8_t *payload, 52 const int payloadSize) override; 53 bool ServerCommand(const uint32_t sessionId, const uint32_t channelId, const uint16_t command, uint8_t *bufPtr, 54 const int size) override; 55 void ClearKnownHosts(); 56 void *clsTCPServ; 57 void *clsUSBServ; 58 #ifdef HDC_EMULATOR 59 void *clsBridgeServ; 60 #endif 61 #ifdef HDC_SUPPORT_UART 62 void *clsUARTServ; 63 #endif 64 void *clsJdwp; 65 66 private: 67 bool RemoveInstanceTask(const uint8_t op, HTaskInfo hTask) override; 68 bool RedirectToTask(HTaskInfo hTaskInfo, HSession hSession, const uint32_t channelId, const uint16_t command, 69 uint8_t *payload, const int payloadSize) override; 70 void JdwpNewFileDescriptor(const uint8_t *buf, const int bytesIO) override; 71 static bool CheckControl(const uint16_t command); 72 static bool IsExpectedParam(const std::string& param, const std::string& expect); 73 bool HandDaemonAuth(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 74 bool GetHostPubkeyInfo(const string& buf, string& hostname, string& pubkey); 75 bool AlreadyInKnownHosts(const string& key); 76 void AddFeatureTagToEmgmsg(string& emgmsg); 77 void UpdateKnownHosts(const string& key); 78 void ClearInstanceResource() override; 79 void DaemonSessionHandshakeInit(HSession &hSession, SessionHandShake &handshake); 80 void GetServerCapability(HSession &hSession, SessionHandShake &handshake); 81 bool DaemonSessionHandshake(HSession hSession, const uint32_t channelId, uint8_t *payload, int payloadSize); 82 void TryStopInstance(); 83 UserPermit PostUIConfirm(string hostname, string pubkey); 84 bool ShowPermitDialog(); 85 bool HandDaemonAuthInit(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 86 bool HandDaemonAuthPubkey(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 87 bool HandDaemonAuthSignature(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 88 // deprecated, remove later 89 #ifdef HDC_SUPPORT_FLASHD 90 // null 91 #else 92 void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override; 93 #endif 94 95 bool HandDaemonAuthBypass(void); 96 void SendAuthSignMsg(SessionHandShake &handshake, 97 uint32_t channelId, uint32_t sessionid, string pubkey, string token); 98 void SendAuthOkMsg(SessionHandShake &handshake, uint32_t channelid, 99 uint32_t sessionid, string msg = "", string daemonAuthResult = DAEOMN_AUTH_SUCCESS); 100 void AuthRejectLowClient(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid); 101 void EchoHandshakeMsg(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid, string msg); 102 bool AuthVerify(HSession hSession, const string &encryptToken, const string &token, const string &pubkey); 103 bool AuthVerifyRsaSign(HSession hSession, const string &tokenSignBase64, const string &token, RSA *rsa); 104 bool RsaSignVerify(HSession hSession, EVP_PKEY_CTX *ctx, const string &tokenSignBase64, const string &token); 105 bool AuthVerifyRsa(HSession hSession, const string &encryptToken, const string &token, RSA *rsa); 106 void InitSessionAuthInfo(uint32_t sessionid, string token); 107 void UpdateSessionAuthOk(uint32_t sessionid); 108 void UpdateSessionAuthmsg(uint32_t sessionid, string authmsg); 109 void UpdateSessionAuthPubkey(uint32_t sessionid, string pubkey); 110 void DeleteSessionAuthStatus(uint32_t sessionid); 111 AuthType GetSessionAuthStatus(uint32_t sessionid); 112 string GetSessionAuthmsg(uint32_t sessionid); 113 string GetSessionAuthToken(uint32_t sessionid); 114 string GetSessionAuthPubkey(uint32_t sessionid); 115 bool GetAuthByPassValue(); 116 bool CheckAuthStatus(HSession hSession, const uint32_t channelId, const uint16_t command); 117 std::map<uint32_t, HdcDaemonAuthInfo> mapAuthStatus; 118 std::mutex mapAuthStatusMutex; 119 bool authEnable; 120 }; 121 } // namespace Hdc 122 #endif 123