• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "message_parcel_utils.h"
17 
18 #include "edm_log.h"
19 #include "securec.h"
20 
21 namespace OHOS {
22 namespace EDM {
WriteWifiDeviceConfig(Wifi::WifiDeviceConfig & config,MessageParcel & data)23 void MessageParcelUtils::WriteWifiDeviceConfig(Wifi::WifiDeviceConfig &config, MessageParcel &data)
24 {
25     data.WriteInt32(config.networkId);
26     data.WriteInt32(config.status);
27     data.WriteString(config.bssid);
28     data.WriteString(config.ssid);
29     data.WriteInt32(config.band);
30     data.WriteInt32(config.channel);
31     data.WriteInt32(config.frequency);
32     data.WriteInt32(config.level);
33     data.WriteBool(config.isPasspoint);
34     data.WriteBool(config.isEphemeral);
35     data.WriteString(config.preSharedKey);
36     data.WriteString(config.keyMgmt);
37     for (int i = 0; i < WEPKEYS_SIZE; i++) {
38         data.WriteString(config.wepKeys[i]);
39     }
40     data.WriteInt32(config.wepTxKeyIndex);
41     data.WriteInt32(config.priority);
42     data.WriteBool(config.hiddenSSID);
43     data.WriteInt32(static_cast<int32_t>(config.wifiIpConfig.assignMethod));
44     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.ipAddress.address);
45     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.prefixLength);
46     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.flags);
47     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.scope);
48     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.gateway);
49     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer1);
50     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer2);
51     data.WriteString(config.wifiIpConfig.staticIpAddress.domains);
52     data.WriteString(config.wifiEapConfig.eap);
53     data.WriteString(config.wifiEapConfig.identity);
54     data.WriteString(config.wifiEapConfig.password);
55     data.WriteString(config.wifiEapConfig.clientCert);
56     data.WriteString(config.wifiEapConfig.privateKey);
57     data.WriteUInt8Vector(config.wifiEapConfig.certEntry);
58     data.WriteCString(config.wifiEapConfig.certPassword);
59     memset_s(config.wifiEapConfig.certPassword, sizeof(config.wifiEapConfig.certPassword), 0,
60         sizeof(config.wifiEapConfig.certPassword));
61     data.WriteInt32(static_cast<int32_t>(config.wifiEapConfig.phase2Method));
62     data.WriteInt32(static_cast<int32_t>(config.wifiProxyconfig.configureMethod));
63     data.WriteString(config.wifiProxyconfig.autoProxyConfig.pacWebAddress);
64     data.WriteString(config.wifiProxyconfig.manualProxyConfig.serverHostName);
65     data.WriteInt32(config.wifiProxyconfig.manualProxyConfig.serverPort);
66     data.WriteString(config.wifiProxyconfig.manualProxyConfig.exclusionObjectList);
67     data.WriteInt32(static_cast<int32_t>(config.wifiPrivacySetting));
68 }
69 
WriteInstallParam(const AppExecFwk::InstallParam & installParam,MessageParcel & data)70 void MessageParcelUtils::WriteInstallParam(const AppExecFwk::InstallParam &installParam, MessageParcel &data)
71 {
72     data.WriteInt32(installParam.userId);
73     data.WriteInt32(static_cast<int>(installParam.installFlag));
74 }
75 
WriteIpAddress(MessageParcel & data,const Wifi::WifiIpAddress & address)76 void MessageParcelUtils::WriteIpAddress(MessageParcel &data, const Wifi::WifiIpAddress &address)
77 {
78     data.WriteInt32(address.family);
79     data.WriteUint32(address.addressIpv4);
80     size_t size = address.addressIpv6.size();
81     data.WriteUint32(size);
82     for (size_t i = 0; i < size; i++) {
83         data.WriteUint8(address.addressIpv6[i]);
84     }
85 }
86 
ReadWifiDeviceConfig(MessageParcel & data,Wifi::WifiDeviceConfig & config)87 void MessageParcelUtils::ReadWifiDeviceConfig(MessageParcel &data, Wifi::WifiDeviceConfig &config)
88 {
89     config.networkId = data.ReadInt32();
90     config.status = data.ReadInt32();
91     config.bssid = data.ReadString();
92     config.ssid = data.ReadString();
93     config.band = data.ReadInt32();
94     config.channel = data.ReadInt32();
95     config.frequency = data.ReadInt32();
96     config.level = data.ReadInt32();
97     config.isPasspoint = data.ReadBool();
98     config.isEphemeral = data.ReadBool();
99     config.preSharedKey = data.ReadString();
100     config.keyMgmt = data.ReadString();
101     for (int i = 0; i < WEPKEYS_SIZE; i++) {
102         config.wepKeys[i] = data.ReadString();
103     }
104     config.wepTxKeyIndex = data.ReadInt32();
105     config.priority = data.ReadInt32();
106     config.hiddenSSID = data.ReadBool();
107     ProcessAssignIpMethod(data.ReadInt32(), config.wifiIpConfig);
108     ReadIpAddress(data, config.wifiIpConfig.staticIpAddress.ipAddress.address);
109     config.wifiIpConfig.staticIpAddress.ipAddress.prefixLength = data.ReadInt32();
110     config.wifiIpConfig.staticIpAddress.ipAddress.flags = data.ReadInt32();
111     config.wifiIpConfig.staticIpAddress.ipAddress.scope = data.ReadInt32();
112     ReadIpAddress(data, config.wifiIpConfig.staticIpAddress.gateway);
113     ReadIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer1);
114     ReadIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer2);
115     config.wifiIpConfig.staticIpAddress.domains = data.ReadString();
116     config.wifiEapConfig.eap = data.ReadString();
117     config.wifiEapConfig.identity = data.ReadString();
118     config.wifiEapConfig.password = data.ReadString();
119     config.wifiEapConfig.clientCert = data.ReadString();
120     config.wifiEapConfig.privateKey = data.ReadString();
121     data.ReadUInt8Vector(&config.wifiEapConfig.certEntry);
122     strncpy_s(config.wifiEapConfig.certPassword, sizeof(config.wifiEapConfig.certPassword), data.ReadCString(),
123         sizeof(config.wifiEapConfig.certPassword) - 1);
124     ProcessPhase2Method(data.ReadInt32(), config.wifiEapConfig);
125     ProcessConfigureProxyMethod(data.ReadInt32(), config.wifiProxyconfig);
126     config.wifiProxyconfig.autoProxyConfig.pacWebAddress = data.ReadString();
127     config.wifiProxyconfig.manualProxyConfig.serverHostName = data.ReadString();
128     config.wifiProxyconfig.manualProxyConfig.serverPort = data.ReadInt32();
129     config.wifiProxyconfig.manualProxyConfig.exclusionObjectList = data.ReadString();
130     ProcessPrivacyConfig(data.ReadInt32(), config);
131 }
132 
ProcessAssignIpMethod(int32_t ipMethod,Wifi::WifiIpConfig & ipConfig)133 void MessageParcelUtils::ProcessAssignIpMethod(int32_t ipMethod, Wifi::WifiIpConfig &ipConfig)
134 {
135     switch (ipMethod) {
136         case static_cast<int32_t>(Wifi::AssignIpMethod::DHCP):
137             ipConfig.assignMethod = Wifi::AssignIpMethod::DHCP;
138             break;
139         case static_cast<int32_t>(Wifi::AssignIpMethod::STATIC):
140             ipConfig.assignMethod = Wifi::AssignIpMethod::STATIC;
141             break;
142         default:
143             ipConfig.assignMethod = Wifi::AssignIpMethod::UNASSIGNED;
144             break;
145     }
146 }
147 
ProcessPhase2Method(int32_t phase2,Wifi::WifiEapConfig & eapConfig)148 void MessageParcelUtils::ProcessPhase2Method(int32_t phase2, Wifi::WifiEapConfig &eapConfig)
149 {
150     switch (phase2) {
151         case static_cast<int32_t>(Wifi::Phase2Method::PAP):
152             eapConfig.phase2Method = Wifi::Phase2Method::PAP;
153             break;
154         case static_cast<int32_t>(Wifi::Phase2Method::MSCHAP):
155             eapConfig.phase2Method = Wifi::Phase2Method::MSCHAP;
156             break;
157         case static_cast<int32_t>(Wifi::Phase2Method::MSCHAPV2):
158             eapConfig.phase2Method = Wifi::Phase2Method::MSCHAPV2;
159             break;
160         case static_cast<int32_t>(Wifi::Phase2Method::GTC):
161             eapConfig.phase2Method = Wifi::Phase2Method::GTC;
162             break;
163         case static_cast<int32_t>(Wifi::Phase2Method::SIM):
164             eapConfig.phase2Method = Wifi::Phase2Method::SIM;
165             break;
166         case static_cast<int32_t>(Wifi::Phase2Method::AKA):
167             eapConfig.phase2Method = Wifi::Phase2Method::AKA;
168             break;
169         case static_cast<int32_t>(Wifi::Phase2Method::AKA_PRIME):
170             eapConfig.phase2Method = Wifi::Phase2Method::AKA_PRIME;
171             break;
172         default:
173             eapConfig.phase2Method = Wifi::Phase2Method::NONE;
174             break;
175     }
176 }
177 
ProcessConfigureProxyMethod(int32_t proxyMethod,Wifi::WifiProxyConfig & proxyConfig)178 void MessageParcelUtils::ProcessConfigureProxyMethod(int32_t proxyMethod, Wifi::WifiProxyConfig &proxyConfig)
179 {
180     switch (proxyMethod) {
181         case static_cast<int32_t>(Wifi::ConfigureProxyMethod::AUTOCONFIGUE):
182             proxyConfig.configureMethod = Wifi::ConfigureProxyMethod::AUTOCONFIGUE;
183             break;
184         case static_cast<int32_t>(Wifi::ConfigureProxyMethod::MANUALCONFIGUE):
185             proxyConfig.configureMethod = Wifi::ConfigureProxyMethod::MANUALCONFIGUE;
186             break;
187         default:
188             proxyConfig.configureMethod = Wifi::ConfigureProxyMethod::CLOSED;
189             break;
190     }
191 }
192 
ProcessPrivacyConfig(int32_t privacyConfig,Wifi::WifiDeviceConfig & config)193 void MessageParcelUtils::ProcessPrivacyConfig(int32_t privacyConfig, Wifi::WifiDeviceConfig &config)
194 {
195     if (privacyConfig == static_cast<int32_t>(Wifi::WifiPrivacyConfig::RANDOMMAC)) {
196         config.wifiPrivacySetting = Wifi::WifiPrivacyConfig::RANDOMMAC;
197     } else {
198         config.wifiPrivacySetting = Wifi::WifiPrivacyConfig::DEVICEMAC;
199     }
200 }
201 
ReadIpAddress(MessageParcel & data,Wifi::WifiIpAddress & address)202 void MessageParcelUtils::ReadIpAddress(MessageParcel &data, Wifi::WifiIpAddress &address)
203 {
204     constexpr int MAX_LIMIT_SIZE = 1024;
205     address.family = data.ReadInt32();
206     address.addressIpv4 = data.ReadUint32();
207     size_t size = data.ReadUint32();
208     if (size > MAX_LIMIT_SIZE) {
209         EDMLOGE("Read ip address parameter error: %{public}zu", size);
210         return;
211     }
212     for (size_t i = 0; i < size; i++) {
213         address.addressIpv6.push_back(data.ReadUint8());
214     }
215 }
216 } // namespace EDM
217 } // namespace OHOS