collateral.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Package collateral fetches and caches the collateral that TEE evidence is
  2. // verified with: TCB information, identities, certificates and revocation
  3. // lists from Intel's provisioning certification service and AMD's key
  4. // distribution service.
  5. package collateral
  6. import (
  7. "context"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "sync"
  12. "time"
  13. )
  14. // TTL is how long a Getter keeps a response. The verifiers still check each
  15. // item's own validity.
  16. const TTL = time.Hour
  17. // MaxSize limits the size of a response.
  18. const MaxSize = 1 << 20
  19. // HTTPClient is the interface of the client a Getter fetches with, which
  20. // *http.Client implements.
  21. type HTTPClient interface {
  22. Do(*http.Request) (*http.Response, error)
  23. }
  24. // Getter fetches collateral and caches successful responses for TTL, so that
  25. // verifying evidence does not depend on, or overload, the vendors' services on
  26. // every request.
  27. type Getter struct {
  28. client HTTPClient
  29. now func() time.Time
  30. mu sync.Mutex
  31. entries map[string]entry
  32. }
  33. type entry struct {
  34. header http.Header
  35. body []byte
  36. expires time.Time
  37. }
  38. // New returns a Getter that fetches with the given client.
  39. func New(client HTTPClient) *Getter {
  40. return &Getter{
  41. client: client,
  42. now: time.Now,
  43. entries: make(map[string]entry),
  44. }
  45. }
  46. func (g *Getter) get(ctx context.Context, url string) (http.Header, []byte, error) {
  47. now := g.now()
  48. g.mu.Lock()
  49. e, ok := g.entries[url]
  50. g.mu.Unlock()
  51. if ok && now.Before(e.expires) {
  52. return e.header, e.body, nil
  53. }
  54. req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
  55. if err != nil {
  56. return nil, nil, err
  57. }
  58. resp, err := g.client.Do(req)
  59. if err != nil {
  60. return nil, nil, fmt.Errorf("error fetching %s: %w", url, err)
  61. }
  62. defer resp.Body.Close()
  63. if resp.StatusCode != http.StatusOK {
  64. return nil, nil, fmt.Errorf("error fetching %s: status code %d", url, resp.StatusCode)
  65. }
  66. body, err := io.ReadAll(io.LimitReader(resp.Body, MaxSize))
  67. if err != nil {
  68. return nil, nil, fmt.Errorf("error reading %s: %w", url, err)
  69. }
  70. g.mu.Lock()
  71. g.entries[url] = entry{
  72. header: resp.Header,
  73. body: body,
  74. expires: now.Add(TTL),
  75. }
  76. g.mu.Unlock()
  77. return resp.Header, body, nil
  78. }
  79. // TDX adapts a Getter to go-tdx-guest's trust.HTTPSGetter and
  80. // trust.ContextHTTPSGetter, which return the response headers too.
  81. type TDX struct {
  82. *Getter
  83. }
  84. // Get implements go-tdx-guest's trust.HTTPSGetter.
  85. func (g TDX) Get(url string) (map[string][]string, []byte, error) {
  86. return g.GetContext(context.Background(), url)
  87. }
  88. // GetContext implements go-tdx-guest's trust.ContextHTTPSGetter.
  89. func (g TDX) GetContext(ctx context.Context, url string) (map[string][]string, []byte, error) {
  90. return g.get(ctx, url)
  91. }
  92. // SEVSNP adapts a Getter to go-sev-guest's trust.HTTPSGetter and
  93. // trust.ContextHTTPSGetter.
  94. type SEVSNP struct {
  95. *Getter
  96. }
  97. // Get implements go-sev-guest's trust.HTTPSGetter.
  98. func (g SEVSNP) Get(url string) ([]byte, error) {
  99. return g.GetContext(context.Background(), url)
  100. }
  101. // GetContext implements go-sev-guest's trust.ContextHTTPSGetter.
  102. func (g SEVSNP) GetContext(ctx context.Context, url string) ([]byte, error) {
  103. _, body, err := g.get(ctx, url)
  104. return body, err
  105. }