# OIP-0027: NODE-CONSTRAINED profile: compact CBOR encoding, SenML mapping, gateway attestation and OSCORE/EDHOC

| Field | Value |
|---|---|
| Status | DRAFT |
| Functions | `join` (§5.1, gateway-assisted), `observe` (§5.5), `time` (§5.4); no new function (the register keeps exactly twelve) |
| Affects | §4.2 (rows CompactContext, EvidenceBatch), §6 (row for the constrained link), §7.1 (profile row), new §7.5 (NC-1 to NC-11); new schemas `compact-context` and `evidence-batch`; CDDL in `schemas/cddl/`; new register `registers/compact-keys.yaml`; profile `OBSP-NODE-CONSTRAINED` in `registers/conformance-profiles.yaml` and `conformance/suite.yaml`, and in the profile enumerations of `conformance-claim`, `conformance-report` and `participant-manifest`; `registers/functions.yaml` (objects of `join` and `observe`); vectors `conformance/vectors/compact/`; example sets `conformance/examples/OBSP-NODE-CONSTRAINED/` and `conformance/examples/negative/OBSP-NODE-CONSTRAINED/`; cases CT-NODE-140 to CT-NODE-148; reference code `sdk/python/src/obsp/compact.py`, `compact_gen.py`, `constrained.py` |
| Protocol version | obsp/0.1 |
| Work packages | WP-PR-01 (compact encoding), WP-SEC-09 (gateway attestation, constrained security), WP-IM-10 (SenML mapping) |
| Requires | OIP-0006 (the ObservationRecord at `4.0.0`, whose shape the CDDL describes) and OIP-0008 (COSE_Sign1 envelope and algorithm register, whose conventions the gateway signature follows). The patches are made against the live tree and apply to it on their own; the CDDL and the vectors are checked against the `4.0.0` schema with OIP-0006 applied |
| Last call | 90 days (a new profile and two new contracts) unless the editors rule otherwise |

## Problem

A sensor on a low-power radio cannot carry an observation as the protocol defines it. The ObservationRecord at `4.0.0`
is 920 bytes of JSON for one temperature reading, a typical LoRaWAN or NB-IoT payload allows tens of bytes, and no such
device holds a certificate chain, a JCS canonicaliser or a clock it can trust. Today a constrained device has no
protocol-level way in:

1. There is no compact encoding, so every vendor invents one, and each gateway converts it by its own rule (OIP-0007
   gives the connector contract, but not a common compact wire form).
2. Nothing says what a gateway must keep so that a value which crossed a gateway stays checkable: no list of what it
   received, no signature of the gateway over that list, no place for the gateway in provenance.
3. Per-message signatures (OIP-0008 SIG-1 asks observations to be signed) cost 64 bytes a message and a private key in
   the device; most constrained devices have neither.
4. A device without a clock stamps nothing, and §5.4 says every observation carries a TimeQuality. Gateways stamp such
   data today without saying so, so a gateway time looks like a device time.
5. §5.1 forbids long-lived shared secrets, yet the usual way to protect a constrained link is a pre-shared key.
6. Gateway systems already speak SenML (RFC 8428), and no rule says which observations map to it without loss.

The standards exist and the protocol must map to them, not replace them: CBOR (RFC 8949) and CDDL (RFC 8610) for the
encoding, COSE (RFC 9052) for the signature, SenML (RFC 8428), and OSCORE (RFC 8613) with EDHOC (RFC 9528) for the link.

## Change

### Normative text (new §7.5, summarised; the exact text is in `patches/spec.patch`)

| Id | Rule in short |
|---|---|
| NC-1 | Deterministic CBOR (RFC 8949 §4.2.1) over the CDDL and the published key table; receivers reject non-deterministic items and keys or enumeration indexes outside the table |
| NC-2 | Compact form and JSON form convert into each other without loss; a field with no key is an error; a timestamp that is not a whole second in UTC is kept as text |
| NC-3 | A message may leave out what its CompactContext fixes; the gateway expands it to a full ObservationRecord, points its evidence reference at the message digest, and refuses (and records) a message with an unknown context, property or unit |
| NC-4 | A device joins through its gateway: the gateway holds the NodeManifest and publishes the CompactContext (observer, index, gateway key, link to the manifest) |
| NC-5 | Size budget: a measured quantity message (context, property, value, time) is at most 40 bytes; an evidence batch of N messages at most 46 N + 480 bytes |
| NC-6 | A single-value measurement maps one to one to SenML and back; records SenML cannot carry are refused, never reduced |
| NC-7 | The gateway records the SHA-256 digest of every accepted message in an EvidenceBatch with a count, a batch digest and one gateway signature; the device signs nothing |
| NC-8 | The gateway signature is a COSE_Sign1 over the batch payload (identifiers, time, digest, references, declared times); the gateway is a software agent in the batch's provenance |
| NC-9 | A clockless device sends no time; the gateway declares it from its own sound clock and says so (`GATEWAY_DECLARED`, extension `obsp.nc`); a gateway whose time is compromised does not declare |
| NC-10 | The link is OSCORE (RFC 8613) with an EDHOC (RFC 9528) context; no long-lived shared secret as master secret; unprotected, replayed and unknown-context messages are refused |
| NC-11 | The gateway is itself an `OBSP-NODE`; its devices are not admitted participants on their own |

