blob: dd9d1c7c7152b946f8a3197436a0ade8e093c345 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -07002// 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 agrawal8a3188d2011-12-01 20:56:44 +0000116void WiFiService::AutoConnect() {
117 if (IsAutoConnectable()) {
118 // Execute immediately, for two reasons:
119 //
120 // 1. We need IsAutoConnectable to return the correct value for
121 // other WiFiServices, and that depends on WiFi's state.
122 //
123 // 2. We should probably limit the extent to which we queue up
124 // actions (such as AutoConnect) which depend on current state.
125 // If we queued AutoConnects, we could build a long queue of
126 // useless work (one AutoConnect per Service), which blocks
127 // more timely work.
128 ConnectTask();
129 }
130}
131
mukesh agrawal1830fa12011-09-26 14:31:40 -0700132void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700133 LOG(INFO) << __func__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000134 // Defer handling, since dbus-c++ does not permit us to send an
135 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700136 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700137 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700138}
139
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000140void WiFiService::Disconnect(Error */*error*/) {
141 LOG(INFO) << __func__;
142 // Defer handling, since dbus-c++ does not permit us to send an
143 // outbound request while processing an inbound one.
144 dispatcher()->PostTask(
145 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146}
147
Paul Stewart22aa71b2011-09-16 12:15:11 -0700148bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
149 return wifi_->TechnologyIs(type);
150}
151
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000152bool WiFiService::IsAutoConnectable() const {
153 return connectable()
154 // Only auto-connect to Services which have visible Endpoints.
155 // (Needed because hidden Services may remain registered with
156 // Manager even without visible Endpoints.)
157 && HasEndpoints()
158 // Do not preempt other connections (whether pending, or
159 // connected).
160 && wifi_->IsIdle();
161}
162
163bool WiFiService::IsConnecting() const {
164 // WiFi does not move us into the associating state until it gets
165 // feedback from wpa_supplicant. So, to answer whether or
166 // not we're connecting, we consult with |wifi_|.
167 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800168}
169
mukesh agrawal261daca2011-12-02 18:56:56 +0000170void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
171 DCHECK(endpoint->ssid() == ssid());
172 endpoints_.insert(endpoint);
173}
174
175void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
176 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
177 DCHECK(i != endpoints_.end());
178 if (i == endpoints_.end()) {
179 LOG(WARNING) << "In " << __func__ << "(): "
180 << "ignorning non-existent endpoint "
181 << endpoint->bssid_string();
182 return;
183 }
184 endpoints_.erase(i);
185}
186
Chris Masone6515aab2011-10-12 16:19:09 -0700187string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700188 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700189}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700190
mukesh agrawal1a056262011-10-05 14:36:54 -0700191void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
192 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000193 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700194 } else if (security_ == flimflam::kSecurityPsk ||
195 security_ == flimflam::kSecurityWpa ||
196 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000197 ValidateWPAPassphrase(passphrase, error);
198 } else {
199 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700200 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000201
Paul Stewart2706aaf2011-12-14 16:44:04 -0800202 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000203 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800204 need_passphrase_ = false;
205 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000206
207 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700208}
209
Paul Stewartd08f4432011-11-04 07:48:20 -0700210bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
211 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
212 storage->ContainsGroup(GetSpecificStorageIdentifier());
213}
214
Paul Stewarta41e38d2011-11-11 07:47:29 -0800215bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800216 // WiFi Services should be displayed only if they are in range (have
217 // endpoints that have shown up in a scan) or if the service is actively
218 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000219 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800220}
221
Paul Stewartd08f4432011-11-04 07:48:20 -0700222bool WiFiService::Load(StoreInterface *storage) {
223 // First find out which storage identifier is available in priority order
224 // of specific, generic.
225 string id = GetSpecificStorageIdentifier();
226 if (!storage->ContainsGroup(id)) {
227 id = GetGenericStorageIdentifier();
228 if (!storage->ContainsGroup(id)) {
229 LOG(WARNING) << "Service is not available in the persistent store: "
230 << id;
231 return false;
232 }
233 }
234
235 // Set our storage identifier to match the storage name in the Profile.
236 storage_identifier_ = id;
237
238 // Load properties common to all Services.
239 if (!Service::Load(storage)) {
240 return false;
241 }
242
243 // Load properties specific to WiFi services.
244 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000245
Paul Stewart2706aaf2011-12-14 16:44:04 -0800246 // NB: mode, security and ssid parameters are never read in from
247 // Load() as they are provided from the scan.
248
249 string passphrase;
250 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
251 Error error;
252 SetPassphrase(passphrase, &error);
253 if (!error.IsSuccess()) {
254 LOG(ERROR) << "Passphrase could not be set: "
255 << Error::GetName(error.type());
256 }
257 }
258
Paul Stewartd08f4432011-11-04 07:48:20 -0700259 return true;
260}
261
262bool WiFiService::Save(StoreInterface *storage) {
263 // Save properties common to all Services.
264 if (!Service::Save(storage)) {
265 return false;
266 }
267
268 // Save properties specific to WiFi services.
269 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800270 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
271 storage->SetString(id, kStorageMode, mode_);
272 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
273 storage->SetString(id, kStorageSecurity, security_);
274 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000275
276 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700277 return true;
278}
279
Paul Stewart6ab23a92011-11-09 17:17:47 -0800280bool WiFiService::IsSecurityMatch(const string &security) const {
281 return GetSecurityClass(security) == GetSecurityClass(security_);
282}
283
Thieu Le48e6d6d2011-12-06 00:40:27 +0000284void WiFiService::InitializeCustomMetrics() const {
285 string histogram = metrics()->GetFullMetricName(
286 Metrics::kMetricTimeToJoinMilliseconds,
287 technology());
288 metrics()->AddServiceStateTransitionTimer(this,
289 histogram,
290 Service::kStateAssociating,
291 Service::kStateConfiguring);
292}
293
294void WiFiService::SendPostReadyStateMetrics() const {
295 // TODO(thieule): Send physical mode and security metrics.
296 // crosbug.com/24441
297 metrics()->SendEnumToUMA(
298 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
299 technology()),
300 Metrics::WiFiFrequencyToChannel(frequency_),
301 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000302
303 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
304 metrics()->SendEnumToUMA(
305 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
306 technology()),
307 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
308 Metrics::kWiFiNetworkPhyModeMax);
309
310 Metrics::WiFiSecurity security_uma =
311 Metrics::WiFiSecurityStringToEnum(security_);
312 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
313 metrics()->SendEnumToUMA(
314 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
315 technology()),
316 security_uma,
317 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000318}
319
mukesh agrawal32399322011-09-01 10:53:43 -0700320// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000321void WiFiService::HelpRegisterDerivedString(
322 PropertyStore *store,
323 const std::string &name,
324 std::string(WiFiService::*get)(Error *),
325 void(WiFiService::*set)(const std::string&, Error *)) {
326 store->RegisterDerivedString(
327 name,
328 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
329}
330
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700331void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700332 std::map<string, DBus::Variant> params;
333 DBus::MessageIter writer;
334
335 params[wpa_supplicant::kNetworkPropertyMode].writer().
336 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
337
338 if (security_ == flimflam::kSecurity8021x) {
339 NOTIMPLEMENTED();
340 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800341 const string psk_proto = StringPrintf("%s %s",
342 wpa_supplicant::kSecurityModeWPA,
343 wpa_supplicant::kSecurityModeRSN);
344 params[wpa_supplicant::kPropertySecurityProtocol].writer().
345 append_string(psk_proto.c_str());
346 params[wpa_supplicant::kPropertyPreSharedKey].writer().
347 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700348 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700349 params[wpa_supplicant::kPropertySecurityProtocol].writer().
350 append_string(wpa_supplicant::kSecurityModeRSN);
351 params[wpa_supplicant::kPropertyPreSharedKey].writer().
352 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700353 } else if (security_ == flimflam::kSecurityWpa) {
354 params[wpa_supplicant::kPropertySecurityProtocol].writer().
355 append_string(wpa_supplicant::kSecurityModeWPA);
356 params[wpa_supplicant::kPropertyPreSharedKey].writer().
357 append_string(passphrase_.c_str());
358 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000359 params[wpa_supplicant::kPropertyAuthAlg].writer().
360 append_string(wpa_supplicant::kSecurityAuthAlg);
361 Error error;
362 int key_index;
363 std::vector<uint8> password_bytes;
364 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
365 writer = params[wpa_supplicant::kPropertyWEPKey +
366 base::IntToString(key_index)].writer();
367 writer << password_bytes;
368 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
369 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700370 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800371 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700372 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800373 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700374 }
375
376 params[wpa_supplicant::kPropertyKeyManagement].writer().
377 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800378
379 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700380 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
381 writer << ssid_;
382
383 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700384}
385
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000386void WiFiService::DisconnectTask() {
387 wifi_->DisconnectFrom(this);
388}
389
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800390string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700391 return wifi_->GetRpcIdentifier();
392}
393
mukesh agrawal29c13a12011-11-24 00:09:19 +0000394void WiFiService::UpdateConnectable() {
395 if (security_ == flimflam::kSecurityNone) {
396 DCHECK(passphrase_.empty());
397 set_connectable(true);
398 } else if (security_ == flimflam::kSecurityWep ||
399 security_ == flimflam::kSecurityWpa ||
400 security_ == flimflam::kSecurityPsk ||
401 security_ == flimflam::kSecurityRsn) {
402 set_connectable(!passphrase_.empty());
403 } else {
404 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
405 set_connectable(false);
406 }
407}
408
mukesh agrawal1a056262011-10-05 14:36:54 -0700409// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000410void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
411 Error *error) {
412 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700413}
414
415// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000416void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
417 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700418 unsigned int length = passphrase.length();
419 vector<uint8> passphrase_bytes;
420
421 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
422 if (length != IEEE_80211::kWPAHexLen &&
423 (length < IEEE_80211::kWPAAsciiMinLen ||
424 length > IEEE_80211::kWPAAsciiMaxLen)) {
425 error->Populate(Error::kInvalidPassphrase);
426 }
427 } else {
428 if (length < IEEE_80211::kWPAAsciiMinLen ||
429 length > IEEE_80211::kWPAAsciiMaxLen) {
430 error->Populate(Error::kInvalidPassphrase);
431 }
432 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000433}
mukesh agrawal1a056262011-10-05 14:36:54 -0700434
Thieu Lef4cbda92011-11-10 23:41:24 +0000435// static
436void WiFiService::ParseWEPPassphrase(const string &passphrase,
437 int *key_index,
438 std::vector<uint8> *password_bytes,
439 Error *error) {
440 unsigned int length = passphrase.length();
441 int key_index_local;
442 std::string password_text;
443 bool is_hex = false;
444
445 switch (length) {
446 case IEEE_80211::kWEP40AsciiLen:
447 case IEEE_80211::kWEP104AsciiLen:
448 key_index_local = 0;
449 password_text = passphrase;
450 break;
451 case IEEE_80211::kWEP40AsciiLen + 2:
452 case IEEE_80211::kWEP104AsciiLen + 2:
453 if (CheckWEPKeyIndex(passphrase, error)) {
454 base::StringToInt(passphrase.substr(0,1), &key_index_local);
455 password_text = passphrase.substr(2);
456 }
457 break;
458 case IEEE_80211::kWEP40HexLen:
459 case IEEE_80211::kWEP104HexLen:
460 if (CheckWEPIsHex(passphrase, error)) {
461 key_index_local = 0;
462 password_text = passphrase;
463 is_hex = true;
464 }
465 break;
466 case IEEE_80211::kWEP40HexLen + 2:
467 case IEEE_80211::kWEP104HexLen + 2:
468 if(CheckWEPKeyIndex(passphrase, error) &&
469 CheckWEPIsHex(passphrase.substr(2), error)) {
470 base::StringToInt(passphrase.substr(0,1), &key_index_local);
471 password_text = passphrase.substr(2);
472 is_hex = true;
473 } else if (CheckWEPPrefix(passphrase, error) &&
474 CheckWEPIsHex(passphrase.substr(2), error)) {
475 key_index_local = 0;
476 password_text = passphrase.substr(2);
477 is_hex = true;
478 }
479 break;
480 case IEEE_80211::kWEP40HexLen + 4:
481 case IEEE_80211::kWEP104HexLen + 4:
482 if (CheckWEPKeyIndex(passphrase, error) &&
483 CheckWEPPrefix(passphrase.substr(2), error) &&
484 CheckWEPIsHex(passphrase.substr(4), error)) {
485 base::StringToInt(passphrase.substr(0,1), &key_index_local);
486 password_text = passphrase.substr(4);
487 is_hex = true;
488 }
489 break;
490 default:
491 error->Populate(Error::kInvalidPassphrase);
492 break;
493 }
494
mukesh agrawal1a056262011-10-05 14:36:54 -0700495 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000496 if (key_index)
497 *key_index = key_index_local;
498 if (password_bytes) {
499 if (is_hex)
500 base::HexStringToBytes(password_text, password_bytes);
501 else
502 password_bytes->insert(password_bytes->end(),
503 password_text.begin(),
504 password_text.end());
505 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700506 }
507}
508
509// static
510bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
511 vector<uint8> passphrase_bytes;
512 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
513 return true;
514 } else {
515 error->Populate(Error::kInvalidPassphrase);
516 return false;
517 }
518}
519
520// static
521bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
522 if (StartsWithASCII(passphrase, "0:", false) ||
523 StartsWithASCII(passphrase, "1:", false) ||
524 StartsWithASCII(passphrase, "2:", false) ||
525 StartsWithASCII(passphrase, "3:", false)) {
526 return true;
527 } else {
528 error->Populate(Error::kInvalidPassphrase);
529 return false;
530 }
531}
532
533// static
534bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
535 if (StartsWithASCII(passphrase, "0x", false)) {
536 return true;
537 } else {
538 error->Populate(Error::kInvalidPassphrase);
539 return false;
540 }
541}
542
Paul Stewart6ab23a92011-11-09 17:17:47 -0800543// static
mukesh agrawald835b202011-10-07 15:26:47 -0700544bool WiFiService::SanitizeSSID(string *ssid) {
545 CHECK(ssid);
546
547 size_t ssid_len = ssid->length();
548 size_t i;
549 bool changed = false;
550
551 for (i=0; i < ssid_len; ++i) {
552 if (!g_ascii_isprint((*ssid)[i])) {
553 (*ssid)[i] = '?';
554 changed = true;
555 }
556 }
557
558 return changed;
559}
560
Paul Stewart6ab23a92011-11-09 17:17:47 -0800561// static
562string WiFiService::GetSecurityClass(const string &security) {
563 if (security == flimflam::kSecurityRsn ||
564 security == flimflam::kSecurityWpa) {
565 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700566 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800567 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700568 }
569}
570
Paul Stewarta41e38d2011-11-11 07:47:29 -0800571// static
572bool WiFiService::ParseStorageIdentifier(const string &storage_name,
573 string *address,
574 string *mode,
575 string *security) {
576 vector<string> wifi_parts;
577 base::SplitString(storage_name, '_', &wifi_parts);
578 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
579 return false;
580 }
581 *address = wifi_parts[1];
582 *mode = wifi_parts[3];
583 *security = wifi_parts[4];
584 return true;
585}
586
Paul Stewart6ab23a92011-11-09 17:17:47 -0800587string WiFiService::GetGenericStorageIdentifier() const {
588 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
589}
590
Paul Stewartd08f4432011-11-04 07:48:20 -0700591string WiFiService::GetSpecificStorageIdentifier() const {
592 return GetStorageIdentifierForSecurity(security_);
593}
594
595string WiFiService::GetStorageIdentifierForSecurity(
596 const string &security) const {
597 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
598 flimflam::kTypeWifi,
599 wifi_->address().c_str(),
600 hex_ssid_.c_str(),
601 mode_.c_str(),
602 security.c_str()));
603}
604
mukesh agrawalb54601c2011-06-07 17:39:22 -0700605} // namespace shill