tsm.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Package tsm parses Linux configfs-tsm reports in the media types Veraison
  2. // registered for them, application/vnd.veraison.tsm-report+json and
  3. // application/vnd.veraison.tsm-report+cbor, described by
  4. // https://github.com/veraison/ratsd/blob/main/docs/tsm-report.cddl.
  5. package tsm
  6. import (
  7. "encoding/base64"
  8. "encoding/json"
  9. "fmt"
  10. "mime"
  11. "strings"
  12. "github.com/fxamacker/cbor/v2"
  13. "github.com/smallstep/certificates/attestation"
  14. )
  15. // The media types of a configfs-tsm report.
  16. const (
  17. MediaTypeJSON = "application/vnd.veraison.tsm-report+json"
  18. MediaTypeCBOR = "application/vnd.veraison.tsm-report+cbor"
  19. )
  20. // The providers of the configfs-tsm reports this package's callers verify.
  21. const (
  22. // ProviderTDX is an Intel TDX guest, whose outblob is a quote.
  23. ProviderTDX = "tdx_guest"
  24. // ProviderSEVSNP is an AMD SEV-SNP guest, whose outblob is an attestation
  25. // report and whose auxblob is its certificate table.
  26. ProviderSEVSNP = "sev_guest"
  27. )
  28. // Report is a configfs-tsm report.
  29. type Report struct {
  30. // Provider names the format of OutBlob, for example tdx_guest.
  31. Provider string
  32. // OutBlob is the evidence the provider generated.
  33. OutBlob []byte
  34. // AuxBlob is the provider's auxiliary data, if any.
  35. AuxBlob []byte
  36. }
  37. // IsMediaType reports whether mediaType, with or without parameters, is one of
  38. // the media types of a configfs-tsm report.
  39. func IsMediaType(mediaType string) bool {
  40. mt, _, err := mime.ParseMediaType(mediaType)
  41. return err == nil && (mt == MediaTypeJSON || mt == MediaTypeCBOR)
  42. }
  43. // Parse parses a configfs-tsm report sent with the given media type. If the
  44. // media type has a provider parameter, it must match the report's provider.
  45. // Errors match attestation.ErrInvalidEvidence.
  46. func Parse(mediaType string, value []byte) (*Report, error) {
  47. mt, params, err := mime.ParseMediaType(mediaType)
  48. if err != nil {
  49. return nil, attestation.Invalidf("error parsing media type %q: %w", mediaType, err)
  50. }
  51. var r *Report
  52. switch mt {
  53. case MediaTypeJSON:
  54. r, err = parseJSON(value)
  55. case MediaTypeCBOR:
  56. r, err = parseCBOR(value)
  57. default:
  58. return nil, attestation.Invalidf("media type %q is not a tsm report", mt)
  59. }
  60. if err != nil {
  61. return nil, attestation.Invalid(err)
  62. }
  63. if r.Provider == "" {
  64. return nil, attestation.Invalidf("tsm report has no provider")
  65. }
  66. if len(r.OutBlob) == 0 {
  67. return nil, attestation.Invalidf("tsm report has no outblob")
  68. }
  69. if p, ok := params["provider"]; ok && p != r.Provider {
  70. return nil, attestation.Invalidf("tsm report provider %q does not match the media type's provider %q", r.Provider, p)
  71. }
  72. return r, nil
  73. }
  74. func parseJSON(value []byte) (*Report, error) {
  75. var v struct {
  76. Provider string `json:"provider"`
  77. OutBlob string `json:"outblob"`
  78. AuxBlob string `json:"auxblob"`
  79. }
  80. if err := json.Unmarshal(value, &v); err != nil {
  81. return nil, fmt.Errorf("error parsing tsm report: %w", err)
  82. }
  83. out, err := decodeBase64(v.OutBlob)
  84. if err != nil {
  85. return nil, fmt.Errorf("tsm report outblob must be base64url: %w", err)
  86. }
  87. aux, err := decodeBase64(v.AuxBlob)
  88. if err != nil {
  89. return nil, fmt.Errorf("tsm report auxblob must be base64url: %w", err)
  90. }
  91. return &Report{Provider: v.Provider, OutBlob: out, AuxBlob: aux}, nil
  92. }
  93. func parseCBOR(value []byte) (*Report, error) {
  94. var v struct {
  95. Provider string `cbor:"provider"`
  96. OutBlob []byte `cbor:"outblob"`
  97. AuxBlob []byte `cbor:"auxblob"`
  98. }
  99. if err := cbor.Unmarshal(value, &v); err != nil {
  100. return nil, fmt.Errorf("error parsing tsm report: %w", err)
  101. }
  102. return &Report{Provider: v.Provider, OutBlob: v.OutBlob, AuxBlob: v.AuxBlob}, nil
  103. }
  104. // decodeBase64 decodes base64url, as the CDDL requires, and also accepts the
  105. // standard alphabet and padding.
  106. func decodeBase64(s string) ([]byte, error) {
  107. s = strings.TrimRight(s, "=")
  108. if s == "" {
  109. return nil, nil
  110. }
  111. if strings.ContainsAny(s, "+/") {
  112. return base64.RawStdEncoding.DecodeString(s)
  113. }
  114. return base64.RawURLEncoding.DecodeString(s)
  115. }