libsecret_util_linux.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 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 COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_LINUX_H_
  5. #define COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_LINUX_H_
  6. #include <libsecret/secret.h>
  7. #include <list>
  8. #include <string>
  9. #include "base/component_export.h"
  10. #include "base/memory/raw_ptr.h"
  11. // Utility for dynamically loading libsecret.
  12. class LibsecretLoader {
  13. public:
  14. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_item_get_attributes)
  15. secret_item_get_attributes;
  16. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_item_get_created)
  17. secret_item_get_created;
  18. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_item_get_modified)
  19. secret_item_get_modified;
  20. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_item_get_secret)
  21. secret_item_get_secret;
  22. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_item_load_secret_sync)
  23. secret_item_load_secret_sync;
  24. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_password_clear_sync)
  25. secret_password_clear_sync;
  26. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_password_store_sync)
  27. secret_password_store_sync;
  28. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_service_search_sync)
  29. secret_service_search_sync;
  30. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_value_get_text)
  31. secret_value_get_text;
  32. static COMPONENT_EXPORT(OS_CRYPT) decltype(&::secret_value_unref)
  33. secret_value_unref;
  34. // Wrapper for secret_service_search_sync that prevents common leaks. See
  35. // https://crbug.com/393395.
  36. class COMPONENT_EXPORT(OS_CRYPT) SearchHelper {
  37. public:
  38. SearchHelper();
  39. SearchHelper(const SearchHelper&) = delete;
  40. SearchHelper& operator=(const SearchHelper&) = delete;
  41. ~SearchHelper();
  42. // Search must be called exactly once for success() and results() to be
  43. // populated.
  44. void Search(const SecretSchema* schema, GHashTable* attrs, int flags);
  45. bool success() { return !error_; }
  46. GList* results() { return results_; }
  47. GError* error() { return error_; }
  48. private:
  49. // |results_| and |error_| are C-style objects owned by this instance.
  50. raw_ptr<GList> results_ = nullptr;
  51. GError* error_ = nullptr;
  52. };
  53. LibsecretLoader() = delete;
  54. LibsecretLoader(const LibsecretLoader&) = delete;
  55. LibsecretLoader& operator=(const LibsecretLoader&) = delete;
  56. // Loads the libsecret library and checks that it responds to queries.
  57. // Returns false if either step fails.
  58. // Repeated calls check the responsiveness every time, but do not load the
  59. // the library again if already successful.
  60. static COMPONENT_EXPORT(OS_CRYPT) bool EnsureLibsecretLoaded();
  61. // Ensure that the default keyring is accessible. This won't prevent the user
  62. // from locking their keyring while Chrome is running.
  63. static COMPONENT_EXPORT(OS_CRYPT) void EnsureKeyringUnlocked();
  64. protected:
  65. static COMPONENT_EXPORT(OS_CRYPT) bool libsecret_loaded_;
  66. private:
  67. struct FunctionInfo {
  68. const char* name;
  69. void** pointer;
  70. };
  71. static const FunctionInfo kFunctions[];
  72. // Load the libsecret binaries. Returns true on success.
  73. // If successful, the result is cached and the function can be safely called
  74. // multiple times.
  75. // Checking |LibsecretIsAvailable| is necessary after this to verify that the
  76. // service responds to queries.
  77. static bool LoadLibsecret();
  78. // True if the libsecret binaries have been loaded and the library responds
  79. // to queries.
  80. static bool LibsecretIsAvailable();
  81. };
  82. class COMPONENT_EXPORT(OS_CRYPT) LibsecretAttributesBuilder {
  83. public:
  84. LibsecretAttributesBuilder();
  85. LibsecretAttributesBuilder(const LibsecretAttributesBuilder&) = delete;
  86. LibsecretAttributesBuilder& operator=(const LibsecretAttributesBuilder&) =
  87. delete;
  88. ~LibsecretAttributesBuilder();
  89. void Append(const std::string& name, const std::string& value);
  90. void Append(const std::string& name, int64_t value);
  91. // GHashTable, its keys and values returned from Get() are destroyed in
  92. // |LibsecretAttributesBuilder| destructor.
  93. GHashTable* Get() { return attrs_; }
  94. private:
  95. // |name_values_| is a storage for strings referenced in |attrs_|.
  96. // TODO(crbug.com/607950): Make implementation more robust by not depending on
  97. // the implementation details of containers. External objects keep references
  98. // to the objects stored in this container. Using a vector here will fail the
  99. // ASan tests, because it may move the objects and break the references.
  100. std::list<std::string> name_values_;
  101. raw_ptr<GHashTable> attrs_;
  102. };
  103. #endif // COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_LINUX_H_