1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <base/logging.h>
18 #include <base/strings/stringprintf.h>
19 #include <service_constants.h>
20
21 #include "proxy_rpc_security_types.h"
22 #include "proxy_util.h"
23
24 // Autotest Server test encodes the object type in this key.
25 static const char kXmlRpcStructTypeKey[] = "xmlrpc_struct_type_key";
26 const char SecurityConfig::kDefaultSecurity[] = "none";
27 const int WPAConfig::kMaxPskSize = 64;
28 const char EAPConfig::kDefaultEapUsers[] = "* TLS";
29 const char EAPConfig::kDefaultEAPIdentity[] = "brillo";
30 int EAPConfig::last_tmp_id = 8800;
31 const int DynamicWEPConfig::kDefaultKeyPeriod = 20;
32 const char Tunneled1xConfig::kTTLSPrefix[] = "TTLS-";
33 const char Tunneled1xConfig::kLayer1TypePEAP[] = "PEAP";
34 const char Tunneled1xConfig::kLayer1TypeTTLS[] = "TTLS";
35 const char Tunneled1xConfig::kLayer2TypeGTC[] = "GTC";
36 const char Tunneled1xConfig::kLayer2TypeMSCHAPV2[] = "MSCHAPV2";
37 const char Tunneled1xConfig::kLayer2TypeMD5[] = "MD5";
38 const char Tunneled1xConfig::kLayer2TypeTTLSMSCHAPV2[] = "TTLS-MSCHAPV2";
39 const char Tunneled1xConfig::kLayer2TypeTTLSMSCHAP[] = "TTLS-MSCHAP";
40 const char Tunneled1xConfig::kLayer2TypeTTLSPAP[] = "TTLS-PAP";
41
CreateSecurityConfigObject(XmlRpc::XmlRpcValue * xml_rpc_value_in)42 std::unique_ptr<SecurityConfig> SecurityConfig::CreateSecurityConfigObject(
43 XmlRpc::XmlRpcValue* xml_rpc_value_in) {
44 const std::string& security_type = (*xml_rpc_value_in)[kXmlRpcStructTypeKey];
45 if (security_type == "SecurityConfig") {
46 return std::unique_ptr<SecurityConfig>(new SecurityConfig(xml_rpc_value_in));
47 }
48 if (security_type == "WEPConfig") {
49 return std::unique_ptr<SecurityConfig>(new WEPConfig(xml_rpc_value_in));
50 }
51 if (security_type == "WPAConfig") {
52 return std::unique_ptr<SecurityConfig>(new WPAConfig(xml_rpc_value_in));
53 }
54 LOG(FATAL) << "Unexpected object received. Received: " << security_type;
55 return nullptr;
56 }
57
SecurityConfig(XmlRpc::XmlRpcValue * xml_rpc_value_in)58 SecurityConfig::SecurityConfig(
59 XmlRpc::XmlRpcValue* xml_rpc_value_in) {
60 GetStringValueFromXmlRpcValueStructMember(
61 xml_rpc_value_in, "security", kDefaultSecurity, &security_);
62 }
63
GetServiceProperties(brillo::VariantDictionary * properties)64 void SecurityConfig::GetServiceProperties(brillo::VariantDictionary* properties) {
65 // The base class represents a connection with no security. So, no security
66 // properties to be sent to Shill.
67 }
68
WEPConfig(XmlRpc::XmlRpcValue * xml_rpc_value_in)69 WEPConfig::WEPConfig(XmlRpc::XmlRpcValue* xml_rpc_value_in)
70 : SecurityConfig::SecurityConfig(xml_rpc_value_in) {
71 GetStringVectorFromXmlRpcValueStructMember(
72 xml_rpc_value_in, "wep_keys", std::vector<std::string>(), &wep_keys_);
73 GetIntValueFromXmlRpcValueStructMember(
74 xml_rpc_value_in, "wep_default_key", 0, &wep_default_key_index_);
75 GetIntValueFromXmlRpcValueStructMember(
76 xml_rpc_value_in, "auth_algorithm", (int)kAuthAlgorithmTypeDefault,
77 &auth_algorithm_);
78 if (wep_default_key_index_ > static_cast<int>(wep_keys_.size())) {
79 LOG(FATAL) << "Error in received wep_default_key: "
80 << wep_default_key_index_;
81 }
82 }
83
GetServiceProperties(brillo::VariantDictionary * properties)84 void WEPConfig::GetServiceProperties(brillo::VariantDictionary* properties) {
85 std::string passphrase = base::StringPrintf(
86 "%d:%s", wep_default_key_index_,
87 wep_keys_[wep_default_key_index_].c_str());
88 (*properties)[shill::kPassphraseProperty] = passphrase;
89 }
90
WPAConfig(XmlRpc::XmlRpcValue * xml_rpc_value_in)91 WPAConfig::WPAConfig(XmlRpc::XmlRpcValue* xml_rpc_value_in)
92 : SecurityConfig::SecurityConfig(xml_rpc_value_in) {
93 GetStringValueFromXmlRpcValueStructMember(
94 xml_rpc_value_in, "psk", std::string(), &psk_);
95 GetIntValueFromXmlRpcValueStructMember(
96 xml_rpc_value_in, "wpa_mode", kWpaModeDefault, &wpa_mode_);
97 GetStringVectorFromXmlRpcValueStructMember(
98 xml_rpc_value_in, "wpa_ciphers", std::vector<std::string>(),
99 &wpa_ciphers_);
100 GetStringVectorFromXmlRpcValueStructMember(
101 xml_rpc_value_in, "wpa2_ciphers", std::vector<std::string>(),
102 &wpa2_ciphers_);
103 GetIntValueFromXmlRpcValueStructMember(
104 xml_rpc_value_in, "wpa_ptk_rekey_period", 0,
105 &wpa_ptk_rekey_period_seconds_);
106 GetIntValueFromXmlRpcValueStructMember(
107 xml_rpc_value_in, "wpa_gtk_rekey_period", 0,
108 &wpa_gtk_rekey_period_seconds_);
109 GetIntValueFromXmlRpcValueStructMember(
110 xml_rpc_value_in, "wpa_gmk_rekey_period", 0,
111 &wpa_gmk_rekey_period_seconds_);
112 GetBoolValueFromXmlRpcValueStructMember(
113 xml_rpc_value_in, "use_strict_rekey", 0, &use_strict_rekey_);
114
115 if (psk_.size() > kMaxPskSize) {
116 LOG(FATAL) << "WPA passphrases can be no longer than 63 characters"
117 "(or 64 hex digits). PSK: " << psk_;
118 }
119 if ((psk_.size() == kMaxPskSize) &&
120 (psk_.find_first_not_of("0123456789abcdef") != std::string::npos)) {
121 LOG(FATAL) << "Invalid PSK: " << psk_;
122 }
123 }
124
GetServiceProperties(brillo::VariantDictionary * properties)125 void WPAConfig::GetServiceProperties(brillo::VariantDictionary* properties) {
126 (*properties)[shill::kPassphraseProperty] = psk_;
127 }
128