also looking at this
feat: observe verified metadata before persistence
#1
8 files
+807
-21
| @@ -0,0 +1,158 @@ | ||
| 1 | import { generateKeyPairSync, sign, createHash } from 'node:crypto'; | |
| 2 | import { mkdir, writeFile } from 'node:fs/promises'; | |
| 3 | import { join } from 'node:path'; | |
| 4 | import { | |
| 5 | Key, | |
| 6 | Metadata, | |
| 7 | MetaFile, | |
| 8 | Root, | |
| 9 | Signature, | |
| 10 | Snapshot, | |
| 11 | TargetFile, | |
| 12 | Targets, | |
| 13 | Timestamp, | |
| 14 | } from '@tufjs/models'; | |
| 15 | import { canonicalize } from '@tufjs/canonical-json'; | |
| 16 | ||
| 17 | const digest = (bytes: Uint8Array) => | |
| 18 | createHash('sha256').update(bytes).digest('hex'); | |
| 19 | const fields = { | |
| 20 | version: 1, | |
| 21 | specVersion: '1.0.31', | |
| 22 | expires: '2035-01-01T00:00:00Z', | |
| 23 | }; | |
| 24 | ||
| 25 | function signer() { | |
| 26 | const pair = generateKeyPairSync('ed25519'); | |
| 27 | const publicKey = pair.publicKey | |
| 28 | .export({ format: 'der', type: 'spki' }) | |
| 29 | .subarray(-32) | |
| 30 | .toString('hex'); | |
| 31 | const keyObject = { | |
| 32 | keytype: 'ed25519', | |
| 33 | scheme: 'ed25519', | |
| 34 | keyval: { public: publicKey }, | |
| 35 | }; | |
| 36 | const keyID = digest(Buffer.from(canonicalize(keyObject))); | |
| 37 | const key = new Key({ | |
| 38 | keyID, | |
| 39 | keyType: 'ed25519', | |
| 40 | scheme: 'ed25519', | |
| 41 | keyVal: { public: publicKey }, | |
| 42 | }); | |
| 43 | return { | |
| 44 | key, | |
| 45 | sign: (data: Buffer) => | |
| 46 | new Signature({ | |
| 47 | keyID: key.keyID, | |
| 48 | sig: sign(null, data, pair.privateKey).toString('hex'), | |
| 49 | }), | |
| 50 | }; | |
| 51 | } | |
| 52 | ||
| 53 | /** Throwaway Ed25519 keys and payload bytes are used only by local tests. */ | |
| 54 | export async function repository( | |
| 55 | directory: string, | |
| 56 | options: { | |
| 57 | expires?: string; | |
| 58 | metadataFields?: Partial< | |
| 59 | Record< | |
| 60 | 'root' | 'rotatedRoot' | 'timestamp' | 'snapshot' | 'targets', | |
| 61 | { expires?: string; version?: number } | |
| 62 | > | |
| 63 | >; | |
| 64 | rotate?: boolean; | |
| 65 | badRotation?: boolean; | |
| 66 | badTargetsSignature?: boolean; | |
| 67 | } = {} | |
| 68 | ) { | |
| 69 | await mkdir(join(directory, 'metadata'), { recursive: true }); | |
| 70 | const keys = { | |
| 71 | root: signer(), | |
| 72 | targets: signer(), | |
| 73 | snapshot: signer(), | |
| 74 | timestamp: signer(), | |
| 75 | }; | |
| 76 | const root = new Root({ | |
| 77 | ...fields, | |
| 78 | ...options.metadataFields?.root, | |
| 79 | consistentSnapshot: true, | |
| 80 | }); | |
| 81 | for (const role of ['root', 'targets', 'snapshot', 'timestamp'] as const) | |
| 82 | root.addKey(keys[role].key, role); | |
| 83 | const rootMetadata = new Metadata(root); | |
| 84 | rootMetadata.sign(keys.root.sign); | |
| 85 | const rootPath = join(directory, 'trusted-root.json'); | |
| 86 | await writeFile(rootPath, JSON.stringify(rootMetadata.toJSON())); | |
| 87 | if (options.rotate) { | |
| 88 | const nextKey = signer(); | |
| 89 | const next = new Root({ | |
| 90 | ...fields, | |
| 91 | version: 2, | |
| 92 | ...options.metadataFields?.rotatedRoot, | |
| 93 | consistentSnapshot: true, | |
| 94 | }); | |
| 95 | next.addKey(nextKey.key, 'root'); | |
| 96 | for (const role of ['targets', 'snapshot', 'timestamp'] as const) | |
| 97 | next.addKey(keys[role].key, role); | |
| 98 | const rotated = new Metadata(next); | |
| 99 | if (!options.badRotation) rotated.sign(keys.root.sign); | |
| 100 | rotated.sign(nextKey.sign); | |
| 101 | await writeFile( | |
| 102 | join(directory, 'metadata/2.root.json'), | |
| 103 | JSON.stringify(rotated.toJSON()) | |
| 104 | ); | |
| 105 | } | |
| 106 | const image = Buffer.from('observer test payload'); | |
| 107 | const target = 'observer-fixture.txt'; | |
| 108 | const targets = new Metadata( | |
| 109 | new Targets({ | |
| 110 | ...fields, | |
| 111 | ...options.metadataFields?.targets, | |
| 112 | targets: { | |
| 113 | [target]: new TargetFile({ | |
| 114 | path: target, | |
| 115 | length: image.length, | |
| 116 | hashes: { sha256: digest(image) }, | |
| 117 | }), | |
| 118 | }, | |
| 119 | }) | |
| 120 | ); | |
| 121 | targets.sign(options.badTargetsSignature ? signer().sign : keys.targets.sign); | |
| 122 | const targetsBytes = Buffer.from(JSON.stringify(targets.toJSON())); | |
| 123 | const snapshot = new Metadata( | |
| 124 | new Snapshot({ | |
| 125 | ...fields, | |
| 126 | ...options.metadataFields?.snapshot, | |
| 127 | meta: { | |
| 128 | 'targets.json': new MetaFile({ | |
| 129 | version: 1, | |
| 130 | length: targetsBytes.length, | |
| 131 | hashes: { sha256: digest(targetsBytes) }, | |
| 132 | }), | |
| 133 | }, | |
| 134 | }) | |
| 135 | ); | |
| 136 | snapshot.sign(keys.snapshot.sign); | |
| 137 | const snapshotBytes = Buffer.from(JSON.stringify(snapshot.toJSON())); | |
| 138 | const timestamp = new Metadata( | |
| 139 | new Timestamp({ | |
| 140 | ...fields, | |
| 141 | expires: options.expires ?? fields.expires, | |
| 142 | ...options.metadataFields?.timestamp, | |
| 143 | snapshotMeta: new MetaFile({ | |
| 144 | version: 1, | |
| 145 | length: snapshotBytes.length, | |
| 146 | hashes: { sha256: digest(snapshotBytes) }, | |
| 147 | }), | |
| 148 | }) | |
| 149 | ); | |
| 150 | timestamp.sign(keys.timestamp.sign); | |
| 151 | await writeFile( | |
| 152 | join(directory, 'metadata/timestamp.json'), | |
| 153 | JSON.stringify(timestamp.toJSON()) | |
| 154 | ); | |
| 155 | await writeFile(join(directory, 'metadata/1.snapshot.json'), snapshotBytes); | |
| 156 | await writeFile(join(directory, 'metadata/1.targets.json'), targetsBytes); | |
| 157 | return { rootPath }; | |
| 158 | } | |