sevsnp.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Package sevsnp verifies AMD SEV-SNP attestation reports.
  2. package sevsnp
  3. import (
  4. "bytes"
  5. "context"
  6. "crypto/x509"
  7. "encoding/hex"
  8. "encoding/pem"
  9. "errors"
  10. "fmt"
  11. "strings"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "github.com/google/go-sev-guest/abi"
  16. "github.com/google/go-sev-guest/kds"
  17. spb "github.com/google/go-sev-guest/proto/sevsnp"
  18. sevvalidate "github.com/google/go-sev-guest/validate"
  19. sevverify "github.com/google/go-sev-guest/verify"
  20. sevtrust "github.com/google/go-sev-guest/verify/trust"
  21. "github.com/smallstep/certificates/attestation"
  22. )
  23. // ReportDataSize is the size of an SEV-SNP report's report data.
  24. const ReportDataSize = 64
  25. // Verifier verifies AMD SEV-SNP attestation reports: the report's signature by
  26. // the chip's VCEK or the provider's VLEK, and that key's chain through the ASK
  27. // or ASVK to the ARK of the product line. Certificates the workload does not
  28. // send are fetched from AMD's key distribution service, as are the revocation
  29. // lists. Reports from guests whose policy allows debugging are refused. The TCB
  30. // versions must match the signing key's certificate, and are reported, not
  31. // checked against a minimum.
  32. type Verifier struct {
  33. // roots are the ARK, ASK and ASVK certificates to trust per product
  34. // line: the configured ones, or AMD's.
  35. roots map[string][]*sevtrust.AMDRootCerts
  36. getter sevtrust.HTTPSGetter
  37. // checkRevocations is always true outside of tests.
  38. checkRevocations bool
  39. now func() time.Time
  40. }
  41. // amdRoots are AMD's ARK, ASK and ASVK certificates for every product line,
  42. // from the VCEK and VLEK bundles embedded in go-sev-guest.
  43. var amdRoots = sync.OnceValues(func() (map[string][]*sevtrust.AMDRootCerts, error) {
  44. return parseRoots(bytes.Join([][]byte{
  45. sevtrust.AskArkMilanVcekBytes, sevtrust.AskArkMilanVlekBytes,
  46. sevtrust.AskArkGenoaVcekBytes, sevtrust.AskArkGenoaVlekBytes,
  47. sevtrust.AskArkTurinVcekBytes, sevtrust.AskArkTurinVlekBytes,
  48. }, []byte("\n")))
  49. })
  50. // New returns a Verifier that fetches certificates and revocation lists with
  51. // the given getter. If roots, a PEM bundle of ARK, ASK and ASVK certificates, is
  52. // not empty, it replaces AMD's roots.
  53. func New(roots []byte, getter sevtrust.HTTPSGetter) (*Verifier, error) {
  54. v := &Verifier{
  55. getter: getter,
  56. checkRevocations: true,
  57. now: time.Now,
  58. }
  59. // The roots are always passed to go-sev-guest. Before v0.15.0, without
  60. // them it took the ARK and ASK from the evidence's own certificate table
  61. // and, because its embedded roots hold no AMD SEV format certificates to
  62. // cross-check them with, trusted whatever ARK the workload sent.
  63. var err error
  64. if len(roots) > 0 {
  65. v.roots, err = parseRoots(roots)
  66. } else {
  67. v.roots, err = amdRoots()
  68. }
  69. if err != nil {
  70. return nil, err
  71. }
  72. return v, nil
  73. }
  74. // parseRoots reads the ARK, ASK and ASVK certificates in a PEM bundle and
  75. // groups them by product line, which AMD names in their common names: ARK-Milan,
  76. // SEV-Milan and SEV-VLEK-Milan for Milan. Each product line needs its ARK and at
  77. // least one of its ASK and ASVK.
  78. func parseRoots(data []byte) (map[string][]*sevtrust.AMDRootCerts, error) {
  79. certs := make(map[string]*sevtrust.ProductCerts)
  80. line := func(name string) *sevtrust.ProductCerts {
  81. if certs[name] == nil {
  82. certs[name] = new(sevtrust.ProductCerts)
  83. }
  84. return certs[name]
  85. }
  86. for rest := data; len(rest) > 0; {
  87. var block *pem.Block
  88. if block, rest = pem.Decode(rest); block == nil {
  89. break
  90. }
  91. if block.Type != "CERTIFICATE" {
  92. continue
  93. }
  94. cert, err := x509.ParseCertificate(block.Bytes)
  95. if err != nil {
  96. return nil, fmt.Errorf("error parsing the sev-snp roots: %w", err)
  97. }
  98. switch cn := cert.Subject.CommonName; {
  99. case strings.HasPrefix(cn, "ARK-"):
  100. line(strings.TrimPrefix(cn, "ARK-")).Ark = cert
  101. case strings.HasPrefix(cn, "SEV-VLEK-"):
  102. line(strings.TrimPrefix(cn, "SEV-VLEK-")).Asvk = cert
  103. case strings.HasPrefix(cn, "SEV-"):
  104. line(strings.TrimPrefix(cn, "SEV-")).Ask = cert
  105. default:
  106. return nil, fmt.Errorf("sev-snp roots: %q is not an AMD ARK, ASK or ASVK", cn)
  107. }
  108. }
  109. if len(certs) == 0 {
  110. return nil, errors.New("no certificates found in the sev-snp roots")
  111. }
  112. roots := make(map[string][]*sevtrust.AMDRootCerts, len(certs))
  113. for name, pc := range certs {
  114. if pc.Ark == nil || (pc.Ask == nil && pc.Asvk == nil) {
  115. return nil, fmt.Errorf("sev-snp roots: product line %s needs its ARK and its ASK or ASVK", name)
  116. }
  117. r := sevtrust.AMDRootCertsProduct(name)
  118. r.ProductCerts = pc
  119. roots[name] = []*sevtrust.AMDRootCerts{r}
  120. }
  121. return roots, nil
  122. }
  123. // Verify verifies an SEV-SNP attestation report, with the certificate table an
  124. // extended report returns if it is not empty, and returns the report's
  125. // measurements and its report data. Errors of the report match
  126. // attestation.ErrInvalidEvidence; a failure to fetch certificates or revocation
  127. // lists does not.
  128. func (v *Verifier) Verify(ctx context.Context, rawReport, certTable []byte) (*attestation.SEVSNP, []byte, error) {
  129. report, err := abi.ReportToProto(rawReport)
  130. if err != nil {
  131. return nil, nil, attestation.Invalidf("error parsing sev-snp report: %w", err)
  132. }
  133. att := &spb.Attestation{Report: report}
  134. if len(certTable) > 0 {
  135. var table abi.CertTable
  136. if err := table.Unmarshal(certTable); err != nil {
  137. return nil, nil, attestation.Invalidf("error parsing sev-snp certificate table: %w", err)
  138. }
  139. att.CertificateChain = table.Proto()
  140. }
  141. getter := &recordingGetter{getter: v.getter}
  142. opts := &sevverify.Options{
  143. CheckRevocations: v.checkRevocations,
  144. Getter: getter,
  145. Now: v.now(),
  146. TrustedRoots: v.roots,
  147. }
  148. if err := sevverify.SnpAttestationContext(ctx, att, opts); err != nil {
  149. if getter.failed.Load() {
  150. return nil, nil, fmt.Errorf("error fetching sev-snp certificates: %w", err)
  151. }
  152. return nil, nil, attestation.Invalidf("error verifying sev-snp report: %w", err)
  153. }
  154. // The signature proves only that the VCEK or VLEK signed the report. The
  155. // key's certificate carries the TCB it was issued for, and for a VCEK the
  156. // chip's id, which a certificate table from the workload need not match:
  157. // with an old VCEK, the report's TCB fields would say whatever its signer
  158. // chose. validate binds them to the certificate, refusing a reported TCB
  159. // that differs from the certificate's, a current TCB below it, a committed
  160. // TCB above the current one, and a CHIP_ID that is not the VCEK's. Its
  161. // guest policy options permit everything, since debugging is refused below
  162. // with its own error and nothing else of the policy is checked.
  163. if err := sevvalidate.SnpAttestation(att, &sevvalidate.Options{
  164. GuestPolicy: abi.SnpPolicy{MigrateMA: true, Debug: true, SMT: true, CXLAllowed: true},
  165. PermitProvisionalFirmware: true,
  166. }); err != nil {
  167. return nil, nil, attestation.Invalidf("error validating sev-snp report: %w", err)
  168. }
  169. policy, err := abi.ParseSnpPolicy(report.GetPolicy())
  170. if err != nil {
  171. return nil, nil, attestation.Invalidf("error parsing sev-snp guest policy: %w", err)
  172. }
  173. if policy.Debug {
  174. return nil, nil, attestation.Invalidf("sev-snp report is from a guest whose policy allows debugging")
  175. }
  176. signer, err := abi.ParseSignerInfo(report.GetSignerInfo())
  177. if err != nil {
  178. return nil, nil, attestation.Invalidf("error parsing sev-snp signer info: %w", err)
  179. }
  180. product := kds.ProductLine(att.GetProduct())
  181. if fms := report.GetCpuid1EaxFms(); fms != 0 {
  182. product = kds.ProductLineFromFms(fms)
  183. }
  184. reportData := report.GetReportData()
  185. if len(reportData) != ReportDataSize {
  186. return nil, nil, attestation.Invalidf("sev-snp report data has %d bytes, not %d", len(reportData), ReportDataSize)
  187. }
  188. return &attestation.SEVSNP{
  189. Measurement: hex.EncodeToString(report.GetMeasurement()),
  190. HostData: hex.EncodeToString(report.GetHostData()),
  191. IDKeyDigest: hex.EncodeToString(report.GetIdKeyDigest()),
  192. AuthorKeyDigest: hex.EncodeToString(report.GetAuthorKeyDigest()),
  193. FamilyID: hex.EncodeToString(report.GetFamilyId()),
  194. ImageID: hex.EncodeToString(report.GetImageId()),
  195. GuestSVN: report.GetGuestSvn(),
  196. Policy: report.GetPolicy(),
  197. VMPL: report.GetVmpl(),
  198. CurrentTCB: report.GetCurrentTcb(),
  199. ReportedTCB: report.GetReportedTcb(),
  200. LaunchTCB: report.GetLaunchTcb(),
  201. ChipID: hex.EncodeToString(report.GetChipId()),
  202. SigningKey: strings.ToLower(signer.SigningKey.String()),
  203. Product: product,
  204. }, reportData, nil
  205. }
  206. // recordingGetter records whether a fetch failed, so that a report that cannot
  207. // be verified because a service is unreachable is not called invalid.
  208. type recordingGetter struct {
  209. getter sevtrust.HTTPSGetter
  210. failed atomic.Bool
  211. }
  212. func (g *recordingGetter) Get(url string) ([]byte, error) {
  213. body, err := g.getter.Get(url)
  214. if err != nil {
  215. g.failed.Store(true)
  216. }
  217. return body, err
  218. }
  219. func (g *recordingGetter) GetContext(ctx context.Context, url string) ([]byte, error) {
  220. cg, ok := g.getter.(sevtrust.ContextHTTPSGetter)
  221. if !ok {
  222. return g.Get(url)
  223. }
  224. body, err := cg.GetContext(ctx, url)
  225. if err != nil {
  226. g.failed.Store(true)
  227. }
  228. return body, err
  229. }