blob: 8cdfd179fcc9d9a6470df6a68e18fb071d2d7f44 [file] [log] [blame]
Avi Drissman201a9a832022-09-13 19:39:251// Copyright 2012 The Chromium Authors
[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
Michael Ershov6b5e4182021-09-28 13:46:3210#include "base/callback_forward.h"
[email protected]557737f72013-12-06 22:24:0711#include "base/compiler_specific.h"
Michael Ershov0d7512032022-04-21 00:06:0012#include "base/files/file_path.h"
Yuta Hijikatabf953202020-11-12 08:43:5513#include "build/chromeos_buildflags.h"
Fabian Sommer5abef072022-08-27 00:07:3014#include "components/nacl/common/buildflags.h"
[email protected]d613a9902011-08-05 20:59:1115#include "crypto/crypto_export.h"
[email protected]bc1e07c72008-09-16 14:32:4416
[email protected]4b559b4d2011-04-14 17:37:1417namespace base {
[email protected]4b559b4d2011-04-14 17:37:1418class Time;
19} // namespace base
20
[email protected]41c78fa2010-03-22 20:08:4121// 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.
[email protected]4b559b4d2011-04-14 17:37:1424namespace crypto {
[email protected]1b1a264a2010-01-14 22:36:3525
[email protected]730fb132009-09-02 22:50:2526// Initialize NRPR if it isn't already initialized. This function is
[email protected]4b559b4d2011-04-14 17:37:1427// thread-safe, and NSPR will only ever be initialized once.
[email protected]d613a9902011-08-05 20:59:1128CRYPTO_EXPORT void EnsureNSPRInit();
[email protected]730fb132009-09-02 22:50:2529
[email protected]bc1e07c72008-09-16 14:32:4430// Initialize NSS if it isn't already initialized. This must be called before
31// any other NSS functions. This function is thread-safe, and NSS will only
[email protected]4b559b4d2011-04-14 17:37:1432// ever be initialized once.
[email protected]d613a9902011-08-05 20:59:1133CRYPTO_EXPORT void EnsureNSSInit();
[email protected]bc1e07c72008-09-16 14:32:4434
[email protected]f61c3972010-12-23 09:54:1535// Check if the current NSS version is greater than or equals to |version|.
36// A sample version string is "3.12.3".
37bool CheckNSSVersion(const char* version);
38
Fabian Sommer5abef072022-08-27 00:07:3039#if BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_MINIMAL_TOOLCHAIN)
[email protected]74beead2011-04-12 20:40:1240
Michael Ershov6b5e4182021-09-28 13:46:3241// Returns true once the TPM is owned and PKCS#11 initialized with the
42// user and security officer PINs, and Chaps has been successfully loaded into
43// NSS. Returns false if the TPM will never be loaded.
44CRYPTO_EXPORT void IsTPMTokenEnabled(base::OnceCallback<void(bool)> callback);
[email protected]c175cdb2011-06-28 20:41:5545
[email protected]496318862014-07-13 07:19:0046// Initialize the TPM token and system slot. The |callback| will run on the same
47// thread with true if the token and slot were successfully loaded or were
Michael Ershov6b5e4182021-09-28 13:46:3248// already initialized. |callback| will be passed false if loading failed.
49// Should be called only once.
[email protected]496318862014-07-13 07:19:0050CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot(
51 int system_slot_id,
tzik88b34c82018-03-09 05:01:1352 base::OnceCallback<void(bool)> callback);
Michael Ershov6b5e4182021-09-28 13:46:3253
54// Notifies clients that the TPM has finished initialization (i.e. notify
55// the callbacks of `IsTPMTokenEnabled()` or `GetSystemNSSKeySlot()`).
56// If `InitializeTPMTokenAndSystemSlot()` has been called before this method,
57// this signals that the TPM is enabled, and should use the slot configured by
58// those methods. If neither of those methods have been called, this signals
59// that no TPM system slot will be available.
60CRYPTO_EXPORT void FinishInitializingTPMTokenAndSystemSlot();
Michael Ershov0d7512032022-04-21 00:06:0061
62// TODO(crbug.com/1163303) Remove when the bug is fixed.
63// Can be used to collect additional information when public slot fails to open.
64// Mainly checks the access permissions on the files and tries to read them.
65// Crashes Chrome because it will crash anyway when it tries to instantiate
66// NSSCertDatabase with a nullptr public slot, crashing early can provide better
67// logs/stacktraces for diagnosing.
68// Takes `nss_path` where NSS is supposed to be (or created). Will attempt
69// creating the path if it doesn't exist (to check that it can be done).
70// Theoretically the path should already exist because it's created when Chrome
71// tries to open the public slot.
72CRYPTO_EXPORT void DiagnosePublicSlotAndCrash(const base::FilePath& nss_path);
73
Fabian Sommer5abef072022-08-27 00:07:3074#endif // BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_MINIMAL_TOOLCHAIN)
[email protected]dcce6cf2010-04-29 17:50:0675
[email protected]1b1a264a2010-01-14 22:36:3576// Convert a NSS PRTime value into a base::Time object.
avidd373b8b2015-12-21 21:34:4377// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
78CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime);
[email protected]1b1a264a2010-01-14 22:36:3579
[email protected]ca929ed32011-12-15 20:37:2880// Convert a base::Time object into a PRTime value.
avidd373b8b2015-12-21 21:34:4381// We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
82CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time);
[email protected]ca929ed32011-12-15 20:37:2883
[email protected]4b559b4d2011-04-14 17:37:1484} // namespace crypto
[email protected]bc1e07c72008-09-16 14:32:4485
[email protected]4b559b4d2011-04-14 17:37:1486#endif // CRYPTO_NSS_UTIL_H_