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 UpdateKnownHosts(const string& key); 77 void ClearInstanceResource() override; 78 void DaemonSessionHandshakeInit(HSession &hSession, SessionHandShake &handshake); 79 void GetServerCapability(HSession &hSession, SessionHandShake &handshake); 80 bool DaemonSessionHandshake(HSession hSession, const uint32_t channelId, uint8_t *payload, int payloadSize); 81 void TryStopInstance(); 82 UserPermit PostUIConfirm(string hostname, string pubkey); 83 bool ShowPermitDialog(); 84 bool HandDaemonAuthInit(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 85 bool HandDaemonAuthPubkey(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 86 bool HandDaemonAuthSignature(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 87 // deprecated, remove later 88 #ifdef HDC_SUPPORT_FLASHD 89 // null 90 #else 91 void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override; 92 #endif 93 94 bool HandDaemonAuthBypass(void); 95 void SendAuthSignMsg(SessionHandShake &handshake, 96 uint32_t channelId, uint32_t sessionid, string pubkey, string token); 97 void SendAuthOkMsg(SessionHandShake &handshake, uint32_t channelid, 98 uint32_t sessionid, string msg = "", string daemonAuthResult = DAEOMN_AUTH_SUCCESS); 99 void AuthRejectLowClient(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid); 100 void EchoHandshakeMsg(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid, string msg); 101 bool AuthVerify(HSession hSession, const string &encryptToken, const string &token, const string &pubkey); 102 bool AuthVerifyRsaSign(HSession hSession, const string &tokenSignBase64, const string &token, RSA *rsa); 103 bool RsaSignVerify(HSession hSession, EVP_PKEY_CTX *ctx, const string &tokenSignBase64, const string &token); 104 bool AuthVerifyRsa(HSession hSession, const string &encryptToken, const string &token, RSA *rsa); 105 void InitSessionAuthInfo(uint32_t sessionid, string token); 106 void UpdateSessionAuthOk(uint32_t sessionid); 107 void UpdateSessionAuthmsg(uint32_t sessionid, string authmsg); 108 void UpdateSessionAuthPubkey(uint32_t sessionid, string pubkey); 109 void DeleteSessionAuthStatus(uint32_t sessionid); 110 AuthType GetSessionAuthStatus(uint32_t sessionid); 111 string GetSessionAuthmsg(uint32_t sessionid); 112 string GetSessionAuthToken(uint32_t sessionid); 113 string GetSessionAuthPubkey(uint32_t sessionid); 114 std::map<uint32_t, HdcDaemonAuthInfo> mapAuthStatus; 115 std::mutex mapAuthStatusMutex; 116 bool enableSecure; 117 }; 118 } // namespace Hdc 119 #endif 120