# Connector release signing and hosting

The release tool signs **manifest.json** with Ed25519 using OpenSSL
(`pkeyutl -sign -rawin`; use OpenSSL 3.x). The manifest binds the version, minimum Hermes version,
UTC creation time, and each Linux binary's name, SHA-256 and byte count.
`SHA256SUMS` is also signed as `SHA256SUMS.sig` for standalone checks. The
installer trusts only `manifest.json.sig`, never an unsigned checksum list.

Local tests use OpenSSL 3.5.5. OpenSSL 1.1.1 compatibility is not certified
by this lane; the installer probes for `pkeyutl -rawin` and refuses a CLI
without it. Do not announce 1.1.1 support without testing that exact version.

No production key or release is included in this source change. Source remains
private until the open-source launch. The release owner does the steps below.

## First release

1. Create an Ed25519 private PEM through the approved credential workflow
   (`openssl genpkey -algorithm ED25519`). Store it in Bitwarden; inject its
   owner-only temporary working file as `CONNECTOR_RELEASE_SIGNING_KEY_FILE`.
   Never commit it or copy it into the staged release. Export its public PEM
   with `openssl pkey -in <private-file> -pubout -out <public-file>`.
2. Run the machine guard from AGENTS.md before local build/tool commands.
   From the repository root, with existing dependencies:

   ```sh
   dart tool/connector_release.dart build
   dart tool/connector_release.dart stage --version 0.1.0
   dart tool/connector_release.dart verify --version 0.1.0 --public-key-file <trusted-public-file>
   dart tool/connector_release.dart upload-plan --version 0.1.0 --public-key-file <trusted-public-file>
   ```

   `build` invokes `packaging/build.sh` once per Linux target. Cross-building
   can need Bun's runtime download. `stage` requires the package version to
   match. It copies exactly those two binaries to `build/connector-release/v0.1.0/`,
   writes metadata, derives the Ed25519 public key and requires it to match
   `lib/core/config/connector_release_public_key.dart`. It embeds that shared
   public pin inside the staged `install.sh`; cloud-init bundles the same pin
   as a root-owned `0644` file beside the connector executable. It does not
   change the source template. Without the
   key env var it writes an unsigned stage; verification and upload planning
   then fail closed. An existing stage is never overwritten. Inspect and remove
   an incomplete local stage before retrying; never overwrite a published version.
3. Review the pinned public key against the trusted public PEM and publish its
   SHA-256 fingerprint through a separate trusted channel. The default `verify`
   checks against the staged installer's pin; `--public-key-file` also compares
   that pin with the independently supplied key. An attacker replacing an entire
   local stage, including its installer, requires this independent check.
4. Create R2 bucket `unboundchat-connector-releases`, bound to the website Worker
   as `CONNECTOR_RELEASES`. The source config declares that binding only; it
   does not create the bucket. Review the printed upload commands and run them
   from `website/` with its installed Wrangler. They use `--remote`; the tool
   never uploads. Upload all versioned objects first, then `install.sh`,
   `latest.json.sig` and finally `latest.json`. Keep existing versioned objects unchanged.
5. Deploy the website Worker through its normal release path. Confirm real
   GET/HEAD responses and install on each target architecture before announcing.
   No real build, upload, deploy or live-host check is part of these source tests.

## Published objects

- `/connector/install.sh`, `/connector/latest.json` and
  `/connector/latest.json.sig`: `no-cache`. The pointer signature uses
  `application/octet-stream`, like `manifest.json.sig`.
- `/connector/vX.Y.Z/unbound-connector-linux-x64` and `-arm64`.
- `/connector/vX.Y.Z/SHA256SUMS` and `SHA256SUMS.sig`.
- `/connector/vX.Y.Z/manifest.json` and `manifest.json.sig`.

All versioned objects use `public, max-age=31536000, immutable`. Worker GETs
stream directly from R2; HEAD uses metadata without loading the body. Only
these exact names are served. Missing or forbidden names return 404.

## Installer trust and local tests

