Debian Codegraph @

launcher/agent/agent.go

raw
1 // Package agent coordinates the communication between the TPM and the remote
2 // attestation service. It handles:
3 // - All TPM-related functionality (quotes, logs, certs, etc...)
4 // - Fetching the relevant principal ID tokens
5 // - Calling VerifyAttestation on the remote service
6 package agent
7
8 import (
9 "bytes"
10 "context"
11 "crypto"
12 "crypto/sha256"
13 "crypto/sha512"
14 "encoding/base64"
15 "fmt"
16 "io"
17 "net/http"
18 "os"
19 "sync"
20 "time"
21
22 "github.com/cenkalti/backoff/v4"
23 "github.com/google/go-tpm/legacy/tpm2"
24 "github.com/google/go-tpm/tpmutil"
25
26 tg "github.com/google/go-tdx-guest/client"
27 tlabi "github.com/google/go-tdx-guest/client/linuxabi"
28 "github.com/google/go-tdx-guest/rtmr"
29
30 gecel "github.com/google/go-eventlog/cel"
31
32 "github.com/GoogleCloudPlatform/confidential-space/server/labels"
33 attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation"
34 "github.com/google/go-tpm-tools/cel"
35 "github.com/google/go-tpm-tools/client"
36 "github.com/google/go-tpm-tools/internal"
37 "github.com/google/go-tpm-tools/launcher/internal/logging"
38 "github.com/google/go-tpm-tools/launcher/internal/signaturediscovery"
39 "github.com/google/go-tpm-tools/launcher/spec"
40 pb "github.com/google/go-tpm-tools/proto/attest"
41 "github.com/google/go-tpm-tools/verifier"
42 "github.com/google/go-tpm-tools/verifier/models"
43 "github.com/google/go-tpm-tools/verifier/oci"
44 "github.com/google/go-tpm-tools/verifier/util"
45 )
46
47 const (
48 audienceSTS = "https://sts.googleapis.com"
49 )
50
51 type principalIDTokenFetcher func(audience string) ([][]byte, error)
52
53 // AttestationAgent is an agent that interacts with GCE's Attestation Service
54 // to Verify an attestation message. It is an interface instead of a concrete
55 // struct to make testing easier.
56 type AttestationAgent interface {
57 MeasureEvent(gecel.Content) error
58 Attest(context.Context, AttestAgentOpts) ([]byte, error)
59 AttestWithClient(ctx context.Context, opts AttestAgentOpts, client verifier.Client) ([]byte, error)
60 AttestationEvidence(ctx context.Context, challenge []byte, extraData []byte) (*attestationpb.VmAttestation, error)
61 Refresh(context.Context) error
62 Close() error
63 }
64
65 type attestRoot interface {
66 // Extend measures the cel content into a measurement register and appends to the CEL.
67 Extend(gecel.Content) error
68 // GetCEL fetches the CEL with events corresponding to the sequence of Extended measurements
69 // to this attestation root
70 GetCEL() gecel.CEL
71 // Attest fetches a technology-specific quote from the root of trust.
72 Attest(nonce []byte) (any, error)
73 // ComputeNonce hashes the challenge and extraData using the algorithm preferred by the attestation root.
74 ComputeNonce(challenge []byte, extraData []byte) []byte
75 // AddDeviceROTs adds detected device RoTs(root of trust).
76 AddDeviceROTs([]DeviceROT)
77 }
78
79 // DeviceROT defines an interface for all attached devices to collect attestation.
80 type DeviceROT interface {
81 // Attest fetches an attestation from the attached device detected by launcher.
82 Attest(nonce []byte) (any, error)
83 }
84
85 // AttestAgentOpts contains user generated options when calling the
86 // VerifyAttestation API
87 type AttestAgentOpts struct {
88 TokenOptions *models.TokenOptions
89 }
90
91 type agent struct {
92 measuredRots []attestRoot
93 avRot attestRoot
94 fetchedAK *client.Key
95 client verifier.Client
96 principalFetcher principalIDTokenFetcher
97 sigsFetcher signaturediscovery.Fetcher
98 launchSpec spec.LaunchSpec
99 logger logging.Logger
100 sigsCache *sigsCache
101 }
102
103 // CreateAttestationAgent returns an agent capable of performing remote
104 // attestation using the machine's (v)TPM to GCE's Attestation Service.
105 // - tpm is a handle to the TPM on the instance
106 // - akFetcher is a func to fetch an attestation key: see go-tpm-tools/client.
107 // - principalFetcher is a func to fetch GCE principal tokens for a given audience.
108 // - signaturesFetcher is a func to fetch container image signatures associated with the running workload.
109 // - logger will log any partial errors returned by VerifyAttestation.
110 func CreateAttestationAgent(tpm io.ReadWriteCloser, akFetcher util.TpmKeyFetcher, verifierClient verifier.Client, principalFetcher principalIDTokenFetcher, sigsFetcher signaturediscovery.Fetcher, launchSpec spec.LaunchSpec, logger logging.Logger, deviceROTs []DeviceROT) (AttestationAgent, error) {
111 // Fetched the AK and save it, so the agent doesn't need to create a new key everytime
112 ak, err := akFetcher(tpm)
113 if err != nil {
114 return nil, fmt.Errorf("failed to create an Attestation Agent: %w", err)
115 }
116
117 attestAgent := &agent{
118 client: verifierClient,
119 fetchedAK: ak,
120 principalFetcher: principalFetcher,
121 sigsFetcher: sigsFetcher,
122 launchSpec: launchSpec,
123 logger: logger,
124 sigsCache: &sigsCache{},
125 }
126
127 // Add TPM
128 logger.Info("Adding TPM PCRs for measurement.")
129
130 pcrSels, err := client.AllocatedPCRs(tpm)
131 if err != nil {
132 return nil, fmt.Errorf("failed to get PCR selections: %v", err)
133 }
134
135 var hashAlgos []crypto.Hash
136 for _, sel := range pcrSels {
137 hashAlgo, err := sel.Hash.Hash()
138 if err != nil {
139 return nil, fmt.Errorf("failed to get TPM hash algorithm: %v", err)
140 }
141 hashAlgos = append(hashAlgos, hashAlgo)
142 }
143
144 var tpmAR = &tpmAttestRoot{
145 fetchedAK: ak,
146 tpm: tpm,
147 hashAlgos: hashAlgos,
148 cosCel: gecel.NewPCR(),
149 }
150 attestAgent.measuredRots = append(attestAgent.measuredRots, tpmAR)
151
152 // check if is a TDX machine
153 qp, err := tg.GetQuoteProvider()
154 if err != nil {
155 return nil, err
156 }
157 // Use qp.IsSupported to check the TDX RTMR interface is enabled
158 if qp.IsSupported() == nil {
159 logger.Info("Adding TDX RTMRs for measurement.")
160 var tdxAR = &tdxAttestRoot{
161 qp: qp,
162 cosCel: gecel.NewConfComputeMR(),
163 }
164 attestAgent.measuredRots = append(attestAgent.measuredRots, tdxAR)
165
166 logger.Info("Using TDX RTMR as attestation root.")
167 attestAgent.avRot = tdxAR
168 } else {
169 logger.Info("Using TPM PCR as attestation root.")
170 attestAgent.avRot = tpmAR
171 }
172
173 // Add deviceRoTs to the CPU attestation root.
174 attestAgent.avRot.AddDeviceROTs(deviceROTs)
175 return attestAgent, nil
176 }
177
178 // Close cleans up the agent
179 func (a *agent) Close() error {
180 a.fetchedAK.Close()
181 return nil
182 }
183
184 // MeasureEvent takes in a cel.Content and appends it to the CEL eventlog
185 // under the attestation agent.
186 // MeasureEvent measures to all Attest Roots.
187 func (a *agent) MeasureEvent(event gecel.Content) error {
188 for _, attestRoot := range a.measuredRots {
189 if err := attestRoot.Extend(event); err != nil {
190 return err
191 }
192 }
193 return nil
194 }
195
196 // Attest fetches the nonce and connection ID from the Attestation Service,
197 // creates an attestation message, and returns the resultant
198 // principalIDTokens and Metadata Server-generated ID tokens for the instance.
199 // When possible, Attest uses the technology-specific attestation root-of-trust
200 // (TDX RTMR), otherwise falls back to the vTPM.
201 func (a *agent) Attest(ctx context.Context, opts AttestAgentOpts) ([]byte, error) {
202 if a.client == nil {
203 return nil, fmt.Errorf("attest agent does not have initialized verifier client")
204 }
205
206 return a.AttestWithClient(ctx, opts, a.client)
207 }
208
209 // AttestWithClient fetches the nonce and connection ID from the Attestation Service via the provided client,
210 // creates an attestation message, and returns the resultant
211 // principalIDTokens and Metadata Server-generated ID tokens for the instance.
212 // When possible, Attest uses the technology-specific attestation root-of-trust
213 // (TDX RTMR), otherwise falls back to the vTPM.
214 func (a *agent) AttestWithClient(ctx context.Context, opts AttestAgentOpts, client verifier.Client) ([]byte, error) {
215 challenge, err := client.CreateChallenge(ctx)
216 if err != nil {
217 return nil, err
218 }
219
220 tokenOpts := opts.TokenOptions
221 if tokenOpts == nil {
222 tokenOpts = &models.TokenOptions{TokenType: "OIDC"}
223 }
224
225 // The customer is responsible for providing an audience if they provided nonces.
226 if tokenOpts.Audience == "" && len(tokenOpts.Nonces) == 0 {
227 tokenOpts.Audience = audienceSTS
228 }
229
230 principalTokens, err := a.principalFetcher(challenge.Name)
231 if err != nil {
232 return nil, fmt.Errorf("failed to get principal tokens: %w", err)
233 }
234
235 // attResult can be tdx or tpm or other attest root
236 attResult, err := a.avRot.Attest(challenge.Nonce)
237 if err != nil {
238 return nil, fmt.Errorf("failed to attest: %v", err)
239 }
240
241 var cosCel bytes.Buffer
242 if err := a.avRot.GetCEL().EncodeCEL(&cosCel); err != nil {
243 return nil, err
244 }
245
246 req := verifier.VerifyAttestationRequest{
247 Challenge: challenge,
248 GcpCredentials: principalTokens,
249 TokenOptions: tokenOpts,
250 }
251
252 switch v := attResult.(type) {
253 case *pb.Attestation:
254 a.logger.Info("attestation through TPM quote")
255
256 v.CanonicalEventLog = cosCel.Bytes()
257 req.Attestation = v
258 case *verifier.TDCCELAttestation:
259 a.logger.Info("attestation through TDX quote")
260
261 certChain, err := internal.GetCertificateChain(a.fetchedAK.Cert(), http.DefaultClient)
262 if err != nil {
263 return nil, fmt.Errorf("failed when fetching certificate chain: %w", err)
264 }
265
266 v.CanonicalEventLog = cosCel.Bytes()
267 v.IntermediateCerts = certChain
268 v.AkCert = a.fetchedAK.CertDERBytes()
269
270 req.TDCCELAttestation = v
271 default:
272 return nil, fmt.Errorf("received an unsupported attestation type! %v", v)
273 }
274
275 signatures := a.sigsCache.get()
276 if len(signatures) > 0 {
277 for _, sig := range signatures {
278 verifierSig, err := convertOCIToContainerSignature(sig)
279 if err != nil {
280 a.logger.Error(fmt.Sprintf("error converting container signatures: %v", err))
281 continue
282 }
283 req.ContainerImageSignatures = append(req.ContainerImageSignatures, verifierSig)
284 }
285 a.logger.Info("Found container image signatures: %v\n", signatures)
286 }
287
288 resp, err := a.verify(ctx, req, client)
289 if err != nil {
290 return nil, err
291 }
292
293 if len(resp.PartialErrs) > 0 {
294 a.logger.Error(fmt.Sprintf("Partial errors from VerifyAttestation: %v", resp.PartialErrs))
295 }
296 return resp.ClaimsToken, nil
297 }
298
299 // AttestationEvidence returns the attestation evidence (TPM or TDX).
300 func (a *agent) AttestationEvidence(_ context.Context, challenge []byte, extraData []byte) (*attestationpb.VmAttestation, error) {
301 if !a.launchSpec.Experiments.EnableAttestationEvidence {
302 return nil, fmt.Errorf("attestation evidence is disabled")
303 }
304
305 if a.avRot == nil {
306 return nil, fmt.Errorf("attestation agent does not have an initialized attestation root")
307 }
308
309 // Use nested hashing to separate the prefix, the challenge, and extraData
310 // and normalize input length.
311 finalNonce := a.avRot.ComputeNonce(challenge, extraData)
312 attResult, err := a.avRot.Attest(finalNonce)
313 if err != nil {
314 return nil, fmt.Errorf("failed to attest: %v", err)
315 }
316
317 var cosCel bytes.Buffer
318 if err := a.avRot.GetCEL().EncodeCEL(&cosCel); err != nil {
319 return nil, err
320 }
321
322 attestation := &attestationpb.VmAttestation{
323 Label: []byte(labels.WorkloadAttestation),
324 Challenge: challenge,
325 ExtraData: extraData,
326 Quote: &attestationpb.VmAttestationQuote{},
327 }
328
329 switch v := attResult.(type) {
330 case *pb.Attestation:
331 v.CanonicalEventLog = cosCel.Bytes()
332 attestation.Quote.Quote = &attestationpb.VmAttestationQuote_TpmQuote{
333 TpmQuote: convertToTPMQuote(v),
334 }
335 case *verifier.TDCCELAttestation:
336 attestation.Quote.Quote = &attestationpb.VmAttestationQuote_TdxCcelQuote{
337 TdxCcelQuote: &attestationpb.TdxCcelQuote{
338 CcelBootEventLog: v.CcelData,
339 CelLaunchEventLog: cosCel.Bytes(),
340 TdQuote: v.TdQuote,
341 },
342 }
343 default:
344 return nil, fmt.Errorf("unknown attestation type: %T", v)
345 }
346 return attestation, nil
347 }
348
349 func (a *agent) verify(ctx context.Context, req verifier.VerifyAttestationRequest, client verifier.Client) (*verifier.VerifyAttestationResponse, error) {
350 if a.launchSpec.Experiments.EnableVerifyCS {
351 return client.VerifyConfidentialSpace(ctx, req)
352 }
353 return client.VerifyAttestation(ctx, req)
354 }
355
356 func convertOCIToContainerSignature(ociSig oci.Signature) (*verifier.ContainerSignature, error) {
357 payload, err := ociSig.Payload()
358 if err != nil {
359 return nil, fmt.Errorf("failed to get payload from signature [%v]: %v", ociSig, err)
360 }
361 b64Sig, err := ociSig.Base64Encoded()
362 if err != nil {
363 return nil, fmt.Errorf("failed to get base64 signature from signature [%v]: %v", ociSig, err)
364 }
365 sigBytes, err := base64.StdEncoding.DecodeString(b64Sig)
366 if err != nil {
367 return nil, fmt.Errorf("failed to decode signature for signature [%v]: %v", ociSig, err)
368 }
369 return &verifier.ContainerSignature{
370 Payload: payload,
371 Signature: sigBytes,
372 }, nil
373 }
374
375 type tpmAttestRoot struct {
376 tpmMu sync.Mutex
377 fetchedAK *client.Key
378 tpm io.ReadWriteCloser
379 cosCel gecel.CEL
380 hashAlgos []crypto.Hash
381 deviceROTs []DeviceROT
382 }
383
384 func (t *tpmAttestRoot) GetCEL() gecel.CEL {
385 return t.cosCel
386 }
387
388 func (t *tpmAttestRoot) Extend(c gecel.Content) error {
389 return t.cosCel.AppendEvent(c, t.hashAlgos, cel.CosEventPCR, func(hs crypto.Hash, pcr int, digest []byte) error {
390 tpm2Alg, err := tpm2.HashToAlgorithm(hs)
391 if err != nil {
392 return err
393 }
394 if err := tpm2.PCRExtend(t.tpm, tpmutil.Handle(pcr), tpm2Alg, digest, ""); err != nil {
395 return fmt.Errorf("failed to extend event to PCR%d: %v", pcr, err)
396 }
397 return nil
398 })
399 }
400
401 func (t *tpmAttestRoot) Attest(nonce []byte) (any, error) {
402 t.tpmMu.Lock()
403 defer t.tpmMu.Unlock()
404
405 return t.fetchedAK.Attest(client.AttestOpts{
406 Nonce: nonce,
407 CertChainFetcher: http.DefaultClient,
408 })
409 }
410
411 func (t *tpmAttestRoot) ComputeNonce(challenge []byte, extraData []byte) []byte {
412 challengeData := challenge
413 if extraData != nil {
414 extraDataDigest := sha256.Sum256(extraData)
415 challengeData = append(challenge, extraDataDigest[:]...)
416 }
417 challengeDigest := sha256.Sum256(challengeData)
418 finalNonce := sha256.Sum256(append([]byte(labels.WorkloadAttestation), challengeDigest[:]...))
419 return finalNonce[:]
420 }
421
422 func (t *tpmAttestRoot) AddDeviceROTs(deviceROTs []DeviceROT) {
423 t.deviceROTs = append(t.deviceROTs, deviceROTs...)
424 }
425
426 type tdxAttestRoot struct {
427 tdxMu sync.Mutex
428 qp *tg.LinuxConfigFsQuoteProvider
429 cosCel gecel.CEL
430 deviceROTs []DeviceROT
431 }
432
433 func (t *tdxAttestRoot) GetCEL() gecel.CEL {
434 return t.cosCel
435 }
436
437 func (t *tdxAttestRoot) Extend(c gecel.Content) error {
438 return t.cosCel.AppendEvent(c, []crypto.Hash{crypto.SHA384}, cel.CosCCELMRIndex, func(_ crypto.Hash, mrIndex int, digest []byte) error {
439 return rtmr.ExtendDigestSysfs(mrIndex-1, digest) // MR_INDEX - 1 == RTMR_INDEX
440 })
441 }
442
443 func (t *tdxAttestRoot) Attest(nonce []byte) (any, error) {
444 t.tdxMu.Lock()
445 defer t.tdxMu.Unlock()
446
447 var tdxNonce [tlabi.TdReportDataSize]byte
448 copy(tdxNonce[:], nonce)
449
450 rawQuote, err := tg.GetRawQuote(t.qp, tdxNonce)
451 if err != nil {
452 return nil, err
453 }
454
455 ccelData, err := os.ReadFile("/sys/firmware/acpi/tables/data/CCEL")
456 if err != nil {
457 return nil, err
458 }
459 ccelTable, err := os.ReadFile("/sys/firmware/acpi/tables/CCEL")
460 if err != nil {
461 return nil, err
462 }
463
464 var nvAtt *attestationpb.NvidiaAttestationReport
465 for _, deviceRoT := range t.deviceROTs {
466 att, err := deviceRoT.Attest(nonce)
467 if err != nil {
468 return nil, err
469 }
470 switch v := att.(type) {
471 case *attestationpb.NvidiaAttestationReport:
472 nvAtt = v
473 default:
474 return nil, fmt.Errorf("unknown device attestation type: %T", v)
475 }
476 }
477
478 return &verifier.TDCCELAttestation{
479 CcelAcpiTable: ccelTable,
480 CcelData: ccelData,
481 TdQuote: rawQuote,
482 NvidiaAttestation: nvAtt,
483 }, nil
484 }
485
486 func (t *tdxAttestRoot) ComputeNonce(challenge []byte, extraData []byte) []byte {
487 challengeData := challenge
488 if extraData != nil {
489 extraDataDigest := sha512.Sum512(extraData)
490 challengeData = append(challenge, extraDataDigest[:]...)
491 }
492 challengeDigest := sha512.Sum512(challengeData)
493 finalNonce := sha512.Sum512(append([]byte(labels.WorkloadAttestation), challengeDigest[:]...))
494 return finalNonce[:]
495 }
496
497 func (t *tdxAttestRoot) AddDeviceROTs(deviceROTs []DeviceROT) {
498 t.deviceROTs = append(t.deviceROTs, deviceROTs...)
499 }
500
501 // Refresh refreshes the internal state of the attestation agent.
502 // It will reset the container image signatures for now.
503 func (a *agent) Refresh(ctx context.Context) error {
504 signatures := fetchContainerImageSignatures(ctx, a.sigsFetcher, a.launchSpec.SignedImageRepos, defaultRetryPolicy, a.logger)
505 a.sigsCache.set(signatures)
506 a.logger.Info("Refreshed container image signature cache", "signatures", signatures)
507 return nil
508 }
509
510 func fetchContainerImageSignatures(ctx context.Context, fetcher signaturediscovery.Fetcher, targetRepos []string, retry func() backoff.BackOff, logger logging.Logger) []oci.Signature {
511 signatures := make([][]oci.Signature, len(targetRepos))
512
513 var wg sync.WaitGroup
514 for i, repo := range targetRepos {
515 wg.Add(1)
516 go func(targetRepo string, index int) {
517 defer wg.Done()
518
519 // backoff independently per repo
520 var sigs []oci.Signature
521 err := backoff.RetryNotify(
522 func() error {
523 s, err := fetcher.FetchImageSignatures(ctx, targetRepo)
524 sigs = s
525 return err
526 },
527 retry(),
528 func(err error, _ time.Duration) {
529 logger.Error(fmt.Sprintf("Failed to fetch container image signatures from repo: %v", err.Error()), "repo", targetRepo)
530 })
531 if err != nil {
532 logger.Error(fmt.Sprintf("Failed all attempts to refresh container signatures from repo: %v", err.Error()), "repo", targetRepo)
533 } else {
534 signatures[index] = sigs
535 }
536
537 }(repo, i)
538 }
539 wg.Wait()
540
541 var foundSigs []oci.Signature
542 for _, sigs := range signatures {
543 foundSigs = append(foundSigs, sigs...)
544 }
545 return foundSigs
546 }
547
548 func defaultRetryPolicy() backoff.BackOff {
549 b := backoff.NewConstantBackOff(time.Millisecond * 300)
550 return backoff.WithMaxRetries(b, 3)
551 }
552
553 type sigsCache struct {
554 mu sync.RWMutex
555 items []oci.Signature
556 }
557
558 func (c *sigsCache) set(sigs []oci.Signature) {
559 c.mu.Lock()
560 defer c.mu.Unlock()
561 c.items = make([]oci.Signature, len(sigs))
562 copy(c.items, sigs)
563 }
564
565 func (c *sigsCache) get() []oci.Signature {
566 c.mu.RLock()
567 defer c.mu.RUnlock()
568 return c.items
569 }
570
571 func convertToTPMQuote(v *pb.Attestation) *attestationpb.TpmQuote {
572 var quotes []*attestationpb.TpmQuote_SignedQuote
573 for _, q := range v.GetQuotes() {
574 quote := &attestationpb.TpmQuote_SignedQuote{
575 TpmsAttest: q.GetQuote(),
576 TpmtSignature: q.GetRawSig(),
577 }
578 if pcrs := q.GetPcrs(); pcrs != nil {
579 quote.HashAlgorithm = uint32(pcrs.GetHash())
580 quote.PcrValues = pcrs.GetPcrs()
581 }
582 quotes = append(quotes, quote)
583 }
584
585 return &attestationpb.TpmQuote{
586 Quotes: quotes,
587 PcclientBootEventLog: v.GetEventLog(),
588 CelLaunchEventLog: v.GetCanonicalEventLog(),
589 Endorsement: &attestationpb.TpmAttestationEndorsement{
590 Endorsement: &attestationpb.TpmAttestationEndorsement_AkCertEndorsement_{
591 AkCertEndorsement: &attestationpb.TpmAttestationEndorsement_AkCertEndorsement{
592 AkCert: v.GetAkCert(),
593 AkCertChain: v.GetIntermediateCerts(),
594 },
595 },
596 },
597 }
598 }