blob: 5c34fc8f0782b5405e4a9f39b614bc668bdef6e4 [file] [log] [blame]
Alex Vakulenkof6024732016-01-22 16:52:43 -08001// 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
Alex Vakulenko24854742016-01-22 16:55:13 -08008#include <stdint.h>
9
Alex Vakulenkof6024732016-01-22 16:52:43 -080010#include <string>
Alex Vakulenkof6024732016-01-22 16:52:43 -080011#include "base/callback.h"
12#include "base/compiler_specific.h"
Alex Vakulenko24854742016-01-22 16:55:13 -080013#include "base/macros.h"
Alex Vakulenkof6024732016-01-22 16:52:43 -080014#include "crypto/crypto_export.h"
15
16namespace base {
Alex Vakulenkof6024732016-01-22 16:52:43 -080017class Lock;
18class 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.
24namespace crypto {
25
Alex Vakulenkof6024732016-01-22 16:52:43 -080026// EarlySetupForNSSInit performs lightweight setup which must occur before the
27// process goes multithreaded. This does not initialise NSS. For test, see
28// EnsureNSSInit.
29CRYPTO_EXPORT void EarlySetupForNSSInit();
Alex Vakulenkof6024732016-01-22 16:52:43 -080030
31// Initialize NRPR if it isn't already initialized. This function is
32// thread-safe, and NSPR will only ever be initialized once.
33CRYPTO_EXPORT void EnsureNSPRInit();
34
Alex Vakulenkof6024732016-01-22 16:52:43 -080035// 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.
38CRYPTO_EXPORT void EnsureNSSInit();
39
Alex Vakulenkof6024732016-01-22 16:52:43 -080040// Check if the current NSS version is greater than or equals to |version|.
41// A sample version string is "3.12.3".
42bool 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.
48CRYPTO_EXPORT void EnableTPMTokenForNSS();
49
50// Returns true if EnableTPMTokenForNSS has been called.
51CRYPTO_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.
60CRYPTO_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.
68CRYPTO_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.
Alex Vakulenko24854742016-01-22 16:55:13 -080074// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
75CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime);
Alex Vakulenkof6024732016-01-22 16:52:43 -080076
77// Convert a base::Time object into a PRTime value.
Alex Vakulenko24854742016-01-22 16:55:13 -080078// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
79CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time);
Alex Vakulenkof6024732016-01-22 16:52:43 -080080
Alex Vakulenkof6024732016-01-22 16:52:43 -080081// 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
89base::Lock* GetNSSWriteLock();
90
91// A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
92// is in scope.
93class CRYPTO_EXPORT AutoNSSWriteLock {
94 public:
95 AutoNSSWriteLock();
96 ~AutoNSSWriteLock();
97 private:
98 base::Lock *lock_;
99 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
100};
Alex Vakulenkof6024732016-01-22 16:52:43 -0800101
102} // namespace crypto
103
104#endif // CRYPTO_NSS_UTIL_H_