1 /*
2  * Copyright (c) 2021-2024 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 #include "stream_adaptor.h"
17 
18 #include <map>
19 #include <mutex>
20 #include <string>
21 
22 #include "client_trans_udp_stream_interface.h"
23 #include "securec.h"
24 #include "softbus_adapter_crypto.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "stream_adaptor_listener.h"
28 #include "trans_log.h"
29 
30 using namespace OHOS;
31 
StreamAdaptor(const std::string & pkgName)32 StreamAdaptor::StreamAdaptor(const std::string &pkgName) : pkgName_(pkgName)
33 {
34     serverSide_ = false;
35 }
36 
GetEncryptOverhead()37 ssize_t StreamAdaptor::GetEncryptOverhead()
38 {
39     return OVERHEAD_LEN;
40 }
41 
GetStreamType()42 int StreamAdaptor::GetStreamType()
43 {
44     return streamType_;
45 }
46 
GetSessionKey()47 const std::pair<uint8_t*, uint32_t> StreamAdaptor::GetSessionKey()
48 {
49     return sessionKey_;
50 }
51 
GetChannelId()52 int64_t StreamAdaptor::GetChannelId()
53 {
54     return channelId_;
55 }
56 
GetListenerCallback()57 const IStreamListener *StreamAdaptor::GetListenerCallback()
58 {
59     return callback_;
60 }
61 
GetStreamManager()62 std::shared_ptr<Communication::SoftBus::IStreamManager> StreamAdaptor::GetStreamManager()
63 {
64     return streamManager_;
65 }
66 
GetAliveState()67 bool StreamAdaptor::GetAliveState()
68 {
69     return aliveState_;
70 }
71 
SetAliveState(bool state)72 void StreamAdaptor::SetAliveState(bool state)
73 {
74     aliveState_.exchange(state);
75 }
76 
InitAdaptor(int32_t channelId,const VtpStreamOpenParam * param,bool isServerSide,const IStreamListener * callback)77 void StreamAdaptor::InitAdaptor(int32_t channelId, const VtpStreamOpenParam *param, bool isServerSide,
78     const IStreamListener *callback)
79 {
80     if (param == nullptr) {
81         TRANS_LOGE(TRANS_STREAM, "param invalid");
82         return;
83     }
84     auto adaptor = shared_from_this();
85     auto adaptorListener = std::make_shared<StreamAdaptorListener>(adaptor);
86     streamManager_ =  Communication::SoftBus::IStreamManager::GetInstance(nullptr, adaptorListener);
87     streamManager_->PrepareEnvironment(param->pkgName);
88     serverSide_ = isServerSide;
89     if (sessionKey_.first == nullptr) {
90         sessionKey_.first = new uint8_t[param->keyLen];
91     }
92     if (memcpy_s(sessionKey_.first, param->keyLen, param->sessionKey, param->keyLen) != EOK) {
93         TRANS_LOGE(TRANS_STREAM, "memcpy key error.");
94         return;
95     }
96 
97     sessionKey_.second = param->keyLen;
98     callback_ = callback;
99     streamType_ = param->type;
100     channelId_ = channelId;
101     isRawStreamEncrypt_ = param->isRawStreamEncrypt;
102 }
103 
ReleaseAdaptor()104 void StreamAdaptor::ReleaseAdaptor()
105 {
106     streamManager_->DestroyStreamDataChannel();
107     streamManager_->DestroyEnvironment(pkgName_);
108     channelId_ = -1;
109     if (sessionKey_.first != nullptr) {
110         (void)memset_s(sessionKey_.first, sessionKey_.second, 0, sessionKey_.second);
111         delete [] sessionKey_.first;
112     }
113     sessionKey_.first = nullptr;
114 }
115 
Encrypt(const void * in,ssize_t inLen,void * out,ssize_t outLen,std::pair<uint8_t *,uint32_t> sessionKey)116 ssize_t StreamAdaptor::Encrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen,
117     std::pair<uint8_t*, uint32_t> sessionKey)
118 {
119     AesGcmCipherKey cipherKey = {0};
120 
121     if (inLen - OVERHEAD_LEN > outLen) {
122         TRANS_LOGE(TRANS_STREAM, "Encrypt invalid para.");
123         return SOFTBUS_INVALID_PARAM;
124     }
125 
126     cipherKey.keyLen = SESSION_KEY_LENGTH;
127     if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey.first, sessionKey.second) != EOK) {
128         TRANS_LOGE(TRANS_STREAM, "memcpy key error.");
129         return SOFTBUS_MEM_ERR;
130     }
131 
132     int ret = SoftBusEncryptData(&cipherKey, reinterpret_cast<const unsigned char *>(in), inLen,
133         reinterpret_cast<unsigned char *>(out), reinterpret_cast<unsigned int *>(&outLen));
134     (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
135     if (ret != SOFTBUS_OK || outLen != inLen + OVERHEAD_LEN) {
136         TRANS_LOGE(TRANS_STREAM, "Encrypt Data fail. ret=%{public}d", ret);
137         return SOFTBUS_ENCRYPT_ERR;
138     }
139 
140     return outLen;
141 }
142 
Decrypt(const void * in,ssize_t inLen,void * out,ssize_t outLen,std::pair<uint8_t *,uint32_t> sessionKey)143 ssize_t StreamAdaptor::Decrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen,
144     std::pair<uint8_t*, uint32_t> sessionKey)
145 {
146     AesGcmCipherKey cipherKey = {0};
147 
148     if (inLen - OVERHEAD_LEN > outLen) {
149         TRANS_LOGE(TRANS_STREAM, "Decrypt invalid para.");
150         return SOFTBUS_INVALID_PARAM;
151     }
152 
153     cipherKey.keyLen = SESSION_KEY_LENGTH; // 256 bit encryption
154     if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey.first, sessionKey.second) != EOK) {
155         TRANS_LOGE(TRANS_STREAM, "memcpy key error.");
156         return SOFTBUS_MEM_ERR;
157     }
158     int ret = SoftBusDecryptData(&cipherKey, reinterpret_cast<const unsigned char *>(in), inLen,
159         reinterpret_cast<unsigned char *>(out), reinterpret_cast<unsigned int *>(&outLen));
160     (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
161     if (ret != SOFTBUS_OK) {
162         TRANS_LOGE(TRANS_STREAM, "Decrypt Data fail. ret=%{public}d ", ret);
163         return SOFTBUS_DECRYPT_ERR;
164     }
165 
166     return outLen;
167 }
168 
IsEncryptedRawStream()169 bool StreamAdaptor::IsEncryptedRawStream()
170 {
171     // This option only applies to raw stream data
172     return isRawStreamEncrypt_;
173 }