# OIP-0003: Signed plans: TUF roles, expiry, version and rollback protection

| Field | Value |
|---|---|
| Status | DRAFT |
| Function | `orchestrate` (section 5.8); no function added, the register keeps exactly twelve |
| Affects | §5.8 (ORCH-1 to ORCH-9), §9 (G-ORCH-1 closed); `plan` (revision 2), `observed-state-receipt` and `reconciliation-receipt` (`planBasis`); `registers/functions.yaml` (`orchestrate` CONTRACTED); `registers/requirements.yaml` regenerated; vectors `conformance/vectors/plans/`; cases CT-NODE-030 to CT-NODE-036, CT-SITE-030, CT-SITE-031, CT-GRID-030 |
| Protocol version | obsp/0.1 |
| Closes | G-ORCH-1 (plan signing path and TUF update metadata) |
| Depends on | OIP-0008 (signature envelope, `urn:obsp:schema:v3.1:signatureenvelope`); see "Dependency and acceptance order" |
| Relates to | OIP-0010 (tasking plans for satellites and drones) takes `kind` `tasking`, reserved here |

## Problem

Section 5.8 says a plan MUST be signed and update metadata MUST follow the TUF roles "so a compromised mirror cannot
roll back or freeze a node", but no contract carries any of it. The threat review of the conformance suite found:

| Threat (section 8) | Today |
|---|---|
| Replayed plan | The `plan` schema has `planVersion` (a free label) and compiler digests, but no signature, no expiry and no ordered version; `additionalProperties` is false, so a signature is refused. An old, well-formed plan presented again passes CT-NODE-008 |
| Rolled-back update | No invariant compares a plan with the version a node holds; no snapshot or timestamp metadata exists, so neither rollback nor freeze is detectable |
| Mix-and-match | Nothing binds a plan to the metadata that announced it, or its artifacts to the plan |
| Receipts | `observed-state-receipt.planBasis` names a plan by label and a digest of unstated content; `reconciliation-receipt` names no plan at all |

CT-NODE-008 is titled "a signed plan applied with a reconciliation receipt" but checks no signature.

## Change

### Model

The plan becomes the TUF targets metadata of its deployment. The four TUF roles map as follows (The Update Framework
specification 1.0, §2.1 roles, §4.3 to §4.6 metadata formats, §5.3 to §5.6 client workflow; section numbers from the
1.0.x revisions as recalled, `checked: not checked`):