§4.2 gains the rows CompactContext and EvidenceBatch; §6 gains a row for the constrained link; §7.1 gains the profile
row; the profile is `OBSP-NODE-CONSTRAINED` with the functions `join`, `observe`, `time`.

### The compact encoding (NC-1, NC-2)

Every compact object is a CBOR map with small integer keys, in deterministic form: shortest integers and lengths,
definite lengths, map keys sorted bytewise by their encodings, floats in preferred form (RFC 8949 §4.2.1 and §4.2.2).
The CDDL is in `schemas/cddl/`:

| File | Rules |
|---|---|
| `obsp-common.cddl` | `ts`, `interval`, `digest`, `uni`, the enumerations, `record-ref`, `evidence-ref`, `policy-ref` |
| `obsp-observation.cddl` | `observation` (the whole `4.0.0` record) and `device-message` (what a device sends) |
| `obsp-time-quality.cddl` | `time-quality` (`3.1.0`) |
| `obsp-evidence-batch.cddl` | `evidence-batch` and `evidence-batch-payload` (the signed bytes) |
| `obsp-signature-envelope.cddl` | `cose-sign1-gateway` and its protected header |

`registers/compact-keys.yaml` publishes the key table (generated from `sdk/python/src/obsp/compact.py`; a test compares
it with the tables and with the CDDL, key by key and enumeration by enumeration). Hot fields have one-byte keys:

| Key | Field | Key | Field |
|---|---|---|---|
| 1 | `ctx` (context index, a message field only) | 7 | `resultState` |
| 2 | `observedProperty` (index into the context, or the id) | 8 | `uncertainty` |
| 3 | result value (`null` for no result) | 9 | `origin` |
| 4 | result unit | 10 | `subjectClass` |
| 5 | `phenomenonTime` (instant or `[start, end]`) | 11 | any other result form |
| 6 | `resultTime` | 12 to 28 | the other fields of the record |

Rules of the mapping, all tested:

- `schemaVersion`, `apiVersion`, `kind` and `protocolVersion` are implied by the rule and not sent.
- The result is flattened (keys 3, 4 and 11); an enumeration is sent as its index (the register lists them); a SHA-256
  digest is its 32 raw bytes, any other digest stays text; a UNI stays text.
- A timestamp that is a whole second in UTC from 1970 is an unsigned integer of epoch seconds; any other RFC 3339 text is
  kept verbatim as tag 0. Nothing is rounded, and the converter round-trips such values exactly.
- Free-form members (`extensions`, geometry coordinates, an uncertainty interval) keep their JSON text keys.
- A key outside a table, an index outside an enumeration or a field with no key is an error in both directions. An
  enumeration name that is not in the list is carried as text so that the schema, not the codec, refuses it.

Measured on the vectors: the full record of the first vector is 386 bytes compact against 920 bytes of JSON; the
device message of the same reading is 15 bytes.

### Contract: `compact-context` (schemaVersion `3.1.0`, `$id` `urn:obsp:schema:v3.1:compactcontext`)

