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.
5 #ifndef CRYPTO_NSS_UTIL_H_
6 #define CRYPTO_NSS_UTIL_H_
9 #include "base/basictypes.h"
10 #include "base/callback_forward.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "crypto/crypto_export.h"
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.
26 // The TPMToken name used for the NSS slot opened by ScopedTestNSSDB.
27 CRYPTO_EXPORT
extern const char kTestTPMTokenName
[];
30 // EarlySetupForNSSInit performs lightweight setup which must occur before the
31 // process goes multithreaded. This does not initialise NSS. For test, see
33 CRYPTO_EXPORT
void EarlySetupForNSSInit();
36 // Initialize NRPR if it isn't already initialized. This function is
37 // thread-safe, and NSPR will only ever be initialized once.
38 CRYPTO_EXPORT
void EnsureNSPRInit();
40 // Initialize NSS safely for strict sandboxing. This function tells NSS to not
41 // load user security modules, and makes sure NSS will have proper entropy in a
42 // restricted, sandboxed environment.
44 // As a defense in depth measure, this function should be called in a sandboxed
45 // environment. That way, in the event of a bug, NSS will still not be able to
46 // load security modules that could expose private data and keys.
48 // Make sure to get an LGTM from the Chrome Security Team if you use this.
49 CRYPTO_EXPORT
void InitNSSSafely();
51 // Initialize NSS if it isn't already initialized. This must be called before
52 // any other NSS functions. This function is thread-safe, and NSS will only
53 // ever be initialized once.
54 CRYPTO_EXPORT
void EnsureNSSInit();
56 // Call this before calling EnsureNSSInit() will force NSS to initialize
57 // without a persistent DB. This is used for the special case where access of
58 // persistent DB is prohibited.
60 // TODO(hclam): Isolate loading default root certs.
62 // NSS will be initialized without loading any user security modules, including
63 // the built-in root certificates module. User security modules need to be
64 // loaded manually after NSS initialization.
66 // If EnsureNSSInit() is called before then this function has no effect.
68 // Calling this method only has effect on Linux.
70 // WARNING: Use this with caution.
71 CRYPTO_EXPORT
void ForceNSSNoDBInit();
73 // This method is used to disable checks in NSS when used in a forked process.
74 // NSS checks whether it is running a forked process to avoid problems when
75 // using user security modules in a forked process. However if we are sure
76 // there are no modules loaded before the process is forked then there is no
77 // harm disabling the check.
79 // This method must be called before EnsureNSSInit() to take effect.
81 // WARNING: Use this with caution.
82 CRYPTO_EXPORT
void DisableNSSForkCheck();
84 // Load NSS library files. This function has no effect on Mac and Windows.
85 // This loads the necessary NSS library files so that NSS can be initialized
86 // after loading additional library files is disallowed, for example when the
89 // Note that this does not load libnssckbi.so which contains the root
91 CRYPTO_EXPORT
void LoadNSSLibraries();
93 // Check if the current NSS version is greater than or equals to |version|.
94 // A sample version string is "3.12.3".
95 bool CheckNSSVersion(const char* version
);
97 #if defined(OS_CHROMEOS)
98 // Open the r/w nssdb that's stored inside the user's encrypted home
99 // directory. This is the default slot returned by
100 // GetPublicNSSKeySlot().
101 CRYPTO_EXPORT
void OpenPersistentNSSDB();
103 // Indicates that NSS should load the Chaps library so that we
104 // can access the TPM through NSS. Once this is called,
105 // GetPrivateNSSKeySlot() will return the TPM slot if one was found.
106 CRYPTO_EXPORT
void EnableTPMTokenForNSS();
108 // Returns true if EnableTPMTokenForNSS has been called.
109 CRYPTO_EXPORT
bool IsTPMTokenEnabledForNSS();
111 // Returns true if the TPM is owned and PKCS#11 initialized with the
112 // user and security officer PINs, and has been enabled in NSS by
113 // calling EnableTPMForNSS, and Chaps has been successfully
115 // If |callback| is non-null and the function returns false, the |callback| will
116 // be run once the TPM is ready. |callback| will never be run if the function
118 CRYPTO_EXPORT
bool IsTPMTokenReady(const base::Closure
& callback
)
121 // Initialize the TPM token. Does nothing if it is already initialized.
122 CRYPTO_EXPORT
bool InitializeTPMToken(int token_slot_id
);
124 // Exposed for unittests only.
125 class CRYPTO_EXPORT_PRIVATE ScopedTestNSSChromeOSUser
{
127 explicit ScopedTestNSSChromeOSUser(const std::string
& username_hash
);
128 ~ScopedTestNSSChromeOSUser();
130 std::string
username_hash() const { return username_hash_
; }
131 bool constructed_successfully() const { return constructed_successfully_
; }
133 // Completes initialization of user. Causes any waiting private slot callbacks
138 const std::string username_hash_
;
139 base::ScopedTempDir temp_dir_
;
140 bool constructed_successfully_
;
141 DISALLOW_COPY_AND_ASSIGN(ScopedTestNSSChromeOSUser
);
145 // Convert a NSS PRTime value into a base::Time object.
146 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
147 CRYPTO_EXPORT
base::Time
PRTimeToBaseTime(int64 prtime
);
149 // Convert a base::Time object into a PRTime value.
150 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
151 CRYPTO_EXPORT int64
BaseTimeToPRTime(base::Time time
);
154 // Exposed for unittests only.
155 // TODO(mattm): When NSS 3.14 is the minimum version required,
156 // switch back to using a separate user DB for each test.
157 // Because of https://bugzilla.mozilla.org/show_bug.cgi?id=588269 , the
158 // opened user DB is not automatically closed.
159 class CRYPTO_EXPORT_PRIVATE ScopedTestNSSDB
{
164 bool is_open() { return is_open_
; }
168 DISALLOW_COPY_AND_ASSIGN(ScopedTestNSSDB
);
171 // NSS has a bug which can cause a deadlock or stall in some cases when writing
172 // to the certDB and keyDB. It also has a bug which causes concurrent key pair
173 // generations to scribble over each other. To work around this, we synchronize
174 // writes to the NSS databases with a global lock. The lock is hidden beneath a
175 // function for easy disabling when the bug is fixed. Callers should allow for
176 // it to return NULL in the future.
178 // See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
179 base::Lock
* GetNSSWriteLock();
181 // A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
183 class CRYPTO_EXPORT AutoNSSWriteLock
{
189 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock
);
192 #endif // defined(USE_NSS)
194 } // namespace crypto
196 #endif // CRYPTO_NSS_UTIL_H_