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. 4 5 #ifndef CRYPTO_NSS_UTIL_H_ 6 #define CRYPTO_NSS_UTIL_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 #include "base/callback.h" 12 #include "base/compiler_specific.h" 13 #include "base/macros.h" 14 #include "crypto/crypto_export.h" 15 16 namespace base { 17 class Lock; 18 class Time; 19 } // namespace base 20 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 { 25 26 // EarlySetupForNSSInit performs lightweight setup which must occur before the 27 // process goes multithreaded. This does not initialise NSS. For test, see 28 // EnsureNSSInit. 29 CRYPTO_EXPORT void EarlySetupForNSSInit(); 30 31 // Initialize NRPR if it isn't already initialized. This function is 32 // thread-safe, and NSPR will only ever be initialized once. 33 CRYPTO_EXPORT void EnsureNSPRInit(); 34 35 // Initialize NSS if it isn't already initialized. This must be called before 36 // any other NSS functions. This function is thread-safe, and NSS will only 37 // ever be initialized once. 38 CRYPTO_EXPORT void EnsureNSSInit(); 39 40 // Check if the current NSS version is greater than or equals to |version|. 41 // A sample version string is "3.12.3". 42 bool CheckNSSVersion(const char* version); 43 44 #if defined(OS_CHROMEOS) 45 // Indicates that NSS should use the Chaps library so that we 46 // can access the TPM through NSS. InitializeTPMTokenAndSystemSlot and 47 // InitializeTPMForChromeOSUser must still be called to load the slots. 48 CRYPTO_EXPORT void EnableTPMTokenForNSS(); 49 50 // Returns true if EnableTPMTokenForNSS has been called. 51 CRYPTO_EXPORT bool IsTPMTokenEnabledForNSS(); 52 53 // Returns true if the TPM is owned and PKCS#11 initialized with the 54 // user and security officer PINs, and has been enabled in NSS by 55 // calling EnableTPMForNSS, and Chaps has been successfully 56 // loaded into NSS. 57 // If |callback| is non-null and the function returns false, the |callback| will 58 // be run once the TPM is ready. |callback| will never be run if the function 59 // returns true. 60 CRYPTO_EXPORT bool IsTPMTokenReady(const base::Closure& callback) 61 WARN_UNUSED_RESULT; 62 63 // Initialize the TPM token and system slot. The |callback| will run on the same 64 // thread with true if the token and slot were successfully loaded or were 65 // already initialized. |callback| will be passed false if loading failed. Once 66 // called, InitializeTPMTokenAndSystemSlot must not be called again until the 67 // |callback| has been run. 68 CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot( 69 int system_slot_id, 70 const base::Callback<void(bool)>& callback); 71 #endif 72 73 // Convert a NSS PRTime value into a base::Time object. 74 // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. 75 CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime); 76 77 // Convert a base::Time object into a PRTime value. 78 // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. 79 CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time); 80 81 // NSS has a bug which can cause a deadlock or stall in some cases when writing 82 // to the certDB and keyDB. It also has a bug which causes concurrent key pair 83 // generations to scribble over each other. To work around this, we synchronize 84 // writes to the NSS databases with a global lock. The lock is hidden beneath a 85 // function for easy disabling when the bug is fixed. Callers should allow for 86 // it to return NULL in the future. 87 // 88 // See https://bugzilla.mozilla.org/show_bug.cgi?id=564011 89 base::Lock* GetNSSWriteLock(); 90 91 // A helper class that acquires the NSS write Lock while the AutoNSSWriteLock 92 // is in scope. 93 class CRYPTO_EXPORT AutoNSSWriteLock { 94 public: 95 AutoNSSWriteLock(); 96 ~AutoNSSWriteLock(); 97 private: 98 base::Lock *lock_; 99 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock); 100 }; 101 102 } // namespace crypto 103 104 #endif // CRYPTO_NSS_UTIL_H_ 105