forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_auth_util_unittest.cc
378 lines (334 loc) · 14.6 KB
/
cast_auth_util_unittest.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
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
// Copyright 2014 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 "components/cast_channel/cast_auth_util.h"
#include <string>
#include "base/logging.h"
#include "base/macros.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "components/cast_certificate/cast_cert_validator.h"
#include "components/cast_certificate/cast_cert_validator_test_helpers.h"
#include "components/cast_certificate/cast_crl.h"
#include "components/cast_certificate/proto/test_suite.pb.h"
#include "components/cast_channel/proto/cast_channel.pb.h"
#include "net/cert/internal/trust_store_in_memory.h"
#include "net/cert/x509_certificate.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cast_channel {
namespace {
class CastAuthUtilTest : public testing::Test {
public:
CastAuthUtilTest() {}
~CastAuthUtilTest() override {}
void SetUp() override {}
protected:
static AuthResponse CreateAuthResponse(std::string* signed_data,
HashAlgorithm digest_algorithm) {
auto chain = cast_certificate::testing::ReadCertificateChainFromFile(
"certificates/chromecast_gen1.pem");
CHECK(!chain.empty());
auto signature_data = cast_certificate::testing::ReadSignatureTestData(
"signeddata/2ZZBG9_FA8FCA3EF91A.pem");
AuthResponse response;
response.set_client_auth_certificate(chain[0]);
for (size_t i = 1; i < chain.size(); ++i)
response.add_intermediate_certificate(chain[i]);
response.set_hash_algorithm(digest_algorithm);
switch (digest_algorithm) {
case SHA1:
response.set_signature(signature_data.signature_sha1);
break;
case SHA256:
response.set_signature(signature_data.signature_sha256);
break;
}
*signed_data = signature_data.message;
return response;
}
// Mangles a string by inverting the first byte.
static void MangleString(std::string* str) { (*str)[0] = ~(*str)[0]; }
};
// Note on expiration: VerifyCredentials() depends on the system clock. In
// practice this shouldn't be a problem though since the certificate chain
// being verified doesn't expire until 2032!
TEST_F(CastAuthUtilTest, VerifySuccess) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
base::Time now = base::Time::Now();
AuthResult result = VerifyCredentialsForTest(
auth_response, signed_data, cast_certificate::CRLPolicy::CRL_OPTIONAL,
nullptr, nullptr, now);
EXPECT_TRUE(result.success());
EXPECT_EQ(AuthResult::POLICY_NONE, result.channel_policies);
}
TEST_F(CastAuthUtilTest, VerifyBadCA) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
MangleString(auth_response.mutable_intermediate_certificate(0));
AuthResult result = VerifyCredentials(auth_response, signed_data);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_CERT_PARSING_FAILED, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyBadClientAuthCert) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
MangleString(auth_response.mutable_client_auth_certificate());
AuthResult result = VerifyCredentials(auth_response, signed_data);
EXPECT_FALSE(result.success());
// TODO(eroman): Not quite right of an error.
EXPECT_EQ(AuthResult::ERROR_CERT_PARSING_FAILED, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyBadSignature) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
MangleString(auth_response.mutable_signature());
AuthResult result = VerifyCredentials(auth_response, signed_data);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_SIGNED_BLOBS_MISMATCH, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyEmptySignature) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
auth_response.mutable_signature()->clear();
AuthResult result = VerifyCredentials(auth_response, signed_data);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_SIGNATURE_EMPTY, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyUnsupportedDigest) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
base::Feature{"CastSHA256Enforced", base::FEATURE_DISABLED_BY_DEFAULT});
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA1);
base::Time now = base::Time::Now();
AuthResult result = VerifyCredentialsForTest(
auth_response, signed_data, cast_certificate::CRLPolicy::CRL_OPTIONAL,
nullptr, nullptr, now);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_DIGEST_UNSUPPORTED, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyBackwardsCompatibleDigest) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA1);
base::Time now = base::Time::Now();
AuthResult result = VerifyCredentialsForTest(
auth_response, signed_data, cast_certificate::CRLPolicy::CRL_OPTIONAL,
nullptr, nullptr, now);
EXPECT_TRUE(result.success());
}
TEST_F(CastAuthUtilTest, VerifyBadPeerCert) {
std::string signed_data;
AuthResponse auth_response = CreateAuthResponse(&signed_data, SHA256);
MangleString(&signed_data);
AuthResult result = VerifyCredentials(auth_response, signed_data);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_SIGNED_BLOBS_MISMATCH, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifySenderNonceMatch) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
base::Feature{"CastNonceEnforced", base::FEATURE_DISABLED_BY_DEFAULT});
AuthContext context = AuthContext::Create();
AuthResult result = context.VerifySenderNonce(context.nonce());
EXPECT_TRUE(result.success());
}
TEST_F(CastAuthUtilTest, VerifySenderNonceMismatch) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
base::Feature{"CastNonceEnforced", base::FEATURE_DISABLED_BY_DEFAULT});
AuthContext context = AuthContext::Create();
std::string received_nonce = "test2";
EXPECT_NE(received_nonce, context.nonce());
AuthResult result = context.VerifySenderNonce(received_nonce);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_SENDER_NONCE_MISMATCH, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifySenderNonceMissing) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
base::Feature{"CastNonceEnforced", base::FEATURE_DISABLED_BY_DEFAULT});
AuthContext context = AuthContext::Create();
std::string received_nonce = "";
EXPECT_FALSE(context.nonce().empty());
AuthResult result = context.VerifySenderNonce(received_nonce);
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_SENDER_NONCE_MISMATCH, result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyTLSCertificateSuccess) {
auto tls_cert_der = cast_certificate::testing::ReadCertificateChainFromFile(
"certificates/test_tls_cert.pem");
scoped_refptr<net::X509Certificate> tls_cert =
net::X509Certificate::CreateFromBytes(tls_cert_der[0].data(),
tls_cert_der[0].size());
std::string peer_cert_der;
AuthResult result =
VerifyTLSCertificate(*tls_cert, &peer_cert_der, tls_cert->valid_start());
EXPECT_TRUE(result.success());
}
TEST_F(CastAuthUtilTest, VerifyTLSCertificateTooEarly) {
auto tls_cert_der = cast_certificate::testing::ReadCertificateChainFromFile(
"certificates/test_tls_cert.pem");
scoped_refptr<net::X509Certificate> tls_cert =
net::X509Certificate::CreateFromBytes(tls_cert_der[0].data(),
tls_cert_der[0].size());
std::string peer_cert_der;
AuthResult result = VerifyTLSCertificate(
*tls_cert, &peer_cert_der,
tls_cert->valid_start() - base::TimeDelta::FromSeconds(1));
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE,
result.error_type);
}
TEST_F(CastAuthUtilTest, VerifyTLSCertificateTooLate) {
auto tls_cert_der = cast_certificate::testing::ReadCertificateChainFromFile(
"certificates/test_tls_cert.pem");
scoped_refptr<net::X509Certificate> tls_cert =
net::X509Certificate::CreateFromBytes(tls_cert_der[0].data(),
tls_cert_der[0].size());
std::string peer_cert_der;
AuthResult result = VerifyTLSCertificate(
*tls_cert, &peer_cert_der,
tls_cert->valid_expiry() + base::TimeDelta::FromSeconds(2));
EXPECT_FALSE(result.success());
EXPECT_EQ(AuthResult::ERROR_TLS_CERT_EXPIRED, result.error_type);
}
// Indicates the expected result of test step's verification.
enum TestStepResult {
RESULT_SUCCESS,
RESULT_FAIL,
};
// Verifies that the certificate chain provided is not revoked according to
// the provided Cast CRL at |verification_time|.
// The provided CRL is verified at |verification_time|.
// If |crl_required| is set, then a valid Cast CRL must be provided.
// Otherwise, a missing CRL is be ignored.
AuthResult TestVerifyRevocation(
const std::vector<std::string>& certificate_chain,
const std::string& crl_bundle,
const base::Time& verification_time,
bool crl_required,
net::TrustStore* cast_trust_store,
net::TrustStore* crl_trust_store) {
AuthResponse response;
if (certificate_chain.size() > 0) {
response.set_client_auth_certificate(certificate_chain[0]);
for (size_t i = 1; i < certificate_chain.size(); ++i)
response.add_intermediate_certificate(certificate_chain[i]);
}
response.set_crl(crl_bundle);
cast_certificate::CRLPolicy crl_policy =
cast_certificate::CRLPolicy::CRL_REQUIRED;
if (!crl_required && crl_bundle.empty())
crl_policy = cast_certificate::CRLPolicy::CRL_OPTIONAL;
AuthResult result =
VerifyCredentialsForTest(response, "", crl_policy, cast_trust_store,
crl_trust_store, verification_time);
// This test doesn't set the signature so it will just fail there.
EXPECT_FALSE(result.success());
return result;
}
// Runs a single test case.
bool RunTest(const cast_certificate::DeviceCertTest& test_case) {
std::unique_ptr<net::TrustStore> crl_trust_store;
std::unique_ptr<net::TrustStore> cast_trust_store;
if (test_case.use_test_trust_anchors()) {
crl_trust_store = cast_certificate::testing::CreateTrustStoreFromFile(
"certificates/cast_crl_test_root_ca.pem");
cast_trust_store = cast_certificate::testing::CreateTrustStoreFromFile(
"certificates/cast_test_root_ca.pem");
EXPECT_TRUE(crl_trust_store.get());
EXPECT_TRUE(cast_trust_store.get());
}
std::vector<std::string> certificate_chain;
for (auto const& cert : test_case.der_cert_path()) {
certificate_chain.push_back(cert);
}
// CastAuthUtil verifies the CRL at the same time as the certificate.
base::Time verification_time;
uint64_t cert_verify_time = test_case.cert_verification_time_seconds();
if (cert_verify_time) {
verification_time = cast_certificate::testing::ConvertUnixTimestampSeconds(
cert_verify_time);
} else {
verification_time = cast_certificate::testing::ConvertUnixTimestampSeconds(
test_case.crl_verification_time_seconds());
}
std::string crl_bundle = test_case.crl_bundle();
AuthResult result;
switch (test_case.expected_result()) {
case cast_certificate::PATH_VERIFICATION_FAILED:
result = TestVerifyRevocation(
certificate_chain, crl_bundle, verification_time, false,
cast_trust_store.get(), crl_trust_store.get());
EXPECT_EQ(result.error_type,
AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA);
return result.error_type ==
AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA;
case cast_certificate::CRL_VERIFICATION_FAILED:
// Fall-through intended.
case cast_certificate::REVOCATION_CHECK_FAILED_WITHOUT_CRL:
result = TestVerifyRevocation(
certificate_chain, crl_bundle, verification_time, true,
cast_trust_store.get(), crl_trust_store.get());
EXPECT_EQ(result.error_type, AuthResult::ERROR_CRL_INVALID);
return result.error_type == AuthResult::ERROR_CRL_INVALID;
case cast_certificate::CRL_EXPIRED_AFTER_INITIAL_VERIFICATION:
// By-pass this test because CRL is always verified at the time the
// certificate is verified.
return true;
case cast_certificate::REVOCATION_CHECK_FAILED:
result = TestVerifyRevocation(
certificate_chain, crl_bundle, verification_time, true,
cast_trust_store.get(), crl_trust_store.get());
EXPECT_EQ(result.error_type, AuthResult::ERROR_CERT_REVOKED);
return result.error_type == AuthResult::ERROR_CERT_REVOKED;
case cast_certificate::SUCCESS:
result = TestVerifyRevocation(
certificate_chain, crl_bundle, verification_time, false,
cast_trust_store.get(), crl_trust_store.get());
EXPECT_EQ(result.error_type, AuthResult::ERROR_SIGNED_BLOBS_MISMATCH);
return result.error_type == AuthResult::ERROR_SIGNED_BLOBS_MISMATCH;
case UNSPECIFIED:
return false;
}
return false;
}
// Parses the provided test suite provided in wire-format proto.
// Each test contains the inputs and the expected output.
// To see the description of the test, execute the test.
// These tests are generated by a test generator in google3.
void RunTestSuite(const std::string& test_suite_file_name) {
std::string testsuite_raw =
cast_certificate::testing::ReadTestFileToString(test_suite_file_name);
cast_certificate::DeviceCertTestSuite test_suite;
EXPECT_TRUE(test_suite.ParseFromString(testsuite_raw));
uint16_t success = 0;
uint16_t failed = 0;
std::vector<std::string> failed_tests;
for (auto const& test_case : test_suite.tests()) {
LOG(INFO) << "[ RUN ] " << test_case.description();
bool result = RunTest(test_case);
EXPECT_TRUE(result);
if (!result) {
LOG(INFO) << "[ FAILED ] " << test_case.description();
++failed;
failed_tests.push_back(test_case.description());
} else {
LOG(INFO) << "[ PASSED ] " << test_case.description();
++success;
}
}
LOG(INFO) << "[ PASSED ] " << success << " test(s).";
if (failed) {
LOG(INFO) << "[ FAILED ] " << failed << " test(s), listed below:";
for (const auto& failed_test : failed_tests) {
LOG(INFO) << "[ FAILED ] " << failed_test;
}
}
}
TEST_F(CastAuthUtilTest, CRLTestSuite) {
RunTestSuite("testsuite/testsuite1.pb");
}
} // namespace
} // namespace cast_channel