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