Reinstate curtain mode on non-Lion Mac.
[chromium-blink-merge.git] / crypto / nss_util.h
blob25043f973b226445da2e8d46ba7eb9782956ba7b
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_
8 #include <string>
9 #include "base/basictypes.h"
10 #include "crypto/crypto_export.h"
12 #if defined(USE_NSS)
13 class FilePath;
14 #endif // defined(USE_NSS)
16 namespace base {
17 class Lock;
18 class Time;
19 } // namespace base
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.
24 namespace crypto {
26 class SymmetricKey;
28 #if defined(USE_NSS)
29 // EarlySetupForNSSInit performs lightweight setup which must occur before the
30 // process goes multithreaded. This does not initialise NSS. For test, see
31 // EnsureNSSInit.
32 CRYPTO_EXPORT void EarlySetupForNSSInit();
33 #endif
35 // Initialize NRPR if it isn't already initialized. This function is
36 // thread-safe, and NSPR will only ever be initialized once.
37 CRYPTO_EXPORT void EnsureNSPRInit();
39 // Initialize NSS safely for strict sandboxing. This function makes sure that
40 // NSS is initialized safely and will have proper entropy in a restricted,
41 // sandboxed environment.
43 // As a defense in depth measure, this function should be called in a sandboxed
44 // environment to make sure NSS will not load security modules that could
45 // expose private data and keys. Make sure to get an LGTM from Security
46 // if you use this.
47 CRYPTO_EXPORT void WarmUpNSSSafely();
49 // Initialize NSS if it isn't already initialized. This must be called before
50 // any other NSS functions. This function is thread-safe, and NSS will only
51 // ever be initialized once.
52 CRYPTO_EXPORT void EnsureNSSInit();
54 // Call this before calling EnsureNSSInit() will force NSS to initialize
55 // without a persistent DB. This is used for the special case where access of
56 // persistent DB is prohibited.
58 // TODO(hclam): Isolate loading default root certs.
60 // NSS will be initialized without loading any user security modules, including
61 // the built-in root certificates module. User security modules need to be
62 // loaded manually after NSS initialization.
64 // If EnsureNSSInit() is called before then this function has no effect.
66 // Calling this method only has effect on Linux.
68 // WARNING: Use this with caution.
69 CRYPTO_EXPORT void ForceNSSNoDBInit();
71 // This method is used to disable checks in NSS when used in a forked process.
72 // NSS checks whether it is running a forked process to avoid problems when
73 // using user security modules in a forked process. However if we are sure
74 // there are no modules loaded before the process is forked then there is no
75 // harm disabling the check.
77 // This method must be called before EnsureNSSInit() to take effect.
79 // WARNING: Use this with caution.
80 CRYPTO_EXPORT void DisableNSSForkCheck();
82 // Load NSS library files. This function has no effect on Mac and Windows.
83 // This loads the necessary NSS library files so that NSS can be initialized
84 // after loading additional library files is disallowed, for example when the
85 // sandbox is active.
87 // Note that this does not load libnssckbi.so which contains the root
88 // certificates.
89 CRYPTO_EXPORT void LoadNSSLibraries();
91 // Check if the current NSS version is greater than or equals to |version|.
92 // A sample version string is "3.12.3".
93 bool CheckNSSVersion(const char* version);
95 #if defined(OS_CHROMEOS)
96 // Open the r/w nssdb that's stored inside the user's encrypted home
97 // directory. This is the default slot returned by
98 // GetPublicNSSKeySlot().
99 CRYPTO_EXPORT void OpenPersistentNSSDB();
101 // Indicates that NSS should load the Chaps library so that we
102 // can access the TPM through NSS. Once this is called,
103 // GetPrivateNSSKeySlot() will return the TPM slot if one was found.
104 CRYPTO_EXPORT void EnableTPMTokenForNSS();
106 // Get name and user PIN for the built-in TPM token on ChromeOS.
107 // Either one can safely be NULL. Should only be called after
108 // EnableTPMTokenForNSS has been called with a non-null delegate.
109 CRYPTO_EXPORT void GetTPMTokenInfo(std::string* token_name,
110 std::string* user_pin);
112 // Returns true if the TPM is owned and PKCS#11 initialized with the
113 // user and security officer PINs, and has been enabled in NSS by
114 // calling EnableTPMForNSS, and Chaps has been successfully
115 // loaded into NSS.
116 CRYPTO_EXPORT bool IsTPMTokenReady();
118 // Initialize the TPM token. Does nothing if it is already initialized.
119 CRYPTO_EXPORT bool InitializeTPMToken(const std::string& token_name,
120 const std::string& user_pin);
122 // Gets supplemental user key. Creates one in NSS database if it does not exist.
123 // The supplemental user key is used for AES encryption of user data that is
124 // stored and protected by cryptohome. This additional layer of encryption of
125 // provided to ensure that sensitive data wouldn't be exposed in plain text in
126 // case when an attacker would somehow gain access to all content within
127 // cryptohome.
128 CRYPTO_EXPORT SymmetricKey* GetSupplementalUserKey();
129 #endif
131 // Convert a NSS PRTime value into a base::Time object.
132 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
133 CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64 prtime);
135 // Convert a base::Time object into a PRTime value.
136 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
137 CRYPTO_EXPORT int64 BaseTimeToPRTime(base::Time time);
139 #if defined(USE_NSS)
140 // Exposed for unittests only.
141 // TODO(mattm): When NSS 3.14 is the minimum version required,
142 // switch back to using a separate user DB for each test.
143 // Because of https://bugzilla.mozilla.org/show_bug.cgi?id=588269 , the
144 // opened user DB is not automatically closed.
145 class CRYPTO_EXPORT_PRIVATE ScopedTestNSSDB {
146 public:
147 ScopedTestNSSDB();
148 ~ScopedTestNSSDB();
150 bool is_open() { return is_open_; }
152 private:
153 bool is_open_;
154 DISALLOW_COPY_AND_ASSIGN(ScopedTestNSSDB);
157 // NSS has a bug which can cause a deadlock or stall in some cases when writing
158 // to the certDB and keyDB. It also has a bug which causes concurrent key pair
159 // generations to scribble over each other. To work around this, we synchronize
160 // writes to the NSS databases with a global lock. The lock is hidden beneath a
161 // function for easy disabling when the bug is fixed. Callers should allow for
162 // it to return NULL in the future.
164 // See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
165 base::Lock* GetNSSWriteLock();
167 // A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
168 // is in scope.
169 class CRYPTO_EXPORT AutoNSSWriteLock {
170 public:
171 AutoNSSWriteLock();
172 ~AutoNSSWriteLock();
173 private:
174 base::Lock *lock_;
175 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
178 #endif // defined(USE_NSS)
180 } // namespace crypto
182 #endif // CRYPTO_NSS_UTIL_H_