| 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 bool get_plaintext_key, |
| 215 std::string* key_data, |
| 216 std::string* error) OVERRIDE; |
| 217 |
| 211 virtual void SetEventObservers( | 218 virtual void SetEventObservers( |
| 212 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 219 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 213 const NetworkGuidListCallback& networks_changed_observer, | 220 const NetworkGuidListCallback& networks_changed_observer, |
| 214 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; | 221 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; |
| 215 | 222 |
| 216 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} | 223 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} |
| 217 | 224 |
| 218 private: | 225 private: |
| 219 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification | 226 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
| 220 // on WiFiServiceImpl passed back as |context|. | 227 // 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; | 360 Frequency GetNormalizedFrequency(int frequency_in_mhz) const; |
| 354 | 361 |
| 355 // Create |profile_xml| based on |network_properties|. | 362 // Create |profile_xml| based on |network_properties|. |
| 356 bool CreateProfile(const NetworkProperties& network_properties, | 363 bool CreateProfile(const NetworkProperties& network_properties, |
| 357 std::string* profile_xml); | 364 std::string* profile_xml); |
| 358 | 365 |
| 359 // Save temporary wireless profile for |network_guid|. | 366 // Save temporary wireless profile for |network_guid|. |
| 360 DWORD SaveTempProfile(const std::string& network_guid); | 367 DWORD SaveTempProfile(const std::string& network_guid); |
| 361 | 368 |
| 362 // Get previously stored |profile_xml| for |network_guid|. | 369 // Get previously stored |profile_xml| for |network_guid|. |
| 363 DWORD GetProfile(const std::string& network_guid, std::string* profile_xml); | 370 // If |get_plaintext_key| is true, and process has sufficient privileges, then |
| 371 // <sharedKey> data in |profile_xml| will be unprotected. |
| 372 DWORD GetProfile(const std::string& network_guid, |
| 373 bool get_plaintext_key, |
| 374 std::string* profile_xml); |
| 364 | 375 |
| 365 // Return true if there is previously stored profile xml for |network_guid|. | 376 // Return true if there is previously stored profile xml for |network_guid|. |
| 366 bool HaveProfile(const std::string& network_guid); | 377 bool HaveProfile(const std::string& network_guid); |
| 367 | 378 |
| 368 // Notify |network_list_changed_observer_| that list of visible networks has | 379 // Notify |network_list_changed_observer_| that list of visible networks has |
| 369 // changed to |networks|. | 380 // changed to |networks|. |
| 370 void NotifyNetworkListChanged(const NetworkList& networks); | 381 void NotifyNetworkListChanged(const NetworkList& networks); |
| 371 | 382 |
| 372 // Notify |networks_changed_observer_| that network |network_guid| status has | 383 // Notify |networks_changed_observer_| that network |network_guid| status has |
| 373 // changed. | 384 // changed. |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 error_code = Disconnect(); | 641 error_code = Disconnect(); |
| 631 if (error_code == ERROR_SUCCESS) { | 642 if (error_code == ERROR_SUCCESS) { |
| 632 NotifyNetworkChanged(network_guid); | 643 NotifyNetworkChanged(network_guid); |
| 633 return; | 644 return; |
| 634 } | 645 } |
| 635 } | 646 } |
| 636 } | 647 } |
| 637 CheckError(error_code, kWiFiServiceError, error); | 648 CheckError(error_code, kWiFiServiceError, error); |
| 638 } | 649 } |
| 639 | 650 |
| 651 void WiFiServiceImpl::GetKeyFromSystem(const std::string& network_guid, |
| 652 bool get_plaintext_key, |
| 653 std::string* key_data, |
| 654 std::string* error) { |
| 655 DWORD error_code = EnsureInitialized(); |
| 656 if (CheckError(error_code, kWiFiServiceError, error)) |
| 657 return; |
| 658 |
| 659 std::string profile_xml; |
| 660 error_code = GetProfile(network_guid, get_plaintext_key, &profile_xml); |
| 661 if (CheckError(error_code, kWiFiServiceError, error)) |
| 662 return; |
| 663 |
| 664 const char kSharedKeyElement[] = "sharedKey"; |
| 665 const char kProtectedElement[] = "protected"; |
| 666 const char kKeyMaterialElement[] = "keyMaterial"; |
| 667 |
| 668 // Quick check to verify presence of <sharedKey> element. |
| 669 if (profile_xml.find(kSharedKeyElement) == std::string::npos) { |
| 670 *error = kWiFiServiceError; |
| 671 return; |
| 672 } |
| 673 |
| 674 XmlReader reader; |
| 675 if (reader.Load(profile_xml)) { |
| 676 while(reader.Read()) { |
| 677 reader.SkipToElement(); |
| 678 if (reader.NodeName() == kSharedKeyElement) { |
| 679 while(reader.Read()) { |
| 680 reader.SkipToElement(); |
| 681 if (reader.NodeName() == kKeyMaterialElement) { |
| 682 reader.ReadElementContent(key_data); |
| 683 } |
| 684 if (reader.NodeName() == kProtectedElement) { |
| 685 std::string protected_data; |
| 686 reader.ReadElementContent(&protected_data); |
| 687 if (get_plaintext_key && protected_data != "false") |
| 688 *error = kWiFiServiceError; |
| 689 } |
| 690 } |
| 691 return; |
| 692 } |
| 693 } |
| 694 } |
| 695 |
| 696 // Did not find passphrase in the profile. |
| 697 *error = kWiFiServiceError; |
| 698 } |
| 699 |
| 640 void WiFiServiceImpl::SetEventObservers( | 700 void WiFiServiceImpl::SetEventObservers( |
| 641 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 701 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 642 const NetworkGuidListCallback& networks_changed_observer, | 702 const NetworkGuidListCallback& networks_changed_observer, |
| 643 const NetworkGuidListCallback& network_list_changed_observer) { | 703 const NetworkGuidListCallback& network_list_changed_observer) { |
| 644 message_loop_proxy_.swap(message_loop_proxy); | 704 message_loop_proxy_.swap(message_loop_proxy); |
| 645 networks_changed_observer_ = networks_changed_observer; | 705 networks_changed_observer_ = networks_changed_observer; |
| 646 network_list_changed_observer_ = network_list_changed_observer; | 706 network_list_changed_observer_ = network_list_changed_observer; |
| 647 } | 707 } |
| 648 | 708 |
| 649 void WiFiServiceImpl::OnWlanNotificationCallback( | 709 void WiFiServiceImpl::OnWlanNotificationCallback( |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 } | 808 } |
| 749 | 809 |
| 750 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork( | 810 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork( |
| 751 std::string* connected_network_guid) { | 811 std::string* connected_network_guid) { |
| 752 // Find currently connected network. | 812 // Find currently connected network. |
| 753 DWORD error = FindConnectedNetwork(connected_network_guid); | 813 DWORD error = FindConnectedNetwork(connected_network_guid); |
| 754 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) { | 814 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) { |
| 755 if (error == ERROR_SUCCESS) { | 815 if (error == ERROR_SUCCESS) { |
| 756 SaveTempProfile(*connected_network_guid); | 816 SaveTempProfile(*connected_network_guid); |
| 757 std::string profile_xml; | 817 std::string profile_xml; |
| 758 error = GetProfile(*connected_network_guid, &profile_xml); | 818 error = GetProfile(*connected_network_guid, false, &profile_xml); |
| 759 if (error == ERROR_SUCCESS) { | 819 if (error == ERROR_SUCCESS) { |
| 760 saved_profiles_xml_[*connected_network_guid] = profile_xml; | 820 saved_profiles_xml_[*connected_network_guid] = profile_xml; |
| 761 } | 821 } |
| 762 } | 822 } |
| 763 } | 823 } |
| 764 return error; | 824 return error; |
| 765 } | 825 } |
| 766 | 826 |
| 767 void WiFiServiceImpl::SortNetworks(NetworkList* networks) { | 827 void WiFiServiceImpl::SortNetworks(NetworkList* networks) { |
| 768 networks->sort(NetworkProperties::OrderByType); | 828 networks->sort(NetworkProperties::OrderByType); |
| (...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1435 WLAN_PROFILE_USER, | 1495 WLAN_PROFILE_USER, |
| 1436 true, | 1496 true, |
| 1437 NULL); | 1497 NULL); |
| 1438 } else { | 1498 } else { |
| 1439 error = ERROR_NOT_SUPPORTED; | 1499 error = ERROR_NOT_SUPPORTED; |
| 1440 } | 1500 } |
| 1441 return error; | 1501 return error; |
| 1442 } | 1502 } |
| 1443 | 1503 |
| 1444 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, | 1504 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, |
| 1505 bool get_plaintext_key, |
| 1445 std::string* profile_xml) { | 1506 std::string* profile_xml) { |
| 1446 if (client_ == NULL) { | 1507 if (client_ == NULL) { |
| 1447 NOTREACHED(); | 1508 NOTREACHED(); |
| 1448 return ERROR_NOINTERFACE; | 1509 return ERROR_NOINTERFACE; |
| 1449 } | 1510 } |
| 1450 | 1511 |
| 1451 DWORD error = ERROR_SUCCESS; | 1512 DWORD error = ERROR_SUCCESS; |
| 1452 base::string16 profile_name = ProfileNameFromGUID(network_guid); | 1513 base::string16 profile_name = ProfileNameFromGUID(network_guid); |
| 1514 DWORD flags = get_plaintext_key ? WLAN_PROFILE_GET_PLAINTEXT_KEY : 0; |
| 1453 LPWSTR str_profile_xml = NULL; | 1515 LPWSTR str_profile_xml = NULL; |
| 1454 error = WlanGetProfile_function_(client_, | 1516 error = WlanGetProfile_function_(client_, |
| 1455 &interface_guid_, | 1517 &interface_guid_, |
| 1456 profile_name.c_str(), | 1518 profile_name.c_str(), |
| 1457 NULL, | 1519 NULL, |
| 1458 &str_profile_xml, | 1520 &str_profile_xml, |
| 1459 NULL, | 1521 &flags, |
| 1460 NULL); | 1522 NULL); |
| 1461 | 1523 |
| 1462 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { | 1524 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { |
| 1463 *profile_xml = base::UTF16ToUTF8(str_profile_xml); | 1525 *profile_xml = base::UTF16ToUTF8(str_profile_xml); |
| 1464 } | 1526 } |
| 1465 // Clean up. | 1527 // Clean up. |
| 1466 if (str_profile_xml != NULL) { | 1528 if (str_profile_xml != NULL) { |
| 1467 WlanFreeMemory_function_(str_profile_xml); | 1529 WlanFreeMemory_function_(str_profile_xml); |
| 1468 } | 1530 } |
| 1469 | 1531 |
| 1470 return error; | 1532 return error; |
| 1471 } | 1533 } |
| 1472 | 1534 |
| 1473 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { | 1535 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { |
| 1474 DWORD error = ERROR_SUCCESS; | 1536 DWORD error = ERROR_SUCCESS; |
| 1475 std::string profile_xml; | 1537 std::string profile_xml; |
| 1476 return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS; | 1538 return GetProfile(network_guid, false, &profile_xml) == ERROR_SUCCESS; |
| 1477 } | 1539 } |
| 1478 | 1540 |
| 1479 bool WiFiServiceImpl::AuthEncryptionFromSecurity( | 1541 bool WiFiServiceImpl::AuthEncryptionFromSecurity( |
| 1480 const std::string& security, | 1542 const std::string& security, |
| 1481 std::string* authentication, | 1543 std::string* authentication, |
| 1482 std::string* encryption, | 1544 std::string* encryption, |
| 1483 std::string* key_type) const { | 1545 std::string* key_type) const { |
| 1484 if (security == onc::wifi::kNone) { | 1546 if (security == onc::wifi::kNone) { |
| 1485 *authentication = kAuthenticationOpen; | 1547 *authentication = kAuthenticationOpen; |
| 1486 *encryption = kEncryptionNone; | 1548 *encryption = kEncryptionNone; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1580 NetworkGuidList changed_networks(1, network_guid); | 1642 NetworkGuidList changed_networks(1, network_guid); |
| 1581 message_loop_proxy_->PostTask( | 1643 message_loop_proxy_->PostTask( |
| 1582 FROM_HERE, | 1644 FROM_HERE, |
| 1583 base::Bind(networks_changed_observer_, changed_networks)); | 1645 base::Bind(networks_changed_observer_, changed_networks)); |
| 1584 } | 1646 } |
| 1585 } | 1647 } |
| 1586 | 1648 |
| 1587 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } | 1649 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } |
| 1588 | 1650 |
| 1589 } // namespace wifi | 1651 } // namespace wifi |
| OLD | NEW |