/* pic's only credential code. A key is fetched into a caller buffer just
* before a request and wiped right after; it is never journaled, logged or
* put in an error message. Keys come from pic's credential store, the models file (a
* literal) or an environment variable, in that order: rows of picu_credential
* in ~/.pic/pic.sqlite, each sealed with ChaCha20-Poly1305 under a master
* key that never touches the database; a subscription sign-in's tokens are
* sealed the same way (see "subscription sign-in" below). The master key comes from the
* platform's secret_key hook (platform.h): on a host, a passphrase stretched
* with PBKDF2-HMAC-SHA256. It is asked for only when a stored key is needed,
* and kept for the life of the process. */
#ifndef PIC_CRED_H
#define PIC_CRED_H
#include <stddef.h>
#include <stdint.h>
/* The size of a buffer that holds any key pic accepts, its NUL included: a
* key is at most PIC_CRED_KEY_CAP - 1 bytes, at login and at use alike. */
#define PIC_CRED_KEY_CAP 512
/* The size of a buffer for what pic_cred_get returns: an API key, or a
* subscription's access token (a signed token, longer than any key). */
#define PIC_CRED_TOKEN_CAP 8192
/* The largest subscription credential (its JSON object) pic stores. */
#define PIC_CRED_OAUTH_CAP 32768
/* Fetches PROVIDER's key, in Pi's order: the store's credential for PROVIDER
* (unlocking the store, which may ask for the passphrase), else LITERAL (the
* models file's), else the variable ENV. A stored credential that cannot be
* had is an error: it never falls back to the variable. A stored subscription sign-in gives
* its access token, refreshed first when it expires within a minute. 0 with
* the key in buf; 1 when nothing
* is configured or stored (a server that needs no key); -1 with err (never
* the value), naming `pic login`, when no key can be had. */
int pic_cred_get(const char *provider, const char *env, const char *literal, char *buf, size_t cap, char *err,
size_t errcap);
/* Overwrites n bytes in a way the compiler may not remove. */
void pic_cred_wipe(void *p, size_t n);
/* Seals KEY for PROVIDER into the store (replacing any), unlocking it
* first, or creating it (a new passphrase) when it has no master key yet. 0,
* or -1 with err. */
int pic_cred_login(const char *provider, const char *key, char *err, size_t errcap);
/* Removes PROVIDER's stored key: 1 removed, 0 none was stored, -1. */
int pic_cred_logout(const char *provider);
/* Whether PROVIDER has a stored key (no unlock needed): 1 or 0. */
int pic_cred_stored(const char *provider);
/* Calls fn for each stored credential's name and kind ("api_key", or
* "oauth" for a subscription sign-in), oldest first, until it returns
* nonzero; used_ms 0 when never used. Values are never read. 0 or -1. */
int pic_cred_each(int (*fn)(void *ctx, const char *name, const char *kind, long long created_ms, long long used_ms),
void *ctx);
/* Where a request for PROVIDER would take its credential from, without
* unlocking anything, in the order pic_cred_get uses: a stored API key or
* sign-in, the models file's literal, the variable ENV, or nothing. */
typedef enum { PIC_CRED_NONE, PIC_CRED_LITERAL, PIC_CRED_ENV, PIC_CRED_STORED_KEY, PIC_CRED_STORED_OAUTH } pic_cred_source;
pic_cred_source pic_cred_source_of(const char *provider, const char *env, const char *literal);
/* The source in use as a phrase, naming what it shadows: "stored key (in use;
* FOO_API_KEY also set)", "FOO_API_KEY (in use)", or "none". */
void pic_cred_describe(const char *provider, const char *env, const char *literal, char *out, size_t cap);
/* ---- subscription sign-in ---------------------------------------------------
* The flows are provider code, outside core: the composition root registers
* them here. A subscription credential is a JSON object with at least
* "access" (the bearer token), "refresh" and "expires" (ms since the epoch);
* the flow may keep more fields in it. It is sealed whole, like a key. */
#define PIC_CRED_NO_BROWSER 1 /* print the URL and take a pasted code; open no browser */
#define PIC_CRED_NO_TERMINAL 2 /* messages to stderr and nothing pasted: stdout is a protocol (RPC) */
#define PIC_CRED_PIPE 4 /* the terminal interface's child: JSON lines out, pasted lines on stdin */
typedef struct {
/* PROVIDER's sign-in label ("Sign in with ..."), or NULL when it has none. */
const char *(*label)(const char *provider);
/* Signs in on the terminal (FLAGS: PIC_CRED_NO_BROWSER, PIC_CRED_NO_TERMINAL). 0 with *kind
* ("oauth", or "api_key" for a flow that hands back a key) and *secret
* (malloc'd: the credential JSON, or the key; the caller wipes and frees
* it); -1 with err. */
int (*login)(const char *provider, int flags, const char **kind, char **secret, char *err, size_t errcap);
/* Exchanges CRED's refresh token: 0 with the new credential JSON in *out
* (malloc'd); -1 with err, never a token in it. */
int (*refresh)(const char *provider, const char *cred, char **out, char *err, size_t errcap);
/* The only address prefix (scheme, host, "/") PROVIDER's subscription
* token may be sent to, or NULL when its sign-in stores a plain key. */
const char *(*origin)(const char *provider);
} pic_cred_oauth_ops;
void pic_cred_oauth_register(const pic_cred_oauth_ops *ops);
/* Where PROVIDER's subscription token may be sent (cred ops' origin), or NULL. */
const char *pic_cred_oauth_origin(const char *provider);
/* PROVIDER's sign-in label, or NULL when it has no subscription sign-in. */
const char *pic_cred_oauth_label(const char *provider);
/* Unlocks the store (or makes it), runs PROVIDER's sign-in and seals what it
* returns. 0, or -1 with err. */
int pic_cred_oauth_login(const char *provider, int flags, char *err, size_t errcap);
/* After a request was refused with STALE (the access token it sent): the
* current access token in buf, refreshed unless another process already
* replaced STALE. One process refreshes at a time (a lock beside the user
* database), so a refresh token that rotates is spent once. 0, or -1 with
* err. */
int pic_cred_refresh(const char *provider, const char *stale, char *buf, size_t cap, char *err, size_t errcap);
/* A key check pic_cred_get and pic_cred_login share: 0, or -1 with err
* (never the value) when KEY is empty, too long or not header-safe. */
int pic_cred_check(const char *what, const char *key, char *err, size_t errcap);
/* Whether a request for PROVIDER (with the model's ENV and LITERAL) would
* read the store while it is still locked: 1 or 0. An interface uses it to
* ask for the passphrase before it starts a request in a child. */
int pic_cred_needs_unlock(const char *provider, const char *env, const char *literal);
/* Unlocks the store now (asking for the passphrase if need be). 0, or -1
* with err; a wrong passphrase is an error, never a garbage key. */
int pic_cred_unlock(char *err, size_t errcap);
/* The store's state, asking nothing: PIC_CRED_NEW (no master key yet, so a
* new passphrase is wanted), PIC_CRED_LOCKED, or PIC_CRED_OPEN. */
enum { PIC_CRED_NEW, PIC_CRED_LOCKED, PIC_CRED_OPEN };
int pic_cred_state(void);
/* Unlocks the store, or makes it when it is new (the passphrase from the
* platform's secret_key, as always). 0, or -1 with err. */
int pic_cred_prepare(char *err, size_t errcap);
/* Handing the unlocked master key to a child pic (the interface's request
* processes) without asking again, and without it touching argv, the
* environment's values, the journal or any file:
* the parent calls pic_cred_handoff() before fork; it returns the read end
* of a pipe that already holds the 32 key bytes (write end closed), or -1
* when the store is not unlocked. The fd is close-on-exec; the child clears
* that and sets PIC_KEY_FD to its number (only the number is in the
* environment), and the parent closes its copy after fork.
* the child calls pic_cred_adopt() first thing: it reads the bytes, closes
* the fd and unsets PIC_KEY_FD, so nothing it runs inherits either. The
* bytes are checked against the store's verifier before first use.
* A pipe holds the bytes only in the kernel; nothing reaches a disk. */
int pic_cred_handoff(void);
void pic_cred_adopt(void);
/* ---- the primitives, exposed for the unit tests ------------------------ */
#define PIC_CRED_KEY 32
#define PIC_CRED_NONCE 12
#define PIC_CRED_TAG 16
#define PIC_CRED_OVERHEAD (PIC_CRED_NONCE + PIC_CRED_TAG)
/* Seals N bytes of PT under KEY with AAD bound: out gets nonce, ciphertext
* and tag (N + PIC_CRED_OVERHEAD bytes), with a fresh random nonce. 0 or -1
* (no entropy). */
int pic_cred_seal(const unsigned char key[PIC_CRED_KEY], const char *aad, const void *pt, size_t n, unsigned char *out);
/* Opens what pic_cred_seal made (N bytes in all) into pt (N -
* PIC_CRED_OVERHEAD bytes): 0, or -1 with pt wiped when the key, the AAD or
* any byte is wrong. */
int pic_cred_open(const unsigned char key[PIC_CRED_KEY], const char *aad, const unsigned char *in, size_t n, void *pt);
/* PBKDF2-HMAC-SHA256 to a 32-byte key. */
void pic_cred_kdf(const void *pass, size_t n, const unsigned char *salt, size_t saltn, uint32_t iterations,
unsigned char out[PIC_CRED_KEY]);
/* Forgets the cached master key (tests). */
void pic_cred_lock(void);
#endif