blob: 85ed363716d83649746be6d441f555613a0acce1 [file] [log] [blame]
Elly Joneseca6ef12012-03-07 14:37:08 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Will Drewrydcd09942011-04-15 09:31:33 -05002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// InstallAttributes - class for managing install-time system attributes.
6
7#ifndef CRYPTOHOME_INSTALL_ATTRIBUTES_H_
8#define CRYPTOHOME_INSTALL_ATTRIBUTES_H_
9
Gwendal Grignouc7acaa92016-06-24 15:34:06 -070010#include <memory>
Alex Vakulenkob4b694a2014-07-18 17:21:35 -070011#include <string>
12
Gwendal Grignoudcdc1a42016-06-30 09:41:01 -070013#include <base/files/file_path.h>
Ben Chan4ac108c2014-09-02 23:26:04 -070014#include <base/macros.h>
Darren Krahn1f9b3e42013-12-18 17:33:54 -080015#include <base/observer_list.h>
Qijiang Fan41a483a2020-05-07 13:12:37 +090016#include <base/observer_list_types.h>
Elly Joneseca6ef12012-03-07 14:37:08 -050017#include <base/values.h>
Alex Vakulenkoe7696532015-10-16 16:27:29 -070018#include <brillo/secure_blob.h>
Will Drewrydcd09942011-04-15 09:31:33 -050019
Alex Vakulenkob4b694a2014-07-18 17:21:35 -070020#include "cryptohome/crypto.h"
Greg Kerrf9b0fc12020-08-13 22:29:10 +000021#include "cryptohome/install_attributes.pb.h"
Alex Vakulenkob4b694a2014-07-18 17:21:35 -070022#include "cryptohome/lockbox.h"
23#include "cryptohome/platform.h"
24#include "cryptohome/tpm.h"
Alex Vakulenkob4b694a2014-07-18 17:21:35 -070025
Will Drewrydcd09942011-04-15 09:31:33 -050026namespace cryptohome {
27
28// InstallAttributes - manages secure, install-time attributes
29//
30// Provides setting and getting of tamper-evident install-time
31// attributes. Upon finalization, the underlying tamper-evident
32// store will "lock" the attributes such that they become read-only
33// until the next install.
34//
35// InstallAttributes is not thread-safe and should not be accessed in parallel.
36class InstallAttributes {
37 public:
Mattias Nisslerbb0191f2018-12-11 16:07:05 +010038 enum class Status {
39 kUnknown, // Not initialized yet.
40 kTpmNotOwned, // TPM not owned yet.
41 kFirstInstall, // Allows writing.
42 kValid, // Validated successfully.
43 kInvalid, // Not valid, e.g. clobbered, absent.
John L Chencbf32cf2019-05-29 18:34:42 +080044 COUNT, // This is unused, just for counting the number of elements.
45 // Note that COUNT should always be the last element.
Mattias Nisslerbb0191f2018-12-11 16:07:05 +010046 };
47
Qijiang Fan41a483a2020-05-07 13:12:37 +090048 class Observer : public base::CheckedObserver {
Darren Krahn1f9b3e42013-12-18 17:33:54 -080049 public:
50 virtual void OnFinalized() = 0;
51 };
52
Will Drewrydcd09942011-04-15 09:31:33 -050053 // Creates an instance of install attributes that will use the |tpm|. If |tpm|
54 // is NULL, InstallAttributes will proceed insecurely (unless it is set with
Nam T. Nguyen0e6fbb62014-03-18 09:51:43 -070055 // SetTpm at a later time).
Elly Joneseca6ef12012-03-07 14:37:08 -050056 explicit InstallAttributes(Tpm* tpm);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090057 InstallAttributes(const InstallAttributes&) = delete;
58 InstallAttributes& operator=(const InstallAttributes&) = delete;
59
Will Drewrydcd09942011-04-15 09:31:33 -050060 virtual ~InstallAttributes();
61
John L Chencbf32cf2019-05-29 18:34:42 +080062 virtual Status status() const { return status_; }
Mattias Nisslerbb0191f2018-12-11 16:07:05 +010063
64 // Sets status (for testing).
65 void set_status_for_testing(Status status) { status_ = status; }
66
Will Drewrydcd09942011-04-15 09:31:33 -050067 // Updates the TPM used by Lockbox or disables the use of the TPM.
68 // This does NOT take ownership of the pointer.
69 virtual void SetTpm(Tpm* tpm);
70
71 // Prepares the class for use including instantiating a new environment
Ching-Kang Yendb834d22021-03-08 01:18:40 +080072 // if needed. If initialization completes, |tpm| will be used to remove
Nam T. Nguyen0e6fbb62014-03-18 09:51:43 -070073 // this instance's dependency on the TPM ownership.
Ching-Kang Yendb834d22021-03-08 01:18:40 +080074 virtual bool Init(Tpm* tpm);
Will Drewrydcd09942011-04-15 09:31:33 -050075
Will Drewrydcd09942011-04-15 09:31:33 -050076 // Populates |value| based on the content referenced by |name|.
77 //
78 // Parameters
79 // - name: addressable name of the entry to retrieve
80 // - value: pointer to a Blob to populate with the value, if found.
81 // Returns true if |name| exists in the store and |value| will be populated.
82 // Returns false if the |name| does not exist.
Alex Vakulenkoe7696532015-10-16 16:27:29 -070083 virtual bool Get(const std::string& name, brillo::Blob* value) const;
Will Drewrydcd09942011-04-15 09:31:33 -050084
85 // Populates |name| and |value| based on the content referenced by |index|.
86 //
87 // Parameters
88 // - index: 0-addressable index of the desired entry.
89 // - name: addressable name of the entry to retrieve
90 // - value: pointer to a Blob to populate with the value, if found.
91 // Returns true if |index| exists in the store.
92 // Returns false if the |index| does not exist.
93 virtual bool GetByIndex(int index,
94 std::string* name,
Alex Vakulenkoe7696532015-10-16 16:27:29 -070095 brillo::Blob* value) const;
Will Drewrydcd09942011-04-15 09:31:33 -050096
97 // Appends |name| and |value| as an attribute pair to the internal store.
98 //
99 // Parameters
100 // - name: attribute name to associate |value| with in the store
101 // - value: Blob of data to store with |name|.
102 // Returns true if the association can be stored, and false if it can't.
103 // If the given |name| already exists, it will be replaced.
Alex Vakulenkoe7696532015-10-16 16:27:29 -0700104 virtual bool Set(const std::string& name, const brillo::Blob& value);
Will Drewrydcd09942011-04-15 09:31:33 -0500105
106 // Finalizes the install-time attributes making them tamper-evident.
107 virtual bool Finalize();
108
109 // Returns the number of entries in the Lockbox.
110 virtual int Count() const;
111
Will Drewrydcd09942011-04-15 09:31:33 -0500112 // Return InstallAttributes version.
113 // This is populated from the default value in install_attributes.proto and
114 // should be incremented there when behavior vesioning is needed.
115 virtual uint64_t version() const { return version_; }
116
117 // Allows overriding the version, often for testing.
118 virtual void set_version(uint64_t version) { version_ = version; }
119
Will Drewrydcd09942011-04-15 09:31:33 -0500120 // Returns true if the attribute storage is securely stored. It does not
121 // indicate if the store has been finalized, just if the system TPM/Lockbox
122 // is being used.
123 virtual bool is_secure() const { return is_secure_; }
124
125 virtual void set_is_secure(bool is_secure) { is_secure_ = is_secure; }
126
Will Drewrydcd09942011-04-15 09:31:33 -0500127 // Allows replacement of the underlying lockbox.
128 // This does NOT take ownership of the pointer.
129 virtual void set_lockbox(Lockbox* lockbox) { lockbox_ = lockbox; }
130
131 virtual Lockbox* lockbox() { return lockbox_; }
132
133 // Replaces the platform implementation.
134 // Does NOT take ownership of the pointer.
135 virtual void set_platform(Platform* platform) { platform_ = platform; }
136
137 virtual Platform* platform() { return platform_; }
138
Elly Joneseca6ef12012-03-07 14:37:08 -0500139 // Returns a description of the system's install attributes as a Value.
140 //
hscham6d748eb2020-10-09 14:46:06 +0900141 // The Value is of type Dictionary, with keys "initialized", "version",
Elly Joneseca6ef12012-03-07 14:37:08 -0500142 // "lockbox_index", "secure", "invalid", "first_install" and "size".
hscham6d748eb2020-10-09 14:46:06 +0900143 virtual base::Value GetStatus();
Elly Joneseca6ef12012-03-07 14:37:08 -0500144
Tom Hughes6711cdc2020-09-14 08:34:01 -0700145 void AddObserver(Observer* obs) { observer_list_.AddObserver(obs); }
Darren Krahn1f9b3e42013-12-18 17:33:54 -0800146
Tom Hughes6711cdc2020-09-14 08:34:01 -0700147 void RemoveObserver(Observer* obs) { observer_list_.RemoveObserver(obs); }
Darren Krahn1f9b3e42013-12-18 17:33:54 -0800148
149 void NotifyFinalized() {
Eric Caruso388ba9a2018-01-12 14:53:36 -0800150 for (Observer& observer : observer_list_)
151 observer.OnFinalized();
Darren Krahn1f9b3e42013-12-18 17:33:54 -0800152 }
153
Will Drewrydcd09942011-04-15 09:31:33 -0500154 // Provides the TPM NVRAM index to be used by the underlying Lockbox instance.
155 static const uint32_t kLockboxIndex;
156 // Provides the default location for the attributes data file.
Gwendal Grignoudcdc1a42016-06-30 09:41:01 -0700157 static const char kDefaultDataFile[];
Thiemo Nagelea7dbf32014-10-16 20:12:54 +0200158 // File permissions of attributes data file (modulo umask).
159 static const mode_t kDataFilePermissions;
Mattias Nissler0fae3692013-01-23 16:20:47 +0100160 // Provides the default location for the cache file.
Gwendal Grignoudcdc1a42016-06-30 09:41:01 -0700161 static const char kDefaultCacheFile[];
Thiemo Nagelea7dbf32014-10-16 20:12:54 +0200162 // File permissions of cache file (modulo umask).
163 static const mode_t kCacheFilePermissions;
Elly Joneseca6ef12012-03-07 14:37:08 -0500164
Will Drewrydcd09942011-04-15 09:31:33 -0500165 protected:
166 // Helper to find a given entry index using its name.
167 virtual int FindIndexByName(const std::string& name) const;
168 // Convert the current attributes to a byte stream and write it
169 // to |out_bytes|.
Alex Vakulenkoe7696532015-10-16 16:27:29 -0700170 virtual bool SerializeAttributes(brillo::Blob* out_bytes);
Mattias Nisslereb2d2072018-12-05 16:08:35 +0100171 // Remove the data file on disk if it exists.
172 bool ClearData();
Will Drewrydcd09942011-04-15 09:31:33 -0500173
174 private:
Mattias Nisslerbb0191f2018-12-11 16:07:05 +0100175 Status status_ = Status::kUnknown;
Tom Hughes6711cdc2020-09-14 08:34:01 -0700176 bool is_secure_ = false; // Indicates if there is hardware protection (TPM).
177 base::FilePath data_file_; // Location data is persisted to.
178 base::FilePath cache_file_; // World-readable data cache file.
179 uint64_t version_ = 0; // Default implementation version.
Will Drewrydcd09942011-04-15 09:31:33 -0500180 // Default implementations of dependencies
Gwendal Grignouc7acaa92016-06-24 15:34:06 -0700181 std::unique_ptr<SerializedInstallAttributes> default_attributes_;
182 std::unique_ptr<Lockbox> default_lockbox_;
183 std::unique_ptr<Platform> default_platform_;
Will Drewrydcd09942011-04-15 09:31:33 -0500184 // Overridable dependency pointer which allow for easy injection.
Mattias Nisslereb2d2072018-12-05 16:08:35 +0100185 SerializedInstallAttributes* attributes_ = nullptr;
186 Lockbox* lockbox_ = nullptr;
187 Platform* platform_ = nullptr;
Alex Vakulenko5e5accd2015-06-15 12:53:22 -0700188 base::ObserverList<Observer> observer_list_;
Will Drewrydcd09942011-04-15 09:31:33 -0500189};
190
191} // namespace cryptohome
192
193#endif // CRYPTOHOME_INSTALL_ATTRIBUTES_H_