• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool DaemonSSLHandshake(HSession hSession, const uint32_t channelId, SessionHandShake &handshake);
89 // deprecated, remove later
90 #ifdef HDC_SUPPORT_FLASHD
91 // null
92 #else
93     void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override;
94 #endif
95 
96     bool HandDaemonAuthBypass(void);
97     void SendAuthSignMsg(SessionHandShake &handshake,
98             uint32_t channelId, uint32_t sessionid, string pubkey, string token);
99     void SendAuthMsg(SessionHandShake &handshake, const uint32_t channelId,
100             HSession &hSession, string pubkey);
101     void SendAuthEncryptPsk(SessionHandShake &handshake, const uint32_t channelid,
102             HSession &hSession, string pubkey);
103     void SendAuthOkMsg(SessionHandShake &handshake, uint32_t channelid,
104                        uint32_t sessionid, string msg = "", string daemonAuthResult = DAEOMN_AUTH_SUCCESS);
105     void AuthRejectLowClient(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid);
106     void EchoHandshakeMsg(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid, string msg);
107     bool AuthVerify(HSession hSession, const string &encryptToken, const string &token, const string &pubkey);
108     bool AuthVerifyRsaSign(HSession hSession, const string &tokenSignBase64, const string &token, RSA *rsa);
109     bool RsaSignVerify(HSession hSession, EVP_PKEY_CTX *ctx, const string &tokenSignBase64, const string &token);
110     bool AuthVerifyRsa(HSession hSession, const string &encryptToken, const string &token, RSA *rsa);
111     void InitSessionAuthInfo(uint32_t sessionid, string token);
112     void UpdateSessionAuthOk(uint32_t sessionid);
113     void UpdateSessionAuthmsg(uint32_t sessionid, string authmsg);
114     void UpdateSessionAuthPubkey(uint32_t sessionid, string pubkey);
115     void DeleteSessionAuthStatus(uint32_t sessionid);
116     AuthType GetSessionAuthStatus(uint32_t sessionid);
117     string GetSessionAuthmsg(uint32_t sessionid);
118     string GetSessionAuthToken(uint32_t sessionid);
119     string GetSessionAuthPubkey(uint32_t sessionid);
120     bool GetAuthByPassValue();
121     bool CheckAuthStatus(HSession hSession, const uint32_t channelId, const uint16_t command);
122     std::map<uint32_t, HdcDaemonAuthInfo> mapAuthStatus;
123     std::mutex mapAuthStatusMutex;
124     bool authEnable;
125 };
126 }  // namespace Hdc
127 #endif
128