Debian Codegraph @

client/attest.go

raw
1 package client
2
3 import (
4 "fmt"
5 "net/http"
6
7 sabi "github.com/google/go-sev-guest/abi"
8 sg "github.com/google/go-sev-guest/client"
9 tg "github.com/google/go-tdx-guest/client"
10 tabi "github.com/google/go-tdx-guest/client/linuxabi"
11 tpb "github.com/google/go-tdx-guest/proto/tdx"
12 "github.com/google/go-tpm-tools/internal"
13 pb "github.com/google/go-tpm-tools/proto/attest"
14 )
15
16 // TEEDevice is an interface to add an attestation report from a TEE technology's
17 // attestation driver or quote provider.
18 type TEEDevice interface {
19 // AddAttestation uses the TEE device's attestation driver or quote provider to collect an
20 // attestation report, then adds it to the correct field of `attestation`.
21 AddAttestation(attestation *pb.Attestation, options AttestOpts) error
22 // Close finalizes any resources in use by the TEEDevice.
23 Close() error
24 }
25
26 // AttestOpts allows for customizing the functionality of Attest.
27 type AttestOpts struct {
28 // A unique, application-specific nonce used to guarantee freshness of the
29 // attestation. This must not be empty, and should generally be long enough
30 // to make brute force attacks infeasible.
31 //
32 // For security reasons, applications should not allow for attesting with
33 // arbitrary, externally-provided nonces. The nonce should be prefixed or
34 // otherwise bound (i.e. via a KDF) to application-specific data. For more
35 // information on why this is an issue, see this paper on robust remote
36 // attestation protocols:
37 // https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.70.4562&rep=rep1&type=pdf
38 Nonce []byte
39 // TCG Event Log to add to the attestation.
40 // If not specified then it take Event Log by calling GetEventLog().
41 TCGEventLog []byte
42 // TCG Canonical Event Log to add to the attestation.
43 // Currently, we only support PCR replay for PCRs orthogonal to those in the
44 // firmware event log, where PCRs 0-9 and 14 are often measured. If the two
45 // logs overlap, server-side verification using this library may fail.
46 // Deprecated: Manually populate the pb.Attestation instead.
47 CanonicalEventLog []byte
48 // If non-nil, will be used to fetch the AK certificate chain for validation.
49 // Key.Attest() will construct the certificate chain by making GET requests to
50 // the contents of Key.cert.IssuingCertificateURL using this client.
51 CertChainFetcher *http.Client
52 // TEEDevice implements the TEEDevice interface for collecting a Trusted execution
53 // environment attestation. If nil, then Attest will try all known TEE devices,
54 // and TEENonce must be nil. If not nil, Attest will not call Close() on the device.
55 TEEDevice TEEDevice
56 // TEENonce is the nonce that will be used in the TEE's attestation collection
57 // mechanism. It is expected to be the size required by the technology. If nil,
58 // then the nonce will be populated with Nonce, either truncated or zero-filled
59 // depending on the technology's size. Leaving this nil is not recommended. If
60 // nil, then TEEDevice must be nil.
61 TEENonce []byte
62
63 // Setting this skips attaching the TEE attestation
64 SkipTeeAttestation bool
65 }
66
67 // SevSnpQuoteProvider encapsulates the SEV-SNP attestation device to add its attestation report
68 // to a pb.Attestation.
69 type SevSnpQuoteProvider struct {
70 QuoteProvider sg.QuoteProvider
71 }
72
73 // TdxDevice encapsulates the TDX attestation device to add its attestation quote
74 // to a pb.Attestation.
75 // Deprecated: TdxDevice is deprecated. It is recommended to use TdxQuoteProvider.
76 type TdxDevice struct {
77 Device tg.Device
78 }
79
80 // TdxQuoteProvider encapsulates the TDX attestation device to add its attestation quote
81 // to a pb.Attestation.
82 type TdxQuoteProvider struct {
83 QuoteProvider tg.QuoteProvider
84 }
85
86 // AddAttestation will get the SEV-SNP attestation report given opts.TEENonce with
87 // associated certificates and add them to `attestation`. If opts.TEENonce is empty,
88 // then uses contents of opts.Nonce.
89 func (d *SevSnpQuoteProvider) AddAttestation(attestation *pb.Attestation, opts AttestOpts) error {
90 var snpNonce [sabi.ReportDataSize]byte
91 if len(opts.TEENonce) == 0 {
92 copy(snpNonce[:], opts.Nonce)
93 } else if len(opts.TEENonce) != sabi.ReportDataSize {
94 return fmt.Errorf("the TEENonce size is %d. SEV-SNP device requires 64", len(opts.TEENonce))
95 } else {
96 copy(snpNonce[:], opts.TEENonce)
97 }
98 raw, err := d.QuoteProvider.GetRawQuote(snpNonce)
99 if err != nil {
100 return err
101 }
102 extReport, err := sabi.ReportCertsToProto(raw)
103 if err != nil {
104 return err
105 }
106 attestation.TeeAttestation = &pb.Attestation_SevSnpAttestation{
107 SevSnpAttestation: extReport,
108 }
109 return nil
110 }
111
112 // Close is a no-op.
113 func (d *SevSnpQuoteProvider) Close() error {
114 return nil
115 }
116
117 // CreateSevSnpQuoteProvider creates the SEV-SNP quote provider and wraps it with behavior
118 // that allows it to add an attestation quote to pb.Attestation.
119 func CreateSevSnpQuoteProvider() (TEEDevice, error) {
120 qp, err := sg.GetQuoteProvider()
121 if err != nil {
122 return nil, err
123 }
124 if !qp.IsSupported() {
125 return nil, fmt.Errorf("sev-snp attestation reports not available")
126 }
127 return &SevSnpQuoteProvider{QuoteProvider: qp}, nil
128 }
129
130 // CreateTdxDevice opens the TDX attestation driver and wraps it with behavior
131 // that allows it to add an attestation quote to pb.Attestation.
132 // Deprecated: TdxDevice is deprecated, and use of CreateTdxQuoteProvider is
133 // recommended to create a TEEDevice.
134 func CreateTdxDevice() (*TdxDevice, error) {
135 d, err := tg.OpenDevice()
136 if err != nil {
137 return nil, err
138 }
139 return &TdxDevice{Device: d}, nil
140 }
141
142 // AddAttestation will get the TDX attestation quote given opts.TEENonce
143 // and add them to `attestation`. If opts.TEENonce is empty, then uses
144 // contents of opts.Nonce.
145 func (d *TdxDevice) AddAttestation(attestation *pb.Attestation, opts AttestOpts) error {
146 var tdxNonce [tabi.TdReportDataSize]byte
147 err := fillTdxNonce(opts, tdxNonce[:])
148 if err != nil {
149 return err
150 }
151 quote, err := tg.GetQuote(d.Device, tdxNonce)
152 if err != nil {
153 return err
154 }
155 return setTeeAttestationTdxQuote(quote, attestation)
156 }
157
158 // Close will free the device handle held by the TdxDevice. Calling more
159 // than once has no effect.
160 func (d *TdxDevice) Close() error {
161 if d.Device != nil {
162 err := d.Device.Close()
163 d.Device = nil
164 return err
165 }
166 return nil
167 }
168
169 // CreateTdxQuoteProvider creates the TDX quote provider and wraps it with behavior
170 // that allows it to add an attestation quote to pb.Attestation.
171 func CreateTdxQuoteProvider() (*TdxQuoteProvider, error) {
172 qp, err := tg.GetQuoteProvider()
173 if err != nil {
174 return nil, err
175 }
176 if qp.IsSupported() != nil {
177 // TDX quote provider has a fallback mechanism to fetch attestation quote
178 // via device driver in case ConfigFS is not supported, so checking for TDX
179 // device availability here. Once Device interface is fully removed from
180 // subsequent go-tdx-guest versions, then below OpenDevice call should be
181 // removed as well.
182 d, err2 := tg.OpenDevice()
183 if err2 != nil {
184 return nil, fmt.Errorf("neither TDX device, nor quote provider is supported")
185 }
186 d.Close()
187 }
188
189 return &TdxQuoteProvider{QuoteProvider: qp}, nil
190 }
191
192 // AddAttestation will get the TDX attestation quote given opts.TEENonce
193 // and add them to `attestation`. If opts.TEENonce is empty, then uses
194 // contents of opts.Nonce.
195 func (qp *TdxQuoteProvider) AddAttestation(attestation *pb.Attestation, opts AttestOpts) error {
196 var tdxNonce [tabi.TdReportDataSize]byte
197 err := fillTdxNonce(opts, tdxNonce[:])
198 if err != nil {
199 return err
200 }
201 quote, err := tg.GetQuote(qp.QuoteProvider, tdxNonce)
202 if err != nil {
203 return err
204 }
205 return setTeeAttestationTdxQuote(quote, attestation)
206 }
207
208 // Close will free resources held by QuoteProvider.
209 func (qp *TdxQuoteProvider) Close() error {
210 return nil
211 }
212
213 func fillTdxNonce(opts AttestOpts, tdxNonce []byte) error {
214 if len(opts.TEENonce) == 0 {
215 copy(tdxNonce[:], opts.Nonce)
216 } else if len(opts.TEENonce) != tabi.TdReportDataSize {
217 return fmt.Errorf("the TEENonce size is %d. Intel TDX device requires %d", len(opts.TEENonce), tabi.TdReportDataSize)
218 } else {
219 copy(tdxNonce[:], opts.TEENonce)
220 }
221 return nil
222 }
223
224 func setTeeAttestationTdxQuote(quote any, attestation *pb.Attestation) error {
225 switch q := quote.(type) {
226 case *tpb.QuoteV4:
227 attestation.TeeAttestation = &pb.Attestation_TdxAttestation{
228 TdxAttestation: q,
229 }
230 default:
231 return fmt.Errorf("unsupported quote type: %T", quote)
232 }
233 return nil
234 }
235
236 // Does best effort to get a TEE hardware rooted attestation, but won't fail fatally
237 // unless the user provided a TEEDevice object.
238 func getTEEAttestationReport(attestation *pb.Attestation, opts AttestOpts) error {
239 if opts.SkipTeeAttestation {
240 return nil
241 }
242 device := opts.TEEDevice
243 if device != nil {
244 return device.AddAttestation(attestation, opts)
245 }
246
247 // TEEDevice can't be nil while TEENonce is non-nil
248 if opts.TEENonce != nil {
249 return fmt.Errorf("got non-nil TEENonce when TEEDevice is nil: %v", opts.TEENonce)
250 }
251
252 // Try SEV-SNP.
253 if sevqp, err := CreateSevSnpQuoteProvider(); err == nil {
254 // Don't return errors if the attestation collection fails, since
255 // the user didn't specify a TEEDevice.
256 sevqp.AddAttestation(attestation, opts)
257 return nil
258 }
259
260 // Try TDX.
261 if quoteProvider, err := CreateTdxQuoteProvider(); err == nil {
262 // Don't return errors if the attestation collection fails, since
263 // the user didn't specify a TEEDevice.
264 quoteProvider.AddAttestation(attestation, opts)
265 quoteProvider.Close()
266 return nil
267 }
268 // Add more devices here.
269 return nil
270 }
271
272 // Attest generates an Attestation containing the TCG Event Log and a Quote over
273 // all PCR banks. The provided nonce can be used to guarantee freshness of the
274 // attestation. This function will return an error if the key is not a
275 // restricted signing key.
276 //
277 // AttestOpts is used for additional configuration of the Attestation process.
278 // This is primarily used to pass the attestation's nonce:
279 //
280 // attestation, err := key.Attest(client.AttestOpts{Nonce: my_nonce})
281 func (k *Key) Attest(opts AttestOpts) (*pb.Attestation, error) {
282 if len(opts.Nonce) == 0 {
283 return nil, fmt.Errorf("provided nonce must not be empty")
284 }
285 sels, err := AllocatedPCRs(k.rw)
286 if err != nil {
287 return nil, err
288 }
289
290 attestation := pb.Attestation{}
291 if attestation.AkPub, err = k.PublicArea().Encode(); err != nil {
292 return nil, fmt.Errorf("failed to encode public area: %w", err)
293 }
294 attestation.AkCert = k.CertDERBytes()
295 for _, sel := range sels {
296 quote, err := k.Quote(sel, opts.Nonce)
297 if err != nil {
298 return nil, err
299 }
300 attestation.Quotes = append(attestation.Quotes, quote)
301 }
302 if opts.TCGEventLog == nil {
303 if attestation.EventLog, err = GetEventLog(k.rw); err != nil {
304 return nil, fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
305 }
306 } else {
307 attestation.EventLog = opts.TCGEventLog
308 }
309 if len(opts.CanonicalEventLog) != 0 {
310 attestation.CanonicalEventLog = opts.CanonicalEventLog
311 }
312
313 // Attempt to construct certificate chain. fetchIssuingCertificate checks if
314 // AK cert is present and contains intermediate cert URLs.
315 if opts.CertChainFetcher != nil {
316 attestation.IntermediateCerts, err = internal.GetCertificateChain(k.cert, opts.CertChainFetcher)
317 if err != nil {
318 return nil, fmt.Errorf("fetching certificate chain: %w", err)
319 }
320 }
321
322 // TODO: issues/504 this should be outside of this function, not related to TPM attestation
323 if err := getTEEAttestationReport(&attestation, opts); err != nil {
324 return nil, fmt.Errorf("collecting TEE attestation report: %w", err)
325 }
326
327 return &attestation, nil
328 }