[email protected] | cf21188 | 2012-07-11 07:19:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [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_INTERNAL_H_ |
| 6 | #define CRYPTO_NSS_UTIL_INTERNAL_H_ |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 7 | |
| 8 | #include <secmodt.h> |
| 9 | |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 12 | #include "base/callback.h" |
| 13 | #include "base/compiler_specific.h" |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 15 | #include "crypto/crypto_export.h" |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 16 | #include "crypto/scoped_nss_types.h" |
| 17 | |
| 18 | namespace base { |
| 19 | class FilePath; |
| 20 | } |
[email protected] | e4c9dda | 2011-06-20 23:24:09 | [diff] [blame] | 21 | |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 22 | // These functions return a type defined in an NSS header, and so cannot be |
| 23 | // declared in nss_util.h. Hence, they are declared here. |
| 24 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 25 | namespace crypto { |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 26 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 27 | // Opens an NSS software database in folder |path|, with the (potentially) |
| 28 | // user-visible description |description|. Returns the slot for the opened |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 29 | // database, or nullptr if the database could not be opened. |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 30 | CRYPTO_EXPORT ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path, |
| 31 | const std::string& description); |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 32 | |
[email protected] | dd24ffc | 2011-06-08 19:46:42 | [diff] [blame] | 33 | // A helper class that acquires the SECMOD list read lock while the |
| 34 | // AutoSECMODListReadLock is in scope. |
[email protected] | 7037a43c | 2014-01-14 14:00:46 | [diff] [blame] | 35 | class CRYPTO_EXPORT AutoSECMODListReadLock { |
[email protected] | dd24ffc | 2011-06-08 19:46:42 | [diff] [blame] | 36 | public: |
| 37 | AutoSECMODListReadLock(); |
| 38 | ~AutoSECMODListReadLock(); |
| 39 | |
| 40 | private: |
| 41 | SECMODListLock* lock_; |
| 42 | DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock); |
| 43 | }; |
| 44 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 45 | #if defined(OS_CHROMEOS) |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 46 | // Returns a reference to the system-wide TPM slot if it is loaded. If it is not |
| 47 | // loaded and |callback| is non-null, the |callback| will be run once the slot |
| 48 | // is loaded. |
| 49 | CRYPTO_EXPORT ScopedPK11Slot GetSystemNSSKeySlot( |
tzik | 88b34c8 | 2018-03-09 05:01:13 | [diff] [blame] | 50 | base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT; |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 51 | |
[email protected] | 442233d4 | 2014-08-02 07:37:24 | [diff] [blame] | 52 | // Sets the test system slot to |slot|, which means that |slot| will be exposed |
| 53 | // through |GetSystemNSSKeySlot| and |IsTPMTokenReady| will return true. |
| 54 | // |InitializeTPMTokenAndSystemSlot|, which triggers the TPM initialization, |
| 55 | // does not have to be called if the test system slot is set. |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 56 | // This must must not be called consecutively with a |slot| != nullptr. If |
| 57 | // |slot| is nullptr, the test system slot is unset. |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 58 | CRYPTO_EXPORT void SetSystemKeySlotForTesting(ScopedPK11Slot slot); |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 59 | |
Maksim Ivanov | d4e889a | 2019-10-11 20:13:30 | [diff] [blame] | 60 | // Injects the given |slot| as a system slot set by the future |
| 61 | // |InitializeTPMTokenAndSystemSlot| call. |
| 62 | // This must must not be called consecutively with a |slot| != nullptr. If |
| 63 | // |slot| is nullptr and the system slot is already initialized to the |
| 64 | // previously passed test value, the system slot is unset. |
| 65 | CRYPTO_EXPORT void SetSystemKeySlotWithoutInitializingTPMForTesting( |
| 66 | ScopedPK11Slot slot); |
| 67 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 68 | // Prepare per-user NSS slot mapping. It is safe to call this function multiple |
| 69 | // times. Returns true if the user was added, or false if it already existed. |
| 70 | CRYPTO_EXPORT bool InitializeNSSForChromeOSUser( |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 71 | const std::string& username_hash, |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 72 | const base::FilePath& path); |
| 73 | |
| 74 | // Returns whether TPM for ChromeOS user still needs initialization. If |
| 75 | // true is returned, the caller can proceed to initialize TPM slot for the |
| 76 | // user, but should call |WillInitializeTPMForChromeOSUser| first. |
| 77 | // |InitializeNSSForChromeOSUser| must have been called first. |
| 78 | CRYPTO_EXPORT bool ShouldInitializeTPMForChromeOSUser( |
| 79 | const std::string& username_hash) WARN_UNUSED_RESULT; |
| 80 | |
| 81 | // Makes |ShouldInitializeTPMForChromeOSUser| start returning false. |
| 82 | // Should be called before starting TPM initialization for the user. |
| 83 | // Assumes |InitializeNSSForChromeOSUser| had already been called. |
| 84 | CRYPTO_EXPORT void WillInitializeTPMForChromeOSUser( |
| 85 | const std::string& username_hash); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 86 | |
| 87 | // Use TPM slot |slot_id| for user. InitializeNSSForChromeOSUser must have been |
| 88 | // called first. |
| 89 | CRYPTO_EXPORT void InitializeTPMForChromeOSUser( |
| 90 | const std::string& username_hash, |
| 91 | CK_SLOT_ID slot_id); |
| 92 | |
| 93 | // Use the software slot as the private slot for user. |
| 94 | // InitializeNSSForChromeOSUser must have been called first. |
| 95 | CRYPTO_EXPORT void InitializePrivateSoftwareSlotForChromeOSUser( |
| 96 | const std::string& username_hash); |
| 97 | |
| 98 | // Returns a reference to the public slot for user. |
| 99 | CRYPTO_EXPORT ScopedPK11Slot GetPublicSlotForChromeOSUser( |
| 100 | const std::string& username_hash) WARN_UNUSED_RESULT; |
| 101 | |
| 102 | // Returns the private slot for |username_hash| if it is loaded. If it is not |
| 103 | // loaded and |callback| is non-null, the |callback| will be run once the slot |
| 104 | // is loaded. |
| 105 | CRYPTO_EXPORT ScopedPK11Slot GetPrivateSlotForChromeOSUser( |
| 106 | const std::string& username_hash, |
tzik | 88b34c8 | 2018-03-09 05:01:13 | [diff] [blame] | 107 | base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT; |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 108 | |
| 109 | // Closes the NSS DB for |username_hash| that was previously opened by the |
| 110 | // *Initialize*ForChromeOSUser functions. |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 111 | CRYPTO_EXPORT void CloseChromeOSUserForTesting( |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 112 | const std::string& username_hash); |
Pavol Marko | 725eaa4 | 2018-05-10 21:00:15 | [diff] [blame] | 113 | |
| 114 | // Sets the slot which should be used as private slot for the next |
| 115 | // |InitializePrivateSoftwareSlotForChromeOSUser| called. This is intended for |
| 116 | // simulating a separate private slot in Chrome OS browser tests. |
| 117 | // As a sanity check, it is recommended to check that the private slot of the |
| 118 | // profile's certificate database is set to |slot| when the profile is |
| 119 | // available, because |slot| will be used as private slot for whichever profile |
| 120 | // is initialized next. |
| 121 | CRYPTO_EXPORT void SetPrivateSoftwareSlotForChromeOSUserForTesting( |
| 122 | ScopedPK11Slot slot); |
| 123 | |
Omar Morsi | 3761477 | 2020-02-20 10:46:47 | [diff] [blame] | 124 | // Returns true if chaps is the module to which |slot| is attached. |
| 125 | CRYPTO_EXPORT bool IsSlotProvidedByChaps(PK11SlotInfo* slot); |
| 126 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 127 | #endif // defined(OS_CHROMEOS) |
| 128 | |
David Benjamin | 2f2cb36 | 2019-10-15 22:51:05 | [diff] [blame] | 129 | // Loads the given module for this NSS session. |
| 130 | SECMODModule* LoadNSSModule(const char* name, |
| 131 | const char* library_path, |
| 132 | const char* params); |
| 133 | |
| 134 | // Returns the current NSS error message. |
| 135 | std::string GetNSSErrorMessage(); |
| 136 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 137 | } // namespace crypto |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 138 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 139 | #endif // CRYPTO_NSS_UTIL_INTERNAL_H_ |