aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / m5 / tools / m5_migsig.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* m5_migsig: build, sign and verify M5 owner-signed migration records. |
| 18 | * Hosted tool (libc I/O); the record logic is libaienos_m5 + native/sig. |
| 19 | * |
| 20 | * m5_migsig pubkey SECRET_KEY_FILE PUBLIC_KEY_OUT |
| 21 | * m5_migsig body SPEC_FILE PUBLIC_KEY_FILE BODY_OUT |
| 22 | * m5_migsig sign SECRET_KEY_FILE BODY_FILE RECORD_OUT |
| 23 | * m5_migsig verify PUBLIC_KEY_FILE RECORD_FILE SPEC_FILE LAST_COUNTER |
| 24 | * |
| 25 | * The tool never creates a key. The owner secret key is only ever read from |
| 26 | * the path given (Drake's offline Gate 3 ceremony); tests use TEST keys. |
| 27 | * Secret key file: 32 raw bytes, 64 hex digits (+ optional newline), or the |
| 28 | * 48-byte unencrypted PKCS#8 DER of an Ed25519 key; it must not be readable |
| 29 | * by group or others. Public key file: 32 raw bytes, 64 hex digits, or the |
| 30 | * 44-byte SubjectPublicKeyInfo DER. Output files are created new (O_EXCL). |
| 31 | * |
| 32 | * SPEC_FILE: one key=value per line, '#' comments, every key once except |
| 33 | * envelope (any number, any order; the tool sorts and refuses duplicates): |
| 34 | * class=production|test agent_root=<64 hex> source_store=<32 hex> |
| 35 | * dest_store=<32 hex> source_store_generation=<u64> store_format_version=<u32> |
| 36 | * envelope=<64 hex ObjectId> migration_manifest_digest=<64 hex> |
| 37 | * migration_counter=<u64> owner_hierarchy_generation=<u64> |
| 38 | * |
| 39 | * Exit: 0 ok, 1 record refused (verify) or signing refused, 2 usage/I/O/spec error. */ |
| 40 | #include <errno.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | #include "../m5.h" |
| 49 | #include "../../sig/aienos_sig.h" |
| 50 | |
| 51 | #define MAX_ENVELOPES 65536u |
| 52 | |
| 53 | static void wipe(void *p, size_t n) |
| 54 | { |
| 55 | volatile uint8_t *v = p; |
| 56 | for (size_t i = 0; i < n; i++) v[i] = 0; |
| 57 | } |
| 58 | |
| 59 | static int die(const char *msg, const char *arg) |
| 60 | { |
| 61 | fprintf(stderr, "m5_migsig: %s%s%s\n", msg, arg ? ": " : "", arg ? arg : ""); |
| 62 | return 2; |
| 63 | } |
| 64 | |
| 65 | static int hexval(int c) |
| 66 | { |
| 67 | if (c >= '0' && c <= '9') return c - '0'; |
| 68 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 69 | if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | /* Exactly 2*n hex digits. */ |
| 74 | static int parse_hex(const char *s, size_t slen, uint8_t *out, size_t n) |
| 75 | { |
| 76 | if (slen != 2 * n) return -1; |
| 77 | for (size_t i = 0; i < n; i++) { |
| 78 | int h = hexval(s[2 * i]), l = hexval(s[2 * i + 1]); |
| 79 | if (h < 0 || l < 0) return -1; |
| 80 | out[i] = (uint8_t)(h << 4 | l); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int parse_u64(const char *s, uint64_t max, uint64_t *out) |
| 86 | { |
| 87 | if (!*s || strlen(s) > 20) return -1; |
| 88 | uint64_t v = 0; |
| 89 | for (const char *p = s; *p; p++) { |
| 90 | if (*p < '0' || *p > '9') return -1; |
| 91 | uint64_t d = (uint64_t)(*p - '0'); |
| 92 | if (v > (UINT64_MAX - d) / 10) return -1; |
| 93 | v = v * 10 + d; |
| 94 | } |
| 95 | if (v > max) return -1; |
| 96 | *out = v; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* Reads at most cap bytes; *len gets the size. Refuses a larger file. */ |
| 101 | static int read_small(const char *path, uint8_t *buf, size_t cap, size_t *len, int secret) |
| 102 | { |
| 103 | int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 104 | if (fd < 0) return -1; |
| 105 | struct stat st; |
| 106 | if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { close(fd); return -1; } |
| 107 | if (secret && (st.st_mode & 077)) { close(fd); return -3; } |
| 108 | size_t n = 0; |
| 109 | for (;;) { |
| 110 | ssize_t r = read(fd, buf + n, cap - n); |
| 111 | if (r < 0) { if (errno == EINTR) continue; close(fd); return -1; } |
| 112 | if (r == 0) break; |
| 113 | n += (size_t)r; |
| 114 | if (n == cap) { |
| 115 | uint8_t extra; |
| 116 | ssize_t r2 = read(fd, &extra, 1); |
| 117 | if (r2 != 0) { close(fd); return -2; } |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | close(fd); |
| 122 | *len = n; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static const uint8_t PKCS8_PREFIX[16] = { 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, |
| 127 | 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20 }; |
| 128 | static const uint8_t SPKI_PREFIX[12] = { 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, |
| 129 | 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00 }; |
| 130 | |
| 131 | /* 32 raw, 64 hex (+ "\n"), or DER with the given prefix. */ |
| 132 | static int decode_key(const uint8_t *buf, size_t n, const uint8_t *prefix, size_t plen, uint8_t key[32]) |
| 133 | { |
| 134 | if (n == 32) { memcpy(key, buf, 32); return 0; } |
| 135 | if (n == 64 || (n == 65 && buf[64] == '\n')) |
| 136 | return parse_hex((const char *)buf, 64, key, 32); |
| 137 | if (n == plen + 32 && memcmp(buf, prefix, plen) == 0) { memcpy(key, buf + plen, 32); return 0; } |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | static int load_secret(const char *path, uint8_t sk[32]) |
| 142 | { |
| 143 | uint8_t buf[80]; |
| 144 | size_t n = 0; |
| 145 | int r = read_small(path, buf, sizeof buf, &n, 1); |
| 146 | if (r == -3) { wipe(buf, sizeof buf); return die("secret key file is readable by group or others (chmod 600)", path); } |
| 147 | if (r != 0) { wipe(buf, sizeof buf); return die("cannot read secret key file", path); } |
| 148 | r = decode_key(buf, n, PKCS8_PREFIX, sizeof PKCS8_PREFIX, sk); |
| 149 | wipe(buf, sizeof buf); |
| 150 | if (r != 0) { wipe(sk, 32); return die("secret key file is not 32 raw bytes, 64 hex digits or Ed25519 PKCS#8 DER", path); } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int load_public(const char *path, uint8_t pk[32]) |
| 155 | { |
| 156 | uint8_t buf[80]; |
| 157 | size_t n = 0; |
| 158 | if (read_small(path, buf, sizeof buf, &n, 0) != 0) return die("cannot read public key file", path); |
| 159 | if (decode_key(buf, n, SPKI_PREFIX, sizeof SPKI_PREFIX, pk) != 0) |
| 160 | return die("public key file is not 32 raw bytes, 64 hex digits or Ed25519 SPKI DER", path); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int write_new(const char *path, const void *data, size_t n) |
| 165 | { |
| 166 | int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644); |
| 167 | if (fd < 0) return die("cannot create output (it must not exist yet)", path); |
| 168 | const uint8_t *p = data; |
| 169 | size_t off = 0; |
| 170 | while (off < n) { |
| 171 | ssize_t w = write(fd, p + off, n - off); |
| 172 | if (w < 0) { if (errno == EINTR) continue; close(fd); unlink(path); return die("write failed", path); } |
| 173 | off += (size_t)w; |
| 174 | } |
| 175 | if (fsync(fd) != 0 || close(fd) != 0) { unlink(path); return die("write failed", path); } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int cmp32(const void *a, const void *b) { return memcmp(a, b, 32); } |
| 180 | |
| 181 | enum { K_CLASS, K_AGENT, K_SRC, K_DST, K_GEN, K_FMT, K_MAN, K_CTR, K_OGEN, K_N }; |
| 182 | static const char *const KEYS[K_N] = { "class", "agent_root", "source_store", "dest_store", |
| 183 | "source_store_generation", "store_format_version", |
| 184 | "migration_manifest_digest", "migration_counter", |
| 185 | "owner_hierarchy_generation" }; |
| 186 | |
| 187 | /* Parse SPEC_FILE into m (owner_key_id left zero). */ |
| 188 | static int load_spec(const char *path, m5_owner_migration *m) |
| 189 | { |
| 190 | FILE *f = fopen(path, "r"); |
| 191 | if (!f) return die("cannot read spec file", path); |
| 192 | memset(m, 0, sizeof *m); |
| 193 | int seen[K_N] = { 0 }; |
| 194 | uint8_t (*ids)[32] = calloc(MAX_ENVELOPES, 32); |
| 195 | if (!ids) { fclose(f); return die("out of memory", NULL); } |
| 196 | uint32_t n_ids = 0; |
| 197 | char line[256]; |
| 198 | int lineno = 0, rc = 0; |
| 199 | while (fgets(line, sizeof line, f)) { |
| 200 | lineno++; |
| 201 | size_t len = strlen(line); |
| 202 | if (len && line[len - 1] == '\n') line[--len] = 0; |
| 203 | else if (!feof(f)) { rc = die("spec line too long", path); break; } |
| 204 | if (len && line[len - 1] == '\r') { rc = die("spec has CR line endings", path); break; } |
| 205 | if (!len || line[0] == '#') continue; |
| 206 | char *eq = strchr(line, '='); |
| 207 | if (!eq) { fprintf(stderr, "m5_migsig: spec line %d has no '='\n", lineno); rc = 2; break; } |
| 208 | *eq = 0; |
| 209 | const char *k = line, *v = eq + 1; |
| 210 | size_t vl = strlen(v); |
| 211 | uint64_t u = 0; |
| 212 | int bad = 0; |
| 213 | if (strcmp(k, "envelope") == 0) { |
| 214 | if (n_ids >= MAX_ENVELOPES) { rc = die("too many envelope lines", path); break; } |
| 215 | bad = parse_hex(v, vl, ids[n_ids], 32); |
| 216 | if (!bad) n_ids++; |
| 217 | } else { |
| 218 | int idx = -1; |
| 219 | for (int i = 0; i < K_N; i++) if (strcmp(k, KEYS[i]) == 0) idx = i; |
| 220 | if (idx < 0) { fprintf(stderr, "m5_migsig: spec line %d: unknown key '%s'\n", lineno, k); rc = 2; break; } |
| 221 | if (seen[idx]++) { fprintf(stderr, "m5_migsig: spec line %d: '%s' given twice\n", lineno, k); rc = 2; break; } |
| 222 | switch (idx) { |
| 223 | case K_CLASS: |
| 224 | if (strcmp(v, "production") == 0) m->identity_class = M5_ID_PRODUCTION; |
| 225 | else if (strcmp(v, "test") == 0) m->identity_class = M5_ID_TEST; |
| 226 | else bad = 1; |
| 227 | break; |
| 228 | case K_AGENT: bad = parse_hex(v, vl, m->agent_root_id, 32); break; |
| 229 | case K_SRC: bad = parse_hex(v, vl, m->source_store_uuid, 16); break; |
| 230 | case K_DST: bad = parse_hex(v, vl, m->dest_store_uuid, 16); break; |
| 231 | case K_GEN: bad = parse_u64(v, UINT64_MAX, &u); m->source_store_generation = u; break; |
| 232 | case K_FMT: bad = parse_u64(v, UINT32_MAX, &u); m->store_format_version = (uint32_t)u; break; |
| 233 | case K_MAN: bad = parse_hex(v, vl, m->migration_manifest_digest, 32); break; |
| 234 | case K_CTR: bad = parse_u64(v, UINT64_MAX, &u); m->migration_counter = u; break; |
| 235 | case K_OGEN: bad = parse_u64(v, UINT64_MAX, &u); m->owner_hierarchy_generation = u; break; |
| 236 | } |
| 237 | } |
| 238 | if (bad) { fprintf(stderr, "m5_migsig: spec line %d: bad value for '%s'\n", lineno, k); rc = 2; break; } |
| 239 | } |
| 240 | if (!rc && ferror(f)) rc = die("cannot read spec file", path); |
| 241 | fclose(f); |
| 242 | for (int i = 0; !rc && i < K_N; i++) |
| 243 | if (!seen[i]) { fprintf(stderr, "m5_migsig: spec is missing '%s'\n", KEYS[i]); rc = 2; } |
| 244 | if (!rc) { |
| 245 | qsort(ids, n_ids, 32, cmp32); |
| 246 | m->envelope_count = n_ids; |
| 247 | if (m5_envelope_set_digest((const uint8_t (*)[32])ids, n_ids, m->envelope_set_digest) != M5_OK) |
| 248 | rc = die("duplicate envelope ObjectId in spec", path); |
| 249 | } |
| 250 | free(ids); |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | static void print_hex(const char *label, const uint8_t *p, size_t n) |
| 255 | { |
| 256 | printf("%s=", label); |
| 257 | for (size_t i = 0; i < n; i++) printf("%02x", p[i]); |
| 258 | printf("\n"); |
| 259 | } |
| 260 | |
| 261 | static const char *err_name(int rc) |
| 262 | { |
| 263 | switch (rc) { |
| 264 | case M5_ERR_ARG: return "ARG"; case M5_ERR_FORMAT: return "FORMAT"; case M5_ERR_BOUNDS: return "BOUNDS"; |
| 265 | case M5_ERR_AUTH: return "AUTH (wrong key or bad signature)"; case M5_ERR_BINDING: return "BINDING (field mismatch)"; |
| 266 | case M5_ERR_IDENTITY: return "IDENTITY (class)"; case M5_ERR_REPLAY: return "REPLAY (counter not above last)"; |
| 267 | default: return "error"; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static int cmd_pubkey(const char *skf, const char *out) |
| 272 | { |
| 273 | uint8_t sk[32], pk[32]; |
| 274 | if (load_secret(skf, sk)) return 2; |
| 275 | int r = aienos_ed25519_public_key(pk, sk); |
| 276 | wipe(sk, sizeof sk); |
| 277 | if (r != AIENOS_SIG_OK) return die("key derivation failed", NULL); |
| 278 | char hex[65]; |
| 279 | for (int i = 0; i < 32; i++) snprintf(hex + 2 * i, 3, "%02x", pk[i]); |
| 280 | hex[64] = '\n'; |
| 281 | return write_new(out, hex, 65); |
| 282 | } |
| 283 | |
| 284 | static int cmd_body(const char *spec, const char *pkf, const char *out) |
| 285 | { |
| 286 | m5_owner_migration m; |
| 287 | uint8_t pk[32], rec[M5_OWNER_MIG_LEN]; |
| 288 | if (load_spec(spec, &m) || load_public(pkf, pk)) return 2; |
| 289 | if (m5_owner_migration_body(&m, pk, rec) != M5_OK) return die("cannot build body", NULL); |
| 290 | return write_new(out, rec, sizeof rec); |
| 291 | } |
| 292 | |
| 293 | static int cmd_sign(const char *skf, const char *body, const char *out) |
| 294 | { |
| 295 | uint8_t sk[32], rec[M5_OWNER_MIG_LEN + 1]; |
| 296 | size_t n = 0; |
| 297 | int r = read_small(body, rec, sizeof rec, &n, 0); |
| 298 | if (r != 0 || n != M5_OWNER_MIG_LEN) return die("body file is not a 272-byte record", body); |
| 299 | if (load_secret(skf, sk)) return 2; |
| 300 | r = m5_owner_migration_sign_body(rec, sk); |
| 301 | wipe(sk, sizeof sk); |
| 302 | if (r != M5_OK) { fprintf(stderr, "m5_migsig: signing refused: %s\n", err_name(r)); return 1; } |
| 303 | return write_new(out, rec, M5_OWNER_MIG_LEN); |
| 304 | } |
| 305 | |
| 306 | static int cmd_verify(const char *pkf, const char *recf, const char *spec, const char *last_s) |
| 307 | { |
| 308 | uint8_t pk[32], rec[M5_OWNER_MIG_LEN + 1]; |
| 309 | m5_owner_migration e, o; |
| 310 | uint64_t last; |
| 311 | size_t n = 0; |
| 312 | if (parse_u64(last_s, UINT64_MAX, &last)) return die("LAST_COUNTER is not a decimal u64", last_s); |
| 313 | if (load_public(pkf, pk) || load_spec(spec, &e)) return 2; |
| 314 | int r = read_small(recf, rec, sizeof rec, &n, 0); |
| 315 | if (r == -1) return die("cannot read record", recf); |
| 316 | if (r == -2) n = sizeof rec; /* longer than a record: the library refuses it */ |
| 317 | r = m5_owner_migration_verify(rec, n, pk, e.identity_class, &e, last, &o); |
| 318 | if (r != M5_OK) { printf("M5_OWNER_MIGRATION_VERIFY: REFUSED %s\n", err_name(r)); return 1; } |
| 319 | print_hex("owner_key_id", o.owner_key_id, 32); |
| 320 | print_hex("source_store", o.source_store_uuid, 16); |
| 321 | print_hex("dest_store", o.dest_store_uuid, 16); |
| 322 | printf("source_store_generation=%llu\n", (unsigned long long)o.source_store_generation); |
| 323 | printf("envelope_count=%u\n", o.envelope_count); |
| 324 | printf("migration_counter=%llu\n", (unsigned long long)o.migration_counter); |
| 325 | printf("M5_OWNER_MIGRATION_VERIFY: OK\n"); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | int main(int argc, char **argv) |
| 330 | { |
| 331 | if (argc == 4 && strcmp(argv[1], "pubkey") == 0) return cmd_pubkey(argv[2], argv[3]); |
| 332 | if (argc == 5 && strcmp(argv[1], "body") == 0) return cmd_body(argv[2], argv[3], argv[4]); |
| 333 | if (argc == 5 && strcmp(argv[1], "sign") == 0) return cmd_sign(argv[2], argv[3], argv[4]); |
| 334 | if (argc == 6 && strcmp(argv[1], "verify") == 0) return cmd_verify(argv[2], argv[3], argv[4], argv[5]); |
| 335 | fprintf(stderr, "usage: m5_migsig pubkey SECRET_KEY_FILE PUBLIC_KEY_OUT\n" |
| 336 | " m5_migsig body SPEC_FILE PUBLIC_KEY_FILE BODY_OUT\n" |
| 337 | " m5_migsig sign SECRET_KEY_FILE BODY_FILE RECORD_OUT\n" |
| 338 | " m5_migsig verify PUBLIC_KEY_FILE RECORD_FILE SPEC_FILE LAST_COUNTER\n"); |
| 339 | return 2; |
| 340 | } |