Walk a certificate chain

A browser trusts a site only if it can walk from the site's certificate, issuer by issuer, to a root it already trusts, with every signature good along the way.

What to build

Check certificate chains and report where each one ends.

Requirements

  • Line 1: the trusted roots' names, separated by spaces. Line 2: N. Then N lines subject issuer ok or subject issuer bad. Each remaining line is a name to check.
  • Start at the name and repeat: if there is no certificate for it, stop with unknown certificate; if its signature is bad, stop with bad signature; if it is a trusted root, stop with trusted; if it issued itself and is not trusted, stop with untrusted root; if its issuer is already on the path, stop with loop; otherwise move to its issuer.
  • Print the path joined by <-, a colon, a space and the result. For unknown certificate the path ends with the unknown name.

Input

As described. Names contain no spaces.

Output

One line per name checked.

Constraints

Up to 50 certificates and 20 names to check.

Example

Input:

RootCA
3
portal.edu CampusCA ok
CampusCA RootCA ok
RootCA RootCA ok
portal.edu

Output:

portal.edu <- CampusCA <- RootCA: trusted

Why it works

Each step trusts a certificate only because the one above it signed it, so the chain is trustworthy exactly when every link is good and it ends at a root the browser already trusts; the loop check stops a chain that never reaches a root.

Examples

Example 1 · Input
RootCA
3
portal.edu CampusCA ok
CampusCA RootCA ok
RootCA RootCA ok
portal.edu
Expected output
portal.edu <- CampusCA <- RootCA: trusted
Example 2 · Input
RootCA
2
shop.com MysteryCA ok
RootCA RootCA ok
shop.com
Expected output
shop.com <- MysteryCA: unknown certificate

Hints

Hint 1

Keep the certificates in an object keyed by subject.

Hint 2

Check the stop rules in the order given; the order matters when a root's own signature is bad.

Solution

Show one correct solution (try it yourself first)
const roots = new Set(prompt().trim().split(/\s+/));
const n = Number(prompt());
const certs = {};
for (let k = 0; k < n; k++) {
  const [subject, issuer, status] = prompt().trim().split(/\s+/);
  certs[subject] = { issuer, ok: status === "ok" };
}
function check(name) {
  const path = [];
  let current = name;
  while (true) {
    const cert = certs[current];
    if (!cert) return `${[...path, current].join(" <- ")}: unknown certificate`;
    path.push(current);
    if (!cert.ok) return `${path.join(" <- ")}: bad signature`;
    if (roots.has(current)) return `${path.join(" <- ")}: trusted`;
    if (cert.issuer === current) return `${path.join(" <- ")}: untrusted root`;
    if (path.includes(cert.issuer)) return `${path.join(" <- ")}: loop`;
    current = cert.issuer;
  }
}
let line = prompt();
while (line !== null) {
  if (line.trim()) console.log(check(line.trim()));
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Digital signatures and certificates →