| 1 |
package agent |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"crypto" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/sha256" |
| 10 |
"crypto/sha512" |
| 11 |
_ "embed" |
| 12 |
"encoding/base64" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"math" |
| 16 |
"runtime" |
| 17 |
"sync" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
|
| 21 |
"github.com/GoogleCloudPlatform/confidential-space/server/extract" |
| 22 |
"github.com/GoogleCloudPlatform/confidential-space/server/labels" |
| 23 |
attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation" |
| 24 |
"github.com/cenkalti/backoff/v4" |
| 25 |
"github.com/golang-jwt/jwt/v4" |
| 26 |
"github.com/google/go-cmp/cmp" |
| 27 |
gecel "github.com/google/go-eventlog/cel" |
| 28 |
"github.com/google/go-tdx-guest/testing/testdata" |
| 29 |
"github.com/google/go-tpm-tools/cel" |
| 30 |
"github.com/google/go-tpm-tools/client" |
| 31 |
"github.com/google/go-tpm-tools/internal/test" |
| 32 |
"github.com/google/go-tpm-tools/launcher/internal/experiments" |
| 33 |
"github.com/google/go-tpm-tools/launcher/internal/logging" |
| 34 |
"github.com/google/go-tpm-tools/launcher/internal/signaturediscovery" |
| 35 |
"github.com/google/go-tpm-tools/launcher/spec" |
| 36 |
attestpb "github.com/google/go-tpm-tools/proto/attest" |
| 37 |
tpmpb "github.com/google/go-tpm-tools/proto/tpm" |
| 38 |
"github.com/google/go-tpm-tools/server" |
| 39 |
"github.com/google/go-tpm-tools/verifier" |
| 40 |
"github.com/google/go-tpm-tools/verifier/fake" |
| 41 |
"github.com/google/go-tpm-tools/verifier/oci" |
| 42 |
"github.com/google/go-tpm-tools/verifier/oci/cosign" |
| 43 |
"google.golang.org/protobuf/encoding/protojson" |
| 44 |
"google.golang.org/protobuf/testing/protocmp" |
| 45 |
) |
| 46 |
|
| 47 |
const ( |
| 48 |
imageRef = "gcr.io/fakeRepo/fakeTestImage:latest" |
| 49 |
imageDigest = "sha256:adb591795f9e9047f9117163b83c2ebcd5edc4503644d59a98cf911aef0367f8" |
| 50 |
restartPolicy = spec.Always |
| 51 |
imageID = "sha256:d5496fd75dd8262f0495ab5706fc464659eb7f481e384700e6174b6c44144cae" |
| 52 |
arg = "-h" |
| 53 |
envK = "foo" |
| 54 |
envV = "foo" |
| 55 |
env = envK + "=" + envV |
| 56 |
) |
| 57 |
|
| 58 |
func TestAttestRacing(t *testing.T) { |
| 59 |
ctx := context.Background() |
| 60 |
tpm := test.GetTPM(t) |
| 61 |
defer client.CheckedClose(t, tpm) |
| 62 |
|
| 63 |
fakeSigner, err := rsa.GenerateKey(rand.Reader, 2048) |
| 64 |
if err != nil { |
| 65 |
t.Fatalf("failed to generate signing key %v", err) |
| 66 |
} |
| 67 |
|
| 68 |
verifierClient := fake.NewClient(fakeSigner) |
| 69 |
agent, err := CreateAttestationAgent(tpm, client.AttestationKeyECC, verifierClient, placeholderPrincipalFetcher, signaturediscovery.NewFakeClient(), spec.LaunchSpec{}, logging.SimpleLogger(), nil) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
var wg sync.WaitGroup |
| 74 |
for i := 0; i < 5; i++ { |
| 75 |
wg.Add(1) |
| 76 |
go func() { |
| 77 |
defer wg.Done() |
| 78 |
_, err := agent.Attest(ctx, AttestAgentOpts{}) |
| 79 |
if err != nil { |
| 80 |
t.Error(err) |
| 81 |
} |
| 82 |
}() |
| 83 |
} |
| 84 |
wg.Wait() |
| 85 |
agent.Close() |
| 86 |
} |
| 87 |
|
| 88 |
func TestAttest(t *testing.T) { |
| 89 |
ctx := context.Background() |
| 90 |
testCases := []struct { |
| 91 |
name string |
| 92 |
launchSpec spec.LaunchSpec |
| 93 |
principalIDTokenFetcher func(string) ([][]byte, error) |
| 94 |
containerSignaturesFetcher signaturediscovery.Fetcher |
| 95 |
}{ |
| 96 |
{ |
| 97 |
name: "Happy path with container signatures", |
| 98 |
launchSpec: spec.LaunchSpec{ |
| 99 |
SignedImageRepos: []string{signaturediscovery.FakeRepoWithSignatures}, |
| 100 |
}, |
| 101 |
principalIDTokenFetcher: placeholderPrincipalFetcher, |
| 102 |
containerSignaturesFetcher: signaturediscovery.NewFakeClient(), |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
for _, tc := range testCases { |
| 107 |
tc := tc |
| 108 |
t.Run(tc.name, func(t *testing.T) { |
| 109 |
t.Parallel() |
| 110 |
|
| 111 |
tpm := test.GetTPM(t) |
| 112 |
defer client.CheckedClose(t, tpm) |
| 113 |
|
| 114 |
fakeSigner, err := rsa.GenerateKey(rand.Reader, 2048) |
| 115 |
if err != nil { |
| 116 |
t.Fatalf("failed to generate signing key %v", err) |
| 117 |
} |
| 118 |
|
| 119 |
verifierClient := fake.NewClient(fakeSigner) |
| 120 |
|
| 121 |
agent, err := CreateAttestationAgent(tpm, client.AttestationKeyECC, verifierClient, tc.principalIDTokenFetcher, tc.containerSignaturesFetcher, tc.launchSpec, logging.SimpleLogger(), nil) |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("failed to create an attestation agent %v", err) |
| 124 |
} |
| 125 |
err = measureFakeEvents(agent) |
| 126 |
if err != nil { |
| 127 |
t.Errorf("failed to measure events: %v", err) |
| 128 |
} |
| 129 |
if err := agent.Refresh(ctx); err != nil { |
| 130 |
t.Fatalf("failed to fresh attestation agent: %v", err) |
| 131 |
} |
| 132 |
tokenBytes, err := agent.Attest(ctx, AttestAgentOpts{}) |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("failed to attest to Attestation Service: %v", err) |
| 135 |
} |
| 136 |
agent.Close() |
| 137 |
|
| 138 |
claims := &fake.Claims{} |
| 139 |
keyFunc := func(_ *jwt.Token) (interface{}, error) { return fakeSigner.Public(), nil } |
| 140 |
token, err := jwt.ParseWithClaims(string(tokenBytes), claims, keyFunc) |
| 141 |
if err != nil { |
| 142 |
t.Errorf("failed to parse token %s", err) |
| 143 |
} |
| 144 |
|
| 145 |
if err = claims.Valid(); err != nil { |
| 146 |
t.Errorf("Invalid exp, iat, or nbf: %s", err) |
| 147 |
} |
| 148 |
|
| 149 |
if !claims.VerifyAudience("https://sts.googleapis.com", true) { |
| 150 |
t.Errorf("Invalid aud") |
| 151 |
} |
| 152 |
|
| 153 |
if !claims.VerifyIssuer("fake-issuer-for-testing", true) { |
| 154 |
t.Errorf("Invalid iss") |
| 155 |
} |
| 156 |
|
| 157 |
if claims.Subject != "https://www.googleapis.com/compute/v1/projects/fakeProject/zones/fakeZone/instances/fakeInstance" { |
| 158 |
t.Errorf("Invalid sub") |
| 159 |
} |
| 160 |
|
| 161 |
got := claims.ContainerImageSignatures |
| 162 |
want := []fake.ContainerImageSignatureClaims{ |
| 163 |
{ |
| 164 |
Payload: "test data,ECDSA_P256_SHA256", |
| 165 |
Signature: base64.StdEncoding.EncodeToString([]byte("test data")), |
| 166 |
PubKey: "test data", |
| 167 |
SigAlg: "ECDSA_P256_SHA256", |
| 168 |
}, |
| 169 |
{ |
| 170 |
Payload: "hello world,RSASSA_PKCS1V15_SHA256", |
| 171 |
Signature: base64.StdEncoding.EncodeToString([]byte("hello world")), |
| 172 |
PubKey: "hello world", |
| 173 |
SigAlg: "RSASSA_PKCS1V15_SHA256", |
| 174 |
}, |
| 175 |
} |
| 176 |
if !cmp.Equal(got, want) { |
| 177 |
t.Errorf("ContainerImageSignatureClaims does not match expected value: got %v, want %v", got, want) |
| 178 |
} |
| 179 |
|
| 180 |
ms := &attestpb.MachineState{} |
| 181 |
err = protojson.Unmarshal([]byte(claims.MachineStateMarshaled), ms) |
| 182 |
if err != nil { |
| 183 |
t.Fatalf("failed to unmarshal claims as MachineState: %v", err) |
| 184 |
} |
| 185 |
validateContainerState(t, ms.GetCos()) |
| 186 |
fmt.Printf("token.Claims: %v\n", token.Claims) |
| 187 |
}) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func placeholderPrincipalFetcher(_ string) ([][]byte, error) { |
| 192 |
return [][]byte{}, nil |
| 193 |
} |
| 194 |
|
| 195 |
func TestFetchContainerImageSignatures(t *testing.T) { |
| 196 |
ctx := context.Background() |
| 197 |
|
| 198 |
testCases := []struct { |
| 199 |
name string |
| 200 |
targetRepos []string |
| 201 |
wantBase64Sigs []string |
| 202 |
wantSignatureClaims []fake.ContainerImageSignatureClaims |
| 203 |
wantPartialErrLen int |
| 204 |
}{ |
| 205 |
{ |
| 206 |
name: "fetchContainerImageSignatures with repos that have signatures", |
| 207 |
targetRepos: []string{signaturediscovery.FakeRepoWithSignatures}, |
| 208 |
wantBase64Sigs: []string{ |
| 209 |
"dGVzdCBkYXRh", // base64 encoded "test data". |
| 210 |
"aGVsbG8gd29ybGQ=", // base64 encoded "hello world". |
| 211 |
}, |
| 212 |
wantSignatureClaims: []fake.ContainerImageSignatureClaims{ |
| 213 |
{ |
| 214 |
Payload: "test data,ECDSA_P256_SHA256", |
| 215 |
Signature: base64.StdEncoding.EncodeToString([]byte("test data")), |
| 216 |
PubKey: "test data", |
| 217 |
SigAlg: "ECDSA_P256_SHA256", |
| 218 |
}, |
| 219 |
{ |
| 220 |
Payload: "hello world,RSASSA_PKCS1V15_SHA256", |
| 221 |
Signature: base64.StdEncoding.EncodeToString([]byte("hello world")), |
| 222 |
PubKey: "hello world", |
| 223 |
SigAlg: "RSASSA_PKCS1V15_SHA256", |
| 224 |
}, |
| 225 |
}, |
| 226 |
wantPartialErrLen: 0, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "fetchContainerImageSignatures with nil target repos", |
| 230 |
targetRepos: nil, |
| 231 |
wantBase64Sigs: nil, |
| 232 |
wantSignatureClaims: nil, |
| 233 |
wantPartialErrLen: 0, |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "fetchContainerImageSignatures with empty target repos", |
| 237 |
targetRepos: []string{}, |
| 238 |
wantBase64Sigs: nil, |
| 239 |
wantSignatureClaims: nil, |
| 240 |
wantPartialErrLen: 0, |
| 241 |
}, |
| 242 |
{ |
| 243 |
name: "fetchContainerImageSignatures with non exist repos", |
| 244 |
targetRepos: []string{signaturediscovery.FakeNonExistRepo}, |
| 245 |
wantBase64Sigs: nil, |
| 246 |
wantSignatureClaims: nil, |
| 247 |
wantPartialErrLen: 0, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "fetchContainerImageSignatures with repos that don't have signatures", |
| 251 |
targetRepos: []string{signaturediscovery.FakeRepoWithNoSignatures}, |
| 252 |
wantBase64Sigs: nil, |
| 253 |
wantSignatureClaims: nil, |
| 254 |
wantPartialErrLen: 0, |
| 255 |
}, |
| 256 |
{ |
| 257 |
name: "fetchContainerImageSignatures with repos that have all invalid signatures", |
| 258 |
targetRepos: []string{signaturediscovery.FakeRepoWithAllInvalidSignatures}, |
| 259 |
wantBase64Sigs: []string{ |
| 260 |
"aW52YWxpZCBzaWduYXR1cmU=", // base64 encoded "invalid signature". |
| 261 |
"aW52YWxpZCBzaWduYXR1cmU=", // base64 encoded "invalid signature". |
| 262 |
}, |
| 263 |
wantSignatureClaims: nil, |
| 264 |
wantPartialErrLen: 2, |
| 265 |
}, |
| 266 |
{ |
| 267 |
name: "fetchContainerImageSignatures with repos that have partial valid signatures", |
| 268 |
targetRepos: []string{signaturediscovery.FakeRepoWithPartialValidSignatures}, |
| 269 |
wantBase64Sigs: []string{ |
| 270 |
"dGVzdCBkYXRh", // base64 encoded "test data". |
| 271 |
"aW52YWxpZCBzaWduYXR1cmU=", // base64 encoded "invalid signature". |
| 272 |
}, |
| 273 |
wantSignatureClaims: []fake.ContainerImageSignatureClaims{ |
| 274 |
{ |
| 275 |
Payload: "test data,ECDSA_P256_SHA256", |
| 276 |
Signature: base64.StdEncoding.EncodeToString([]byte("test data")), |
| 277 |
PubKey: "test data", |
| 278 |
SigAlg: "ECDSA_P256_SHA256", |
| 279 |
}, |
| 280 |
}, |
| 281 |
wantPartialErrLen: 1, |
| 282 |
}, |
| 283 |
} |
| 284 |
|
| 285 |
for _, tc := range testCases { |
| 286 |
t.Run(tc.name, func(t *testing.T) { |
| 287 |
tpm := test.GetTPM(t) |
| 288 |
defer client.CheckedClose(t, tpm) |
| 289 |
ak, err := client.AttestationKeyECC(tpm) |
| 290 |
if err != nil { |
| 291 |
t.Fatalf("failed to create AK: %v", err) |
| 292 |
} |
| 293 |
|
| 294 |
testRetryPolicy := func() backoff.BackOff { |
| 295 |
b := backoff.NewExponentialBackOff() |
| 296 |
b.MaxElapsedTime = time.Millisecond |
| 297 |
return b |
| 298 |
} |
| 299 |
|
| 300 |
sdClient := signaturediscovery.NewFakeClient() |
| 301 |
gotSigs := fetchContainerImageSignatures(ctx, sdClient, tc.targetRepos, testRetryPolicy, logging.SimpleLogger()) |
| 302 |
if len(gotSigs) != len(tc.wantBase64Sigs) { |
| 303 |
t.Errorf("fetchContainerImageSignatures did not return expected signatures for test case %s, got signatures length %d, but want %d", tc.name, len(gotSigs), len(tc.wantBase64Sigs)) |
| 304 |
} |
| 305 |
gotBase64Sigs := convertOCISignatureToBase64(t, gotSigs) |
| 306 |
if !cmp.Equal(gotBase64Sigs, tc.wantBase64Sigs) { |
| 307 |
t.Errorf("fetchContainerImageSignatures did not return expected signatures for test case %s, got signatures %v, but want %v", tc.name, gotBase64Sigs, tc.wantBase64Sigs) |
| 308 |
} |
| 309 |
|
| 310 |
fakeSigner, err := rsa.GenerateKey(rand.Reader, 2048) |
| 311 |
if err != nil { |
| 312 |
t.Errorf("failed to generate signing key %v", err) |
| 313 |
} |
| 314 |
verifierClient := fake.NewClient(fakeSigner) |
| 315 |
chal, err := verifierClient.CreateChallenge(ctx) |
| 316 |
if err != nil { |
| 317 |
t.Fatalf("failed to create challenge %v", err) |
| 318 |
} |
| 319 |
attestation, err := ak.Attest(client.AttestOpts{Nonce: chal.Nonce}) |
| 320 |
if err != nil { |
| 321 |
t.Fatalf("failed to attest %v", err) |
| 322 |
} |
| 323 |
|
| 324 |
var containerSigs []*verifier.ContainerSignature |
| 325 |
for _, gotSig := range gotSigs { |
| 326 |
sig, err := convertOCIToContainerSignature(gotSig) |
| 327 |
if err != nil { |
| 328 |
t.Fatalf("failed to convert gotSigs: %v", err) |
| 329 |
} |
| 330 |
|
| 331 |
containerSigs = append(containerSigs, sig) |
| 332 |
} |
| 333 |
req := verifier.VerifyAttestationRequest{ |
| 334 |
Attestation: attestation, |
| 335 |
ContainerImageSignatures: containerSigs, |
| 336 |
} |
| 337 |
got, err := verifierClient.VerifyAttestation(context.Background(), req) |
| 338 |
if err != nil { |
| 339 |
t.Fatalf("VerifyAttestation failed: %v", err) |
| 340 |
} |
| 341 |
claims := &fake.Claims{} |
| 342 |
keyFunc := func(_ *jwt.Token) (interface{}, error) { return fakeSigner.Public(), nil } |
| 343 |
_, err = jwt.ParseWithClaims(string(got.ClaimsToken), claims, keyFunc) |
| 344 |
if err != nil { |
| 345 |
t.Errorf("failed to parse token %s", err) |
| 346 |
} |
| 347 |
|
| 348 |
gotSignatureClaims := claims.ContainerImageSignatures |
| 349 |
if !cmp.Equal(gotSignatureClaims, tc.wantSignatureClaims) { |
| 350 |
t.Errorf("ContainerImageSignatureClaims does not match expected value: got %v, want %v", gotSignatureClaims, tc.wantSignatureClaims) |
| 351 |
} |
| 352 |
if len(got.PartialErrs) != tc.wantPartialErrLen { |
| 353 |
t.Errorf("VerifyAttestation did not return expected partial error length for test case %s, got partial errors length %d, but want %d", tc.name, len(got.ClaimsToken), tc.wantPartialErrLen) |
| 354 |
} |
| 355 |
}) |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
// Represents the return value from FetchImageSignatures |
| 360 |
type returnVal struct { |
| 361 |
result []oci.Signature |
| 362 |
err error |
| 363 |
} |
| 364 |
|
| 365 |
// Implments signaturediscovery.Fetcher methods |
| 366 |
type failingClient struct { |
| 367 |
mu sync.Mutex |
| 368 |
results map[string][]returnVal |
| 369 |
numTimes map[string]int |
| 370 |
} |
| 371 |
|
| 372 |
func NewFailingClient(mymap map[string][]returnVal) signaturediscovery.Fetcher { |
| 373 |
numTimes := map[string]int{} |
| 374 |
for k := range mymap { |
| 375 |
numTimes[k] = 0 |
| 376 |
} |
| 377 |
return &failingClient{ |
| 378 |
results: mymap, |
| 379 |
numTimes: numTimes, |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// Return test data in a round robin fashion |
| 384 |
func (f *failingClient) FetchImageSignatures(_ context.Context, targetRepository string) ([]oci.Signature, error) { |
| 385 |
f.mu.Lock() |
| 386 |
defer f.mu.Unlock() |
| 387 |
|
| 388 |
attempt := f.numTimes[targetRepository] |
| 389 |
r := f.results[targetRepository][attempt] |
| 390 |
f.numTimes[targetRepository] = intMin(attempt+1, len(f.results[targetRepository])-1) |
| 391 |
return r.result, r.err |
| 392 |
} |
| 393 |
|
| 394 |
func intMin(a, b int) int { |
| 395 |
return int(math.Min(float64(a), float64(b))) |
| 396 |
} |
| 397 |
|
| 398 |
func TestFetchContainerImageSignatures_RetriesOnFailure(t *testing.T) { |
| 399 |
ctx := context.Background() |
| 400 |
|
| 401 |
testCases := []struct { |
| 402 |
name string |
| 403 |
resultmap map[string][]returnVal |
| 404 |
}{ |
| 405 |
{ |
| 406 |
name: "one repo, no failures", |
| 407 |
resultmap: map[string][]returnVal{ |
| 408 |
"repo1": { |
| 409 |
returnVal{ |
| 410 |
result: []oci.Signature{cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256)}, |
| 411 |
err: nil, |
| 412 |
}, |
| 413 |
}, |
| 414 |
}, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "one repo fails", |
| 418 |
resultmap: map[string][]returnVal{ |
| 419 |
"repo1": { |
| 420 |
returnVal{ |
| 421 |
result: []oci.Signature{cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256)}, |
| 422 |
err: fmt.Errorf("partial error"), |
| 423 |
}, |
| 424 |
}, |
| 425 |
}, |
| 426 |
}, |
| 427 |
{ |
| 428 |
name: "one repo, failure then success", |
| 429 |
resultmap: map[string][]returnVal{ |
| 430 |
"repo1": { |
| 431 |
returnVal{ |
| 432 |
result: []oci.Signature{}, |
| 433 |
err: fmt.Errorf("failure 1"), |
| 434 |
}, |
| 435 |
returnVal{ |
| 436 |
result: []oci.Signature{cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256)}, |
| 437 |
err: nil, |
| 438 |
}, |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
{ |
| 443 |
name: "two repos, no failures", |
| 444 |
resultmap: map[string][]returnVal{ |
| 445 |
"repo1": { |
| 446 |
returnVal{ |
| 447 |
result: []oci.Signature{cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256)}, |
| 448 |
err: nil, |
| 449 |
}, |
| 450 |
}, |
| 451 |
"repo2": { |
| 452 |
returnVal{ |
| 453 |
result: []oci.Signature{cosign.NewFakeSignature("test data again", oci.ECDSAP256SHA256)}, |
| 454 |
err: nil, |
| 455 |
}, |
| 456 |
}, |
| 457 |
}, |
| 458 |
}, |
| 459 |
{ |
| 460 |
name: "two repos, failure then success", |
| 461 |
resultmap: map[string][]returnVal{ |
| 462 |
"failrepo": { |
| 463 |
returnVal{ |
| 464 |
result: []oci.Signature{}, |
| 465 |
err: fmt.Errorf("failure 1"), |
| 466 |
}, |
| 467 |
returnVal{ |
| 468 |
result: []oci.Signature{cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256)}, |
| 469 |
err: nil, |
| 470 |
}, |
| 471 |
}, |
| 472 |
"successRepo": { |
| 473 |
returnVal{ |
| 474 |
result: []oci.Signature{cosign.NewFakeSignature("test data again", oci.ECDSAP256SHA256)}, |
| 475 |
err: nil, |
| 476 |
}, |
| 477 |
}, |
| 478 |
}, |
| 479 |
}, |
| 480 |
{ |
| 481 |
name: "two repos, failures", |
| 482 |
resultmap: map[string][]returnVal{ |
| 483 |
"repo1": { |
| 484 |
returnVal{ |
| 485 |
result: []oci.Signature{}, |
| 486 |
err: fmt.Errorf("failure 1"), |
| 487 |
}, |
| 488 |
}, |
| 489 |
"repo2": { |
| 490 |
returnVal{ |
| 491 |
result: []oci.Signature{}, |
| 492 |
err: fmt.Errorf("failure 2"), |
| 493 |
}, |
| 494 |
}, |
| 495 |
}, |
| 496 |
}, |
| 497 |
} |
| 498 |
|
| 499 |
for _, tc := range testCases { |
| 500 |
t.Run(tc.name, func(t *testing.T) { |
| 501 |
sdClient := NewFailingClient(tc.resultmap) |
| 502 |
retryPolicy := func() backoff.BackOff { |
| 503 |
b := backoff.NewExponentialBackOff() |
| 504 |
return backoff.WithMaxRetries(b, 2) |
| 505 |
} |
| 506 |
|
| 507 |
repos := []string{} |
| 508 |
wantSigs := []oci.Signature{} |
| 509 |
for k, v := range tc.resultmap { |
| 510 |
repos = append(repos, k) |
| 511 |
for _, result := range v { |
| 512 |
if result.err == nil { |
| 513 |
wantSigs = append(wantSigs, result.result...) |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
gotSigs := fetchContainerImageSignatures(ctx, sdClient, repos, retryPolicy, logging.SimpleLogger()) |
| 519 |
|
| 520 |
if len(gotSigs) != len(wantSigs) { |
| 521 |
t.Errorf("fetchContainerImageSignatures did not return expected signatures for test case %s, got signatures length %d, but want %d", tc.name, len(gotSigs), len(wantSigs)) |
| 522 |
} |
| 523 |
if !cmp.Equal(convertOCISignatureToBase64(t, gotSigs), convertOCISignatureToBase64(t, wantSigs)) { |
| 524 |
t.Errorf("fetchContainerImageSignatures did not return expected signatures for test case %s, got signatures %v, but want %v", tc.name, gotSigs, wantSigs) |
| 525 |
} |
| 526 |
}) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
func TestCacheConcurrentSetGet(t *testing.T) { |
| 531 |
cache := &sigsCache{} |
| 532 |
if sigs := cache.get(); len(sigs) != 0 { |
| 533 |
t.Errorf("signature cache should be empty, but got: %v", sigs) |
| 534 |
} |
| 535 |
|
| 536 |
var wg sync.WaitGroup |
| 537 |
for i := 0; i < runtime.NumCPU(); i++ { |
| 538 |
wg.Add(1) |
| 539 |
go func(idx int) { |
| 540 |
defer wg.Done() |
| 541 |
if idx%2 == 1 { |
| 542 |
sigs := generateRandSigs(t) |
| 543 |
cache.set(sigs) |
| 544 |
} else { |
| 545 |
cache.get() |
| 546 |
} |
| 547 |
}(i) |
| 548 |
} |
| 549 |
wg.Wait() |
| 550 |
} |
| 551 |
|
| 552 |
func generateRandSigs(t *testing.T) []oci.Signature { |
| 553 |
t.Helper() |
| 554 |
|
| 555 |
b := make([]byte, 32) |
| 556 |
_, err := rand.Read(b) |
| 557 |
if err != nil { |
| 558 |
t.Fatalf("Unable to generate random bytes: %v", err) |
| 559 |
} |
| 560 |
|
| 561 |
randB64Str := base64.StdEncoding.EncodeToString(b) |
| 562 |
return []oci.Signature{ |
| 563 |
cosign.NewFakeSignature(randB64Str, oci.ECDSAP256SHA256), |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
func convertOCISignatureToBase64(t *testing.T, sigs []oci.Signature) []string { |
| 568 |
t.Helper() |
| 569 |
|
| 570 |
var base64Sigs []string |
| 571 |
for _, sig := range sigs { |
| 572 |
b64Sig, err := sig.Base64Encoded() |
| 573 |
if err != nil { |
| 574 |
t.Fatalf("oci.Signature did not return expected base64 signature: %v", err) |
| 575 |
} |
| 576 |
base64Sigs = append(base64Sigs, b64Sig) |
| 577 |
} |
| 578 |
|
| 579 |
return base64Sigs |
| 580 |
} |
| 581 |
|
| 582 |
func validateContainerState(t *testing.T, cos *attestpb.AttestedCosState) { |
| 583 |
if cos == nil { |
| 584 |
t.Errorf("failed to find COS state in MachineState") |
| 585 |
} |
| 586 |
ctr := cos.GetContainer() |
| 587 |
if ctr == nil { |
| 588 |
t.Errorf("failed to find ContainerState in CosState") |
| 589 |
return |
| 590 |
} |
| 591 |
if ctr.ImageReference != imageRef { |
| 592 |
t.Errorf("got image ref %v, want image ref %v", ctr.ImageReference, imageRef) |
| 593 |
} |
| 594 |
if ctr.ImageDigest != imageDigest { |
| 595 |
t.Errorf("got image digest %v, want image digest %v", ctr.ImageDigest, imageDigest) |
| 596 |
} |
| 597 |
if ctr.RestartPolicy.String() != string(restartPolicy) { |
| 598 |
t.Errorf("got restart policy %v, want restart policy %v", ctr.RestartPolicy.String(), restartPolicy) |
| 599 |
} |
| 600 |
if len(ctr.Args) != 1 { |
| 601 |
t.Fatalf("got args %v, want length 1", ctr.Args) |
| 602 |
} |
| 603 |
if ctr.Args[0] != arg { |
| 604 |
t.Errorf("got args %v, want [%v]", ctr.Args, arg) |
| 605 |
} |
| 606 |
if len(ctr.OverriddenArgs) != 1 { |
| 607 |
t.Fatalf("got overridden args %v, want length 1", ctr.OverriddenArgs) |
| 608 |
} |
| 609 |
if ctr.OverriddenArgs[0] != arg { |
| 610 |
t.Errorf("got overridden args %v, want [%v]", ctr.OverriddenArgs, arg) |
| 611 |
} |
| 612 |
|
| 613 |
if len(ctr.EnvVars) != 1 { |
| 614 |
t.Fatalf("got envs %v, want length 1", ctr.EnvVars) |
| 615 |
} |
| 616 |
if val := ctr.EnvVars[envK]; val != envV { |
| 617 |
t.Errorf("got args %v, want map[%v]", ctr.EnvVars, env) |
| 618 |
} |
| 619 |
if len(ctr.OverriddenEnvVars) != 1 { |
| 620 |
t.Fatalf("got overridden envs %v, want length 1", ctr.OverriddenEnvVars) |
| 621 |
} |
| 622 |
if val := ctr.EnvVars[envK]; val != envV { |
| 623 |
t.Errorf("got overridden args %v, want map[%v]", ctr.OverriddenEnvVars, env) |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
func measureFakeEvents(attestAgent AttestationAgent) error { |
| 628 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ImageRefType, EventContent: []byte(imageRef)}); err != nil { |
| 629 |
return err |
| 630 |
} |
| 631 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ImageDigestType, EventContent: []byte(imageDigest)}); err != nil { |
| 632 |
return err |
| 633 |
} |
| 634 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.RestartPolicyType, EventContent: []byte(restartPolicy)}); err != nil { |
| 635 |
return err |
| 636 |
} |
| 637 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ImageIDType, EventContent: []byte(imageID)}); err != nil { |
| 638 |
return err |
| 639 |
} |
| 640 |
|
| 641 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ArgType, EventContent: []byte(arg)}); err != nil { |
| 642 |
return err |
| 643 |
} |
| 644 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.EnvVarType, EventContent: []byte(env)}); err != nil { |
| 645 |
return err |
| 646 |
} |
| 647 |
|
| 648 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.OverrideEnvType, EventContent: []byte(env)}); err != nil { |
| 649 |
return err |
| 650 |
} |
| 651 |
if err := attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.OverrideArgType, EventContent: []byte(arg)}); err != nil { |
| 652 |
return err |
| 653 |
} |
| 654 |
return nil |
| 655 |
} |
| 656 |
|
| 657 |
type fakeTdxAttestRoot struct { |
| 658 |
cel gecel.CEL |
| 659 |
receivedNonce []byte |
| 660 |
tdxQuote []byte |
| 661 |
deviceRoTS []DeviceROT |
| 662 |
} |
| 663 |
|
| 664 |
func (f *fakeTdxAttestRoot) Extend(c gecel.Content) error { |
| 665 |
return f.cel.AppendEvent(c, []crypto.Hash{crypto.SHA384}, cel.CosRTMR, func(_ crypto.Hash, _ int, _ []byte) error { |
| 666 |
return nil |
| 667 |
}) |
| 668 |
} |
| 669 |
|
| 670 |
func (f *fakeTdxAttestRoot) GetCEL() gecel.CEL { |
| 671 |
return f.cel |
| 672 |
} |
| 673 |
|
| 674 |
func (f *fakeTdxAttestRoot) Attest(nonce []byte) (any, error) { |
| 675 |
f.receivedNonce = nonce |
| 676 |
var nvAtt *attestationpb.NvidiaAttestationReport |
| 677 |
for _, deviceRoT := range f.deviceRoTS { |
| 678 |
att, err := deviceRoT.Attest(nonce) |
| 679 |
if err != nil { |
| 680 |
return nil, err |
| 681 |
} |
| 682 |
switch v := att.(type) { |
| 683 |
case *attestationpb.NvidiaAttestationReport: |
| 684 |
nvAtt = v |
| 685 |
default: |
| 686 |
return nil, fmt.Errorf("unknown device attestation type: %T", v) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
return &verifier.TDCCELAttestation{ |
| 691 |
TdQuote: f.tdxQuote, |
| 692 |
NvidiaAttestation: nvAtt, |
| 693 |
}, nil |
| 694 |
} |
| 695 |
|
| 696 |
func (f *fakeTdxAttestRoot) ComputeNonce(challenge []byte, extraData []byte) []byte { |
| 697 |
challengeData := challenge |
| 698 |
if extraData != nil { |
| 699 |
extraDataDigest := sha512.Sum512(extraData) |
| 700 |
challengeData = append(challenge, extraDataDigest[:]...) |
| 701 |
} |
| 702 |
challengeDigest := sha512.Sum512(challengeData) |
| 703 |
finalNonce := sha512.Sum512(append([]byte(labels.WorkloadAttestation), challengeDigest[:]...)) |
| 704 |
return finalNonce[:] |
| 705 |
} |
| 706 |
|
| 707 |
//go:embed testdata/cel.b64 |
| 708 |
var celB64 string |
| 709 |
|
| 710 |
func (f *fakeTdxAttestRoot) AddDeviceROTs(deviceRoTS []DeviceROT) { |
| 711 |
f.deviceRoTS = append(f.deviceRoTS, deviceRoTS...) |
| 712 |
} |
| 713 |
|
| 714 |
type fakeGPURoT struct{} |
| 715 |
|
| 716 |
func (f *fakeGPURoT) Attest(nonce []byte) (any, error) { |
| 717 |
if len(nonce) == 0 { |
| 718 |
return nil, fmt.Errorf("fake GPU attestation failed") |
| 719 |
} |
| 720 |
return &attestationpb.NvidiaAttestationReport{ |
| 721 |
CcFeature: &attestationpb.NvidiaAttestationReport_Spt{ |
| 722 |
Spt: &attestationpb.NvidiaAttestationReport_SinglePassthroughAttestation{ |
| 723 |
GpuQuote: &attestationpb.GpuInfo{Uuid: "fake-gpu-uuid"}, |
| 724 |
}, |
| 725 |
}, |
| 726 |
}, nil |
| 727 |
} |
| 728 |
func TestTdxAttestRoot(t *testing.T) { |
| 729 |
testCases := []struct { |
| 730 |
name string |
| 731 |
tdxAttestRoot *fakeTdxAttestRoot |
| 732 |
nonce []byte |
| 733 |
wantGPU bool |
| 734 |
wantPass bool |
| 735 |
}{ |
| 736 |
{ |
| 737 |
name: "success tdxAttestRoot w/o GPU device", |
| 738 |
tdxAttestRoot: &fakeTdxAttestRoot{}, |
| 739 |
nonce: []byte("test-nonce"), |
| 740 |
wantPass: true, |
| 741 |
}, |
| 742 |
{ |
| 743 |
name: "success tdxAttestRoot w/ GPU device", |
| 744 |
tdxAttestRoot: &fakeTdxAttestRoot{ |
| 745 |
deviceRoTS: []DeviceROT{&fakeGPURoT{}}, |
| 746 |
}, |
| 747 |
nonce: []byte("test-nonce"), |
| 748 |
wantGPU: true, |
| 749 |
wantPass: true, |
| 750 |
}, |
| 751 |
{ |
| 752 |
name: "failed tdxAttestRoot w/ GPU device", |
| 753 |
tdxAttestRoot: &fakeTdxAttestRoot{ |
| 754 |
deviceRoTS: []DeviceROT{&fakeGPURoT{}}, |
| 755 |
}, |
| 756 |
nonce: []byte(""), |
| 757 |
wantPass: false, |
| 758 |
}, |
| 759 |
} |
| 760 |
|
| 761 |
for _, tc := range testCases { |
| 762 |
t.Run(tc.name, func(t *testing.T) { |
| 763 |
attestation, err := tc.tdxAttestRoot.Attest(tc.nonce) |
| 764 |
if gotPass := (err == nil); gotPass != tc.wantPass { |
| 765 |
t.Errorf("tdxAttestRoot.Attest() did not return expected attestation result, got %v, want %v", gotPass, tc.wantPass) |
| 766 |
} |
| 767 |
if tc.wantPass && tc.wantGPU { |
| 768 |
if att := attestation.(*verifier.TDCCELAttestation); att.NvidiaAttestation == nil { |
| 769 |
t.Error("tdxAttestRoot.Attest() did not return expected GPU attestation, want GPU attestation, but got nil") |
| 770 |
} |
| 771 |
} |
| 772 |
}) |
| 773 |
} |
| 774 |
|
| 775 |
} |
| 776 |
|
| 777 |
func TestAttestationEvidence_TDX_Success(t *testing.T) { |
| 778 |
ctx := context.Background() |
| 779 |
tpm := test.GetTPM(t) |
| 780 |
defer client.CheckedClose(t, tpm) |
| 781 |
|
| 782 |
ak, err := client.AttestationKeyECC(tpm) |
| 783 |
if err != nil { |
| 784 |
t.Fatalf("failed to create AK: %v", err) |
| 785 |
} |
| 786 |
defer ak.Close() |
| 787 |
fakeCert := test.GetTestCertForKey(t, ak.PublicKey()) |
| 788 |
if err := ak.SetCert(fakeCert); err != nil { |
| 789 |
t.Fatal(err) |
| 790 |
} |
| 791 |
|
| 792 |
testTDXQuote := testdata.RawQuote |
| 793 |
testCEL, err := base64.StdEncoding.DecodeString(celB64) |
| 794 |
if err != nil { |
| 795 |
t.Fatalf("failed to decode cel.b64: %v", err) |
| 796 |
} |
| 797 |
decodedCEL, err := gecel.DecodeToCEL(bytes.NewBuffer(testCEL)) |
| 798 |
if err != nil { |
| 799 |
t.Fatalf("failed to decode test CEL: %v", err) |
| 800 |
} |
| 801 |
fakeRoot := &fakeTdxAttestRoot{ |
| 802 |
cel: decodedCEL, |
| 803 |
tdxQuote: testTDXQuote, |
| 804 |
} |
| 805 |
attestAgent := &agent{ |
| 806 |
avRot: fakeRoot, |
| 807 |
fetchedAK: ak, |
| 808 |
launchSpec: spec.LaunchSpec{ |
| 809 |
Experiments: experiments.Experiments{ |
| 810 |
EnableAttestationEvidence: true, |
| 811 |
}, |
| 812 |
}, |
| 813 |
} |
| 814 |
|
| 815 |
if err := measureFakeEvents(attestAgent); err != nil { |
| 816 |
t.Fatalf("failed to measure events: %v", err) |
| 817 |
} |
| 818 |
|
| 819 |
challenge := []byte("test-challenge") |
| 820 |
extraData := []byte("test-extra-data") |
| 821 |
att, err := attestAgent.AttestationEvidence(ctx, challenge, extraData) |
| 822 |
if err != nil { |
| 823 |
t.Fatalf("AttestationEvidence failed: %v", err) |
| 824 |
} |
| 825 |
|
| 826 |
// Verify the nonce passed to Attest was derived from challenge+extraData. |
| 827 |
expectedNonce := fakeRoot.ComputeNonce(challenge, extraData) |
| 828 |
if !bytes.Equal(fakeRoot.receivedNonce, expectedNonce) { |
| 829 |
t.Errorf("got nonce %x, want %x", fakeRoot.receivedNonce, expectedNonce) |
| 830 |
} |
| 831 |
|
| 832 |
if att.GetQuote().GetTdxCcelQuote() == nil { |
| 833 |
t.Fatal("expected TDCCELAttestation to be populated for TDX") |
| 834 |
} |
| 835 |
|
| 836 |
if !bytes.Equal(att.GetQuote().GetTdxCcelQuote().GetTdQuote(), testTDXQuote) { |
| 837 |
t.Errorf("TDQuote mismatch: got %x, want %x", att.GetQuote().GetTdxCcelQuote().GetTdQuote(), testTDXQuote) |
| 838 |
} |
| 839 |
if att.GetQuote().GetTpmQuote() != nil { |
| 840 |
t.Error("expected TPMQuote to be nil for TDX attestation") |
| 841 |
} |
| 842 |
if !bytes.Equal(att.GetQuote().GetTdxCcelQuote().GetCelLaunchEventLog(), testCEL) { |
| 843 |
t.Errorf("CELLaunchEventLog mismatch: got %x, want %x", att.GetQuote().GetTdxCcelQuote().GetCelLaunchEventLog(), testCEL) |
| 844 |
} |
| 845 |
if !bytes.Equal(att.Challenge, challenge) { |
| 846 |
t.Errorf("challenge mismatch: got %x, want %x", att.Challenge, challenge) |
| 847 |
} |
| 848 |
if !bytes.Equal(att.ExtraData, extraData) { |
| 849 |
t.Errorf("extraData mismatch: got %x, want %x", att.ExtraData, extraData) |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
func TestAttestationEvidence_TPM_Success(t *testing.T) { |
| 854 |
ctx := context.Background() |
| 855 |
tpm := test.GetTPM(t) |
| 856 |
defer client.CheckedClose(t, tpm) |
| 857 |
|
| 858 |
fakeSigner, err := rsa.GenerateKey(rand.Reader, 2048) |
| 859 |
if err != nil { |
| 860 |
t.Fatalf("failed to generate signing key %v", err) |
| 861 |
} |
| 862 |
verifierClient := fake.NewClient(fakeSigner) |
| 863 |
|
| 864 |
ak, err := client.AttestationKeyECC(tpm) |
| 865 |
if err != nil { |
| 866 |
t.Fatalf("failed to create AK: %v", err) |
| 867 |
} |
| 868 |
defer ak.Close() |
| 869 |
|
| 870 |
agent, err := CreateAttestationAgent(tpm, client.AttestationKeyECC, verifierClient, placeholderPrincipalFetcher, signaturediscovery.NewFakeClient(), spec.LaunchSpec{ |
| 871 |
Experiments: experiments.Experiments{ |
| 872 |
EnableAttestationEvidence: true, |
| 873 |
}, |
| 874 |
}, logging.SimpleLogger(), nil) |
| 875 |
if err != nil { |
| 876 |
t.Fatalf("failed to create agent: %v", err) |
| 877 |
} |
| 878 |
defer agent.Close() |
| 879 |
|
| 880 |
if err := measureFakeEvents(agent); err != nil { |
| 881 |
t.Fatalf("failed to measure events: %v", err) |
| 882 |
} |
| 883 |
|
| 884 |
challenge := []byte("test-challenge") |
| 885 |
extraData := []byte("test-extra-data") |
| 886 |
att, err := agent.AttestationEvidence(ctx, challenge, extraData) |
| 887 |
if err != nil { |
| 888 |
t.Fatalf("AttestationEvidence failed on TPM: %v", err) |
| 889 |
} |
| 890 |
|
| 891 |
if att.GetQuote().GetTpmQuote() == nil { |
| 892 |
t.Fatal("expected Attestation to be populated for TPM") |
| 893 |
} |
| 894 |
if att.GetQuote().GetTdxCcelQuote() != nil { |
| 895 |
t.Fatal("expected TDCCELAttestation to be nil for TPM") |
| 896 |
} |
| 897 |
|
| 898 |
// Test the evidence can be verified by the server by re-computing the nonce and calling server.VerifyAttestation. |
| 899 |
extraDataDigest := sha256.Sum256(extraData) |
| 900 |
challengeData := append(challenge, extraDataDigest[:]...) |
| 901 |
challengeDigest := sha256.Sum256(challengeData) |
| 902 |
tpmNonce := sha256.Sum256(append([]byte(labels.WorkloadAttestation), challengeDigest[:]...)) |
| 903 |
|
| 904 |
pbAttestation, err := ak.Attest(client.AttestOpts{Nonce: tpmNonce[:]}) |
| 905 |
if err != nil { |
| 906 |
t.Fatalf("ak.Attest() failed: %v", err) |
| 907 |
} |
| 908 |
|
| 909 |
_, err = server.VerifyAttestation(pbAttestation, server.VerifyOpts{ |
| 910 |
Nonce: tpmNonce[:], |
| 911 |
TrustedAKs: []crypto.PublicKey{ak.PublicKey()}, |
| 912 |
}) |
| 913 |
if err != nil { |
| 914 |
t.Errorf("server.VerifyAttestation failed: %v", err) |
| 915 |
} |
| 916 |
|
| 917 |
// Test that the COS state can be extracted from the CEL and contains the expected values. |
| 918 |
decodedCEL, err := gecel.DecodeToCEL(bytes.NewBuffer(att.GetQuote().GetTpmQuote().GetCelLaunchEventLog())) |
| 919 |
if err != nil { |
| 920 |
t.Fatalf("gecel.DecodeToCEL failed: %v", err) |
| 921 |
} |
| 922 |
|
| 923 |
cosState, err := extract.VerifiedCOSState(decodedCEL, uint8(gecel.PCRType)) |
| 924 |
if err != nil { |
| 925 |
t.Errorf("extract.VerifiedCOSState failed: %v", err) |
| 926 |
} else { |
| 927 |
ctr := cosState.GetContainer() |
| 928 |
if ctr.GetImageReference() != imageRef { |
| 929 |
t.Errorf("ImageReference: got %q, want %q", ctr.GetImageReference(), imageRef) |
| 930 |
} |
| 931 |
if ctr.GetImageDigest() != imageDigest { |
| 932 |
t.Errorf("ImageDigest: got %q, want %q", ctr.GetImageDigest(), imageDigest) |
| 933 |
} |
| 934 |
if ctr.GetImageId() != imageID { |
| 935 |
t.Errorf("ImageId: got %q, want %q", ctr.GetImageId(), imageID) |
| 936 |
} |
| 937 |
if len(ctr.GetArgs()) == 0 || ctr.GetArgs()[0] != arg { |
| 938 |
t.Errorf("Args[0]: got %v, want %q", ctr.GetArgs(), arg) |
| 939 |
} |
| 940 |
if ctr.GetEnvVars()[envK] != envV { |
| 941 |
t.Errorf("EnvVars[%q]: got %q, want %q", envK, ctr.GetEnvVars()[envK], envV) |
| 942 |
} |
| 943 |
t.Logf("extract.VerifiedCOSState succeeded: ImageRef=%s", ctr.GetImageReference()) |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
type testClient struct { |
| 948 |
verifyCSResp *verifier.VerifyAttestationResponse |
| 949 |
verifyAttResp *verifier.VerifyAttestationResponse |
| 950 |
} |
| 951 |
|
| 952 |
func (t *testClient) CreateChallenge(_ context.Context) (*verifier.Challenge, error) { |
| 953 |
return nil, errors.New("unimplemented") |
| 954 |
} |
| 955 |
|
| 956 |
func (t *testClient) VerifyAttestation(_ context.Context, _ verifier.VerifyAttestationRequest) (*verifier.VerifyAttestationResponse, error) { |
| 957 |
return t.verifyAttResp, nil |
| 958 |
} |
| 959 |
|
| 960 |
func (t *testClient) VerifyConfidentialSpace(_ context.Context, _ verifier.VerifyAttestationRequest) (*verifier.VerifyAttestationResponse, error) { |
| 961 |
return t.verifyCSResp, nil |
| 962 |
} |
| 963 |
|
| 964 |
func TestVerify(t *testing.T) { |
| 965 |
expectedCSResp := &verifier.VerifyAttestationResponse{ |
| 966 |
ClaimsToken: []byte("verify-cs-token"), |
| 967 |
} |
| 968 |
expectedAttResp := &verifier.VerifyAttestationResponse{ |
| 969 |
ClaimsToken: []byte("verify-att-token"), |
| 970 |
} |
| 971 |
|
| 972 |
vClient := &testClient{ |
| 973 |
verifyCSResp: expectedCSResp, |
| 974 |
verifyAttResp: expectedAttResp, |
| 975 |
} |
| 976 |
|
| 977 |
testcases := []struct { |
| 978 |
name string |
| 979 |
opts AttestAgentOpts |
| 980 |
exps experiments.Experiments |
| 981 |
expectedResp *verifier.VerifyAttestationResponse |
| 982 |
}{ |
| 983 |
{ |
| 984 |
name: "VerifyCS in experiment", |
| 985 |
exps: experiments.Experiments{ |
| 986 |
EnableVerifyCS: true, |
| 987 |
}, |
| 988 |
expectedResp: expectedCSResp, |
| 989 |
}, |
| 990 |
{ |
| 991 |
name: "VerifyAtt in experiment", |
| 992 |
exps: experiments.Experiments{ |
| 993 |
EnableVerifyCS: false, |
| 994 |
}, |
| 995 |
expectedResp: expectedAttResp, |
| 996 |
}, |
| 997 |
} |
| 998 |
|
| 999 |
ctx := context.Background() |
| 1000 |
|
| 1001 |
for _, tc := range testcases { |
| 1002 |
t.Run(tc.name, func(t *testing.T) { |
| 1003 |
attAgent := agent{ |
| 1004 |
launchSpec: spec.LaunchSpec{ |
| 1005 |
Experiments: tc.exps, |
| 1006 |
}, |
| 1007 |
} |
| 1008 |
|
| 1009 |
resp, err := attAgent.verify(ctx, verifier.VerifyAttestationRequest{}, vClient) |
| 1010 |
if err != nil { |
| 1011 |
t.Fatalf("verify() returned failure: %v", err) |
| 1012 |
} |
| 1013 |
|
| 1014 |
if diff := cmp.Diff(resp, tc.expectedResp); diff != "" { |
| 1015 |
t.Errorf("verify() did not return expected response (-got, +want): %v", diff) |
| 1016 |
} |
| 1017 |
}) |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
func TestConvertPBToTPMQuote(t *testing.T) { |
| 1022 |
pbAtt := &attestpb.Attestation{ |
| 1023 |
Quotes: []*tpmpb.Quote{ |
| 1024 |
{Quote: []byte("quote")}, |
| 1025 |
}, |
| 1026 |
EventLog: []byte("pcclient-boot-event-log"), |
| 1027 |
CanonicalEventLog: []byte("cel-launch-event-log"), |
| 1028 |
} |
| 1029 |
|
| 1030 |
want := &attestationpb.TpmQuote{ |
| 1031 |
Quotes: []*attestationpb.TpmQuote_SignedQuote{ |
| 1032 |
{TpmsAttest: []byte("quote")}, |
| 1033 |
}, |
| 1034 |
PcclientBootEventLog: []byte("pcclient-boot-event-log"), |
| 1035 |
CelLaunchEventLog: []byte("cel-launch-event-log"), |
| 1036 |
Endorsement: &attestationpb.TpmAttestationEndorsement{ |
| 1037 |
Endorsement: &attestationpb.TpmAttestationEndorsement_AkCertEndorsement_{ |
| 1038 |
AkCertEndorsement: &attestationpb.TpmAttestationEndorsement_AkCertEndorsement{}, |
| 1039 |
}, |
| 1040 |
}, |
| 1041 |
} |
| 1042 |
|
| 1043 |
got := convertToTPMQuote(pbAtt) |
| 1044 |
|
| 1045 |
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { |
| 1046 |
t.Errorf("convertPBToTPMQuote() mismatch (-got +want):\n%s", diff) |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
// TestAttestationEvidence_ExperimentDisabled verifies that AttestationEvidence |
| 1051 |
// returns an error when the EnableAttestationEvidence experiment flag is off. |
| 1052 |
func TestAttestationEvidence_ExperimentDisabled(t *testing.T) { |
| 1053 |
ctx := context.Background() |
| 1054 |
tpm := test.GetTPM(t) |
| 1055 |
defer client.CheckedClose(t, tpm) |
| 1056 |
|
| 1057 |
fakeSigner, err := rsa.GenerateKey(rand.Reader, 2048) |
| 1058 |
if err != nil { |
| 1059 |
t.Fatalf("failed to generate signing key: %v", err) |
| 1060 |
} |
| 1061 |
agent, err := CreateAttestationAgent(tpm, client.AttestationKeyECC, fake.NewClient(fakeSigner), |
| 1062 |
placeholderPrincipalFetcher, signaturediscovery.NewFakeClient(), |
| 1063 |
spec.LaunchSpec{ /* EnableAttestationEvidence defaults to false */ }, |
| 1064 |
logging.SimpleLogger(), nil) |
| 1065 |
if err != nil { |
| 1066 |
t.Fatalf("failed to create agent: %v", err) |
| 1067 |
} |
| 1068 |
defer agent.Close() |
| 1069 |
|
| 1070 |
_, err = agent.AttestationEvidence(ctx, []byte("challenge"), nil) |
| 1071 |
if err == nil { |
| 1072 |
t.Error("expected error when EnableAttestationEvidence is disabled, got nil") |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
// TestAttestationEvidence_TDX_NilExtraData verifies that AttestationEvidence |
| 1077 |
// handles nil extraData correctly, deriving the nonce from the challenge alone. |
| 1078 |
func TestAttestationEvidence_TDX_NilExtraData(t *testing.T) { |
| 1079 |
ctx := context.Background() |
| 1080 |
tpm := test.GetTPM(t) |
| 1081 |
defer client.CheckedClose(t, tpm) |
| 1082 |
|
| 1083 |
ak, err := client.AttestationKeyECC(tpm) |
| 1084 |
if err != nil { |
| 1085 |
t.Fatalf("failed to create AK: %v", err) |
| 1086 |
} |
| 1087 |
defer ak.Close() |
| 1088 |
|
| 1089 |
fakeRoot := &fakeTdxAttestRoot{ |
| 1090 |
cel: gecel.NewConfComputeMR(), |
| 1091 |
tdxQuote: []byte("fake-tdx-quote"), |
| 1092 |
} |
| 1093 |
attestatAgent := &agent{ |
| 1094 |
avRot: fakeRoot, |
| 1095 |
fetchedAK: ak, |
| 1096 |
launchSpec: spec.LaunchSpec{ |
| 1097 |
Experiments: experiments.Experiments{ |
| 1098 |
EnableAttestationEvidence: true, |
| 1099 |
}, |
| 1100 |
}, |
| 1101 |
} |
| 1102 |
|
| 1103 |
challenge := []byte("test-challenge") |
| 1104 |
att, err := attestatAgent.AttestationEvidence(ctx, challenge, nil) |
| 1105 |
if err != nil { |
| 1106 |
t.Fatalf("AttestationEvidence failed with nil extraData: %v", err) |
| 1107 |
} |
| 1108 |
|
| 1109 |
expectedNonce := fakeRoot.ComputeNonce(challenge, nil) |
| 1110 |
if !bytes.Equal(fakeRoot.receivedNonce, expectedNonce) { |
| 1111 |
t.Errorf("nonce mismatch with nil extraData: got %x, want %x", |
| 1112 |
fakeRoot.receivedNonce, expectedNonce) |
| 1113 |
} |
| 1114 |
if att.ExtraData != nil { |
| 1115 |
t.Errorf("expected nil ExtraData in attestation, got %x", att.ExtraData) |
| 1116 |
} |
| 1117 |
} |