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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "net/cert/ct_log_verifier.h"
#include <stdint.h>
#include <algorithm>
#include <array>
#include <bit>
#include <memory>
#include <string>
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_view_util.h"
#include "base/time/time.h"
#include "crypto/secure_hash.h"
#include "net/base/hash_value.h"
#include "net/cert/ct_log_verifier_util.h"
#include "net/cert/merkle_audit_proof.h"
#include "net/cert/merkle_consistency_proof.h"
#include "net/cert/signed_certificate_timestamp.h"
#include "net/cert/signed_tree_head.h"
#include "net/test/ct_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
// All test data replicated from
// https://github.com/google/certificate-transparency/blob/c41b090ecc14ddd6b3531dc7e5ce36b21e253fdd/cpp/merkletree/merkle_tree_test.cc
// The SHA-256 hash of an empty Merkle tree.
const uint8_t kEmptyTreeHash[32] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
std::string GetEmptyTreeHash() {
return std::string(std::begin(kEmptyTreeHash), std::end(kEmptyTreeHash));
}
// SHA-256 Merkle leaf hashes for the sample tree that all of the other test
// data relates to (8 leaves).
const std::array<const char*, 8> kLeafHashes = {
"6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
"96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7",
"0298d122906dcfc10892cb53a73992fc5b9f493ea4c9badb27b791b4127a7fe7",
"07506a85fd9dd2f120eb694f86011e5bb4662e5c415a62917033d4a9624487e7",
"bc1a0643b12e4d2d7c77918f44e0f4f79a838b6cf9ec5b5c283e1f4d88599e6b",
"4271a26be0d8a84f0bd54c8c302e7cb3a3b5d1fa6780a40bcce2873477dab658",
"b08693ec2e721597130641e8211e7eedccb4c26413963eee6c1e2ed16ffb1a5f",
"46f6ffadd3d06a09ff3c5860d2755c8b9819db7df44251788c7d8e3180de8eb1",
};
// SHA-256 Merkle root hashes from building the sample tree leaf-by-leaf.
// The first entry is the root when the tree contains 1 leaf, and the last is
// the root when the tree contains all 8 leaves.
const std::array<const char*, 8> kRootHashes = {
"6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
"fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125",
"aeb6bcfe274b70a14fb067a5e5578264db0fa9b51af5e0ba159158f329e06e77",
"d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7",
"4e3bbb1f7b478dcfe71fb631631519a3bca12c9aefca1612bfce4c13a86264d4",
"76e67dadbcdf1e10e1b74ddc608abd2f98dfb16fbce75277b5232a127f2087ef",
"ddb89be403809e325750d3d263cd78929c2942b7942a34b77e122c9594a74c8c",
"5dc9da79a70659a9ad559cb701ded9a2ab9d823aad2f4960cfe370eff4604328",
};
// A single consistency proof. Contains at most 3 proof nodes (all test proofs
// will be for a tree of size 8).
struct ConsistencyProofTestVector {
size_t old_tree_size;
size_t new_tree_size;
size_t proof_length;
const char* const proof[3];
};
// A collection of consistency proofs between various sub-trees of the sample
// tree.
constexpr auto kConsistencyProofs = std::to_array<ConsistencyProofTestVector>({
// Empty consistency proof between trees of the same size (1).
{1, 1, 0, {"", "", ""}},
// Consistency proof between tree of size 1 and tree of size 8, with 3
// nodes in the proof.
{1,
8,
3,
{"96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7",
"5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e",
"6b47aaf29ee3c2af9af889bc1fb9254dabd31177f16232dd6aab035ca39bf6e4"}},
// Consistency proof between tree of size 6 and tree of size 8, with 3
// nodes in the proof.
{6,
8,
3,
{"0ebc5d3437fbe2db158b9f126a1d118e308181031d0a949f8dededebc558ef6a",
"ca854ea128ed050b41b35ffc1b87b8eb2bde461e9e3b5596ece6b9d5975a0ae0",
"d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7"}},
// Consistency proof between tree of size 2 and tree of size 5, with 2
// nodes in the proof.
{2,
5,
2,
{"5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e",
"bc1a0643b12e4d2d7c77918f44e0f4f79a838b6cf9ec5b5c283e1f4d88599e6b", ""}},
});
// A single audit proof. Contains at most 3 proof nodes (all test proofs will be
// for a tree of size 8).
struct AuditProofTestVector {
size_t leaf;
size_t tree_size;
size_t proof_length;
const char* const proof[3];
};
// A collection of audit proofs for various leaves and sub-trees of the tree
// defined by |kRootHashes|.
constexpr auto kAuditProofs = std::to_array<AuditProofTestVector>({
{0, 1, 0, {"", "", ""}},
{0,
8,
3,
{"96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7",
"5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e",
"6b47aaf29ee3c2af9af889bc1fb9254dabd31177f16232dd6aab035ca39bf6e4"}},
{5,
8,
3,
{"bc1a0643b12e4d2d7c77918f44e0f4f79a838b6cf9ec5b5c283e1f4d88599e6b",
"ca854ea128ed050b41b35ffc1b87b8eb2bde461e9e3b5596ece6b9d5975a0ae0",
"d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7"}},
{2,
3,
1,
{"fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125", "",
""}},
{1,
5,
3,
{"6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
"5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e",
"bc1a0643b12e4d2d7c77918f44e0f4f79a838b6cf9ec5b5c283e1f4d88599e6b"}},
});
// Decodes a hexadecimal string into the binary data it represents.
std::string HexToBytes(const std::string& hex_data) {
std::string result;
if (!base::HexStringToString(hex_data, &result))
result.clear();
return result;
}
// Constructs a consistency/audit proof from a test vector.
// This is templated so that it can be used with both ConsistencyProofTestVector
// and AuditProofTestVector.
template <typename TestVectorType>
std::vector<std::string> GetProof(const TestVectorType& test_vector) {
std::vector<std::string> proof(test_vector.proof_length);
std::transform(test_vector.proof,
test_vector.proof + test_vector.proof_length, proof.begin(),
&HexToBytes);
return proof;
}
// Creates a ct::MerkleConsistencyProof from its arguments and returns the
// result of passing this to log.VerifyConsistencyProof().
bool VerifyConsistencyProof(const CTLogVerifier& log,
size_t old_tree_size,
const std::string& old_tree_root,
size_t new_tree_size,
const std::string& new_tree_root,
const std::vector<std::string>& proof) {
return log.VerifyConsistencyProof(
ct::MerkleConsistencyProof(log.key_id(), proof, old_tree_size,
new_tree_size),
old_tree_root, new_tree_root);
}
// Creates a ct::MerkleAuditProof from its arguments and returns the result of
// passing this to log.VerifyAuditProof().
bool VerifyAuditProof(const CTLogVerifier& log,
size_t leaf,
size_t tree_size,
const std::vector<std::string>& proof,
const std::string& tree_root,
const std::string& leaf_hash) {
return log.VerifyAuditProof(ct::MerkleAuditProof(leaf, tree_size, proof),
tree_root, leaf_hash);
}
class CTLogVerifierTest : public ::testing::Test {
public:
void SetUp() override {
log_ = CTLogVerifier::Create(ct::GetTestPublicKey(), "testlog");
ASSERT_TRUE(log_);
EXPECT_EQ(ct::GetTestPublicKeyId(), log_->key_id());
}
protected:
scoped_refptr<const CTLogVerifier> log_;
};
// Given an audit proof for a leaf in a Merkle tree, asserts that it verifies
// and no other combination of leaves, tree sizes and proof nodes verifies.
void CheckVerifyAuditProof(const CTLogVerifier& log,
size_t leaf,
size_t tree_size,
const std::vector<std::string>& proof,
const std::string& root_hash,
const std::string& leaf_hash) {
EXPECT_TRUE(
VerifyAuditProof(log, leaf, tree_size, proof, root_hash, leaf_hash))
<< "proof for leaf " << leaf << " did not pass verification";
EXPECT_FALSE(
VerifyAuditProof(log, leaf - 1, tree_size, proof, root_hash, leaf_hash))
<< "proof passed verification with wrong leaf index";
EXPECT_FALSE(
VerifyAuditProof(log, leaf + 1, tree_size, proof, root_hash, leaf_hash))
<< "proof passed verification with wrong leaf index";
EXPECT_FALSE(
VerifyAuditProof(log, leaf ^ 2, tree_size, proof, root_hash, leaf_hash))
<< "proof passed verification with wrong leaf index";
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size * 2, proof, root_hash, leaf_hash))
<< "proof passed verification with wrong tree height";
EXPECT_FALSE(VerifyAuditProof(log, leaf / 2, tree_size / 2, proof, root_hash,
leaf_hash))
<< "proof passed verification with wrong leaf index and tree height";
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size / 2, proof, root_hash, leaf_hash))
<< "proof passed verification with wrong tree height";
EXPECT_FALSE(VerifyAuditProof(log, leaf, tree_size, proof, GetEmptyTreeHash(),
leaf_hash))
<< "proof passed verification with wrong root hash";
std::vector<std::string> wrong_proof;
// Modify a single element on the proof.
for (size_t j = 0; j < proof.size(); ++j) {
wrong_proof = proof;
wrong_proof[j] = GetEmptyTreeHash();
EXPECT_FALSE(VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash,
leaf_hash))
<< "proof passed verification with one wrong node (node " << j << ")";
}
wrong_proof = proof;
wrong_proof.emplace_back();
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash, leaf_hash))
<< "proof passed verification with an empty node appended";
wrong_proof.back() = root_hash;
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash, leaf_hash))
<< "proof passed verification with an incorrect node appended";
wrong_proof.pop_back();
if (!wrong_proof.empty()) {
wrong_proof.pop_back();
EXPECT_FALSE(VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash,
leaf_hash))
<< "proof passed verification with the last node missing";
}
wrong_proof.clear();
wrong_proof.emplace_back();
wrong_proof.insert(wrong_proof.end(), proof.begin(), proof.end());
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash, leaf_hash))
<< "proof passed verification with an empty node prepended";
wrong_proof[0] = root_hash;
EXPECT_FALSE(
VerifyAuditProof(log, leaf, tree_size, wrong_proof, root_hash, leaf_hash))
<< "proof passed verification with an incorrect node prepended";
}
// Given a consistency proof between two snapshots of the tree, asserts that it
// verifies and no other combination of tree sizes and proof nodes verifies.
void CheckVerifyConsistencyProof(const CTLogVerifier& log,
int old_tree_size,
int new_tree_size,
const std::string& old_root,
const std::string& new_root,
const std::vector<std::string>& proof) {
// Verify the original consistency proof.
EXPECT_TRUE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, proof))
<< "proof between trees of size " << old_tree_size << " and "
<< new_tree_size << " did not pass verification";
if (proof.empty()) {
// For simplicity test only non-trivial proofs that have old_root !=
// new_root
// old_tree_size != 0 and old_tree_size != new_tree_size.
return;
}
// Wrong tree size: The proof checking code should not accept as a valid proof
// a proof for a tree size different than the original size it was produced
// for. Test that this is not the case for off-by-one changes.
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size - 1, old_root,
new_tree_size, new_root, proof))
<< "proof passed verification with old tree size - 1";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size + 1, old_root,
new_tree_size, new_root, proof))
<< "proof passed verification with old tree size + 1";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size ^ 2, old_root,
new_tree_size, new_root, proof))
<< "proof passed verification with old tree size ^ 2";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size * 2, new_root, proof))
<< "proof passed verification with new tree height + 1";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size / 2, new_root, proof))
<< "proof passed verification with new tree height - 1";
const std::string wrong_root("WrongRoot");
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, wrong_root, proof))
<< "proof passed verification with wrong old root";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, wrong_root,
new_tree_size, new_root, proof))
<< "proof passed verification with wrong new root";
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, new_root,
new_tree_size, old_root, proof))
<< "proof passed verification with old and new root swapped";
// Variations of wrong proofs, all of which should be rejected.
std::vector<std::string> wrong_proof;
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "empty proof passed verification";
// Modify a single element in the proof.
for (size_t j = 0; j < proof.size(); ++j) {
wrong_proof = proof;
wrong_proof[j] = GetEmptyTreeHash();
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with incorrect node (node " << j << ")";
}
wrong_proof = proof;
wrong_proof.emplace_back();
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with empty node appended";
wrong_proof.back() = proof.back();
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with last node duplicated";
wrong_proof.pop_back();
wrong_proof.pop_back();
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with last node missing";
wrong_proof.clear();
wrong_proof.emplace_back();
wrong_proof.insert(wrong_proof.end(), proof.begin(), proof.end());
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with empty node prepended";
wrong_proof[0] = proof[0];
EXPECT_FALSE(VerifyConsistencyProof(log, old_tree_size, old_root,
new_tree_size, new_root, wrong_proof))
<< "proof passed verification with first node duplicated";
}
TEST_F(CTLogVerifierTest, VerifiesCertSCT) {
ct::SignedEntryData cert_entry;
ct::GetX509CertSignedEntry(&cert_entry);
scoped_refptr<ct::SignedCertificateTimestamp> cert_sct;
ct::GetX509CertSCT(&cert_sct);
EXPECT_TRUE(log_->Verify(cert_entry, *cert_sct.get()));
}
TEST_F(CTLogVerifierTest, VerifiesPrecertSCT) {
ct::SignedEntryData precert_entry;
ct::GetPrecertSignedEntry(&precert_entry);
scoped_refptr<ct::SignedCertificateTimestamp> precert_sct;
ct::GetPrecertSCT(&precert_sct);
EXPECT_TRUE(log_->Verify(precert_entry, *precert_sct.get()));
}
TEST_F(CTLogVerifierTest, FailsInvalidTimestamp) {
ct::SignedEntryData cert_entry;
ct::GetX509CertSignedEntry(&cert_entry);
scoped_refptr<ct::SignedCertificateTimestamp> cert_sct;
ct::GetX509CertSCT(&cert_sct);
// Mangle the timestamp, so that it should fail signature validation.
cert_sct->timestamp = base::Time::Now();
EXPECT_FALSE(log_->Verify(cert_entry, *cert_sct.get()));
}
TEST_F(CTLogVerifierTest, FailsInvalidLogID) {
ct::SignedEntryData cert_entry;
ct::GetX509CertSignedEntry(&cert_entry);
scoped_refptr<ct::SignedCertificateTimestamp> cert_sct;
ct::GetX509CertSCT(&cert_sct);
// Mangle the log ID, which should cause it to match a different log before
// attempting signature validation.
cert_sct->log_id.assign(cert_sct->log_id.size(), '\0');
EXPECT_FALSE(log_->Verify(cert_entry, *cert_sct.get()));
}
TEST_F(CTLogVerifierTest, VerifiesValidSTH) {
ct::SignedTreeHead sth;
ASSERT_TRUE(ct::GetSampleSignedTreeHead(&sth));
EXPECT_TRUE(log_->VerifySignedTreeHead(sth));
}
TEST_F(CTLogVerifierTest, DoesNotVerifyInvalidSTH) {
ct::SignedTreeHead sth;
ASSERT_TRUE(ct::GetSampleSignedTreeHead(&sth));
sth.sha256_root_hash[0] = '\x0';
EXPECT_FALSE(log_->VerifySignedTreeHead(sth));
}
TEST_F(CTLogVerifierTest, VerifiesValidEmptySTH) {
ct::SignedTreeHead sth;
ASSERT_TRUE(ct::GetSampleEmptySignedTreeHead(&sth));
EXPECT_TRUE(log_->VerifySignedTreeHead(sth));
}
TEST_F(CTLogVerifierTest, DoesNotVerifyInvalidEmptySTH) {
ct::SignedTreeHead sth;
ASSERT_TRUE(ct::GetBadEmptySignedTreeHead(&sth));
EXPECT_FALSE(log_->VerifySignedTreeHead(sth));
}
// Test that excess data after the public key is rejected.
TEST_F(CTLogVerifierTest, ExcessDataInPublicKey) {
std::string key = ct::GetTestPublicKey();
key += "extra";
scoped_refptr<const CTLogVerifier> log =
CTLogVerifier::Create(key, "testlog");
EXPECT_FALSE(log);
}
TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_EmptyProof) {
std::vector<std::string> empty_proof;
std::string old_root(GetEmptyTreeHash()), new_root(GetEmptyTreeHash());
// Tree snapshots that are always consistent, because the proofs are either
// from an empty tree to a non-empty one or for trees of the same size.
EXPECT_TRUE(
VerifyConsistencyProof(*log_, 0, old_root, 0, new_root, empty_proof));
EXPECT_TRUE(
VerifyConsistencyProof(*log_, 0, old_root, 1, new_root, empty_proof));
EXPECT_TRUE(
VerifyConsistencyProof(*log_, 1, old_root, 1, new_root, empty_proof));
// Invalid consistency proofs.
// Time travel to the past.
EXPECT_FALSE(
VerifyConsistencyProof(*log_, 1, old_root, 0, new_root, empty_proof));
EXPECT_FALSE(
VerifyConsistencyProof(*log_, 2, old_root, 1, new_root, empty_proof));
// Proof between two trees of different size can never be empty.
EXPECT_FALSE(
VerifyConsistencyProof(*log_, 1, old_root, 2, new_root, empty_proof));
}
TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_MismatchingRoots) {
const std::string old_root(GetEmptyTreeHash());
std::string new_root;
std::vector<std::string> empty_proof;
// Roots don't match.
EXPECT_FALSE(
VerifyConsistencyProof(*log_, 0, old_root, 0, new_root, empty_proof));
EXPECT_FALSE(
VerifyConsistencyProof(*log_, 1, old_root, 1, new_root, empty_proof));
}
TEST_F(CTLogVerifierTest,
VerifiesConsistencyProofEdgeCases_MatchingRootsNonEmptyProof) {
const std::string empty_tree_hash(GetEmptyTreeHash());
std::vector<std::string> proof;
proof.push_back(empty_tree_hash);
// Roots match and the tree size is either the same or the old tree size is 0,
// but the proof is not empty (the verification code should not accept
// proofs with redundant nodes in this case).
proof.push_back(empty_tree_hash);
EXPECT_FALSE(VerifyConsistencyProof(*log_, 0, empty_tree_hash, 0,
empty_tree_hash, proof));
EXPECT_FALSE(VerifyConsistencyProof(*log_, 0, empty_tree_hash, 1,
empty_tree_hash, proof));
EXPECT_FALSE(VerifyConsistencyProof(*log_, 1, empty_tree_hash, 1,
empty_tree_hash, proof));
}
class CTLogVerifierConsistencyProofTest
: public CTLogVerifierTest,
public ::testing::WithParamInterface<size_t /* proof index */> {};
// Checks that a sample set of valid consistency proofs verify successfully.
TEST_P(CTLogVerifierConsistencyProofTest, VerifiesValidConsistencyProof) {
const ConsistencyProofTestVector& test_vector =
kConsistencyProofs[GetParam()];
const std::vector<std::string> proof = GetProof(test_vector);
const char* const old_root = kRootHashes[test_vector.old_tree_size - 1];
const char* const new_root = kRootHashes[test_vector.new_tree_size - 1];
CheckVerifyConsistencyProof(*log_, test_vector.old_tree_size,
test_vector.new_tree_size, HexToBytes(old_root),
HexToBytes(new_root), proof);
}
INSTANTIATE_TEST_SUITE_P(KnownGoodProofs,
CTLogVerifierConsistencyProofTest,
::testing::Range(size_t(0),
std::size(kConsistencyProofs)));
class CTLogVerifierAuditProofTest
: public CTLogVerifierTest,
public ::testing::WithParamInterface<size_t /* proof index */> {};
// Checks that a sample set of valid audit proofs verify successfully.
TEST_P(CTLogVerifierAuditProofTest, VerifiesValidAuditProofs) {
const AuditProofTestVector& test_vector = kAuditProofs[GetParam()];
const std::vector<std::string> proof = GetProof(test_vector);
const char* const root_hash = kRootHashes[test_vector.tree_size - 1];
CheckVerifyAuditProof(*log_, test_vector.leaf, test_vector.tree_size, proof,
HexToBytes(root_hash),
HexToBytes(kLeafHashes[test_vector.leaf]));
}
INSTANTIATE_TEST_SUITE_P(KnownGoodProofs,
CTLogVerifierAuditProofTest,
::testing::Range(size_t(0), std::size(kAuditProofs)));
TEST_F(CTLogVerifierTest, VerifiesAuditProofEdgeCases_InvalidLeafIndex) {
std::vector<std::string> proof;
EXPECT_FALSE(
VerifyAuditProof(*log_, 1, 0, proof, std::string(), std::string()));
EXPECT_FALSE(
VerifyAuditProof(*log_, 2, 1, proof, std::string(), std::string()));
const std::string empty_hash = GetEmptyTreeHash();
EXPECT_FALSE(VerifyAuditProof(*log_, 1, 0, proof, empty_hash, std::string()));
EXPECT_FALSE(VerifyAuditProof(*log_, 2, 1, proof, empty_hash, std::string()));
}
// Functions that implement algorithms from RFC6962 necessary for constructing
// Merkle trees and proofs. This allows tests to generate a variety of trees
// for exhaustive testing.
namespace rfc6962 {
// Calculates the hash of a leaf in a Merkle tree, given its content.
// See RFC6962, section 2.1.
std::string HashLeaf(const std::string& leaf) {
const char kLeafPrefix[] = {'\x00'};
SHA256HashValue sha256 = {0};
std::unique_ptr<crypto::SecureHash> hash(
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
hash->Update(kLeafPrefix, 1);
hash->Update(leaf.data(), leaf.size());
hash->Finish(sha256);
return std::string(base::as_string_view(sha256));
}
// Calculates the root hash of a Merkle tree, given its leaf data and size.
// See RFC6962, section 2.1.
std::string HashTree(std::string leaves[], size_t tree_size) {
if (tree_size == 0)
return GetEmptyTreeHash();
if (tree_size == 1)
return HashLeaf(leaves[0]);
// Find the index of the last leaf in the left sub-tree.
const size_t split = std::bit_floor(tree_size - 1);
// Hash the left and right sub-trees, then hash the results.
return ct::internal::HashNodes(HashTree(leaves, split),
HashTree(&leaves[split], tree_size - split));
}
// Returns a Merkle audit proof for the leaf with index |leaf_index|.
// The tree consists of |leaves[0]| to |leaves[tree_size-1]|.
// If |leaf_index| is >= |tree_size|, an empty proof will be returned.
// See RFC6962, section 2.1.1, for more details.
std::vector<std::string> CreateAuditProof(std::string leaves[],
size_t tree_size,
size_t leaf_index) {
std::vector<std::string> proof;
if (leaf_index >= tree_size)
return proof;
if (tree_size == 1)
return proof;
// Find the index of the first leaf in the right sub-tree.
const size_t split = std::bit_floor(tree_size - 1);
// Recurse down the correct branch of the tree (left or right) to reach the
// leaf with |leaf_index|. Add the hash of the branch not taken at each step
// on the way up to build the proof.
if (leaf_index < split) {
proof = CreateAuditProof(leaves, split, leaf_index);
proof.push_back(HashTree(&leaves[split], tree_size - split));
} else {
proof =
CreateAuditProof(&leaves[split], tree_size - split, leaf_index - split);
proof.push_back(HashTree(leaves, split));
}
return proof;
}
// Returns a Merkle consistency proof between two Merkle trees.
// The old tree contains |leaves[0]| to |leaves[old_tree_size-1]|.
// The new tree contains |leaves[0]| to |leaves[new_tree_size-1]|.
// Call with |contains_old_tree| = true.
// See RFC6962, section 2.1.2, for more details.
std::vector<std::string> CreateConsistencyProof(std::string leaves[],
size_t new_tree_size,
size_t old_tree_size,
bool contains_old_tree = true) {
std::vector<std::string> proof;
if (old_tree_size == 0 || old_tree_size > new_tree_size)
return proof;
if (old_tree_size == new_tree_size) {
// Consistency proof for two equal subtrees is empty.
if (!contains_old_tree) {
// Record the hash of this subtree unless it's the root for which
// the proof was originally requested. (This happens when the old tree is
// balanced).
proof.push_back(HashTree(leaves, old_tree_size));
}
return proof;
}
// Find the index of the last leaf in the left sub-tree.
const size_t split = std::bit_floor(new_tree_size - 1);
if (old_tree_size <= split) {
// Root of the old tree is in the left subtree of the new tree.
// Prove that the left subtrees are consistent.
proof =
CreateConsistencyProof(leaves, split, old_tree_size, contains_old_tree);
// Record the hash of the right subtree (only present in the new tree).
proof.push_back(HashTree(&leaves[split], new_tree_size - split));
} else {
// The old tree root is at the same level as the new tree root.
// Prove that the right subtrees are consistent. The right subtree
// doesn't contain the root of the old tree, so set contains_old_tree =
// false.
proof = CreateConsistencyProof(&leaves[split], new_tree_size - split,
old_tree_size - split,
/* contains_old_tree = */ false);
// Record the hash of the left subtree (equal in both trees).
proof.push_back(HashTree(leaves, split));
}
return proof;
}
} // namespace rfc6962
class CTLogVerifierTestUsingGenerator
: public CTLogVerifierTest,
public ::testing::WithParamInterface<size_t /* tree_size */> {};
// Checks that valid consistency proofs for a range of generated Merkle trees
// verify successfully.
TEST_P(CTLogVerifierTestUsingGenerator, VerifiesValidConsistencyProof) {
const size_t tree_size = GetParam();
std::vector<std::string> tree_leaves(tree_size);
for (size_t i = 0; i < tree_size; ++i)
tree_leaves[i].push_back(static_cast<char>(i));
const std::string tree_root =
rfc6962::HashTree(tree_leaves.data(), tree_size);
// Check consistency proofs for every sub-tree.
for (size_t old_tree_size = 0; old_tree_size <= tree_size; ++old_tree_size) {
SCOPED_TRACE(old_tree_size);
const std::string old_tree_root =
rfc6962::HashTree(tree_leaves.data(), old_tree_size);
const std::vector<std::string> proof = rfc6962::CreateConsistencyProof(
tree_leaves.data(), tree_size, old_tree_size);
// Checks that the consistency proof verifies only with the correct tree
// sizes and root hashes.
CheckVerifyConsistencyProof(*log_, old_tree_size, tree_size, old_tree_root,
tree_root, proof);
}
}
// Checks that valid audit proofs for a range of generated Merkle trees verify
// successfully.
TEST_P(CTLogVerifierTestUsingGenerator, VerifiesValidAuditProofs) {
const size_t tree_size = GetParam();
std::vector<std::string> tree_leaves(tree_size);
for (size_t i = 0; i < tree_size; ++i)
tree_leaves[i].push_back(static_cast<char>(i));
const std::string root = rfc6962::HashTree(tree_leaves.data(), tree_size);
// Check audit proofs for every leaf in the tree.
for (size_t leaf = 0; leaf < tree_size; ++leaf) {
SCOPED_TRACE(leaf);
std::vector<std::string> proof =
rfc6962::CreateAuditProof(tree_leaves.data(), tree_size, leaf);
// Checks that the audit proof verifies only for this leaf data, index,
// hash, tree size and root hash.
CheckVerifyAuditProof(*log_, leaf, tree_size, proof, root,
rfc6962::HashLeaf(tree_leaves[leaf]));
}
}
// Test verification of consistency proofs and audit proofs for all tree sizes
// from 0 to 128.
INSTANTIATE_TEST_SUITE_P(RangeOfTreeSizes,
CTLogVerifierTestUsingGenerator,
testing::Range(size_t(0), size_t(129)));
} // namespace
} // namespace net
|