apple_keychain.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2012 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. #ifndef CRYPTO_APPLE_KEYCHAIN_H_
  5. #define CRYPTO_APPLE_KEYCHAIN_H_
  6. #include <Security/Security.h>
  7. #include "build/build_config.h"
  8. #include "crypto/crypto_export.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace crypto {
  11. #if BUILDFLAG(IS_IOS)
  12. using AppleSecKeychainItemRef = void*;
  13. #else
  14. using AppleSecKeychainItemRef = SecKeychainItemRef;
  15. #endif
  16. // Wraps the KeychainServices API in a very thin layer, to allow it to be
  17. // mocked out for testing.
  18. // See Keychain Services documentation for function documentation, as these call
  19. // through directly to their Keychain Services equivalents (Foo ->
  20. // SecKeychainFoo). The only exception is Free, which should be used for
  21. // anything returned from this class that would normally be freed with
  22. // CFRelease (to aid in testing).
  23. class CRYPTO_EXPORT AppleKeychain {
  24. public:
  25. AppleKeychain();
  26. AppleKeychain(const AppleKeychain&) = delete;
  27. AppleKeychain& operator=(const AppleKeychain&) = delete;
  28. virtual ~AppleKeychain();
  29. virtual OSStatus FindGenericPassword(UInt32 service_name_length,
  30. const char* service_name,
  31. UInt32 account_name_length,
  32. const char* account_name,
  33. UInt32* password_length,
  34. void** password_data,
  35. AppleSecKeychainItemRef* item) const;
  36. virtual OSStatus ItemFreeContent(void* data) const;
  37. virtual OSStatus AddGenericPassword(UInt32 service_name_length,
  38. const char* service_name,
  39. UInt32 account_name_length,
  40. const char* account_name,
  41. UInt32 password_length,
  42. const void* password_data,
  43. AppleSecKeychainItemRef* item) const;
  44. #if BUILDFLAG(IS_MAC)
  45. virtual OSStatus ItemDelete(AppleSecKeychainItemRef item) const;
  46. #endif // !BUILDFLAG(IS_MAC)
  47. };
  48. #if BUILDFLAG(IS_MAC)
  49. // Sets whether Keychain Services is permitted to display UI if needed by
  50. // calling SecKeychainSetUserInteractionAllowed. This operates in a scoped
  51. // fashion: on destruction, the previous state will be restored. This is useful
  52. // to interact with the Keychain on a best-effort basis, without displaying any
  53. // Keychain Services UI (which is beyond the application's control) to the user.
  54. class CRYPTO_EXPORT ScopedKeychainUserInteractionAllowed {
  55. public:
  56. ScopedKeychainUserInteractionAllowed(
  57. const ScopedKeychainUserInteractionAllowed&) = delete;
  58. ScopedKeychainUserInteractionAllowed& operator=(
  59. const ScopedKeychainUserInteractionAllowed&) = delete;
  60. explicit ScopedKeychainUserInteractionAllowed(Boolean allowed,
  61. OSStatus* status = nullptr);
  62. ~ScopedKeychainUserInteractionAllowed();
  63. private:
  64. absl::optional<Boolean> was_allowed_;
  65. };
  66. #endif // BUILDFLAG(IS_MAC)
  67. } // namespace crypto
  68. #endif // CRYPTO_APPLE_KEYCHAIN_H_