-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathallowlisting_util_test.cc
346 lines (267 loc) · 10.9 KB
/
allowlisting_util_test.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2019 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 "u2fd/allowlisting_util.h"
#include <functional>
#include <limits>
#include <memory>
#include <attestation/proto_bindings/interface.pb.h>
#include <base/optional.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <policy/device_policy.h>
#include <policy/libpolicy.h>
#include <policy/mock_device_policy.h>
#include <policy/mock_libpolicy.h>
#include "u2fd/util.h"
namespace u2f {
namespace {
using testing::_;
using testing::DoAll;
using testing::Return;
using testing::ReturnRef;
using testing::SetArgPointee;
using testing::StrictMock;
// Certificate, before attestation data is appended.
constexpr uint8_t kCertificateHeader[2] = {0x30, 0x82};
constexpr uint8_t kCertificateLength[2] = {0x01, 0x2c}; // = 300
constexpr uint8_t kCertificateBody[300] = {[0 ... 299] = 0xff};
// Data returned from attestationd.
constexpr char kTpmMetadata[109] = {[0 ... 108] = 0x1a};
constexpr char kTpmSignature[256] = {[0 ... 255] = 0x1e};
// Data loaded from policy.
constexpr char kDeviceId[36 + 1 /* null terminator */] =
"aaaaaaaa-1111-2222-3333-bbbbbbbbbbbb";
// Certificate, after attestation data is appended. Elements not listed here are
// unchanged.
constexpr uint8_t kFinalCertificateMetadataHeader[2] = {
0x04 /* Octet String */, sizeof(kTpmMetadata) /* Metadata Length */};
constexpr uint8_t kFinalCertificateSignatureHeader[4] = {
0x04 /* Octet String */, 0x82 /* Long Form Length, 2 bytes */,
0x01, 0x00 /* Signature Length: 256 */};
constexpr uint8_t kFinalCertificateDeviceIdHeader[2] = {
0x13 /* Printable String */,
sizeof(kDeviceId) - 1 /* Device Id Length (excl. null terminator) */};
// New length of the certificate body is the original length, plus all appended
// data and associated headers.
//
// Original Length: 300 +
// Metadata Header: 2 +
// Metadata 109 +
// Signature Header: 4 +
// Signature: 256 +
// Device Id Header: 2 +
// Device Id: 36
// = 709
constexpr uint8_t kFinalCertificateLength[2] = {0x02, 0xc5};
constexpr int kMaxAsn1FieldSize = std::numeric_limits<uint16_t>::max();
class AllowlistingUtilTest : public ::testing::Test {
public:
AllowlistingUtilTest() : util_(CreateMockAttestationdCallback()) {}
void SetUp() override {
mock_policy_provider_ = new StrictMock<policy::MockPolicyProvider>();
util_.SetPolicyProviderForTest(
std::unique_ptr<policy::PolicyProvider>(mock_policy_provider_));
attestationd_called_ = false;
expect_attestationd_call_ = false;
}
void TearDown() override {
EXPECT_EQ(expect_attestationd_call_, attestationd_called_);
}
protected:
// Build and return a standard G2F certificate.
std::vector<uint8_t> BuildCert() {
std::vector<uint8_t> cert;
util::AppendToVector(kCertificateHeader, &cert);
util::AppendToVector(kCertificateLength, &cert);
util::AppendToVector(kCertificateBody, &cert);
expected_attestationd_cert_size_ = cert.size();
return cert;
}
// Builds a success reply for the specified cert, which will be returned by
// any expected calls to attestationd.
void ReturnAttestationSuccessReply(const std::vector<uint8_t>& cert) {
attestationd_reply_ = attestation::GetCertifiedNvIndexReply();
attestationd_reply_->set_status(attestation::STATUS_SUCCESS);
attestationd_reply_->mutable_certified_data()->append(kTpmMetadata,
sizeof(kTpmMetadata));
attestationd_reply_->mutable_certified_data()->append(
reinterpret_cast<const char*>(cert.data()), cert.size());
attestationd_reply_->mutable_signature()->append(kTpmSignature,
sizeof(kTpmSignature));
}
// Expect a call to attestationd, and respond with a success message
// containing a certified copy of the G2F certificate stored in cert_ (call
// BuildCert() first).
void ExpectAttestationCall() { expect_attestationd_call_ = true; }
// Expect a call to get Device Id, and respond with a success response
// including the the specified |id|.
void ExpectGetDeviceId(std::string id) {
EXPECT_CALL(*mock_policy_provider_, Reload()).WillOnce(Return(true));
EXPECT_CALL(*mock_policy_provider_, GetDevicePolicy())
.WillOnce(ReturnRef(mock_device_policy_));
EXPECT_CALL(mock_device_policy_, GetDeviceDirectoryApiId(_))
.WillOnce(DoAll(SetArgPointee<0>(id), Return(true)));
}
// Get a copy of the certificate, as we expect it to be once the allowlisting
// data has been appended.
std::vector<uint8_t> GetExpectedCertWithAllowlistData() {
std::vector<uint8_t> cert;
// Header with updated length.
util::AppendToVector(kCertificateHeader, &cert);
util::AppendToVector(kFinalCertificateLength, &cert);
// Original Body.
util::AppendToVector(kCertificateBody, &cert);
// TPM Metadata.
util::AppendToVector(kFinalCertificateMetadataHeader, &cert);
util::AppendToVector(kTpmMetadata, &cert);
// Signature.
util::AppendToVector(kFinalCertificateSignatureHeader, &cert);
util::AppendToVector(kTpmSignature, &cert);
// Device Id.
util::AppendToVector(kFinalCertificateDeviceIdHeader, &cert);
util::AppendToVector(std::string(kDeviceId), &cert);
return cert;
}
// Attempt to append data to the specified cert, and check it fails.
void ExpectAppendDataFails(std::vector<uint8_t>* cert) {
std::vector<uint8_t> cert_original = *cert;
EXPECT_FALSE(util_.AppendDataToCert(cert));
// Check we didn't modify cert.
EXPECT_EQ(cert_original, *cert);
}
private:
std::function<base::Optional<attestation::GetCertifiedNvIndexReply>(int)>
CreateMockAttestationdCallback() {
return [this](int size) {
// Check we were expecting this.
EXPECT_TRUE(expect_attestationd_call_);
// We should only ever have at most one call.
EXPECT_FALSE(attestationd_called_);
attestationd_called_ = true;
EXPECT_EQ(expected_attestationd_cert_size_, size);
return attestationd_reply_;
};
}
// Whether we expect a call to attestationd, and if so, the size of the cert
// parameter passed.
bool expect_attestationd_call_;
int expected_attestationd_cert_size_;
// Actual behavior.
bool attestationd_called_;
protected:
AllowlistingUtil util_;
StrictMock<policy::MockPolicyProvider>* mock_policy_provider_; // Not Owned.
StrictMock<policy::MockDevicePolicy> mock_device_policy_;
// If called, what we should return from 'attestationd'
base::Optional<attestation::GetCertifiedNvIndexReply> attestationd_reply_;
};
TEST_F(AllowlistingUtilTest, AppendDataSuccess) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
ExpectGetDeviceId(kDeviceId);
// Sanity Check.
EXPECT_NE(GetExpectedCertWithAllowlistData(), cert);
EXPECT_TRUE(util_.AppendDataToCert(&cert));
// Check contents of resulting cert.
EXPECT_EQ(GetExpectedCertWithAllowlistData(), cert);
}
TEST_F(AllowlistingUtilTest, AppendDataNullCertificate) {
EXPECT_FALSE(util_.AppendDataToCert(nullptr));
}
TEST_F(AllowlistingUtilTest, AppendDataCertTooShort) {
std::vector<uint8_t> cert;
util::AppendToVector(kCertificateHeader, &cert);
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataCertUnexpectedFirstBytes) {
std::vector<uint8_t> cert = {0xff, 0xff, 0xff, 0xff};
EXPECT_FALSE(util_.AppendDataToCert(&cert));
ExpectAppendDataFails(&cert);
cert[0] = kCertificateHeader[0];
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataPolicyReloadFailure) {
std::vector<uint8_t> cert = BuildCert();
EXPECT_CALL(*mock_policy_provider_, Reload()).WillOnce(Return(false));
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataDeviceIdMissing) {
std::vector<uint8_t> cert = BuildCert();
EXPECT_CALL(*mock_policy_provider_, Reload()).WillOnce(Return(true));
EXPECT_CALL(*mock_policy_provider_, GetDevicePolicy())
.WillOnce(ReturnRef(mock_device_policy_));
EXPECT_CALL(mock_device_policy_, GetDeviceDirectoryApiId(_))
.WillOnce(Return(false));
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataAttestationdCallFails) {
std::vector<uint8_t> cert = BuildCert();
ExpectGetDeviceId(kDeviceId);
// Expect the call, we haven't set the reply so will return base::nullopt.
ExpectAttestationCall();
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataAttestationdReturnsError) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
// Override with failure status.
attestationd_reply_->set_status(attestation::STATUS_NOT_AVAILABLE);
ExpectGetDeviceId(kDeviceId);
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataCertifiedDataTooShort) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
// Resize certified data to be smaller than the original cert.
attestationd_reply_->mutable_certified_data()->resize(
sizeof(kCertificateBody));
ExpectGetDeviceId(kDeviceId);
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataCertPrefixTooLong) {
std::vector<uint8_t> cert = BuildCert();
ReturnAttestationSuccessReply(cert);
// Append enough data that the prefix will be too long to store as an ASN1
// field with two byte length.
attestationd_reply_->mutable_certified_data()->append(kMaxAsn1FieldSize, 'a');
ExpectGetDeviceId(kDeviceId);
ExpectAttestationCall();
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataSignatureTooLong) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
// Append enough data that the prefix will be too long to store as an ASN1
// field with two byte length.
attestationd_reply_->mutable_signature()->append(kMaxAsn1FieldSize, 'a');
ExpectGetDeviceId(kDeviceId);
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataDeviceIdTooLong) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
std::string device_id_too_long(kMaxAsn1FieldSize + 1, 'a');
ExpectGetDeviceId(device_id_too_long);
ExpectAppendDataFails(&cert);
}
TEST_F(AllowlistingUtilTest, AppendDataAppendedDataTooLong) {
std::vector<uint8_t> cert = BuildCert();
ExpectAttestationCall();
ReturnAttestationSuccessReply(cert);
// Append enough data that overall, the total appended data will cause the
// size of the modified cert to be too long to store as an ASN1 field with two
// byte length.
attestationd_reply_->mutable_signature()->append(
// The signature is 256 bytes so we'll still exceed the max size.
kMaxAsn1FieldSize - 100, 'a');
ExpectGetDeviceId(kDeviceId);
ExpectAppendDataFails(&cert);
}
} // namespace
} // namespace u2f