blob: 77a964938977126d27616d5f2d2cd346da38d597 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 <string>
9#include "base/basictypes.h"
10#include "crypto/crypto_export.h"
11
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012namespace base {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013class FilePath;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014class Lock;
15class Time;
16} // namespace base
17
18// This file specifically doesn't depend on any NSS or NSPR headers because it
19// is included by various (non-crypto) parts of chrome to call the
20// initialization functions.
21namespace crypto {
22
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023#if defined(USE_NSS)
24// EarlySetupForNSSInit performs lightweight setup which must occur before the
25// process goes multithreaded. This does not initialise NSS. For test, see
26// EnsureNSSInit.
27CRYPTO_EXPORT void EarlySetupForNSSInit();
28#endif
29
30// Initialize NRPR if it isn't already initialized. This function is
31// thread-safe, and NSPR will only ever be initialized once.
32CRYPTO_EXPORT void EnsureNSPRInit();
33
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000034// Initialize NSS safely for strict sandboxing. This function tells NSS to not
35// load user security modules, and makes sure NSS will have proper entropy in a
36// restricted, sandboxed environment.
37//
38// As a defense in depth measure, this function should be called in a sandboxed
39// environment. That way, in the event of a bug, NSS will still not be able to
40// load security modules that could expose private data and keys.
41//
42// Make sure to get an LGTM from the Chrome Security Team if you use this.
43CRYPTO_EXPORT void InitNSSSafely();
44
Torne (Richard Coles)58218062012-11-14 11:43:16 +000045// Initialize NSS if it isn't already initialized. This must be called before
46// any other NSS functions. This function is thread-safe, and NSS will only
47// ever be initialized once.
48CRYPTO_EXPORT void EnsureNSSInit();
49
50// Call this before calling EnsureNSSInit() will force NSS to initialize
51// without a persistent DB. This is used for the special case where access of
52// persistent DB is prohibited.
53//
54// TODO(hclam): Isolate loading default root certs.
55//
56// NSS will be initialized without loading any user security modules, including
57// the built-in root certificates module. User security modules need to be
58// loaded manually after NSS initialization.
59//
60// If EnsureNSSInit() is called before then this function has no effect.
61//
62// Calling this method only has effect on Linux.
63//
64// WARNING: Use this with caution.
65CRYPTO_EXPORT void ForceNSSNoDBInit();
66
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067// This method is used to disable checks in NSS when used in a forked process.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068// NSS checks whether it is running a forked process to avoid problems when
69// using user security modules in a forked process. However if we are sure
70// there are no modules loaded before the process is forked then there is no
71// harm disabling the check.
72//
73// This method must be called before EnsureNSSInit() to take effect.
74//
75// WARNING: Use this with caution.
76CRYPTO_EXPORT void DisableNSSForkCheck();
77
78// Load NSS library files. This function has no effect on Mac and Windows.
79// This loads the necessary NSS library files so that NSS can be initialized
80// after loading additional library files is disallowed, for example when the
81// sandbox is active.
82//
83// Note that this does not load libnssckbi.so which contains the root
84// certificates.
85CRYPTO_EXPORT void LoadNSSLibraries();
86
87// Check if the current NSS version is greater than or equals to |version|.
88// A sample version string is "3.12.3".
89bool CheckNSSVersion(const char* version);
90
91#if defined(OS_CHROMEOS)
92// Open the r/w nssdb that's stored inside the user's encrypted home
93// directory. This is the default slot returned by
94// GetPublicNSSKeySlot().
95CRYPTO_EXPORT void OpenPersistentNSSDB();
96
97// Indicates that NSS should load the Chaps library so that we
98// can access the TPM through NSS. Once this is called,
99// GetPrivateNSSKeySlot() will return the TPM slot if one was found.
100CRYPTO_EXPORT void EnableTPMTokenForNSS();
101
102// Get name and user PIN for the built-in TPM token on ChromeOS.
103// Either one can safely be NULL. Should only be called after
104// EnableTPMTokenForNSS has been called with a non-null delegate.
105CRYPTO_EXPORT void GetTPMTokenInfo(std::string* token_name,
106 std::string* user_pin);
107
108// Returns true if the TPM is owned and PKCS#11 initialized with the
109// user and security officer PINs, and has been enabled in NSS by
110// calling EnableTPMForNSS, and Chaps has been successfully
111// loaded into NSS.
112CRYPTO_EXPORT bool IsTPMTokenReady();
113
114// Initialize the TPM token. Does nothing if it is already initialized.
115CRYPTO_EXPORT bool InitializeTPMToken(const std::string& token_name,
116 const std::string& user_pin);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000117#endif
118
119// Convert a NSS PRTime value into a base::Time object.
120// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
121CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64 prtime);
122
123// Convert a base::Time object into a PRTime value.
124// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
125CRYPTO_EXPORT int64 BaseTimeToPRTime(base::Time time);
126
127#if defined(USE_NSS)
128// Exposed for unittests only.
129// TODO(mattm): When NSS 3.14 is the minimum version required,
130// switch back to using a separate user DB for each test.
131// Because of https://bugzilla.mozilla.org/show_bug.cgi?id=588269 , the
132// opened user DB is not automatically closed.
133class CRYPTO_EXPORT_PRIVATE ScopedTestNSSDB {
134 public:
135 ScopedTestNSSDB();
136 ~ScopedTestNSSDB();
137
138 bool is_open() { return is_open_; }
139
140 private:
141 bool is_open_;
142 DISALLOW_COPY_AND_ASSIGN(ScopedTestNSSDB);
143};
144
145// NSS has a bug which can cause a deadlock or stall in some cases when writing
146// to the certDB and keyDB. It also has a bug which causes concurrent key pair
147// generations to scribble over each other. To work around this, we synchronize
148// writes to the NSS databases with a global lock. The lock is hidden beneath a
149// function for easy disabling when the bug is fixed. Callers should allow for
150// it to return NULL in the future.
151//
152// See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
153base::Lock* GetNSSWriteLock();
154
155// A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
156// is in scope.
157class CRYPTO_EXPORT AutoNSSWriteLock {
158 public:
159 AutoNSSWriteLock();
160 ~AutoNSSWriteLock();
161 private:
162 base::Lock *lock_;
163 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
164};
165
166#endif // defined(USE_NSS)
167
168} // namespace crypto
169
170#endif // CRYPTO_NSS_UTIL_H_