blob: 995394fd694ec7cd6790ed2e20341bfc5869f65e [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 Le48e6d6d2011-12-06 00:40:27 +000023#include "shill/metrics.h"
Thieu Lef7709452011-11-15 01:13:19 +000024#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070025#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070026#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070027#include "shill/wifi_endpoint.h"
28#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070029
mukesh agrawal261daca2011-12-02 18:56:56 +000030using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070031using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070032using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070033
34namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070035
Paul Stewartd08f4432011-11-04 07:48:20 -070036const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080037const char WiFiService::kStorageMode[] = "WiFi.Mode";
38const char WiFiService::kStoragePassphrase[] = "Passphrase";
39const char WiFiService::kStorageSecurity[] = "WiFi.Security";
40const char WiFiService::kStorageSSID[] = "SSID";
Paul Stewartd08f4432011-11-04 07:48:20 -070041
mukesh agrawalb54601c2011-06-07 17:39:22 -070042WiFiService::WiFiService(ControlInterface *control_interface,
43 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070044 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070045 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080046 const vector<uint8_t> &ssid,
47 const string &mode,
48 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080049 bool hidden_ssid)
Gaurav Shah435de2c2011-11-17 19:01:07 -080050 : Service(control_interface, dispatcher, manager, Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070051 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070052 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070053 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080054 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070055 task_factory_(this),
56 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070057 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070058 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070059 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Thieu Lef7709452011-11-15 01:13:19 +000060 HelpRegisterDerivedString(store,
61 flimflam::kPassphraseProperty,
62 NULL,
63 &WiFiService::SetPassphrase);
Paul Stewartac4ac002011-08-26 12:04:26 -070064 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
65 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
66 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070067
Paul Stewartac4ac002011-08-26 12:04:26 -070068 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
69 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
70 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
71 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070072
mukesh agrawald835b202011-10-07 15:26:47 -070073 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
74 string ssid_string(
75 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
76 if (SanitizeSSID(&ssid_string)) {
77 // WifiHexSsid property should only be present if Name property
78 // has been munged.
79 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
80 }
81 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070082
mukesh agrawal6e277772011-09-29 15:04:23 -070083 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
84 // a service that is not 802.1x.
85 if (security_ == flimflam::kSecurity8021x) {
86 NOTIMPLEMENTED();
87 // XXX needs_passpharse_ = false ?
88 } else if (security_ == flimflam::kSecurityPsk) {
89 SetEAPKeyManagement("WPA-PSK");
90 need_passphrase_ = true;
91 } else if (security_ == flimflam::kSecurityRsn) {
92 SetEAPKeyManagement("WPA-PSK");
93 need_passphrase_ = true;
94 } else if (security_ == flimflam::kSecurityWpa) {
95 SetEAPKeyManagement("WPA-PSK");
96 need_passphrase_ = true;
97 } else if (security_ == flimflam::kSecurityWep) {
98 SetEAPKeyManagement("NONE");
99 need_passphrase_ = true;
100 } else if (security_ == flimflam::kSecurityNone) {
101 SetEAPKeyManagement("NONE");
102 need_passphrase_ = false;
103 } else {
104 LOG(ERROR) << "unsupported security method " << security_;
105 }
106
Paul Stewartd08f4432011-11-04 07:48:20 -0700107 // Until we know better (at Profile load time), use the generic name.
108 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000109 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700110}
111
112WiFiService::~WiFiService() {
113 LOG(INFO) << __func__;
114}
115
mukesh agrawal1830fa12011-09-26 14:31:40 -0700116void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700117 LOG(INFO) << __func__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000118 // Defer handling, since dbus-c++ does not permit us to send an
119 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700120 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700121 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700122}
123
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000124void WiFiService::Disconnect(Error */*error*/) {
125 LOG(INFO) << __func__;
126 // Defer handling, since dbus-c++ does not permit us to send an
127 // outbound request while processing an inbound one.
128 dispatcher()->PostTask(
129 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700130}
131
Paul Stewart22aa71b2011-09-16 12:15:11 -0700132bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
133 return wifi_->TechnologyIs(type);
134}
135
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800136bool WiFiService::IsAutoConnectable() {
137 // TODO(quiche): Need to also handle the case where there might be
138 // another service that has posted a Connect task. crosbug.com/24276
139 return connectable() && wifi_->IsIdle();
140}
141
mukesh agrawal261daca2011-12-02 18:56:56 +0000142void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
143 DCHECK(endpoint->ssid() == ssid());
144 endpoints_.insert(endpoint);
145}
146
147void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
148 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
149 DCHECK(i != endpoints_.end());
150 if (i == endpoints_.end()) {
151 LOG(WARNING) << "In " << __func__ << "(): "
152 << "ignorning non-existent endpoint "
153 << endpoint->bssid_string();
154 return;
155 }
156 endpoints_.erase(i);
157}
158
Chris Masone6515aab2011-10-12 16:19:09 -0700159string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700160 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700161}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700162
mukesh agrawal1a056262011-10-05 14:36:54 -0700163void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
164 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000165 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700166 } else if (security_ == flimflam::kSecurityPsk ||
167 security_ == flimflam::kSecurityWpa ||
168 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000169 ValidateWPAPassphrase(passphrase, error);
170 } else {
171 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700172 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000173
Paul Stewart2706aaf2011-12-14 16:44:04 -0800174 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000175 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800176 need_passphrase_ = false;
177 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000178
179 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700180}
181
Paul Stewartd08f4432011-11-04 07:48:20 -0700182bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
183 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
184 storage->ContainsGroup(GetSpecificStorageIdentifier());
185}
186
Paul Stewarta41e38d2011-11-11 07:47:29 -0800187bool WiFiService::IsVisible() const {
mukesh agrawal261daca2011-12-02 18:56:56 +0000188 const bool is_visible_in_scan = !endpoints_.empty();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800189
190 // WiFi Services should be displayed only if they are in range (have
191 // endpoints that have shown up in a scan) or if the service is actively
192 // being connected.
193 return is_visible_in_scan || IsConnected() || IsConnecting();
194}
195
Paul Stewartd08f4432011-11-04 07:48:20 -0700196bool WiFiService::Load(StoreInterface *storage) {
197 // First find out which storage identifier is available in priority order
198 // of specific, generic.
199 string id = GetSpecificStorageIdentifier();
200 if (!storage->ContainsGroup(id)) {
201 id = GetGenericStorageIdentifier();
202 if (!storage->ContainsGroup(id)) {
203 LOG(WARNING) << "Service is not available in the persistent store: "
204 << id;
205 return false;
206 }
207 }
208
209 // Set our storage identifier to match the storage name in the Profile.
210 storage_identifier_ = id;
211
212 // Load properties common to all Services.
213 if (!Service::Load(storage)) {
214 return false;
215 }
216
217 // Load properties specific to WiFi services.
218 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000219
Paul Stewart2706aaf2011-12-14 16:44:04 -0800220 // NB: mode, security and ssid parameters are never read in from
221 // Load() as they are provided from the scan.
222
223 string passphrase;
224 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
225 Error error;
226 SetPassphrase(passphrase, &error);
227 if (!error.IsSuccess()) {
228 LOG(ERROR) << "Passphrase could not be set: "
229 << Error::GetName(error.type());
230 }
231 }
232
Paul Stewartd08f4432011-11-04 07:48:20 -0700233 return true;
234}
235
236bool WiFiService::Save(StoreInterface *storage) {
237 // Save properties common to all Services.
238 if (!Service::Save(storage)) {
239 return false;
240 }
241
242 // Save properties specific to WiFi services.
243 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800244 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
245 storage->SetString(id, kStorageMode, mode_);
246 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
247 storage->SetString(id, kStorageSecurity, security_);
248 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000249
250 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700251 return true;
252}
253
Paul Stewart6ab23a92011-11-09 17:17:47 -0800254bool WiFiService::IsSecurityMatch(const string &security) const {
255 return GetSecurityClass(security) == GetSecurityClass(security_);
256}
257
Thieu Le48e6d6d2011-12-06 00:40:27 +0000258void WiFiService::InitializeCustomMetrics() const {
259 string histogram = metrics()->GetFullMetricName(
260 Metrics::kMetricTimeToJoinMilliseconds,
261 technology());
262 metrics()->AddServiceStateTransitionTimer(this,
263 histogram,
264 Service::kStateAssociating,
265 Service::kStateConfiguring);
266}
267
268void WiFiService::SendPostReadyStateMetrics() const {
269 // TODO(thieule): Send physical mode and security metrics.
270 // crosbug.com/24441
271 metrics()->SendEnumToUMA(
272 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
273 technology()),
274 Metrics::WiFiFrequencyToChannel(frequency_),
275 Metrics::kMetricNetworkChannelMax);
276}
277
mukesh agrawal32399322011-09-01 10:53:43 -0700278// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000279void WiFiService::HelpRegisterDerivedString(
280 PropertyStore *store,
281 const std::string &name,
282 std::string(WiFiService::*get)(Error *),
283 void(WiFiService::*set)(const std::string&, Error *)) {
284 store->RegisterDerivedString(
285 name,
286 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
287}
288
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700289void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700290 std::map<string, DBus::Variant> params;
291 DBus::MessageIter writer;
292
293 params[wpa_supplicant::kNetworkPropertyMode].writer().
294 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
295
296 if (security_ == flimflam::kSecurity8021x) {
297 NOTIMPLEMENTED();
298 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800299 const string psk_proto = StringPrintf("%s %s",
300 wpa_supplicant::kSecurityModeWPA,
301 wpa_supplicant::kSecurityModeRSN);
302 params[wpa_supplicant::kPropertySecurityProtocol].writer().
303 append_string(psk_proto.c_str());
304 params[wpa_supplicant::kPropertyPreSharedKey].writer().
305 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700306 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700307 params[wpa_supplicant::kPropertySecurityProtocol].writer().
308 append_string(wpa_supplicant::kSecurityModeRSN);
309 params[wpa_supplicant::kPropertyPreSharedKey].writer().
310 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700311 } else if (security_ == flimflam::kSecurityWpa) {
312 params[wpa_supplicant::kPropertySecurityProtocol].writer().
313 append_string(wpa_supplicant::kSecurityModeWPA);
314 params[wpa_supplicant::kPropertyPreSharedKey].writer().
315 append_string(passphrase_.c_str());
316 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000317 params[wpa_supplicant::kPropertyAuthAlg].writer().
318 append_string(wpa_supplicant::kSecurityAuthAlg);
319 Error error;
320 int key_index;
321 std::vector<uint8> password_bytes;
322 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
323 writer = params[wpa_supplicant::kPropertyWEPKey +
324 base::IntToString(key_index)].writer();
325 writer << password_bytes;
326 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
327 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700328 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800329 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700330 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800331 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700332 }
333
334 params[wpa_supplicant::kPropertyKeyManagement].writer().
335 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800336
337 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700338 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
339 writer << ssid_;
340
341 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700342}
343
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000344void WiFiService::DisconnectTask() {
345 wifi_->DisconnectFrom(this);
346}
347
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800348string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700349 return wifi_->GetRpcIdentifier();
350}
351
mukesh agrawal29c13a12011-11-24 00:09:19 +0000352void WiFiService::UpdateConnectable() {
353 if (security_ == flimflam::kSecurityNone) {
354 DCHECK(passphrase_.empty());
355 set_connectable(true);
356 } else if (security_ == flimflam::kSecurityWep ||
357 security_ == flimflam::kSecurityWpa ||
358 security_ == flimflam::kSecurityPsk ||
359 security_ == flimflam::kSecurityRsn) {
360 set_connectable(!passphrase_.empty());
361 } else {
362 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
363 set_connectable(false);
364 }
365}
366
mukesh agrawal1a056262011-10-05 14:36:54 -0700367// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000368void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
369 Error *error) {
370 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700371}
372
373// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000374void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
375 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700376 unsigned int length = passphrase.length();
377 vector<uint8> passphrase_bytes;
378
379 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
380 if (length != IEEE_80211::kWPAHexLen &&
381 (length < IEEE_80211::kWPAAsciiMinLen ||
382 length > IEEE_80211::kWPAAsciiMaxLen)) {
383 error->Populate(Error::kInvalidPassphrase);
384 }
385 } else {
386 if (length < IEEE_80211::kWPAAsciiMinLen ||
387 length > IEEE_80211::kWPAAsciiMaxLen) {
388 error->Populate(Error::kInvalidPassphrase);
389 }
390 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000391}
mukesh agrawal1a056262011-10-05 14:36:54 -0700392
Thieu Lef4cbda92011-11-10 23:41:24 +0000393// static
394void WiFiService::ParseWEPPassphrase(const string &passphrase,
395 int *key_index,
396 std::vector<uint8> *password_bytes,
397 Error *error) {
398 unsigned int length = passphrase.length();
399 int key_index_local;
400 std::string password_text;
401 bool is_hex = false;
402
403 switch (length) {
404 case IEEE_80211::kWEP40AsciiLen:
405 case IEEE_80211::kWEP104AsciiLen:
406 key_index_local = 0;
407 password_text = passphrase;
408 break;
409 case IEEE_80211::kWEP40AsciiLen + 2:
410 case IEEE_80211::kWEP104AsciiLen + 2:
411 if (CheckWEPKeyIndex(passphrase, error)) {
412 base::StringToInt(passphrase.substr(0,1), &key_index_local);
413 password_text = passphrase.substr(2);
414 }
415 break;
416 case IEEE_80211::kWEP40HexLen:
417 case IEEE_80211::kWEP104HexLen:
418 if (CheckWEPIsHex(passphrase, error)) {
419 key_index_local = 0;
420 password_text = passphrase;
421 is_hex = true;
422 }
423 break;
424 case IEEE_80211::kWEP40HexLen + 2:
425 case IEEE_80211::kWEP104HexLen + 2:
426 if(CheckWEPKeyIndex(passphrase, error) &&
427 CheckWEPIsHex(passphrase.substr(2), error)) {
428 base::StringToInt(passphrase.substr(0,1), &key_index_local);
429 password_text = passphrase.substr(2);
430 is_hex = true;
431 } else if (CheckWEPPrefix(passphrase, error) &&
432 CheckWEPIsHex(passphrase.substr(2), error)) {
433 key_index_local = 0;
434 password_text = passphrase.substr(2);
435 is_hex = true;
436 }
437 break;
438 case IEEE_80211::kWEP40HexLen + 4:
439 case IEEE_80211::kWEP104HexLen + 4:
440 if (CheckWEPKeyIndex(passphrase, error) &&
441 CheckWEPPrefix(passphrase.substr(2), error) &&
442 CheckWEPIsHex(passphrase.substr(4), error)) {
443 base::StringToInt(passphrase.substr(0,1), &key_index_local);
444 password_text = passphrase.substr(4);
445 is_hex = true;
446 }
447 break;
448 default:
449 error->Populate(Error::kInvalidPassphrase);
450 break;
451 }
452
mukesh agrawal1a056262011-10-05 14:36:54 -0700453 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000454 if (key_index)
455 *key_index = key_index_local;
456 if (password_bytes) {
457 if (is_hex)
458 base::HexStringToBytes(password_text, password_bytes);
459 else
460 password_bytes->insert(password_bytes->end(),
461 password_text.begin(),
462 password_text.end());
463 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700464 }
465}
466
467// static
468bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
469 vector<uint8> passphrase_bytes;
470 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
471 return true;
472 } else {
473 error->Populate(Error::kInvalidPassphrase);
474 return false;
475 }
476}
477
478// static
479bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
480 if (StartsWithASCII(passphrase, "0:", false) ||
481 StartsWithASCII(passphrase, "1:", false) ||
482 StartsWithASCII(passphrase, "2:", false) ||
483 StartsWithASCII(passphrase, "3:", false)) {
484 return true;
485 } else {
486 error->Populate(Error::kInvalidPassphrase);
487 return false;
488 }
489}
490
491// static
492bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
493 if (StartsWithASCII(passphrase, "0x", false)) {
494 return true;
495 } else {
496 error->Populate(Error::kInvalidPassphrase);
497 return false;
498 }
499}
500
Paul Stewart6ab23a92011-11-09 17:17:47 -0800501// static
mukesh agrawald835b202011-10-07 15:26:47 -0700502bool WiFiService::SanitizeSSID(string *ssid) {
503 CHECK(ssid);
504
505 size_t ssid_len = ssid->length();
506 size_t i;
507 bool changed = false;
508
509 for (i=0; i < ssid_len; ++i) {
510 if (!g_ascii_isprint((*ssid)[i])) {
511 (*ssid)[i] = '?';
512 changed = true;
513 }
514 }
515
516 return changed;
517}
518
Paul Stewart6ab23a92011-11-09 17:17:47 -0800519// static
520string WiFiService::GetSecurityClass(const string &security) {
521 if (security == flimflam::kSecurityRsn ||
522 security == flimflam::kSecurityWpa) {
523 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700524 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800525 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700526 }
527}
528
Paul Stewarta41e38d2011-11-11 07:47:29 -0800529// static
530bool WiFiService::ParseStorageIdentifier(const string &storage_name,
531 string *address,
532 string *mode,
533 string *security) {
534 vector<string> wifi_parts;
535 base::SplitString(storage_name, '_', &wifi_parts);
536 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
537 return false;
538 }
539 *address = wifi_parts[1];
540 *mode = wifi_parts[3];
541 *security = wifi_parts[4];
542 return true;
543}
544
Paul Stewart6ab23a92011-11-09 17:17:47 -0800545string WiFiService::GetGenericStorageIdentifier() const {
546 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
547}
548
Paul Stewartd08f4432011-11-04 07:48:20 -0700549string WiFiService::GetSpecificStorageIdentifier() const {
550 return GetStorageIdentifierForSecurity(security_);
551}
552
553string WiFiService::GetStorageIdentifierForSecurity(
554 const string &security) const {
555 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
556 flimflam::kTypeWifi,
557 wifi_->address().c_str(),
558 hex_ssid_.c_str(),
559 mode_.c_str(),
560 security.c_str()));
561}
562
mukesh agrawalb54601c2011-06-07 17:39:22 -0700563} // namespace shill