Trending

World technology news

→
Certificate Transparency in RFC 9162: verifying a Merkle tree entry
Software Engineering · 2021
Software Engineering2021

Certificate Transparency in RFC 9162: verifying a Merkle tree entry

Explore the inclusion proof from the 2021 experimental CT 2.0 specification in a five-entry Node.js example. Local tests reject changed data, a wrong index and a truncated proof.

Archive retrospective · Topic year 2021

Developer's JournalPublished: Dec 5, 20266 min read
Reading progress0%
In this article
  1. Showing that a certificate is visible
  2. Order, tree size and two hash inputs
  3. Run the exercise locally
  4. Where the trusted root comes from
  5. Comparing two versions of the tree
  6. Integrating with certificate monitoring

Showing that a certificate is visible

When a domain operator suspects someone has issued a certificate without their knowledge, they need observable records. Certificate Transparency describes public append-only logs of TLS server certificates and precertificates. A monitor can inspect new entries for unexpected names. A client with a single entry can verify its inclusion in a specific tree with a much smaller proof. Experimental RFC 9162, published in December 2021, describes version 2.0 of that protocol. Its publication status and relationship to the earlier RFC do not by themselves identify the policies of a browser today. For a deployed client, check the accepted protocol version, configured log keys and certificate policy separately. The local exercise follows the hash-tree rules without uploading an actual certificate. It uses five invented entries so a reader can reproduce the data and test outcome without a network connection or third-party account.

Order, tree size and two hash inputs

The records in the tree are ordered. A leaf hash starts with the byte 00, and an internal node starts with 01. This separates leaf data from the concatenation of the digests of two child nodes. Changing the order of the children changes the root. There is no duplication of leaves to fill a tree whose size is not a power of two. In a five-entry tree, the root joins the subtree of the first four entries with the single remaining leaf. The left side then divides into two pairs. Index three selects the fourth entry under zero-based indexing. Its proof includes sibling hashes outside the path from that entry to the root. The verifier works out the direction of each sibling from the position and tree size. An API that presents these values separately needs to keep them tied to the same snapshot.

Application flowConceptual sketch · summary of the article's technical flow

Run the exercise locally

The example uses only the cryptography and assertion modules included with Node.js. It computes a root for five entries and produces the path for the fourth. Verification starts by hashing the entry. The path is consumed from the bottom, one sibling at a time. A valid path reconstructs the chosen root and consumes each hash exactly once. The exercise repeats the check with different data, then with a different index, then with the final sibling removed. The live run returns true for the original entry, false for each of the three altered cases, and reports three sibling digests. These checks demonstrate that a proof carries context as well as hash values. The script does not parse a signed tree head or establish a TLS connection. Running it on a modern Node.js runtime reproduces the calculation defined by the 2021 document.

import assert from 'node:assert/strict';
import {createHash} from 'node:crypto';

function hash(prefix, ...parts) {
  const h = createHash('sha256').update(Buffer.from([prefix]));
  for (const part of parts) h.update(part);
  return h.digest();
}
function split(size) {
  return 2 ** Math.floor(Math.log2(size - 1));
}
function root(entries) {
  if (entries.length === 1) return hash(0, entries[0]);
  const left = split(entries.length);
  return hash(1, root(entries.slice(0, left)), root(entries.slice(left)));
}
function proof(entries, index) {
  if (entries.length === 1) return [];
  const left = split(entries.length);
  if (index < left) return [...proof(entries.slice(0, left), index), root(entries.slice(left))];
  return [...proof(entries.slice(left), index - left), root(entries.slice(0, left))];
}
function verify(entry, index, size, siblings, expected) {
  if (!Number.isSafeInteger(index) || !Number.isSafeInteger(size) || index < 0 || index >= size) return false;
  let used = 0;
  function rebuild(offset, length) {
    if (length === 1) return hash(0, entry);
    const leftSize = split(length);
    const onLeft = offset < leftSize;
    const child = onLeft ? rebuild(offset, leftSize) : rebuild(offset - leftSize, length - leftSize);
    const sibling = siblings[used++];
    if (!child || sibling?.length !== 32) return null;
    return onLeft ? hash(1, child, sibling) : hash(1, sibling, child);
  }
  const actual = rebuild(index, size);
  return Boolean(actual && used === siblings.length && actual.equals(expected));
}

const entries = ['orders','catalog','tracking','returns','billing'].map(name => Buffer.from(name));
const checkpoint = root(entries);
const path = proof(entries, 3);
const results = {
  valid: verify(entries[3], 3, entries.length, path, checkpoint),
  changed: verify(Buffer.from('returns-alt'), 3, entries.length, path, checkpoint),
  wrongIndex: verify(entries[3], 2, entries.length, path, checkpoint),
  truncated: verify(entries[3], 3, entries.length, path.slice(0,-1), checkpoint),
  siblings: path.length,
};
assert.deepEqual(results, {valid:true, changed:false, wrongIndex:false, truncated:false, siblings:3});
console.log(JSON.stringify(results));

Where the trusted root comes from

If a service offers both an entry and a matching root, a passing Merkle calculation provides little assurance on its own. That service could construct a new tree around any data. A real verifier ties the size and root to a tree head authenticated under a known log key. It then checks that the head belongs to the intended log and observation window. The demonstration chooses its root in advance. A deployment instead relies on its verified checkpoint and log configuration. A signed certificate timestamp commits a log to including an accepted entry. An inclusion proof shows that the entry is in a particular tree. Record which one was checked, along with the available timestamps. An entry that has not yet appeared and an invalid proof for an observed entry need different handling.

Comparing two versions of the tree

An inclusion proof establishes membership in one snapshot. If a log grows from thirty to forty entries, keeping the original thirty entries intact is a separate property. A consistency proof combines hashes of shared subtrees to link the two snapshots. A monitor should retain its previous authenticated tree head for that comparison. After a transient network failure, the most recent verified head and observation time provide a useful starting point. When a new head becomes available, check the log identity and both tree sizes alongside the proof. Independent observations can help detect disagreements that a single server's replies will not reveal. Keeping inclusion and consistency as separate operations also makes it easier to test how each component responds to malformed data and to give each failure a meaningful audit record.

Integrating with certificate monitoring

Start with the set of names the team owns, the intended coverage of subdomains, and the approved issuance workflow. Store the observed entry with its log identity and authenticated tree size. When a proof fails, capture the index and path length without claiming that the certificate has been misissued. The investigation needs to establish what the certificate covers and whether it corresponds to an authorized request. Extend the exercise with an empty tree, a single leaf, several tree sizes and indexes outside the valid range. A certificate recorded in a log still has to meet ordinary chain, identity and validity checks. Teams get more value by keeping the evidence of visibility together with their issuance records and by connecting a monitoring alert to a person who can investigate. The small fixture can serve as a regression vector after a library upgrade. Keep the input bytes next to their expected outcome so future maintainers can tell whether a failure reflects a real change or a mistaken assumption about how the fixture was encoded.

873 reads ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.