package main
// Exhibit 27. A browser that never opened your sealed postcard gets in only two ways: a code it shows,
// typed on a device that already can (both ends check an HMAC keyed by the code, so the server can
// neither swap the new device's key nor hand it a key of its own), or your recovery key (in storage's
// window, so the root never reaches this page). rc-seal.js does both; the card uses exhibit 26's
// instance. The self-check plays three of the robot's browsers, and the server twice.
import "core:encoding/json"
import "core:fmt"
import "core:strings"
import "ratcloud:rc"
e27 :: proc() -> Exhibit {
return {
n = 27, wing = "Sealed",
title = "A new device needs an old one",
promise = "A device that never opened your sealed files gets in only with a code typed on one that has, or with your recovery key.",
block = "rc-seal.js: seal.request() shows a code, seal.approve(code) on a device that has the key lets the new one in, seal.useStorage() lets storage's window do it with your recovery key; every keyring is checked under the key it guards.",
src = {"e27_new_device.odin", #load("e27_new_device.odin", string)},
files = srcs({"robotseal.odin", #load("robotseal.odin", string)}, {"seal.odin", #load("seal.odin", string)}),
script = {"web/e27.js", #load("web/e27.js", string)},
show = proc(me: ^rc.User) -> string {
if me == nil do return signin_line(", seal a postcard (26), then open this page in a private window.")
return `
`
},
check = proc() -> Check {
s, err := robot_sealing()
if err != "" do return fail(err)
kA, kr, kerr := robot_zoo_key(&s)
if kerr != "" do return fail(kerr)
words := postcard_words()
if _, _, perr := robot_seal_postcard(kA, words); perr != "" do return fail(perr)
steps := make([dynamic]string)
// B: a browser that never opened it. It shows a code and asks.
bk := ec_new()
B := Dev{id = b64u(random_bytes(18)), priv = bk, pub = b64(bk.pub), code = new_code()}
defer {
// B leaves again, so the robot's keyring does not grow every five minutes.
if k2, kr2, e2 := robot_zoo_key(&s); e2 == "" {
keep := make([dynamic]Kr_Device)
for d in kr2.devices do if d.id != B.id do append(&keep, d)
if len(keep) != len(kr2.devices) do robot_zoo_write(&s, k2, kr2, keep[:])
}
robot_zoo_storage("keyring", fmt.tprintf(`{{"op":"decline","id":%s}}`, jq(B.id)))
}
if _, e := robot_zoo_storage("keyring", fmt.tprintf(`{{"op":"request","id":%s,"name":"Robot's new device","pub":%s,"tag":%s}}`, jq(B.id), jq(B.pub), jq(request_tag(B.code, B.id, B.pub)))); e != "" do return fail(e)
if _, terr := trust_keyring(kr, B, s.user, "zoo"); terr == "" do return fail("a device that was never let in opened the keyring")
_, held, herr := robot_postcard_bytes()
if herr != "" do return fail(herr)
none :: proc(id: string, arg: rawptr) -> []u8 {return nil}
if _, e := envelope_open(none, nil, held); e == "" do return fail("the postcard opened with no key")
append(&steps, "locked, showing a code")
// A, the robot's first device, is told a wrong code: no waiting device matches it.
g, gerr := robot_zoo_storage("keyring", `{"op":"get","fresh":true}`)
if gerr != "" do return fail(gerr)
reqs := jarr(g, "requests")
q_i := -1
for q, i in reqs do if jstr(q, "id") == B.id do q_i = i
if q_i < 0 do return fail("storage does not show the new device's request")
wrong := new_code()
for q in reqs do if check_request(wrong, q) do return fail("a wrong code matched a waiting device")
append(&steps, "a wrong code refused")
q := reqs[q_i]
// The server swaps the waiting device's public key for one of its own (the check plays the server).
swapped := parse(to_json(q))
if o, ok := swapped.(json.Object); ok do o["pub"] = json.String(b64(ec_new().pub))
if check_request(B.code, swapped) do return fail("a key the server swapped into the request was accepted")
append(&steps, "a key the server swapped into the request refused")
// The right code: A wraps the zoo's key for B and vouches for it under the code.
if !check_request(B.code, q) do return fail("the right code did not match the request")
w, _ := wrap_for_device(jstr(q, "pub"), kA)
devs := make([dynamic]Kr_Device)
append(&devs, ..kr.devices)
append(&devs, Kr_Device{id = jstr(q, "id"), name = jstr(q, "name"), pub = jstr(q, "pub"), wrap = w, ok = approval_tag(B.code, jstr(q, "id"), jstr(q, "pub"), w, kr.mk_id), created = now(), via = "approved"})
kr2, werr := robot_zoo_write(&s, kA, kr, devs[:])
if werr != "" do return fail(werr)
// The server hands B a wrap of a key of its own instead (keeping A's tag): refused.
fake := kr2
fake.devices = make([]Kr_Device, len(kr2.devices))
copy(fake.devices, kr2.devices)
for &d in fake.devices do if d.id == B.id {d.wrap, _ = wrap_for_device(B.pub, random_bytes(32))}
if _, e := trust_keyring(fake, B, s.user, "zoo"); e == "" do return fail("a wrap of a key the server chose was accepted")
append(&steps, "a key the server swapped in for the new device refused")
g2, g2err := robot_zoo_storage("keyring", `{"op":"get","fresh":true}`)
if g2err != "" do return fail(g2err)
kr3, _ := keyring_of(jget(g2, "keyring"))
kB, berr := trust_keyring(kr3, B, s.user, "zoo")
if berr != "" do return fail(cat("the new device, let in with the right code, is refused: ", berr))
_, held2, h2err := robot_postcard_bytes()
if h2err != "" do return fail(h2err)
Keys :: struct {
id: string,
k: []u8,
}
key_for :: proc(id: string, arg: rawptr) -> []u8 {x := (^Keys)(arg); return x.k if id == x.id else nil}
kb := Keys{kr3.mk_id, kB}
if back, e := envelope_open(key_for, &kb, held2); e != "" || string(back) != words do return fail("the new device opened other words")
append(&steps, "with the right code, the new device opens the postcard")
// C: nothing but the recovery key. In a browser this happens in storage's window.
rj, rerr := robot_storage_seal(`{"op":"get","fresh":true}`)
if rerr != "" do return fail(rerr)
root, rok := keyring_of(jget(rj, "keyring"))
if !rok || root.recovery == nil {
keys := make([dynamic]string)
if o, isobj := jget(rj, "keyring").(json.Object); isobj do for k in o do append(&keys, k)
return fail(cat("storage's keyring for the robot has no recovery entry (it has: ", strings.join(keys[:], ", "), "; it parsed: ", "yes" if rok else g_keyring_err, ")"))
}
R, uerr := recovery_unwrap(s.phrase, root.recovery^)
if uerr != "" do return fail(uerr)
if mk_id(R) != root.mk_id || !check_keyring(R, root, s.user, "-") do return fail("the recovery key does not open a keyring that checks out")
rwk, oerr := open_root_wrap_key(R, root.root)
if oerr != "" do return fail(oerr)
M, merr := unwrap_from_root(rwk, root.root.pub, "zoo", kr3.mk_id, kr3.root)
if merr != "" do return fail(merr)
if !check_keyring(M, kr3, s.user, "zoo") do return fail("the zoo's keyring does not check out under the key the recovery key opened")
km := Keys{kr3.mk_id, M}
if back, e := envelope_open(key_for, &km, held2); e != "" || string(back) != words do return fail("the recovery key opened other words")
append(&steps, "the recovery key opens it too")
return pass(cat("a new device of the robot's: ", strings.join(steps[:], "; ")), struct {
steps: []string `json:"steps"`,
}{steps[:]})
},
}
}