nss_util_internal.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_NSS_UTIL_INTERNAL_H_
  5. #define CRYPTO_NSS_UTIL_INTERNAL_H_
  6. #include <secmodt.h>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "crypto/crypto_export.h"
  11. #include "crypto/scoped_nss_types.h"
  12. namespace base {
  13. class FilePath;
  14. }
  15. // These functions return a type defined in an NSS header, and so cannot be
  16. // declared in nss_util.h. Hence, they are declared here.
  17. namespace crypto {
  18. // Opens an NSS software database in folder `path`, with the (potentially)
  19. // user-visible description `description`. Returns the slot for the opened
  20. // database, or nullptr if the database could not be opened. Can be called
  21. // multiple times for the same `path`, thread-safe.
  22. CRYPTO_EXPORT ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path,
  23. const std::string& description);
  24. // Closes the underlying database for the `slot`. All remaining slots
  25. // referencing the same database will remain valid objects, but won't be able to
  26. // successfully retrieve certificates, etc. Should be used for all databases
  27. // that were opened with `OpenSoftwareNSSDB` (instead of `SECMOD_CloseUserDB`).
  28. // Can be called multiple times. Returns `SECSuccess` if the database was
  29. // successfully closed, returns `SECFailure` if it was never opened, was already
  30. // closed by an earlier call, or failed to close. Thread-safe.
  31. CRYPTO_EXPORT SECStatus CloseSoftwareNSSDB(PK11SlotInfo* slot);
  32. // A helper class that acquires the SECMOD list read lock while the
  33. // AutoSECMODListReadLock is in scope.
  34. class CRYPTO_EXPORT AutoSECMODListReadLock {
  35. public:
  36. AutoSECMODListReadLock();
  37. AutoSECMODListReadLock(const AutoSECMODListReadLock&) = delete;
  38. AutoSECMODListReadLock& operator=(const AutoSECMODListReadLock&) = delete;
  39. ~AutoSECMODListReadLock();
  40. private:
  41. SECMODListLock* lock_;
  42. };
  43. #if BUILDFLAG(IS_CHROMEOS_ASH)
  44. // Returns path to the NSS database file in the provided profile
  45. // directory.
  46. CRYPTO_EXPORT base::FilePath GetSoftwareNSSDBPath(
  47. const base::FilePath& profile_directory_path);
  48. // Returns a reference to the system-wide TPM slot (or nullptr if it will never
  49. // be loaded).
  50. CRYPTO_EXPORT void GetSystemNSSKeySlot(
  51. base::OnceCallback<void(ScopedPK11Slot)> callback);
  52. // Injects the given |slot| as a system slot set by the future
  53. // |InitializeTPMTokenAndSystemSlot| call.
  54. CRYPTO_EXPORT void PrepareSystemSlotForTesting(ScopedPK11Slot slot);
  55. // Attempt to unset the testing system slot.
  56. // Note: After this method is called, the system is in an undefined state; it is
  57. // NOT possible to call `PrepareSystemSlotForTesting()` and have it return to a
  58. // known-good state. The primary purpose is to attempt to release system
  59. // resources, such as file handles, to allow the cleanup of files on disk, but
  60. // because of the process-wide effect, it's not possible to unwind any/all
  61. // initialization that depended on this previously-configured system slot.
  62. CRYPTO_EXPORT void ResetSystemSlotForTesting();
  63. // Reset the global ChromeOSTokenManager. This is used between tests, so
  64. // tests that run in the same process won't hit DCHECKS because they have
  65. // different BrowserIO threads.
  66. CRYPTO_EXPORT void ResetTokenManagerForTesting();
  67. // Prepare per-user NSS slot mapping. It is safe to call this function multiple
  68. // times. Returns true if the user was added, or false if it already existed.
  69. CRYPTO_EXPORT bool InitializeNSSForChromeOSUser(
  70. const std::string& username_hash,
  71. const base::FilePath& path);
  72. // Returns whether TPM for ChromeOS user still needs initialization. If
  73. // true is returned, the caller can proceed to initialize TPM slot for the
  74. // user, but should call |WillInitializeTPMForChromeOSUser| first.
  75. // |InitializeNSSForChromeOSUser| must have been called first.
  76. [[nodiscard]] CRYPTO_EXPORT bool ShouldInitializeTPMForChromeOSUser(
  77. const std::string& username_hash);
  78. // Makes |ShouldInitializeTPMForChromeOSUser| start returning false.
  79. // Should be called before starting TPM initialization for the user.
  80. // Assumes |InitializeNSSForChromeOSUser| had already been called.
  81. CRYPTO_EXPORT void WillInitializeTPMForChromeOSUser(
  82. const std::string& username_hash);
  83. // Use TPM slot |slot_id| for user. InitializeNSSForChromeOSUser must have been
  84. // called first.
  85. CRYPTO_EXPORT void InitializeTPMForChromeOSUser(
  86. const std::string& username_hash,
  87. CK_SLOT_ID slot_id);
  88. // Use the software slot as the private slot for user.
  89. // InitializeNSSForChromeOSUser must have been called first.
  90. CRYPTO_EXPORT void InitializePrivateSoftwareSlotForChromeOSUser(
  91. const std::string& username_hash);
  92. // Returns a reference to the public slot for user.
  93. [[nodiscard]] CRYPTO_EXPORT ScopedPK11Slot
  94. GetPublicSlotForChromeOSUser(const std::string& username_hash);
  95. // Returns the private slot for |username_hash| if it is loaded. If it is not
  96. // loaded and |callback| is non-null, the |callback| will be run once the slot
  97. // is loaded.
  98. [[nodiscard]] CRYPTO_EXPORT ScopedPK11Slot GetPrivateSlotForChromeOSUser(
  99. const std::string& username_hash,
  100. base::OnceCallback<void(ScopedPK11Slot)> callback);
  101. // Closes the NSS DB for |username_hash| that was previously opened by the
  102. // *Initialize*ForChromeOSUser functions.
  103. CRYPTO_EXPORT void CloseChromeOSUserForTesting(
  104. const std::string& username_hash);
  105. // Sets the slot which should be used as private slot for the next
  106. // |InitializePrivateSoftwareSlotForChromeOSUser| called. This is intended for
  107. // simulating a separate private slot in Chrome OS browser tests.
  108. // As a sanity check, it is recommended to check that the private slot of the
  109. // profile's certificate database is set to |slot| when the profile is
  110. // available, because |slot| will be used as private slot for whichever profile
  111. // is initialized next.
  112. CRYPTO_EXPORT void SetPrivateSoftwareSlotForChromeOSUserForTesting(
  113. ScopedPK11Slot slot);
  114. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  115. // Loads the given module for this NSS session.
  116. SECMODModule* LoadNSSModule(const char* name,
  117. const char* library_path,
  118. const char* params);
  119. // Returns the current NSS error message.
  120. std::string GetNSSErrorMessage();
  121. } // namespace crypto
  122. #endif // CRYPTO_NSS_UTIL_INTERNAL_H_