[go: nahoru, domu]

blob: 1d7503d81315649039de8f3a2eabac3a2b7e5c0c [file] [log] [blame]
mattm@chromium.org02746292012-01-25 04:37:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
deanm@google.combc1e07c72008-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
rvargas@google.com4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_NSS_UTIL_H_
6#define CRYPTO_NSS_UTIL_H_
mattm@chromium.org1b1a264a2010-01-14 22:36:357
gspencer@chromium.org6a89ef22011-04-07 17:34:218#include <string>
mattm@chromium.org1b1a264a2010-01-14 22:36:359#include "base/basictypes.h"
darin@chromium.orgd613a9902011-08-05 20:59:1110#include "crypto/crypto_export.h"
deanm@google.combc1e07c72008-09-16 14:32:4411
rvargas@google.com4b559b4d2011-04-14 17:37:1412namespace base {
brettw@chromium.orga3ef4832013-02-02 05:12:3313class FilePath;
rvargas@google.com4b559b4d2011-04-14 17:37:1414class Lock;
15class Time;
16} // namespace base
17
albertb@chromium.org41c78fa2010-03-22 20:08:4118// 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.
rvargas@google.com4b559b4d2011-04-14 17:37:1421namespace crypto {
deanm@google.combc1e07c72008-09-16 14:32:4422
pneubeck@chromium.orgb17bc3662013-08-12 14:09:1923// The TPMToken name used for the NSS slot opened by ScopedTestNSSDB.
24CRYPTO_EXPORT extern const char kTestTPMTokenName[];
25
agl@chromium.orgac3d5972011-01-13 20:33:4526#if defined(USE_NSS)
27// EarlySetupForNSSInit performs lightweight setup which must occur before the
28// process goes multithreaded. This does not initialise NSS. For test, see
29// EnsureNSSInit.
darin@chromium.orgd613a9902011-08-05 20:59:1130CRYPTO_EXPORT void EarlySetupForNSSInit();
agl@chromium.orgac3d5972011-01-13 20:33:4531#endif
32
evan@chromium.org730fb132009-09-02 22:50:2533// Initialize NRPR if it isn't already initialized. This function is
rvargas@google.com4b559b4d2011-04-14 17:37:1434// thread-safe, and NSPR will only ever be initialized once.
darin@chromium.orgd613a9902011-08-05 20:59:1135CRYPTO_EXPORT void EnsureNSPRInit();
evan@chromium.org730fb132009-09-02 22:50:2536
jorgelo@chromium.org2fdd99e82012-11-29 04:46:4137// Initialize NSS safely for strict sandboxing. This function tells NSS to not
38// load user security modules, and makes sure NSS will have proper entropy in a
39// restricted, sandboxed environment.
jorgelo@chromium.org5fe0c7692012-11-17 03:57:5640//
41// As a defense in depth measure, this function should be called in a sandboxed
jorgelo@chromium.org2fdd99e82012-11-29 04:46:4142// environment. That way, in the event of a bug, NSS will still not be able to
43// load security modules that could expose private data and keys.
44//
45// Make sure to get an LGTM from the Chrome Security Team if you use this.
46CRYPTO_EXPORT void InitNSSSafely();
jorgelo@chromium.org5fe0c7692012-11-17 03:57:5647
deanm@google.combc1e07c72008-09-16 14:32:4448// Initialize NSS if it isn't already initialized. This must be called before
49// any other NSS functions. This function is thread-safe, and NSS will only
rvargas@google.com4b559b4d2011-04-14 17:37:1450// ever be initialized once.
darin@chromium.orgd613a9902011-08-05 20:59:1151CRYPTO_EXPORT void EnsureNSSInit();
deanm@google.combc1e07c72008-09-16 14:32:4452
hclam@google.comed450f32011-03-16 01:26:4953// Call this before calling EnsureNSSInit() will force NSS to initialize
54// without a persistent DB. This is used for the special case where access of
55// persistent DB is prohibited.
56//
57// TODO(hclam): Isolate loading default root certs.
58//
59// NSS will be initialized without loading any user security modules, including
60// the built-in root certificates module. User security modules need to be
61// loaded manually after NSS initialization.
62//
63// If EnsureNSSInit() is called before then this function has no effect.
64//
65// Calling this method only has effect on Linux.
66//
67// WARNING: Use this with caution.
darin@chromium.orgd613a9902011-08-05 20:59:1168CRYPTO_EXPORT void ForceNSSNoDBInit();
hclam@google.comed450f32011-03-16 01:26:4969
jorgelo@chromium.org5fe0c7692012-11-17 03:57:5670// This method is used to disable checks in NSS when used in a forked process.
hclam@google.comed450f32011-03-16 01:26:4971// NSS checks whether it is running a forked process to avoid problems when
72// using user security modules in a forked process. However if we are sure
73// there are no modules loaded before the process is forked then there is no
74// harm disabling the check.
75//
76// This method must be called before EnsureNSSInit() to take effect.
77//
78// WARNING: Use this with caution.
darin@chromium.orgd613a9902011-08-05 20:59:1179CRYPTO_EXPORT void DisableNSSForkCheck();
hclam@google.comed450f32011-03-16 01:26:4980
hclam@google.comf6a67b42011-03-17 23:49:2181// Load NSS library files. This function has no effect on Mac and Windows.
82// This loads the necessary NSS library files so that NSS can be initialized
83// after loading additional library files is disallowed, for example when the
84// sandbox is active.
85//
86// Note that this does not load libnssckbi.so which contains the root
87// certificates.
darin@chromium.orgd613a9902011-08-05 20:59:1188CRYPTO_EXPORT void LoadNSSLibraries();
hclam@google.comf6a67b42011-03-17 23:49:2189
hclam@chromium.orgf61c3972010-12-23 09:54:1590// Check if the current NSS version is greater than or equals to |version|.
91// A sample version string is "3.12.3".
92bool CheckNSSVersion(const char* version);
93
cmasone@google.comdcce6cf2010-04-29 17:50:0694#if defined(OS_CHROMEOS)
gspencer@chromium.org6a89ef22011-04-07 17:34:2195// Open the r/w nssdb that's stored inside the user's encrypted home
96// directory. This is the default slot returned by
97// GetPublicNSSKeySlot().
darin@chromium.orgd613a9902011-08-05 20:59:1198CRYPTO_EXPORT void OpenPersistentNSSDB();
gspencer@chromium.org6a89ef22011-04-07 17:34:2199
dkrahn@chromium.org84e47722011-11-17 05:12:02100// Indicates that NSS should load the Chaps library so that we
gspencer@chromium.orgc64b9142011-04-19 18:49:54101// can access the TPM through NSS. Once this is called,
102// GetPrivateNSSKeySlot() will return the TPM slot if one was found.
hashimoto@chromium.org450b4ad72012-05-17 10:04:17103CRYPTO_EXPORT void EnableTPMTokenForNSS();
gspencer@chromium.org74beead2011-04-12 20:40:12104
gspencer@chromium.orgc64b9142011-04-19 18:49:54105// Get name and user PIN for the built-in TPM token on ChromeOS.
106// Either one can safely be NULL. Should only be called after
107// EnableTPMTokenForNSS has been called with a non-null delegate.
darin@chromium.orgd613a9902011-08-05 20:59:11108CRYPTO_EXPORT void GetTPMTokenInfo(std::string* token_name,
109 std::string* user_pin);
stevenjb@google.comc175cdb2011-06-28 20:41:55110
gspencer@chromium.orgc64b9142011-04-19 18:49:54111// Returns true if the TPM is owned and PKCS#11 initialized with the
112// user and security officer PINs, and has been enabled in NSS by
dkrahn@chromium.org84e47722011-11-17 05:12:02113// calling EnableTPMForNSS, and Chaps has been successfully
gspencer@chromium.orgc64b9142011-04-19 18:49:54114// loaded into NSS.
darin@chromium.orgd613a9902011-08-05 20:59:11115CRYPTO_EXPORT bool IsTPMTokenReady();
stevenjb@google.comc175cdb2011-06-28 20:41:55116
hashimoto@chromium.org14172c82012-02-28 10:34:21117// Initialize the TPM token. Does nothing if it is already initialized.
hashimoto@chromium.org450b4ad72012-05-17 10:04:17118CRYPTO_EXPORT bool InitializeTPMToken(const std::string& token_name,
119 const std::string& user_pin);
cmasone@google.comdcce6cf2010-04-29 17:50:06120#endif
121
mattm@chromium.org1b1a264a2010-01-14 22:36:35122// Convert a NSS PRTime value into a base::Time object.
123// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
darin@chromium.orgd613a9902011-08-05 20:59:11124CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64 prtime);
mattm@chromium.org1b1a264a2010-01-14 22:36:35125
mattm@chromium.orgca929ed32011-12-15 20:37:28126// Convert a base::Time object into a PRTime value.
127// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
128CRYPTO_EXPORT int64 BaseTimeToPRTime(base::Time time);
129
davidben@chromium.org69138472010-06-25 22:44:48130#if defined(USE_NSS)
mattm@chromium.org02746292012-01-25 04:37:51131// Exposed for unittests only.
toyoshim@chromium.org7025e932012-10-18 07:02:54132// TODO(mattm): When NSS 3.14 is the minimum version required,
133// switch back to using a separate user DB for each test.
134// Because of https://bugzilla.mozilla.org/show_bug.cgi?id=588269 , the
135// opened user DB is not automatically closed.
136class CRYPTO_EXPORT_PRIVATE ScopedTestNSSDB {
137 public:
138 ScopedTestNSSDB();
139 ~ScopedTestNSSDB();
140
141 bool is_open() { return is_open_; }
142
143 private:
144 bool is_open_;
145 DISALLOW_COPY_AND_ASSIGN(ScopedTestNSSDB);
146};
mattm@chromium.orgbb639032010-08-12 19:49:40147
davidben@chromium.org69138472010-06-25 22:44:48148// NSS has a bug which can cause a deadlock or stall in some cases when writing
149// to the certDB and keyDB. It also has a bug which causes concurrent key pair
150// generations to scribble over each other. To work around this, we synchronize
151// writes to the NSS databases with a global lock. The lock is hidden beneath a
152// function for easy disabling when the bug is fixed. Callers should allow for
153// it to return NULL in the future.
154//
155// See https://bugzilla.mozilla.org/show_bug.cgi?id=564011
rvargas@google.com4b559b4d2011-04-14 17:37:14156base::Lock* GetNSSWriteLock();
davidben@chromium.org69138472010-06-25 22:44:48157
158// A helper class that acquires the NSS write Lock while the AutoNSSWriteLock
159// is in scope.
darin@chromium.orgd613a9902011-08-05 20:59:11160class CRYPTO_EXPORT AutoNSSWriteLock {
davidben@chromium.org69138472010-06-25 22:44:48161 public:
162 AutoNSSWriteLock();
163 ~AutoNSSWriteLock();
164 private:
rvargas@google.com4b559b4d2011-04-14 17:37:14165 base::Lock *lock_;
davidben@chromium.org69138472010-06-25 22:44:48166 DISALLOW_COPY_AND_ASSIGN(AutoNSSWriteLock);
167};
168
169#endif // defined(USE_NSS)
170
rvargas@google.com4b559b4d2011-04-14 17:37:14171} // namespace crypto
deanm@google.combc1e07c72008-09-16 14:32:44172
rvargas@google.com4b559b4d2011-04-14 17:37:14173#endif // CRYPTO_NSS_UTIL_H_