also looking at this
feat: own runtime firmware candidates
#49
24 files
+1245
-92
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | import { randomBytes } from 'node:crypto' |
| 2 | 2 | import { mkdirSync, mkdtempSync, realpathSync, rmSync } from 'node:fs' |
| 3 | import { dirname, join, relative, resolve, sep } from 'node:path' | |
| 3 | import { join, resolve } from 'node:path' | |
| 4 | 4 | import { tmpdir } from 'node:os' |
| 5 | 5 | import { Readable } from 'node:stream' |
| 6 | 6 | import { isDeepStrictEqual } from 'node:util' |
| 7 | 7 | import { Updater } from 'tuf-js' |
| 8 | import { directory, imageLimit, metadataLimit, regularFile, repositoryFile, safePath, sha256, syncDirectory, syncTree, writeExclusive } from './files' | |
| 8 | import { directory, imageLimit, metadataLimit, regularFile, repositoryFile, safePath, sha256, writeExclusive } from './files' | |
| 9 | 9 | import { object, validateMetadata } from './metadata' |
| 10 | 10 | import { admitSignedRuntime } from './manifest' |
| 11 | import { finishRuntimeCandidate, runtimeCandidateOutput } from './candidate-files' | |
| 11 | 12 | import { admitPolicy } from './policy' |
| 12 | 13 | import type { RuntimeFirmwarePolicy } from './policy' |
| 13 | 14 | export type { RuntimeFirmwarePolicy } from './policy' |
| @@ -85,29 +86,14 @@ async function verifyRepository(repository: string, root: Buffer, work: string, | ||
| 85 | 86 | /** Exclusive offline candidate only. This never changes installed firmware, boot slots or trust floors. */ |
| 86 | 87 | export async function stageRuntimeFirmware(options: RuntimeFirmwarePolicy & { repository: string, trustedRoot: string, output: string }): Promise<RuntimeFirmwareStage> { |
| 87 | 88 | const policy = admitPolicy(options) |
| 88 | const repository = realpathSync(options.repository), output = resolve(options.output) | |
| 89 | const repository = realpathSync(options.repository), output = runtimeCandidateOutput(options.output, [repository]) | |
| 89 | 90 | directory(repository) |
| 90 | 91 | const root = regularFile(options.trustedRoot, metadataLimit) |
| 91 | 92 | if (sha256(root) !== policy.trustedRootSha256) throw new Error('Trusted root SHA-256 does not match the independent pin') |
| 92 | // Resolve the existing parent before creating anything, so a symlinked parent cannot hide overlap. | |
| 93 | const destination = join(realpathSync(dirname(output)), output.slice(dirname(output).length + 1)) | |
| 94 | const child = relative(repository, destination), parent = relative(destination, repository) | |
| 95 | if (!child || (!child.startsWith(`..${sep}`) && child !== '..') || (!parent.startsWith(`..${sep}`) && parent !== '..')) throw new Error('Candidate and source repository must be separate') | |
| 96 | 93 | mkdirSync(output, { mode: 0o700 }) |
| 97 | 94 | writeExclusive(join(output, 'trust-root.json'), root) |
| 98 | 95 | const { result, fetched, info } = await verifyRepository(repository, root, join(output, '.verification'), policy, join(output, 'image.bin')) |
| 99 | // Re-read the actual staged bytes, rather than trusting a successful copy or receipt. | |
| 100 | await info.verify(Readable.from(regularFile(join(output, 'image.bin'), imageLimit))) | |
| 101 | for (const [path, bytes] of fetched) { | |
| 102 | const file = join(output, 'repository', path) | |
| 103 | mkdirSync(dirname(file), { recursive: true, mode: 0o700 }) | |
| 104 | writeExclusive(file, bytes) | |
| 105 | } | |
| 106 | syncTree(output) | |
| 107 | // A final marker means logical completeness. It cannot attest to power-loss durability after fsync. | |
| 108 | writeExclusive(join(output, 'stage.json'), Buffer.from(`${JSON.stringify(result, null, 2)}\n`)) | |
| 109 | syncDirectory(output) | |
| 110 | syncDirectory(dirname(output)) | |
| 96 | await finishRuntimeCandidate(output, info, result, fetched) | |
| 111 | 97 | return result |
| 112 | 98 | } |
| 113 | 99 | |