1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef CRYPTO_NSS_UTIL_H_ 6*635a8641SAndroid Build Coastguard Worker #define CRYPTO_NSS_UTIL_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <string> 11*635a8641SAndroid Build Coastguard Worker #include "base/callback.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 14*635a8641SAndroid Build Coastguard Worker #include "crypto/crypto_export.h" 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker namespace base { 17*635a8641SAndroid Build Coastguard Worker class Time; 18*635a8641SAndroid Build Coastguard Worker } // namespace base 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Worker // This file specifically doesn't depend on any NSS or NSPR headers because it 21*635a8641SAndroid Build Coastguard Worker // is included by various (non-crypto) parts of chrome to call the 22*635a8641SAndroid Build Coastguard Worker // initialization functions. 23*635a8641SAndroid Build Coastguard Worker namespace crypto { 24*635a8641SAndroid Build Coastguard Worker 25*635a8641SAndroid Build Coastguard Worker // Initialize NRPR if it isn't already initialized. This function is 26*635a8641SAndroid Build Coastguard Worker // thread-safe, and NSPR will only ever be initialized once. 27*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void EnsureNSPRInit(); 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker // Initialize NSS if it isn't already initialized. This must be called before 30*635a8641SAndroid Build Coastguard Worker // any other NSS functions. This function is thread-safe, and NSS will only 31*635a8641SAndroid Build Coastguard Worker // ever be initialized once. 32*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void EnsureNSSInit(); 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Worker // Check if the current NSS version is greater than or equals to |version|. 35*635a8641SAndroid Build Coastguard Worker // A sample version string is "3.12.3". 36*635a8641SAndroid Build Coastguard Worker bool CheckNSSVersion(const char* version); 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS) 39*635a8641SAndroid Build Coastguard Worker // Indicates that NSS should use the Chaps library so that we 40*635a8641SAndroid Build Coastguard Worker // can access the TPM through NSS. InitializeTPMTokenAndSystemSlot and 41*635a8641SAndroid Build Coastguard Worker // InitializeTPMForChromeOSUser must still be called to load the slots. 42*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void EnableTPMTokenForNSS(); 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker // Returns true if EnableTPMTokenForNSS has been called. 45*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT bool IsTPMTokenEnabledForNSS(); 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker // Returns true if the TPM is owned and PKCS#11 initialized with the 48*635a8641SAndroid Build Coastguard Worker // user and security officer PINs, and has been enabled in NSS by 49*635a8641SAndroid Build Coastguard Worker // calling EnableTPMForNSS, and Chaps has been successfully 50*635a8641SAndroid Build Coastguard Worker // loaded into NSS. 51*635a8641SAndroid Build Coastguard Worker // If |callback| is non-null and the function returns false, the |callback| will 52*635a8641SAndroid Build Coastguard Worker // be run once the TPM is ready. |callback| will never be run if the function 53*635a8641SAndroid Build Coastguard Worker // returns true. 54*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT bool IsTPMTokenReady(base::OnceClosure callback) 55*635a8641SAndroid Build Coastguard Worker WARN_UNUSED_RESULT; 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker // Initialize the TPM token and system slot. The |callback| will run on the same 58*635a8641SAndroid Build Coastguard Worker // thread with true if the token and slot were successfully loaded or were 59*635a8641SAndroid Build Coastguard Worker // already initialized. |callback| will be passed false if loading failed. Once 60*635a8641SAndroid Build Coastguard Worker // called, InitializeTPMTokenAndSystemSlot must not be called again until the 61*635a8641SAndroid Build Coastguard Worker // |callback| has been run. 62*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot( 63*635a8641SAndroid Build Coastguard Worker int system_slot_id, 64*635a8641SAndroid Build Coastguard Worker base::OnceCallback<void(bool)> callback); 65*635a8641SAndroid Build Coastguard Worker #endif 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker // Convert a NSS PRTime value into a base::Time object. 68*635a8641SAndroid Build Coastguard Worker // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. 69*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker // Convert a base::Time object into a PRTime value. 72*635a8641SAndroid Build Coastguard Worker // We use a int64_t instead of PRTime here to avoid depending on NSPR headers. 73*635a8641SAndroid Build Coastguard Worker CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time); 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker } // namespace crypto 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker #endif // CRYPTO_NSS_UTIL_H_ 78