Avi Drissman | 201a9a83 | 2022-09-13 19:39:25 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_NSS_UTIL_H_ |
6 | #define CRYPTO_NSS_UTIL_H_ | ||||
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 7 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stdint.h> |
9 | |||||
Michael Ershov | 6b5e418 | 2021-09-28 13:46:32 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
Michael Ershov | 0d751203 | 2022-04-21 00:06:00 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
Yuta Hijikata | bf95320 | 2020-11-12 08:43:55 | [diff] [blame] | 13 | #include "build/chromeos_buildflags.h" |
Fabian Sommer | 5abef07 | 2022-08-27 00:07:30 | [diff] [blame] | 14 | #include "components/nacl/common/buildflags.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 15 | #include "crypto/crypto_export.h" |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 16 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | namespace base { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 18 | class Time; |
19 | } // namespace base | ||||
20 | |||||
[email protected] | 41c78fa | 2010-03-22 20:08:41 | [diff] [blame] | 21 | // This file specifically doesn't depend on any NSS or NSPR headers because it |
22 | // is included by various (non-crypto) parts of chrome to call the | ||||
23 | // initialization functions. | ||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 24 | namespace crypto { |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 25 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 26 | // Initialize NRPR if it isn't already initialized. This function is |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 27 | // thread-safe, and NSPR will only ever be initialized once. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 28 | CRYPTO_EXPORT void EnsureNSPRInit(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 29 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 30 | // Initialize NSS if it isn't already initialized. This must be called before |
31 | // any other NSS functions. This function is thread-safe, and NSS will only | ||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 32 | // ever be initialized once. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 33 | CRYPTO_EXPORT void EnsureNSSInit(); |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 34 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 35 | // Check if the current NSS version is greater than or equals to |version|. |
36 | // A sample version string is "3.12.3". | ||||
37 | bool CheckNSSVersion(const char* version); | ||||
38 | |||||
Fabian Sommer | 5abef07 | 2022-08-27 00:07:30 | [diff] [blame] | 39 | #if BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_MINIMAL_TOOLCHAIN) |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 40 | |
Michael Ershov | 6b5e418 | 2021-09-28 13:46:32 | [diff] [blame] | 41 | // Returns true once the TPM is owned and PKCS#11 initialized with the |
42 | // user and security officer PINs, and Chaps has been successfully loaded into | ||||
43 | // NSS. Returns false if the TPM will never be loaded. | ||||
44 | CRYPTO_EXPORT void IsTPMTokenEnabled(base::OnceCallback<void(bool)> callback); | ||||
[email protected] | c175cdb | 2011-06-28 20:41:55 | [diff] [blame] | 45 | |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 46 | // Initialize the TPM token and system slot. The |callback| will run on the same |
47 | // thread with true if the token and slot were successfully loaded or were | ||||
Michael Ershov | 6b5e418 | 2021-09-28 13:46:32 | [diff] [blame] | 48 | // already initialized. |callback| will be passed false if loading failed. |
49 | // Should be called only once. | ||||
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 50 | CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot( |
51 | int system_slot_id, | ||||
tzik | 88b34c8 | 2018-03-09 05:01:13 | [diff] [blame] | 52 | base::OnceCallback<void(bool)> callback); |
Michael Ershov | 6b5e418 | 2021-09-28 13:46:32 | [diff] [blame] | 53 | |
54 | // Notifies clients that the TPM has finished initialization (i.e. notify | ||||
55 | // the callbacks of `IsTPMTokenEnabled()` or `GetSystemNSSKeySlot()`). | ||||
56 | // If `InitializeTPMTokenAndSystemSlot()` has been called before this method, | ||||
57 | // this signals that the TPM is enabled, and should use the slot configured by | ||||
58 | // those methods. If neither of those methods have been called, this signals | ||||
59 | // that no TPM system slot will be available. | ||||
60 | CRYPTO_EXPORT void FinishInitializingTPMTokenAndSystemSlot(); | ||||
Michael Ershov | 0d751203 | 2022-04-21 00:06:00 | [diff] [blame] | 61 | |
62 | // TODO(crbug.com/1163303) Remove when the bug is fixed. | ||||
63 | // Can be used to collect additional information when public slot fails to open. | ||||
64 | // Mainly checks the access permissions on the files and tries to read them. | ||||
65 | // Crashes Chrome because it will crash anyway when it tries to instantiate | ||||
66 | // NSSCertDatabase with a nullptr public slot, crashing early can provide better | ||||
67 | // logs/stacktraces for diagnosing. | ||||
68 | // Takes `nss_path` where NSS is supposed to be (or created). Will attempt | ||||
69 | // creating the path if it doesn't exist (to check that it can be done). | ||||
70 | // Theoretically the path should already exist because it's created when Chrome | ||||
71 | // tries to open the public slot. | ||||
72 | CRYPTO_EXPORT void DiagnosePublicSlotAndCrash(const base::FilePath& nss_path); | ||||
73 | |||||
Fabian Sommer | 5abef07 | 2022-08-27 00:07:30 | [diff] [blame] | 74 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_MINIMAL_TOOLCHAIN) |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 75 | |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 76 | // Convert a NSS PRTime value into a base::Time object. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 77 | // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. |
78 | CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime); | ||||
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 79 | |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 80 | // Convert a base::Time object into a PRTime value. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 81 | // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. |
82 | CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time); | ||||
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 83 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 84 | } // namespace crypto |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 85 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 86 | #endif // CRYPTO_NSS_UTIL_H_ |