mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/wifi_service.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame^] | 12 | #include <base/string_split.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 13 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 15 | #include <dbus/dbus.h> |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 16 | #include <glib.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/control_interface.h" |
| 19 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 20 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 21 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 22 | #include "shill/ieee80211.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 23 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 24 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 25 | #include "shill/wifi_endpoint.h" |
| 26 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 27 | |
| 28 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 29 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 30 | |
| 31 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 32 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 33 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
| 34 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 35 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 36 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 37 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 38 | const WiFiRefPtr &device, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame^] | 39 | const vector<uint8_t> &ssid, |
| 40 | const string &mode, |
| 41 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 42 | bool hidden_ssid) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 43 | : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 44 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 45 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 46 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 47 | hidden_ssid_(hidden_ssid), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 48 | task_factory_(this), |
| 49 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 50 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 51 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 52 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 53 | store->RegisterWriteOnlyString(flimflam::kPassphraseProperty, &passphrase_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 54 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 55 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
| 56 | store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 57 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 58 | store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_); |
| 59 | store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_); |
| 60 | store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_); |
| 61 | store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 62 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 63 | hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size()); |
| 64 | string ssid_string( |
| 65 | reinterpret_cast<const char *>(ssid_.data()), ssid_.size()); |
| 66 | if (SanitizeSSID(&ssid_string)) { |
| 67 | // WifiHexSsid property should only be present if Name property |
| 68 | // has been munged. |
| 69 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 70 | } |
| 71 | set_friendly_name(ssid_string); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 72 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 73 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 74 | // a service that is not 802.1x. |
| 75 | if (security_ == flimflam::kSecurity8021x) { |
| 76 | NOTIMPLEMENTED(); |
| 77 | // XXX needs_passpharse_ = false ? |
| 78 | } else if (security_ == flimflam::kSecurityPsk) { |
| 79 | SetEAPKeyManagement("WPA-PSK"); |
| 80 | need_passphrase_ = true; |
| 81 | } else if (security_ == flimflam::kSecurityRsn) { |
| 82 | SetEAPKeyManagement("WPA-PSK"); |
| 83 | need_passphrase_ = true; |
| 84 | } else if (security_ == flimflam::kSecurityWpa) { |
| 85 | SetEAPKeyManagement("WPA-PSK"); |
| 86 | need_passphrase_ = true; |
| 87 | } else if (security_ == flimflam::kSecurityWep) { |
| 88 | SetEAPKeyManagement("NONE"); |
| 89 | need_passphrase_ = true; |
| 90 | } else if (security_ == flimflam::kSecurityNone) { |
| 91 | SetEAPKeyManagement("NONE"); |
| 92 | need_passphrase_ = false; |
| 93 | } else { |
| 94 | LOG(ERROR) << "unsupported security method " << security_; |
| 95 | } |
| 96 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 97 | // Until we know better (at Profile load time), use the generic name. |
| 98 | storage_identifier_ = GetGenericStorageIdentifier(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | WiFiService::~WiFiService() { |
| 102 | LOG(INFO) << __func__; |
| 103 | } |
| 104 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 105 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 106 | LOG(INFO) << __func__; |
| 107 | |
| 108 | // NB(quiche) defer handling, since dbus-c++ does not permit us to |
| 109 | // send an outbound request while processing an inbound one. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 110 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 111 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 114 | void WiFiService::Disconnect() { |
| 115 | // TODO(quiche) RemoveNetwork from supplicant |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 118 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 119 | return wifi_->TechnologyIs(type); |
| 120 | } |
| 121 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 122 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 123 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 124 | } |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 125 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 126 | return mode_; |
| 127 | } |
| 128 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 129 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 130 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame^] | 133 | const vector<uint8_t> &WiFiService::ssid() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 134 | return ssid_; |
| 135 | } |
| 136 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 137 | void WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
| 138 | if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 139 | ValidateWEPPassphrase(passphrase, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 140 | } else if (security_ == flimflam::kSecurityPsk || |
| 141 | security_ == flimflam::kSecurityWpa || |
| 142 | security_ == flimflam::kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 143 | ValidateWPAPassphrase(passphrase, error); |
| 144 | } else { |
| 145 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 146 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 147 | |
| 148 | if (error->IsSuccess()) |
| 149 | passphrase_ = passphrase; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 152 | bool WiFiService::IsLoadableFrom(StoreInterface *storage) const { |
| 153 | return storage->ContainsGroup(GetGenericStorageIdentifier()) || |
| 154 | storage->ContainsGroup(GetSpecificStorageIdentifier()); |
| 155 | } |
| 156 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame^] | 157 | bool WiFiService::IsVisible() const { |
| 158 | // TODO(quiche): Write a function that returns whether (or which) |
| 159 | // endpoints are associated with this service. crosbug.com/22948 |
| 160 | const bool is_visible_in_scan = true; |
| 161 | |
| 162 | // WiFi Services should be displayed only if they are in range (have |
| 163 | // endpoints that have shown up in a scan) or if the service is actively |
| 164 | // being connected. |
| 165 | return is_visible_in_scan || IsConnected() || IsConnecting(); |
| 166 | } |
| 167 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 168 | bool WiFiService::Load(StoreInterface *storage) { |
| 169 | // First find out which storage identifier is available in priority order |
| 170 | // of specific, generic. |
| 171 | string id = GetSpecificStorageIdentifier(); |
| 172 | if (!storage->ContainsGroup(id)) { |
| 173 | id = GetGenericStorageIdentifier(); |
| 174 | if (!storage->ContainsGroup(id)) { |
| 175 | LOG(WARNING) << "Service is not available in the persistent store: " |
| 176 | << id; |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Set our storage identifier to match the storage name in the Profile. |
| 182 | storage_identifier_ = id; |
| 183 | |
| 184 | // Load properties common to all Services. |
| 185 | if (!Service::Load(storage)) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Load properties specific to WiFi services. |
| 190 | storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool WiFiService::Save(StoreInterface *storage) { |
| 195 | // Save properties common to all Services. |
| 196 | if (!Service::Save(storage)) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | // Save properties specific to WiFi services. |
| 201 | const string id = GetStorageIdentifier(); |
| 202 | storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
| 203 | return true; |
| 204 | } |
| 205 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 206 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 207 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 208 | } |
| 209 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 210 | // private methods |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 211 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 212 | std::map<string, DBus::Variant> params; |
| 213 | DBus::MessageIter writer; |
| 214 | |
| 215 | params[wpa_supplicant::kNetworkPropertyMode].writer(). |
| 216 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 217 | |
| 218 | if (security_ == flimflam::kSecurity8021x) { |
| 219 | NOTIMPLEMENTED(); |
| 220 | } else if (security_ == flimflam::kSecurityPsk) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 221 | const string psk_proto = StringPrintf("%s %s", |
| 222 | wpa_supplicant::kSecurityModeWPA, |
| 223 | wpa_supplicant::kSecurityModeRSN); |
| 224 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 225 | append_string(psk_proto.c_str()); |
| 226 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 227 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 228 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 229 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 230 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 231 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 232 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 233 | } else if (security_ == flimflam::kSecurityWpa) { |
| 234 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 235 | append_string(wpa_supplicant::kSecurityModeWPA); |
| 236 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 237 | append_string(passphrase_.c_str()); |
| 238 | } else if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 239 | params[wpa_supplicant::kPropertyAuthAlg].writer(). |
| 240 | append_string(wpa_supplicant::kSecurityAuthAlg); |
| 241 | Error error; |
| 242 | int key_index; |
| 243 | std::vector<uint8> password_bytes; |
| 244 | ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error); |
| 245 | writer = params[wpa_supplicant::kPropertyWEPKey + |
| 246 | base::IntToString(key_index)].writer(); |
| 247 | writer << password_bytes; |
| 248 | params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer(). |
| 249 | append_uint32(key_index); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 250 | } else if (security_ == flimflam::kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 251 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 252 | } else { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 253 | LOG(ERROR) << "Can't connect. Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | params[wpa_supplicant::kPropertyKeyManagement].writer(). |
| 257 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 258 | |
| 259 | // See note in dbus_adaptor.cc on why we need to use a local. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 260 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 261 | writer << ssid_; |
| 262 | |
| 263 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 266 | string WiFiService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 267 | return wifi_->GetRpcIdentifier(); |
| 268 | } |
| 269 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 270 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 271 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 272 | Error *error) { |
| 273 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 277 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 278 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 279 | unsigned int length = passphrase.length(); |
| 280 | vector<uint8> passphrase_bytes; |
| 281 | |
| 282 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 283 | if (length != IEEE_80211::kWPAHexLen && |
| 284 | (length < IEEE_80211::kWPAAsciiMinLen || |
| 285 | length > IEEE_80211::kWPAAsciiMaxLen)) { |
| 286 | error->Populate(Error::kInvalidPassphrase); |
| 287 | } |
| 288 | } else { |
| 289 | if (length < IEEE_80211::kWPAAsciiMinLen || |
| 290 | length > IEEE_80211::kWPAAsciiMaxLen) { |
| 291 | error->Populate(Error::kInvalidPassphrase); |
| 292 | } |
| 293 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 294 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 295 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 296 | // static |
| 297 | void WiFiService::ParseWEPPassphrase(const string &passphrase, |
| 298 | int *key_index, |
| 299 | std::vector<uint8> *password_bytes, |
| 300 | Error *error) { |
| 301 | unsigned int length = passphrase.length(); |
| 302 | int key_index_local; |
| 303 | std::string password_text; |
| 304 | bool is_hex = false; |
| 305 | |
| 306 | switch (length) { |
| 307 | case IEEE_80211::kWEP40AsciiLen: |
| 308 | case IEEE_80211::kWEP104AsciiLen: |
| 309 | key_index_local = 0; |
| 310 | password_text = passphrase; |
| 311 | break; |
| 312 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 313 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 314 | if (CheckWEPKeyIndex(passphrase, error)) { |
| 315 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 316 | password_text = passphrase.substr(2); |
| 317 | } |
| 318 | break; |
| 319 | case IEEE_80211::kWEP40HexLen: |
| 320 | case IEEE_80211::kWEP104HexLen: |
| 321 | if (CheckWEPIsHex(passphrase, error)) { |
| 322 | key_index_local = 0; |
| 323 | password_text = passphrase; |
| 324 | is_hex = true; |
| 325 | } |
| 326 | break; |
| 327 | case IEEE_80211::kWEP40HexLen + 2: |
| 328 | case IEEE_80211::kWEP104HexLen + 2: |
| 329 | if(CheckWEPKeyIndex(passphrase, error) && |
| 330 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 331 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 332 | password_text = passphrase.substr(2); |
| 333 | is_hex = true; |
| 334 | } else if (CheckWEPPrefix(passphrase, error) && |
| 335 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 336 | key_index_local = 0; |
| 337 | password_text = passphrase.substr(2); |
| 338 | is_hex = true; |
| 339 | } |
| 340 | break; |
| 341 | case IEEE_80211::kWEP40HexLen + 4: |
| 342 | case IEEE_80211::kWEP104HexLen + 4: |
| 343 | if (CheckWEPKeyIndex(passphrase, error) && |
| 344 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 345 | CheckWEPIsHex(passphrase.substr(4), error)) { |
| 346 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 347 | password_text = passphrase.substr(4); |
| 348 | is_hex = true; |
| 349 | } |
| 350 | break; |
| 351 | default: |
| 352 | error->Populate(Error::kInvalidPassphrase); |
| 353 | break; |
| 354 | } |
| 355 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 356 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 357 | if (key_index) |
| 358 | *key_index = key_index_local; |
| 359 | if (password_bytes) { |
| 360 | if (is_hex) |
| 361 | base::HexStringToBytes(password_text, password_bytes); |
| 362 | else |
| 363 | password_bytes->insert(password_bytes->end(), |
| 364 | password_text.begin(), |
| 365 | password_text.end()); |
| 366 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // static |
| 371 | bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 372 | vector<uint8> passphrase_bytes; |
| 373 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 374 | return true; |
| 375 | } else { |
| 376 | error->Populate(Error::kInvalidPassphrase); |
| 377 | return false; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // static |
| 382 | bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 383 | if (StartsWithASCII(passphrase, "0:", false) || |
| 384 | StartsWithASCII(passphrase, "1:", false) || |
| 385 | StartsWithASCII(passphrase, "2:", false) || |
| 386 | StartsWithASCII(passphrase, "3:", false)) { |
| 387 | return true; |
| 388 | } else { |
| 389 | error->Populate(Error::kInvalidPassphrase); |
| 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // static |
| 395 | bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 396 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 397 | return true; |
| 398 | } else { |
| 399 | error->Populate(Error::kInvalidPassphrase); |
| 400 | return false; |
| 401 | } |
| 402 | } |
| 403 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 404 | // static |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 405 | bool WiFiService::SanitizeSSID(string *ssid) { |
| 406 | CHECK(ssid); |
| 407 | |
| 408 | size_t ssid_len = ssid->length(); |
| 409 | size_t i; |
| 410 | bool changed = false; |
| 411 | |
| 412 | for (i=0; i < ssid_len; ++i) { |
| 413 | if (!g_ascii_isprint((*ssid)[i])) { |
| 414 | (*ssid)[i] = '?'; |
| 415 | changed = true; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return changed; |
| 420 | } |
| 421 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 422 | // static |
| 423 | string WiFiService::GetSecurityClass(const string &security) { |
| 424 | if (security == flimflam::kSecurityRsn || |
| 425 | security == flimflam::kSecurityWpa) { |
| 426 | return flimflam::kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 427 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 428 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame^] | 432 | // static |
| 433 | bool WiFiService::ParseStorageIdentifier(const string &storage_name, |
| 434 | string *address, |
| 435 | string *mode, |
| 436 | string *security) { |
| 437 | vector<string> wifi_parts; |
| 438 | base::SplitString(storage_name, '_', &wifi_parts); |
| 439 | if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) { |
| 440 | return false; |
| 441 | } |
| 442 | *address = wifi_parts[1]; |
| 443 | *mode = wifi_parts[3]; |
| 444 | *security = wifi_parts[4]; |
| 445 | return true; |
| 446 | } |
| 447 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 448 | string WiFiService::GetGenericStorageIdentifier() const { |
| 449 | return GetStorageIdentifierForSecurity(GetSecurityClass(security_)); |
| 450 | } |
| 451 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 452 | string WiFiService::GetSpecificStorageIdentifier() const { |
| 453 | return GetStorageIdentifierForSecurity(security_); |
| 454 | } |
| 455 | |
| 456 | string WiFiService::GetStorageIdentifierForSecurity( |
| 457 | const string &security) const { |
| 458 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 459 | flimflam::kTypeWifi, |
| 460 | wifi_->address().c_str(), |
| 461 | hex_ssid_.c_str(), |
| 462 | mode_.c_str(), |
| 463 | security.c_str())); |
| 464 | } |
| 465 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 466 | } // namespace shill |