| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
tgtest "github.com/google/go-tdx-guest/testing" |
| 11 |
tgtestclient "github.com/google/go-tdx-guest/testing/client" |
| 12 |
tgtestdata "github.com/google/go-tdx-guest/testing/testdata" |
| 13 |
"github.com/google/go-tpm-tools/client" |
| 14 |
"github.com/google/go-tpm-tools/internal/test" |
| 15 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 16 |
"github.com/google/go-tpm-tools/verifier/util" |
| 17 |
"github.com/google/go-tpm/legacy/tpm2" |
| 18 |
"github.com/google/go-tpm/tpmutil" |
| 19 |
"google.golang.org/protobuf/proto" |
| 20 |
) |
| 21 |
|
| 22 |
func TestVerifyNoncePass(t *testing.T) { |
| 23 |
rwc := test.GetTPM(t) |
| 24 |
defer client.CheckedClose(t, rwc) |
| 25 |
ExternalTPM = rwc |
| 26 |
|
| 27 |
file1 := makeOutputFile(t, "attest") |
| 28 |
file2 := makeOutputFile(t, "verify") |
| 29 |
defer os.RemoveAll(file1) |
| 30 |
defer os.RemoveAll(file2) |
| 31 |
|
| 32 |
RootCmd.SetArgs([]string{"attest", "--nonce", "1234", "--key", "AK", "--tee-nonce", "", "--output", file1, "--tee-technology", ""}) |
| 33 |
if err := RootCmd.Execute(); err != nil { |
| 34 |
t.Error(err) |
| 35 |
} |
| 36 |
|
| 37 |
RootCmd.SetArgs([]string{"verify", "debug", "--nonce", "1234", "--input", file1, "--output", file2}) |
| 38 |
if err := RootCmd.Execute(); err != nil { |
| 39 |
t.Error(err) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestVerifyNonceFail(t *testing.T) { |
| 44 |
rwc := test.GetTPM(t) |
| 45 |
defer client.CheckedClose(t, rwc) |
| 46 |
ExternalTPM = rwc |
| 47 |
|
| 48 |
file1 := makeOutputFile(t, "attest") |
| 49 |
file2 := makeOutputFile(t, "verify") |
| 50 |
defer os.RemoveAll(file1) |
| 51 |
defer os.RemoveAll(file2) |
| 52 |
|
| 53 |
RootCmd.SetArgs([]string{"attest", "--nonce", "1234", "--output", file1}) |
| 54 |
if err := RootCmd.Execute(); err != nil { |
| 55 |
t.Error(err) |
| 56 |
} |
| 57 |
|
| 58 |
RootCmd.SetArgs([]string{"verify", "debug", "--nonce", "4321", "--input", file1, "--output", file2}) |
| 59 |
if err := RootCmd.Execute(); err == nil { |
| 60 |
t.Error("expected non-nil error") |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestVerifyWithGCEAK(t *testing.T) { |
| 65 |
rwc := test.GetTPM(t) |
| 66 |
defer client.CheckedClose(t, rwc) |
| 67 |
ExternalTPM = rwc |
| 68 |
|
| 69 |
file1 := makeOutputFile(t, "attest") |
| 70 |
file2 := makeOutputFile(t, "verify") |
| 71 |
defer os.RemoveAll(file1) |
| 72 |
defer os.RemoveAll(file2) |
| 73 |
|
| 74 |
var template = map[string]tpm2.Public{ |
| 75 |
"rsa": GCEAKTemplateRSA(), |
| 76 |
"ecc": GCEAKTemplateECC(), |
| 77 |
} |
| 78 |
tests := []struct { |
| 79 |
name string |
| 80 |
nonce string |
| 81 |
keyAlgo string |
| 82 |
}{ |
| 83 |
{"gceAK:RSA", "1234", "rsa"}, |
| 84 |
{"gceAK:ECC", "1234", "ecc"}, |
| 85 |
} |
| 86 |
for _, op := range tests { |
| 87 |
t.Run(op.name, func(t *testing.T) { |
| 88 |
data, err := template[op.keyAlgo].Encode() |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("failed to encode GCEAKTemplateRSA: %v", err) |
| 91 |
} |
| 92 |
err = setGCEAKTemplate(t, rwc, op.keyAlgo, data) |
| 93 |
if err != nil { |
| 94 |
t.Error(err) |
| 95 |
} |
| 96 |
defer tpm2.NVUndefineSpace(rwc, "", tpm2.HandlePlatform, tpmutil.Handle(getIndex[op.keyAlgo])) |
| 97 |
|
| 98 |
var dummyInstance = util.Instance{ProjectID: "test-project", ProjectNumber: "1922337278274", Zone: "us-central-1a", InstanceID: "12345678", InstanceName: "default"} |
| 99 |
mock, err := util.NewMetadataServer(dummyInstance) |
| 100 |
if err != nil { |
| 101 |
t.Error(err) |
| 102 |
} |
| 103 |
defer mock.Stop() |
| 104 |
|
| 105 |
RootCmd.SetArgs([]string{"attest", "--nonce", op.nonce, "--key", "gceAK", "--algo", op.keyAlgo, "--output", file1, "--format", "binarypb", "--tee-technology", ""}) |
| 106 |
if err := RootCmd.Execute(); err != nil { |
| 107 |
t.Error(err) |
| 108 |
} |
| 109 |
|
| 110 |
RootCmd.SetArgs([]string{"verify", "debug", "--nonce", op.nonce, "--input", file1, "--output", file2}) |
| 111 |
if err := RootCmd.Execute(); err != nil { |
| 112 |
t.Error(err) |
| 113 |
} |
| 114 |
}) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestHwAttestationPass(t *testing.T) { |
| 119 |
rwc := test.GetTPM(t) |
| 120 |
defer client.CheckedClose(t, rwc) |
| 121 |
ExternalTPM = rwc |
| 122 |
|
| 123 |
inputFile := makeOutputFile(t, "attest") |
| 124 |
outputFile := makeOutputFile(t, "attestout") |
| 125 |
defer os.RemoveAll(inputFile) |
| 126 |
defer os.RemoveAll(outputFile) |
| 127 |
teenonce := "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678" |
| 128 |
tests := []struct { |
| 129 |
name string |
| 130 |
nonce string |
| 131 |
teetech string |
| 132 |
wanterr string |
| 133 |
}{ |
| 134 |
{"TdxPass", "1234", "tdx", "failed to create tdx quote provider"}, |
| 135 |
{"SevSnpPass", "1234", "sev-snp", "failed to open sev-snp device"}, |
| 136 |
} |
| 137 |
for _, op := range tests { |
| 138 |
t.Run(op.name, func(t *testing.T) { |
| 139 |
attestArgs := []string{"attest", "--nonce", op.nonce, "--key", "AK", "--output", inputFile, "--format", "textproto", "--tee-nonce", teenonce, "--tee-technology", op.teetech} |
| 140 |
RootCmd.SetArgs(attestArgs) |
| 141 |
if err := RootCmd.Execute(); err != nil { |
| 142 |
if !strings.Contains(err.Error(), op.wanterr) { |
| 143 |
t.Error(err) |
| 144 |
} |
| 145 |
} else { |
| 146 |
RootCmd.SetArgs([]string{"verify", "debug", "--nonce", op.nonce, "--input", inputFile, "--output", outputFile, "--format", "textproto", "--tee-nonce", teenonce}) |
| 147 |
if err := RootCmd.Execute(); err != nil { |
| 148 |
t.Error(err) |
| 149 |
} |
| 150 |
msBytes, err := os.ReadFile(outputFile) |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("failed to read file: %v", err) |
| 153 |
} |
| 154 |
ms := &pb.MachineState{} |
| 155 |
err = proto.Unmarshal(msBytes, ms) |
| 156 |
if err != nil { |
| 157 |
t.Fatalf("failed to unmarshal proto: %v", err) |
| 158 |
} |
| 159 |
if ms.TeeAttestation == nil { |
| 160 |
t.Error("found nil TEE attestation, expected a set TEEattestation") |
| 161 |
} |
| 162 |
} |
| 163 |
}) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestTdxAttestation(t *testing.T) { |
| 168 |
dir := t.TempDir() |
| 169 |
file1, err := os.Create(dir + "/attestFile") |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
file2 := makeOutputFile(t, "verifyFile") |
| 174 |
defer os.RemoveAll(file2) |
| 175 |
tpmNonce := "1234" |
| 176 |
teeNonce := hex.EncodeToString(test.TdxReportData) |
| 177 |
wrongTeeNonce := hex.EncodeToString([]byte("wrongTdxNonce")) |
| 178 |
attestation, err := createAttestationWithFakeTdx([]byte(tpmNonce), test.TdxReportData, t) |
| 179 |
if err != nil { |
| 180 |
t.Fatal(err) |
| 181 |
} |
| 182 |
out := []byte(marshalOptions.Format(attestation)) |
| 183 |
file1.Write(out) |
| 184 |
hexTpmNonce := hex.EncodeToString([]byte(tpmNonce)) |
| 185 |
tests := []struct { |
| 186 |
name string |
| 187 |
tdxNonce string |
| 188 |
wantErr string |
| 189 |
}{ |
| 190 |
{"Correct TEE Nonce", teeNonce, ""}, |
| 191 |
{"Incorrect TEE Nonce", wrongTeeNonce, "quote field REPORT_DATA"}, |
| 192 |
{"Incorrect Nonce Using TPM Nonce", wrongTeeNonce, "quote field REPORT_DATA"}, |
| 193 |
} |
| 194 |
|
| 195 |
for _, op := range tests { |
| 196 |
t.Run(op.name, func(t *testing.T) { |
| 197 |
RootCmd.SetArgs([]string{"verify", "debug", "--nonce", hexTpmNonce, "--input", file1.Name(), "--output", file2, "--tee-nonce", op.tdxNonce, "--format", "textproto"}) |
| 198 |
if err := RootCmd.Execute(); (err == nil && op.wantErr != "") || |
| 199 |
(err != nil && !strings.Contains(err.Error(), op.wantErr)) { |
| 200 |
t.Errorf("Expected error: %v, got: %v", op.wantErr, err) |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func createAttestationWithFakeTdx(tpmNonce []byte, teeNonce []byte, tb *testing.T) (*pb.Attestation, error) { |
| 207 |
tdxEventLog := test.CreateTpm2EventLog(3) // Enum 3- TDX |
| 208 |
rwc := test.GetSimulatorWithLog(tb, tdxEventLog) |
| 209 |
defer client.CheckedClose(tb, rwc) |
| 210 |
ak, err := client.AttestationKeyRSA(rwc) |
| 211 |
if err != nil { |
| 212 |
return nil, fmt.Errorf("failed to generate AK: %v", err) |
| 213 |
} |
| 214 |
defer ak.Close() |
| 215 |
var teeNonce64 [64]byte |
| 216 |
copy(teeNonce64[:], teeNonce) |
| 217 |
tdxTestDevice := tgtestclient.GetTdxGuest([]tgtest.TestCase{ |
| 218 |
{ |
| 219 |
Input: teeNonce64, |
| 220 |
Quote: tgtestdata.RawQuote, |
| 221 |
}, |
| 222 |
}, tb) |
| 223 |
|
| 224 |
defer tdxTestDevice.Close() |
| 225 |
attestation, err := ak.Attest(client.AttestOpts{ |
| 226 |
Nonce: tpmNonce, |
| 227 |
TEEDevice: &client.TdxDevice{Device: tdxTestDevice}, |
| 228 |
TEENonce: teeNonce64[:], |
| 229 |
}) |
| 230 |
if err != nil { |
| 231 |
return nil, fmt.Errorf("failed to attest: %v", err) |
| 232 |
} |
| 233 |
return attestation, nil |
| 234 |
} |