feat(poc-a): gate-a dynamic elf proof of concept (#13)
## Summary PoC-A proved that dynamically loaded ELF plugins are workable on ESP32-S3: plugins ship as signed MPB packages, are parsed by a strict single-type parser shared between host and target, and execute from PSRAM+IRAM within a measured admission budget. A 1000-cycle load/unload soak passed 18/18 trend gates with stable heaps, the capacity ladder settled at 8 coexisting plugin instances with a 3356B per-instance budget, and the fail-closed corpus executed zero attacker-controlled bytes across 14 rejection classes. Scheduler semantics (strand FIFO ordering, completion-cancel CAS, quiesce-as-noop ABI) were validated on both host (TSan suite) and target. **Gate-A verdict: PASS** — ADR-0002 moves to **Accepted** with the capacity value 8 and global-sum admission rule as PoC-derived proposals. ## Changes by area Grouped from `git log main..HEAD --oneline` (18 commits): - **Vendored loader + patches**: `4bb53f2` vendor elf_loader 1.3.3 + harness scaffold; `2f67b4f` plugin_iram loader patch with budget consistency (p1-p5 patch series in `PATCHES.md`). - **MPB toolchain**: `4793cc2` deterministic MPB manifest builder with test signing; `408df44` strict single-type MPB parser shared host/target; `796f9d2` host MPB corpus + policy CI lane. - **Plugin pipeline + probes**: `21d2842` plugin pipeline and first PSRAM execution on target; `cab9664` relocation/import allowlist matrix with probes; `c5d6c6c` C++ feature rejection matrix. - **Harness commands**: `e0e7159` generation coexistence with measured admission budget; `2a37556` thousand-cycle lifecycle soak with heap trend gate; `093a72b` on-target scheduler semantics validation (+ `eea3876` clang-format-18 sweep); `a302151` capacity ladder with signed budget; `f34dc72` fail-closed corpus with execution sentinel. - **Host prototypes**: `6198bca` strand FIFO prototype with TSan suite; `1c3ad59` completion-cancel CAS and cleanup semantics. - **Evidence + ADR/docs**: `887d0f7` Gate-A manifest and report; `1c29e8a` ADR-0002 outcome + frozen PoC values. ## QA evidence - `poc/evidence/poc-a-20260901/manifest.json` — 67-artifact hash index, every sha256 recomputed at assembly. - `poc/evidence/poc-a-20260901/GATE-A-REPORT.md` — full nine-item development-plan §4 mapping; TEST-003 10/10 clauses. - 14 verbatim per-task transcripts (`task-1` … `task-15-*.log`) + fresh build artifacts (`build/frame_poc_a.map`, size components). - Frozen in-repo references: `ALLOWLIST.md`, `REJECT-MATRIX.md`, `ADMISSION-BUDGET.md`, `soak_samples_1000.csv`, fc corpus (33 committed `.mpb` containers). - F1-F4 final-wave verdicts: **APPROVE**. - Key numbers: soak **18/18** trend gates over 1000 cycles; capacity **8/8** instances, **3356B** per-instance; fc corpus **14 rejection classes, zero attacker bytes executed**; **4 upstream loader defects found and patched** (p1-p5). ## Requirement IDs (GOV-006) ADR-0002; TEST-002, TEST-003; PLUG-001, PLUG-004, PLUG-008, PLUG-009; MEM-001, MEM-003, MEM-007, MEM-008; LIFE-001, LIFE-002, LIFE-005, LIFE-007; SEC-002, SEC-003, SEC-004, SEC-005; GH-003; GOV-004, GOV-005. ## Risks / known limitations From `GATE-A-REPORT.md` §(f) plus the trade-off table: 1. **Capacity descent path 7-5 unexercised** — rung 8 passed first attempt; rungs 7/6/5 and retry-once logic never ran on hardware (untested against real OOM). 2. **No Wi-Fi in the harness (MEM-001 caveat)** — all PSRAM/IRAM budget numbers are radio-off harness-class; product admission budgets must be re-measured with the network stack active. 3. **On-target scheduler vectors are a subset of the host suite** (256-node pool / 2e4 storm posts vs host 31000 / 1e5); consistency declaration covers only the vectors actually run. 4. **Quiesce is a no-op by ABI semantics** — plugin ABI 1.2 has no quiesce slot; NULL entry is defined as trivial success; real quiesce coordination is later-gate work. 5. **MEMPROT=n trade-off** — `CONFIG_ESP_SYSTEM_MEMPROT=n` is required for the IDF exec heap (`MALLOC_CAP_EXEC`) that `.plugin_iram` needs; software-only PMP config, no eFuse burned (documented in `PATCHES.md` p5); production must re-decide security vs plugin IRAM. 6. **Owner sign-off PENDING** — Gate-A PASS is the PoC team's verdict; formal acceptance of ADR-0002 awaits the owner. ## Not in scope - Product components (GOV-004) — this PR is PoC evidence only; no product code paths change. - **Merge is the owner's call** — do not merge without owner sign-off on ADR-0002. --------- Co-authored-by: ania-lain <[email protected]>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"src/mpb_mbedtls_glue.c"
|
||||
"src/mpb_parser.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES frame_poc_runtime
|
||||
PRIV_REQUIRES mbedtls
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef FRAME_POC_MPB_MBEDTLS_GLUE_H
|
||||
#define FRAME_POC_MPB_MBEDTLS_GLUE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "frame_poc/frame_abi.h"
|
||||
#include "frame_poc/mpb_parser.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Target-side crypto injection for mpb_parse() on top of the mbedtls PSA
|
||||
* crypto API shipped with ESP-IDF (mbedtls 4.x). Compiled only inside the
|
||||
* ESP-IDF build; host tests inject their own callbacks instead. The host
|
||||
* parser source itself never includes this header. */
|
||||
|
||||
typedef struct {
|
||||
uint32_t key_id;
|
||||
uint8_t uncompressed_point[65]; /* 0x04 || X || Y, secp256r1 */
|
||||
} mpb_trusted_key_t;
|
||||
|
||||
/* Fill out_crypto with PSA-backed sha256_fn and ecdsa_verify_fn bound to the
|
||||
* given trusted keys. psa_crypto_init() must have succeeded before the first
|
||||
* mpb_parse() call; this maker returns FRAME_ERR_INVALID_ARGUMENT for null
|
||||
* arguments and FRAME_ERR_SIGNATURE_INVALID when a key cannot be imported. */
|
||||
frame_err_t mpb_mbedtls_crypto_make(const mpb_trusted_key_t* trusted_keys, size_t trusted_key_count,
|
||||
mpb_crypto_t* out_crypto);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,230 @@
|
||||
#ifndef FRAME_POC_MPB_PARSER_H
|
||||
#define FRAME_POC_MPB_PARSER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "frame_poc/frame_abi.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Strict single-type MPB container parser (poc-a task T3).
|
||||
*
|
||||
* The container layout, the frozen offsets and the verification order are
|
||||
* defined by tools/frame_tools/manifest_builder.py (appendix A of
|
||||
* _doc/project.md section 9, v4.2 single-type semantics of PLUG-001). That
|
||||
* python module is the reference oracle; this parser must agree with it.
|
||||
*
|
||||
* Layout contract (little-endian, field-by-field reads, no unaligned casts):
|
||||
* fixed_header 92B magic u32 0x4D504246 ("FBPM") @0 | format_major u16 @4
|
||||
* format_minor u16 @6 | header_size u32 @8 | total_size
|
||||
* u32 @12 | flags u32 @16 (bit0 resource-only) |
|
||||
* target_id u32 @20 | core_abi_major/minor u16 @24/@26 |
|
||||
* required/optional features u64 @28/@36 |
|
||||
* manifest_offset u32 @44 | manifest_length u32 @48 |
|
||||
* payload_table_offset u32 @52 | payload_count u16 @56
|
||||
* (1..16) | payload_entry_size u16 @58 (=60) |
|
||||
* signature_offset u32 @60 | signature_length u32 @64
|
||||
* (=72) | key_id u32 @68 | security_epoch u32 @72 |
|
||||
* reserved[4] u32 @76 (ignored on read)
|
||||
* manifest TLV stream: field_id u16 | flags u16 | length u32 | value.
|
||||
* ids 1..35 are canonical; unknown REQUIRED ids reject,
|
||||
* unknown OPTIONAL ids are skipped; duplicated known
|
||||
* ids reject; name(1) and version(2) must be present.
|
||||
* payload_table count * 60B entries: type u16 @0 (ELF=1/RESOURCE=2) |
|
||||
* flags u16 @2 (REQUIRED=0x1, COMPRESSED=0x2 forbidden
|
||||
* for ELF) | offset u32 @4 | length u32 @8 |
|
||||
* unpacked u32 @12 (== length) | alignment u32 @16
|
||||
* (power of two, 1..4096) | sha256[32] @20 |
|
||||
* reserved u32[2] @52 (ignored on read)
|
||||
* payloads live in [table_end, signature_offset); alignment padding
|
||||
* inside the window is unauthenticated by design and is
|
||||
* never read by this parser
|
||||
* signature 72B algorithm u16 (=1) @0 | record_version u16 (=1) @2 |
|
||||
* key_id u32 @4 (must equal header key_id) | IEEE P1363
|
||||
* r||s 64B @8. Digest = SHA-256(header || manifest ||
|
||||
* payload_table) over the contiguous prefix
|
||||
* [0, table_end) of the staging buffer.
|
||||
*
|
||||
* Verification order is hardcoded and must not be bypassed (SEC-004):
|
||||
* 1. structure/bounds/type checks -> FRAME_ERR_PACKAGE_INVALID
|
||||
* 2. signature (ECDSA P-256 over digest) -> FRAME_ERR_SIGNATURE_INVALID
|
||||
* 3. per-payload SHA-256 -> FRAME_ERR_HASH_MISMATCH
|
||||
* 4. epoch/target/ABI/features policy -> FRAME_ERR_EPOCH_ROLLBACK,
|
||||
* FRAME_ERR_TARGET_MISMATCH, FRAME_ERR_ABI_MISMATCH
|
||||
*
|
||||
* Hostile-input rules: malformed input is the subject. Every integer is read
|
||||
* byte by byte, every region computation is carried out in uint64_t before it
|
||||
* is narrowed, reserved fields are ignored, and the parser never touches
|
||||
* payload-window padding bytes. The parser performs no allocation and keeps
|
||||
* no global state; it never writes to the staging buffer (PLUG-008 immutable
|
||||
* staging: the caller owns the buffer and must keep it alive as long as the
|
||||
* returned view is used, because view fields point into it).
|
||||
*
|
||||
* Semantics note: this parser validates container structure, TLV shape,
|
||||
* signature, hashes and device policy. Deep manifest value semantics (utf-8
|
||||
* validity, strict semver syntax, quota cross-field rules) stay in the python
|
||||
* oracle: manifest bytes are inside the signed region, so post-signature
|
||||
* tampering is already rejected, and the builder refuses to emit containers
|
||||
* that violate the semantic rules.
|
||||
*/
|
||||
|
||||
#define MPB_MAGIC 0x4D504246u /* bytes 46 42 50 4D little-endian */
|
||||
#define MPB_TARGET_ESP32S3 0x33505345u /* "ESP3" */
|
||||
#define MPB_HEADER_SIZE 92u
|
||||
#define MPB_PAYLOAD_ENTRY_SIZE 60u
|
||||
#define MPB_SIGNATURE_RECORD_SIZE 72u
|
||||
#define MPB_TLV_HEADER_SIZE 8u
|
||||
#define MPB_MAX_PAYLOAD_COUNT 16u
|
||||
#define MPB_MAX_NAME_BYTES 64u
|
||||
#define MPB_MAX_VERSION_BYTES 32u
|
||||
#define MPB_MAX_REQUIRES_ENTRIES 32u
|
||||
#define MPB_MAX_REQUIRE_BYTES 96u
|
||||
#define MPB_MAX_ALIGNMENT 4096u
|
||||
|
||||
#define MPB_FLAG_RESOURCE_ONLY 0x00000001u
|
||||
|
||||
#define MPB_PAYLOAD_TYPE_ELF 1u
|
||||
#define MPB_PAYLOAD_TYPE_RESOURCE 2u
|
||||
|
||||
#define MPB_PAYLOAD_FLAG_REQUIRED 0x0001u
|
||||
#define MPB_PAYLOAD_FLAG_COMPRESSED 0x0002u
|
||||
|
||||
#define MPB_SIG_ALGORITHM_ECDSA_P256_SHA256 1u
|
||||
#define MPB_SIG_RECORD_VERSION 1u
|
||||
|
||||
#define MPB_TLV_FLAG_REQUIRED 0x0001u
|
||||
|
||||
typedef enum {
|
||||
MPB_PACKAGE_CODE = 1, /* exactly one ELF payload, flags bit0 clear */
|
||||
MPB_PACKAGE_RESOURCE_ONLY = 2, /* 1..16 resource payloads, flags bit0 set */
|
||||
} mpb_package_kind_t;
|
||||
|
||||
/* Canonical manifest TLV field ids (1..35); see manifest_builder._FIELD_DESCS. */
|
||||
enum {
|
||||
MPB_TLV_NAME = 1,
|
||||
MPB_TLV_VERSION = 2,
|
||||
MPB_TLV_REQUIRES = 3,
|
||||
MPB_TLV_MAX_MEMORY_BYTES = 4,
|
||||
MPB_TLV_IRAM_REQUIRED_BYTES = 5,
|
||||
MPB_TLV_CORE_AFFINITY = 6,
|
||||
MPB_TLV_STRAND_QUEUE_CAPACITY = 7,
|
||||
MPB_TLV_MAX_OUTSTANDING_OPERATIONS = 8,
|
||||
MPB_TLV_MAX_TIMERS = 9,
|
||||
MPB_TLV_MAX_MANAGED_TASKS = 10,
|
||||
MPB_TLV_MANAGED_TASK_PRIORITY = 11,
|
||||
MPB_TLV_MANAGED_TASK_STACK_SIZE = 12,
|
||||
MPB_TLV_HEARTBEAT_INTERVAL_MS = 13,
|
||||
MPB_TLV_EVENT_RATE_LIMIT = 14,
|
||||
MPB_TLV_MAX_EVENT_SUBSCRIPTIONS = 15,
|
||||
MPB_TLV_MAX_EVENT_MESSAGE_BYTES = 16,
|
||||
MPB_TLV_MAX_EVENT_BUFFER_BYTES = 17,
|
||||
MPB_TLV_MAX_OUTSTANDING_RPC = 18,
|
||||
MPB_TLV_MAX_DISPLAY_COMMANDS = 19,
|
||||
MPB_TLV_MAX_DISPLAY_BYTES = 20,
|
||||
MPB_TLV_MAX_STORAGE_REQUESTS = 21,
|
||||
MPB_TLV_MAX_STORAGE_IO_BYTES = 22,
|
||||
MPB_TLV_MAX_STORAGE_COPY_BYTES = 23,
|
||||
MPB_TLV_MAX_TCP_CONNECTIONS = 24,
|
||||
MPB_TLV_MAX_UDP_SOCKETS = 25,
|
||||
MPB_TLV_MAX_NETWORK_REQUESTS = 26,
|
||||
MPB_TLV_MAX_NETWORK_BUFFER_BYTES = 27,
|
||||
MPB_TLV_MAX_NETWORK_IO_BYTES = 28,
|
||||
MPB_TLV_MAX_LOG_RECORDS_PER_SECOND = 29,
|
||||
MPB_TLV_MAX_LOG_BUFFER_BYTES = 30,
|
||||
MPB_TLV_MAX_RESOURCE_LEASES = 31,
|
||||
MPB_TLV_MAX_UPDATE_BACKLOG_EVENTS = 32,
|
||||
MPB_TLV_MAX_UPDATE_BACKLOG_BYTES = 33,
|
||||
MPB_TLV_STATE_SCHEMA_ID = 34,
|
||||
MPB_TLV_STATE_SCHEMA_VERSION = 35,
|
||||
};
|
||||
|
||||
/* Injected crypto backend (SEC-002/SEC-004). The same parser source compiles
|
||||
* on host (tests inject a reference SHA-256 plus a known-answer ECDSA double)
|
||||
* and on target (mpb_mbedtls_glue.c injects PSA crypto / mbedtls). */
|
||||
typedef struct mpb_crypto {
|
||||
void* ctx; /* caller-owned context passed to every callback */
|
||||
/* One-shot SHA-256 over data[0..length). Always writes digest_out. */
|
||||
void (*sha256_fn)(void* ctx, const uint8_t* data, size_t length, uint8_t digest_out[32]);
|
||||
/* ECDSA P-256 verification of the IEEE P1363 r||s signature over the
|
||||
* 32 byte digest, restricted to key_id. Returns 1 when the signature is
|
||||
* valid for a trusted key with that id, 0 otherwise (unknown key_id
|
||||
* counts as invalid). */
|
||||
int (*ecdsa_verify_fn)(void* ctx, uint32_t key_id, const uint8_t digest[32],
|
||||
const uint8_t signature_p1363[64]);
|
||||
} mpb_crypto_t;
|
||||
|
||||
/* Device policy applied in the final verification stage. The epoch floor is
|
||||
* the compile-time virtual floor of the PoC (no eFuse counters are burned). */
|
||||
typedef struct mpb_policy {
|
||||
uint32_t max_package_bytes; /* FRAME hard maximum, e.g. 512*1024 for PoC */
|
||||
uint32_t epoch_floor;
|
||||
uint16_t abi_major;
|
||||
uint16_t abi_minor;
|
||||
uint64_t device_features;
|
||||
} mpb_policy_t;
|
||||
|
||||
/* Verified byte span inside the caller's staging buffer. */
|
||||
typedef struct {
|
||||
size_t offset;
|
||||
size_t length;
|
||||
} mpb_span_t;
|
||||
|
||||
/* Result view. All pointers refer to the immutable staging buffer passed to
|
||||
* mpb_parse(); they stay valid exactly as long as that buffer stays alive
|
||||
* and unmodified. No payload bytes are copied. */
|
||||
typedef struct {
|
||||
const uint8_t* staging; /* staging base the spans and pointers refer to */
|
||||
mpb_package_kind_t package_kind;
|
||||
uint16_t payload_count;
|
||||
mpb_span_t payloads[MPB_MAX_PAYLOAD_COUNT];
|
||||
/* fixed header fields the loader/harness consume */
|
||||
uint32_t security_epoch;
|
||||
uint32_t key_id;
|
||||
uint32_t target_id;
|
||||
uint16_t core_abi_major;
|
||||
uint16_t core_abi_minor;
|
||||
uint64_t required_features;
|
||||
uint64_t optional_features;
|
||||
/* manifest essentials (zero when the package does not declare them) */
|
||||
const uint8_t* name; /* not NUL terminated; use name_length */
|
||||
size_t name_length;
|
||||
const uint8_t* version;
|
||||
size_t version_length;
|
||||
uint32_t max_memory_bytes;
|
||||
uint32_t iram_required_bytes;
|
||||
/* manifest region for mpb_manifest_field() */
|
||||
uint32_t manifest_offset;
|
||||
uint32_t manifest_length;
|
||||
uint64_t seen_fields_mask; /* bit (id-1) set for every decoded TLV id */
|
||||
} mpb_view_t;
|
||||
|
||||
/* Raw manifest TLV accessor for verified views: returns FRAME_OK and fills
|
||||
* out with the field's flags and value span (pointing into staging), or
|
||||
* FRAME_ERR_NOT_FOUND when the package does not carry the field. */
|
||||
typedef struct {
|
||||
uint16_t field_id;
|
||||
uint16_t flags;
|
||||
const uint8_t* value; /* points into the staging buffer */
|
||||
uint32_t value_length;
|
||||
} mpb_field_t;
|
||||
|
||||
/* Parse and fully verify an MPB container held in an immutable staging
|
||||
* buffer. Returns FRAME_OK and fills out_view, or a FRAME_ERR_* code with
|
||||
* out_view zeroed. Fails closed: any malformed byte, unknown construct,
|
||||
* wrong key, bad signature, hash mismatch or policy violation rejects the
|
||||
* whole container before any payload byte is executed or re-parsed. */
|
||||
frame_err_t mpb_parse(const uint8_t* staging, size_t size, const mpb_policy_t* policy,
|
||||
const mpb_crypto_t* crypto, mpb_view_t* out_view);
|
||||
|
||||
frame_err_t mpb_manifest_field(const mpb_view_t* view, uint16_t field_id, mpb_field_t* out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "frame_poc/mpb_mbedtls_glue.h"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
typedef struct {
|
||||
const mpb_trusted_key_t* keys;
|
||||
size_t key_count;
|
||||
} mpb_glue_ctx_t;
|
||||
|
||||
static mpb_glue_ctx_t mpb_glue_ctx;
|
||||
|
||||
/* PSA ECDSA uses the fixed-length r||s (IEEE P1363) signature format and the
|
||||
* uncompressed-point public key format, which matches the container contract
|
||||
* (manifest_builder SIGNATURE_RECORD / P1363 r||s) without conversions. */
|
||||
|
||||
static void mpb_glue_sha256(void* ctx, const uint8_t* data, size_t length, uint8_t digest_out[32]) {
|
||||
(void)ctx;
|
||||
size_t produced = 0;
|
||||
(void)psa_hash_compute(PSA_ALG_SHA_256, data, length, digest_out, 32, &produced);
|
||||
}
|
||||
|
||||
static const mpb_trusted_key_t* mpb_glue_find_key(uint32_t key_id) {
|
||||
for (size_t i = 0; i < mpb_glue_ctx.key_count; ++i) {
|
||||
if (mpb_glue_ctx.keys[i].key_id == key_id) {
|
||||
return &mpb_glue_ctx.keys[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int mpb_glue_verify(void* ctx, uint32_t key_id, const uint8_t digest[32],
|
||||
const uint8_t signature_p1363[64]) {
|
||||
(void)ctx;
|
||||
const mpb_trusted_key_t* key = mpb_glue_find_key(key_id);
|
||||
if (key == NULL) {
|
||||
return 0;
|
||||
}
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA_ANY);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_key_id_t imported = 0;
|
||||
psa_status_t status = psa_import_key(&attributes, key->uncompressed_point,
|
||||
sizeof(key->uncompressed_point), &imported);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return 0;
|
||||
}
|
||||
status = psa_verify_hash(imported, PSA_ALG_ECDSA_ANY, digest, 32, signature_p1363, 64);
|
||||
(void)psa_destroy_key(imported);
|
||||
return status == PSA_SUCCESS;
|
||||
}
|
||||
|
||||
frame_err_t mpb_mbedtls_crypto_make(const mpb_trusted_key_t* trusted_keys, size_t trusted_key_count,
|
||||
mpb_crypto_t* out_crypto) {
|
||||
if (trusted_keys == NULL || trusted_key_count == 0 || out_crypto == NULL) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
for (size_t i = 0; i < trusted_key_count; ++i) {
|
||||
if (trusted_keys[i].uncompressed_point[0] != 0x04) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
if (psa_crypto_init() != PSA_SUCCESS) {
|
||||
return FRAME_ERR_SIGNATURE_INVALID;
|
||||
}
|
||||
mpb_glue_ctx.keys = trusted_keys;
|
||||
mpb_glue_ctx.key_count = trusted_key_count;
|
||||
out_crypto->ctx = &mpb_glue_ctx;
|
||||
out_crypto->sha256_fn = &mpb_glue_sha256;
|
||||
out_crypto->ecdsa_verify_fn = &mpb_glue_verify;
|
||||
return FRAME_OK;
|
||||
}
|
||||
@@ -0,0 +1,454 @@
|
||||
#include "frame_poc/mpb_parser.h"
|
||||
|
||||
/* Byte-by-byte little-endian readers: no unaligned casts, no padding reads,
|
||||
* no signed shift overflow. All region arithmetic below runs in uint64_t
|
||||
* before any narrowing comparison. */
|
||||
|
||||
static uint16_t mpb_rd16(const uint8_t* p) {
|
||||
return (uint16_t)((uint16_t)p[0] | (uint16_t)((uint16_t)p[1] << 8));
|
||||
}
|
||||
|
||||
static uint32_t mpb_rd32(const uint8_t* p) {
|
||||
return (uint32_t)p[0] | (uint32_t)((uint32_t)p[1] << 8) | (uint32_t)((uint32_t)p[2] << 16) |
|
||||
(uint32_t)((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
static uint64_t mpb_rd64(const uint8_t* p) {
|
||||
return (uint64_t)mpb_rd32(p) | ((uint64_t)mpb_rd32(p + 4) << 32);
|
||||
}
|
||||
|
||||
/* Constant-time equality for secrets and digests. */
|
||||
static int mpb_bytes_equal(const uint8_t* a, const uint8_t* b, size_t length) {
|
||||
uint8_t diff = 0;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
diff |= (uint8_t)(a[i] ^ b[i]);
|
||||
}
|
||||
return diff == 0;
|
||||
}
|
||||
|
||||
static void mpb_zero_view(mpb_view_t* view) {
|
||||
uint8_t* p = (uint8_t*)view;
|
||||
for (size_t i = 0; i < sizeof(*view); ++i) {
|
||||
p[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Canonical manifest field kinds (mirrors manifest_builder._FIELD_DESCS). */
|
||||
typedef enum {
|
||||
MPB_KIND_U32 = 1,
|
||||
MPB_KIND_NAME,
|
||||
MPB_KIND_SEMVER,
|
||||
MPB_KIND_STRLIST,
|
||||
MPB_KIND_AFFINITY,
|
||||
MPB_KIND_SCHEMA_ID,
|
||||
} mpb_kind_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t id;
|
||||
uint8_t kind;
|
||||
} mpb_field_desc_t;
|
||||
|
||||
static const mpb_field_desc_t mpb_field_table[35] = {
|
||||
{1, MPB_KIND_NAME}, {2, MPB_KIND_SEMVER}, {3, MPB_KIND_STRLIST}, {4, MPB_KIND_U32},
|
||||
{5, MPB_KIND_U32}, {6, MPB_KIND_AFFINITY}, {7, MPB_KIND_U32}, {8, MPB_KIND_U32},
|
||||
{9, MPB_KIND_U32}, {10, MPB_KIND_U32}, {11, MPB_KIND_U32}, {12, MPB_KIND_U32},
|
||||
{13, MPB_KIND_U32}, {14, MPB_KIND_U32}, {15, MPB_KIND_U32}, {16, MPB_KIND_U32},
|
||||
{17, MPB_KIND_U32}, {18, MPB_KIND_U32}, {19, MPB_KIND_U32}, {20, MPB_KIND_U32},
|
||||
{21, MPB_KIND_U32}, {22, MPB_KIND_U32}, {23, MPB_KIND_U32}, {24, MPB_KIND_U32},
|
||||
{25, MPB_KIND_U32}, {26, MPB_KIND_U32}, {27, MPB_KIND_U32}, {28, MPB_KIND_U32},
|
||||
{29, MPB_KIND_U32}, {30, MPB_KIND_U32}, {31, MPB_KIND_U32}, {32, MPB_KIND_U32},
|
||||
{33, MPB_KIND_U32}, {34, MPB_KIND_SCHEMA_ID}, {35, MPB_KIND_U32},
|
||||
};
|
||||
|
||||
static const mpb_field_desc_t* mpb_lookup_field(uint16_t field_id) {
|
||||
for (size_t i = 0; i < sizeof(mpb_field_table) / sizeof(mpb_field_table[0]); ++i) {
|
||||
if (mpb_field_table[i].id == field_id) {
|
||||
return &mpb_field_table[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Structural validation of the requires str-list encoding:
|
||||
* count u8 | (entry_length u16 | entry bytes){count}. */
|
||||
static frame_err_t mpb_check_strlist(const uint8_t* body, uint32_t length) {
|
||||
if (length < 1) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint32_t count = body[0];
|
||||
if (count > MPB_MAX_REQUIRES_ENTRIES) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint32_t cursor = 1;
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (length - cursor < 2) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint32_t entry_length =
|
||||
(uint32_t)body[cursor] | (uint32_t)((uint32_t)body[cursor + 1] << 8);
|
||||
cursor += 2;
|
||||
if (entry_length < 1 || entry_length > MPB_MAX_REQUIRE_BYTES ||
|
||||
entry_length > length - cursor) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
cursor += entry_length;
|
||||
}
|
||||
if (cursor != length) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
return FRAME_OK;
|
||||
}
|
||||
|
||||
/* Walk the manifest TLV region: bounds, unknown/required, duplicates, value
|
||||
* shapes, presence of name and version. Captures the spans and quota values
|
||||
* the view exposes plus the seen-id mask for mpb_manifest_field(). */
|
||||
static frame_err_t mpb_validate_manifest(const uint8_t* region, uint32_t length,
|
||||
uint64_t* seen_mask, mpb_view_t* view) {
|
||||
uint64_t mask = 0;
|
||||
const uint8_t* name = NULL;
|
||||
uint32_t name_length = 0;
|
||||
const uint8_t* version = NULL;
|
||||
uint32_t version_length = 0;
|
||||
uint32_t cursor = 0;
|
||||
|
||||
while (cursor < length) {
|
||||
if ((uint64_t)length - cursor < MPB_TLV_HEADER_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
const uint8_t* head = region + cursor;
|
||||
uint16_t field_id = mpb_rd16(head);
|
||||
uint16_t flags = mpb_rd16(head + 2);
|
||||
uint32_t value_length = mpb_rd32(head + 4);
|
||||
cursor += MPB_TLV_HEADER_SIZE;
|
||||
if ((flags & ~(uint16_t)MPB_TLV_FLAG_REQUIRED) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (value_length > length - cursor) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
const uint8_t* body = region + cursor;
|
||||
cursor += value_length;
|
||||
|
||||
const mpb_field_desc_t* desc = mpb_lookup_field(field_id);
|
||||
bool required = (flags & MPB_TLV_FLAG_REQUIRED) != 0;
|
||||
if (desc == NULL) {
|
||||
if (required) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
continue; /* unknown optional TLV: bounds-checked and skipped */
|
||||
}
|
||||
if ((mask & ((uint64_t)1 << (field_id - 1))) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* duplicated known field */
|
||||
}
|
||||
mask |= (uint64_t)1 << (field_id - 1);
|
||||
|
||||
switch (desc->kind) {
|
||||
case MPB_KIND_U32:
|
||||
if (value_length != 4) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (field_id == MPB_TLV_MAX_MEMORY_BYTES) {
|
||||
view->max_memory_bytes = mpb_rd32(body);
|
||||
} else if (field_id == MPB_TLV_IRAM_REQUIRED_BYTES) {
|
||||
view->iram_required_bytes = mpb_rd32(body);
|
||||
}
|
||||
break;
|
||||
case MPB_KIND_AFFINITY:
|
||||
if (value_length != 1 || body[0] > 2) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
break;
|
||||
case MPB_KIND_SCHEMA_ID:
|
||||
if (value_length != 16) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
break;
|
||||
case MPB_KIND_NAME:
|
||||
if (value_length < 1 || value_length > MPB_MAX_NAME_BYTES) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
name = body;
|
||||
name_length = value_length;
|
||||
break;
|
||||
case MPB_KIND_SEMVER:
|
||||
if (value_length < 1 || value_length > MPB_MAX_VERSION_BYTES) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
version = body;
|
||||
version_length = value_length;
|
||||
break;
|
||||
case MPB_KIND_STRLIST: {
|
||||
frame_err_t err = mpb_check_strlist(body, value_length);
|
||||
if (err != FRAME_OK) {
|
||||
return err;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
if (name == NULL || version == NULL) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* name and version are required */
|
||||
}
|
||||
view->name = name;
|
||||
view->name_length = name_length;
|
||||
view->version = version;
|
||||
view->version_length = version_length;
|
||||
*seen_mask = mask;
|
||||
return FRAME_OK;
|
||||
}
|
||||
|
||||
frame_err_t mpb_parse(const uint8_t* staging, size_t size, const mpb_policy_t* policy,
|
||||
const mpb_crypto_t* crypto, mpb_view_t* out_view) {
|
||||
if (staging == NULL || policy == NULL || crypto == NULL || out_view == NULL) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (crypto->sha256_fn == NULL || crypto->ecdsa_verify_fn == NULL) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (policy->max_package_bytes < MPB_HEADER_SIZE) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
mpb_zero_view(out_view);
|
||||
|
||||
/* --- stage 1: structure, bounds, types (all PACKAGE_INVALID) --- */
|
||||
|
||||
if ((uint64_t)size < MPB_HEADER_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((uint64_t)size > policy->max_package_bytes) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (mpb_rd32(staging + 0) != MPB_MAGIC) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (mpb_rd16(staging + 4) != 1) { /* format_major */
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (mpb_rd32(staging + 8) != MPB_HEADER_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((uint64_t)mpb_rd32(staging + 12) != (uint64_t)size) { /* total_size */
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint32_t flags = mpb_rd32(staging + 16);
|
||||
if ((flags & ~(uint32_t)MPB_FLAG_RESOURCE_ONLY) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint32_t target_id = mpb_rd32(staging + 20);
|
||||
uint16_t core_abi_major = mpb_rd16(staging + 24);
|
||||
uint16_t core_abi_minor = mpb_rd16(staging + 26);
|
||||
uint64_t required_features = mpb_rd64(staging + 28);
|
||||
uint64_t optional_features = mpb_rd64(staging + 36);
|
||||
uint32_t manifest_offset = mpb_rd32(staging + 44);
|
||||
uint32_t manifest_length = mpb_rd32(staging + 48);
|
||||
uint32_t table_offset = mpb_rd32(staging + 52);
|
||||
uint16_t payload_count = mpb_rd16(staging + 56);
|
||||
uint16_t entry_size = mpb_rd16(staging + 58);
|
||||
uint32_t signature_offset = mpb_rd32(staging + 60);
|
||||
uint32_t signature_length = mpb_rd32(staging + 64);
|
||||
uint32_t key_id = mpb_rd32(staging + 68);
|
||||
uint32_t security_epoch = mpb_rd32(staging + 72);
|
||||
|
||||
if (payload_count < 1 || payload_count > MPB_MAX_PAYLOAD_COUNT) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (entry_size != MPB_PAYLOAD_ENTRY_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (signature_length != MPB_SIGNATURE_RECORD_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (manifest_offset != MPB_HEADER_SIZE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (manifest_length < MPB_TLV_HEADER_SIZE + 1) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((uint64_t)table_offset != (uint64_t)manifest_offset + manifest_length) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
uint64_t table_end = (uint64_t)table_offset + (uint64_t)payload_count * entry_size;
|
||||
if (table_end > (uint64_t)size) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((uint64_t)signature_offset < table_end) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((uint64_t)signature_offset + MPB_SIGNATURE_RECORD_SIZE != (uint64_t)size) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* no trailing undescribed bytes */
|
||||
}
|
||||
|
||||
uint32_t elf_count = 0;
|
||||
uint32_t resource_count = 0;
|
||||
uint64_t offsets[MPB_MAX_PAYLOAD_COUNT];
|
||||
uint64_t lengths[MPB_MAX_PAYLOAD_COUNT];
|
||||
for (uint32_t i = 0; i < payload_count; ++i) {
|
||||
const uint8_t* entry = staging + table_offset + (uint64_t)i * MPB_PAYLOAD_ENTRY_SIZE;
|
||||
uint16_t payload_type = mpb_rd16(entry + 0);
|
||||
uint16_t payload_flags = mpb_rd16(entry + 2);
|
||||
uint64_t offset = mpb_rd32(entry + 4);
|
||||
uint64_t length = mpb_rd32(entry + 8);
|
||||
uint64_t unpacked = mpb_rd32(entry + 12);
|
||||
uint64_t alignment = mpb_rd32(entry + 16);
|
||||
|
||||
if (payload_type != MPB_PAYLOAD_TYPE_ELF && payload_type != MPB_PAYLOAD_TYPE_RESOURCE) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((payload_flags &
|
||||
~(uint16_t)(MPB_PAYLOAD_FLAG_REQUIRED | MPB_PAYLOAD_FLAG_COMPRESSED)) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if ((payload_flags & MPB_PAYLOAD_FLAG_REQUIRED) == 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (payload_type == MPB_PAYLOAD_TYPE_ELF &&
|
||||
(payload_flags & MPB_PAYLOAD_FLAG_COMPRESSED) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (unpacked != length) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (alignment < 1 || alignment > MPB_MAX_ALIGNMENT || (alignment & (alignment - 1)) != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (offset % alignment != 0) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
if (offset < table_end || offset + length > (uint64_t)signature_offset) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
for (uint32_t j = 0; j < i; ++j) {
|
||||
if (offset < offsets[j] + lengths[j] && offsets[j] < offset + length) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* region overlap */
|
||||
}
|
||||
}
|
||||
offsets[i] = offset;
|
||||
lengths[i] = length;
|
||||
if (payload_type == MPB_PAYLOAD_TYPE_ELF) {
|
||||
elf_count += 1;
|
||||
} else {
|
||||
resource_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool resource_only_flag = (flags & MPB_FLAG_RESOURCE_ONLY) != 0;
|
||||
if (elf_count > 1 || (elf_count > 0 && resource_count > 0)) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* strict single-type (PLUG-001) */
|
||||
}
|
||||
if (elf_count == 1 && resource_only_flag) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* flags disagree with composition */
|
||||
}
|
||||
if (elf_count == 0 && resource_count > 0 && !resource_only_flag) {
|
||||
return FRAME_ERR_PACKAGE_INVALID; /* resource-only must set bit0 */
|
||||
}
|
||||
|
||||
uint64_t seen_mask = 0;
|
||||
frame_err_t err =
|
||||
mpb_validate_manifest(staging + manifest_offset, manifest_length, &seen_mask, out_view);
|
||||
if (err != FRAME_OK) {
|
||||
mpb_zero_view(out_view);
|
||||
return err;
|
||||
}
|
||||
|
||||
const uint8_t* record = staging + signature_offset;
|
||||
if (mpb_rd16(record + 0) != MPB_SIG_ALGORITHM_ECDSA_P256_SHA256 ||
|
||||
mpb_rd16(record + 2) != MPB_SIG_RECORD_VERSION || mpb_rd32(record + 4) != key_id) {
|
||||
return FRAME_ERR_PACKAGE_INVALID;
|
||||
}
|
||||
const uint8_t* signature = record + 8;
|
||||
|
||||
/* --- stage 2: signature over header || manifest || payload_table --- */
|
||||
|
||||
uint8_t digest[32];
|
||||
crypto->sha256_fn(crypto->ctx, staging, (size_t)table_end, digest);
|
||||
if (crypto->ecdsa_verify_fn(crypto->ctx, key_id, digest, signature) != 1) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_SIGNATURE_INVALID;
|
||||
}
|
||||
|
||||
/* --- stage 3: per-payload hashes --- */
|
||||
|
||||
for (uint32_t i = 0; i < payload_count; ++i) {
|
||||
const uint8_t* entry = staging + table_offset + (uint64_t)i * MPB_PAYLOAD_ENTRY_SIZE;
|
||||
uint8_t actual[32];
|
||||
crypto->sha256_fn(crypto->ctx, staging + offsets[i], (size_t)lengths[i], actual);
|
||||
if (!mpb_bytes_equal(actual, entry + 20, 32)) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_HASH_MISMATCH;
|
||||
}
|
||||
}
|
||||
|
||||
/* --- stage 4: epoch / target / ABI / features policy --- */
|
||||
|
||||
if (security_epoch < policy->epoch_floor) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_EPOCH_ROLLBACK;
|
||||
}
|
||||
if (target_id != MPB_TARGET_ESP32S3) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_TARGET_MISMATCH;
|
||||
}
|
||||
if (core_abi_major != policy->abi_major || core_abi_minor > policy->abi_minor) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_ABI_MISMATCH;
|
||||
}
|
||||
if ((required_features & ~policy->device_features) != 0) {
|
||||
mpb_zero_view(out_view);
|
||||
return FRAME_ERR_ABI_MISMATCH;
|
||||
}
|
||||
|
||||
out_view->staging = staging;
|
||||
out_view->package_kind = resource_only_flag ? MPB_PACKAGE_RESOURCE_ONLY : MPB_PACKAGE_CODE;
|
||||
out_view->payload_count = payload_count;
|
||||
for (uint32_t i = 0; i < payload_count; ++i) {
|
||||
out_view->payloads[i].offset = (size_t)offsets[i];
|
||||
out_view->payloads[i].length = (size_t)lengths[i];
|
||||
}
|
||||
out_view->security_epoch = security_epoch;
|
||||
out_view->key_id = key_id;
|
||||
out_view->target_id = target_id;
|
||||
out_view->core_abi_major = core_abi_major;
|
||||
out_view->core_abi_minor = core_abi_minor;
|
||||
out_view->required_features = required_features;
|
||||
out_view->optional_features = optional_features;
|
||||
out_view->manifest_offset = manifest_offset;
|
||||
out_view->manifest_length = manifest_length;
|
||||
out_view->seen_fields_mask = seen_mask;
|
||||
return FRAME_OK;
|
||||
}
|
||||
|
||||
frame_err_t mpb_manifest_field(const mpb_view_t* view, uint16_t field_id, mpb_field_t* out) {
|
||||
if (view == NULL || out == NULL || view->staging == NULL) {
|
||||
return FRAME_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (field_id < 1 || field_id > 35 ||
|
||||
(view->seen_fields_mask & ((uint64_t)1 << (field_id - 1))) == 0) {
|
||||
return FRAME_ERR_NOT_FOUND;
|
||||
}
|
||||
const uint8_t* region = view->staging + view->manifest_offset;
|
||||
uint32_t remaining = view->manifest_length;
|
||||
while (remaining >= MPB_TLV_HEADER_SIZE) {
|
||||
uint16_t id = mpb_rd16(region);
|
||||
uint16_t value_flags = mpb_rd16(region + 2);
|
||||
uint32_t value_length = mpb_rd32(region + 4);
|
||||
region += MPB_TLV_HEADER_SIZE;
|
||||
remaining -= MPB_TLV_HEADER_SIZE;
|
||||
if (id == field_id) {
|
||||
out->field_id = id;
|
||||
out->flags = value_flags;
|
||||
out->value = region;
|
||||
out->value_length = value_length;
|
||||
return FRAME_OK;
|
||||
}
|
||||
/* Region already validated by mpb_parse; guard defensively anyway. */
|
||||
if (value_length > remaining) {
|
||||
return FRAME_ERR_NOT_FOUND;
|
||||
}
|
||||
region += value_length;
|
||||
remaining -= value_length;
|
||||
}
|
||||
return FRAME_ERR_NOT_FOUND;
|
||||
}
|
||||
Reference in New Issue
Block a user