159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved. 259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Use of this source code is governed by a BSD-style license that can be 359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// found in the LICENSE file. 459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#ifndef CRYPTO_NSS_UTIL_INTERNAL_H_ 659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#define CRYPTO_NSS_UTIL_INTERNAL_H_ 759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#include <secmodt.h> 959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 1059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#include "base/callback.h" 1159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#include "base/compiler_specific.h" 12cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include "base/macros.h" 1359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#include "crypto/crypto_export.h" 1459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#include "crypto/scoped_nss_types.h" 1559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 1659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Eratnamespace base { 1759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Eratclass FilePath; 1859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat} 1959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 2059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// These functions return a type defined in an NSS header, and so cannot be 2159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// declared in nss_util.h. Hence, they are declared here. 2259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 2359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Eratnamespace crypto { 2459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 2559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Opens an NSS software database in folder |path|, with the (potentially) 2659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// user-visible description |description|. Returns the slot for the opened 2759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// database, or NULL if the database could not be opened. 28cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoCRYPTO_EXPORT ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path, 29cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko const std::string& description); 3059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 3159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#if !defined(OS_CHROMEOS) 3259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Returns a reference to the default NSS key slot for storing persistent data. 3359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Caller must release returned reference with PK11_FreeSlot. 3459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT PK11SlotInfo* GetPersistentNSSKeySlot() WARN_UNUSED_RESULT; 3559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#endif 3659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 3759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// A helper class that acquires the SECMOD list read lock while the 3859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// AutoSECMODListReadLock is in scope. 3959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Eratclass CRYPTO_EXPORT AutoSECMODListReadLock { 4059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat public: 4159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat AutoSECMODListReadLock(); 4259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat ~AutoSECMODListReadLock(); 4359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 4459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat private: 4559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat SECMODListLock* lock_; 4659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock); 4759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat}; 4859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 4959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#if defined(OS_CHROMEOS) 5059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Returns a reference to the system-wide TPM slot if it is loaded. If it is not 5159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// loaded and |callback| is non-null, the |callback| will be run once the slot 5259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// is loaded. 5359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT ScopedPK11Slot GetSystemNSSKeySlot( 5459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const base::Callback<void(ScopedPK11Slot)>& callback) WARN_UNUSED_RESULT; 5559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 5659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Sets the test system slot to |slot|, which means that |slot| will be exposed 5759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// through |GetSystemNSSKeySlot| and |IsTPMTokenReady| will return true. 5859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// |InitializeTPMTokenAndSystemSlot|, which triggers the TPM initialization, 5959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// does not have to be called if the test system slot is set. 6059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// This must must not be called consecutively with a |slot| != NULL. If |slot| 6159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// is NULL, the test system slot is unset. 62cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoCRYPTO_EXPORT void SetSystemKeySlotForTesting(ScopedPK11Slot slot); 6359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 6459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Prepare per-user NSS slot mapping. It is safe to call this function multiple 6559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// times. Returns true if the user was added, or false if it already existed. 6659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT bool InitializeNSSForChromeOSUser( 6759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash, 6859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const base::FilePath& path); 6959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 7059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Returns whether TPM for ChromeOS user still needs initialization. If 7159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// true is returned, the caller can proceed to initialize TPM slot for the 7259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// user, but should call |WillInitializeTPMForChromeOSUser| first. 7359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// |InitializeNSSForChromeOSUser| must have been called first. 7459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT bool ShouldInitializeTPMForChromeOSUser( 7559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash) WARN_UNUSED_RESULT; 7659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 7759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Makes |ShouldInitializeTPMForChromeOSUser| start returning false. 7859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Should be called before starting TPM initialization for the user. 7959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Assumes |InitializeNSSForChromeOSUser| had already been called. 8059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT void WillInitializeTPMForChromeOSUser( 8159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash); 8259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 8359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Use TPM slot |slot_id| for user. InitializeNSSForChromeOSUser must have been 8459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// called first. 8559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT void InitializeTPMForChromeOSUser( 8659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash, 8759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat CK_SLOT_ID slot_id); 8859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 8959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Use the software slot as the private slot for user. 9059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// InitializeNSSForChromeOSUser must have been called first. 9159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT void InitializePrivateSoftwareSlotForChromeOSUser( 9259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash); 9359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 9459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Returns a reference to the public slot for user. 9559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT ScopedPK11Slot GetPublicSlotForChromeOSUser( 9659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash) WARN_UNUSED_RESULT; 9759c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 9859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Returns the private slot for |username_hash| if it is loaded. If it is not 9959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// loaded and |callback| is non-null, the |callback| will be run once the slot 10059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// is loaded. 10159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel EratCRYPTO_EXPORT ScopedPK11Slot GetPrivateSlotForChromeOSUser( 10259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash, 10359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const base::Callback<void(ScopedPK11Slot)>& callback) WARN_UNUSED_RESULT; 10459c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 10559c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// Closes the NSS DB for |username_hash| that was previously opened by the 10659c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat// *Initialize*ForChromeOSUser functions. 107cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoCRYPTO_EXPORT void CloseChromeOSUserForTesting( 10859c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat const std::string& username_hash); 10959c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#endif // defined(OS_CHROMEOS) 11059c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 11159c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat} // namespace crypto 11259c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat 11359c5f4b0fb104e8e4806e4934a3d5d112ad695abDaniel Erat#endif // CRYPTO_NSS_UTIL_INTERNAL_H_ 114