| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"os" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
sgtest "github.com/google/go-sev-guest/testing" |
| 12 |
sgtestclient "github.com/google/go-sev-guest/testing/client" |
| 13 |
tgtest "github.com/google/go-tdx-guest/testing" |
| 14 |
tgtestclient "github.com/google/go-tdx-guest/testing/client" |
| 15 |
"github.com/google/go-tpm-tools/client" |
| 16 |
"github.com/google/go-tpm-tools/internal/test" |
| 17 |
"github.com/google/go-tpm-tools/verifier/util" |
| 18 |
"github.com/google/go-tpm/legacy/tpm2" |
| 19 |
"github.com/google/go-tpm/tpmutil" |
| 20 |
) |
| 21 |
|
| 22 |
var getIndex = map[string]uint32{ |
| 23 |
"rsa": client.GceAKTemplateNVIndexRSA, |
| 24 |
"ecc": client.GceAKTemplateNVIndexECC, |
| 25 |
} |
| 26 |
|
| 27 |
func GCEAKTemplateECC() tpm2.Public { |
| 28 |
return tpm2.Public{ |
| 29 |
Type: tpm2.AlgECC, |
| 30 |
NameAlg: tpm2.AlgSHA256, |
| 31 |
Attributes: tpm2.FlagSignerDefault, |
| 32 |
ECCParameters: &tpm2.ECCParams{ |
| 33 |
Sign: &tpm2.SigScheme{ |
| 34 |
Alg: tpm2.AlgECDSA, |
| 35 |
Hash: tpm2.AlgSHA256, |
| 36 |
}, |
| 37 |
CurveID: 3, |
| 38 |
}, |
| 39 |
} |
| 40 |
} |
| 41 |
func GCEAKTemplateRSA() tpm2.Public { |
| 42 |
return tpm2.Public{ |
| 43 |
Type: tpm2.AlgRSA, |
| 44 |
NameAlg: tpm2.AlgSHA256, |
| 45 |
Attributes: tpm2.FlagSignerDefault, |
| 46 |
RSAParameters: &tpm2.RSAParams{ |
| 47 |
Sign: &tpm2.SigScheme{ |
| 48 |
Alg: tpm2.AlgRSASSA, |
| 49 |
Hash: tpm2.AlgSHA256, |
| 50 |
}, |
| 51 |
KeyBits: 2048, |
| 52 |
}, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Need to call tpm2.NVUndefinespace on the handle with authHandle tpm2.HandlePlatform. |
| 57 |
// e.g defer tpm2.NVUndefineSpace(rwc, "", tpm2.HandlePlatform, tpmutil.Handle(client.GceAKTemplateNVIndexRSA)) |
| 58 |
func setGCEAKTemplate(tb testing.TB, rwc io.ReadWriteCloser, algo string, data []byte) error { |
| 59 |
// Since this mutates the TPM, any tests using real TPMs must skip. |
| 60 |
test.SkipForRealTPM(tb) |
| 61 |
var err error |
| 62 |
idx := tpmutil.Handle(getIndex[algo]) |
| 63 |
if err := tpm2.NVDefineSpace(rwc, tpm2.HandlePlatform, idx, |
| 64 |
"", "", nil, |
| 65 |
tpm2.AttrPPWrite|tpm2.AttrPPRead|tpm2.AttrWriteDefine|tpm2.AttrOwnerRead|tpm2.AttrAuthRead|tpm2.AttrPlatformCreate|tpm2.AttrNoDA, |
| 66 |
uint16(len(data))); err != nil { |
| 67 |
tb.Fatalf("NVDefineSpace failed: %v", err) |
| 68 |
} |
| 69 |
err = tpm2.NVWrite(rwc, tpm2.HandlePlatform, idx, "", data, 0) |
| 70 |
if err != nil { |
| 71 |
tb.Fatalf("failed to write NVIndex: %v", err) |
| 72 |
} |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
func makeOutputFile(tb testing.TB, output string) string { |
| 77 |
tb.Helper() |
| 78 |
file, err := os.CreateTemp("", output) |
| 79 |
if err != nil { |
| 80 |
tb.Fatal(err) |
| 81 |
} |
| 82 |
defer file.Close() |
| 83 |
return file.Name() |
| 84 |
} |
| 85 |
|
| 86 |
func TestNonce(t *testing.T) { |
| 87 |
rwc := test.GetTPM(t) |
| 88 |
defer client.CheckedClose(t, rwc) |
| 89 |
ExternalTPM = rwc |
| 90 |
// Without nonce. |
| 91 |
RootCmd.SetArgs([]string{"attest", "--key", "AK"}) |
| 92 |
if err := RootCmd.Execute(); err == nil { |
| 93 |
t.Error("expected not-nil error") |
| 94 |
} |
| 95 |
// With odd length nonce. |
| 96 |
RootCmd.SetArgs([]string{"attest", "--nonce", "12345", "--key", "AK"}) |
| 97 |
if err := RootCmd.Execute(); err == nil { |
| 98 |
t.Error("expected not-nil error") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestAttestPass(t *testing.T) { |
| 103 |
rwc := test.GetTPM(t) |
| 104 |
defer client.CheckedClose(t, rwc) |
| 105 |
ExternalTPM = rwc |
| 106 |
tests := []struct { |
| 107 |
name string |
| 108 |
key string |
| 109 |
algo string |
| 110 |
nonce string |
| 111 |
}{ |
| 112 |
{"defaultKey", "", "rsa", "1234"}, |
| 113 |
{"AKWithRSA", "AK", "rsa", "2222"}, |
| 114 |
{"AKWithECC", "AK", "ecc", "2222"}, |
| 115 |
} |
| 116 |
for _, op := range tests { |
| 117 |
t.Run(op.name, func(t *testing.T) { |
| 118 |
secretFile1 := makeOutputFile(t, "attest") |
| 119 |
defer os.RemoveAll(secretFile1) |
| 120 |
attestArgs := []string{"attest", "--output", secretFile1} |
| 121 |
if op.key != "" { |
| 122 |
attestArgs = append(attestArgs, "--key", op.key) |
| 123 |
} |
| 124 |
if op.algo != "" { |
| 125 |
attestArgs = append(attestArgs, "--algo", op.algo) |
| 126 |
} |
| 127 |
if op.nonce != "" { |
| 128 |
attestArgs = append(attestArgs, "--nonce", op.nonce) |
| 129 |
} |
| 130 |
RootCmd.SetArgs(attestArgs) |
| 131 |
if err := RootCmd.Execute(); err != nil { |
| 132 |
t.Error(err) |
| 133 |
} |
| 134 |
}) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestFormatFlagPass(t *testing.T) { |
| 139 |
rwc := test.GetTPM(t) |
| 140 |
defer client.CheckedClose(t, rwc) |
| 141 |
ExternalTPM = rwc |
| 142 |
inputFile := makeOutputFile(t, "attestXYZQ") |
| 143 |
outputFile := makeOutputFile(t, "attestout") |
| 144 |
defer os.RemoveAll(inputFile) |
| 145 |
defer os.RemoveAll(outputFile) |
| 146 |
tests := []struct { |
| 147 |
name string |
| 148 |
nonce string |
| 149 |
report string |
| 150 |
verifiedReport string |
| 151 |
format string |
| 152 |
}{ |
| 153 |
{"Format:binary", "abcd", inputFile, outputFile, "binarypb"}, |
| 154 |
{"Format:textproto", "abcd", inputFile, outputFile, "textproto"}, |
| 155 |
} |
| 156 |
for _, op := range tests { |
| 157 |
t.Run(op.name, func(t *testing.T) { |
| 158 |
attestArgs := []string{"attest", "--nonce", op.nonce, "--output", op.report, "--format", op.format} |
| 159 |
RootCmd.SetArgs(attestArgs) |
| 160 |
if err := RootCmd.Execute(); err != nil { |
| 161 |
t.Error(err) |
| 162 |
} |
| 163 |
debugArgs := []string{"verify", "debug", "--nonce", op.nonce, "--input", op.report, "--output", op.verifiedReport, "--format", op.format} |
| 164 |
RootCmd.SetArgs(debugArgs) |
| 165 |
if err := RootCmd.Execute(); err != nil { |
| 166 |
t.Error(err) |
| 167 |
} |
| 168 |
}) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func TestFormatFlagFail(t *testing.T) { |
| 173 |
rwc := test.GetTPM(t) |
| 174 |
defer client.CheckedClose(t, rwc) |
| 175 |
ExternalTPM = rwc |
| 176 |
inputFile := makeOutputFile(t, "attest") |
| 177 |
outputFile := makeOutputFile(t, "attestout") |
| 178 |
defer os.RemoveAll(inputFile) |
| 179 |
defer os.RemoveAll(outputFile) |
| 180 |
tests := []struct { |
| 181 |
name string |
| 182 |
nonce string |
| 183 |
report string |
| 184 |
verifiedReport string |
| 185 |
formatAttest string |
| 186 |
formatDebug string |
| 187 |
}{ |
| 188 |
{"Format:binary", "abcd", inputFile, outputFile, "binarypb", "textproto"}, |
| 189 |
{"Format:textproto", "abcd", inputFile, outputFile, "textproto", "binarypb"}, |
| 190 |
{"Format:textproto", "abcd", inputFile, outputFile, "textproto", "xyz"}, |
| 191 |
} |
| 192 |
for _, op := range tests { |
| 193 |
t.Run(op.name, func(t *testing.T) { |
| 194 |
attestArgs := []string{"attest", "--nonce", op.nonce, "--output", op.report, "--format", op.formatAttest} |
| 195 |
RootCmd.SetArgs(attestArgs) |
| 196 |
if err := RootCmd.Execute(); err != nil { |
| 197 |
t.Error(err) |
| 198 |
} |
| 199 |
debugArgs := []string{"verify", "debug", "--nonce", op.nonce, "--input", op.report, "--output", op.verifiedReport, "--format", op.formatDebug} |
| 200 |
RootCmd.SetArgs(debugArgs) |
| 201 |
if err := RootCmd.Execute(); err == nil { |
| 202 |
t.Error(err) |
| 203 |
} |
| 204 |
}) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestMetadataPass(t *testing.T) { |
| 209 |
var dummyInstance = util.Instance{ProjectID: "test-project", ProjectNumber: "1922337278274", Zone: "us-central-1a", InstanceID: "12345678", InstanceName: "default"} |
| 210 |
mock, err := util.NewMetadataServer(dummyInstance) |
| 211 |
if err != nil { |
| 212 |
t.Error(err) |
| 213 |
} |
| 214 |
defer mock.Stop() |
| 215 |
instanceInfo, err := getInstanceInfoFromMetadata() |
| 216 |
if err != nil { |
| 217 |
t.Error(err) |
| 218 |
} |
| 219 |
if instanceInfo.ProjectId != dummyInstance.ProjectID { |
| 220 |
t.Errorf("metadata.ProjectID() = %v, want %v", instanceInfo.ProjectId, dummyInstance.ProjectID) |
| 221 |
} |
| 222 |
projectNumber, err := strconv.ParseUint(dummyInstance.ProjectNumber, 10, 64) |
| 223 |
if err != nil { |
| 224 |
t.Error(err) |
| 225 |
} |
| 226 |
if instanceInfo.ProjectNumber != projectNumber { |
| 227 |
t.Errorf("metadata.NumericProjectID() = %v, want %v", instanceInfo.ProjectNumber, projectNumber) |
| 228 |
} |
| 229 |
if instanceInfo.InstanceName != dummyInstance.InstanceName { |
| 230 |
t.Errorf("metadata.InstanceName() = %v, want %v", instanceInfo.InstanceName, dummyInstance.InstanceName) |
| 231 |
} |
| 232 |
instanceID, err := strconv.ParseUint(dummyInstance.InstanceID, 10, 64) |
| 233 |
if err != nil { |
| 234 |
t.Error(err) |
| 235 |
} |
| 236 |
if instanceInfo.InstanceId != instanceID { |
| 237 |
t.Errorf("metadata.InstanceID() = %v, want %v", instanceInfo.InstanceId, instanceID) |
| 238 |
} |
| 239 |
if instanceInfo.Zone != dummyInstance.Zone { |
| 240 |
t.Errorf("metadata.Zone() = %v, want %v", instanceInfo.Zone, dummyInstance.Zone) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestAttestWithGCEAK(t *testing.T) { |
| 245 |
rwc := test.GetTPM(t) |
| 246 |
defer client.CheckedClose(t, rwc) |
| 247 |
ExternalTPM = rwc |
| 248 |
secretFile1 := makeOutputFile(t, "attest") |
| 249 |
defer os.RemoveAll(secretFile1) |
| 250 |
var template = map[string]tpm2.Public{ |
| 251 |
"rsa": GCEAKTemplateRSA(), |
| 252 |
"ecc": GCEAKTemplateECC(), |
| 253 |
} |
| 254 |
tests := []struct { |
| 255 |
name string |
| 256 |
nonce string |
| 257 |
keyAlgo string |
| 258 |
}{ |
| 259 |
{"gceAK:RSA", "1234", "rsa"}, |
| 260 |
{"gceAK:ECC", "1234", "ecc"}, |
| 261 |
} |
| 262 |
for _, op := range tests { |
| 263 |
t.Run(op.name, func(t *testing.T) { |
| 264 |
data, err := template[op.keyAlgo].Encode() |
| 265 |
if err != nil { |
| 266 |
t.Fatalf("failed to encode GCEAKTemplateRSA: %v", err) |
| 267 |
} |
| 268 |
err = setGCEAKTemplate(t, rwc, op.keyAlgo, data) |
| 269 |
if err != nil { |
| 270 |
t.Error(err) |
| 271 |
} |
| 272 |
defer tpm2.NVUndefineSpace(rwc, "", tpm2.HandlePlatform, tpmutil.Handle(getIndex[op.keyAlgo])) |
| 273 |
|
| 274 |
var dummyInstance = util.Instance{ProjectID: "test-project", ProjectNumber: "1922337278274", Zone: "us-central-1a", InstanceID: "12345678", InstanceName: "default"} |
| 275 |
mock, err := util.NewMetadataServer(dummyInstance) |
| 276 |
if err != nil { |
| 277 |
t.Error(err) |
| 278 |
} |
| 279 |
defer mock.Stop() |
| 280 |
|
| 281 |
RootCmd.SetArgs([]string{"attest", "--nonce", op.nonce, "--key", "gceAK", "--algo", op.keyAlgo, "--output", secretFile1, "--format", "binarypb"}) |
| 282 |
if err := RootCmd.Execute(); err != nil { |
| 283 |
t.Error(err) |
| 284 |
} |
| 285 |
}) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func TestTeeTechnologyFail(t *testing.T) { |
| 290 |
rwc := test.GetTPM(t) |
| 291 |
defer client.CheckedClose(t, rwc) |
| 292 |
ExternalTPM = rwc |
| 293 |
|
| 294 |
// value of tee-technology flag should be sev-snp |
| 295 |
RootCmd.SetArgs([]string{"attest", "--nonce", "1234", "--key", "AK", "--tee-nonce", "12345678", "--tee-technology", "sev"}) |
| 296 |
if err := RootCmd.Execute(); err == nil { |
| 297 |
t.Error("expected not-nil error") |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
func TestSevAttestTeeNonceFail(t *testing.T) { |
| 302 |
rwc := test.GetTPM(t) |
| 303 |
defer client.CheckedClose(t, rwc) |
| 304 |
ExternalTPM = rwc |
| 305 |
// non-nil TEENonce when TEEDevice is nil |
| 306 |
RootCmd.SetArgs([]string{"attest", "--nonce", "1234", "--key", "AK", "--tee-nonce", "12345678", "--tee-technology", ""}) |
| 307 |
if err := RootCmd.Execute(); err == nil { |
| 308 |
t.Error("expected not-nil error") |
| 309 |
} |
| 310 |
|
| 311 |
// TEENonce with length less than 64 bytes. |
| 312 |
sevTestQp, _, _, _ := sgtestclient.GetSevQuoteProvider([]sgtest.TestCase{ |
| 313 |
{ |
| 314 |
Input: [64]byte{1, 2, 3, 4}, |
| 315 |
}, |
| 316 |
}, &sgtest.DeviceOptions{Now: time.Now()}, t) |
| 317 |
|
| 318 |
ak, err := client.AttestationKeyRSA(rwc) |
| 319 |
if err != nil { |
| 320 |
t.Error(err) |
| 321 |
} |
| 322 |
defer ak.Close() |
| 323 |
attestopts := client.AttestOpts{ |
| 324 |
Nonce: []byte{1, 2, 3, 4}, |
| 325 |
TEENonce: []byte{1, 2, 3, 4}, |
| 326 |
TEEDevice: &client.SevSnpQuoteProvider{QuoteProvider: sevTestQp}, |
| 327 |
} |
| 328 |
_, err = ak.Attest(attestopts) |
| 329 |
if err == nil { |
| 330 |
t.Error("expected non-nil error") |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
func TestTdxAttestTeeNonceFail(t *testing.T) { |
| 336 |
rwc := test.GetTPM(t) |
| 337 |
defer client.CheckedClose(t, rwc) |
| 338 |
ExternalTPM = rwc |
| 339 |
// non-nil TEENonce when TEEDevice is nil |
| 340 |
RootCmd.SetArgs([]string{"attest", "--nonce", "1234", "--key", "AK", "--tee-nonce", "12345678", "--tee-technology", ""}) |
| 341 |
if err := RootCmd.Execute(); err == nil { |
| 342 |
t.Error("expected not-nil error") |
| 343 |
} |
| 344 |
|
| 345 |
// TEENonce with length less than 64 bytes. |
| 346 |
mockTdxQuoteProvider := tgtestclient.GetMockTdxQuoteProvider([]tgtest.TestCase{ |
| 347 |
{ |
| 348 |
Input: [64]byte{1, 2, 3, 4}, |
| 349 |
}, |
| 350 |
}, t) |
| 351 |
|
| 352 |
ak, err := client.AttestationKeyRSA(rwc) |
| 353 |
if err != nil { |
| 354 |
t.Error(err) |
| 355 |
} |
| 356 |
defer ak.Close() |
| 357 |
attestopts := client.AttestOpts{ |
| 358 |
Nonce: []byte{1, 2, 3, 4}, |
| 359 |
TEENonce: []byte{1, 2, 3, 4}, |
| 360 |
TEEDevice: &client.TdxQuoteProvider{QuoteProvider: mockTdxQuoteProvider}, |
| 361 |
} |
| 362 |
_, err = ak.Attest(attestopts) |
| 363 |
if err == nil { |
| 364 |
t.Error("expected non-nil error") |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
func TestHardwareAttestationPass(t *testing.T) { |
| 369 |
rwc := test.GetTPM(t) |
| 370 |
defer client.CheckedClose(t, rwc) |
| 371 |
ExternalTPM = rwc |
| 372 |
|
| 373 |
inputFile := makeOutputFile(t, "attest") |
| 374 |
outputFile := makeOutputFile(t, "attestout") |
| 375 |
defer os.RemoveAll(inputFile) |
| 376 |
defer os.RemoveAll(outputFile) |
| 377 |
teenonce := "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678" |
| 378 |
tests := []struct { |
| 379 |
name string |
| 380 |
nonce string |
| 381 |
teetech string |
| 382 |
wanterr string |
| 383 |
}{ |
| 384 |
{"TdxPass", "1234", "tdx", "failed to create tdx quote provider"}, |
| 385 |
{"SevSnpPass", "1234", "sev-snp", "failed to open sev-snp device"}, |
| 386 |
} |
| 387 |
for _, op := range tests { |
| 388 |
t.Run(op.name, func(t *testing.T) { |
| 389 |
attestArgs := []string{"attest", "--nonce", op.nonce, "--output", inputFile, "--format", "textproto", "--tee-nonce", teenonce, "--tee-technology", op.teetech} |
| 390 |
RootCmd.SetArgs(attestArgs) |
| 391 |
if err := RootCmd.Execute(); err != nil { |
| 392 |
if !strings.Contains(err.Error(), op.wanterr) { |
| 393 |
t.Error(err) |
| 394 |
} |
| 395 |
} |
| 396 |
}) |
| 397 |
} |
| 398 |
} |