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 char *StreamAdaptor::GetSessionKey()
47 {
48 return sessionKey_.c_str();
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 sessionKey_ = std::string(param->sessionKey, SESSION_KEY_LENGTH);
85 callback_ = callback;
86 streamType_ = param->type;
87 channelId_ = channelId;
88 }
89
ReleaseAdaptor()90 void StreamAdaptor::ReleaseAdaptor()
91 {
92 streamManager_->DestroyStreamDataChannel();
93 streamManager_->DestroyEnvironment(pkgName_);
94 channelId_ = -1;
95
96 size_t len = sessionKey_.length();
97 if (len != 0) {
98 sessionKey_.replace(0, len, len, '\0');
99 }
100 }
101
Encrypt(const void * in,ssize_t inLen,void * out,ssize_t outLen,const char * sessionKey)102 ssize_t StreamAdaptor::Encrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen, const char* sessionKey)
103 {
104 AesGcmCipherKey cipherKey = {0};
105
106 if (inLen - OVERHEAD_LEN > outLen) {
107 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Encrypt invalid para.");
108 return SOFTBUS_ERR;
109 }
110
111 cipherKey.keyLen = SESSION_KEY_LENGTH;
112 if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey, SESSION_KEY_LENGTH) != EOK) {
113 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memcpy key error.");
114 return SOFTBUS_ERR;
115 }
116
117 int ret = SoftBusEncryptData(&cipherKey, (unsigned char *)in, inLen, (unsigned char *)out, (unsigned int *)&outLen);
118 (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
119 if (ret != SOFTBUS_OK || outLen != inLen + OVERHEAD_LEN) {
120 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Encrypt Data fail. %d", ret);
121 return SOFTBUS_ENCRYPT_ERR;
122 }
123
124 return outLen;
125 }
126
Decrypt(const void * in,ssize_t inLen,void * out,ssize_t outLen,const char * sessionKey)127 ssize_t StreamAdaptor::Decrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen, const char *sessionKey)
128 {
129 AesGcmCipherKey cipherKey = {0};
130
131 if (inLen - OVERHEAD_LEN > outLen) {
132 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Decrypt invalid para.");
133 return SOFTBUS_ERR;
134 }
135
136 cipherKey.keyLen = SESSION_KEY_LENGTH; // 256 bit encryption
137 if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey, SESSION_KEY_LENGTH) != EOK) {
138 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memcpy key error.");
139 return SOFTBUS_ERR;
140 }
141 int ret = SoftBusDecryptData(&cipherKey, (unsigned char *)in, inLen, (unsigned char *)out, (unsigned int *)&outLen);
142 (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
143 if (ret != SOFTBUS_OK) {
144 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Decrypt Data fail. %d ", ret);
145 return SOFTBUS_DECRYPT_ERR;
146 }
147
148 return outLen;
149 }
150