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