keychain_password_mac.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.
  4. #include "components/os_crypt/keychain_password_mac.h"
  5. #import <Security/Security.h>
  6. #include "base/base64.h"
  7. #include "base/mac/mac_logging.h"
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "base/no_destructor.h"
  10. #include "base/rand_util.h"
  11. #include "build/branding_buildflags.h"
  12. #include "crypto/apple_keychain.h"
  13. using crypto::AppleKeychain;
  14. #if defined(ALLOW_RUNTIME_CONFIGURABLE_KEY_STORAGE)
  15. using KeychainNameContainerType = base::NoDestructor<std::string>;
  16. #else
  17. using KeychainNameContainerType = const base::NoDestructor<std::string>;
  18. #endif
  19. namespace {
  20. // These two strings ARE indeed user facing. But they are used to access
  21. // the encryption keyword. So as to not lose encrypted data when system
  22. // locale changes we DO NOT LOCALIZE.
  23. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  24. const char kDefaultServiceName[] = "Chrome Safe Storage";
  25. const char kDefaultAccountName[] = "Chrome";
  26. #else
  27. const char kDefaultServiceName[] = "Chromium Safe Storage";
  28. const char kDefaultAccountName[] = "Chromium";
  29. #endif
  30. // Generates a random password and adds it to the Keychain. The added password
  31. // is returned from the function. If an error occurs, an empty password is
  32. // returned.
  33. std::string AddRandomPasswordToKeychain(const AppleKeychain& keychain,
  34. const std::string& service_name,
  35. const std::string& account_name) {
  36. // Generate a password with 128 bits of randomness.
  37. const int kBytes = 128 / 8;
  38. std::string password;
  39. base::Base64Encode(base::RandBytesAsString(kBytes), &password);
  40. void* password_data =
  41. const_cast<void*>(static_cast<const void*>(password.data()));
  42. OSStatus error = keychain.AddGenericPassword(
  43. service_name.size(), service_name.data(), account_name.size(),
  44. account_name.data(), password.size(), password_data, NULL);
  45. if (error != noErr) {
  46. OSSTATUS_DLOG(ERROR, error) << "Keychain add failed";
  47. return std::string();
  48. }
  49. return password;
  50. }
  51. } // namespace
  52. // static
  53. KeychainPassword::KeychainNameType& KeychainPassword::GetServiceName() {
  54. static KeychainNameContainerType service_name(kDefaultServiceName);
  55. return *service_name;
  56. }
  57. // static
  58. KeychainPassword::KeychainNameType& KeychainPassword::GetAccountName() {
  59. static KeychainNameContainerType account_name(kDefaultAccountName);
  60. return *account_name;
  61. }
  62. KeychainPassword::KeychainPassword(const AppleKeychain& keychain)
  63. : keychain_(keychain) {}
  64. KeychainPassword::~KeychainPassword() = default;
  65. std::string KeychainPassword::GetPassword() const {
  66. UInt32 password_length = 0;
  67. void* password_data = nullptr;
  68. OSStatus error = keychain_.FindGenericPassword(
  69. GetServiceName().size(), GetServiceName().c_str(),
  70. GetAccountName().size(), GetAccountName().c_str(), &password_length,
  71. &password_data, nullptr);
  72. if (error == noErr) {
  73. std::string password =
  74. std::string(static_cast<char*>(password_data), password_length);
  75. keychain_.ItemFreeContent(password_data);
  76. return password;
  77. }
  78. if (error == errSecItemNotFound) {
  79. std::string password = AddRandomPasswordToKeychain(
  80. keychain_, GetServiceName(), GetAccountName());
  81. return password;
  82. }
  83. OSSTATUS_LOG(ERROR, error) << "Keychain lookup failed";
  84. return std::string();
  85. }