sevsnp_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //go:build unix
  2. package sevsnp
  3. import (
  4. "context"
  5. "encoding/binary"
  6. "encoding/pem"
  7. "errors"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/smallstep/certificates/attestation"
  13. "github.com/smallstep/certificates/attestation/sevsnp/sevsnptest"
  14. )
  15. // failingGetter fails every fetch, so a test fails if the verifier needs AMD's
  16. // key distribution service, and stands for an unreachable service.
  17. type failingGetter struct{}
  18. func (failingGetter) Get(url string) ([]byte, error) {
  19. return nil, errors.New("unexpected fetch of " + url)
  20. }
  21. func newVerifier(t *testing.T, roots []byte) *Verifier {
  22. t.Helper()
  23. v, err := New(roots, failingGetter{})
  24. require.NoError(t, err)
  25. v.checkRevocations = false
  26. return v
  27. }
  28. func TestVerifier(t *testing.T) {
  29. s := sevsnptest.New(t, false)
  30. other := sevsnptest.New(t, true)
  31. reportData := make([]byte, ReportDataSize)
  32. for i := range reportData {
  33. reportData[i] = byte(i)
  34. }
  35. t.Run("ok", func(t *testing.T) {
  36. got, gotReportData, err := newVerifier(t, s.Roots).Verify(context.Background(), s.Report(t, reportData, false), s.CertTable)
  37. require.NoError(t, err)
  38. assert.Equal(t, reportData, gotReportData)
  39. assert.Len(t, got.Measurement, 96)
  40. assert.Len(t, got.HostData, 64)
  41. assert.Len(t, got.ChipID, 128)
  42. assert.Equal(t, "vcek", got.SigningKey)
  43. assert.Equal(t, "Milan", got.Product)
  44. })
  45. tests := []struct {
  46. name string
  47. v *Verifier
  48. report []byte
  49. certTable []byte
  50. wantErr string
  51. wantInvalid bool
  52. }{
  53. {"fail debug", newVerifier(t, s.Roots), s.Report(t, reportData, true), s.CertTable,
  54. "policy allows debugging", true},
  55. // A report under the workload's own ARK, sent in its certificate
  56. // table, is not trusted when the verifier has AMD's roots.
  57. {"fail amd roots", newVerifier(t, nil), s.Report(t, reportData, false), s.CertTable,
  58. "could not be verified by any trusted roots", true},
  59. // go-sev-guest before v0.15.0 trusted the evidence's own ARK when it
  60. // was given no roots; the verifier never relies on that, and this
  61. // case fails if the module is downgraded below the fix.
  62. {"fail library default roots", &Verifier{getter: failingGetter{}, now: time.Now},
  63. s.Report(t, reportData, false), s.CertTable, "could not be verified by any trusted roots", true},
  64. {"fail other vcek", newVerifier(t, s.Roots), other.Report(t, reportData, false), s.CertTable,
  65. "error verifying sev-snp report", true},
  66. // Without a certificate table, the VCEK is fetched, and the failure
  67. // to fetch it is not the report's.
  68. {"fail vcek unreachable", newVerifier(t, s.Roots), s.Report(t, reportData, false), nil,
  69. "error fetching sev-snp certificates", false},
  70. {"fail tampered", newVerifier(t, s.Roots), func() []byte {
  71. report := s.Report(t, reportData, false)
  72. report[0x50] ^= 1 // the first byte of REPORT_DATA
  73. return report
  74. }(), s.CertTable, "error verifying sev-snp report", true},
  75. // The VCEK signed the report but was issued for another TCB, so the
  76. // report's TCB fields would be whatever its signer chose.
  77. {"fail tcb of another vcek", newVerifier(t, s.Roots), s.EditedReport(t, reportData, false, func(report []byte) {
  78. const newer = uint64(0x1800000000000a04)
  79. binary.LittleEndian.PutUint64(report[0x38:0x40], newer) // CURRENT_TCB
  80. binary.LittleEndian.PutUint64(report[0x180:0x188], newer) // REPORTED_TCB
  81. }), s.CertTable, "does not match the TCB of the V[CL]EK certificate", true},
  82. {"fail chip id of another vcek", newVerifier(t, s.Roots), s.EditedReport(t, reportData, false, func(report []byte) {
  83. report[0x1a0] ^= 1 // the first byte of CHIP_ID
  84. }), s.CertTable, "is not the same as the VCEK certificate's HWID", true},
  85. {"fail parse", newVerifier(t, s.Roots), []byte("not a report"), nil, "error parsing sev-snp report", true},
  86. {"fail certificate table", newVerifier(t, s.Roots), s.Report(t, reportData, false), []byte("not a table"),
  87. "error parsing sev-snp certificate table", true},
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.name, func(t *testing.T) {
  91. _, _, err := tt.v.Verify(context.Background(), tt.report, tt.certTable)
  92. assert.ErrorContains(t, err, tt.wantErr)
  93. assert.Equal(t, tt.wantInvalid, errors.Is(err, attestation.ErrInvalidEvidence))
  94. })
  95. }
  96. }
  97. func TestParseRoots(t *testing.T) {
  98. s := sevsnptest.New(t, false)
  99. roots, err := parseRoots(s.Roots)
  100. require.NoError(t, err)
  101. require.Contains(t, roots, "Milan")
  102. require.Len(t, roots["Milan"], 1)
  103. assert.Equal(t, s.Ark(), roots["Milan"][0].ProductCerts.Ark)
  104. assert.Equal(t, s.Ask(), roots["Milan"][0].ProductCerts.Ask)
  105. assert.Equal(t, s.Asvk(), roots["Milan"][0].ProductCerts.Asvk)
  106. _, err = parseRoots(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: s.Ask().Raw}))
  107. assert.ErrorContains(t, err, "product line Milan needs its ARK")
  108. _, err = parseRoots([]byte("not pem"))
  109. assert.ErrorContains(t, err, "no certificates found")
  110. amd, err := amdRoots()
  111. require.NoError(t, err)
  112. for _, line := range []string{"Milan", "Genoa", "Turin"} {
  113. require.Len(t, amd[line], 1, line)
  114. pc := amd[line][0].ProductCerts
  115. assert.Equal(t, "ARK-"+line, pc.Ark.Subject.CommonName)
  116. assert.Equal(t, "SEV-"+line, pc.Ask.Subject.CommonName)
  117. assert.Equal(t, "SEV-VLEK-"+line, pc.Asvk.Subject.CommonName)
  118. }
  119. }