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