Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: components/wifi/wifi_service_win.cc

Issue 156943002: Add method GetKeyFromSystem to WiFiService on Windows and Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address codereview comments. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/wifi/wifi_service_mac.mm ('k') | components/wifi/wifi_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 if (!get_plaintext_key)
684 break;
685 } else if (reader.NodeName() == kProtectedElement) {
686 std::string protected_data;
687 reader.ReadElementContent(&protected_data);
688 if (get_plaintext_key && protected_data != "false") {
689 *error = kWiFiServiceError;
690 break;
afontan 2014/02/12 17:57:18 Interesting that the call can partially succeed de
mef 2014/02/12 22:26:22 Done.
691 }
692 }
693 }
694 return;
695 }
696 }
697 }
698
699 // Did not find passphrase in the profile.
700 *error = kWiFiServiceError;
701 }
702
640 void WiFiServiceImpl::SetEventObservers( 703 void WiFiServiceImpl::SetEventObservers(
641 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, 704 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
642 const NetworkGuidListCallback& networks_changed_observer, 705 const NetworkGuidListCallback& networks_changed_observer,
643 const NetworkGuidListCallback& network_list_changed_observer) { 706 const NetworkGuidListCallback& network_list_changed_observer) {
644 message_loop_proxy_.swap(message_loop_proxy); 707 message_loop_proxy_.swap(message_loop_proxy);
645 networks_changed_observer_ = networks_changed_observer; 708 networks_changed_observer_ = networks_changed_observer;
646 network_list_changed_observer_ = network_list_changed_observer; 709 network_list_changed_observer_ = network_list_changed_observer;
647 } 710 }
648 711
649 void WiFiServiceImpl::OnWlanNotificationCallback( 712 void WiFiServiceImpl::OnWlanNotificationCallback(
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 } 811 }
749 812
750 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork( 813 DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork(
751 std::string* connected_network_guid) { 814 std::string* connected_network_guid) {
752 // Find currently connected network. 815 // Find currently connected network.
753 DWORD error = FindConnectedNetwork(connected_network_guid); 816 DWORD error = FindConnectedNetwork(connected_network_guid);
754 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) { 817 if (error == ERROR_SUCCESS && !connected_network_guid->empty()) {
755 if (error == ERROR_SUCCESS) { 818 if (error == ERROR_SUCCESS) {
756 SaveTempProfile(*connected_network_guid); 819 SaveTempProfile(*connected_network_guid);
757 std::string profile_xml; 820 std::string profile_xml;
758 error = GetProfile(*connected_network_guid, &profile_xml); 821 error = GetProfile(*connected_network_guid, false, &profile_xml);
759 if (error == ERROR_SUCCESS) { 822 if (error == ERROR_SUCCESS) {
760 saved_profiles_xml_[*connected_network_guid] = profile_xml; 823 saved_profiles_xml_[*connected_network_guid] = profile_xml;
761 } 824 }
762 } 825 }
763 } 826 }
764 return error; 827 return error;
765 } 828 }
766 829
767 void WiFiServiceImpl::SortNetworks(NetworkList* networks) { 830 void WiFiServiceImpl::SortNetworks(NetworkList* networks) {
768 networks->sort(NetworkProperties::OrderByType); 831 networks->sort(NetworkProperties::OrderByType);
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 WLAN_PROFILE_USER, 1498 WLAN_PROFILE_USER,
1436 true, 1499 true,
1437 NULL); 1500 NULL);
1438 } else { 1501 } else {
1439 error = ERROR_NOT_SUPPORTED; 1502 error = ERROR_NOT_SUPPORTED;
1440 } 1503 }
1441 return error; 1504 return error;
1442 } 1505 }
1443 1506
1444 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, 1507 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid,
1508 bool get_plaintext_key,
1445 std::string* profile_xml) { 1509 std::string* profile_xml) {
1446 if (client_ == NULL) { 1510 if (client_ == NULL) {
1447 NOTREACHED(); 1511 NOTREACHED();
1448 return ERROR_NOINTERFACE; 1512 return ERROR_NOINTERFACE;
1449 } 1513 }
1450 1514
1451 DWORD error = ERROR_SUCCESS; 1515 DWORD error = ERROR_SUCCESS;
1452 base::string16 profile_name = ProfileNameFromGUID(network_guid); 1516 base::string16 profile_name = ProfileNameFromGUID(network_guid);
1517 DWORD flags = get_plaintext_key ? WLAN_PROFILE_GET_PLAINTEXT_KEY : 0;
1453 LPWSTR str_profile_xml = NULL; 1518 LPWSTR str_profile_xml = NULL;
1454 error = WlanGetProfile_function_(client_, 1519 error = WlanGetProfile_function_(client_,
1455 &interface_guid_, 1520 &interface_guid_,
1456 profile_name.c_str(), 1521 profile_name.c_str(),
1457 NULL, 1522 NULL,
1458 &str_profile_xml, 1523 &str_profile_xml,
1459 NULL, 1524 &flags,
1460 NULL); 1525 NULL);
1461 1526
1462 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { 1527 if (error == ERROR_SUCCESS && str_profile_xml != NULL) {
1463 *profile_xml = base::UTF16ToUTF8(str_profile_xml); 1528 *profile_xml = base::UTF16ToUTF8(str_profile_xml);
1464 } 1529 }
1465 // Clean up. 1530 // Clean up.
1466 if (str_profile_xml != NULL) { 1531 if (str_profile_xml != NULL) {
1467 WlanFreeMemory_function_(str_profile_xml); 1532 WlanFreeMemory_function_(str_profile_xml);
1468 } 1533 }
1469 1534
1470 return error; 1535 return error;
1471 } 1536 }
1472 1537
1473 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { 1538 bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) {
1474 DWORD error = ERROR_SUCCESS; 1539 DWORD error = ERROR_SUCCESS;
1475 std::string profile_xml; 1540 std::string profile_xml;
1476 return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS; 1541 return GetProfile(network_guid, false, &profile_xml) == ERROR_SUCCESS;
1477 } 1542 }
1478 1543
1479 bool WiFiServiceImpl::AuthEncryptionFromSecurity( 1544 bool WiFiServiceImpl::AuthEncryptionFromSecurity(
1480 const std::string& security, 1545 const std::string& security,
1481 std::string* authentication, 1546 std::string* authentication,
1482 std::string* encryption, 1547 std::string* encryption,
1483 std::string* key_type) const { 1548 std::string* key_type) const {
1484 if (security == onc::wifi::kNone) { 1549 if (security == onc::wifi::kNone) {
1485 *authentication = kAuthenticationOpen; 1550 *authentication = kAuthenticationOpen;
1486 *encryption = kEncryptionNone; 1551 *encryption = kEncryptionNone;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1580 NetworkGuidList changed_networks(1, network_guid); 1645 NetworkGuidList changed_networks(1, network_guid);
1581 message_loop_proxy_->PostTask( 1646 message_loop_proxy_->PostTask(
1582 FROM_HERE, 1647 FROM_HERE,
1583 base::Bind(networks_changed_observer_, changed_networks)); 1648 base::Bind(networks_changed_observer_, changed_networks));
1584 } 1649 }
1585 } 1650 }
1586 1651
1587 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } 1652 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); }
1588 1653
1589 } // namespace wifi 1654 } // namespace wifi
OLDNEW
« no previous file with comments | « components/wifi/wifi_service_mac.mm ('k') | components/wifi/wifi_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698