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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
// Copyright 2013 The Chromium 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 <string>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/attestation/attestation_signed_data.pb.h"
#include "chrome/browser/chromeos/attestation/fake_certificate.h"
#include "chrome/browser/chromeos/attestation/platform_verification_flow.h"
#include "chrome/browser/chromeos/login/users/mock_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/profiles/profile_impl.h"
#include "chrome/browser/renderer_host/pepper/device_id_fetcher.h"
#include "chrome/common/content_settings_pattern.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chromeos/attestation/mock_attestation_flow.h"
#include "chromeos/cryptohome/mock_async_method_caller.h"
#include "chromeos/dbus/fake_cryptohome_client.h"
#include "chromeos/settings/cros_settings_names.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::DoAll;
using testing::Invoke;
using testing::Return;
using testing::SetArgumentPointee;
using testing::StrictMock;
using testing::WithArgs;
namespace chromeos {
namespace attestation {
namespace {
const char kTestID[] = "test_id";
const char kTestChallenge[] = "test_challenge";
const char kTestSignedData[] = "test_challenge_with_salt";
const char kTestSignature[] = "test_signature";
const char kTestCertificate[] = "test_certificate";
const char kTestEmail[] = "test_email@chromium.org";
const char kTestURL[] = "http://mytestdomain/test";
const char kTestURLSecure[] = "https://mytestdomain/test";
const char kTestURLExtension[] = "chrome-extension://mytestextension";
class FakeDelegate : public PlatformVerificationFlow::Delegate {
public:
FakeDelegate() : response_(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW),
num_consent_calls_(0),
url_(kTestURL),
is_incognito_(false) {
// Configure a user for the mock user manager.
mock_user_manager_.SetActiveUser(kTestEmail);
}
virtual ~FakeDelegate() {}
void SetUp() {
ProfileImpl::RegisterProfilePrefs(pref_service_.registry());
chrome::DeviceIDFetcher::RegisterProfilePrefs(pref_service_.registry());
PlatformVerificationFlow::RegisterProfilePrefs(pref_service_.registry());
HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
content_settings_ = new HostContentSettingsMap(&pref_service_, false);
}
void TearDown() {
content_settings_->ShutdownOnUIThread();
}
virtual void ShowConsentPrompt(
content::WebContents* web_contents,
const PlatformVerificationFlow::Delegate::ConsentCallback& callback)
OVERRIDE {
num_consent_calls_++;
callback.Run(response_);
}
virtual PrefService* GetPrefs(content::WebContents* web_contents) OVERRIDE {
return &pref_service_;
}
virtual const GURL& GetURL(content::WebContents* web_contents) OVERRIDE {
return url_;
}
virtual User* GetUser(content::WebContents* web_contents) OVERRIDE {
return mock_user_manager_.GetActiveUser();
}
virtual HostContentSettingsMap* GetContentSettings(
content::WebContents* web_contents) OVERRIDE {
return content_settings_;
}
virtual bool IsGuestOrIncognito(content::WebContents* web_contents) OVERRIDE {
return is_incognito_;
}
void set_response(PlatformVerificationFlow::ConsentResponse response) {
response_ = response;
}
int num_consent_calls() {
return num_consent_calls_;
}
TestingPrefServiceSyncable& pref_service() {
return pref_service_;
}
void set_url(const GURL& url) {
url_ = url;
}
void set_is_incognito(bool is_incognito) {
is_incognito_ = is_incognito;
}
private:
PlatformVerificationFlow::ConsentResponse response_;
int num_consent_calls_;
TestingPrefServiceSyncable pref_service_;
MockUserManager mock_user_manager_;
GURL url_;
scoped_refptr<HostContentSettingsMap> content_settings_;
bool is_incognito_;
DISALLOW_COPY_AND_ASSIGN(FakeDelegate);
};
class CustomFakeCryptohomeClient : public FakeCryptohomeClient {
public:
CustomFakeCryptohomeClient() : call_status_(DBUS_METHOD_CALL_SUCCESS),
attestation_enrolled_(true),
attestation_prepared_(true) {}
virtual void TpmAttestationIsEnrolled(
const BoolDBusMethodCallback& callback) OVERRIDE {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback,
call_status_,
attestation_enrolled_));
}
virtual void TpmAttestationIsPrepared(
const BoolDBusMethodCallback& callback) OVERRIDE {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback,
call_status_,
attestation_prepared_));
}
void set_call_status(DBusMethodCallStatus call_status) {
call_status_ = call_status;
}
void set_attestation_enrolled(bool attestation_enrolled) {
attestation_enrolled_ = attestation_enrolled;
}
void set_attestation_prepared(bool attestation_prepared) {
attestation_prepared_ = attestation_prepared;
}
private:
DBusMethodCallStatus call_status_;
bool attestation_enrolled_;
bool attestation_prepared_;
};
} // namespace
class PlatformVerificationFlowTest : public ::testing::Test {
public:
PlatformVerificationFlowTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_),
certificate_success_(true),
fake_certificate_index_(0),
sign_challenge_success_(true),
result_(PlatformVerificationFlow::INTERNAL_ERROR) {}
void SetUp() {
fake_delegate_.SetUp();
// Create a verifier for tests to call.
verifier_ = new PlatformVerificationFlow(&mock_attestation_flow_,
&mock_async_caller_,
&fake_cryptohome_client_,
&fake_delegate_);
// Create callbacks for tests to use with verifier_.
callback_ = base::Bind(&PlatformVerificationFlowTest::FakeChallengeCallback,
base::Unretained(this));
// Configure the global cros_settings.
CrosSettings* cros_settings = CrosSettings::Get();
device_settings_provider_ =
cros_settings->GetProvider(kAttestationForContentProtectionEnabled);
cros_settings->RemoveSettingsProvider(device_settings_provider_);
cros_settings->AddSettingsProvider(&stub_settings_provider_);
cros_settings->SetBoolean(kAttestationForContentProtectionEnabled, true);
// Start with the first-time setting set since most tests want this.
fake_delegate_.pref_service().SetUserPref(prefs::kRAConsentFirstTime,
new base::FundamentalValue(true));
}
void TearDown() {
// Restore the real DeviceSettingsProvider.
CrosSettings* cros_settings = CrosSettings::Get();
cros_settings->RemoveSettingsProvider(&stub_settings_provider_);
cros_settings->AddSettingsProvider(device_settings_provider_);
fake_delegate_.TearDown();
}
void ExpectAttestationFlow() {
// When consent is not given or the feature is disabled, it is important
// that there are no calls to the attestation service. Thus, a test must
// explicitly expect these calls or the mocks will fail the test.
// Configure the mock AttestationFlow to call FakeGetCertificate.
EXPECT_CALL(mock_attestation_flow_,
GetCertificate(PROFILE_CONTENT_PROTECTION_CERTIFICATE,
kTestEmail, kTestID, _, _))
.WillRepeatedly(WithArgs<4>(Invoke(
this, &PlatformVerificationFlowTest::FakeGetCertificate)));
// Configure the mock AsyncMethodCaller to call FakeSignChallenge.
std::string expected_key_name = std::string(kContentProtectionKeyPrefix) +
std::string(kTestID);
EXPECT_CALL(mock_async_caller_,
TpmAttestationSignSimpleChallenge(KEY_USER, kTestEmail,
expected_key_name,
kTestChallenge, _))
.WillRepeatedly(WithArgs<4>(Invoke(
this, &PlatformVerificationFlowTest::FakeSignChallenge)));
}
void SetUserConsent(const GURL& url, bool allow) {
verifier_->RecordDomainConsent(fake_delegate_.GetContentSettings(NULL),
url,
allow);
}
void FakeGetCertificate(
const AttestationFlow::CertificateCallback& callback) {
std::string certificate =
(fake_certificate_index_ < fake_certificate_list_.size()) ?
fake_certificate_list_[fake_certificate_index_] : kTestCertificate;
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback,
certificate_success_,
certificate));
++fake_certificate_index_;
}
void FakeSignChallenge(
const cryptohome::AsyncMethodCaller::DataCallback& callback) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback,
sign_challenge_success_,
CreateFakeResponseProto()));
}
void FakeChallengeCallback(PlatformVerificationFlow::Result result,
const std::string& salt,
const std::string& signature,
const std::string& certificate) {
result_ = result;
challenge_salt_ = salt;
challenge_signature_ = signature;
certificate_ = certificate;
}
std::string CreateFakeResponseProto() {
SignedData pb;
pb.set_data(kTestSignedData);
pb.set_signature(kTestSignature);
std::string serial;
CHECK(pb.SerializeToString(&serial));
return serial;
}
protected:
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
StrictMock<MockAttestationFlow> mock_attestation_flow_;
cryptohome::MockAsyncMethodCaller mock_async_caller_;
CustomFakeCryptohomeClient fake_cryptohome_client_;
FakeDelegate fake_delegate_;
CrosSettingsProvider* device_settings_provider_;
StubCrosSettingsProvider stub_settings_provider_;
ScopedTestDeviceSettingsService test_device_settings_service_;
ScopedTestCrosSettings test_cros_settings_;
scoped_refptr<PlatformVerificationFlow> verifier_;
// Controls result of FakeGetCertificate.
bool certificate_success_;
std::vector<std::string> fake_certificate_list_;
size_t fake_certificate_index_;
// Controls result of FakeSignChallenge.
bool sign_challenge_success_;
// Callback functions and data.
PlatformVerificationFlow::ChallengeCallback callback_;
PlatformVerificationFlow::Result result_;
std::string challenge_salt_;
std::string challenge_signature_;
std::string certificate_;
};
TEST_F(PlatformVerificationFlowTest, SuccessNoConsent) {
SetUserConsent(GURL(kTestURL), true);
// Make sure the call will fail if consent is requested.
fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_);
EXPECT_EQ(kTestSignedData, challenge_salt_);
EXPECT_EQ(kTestSignature, challenge_signature_);
EXPECT_EQ(kTestCertificate, certificate_);
EXPECT_EQ(0, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, SuccessWithAttestationConsent) {
SetUserConsent(GURL(kTestURL), true);
fake_cryptohome_client_.set_attestation_enrolled(false);
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_);
EXPECT_EQ(kTestSignedData, challenge_salt_);
EXPECT_EQ(kTestSignature, challenge_signature_);
EXPECT_EQ(kTestCertificate, certificate_);
EXPECT_EQ(1, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, SuccessWithFirstTimeConsent) {
SetUserConsent(GURL(kTestURL), true);
fake_delegate_.pref_service().SetUserPref(prefs::kRAConsentFirstTime,
new base::FundamentalValue(false));
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_);
EXPECT_EQ(kTestSignedData, challenge_salt_);
EXPECT_EQ(kTestSignature, challenge_signature_);
EXPECT_EQ(kTestCertificate, certificate_);
EXPECT_EQ(1, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, ConsentRejected) {
fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_);
EXPECT_EQ(1, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, FeatureDisabled) {
CrosSettings::Get()->SetBoolean(kAttestationForContentProtectionEnabled,
false);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::POLICY_REJECTED, result_);
EXPECT_EQ(0, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, FeatureDisabledByUser) {
fake_delegate_.pref_service().SetUserPref(prefs::kEnableDRM,
new base::FundamentalValue(false));
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::POLICY_REJECTED, result_);
EXPECT_EQ(0, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, FeatureDisabledByUserForDomain) {
SetUserConsent(GURL(kTestURL), false);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::POLICY_REJECTED, result_);
EXPECT_EQ(0, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, NotVerified) {
certificate_success_ = false;
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::PLATFORM_NOT_VERIFIED, result_);
}
TEST_F(PlatformVerificationFlowTest, ChallengeSigningError) {
sign_challenge_success_ = false;
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::INTERNAL_ERROR, result_);
}
TEST_F(PlatformVerificationFlowTest, DBusFailure) {
fake_cryptohome_client_.set_call_status(DBUS_METHOD_CALL_FAILURE);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::INTERNAL_ERROR, result_);
}
TEST_F(PlatformVerificationFlowTest, ConsentNoResponse) {
fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_NONE);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_);
}
TEST_F(PlatformVerificationFlowTest, ConsentPerScheme) {
fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_);
// Call again and expect denial based on previous response.
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::POLICY_REJECTED, result_);
// Call with a different scheme and expect another consent prompt.
fake_delegate_.set_url(GURL(kTestURLSecure));
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_);
EXPECT_EQ(2, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, ConsentForExtension) {
fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
fake_delegate_.set_url(GURL(kTestURLExtension));
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_);
EXPECT_EQ(1, fake_delegate_.num_consent_calls());
}
TEST_F(PlatformVerificationFlowTest, Timeout) {
verifier_->set_timeout_delay(base::TimeDelta::FromSeconds(0));
ExpectAttestationFlow();
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::TIMEOUT, result_);
}
TEST_F(PlatformVerificationFlowTest, ExpiredCert) {
ExpectAttestationFlow();
fake_certificate_list_.resize(2);
ASSERT_TRUE(GetFakeCertificate(base::TimeDelta::FromDays(-1),
&fake_certificate_list_[0]));
ASSERT_TRUE(GetFakeCertificate(base::TimeDelta::FromDays(1),
&fake_certificate_list_[1]));
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_);
EXPECT_EQ(certificate_, fake_certificate_list_[1]);
}
TEST_F(PlatformVerificationFlowTest, IncognitoMode) {
fake_delegate_.set_is_incognito(true);
verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(PlatformVerificationFlow::PLATFORM_NOT_VERIFIED, result_);
}
} // namespace attestation
} // namespace chromeos
|