blob: 1f2d978eaba425c8e3613ef15ab67d02f317dc44 [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
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800135bool WiFiService::IsAutoConnectable() {
136 // TODO(quiche): Need to also handle the case where there might be
137 // another service that has posted a Connect task. crosbug.com/24276
138 return connectable() && wifi_->IsIdle();
139}
140
mukesh agrawal261daca2011-12-02 18:56:56 +0000141void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
142 DCHECK(endpoint->ssid() == ssid());
143 endpoints_.insert(endpoint);
144}
145
146void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
147 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
148 DCHECK(i != endpoints_.end());
149 if (i == endpoints_.end()) {
150 LOG(WARNING) << "In " << __func__ << "(): "
151 << "ignorning non-existent endpoint "
152 << endpoint->bssid_string();
153 return;
154 }
155 endpoints_.erase(i);
156}
157
Chris Masone6515aab2011-10-12 16:19:09 -0700158string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700159 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700160}
Chris Masone092df3e2011-08-22 09:41:39 -0700161const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700162 return mode_;
163}
164
Chris Masone092df3e2011-08-22 09:41:39 -0700165const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700166 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700167}
168
Paul Stewarta41e38d2011-11-11 07:47:29 -0800169const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700170 return ssid_;
171}
172
mukesh agrawal1a056262011-10-05 14:36:54 -0700173void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
174 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000175 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700176 } else if (security_ == flimflam::kSecurityPsk ||
177 security_ == flimflam::kSecurityWpa ||
178 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000179 ValidateWPAPassphrase(passphrase, error);
180 } else {
181 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700182 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000183
Paul Stewart2706aaf2011-12-14 16:44:04 -0800184 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000185 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800186 need_passphrase_ = false;
187 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000188
189 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700190}
191
Paul Stewartd08f4432011-11-04 07:48:20 -0700192bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
193 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
194 storage->ContainsGroup(GetSpecificStorageIdentifier());
195}
196
Paul Stewarta41e38d2011-11-11 07:47:29 -0800197bool WiFiService::IsVisible() const {
mukesh agrawal261daca2011-12-02 18:56:56 +0000198 const bool is_visible_in_scan = !endpoints_.empty();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800199
200 // WiFi Services should be displayed only if they are in range (have
201 // endpoints that have shown up in a scan) or if the service is actively
202 // being connected.
203 return is_visible_in_scan || IsConnected() || IsConnecting();
204}
205
Paul Stewartd08f4432011-11-04 07:48:20 -0700206bool WiFiService::Load(StoreInterface *storage) {
207 // First find out which storage identifier is available in priority order
208 // of specific, generic.
209 string id = GetSpecificStorageIdentifier();
210 if (!storage->ContainsGroup(id)) {
211 id = GetGenericStorageIdentifier();
212 if (!storage->ContainsGroup(id)) {
213 LOG(WARNING) << "Service is not available in the persistent store: "
214 << id;
215 return false;
216 }
217 }
218
219 // Set our storage identifier to match the storage name in the Profile.
220 storage_identifier_ = id;
221
222 // Load properties common to all Services.
223 if (!Service::Load(storage)) {
224 return false;
225 }
226
227 // Load properties specific to WiFi services.
228 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000229
Paul Stewart2706aaf2011-12-14 16:44:04 -0800230 // NB: mode, security and ssid parameters are never read in from
231 // Load() as they are provided from the scan.
232
233 string passphrase;
234 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
235 Error error;
236 SetPassphrase(passphrase, &error);
237 if (!error.IsSuccess()) {
238 LOG(ERROR) << "Passphrase could not be set: "
239 << Error::GetName(error.type());
240 }
241 }
242
Paul Stewartd08f4432011-11-04 07:48:20 -0700243 return true;
244}
245
246bool WiFiService::Save(StoreInterface *storage) {
247 // Save properties common to all Services.
248 if (!Service::Save(storage)) {
249 return false;
250 }
251
252 // Save properties specific to WiFi services.
253 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800254 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
255 storage->SetString(id, kStorageMode, mode_);
256 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
257 storage->SetString(id, kStorageSecurity, security_);
258 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000259
260 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700261 return true;
262}
263
Paul Stewart6ab23a92011-11-09 17:17:47 -0800264bool WiFiService::IsSecurityMatch(const string &security) const {
265 return GetSecurityClass(security) == GetSecurityClass(security_);
266}
267
mukesh agrawal32399322011-09-01 10:53:43 -0700268// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000269void WiFiService::HelpRegisterDerivedString(
270 PropertyStore *store,
271 const std::string &name,
272 std::string(WiFiService::*get)(Error *),
273 void(WiFiService::*set)(const std::string&, Error *)) {
274 store->RegisterDerivedString(
275 name,
276 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
277}
278
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700279void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700280 std::map<string, DBus::Variant> params;
281 DBus::MessageIter writer;
282
283 params[wpa_supplicant::kNetworkPropertyMode].writer().
284 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
285
286 if (security_ == flimflam::kSecurity8021x) {
287 NOTIMPLEMENTED();
288 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800289 const string psk_proto = StringPrintf("%s %s",
290 wpa_supplicant::kSecurityModeWPA,
291 wpa_supplicant::kSecurityModeRSN);
292 params[wpa_supplicant::kPropertySecurityProtocol].writer().
293 append_string(psk_proto.c_str());
294 params[wpa_supplicant::kPropertyPreSharedKey].writer().
295 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700296 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700297 params[wpa_supplicant::kPropertySecurityProtocol].writer().
298 append_string(wpa_supplicant::kSecurityModeRSN);
299 params[wpa_supplicant::kPropertyPreSharedKey].writer().
300 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700301 } else if (security_ == flimflam::kSecurityWpa) {
302 params[wpa_supplicant::kPropertySecurityProtocol].writer().
303 append_string(wpa_supplicant::kSecurityModeWPA);
304 params[wpa_supplicant::kPropertyPreSharedKey].writer().
305 append_string(passphrase_.c_str());
306 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000307 params[wpa_supplicant::kPropertyAuthAlg].writer().
308 append_string(wpa_supplicant::kSecurityAuthAlg);
309 Error error;
310 int key_index;
311 std::vector<uint8> password_bytes;
312 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
313 writer = params[wpa_supplicant::kPropertyWEPKey +
314 base::IntToString(key_index)].writer();
315 writer << password_bytes;
316 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
317 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700318 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800319 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700320 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800321 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700322 }
323
324 params[wpa_supplicant::kPropertyKeyManagement].writer().
325 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800326
327 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700328 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
329 writer << ssid_;
330
331 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700332}
333
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000334void WiFiService::DisconnectTask() {
335 wifi_->DisconnectFrom(this);
336}
337
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800338string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700339 return wifi_->GetRpcIdentifier();
340}
341
mukesh agrawal29c13a12011-11-24 00:09:19 +0000342void WiFiService::UpdateConnectable() {
343 if (security_ == flimflam::kSecurityNone) {
344 DCHECK(passphrase_.empty());
345 set_connectable(true);
346 } else if (security_ == flimflam::kSecurityWep ||
347 security_ == flimflam::kSecurityWpa ||
348 security_ == flimflam::kSecurityPsk ||
349 security_ == flimflam::kSecurityRsn) {
350 set_connectable(!passphrase_.empty());
351 } else {
352 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
353 set_connectable(false);
354 }
355}
356
mukesh agrawal1a056262011-10-05 14:36:54 -0700357// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000358void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
359 Error *error) {
360 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700361}
362
363// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000364void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
365 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700366 unsigned int length = passphrase.length();
367 vector<uint8> passphrase_bytes;
368
369 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
370 if (length != IEEE_80211::kWPAHexLen &&
371 (length < IEEE_80211::kWPAAsciiMinLen ||
372 length > IEEE_80211::kWPAAsciiMaxLen)) {
373 error->Populate(Error::kInvalidPassphrase);
374 }
375 } else {
376 if (length < IEEE_80211::kWPAAsciiMinLen ||
377 length > IEEE_80211::kWPAAsciiMaxLen) {
378 error->Populate(Error::kInvalidPassphrase);
379 }
380 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000381}
mukesh agrawal1a056262011-10-05 14:36:54 -0700382
Thieu Lef4cbda92011-11-10 23:41:24 +0000383// static
384void WiFiService::ParseWEPPassphrase(const string &passphrase,
385 int *key_index,
386 std::vector<uint8> *password_bytes,
387 Error *error) {
388 unsigned int length = passphrase.length();
389 int key_index_local;
390 std::string password_text;
391 bool is_hex = false;
392
393 switch (length) {
394 case IEEE_80211::kWEP40AsciiLen:
395 case IEEE_80211::kWEP104AsciiLen:
396 key_index_local = 0;
397 password_text = passphrase;
398 break;
399 case IEEE_80211::kWEP40AsciiLen + 2:
400 case IEEE_80211::kWEP104AsciiLen + 2:
401 if (CheckWEPKeyIndex(passphrase, error)) {
402 base::StringToInt(passphrase.substr(0,1), &key_index_local);
403 password_text = passphrase.substr(2);
404 }
405 break;
406 case IEEE_80211::kWEP40HexLen:
407 case IEEE_80211::kWEP104HexLen:
408 if (CheckWEPIsHex(passphrase, error)) {
409 key_index_local = 0;
410 password_text = passphrase;
411 is_hex = true;
412 }
413 break;
414 case IEEE_80211::kWEP40HexLen + 2:
415 case IEEE_80211::kWEP104HexLen + 2:
416 if(CheckWEPKeyIndex(passphrase, error) &&
417 CheckWEPIsHex(passphrase.substr(2), error)) {
418 base::StringToInt(passphrase.substr(0,1), &key_index_local);
419 password_text = passphrase.substr(2);
420 is_hex = true;
421 } else if (CheckWEPPrefix(passphrase, error) &&
422 CheckWEPIsHex(passphrase.substr(2), error)) {
423 key_index_local = 0;
424 password_text = passphrase.substr(2);
425 is_hex = true;
426 }
427 break;
428 case IEEE_80211::kWEP40HexLen + 4:
429 case IEEE_80211::kWEP104HexLen + 4:
430 if (CheckWEPKeyIndex(passphrase, error) &&
431 CheckWEPPrefix(passphrase.substr(2), error) &&
432 CheckWEPIsHex(passphrase.substr(4), error)) {
433 base::StringToInt(passphrase.substr(0,1), &key_index_local);
434 password_text = passphrase.substr(4);
435 is_hex = true;
436 }
437 break;
438 default:
439 error->Populate(Error::kInvalidPassphrase);
440 break;
441 }
442
mukesh agrawal1a056262011-10-05 14:36:54 -0700443 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000444 if (key_index)
445 *key_index = key_index_local;
446 if (password_bytes) {
447 if (is_hex)
448 base::HexStringToBytes(password_text, password_bytes);
449 else
450 password_bytes->insert(password_bytes->end(),
451 password_text.begin(),
452 password_text.end());
453 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700454 }
455}
456
457// static
458bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
459 vector<uint8> passphrase_bytes;
460 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
461 return true;
462 } else {
463 error->Populate(Error::kInvalidPassphrase);
464 return false;
465 }
466}
467
468// static
469bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
470 if (StartsWithASCII(passphrase, "0:", false) ||
471 StartsWithASCII(passphrase, "1:", false) ||
472 StartsWithASCII(passphrase, "2:", false) ||
473 StartsWithASCII(passphrase, "3:", false)) {
474 return true;
475 } else {
476 error->Populate(Error::kInvalidPassphrase);
477 return false;
478 }
479}
480
481// static
482bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
483 if (StartsWithASCII(passphrase, "0x", false)) {
484 return true;
485 } else {
486 error->Populate(Error::kInvalidPassphrase);
487 return false;
488 }
489}
490
Paul Stewart6ab23a92011-11-09 17:17:47 -0800491// static
mukesh agrawald835b202011-10-07 15:26:47 -0700492bool WiFiService::SanitizeSSID(string *ssid) {
493 CHECK(ssid);
494
495 size_t ssid_len = ssid->length();
496 size_t i;
497 bool changed = false;
498
499 for (i=0; i < ssid_len; ++i) {
500 if (!g_ascii_isprint((*ssid)[i])) {
501 (*ssid)[i] = '?';
502 changed = true;
503 }
504 }
505
506 return changed;
507}
508
Paul Stewart6ab23a92011-11-09 17:17:47 -0800509// static
510string WiFiService::GetSecurityClass(const string &security) {
511 if (security == flimflam::kSecurityRsn ||
512 security == flimflam::kSecurityWpa) {
513 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700514 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800515 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700516 }
517}
518
Paul Stewarta41e38d2011-11-11 07:47:29 -0800519// static
520bool WiFiService::ParseStorageIdentifier(const string &storage_name,
521 string *address,
522 string *mode,
523 string *security) {
524 vector<string> wifi_parts;
525 base::SplitString(storage_name, '_', &wifi_parts);
526 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
527 return false;
528 }
529 *address = wifi_parts[1];
530 *mode = wifi_parts[3];
531 *security = wifi_parts[4];
532 return true;
533}
534
Paul Stewart6ab23a92011-11-09 17:17:47 -0800535string WiFiService::GetGenericStorageIdentifier() const {
536 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
537}
538
Paul Stewartd08f4432011-11-04 07:48:20 -0700539string WiFiService::GetSpecificStorageIdentifier() const {
540 return GetStorageIdentifierForSecurity(security_);
541}
542
543string WiFiService::GetStorageIdentifierForSecurity(
544 const string &security) const {
545 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
546 flimflam::kTypeWifi,
547 wifi_->address().c_str(),
548 hex_ssid_.c_str(),
549 mode_.c_str(),
550 security.c_str()));
551}
552
mukesh agrawalb54601c2011-06-07 17:39:22 -0700553} // namespace shill