#include #include #include #include "cred.h" void pic_cred_wipe(void *p, size_t n) { volatile unsigned char *v = p; while (n--) { *v++ = 0; } } int pic_cred_get(const char *env, const char *literal, char *buf, size_t cap, char *err, size_t errcap) { const char *value = literal && *literal ? literal : NULL; const char *what = value ? "the models file's apiKey" : env; size_t n; if (!value) { if (!env || !*env) { return 1; /* nothing configured: a server that needs no key */ } value = getenv(env); if (!value || !*value) { (void)snprintf(err, errcap, "%s is not set (run pic under `doppler run --`)", env); return -1; } } n = strlen(value); if (n >= cap) { (void)snprintf(err, errcap, "%s is longer than %zu bytes", what, cap - 1); return -1; } for (size_t i = 0; i < n; i++) { if ((unsigned char)value[i] < 0x21 || (unsigned char)value[i] > 0x7e) { (void)snprintf(err, errcap, "%s has a character that cannot go in a header", what); return -1; } } memcpy(buf, value, n + 1); return 0; }