|
| 1 | +// Orchestrating verification core (#9212, epic #8534): "did this exact code run on this exact corpus inside |
| 2 | +// genuine SNP hardware" -- answerable by a third party who trusts nothing but AMD's own published root keys |
| 3 | +// and the code in this file. Pure with respect to IO (the CLI reads files/network, this module only takes |
| 4 | +// already-loaded bytes and PEM strings) so every failure class below is independently, deterministically |
| 5 | +// testable with real cryptography (see this file's companion test suite -- a synthetic-but-genuine 3-tier |
| 6 | +// PKI, never a mocked "verification" that can't actually fail). |
| 7 | +// |
| 8 | +// SECURITY-CRITICAL ORDERING: nothing the report itself claims (its TCB fields, its measurement, its |
| 9 | +// report_data) is trusted until BOTH the certificate chain is trusted AND the report's own signature has |
| 10 | +// verified against that chain's VCEK. Checking any report content first would mean a forged report with a |
| 11 | +// fabricated (but plausible-looking) measurement could pass checks 6-8 below by coincidence, before ever |
| 12 | +// being rejected for the invalid signature that actually damns it. |
| 13 | +import { buildAttestationReportData } from "@loopover/engine/calibration/attestation-envelope"; |
| 14 | +import { isSampleAttestationReport } from "@loopover/engine/calibration/attester"; |
| 15 | +import type { AttestationEnvelope } from "@loopover/engine/calibration/attestation-envelope"; |
| 16 | +import { X509Certificate, verify as cryptoVerify } from "node:crypto"; |
| 17 | + |
| 18 | +import { encodeOid, findExtensionValue, parseDer, readSmallDerInteger } from "./verify-attested-run-der"; |
| 19 | +import { parseSnpReport, type SnpTcbVersion } from "./verify-attested-run-report"; |
| 20 | + |
| 21 | +export type VerificationFailureClass = |
| 22 | + | "sample_attestation" |
| 23 | + | "envelope_invalid" |
| 24 | + | "malformed_report" |
| 25 | + | "chain_untrusted" |
| 26 | + | "signature_invalid" |
| 27 | + | "tcb_mismatch" |
| 28 | + | "measurement_mismatch" |
| 29 | + | "report_data_mismatch"; |
| 30 | + |
| 31 | +export type VerificationResult = |
| 32 | + | { verified: true } |
| 33 | + | { verified: false; failureClass: VerificationFailureClass; reason: string }; |
| 34 | + |
| 35 | +export type VerifyAttestedRunInput = { |
| 36 | + envelope: AttestationEnvelope; |
| 37 | + /** Raw SNP report bytes, i.e. `Buffer.from(envelope.attestationReport, "base64")` -- decoding is the |
| 38 | + * CLI's job so this module never has to guess an encoding. */ |
| 39 | + rawReportBytes: Uint8Array; |
| 40 | + vcekCertPem: string; |
| 41 | + askCertPem: string; |
| 42 | + arkCertPem: string; |
| 43 | + /** The operator's own vendored, trusted root -- compared byte-for-byte (not merely "is self-signed") against |
| 44 | + * `arkCertPem`, so a caller can never be tricked by an attacker-supplied self-signed "ARK" that isn't |
| 45 | + * actually AMD's. */ |
| 46 | + pinnedArkCertPem: string; |
| 47 | + /** Hex, from the tenant/operator's own manifest -- what the launch measurement is expected to be. */ |
| 48 | + expectedMeasurementHex: string; |
| 49 | + corpusChecksum: string; |
| 50 | + headSha: string; |
| 51 | + baseSha: string; |
| 52 | + allowSample: boolean; |
| 53 | +}; |
| 54 | + |
| 55 | +const AMD_SVN_OIDS = { |
| 56 | + bootloaderSpl: encodeOid([1, 3, 6, 1, 4, 1, 3704, 1, 3, 1]), |
| 57 | + teeSpl: encodeOid([1, 3, 6, 1, 4, 1, 3704, 1, 3, 2]), |
| 58 | + snpSpl: encodeOid([1, 3, 6, 1, 4, 1, 3704, 1, 3, 3]), |
| 59 | + microcodeSpl: encodeOid([1, 3, 6, 1, 4, 1, 3704, 1, 3, 8]), |
| 60 | +} as const; |
| 61 | + |
| 62 | +function toHex(bytes: Uint8Array): string { |
| 63 | + return Buffer.from(bytes).toString("hex"); |
| 64 | +} |
| 65 | + |
| 66 | +function pemToDer(pem: string): Uint8Array { |
| 67 | + const base64 = pem |
| 68 | + .split("\n") |
| 69 | + .filter((line) => line.length > 0 && !line.includes("BEGIN") && !line.includes("END")) |
| 70 | + .join(""); |
| 71 | + return new Uint8Array(Buffer.from(base64, "base64")); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Read the four AMD KDS SVN OIDs (bootloader/TEE/SNP/microcode) off a VCEK certificate's raw DER, per |
| 76 | + * kds/kds.go's OID table in google/go-sev-guest. Any missing or malformed OID is a certificate this function |
| 77 | + * refuses to interpret -- it throws rather than substituting a default SVN, since a default here would mean |
| 78 | + * silently trusting an unattested claim. |
| 79 | + */ |
| 80 | +export function readVcekTcbFromCertificate(vcekCertDer: Uint8Array): SnpTcbVersion { |
| 81 | + const tree = parseDer(vcekCertDer); |
| 82 | + const readOne = (oid: Uint8Array, name: string): number => { |
| 83 | + const value = findExtensionValue(vcekCertDer, tree, oid); |
| 84 | + if (!value) throw new Error(`readVcekTcbFromCertificate: missing AMD SVN extension for ${name}`); |
| 85 | + return readSmallDerInteger(value); |
| 86 | + }; |
| 87 | + return { |
| 88 | + bootloaderSpl: readOne(AMD_SVN_OIDS.bootloaderSpl, "bootloaderSpl"), |
| 89 | + teeSpl: readOne(AMD_SVN_OIDS.teeSpl, "teeSpl"), |
| 90 | + snpSpl: readOne(AMD_SVN_OIDS.snpSpl, "snpSpl"), |
| 91 | + microcodeSpl: readOne(AMD_SVN_OIDS.microcodeSpl, "microcodeSpl"), |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +/** `error.message` for a real Error, `String(error)` for anything else a `catch` clause could technically |
| 96 | + * hand back (a thrown string/object, however unusual in practice) -- isolated into its own function so both |
| 97 | + * arms are independently unit-testable rather than only reachable through whatever a specific call site |
| 98 | + * happens to throw today. */ |
| 99 | +export function formatCaughtError(error: unknown): string { |
| 100 | + return error instanceof Error ? error.message : String(error); |
| 101 | +} |
| 102 | + |
| 103 | +function tcbVersionsEqual(a: SnpTcbVersion, b: SnpTcbVersion): boolean { |
| 104 | + return a.bootloaderSpl === b.bootloaderSpl && a.teeSpl === b.teeSpl && a.snpSpl === b.snpSpl && a.microcodeSpl === b.microcodeSpl; |
| 105 | +} |
| 106 | + |
| 107 | +function formatTcb(tcb: SnpTcbVersion): string { |
| 108 | + return `bootloader=${tcb.bootloaderSpl} tee=${tcb.teeSpl} snp=${tcb.snpSpl} microcode=${tcb.microcodeSpl}`; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Verify an attested run end to end. Never throws for an ordinarily-invalid input (a bad signature, a |
| 113 | + * mismatched measurement, an untrusted chain) -- those are all `{ verified: false, failureClass, reason }`. |
| 114 | + * Only a structurally impossible input (certificates that don't even parse as X.509, for instance) propagates |
| 115 | + * as a thrown error, since that is a caller-side bug (malformed PEM), not a fact about the attested run. |
| 116 | + */ |
| 117 | +export function verifyAttestedRun(input: VerifyAttestedRunInput): VerificationResult { |
| 118 | + if (!input.allowSample && isSampleAttestationReport(input.envelope.attestationReport)) { |
| 119 | + return { |
| 120 | + verified: false, |
| 121 | + failureClass: "sample_attestation", |
| 122 | + reason: "envelope is sample-attested (dev artifact, not evidence) -- pass --allow-sample to accept it anyway for local development", |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + let report: ReturnType<typeof parseSnpReport>; |
| 127 | + try { |
| 128 | + report = parseSnpReport(input.rawReportBytes); |
| 129 | + } catch (error) { |
| 130 | + return { verified: false, failureClass: "malformed_report", reason: formatCaughtError(error) }; |
| 131 | + } |
| 132 | + |
| 133 | + // Chain of trust: the operator's pinned root, byte-for-byte, is what "ARK" means here -- not merely |
| 134 | + // "some self-signed certificate". ARK -> ASK -> VCEK, each link a real signature check. |
| 135 | + const pinnedArkDer = pemToDer(input.pinnedArkCertPem); |
| 136 | + const suppliedArkDer = pemToDer(input.arkCertPem); |
| 137 | + if (toHex(pinnedArkDer) !== toHex(suppliedArkDer)) { |
| 138 | + return { verified: false, failureClass: "chain_untrusted", reason: "supplied ARK certificate does not match the pinned, vendored root" }; |
| 139 | + } |
| 140 | + const ark = new X509Certificate(input.arkCertPem); |
| 141 | + const ask = new X509Certificate(input.askCertPem); |
| 142 | + const vcek = new X509Certificate(input.vcekCertPem); |
| 143 | + if (!ark.verify(ark.publicKey)) { |
| 144 | + return { verified: false, failureClass: "chain_untrusted", reason: "pinned ARK certificate does not verify against its own public key (not self-signed)" }; |
| 145 | + } |
| 146 | + if (!ask.verify(ark.publicKey)) { |
| 147 | + return { verified: false, failureClass: "chain_untrusted", reason: "ASK certificate was not signed by the pinned ARK" }; |
| 148 | + } |
| 149 | + if (!vcek.verify(ask.publicKey)) { |
| 150 | + return { verified: false, failureClass: "chain_untrusted", reason: "VCEK certificate was not signed by the verified ASK" }; |
| 151 | + } |
| 152 | + |
| 153 | + // The report's own signature, checked against the now-trusted VCEK's public key -- nothing about the |
| 154 | + // report's CONTENT (below) is meaningful until this passes. |
| 155 | + const vcekPublicKeyPem = vcek.publicKey.export({ type: "spki", format: "pem" }); |
| 156 | + const signatureValid = verifySnpSignature(report.signedBytes, report.signatureIeeeP1363, vcekPublicKeyPem); |
| 157 | + if (!signatureValid) { |
| 158 | + return { verified: false, failureClass: "signature_invalid", reason: "report signature does not verify against the trusted VCEK public key" }; |
| 159 | + } |
| 160 | + |
| 161 | + const certTcb = readVcekTcbFromCertificate(pemToDer(input.vcekCertPem)); |
| 162 | + if (!tcbVersionsEqual(certTcb, report.reportedTcb)) { |
| 163 | + return { |
| 164 | + verified: false, |
| 165 | + failureClass: "tcb_mismatch", |
| 166 | + reason: `report's reported_tcb (${formatTcb(report.reportedTcb)}) does not match the VCEK certificate's provisioned TCB (${formatTcb(certTcb)})`, |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + const measurementHex = toHex(report.measurement); |
| 171 | + if (measurementHex !== input.expectedMeasurementHex.toLowerCase()) { |
| 172 | + return { |
| 173 | + verified: false, |
| 174 | + failureClass: "measurement_mismatch", |
| 175 | + reason: `report measurement ${measurementHex} does not match the expected pinned digest ${input.expectedMeasurementHex.toLowerCase()}`, |
| 176 | + }; |
| 177 | + } |
| 178 | + |
| 179 | + const expectedReportData = buildAttestationReportData({ |
| 180 | + corpusChecksum: input.corpusChecksum, |
| 181 | + headSha: input.headSha, |
| 182 | + baseSha: input.baseSha, |
| 183 | + runId: input.envelope.runId, |
| 184 | + }); |
| 185 | + if (toHex(report.reportData) !== expectedReportData) { |
| 186 | + return { |
| 187 | + verified: false, |
| 188 | + failureClass: "report_data_mismatch", |
| 189 | + reason: "report_data re-derived from the envelope's claimed corpus checksum, SHAs, and runId does not match the report's own report_data field", |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + return { verified: true }; |
| 194 | +} |
| 195 | + |
| 196 | +/** Real ECDSA-P384-SHA384 verification, isolated into its own function purely so the core orchestration |
| 197 | + * above reads as a flat sequence of named checks. */ |
| 198 | +function verifySnpSignature(signedBytes: Uint8Array, signatureIeeeP1363: Uint8Array, vcekPublicKeyPem: string | Buffer): boolean { |
| 199 | + return cryptoVerify("sha384", signedBytes, { key: vcekPublicKeyPem, dsaEncoding: "ieee-p1363" }, signatureIeeeP1363); |
| 200 | +} |
0 commit comments