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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/component_updater/pki_metadata_component_installer.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/strings/string_view_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/certificate_transparency/certificate_transparency_config.pb.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/ssl_status.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "crypto/hash.h"
#include "crypto/keypair.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "net/cert/test_root_certs.h"
#include "net/cert/x509_certificate.h"
#include "net/dns/mock_host_resolver.h"
#include "net/net_buildflags.h"
#include "net/test/cert_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/test_data_directory.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
#include "chrome/browser/ssl/ssl_browsertest_util.h"
#include "net/base/features.h"
#include "net/cert/internal/trust_store_chrome.h"
#include "net/cert/x509_util.h"
#include "net/test/cert_builder.h"
#endif
namespace {
enum class CTEnforcement {
// Enables CT enforcement.
kEnabled,
// Enables CT with Static CT API policy enforcement.
kEnabledWithStaticCTEnforcement,
// Disables CT enforcement via component updater proto.
kDisabledByProto,
// Disables CT enforcement via feature flag.
kDisabledByFeature
};
int64_t SecondsSinceEpoch(base::Time t) {
return (t - base::Time::UnixEpoch()).InSeconds();
}
// A CTLog generates a log identity private key, then computes and
// caches several properties from that key that are needed in test cases.
class CTLog {
public:
CTLog(std::string_view name,
base::Time start,
base::Time end,
chrome_browser_certificate_transparency::CTLog::LogType type)
: name_(name), start_(start), end_(end), type_(type) {}
std::string_view name() const { return name_; }
base::Time start() const { return start_; }
base::Time end() const { return end_; }
chrome_browser_certificate_transparency::CTLog::LogType type() const {
return type_;
}
base::span<const uint8_t> spki() const { return spki_; }
std::string_view spki_base64() const { return spki_base64_; }
// Even though the id is just a span of bytes, so this should theoretically
// return a base::span<const uint8_t> referencing the data we've cached, all the
// call sites want it as a string.
std::string id() const { return std::string(base::as_string_view(id_)); }
std::string_view id_base64() const { return id_base64_; }
bssl::UniquePtr<EVP_PKEY> key() { return bssl::UpRef(private_key_.key()); }
private:
const std::string name_;
const base::Time start_;
const base::Time end_;
const chrome_browser_certificate_transparency::CTLog::LogType type_;
// The generated private key and things derived from it. Note that the private
// key itself can't be const, because returning a reference to it in key()
// above requires mutating its inner refcount.
crypto::keypair::PrivateKey private_key_{
crypto::keypair::PrivateKey::GenerateEcP256()};
const std::vector<uint8_t> spki_{private_key_.ToSubjectPublicKeyInfo()};
const std::string spki_base64_{base::Base64Encode(spki_)};
const std::array<uint8_t, crypto::hash::kSha256Size> id_{
crypto::hash::Sha256(spki_)};
const std::string id_base64_{base::Base64Encode(id_)};
};
void AddLogToCTConfig(chrome_browser_certificate_transparency::CTConfig* config,
const CTLog& log) {
chrome_browser_certificate_transparency::CTLog* entry =
config->mutable_log_list()->add_logs();
entry->set_log_id(log.id_base64());
entry->set_key(log.spki_base64());
entry->set_purpose(chrome_browser_certificate_transparency::CTLog::PROD);
entry->set_log_type(log.type());
entry->mutable_temporal_interval()->mutable_start()->set_seconds(
SecondsSinceEpoch(log.start()));
entry->mutable_temporal_interval()->mutable_end()->set_seconds(
SecondsSinceEpoch(log.end()));
chrome_browser_certificate_transparency::CTLog_State* log_state =
entry->add_state();
log_state->set_current_state(
chrome_browser_certificate_transparency::CTLog::USABLE);
log_state->mutable_state_start()->set_seconds(SecondsSinceEpoch(log.start()));
chrome_browser_certificate_transparency::CTLog_OperatorChange*
operator_history = entry->add_operator_history();
operator_history->set_name(log.name());
operator_history->mutable_operator_start()->set_seconds(
SecondsSinceEpoch(log.start()));
}
} // namespace
namespace component_updater {
// TODO(crbug.com/341136041): add tests for pinning enforcement.
class PKIMetadataComponentUpdaterTest
: public InProcessBrowserTest,
public testing::WithParamInterface<CTEnforcement>,
public PKIMetadataComponentInstallerService::Observer {
public:
PKIMetadataComponentUpdaterTest() {
switch (GetParam()) {
case CTEnforcement::kEnabled:
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/
{features::kCertificateTransparencyAskBeforeEnabling},
/*disabled_features=*/{
net::features::kEnableStaticCTAPIEnforcement});
break;
case CTEnforcement::kEnabledWithStaticCTEnforcement:
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::
kCertificateTransparencyAskBeforeEnabling,
net::features::kEnableStaticCTAPIEnforcement},
/*disabled_features=*/{});
break;
case CTEnforcement::kDisabledByProto:
scoped_feature_list_.InitAndEnableFeature(
features::kCertificateTransparencyAskBeforeEnabling);
break;
case CTEnforcement::kDisabledByFeature:
scoped_feature_list_.InitAndDisableFeature(
features::kCertificateTransparencyAskBeforeEnabling);
break;
}
}
void SetUpInProcessBrowserTestFixture() override {
PKIMetadataComponentInstallerService::GetInstance()->AddObserver(this);
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
ASSERT_TRUE(component_dir_.CreateUniqueTempDir());
host_resolver()->AddRule("*", "127.0.0.1");
// Set up a configuration that will enable or disable CT enforcement
// depending on the test parameter.
chrome_browser_certificate_transparency::CTConfig ct_config;
ct_config.set_disable_ct_enforcement(GetParam() ==
CTEnforcement::kDisabledByProto);
ct_config.mutable_log_list()->mutable_timestamp()->set_seconds(
SecondsSinceEpoch(base::Time::Now()));
ASSERT_TRUE(PKIMetadataComponentInstallerService::GetInstance()
->WriteCTDataForTesting(component_dir_.GetPath(),
ct_config.SerializeAsString()));
}
void TearDownInProcessBrowserTestFixture() override {
PKIMetadataComponentInstallerService::GetInstance()->RemoveObserver(this);
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
// Wait for configuration set in `SetUpInProcessBrowserTestFixture` to load.
WaitForPKIConfiguration(1);
}
protected:
// Waits for the PKI to have been configured at least |expected_times|.
void WaitForPKIConfiguration(int expected_times) {
if (GetParam() == CTEnforcement::kDisabledByFeature) {
// When CT is disabled by the feature flag there are no callbacks to
// wait on, so just spin the runloop.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(pki_metadata_configured_times_, 0);
} else {
expected_pki_metadata_configured_times_ = expected_times;
if (pki_metadata_configured_times_ >=
expected_pki_metadata_configured_times_) {
return;
}
base::RunLoop run_loop;
pki_metadata_config_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
}
const base::FilePath& GetComponentDirPath() const {
return component_dir_.GetPath();
}
bool is_ct_enforced() const {
return GetParam() == CTEnforcement::kEnabled ||
GetParam() == CTEnforcement::kEnabledWithStaticCTEnforcement;
}
void DoTestAtLeastOneRFC6962LogPolicy(
chrome_browser_certificate_transparency::CTLog::LogType log_type,
bool expect_ct_error);
private:
void OnCTLogListConfigured() override {
++pki_metadata_configured_times_;
if (pki_metadata_config_closure_ &&
pki_metadata_configured_times_ >=
expected_pki_metadata_configured_times_) {
std::move(pki_metadata_config_closure_).Run();
}
}
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir component_dir_;
base::OnceClosure pki_metadata_config_closure_;
int expected_pki_metadata_configured_times_ = 0;
int pki_metadata_configured_times_ = 0;
};
// Tests that the PKI Metadata configuration is recovered after a network
// service restart.
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentUpdaterTest,
ReloadsPKIMetadataConfigAfterCrash) {
// Network service is not running out of process, so cannot be crashed.
if (!content::IsOutOfProcessNetworkService()) {
return;
}
// Make the test root be interpreted as a known root so that CT will be
// required.
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
net::ScopedTestKnownRoot scoped_known_root(root_cert.get());
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
static constexpr char kHostname[] = "example.com";
https_server_ok.SetCertHostnames({kHostname});
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(https_server_ok.Start());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL(kHostname, "/simple.html")));
// Check that the page is blocked depending on CT enforcement.
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
if (is_ct_enforced()) {
EXPECT_NE(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
// Restart the network service.
SimulateNetworkServiceCrash();
// Wait for the restarted network service to load the component update data
// that is already on disk.
WaitForPKIConfiguration(2);
// Check that the page is still blocked depending on CT enforcement.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL(kHostname, "/simple.html")));
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
if (is_ct_enforced()) {
EXPECT_NE(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
}
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentUpdaterTest, TestCTUpdate) {
const base::Time kLogStart = base::Time::Now() - base::Days(1);
const base::Time kLogEnd = base::Time::Now() + base::Days(1);
CTLog log1("log operator 1", kLogStart, kLogEnd,
chrome_browser_certificate_transparency::CTLog::RFC6962);
CTLog log2(
"log operator 2", kLogStart, kLogEnd,
chrome_browser_certificate_transparency::CTLog::LOG_TYPE_UNSPECIFIED);
// Make the test root be interpreted as a known root so that CT will be
// required.
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
net::ScopedTestKnownRoot scoped_known_root(root_cert.get());
// Start a test server that uses a certificate with SCTs for the above test
// logs.
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
// The same hostname is used for each request, which verifies that the CT log
// updates cause verifier caches and socket pool invalidation, so that the
// next request for the same host will use the updated CT state.
server_config.dns_names = {"example.com"};
server_config.embedded_scts.emplace_back(log1.id(), log1.key(),
base::Time::Now());
server_config.embedded_scts.emplace_back(log2.id(), log2.key(),
base::Time::Now());
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(https_server_ok.Start());
// Check that the page is blocked depending on CT enforcement.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("example.com", "/simple.html")));
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
if (is_ct_enforced()) {
EXPECT_NE(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
// Update with a CT configuration that trusts log1 and log2
//
// Set up a configuration that will enable or disable CT enforcement
// depending on the test parameter.
chrome_browser_certificate_transparency::CTConfig ct_config;
ct_config.set_disable_ct_enforcement(GetParam() ==
CTEnforcement::kDisabledByProto);
ct_config.mutable_log_list()->mutable_timestamp()->set_seconds(
SecondsSinceEpoch(base::Time::Now()));
AddLogToCTConfig(&ct_config, log1);
AddLogToCTConfig(&ct_config, log2);
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(PKIMetadataComponentInstallerService::GetInstance()
->WriteCTDataForTesting(GetComponentDirPath(),
ct_config.SerializeAsString()));
}
// Should be trusted now.
PKIMetadataComponentInstallerService::GetInstance()
->ReconfigureAfterNetworkRestart();
WaitForPKIConfiguration(2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("example.com", "/simple.html")));
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
// Update CT configuration again with the same CT logs but mark the 1st log
// as retired.
{
chrome_browser_certificate_transparency::CTLog* log =
ct_config.mutable_log_list()->mutable_logs(0);
log->clear_state();
// Log states are in reverse chronological order, so the most recent state
// comes first.
{
chrome_browser_certificate_transparency::CTLog_State* log_state =
log->add_state();
log_state->set_current_state(
chrome_browser_certificate_transparency::CTLog::RETIRED);
log_state->mutable_state_start()->set_seconds(
SecondsSinceEpoch(kLogStart) + 1);
}
{
chrome_browser_certificate_transparency::CTLog_State* log_state =
log->add_state();
log_state->set_current_state(
chrome_browser_certificate_transparency::CTLog::USABLE);
log_state->mutable_state_start()->set_seconds(
SecondsSinceEpoch(kLogStart));
}
}
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(PKIMetadataComponentInstallerService::GetInstance()
->WriteCTDataForTesting(GetComponentDirPath(),
ct_config.SerializeAsString()));
}
// Should be untrusted again since 2 logs are required for diversity. Both
// SCTs should verify successfully but only one of them is accepted as the
// other has a timestamp after the log retirement state change timestamp.
PKIMetadataComponentInstallerService::GetInstance()
->ReconfigureAfterNetworkRestart();
WaitForPKIConfiguration(3);
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("example.com", "/simple.html")));
if (is_ct_enforced()) {
EXPECT_NE(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
}
// Tests that at least one RFC6962 log policy is correctly applied when Static
// CT API enforcement is enabled. All logs in the test will be set to
// `log_type`. If `expect_ct_error_with_static_ct_api_enforcement` is true,
// CT checks with Static CT API enforcement should cause an SSL error.
void PKIMetadataComponentUpdaterTest::DoTestAtLeastOneRFC6962LogPolicy(
chrome_browser_certificate_transparency::CTLog::LogType log_type,
bool expect_ct_error_with_static_ct_api_enforcement) {
const base::Time kLogStart = base::Time::Now() - base::Days(1);
const base::Time kLogEnd = base::Time::Now() + base::Days(1);
CTLog log1("log operator 1", kLogStart, kLogEnd, log_type);
CTLog log2("log operator 2", kLogStart, kLogEnd, log_type);
// Make the test root be interpreted as a known root so that CT will be
// required.
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
net::ScopedTestKnownRoot scoped_known_root(root_cert.get());
// Start a test server that uses a certificate with SCTs for the above test
// logs.
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
// The same hostname is used for each request, which verifies that the CT log
// updates cause verifier caches and socket pool invalidation, so that the
// next request for the same host will use the updated CT state.
server_config.dns_names = {"example.com"};
server_config.embedded_scts.emplace_back(log1.id(), log1.key(),
base::Time::Now());
server_config.embedded_scts.emplace_back(log2.id(), log2.key(),
base::Time::Now());
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(https_server_ok.Start());
// Check that the page is blocked depending on CT enforcement.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("example.com", "/simple.html")));
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
if (is_ct_enforced()) {
EXPECT_NE(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
// Update with a CT configuration that trusts log1 and log2. Neither of
// these logs is RFC6962, so the SCTs will not pass validation.
//
// Set up a configuration that will enable or disable CT enforcement
// depending on the test parameter.
chrome_browser_certificate_transparency::CTConfig ct_config;
ct_config.set_disable_ct_enforcement(GetParam() ==
CTEnforcement::kDisabledByProto);
ct_config.mutable_log_list()->mutable_timestamp()->set_seconds(
SecondsSinceEpoch(base::Time::Now()));
AddLogToCTConfig(&ct_config, log1);
AddLogToCTConfig(&ct_config, log2);
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(PKIMetadataComponentInstallerService::GetInstance()
->WriteCTDataForTesting(GetComponentDirPath(),
ct_config.SerializeAsString()));
}
PKIMetadataComponentInstallerService::GetInstance()
->ReconfigureAfterNetworkRestart();
WaitForPKIConfiguration(2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("example.com", "/simple.html")));
if (GetParam() == CTEnforcement::kEnabledWithStaticCTEnforcement) {
if (expect_ct_error_with_static_ct_api_enforcement) {
EXPECT_NE(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
} else {
EXPECT_EQ(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
} else {
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
}
}
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentUpdaterTest,
TestAtLeastOneRFC6962LogPolicy_StaticCTAPILogs) {
// Test with all logs with Static CT API type. Since at least one RFC6962 log
// is expected, this should show an SSL error caused by CT.
DoTestAtLeastOneRFC6962LogPolicy(
chrome_browser_certificate_transparency::CTLog::STATIC_CT_API,
/*expect_ct_error_with_static_ct_api_enforcement=*/true);
}
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentUpdaterTest,
TestAtLeastOneRFC6962LogPolicy_UnspecifiedLogTypes) {
// Test with all logs with unspecified type. These are treated as RFC6962
// logs so they shouldn't cause an SSL error.
// TODO(crbug.com/370724580): Disallow unspecified log type once all logs in
// the hardcoded and component updater protos have proper log types.
DoTestAtLeastOneRFC6962LogPolicy(
chrome_browser_certificate_transparency::CTLog::LOG_TYPE_UNSPECIFIED,
/*expect_ct_error_with_static_ct_api_enforcement=*/false);
}
INSTANTIATE_TEST_SUITE_P(
PKIMetadataComponentUpdater,
PKIMetadataComponentUpdaterTest,
testing::Values(CTEnforcement::kEnabled,
CTEnforcement::kEnabledWithStaticCTEnforcement,
CTEnforcement::kDisabledByProto,
CTEnforcement::kDisabledByFeature));
#if BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
class PKIMetadataComponentChromeRootStoreUpdateTest
: public InProcessBrowserTest,
public PKIMetadataComponentInstallerService::Observer {
public:
void SetUpInProcessBrowserTestFixture() override {
SystemNetworkContextManager::SetEnableCertificateTransparencyForTesting(
false);
PKIMetadataComponentInstallerService::GetInstance()->AddObserver(this);
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
ASSERT_TRUE(component_dir_.CreateUniqueTempDir());
host_resolver()->AddRule("*", "127.0.0.1");
}
void TearDownInProcessBrowserTestFixture() override {
PKIMetadataComponentInstallerService::GetInstance()->RemoveObserver(this);
SystemNetworkContextManager::SetEnableCertificateTransparencyForTesting(
std::nullopt);
}
class CRSWaiter {
public:
explicit CRSWaiter(PKIMetadataComponentChromeRootStoreUpdateTest* test) {
test_ = test;
test_->crs_config_closure_ = run_loop_.QuitClosure();
}
void Wait() { run_loop_.Run(); }
private:
base::RunLoop run_loop_;
raw_ptr<PKIMetadataComponentChromeRootStoreUpdateTest> test_;
};
void InstallCRSUpdate(chrome_root_store::RootStore root_store_proto) {
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(
PKIMetadataComponentInstallerService::GetInstance()
->WriteCRSDataForTesting(component_dir_.GetPath(),
root_store_proto.SerializeAsString()));
}
CRSWaiter waiter(this);
PKIMetadataComponentInstallerService::GetInstance()
->ConfigureChromeRootStore();
waiter.Wait();
}
void InstallCRSUpdate(const std::vector<std::string>& der_roots) {
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++last_used_crs_version_);
for (const auto& der_root : der_roots) {
root_store_proto.add_trust_anchors()->set_der(der_root);
}
InstallCRSUpdate(std::move(root_store_proto));
}
protected:
base::ScopedTempDir component_dir_;
private:
void OnChromeRootStoreConfigured() override {
if (crs_config_closure_) {
std::move(crs_config_closure_).Run();
}
}
base::OnceClosure crs_config_closure_;
int64_t last_used_crs_version_ = net::CompiledChromeRootStoreVersion();
};
IN_PROC_BROWSER_TEST_F(PKIMetadataComponentChromeRootStoreUpdateTest,
CheckCRSUpdate) {
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
server_config.dns_names = {"*.example.com"};
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
// Clear test roots so that cert validation only happens with
// what's in Chrome Root Store.
net::TestRootCerts::GetInstance()->Clear();
ASSERT_TRUE(https_server_ok.Start());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("a.example.com", "/simple.html")));
// Check that the page is blocked depending on contents of Chrome Root Store.
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
{
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
InstallCRSUpdate({std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer()))});
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("b.example.com", "/simple.html")));
// Check that the page is allowed due to contents of Chrome Root Store.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
{
// We reject empty CRS updates, so create a new cert root that doesn't match
// what the test server uses.
auto [leaf, root] = net::CertBuilder::CreateSimpleChain2();
InstallCRSUpdate({root->GetDER()});
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
// Check that the page is blocked depending on contents of Chrome Root Store.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
}
// Similar to CheckCRSUpdate, except using the same hostname for all requests.
// This tests whether the CRS update causes cached verification results to be
// disregarded.
IN_PROC_BROWSER_TEST_F(PKIMetadataComponentChromeRootStoreUpdateTest,
CheckCRSUpdateAffectsCachedVerifications) {
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
server_config.dns_names = {"*.example.com"};
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
// Clear test roots so that cert validation only happens with
// what's in Chrome Root Store.
net::TestRootCerts::GetInstance()->Clear();
static constexpr char kHostname[] = "a.example.com";
ASSERT_TRUE(https_server_ok.Start());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL(kHostname, "/simple.html")));
// Check that the page is blocked depending on contents of Chrome Root Store.
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
{
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
InstallCRSUpdate({std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer()))});
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL(kHostname, "/title2.html")));
// Check that the page is allowed due to contents of Chrome Root Store.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(),
u"Title Of Awesomeness");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
{
// We reject empty CRS updates, so create a new cert root that doesn't match
// what the test server uses.
auto [leaf, root] = net::CertBuilder::CreateSimpleChain2();
InstallCRSUpdate({root->GetDER()});
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL(kHostname, "/title3.html")));
// Check that the page is blocked depending on contents of Chrome Root Store.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(),
u"Title Of Awesomeness");
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(),
u"Title Of More Awesomeness");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
}
IN_PROC_BROWSER_TEST_F(PKIMetadataComponentChromeRootStoreUpdateTest,
UpdateTrustAnchorIDs) {
content::StoragePartition* partition =
chrome_test_utils::GetActiveWebContents(this)
->GetBrowserContext()
->GetDefaultStoragePartition();
int64_t crs_version = net::CompiledChromeRootStoreVersion();
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
scoped_refptr<net::X509Certificate> intermediate1 = net::ImportCertFromFile(
net::GetTestCertsDirectory(), "intermediate_ca_cert.pem");
ASSERT_TRUE(intermediate1);
scoped_refptr<net::X509Certificate> intermediate2 = net::ImportCertFromFile(
net::GetTestCertsDirectory(), "verisign_intermediate_ca_2016.pem");
ASSERT_TRUE(intermediate2);
// Test that the initial set of Trust Anchor IDs comes from the compiled-in
// root store.
{
std::vector<std::vector<uint8_t>> expected_trust_anchor_ids =
net::TrustStoreChrome::GetTrustAnchorIDsFromCompiledInRootStore();
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
std::vector<std::vector<uint8_t>> trust_anchor_ids;
partition->GetNetworkContext()->GetTrustAnchorIDsForTesting(
&trust_anchor_ids);
EXPECT_THAT(trust_anchor_ids,
testing::UnorderedElementsAreArray(expected_trust_anchor_ids));
}
// Install CRS update that contains no trusted Trust Anchor IDs.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
InstallCRSUpdate(std::move(root_store_proto));
// Ensure that SSLConfigClients have been notified of the new trust anchor
// IDs.
SystemNetworkContextManager::GetInstance()
->FlushSSLConfigManagerForTesting();
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
std::vector<std::vector<uint8_t>> trust_anchor_ids;
partition->GetNetworkContext()->GetTrustAnchorIDsForTesting(
&trust_anchor_ids);
EXPECT_TRUE(trust_anchor_ids.empty());
}
// Install CRS update that contains two trusted Trust Anchor IDs.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->set_trust_anchor_id({0x01, 0x02, 0x03});
chrome_root_store::TrustAnchor* additional_cert1 =
root_store_proto.add_additional_certs();
additional_cert1->set_der(
std::string(net::x509_util::CryptoBufferAsStringPiece(
intermediate1->cert_buffer())));
additional_cert1->set_trust_anchor_id({0x01, 0x02});
// `additional_cert1`'s trust anchor ID should be ignored because it is not
// configured as a TLS trust anchor.
additional_cert1->set_tls_trust_anchor(false);
chrome_root_store::TrustAnchor* additional_cert2 =
root_store_proto.add_additional_certs();
additional_cert2->set_der(
std::string(net::x509_util::CryptoBufferAsStringPiece(
intermediate2->cert_buffer())));
additional_cert2->set_trust_anchor_id({0x02, 0x03});
additional_cert2->set_tls_trust_anchor(true);
InstallCRSUpdate(std::move(root_store_proto));
// Ensure that SSLConfigClients have been notified of the new trust anchor
// IDs.
SystemNetworkContextManager::GetInstance()
->FlushSSLConfigManagerForTesting();
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
std::vector<std::vector<uint8_t>> trust_anchor_ids;
partition->GetNetworkContext()->GetTrustAnchorIDsForTesting(
&trust_anchor_ids);
EXPECT_THAT(trust_anchor_ids, testing::UnorderedElementsAre(
std::vector<uint8_t>({0x01, 0x02, 0x3}),
std::vector<uint8_t>({0x02, 0x03})));
}
}
// Tests that when new network contexts are created after a Trust Anchor IDs
// component update is received, the new network context uses the Trust Anchor
// IDs from the component updater.
IN_PROC_BROWSER_TEST_F(PKIMetadataComponentChromeRootStoreUpdateTest,
NewNetworkContextAfterUpdatingTrustAnchorIDs) {
// This test is only works with an out-of-process network service because it
// uses a network service crash/restart to test what happens when a new
// network context is created.
if (content::IsInProcessNetworkService()) {
return;
}
content::StoragePartition* partition =
chrome_test_utils::GetActiveWebContents(this)
->GetBrowserContext()
->GetDefaultStoragePartition();
int64_t crs_version = net::CompiledChromeRootStoreVersion();
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
// Install CRS update that contains one trusted Trust Anchor IDs.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->set_trust_anchor_id(
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
InstallCRSUpdate(std::move(root_store_proto));
// Ensure that SSLConfigClients have been notified of the new trust anchor
// IDs.
SystemNetworkContextManager::GetInstance()
->FlushSSLConfigManagerForTesting();
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
std::vector<std::vector<uint8_t>> trust_anchor_ids;
partition->GetNetworkContext()->GetTrustAnchorIDsForTesting(
&trust_anchor_ids);
EXPECT_THAT(trust_anchor_ids,
testing::UnorderedElementsAre(std::vector<uint8_t>(
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})));
}
network::mojom::NetworkContext* old_network_context =
partition->GetNetworkContext();
// Simulate a network service crash and restart, and check that the newly
// created network service uses the Trust Anchor ID from the prior component
// update.
SimulateNetworkServiceCrash();
// Flush the interface to make sure it notices the crash.
partition->FlushNetworkInterfaceForTesting();
{
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
std::vector<std::vector<uint8_t>> trust_anchor_ids;
// Just to be sure that the test is testing what it intends to, check that a
// new network context has been created.
ASSERT_NE(old_network_context, partition->GetNetworkContext());
partition->GetNetworkContext()->GetTrustAnchorIDsForTesting(
&trust_anchor_ids);
EXPECT_THAT(trust_anchor_ids,
testing::UnorderedElementsAre(std::vector<uint8_t>(
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})));
}
}
IN_PROC_BROWSER_TEST_F(PKIMetadataComponentChromeRootStoreUpdateTest,
CheckCRSUpdateDnsConstraint) {
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
server_config.dns_names = {"*.example.com"};
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
// Clear test roots so that cert validation only happens with
// what's in Chrome Root Store.
net::TestRootCerts::GetInstance()->Clear();
ASSERT_TRUE(https_server_ok.Start());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("a.example.com", "/simple.html")));
// The page should be blocked as the test root is not trusted yet.
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
int64_t crs_version = net::CompiledChromeRootStoreVersion();
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
// Install CRS update that trusts root with a constraint that matches the
// leaf's subjectAltName.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->add_permitted_dns_names("example.com");
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("b.example.com", "/simple.html")));
// Check that the page is allowed now.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
// Install CRS update that trusts root with a constraint that does not match
// the leaf's subjectAltName.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->add_permitted_dns_names("example.org");
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
// Check that the page is blocked now.
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_NE(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticationBrokenState(
tab, net::CERT_STATUS_AUTHORITY_INVALID,
ssl_test_util::AuthState::SHOWING_INTERSTITIAL);
}
class PKIMetadataComponentChromeRootStoreUpdateQwacTest
: public PKIMetadataComponentChromeRootStoreUpdateTest,
public testing::WithParamInterface<bool> {
public:
PKIMetadataComponentChromeRootStoreUpdateQwacTest() {
if (GetParam()) {
feature_list_.InitAndEnableFeature(net::features::kVerifyQWACs);
} else {
feature_list_.InitAndDisableFeature(net::features::kVerifyQWACs);
}
}
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(,
PKIMetadataComponentChromeRootStoreUpdateQwacTest,
testing::Bool());
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentChromeRootStoreUpdateQwacTest,
CheckCrsEutlUpdate) {
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
server_config.dns_names = {"*.example.com"};
// Set policy OIDs and QWAC QC types on the leaf so that it will validate as
// a QWAC. Also include an intermediate so we can set the intermediate as
// part of the EUTL trust store in the CRS update.
// OIDs: CABF OV, ETSI QNCP-w
server_config.policy_oids = {"2.23.140.1.2.2", "0.4.0.194112.1.5"};
server_config.qwac_qc_types = {bssl::der::Input(net::kEtsiQctWebOid)};
server_config.intermediate =
net::EmbeddedTestServer::IntermediateType::kInHandshake;
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
// Install only the root cert as a trust anchor in CRS and check that the
// page load is successful but the cert is not a valid QWAC.
net::TestRootCerts::GetInstance()->Clear();
int64_t crs_version = net::CompiledChromeRootStoreVersion();
{
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
auto* trust_anchor = root_store_proto.add_trust_anchors();
trust_anchor->set_der(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer()));
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(https_server_ok.Start());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("a.example.com", "/simple.html")));
// Check that the page's cert status is not a QWAC.
content::WebContents* tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
content::NavigationEntry* entry = tab->GetController().GetVisibleEntry();
net::CertStatus cert_status = entry->GetSSL().cert_status;
EXPECT_FALSE(cert_status & net::CERT_STATUS_IS_QWAC);
// Install CRS update that has the root as a trust anchor in CRS and the
// intermediate as a QWAC issuer.
{
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
scoped_refptr<net::X509Certificate> intermediate_cert =
https_server_ok.GetGeneratedIntermediate();
ASSERT_TRUE(intermediate_cert);
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
auto* trust_anchor = root_store_proto.add_trust_anchors();
trust_anchor->set_der(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer()));
auto* additional_cert = root_store_proto.add_additional_certs();
additional_cert->set_der(net::x509_util::CryptoBufferAsStringPiece(
intermediate_cert->cert_buffer()));
additional_cert->set_eutl(true);
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("b.example.com", "/simple.html")));
// Check the page's cert status is a QWAC (if net::features::kVerifyQWACs is
// enabled).
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
cert_status = tab->GetController().GetVisibleEntry()->GetSSL().cert_status;
EXPECT_EQ(GetParam(), !!(cert_status & net::CERT_STATUS_IS_QWAC));
// Install a CRS update that has the root as both a trust anchor in CRS and
// a QWAC issuer
{
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
auto* trust_anchor = root_store_proto.add_trust_anchors();
trust_anchor->set_der(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer()));
trust_anchor->set_eutl(true);
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
// Check the page's cert status is a QWAC (if net::features::kVerifyQWACs is
// enabled).
tab = chrome_test_utils::GetActiveWebContents(this);
ASSERT_TRUE(WaitForRenderFrameReady(tab->GetPrimaryMainFrame()));
EXPECT_EQ(chrome_test_utils::GetActiveWebContents(this)->GetTitle(), u"OK");
ssl_test_util::CheckAuthenticatedState(tab, ssl_test_util::AuthState::NONE);
cert_status = tab->GetController().GetVisibleEntry()->GetSSL().cert_status;
EXPECT_EQ(GetParam(), !!(cert_status & net::CERT_STATUS_IS_QWAC));
}
// Test suite for tests that depend on both Certificate Transparency and Chrome
// Root Store updates.
class PKIMetadataComponentCtAndCrsUpdaterTest
: public InProcessBrowserTest,
public testing::WithParamInterface<CTEnforcement>,
public PKIMetadataComponentInstallerService::Observer {
public:
PKIMetadataComponentCtAndCrsUpdaterTest() {
if (GetParam() == CTEnforcement::kDisabledByFeature) {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/
{
#if BUILDFLAG(CHROME_ROOT_STORE_OPTIONAL)
net::features::kChromeRootStoreUsed
#endif
},
/*disabled_features=*/{
features::kCertificateTransparencyAskBeforeEnabling});
} else {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/
{features::kCertificateTransparencyAskBeforeEnabling,
#if BUILDFLAG(CHROME_ROOT_STORE_OPTIONAL)
net::features::kChromeRootStoreUsed
#endif
},
/*disabled_features=*/{});
}
}
void SetUpInProcessBrowserTestFixture() override {
PKIMetadataComponentInstallerService::GetInstance()->AddObserver(this);
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
ASSERT_TRUE(component_dir_.CreateUniqueTempDir());
host_resolver()->AddRule("*", "127.0.0.1");
}
void TearDownInProcessBrowserTestFixture() override {
PKIMetadataComponentInstallerService::GetInstance()->RemoveObserver(this);
}
protected:
// Waits for the CT log lists to have been configured at least
// |expected_times|.
void WaitForCtConfiguration(int expected_times) {
if (GetParam() == CTEnforcement::kDisabledByFeature) {
// When CT is disabled by the feature flag there are no callbacks to
// wait on, so just spin the runloop.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ct_log_list_configured_times_, 0);
} else {
expected_ct_log_list_configured_times_ = expected_times;
if (ct_log_list_configured_times_ >=
expected_ct_log_list_configured_times_) {
return;
}
base::RunLoop run_loop;
pki_metadata_config_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
}
const base::FilePath& GetComponentDirPath() const {
return component_dir_.GetPath();
}
void InstallCRSUpdate(chrome_root_store::RootStore root_store_proto) {
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(
PKIMetadataComponentInstallerService::GetInstance()
->WriteCRSDataForTesting(component_dir_.GetPath(),
root_store_proto.SerializeAsString()));
}
CRSWaiter waiter(this);
PKIMetadataComponentInstallerService::GetInstance()
->ConfigureChromeRootStore();
waiter.Wait();
}
private:
void OnCTLogListConfigured() override {
++ct_log_list_configured_times_;
if (pki_metadata_config_closure_ &&
ct_log_list_configured_times_ >=
expected_ct_log_list_configured_times_) {
std::move(pki_metadata_config_closure_).Run();
}
}
void OnChromeRootStoreConfigured() override {
if (crs_config_closure_) {
std::move(crs_config_closure_).Run();
}
}
class CRSWaiter {
public:
explicit CRSWaiter(PKIMetadataComponentCtAndCrsUpdaterTest* test) {
test_ = test;
test_->crs_config_closure_ = run_loop_.QuitClosure();
}
void Wait() { run_loop_.Run(); }
private:
base::RunLoop run_loop_;
raw_ptr<PKIMetadataComponentCtAndCrsUpdaterTest> test_;
};
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir component_dir_;
base::OnceClosure pki_metadata_config_closure_;
int expected_ct_log_list_configured_times_ = 0;
int ct_log_list_configured_times_ = 0;
base::OnceClosure crs_config_closure_;
int64_t last_used_crs_version_ = net::CompiledChromeRootStoreVersion();
};
IN_PROC_BROWSER_TEST_P(PKIMetadataComponentCtAndCrsUpdaterTest,
TestChromeRootStoreConstraintsSct) {
const base::Time kLogStart = base::Time::Now() - base::Days(1);
const base::Time kLogEnd = base::Time::Now() + base::Days(1);
CTLog log1("log operator 1", kLogStart, kLogEnd,
chrome_browser_certificate_transparency::CTLog::RFC6962);
CTLog log2(
"log operator 2", kLogStart, kLogEnd,
chrome_browser_certificate_transparency::CTLog::LOG_TYPE_UNSPECIFIED);
CTLog unknown_log(
"unknown log operator", kLogStart, kLogEnd,
chrome_browser_certificate_transparency::CTLog::LOG_TYPE_UNSPECIFIED);
const base::Time kSctTime0UnknownLog = base::Time::Now() - base::Minutes(30);
const base::Time kSctTime1 = base::Time::Now() - base::Minutes(20);
const base::Time kSctTime2 = base::Time::Now() - base::Minutes(10);
// Start a test server that uses a certificate with SCTs for the above test
// logs.
net::EmbeddedTestServer https_server_ok(net::EmbeddedTestServer::TYPE_HTTPS);
net::EmbeddedTestServer::ServerCertificateConfig server_config;
server_config.dns_names = {"*.example.com"};
server_config.embedded_scts.emplace_back(log1.id(), log1.key(), kSctTime1);
server_config.embedded_scts.emplace_back(log2.id(), log2.key(), kSctTime2);
server_config.embedded_scts.emplace_back(unknown_log.id(), unknown_log.key(),
kSctTime0UnknownLog);
https_server_ok.SetSSLConfig(server_config);
https_server_ok.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(https_server_ok.Start());
// Clear test roots so that cert validation only happens with
// what's in Chrome Root Store.
net::TestRootCerts::GetInstance()->Clear();
scoped_refptr<net::X509Certificate> root_cert =
net::ImportCertFromFile(net::EmbeddedTestServer::GetRootCertPemPath());
ASSERT_TRUE(root_cert);
int64_t crs_version = net::CompiledChromeRootStoreVersion();
// Install CRS update that trusts root without constraints.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
InstallCRSUpdate(std::move(root_store_proto));
}
// Install CT configuration that trusts log1 and log2.
//
// Set up a configuration that will enable or disable CT enforcement
// depending on the test parameter.
chrome_browser_certificate_transparency::CTConfig ct_config;
ct_config.set_disable_ct_enforcement(GetParam() ==
CTEnforcement::kDisabledByProto);
ct_config.mutable_log_list()->mutable_timestamp()->set_seconds(
SecondsSinceEpoch(base::Time::Now()));
AddLogToCTConfig(&ct_config, log1);
AddLogToCTConfig(&ct_config, log2);
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(PKIMetadataComponentInstallerService::GetInstance()
->WriteCTDataForTesting(GetComponentDirPath(),
ct_config.SerializeAsString()));
}
PKIMetadataComponentInstallerService::GetInstance()
->ReconfigureAfterNetworkRestart();
WaitForCtConfiguration(1);
// Should be trusted.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("b.example.com", "/simple.html")));
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
// Install CRS update that trusts root with a SCTNotAfter constraint.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->set_sct_not_after_sec(
SecondsSinceEpoch(kSctTime1 + base::Seconds(1)));
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
// Should be trusted if CT is enabled since the SCTNotAfter constraint is
// satisfied by the SCT from log1. Should be trusted if CT feature is
// disabled since SCTNotAfter fails open when CT is disabled.
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
// Install CRS update that trusts root with a SCTNotAfter constraint that is
// before both of the valid SCTs.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->set_sct_not_after_sec(
SecondsSinceEpoch(kSctTime0UnknownLog + base::Seconds(1)));
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
switch (GetParam()) {
case CTEnforcement::kEnabled:
case CTEnforcement::kEnabledWithStaticCTEnforcement:
// Should be distrusted if CT is enabled. The SCTNotAfter constraint is
// not satisfied by any valid SCT. The SCT from the unknown log is not
// counted even though the timestamp matches the constraint.
EXPECT_NE(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
break;
case CTEnforcement::kDisabledByProto:
case CTEnforcement::kDisabledByFeature:
// Should be trusted if CT feature is disabled since SCTNotAfter fails
// open when CT is disabled.
EXPECT_EQ(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
break;
}
// Install CRS update that trusts root with a SCTAllAfter constraint that is
// before both of the valid SCTs.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->set_sct_all_after_sec(
SecondsSinceEpoch(kSctTime1 - base::Seconds(1)));
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
// Should be trusted if CT is enabled since the SCTAlltAfter constraint is
// satisfied by the SCT from both logs.
// Should be trusted if CT feature is disabled since SCTAllAfter fails
// open when CT is disabled.
EXPECT_EQ(u"OK", chrome_test_utils::GetActiveWebContents(this)->GetTitle());
// Install CRS update that trusts root with a SCTAllAfter constraint that is
// before one of the SCTs but after the other.
{
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version);
chrome_root_store::TrustAnchor* anchor =
root_store_proto.add_trust_anchors();
anchor->set_der(std::string(
net::x509_util::CryptoBufferAsStringPiece(root_cert->cert_buffer())));
anchor->add_constraints()->set_sct_all_after_sec(
SecondsSinceEpoch(kSctTime1 + base::Seconds(1)));
InstallCRSUpdate(std::move(root_store_proto));
}
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), https_server_ok.GetURL("c.example.com", "/simple.html")));
switch (GetParam()) {
case CTEnforcement::kEnabled:
case CTEnforcement::kEnabledWithStaticCTEnforcement:
// Should be distrusted since one of the SCTs was before the SCTAllAfter
// constraint.
EXPECT_NE(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
break;
case CTEnforcement::kDisabledByProto:
case CTEnforcement::kDisabledByFeature:
// Should be trusted if CT feature is disabled since SCTAllAfter fails
// open when CT is disabled.
EXPECT_EQ(u"OK",
chrome_test_utils::GetActiveWebContents(this)->GetTitle());
break;
}
}
INSTANTIATE_TEST_SUITE_P(
PKIMetadataComponentUpdater,
PKIMetadataComponentCtAndCrsUpdaterTest,
testing::Values(CTEnforcement::kEnabled,
CTEnforcement::kEnabledWithStaticCTEnforcement,
CTEnforcement::kDisabledByProto,
CTEnforcement::kDisabledByFeature));
// TODO(crbug.com/40816087) additional Chrome Root Store browser tests to
// add:
//
// * Test that AIA fetching still works after updating CRS.
#endif // BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
} // namespace component_updater
|