blob: a8b57ff9f0e8bd3dbc3d9a7804ac9b9de0910830 [file] [log] [blame]
[email protected]02746292012-01-25 04:37:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bc1e07c72008-09-16 14:32:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_NSS_UTIL_H_
6#define CRYPTO_NSS_UTIL_H_
[email protected]1b1a264a2010-01-14 22:36:357
avidd373b8b2015-12-21 21:34:438#include <stdint.h>
9
[email protected]6a89ef22011-04-07 17:34:2110#include <string>
[email protected]e2ea5ca2014-02-27 22:27:2111#include "base/callback.h"
[email protected]557737f72013-12-06 22:24:0712#include "base/compiler_specific.h"
avidd373b8b2015-12-21 21:34:4313#include "base/macros.h"
[email protected]d613a9902011-08-05 20:59:1114#include "crypto/crypto_export.h"
[email protected]bc1e07c72008-09-16 14:32:4415
[email protected]4b559b4d2011-04-14 17:37:1416namespace base {
[email protected]a3ef4832013-02-02 05:12:3317class FilePath;
[email protected]4b559b4d2011-04-14 17:37:1418class Lock;
19class Time;
20} // namespace base
21
[email protected]41c78fa2010-03-22 20:08:4122// 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.
[email protected]4b559b4d2011-04-14 17:37:1425namespace crypto {
[email protected]1b1a264a2010-01-14 22:36:3526
[email protected]ac3d5972011-01-13 20:33:4527// EarlySetupForNSSInit performs lightweight setup which must occur before the
28// process goes multithreaded. This does not initialise NSS. For test, see
29// EnsureNSSInit.
[email protected]d613a9902011-08-05 20:59:1130CRYPTO_EXPORT void EarlySetupForNSSInit();
[email protected]ac3d5972011-01-13 20:33:4531
[email protected]730fb132009-09-02 22:50:2532// Initialize NRPR if it isn't already initialized. This function is
[email protected]4b559b4d2011-04-14 17:37:1433// thread-safe, and NSPR will only ever be initialized once.
[email protected]d613a9902011-08-05 20:59:1134CRYPTO_EXPORT void EnsureNSPRInit();
[email protected]730fb132009-09-02 22:50:2535
[email protected]bc1e07c72008-09-16 14:32:4436// Initialize NSS if it isn't already initialized. This must be called before
37// any other NSS functions. This function is thread-safe, and NSS will only
[email protected]4b559b4d2011-04-14 17:37:1438// ever be initialized once.
[email protected]d613a9902011-08-05 20:59:1139CRYPTO_EXPORT void EnsureNSSInit();
[email protected]bc1e07c72008-09-16 14:32:4440
[email protected]f61c3972010-12-23 09:54:1541// Check if the current NSS version is greater than or equals to |version|.
42// A sample version string is "3.12.3".
43bool CheckNSSVersion(const char* version);
44
[email protected]dcce6cf2010-04-29 17:50:0645#if defined(OS_CHROMEOS)
[email protected]496318862014-07-13 07:19:0046// Indicates that NSS should use the Chaps library so that we
47// can access the TPM through NSS. InitializeTPMTokenAndSystemSlot and
48// InitializeTPMForChromeOSUser must still be called to load the slots.
[email protected]450b4ad72012-05-17 10:04:1749CRYPTO_EXPORT void EnableTPMTokenForNSS();
[email protected]74beead2011-04-12 20:40:1250
[email protected]557737f72013-12-06 22:24:0751// Returns true if EnableTPMTokenForNSS has been called.
52CRYPTO_EXPORT bool IsTPMTokenEnabledForNSS();
53
[email protected]c64b9142011-04-19 18:49:5454// Returns true if the TPM is owned and PKCS#11 initialized with the
55// user and security officer PINs, and has been enabled in NSS by
[email protected]84e47722011-11-17 05:12:0256// calling EnableTPMForNSS, and Chaps has been successfully
[email protected]c64b9142011-04-19 18:49:5457// loaded into NSS.
[email protected]557737f72013-12-06 22:24:0758// If |callback| is non-null and the function returns false, the |callback| will
59// be run once the TPM is ready. |callback| will never be run if the function
60// returns true.
61CRYPTO_EXPORT bool IsTPMTokenReady(const base::Closure& callback)
62 WARN_UNUSED_RESULT;
[email protected]c175cdb2011-06-28 20:41:5563
[email protected]496318862014-07-13 07:19:0064// Initialize the TPM token and system slot. The |callback| will run on the same
65// thread with true if the token and slot were successfully loaded or were
66// already initialized. |callback| will be passed false if loading failed. Once
67// called, InitializeTPMTokenAndSystemSlot must not be called again until the
68// |callback| has been run.
69CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot(
70 int system_slot_id,
[email protected]e2ea5ca2014-02-27 22:27:2171 const base::Callback<void(bool)>& callback);
[email protected]dcce6cf2010-04-29 17:50:0672#endif
73
[email protected]1b1a264a2010-01-14 22:36:3574// Convert a NSS PRTime value into a base::Time object.
avidd373b8b2015-12-21 21:34:4375// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
76CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime);
[email protected]1b1a264a2010-01-14 22:36:3577
[email protected]ca929ed32011-12-15 20:37:2878// Convert a base::Time object into a PRTime value.
avidd373b8b2015-12-21 21:34:4379// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
80CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time);
[email protected]ca929ed32011-12-15 20:37:2881
[email protected]69138472010-06-25 22:44:4882// NSS has a bug which can cause a deadlock or stall in some cases when writing
83// to the certDB and keyDB. It also has a bug which causes concurrent key pair
84// generations to scribble over each other. To work around this, we synchronize
85// writes to the NSS databases with a global lock. The lock is hidden beneath a
86// function for easy disabling when the bug is fixed. Callers should allow for
87// it to return NULL in the future.
88//
89// See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
[email protected]4b559b4d2011-04-14 17:37:1490base::Lock* GetNSSWriteLock();
[email protected]69138472010-06-25 22:44:4891
92// A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
93// is in scope.
[email protected]d613a9902011-08-05 20:59:1194class CRYPTO_EXPORT AutoNSSWriteLock {
[email protected]69138472010-06-25 22:44:4895 public:
96 AutoNSSWriteLock();
97 ~AutoNSSWriteLock();
98 private:
[email protected]4b559b4d2011-04-14 17:37:1499 base::Lock *lock_;
[email protected]69138472010-06-25 22:44:48100 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
101};
[email protected]69138472010-06-25 22:44:48102
[email protected]4b559b4d2011-04-14 17:37:14103} // namespace crypto
[email protected]bc1e07c72008-09-16 14:32:44104
[email protected]4b559b4d2011-04-14 17:37:14105#endif // CRYPTO_NSS_UTIL_H_