• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "sysvpn_config.h"
17 #include "ipsecvpn_config.h"
18 #include "l2tpvpn_config.h"
19 #include "netmgr_ext_log_wrapper.h"
20 #include "openvpn_config.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 
Marshalling(Parcel & parcel) const25 bool SysVpnConfig::Marshalling(Parcel &parcel) const
26 {
27     // add vpnType first
28     parcel.WriteInt32(vpnType_);
29 
30     bool allOK = VpnConfig::Marshalling(parcel) &&
31                  parcel.WriteString(vpnName_) &&
32                  parcel.WriteInt32(vpnType_) &&
33                  parcel.WriteString(userName_) &&
34                  parcel.WriteString(password_) &&
35                  parcel.WriteBool(saveLogin_) &&
36                  parcel.WriteInt32(userId_) &&
37                  parcel.WriteString(forwardingRoutes_) &&
38                  parcel.WriteString(pkcs12Password_);
39 
40     allOK = allOK && parcel.WriteInt32(static_cast<int32_t>(pkcs12FileData_.size()));
41     for (uint8_t byte : pkcs12FileData_) {
42         allOK = allOK && parcel.WriteUint8(byte);
43     }
44     if (!allOK) {
45         NETMGR_EXT_LOG_I("sysvpn SysVpnConfig Marshalling failed");
46     }
47     return allOK;
48 }
49 
Unmarshalling(Parcel & parcel)50 SysVpnConfig* SysVpnConfig::Unmarshalling(Parcel &parcel)
51 {
52     // get vpnType first
53     int32_t type = -1;
54     parcel.ReadInt32(type);
55 
56     switch (type) {
57         case VpnType::IKEV2_IPSEC_MSCHAPv2:
58         case VpnType::IKEV2_IPSEC_PSK:
59         case VpnType::IKEV2_IPSEC_RSA:
60         case VpnType::IPSEC_XAUTH_PSK:
61         case VpnType::IPSEC_XAUTH_RSA:
62         case VpnType::IPSEC_HYBRID_RSA:
63             return IpsecVpnConfig::Unmarshalling(parcel);
64         case VpnType::OPENVPN:
65             return OpenvpnConfig::Unmarshalling(parcel);
66         case VpnType::L2TP:
67         case VpnType::L2TP_IPSEC_PSK:
68         case VpnType::L2TP_IPSEC_RSA:
69             return L2tpVpnConfig::Unmarshalling(parcel);
70         default:
71             NETMGR_EXT_LOG_E("sysvpn SysVpnConfig Unmarshalling failed, type=%{public}d", type);
72             return nullptr;
73     }
74 }
75 
Unmarshalling(Parcel & parcel,SysVpnConfig * ptr)76 bool SysVpnConfig::Unmarshalling(Parcel &parcel, SysVpnConfig* ptr)
77 {
78     bool allOK = VpnConfig::UnmarshallingVpnConfig(parcel, ptr) &&
79                  parcel.ReadString(ptr->vpnName_) &&
80                  parcel.ReadInt32(ptr->vpnType_) &&
81                  parcel.ReadString(ptr->userName_) &&
82                  parcel.ReadString(ptr->password_) &&
83                  parcel.ReadBool(ptr->saveLogin_) &&
84                  parcel.ReadInt32(ptr->userId_) &&
85                  parcel.ReadString(ptr->forwardingRoutes_) &&
86                  parcel.ReadString(ptr->pkcs12Password_);
87 
88     int32_t pkcs12FileDataSize = 0;
89     uint8_t data = 0;
90     allOK = allOK && parcel.ReadInt32(pkcs12FileDataSize);
91     for (int32_t i = 0; i < pkcs12FileDataSize; i++) {
92         allOK = allOK && parcel.ReadUint8(data);
93         ptr->pkcs12FileData_.push_back(data);
94     }
95     if (!allOK) {
96         NETMGR_EXT_LOG_I("sysvpn SysVpnConfig Unmarshalling failed");
97     }
98     return allOK;
99 }
100 } // namespace NetManagerStandard
101 } // namespace OHOS