Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / networking_private / networking_private_credentials_getter_win.cc
index 16fb3ba..4832eb6 100644 (file)
@@ -6,10 +6,10 @@
 
 #include "base/base64.h"
 #include "base/bind.h"
-#include "base/memory/scoped_handle.h"
 #include "base/message_loop/message_loop.h"
 #include "base/threading/sequenced_worker_pool.h"
-#include "chrome/common/chrome_utility_messages.h"
+#include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
+#include "chrome/common/extensions/chrome_utility_extensions_messages.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/utility_process_host.h"
 
@@ -21,7 +21,7 @@ namespace {
 
 class CredentialsGetterHostClient : public content::UtilityProcessHostClient {
  public:
-  CredentialsGetterHostClient();
+  explicit CredentialsGetterHostClient(const std::string& public_key);
 
   virtual ~CredentialsGetterHostClient();
 
@@ -31,17 +31,18 @@ class CredentialsGetterHostClient : public content::UtilityProcessHostClient {
   virtual void OnProcessLaunchFailed() OVERRIDE;
 
   // IPC message handlers.
-  void OnGotEncryptedCredentials(const std::vector<uint8>& key_data,
-                                 bool success);
+  void OnGotCredentials(const std::string& key_data, bool success);
 
   // Starts the utility process that gets wifi passphrase from system.
   void StartProcessOnIOThread(
       const std::string& network_guid,
-      const std::string& public_key,
       const extensions::NetworkingPrivateServiceClient::CryptoVerify::
           VerifyAndEncryptCredentialsCallback& callback);
 
  private:
+  // Public key used to encrypt results
+  std::vector<uint8> public_key_;
+
   // Callback for reporting the result.
   extensions::NetworkingPrivateServiceClient::CryptoVerify::
       VerifyAndEncryptCredentialsCallback callback_;
@@ -49,7 +50,10 @@ class CredentialsGetterHostClient : public content::UtilityProcessHostClient {
   DISALLOW_COPY_AND_ASSIGN(CredentialsGetterHostClient);
 };
 
-CredentialsGetterHostClient::CredentialsGetterHostClient() {}
+CredentialsGetterHostClient::CredentialsGetterHostClient(
+    const std::string& public_key)
+    : public_key_(public_key.begin(), public_key.end()) {
+}
 
 CredentialsGetterHostClient::~CredentialsGetterHostClient() {}
 
@@ -57,8 +61,7 @@ bool CredentialsGetterHostClient::OnMessageReceived(
     const IPC::Message& message) {
   bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(CredentialsGetterHostClient, message)
-  IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotEncryptedWiFiCredentials,
-                      OnGotEncryptedCredentials)
+  IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotWiFiCredentials, OnGotCredentials)
   IPC_MESSAGE_UNHANDLED(handled = false)
   IPC_END_MESSAGE_MAP()
   return handled;
@@ -72,12 +75,18 @@ void CredentialsGetterHostClient::OnProcessLaunchFailed() {
   callback_.Run("", "Process Launch Failed");
 }
 
-void CredentialsGetterHostClient::OnGotEncryptedCredentials(
-    const std::vector<uint8>& key_data,
-    bool success) {
+void CredentialsGetterHostClient::OnGotCredentials(const std::string& key_data,
+                                                   bool success) {
   if (success) {
+    std::vector<uint8> ciphertext;
+    if (!networking_private_crypto::EncryptByteString(
+            public_key_, key_data, &ciphertext)) {
+      callback_.Run("", "Encrypt Credentials Failed");
+      return;
+    }
+
     std::string base64_encoded_key_data;
-    base::Base64Encode(std::string(key_data.begin(), key_data.end()),
+    base::Base64Encode(std::string(ciphertext.begin(), ciphertext.end()),
                        &base64_encoded_key_data);
     callback_.Run(base64_encoded_key_data, "");
   } else {
@@ -87,17 +96,14 @@ void CredentialsGetterHostClient::OnGotEncryptedCredentials(
 
 void CredentialsGetterHostClient::StartProcessOnIOThread(
     const std::string& network_guid,
-    const std::string& public_key,
     const extensions::NetworkingPrivateServiceClient::CryptoVerify::
         VerifyAndEncryptCredentialsCallback& callback) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
-  std::vector<uint8> public_key_data(public_key.begin(), public_key.end());
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
   UtilityProcessHost* host =
       UtilityProcessHost::Create(this, base::MessageLoopProxy::current());
   callback_ = callback;
   host->ElevatePrivileges();
-  host->Send(new ChromeUtilityHostMsg_GetAndEncryptWiFiCredentials(
-      network_guid, public_key_data));
+  host->Send(new ChromeUtilityHostMsg_GetWiFiCredentials(network_guid));
 }
 
 }  // namespace
@@ -133,9 +139,8 @@ void NetworkingPrivateCredentialsGetterWin::Start(
       BrowserThread::IO,
       FROM_HERE,
       base::Bind(&CredentialsGetterHostClient::StartProcessOnIOThread,
-                 new CredentialsGetterHostClient(),
+                 new CredentialsGetterHostClient(public_key),
                  network_guid,
-                 public_key,
                  callback));
 }