xref: /aosp_15_r20/external/libchrome/crypto/nss_util_internal.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef CRYPTO_NSS_UTIL_INTERNAL_H_
6*635a8641SAndroid Build Coastguard Worker #define CRYPTO_NSS_UTIL_INTERNAL_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <secmodt.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <string>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
15*635a8641SAndroid Build Coastguard Worker #include "crypto/crypto_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "crypto/scoped_nss_types.h"
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker namespace base {
19*635a8641SAndroid Build Coastguard Worker class FilePath;
20*635a8641SAndroid Build Coastguard Worker }
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker // These functions return a type defined in an NSS header, and so cannot be
23*635a8641SAndroid Build Coastguard Worker // declared in nss_util.h.  Hence, they are declared here.
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker namespace crypto {
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker // Opens an NSS software database in folder |path|, with the (potentially)
28*635a8641SAndroid Build Coastguard Worker // user-visible description |description|. Returns the slot for the opened
29*635a8641SAndroid Build Coastguard Worker // database, or nullptr if the database could not be opened.
30*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path,
31*635a8641SAndroid Build Coastguard Worker                                                const std::string& description);
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker #if !defined(OS_CHROMEOS)
34*635a8641SAndroid Build Coastguard Worker // Returns a reference to the default NSS key slot for storing persistent data.
35*635a8641SAndroid Build Coastguard Worker // Caller must release returned reference with PK11_FreeSlot.
36*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT PK11SlotInfo* GetPersistentNSSKeySlot() WARN_UNUSED_RESULT;
37*635a8641SAndroid Build Coastguard Worker #endif
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker // A helper class that acquires the SECMOD list read lock while the
40*635a8641SAndroid Build Coastguard Worker // AutoSECMODListReadLock is in scope.
41*635a8641SAndroid Build Coastguard Worker class CRYPTO_EXPORT AutoSECMODListReadLock {
42*635a8641SAndroid Build Coastguard Worker  public:
43*635a8641SAndroid Build Coastguard Worker   AutoSECMODListReadLock();
44*635a8641SAndroid Build Coastguard Worker   ~AutoSECMODListReadLock();
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker  private:
47*635a8641SAndroid Build Coastguard Worker   SECMODListLock* lock_;
48*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock);
49*635a8641SAndroid Build Coastguard Worker };
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
52*635a8641SAndroid Build Coastguard Worker // Returns a reference to the system-wide TPM slot if it is loaded. If it is not
53*635a8641SAndroid Build Coastguard Worker // loaded and |callback| is non-null, the |callback| will be run once the slot
54*635a8641SAndroid Build Coastguard Worker // is loaded.
55*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT ScopedPK11Slot GetSystemNSSKeySlot(
56*635a8641SAndroid Build Coastguard Worker     base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT;
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker // Sets the test system slot to |slot|, which means that |slot| will be exposed
59*635a8641SAndroid Build Coastguard Worker // through |GetSystemNSSKeySlot| and |IsTPMTokenReady| will return true.
60*635a8641SAndroid Build Coastguard Worker // |InitializeTPMTokenAndSystemSlot|, which triggers the TPM initialization,
61*635a8641SAndroid Build Coastguard Worker // does not have to be called if the test system slot is set.
62*635a8641SAndroid Build Coastguard Worker // This must must not be called consecutively with a |slot| != nullptr. If
63*635a8641SAndroid Build Coastguard Worker // |slot| is nullptr, the test system slot is unset.
64*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void SetSystemKeySlotForTesting(ScopedPK11Slot slot);
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker // Prepare per-user NSS slot mapping. It is safe to call this function multiple
67*635a8641SAndroid Build Coastguard Worker // times. Returns true if the user was added, or false if it already existed.
68*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT bool InitializeNSSForChromeOSUser(
69*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash,
70*635a8641SAndroid Build Coastguard Worker     const base::FilePath& path);
71*635a8641SAndroid Build Coastguard Worker 
72*635a8641SAndroid Build Coastguard Worker // Returns whether TPM for ChromeOS user still needs initialization. If
73*635a8641SAndroid Build Coastguard Worker // true is returned, the caller can proceed to initialize TPM slot for the
74*635a8641SAndroid Build Coastguard Worker // user, but should call |WillInitializeTPMForChromeOSUser| first.
75*635a8641SAndroid Build Coastguard Worker // |InitializeNSSForChromeOSUser| must have been called first.
76*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT bool ShouldInitializeTPMForChromeOSUser(
77*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash) WARN_UNUSED_RESULT;
78*635a8641SAndroid Build Coastguard Worker 
79*635a8641SAndroid Build Coastguard Worker // Makes |ShouldInitializeTPMForChromeOSUser| start returning false.
80*635a8641SAndroid Build Coastguard Worker // Should be called before starting TPM initialization for the user.
81*635a8641SAndroid Build Coastguard Worker // Assumes |InitializeNSSForChromeOSUser| had already been called.
82*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void WillInitializeTPMForChromeOSUser(
83*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash);
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker // Use TPM slot |slot_id| for user.  InitializeNSSForChromeOSUser must have been
86*635a8641SAndroid Build Coastguard Worker // called first.
87*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void InitializeTPMForChromeOSUser(
88*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash,
89*635a8641SAndroid Build Coastguard Worker     CK_SLOT_ID slot_id);
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker // Use the software slot as the private slot for user.
92*635a8641SAndroid Build Coastguard Worker // InitializeNSSForChromeOSUser must have been called first.
93*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void InitializePrivateSoftwareSlotForChromeOSUser(
94*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash);
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker // Returns a reference to the public slot for user.
97*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT ScopedPK11Slot GetPublicSlotForChromeOSUser(
98*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash) WARN_UNUSED_RESULT;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker // Returns the private slot for |username_hash| if it is loaded. If it is not
101*635a8641SAndroid Build Coastguard Worker // loaded and |callback| is non-null, the |callback| will be run once the slot
102*635a8641SAndroid Build Coastguard Worker // is loaded.
103*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT ScopedPK11Slot GetPrivateSlotForChromeOSUser(
104*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash,
105*635a8641SAndroid Build Coastguard Worker     base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT;
106*635a8641SAndroid Build Coastguard Worker 
107*635a8641SAndroid Build Coastguard Worker // Closes the NSS DB for |username_hash| that was previously opened by the
108*635a8641SAndroid Build Coastguard Worker // *Initialize*ForChromeOSUser functions.
109*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void CloseChromeOSUserForTesting(
110*635a8641SAndroid Build Coastguard Worker     const std::string& username_hash);
111*635a8641SAndroid Build Coastguard Worker 
112*635a8641SAndroid Build Coastguard Worker // Sets the slot which should be used as private slot for the next
113*635a8641SAndroid Build Coastguard Worker // |InitializePrivateSoftwareSlotForChromeOSUser| called. This is intended for
114*635a8641SAndroid Build Coastguard Worker // simulating a separate private slot in Chrome OS browser tests.
115*635a8641SAndroid Build Coastguard Worker // As a sanity check, it is recommended to check that the private slot of the
116*635a8641SAndroid Build Coastguard Worker // profile's certificate database is set to |slot| when the profile is
117*635a8641SAndroid Build Coastguard Worker // available, because |slot| will be used as private slot for whichever profile
118*635a8641SAndroid Build Coastguard Worker // is initialized next.
119*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void SetPrivateSoftwareSlotForChromeOSUserForTesting(
120*635a8641SAndroid Build Coastguard Worker     ScopedPK11Slot slot);
121*635a8641SAndroid Build Coastguard Worker 
122*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
123*635a8641SAndroid Build Coastguard Worker 
124*635a8641SAndroid Build Coastguard Worker }  // namespace crypto
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker #endif  // CRYPTO_NSS_UTIL_INTERNAL_H_
127