nss_util.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_H_
  5. #define CRYPTO_NSS_UTIL_H_
  6. #include <stdint.h>
  7. #include "base/callback_forward.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/files/file_path.h"
  10. #include "build/chromeos_buildflags.h"
  11. #include "crypto/crypto_export.h"
  12. namespace base {
  13. class Time;
  14. } // namespace base
  15. // This file specifically doesn't depend on any NSS or NSPR headers because it
  16. // is included by various (non-crypto) parts of chrome to call the
  17. // initialization functions.
  18. namespace crypto {
  19. // Initialize NRPR if it isn't already initialized. This function is
  20. // thread-safe, and NSPR will only ever be initialized once.
  21. CRYPTO_EXPORT void EnsureNSPRInit();
  22. // Initialize NSS if it isn't already initialized. This must be called before
  23. // any other NSS functions. This function is thread-safe, and NSS will only
  24. // ever be initialized once.
  25. CRYPTO_EXPORT void EnsureNSSInit();
  26. // Check if the current NSS version is greater than or equals to |version|.
  27. // A sample version string is "3.12.3".
  28. bool CheckNSSVersion(const char* version);
  29. #if BUILDFLAG(IS_CHROMEOS_ASH)
  30. // Returns true once the TPM is owned and PKCS#11 initialized with the
  31. // user and security officer PINs, and Chaps has been successfully loaded into
  32. // NSS. Returns false if the TPM will never be loaded.
  33. CRYPTO_EXPORT void IsTPMTokenEnabled(base::OnceCallback<void(bool)> callback);
  34. // Initialize the TPM token and system slot. The |callback| will run on the same
  35. // thread with true if the token and slot were successfully loaded or were
  36. // already initialized. |callback| will be passed false if loading failed.
  37. // Should be called only once.
  38. CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot(
  39. int system_slot_id,
  40. base::OnceCallback<void(bool)> callback);
  41. // Notifies clients that the TPM has finished initialization (i.e. notify
  42. // the callbacks of `IsTPMTokenEnabled()` or `GetSystemNSSKeySlot()`).
  43. // If `InitializeTPMTokenAndSystemSlot()` has been called before this method,
  44. // this signals that the TPM is enabled, and should use the slot configured by
  45. // those methods. If neither of those methods have been called, this signals
  46. // that no TPM system slot will be available.
  47. CRYPTO_EXPORT void FinishInitializingTPMTokenAndSystemSlot();
  48. // TODO(crbug.com/1163303) Remove when the bug is fixed.
  49. // Can be used to collect additional information when public slot fails to open.
  50. // Mainly checks the access permissions on the files and tries to read them.
  51. // Crashes Chrome because it will crash anyway when it tries to instantiate
  52. // NSSCertDatabase with a nullptr public slot, crashing early can provide better
  53. // logs/stacktraces for diagnosing.
  54. // Takes `nss_path` where NSS is supposed to be (or created). Will attempt
  55. // creating the path if it doesn't exist (to check that it can be done).
  56. // Theoretically the path should already exist because it's created when Chrome
  57. // tries to open the public slot.
  58. CRYPTO_EXPORT void DiagnosePublicSlotAndCrash(const base::FilePath& nss_path);
  59. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  60. // Convert a NSS PRTime value into a base::Time object.
  61. // We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
  62. CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime);
  63. // Convert a base::Time object into a PRTime value.
  64. // We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
  65. CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time);
  66. } // namespace crypto
  67. #endif // CRYPTO_NSS_UTIL_H_