OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/wifi/wifi_service.h" | 5 #include "components/wifi/wifi_service.h" |
6 | 6 |
7 #include <iphlpapi.h> | 7 #include <iphlpapi.h> |
8 #include <objbase.h> | 8 #include <objbase.h> |
9 #include <wlanapi.h> | 9 #include <wlanapi.h> |
10 | 10 |
11 #include <set> | 11 #include <set> |
12 | 12 |
13 #include "base/base_paths_win.h" | 13 #include "base/base_paths_win.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/win/registry.h" | 22 #include "base/win/registry.h" |
23 #include "components/onc/onc_constants.h" | 23 #include "components/onc/onc_constants.h" |
24 #include "third_party/libxml/chromium/libxml_utils.h" | 24 #include "third_party/libxml/chromium/libxml_utils.h" |
25 | 25 |
26 namespace { | 26 namespace { |
27 const char kWiFiServiceError[] = "Error.WiFiService"; | 27 const char kWiFiServiceError[] = "Error.WiFiService"; |
| 28 const char kWiFiServiceErrorNotImplemented[] = |
| 29 "Error.WiFiService.NotImplemented"; |
28 const wchar_t kNwCategoryWizardRegKey[] = | 30 const wchar_t kNwCategoryWizardRegKey[] = |
29 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Network\\" | 31 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Network\\" |
30 L"NwCategoryWizard"; | 32 L"NwCategoryWizard"; |
31 const wchar_t kNwCategoryWizardRegValue[] = L"Show"; | 33 const wchar_t kNwCategoryWizardRegValue[] = L"Show"; |
32 const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved"; | 34 const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved"; |
33 const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete"; | 35 const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete"; |
34 const wchar_t kWlanApiDll[] = L"wlanapi.dll"; | 36 const wchar_t kWlanApiDll[] = L"wlanapi.dll"; |
35 | 37 |
36 // WlanApi function names | 38 // WlanApi function names |
37 const char kWlanConnect[] = "WlanConnect"; | 39 const char kWlanConnect[] = "WlanConnect"; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 base::ListValue* network_list) OVERRIDE; | 203 base::ListValue* network_list) OVERRIDE; |
202 | 204 |
203 virtual void RequestNetworkScan() OVERRIDE; | 205 virtual void RequestNetworkScan() OVERRIDE; |
204 | 206 |
205 virtual void StartConnect(const std::string& network_guid, | 207 virtual void StartConnect(const std::string& network_guid, |
206 std::string* error) OVERRIDE; | 208 std::string* error) OVERRIDE; |
207 | 209 |
208 virtual void StartDisconnect(const std::string& network_guid, | 210 virtual void StartDisconnect(const std::string& network_guid, |
209 std::string* error) OVERRIDE; | 211 std::string* error) OVERRIDE; |
210 | 212 |
| 213 virtual void GetKeyFromSystem(const std::string& network_guid, |
| 214 std::string* key_data, |
| 215 std::string* error) OVERRIDE; |
| 216 |
211 virtual void SetEventObservers( | 217 virtual void SetEventObservers( |
212 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 218 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
213 const NetworkGuidListCallback& networks_changed_observer, | 219 const NetworkGuidListCallback& networks_changed_observer, |
214 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; | 220 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; |
215 | 221 |
216 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} | 222 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} |
217 | 223 |
218 private: | 224 private: |
219 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification | 225 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
220 // on WiFiServiceImpl passed back as |context|. | 226 // on WiFiServiceImpl passed back as |context|. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 Frequency GetNormalizedFrequency(int frequency_in_mhz) const; | 359 Frequency GetNormalizedFrequency(int frequency_in_mhz) const; |
354 | 360 |
355 // Create |profile_xml| based on |network_properties|. | 361 // Create |profile_xml| based on |network_properties|. |
356 bool CreateProfile(const NetworkProperties& network_properties, | 362 bool CreateProfile(const NetworkProperties& network_properties, |
357 std::string* profile_xml); | 363 std::string* profile_xml); |
358 | 364 |
359 // Save temporary wireless profile for |network_guid|. | 365 // Save temporary wireless profile for |network_guid|. |
360 DWORD SaveTempProfile(const std::string& network_guid); | 366 DWORD SaveTempProfile(const std::string& network_guid); |
361 | 367 |
362 // Get previously stored |profile_xml| for |network_guid|. | 368 // Get previously stored |profile_xml| for |network_guid|. |
363 DWORD GetProfile(const std::string& network_guid, std::string* profile_xml); | 369 // If |get_plaintext_key| is true, and process has sufficient privileges, then |
| 370 // <sharedKey> data in |profile_xml| will be unprotected. |
| 371 DWORD GetProfile(const std::string& network_guid, |
| 372 bool get_plaintext_key, |
| 373 std::string* profile_xml); |
364 | 374 |
365 // Return true if there is previously stored profile xml for |network_guid|. | 375 // Return true if there is previously stored profile xml for |network_guid|. |
366 bool HaveProfile(const std::string& network_guid); | 376 bool HaveProfile(const std::string& network_guid); |
367 | 377 |
368 // Notify |network_list_changed_observer_| that list of visible networks has | 378 // Notify |network_list_changed_observer_| that list of visible networks has |
369 // changed to |networks|. | 379 // changed to |networks|. |
370 void NotifyNetworkListChanged(const NetworkList& networks); | 380 void NotifyNetworkListChanged(const NetworkList& networks); |
371 | 381 |
372 // Notify |networks_changed_observer_| that network |network_guid| status has | 382 // Notify |networks_changed_observer_| that network |network_guid| status has |
373 // changed. | 383 // changed. |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 error_code = Disconnect(); | 640 error_code = Disconnect(); |
631 if (error_code == ERROR_SUCCESS) { | 641 if (error_code == ERROR_SUCCESS) { |
632 NotifyNetworkChanged(network_guid); | 642 NotifyNetworkChanged(network_guid); |
633 return; | 643 return; |
634 } | 644 } |
635 } | 645 } |
636 } | 646 } |
637 CheckError(error_code, kWiFiServiceError, error); | 647 CheckError(error_code, kWiFiServiceError, error); |
638 } | 648 } |
639 | 649 |
| 650 void WiFiServiceImpl::GetKeyFromSystem(const std::string& network_guid, |
| 651 std::string* key_data, |
| 652 std::string* error) { |
| 653 DWORD error_code = EnsureInitialized(); |
| 654 if (CheckError(error_code, kWiFiServiceError, error)) |
| 655 return; |
| 656 |
| 657 std::string profile_xml; |
| 658 error_code = GetProfile(network_guid, true, &profile_xml); |
| 659 if (CheckError(error_code, kWiFiServiceError, error)) |
| 660 return; |
| 661 |
| 662 const char kSharedKeyElement[] = "sharedKey"; |
| 663 const char kProtectedElement[] = "protected"; |
| 664 const char kKeyMaterialElement[] = "keyMaterial"; |
| 665 |
| 666 // Quick check to verify presence of <sharedKey> element. |
| 667 if (profile_xml.find(kSharedKeyElement) == std::string::npos) { |
| 668 *error = kWiFiServiceError; |
| 669 return; |
| 670 } |
| 671 |
| 672 XmlReader reader; |
| 673 if (reader.Load(profile_xml)) { |
| 674 while (reader.Read()) { |
| 675 reader.SkipToElement(); |
| 676 if (reader.NodeName() == kSharedKeyElement) { |
| 677 while (reader.Read()) { |
| 678 reader.SkipToElement(); |
| 679 if (reader.NodeName() == kKeyMaterialElement) { |
| 680 reader.ReadElementContent(key_data); |
| 681 } else if (reader.NodeName() == kProtectedElement) { |
| 682 std::string protected_data; |
| 683 reader.ReadElementContent(&protected_data); |
| 684 // Without UAC privilege escalation call to |GetProfile| with |
| 685 // |WLAN_PROFILE_GET_PLAINTEXT_KEY| flag returns success, but has |
| 686 // protected keyMaterial. Report an error in this case. |
| 687 if (protected_data != "false") { |
| 688 *error = kWiFiServiceError; |
| 689 break; |
| 690 } |
| 691 } |
| 692 } |
| 693 return; |
| 694 } |
| 695 } |
| 696 } |
| 697 |
| 698 // Did not find passphrase in the profile. |
| 699 *error = kWiFiServiceError; |
| 700 } |
| 701 |
640 void WiFiServiceImpl::SetEventObservers( | 702 void WiFiServiceImpl::SetEventObservers( |
641 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 703 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
642 const NetworkGuidListCallback& networks_changed_observer, | 704 const NetworkGuidListCallback& networks_changed_observer, |
643 const NetworkGuidListCallback& network_list_changed_observer) { | 705 const NetworkGuidListCallback& network_list_changed_observer) { |
644 message_loop_proxy_.swap(message_loop_proxy); | 706 message_loop_proxy_.swap(message_loop_proxy); |
645 networks_changed_observer_ = networks_changed_observer; | 707 networks_changed_observer_ = networks_changed_observer; |
646 network_list_changed_observer_ = network_list_changed_observer; | 708 network_list_changed_observer_ = network_list_changed_observer; |
647 } | 709 } |
648 | 710 |
649 void WiFiServiceImpl::OnWlanNotificationCallback( | 711 void WiFiServiceImpl::OnWlanNotificationCallback( |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 } | 810 } |
749 | 811 |
750 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork( | 812 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork( |
751 std::string* connected_network_guid) { | 813 std::string* connected_network_guid) { |
752 // Find currently connected network. | 814 // Find currently connected network. |
753 DWORD error = FindConnectedNetwork(connected_network_guid); | 815 DWORD error = FindConnectedNetwork(connected_network_guid); |
754 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) { | 816 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) { |
755 if (error == ERROR_SUCCESS) { | 817 if (error == ERROR_SUCCESS) { |
756 SaveTempProfile(*connected_network_guid); | 818 SaveTempProfile(*connected_network_guid); |
757 std::string profile_xml; | 819 std::string profile_xml; |
758 error = GetProfile(*connected_network_guid, &profile_xml); | 820 error = GetProfile(*connected_network_guid, false, &profile_xml); |
759 if (error == ERROR_SUCCESS) { | 821 if (error == ERROR_SUCCESS) { |
760 saved_profiles_xml_[*connected_network_guid] = profile_xml; | 822 saved_profiles_xml_[*connected_network_guid] = profile_xml; |
761 } | 823 } |
762 } | 824 } |
763 } | 825 } |
764 return error; | 826 return error; |
765 } | 827 } |
766 | 828 |
767 void WiFiServiceImpl::SortNetworks(NetworkList* networks) { | 829 void WiFiServiceImpl::SortNetworks(NetworkList* networks) { |
768 networks->sort(NetworkProperties::OrderByType); | 830 networks->sort(NetworkProperties::OrderByType); |
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1435 WLAN_PROFILE_USER, | 1497 WLAN_PROFILE_USER, |
1436 true, | 1498 true, |
1437 NULL); | 1499 NULL); |
1438 } else { | 1500 } else { |
1439 error = ERROR_NOT_SUPPORTED; | 1501 error = ERROR_NOT_SUPPORTED; |
1440 } | 1502 } |
1441 return error; | 1503 return error; |
1442 } | 1504 } |
1443 | 1505 |
1444 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, | 1506 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, |
| 1507 bool get_plaintext_key, |
1445 std::string* profile_xml) { | 1508 std::string* profile_xml) { |
1446 if (client_ == NULL) { | 1509 if (client_ == NULL) { |
1447 NOTREACHED(); | 1510 NOTREACHED(); |
1448 return ERROR_NOINTERFACE; | 1511 return ERROR_NOINTERFACE; |
1449 } | 1512 } |
1450 | 1513 |
1451 DWORD error = ERROR_SUCCESS; | 1514 DWORD error = ERROR_SUCCESS; |
1452 base::string16 profile_name = ProfileNameFromGUID(network_guid); | 1515 base::string16 profile_name = ProfileNameFromGUID(network_guid); |
| 1516 DWORD flags = get_plaintext_key ? WLAN_PROFILE_GET_PLAINTEXT_KEY : 0; |
1453 LPWSTR str_profile_xml = NULL; | 1517 LPWSTR str_profile_xml = NULL; |
1454 error = WlanGetProfile_function_(client_, | 1518 error = WlanGetProfile_function_(client_, |
1455 &interface_guid_, | 1519 &interface_guid_, |
1456 profile_name.c_str(), | 1520 profile_name.c_str(), |
1457 NULL, | 1521 NULL, |
1458 &str_profile_xml, | 1522 &str_profile_xml, |
1459 NULL, | 1523 &flags, |
1460 NULL); | 1524 NULL); |
1461 | 1525 |
1462 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { | 1526 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { |
1463 *profile_xml = base::UTF16ToUTF8(str_profile_xml); | 1527 *profile_xml = base::UTF16ToUTF8(str_profile_xml); |
1464 } | 1528 } |
1465 // Clean up. | 1529 // Clean up. |
1466 if (str_profile_xml != NULL) { | 1530 if (str_profile_xml != NULL) { |
1467 WlanFreeMemory_function_(str_profile_xml); | 1531 WlanFreeMemory_function_(str_profile_xml); |
1468 } | 1532 } |
1469 | 1533 |
1470 return error; | 1534 return error; |
1471 } | 1535 } |
1472 | 1536 |
1473 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { | 1537 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { |
1474 DWORD error = ERROR_SUCCESS; | 1538 DWORD error = ERROR_SUCCESS; |
1475 std::string profile_xml; | 1539 std::string profile_xml; |
1476 return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS; | 1540 return GetProfile(network_guid, false, &profile_xml) == ERROR_SUCCESS; |
1477 } | 1541 } |
1478 | 1542 |
1479 bool WiFiServiceImpl::AuthEncryptionFromSecurity( | 1543 bool WiFiServiceImpl::AuthEncryptionFromSecurity( |
1480 const std::string& security, | 1544 const std::string& security, |
1481 std::string* authentication, | 1545 std::string* authentication, |
1482 std::string* encryption, | 1546 std::string* encryption, |
1483 std::string* key_type) const { | 1547 std::string* key_type) const { |
1484 if (security == onc::wifi::kNone) { | 1548 if (security == onc::wifi::kNone) { |
1485 *authentication = kAuthenticationOpen; | 1549 *authentication = kAuthenticationOpen; |
1486 *encryption = kEncryptionNone; | 1550 *encryption = kEncryptionNone; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1580 NetworkGuidList changed_networks(1, network_guid); | 1644 NetworkGuidList changed_networks(1, network_guid); |
1581 message_loop_proxy_->PostTask( | 1645 message_loop_proxy_->PostTask( |
1582 FROM_HERE, | 1646 FROM_HERE, |
1583 base::Bind(networks_changed_observer_, changed_networks)); | 1647 base::Bind(networks_changed_observer_, changed_networks)); |
1584 } | 1648 } |
1585 } | 1649 } |
1586 | 1650 |
1587 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } | 1651 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } |
1588 | 1652 |
1589 } // namespace wifi | 1653 } // namespace wifi |
OLD | NEW |