chromeos: bluetooth: add BluetoothInputClient
[chromium-blink-merge.git] / crypto / nss_util.h
blob9cfdf0b542d78993ec86b4d37045ddbf34ec5672
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_
7 #pragma once
9 #include <string>
10 #include "base/basictypes.h"
11 #include "crypto/crypto_export.h"
13 #if defined(USE_NSS)
14 class FilePath;
15 #endif // defined(USE_NSS)
17 namespace base {
18 class Lock;
19 class Time;
20 } // namespace base
22 // This file specifically doesn't depend on any NSS or NSPR headers because it
23 // is included by various (non-crypto) parts of chrome to call the
24 // initialization functions.
25 namespace crypto {
27 class SymmetricKey;
29 #if defined(USE_NSS)
30 // EarlySetupForNSSInit performs lightweight setup which must occur before the
31 // process goes multithreaded. This does not initialise NSS. For test, see
32 // EnsureNSSInit.
33 CRYPTO_EXPORT void EarlySetupForNSSInit();
34 #endif
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 if it isn't already initialized. This must be called before
41 // any other NSS functions. This function is thread-safe, and NSS will only
42 // ever be initialized once.
43 CRYPTO_EXPORT void EnsureNSSInit();
45 // Call this before calling EnsureNSSInit() will force NSS to initialize
46 // without a persistent DB. This is used for the special case where access of
47 // persistent DB is prohibited.
49 // TODO(hclam): Isolate loading default root certs.
51 // NSS will be initialized without loading any user security modules, including
52 // the built-in root certificates module. User security modules need to be
53 // loaded manually after NSS initialization.
55 // If EnsureNSSInit() is called before then this function has no effect.
57 // Calling this method only has effect on Linux.
59 // WARNING: Use this with caution.
60 CRYPTO_EXPORT void ForceNSSNoDBInit();
62 // This methods is used to disable checks in NSS when used in a forked process.
63 // NSS checks whether it is running a forked process to avoid problems when
64 // using user security modules in a forked process. However if we are sure
65 // there are no modules loaded before the process is forked then there is no
66 // harm disabling the check.
68 // This method must be called before EnsureNSSInit() to take effect.
70 // WARNING: Use this with caution.
71 CRYPTO_EXPORT void DisableNSSForkCheck();
73 // Load NSS library files. This function has no effect on Mac and Windows.
74 // This loads the necessary NSS library files so that NSS can be initialized
75 // after loading additional library files is disallowed, for example when the
76 // sandbox is active.
78 // Note that this does not load libnssckbi.so which contains the root
79 // certificates.
80 CRYPTO_EXPORT void LoadNSSLibraries();
82 // Check if the current NSS version is greater than or equals to |version|.
83 // A sample version string is "3.12.3".
84 bool CheckNSSVersion(const char* version);
86 #if defined(OS_CHROMEOS)
87 // Open the r/w nssdb that's stored inside the user's encrypted home
88 // directory. This is the default slot returned by
89 // GetPublicNSSKeySlot().
90 CRYPTO_EXPORT void OpenPersistentNSSDB();
92 // A delegate class that we can use to access the cros API for
93 // communication with cryptohomed and the TPM.
94 class CRYPTO_EXPORT TPMTokenInfoDelegate {
95 public:
96 TPMTokenInfoDelegate();
97 virtual ~TPMTokenInfoDelegate();
99 // Returns true if the hardware supports a TPM Token and the TPM is enabled.
100 virtual bool IsTokenAvailable() const = 0;
102 // Returns true if the TPM and PKCS#11 token slot is ready to be used.
103 // If IsTokenAvailable() is false this should return false.
104 // If IsTokenAvailable() is true, this should eventually return true.
105 virtual bool IsTokenReady() const = 0;
107 // Fetches token properties. TODO(stevenjb): make this interface asynchronous
108 // so that the implementation does not have to be blocking.
109 virtual void GetTokenInfo(std::string* token_name,
110 std::string* user_pin) const = 0;
113 // Indicates that NSS should load the Chaps library so that we
114 // can access the TPM through NSS. Once this is called,
115 // GetPrivateNSSKeySlot() will return the TPM slot if one was found.
116 // Takes ownership of the passed-in delegate object so it can access
117 // the cros library to talk to cryptohomed.
118 CRYPTO_EXPORT void EnableTPMTokenForNSS(TPMTokenInfoDelegate* delegate);
120 // Get name and user PIN for the built-in TPM token on ChromeOS.
121 // Either one can safely be NULL. Should only be called after
122 // EnableTPMTokenForNSS has been called with a non-null delegate.
123 CRYPTO_EXPORT void GetTPMTokenInfo(std::string* token_name,
124 std::string* user_pin);
126 // Returns true if the machine has a TPM and it can be used to store tokens.
127 CRYPTO_EXPORT bool IsTPMTokenAvailable();
129 // Returns true if the TPM is owned and PKCS#11 initialized with the
130 // user and security officer PINs, and has been enabled in NSS by
131 // calling EnableTPMForNSS, and Chaps has been successfully
132 // loaded into NSS.
133 CRYPTO_EXPORT bool IsTPMTokenReady();
135 // Same as IsTPMTokenReady() except this attempts to initialize the token
136 // if necessary.
137 CRYPTO_EXPORT bool EnsureTPMTokenReady();
139 // Gets supplemental user key. Creates one in NSS database if it does not exist.
140 // The supplemental user key is used for AES encryption of user data that is
141 // stored and protected by cryptohome. This additional layer of encryption of
142 // provided to ensure that sensitive data wouldn't be exposed in plain text in
143 // case when an attacker would somehow gain access to all content within
144 // cryptohome.
145 CRYPTO_EXPORT SymmetricKey* GetSupplementalUserKey();
146 #endif
148 // Convert a NSS PRTime value into a base::Time object.
149 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
150 CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64 prtime);
152 // Convert a base::Time object into a PRTime value.
153 // We use a int64 instead of PRTime here to avoid depending on NSPR headers.
154 CRYPTO_EXPORT int64 BaseTimeToPRTime(base::Time time);
156 #if defined(USE_NSS)
157 // Exposed for unittests only.
158 // TODO(mattm): when https://bugzilla.mozilla.org/show_bug.cgi?id=588269 is
159 // fixed, switch back to using a separate userdb for each test. (Maybe refactor
160 // to provide a ScopedTestNSSDB instead of open/close methods.)
161 CRYPTO_EXPORT bool OpenTestNSSDB();
162 // NOTE: due to NSS bug 588269, mentioned above, there is no CloseTestNSSDB.
164 // NSS has a bug which can cause a deadlock or stall in some cases when writing
165 // to the certDB and keyDB. It also has a bug which causes concurrent key pair
166 // generations to scribble over each other. To work around this, we synchronize
167 // writes to the NSS databases with a global lock. The lock is hidden beneath a
168 // function for easy disabling when the bug is fixed. Callers should allow for
169 // it to return NULL in the future.
171 // See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
172 base::Lock* GetNSSWriteLock();
174 // A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
175 // is in scope.
176 class CRYPTO_EXPORT AutoNSSWriteLock {
177 public:
178 AutoNSSWriteLock();
179 ~AutoNSSWriteLock();
180 private:
181 base::Lock *lock_;
182 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
185 #endif // defined(USE_NSS)
187 } // namespace crypto
189 #endif // CRYPTO_NSS_UTIL_H_