A browser trusts a website when its certificate chain leads to a trusted root and every certificate is valid today. Check a chain in a simplified model.
What to build
A program that follows a chain from a leaf certificate up and reports whether it is valid.
Requirements
- Each certificate has a name, an issuer and the first and last day it is valid (both included). A certificate whose issuer is its own name is self-signed.
- Start at the leaf and follow issuers. For each certificate, in order: if no certificate has that name, print
invalid: missing NAME; if today is outside its dates, printinvalid: expired NAME; if it is self-signed, accept the chain only when NAME is a trusted root, otherwise printinvalid: untrusted root NAME; else go on to its issuer. - If the chain is accepted print
valid chain K, where K is the number of certificates followed. If a name repeats before the chain ends, printinvalid: loop NAME.
Input
Line 1: today (a whole number). Line 2: the trusted root names, separated by spaces. Line 3: n. Then n lines name issuer from to. The last line is the leaf name to check.
Output
One line.
Constraints
1 ≤ n ≤ 20; names have no spaces; dates are whole numbers.
Example
Input:
100
RootCA
3
shop.test MidCA 50 200
MidCA RootCA 10 300
RootCA RootCA 0 1000
shop.test
Output:
valid chain 3
Why it works
Trust has to come from somewhere, so the chain must end in a root already known to the computer. Checking the dates and the existence of every certificate on the way is what makes an expired or missing intermediate fail the whole chain.
Examples
100 RootCA 3 shop.test MidCA 50 200 MidCA RootCA 10 300 RootCA RootCA 0 1000 shop.test
valid chain 3
250 RootCA 3 shop.test MidCA 50 200 MidCA RootCA 10 300 RootCA RootCA 0 1000 shop.test
invalid: expired shop.test
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Store each certificate by name: its issuer and its first and last valid day. Start at the leaf name.
Hint 2
At each step check in this order: the certificate exists, today is inside its dates (both ends included), then whether it signs itself (issuer equals name).
Hint 3
A self-signed certificate is accepted only if its name is in the list of trusted roots. Otherwise move to the issuer, and count the certificates passed.
Solution
Show one correct solution (try it yourself first)
today = int(input())
roots = input().split()
n = int(input())
certs = {}
for _ in range(n):
name, issuer, start, end = input().split()
certs[name] = (issuer, int(start), int(end))
leaf = input().strip()
seen = set()
name = leaf
count = 0
while True:
if name in seen:
print("invalid: loop " + name)
break
seen.add(name)
if name not in certs:
print("invalid: missing " + name)
break
issuer, start, end = certs[name]
count += 1
if today < start or today > end:
print("invalid: expired " + name)
break
if issuer == name:
if name in roots:
print("valid chain " + str(count))
else:
print("invalid: untrusted root " + name)
break
name = issuer
▶ Open this solution in the compiler, then press Visualize to watch it run step by step