| 1 |
// Copyright (c) 2014, Google LLC All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package tpm |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto/rand" |
| 20 |
"crypto/rsa" |
| 21 |
"crypto/sha1" |
| 22 |
"crypto/x509" |
| 23 |
mathrand "math/rand" |
| 24 |
"os" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/google/go-tpm/tpmutil" |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
ownerAuthEnvVar = "TPM_OWNER_AUTH" |
| 32 |
srkAuthEnvVar = "TPM_SRK_AUTH" |
| 33 |
aikAuthEnvVar = "TPM_AIK_AUTH" |
| 34 |
tpmPathEnvVar = "TPM_PATH" |
| 35 |
) |
| 36 |
|
| 37 |
// getAuth looks in the environment variables to find a given auth input value. |
| 38 |
// If the environment variable is not present, then getAuth returns the |
| 39 |
// well-known auth value of 20 bytes of zeros. |
| 40 |
func getAuth(name string) Digest { |
| 41 |
var auth Digest |
| 42 |
authInput := os.Getenv(name) |
| 43 |
if authInput != "" { |
| 44 |
aa := sha1.Sum([]byte(authInput)) |
| 45 |
copy(auth[:], aa[:]) |
| 46 |
} |
| 47 |
return auth |
| 48 |
} |
| 49 |
|
| 50 |
func TestGetKeys(t *testing.T) { |
| 51 |
rwc := openTPMOrSkip(t) |
| 52 |
defer rwc.Close() |
| 53 |
|
| 54 |
handles, err := GetKeys(rwc) |
| 55 |
if err != nil { |
| 56 |
t.Fatal("Couldn't enumerate keys in the TPM:", err) |
| 57 |
} |
| 58 |
|
| 59 |
t.Logf("Got %d keys: % d\n", len(handles), handles) |
| 60 |
} |
| 61 |
|
| 62 |
func TestGetManufacturer(t *testing.T) { |
| 63 |
rwc := openTPMOrSkip(t) |
| 64 |
defer rwc.Close() |
| 65 |
|
| 66 |
vendorID, err := GetManufacturer(rwc) |
| 67 |
if err != nil { |
| 68 |
t.Fatal("Couldn't read VendorID from TPM:", err) |
| 69 |
} |
| 70 |
|
| 71 |
t.Logf("TPM VendorID: %v\n", vendorID) |
| 72 |
} |
| 73 |
|
| 74 |
func TestGetAlgs(t *testing.T) { |
| 75 |
rwc := openTPMOrSkip(t) |
| 76 |
defer rwc.Close() |
| 77 |
|
| 78 |
algs, err := GetAlgs(rwc) |
| 79 |
if err != nil { |
| 80 |
t.Fatalf("Couldn't read Algorithms: %v", err) |
| 81 |
} |
| 82 |
want := []Algorithm{AlgRSA, AlgSHA, AlgHMAC, AlgMGF1} |
| 83 |
outer: |
| 84 |
for _, alg := range want { |
| 85 |
for _, got := range algs { |
| 86 |
if got == alg { |
| 87 |
continue outer |
| 88 |
} |
| 89 |
t.Errorf("GetAlgs returned %v, which does not contain a required algorithm %v", algs, alg.String()) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestGetNVList(t *testing.T) { |
| 95 |
rwc := openTPMOrSkip(t) |
| 96 |
defer rwc.Close() |
| 97 |
|
| 98 |
nvList, err := GetNVList(rwc) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("Couldn't read NVList %v", err) |
| 101 |
} |
| 102 |
t.Logf("NVList is: %v", nvList) |
| 103 |
} |
| 104 |
|
| 105 |
func TestGetNVIndex(t *testing.T) { |
| 106 |
rwc := openTPMOrSkip(t) |
| 107 |
defer rwc.Close() |
| 108 |
|
| 109 |
nvList, err := GetNVList(rwc) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("Couldn't read NVList %v", err) |
| 112 |
} |
| 113 |
t.Logf("NVList: %v", nvList) |
| 114 |
var nvInfo []NVDataPublic |
| 115 |
for _, nvEntry := range nvList { |
| 116 |
index, err := GetNVIndex(rwc, nvEntry) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("Can't read NVDataPublic of index: %v with: %v", nvEntry, err) |
| 119 |
} |
| 120 |
nvInfo = append(nvInfo, *index) |
| 121 |
} |
| 122 |
t.Logf("NVIndices with Attributes:%v", nvInfo) |
| 123 |
} |
| 124 |
|
| 125 |
func TestPcrExtend(t *testing.T) { |
| 126 |
rwc := openTPMOrSkip(t) |
| 127 |
defer rwc.Close() |
| 128 |
|
| 129 |
var pcrValue [20]byte |
| 130 |
var value = "FFFFFFFFFFFFFFFFFFFF" |
| 131 |
copy(pcrValue[:], value) |
| 132 |
|
| 133 |
oldPcrValue, err := ReadPCR(rwc, 12) |
| 134 |
if err != nil { |
| 135 |
t.Fatal("Couldn't read PCR 12 from the TPM:", err) |
| 136 |
} |
| 137 |
|
| 138 |
newPcrValue, err := PcrExtend(rwc, 12, pcrValue) |
| 139 |
if err != nil { |
| 140 |
t.Fatal("Couldn't extend PCR 12 from the TPM:", err) |
| 141 |
} |
| 142 |
|
| 143 |
finalPcr := sha1.Sum(append(oldPcrValue, pcrValue[:]...)) |
| 144 |
|
| 145 |
if bytes.Equal(finalPcr[:], newPcrValue) { |
| 146 |
t.Logf("PCR are equal!\n") |
| 147 |
} else { |
| 148 |
t.Fatal("PCR are not equal! Test failed.\n") |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func TestReadEKCert(t *testing.T) { |
| 153 |
rwc := openTPMOrSkip(t) |
| 154 |
defer rwc.Close() |
| 155 |
|
| 156 |
ownAuth := getAuth(ownerAuthEnvVar) |
| 157 |
cert, err := ReadEKCert(rwc, ownAuth) |
| 158 |
if err != nil { |
| 159 |
t.Fatal("Unable to read EKCert from NVRAM:", err) |
| 160 |
} |
| 161 |
|
| 162 |
x509cert, err := x509.ParseCertificate(cert) |
| 163 |
if err != nil { |
| 164 |
t.Logf("Malformed certificate: %v\n", err) |
| 165 |
} else { |
| 166 |
t.Logf("Certificate: %v\n", x509cert) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestReadPCR(t *testing.T) { |
| 171 |
rwc := openTPMOrSkip(t) |
| 172 |
defer rwc.Close() |
| 173 |
|
| 174 |
res, err := ReadPCR(rwc, 18) |
| 175 |
if err != nil { |
| 176 |
t.Fatal("Couldn't read PCR 18 from the TPM:", err) |
| 177 |
} |
| 178 |
|
| 179 |
t.Logf("Got PCR 18 value % x\n", res) |
| 180 |
} |
| 181 |
|
| 182 |
func TestFetchPCRValues(t *testing.T) { |
| 183 |
rwc := openTPMOrSkip(t) |
| 184 |
defer rwc.Close() |
| 185 |
|
| 186 |
var mask pcrMask |
| 187 |
if err := mask.setPCR(17); err != nil { |
| 188 |
t.Fatal("Couldn't set PCR 17:", err) |
| 189 |
} |
| 190 |
|
| 191 |
pcrs, err := FetchPCRValues(rwc, []int{17}) |
| 192 |
if err != nil { |
| 193 |
t.Fatal("Couldn't get PCRs 17:", err) |
| 194 |
} |
| 195 |
|
| 196 |
comp, err := createPCRComposite(mask, pcrs) |
| 197 |
if err != nil { |
| 198 |
t.Fatal("Couldn't create PCR composite") |
| 199 |
} |
| 200 |
|
| 201 |
if len(comp) != 20 { |
| 202 |
t.Fatal("Invalid PCR composite") |
| 203 |
} |
| 204 |
|
| 205 |
var loc Locality |
| 206 |
_, err = createPCRInfoLong(loc, mask, pcrs) |
| 207 |
if err != nil { |
| 208 |
t.Fatal("Couldn't create a pcrInfoLong structure for these PCRs") |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestGetRandom(t *testing.T) { |
| 213 |
rwc := openTPMOrSkip(t) |
| 214 |
defer rwc.Close() |
| 215 |
|
| 216 |
// Try to get 16 bytes of randomness from the TPM. |
| 217 |
b, err := GetRandom(rwc, 16) |
| 218 |
if err != nil { |
| 219 |
t.Fatal("Couldn't get 16 bytes of randomness from the TPM:", err) |
| 220 |
} |
| 221 |
|
| 222 |
if len(b) != 16 { |
| 223 |
t.Fatal("Couldn't get 16 bytes of randomness from the TPM") |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestNVDefineSpace(t *testing.T) { |
| 228 |
|
| 229 |
rwc := openTPMOrSkip(t) |
| 230 |
defer rwc.Close() |
| 231 |
// Address reserved for testing as described in spec |
| 232 |
// TPM-Main-Part-2-TPM-Structures_v1.2_rev116_01032011, P.139 |
| 233 |
owner := getAuth(ownerAuthEnvVar) |
| 234 |
nvIndex := uint32(0x0000F004) |
| 235 |
pcrInfoRead, err := newPCRInfoShort(rwc, LocZero, []int{1}) |
| 236 |
if err != nil { |
| 237 |
t.Fatalf("Couldn't create PCRInfoRead in define space") |
| 238 |
} |
| 239 |
pcrInfoWrite, err := newPCRInfoShort(rwc, LocZero, []int{1}) |
| 240 |
if err != nil { |
| 241 |
t.Fatalf("Couldn't create PCRInfoWrite in define space") |
| 242 |
} |
| 243 |
nvAtt := &nvAttributes{tagNVAttributes, NVPerOwnerWrite} |
| 244 |
nvData := NVDataPublic{tagNVDataPublic, nvIndex, *pcrInfoRead, *pcrInfoWrite, *nvAtt, true, true, false, uint32(20)} |
| 245 |
|
| 246 |
err = NVDefineSpace(rwc, nvData, []byte(owner[:])) |
| 247 |
if err != nil { |
| 248 |
t.Fatalf("Couldn't define space in NV RAM %v", err) |
| 249 |
} |
| 250 |
t.Log(err) |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
func TestOIAP(t *testing.T) { |
| 255 |
rwc := openTPMOrSkip(t) |
| 256 |
defer rwc.Close() |
| 257 |
|
| 258 |
// Get auth info from OIAP. |
| 259 |
oiapr, err := oiap(rwc) |
| 260 |
if err != nil { |
| 261 |
t.Fatal("Couldn't run OIAP:", err) |
| 262 |
} |
| 263 |
defer oiapr.Close(rwc) |
| 264 |
} |
| 265 |
|
| 266 |
func TestOSAP(t *testing.T) { |
| 267 |
rwc := openTPMOrSkip(t) |
| 268 |
defer rwc.Close() |
| 269 |
|
| 270 |
// Try to run OSAP for the SRK. |
| 271 |
osapc := &osapCommand{ |
| 272 |
EntityType: etSRK, |
| 273 |
EntityValue: khSRK, |
| 274 |
} |
| 275 |
|
| 276 |
if _, err := rand.Read(osapc.OddOSAP[:]); err != nil { |
| 277 |
t.Fatal("Couldn't get a random odd OSAP nonce") |
| 278 |
} |
| 279 |
|
| 280 |
osapr, err := osap(rwc, osapc) |
| 281 |
if err != nil { |
| 282 |
t.Fatal("Couldn't run OSAP:", err) |
| 283 |
} |
| 284 |
defer osapr.Close(rwc) |
| 285 |
} |
| 286 |
|
| 287 |
func TestResizeableSlice(t *testing.T) { |
| 288 |
// Set up an encoded slice with a byte array. |
| 289 |
ra := &responseAuth{ |
| 290 |
NonceEven: [20]byte{}, |
| 291 |
ContSession: 1, |
| 292 |
Auth: [20]byte{}, |
| 293 |
} |
| 294 |
|
| 295 |
b := make([]byte, 322) |
| 296 |
if _, err := rand.Read(b); err != nil { |
| 297 |
t.Fatal("Couldn't read random bytes into the byte array") |
| 298 |
} |
| 299 |
|
| 300 |
bb, err := tpmutil.Pack(ra, tpmutil.U32Bytes(b)) |
| 301 |
if err != nil { |
| 302 |
t.Fatal("Couldn't pack the bytes:", err) |
| 303 |
} |
| 304 |
|
| 305 |
var ra2 responseAuth |
| 306 |
var b2 tpmutil.U32Bytes |
| 307 |
if _, err := tpmutil.Unpack(bb, &ra2, &b2); err != nil { |
| 308 |
t.Fatal("Couldn't unpack the resizeable values:", err) |
| 309 |
} |
| 310 |
|
| 311 |
if !bytes.Equal(b2, b) { |
| 312 |
t.Fatal("ResizeableSlice was not resized or copied correctly") |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
func TestSeal(t *testing.T) { |
| 317 |
rwc := openTPMOrSkip(t) |
| 318 |
defer rwc.Close() |
| 319 |
|
| 320 |
data := make([]byte, 64) |
| 321 |
data[0] = 137 |
| 322 |
data[1] = 138 |
| 323 |
data[2] = 139 |
| 324 |
|
| 325 |
srkAuth := getAuth(srkAuthEnvVar) |
| 326 |
sealed, err := Seal(rwc, LocZero, []int{17} /* PCR 17 */, data, srkAuth[:]) |
| 327 |
if err != nil { |
| 328 |
t.Fatal("Couldn't seal the data:", err) |
| 329 |
} |
| 330 |
|
| 331 |
data2, err := Unseal(rwc, sealed, srkAuth[:]) |
| 332 |
if err != nil { |
| 333 |
t.Fatal("Couldn't unseal the data:", err) |
| 334 |
} |
| 335 |
|
| 336 |
if !bytes.Equal(data2, data) { |
| 337 |
t.Fatal("Unsealed data doesn't match original data") |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
func TestReseal(t *testing.T) { |
| 342 |
rwc := openTPMOrSkip(t) |
| 343 |
defer rwc.Close() |
| 344 |
|
| 345 |
data := make([]byte, 64) |
| 346 |
data[0] = 137 |
| 347 |
data[1] = 138 |
| 348 |
data[2] = 139 |
| 349 |
|
| 350 |
pcrMap := make(map[int][]byte) |
| 351 |
pcrMap[23] = make([]byte, 20) |
| 352 |
pcrMap[16] = make([]byte, 20) |
| 353 |
srkAuth := getAuth(srkAuthEnvVar) |
| 354 |
sealed, err := Reseal(rwc, LocZero, pcrMap, data, srkAuth[:]) |
| 355 |
if err != nil { |
| 356 |
t.Fatal("Couldn't seal the data:", err) |
| 357 |
} |
| 358 |
|
| 359 |
data2, err := Unseal(rwc, sealed, srkAuth[:]) |
| 360 |
if err != nil { |
| 361 |
t.Fatal("Couldn't unseal the data:", err) |
| 362 |
} |
| 363 |
|
| 364 |
if !bytes.Equal(data2, data) { |
| 365 |
t.Fatal("Unsealed data doesn't match original data") |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestLoadKey2(t *testing.T) { |
| 370 |
rwc := openTPMOrSkip(t) |
| 371 |
defer rwc.Close() |
| 372 |
|
| 373 |
// Get the key from aikblob, assuming it exists. Otherwise, skip the test. |
| 374 |
blob, err := os.ReadFile("./aikblob") |
| 375 |
if err != nil { |
| 376 |
t.Skip("No aikblob file; skipping test") |
| 377 |
} |
| 378 |
|
| 379 |
// We're using the well-known authenticator of 20 bytes of zeros. |
| 380 |
srkAuth := getAuth(srkAuthEnvVar) |
| 381 |
handle, err := LoadKey2(rwc, blob, srkAuth[:]) |
| 382 |
if err != nil { |
| 383 |
t.Fatal("Couldn't load the AIK into the TPM and get a handle for it:", err) |
| 384 |
} |
| 385 |
|
| 386 |
if err := CloseKey(rwc, handle); err != nil { |
| 387 |
t.Fatal("Couldn't flush the AIK from the TPM:", err) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
func TestQuote2(t *testing.T) { |
| 392 |
rwc := openTPMOrSkip(t) |
| 393 |
defer rwc.Close() |
| 394 |
|
| 395 |
// Get the key from aikblob, assuming it exists. Otherwise, skip the test. |
| 396 |
blob, err := os.ReadFile("./aikblob") |
| 397 |
if err != nil { |
| 398 |
t.Skip("No aikblob file; skipping test") |
| 399 |
} |
| 400 |
|
| 401 |
// Load the AIK for the quote. |
| 402 |
// We're using the well-known authenticator of 20 bytes of zeros. |
| 403 |
srkAuth := getAuth(srkAuthEnvVar) |
| 404 |
handle, err := LoadKey2(rwc, blob, srkAuth[:]) |
| 405 |
if err != nil { |
| 406 |
t.Fatal("Couldn't load the AIK into the TPM and get a handle for it:", err) |
| 407 |
} |
| 408 |
defer CloseKey(rwc, handle) |
| 409 |
|
| 410 |
// Data to quote. |
| 411 |
data := []byte(`The OS says this test is good`) |
| 412 |
aikAuth := getAuth(aikAuthEnvVar) |
| 413 |
q, err := Quote2(rwc, handle, data, []int{17, 18}, 1 /* addVersion */, aikAuth[:]) |
| 414 |
if err != nil { |
| 415 |
t.Fatal("Couldn't quote the data:", err) |
| 416 |
} |
| 417 |
|
| 418 |
if len(q) == 0 { |
| 419 |
t.Fatal("Couldn't get a quote using an AIK") |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
func TestGetPubKey(t *testing.T) { |
| 424 |
// For testing purposes, use the aikblob if it exists. Otherwise, just skip |
| 425 |
// this test. TODO(tmroeder): implement AIK creation so we can always run |
| 426 |
// this test. |
| 427 |
rwc := openTPMOrSkip(t) |
| 428 |
defer rwc.Close() |
| 429 |
|
| 430 |
// Get the key from aikblob, assuming it exists. Otherwise, skip the test. |
| 431 |
blob, err := os.ReadFile("./aikblob") |
| 432 |
if err != nil { |
| 433 |
t.Skip("No aikblob file; skipping test") |
| 434 |
} |
| 435 |
|
| 436 |
// Load the AIK for the quote. |
| 437 |
// We're using the well-known authenticator of 20 bytes of zeros. |
| 438 |
srkAuth := getAuth(srkAuthEnvVar) |
| 439 |
handle, err := LoadKey2(rwc, blob, srkAuth[:]) |
| 440 |
if err != nil { |
| 441 |
t.Fatal("Couldn't load the AIK into the TPM and get a handle for it:", err) |
| 442 |
} |
| 443 |
defer CloseKey(rwc, handle) |
| 444 |
|
| 445 |
k, err := GetPubKey(rwc, handle, srkAuth[:]) |
| 446 |
if err != nil { |
| 447 |
t.Fatal("Couldn't get the pub key for the AIK") |
| 448 |
} |
| 449 |
|
| 450 |
if len(k) == 0 { |
| 451 |
t.Fatal("Couldn't get a pubkey blob from an AIK") |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
func TestQuote(t *testing.T) { |
| 456 |
rwc := openTPMOrSkip(t) |
| 457 |
defer rwc.Close() |
| 458 |
|
| 459 |
// Get the key from aikblob, assuming it exists. Otherwise, skip the test. |
| 460 |
blob, err := os.ReadFile("./aikblob") |
| 461 |
if err != nil { |
| 462 |
t.Skip("No aikblob file; skipping test") |
| 463 |
} |
| 464 |
|
| 465 |
// Load the AIK for the quote. |
| 466 |
srkAuth := getAuth(srkAuthEnvVar) |
| 467 |
handle, err := LoadKey2(rwc, blob, srkAuth[:]) |
| 468 |
if err != nil { |
| 469 |
t.Fatal("Couldn't load the AIK into the TPM and get a handle for it:", err) |
| 470 |
} |
| 471 |
defer CloseKey(rwc, handle) |
| 472 |
|
| 473 |
// Data to quote. |
| 474 |
data := []byte(`The OS says this test is good`) |
| 475 |
pcrNums := []int{17, 18} |
| 476 |
aikAuth := getAuth(aikAuthEnvVar) |
| 477 |
q, values, err := Quote(rwc, handle, data, pcrNums, aikAuth[:]) |
| 478 |
if err != nil { |
| 479 |
t.Fatal("Couldn't quote the data:", err) |
| 480 |
} |
| 481 |
|
| 482 |
// Verify the quote. |
| 483 |
pk, err := UnmarshalRSAPublicKey(blob) |
| 484 |
if err != nil { |
| 485 |
t.Fatal("Couldn't extract an RSA key from the AIK blob:", err) |
| 486 |
} |
| 487 |
|
| 488 |
if err := VerifyQuote(pk, data, q, pcrNums, values); err != nil { |
| 489 |
t.Fatal("The quote didn't pass verification:", err) |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestUnmarshalRSAPublicKey(t *testing.T) { |
| 494 |
// Get the key from aikblob, assuming it exists. Otherwise, skip the test. |
| 495 |
blob, err := os.ReadFile("./aikblob") |
| 496 |
if err != nil { |
| 497 |
t.Skip("No aikblob file; skipping test") |
| 498 |
} |
| 499 |
|
| 500 |
if _, err := UnmarshalRSAPublicKey(blob); err != nil { |
| 501 |
t.Fatal("Couldn't extract an RSA key from the AIK blob:", err) |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
func TestMakeIdentity(t *testing.T) { |
| 506 |
rwc := openTPMOrSkip(t) |
| 507 |
defer rwc.Close() |
| 508 |
|
| 509 |
srkAuth := getAuth(srkAuthEnvVar) |
| 510 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 511 |
aikAuth := getAuth(aikAuthEnvVar) |
| 512 |
|
| 513 |
// In the simplest case, we pass in nil for the Privacy CA key and the |
| 514 |
// label. |
| 515 |
blob, err := MakeIdentity(rwc, srkAuth[:], ownerAuth[:], aikAuth[:], nil, nil) |
| 516 |
if err != nil { |
| 517 |
t.Fatal("Couldn't make a new AIK in the TPM:", err) |
| 518 |
} |
| 519 |
|
| 520 |
handle, err := LoadKey2(rwc, blob, srkAuth[:]) |
| 521 |
if err != nil { |
| 522 |
t.Fatal("Couldn't load the freshly-generated AIK into the TPM and get a handle for it:", err) |
| 523 |
} |
| 524 |
defer CloseKey(rwc, handle) |
| 525 |
|
| 526 |
// Data to quote. |
| 527 |
data := []byte(`The OS says this test and new AIK is good`) |
| 528 |
pcrNums := []int{17, 18} |
| 529 |
q, values, err := Quote(rwc, handle, data, pcrNums, aikAuth[:]) |
| 530 |
if err != nil { |
| 531 |
t.Fatal("Couldn't quote the data:", err) |
| 532 |
} |
| 533 |
|
| 534 |
// Verify the quote. |
| 535 |
pk, err := UnmarshalRSAPublicKey(blob) |
| 536 |
if err != nil { |
| 537 |
t.Fatal("Couldn't extract an RSA key from the AIK blob:", err) |
| 538 |
} |
| 539 |
|
| 540 |
if err := VerifyQuote(pk, data, q, pcrNums, values); err != nil { |
| 541 |
t.Fatal("The quote didn't pass verification:", err) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
func TestResetLockValue(t *testing.T) { |
| 546 |
rwc := openTPMOrSkip(t) |
| 547 |
defer rwc.Close() |
| 548 |
|
| 549 |
// This test code assumes that the owner auth is the well-known value. |
| 550 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 551 |
if err := ResetLockValue(rwc, ownerAuth); err != nil { |
| 552 |
t.Fatal("Couldn't reset the lock value:", err) |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
func TestOwnerReadSRK(t *testing.T) { |
| 557 |
rwc := openTPMOrSkip(t) |
| 558 |
defer rwc.Close() |
| 559 |
|
| 560 |
// This test code assumes that the owner auth is the well-known value. |
| 561 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 562 |
srkb, err := OwnerReadSRK(rwc, ownerAuth) |
| 563 |
if err != nil { |
| 564 |
t.Fatal("Couldn't read the SRK using owner auth:", err) |
| 565 |
} |
| 566 |
|
| 567 |
if len(srkb) == 0 { |
| 568 |
t.Fatal("Couldn't get an SRK blob from the TPM") |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
func TestOwnerReadPubEK(t *testing.T) { |
| 573 |
rwc := openTPMOrSkip(t) |
| 574 |
defer rwc.Close() |
| 575 |
|
| 576 |
// This test code assumes that the owner auth is the well-known value. |
| 577 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 578 |
pkb, err := OwnerReadPubEK(rwc, ownerAuth) |
| 579 |
if err != nil { |
| 580 |
t.Fatal("Couldn't read the pub EK using owner auth:", err) |
| 581 |
} |
| 582 |
|
| 583 |
pk, err := UnmarshalPubRSAPublicKey(pkb) |
| 584 |
if err != nil { |
| 585 |
t.Fatal("Couldn't unmarshal the endorsement key:", err) |
| 586 |
} |
| 587 |
|
| 588 |
if pk.N.BitLen() != 2048 { |
| 589 |
t.Fatal("Invalid endorsement key: not a 2048-bit RSA key") |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
func TestOwnerClear(t *testing.T) { |
| 594 |
// Only enable this if you know what you're doing. |
| 595 |
t.Skip() |
| 596 |
rwc := openTPMOrSkip(t) |
| 597 |
defer rwc.Close() |
| 598 |
|
| 599 |
// This test code assumes that the owner auth is the well-known value. |
| 600 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 601 |
if err := OwnerClear(rwc, ownerAuth); err != nil { |
| 602 |
t.Fatal("Couldn't clear the TPM using owner auth:", err) |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func TestTakeOwnership(t *testing.T) { |
| 607 |
// This only works in limited circumstances, so it's disabled in general. |
| 608 |
t.Skip() |
| 609 |
rwc := openTPMOrSkip(t) |
| 610 |
defer rwc.Close() |
| 611 |
|
| 612 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 613 |
srkAuth := getAuth(srkAuthEnvVar) |
| 614 |
|
| 615 |
// This test assumes that the TPM has been cleared using OwnerClear. |
| 616 |
pubEK, err := ReadPubEK(rwc) |
| 617 |
// Create the EK if needed. |
| 618 |
if err == tpmError(errNoEndorsement) { |
| 619 |
if err = createEK(rwc); err == nil { |
| 620 |
pubEK, err = ReadPubEK(rwc) |
| 621 |
} |
| 622 |
} |
| 623 |
if err != nil { |
| 624 |
t.Fatal("Couldn't read the public endorsement key from the TPM:", err) |
| 625 |
} |
| 626 |
|
| 627 |
if err := TakeOwnership(rwc, ownerAuth, srkAuth, pubEK); err != nil { |
| 628 |
t.Fatal("Couldn't take ownership of the TPM:", err) |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
func TestForceClear(t *testing.T) { |
| 633 |
// Only enable this if you know what you're doing. |
| 634 |
// TPM force clear clears the ownership of the TPM. |
| 635 |
// Beware of running this test on a production system. |
| 636 |
t.Skip() |
| 637 |
rwc := openTPMOrSkip(t) |
| 638 |
defer rwc.Close() |
| 639 |
|
| 640 |
if err := ForceClear(rwc); err != nil { |
| 641 |
t.Fatal("Couldn't clear the TPM without owner auth in physical presence mode:", err) |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
func TestNVWriteValue(t *testing.T) { |
| 646 |
mathrand.Seed(42) |
| 647 |
rwc := openTPMOrSkip(t) |
| 648 |
defer rwc.Close() |
| 649 |
nvList, err := GetNVList(rwc) |
| 650 |
if err != nil { |
| 651 |
t.Fatalf("Couldn't open TPM connection: %v", err) |
| 652 |
} |
| 653 |
// If no indices are set, skip the test. |
| 654 |
// Need DefineSpace-Function for implementing the other case |
| 655 |
if len(nvList) == 0 { |
| 656 |
t.Skip() |
| 657 |
} |
| 658 |
rndIndex := nvList[mathrand.Intn(len(nvList))] |
| 659 |
rndIndexInfo, err := GetNVIndex(rwc, rndIndex) |
| 660 |
if err != nil { |
| 661 |
t.Fatalf("Couldn't retrieve index information: %v", err) |
| 662 |
} |
| 663 |
|
| 664 |
indexValue, err := NVReadValue(rwc, rndIndex, 0, rndIndexInfo.Size, nil) |
| 665 |
if err != nil { |
| 666 |
t.Fatalf("Couldn't read index value @ index: %v with error: %v", rndIndex, err) |
| 667 |
} |
| 668 |
|
| 669 |
err = NVWriteValue(rwc, rndIndex, 0, indexValue, nil) |
| 670 |
if err != nil { |
| 671 |
t.Errorf("Couldn't write to index: %v with error: %v", rndIndex, err) |
| 672 |
} |
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
func TestNVReadValueNoAuth(t *testing.T) { |
| 677 |
rwc := openTPMOrSkip(t) |
| 678 |
defer rwc.Close() |
| 679 |
//Get NVList |
| 680 |
nvList, err := GetNVList(rwc) |
| 681 |
if err != nil { |
| 682 |
t.Fatalf("error reading nvlist: %v", err) |
| 683 |
} |
| 684 |
for _, item := range nvList { |
| 685 |
pubData, err := GetNVIndex(rwc, item) |
| 686 |
if err != nil { |
| 687 |
t.Errorf("reading index: %v has no data: %v", item, err) |
| 688 |
} |
| 689 |
indexvalue, err := NVReadValue(rwc, pubData.NVIndex, 0, pubData.Size, nil) |
| 690 |
if err != nil { |
| 691 |
t.Logf("cant read index: %v", err) |
| 692 |
} |
| 693 |
t.Logf("indexvalue: %v", indexvalue) |
| 694 |
} |
| 695 |
if len(nvList) == 0 { |
| 696 |
t.Logf("no values to read in nvram") |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
func TestKeyMigration(t *testing.T) { |
| 701 |
rwc := openTPMOrSkip(t) |
| 702 |
defer rwc.Close() |
| 703 |
ownerAuth := getAuth(ownerAuthEnvVar) |
| 704 |
srkAuth := getAuth(srkAuthEnvVar) |
| 705 |
// Generate some random migration and usage auth values. |
| 706 |
migrationAuth := Digest{} |
| 707 |
usageAuth := Digest{} |
| 708 |
rand.Read(migrationAuth[:]) |
| 709 |
rand.Read(usageAuth[:]) |
| 710 |
|
| 711 |
// First generate our private key we'll use for migration. |
| 712 |
mk, err := rsa.GenerateKey(rand.Reader, 2048) |
| 713 |
if err != nil { |
| 714 |
t.Fatalf("error generating migration key: %v", err) |
| 715 |
} |
| 716 |
|
| 717 |
// Then get the key endorsed by the TPM for doing migrations. |
| 718 |
mb, err := AuthorizeMigrationKey(rwc, ownerAuth, mk.Public()) |
| 719 |
if err != nil { |
| 720 |
t.Fatalf("error authorizing migration key: %v", err) |
| 721 |
} |
| 722 |
|
| 723 |
// Create a migratable key in the TPM. |
| 724 |
_, privkey, err := CreateMigratableWrapKey( |
| 725 |
rwc, |
| 726 |
srkAuth[:], |
| 727 |
usageAuth, |
| 728 |
migrationAuth, |
| 729 |
[]int{}, // no PCR's |
| 730 |
) |
| 731 |
if err != nil { |
| 732 |
t.Fatalf("error creating key: %v", err) |
| 733 |
} |
| 734 |
|
| 735 |
// Migrate the key to the saved migration key. |
| 736 |
encPriv, err := CreateMigrationBlob(rwc, srkAuth, migrationAuth, privkey, mb) |
| 737 |
if err != nil { |
| 738 |
t.Fatalf("Error migrating created key: %v", err) |
| 739 |
} |
| 740 |
|
| 741 |
// Decrypt the key. Successful OAEP decryption implies successful migration. |
| 742 |
_, err = rsa.DecryptOAEP(sha1.New(), nil, mk, encPriv, []byte{0x54, 0x43, 0x50, 0x41} /* 'TCPA', see TPM 1.2 spec */) |
| 743 |
if err != nil { |
| 744 |
t.Errorf("Error decrypting migrated key blob: %v", err) |
| 745 |
} |
| 746 |
} |