Debian Codegraph @

cmd/attest.go

raw
1 package cmd
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "log"
8 "strconv"
9
10 "cloud.google.com/go/compute/metadata"
11 "github.com/google/go-tpm-tools/client"
12 "github.com/google/go-tpm-tools/proto/attest"
13 "github.com/google/go-tpm/legacy/tpm2"
14 "github.com/spf13/cobra"
15 "google.golang.org/protobuf/proto"
16 )
17
18 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"
29 )
30
31 var attestationKeys = map[string]map[tpm2.Algorithm]func(rw io.ReadWriter) (*client.Key, error){
32 "AK": {
33 tpm2.AlgRSA: client.AttestationKeyRSA,
34 tpm2.AlgECC: client.AttestationKeyECC,
35 },
36 "gceAK": {
37 tpm2.AlgRSA: client.GceAttestationKeyRSA,
38 tpm2.AlgECC: client.GceAttestationKeyECC,
39 },
40 }
41
42 // If hardware technology needs a variable length teenonce then please modify the flags description
43 var attestCmd = &cobra.Command{
44 Use: "attest",
45 Short: "Create a remote attestation report",
46 Long: `Gather information for remote attestation.
47 The Attestation report contains a quote on all available PCR banks, a way to validate
48 the quote, and a TCG Event Log (Linux only).
49 Use --key to specify the type of attestation key. It can be gceAK for GCE attestation
50 key or AK for a custom attestation key. By default it uses AK.
51 --algo flag overrides the public key algorithm for attestation key. If not provided then
52 by default rsa is used.
53 --tee-nonce attaches a 64 bytes extra data to the attestation report of TDX and SEV-SNP
54 hardware and guarantees a fresh quote.
55 `,
56 Args: cobra.NoArgs,
57 RunE: func(*cobra.Command, []string) error {
58
59 rwc, err := openTpm()
60 if err != nil {
61 return err
62 }
63 defer rwc.Close()
64
65 if !(format == "binarypb" || format == "textproto") {
66 return fmt.Errorf("format should be either binarypb or textproto")
67 }
68
69 var attestationKey *client.Key
70 algoToCreateAK, ok := attestationKeys[key]
71 if !ok {
72 return fmt.Errorf("key should be either AK or gceAK")
73 }
74 createFunc := algoToCreateAK[keyAlgo]
75 attestationKey, err = createFunc(rwc)
76 if err != nil {
77 return fmt.Errorf("failed to create attestation key: %v", err)
78 }
79 defer attestationKey.Close()
80
81 attestOpts := client.AttestOpts{}
82 attestOpts.Nonce = nonce
83
84 // Add logic to open other hardware devices when required.
85 switch teeTechnology {
86 case SevSnp:
87 attestOpts.TEEDevice, err = client.CreateSevSnpQuoteProvider()
88 if err != nil {
89 return fmt.Errorf("failed to open %s device: %v", SevSnp, err)
90 }
91 attestOpts.TEENonce = teeNonce
92 case Tdx:
93 attestOpts.TEEDevice, err = client.CreateTdxQuoteProvider()
94 if err != nil {
95 return fmt.Errorf("failed to create %s quote provider: %v", Tdx, err)
96 }
97 attestOpts.TEENonce = teeNonce
98 case "":
99 if len(teeNonce) != 0 {
100 return fmt.Errorf("use of --tee-nonce requires specifying TEE hardware type with --tee-technology")
101 }
102 default:
103 // Change the return statement when more devices are added
104 return fmt.Errorf("tee-technology should be either empty or should have values %s or %s", SevSnp, Tdx)
105 }
106
107 attestOpts.TCGEventLog, err = client.GetEventLog(rwc)
108 if err != nil {
109 return fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
110 }
111
112 attestation, err := attestationKey.Attest(attestOpts)
113 if err != nil {
114 return fmt.Errorf("failed to collect attestation report : %v", err)
115 }
116
117 if key == "gceAK" {
118 instanceInfo, err := getInstanceInfoFromMetadata()
119 if err != nil {
120 log.Printf("Could not get GCE instance info, continuing without it: %v", err)
121 }
122 attestation.InstanceInfo = instanceInfo
123 }
124
125 var out []byte
126 if format == "binarypb" {
127 out, err = proto.Marshal(attestation)
128 if err != nil {
129 return fmt.Errorf("failed to marshal attestation proto: %v", attestation)
130 }
131 } else {
132 out = []byte(marshalOptions.Format(attestation))
133 }
134 if _, err := dataOutput().Write(out); err != nil {
135 return fmt.Errorf("failed to write attestation report: %v", err)
136 }
137 return nil
138 },
139 }
140
141 func getInstanceInfoFromMetadata() (*attest.GCEInstanceInfo, error) {
142 ctx := context.Background()
143 var err error
144 instanceInfo := &attest.GCEInstanceInfo{}
145
146 instanceInfo.ProjectId, err = metadata.ProjectIDWithContext(ctx)
147 if err != nil {
148 return nil, err
149 }
150
151 projectNumber, err := metadata.NumericProjectIDWithContext(ctx)
152 if err != nil {
153 return nil, err
154 }
155 instanceInfo.ProjectNumber, err = strconv.ParseUint(projectNumber, 10, 64)
156 if err != nil {
157 return nil, err
158 }
159
160 instanceInfo.Zone, err = metadata.ZoneWithContext(ctx)
161 if err != nil {
162 return nil, err
163 }
164
165 instanceID, err := metadata.InstanceIDWithContext(ctx)
166 if err != nil {
167 return nil, err
168 }
169 instanceInfo.InstanceId, err = strconv.ParseUint(instanceID, 10, 64)
170 if err != nil {
171 return nil, err
172 }
173
174 instanceInfo.InstanceName, err = metadata.InstanceNameWithContext(ctx)
175 if err != nil {
176 return nil, err
177 }
178
179 return instanceInfo, err
180 }
181
182 func addKeyFlag(cmd *cobra.Command) {
183 cmd.PersistentFlags().StringVar(&key, "key", "AK", "indicates type of attestation key to use <gceAK|AK>")
184 }
185
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
190 func init() {
191 RootCmd.AddCommand(attestCmd)
192 addKeyFlag(attestCmd)
193 addNonceFlag(attestCmd)
194 addTeeNonceflag(attestCmd)
195 addPublicKeyAlgoFlag(attestCmd)
196 addOutputFlag(attestCmd)
197 addFormatFlag(attestCmd)
198 addTeeTechnology(attestCmd)
199 attestCmd.AddCommand(attestSVSMCmd)
200 }