The source installer contains a key marker and refuses real installation until
staging fills it. There is no key override or unsigned fallback. It downloads
and verifies the manifest signature before parsing any field, checks the exact
requested version, and only then downloads and checks the selected binary.
A missing OpenSSL, missing file, bad key, signature, version, hash or size stops
before replacing the executable or starting a service. `--dry-run` only prints
steps; it does not claim verification. The existing `--user` and system service
installation flow is retained.

The manifest uses a canonical nine-line JSON layout emitted by the tool, with
one compact JSON object per artifact line. This lets POSIX awk check its exact
shape without adding jq/Python to users' servers. Keep the writer and parser
in sync; `verify` rejects noncanonical manifests too.

Tests generate throwaway keys inside worktree-local scratch directories and
remove them on completion. `CONNECTOR_RELEASE_BASE_URL` may point a **staged**
installer at an explicit `file:///.../connector` or HTTP loopback fixture root
(the tool appends `/vX.Y.Z`). HTTPS is the default and the only nonlocal
transport. The pinned key is still required with local fixtures.

## Release authentication and freshness

The release tool signs the exact bytes of `manifest.json`, `SHA256SUMS` and
`latest.json` with the same Ed25519 release key. `latest.json` contains `version`
and `issuedAt` in UTC. Staging uses the same instant as the signed manifest's
`createdAt`. Verification authenticates every signature before decoding metadata.
Upload immutable versioned artifacts first, then the installer, then mutable
`latest.json.sig` and finally mutable `latest.json` (both with `no-cache`). A
reader during pointer publication can fail closed and retry once it finishes.

The updater authenticates the pointer with the public key pinned beside the
installed executable. It rejects missing/invalid signatures and issue times
older than the locally retained `unbound-connector.release-latest.json` floor.
The updater stages the next floor before replacing code, then atomically commits
it after health validation. Failed finalization reports success with
`update_finalization_pending`; the next update resumes it before selecting any
release, including an authenticated same-version check. Same-version checks
atomically retain the pointer and report `already at <version> (pointer issued <date>)`
and do not replace code or restart. Explicit `--version` authenticates the selected
manifest directly, retains the existing freshness floor, and still requires
`--allow-downgrade` to install a lower version.

A fresh shell installation establishes its floor from the signed manifest's
`createdAt` (equal to `issuedAt` for newly staged releases). Older installations
without a floor establish one on their first successful authenticated latest
check. Reinstalling pairing preserves an existing floor.

Residual risk: without expiry or an independent trusted freshness source, an
attacker can replay any matching old signed pointer/signature pair whose issue
time is at least the local floor and whose version passes the downgrade check.
In particular, replaying the installed version with an issue time equal to the
floor reports `already_current`, even when a newer security release exists.
Signatures and the floor prevent forgery and observed-time regression; they
cannot prove that an unseen newer security release exists. The floor is unsigned
local state, not a retained signed pointer: its owner or root can edit, delete
or restore it, and its absence permits reinitialization.

Acceptance does not compare `issuedAt` with the client's wall clock and has no
expiry. Signing timestamps must advance consistently: a signing host clock
moving backward can block a legitimate newer release, while a future-dated
signed pointer can advance the local floor excessively.

## Key rotation

1. Generate/store a new Ed25519 key in Bitwarden. Export and independently
   confirm its public PEM and fingerprint before changing release inputs.
2. Build a new version, stage it with the new private key, and verify with the
   independently trusted new public key. Update the checked-in public constant
   in the reviewed rotation change before staging; staging rejects a different
   signing key. Inspect the staged installer's and cloud-init's new pin, and
   provide the reviewed trust migration for existing installs.
3. Publish the new immutable version, then switch the latest installer,
   `latest.json.sig` and `latest.json`, in that order. Publish the new fingerprint
   via the separate trusted channel.
4. An old saved installer trusts the old key; a new installer trusts the new
   key. `--version` across this boundary fails closed. Keep a reviewed copy of
   each version's installer for manual old-version checks; do not add fallback
   keys or skip verification. Retire the old private key in the credential store
   once the owner no longer needs to sign with it.
5. If compromise caused rotation, announce the affected versions and key.
   Stop serving compromised latest pointers and publish a clean new version;
   do not silently replace an immutable object or reuse a version number.
