[go: nahoru, domu]

blob: eb95afe4c7e61eb334d52a40486df09f00b2e678 [file] [log] [blame]
ajwong@chromium.orgcf211882012-07-11 07:19:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
cmasone@google.comdcce6cf2010-04-29 17:50:062// 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_INTERNAL_H_
6#define CRYPTO_NSS_UTIL_INTERNAL_H_
cmasone@google.comdcce6cf2010-04-29 17:50:067
8#include <secmodt.h>
9
davidben6004dc52017-02-03 04:15:2910#include <string>
11
mattm@google.com557737f72013-12-06 22:24:0712#include "base/callback.h"
13#include "base/compiler_specific.h"
avidd373b82015-12-21 21:34:4314#include "base/macros.h"
darin@chromium.orgd613a9902011-08-05 20:59:1115#include "crypto/crypto_export.h"
mattm@google.com557737f72013-12-06 22:24:0716#include "crypto/scoped_nss_types.h"
17
18namespace base {
19class FilePath;
20}
rvargas@google.come4c9dda2011-06-20 23:24:0921
cmasone@google.comdcce6cf2010-04-29 17:50:0622// These functions return a type defined in an NSS header, and so cannot be
23// declared in nss_util.h. Hence, they are declared here.
24
rvargas@google.com4b559b4d2011-04-14 17:37:1425namespace crypto {
cmasone@google.comdcce6cf2010-04-29 17:50:0626
pneubeck@chromium.org190933f2014-07-28 09:56:5127// Opens an NSS software database in folder |path|, with the (potentially)
28// user-visible description |description|. Returns the slot for the opened
rsleeviffe5a132016-06-28 01:51:5229// database, or nullptr if the database could not be opened.
xhwang3e9ca562015-11-06 18:50:3630CRYPTO_EXPORT ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path,
31 const std::string& description);
pneubeck@chromium.org190933f2014-07-28 09:56:5132
gspencer@google.comdd24ffc2011-06-08 19:46:4233// A helper class that acquires the SECMOD list read lock while the
34// AutoSECMODListReadLock is in scope.
mattm@chromium.org7037a43c2014-01-14 14:00:4635class CRYPTO_EXPORT AutoSECMODListReadLock {
gspencer@google.comdd24ffc2011-06-08 19:46:4236 public:
37 AutoSECMODListReadLock();
38 ~AutoSECMODListReadLock();
39
40 private:
41 SECMODListLock* lock_;
42 DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock);
43};
44
mattm@google.com557737f72013-12-06 22:24:0745#if defined(OS_CHROMEOS)
pneubeck@chromium.org8edd7212014-07-30 12:24:2946// Returns a reference to the system-wide TPM slot if it is loaded. If it is not
47// loaded and |callback| is non-null, the |callback| will be run once the slot
48// is loaded.
49CRYPTO_EXPORT ScopedPK11Slot GetSystemNSSKeySlot(
tzik88b34c82018-03-09 05:01:1350 base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT;
mattm@chromium.org496318862014-07-13 07:19:0051
pneubeck@chromium.org442233d42014-08-02 07:37:2452// Sets the test system slot to |slot|, which means that |slot| will be exposed
53// through |GetSystemNSSKeySlot| and |IsTPMTokenReady| will return true.
54// |InitializeTPMTokenAndSystemSlot|, which triggers the TPM initialization,
55// does not have to be called if the test system slot is set.
rsleeviffe5a132016-06-28 01:51:5256// This must must not be called consecutively with a |slot| != nullptr. If
57// |slot| is nullptr, the test system slot is unset.
xhwang3e9ca562015-11-06 18:50:3658CRYPTO_EXPORT void SetSystemKeySlotForTesting(ScopedPK11Slot slot);
pneubeck@chromium.org190933f2014-07-28 09:56:5159
Maksim Ivanovd4e889a2019-10-11 20:13:3060// Injects the given |slot| as a system slot set by the future
61// |InitializeTPMTokenAndSystemSlot| call.
62// This must must not be called consecutively with a |slot| != nullptr. If
63// |slot| is nullptr and the system slot is already initialized to the
64// previously passed test value, the system slot is unset.
65CRYPTO_EXPORT void SetSystemKeySlotWithoutInitializingTPMForTesting(
66 ScopedPK11Slot slot);
67
mattm@google.com557737f72013-12-06 22:24:0768// Prepare per-user NSS slot mapping. It is safe to call this function multiple
69// times. Returns true if the user was added, or false if it already existed.
70CRYPTO_EXPORT bool InitializeNSSForChromeOSUser(
mattm@google.com557737f72013-12-06 22:24:0771 const std::string& username_hash,
tbarzic@chromium.org4071e6ac2014-07-12 12:46:1772 const base::FilePath& path);
73
74// Returns whether TPM for ChromeOS user still needs initialization. If
75// true is returned, the caller can proceed to initialize TPM slot for the
76// user, but should call |WillInitializeTPMForChromeOSUser| first.
77// |InitializeNSSForChromeOSUser| must have been called first.
78CRYPTO_EXPORT bool ShouldInitializeTPMForChromeOSUser(
79 const std::string& username_hash) WARN_UNUSED_RESULT;
80
81// Makes |ShouldInitializeTPMForChromeOSUser| start returning false.
82// Should be called before starting TPM initialization for the user.
83// Assumes |InitializeNSSForChromeOSUser| had already been called.
84CRYPTO_EXPORT void WillInitializeTPMForChromeOSUser(
85 const std::string& username_hash);
mattm@google.com557737f72013-12-06 22:24:0786
87// Use TPM slot |slot_id| for user. InitializeNSSForChromeOSUser must have been
88// called first.
89CRYPTO_EXPORT void InitializeTPMForChromeOSUser(
90 const std::string& username_hash,
91 CK_SLOT_ID slot_id);
92
93// Use the software slot as the private slot for user.
94// InitializeNSSForChromeOSUser must have been called first.
95CRYPTO_EXPORT void InitializePrivateSoftwareSlotForChromeOSUser(
96 const std::string& username_hash);
97
98// Returns a reference to the public slot for user.
99CRYPTO_EXPORT ScopedPK11Slot GetPublicSlotForChromeOSUser(
100 const std::string& username_hash) WARN_UNUSED_RESULT;
101
102// Returns the private slot for |username_hash| if it is loaded. If it is not
103// loaded and |callback| is non-null, the |callback| will be run once the slot
104// is loaded.
105CRYPTO_EXPORT ScopedPK11Slot GetPrivateSlotForChromeOSUser(
106 const std::string& username_hash,
tzik88b34c82018-03-09 05:01:13107 base::OnceCallback<void(ScopedPK11Slot)> callback) WARN_UNUSED_RESULT;
pneubeck@chromium.org190933f2014-07-28 09:56:51108
109// Closes the NSS DB for |username_hash| that was previously opened by the
110// *Initialize*ForChromeOSUser functions.
xhwang3e9ca562015-11-06 18:50:36111CRYPTO_EXPORT void CloseChromeOSUserForTesting(
pneubeck@chromium.org190933f2014-07-28 09:56:51112 const std::string& username_hash);
Pavol Marko725eaa42018-05-10 21:00:15113
114// Sets the slot which should be used as private slot for the next
115// |InitializePrivateSoftwareSlotForChromeOSUser| called. This is intended for
116// simulating a separate private slot in Chrome OS browser tests.
117// As a sanity check, it is recommended to check that the private slot of the
118// profile's certificate database is set to |slot| when the profile is
119// available, because |slot| will be used as private slot for whichever profile
120// is initialized next.
121CRYPTO_EXPORT void SetPrivateSoftwareSlotForChromeOSUserForTesting(
122 ScopedPK11Slot slot);
123
mattm@google.com557737f72013-12-06 22:24:07124#endif // defined(OS_CHROMEOS)
125
David Benjamin2f2cb362019-10-15 22:51:05126// Loads the given module for this NSS session.
127SECMODModule* LoadNSSModule(const char* name,
128 const char* library_path,
129 const char* params);
130
131// Returns the current NSS error message.
132std::string GetNSSErrorMessage();
133
rvargas@google.com4b559b4d2011-04-14 17:37:14134} // namespace crypto
cmasone@google.comdcce6cf2010-04-29 17:50:06135
rvargas@google.com4b559b4d2011-04-14 17:37:14136#endif // CRYPTO_NSS_UTIL_INTERNAL_H_