package main // The robot's sealing, for the self-checks of exhibits 26 and 27. The robot plays a person's browsers // the way rc-crypt.js and rc-seal.js do in a real one (seal.odin has the same bytes): at storage // (/api/seal, with its storage sign-in), and at the zoo, whose server forwards to storage like any // app's (/api/26/storage). It holds what a person holds: each device's private key and the recovery // key, in robot-seal.json (the zoo's own data, like the robot's passkey), never a key that opens a // file; those are unwrapped again at every check. import "core:encoding/json" import "core:fmt" import "core:strings" import "ratcloud:rc" Rdev :: struct { id: string `json:"id"`, pem: string `json:"pem"`, pub: string `json:"pub"`, pin: string `json:"pin"`, epoch: i64 `json:"epoch"`, } Rseal :: struct { user: string `json:"user"`, storage_dev: Rdev `json:"storage_dev"`, zoo_dev: Rdev `json:"zoo_dev"`, phrase: string `json:"phrase"`, made: i64 `json:"made"`, } RSEAL_FILE :: "robot-seal.json" POSTCARD :: "sealed/postcard.rce" rseal_save :: proc(s: Rseal) -> string { if rc.write_file(RSEAL_FILE, transmute([]u8)to_json(s)) != 0 do return "could not save the robot's sealing state" return "" } robot_at_storage :: proc() -> (^Browser, string) {return robot_at("storage")} // Storage's own page API, as the robot's browser at storage. robot_storage_seal :: proc(in_json: string) -> (json.Value, string) { b, err := robot_at_storage() if err != "" do return nil, err r := post(b, cat(rc.app_url("storage"), "/api/seal"), in_json) if r.status != 200 do return nil, fmt.tprintf("storage's /api/seal answered %d: %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120)) return r.j, "" } // The zoo's own route to storage, as the zoo's page calls it. robot_zoo_storage :: proc(op, body: string) -> (json.Value, string) { b, err := robot_at() if err != "" do return nil, err r := post(b, self_url("/api/26/storage"), fmt.tprintf(`{{"op":%s,"body":%s}}`, jq(op), body)) if r.status != 200 do return nil, fmt.tprintf("the zoo's /api/26/storage (%s) answered %d: %s", op, r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120)) return r.j, "" } rdev :: proc(d: Rdev) -> (Dev, bool) { k, ok := ec_load(d.pem) return {id = d.id, priv = k, pub = d.pub, pin = d.pin, epoch = d.epoch}, ok } new_rdev :: proc() -> Rdev { k := ec_new() return {id = b64u(random_bytes(18)), pem = pem_of(k.d, k.pub), pub = b64(k.pub)} } rdev_entry :: proc(d: Rdev, k: []u8, via, name: string) -> Kr_Device { w, _ := wrap_for_device(d.pub, k) return {id = d.id, name = name, pub = d.pub, wrap = w, created = now(), via = via} } g_keyring_err: string keyring_of :: proc(v: json.Value) -> (kr: Keyring, ok: bool) { if v == nil do return {}, false if _, null := v.(json.Null); null do return {}, false // core:encoding/json does not unmarshal into a pointer: the recovery entry goes in by hand o, isobj := v.(json.Object) if !isobj do return {}, false rec := o["recovery"] delete_key(&o, "recovery") err := json.unmarshal_string(to_json(o), &kr) o["recovery"] = rec if jhas(rec) { r := new(Recovery) if json.unmarshal_string(to_json(rec), r) == nil do kr.recovery = r } if err != nil do g_keyring_err = fmt.tprintf("%v", err) return kr, err == nil } // The robot's devices, set up once per environment: a storage device holding its root (and the // recovery key), and a zoo device holding the zoo's key for it. robot_sealing :: proc() -> (Rseal, string) { uid := itoa(robot_user().id) s: Rseal if b, err := rc.read_file(RSEAL_FILE); err == 0 && json.unmarshal(b, &s) == nil && s.user == uid && uid != "0" do return s, "" if _, err := robot_at_storage(); err != "" do return {}, err uid = itoa(robot_user().id) have, err := robot_storage_seal(`{"op":"get","fresh":true}`) if err != "" do return {}, err if jhas(have, "keyring") do return {}, "the robot has a keyring at storage but not its devices (robot-seal.json was lost): its sealed postcard cannot be opened" root := random_bytes(32) rb, phrase := new_recovery_key() sd := new_rdev() e := new_root_wrap_key(root) rec := recovery_wrap(rb, root) kr := Keyring{v = 2, mk_id = mk_id(root), epoch = 1, root = e, recovery = &rec, devices = {rdev_entry(sd, root, "created", "Zoo robot")}, older = {}} kr.mac = keyring_mac(root, kr, uid, "-") if _, err2 := robot_storage_seal(fmt.tprintf(`{{"op":"put","keyring":%s,"newproof":%s}}`, to_json(kr), jq(mk_proof(root)))); err2 != "" do return {}, err2 sd.pin, sd.epoch = kr.mk_id, 1 // The zoo's key, wrapped to the root's public half: the robot holds the root here, so it opens the // entry and the public half it wraps to is the root's own (in a browser: storage's window). if _, rerr := open_root_wrap_key(root, e); rerr != "" do return {}, rerr app := random_bytes(32) zd := new_rdev() akr := Keyring{v = 2, mk_id = mk_id(app), epoch = 1, root = wrap_for_root(e.id, e.pub, "zoo", app), devices = {rdev_entry(zd, app, "created", "Zoo robot")}, older = {}} akr.mac = keyring_mac(app, akr, uid, "zoo") if _, err3 := robot_zoo_storage("keyring", fmt.tprintf(`{{"op":"put","keyring":%s,"newproof":%s}}`, to_json(akr), jq(mk_proof(app)))); err3 != "" do return {}, err3 zd.pin, zd.epoch = akr.mk_id, 1 s = {user = uid, storage_dev = sd, zoo_dev = zd, phrase = phrase, made = now()} return s, rseal_save(s) } // The zoo's key for the robot, as its zoo device opens it (rc-seal.js's trust()). robot_zoo_key :: proc(s: ^Rseal) -> ([]u8, Keyring, string) { j, err := robot_zoo_storage("keyring", `{"op":"get","fresh":true}`) if err != "" do return nil, {}, err kr, ok := keyring_of(jget(j, "keyring")) if !ok do return nil, {}, "the zoo has no keyring for the robot at storage" dev, dok := rdev(s.zoo_dev) if !dok do return nil, {}, "the robot's zoo device key does not load" k, terr := trust_keyring(kr, dev, s.user, "zoo") if terr != "" do return nil, {}, terr s.zoo_dev.pin, s.zoo_dev.epoch = kr.mk_id, kr.epoch if e := rseal_save(s^); e != "" do return nil, {}, e return k, kr, "" } // A keyring the robot changes: one more epoch, its MAC, the proof of the key it holds. robot_zoo_write :: proc(s: ^Rseal, k: []u8, kr: Keyring, devices: []Kr_Device) -> (Keyring, string) { next := kr next.epoch = kr.epoch + 1 next.devices = devices if next.older == nil do next.older = make([]Kr_Older, 0) next.mac = "" next.mac = keyring_mac(k, next, s.user, "zoo") j, err := robot_zoo_storage("keyring", fmt.tprintf(`{{"op":"put","keyring":%s,"proof":%s}}`, to_json(next), jq(mk_proof(k)))) if err != "" do return {}, err out, ok := keyring_of(jget(j, "keyring")) if !ok do return {}, "storage did not hand the keyring back" s.zoo_dev.epoch = out.epoch return out, rseal_save(s^) } // Seal words under the zoo's key and save them as the robot's postcard, through the public paths: // begin, the bytes straight to B2, commit. The file and the envelope's bytes. robot_seal_postcard :: proc(k: []u8, words: string) -> (json.Value, []u8, string) { env_ := envelope_seal(k, bytes_of(words), "postcard.txt", "text/plain") j, err := robot_zoo_storage("begin", fmt.tprintf(`{{"files":[{{"path":%s,"size":%d,"type":%s}}]}}`, jq(POSTCARD), len(env_), jq(ENV_TYPE))) if err != "" do return nil, nil, err f := jarr(j, "files") if len(f) == 0 || jstr(f[0], "url") == "" do return nil, nil, cat("storage would not begin the postcard: ", to_json(j)) put := put_many({{jstr(f[0], "url"), jstr(f[0], "type"), env_}})[0] if put != 200 do return nil, nil, fmt.tprintf("B2 answered the PUT with %d", put) c, cerr := robot_zoo_storage("commit", fmt.tprintf(`{{"ids":[%d]}}`, jint(f[0], "id"))) if cerr != "" do return nil, nil, cerr cf := jarr(c, "files") if len(cf) == 0 || !jhas(cf[0], "file") do return nil, nil, cat("storage would not commit the postcard: ", to_json(c)) return jget(cf[0], "file"), env_, "" } // The robot's postcard as the bucket holds it (a presigned GET, like a browser's). robot_postcard_bytes :: proc() -> (json.Value, []u8, string) { j, err := robot_zoo_storage("urls", fmt.tprintf(`{{"paths":[%s]}}`, jq(POSTCARD))) if err != "" do return nil, nil, err f := jarr(j, "files") if len(f) == 0 || jstr(f[0], "url") == "" do return nil, nil, cat("storage has no postcard for the robot: ", to_json(j)) g := get_many({jstr(f[0], "url")})[0] if g.status != 200 do return nil, nil, fmt.tprintf("the bucket answered %d", g.status) return f[0], g.body, "" } POSTCARD_WORDS := [?]string{"otter", "lantern", "harbour", "juniper", "comet", "pebble", "meadow", "falcon", "teacup", "glacier", "cinnamon", "lighthouse"} postcard_words :: proc() -> string { return fmt.tprintf("Sealed at %s UTC by the zoo's robot. Today's word: %s-%s.", hm(now()), POSTCARD_WORDS[random_int(0, len(POSTCARD_WORDS) - 1)], random_hex(3)) } _ :: strings