| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/x509" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
sgtest "github.com/google/go-sev-guest/testing" |
| 13 |
testclient "github.com/google/go-sev-guest/testing/client" |
| 14 |
tgtest "github.com/google/go-tdx-guest/testing" |
| 15 |
tgtestclient "github.com/google/go-tdx-guest/testing/client" |
| 16 |
tgtestdata "github.com/google/go-tdx-guest/testing/testdata" |
| 17 |
"github.com/google/go-tpm-tools/internal/test" |
| 18 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 19 |
) |
| 20 |
|
| 21 |
var localClient = http.DefaultClient |
| 22 |
|
| 23 |
func TestKeyAttestSucceedsWithCertChainRetrieval(t *testing.T) { |
| 24 |
testCA, caKey := test.GetTestCert(t, nil, nil, nil) |
| 25 |
|
| 26 |
caServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { |
| 27 |
rw.WriteHeader(http.StatusOK) |
| 28 |
rw.Write(testCA.Raw) |
| 29 |
})) |
| 30 |
|
| 31 |
defer caServer.Close() |
| 32 |
|
| 33 |
leafCert, _ := test.GetTestCert(t, []string{caServer.URL}, testCA, caKey) |
| 34 |
|
| 35 |
rwc := test.GetTPM(t) |
| 36 |
defer CheckedClose(t, rwc) |
| 37 |
|
| 38 |
ak, err := AttestationKeyRSA(rwc) |
| 39 |
if err != nil { |
| 40 |
t.Fatalf("Failed to generate test AK: %v", err) |
| 41 |
} |
| 42 |
|
| 43 |
ak.cert = leafCert |
| 44 |
|
| 45 |
attestation, err := ak.Attest(AttestOpts{Nonce: []byte("some nonce"), CertChainFetcher: localClient}) |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("Attest returned with error: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
// Expect one cert retrieved. |
| 51 |
if len(attestation.IntermediateCerts) != 1 { |
| 52 |
t.Fatalf("Got %v intermediate certs, want 1.", len(attestation.IntermediateCerts)) |
| 53 |
} |
| 54 |
|
| 55 |
if !bytes.Equal(attestation.IntermediateCerts[0], testCA.Raw) { |
| 56 |
t.Errorf("Attestation does not contain the expected intermediate cert: got %v, want %v", attestation.IntermediateCerts[0], testCA.Raw) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestKeyAttestGetCertificateChainConditions(t *testing.T) { |
| 61 |
rwc := test.GetTPM(t) |
| 62 |
defer CheckedClose(t, rwc) |
| 63 |
|
| 64 |
ak, err := AttestationKeyRSA(rwc) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("Failed to generate test AK: %v", err) |
| 67 |
} |
| 68 |
|
| 69 |
akCert, _ := test.GetTestCert(t, nil, nil, nil) |
| 70 |
|
| 71 |
testcases := []struct { |
| 72 |
name string |
| 73 |
fetchCertChainClient *http.Client |
| 74 |
cert *x509.Certificate |
| 75 |
}{ |
| 76 |
{ |
| 77 |
name: "CertChainFetcher is nil", |
| 78 |
fetchCertChainClient: nil, |
| 79 |
cert: nil, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "CertChainFetcher is present, key.cert is nil", |
| 83 |
fetchCertChainClient: localClient, |
| 84 |
cert: nil, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "CertChainFetcher is present, key.cert has nil IssuingCertificateURL", |
| 88 |
fetchCertChainClient: localClient, |
| 89 |
cert: akCert, |
| 90 |
}, |
| 91 |
} |
| 92 |
|
| 93 |
for _, tc := range testcases { |
| 94 |
t.Run(tc.name, func(t *testing.T) { |
| 95 |
ak.cert = tc.cert |
| 96 |
|
| 97 |
att, err := ak.Attest(AttestOpts{Nonce: []byte("some nonce"), CertChainFetcher: tc.fetchCertChainClient}) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("Attest returned error: %v", err) |
| 100 |
} |
| 101 |
|
| 102 |
if len(att.IntermediateCerts) != 0 { |
| 103 |
t.Errorf("Attest() returned with intermediate certs, expected no certs retrieved.") |
| 104 |
} |
| 105 |
}) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestSevSnpQuoteProvider(t *testing.T) { |
| 110 |
rwc := test.GetTPM(t) |
| 111 |
defer CheckedClose(t, rwc) |
| 112 |
|
| 113 |
ak, err := AttestationKeyRSA(rwc) |
| 114 |
if err != nil { |
| 115 |
t.Fatalf("Failed to generate test AK: %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
someNonce := []byte("some nonce") |
| 119 |
var someNonce64 [64]byte |
| 120 |
copy(someNonce64[:], someNonce) |
| 121 |
var nonce64 [64]byte |
| 122 |
copy(nonce64[:], []byte("noncey business")) |
| 123 |
sevTestQp, _, _, _ := testclient.GetSevQuoteProvider([]sgtest.TestCase{ |
| 124 |
{ |
| 125 |
Input: someNonce64, |
| 126 |
Output: sgtest.TestRawReport(someNonce64), |
| 127 |
}, |
| 128 |
{ |
| 129 |
Input: nonce64, |
| 130 |
Output: sgtest.TestRawReport(nonce64), |
| 131 |
}, |
| 132 |
}, &sgtest.DeviceOptions{Now: time.Now()}, t) |
| 133 |
|
| 134 |
testcases := []struct { |
| 135 |
name string |
| 136 |
opts AttestOpts |
| 137 |
wantReportData [64]byte |
| 138 |
wantErr string |
| 139 |
}{ |
| 140 |
{ |
| 141 |
name: "Happy case no nonce", |
| 142 |
opts: AttestOpts{ |
| 143 |
Nonce: someNonce, |
| 144 |
CertChainFetcher: localClient, |
| 145 |
TEEDevice: &SevSnpQuoteProvider{sevTestQp}, |
| 146 |
}, |
| 147 |
wantReportData: someNonce64, |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "Happy case with nonce", |
| 151 |
opts: AttestOpts{ |
| 152 |
Nonce: someNonce, |
| 153 |
CertChainFetcher: localClient, |
| 154 |
TEEDevice: &SevSnpQuoteProvider{sevTestQp}, |
| 155 |
TEENonce: nonce64[:], |
| 156 |
}, |
| 157 |
wantReportData: nonce64, |
| 158 |
}, |
| 159 |
{ |
| 160 |
name: "TEE nonce without TEE", |
| 161 |
opts: AttestOpts{ |
| 162 |
Nonce: someNonce, |
| 163 |
CertChainFetcher: localClient, |
| 164 |
TEENonce: nonce64[:], |
| 165 |
}, |
| 166 |
wantErr: "got non-nil TEENonce when TEEDevice is nil", |
| 167 |
}, |
| 168 |
} |
| 169 |
for _, tc := range testcases { |
| 170 |
t.Run(tc.name, func(t *testing.T) { |
| 171 |
att, err := ak.Attest(tc.opts) |
| 172 |
if (err == nil && tc.wantErr != "") || (err != nil && !strings.Contains(err.Error(), tc.wantErr)) { |
| 173 |
t.Fatalf("Attest(%v) = %v, want %q", tc.opts, err, tc.wantErr) |
| 174 |
} |
| 175 |
// Successful attestation should include a SEV-SNP attestation. |
| 176 |
if err == nil { |
| 177 |
snp, ok := att.GetTeeAttestation().(*pb.Attestation_SevSnpAttestation) |
| 178 |
if !ok { |
| 179 |
t.Fatalf("Attestation missing SEV-SNP attestation: %v", att.GetTeeAttestation()) |
| 180 |
} |
| 181 |
report := snp.SevSnpAttestation.Report |
| 182 |
if !bytes.Equal(report.GetReportData(), tc.wantReportData[:]) { |
| 183 |
t.Fatalf("SEV-SNP nonces differ. Got %v, want %v", report.GetReportData(), tc.wantReportData) |
| 184 |
} |
| 185 |
} |
| 186 |
}) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestTdxDevice(t *testing.T) { |
| 191 |
rwc := test.GetTPM(t) |
| 192 |
defer CheckedClose(t, rwc) |
| 193 |
|
| 194 |
ak, err := AttestationKeyRSA(rwc) |
| 195 |
if err != nil { |
| 196 |
t.Fatalf("Failed to generate test AK: %v", err) |
| 197 |
} |
| 198 |
|
| 199 |
someNonce := []byte("some nonce") |
| 200 |
var someNonce64 [64]byte |
| 201 |
copy(someNonce64[:], someNonce) |
| 202 |
var nonce64 [64]byte |
| 203 |
copy(nonce64[:], []byte("noncey business")) |
| 204 |
tdxTestDevice := tgtestclient.GetTdxGuest([]tgtest.TestCase{ |
| 205 |
{ |
| 206 |
Input: someNonce64, |
| 207 |
Quote: tgtestdata.RawQuote, |
| 208 |
}, |
| 209 |
{ |
| 210 |
Input: nonce64, |
| 211 |
Quote: tgtestdata.RawQuote, |
| 212 |
}, |
| 213 |
}, t) |
| 214 |
defer tdxTestDevice.Close() |
| 215 |
|
| 216 |
testcases := []struct { |
| 217 |
name string |
| 218 |
opts AttestOpts |
| 219 |
wantReportData [64]byte |
| 220 |
wantErr string |
| 221 |
}{ |
| 222 |
{ |
| 223 |
name: "Happy case no nonce", |
| 224 |
opts: AttestOpts{ |
| 225 |
Nonce: someNonce, |
| 226 |
TEEDevice: &TdxDevice{tdxTestDevice}, |
| 227 |
}, |
| 228 |
wantReportData: someNonce64, |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "Happy case with nonce", |
| 232 |
opts: AttestOpts{ |
| 233 |
Nonce: someNonce, |
| 234 |
TEEDevice: &TdxDevice{tdxTestDevice}, |
| 235 |
TEENonce: nonce64[:], |
| 236 |
}, |
| 237 |
wantReportData: nonce64, |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "TEE nonce without TEE", |
| 241 |
opts: AttestOpts{ |
| 242 |
Nonce: someNonce, |
| 243 |
TEENonce: nonce64[:], |
| 244 |
}, |
| 245 |
wantErr: "got non-nil TEENonce when TEEDevice is nil", |
| 246 |
}, |
| 247 |
} |
| 248 |
for _, tc := range testcases { |
| 249 |
t.Run(tc.name, func(t *testing.T) { |
| 250 |
att, err := ak.Attest(tc.opts) |
| 251 |
if (err == nil && tc.wantErr != "") || (err != nil && !strings.Contains(err.Error(), tc.wantErr)) { |
| 252 |
t.Fatalf("Attest(%v) = %v, want %q", tc.opts, err, tc.wantErr) |
| 253 |
} |
| 254 |
// Successful attestation should include a TDX attestation. |
| 255 |
if err == nil { |
| 256 |
_, ok := att.GetTeeAttestation().(*pb.Attestation_TdxAttestation) |
| 257 |
if !ok { |
| 258 |
t.Fatalf("Attestation missing TDX attestation: %v", att.GetTeeAttestation()) |
| 259 |
} |
| 260 |
} |
| 261 |
}) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestTdxQuoteProvider(t *testing.T) { |
| 266 |
rwc := test.GetTPM(t) |
| 267 |
defer CheckedClose(t, rwc) |
| 268 |
|
| 269 |
ak, err := AttestationKeyRSA(rwc) |
| 270 |
if err != nil { |
| 271 |
t.Fatalf("Failed to generate test AK: %v", err) |
| 272 |
} |
| 273 |
|
| 274 |
someNonce := []byte("some nonce") |
| 275 |
var someNonce64 [64]byte |
| 276 |
copy(someNonce64[:], someNonce) |
| 277 |
var nonce64 [64]byte |
| 278 |
copy(nonce64[:], []byte("noncey business")) |
| 279 |
mockTdxQuoteProvider := tgtestclient.GetMockTdxQuoteProvider([]tgtest.TestCase{ |
| 280 |
{ |
| 281 |
Input: someNonce64, |
| 282 |
Quote: tgtestdata.RawQuote, |
| 283 |
}, |
| 284 |
{ |
| 285 |
Input: nonce64, |
| 286 |
Quote: tgtestdata.RawQuote, |
| 287 |
}, |
| 288 |
}, t) |
| 289 |
|
| 290 |
testcases := []struct { |
| 291 |
name string |
| 292 |
opts AttestOpts |
| 293 |
wantReportData [64]byte |
| 294 |
wantErr string |
| 295 |
}{ |
| 296 |
{ |
| 297 |
name: "Happy case no nonce", |
| 298 |
opts: AttestOpts{ |
| 299 |
Nonce: someNonce, |
| 300 |
TEEDevice: &TdxQuoteProvider{mockTdxQuoteProvider}, |
| 301 |
}, |
| 302 |
wantReportData: someNonce64, |
| 303 |
}, |
| 304 |
{ |
| 305 |
name: "Happy case with nonce", |
| 306 |
opts: AttestOpts{ |
| 307 |
Nonce: someNonce, |
| 308 |
TEEDevice: &TdxQuoteProvider{mockTdxQuoteProvider}, |
| 309 |
TEENonce: nonce64[:], |
| 310 |
}, |
| 311 |
wantReportData: nonce64, |
| 312 |
}, |
| 313 |
{ |
| 314 |
name: "TEE nonce without TEE", |
| 315 |
opts: AttestOpts{ |
| 316 |
Nonce: someNonce, |
| 317 |
TEENonce: nonce64[:], |
| 318 |
}, |
| 319 |
wantErr: "got non-nil TEENonce when TEEDevice is nil", |
| 320 |
}, |
| 321 |
} |
| 322 |
for _, tc := range testcases { |
| 323 |
t.Run(tc.name, func(t *testing.T) { |
| 324 |
att, err := ak.Attest(tc.opts) |
| 325 |
if (err == nil && tc.wantErr != "") || (err != nil && !strings.Contains(err.Error(), tc.wantErr)) { |
| 326 |
t.Fatalf("Attest(%v) = %v, want %q", tc.opts, err, tc.wantErr) |
| 327 |
} |
| 328 |
// Successful attestation should include a TDX attestation. |
| 329 |
if err == nil { |
| 330 |
_, ok := att.GetTeeAttestation().(*pb.Attestation_TdxAttestation) |
| 331 |
if !ok { |
| 332 |
t.Fatalf("Attestation missing TDX attestation: %v", att.GetTeeAttestation()) |
| 333 |
} |
| 334 |
} |
| 335 |
}) |
| 336 |
} |
| 337 |
} |