Chromium Code Reviews| Index: chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_mac.cc |
| diff --git a/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_mac.cc b/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_mac.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..afc58bf1bab073d4c2324e46d48540412f53c92c |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_mac.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/networking_private/networking_private_credentials_getter.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/bind.h" |
| +#include "chrome/browser/extensions/api/networking_private/networking_private_crypto.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +#include <Security/Security.h> |
| + |
| +const char kErrorNotFound[] = "Error.NotFound"; |
| +const char kErrorEncryption[] = "Error.Encryption"; |
| + |
| +using content::BrowserThread; |
| + |
| +namespace extensions { |
| + |
| +class NetworkingPrivateCredentialsGetterMac |
| + : public NetworkingPrivateCredentialsGetter { |
| + public: |
| + explicit NetworkingPrivateCredentialsGetterMac( |
| + const GetCredentialsCallback& callback); |
| + |
| + virtual void Start(const std::string& network_guid, |
| + const std::string& public_key) OVERRIDE; |
| + |
| + virtual void ReportResult(const std::string& key_data, |
| + const std::string& error) OVERRIDE; |
| + |
| + private: |
| + |
| + virtual ~NetworkingPrivateCredentialsGetterMac(); |
| + |
| + // The callback. |
| + GetCredentialsCallback callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterMac); |
| +}; |
| + |
| +NetworkingPrivateCredentialsGetterMac::NetworkingPrivateCredentialsGetterMac( |
| + const GetCredentialsCallback& callback) : callback_(callback) { |
|
tbarzic
2014/02/07 22:43:02
nit:
move ': callback_...' to new line
mef
2014/02/11 23:20:05
Done.
|
| +} |
| + |
| +void NetworkingPrivateCredentialsGetterMac::Start( |
| + const std::string& network_guid, |
| + const std::string& public_key) { |
| + static const char kAirPortServiceName[] = "AirPort"; |
| + |
| + UInt32 password_length = 0; |
| + void *password_data = NULL; |
| + OSStatus status = noErr; |
| + status = SecKeychainFindGenericPassword(NULL, |
| + strlen(kAirPortServiceName), |
| + kAirPortServiceName, |
| + network_guid.length(), |
| + network_guid.c_str(), |
| + &password_length, |
| + &password_data, |
| + NULL); |
| + |
| + |
| + if (status != noErr || !password_data) { |
| + ReportResult("", kErrorNotFound); |
| + return; |
| + } |
| + |
| + std::string key_data(reinterpret_cast<char*>(password_data), password_length); |
| + SecKeychainItemFreeContent(NULL, password_data); |
| + |
| + NetworkingPrivateCrypto crypto; |
| + std::string ciphertext; |
| + if (!crypto.EncryptByteString(public_key, key_data, &ciphertext)) { |
| + ReportResult("", kErrorEncryption); |
| + return; |
| + } |
| + |
| + std::string base64_encoded_ciphertext; |
| + base::Base64Encode(ciphertext, &base64_encoded_ciphertext); |
| + ReportResult(base64_encoded_ciphertext, ""); |
| +} |
| + |
| +void NetworkingPrivateCredentialsGetterMac::ReportResult( |
| + const std::string& key_data, |
| + const std::string& error) { |
| + if (!callback_.is_null()) { |
| + BrowserThread::PostTask(BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(callback_, key_data, error)); |
| + } |
| +} |
| + |
| +NetworkingPrivateCredentialsGetterMac::~NetworkingPrivateCredentialsGetterMac() |
| +{ } |
| + |
| +NetworkingPrivateCredentialsGetter* NetworkingPrivateCredentialsGetter::Create( |
| + const GetCredentialsCallback& callback) { |
| + return new NetworkingPrivateCredentialsGetterMac(callback); |
| +} |
| + |
| +} // namespace extensions |