package main // The vault holds every licence's grant and access token (design 39 bus-and-switch §15, §16, B4, B5): the manager hands // what it gets to `licence-put` in two steps, with a key the vault made for the call, and reads a grant with `get`, // sealed to its own public key, for one act. import ( "context" "crypto/rand" "encoding/base64" "encoding/json" "fmt" stdio "git.novox.be/novox/mesh-sdk/go" "golang.org/x/crypto/nacl/box" ) // The parts of a licence the vault holds (§15). const ( PartGrant = "grant" PartAccess = "access" ) // Grants is where a licence's two secrets are kept. type Grants interface { // Put hands one part of a licence, by its key, to the vault at a generation; an older one is refused. Put(ctx context.Context, key, part string, generation int64, value string) error // Get reads one part of a licence from the vault. Get(ctx context.Context, key, part string) (string, error) } // SecretName is the name of one part of a licence in the vault. func SecretName(key, part string) string { return "licence." + key + "." + part } // VaultGrants is the vault on the bus, asked as the seat mesh-vault. type VaultGrants struct{ Keys KeyPair } // Put asks licence-put's two steps. func (v VaultGrants) Put(_ context.Context, key, part string, generation int64, value string) error { raw, err := stdio.Ask("seat:mesh-vault.licence-put", map[string]any{"licence": key, "part": part, "generation": generation}) if err != nil { return err } var opened struct { Call string `json:"call"` SealTo string `json:"seal_to"` } if err := json.Unmarshal(raw, &opened); err != nil { return err } pub, err := base64.StdEncoding.DecodeString(opened.SealTo) if err != nil || len(pub) != 32 || opened.Call == "" { return fmt.Errorf("licence-put answered no call and key to seal to: %s", raw) } var to [32]byte copy(to[:], pub) sealed, err := box.SealAnonymous(nil, []byte(value), &to, rand.Reader) if err != nil { return err } _, err = stdio.Ask("seat:mesh-vault.licence-put", map[string]any{"call": opened.Call, "sealed": base64.StdEncoding.EncodeToString(sealed)}) return err } // Get asks get, sealed to the manager's public key, and opens the answer. func (v VaultGrants) Get(_ context.Context, key, part string) (string, error) { raw, err := stdio.Ask("seat:mesh-vault.get", map[string]any{"name": SecretName(key, part), "public_key": v.Keys.PublicKey}) if err != nil { return "", err } var a struct { Sealed SealedBox `json:"sealed"` } if err := json.Unmarshal(raw, &a); err != nil { return "", err } return Open(a.Sealed, v.Keys.PrivateKey) }