• 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 "sms_wap_push_handler.h"
17 
18 #include <memory>
19 
20 #include "common_event.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "mms_msg.h"
24 #include "securec.h"
25 #include "sms_broadcast_subscriber_receiver.h"
26 #include "sms_hisysevent.h"
27 #include "sms_persist_helper.h"
28 #include "string_utils.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_permission.h"
31 #include "want.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 using namespace OHOS::EventFwk;
36 static constexpr uint8_t PDU_TYPE_PUSH = 0x06;
37 static constexpr uint8_t PDU_TYPE_CONFIRMED_PUSH = 0x07;
38 static constexpr uint32_t PARAMETER_X_WAP_APPLICATION_ID = 0x2F;
39 static constexpr const char *CONTENT_MIME_TYPE_B_PUSH_CO = "application/vnd.wap.coc";
40 
SmsWapPushHandler(int32_t slotId)41 SmsWapPushHandler::SmsWapPushHandler(int32_t slotId) : slotId_(slotId) {}
42 
~SmsWapPushHandler()43 SmsWapPushHandler::~SmsWapPushHandler() {}
44 
DecodeWapPushPduData(SmsWapPushBuffer & decodeBuffer,uint32_t startPos,uint32_t len)45 bool SmsWapPushHandler::DecodeWapPushPduData(SmsWapPushBuffer &decodeBuffer, uint32_t startPos, uint32_t len)
46 {
47     uint32_t headerLength = len;
48     uint32_t startHeader = startPos;
49 
50     std::unique_ptr<char[]> headerBuffer = nullptr;
51     headerBuffer = decodeBuffer.ReadDataBuffer(startHeader, headerLength);
52     if (headerBuffer == nullptr) {
53         TELEPHONY_LOGE("Read Header Buffer nullptr error");
54         return false;
55     }
56     hexHeaderData_ = StringUtils::StringToHex(static_cast<char *>(headerBuffer.get()), headerLength);
57 
58     uint32_t posData = 0;
59     uint32_t dataLength = 0;
60     if (contentType_.GetContentType() == std::string(CONTENT_MIME_TYPE_B_PUSH_CO)) {
61         dataLength = decodeBuffer.GetSize();
62     } else {
63         dataLength = decodeBuffer.GetSize() - startHeader - headerLength;
64         posData = startHeader + headerLength;
65     }
66     std::unique_ptr<char[]> pduBuffer = nullptr;
67     pduBuffer = decodeBuffer.ReadDataBuffer(posData, dataLength);
68     if (pduBuffer == nullptr) {
69         TELEPHONY_LOGE("Read Pdu Buffer nullptr error");
70         return false;
71     }
72     hexWbXmlData_ = StringUtils::StringToHex(static_cast<char *>(pduBuffer.get()), dataLength);
73     return true;
74 }
75 /*
76  * wap-230-wsp-20010705-a 8.2.4.1 Push and ConfirmedPush
77  */
DecodeWapPushPdu(std::shared_ptr<SmsReceiveIndexer> indexer,std::string & wapPdu)78 bool SmsWapPushHandler::DecodeWapPushPdu(std::shared_ptr<SmsReceiveIndexer> indexer, std::string &wapPdu)
79 {
80     if (indexer == nullptr) {
81         TELEPHONY_LOGE("indexer is nullptr");
82         return false;
83     }
84 
85     SmsWapPushBuffer decodeBuffer;
86     if (!decodeBuffer.WriteRawStringBuffer(wapPdu)) {
87         TELEPHONY_LOGE("Wap push WriteRawStringBuffer fail.");
88         return false;
89     }
90     if (!DecodePushType(decodeBuffer)) {
91         TELEPHONY_LOGE("Wap push DecodePushType fail.");
92         return false;
93     }
94 
95     uint32_t count = 0;
96     uint32_t headerLength = 0;
97     if (!decodeBuffer.DecodeUintvar(headerLength, count)) {
98         TELEPHONY_LOGE("Wap push DecodeUintvar fail.");
99         return false;
100     }
101 
102     int32_t contentTypeLength = 0;
103     uint32_t startHeader = decodeBuffer.GetCurPosition();
104     if (!contentType_.DecodeContentType(decodeBuffer, contentTypeLength)) {
105         TELEPHONY_LOGE("Wap push DecodeContentType fail.");
106         return false;
107     }
108     uint32_t headersLen = 0;
109     uint32_t curentPosition = decodeBuffer.GetCurPosition();
110     if (headerLength + startHeader <= curentPosition) {
111         TELEPHONY_LOGE("Wap push headersLen fail.");
112         return false;
113     }
114     headersLen = headerLength - curentPosition + startHeader;
115     DecodeXWapApplication(decodeBuffer, headersLen);
116 
117     if (!DecodeWapPushPduData(decodeBuffer, startHeader, headerLength)) {
118         TELEPHONY_LOGE("Wap push DecodeWapPushPduData fail.");
119         return false;
120     }
121 
122     if (DeocdeCheckIsBlock(hexWbXmlData_)) {
123         TELEPHONY_LOGI("Wap Push Mms-message Is Blocked Dispatcher.");
124         return true;
125     }
126     SendWapPushMessageBroadcast(indexer);
127     return true;
128 }
129 
130 /*
131  * wap-230-wsp-20010705-a 8.2.4.1 Push and ConfirmedPush
132  * 8.2.4 Push and Confirmed Push Facilities
133  */
DecodePushType(SmsWapPushBuffer & decodeBuffer)134 bool SmsWapPushHandler::DecodePushType(SmsWapPushBuffer &decodeBuffer)
135 {
136     transactionId_ = 0;
137     if (!decodeBuffer.GetOneByte(transactionId_)) {
138         TELEPHONY_LOGE("Decode Transaction Id error.");
139         return false;
140     }
141     pushType_ = 0;
142     if (!decodeBuffer.GetOneByte(pushType_)) {
143         TELEPHONY_LOGE("Decode PushType Error.");
144         return false;
145     }
146     /** 8.2.4 Push and Confirmed Push Facilities **/
147     if (pushType_ != PDU_TYPE_PUSH && pushType_ != PDU_TYPE_CONFIRMED_PUSH) {
148         TELEPHONY_LOGE("unSupported this pushType:%{public}d", pushType_);
149         return false;
150     }
151     return true;
152 }
153 
154 /**
155  * @brief DeocdeCheckIsBlock
156  * Check Block From Address From Contact BataBase
157  * @param pdus [in]
158  * @param len [in]
159  * @return true
160  * @return false
161  */
DeocdeCheckIsBlock(std::string & hexData)162 bool SmsWapPushHandler::DeocdeCheckIsBlock(std::string &hexData)
163 {
164     const uint8_t mmsNotificationInd = 130;
165 
166     std::string pdustr = StringUtils::HexToString(hexData);
167     uint32_t pduLen = pdustr.length();
168 
169     std::unique_ptr<char[]> pdus = std::make_unique<char[]>(pduLen);
170     if (pdus == nullptr || pduLen == 0) {
171         TELEPHONY_LOGE("pdu buffer data param error");
172         return false;
173     }
174     if (memcpy_s(pdus.get(), pduLen, pdustr.data(), pduLen) != EOK) {
175         TELEPHONY_LOGE("Memcpy_s DeocdeCheckIsBlock Error.");
176         return false;
177     }
178 
179     MmsMsg mmsMsg;
180     bool result = mmsMsg.DecodeMsg(std::move(pdus), pduLen);
181     if (result && (mmsMsg.GetMmsMessageType() == mmsNotificationInd)) {
182         mmsMsg.DumpMms();
183         MmsAddress fromAddress = mmsMsg.GetMmsFrom();
184         auto helper = DelayedSingleton<SmsPersistHelper>::GetInstance();
185         if (helper == nullptr) {
186             TELEPHONY_LOGE("SmsPersist Helper nullptr error");
187             return false;
188         }
189         std::string address = fromAddress.GetAddressString();
190         std::size_t pos = address.find('/');
191         if (pos != std::string::npos) {
192             return helper->QueryBlockPhoneNumber(address.substr(pos));
193         }
194     }
195     TELEPHONY_LOGI("wap push decode is block.");
196     return false;
197 }
198 
199 /**
200  * @brief DecodeXwapApplication
201  * WAP-251-PushMessage-20010322-a    5.2.1 5.2.2. WAP Headers
202  * @param decodeBuffer [in]
203  * @param headersLen [in]
204  * @return true
205  * @return false
206  */
DecodeXWapApplication(SmsWapPushBuffer & decodeBuffer,uint32_t headersLen)207 bool SmsWapPushHandler::DecodeXWapApplication(SmsWapPushBuffer &decodeBuffer, uint32_t headersLen)
208 {
209     std::unique_ptr<char[]> tempHeadersBuffer = nullptr;
210     tempHeadersBuffer = decodeBuffer.ReadDataBuffer(headersLen);
211     if (headersLen > 0 && tempHeadersBuffer != nullptr) {
212         SmsWapPushBuffer tempXWapDataBuffer;
213         if (!tempXWapDataBuffer.WriteDataBuffer(std::move(tempHeadersBuffer), headersLen)) {
214             TELEPHONY_LOGE("Wap push WriteDataBuffer fail.");
215             return false;
216         }
217         decodeBuffer.IncreasePointer(headersLen);
218         return DecodeXWapApplicationField(tempXWapDataBuffer, strAppId_);
219     }
220     return false;
221 }
222 
223 /**
224  * @brief DecodeXWapApplicationField
225  * WAP-251-PushMessage-20010322-a    5.2.1 5.2.2. WAP Headers
226  * @param decodeBuffer [in]
227  * @param strWapAppId [out]
228  * @return true
229  * @return false
230  */
DecodeXWapApplicationField(SmsWapPushBuffer & decodeBuffer,std::string & strWapAppId)231 bool SmsWapPushHandler::DecodeXWapApplicationField(SmsWapPushBuffer &decodeBuffer, std::string &strWapAppId)
232 {
233     while (decodeBuffer.GetCurPosition() < decodeBuffer.GetSize()) {
234         uint64_t fieldValue = 0;
235         if (!decodeBuffer.DecodeInteger(fieldValue)) {
236             TELEPHONY_LOGE("Wap push DecodeInteger fail.");
237             return false;
238         }
239         if (fieldValue == PARAMETER_X_WAP_APPLICATION_ID) {
240             return DecodeXWapApplicationValue(decodeBuffer, strWapAppId);
241         } else {
242             DecodeXWapAbandonHeaderValue(decodeBuffer);
243         }
244     }
245     return false;
246 }
247 
248 /*
249  * wap-230-wsp-20010705-a
250  * 8.4.2.54 X-Wap-Application-Id field
251  * The following rule is used to encode the X-Wap-Application-Id field.
252  * Application-id-value = Uri-value | App-assigned-code
253  * App-assigned-code = Integer-value
254  */
DecodeXWapApplicationValue(SmsWapPushBuffer & decodeBuffer,std::string & strWapAppId)255 bool SmsWapPushHandler::DecodeXWapApplicationValue(SmsWapPushBuffer &decodeBuffer, std::string &strWapAppId)
256 {
257     uint64_t appIdValue = 0;
258     if (decodeBuffer.DecodeInteger(appIdValue)) {
259         return true;
260     }
261     uint32_t len = 0;
262     if (!decodeBuffer.DecodeText(strWapAppId, len)) {
263         TELEPHONY_LOGE("Wap push DecodeText fail.");
264         return false;
265     }
266     return true;
267 }
268 
269 /*
270  * wap-230-wsp-20010705-a
271  * 8.4.1.2 Field values, We abancon beyond that X-Wap-Application-Id
272  * Value Interpretation of First Octet
273  * 0 - 30 This octet is followed by the indicated number (0 –30) of data octets
274  * 31 This octet is followed by a uintvar, which indicates the number of data octets after it
275  * 32 - 127 The value is a text string, terminated by a zero octet (NUL character)
276  * 128 - 255 It is an encoded 7-bit value; this header has no more data
277  */
DecodeXWapAbandonHeaderValue(SmsWapPushBuffer & decodeBuffer)278 bool SmsWapPushHandler::DecodeXWapAbandonHeaderValue(SmsWapPushBuffer &decodeBuffer)
279 {
280     const uint8_t wapShortLengthMax = 30;
281     const uint8_t wapLengthQuote = 31;
282     const uint8_t textLengthMax = 127;
283 
284     uint8_t oneByte = 0;
285     if (!decodeBuffer.GetOneByte(oneByte)) {
286         TELEPHONY_LOGE("Wap push GetOneByte fail.");
287         return false;
288     }
289 
290     if ((oneByte >= 0) && (oneByte <= wapShortLengthMax)) {
291         if (!decodeBuffer.IncreasePointer(oneByte)) {
292             TELEPHONY_LOGE("Wap push IncreasePointer fail.");
293             return false;
294         }
295     } else if (oneByte == wapLengthQuote) {
296         uint32_t length = 0;
297         uint32_t count = 0;
298         if (!decodeBuffer.DecodeUintvar(length, count)) {
299             TELEPHONY_LOGE("Wap push DecodeUintvar fail.");
300             return false;
301         }
302         if (!decodeBuffer.IncreasePointer(length)) {
303             TELEPHONY_LOGE("Wap push IncreasePointer fail.");
304             return false;
305         }
306     } else if ((wapLengthQuote < oneByte) && (oneByte <= textLengthMax)) {
307         std::string strTemp = "";
308         uint32_t length = 0;
309         if (!decodeBuffer.DecodeText(strTemp, length)) {
310             TELEPHONY_LOGE("Wap push DecodeText fail.");
311             return false;
312         }
313     }
314     return true;
315 }
316 
SendWapPushMessageBroadcast(std::shared_ptr<SmsReceiveIndexer> indexer)317 bool SmsWapPushHandler::SendWapPushMessageBroadcast(std::shared_ptr<SmsReceiveIndexer> indexer)
318 {
319     if (indexer == nullptr) {
320         TELEPHONY_LOGE("indexer is nullptr");
321         return false;
322     }
323 
324     EventFwk::Want want;
325     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED);
326 
327     want.SetParam("slotId", static_cast<int>(slotId_));
328     want.SetParam("pushType", static_cast<int>(pushType_));
329     want.SetParam("applicationId", strAppId_);
330     want.SetParam("transactionId", static_cast<int>(transactionId_));
331     want.SetParam("contentType", contentType_.GetContentType());
332     want.SetParam("headerData", hexHeaderData_);
333     want.SetParam("rawData", hexWbXmlData_);
334 
335     EventFwk::CommonEventData data;
336     data.SetWant(want);
337     data.SetData("Sms WapPush Message");
338     data.SetCode(0);
339     EventFwk::CommonEventPublishInfo publishInfo;
340     publishInfo.SetOrdered(true);
341     std::vector<std::string> wappushPermissions;
342     wappushPermissions.emplace_back(Permission::RECEIVE_MMS);
343     publishInfo.SetSubscriberPermissions(wappushPermissions);
344 
345     MatchingSkills smsSkills;
346     smsSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED);
347     CommonEventSubscribeInfo smsSubscriberInfo(smsSkills);
348     smsSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
349     auto handler = std::make_shared<SmsReceiveReliabilityHandler>(slotId_);
350     auto wapPushReceiver = std::make_shared<SmsBroadcastSubscriberReceiver>(
351         smsSubscriberInfo, handler, indexer->GetMsgRefId(), indexer->GetDataBaseId());
352     bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, wapPushReceiver);
353     HiSysEventWapPushResult(publishResult);
354     return true;
355 }
356 
HiSysEventWapPushResult(bool publishResult)357 void SmsWapPushHandler::HiSysEventWapPushResult(bool publishResult)
358 {
359     if (!publishResult) {
360         TELEPHONY_LOGE("SendBroadcast PublishBroadcastEvent result fail");
361         SmsHiSysEvent::WriteSmsReceiveFaultEvent(slotId_, SmsMmsMessageType::WAP_PUSH,
362             SmsMmsErrorCode::SMS_ERROR_PUBLISH_COMMON_EVENT_FAIL, "publish wpa push broadcast event fail");
363     }
364     DelayedSingleton<SmsHiSysEvent>::GetInstance()->SetWapPushBroadcastStartTime();
365 }
366 } // namespace Telephony
367 } // namespace OHOS
368