-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtpm_state_impl.cc
323 lines (277 loc) · 9.47 KB
/
tpm_state_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "trunks/tpm_state_impl.h"
#include <base/bind.h>
#include <base/logging.h>
#include "trunks/error_codes.h"
#include "trunks/tpm_generated.h"
#include "trunks/trunks_factory.h"
namespace {
// From definition of TPMA_PERMANENT.
const trunks::TPMA_PERMANENT kOwnerAuthSetMask = 1U;
const trunks::TPMA_PERMANENT kEndorsementAuthSetMask = 1U << 1;
const trunks::TPMA_PERMANENT kLockoutAuthSetMask = 1U << 2;
const trunks::TPMA_PERMANENT kInLockoutMask = 1U << 9;
// From definition of TPMA_STARTUP_CLEAR.
const trunks::TPMA_STARTUP_CLEAR kPlatformHierarchyMask = 1U;
const trunks::TPMA_STARTUP_CLEAR kStorageHierarchyMask = 1U << 1;
const trunks::TPMA_STARTUP_CLEAR kEndorsementHierarchyMask = 1U << 2;
const trunks::TPMA_STARTUP_CLEAR kOrderlyShutdownMask = 1U << 31;
} // namespace
namespace trunks {
TpmStateImpl::TpmStateImpl(const TrunksFactory& factory) : factory_(factory) {}
TPM_RC TpmStateImpl::Initialize() {
TPM_RC result = CacheTpmProperties();
if (result != TPM_RC_SUCCESS) {
LOG(ERROR) << "Failed to query TPM properties: " << GetErrorString(result);
return result;
}
if (tpm_properties_.count(TPM_PT_PERMANENT) == 0 ||
tpm_properties_.count(TPM_PT_STARTUP_CLEAR) == 0) {
LOG(ERROR) << "Required properties missing!";
return TRUNKS_RC_INVALID_TPM_CONFIGURATION;
}
result = CacheAlgorithmProperties();
if (result != TPM_RC_SUCCESS) {
LOG(ERROR) << "Failed to query TPM algorithms: " << GetErrorString(result);
return result;
}
initialized_ = true;
return TPM_RC_SUCCESS;
}
bool TpmStateImpl::IsOwnerPasswordSet() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_PERMANENT] & kOwnerAuthSetMask) ==
kOwnerAuthSetMask);
}
bool TpmStateImpl::IsEndorsementPasswordSet() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_PERMANENT] & kEndorsementAuthSetMask) ==
kEndorsementAuthSetMask);
}
bool TpmStateImpl::IsLockoutPasswordSet() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_PERMANENT] & kLockoutAuthSetMask) ==
kLockoutAuthSetMask);
}
bool TpmStateImpl::IsOwned() {
return (IsOwnerPasswordSet() && IsEndorsementPasswordSet() &&
IsLockoutPasswordSet());
}
bool TpmStateImpl::IsInLockout() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_PERMANENT] & kInLockoutMask) ==
kInLockoutMask);
}
bool TpmStateImpl::IsPlatformHierarchyEnabled() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_STARTUP_CLEAR] & kPlatformHierarchyMask) ==
kPlatformHierarchyMask);
}
bool TpmStateImpl::IsStorageHierarchyEnabled() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_STARTUP_CLEAR] & kStorageHierarchyMask) ==
kStorageHierarchyMask);
}
bool TpmStateImpl::IsEndorsementHierarchyEnabled() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_STARTUP_CLEAR] & kEndorsementHierarchyMask) ==
kEndorsementHierarchyMask);
}
bool TpmStateImpl::IsEnabled() {
return (!IsPlatformHierarchyEnabled() && IsStorageHierarchyEnabled() &&
IsEndorsementHierarchyEnabled());
}
bool TpmStateImpl::WasShutdownOrderly() {
CHECK(initialized_);
return ((tpm_properties_[TPM_PT_STARTUP_CLEAR] & kOrderlyShutdownMask) ==
kOrderlyShutdownMask);
}
bool TpmStateImpl::IsRSASupported() {
CHECK(initialized_);
return (algorithm_properties_.count(TPM_ALG_RSA) > 0);
}
bool TpmStateImpl::IsECCSupported() {
CHECK(initialized_);
return (algorithm_properties_.count(TPM_ALG_ECC) > 0);
}
uint32_t TpmStateImpl::GetLockoutCounter() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_LOCKOUT_COUNTER];
}
uint32_t TpmStateImpl::GetLockoutThreshold() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_MAX_AUTH_FAIL];
}
uint32_t TpmStateImpl::GetLockoutInterval() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_LOCKOUT_INTERVAL];
}
uint32_t TpmStateImpl::GetLockoutRecovery() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_LOCKOUT_RECOVERY];
}
uint32_t TpmStateImpl::GetMaxNVSize() {
CHECK(initialized_);
uint32_t max_nv_size;
if (!GetTpmProperty(TPM_PT_NV_INDEX_MAX, &max_nv_size)) {
max_nv_size = 2048;
}
uint32_t max_nv_buffer;
if (GetTpmProperty(TPM_PT_NV_BUFFER_MAX, &max_nv_buffer) &&
max_nv_buffer < max_nv_size) {
max_nv_size = max_nv_buffer;
}
return max_nv_size;
}
bool TpmStateImpl::GetTpmProperty(TPM_PT property, uint32_t* value) {
CHECK(initialized_);
if (tpm_properties_.count(property) == 0) {
return false;
}
if (value) {
*value = tpm_properties_[property];
}
return true;
}
uint32_t TpmStateImpl::GetTpmFamily() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_FAMILY_INDICATOR];
}
uint32_t TpmStateImpl::GetSpecificationLevel() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_LEVEL];
}
uint32_t TpmStateImpl::GetSpecificationRevision() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_REVISION];
}
uint32_t TpmStateImpl::GetManufacturer() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_MANUFACTURER];
}
uint32_t TpmStateImpl::GetTpmModel() {
CHECK(initialized_);
return tpm_properties_[TPM_PT_VENDOR_TPM_TYPE];
}
uint64_t TpmStateImpl::GetFirmwareVersion() {
CHECK(initialized_);
uint64_t version_high = tpm_properties_[TPM_PT_FIRMWARE_VERSION_1];
uint64_t version_low = tpm_properties_[TPM_PT_FIRMWARE_VERSION_2];
return (version_high << 32) | version_low;
}
std::string TpmStateImpl::GetVendorIDString() {
CHECK(initialized_);
std::string result;
for (TPM_PT property : {TPM_PT_VENDOR_STRING_1, TPM_PT_VENDOR_STRING_2,
TPM_PT_VENDOR_STRING_3, TPM_PT_VENDOR_STRING_4}) {
if (tpm_properties_.count(property) == 0) {
break;
}
uint32_t value = tpm_properties_[property];
for (int i = 3; i >= 0; --i) {
char byte = static_cast<char>((value >> (i * 8)) & 0xff);
if (!byte) {
break;
}
result.push_back(byte);
}
}
return result;
}
bool TpmStateImpl::GetAlgorithmProperties(TPM_ALG_ID algorithm,
TPMA_ALGORITHM* properties) {
CHECK(initialized_);
if (algorithm_properties_.count(algorithm) == 0) {
return false;
}
if (properties) {
*properties = algorithm_properties_[algorithm];
}
return true;
}
TPM_RC TpmStateImpl::GetCapability(const CapabilityCallback& callback,
TPM_CAP capability,
uint32_t property,
uint32_t max_properties_per_call) {
TPMI_YES_NO more_data = YES;
while (more_data) {
TPMS_CAPABILITY_DATA capability_data;
TPM_RC result = factory_.GetTpm()->GetCapabilitySync(
capability, property, max_properties_per_call, &more_data,
&capability_data, nullptr);
if (result != TPM_RC_SUCCESS) {
LOG(ERROR) << __func__ << ": " << GetErrorString(result);
return result;
}
if (capability_data.capability != capability) {
LOG(ERROR) << __func__ << ": Unexpected capability data.";
return SAPI_RC_MALFORMED_RESPONSE;
}
uint32_t next_property = callback.Run(capability_data.data);
if (more_data) {
if (next_property == 0) {
LOG(ERROR) << __func__ << ": No properties in response.";
return SAPI_RC_MALFORMED_RESPONSE;
}
if (next_property <= property) {
LOG(ERROR) << __func__ << ": Lower properties in response.";
return SAPI_RC_MALFORMED_RESPONSE;
}
property = next_property;
}
}
return TPM_RC_SUCCESS;
}
uint32_t TpmStateImpl::TpmPropertiesCallback(
const TPMU_CAPABILITIES& capability_data) {
uint32_t next_property = 0;
for (uint32_t i = 0;
i < capability_data.tpm_properties.count && i < MAX_TPM_PROPERTIES;
++i) {
const TPMS_TAGGED_PROPERTY& property =
capability_data.tpm_properties.tpm_property[i];
VLOG(1) << "TPM Property 0x" << std::hex << property.property << " = 0x"
<< property.value;
tpm_properties_[property.property] = property.value;
next_property = property.property + 1;
}
return next_property;
}
TPM_RC TpmStateImpl::CacheTpmProperties() {
CapabilityCallback callback =
base::Bind(&TpmStateImpl::TpmPropertiesCallback, base::Unretained(this));
if (tpm_properties_.empty()) {
TPM_RC result = GetCapability(callback, TPM_CAP_TPM_PROPERTIES, PT_FIXED,
MAX_TPM_PROPERTIES);
if (result != TPM_RC_SUCCESS) {
return result;
}
}
return GetCapability(callback, TPM_CAP_TPM_PROPERTIES, PT_VAR,
MAX_TPM_PROPERTIES);
}
uint32_t TpmStateImpl::AlgorithmCallback(
const TPMU_CAPABILITIES& capability_data) {
uint32_t next_property = 0;
for (uint32_t i = 0; i < capability_data.algorithms.count && i < MAX_CAP_ALGS;
++i) {
const TPMS_ALG_PROPERTY& property =
capability_data.algorithms.alg_properties[i];
VLOG(1) << "Algorithm Properties 0x" << std::hex << property.alg << " = 0x"
<< property.alg_properties;
algorithm_properties_[property.alg] = property.alg_properties;
next_property = property.alg + 1;
}
return next_property;
}
TPM_RC TpmStateImpl::CacheAlgorithmProperties() {
if (algorithm_properties_.empty()) {
return GetCapability(
base::Bind(&TpmStateImpl::AlgorithmCallback, base::Unretained(this)),
TPM_CAP_ALGS, TPM_ALG_FIRST, MAX_CAP_ALGS);
}
return TPM_RC_SUCCESS;
}
} // namespace trunks