Skip to content

Commit 3fef05b

Browse files
committed
Add TDX CCEL support to token command and refactor flags
* Move `teeTechnology` flag to flags.go to share it between `attest` and `token` commands. * Update `token` command to support TDX CCEL attestation. * Enable `tee-nonce` and `tee-technology` flags for the `token` command.
1 parent f07d4b5 commit 3fef05b

File tree

5 files changed

+76
-31
lines changed

5 files changed

+76
-31
lines changed

cmd/attest.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@ import (
1616
)
1717

1818
var (
19-
key string
20-
teeTechnology string
21-
)
22-
23-
// Add constants for other devices when required
24-
const (
25-
// SevSnp is a constant denotes device name for teeTechnology
26-
SevSnp = "sev-snp"
27-
// Tdx is a constant denotes device name for teeTechnology
28-
Tdx = "tdx"
19+
key string
2920
)
3021

3122
var attestationKeys = map[string]map[tpm2.Algorithm]func(rw io.ReadWriter) (*client.Key, error){
@@ -183,10 +174,6 @@ func addKeyFlag(cmd *cobra.Command) {
183174
cmd.PersistentFlags().StringVar(&key, "key", "AK", "indicates type of attestation key to use <gceAK|AK>")
184175
}
185176

186-
func addTeeTechnology(cmd *cobra.Command) {
187-
cmd.PersistentFlags().StringVar(&teeTechnology, "tee-technology", "", "indicates the type of TEE hardware. Should be either empty or one of sev-snp or tdx")
188-
}
189-
190177
func init() {
191178
RootCmd.AddCommand(attestCmd)
192179
addKeyFlag(attestCmd)

cmd/flags.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@ import (
1515
)
1616

1717
var (
18-
output string
19-
input string
20-
nvIndex uint32
21-
nonce []byte
22-
teeNonce []byte
23-
keyAlgo = tpm2.AlgRSA
24-
pcrs []int
25-
format string
26-
asAddress string
27-
audience string
28-
eventLog string
29-
cloudLog bool
30-
customNonce []string
18+
output string
19+
input string
20+
nvIndex uint32
21+
nonce []byte
22+
teeNonce []byte
23+
teeTechnology string
24+
keyAlgo = tpm2.AlgRSA
25+
pcrs []int
26+
format string
27+
asAddress string
28+
audience string
29+
eventLog string
30+
cloudLog bool
31+
customNonce []string
32+
)
33+
34+
// Add constants for other devices when required
35+
const (
36+
// SevSnp is a constant denotes device name for teeTechnology
37+
SevSnp = "sev-snp"
38+
// Tdx is a constant denotes device name for teeTechnology
39+
Tdx = "tdx"
3140
)
3241

3342
type pcrsFlag struct {
@@ -192,6 +201,10 @@ func addTeeNonceflag(cmd *cobra.Command) {
192201
cmd.PersistentFlags().BytesHexVar(&teeNonce, "tee-nonce", []byte{}, "hex encoded teenonce for hardware attestation, can be empty")
193202
}
194203

204+
func addTeeTechnology(cmd *cobra.Command) {
205+
cmd.PersistentFlags().StringVar(&teeTechnology, "tee-technology", "", "indicates the type of TEE hardware. Should be either empty or one of sev-snp or tdx")
206+
}
207+
195208
// alwaysError implements io.ReadWriter by always returning an error
196209
type alwaysError struct {
197210
error

cmd/token.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package cmd
22

33
import (
44
"context"
5+
_ "crypto/sha512" // Ensure SHA384 is available
56
"encoding/json"
67
"errors"
78
"fmt"
89
"log"
910
"net/http"
11+
"os"
1012
"time"
1113

1214
"cloud.google.com/go/compute/metadata"
1315
"cloud.google.com/go/logging"
1416
"github.com/golang-jwt/jwt/v4"
17+
tabi "github.com/google/go-tdx-guest/abi"
1518
"github.com/google/go-tpm-tools/client"
19+
"github.com/google/go-tpm-tools/internal"
1620
"github.com/google/go-tpm-tools/verifier"
1721
"github.com/google/go-tpm-tools/verifier/models"
1822
"github.com/google/go-tpm-tools/verifier/util"
@@ -134,13 +138,46 @@ The OIDC token includes claims regarding the GCE VM, which is verified by Attest
134138
}
135139
ak.Close()
136140

141+
// If teeTechnology is not set, try to detect it from the attestation.
142+
if teeTechnology == "" {
143+
if attestation.GetTdxAttestation() != nil {
144+
teeTechnology = Tdx
145+
}
146+
}
147+
137148
req := verifier.VerifyAttestationRequest{
138149
Challenge: challenge,
139150
GcpCredentials: principalTokens,
140151
Attestation: attestation,
141152
TokenOptions: &models.TokenOptions{Audience: audience, Nonces: customNonce, TokenType: "OIDC"},
142153
}
143154

155+
if teeTechnology == Tdx {
156+
// If TDX, check if we should populate TDCCELAttestation
157+
if attestation.GetTdxAttestation() != nil {
158+
fmt.Fprintln(debugOutput(), "Using Explicit TDCCELAttestation Path (ACPI tables)")
159+
160+
rawQuote, err := tabi.QuoteToAbiBytes(attestation.GetTdxAttestation())
161+
if err != nil {
162+
return fmt.Errorf("failed to convert TDX quote to bytes: %v", err)
163+
}
164+
165+
// Try to read CCEL Table and Data
166+
ccelTable, _ := os.ReadFile(internal.AcpiTableFile)
167+
ccelData, _ := os.ReadFile(internal.CcelEventLogFile)
168+
169+
req.TDCCELAttestation = &verifier.TDCCELAttestation{
170+
TdQuote: rawQuote,
171+
CcelAcpiTable: ccelTable,
172+
CcelData: ccelData,
173+
AkCert: attestation.AkCert,
174+
IntermediateCerts: attestation.IntermediateCerts,
175+
}
176+
// Force using TDCCELAttestation path in verifier client
177+
req.Attestation = nil
178+
}
179+
}
180+
144181
resp, err := verifierClient.VerifyAttestation(ctx, req)
145182
if err != nil {
146183
return err
@@ -210,6 +247,6 @@ func init() {
210247
addEventLogFlag(tokenCmd)
211248
addCustomNonceFlag(tokenCmd)
212249
// TODO: Add TEE hardware OIDC token generation
213-
// addTeeNonceflag(tokenCmd)
214-
// addTeeTechnology(tokenCmd)
250+
addTeeNonceflag(tokenCmd)
251+
addTeeTechnology(tokenCmd)
215252
}

internal/ccel.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package internal
2+
3+
const (
4+
// AcpiTableFile is the path to the CCEL ACPI table.
5+
AcpiTableFile = "/sys/firmware/acpi/tables/CCEL"
6+
// CcelEventLogFile is the path to the CCEL event log data.
7+
CcelEventLogFile = "/sys/firmware/acpi/tables/data/CCEL"
8+
)

launcher/agent/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ func (t *tdxAttestRoot) Attest(nonce []byte) (any, error) {
332332
return nil, err
333333
}
334334

335-
ccelData, err := os.ReadFile("/sys/firmware/acpi/tables/data/CCEL")
335+
ccelData, err := os.ReadFile(internal.CcelEventLogFile)
336336
if err != nil {
337337
return nil, err
338338
}
339-
ccelTable, err := os.ReadFile("/sys/firmware/acpi/tables/CCEL")
339+
ccelTable, err := os.ReadFile(internal.AcpiTableFile)
340340
if err != nil {
341341
return nil, err
342342
}

0 commit comments

Comments
 (0)