| TUF role | In OBSP | Signed payload | Signed by |
|---|---|---|---|
| root | `plan.updateMetadata.root`: version, expiry, public keys, keys and threshold of each of the four roles | the root metadata without `signatures` | the root threshold (and, on rotation, the previous root's threshold too) |
| targets | the plan itself: `version`, `expires`, `targets` (name, digest, length; a SUIT manifest digest for firmware) | the plan without `signatures` and `updateMetadata` | the targets threshold, in `plan.signatures` |
| snapshot | `plan.updateMetadata.snapshot`: version, expiry, and the plan's `planUni`, `version` and payload digest | the snapshot metadata without `signatures` | the snapshot threshold |
| timestamp | `plan.updateMetadata.timestamp`: version, short expiry, and the snapshot's version and digest | the timestamp metadata without `signatures` | the timestamp threshold |

`updateMetadata` is outside the plan payload so that the snapshot and timestamp roles re-sign (the timestamp often)
without re-signing the plan. TUF delegations and multiple targets files are not adopted: one plan is one targets
document. Every signature is a signature envelope (OIP-0008); `plan.schema.json` carries an identical copy of its
definition as `$defs.SignatureEnvelope` because the base tree has no envelope schema yet.

Plan versions are per target and kind: `version` strictly increases per (`deploymentUni`, `kind`), so a firmware
rollout and a configuration rollout for the same deployment do not block each other. `kind` is `configuration`,
`software`, `firmware` or `tasking`; the same plan model is the base for tasking satellites and drones, left to OIP-0010.
Firmware targets reference SUIT manifests (RFC 9019 architecture, RFC 9124 information model; `checked: not checked`)
by digest and sequence number; the protocol does not parse the manifest.

### Normative text (new table in §5.8; the exact text is in `patches/spec.patch`)

| Id | Requirement (summary) | Rejection code |
|---|---|---|
| ORCH-1 | `version` strictly increases per target and kind; an issuer never issues two plans with the same target, kind and version; a node MUST reject a version at or below the last it accepted (this also rejects a replay) and snapshot or timestamp metadata older than it trusts | `OBS-PLAN-0001` |
| ORCH-2 | `expires` required; a node MUST reject a plan when its trusted time is at or after the plan's, the root's or the snapshot's expiry; a node without trusted time accepts no plan | `OBS-PLAN-0002` |
| ORCH-3 | each of the targets, snapshot, timestamp and root roles MUST be signed by at least its threshold of distinct keys the root lists for it, over that role's payload; unlisted keys, repeats and signatures over other bytes do not count | `OBS-PLAN-0003` |
| ORCH-4 | snapshot records the plan's `planUni`, `version` and payload digest; timestamp records the snapshot's version and digest; a node MUST reject a mismatch (mix-and-match) and MUST NOT install a target whose bytes differ from its `targets` digest or length | `OBS-PLAN-0004` |
| ORCH-5 | timestamp metadata expires (SHOULD within the site cell's offline window); a node MUST reject a plan on expired timestamp metadata (freeze) and keeps operating under the plan it applied last | `OBS-PLAN-0005` |
| ORCH-6 | root lists the public keys and, for each role, keys and a threshold from 1 to the number of keys; the first root comes from the bootstrap bundle; a new root is accepted only at trusted version plus one, signed by the root thresholds of both the trusted and the new root | `OBS-PLAN-0006` |
| ORCH-7 | a reconciliation receipt names the applied plan by `planUni`, `version`, `kind`, `deploymentUni`, `planDigest`, payload digest and `acceptedAt`; an observed-state receipt names the plan in effect the same way | |
| ORCH-8 | `kind` vocabulary; firmware targets reference their SUIT manifest by digest and sequence number; a node MUST reject `tasking` until OIP-0010 defines it | `OBS-PLAN-0007` |
| ORCH-9 | the checks run before any fetch or state change; a rejected plan changes nothing; the node reports the code | |

The existing bullets of §5.8 stay; ORCH-1 to ORCH-9 make them testable. §9 loses the G-ORCH-1 row.

### Contract changes (`patches/schemas-fixtures.patch`)

| Schema | Change |
|---|---|
| `plan` (revision 2) | new required `version` (integer from 1), `deploymentUni`, `kind`, `issuedAt`, `expires`, `targets` (at least one; `name`, `digest`, `length`, optional `mediaType`, `uri`, `suitManifest` {`digest`, `sequenceNumber`, `uri`}), `updateMetadata` {`root`, `snapshot`, `timestamp`}, `signatures` (at least one envelope); `$defs` Target, SuitManifestRef, RoleKeys, KeyId, PublicKey (private JWK members refused), RootMetadata, SnapshotMetadata, TimestampMetadata, UpdateMetadata, SignatureEnvelope and ProtocolRole (copied unchanged from OIP-0008); `allOf`: `tasking` refused, `firmware` targets require `suitManifest`. `planVersion` stays as the compiler's label; `executionPermitted` stays `false` (the document alone authorises nothing) |
| `observed-state-receipt` | `planBasis` requires `version`, `kind`, `payloadDigest`; optional `acceptedAt`, `metadataVersions` {`root`, `snapshot`, `timestamp`} |
| `reconciliation-receipt` | new required `planBasis` {`planUni`, `version`, `kind`, `deploymentUni`, `planDigest`, `payloadDigest`, `acceptedAt`, optional `metadataVersions`} |

The example sets (OBSP-NODE, -SITE, -GRID and the negative set) carry a version 1 and a version 2 plan of one
deployment, a firmware plan with a SUIT manifest reference, and the reconciliation and observed-state receipts of both
versions, all generated by `tools/gen_plan_vectors.py` and signed with test keys.

### Reference checks (`patches/cases-invariants.patch`)

- `sdk/python/src/obsp/plans.py`: payload digests, threshold, consistency, expiry, freeze and root-chain checks, and
  `verify(plan, trusted, now)`, the decision a node takes before any state change, returning the `OBS-PLAN-` codes.
  It counts a signature when its key is listed for the role and its `payloadDigest` matches; the cryptographic check
  of each envelope is the envelope's rule (OIP-0008 SIG-2) and is added when that proposal is accepted.
- `sdk/python/src/obsp/plan_invariants.py`: seven suite invariants, registered by `conformance.py` with one import
  placed where no other draft edits the file.

## Dependency and acceptance order

1. OIP-0008 first, then this proposal. The patches here apply to the base tree alone and do not need OIP-0008: the
   envelope definition is copied into `plan.schema.json`, and the specification names the envelope by its `$id`,
   `urn:obsp:schema:v3.1:signatureenvelope`.
2. When this proposal is accepted after OIP-0008, the rebase changes: the `plan` row of `registers/algorithms.yaml`
   (class `plan`) goes from `planned` to `present` with the member `signatures` (an array, plus the three role
   metadata arrays); the `algorithms` validator group learns array members; SIG-2's payload rule ("the object without
   its top-level `signature`") gains the multi-signature form used here; `plans.py` calls the OIP-0008 verifier for
   each counted envelope; the test keys of `conformance/vectors/plans/keys.json` become key records of class `plan`;
   SIG-4 (composite algorithms for `plan` from `obsp/0.5`) applies to all four roles.
3. If OIP-0008 is not accepted, this proposal still stands with the copied envelope, and signature verification
   stays an open question.

Overlaps with the other drafts, all checked with `git apply --check` after these patches are applied: every patch of
OIP-0005, OIP-0006, OIP-0007 and OIP-0008 still applies. OIP-0001 `spec.patch` does not: it removes the G-EXCH-1 row
of §9, whose context is the G-ORCH-1 row this proposal removes; two proposals that each close a gap always overlap
there, and whichever is accepted second is rebased (a one-line merge). The `CT-NODE-036` attestation is inserted in
the middle of `conformance/examples/OBSP-NODE/attestations.yaml` so that OIP-0007 (top) and OIP-0008 (end) still apply.

Two relation rows are proposed but left out of the patches, because `registers/implements-edges.json` is regenerated
by OIP-0005, OIP-0006 and OIP-0008 and a fourth regeneration would stop those patches applying: `EXT-TUF` ("The Update
Framework (TUF) specification 1.0", `orchestrate`, `plan`) and `EXT-SUIT` ("RFC 9019 and RFC 9124", `orchestrate`,
`plan`). They are added at acceptance, followed by `python3 tools/build_edges.py`.

## Compatibility

Breaking for three contracts: a `plan` without the eight new members, an `observed-state-receipt` whose `planBasis`
lacks `version`, `kind` or `payloadDigest`, and a `reconciliation-receipt` without `planBasis` no longer validate.
An unsigned plan cannot be applied after acceptance, which is the intent of §5.8. Migration: the compiler adds
`version` (from its plan series counter), `deploymentUni`, `kind`, `issuedAt`, `expires` and `targets`; a signing
service adds the role metadata and signatures; node agents record the new `planBasis` members. Under §10 a breaking
contract change needs a new `schemaVersion`; the patches keep `3.1.0` for review (open question 1, as in OIP-0008).

## Tests

| Requirement | Case | Kind | Checks |
|---|---|---|---|
| ORCH-1 | CT-NODE-030 (NODE, SITE, GRID) | invariant | `plan-version-monotonic`: per node, deployment and kind, accepted versions strictly increase (by `acceptedAt`, one operation per acceptance, `REJECTED` operations excluded), metadata versions never fall, observed versions never fall (by `observationSequence`) |
| ORCH-2 | CT-NODE-031 | invariant | `plan-not-expired-when-accepted` |
| ORCH-3, ORCH-6 | CT-NODE-032 | invariant | `plan-threshold-signed`: every role meets its threshold with distinct root-listed keys over its payload; no threshold above its key count |
| ORCH-4 | CT-NODE-033 | invariant | `plan-metadata-consistent` |
| ORCH-5 | CT-NODE-034 | invariant | `plan-timestamp-fresh` |
| ORCH-7 | CT-NODE-035 | invariant | `receipt-references-plan`: every receipt names a plan of the set by `planUni` and `version`, with its payload digest, `planDigest`, kind and deployment |
| ORCH-1 to ORCH-9 | CT-NODE-036 (NODE) | manual | the node refuses every vector of `conformance/vectors/plans/` with the listed code before any state change |
| ORCH-1, ORCH-3, ORCH-4 (issuer) | CT-SITE-030 (SITE, GRID) | invariant | `plan-version-unique`, `plan-threshold-signed`, `plan-metadata-consistent` |
| ORCH-8 | CT-SITE-031 (SITE, GRID) | schema | the example firmware plan validates; the schema refuses `tasking` and firmware targets without a SUIT manifest |
| ORCH-6 | CT-GRID-030 (GRID) | invariant | plans next to the system pack carry the four roles with meetable thresholds |

Vectors (`conformance/vectors/plans/`, real Ed25519 signatures with published test keys): `valid`, `rollback`,
`replay`, `equivocation`, `expired`, `below-threshold`, `mix-and-match`, `targets-swapped`, `freeze`, `root-rotation`,
`root-skipped`, `root-rollback`, `tasking-reserved`. Each lists the invariants that must fail on its evidence set
(every other plan invariant passes) and, where a node decision applies, the codes `verify` must return.

Fixtures: positive `plan.valid`, `plan.sha3-digest.valid` (revised), `plan.firmware-suit.valid` (new),
`observed-state-receipt.valid`, `reconciliation-receipt.valid` (revised). Negative, each failing one rule:
`plan.missing-signatures`, `.empty-signatures`, `.signature-not-cose`, `.version-zero`, `.version-as-string`,
`.missing-expires`, `.missing-timestamp-metadata`, `.root-without-snapshot-role`, `.threshold-zero`,
`.private-key-in-root`, `.snapshot-without-plan-digest`, `.target-without-digest`, `.empty-targets`, `.unknown-kind`,
`.tasking-reserved`, `.firmware-without-suit-manifest`; `observed-state-receipt.plan-basis-without-version`,
`.plan-basis-without-payload-digest`; `reconciliation-receipt.without-plan-basis`, `.plan-basis-without-accepted-at`.
The six existing negative fixtures of the three schemas gain the new members and still fail only for their own rule.
Rollback, expiry, threshold, mix-and-match and freeze cannot be refused by a schema (they compare documents, times and
signatures), so their negative cases are the vectors and the unit tests.

Unit tests: `sdk/python/tests/test_plans.py` (28): every vector against the invariants and the node decision; every
vector document against its schema; the example plan's signatures verified with the test keys when the optional
`cryptography` package is installed (skipped, never passed, without it); node decision edge cases (first root,
separate series per kind and deployment, older snapshot or timestamp, expiry at the exact instant, root expiry,
duplicate key, signature over other bytes, unmeetable threshold, snapshot and timestamp mismatches, root changed at the
same version, tasking); invariant edge cases (missing plan, wrong payload digest, wrong kind, falling observed version,
rejected operation, falling metadata version). `python3 tools/gen_plan_vectors.py --check` reports stale generated files.

## Live harness (proposed, not in the patches)

The live harness (`conformance/harness/`) drives the seven operations of the gateway API; none carries a plan, so a
live check that the reference responder rejects a replayed or rolled-back plan has no operation to call. Proposed for
the proposal that adds the operation (it changes `schemas/openapi/obsp-gateway.openapi.yaml`):

- operation `submitPlan`, `POST /nodes/{nodeUni}/plans`, body a `plan`, RequestContext and Idempotency-Key required;
  `202` with a `reconciliation-receipt` on acceptance, `409` problem `OBS-PLAN-0001` on rollback or replay, `422`
  problems `OBS-PLAN-0002` to `OBS-PLAN-0007` for the other rules, nothing changed on refusal;
- the reference responder keeps per-node trusted state and decides with `obsp.plans.verify`; new responder faults
  `no-version-check`, `no-expiry-check`, `no-threshold-check`;
- live cases: the `valid` vector is accepted; the same plan again (replay) and the `rollback` vector's plan are refused
  with `OBS-PLAN-0001`; `expired`, `freeze`, `below-threshold` and `mix-and-match` with their codes; the harness catches
  each fault. The Idempotency-Key rule needs care: a retried identical request answers with the stored response, which
  is not an acceptance, so the replay check uses a new key.

## Open questions

1. `schemaVersion`: does revision 2 of `plan` and the receipt changes bump `schemaVersion` now, or ride with the next
   breaking release (the same question as OIP-0008)?
2. Should `apiVersion` of `plan` change (for example to mark revision 2), and should `executionPermitted` stay a
   constant `false` now that a verified plan is applied?
3. Root distribution: ORCH-6 takes the first root from the bootstrap bundle, but `node-bootstrap-bundle` has no member
   for it yet. Add it here, or in the later proposal for §5.1 that adds the bundle's signature?
4. Public keys: the root lists JWKs as TUF does. With OIP-0008, should the root list key record UNIs instead, resolved
   inside the operator's trust domain (SOV-1)?
5. Firmware: ORCH-8 makes a SUIT manifest reference a MUST for `firmware` plans, while §5.8 says firmware SHOULD use
   SUIT. Is a device class without SUIT support updated through `software` plans, or should ORCH-8 be a SHOULD?
6. Series key: is (`deploymentUni`, `kind`) the right unit for version monotonicity, or should it be per node?
7. Not checked against the sources: the TUF specification version and section numbers, the RFC 9019 and RFC 9124
   clauses on manifest digests and sequence numbers. The terms used (roles, thresholds, rollback, freeze,
   mix-and-match attacks, root rotation) are TUF's as commonly described.
8. Timestamp lifetime: ORCH-5 ties it to the site cell's offline window as a SHOULD. Should a register give default
   lifetimes per role?

Acceptance needs appointed editors and a last-call window. Until then this proposal stays at DRAFT and the live tree
keeps the current `plan` contract and gap G-ORCH-1.