What the gateway fixes for one device when it joins it. Required: `contextUni`, `contextIndex` (0 to 65535, the value of
key 1), `gateway` (`uni`, `kid`, `publicKeyJwk`: OKP Ed25519, public member only), `join` (`mode` `GATEWAY_ASSISTED` and
the versioned reference to the device's NodeManifest), `observer` (UNI and class), `procedure`, `featureOfInterest`,
`origin`, `subjectClass`, `properties` (`index` 0 to 255, `observedProperty` id, UCUM `unit`), `clock` (`DEVICE` or
`NONE`), `security` (`protocol` `OSCORE`, `keyEstablishment` `EDHOC`, optional `edhocMethod` and `edhocCipherSuite`),
`recordedAt`. Rules the schema states: `NONE` needs `timeQualityRef` (the gateway's TimeQuality); `PERSONS` needs
`subjectPolicyRef`; an `INSTRUMENT` observer needs `sensorPassportRef`. The gateway key sits in the context as the key
admitted at the join; once OIP-0008 is accepted a key record replaces that member (Open questions).

### Contract: `evidence-batch` (schemaVersion `3.1.0`, `$id` `urn:obsp:schema:v3.1:evidencebatch`)

Required: `batchUni`, `gatewayUni`, `closedAt`, `digestAlgorithm` (`sha-256`), `count`, `entries`, `batchDigest`,
`provenanceRef`, `gatewaySignature`, `recordedAt`; `timeQualityRef` is required when an entry is gateway-declared. An
entry has `digest` (`sha256:` and 64 lower-case hexadecimal), `timeSource` (`DEVICE` or `GATEWAY_DECLARED`), optional
`contextIndex`, and `declaredTime` exactly when the time is gateway-declared. An entry holds a digest, never message
content. `gatewaySignature` is `alg`, `kid`, `protected` and `signature` (unpadded base64url).

The batch digest is the SHA-256 of the deterministic CBOR array of the raw entry digests, in order. The signed payload is
the deterministic CBOR of the compact batch map without its entries (key 5, which the batch digest stands for) and
without the signature (key 9): so the identifiers, the closing time, the count, the digest, the references to the
gateway's TimeQuality and Provenance, and every declared time are covered by one signature, and a swapped reference
fails it (tested). The signature is COSE_Sign1 (RFC 9052 §4.2) with a detached payload, `alg` (label 1) and `kid`
(label 4) in the protected header, over Sig_structure (§4.4) with empty external data, as OIP-0008 SIG-2 builds it; its
JSON form repeats `alg` and `kid`, which must equal the header. The code point of Ed25519 is −19 (RFC 9864 §2.2, read in
the IANA COSE Algorithms registry).

Why a digest list and not a Merkle tree: a flat list costs 34 bytes a message, needs no proof format, and a verifier that
holds one message checks it against the signed list in one hash and one lookup. Inclusion proofs for very large batches
are left to a later proposal (Open questions).

### Gateway attestation and time (NC-7 to NC-9)

The device never signs. The gateway signs the evidence that it received the listed bytes in the listed order and
declared the stated times, and appears in Provenance (`provenance.schema.json`) as a `SOFTWARE_AGENT` in role `NODE` or
`SITE_CELL` associated with the activity that generated the batch. This is an attestation of receipt and declaration,
not of the measurement: the observation's `origin` and quality stay what the device and its context state.

A device whose context says `clock: NONE` sends no time. The gateway stamps the record with its own time, marks the
entry `GATEWAY_DECLARED` with that time, adds the extension `obsp.nc` (`timeSource`, `gatewayUni`) to the record, and
points its `timeQualityRef` at the gateway's own TimeQuality. A gateway whose TimeQuality reports spoofing, jamming or
an authentication failure does not declare a time (§5.4); the suite checks it (`compact-time-declared`).

### Security by reference (NC-10)

The device link is OSCORE (RFC 8613) with a security context established by EDHOC (RFC 9528); the profile defines no
parameter of either. EDHOC replaces the pre-shared master secret that §5.1 forbids, and OSCORE gives the replay window
and message protection. The context records `edhocMethod` and `edhocCipherSuite` so an auditor can see what was
negotiated. No test vector for OSCORE or EDHOC is published: both need an ephemeral key exchange and the published
traces (RFC 9529) were not read for this draft, so any vector here would be one this repository derived itself and
could not honestly call a known answer. Case CT-NODE-147 is manual.

### SenML mapping (NC-6)

A single-value measurement maps to one SenML record (RFC 8428):

| ObservationRecord | SenML |
|---|---|
| `observer.observerUni` followed by `/` | `bn` |
| `observedProperty` | `n` (the resolved name is `bn` and `n` joined) |
| `result.unit` | `u` |
| `result.value` | `v` |
| `phenomenonTime` (whole second, UTC) | `bt`, seconds since the epoch |

The mapping back returns the measurement (observer, property, value, unit, time); everything else comes from the
context, as for any device message. These are refused with a reason: a result that is not a quantity; `uncertainty`,
`quality`, `resultState`, `validTime`, `times` or `geometry` (SenML has no field for them); an interval time; a result
time that differs from the phenomenon time; a unit that has no SenML form; a name with characters SenML names do not
allow (`@` is allowed in a UNI and not in a SenML name). The unit list of the draft is `Cel K m kg s A Hz Pa W V J m/s
m2 m3 l W/m2 %`, which are UCUM codes believed to be in the SenML units registry (`checked: not checked`); any other
unit, for example `hPa`, is refused rather than renamed. The CBOR labels of RFC 8428 §6 are not used on the wire here.

### Size budget (NC-5)

| What | Bound | Evidence |
|---|---|---|
| a measured quantity message: `ctx` (≤ 65535), property index (≤ 255), value (any float64), time (32-bit epoch), optionally a unit (≤ 8 characters) and a result time | 40 bytes | the worst case is 36 bytes; 2000 random messages inside the limits are at most 40; one more byte of unit breaks it (all tested) |
| the vectors | 15, 14, 23 and 9 bytes | `messages.json` |
| an evidence batch of N messages with identifiers of at most 48 characters | 46 N + 480 bytes | tested for N from 1 to 4000, with and without declared times; 34 bytes of digest, up to 3 of context index, up to 9 of declared time per entry |
| the four-message vector | 498 bytes | `batch.json` (136 bytes of digests; the rest is identifiers, references and the signature) |
| a batch of 100 messages | 4168 bytes (42 a message), under the 64 bytes of one Ed25519 signature a message | tested; with the worst-case identifiers a batch is cheaper than a signature per message from N = 18 |

### Profile

`OBSP-NODE-CONSTRAINED`: `join` (gateway-assisted), `observe`, `time`. It does not extend `OBSP-NODE`: the device is not a
node of the grid; its gateway is (NC-11).

### Relation rows to add on acceptance

Not added by the patches (so that `registers/implements-edges.json`, a generated file, stays untouched, as in OIP-0010):
`EXT-CBOR` (IETF RFC 8949), `EXT-CDDL` (RFC 8610), `EXT-SENML` (RFC 8428, schemas `observation-record`), `EXT-COAP` (RFC 7252),
`EXT-OSCORE` (RFC 8613), `EXT-EDHOC` (RFC 9528), all `aligns-with`, with profile `OBSP-NODE-CONSTRAINED`; `EXT-COSE` is
added by OIP-0008.

## Compatibility

Additive. Two new contracts (`schemaVersion` `3.1.0` like the other new objects of `obsp/0.1`), one new profile, one
new register, a profile id added to three enumerations. No existing document changes; no function is added. A node that
does not claim `OBSP-NODE-CONSTRAINED` is unaffected. The CDDL describes the ObservationRecord at `4.0.0`; if OIP-0006 is
not accepted, the observation rules wait for it (the other rules and the evidence batch do not depend on it). If
OIP-0008 changes the envelope, `gatewaySignature` is replaced by the envelope, which has the same fields and the same
signed-bytes rule for COSE; only the payload rule (the compact map, not JCS) needs its `canonicalization` value
(Open questions). The patches touch lines other drafts also touch (see `README.md`).

## Tests

| Requirement | Case | Kind | Evidence and invariants |
|---|---|---|---|
| NC-4, NC-10 | CT-NODE-140 | invariant | `compact-context`, `node-manifest`; `compact-context-device-joined` |
| NC-5, NC-7 | CT-NODE-141 | invariant | `evidence-batch`, `compact-context`; `evidence-batch-consistent`, `evidence-batch-size-budget` |
| NC-8 | CT-NODE-142 | invariant | `evidence-batch`, `provenance`; `evidence-batch-gateway-agent` |
| NC-9 | CT-NODE-143 | invariant | `time-quality`, `evidence-batch`, `compact-context`; `compact-time-declared` |
| NC-1, NC-2, NC-3 | CT-NODE-144 | manual | the codec over every vector of `conformance/vectors/compact/` |
| NC-6 | CT-NODE-145 | manual | the SenML vectors and refusals |
| NC-5 | CT-NODE-146 | manual | worst-case message and batch sizes |
| NC-10 | CT-NODE-147 | manual | a capture of the EDHOC exchange and the refusals; no vector (see above) |
| NC-11 | CT-NODE-148 | manual | the gateway's own `OBSP-NODE` report and the participant list |

All cases belong to `OBSP-NODE-CONSTRAINED`. The example set passes (the two cases that need a radio capture or a
grid's participant list are attested `NOT_APPLICABLE` in the example set, with the reason); the negative set (one batch
whose first digest no longer matches its message) fails only CT-NODE-141.

Positive fixtures: `compact-context` (`valid`, `clockless.valid`), `evidence-batch` (`valid`, `device-time.valid`).
Negative fixtures, one broken rule each: `compact-context.` `not-gateway-assisted`, `missing-security`,
`security-not-oscore`, `static-key-establishment`, `clockless-without-time-quality`, `unknown-clock`,
`property-not-registered-shape`, `property-without-unit`, `negative-property-index`, `no-properties`,
`private-key-in-jwk`, `gateway-key-not-ed25519`, `instrument-without-passport`, `persons-without-policy`;
`evidence-batch.` `missing-signature`, `padded-signature`, `no-entries`, `digest-not-sha256`, `digest-upper-case`,
`unknown-time-source`, `declared-without-time`, `device-time-with-declared-time`, `declared-without-time-quality`,
`missing-provenance`, `zero-count`, `digest-algorithm`, `entry-with-extra-field`, `signature-inline-private`.

Vectors (`conformance/vectors/compact/`, regenerated and compared by `python3 -m obsp.compact check-all` and the
repository tests): `messages.json` (four device messages as hex, their digests and the records they expand to, the
worst case), `batch.json` (digest list, payload, protected header, Sig_structure, signature, tagged COSE_Sign1 and the
compact batch as hex; the test key is derived from a public label and marked test-only; seven invalid cases),
`roundtrip.json` (six full documents in both directions: three expanded records, a record with an interval, geometry,
quality flag and a fractional-second time, a TimeQuality and the batch), `senml.json` (two packs and six refusals),
`context.json` and `context-noclock.json`. The Ed25519 implementation (RFC 8032 reference algorithm, plain Python) is
checked against RFC 8032 §7.1 tests 1 and 2 and against an independent library; the CBOR codec against the RFC 8949
Appendix A values it was written from (38 items, both ways) and 16 malformed inputs it must refuse. The expansions of the
vectors validate against the `observation-record` `4.0.0` schema of OIP-0006 (checked with that patch applied; the
repository test skips itself while the schema is `3.1.0`).

Unit tests: 41 in `sdk/python/tests/test_compact.py` (codec, Ed25519, converter, expansion refusals, batch faults, size
budget, SenML) and 21 in `tests/test_compact_profile.py` (CDDL against the key tables and the register, artefacts
current, profile example sets, each invariant failing on one fault, requirement coverage). The CDDL was not run through
a CDDL tool (none is available in this environment); the repository test ties it to the key tables mechanically.

## Open questions

1. Not checked against the source: RFC 8949 §4.2.1 and §4.2.2 clause numbers; RFC 9052 §4.2 and §4.4; RFC 8428 §6 (CBOR
   labels) and §12.1 (the units list is from memory); RFC 8613 §7.4 (replay window); RFC 9528 §3.2 and §3.6 (method and
   cipher suite values); RFC 8037 (OKP keys); RFC 7252. The COSE code point −19 for Ed25519 was read in the IANA registry.
2. CDDL was written by hand and not run through a CDDL validator; the editors should run one.
3. A compact media type and CoAP Content-Format number are not allocated; the draft names none. Who registers them?
4. The gateway key in `compact-context` is a placeholder for the key record of OIP-0008 (SIG-5). Which should be
   authoritative, the join or a key record?
5. The signed payload is the compact batch map, not JCS. OIP-0008's envelope fixes `canonicalization` to `JCS`; does it
   gain a second value for compact objects, or is the batch a separate signing context?
6. Should an observation of a constrained device be exempt from OIP-0008 SIG-1 ("observations SHOULD be signed") because
   the batch signature covers it? This draft says yes by construction (the evidence reference names the digest) and
   states it nowhere else.
7. Flat digest list or Merkle tree for batches beyond a few hundred messages? The flat list is simpler to verify and
   costs 34 bytes a message; a tree allows inclusion proofs of constant size.
8. Is 40 bytes the right budget? The figure was chosen to leave room under typical low-power radio payloads (about 50
   bytes at the slowest LoRaWAN data rates, more on NB-IoT); none of those limits was checked for this draft.
9. The SenML unit list is conservative; the editors should replace it with the registry after reading RFC 8428 §12.1.
10. Gateway role in provenance: `NODE` or `SITE_CELL`. Should a gateway have its own protocol role? That would change
    `ProtocolRole`, which every schema copies; this draft does not.
11. Case numbers CT-NODE-140 to CT-NODE-148 are the next free hundred; parallel drafts may need renumbering.
12. Is `OBSP-NODE-CONSTRAINED` better as an extension of `OBSP-NODE` (the gateway) with the device rules as a part? This
    draft keeps them apart so that a device is never read as a node of the grid.

Acceptance needs appointed editors and a last-call window. Until then this proposal stays at DRAFT and the live tree keeps
its profiles and objects.
