/* 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 the models file (a literal), an * environment variable, or pic's credential store: rows of picu_credential * in ~/.pic/pic.sqlite, each sealed with ChaCha20-Poly1305 under a master * key that never touches the database. 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 #include /* 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 /* Fetches PROVIDER's key: LITERAL (the models file's) if given, else the * variable ENV, else the store's key for PROVIDER, unlocking the store * (which may ask for the passphrase). 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 key's name, 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, long long created_ms, long long used_ms), void *ctx); /* 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); /* 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