1 // Copyright 2014 The Chromium 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.
5 #include "chrome/browser/extensions/api/networking_private/networking_private_credentials_getter.h"
7 #include "base/base64.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
12 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/utility_process_host.h"
16 using content::BrowserThread;
17 using content::UtilityProcessHost;
18 using extensions::NetworkingPrivateCredentialsGetter;
22 class CredentialsGetterHostClient : public content::UtilityProcessHostClient {
24 explicit CredentialsGetterHostClient(const std::string& public_key);
26 virtual ~CredentialsGetterHostClient();
28 // UtilityProcessHostClient
29 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
30 virtual void OnProcessCrashed(int exit_code) OVERRIDE;
31 virtual void OnProcessLaunchFailed() OVERRIDE;
33 // IPC message handlers.
34 void OnGotCredentials(const std::string& key_data, bool success);
36 // Starts the utility process that gets wifi passphrase from system.
37 void StartProcessOnIOThread(
38 const std::string& network_guid,
39 const extensions::NetworkingPrivateServiceClient::CryptoVerify::
40 VerifyAndEncryptCredentialsCallback& callback);
43 // Public key used to encrypt results
44 std::vector<uint8> public_key_;
46 // Callback for reporting the result.
47 extensions::NetworkingPrivateServiceClient::CryptoVerify::
48 VerifyAndEncryptCredentialsCallback callback_;
50 DISALLOW_COPY_AND_ASSIGN(CredentialsGetterHostClient);
53 CredentialsGetterHostClient::CredentialsGetterHostClient(
54 const std::string& public_key)
55 : public_key_(public_key.begin(), public_key.end()) {
58 CredentialsGetterHostClient::~CredentialsGetterHostClient() {}
60 bool CredentialsGetterHostClient::OnMessageReceived(
61 const IPC::Message& message) {
63 IPC_BEGIN_MESSAGE_MAP(CredentialsGetterHostClient, message)
64 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotWiFiCredentials, OnGotCredentials)
65 IPC_MESSAGE_UNHANDLED(handled = false)
70 void CredentialsGetterHostClient::OnProcessCrashed(int exit_code) {
71 callback_.Run("", "Process Crashed");
74 void CredentialsGetterHostClient::OnProcessLaunchFailed() {
75 callback_.Run("", "Process Launch Failed");
78 void CredentialsGetterHostClient::OnGotCredentials(const std::string& key_data,
81 std::vector<uint8> ciphertext;
82 if (!networking_private_crypto::EncryptByteString(
83 public_key_, key_data, &ciphertext)) {
84 callback_.Run("", "Encrypt Credentials Failed");
88 std::string base64_encoded_key_data;
89 base::Base64Encode(std::string(ciphertext.begin(), ciphertext.end()),
90 &base64_encoded_key_data);
91 callback_.Run(base64_encoded_key_data, "");
93 callback_.Run("", "Get Credentials Failed");
97 void CredentialsGetterHostClient::StartProcessOnIOThread(
98 const std::string& network_guid,
99 const extensions::NetworkingPrivateServiceClient::CryptoVerify::
100 VerifyAndEncryptCredentialsCallback& callback) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102 UtilityProcessHost* host =
103 UtilityProcessHost::Create(this, base::MessageLoopProxy::current());
104 callback_ = callback;
105 host->ElevatePrivileges();
106 host->Send(new ChromeUtilityHostMsg_GetWiFiCredentials(network_guid));
111 namespace extensions {
113 class NetworkingPrivateCredentialsGetterWin
114 : public NetworkingPrivateCredentialsGetter {
116 NetworkingPrivateCredentialsGetterWin();
119 const std::string& network_guid,
120 const std::string& public_key,
121 const extensions::NetworkingPrivateServiceClient::CryptoVerify::
122 VerifyAndEncryptCredentialsCallback& callback) OVERRIDE;
125 virtual ~NetworkingPrivateCredentialsGetterWin();
127 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin);
130 NetworkingPrivateCredentialsGetterWin::NetworkingPrivateCredentialsGetterWin() {
133 void NetworkingPrivateCredentialsGetterWin::Start(
134 const std::string& network_guid,
135 const std::string& public_key,
136 const extensions::NetworkingPrivateServiceClient::CryptoVerify::
137 VerifyAndEncryptCredentialsCallback& callback) {
138 BrowserThread::PostTask(
141 base::Bind(&CredentialsGetterHostClient::StartProcessOnIOThread,
142 new CredentialsGetterHostClient(public_key),
147 NetworkingPrivateCredentialsGetterWin::
148 ~NetworkingPrivateCredentialsGetterWin() {}
150 NetworkingPrivateCredentialsGetter*
151 NetworkingPrivateCredentialsGetter::Create() {
152 return new NetworkingPrivateCredentialsGetterWin();
155 } // namespace extensions