diff --git a/docs/e2e-secure-rads.txt b/docs/e2e-secure-rads.txt index d88fe76..7525373 100644 --- a/docs/e2e-secure-rads.txt +++ b/docs/e2e-secure-rads.txt @@ -71,7 +71,7 @@ Input: 4 SEED-ENTROPY from NSM // no AWS cost 5 emit unit-ready // no token yet, auth none 6 challenge ← REQUEST-CHALLENGE(Verifier, a, runId) // single-use nonce, verifier API GW/Lambda and DynamoDB metered -7 token ← ATTEST(Verifier, runId, challenge) // NSM nonce ← challenge; verifier API GW/Lambda and DynamoDB metered +7 token ← ATTEST(Verifier, runId, challenge) // NSM nonce ← challenge; claims the run so only one attestation succeeds; verifier API GW/Lambda and DynamoDB metered 8 emit attested 9 records ← FETCH-PDM(a, token, limit ← n) 10 cost apigateway:request + lambda:invocation + lambda:compute + dynamodb:read-capacity // PDM /records diff --git a/secure/verifier-service/README.md b/secure/verifier-service/README.md index b66baf2..7bcf245 100644 --- a/secure/verifier-service/README.md +++ b/secure/verifier-service/README.md @@ -6,7 +6,7 @@ Serverless Framework v4, TypeScript. Receives `AlgorithmId` + PCR values from re - `POST /algorithms` (mTLS domain) - registry-service registers `AlgorithmId` -> PCR0/1/2 at build time. - `POST /challenge` (public domain) - sce-unit asks for a single-use nonce before attesting. The row is stored hashed in `AttestationChallengeTable` with a 60 s TTL and is bound to the `algorithmId` and `runId` in the request. -- `POST /attest` (public domain) - sce-unit submits a real Nitro attestation document (base64 COSE_Sign1 bytes) + `algorithmId` + `runId` + the `challenge` it was issued. The document's `nonce` must equal the challenge, and the challenge is consumed atomically before the PCR check, so a captured document cannot be replayed. Verified against the pinned AWS Nitro root cert chain and the registered PCRs; on success, issues a signed access JWT + opaque refresh token (persisted in `AttestationSessionTable`, including `RunId`). +- `POST /attest` (public domain) - sce-unit submits a real Nitro attestation document (base64 COSE_Sign1 bytes) + `algorithmId` + `runId` + the `challenge` it was issued. The document's `nonce` must equal the challenge, and the challenge is consumed atomically before the PCR check, so a captured document cannot be replayed. After the PCR check, the run is claimed in `RunAttestationTable` with a conditional put, so only the first attestation for a `runId` succeeds; later ones get 409. Verified against the pinned AWS Nitro root cert chain and the registered PCRs; on success, issues a signed access JWT + opaque refresh token (persisted in `AttestationSessionTable`, including `RunId`). - `POST /refresh` (public domain) - exchanges a still-valid refresh token plus its `runId` for a new access JWT, without requiring a fresh attestation document. The `runId` must match the one stored on the session. - `GET /.well-known/jwks.json` (public domain) - the ML-DSA-87 public key downstream services use to verify access JWTs locally. Public by definition, no auth. diff --git a/secure/verifier-service/serverless.yml b/secure/verifier-service/serverless.yml index d599a35..8fb3796 100644 --- a/secure/verifier-service/serverless.yml +++ b/secure/verifier-service/serverless.yml @@ -37,6 +37,9 @@ provider: - dynamodb:PutItem - dynamodb:UpdateItem Resource: !GetAtt AttestationChallengeTable.Arn + - Effect: Allow + Action: dynamodb:PutItem + Resource: !GetAtt RunAttestationTable.Arn - Effect: Allow Action: ssm:GetParameter Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/secure-rads/verifier-service/jwt-signing-key" @@ -44,6 +47,7 @@ provider: ALGORITHM_PCR_TABLE: !Ref AlgorithmPCRTable ATTESTATION_SESSION_TABLE: !Ref AttestationSessionTable ATTESTATION_CHALLENGE_TABLE: !Ref AttestationChallengeTable + RUN_ATTESTATION_TABLE: !Ref RunAttestationTable JWT_SIGNING_KEY_PARAM: /secure-rads/verifier-service/jwt-signing-key COST_HOST: ${env:COST_HOST_SECURE_RADS} @@ -128,6 +132,19 @@ resources: AttributeName: ExpiresAt Enabled: true + # One row per run that has attested. The conditional put is what makes a run single-use: concurrency-safe + # first writer wins, and every later /attest for the same run id is rejected. + RunAttestationTable: + Type: AWS::DynamoDB::Table + Properties: + BillingMode: PAY_PER_REQUEST + AttributeDefinitions: + - AttributeName: RunId + AttributeType: S + KeySchema: + - AttributeName: RunId + KeyType: HASH + # PEM bundle containing registry-service's client cert public cert # (csav/registry-service/tls/client.crt) - upload it here before deploy. TruststoreBucket: diff --git a/secure/verifier-service/src/handlers/attest.ts b/secure/verifier-service/src/handlers/attest.ts index 1a9b9a3..19bee71 100644 --- a/secure/verifier-service/src/handlers/attest.ts +++ b/secure/verifier-service/src/handlers/attest.ts @@ -6,6 +6,7 @@ import type { APIGatewayProxyHandlerV2 } from "aws-lambda"; import { verifyAttestationDocument } from "../lib/attestation.js"; import { type AttestBody, attestRequest } from "../lib/attestFlow.js"; import { consumeChallenge } from "../lib/challenge.js"; +import { claimRunAttestation } from "../lib/runAttestation.js"; import { issueTokenPair } from "../lib/tokens.js"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); @@ -66,6 +67,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { registered: registered.Item as { PCR0: string; PCR1: string; PCR2: string }, verifyDocument: verifyAttestationDocument, consumeChallenge, + claimRunAttestation, issueTokens: issueTokenPair, }); } finally { diff --git a/secure/verifier-service/src/lib/attestFlow.ts b/secure/verifier-service/src/lib/attestFlow.ts index f5e08e1..bbb97d9 100644 --- a/secure/verifier-service/src/lib/attestFlow.ts +++ b/secure/verifier-service/src/lib/attestFlow.ts @@ -18,6 +18,7 @@ export interface AttestDeps { registered: RegisteredPcrs; verifyDocument(raw: Buffer): AttestationDocument; consumeChallenge(challenge: string, algorithmId: string, runId: string): Promise; + claimRunAttestation(algorithmId: string, runId: string): Promise; issueTokens(algorithmId: string, runId: string): Promise; } @@ -62,6 +63,11 @@ export async function attestRequest(body: AttestBody, deps: AttestDeps): Promise return { statusCode: 401, body: "PCR mismatch: enclave measurements do not match registered algorithm" }; } + // A run attests once. Claiming after the measurement check keeps a wrong-image request from burning a run. + if (!(await deps.claimRunAttestation(body.algorithmId, body.runId))) { + return { statusCode: 409, body: "run already attested" }; + } + const tokens = await deps.issueTokens(body.algorithmId, body.runId); return { statusCode: 200, body: JSON.stringify(tokens) }; } diff --git a/secure/verifier-service/src/lib/runAttestation.ts b/secure/verifier-service/src/lib/runAttestation.ts new file mode 100644 index 0000000..bd3a832 --- /dev/null +++ b/secure/verifier-service/src/lib/runAttestation.ts @@ -0,0 +1,36 @@ +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; +import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; +import { CostPhase, costEvent } from "@srads/common/cost"; +import { requiredEnv } from "@srads/common/env"; + +const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); +const tableName = requiredEnv("RUN_ATTESTATION_TABLE"); + +export async function claimRunAttestation(algorithmId: string, runId: string): Promise { + try { + const write = await ddb.send( + new PutCommand({ + TableName: tableName, + Item: { + RunId: runId, + AlgorithmId: algorithmId, + AttestedAt: new Date().toISOString(), + }, + ConditionExpression: "attribute_not_exists(RunId)", + ReturnConsumedCapacity: "TOTAL", + }), + ); + await costEvent( + algorithmId, + runId, + CostPhase.Execution, + "dynamodb", + "write-capacity", + write.ConsumedCapacity?.CapacityUnits ?? 0, + ); + return true; + } catch (err) { + if ((err as Error).name === "ConditionalCheckFailedException") return false; + throw err; + } +}