Check a certificate

Before trusting a server, a browser checks that its certificate is valid today and that it was issued for the name in the address bar.

What to build

Check certificates against a date and a host name.

Requirements

  • Each line is today notBefore notAfter certName host, with dates as YYYY-MM-DD.
  • If today is before notBefore, print not yet valid; if after notAfter, print expired (both dates are inclusive).
  • Otherwise check the name: an exact match (ignoring case) is valid. A name starting with *. matches exactly one extra label at the left: *.example.com matches www.example.com but not example.com or a.b.example.com.
  • Print valid or name mismatch.

Input

One certificate per line.

Output

One word or phrase per line.

Constraints

Up to 50 lines. Names contain only letters, digits, dots, hyphens and the leading *..

Example

Input:

2026-11-30 2026-09-01 2026-12-01 portal.example.edu portal.example.edu
2026-12-02 2026-09-01 2026-12-01 portal.example.edu portal.example.edu

Output:

valid
expired

Why it works

Dates in YYYY-MM-DD order compare correctly as text, and a wildcard covers exactly one label, so splitting the host at its first dot and comparing the rest with the certificate's suffix decides the name.

Examples

Example 1 · Input
2026-11-30 2026-09-01 2026-12-01 portal.example.edu portal.example.edu
2026-12-02 2026-09-01 2026-12-01 portal.example.edu portal.example.edu
Expected output
valid
expired
Example 2 · Input
2026-11-30 2026-09-01 2026-12-01 *.example.com www.example.com
2026-11-30 2026-09-01 2026-12-01 *.example.com example.com
Expected output
valid
name mismatch

Hints

Hint 1

"2026-11-30" < "2026-12-01" is true: compare the date strings directly.

Hint 2

For a wildcard, the host must contain a dot, and host.slice(host.indexOf(".")) must equal certName.slice(1).

Solution

Show one correct solution (try it yourself first)
function nameMatches(name, host) {
  name = name.toLowerCase();
  host = host.toLowerCase();
  if (!name.startsWith("*.")) return name === host;
  const dot = host.indexOf(".");
  if (dot <= 0) return false;
  return host.slice(dot) === name.slice(1);
}
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const [today, from, to, name, host] = line.trim().split(/\s+/);
    if (today < from) console.log("not yet valid");
    else if (today > to) console.log("expired");
    else console.log(nameMatches(name, host) ? "valid" : "name mismatch");
  }
  line = prompt();
}

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

Lesson: The TLS handshake →