1
1
package server
2
2
3
3
import (
4
- "bytes"
5
4
"crypto"
6
5
"crypto/rand"
7
6
"crypto/rsa"
@@ -14,11 +13,8 @@ import (
14
13
"fmt"
15
14
"io"
16
15
"os"
17
- "strings"
18
16
"testing"
19
17
20
- "github.com/google/go-cmp/cmp"
21
- "github.com/google/go-tpm-tools/cel"
22
18
"github.com/google/go-tpm-tools/client"
23
19
"github.com/google/go-tpm-tools/internal"
24
20
"github.com/google/go-tpm-tools/internal/test"
@@ -27,7 +23,6 @@ import (
27
23
"github.com/google/go-tpm/tpmutil"
28
24
"github.com/google/logger"
29
25
"google.golang.org/protobuf/proto"
30
- "google.golang.org/protobuf/testing/protocmp"
31
26
)
32
27
33
28
func getDigestHash (input string ) []byte {
@@ -278,149 +273,6 @@ func TestVerifySHA1Attestation(t *testing.T) {
278
273
}
279
274
}
280
275
281
- func TestVerifyAttestationWithCEL (t * testing.T ) {
282
- test .SkipForRealTPM (t )
283
- rwc := test .GetTPM (t )
284
- defer client .CheckedClose (t , rwc )
285
-
286
- ak , err := client .AttestationKeyRSA (rwc )
287
- if err != nil {
288
- t .Fatalf ("failed to generate AK: %v" , err )
289
- }
290
- defer ak .Close ()
291
-
292
- coscel := & cel.CEL {}
293
- testEvents := []struct {
294
- cosNestedEventType cel.CosType
295
- pcr int
296
- eventPayload []byte
297
- }{
298
- {cel .ImageRefType , cel .CosEventPCR , []byte ("docker.io/bazel/experimental/test:latest" )},
299
- {cel .ImageDigestType , cel .CosEventPCR , []byte ("sha256:781d8dfdd92118436bd914442c8339e653b83f6bf3c1a7a98efcfb7c4fed7483" )},
300
- {cel .RestartPolicyType , cel .CosEventPCR , []byte (attestpb .RestartPolicy_Never .String ())},
301
- {cel .ImageIDType , cel .CosEventPCR , []byte ("sha256:5DF4A1AC347DCF8CF5E9D0ABC04B04DB847D1B88D3B1CC1006F0ACB68E5A1F4B" )},
302
- {cel .EnvVarType , cel .CosEventPCR , []byte ("foo=bar" )},
303
- {cel .EnvVarType , cel .CosEventPCR , []byte ("bar=baz" )},
304
- {cel .EnvVarType , cel .CosEventPCR , []byte ("baz=foo=bar" )},
305
- {cel .EnvVarType , cel .CosEventPCR , []byte ("empty=" )},
306
- {cel .ArgType , cel .CosEventPCR , []byte ("--x" )},
307
- {cel .ArgType , cel .CosEventPCR , []byte ("--y" )},
308
- {cel .OverrideArgType , cel .CosEventPCR , []byte ("--x" )},
309
- {cel .OverrideEnvType , cel .CosEventPCR , []byte ("empty=" )},
310
- {cel .MemoryMonitorType , cel .CosEventPCR , []byte {1 }},
311
- }
312
- for _ , testEvent := range testEvents {
313
- cosEvent := cel.CosTlv {EventType : testEvent .cosNestedEventType , EventContent : testEvent .eventPayload }
314
- if err := coscel .AppendEventPCR (rwc , testEvent .pcr , cosEvent ); err != nil {
315
- t .Fatal (err )
316
- }
317
- }
318
-
319
- var buf bytes.Buffer
320
- if err := coscel .EncodeCEL (& buf ); err != nil {
321
- t .Fatal (err )
322
- }
323
-
324
- nonce := []byte ("super secret nonce" )
325
- attestation , err := ak .Attest (client.AttestOpts {Nonce : nonce , CanonicalEventLog : buf .Bytes ()})
326
- if err != nil {
327
- t .Fatalf ("failed to attest: %v" , err )
328
- }
329
-
330
- opts := VerifyOpts {
331
- Nonce : nonce ,
332
- TrustedAKs : []crypto.PublicKey {ak .PublicKey ()},
333
- }
334
- state , err := VerifyAttestation (attestation , opts )
335
- if err != nil {
336
- t .Fatalf ("failed to verify: %v" , err )
337
- }
338
-
339
- expectedEnvVars := make (map [string ]string )
340
- expectedEnvVars ["foo" ] = "bar"
341
- expectedEnvVars ["bar" ] = "baz"
342
- expectedEnvVars ["baz" ] = "foo=bar"
343
- expectedEnvVars ["empty" ] = ""
344
-
345
- expectedOverriddenEnvVars := make (map [string ]string )
346
- expectedOverriddenEnvVars ["empty" ] = ""
347
-
348
- wantContainerState := attestpb.ContainerState {
349
- ImageReference : string (testEvents [0 ].eventPayload ),
350
- ImageDigest : string (testEvents [1 ].eventPayload ),
351
- RestartPolicy : attestpb .RestartPolicy_Never ,
352
- ImageId : string (testEvents [3 ].eventPayload ),
353
- EnvVars : expectedEnvVars ,
354
- Args : []string {string (testEvents [8 ].eventPayload ), string (testEvents [9 ].eventPayload )},
355
- OverriddenEnvVars : expectedOverriddenEnvVars ,
356
- OverriddenArgs : []string {string (testEvents [10 ].eventPayload )},
357
- }
358
- enabled := true
359
- wantHealthMonitoringState := attestpb.HealthMonitoringState {
360
- MemoryEnabled : & enabled ,
361
- }
362
- if diff := cmp .Diff (state .Cos .Container , & wantContainerState , protocmp .Transform ()); diff != "" {
363
- t .Errorf ("unexpected container state difference:\n %v" , diff )
364
- }
365
- if diff := cmp .Diff (state .Cos .HealthMonitoring , & wantHealthMonitoringState , protocmp .Transform ()); diff != "" {
366
- t .Errorf ("unexpected health monitoring state difference:\n %v" , diff )
367
- }
368
- }
369
-
370
- func TestVerifyFailWithTamperedCELContent (t * testing.T ) {
371
- test .SkipForRealTPM (t )
372
- rwc := test .GetTPM (t )
373
- defer client .CheckedClose (t , rwc )
374
-
375
- ak , err := client .AttestationKeyRSA (rwc )
376
- if err != nil {
377
- t .Fatalf ("failed to generate AK: %v" , err )
378
- }
379
- defer ak .Close ()
380
-
381
- c := & cel.CEL {}
382
-
383
- cosEvent := cel.CosTlv {EventType : cel .ImageRefType , EventContent : []byte ("docker.io/bazel/experimental/test:latest" )}
384
- cosEvent2 := cel.CosTlv {EventType : cel .ImageDigestType , EventContent : []byte ("sha256:781d8dfdd92118436bd914442c8339e653b83f6bf3c1a7a98efcfb7c4fed7483" )}
385
-
386
- if err := c .AppendEventPCR (rwc , cel .CosEventPCR , cosEvent ); err != nil {
387
- t .Fatalf ("failed to append event: %v" , err )
388
- }
389
-
390
- if err := c .AppendEventPCR (rwc , cel .CosEventPCR , cosEvent2 ); err != nil {
391
- t .Fatalf ("failed to append event: %v" , err )
392
- }
393
-
394
- // modify the first record content, but not the record digest
395
- modifiedRecord := cel.CosTlv {EventType : cel .ImageDigestType , EventContent : []byte ("sha256:000000000000000000000000000000000000000000000000000000000000000" )}
396
- modifiedTLV , err := modifiedRecord .GetTLV ()
397
- if err != nil {
398
- t .Fatal (err )
399
- }
400
- c .Records [0 ].Content = modifiedTLV
401
-
402
- var buf bytes.Buffer
403
- if err := c .EncodeCEL (& buf ); err != nil {
404
- t .Fatal (err )
405
- }
406
-
407
- nonce := []byte ("super secret nonce" )
408
- attestation , err := ak .Attest (client.AttestOpts {Nonce : nonce , CanonicalEventLog : buf .Bytes ()})
409
- if err != nil {
410
- t .Fatalf ("failed to attest: %v" , err )
411
- }
412
-
413
- opts := VerifyOpts {
414
- Nonce : nonce ,
415
- TrustedAKs : []crypto.PublicKey {ak .PublicKey ()},
416
- }
417
- if _ , err := VerifyAttestation (attestation , opts ); err == nil {
418
- t .Fatalf ("VerifyAttestation should fail due to modified content" )
419
- } else if ! strings .Contains (err .Error (), "CEL record content digest verification failed" ) {
420
- t .Fatalf ("expect to get digest verification failed error, but got %v" , err )
421
- }
422
- }
423
-
424
276
func TestVerifyAttestationWithCerts (t * testing.T ) {
425
277
tests := []struct {
426
278
name string
0 commit comments