diff --git a/.gitignore b/.gitignore index 51742b4..482cb51 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,7 @@ Thumbs.db Desktop.ini # Claude Code per-user local settings (machine-specific permissions) .claude/settings.local.json + +# The key ceremony writes this; it carries the base private key and must +# never be committed. See ANSMINDCORE docs/model-package-keys.md. +core/ANSModelPackage/ANSModelKeysGenerated.h diff --git a/core/ANSModelPackage/ANSModelKeys.h b/core/ANSModelPackage/ANSModelKeys.h new file mode 100644 index 0000000..7b1c050 --- /dev/null +++ b/core/ANSModelPackage/ANSModelKeys.h @@ -0,0 +1,42 @@ +#ifndef ANSMODELKEYS_H +#define ANSMODELKEYS_H +#pragma once +// ANSModelKeys — the keys this build was given, if it was given any. +// +// The base private key is not in this file and is not in the repository. It is +// written by the key ceremony (ANSMINDCORE's tools/ansmodel_keys.cpp) into +// ANSModelKeysGenerated.h, which is ignored by git and produced on the machine +// that builds a release. This header is the seam: it uses that file when it is +// there and says "no keys" when it is not. +// +// A BUILD WITHOUT THE KEY IS A VALID BUILD. Every zip still loads, and a +// package is refused with a sentence that says why. That is what keeps the key +// out of a developer's checkout without keeping developers out of the code. + +#if defined(__has_include) +#if __has_include("ANSModelKeysGenerated.h") +#include "ANSModelKeysGenerated.h" +#define ANSCORE_HAVE_BUILTIN_MODEL_KEYS 1 +#endif +#endif + +#include "ANSModelPackage.h" + +namespace ANSCENTER { +namespace modelpkg { + +// Add whatever this build ships with to a keyring. False when it ships with +// nothing, which is not an error. +inline bool AddBuiltInKeys(Keyring& ring) { +#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS) + return builtin::addBuiltInKeys(ring); +#else + (void)ring; + return false; +#endif +} + +} // namespace modelpkg +} // namespace ANSCENTER + +#endif // ANSMODELKEYS_H diff --git a/core/ANSModelPackage/ANSModelOpen.h b/core/ANSModelPackage/ANSModelOpen.h index 92fe39e..3f8f853 100644 --- a/core/ANSModelPackage/ANSModelOpen.h +++ b/core/ANSModelPackage/ANSModelOpen.h @@ -14,12 +14,13 @@ // exists. OpenModelPackage decrypts into a buffer the caller owns; the caller // hands that buffer to a session and wipes it. Nothing here writes a file. // -// WHERE THE KEY COMES FROM. The product sets it once at startup with -// SetProcessModelKeys(): the base PRIVATE key packages are sealed to, and the -// root PUBLIC key that vouches for a release signing key. Nothing here reads a -// key out of the package or off the disk. With no keys set, a package is -// refused with a sentence that says so, and every existing zip still loads — a -// build from before the key ceremony runs everything except packages. +// WHERE THE KEY COMES FROM. Three places, in this order: what the build ships +// with (ANSModelKeys.h, filled in by the key ceremony), what the environment +// offers a developer, and whatever a caller sets with SetProcessModelKeys(). +// Nothing here reads a key out of the package or off the disk. With no keys at +// all, a package is refused with a sentence that says so, and every existing +// zip still loads — a build made without the ceremony runs everything except +// packages. // // The base private key must never sit in the binary as one contiguous // constant; assembling it is the job of whatever calls SetProcessModelKeys, @@ -38,6 +39,7 @@ #include #include +#include "ANSModelKeys.h" #include "ANSModelPackage.h" #include "ANSOnnxShape.h" @@ -66,11 +68,16 @@ inline bool keyFromB64(const char* b64, Key& out) { return true; } -// Development only: keys from the environment, once, and said out loud. -inline void loadEnvKeysOnce(Keyring& ring) { - static bool done = false; - if (done) return; - done = true; +// True once this process's keyring is settled — either because the build's own +// keys were installed, or because a caller replaced them outright. A caller +// that sets keys means exactly that: what it set is what this process holds. +inline bool& keysSettled() { + static bool settled = false; + return settled; +} + +// Development only: keys from the environment, and said out loud. +inline void loadEnvKeys(Keyring& ring) { const char* id = std::getenv("ANSCORE_MODEL_KEY_ID"); const char* priv = std::getenv("ANSCORE_MODEL_BASE_PRIVATE"); Key k{}; @@ -97,12 +104,17 @@ inline void loadEnvKeysOnce(Keyring& ring) { inline void SetProcessModelKeys(Keyring ring) { std::lock_guard lk(detail::keysMutex()); detail::keyringStorage() = std::move(ring); + detail::keysSettled() = true; } inline Keyring ProcessModelKeys() { std::lock_guard lk(detail::keysMutex()); Keyring& ring = detail::keyringStorage(); - detail::loadEnvKeysOnce(ring); + if (!detail::keysSettled()) { + detail::keysSettled() = true; + AddBuiltInKeys(ring); // what this build was given at the ceremony + detail::loadEnvKeys(ring); // and what a developer's environment adds + } return ring; // by value: the caller may open on another thread }