blob: 217dd1ecf2e86c2aee5f52cfb484f188dcd9aabf [file] [log] [blame]
mukesh agrawalb54601c2011-06-07 17:39:22 -07001// 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 Masone34af2182011-08-22 11:59:36 -070010#include <base/stringprintf.h>
11#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080012#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070013#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <dbus/dbus.h>
mukesh agrawald835b202011-10-07 15:26:47 -070016#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
18#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Thieu Lef7709452011-11-15 01:13:19 +000023#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070024#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070025#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070026#include "shill/wifi_endpoint.h"
27#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070028
mukesh agrawal261daca2011-12-02 18:56:56 +000029using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070030using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070031using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070032
33namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070034
Paul Stewartd08f4432011-11-04 07:48:20 -070035const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080036const char WiFiService::kStorageMode[] = "WiFi.Mode";
37const char WiFiService::kStoragePassphrase[] = "Passphrase";
38const char WiFiService::kStorageSecurity[] = "WiFi.Security";
39const char WiFiService::kStorageSSID[] = "SSID";
Paul Stewartd08f4432011-11-04 07:48:20 -070040
mukesh agrawalb54601c2011-06-07 17:39:22 -070041WiFiService::WiFiService(ControlInterface *control_interface,
42 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070043 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070044 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080045 const vector<uint8_t> &ssid,
46 const string &mode,
47 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080048 bool hidden_ssid)
Gaurav Shah435de2c2011-11-17 19:01:07 -080049 : Service(control_interface, dispatcher, manager, Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070050 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070051 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070052 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080053 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070054 task_factory_(this),
55 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070056 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070057 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070058 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Thieu Lef7709452011-11-15 01:13:19 +000059 HelpRegisterDerivedString(store,
60 flimflam::kPassphraseProperty,
61 NULL,
62 &WiFiService::SetPassphrase);
Paul Stewartac4ac002011-08-26 12:04:26 -070063 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
64 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
65 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070066
Paul Stewartac4ac002011-08-26 12:04:26 -070067 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
68 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
69 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
70 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070071
mukesh agrawald835b202011-10-07 15:26:47 -070072 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
73 string ssid_string(
74 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
75 if (SanitizeSSID(&ssid_string)) {
76 // WifiHexSsid property should only be present if Name property
77 // has been munged.
78 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
79 }
80 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070081
mukesh agrawal6e277772011-09-29 15:04:23 -070082 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
83 // a service that is not 802.1x.
84 if (security_ == flimflam::kSecurity8021x) {
85 NOTIMPLEMENTED();
86 // XXX needs_passpharse_ = false ?
87 } else if (security_ == flimflam::kSecurityPsk) {
88 SetEAPKeyManagement("WPA-PSK");
89 need_passphrase_ = true;
90 } else if (security_ == flimflam::kSecurityRsn) {
91 SetEAPKeyManagement("WPA-PSK");
92 need_passphrase_ = true;
93 } else if (security_ == flimflam::kSecurityWpa) {
94 SetEAPKeyManagement("WPA-PSK");
95 need_passphrase_ = true;
96 } else if (security_ == flimflam::kSecurityWep) {
97 SetEAPKeyManagement("NONE");
98 need_passphrase_ = true;
99 } else if (security_ == flimflam::kSecurityNone) {
100 SetEAPKeyManagement("NONE");
101 need_passphrase_ = false;
102 } else {
103 LOG(ERROR) << "unsupported security method " << security_;
104 }
105
Paul Stewartd08f4432011-11-04 07:48:20 -0700106 // Until we know better (at Profile load time), use the generic name.
107 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000108 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700109}
110
111WiFiService::~WiFiService() {
112 LOG(INFO) << __func__;
113}
114
mukesh agrawal1830fa12011-09-26 14:31:40 -0700115void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700116 LOG(INFO) << __func__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000117 // Defer handling, since dbus-c++ does not permit us to send an
118 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700119 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700120 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700121}
122
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000123void WiFiService::Disconnect(Error */*error*/) {
124 LOG(INFO) << __func__;
125 // Defer handling, since dbus-c++ does not permit us to send an
126 // outbound request while processing an inbound one.
127 dispatcher()->PostTask(
128 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700129}
130
Paul Stewart22aa71b2011-09-16 12:15:11 -0700131bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
132 return wifi_->TechnologyIs(type);
133}
134
mukesh agrawal261daca2011-12-02 18:56:56 +0000135void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
136 DCHECK(endpoint->ssid() == ssid());
137 endpoints_.insert(endpoint);
138}
139
140void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
141 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
142 DCHECK(i != endpoints_.end());
143 if (i == endpoints_.end()) {
144 LOG(WARNING) << "In " << __func__ << "(): "
145 << "ignorning non-existent endpoint "
146 << endpoint->bssid_string();
147 return;
148 }
149 endpoints_.erase(i);
150}
151
Chris Masone6515aab2011-10-12 16:19:09 -0700152string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700153 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700154}
Chris Masone092df3e2011-08-22 09:41:39 -0700155const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700156 return mode_;
157}
158
Chris Masone092df3e2011-08-22 09:41:39 -0700159const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700160 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700161}
162
Paul Stewarta41e38d2011-11-11 07:47:29 -0800163const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700164 return ssid_;
165}
166
mukesh agrawal1a056262011-10-05 14:36:54 -0700167void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
168 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000169 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700170 } else if (security_ == flimflam::kSecurityPsk ||
171 security_ == flimflam::kSecurityWpa ||
172 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000173 ValidateWPAPassphrase(passphrase, error);
174 } else {
175 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700176 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000177
Paul Stewart2706aaf2011-12-14 16:44:04 -0800178 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000179 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800180 need_passphrase_ = false;
181 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000182
183 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700184}
185
Paul Stewartd08f4432011-11-04 07:48:20 -0700186bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
187 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
188 storage->ContainsGroup(GetSpecificStorageIdentifier());
189}
190
Paul Stewarta41e38d2011-11-11 07:47:29 -0800191bool WiFiService::IsVisible() const {
mukesh agrawal261daca2011-12-02 18:56:56 +0000192 const bool is_visible_in_scan = !endpoints_.empty();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800193
194 // WiFi Services should be displayed only if they are in range (have
195 // endpoints that have shown up in a scan) or if the service is actively
196 // being connected.
197 return is_visible_in_scan || IsConnected() || IsConnecting();
198}
199
Paul Stewartd08f4432011-11-04 07:48:20 -0700200bool WiFiService::Load(StoreInterface *storage) {
201 // First find out which storage identifier is available in priority order
202 // of specific, generic.
203 string id = GetSpecificStorageIdentifier();
204 if (!storage->ContainsGroup(id)) {
205 id = GetGenericStorageIdentifier();
206 if (!storage->ContainsGroup(id)) {
207 LOG(WARNING) << "Service is not available in the persistent store: "
208 << id;
209 return false;
210 }
211 }
212
213 // Set our storage identifier to match the storage name in the Profile.
214 storage_identifier_ = id;
215
216 // Load properties common to all Services.
217 if (!Service::Load(storage)) {
218 return false;
219 }
220
221 // Load properties specific to WiFi services.
222 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000223
Paul Stewart2706aaf2011-12-14 16:44:04 -0800224 // NB: mode, security and ssid parameters are never read in from
225 // Load() as they are provided from the scan.
226
227 string passphrase;
228 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
229 Error error;
230 SetPassphrase(passphrase, &error);
231 if (!error.IsSuccess()) {
232 LOG(ERROR) << "Passphrase could not be set: "
233 << Error::GetName(error.type());
234 }
235 }
236
Paul Stewartd08f4432011-11-04 07:48:20 -0700237 return true;
238}
239
240bool WiFiService::Save(StoreInterface *storage) {
241 // Save properties common to all Services.
242 if (!Service::Save(storage)) {
243 return false;
244 }
245
246 // Save properties specific to WiFi services.
247 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800248 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
249 storage->SetString(id, kStorageMode, mode_);
250 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
251 storage->SetString(id, kStorageSecurity, security_);
252 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000253
254 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700255 return true;
256}
257
Paul Stewart6ab23a92011-11-09 17:17:47 -0800258bool WiFiService::IsSecurityMatch(const string &security) const {
259 return GetSecurityClass(security) == GetSecurityClass(security_);
260}
261
mukesh agrawal32399322011-09-01 10:53:43 -0700262// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000263void WiFiService::HelpRegisterDerivedString(
264 PropertyStore *store,
265 const std::string &name,
266 std::string(WiFiService::*get)(Error *),
267 void(WiFiService::*set)(const std::string&, Error *)) {
268 store->RegisterDerivedString(
269 name,
270 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
271}
272
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700273void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700274 std::map<string, DBus::Variant> params;
275 DBus::MessageIter writer;
276
277 params[wpa_supplicant::kNetworkPropertyMode].writer().
278 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
279
280 if (security_ == flimflam::kSecurity8021x) {
281 NOTIMPLEMENTED();
282 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800283 const string psk_proto = StringPrintf("%s %s",
284 wpa_supplicant::kSecurityModeWPA,
285 wpa_supplicant::kSecurityModeRSN);
286 params[wpa_supplicant::kPropertySecurityProtocol].writer().
287 append_string(psk_proto.c_str());
288 params[wpa_supplicant::kPropertyPreSharedKey].writer().
289 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700290 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700291 params[wpa_supplicant::kPropertySecurityProtocol].writer().
292 append_string(wpa_supplicant::kSecurityModeRSN);
293 params[wpa_supplicant::kPropertyPreSharedKey].writer().
294 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700295 } else if (security_ == flimflam::kSecurityWpa) {
296 params[wpa_supplicant::kPropertySecurityProtocol].writer().
297 append_string(wpa_supplicant::kSecurityModeWPA);
298 params[wpa_supplicant::kPropertyPreSharedKey].writer().
299 append_string(passphrase_.c_str());
300 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000301 params[wpa_supplicant::kPropertyAuthAlg].writer().
302 append_string(wpa_supplicant::kSecurityAuthAlg);
303 Error error;
304 int key_index;
305 std::vector<uint8> password_bytes;
306 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
307 writer = params[wpa_supplicant::kPropertyWEPKey +
308 base::IntToString(key_index)].writer();
309 writer << password_bytes;
310 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
311 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700312 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800313 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700314 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800315 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700316 }
317
318 params[wpa_supplicant::kPropertyKeyManagement].writer().
319 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800320
321 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700322 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
323 writer << ssid_;
324
325 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700326}
327
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000328void WiFiService::DisconnectTask() {
329 wifi_->DisconnectFrom(this);
330}
331
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800332string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700333 return wifi_->GetRpcIdentifier();
334}
335
mukesh agrawal29c13a12011-11-24 00:09:19 +0000336void WiFiService::UpdateConnectable() {
337 if (security_ == flimflam::kSecurityNone) {
338 DCHECK(passphrase_.empty());
339 set_connectable(true);
340 } else if (security_ == flimflam::kSecurityWep ||
341 security_ == flimflam::kSecurityWpa ||
342 security_ == flimflam::kSecurityPsk ||
343 security_ == flimflam::kSecurityRsn) {
344 set_connectable(!passphrase_.empty());
345 } else {
346 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
347 set_connectable(false);
348 }
349}
350
mukesh agrawal1a056262011-10-05 14:36:54 -0700351// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000352void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
353 Error *error) {
354 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700355}
356
357// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000358void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
359 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700360 unsigned int length = passphrase.length();
361 vector<uint8> passphrase_bytes;
362
363 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
364 if (length != IEEE_80211::kWPAHexLen &&
365 (length < IEEE_80211::kWPAAsciiMinLen ||
366 length > IEEE_80211::kWPAAsciiMaxLen)) {
367 error->Populate(Error::kInvalidPassphrase);
368 }
369 } else {
370 if (length < IEEE_80211::kWPAAsciiMinLen ||
371 length > IEEE_80211::kWPAAsciiMaxLen) {
372 error->Populate(Error::kInvalidPassphrase);
373 }
374 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000375}
mukesh agrawal1a056262011-10-05 14:36:54 -0700376
Thieu Lef4cbda92011-11-10 23:41:24 +0000377// static
378void WiFiService::ParseWEPPassphrase(const string &passphrase,
379 int *key_index,
380 std::vector<uint8> *password_bytes,
381 Error *error) {
382 unsigned int length = passphrase.length();
383 int key_index_local;
384 std::string password_text;
385 bool is_hex = false;
386
387 switch (length) {
388 case IEEE_80211::kWEP40AsciiLen:
389 case IEEE_80211::kWEP104AsciiLen:
390 key_index_local = 0;
391 password_text = passphrase;
392 break;
393 case IEEE_80211::kWEP40AsciiLen + 2:
394 case IEEE_80211::kWEP104AsciiLen + 2:
395 if (CheckWEPKeyIndex(passphrase, error)) {
396 base::StringToInt(passphrase.substr(0,1), &key_index_local);
397 password_text = passphrase.substr(2);
398 }
399 break;
400 case IEEE_80211::kWEP40HexLen:
401 case IEEE_80211::kWEP104HexLen:
402 if (CheckWEPIsHex(passphrase, error)) {
403 key_index_local = 0;
404 password_text = passphrase;
405 is_hex = true;
406 }
407 break;
408 case IEEE_80211::kWEP40HexLen + 2:
409 case IEEE_80211::kWEP104HexLen + 2:
410 if(CheckWEPKeyIndex(passphrase, error) &&
411 CheckWEPIsHex(passphrase.substr(2), error)) {
412 base::StringToInt(passphrase.substr(0,1), &key_index_local);
413 password_text = passphrase.substr(2);
414 is_hex = true;
415 } else if (CheckWEPPrefix(passphrase, error) &&
416 CheckWEPIsHex(passphrase.substr(2), error)) {
417 key_index_local = 0;
418 password_text = passphrase.substr(2);
419 is_hex = true;
420 }
421 break;
422 case IEEE_80211::kWEP40HexLen + 4:
423 case IEEE_80211::kWEP104HexLen + 4:
424 if (CheckWEPKeyIndex(passphrase, error) &&
425 CheckWEPPrefix(passphrase.substr(2), error) &&
426 CheckWEPIsHex(passphrase.substr(4), error)) {
427 base::StringToInt(passphrase.substr(0,1), &key_index_local);
428 password_text = passphrase.substr(4);
429 is_hex = true;
430 }
431 break;
432 default:
433 error->Populate(Error::kInvalidPassphrase);
434 break;
435 }
436
mukesh agrawal1a056262011-10-05 14:36:54 -0700437 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000438 if (key_index)
439 *key_index = key_index_local;
440 if (password_bytes) {
441 if (is_hex)
442 base::HexStringToBytes(password_text, password_bytes);
443 else
444 password_bytes->insert(password_bytes->end(),
445 password_text.begin(),
446 password_text.end());
447 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700448 }
449}
450
451// static
452bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
453 vector<uint8> passphrase_bytes;
454 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
455 return true;
456 } else {
457 error->Populate(Error::kInvalidPassphrase);
458 return false;
459 }
460}
461
462// static
463bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
464 if (StartsWithASCII(passphrase, "0:", false) ||
465 StartsWithASCII(passphrase, "1:", false) ||
466 StartsWithASCII(passphrase, "2:", false) ||
467 StartsWithASCII(passphrase, "3:", false)) {
468 return true;
469 } else {
470 error->Populate(Error::kInvalidPassphrase);
471 return false;
472 }
473}
474
475// static
476bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
477 if (StartsWithASCII(passphrase, "0x", false)) {
478 return true;
479 } else {
480 error->Populate(Error::kInvalidPassphrase);
481 return false;
482 }
483}
484
Paul Stewart6ab23a92011-11-09 17:17:47 -0800485// static
mukesh agrawald835b202011-10-07 15:26:47 -0700486bool WiFiService::SanitizeSSID(string *ssid) {
487 CHECK(ssid);
488
489 size_t ssid_len = ssid->length();
490 size_t i;
491 bool changed = false;
492
493 for (i=0; i < ssid_len; ++i) {
494 if (!g_ascii_isprint((*ssid)[i])) {
495 (*ssid)[i] = '?';
496 changed = true;
497 }
498 }
499
500 return changed;
501}
502
Paul Stewart6ab23a92011-11-09 17:17:47 -0800503// static
504string WiFiService::GetSecurityClass(const string &security) {
505 if (security == flimflam::kSecurityRsn ||
506 security == flimflam::kSecurityWpa) {
507 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700508 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800509 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700510 }
511}
512
Paul Stewarta41e38d2011-11-11 07:47:29 -0800513// static
514bool WiFiService::ParseStorageIdentifier(const string &storage_name,
515 string *address,
516 string *mode,
517 string *security) {
518 vector<string> wifi_parts;
519 base::SplitString(storage_name, '_', &wifi_parts);
520 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
521 return false;
522 }
523 *address = wifi_parts[1];
524 *mode = wifi_parts[3];
525 *security = wifi_parts[4];
526 return true;
527}
528
Paul Stewart6ab23a92011-11-09 17:17:47 -0800529string WiFiService::GetGenericStorageIdentifier() const {
530 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
531}
532
Paul Stewartd08f4432011-11-04 07:48:20 -0700533string WiFiService::GetSpecificStorageIdentifier() const {
534 return GetStorageIdentifierForSecurity(security_);
535}
536
537string WiFiService::GetStorageIdentifierForSecurity(
538 const string &security) const {
539 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
540 flimflam::kTypeWifi,
541 wifi_->address().c_str(),
542 hex_ssid_.c_str(),
543 mode_.c_str(),
544 security.c_str()));
545}
546
mukesh agrawalb54601c2011-06-07 17:39:22 -0700547} // namespace shill