| 1 |
// Copyright 2021 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// 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, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
//go:build !localtest && cgo && !gofuzz |
| 16 |
// +build !localtest,cgo,!gofuzz |
| 17 |
|
| 18 |
package attest |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"crypto" |
| 23 |
"crypto/rand" |
| 24 |
"crypto/rsa" |
| 25 |
"slices" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/google/go-cmp/cmp" |
| 29 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 30 |
"github.com/google/go-tpm/legacy/tpm2" |
| 31 |
) |
| 32 |
|
| 33 |
func TestSimCertificationParametersRSA(t *testing.T) { |
| 34 |
sim, tpm := setupSimulatedTPM(t) |
| 35 |
defer sim.Close() |
| 36 |
testCertificationParameters(t, tpm, RSA) |
| 37 |
} |
| 38 |
|
| 39 |
func TestSimCertificationParametersECC(t *testing.T) { |
| 40 |
sim, tpm := setupSimulatedTPM(t) |
| 41 |
defer sim.Close() |
| 42 |
testCertificationParameters(t, tpm, ECDSA) |
| 43 |
} |
| 44 |
|
| 45 |
func TestCertificationParametersRSA(t *testing.T) { |
| 46 |
if !*testLocal { |
| 47 |
t.SkipNow() |
| 48 |
} |
| 49 |
tpm, err := OpenTPM(nil) |
| 50 |
if err != nil { |
| 51 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 52 |
} |
| 53 |
defer tpm.Close() |
| 54 |
testCertificationParameters(t, tpm, RSA) |
| 55 |
} |
| 56 |
|
| 57 |
func TestCertificationParametersECC(t *testing.T) { |
| 58 |
if !*testLocal { |
| 59 |
t.SkipNow() |
| 60 |
} |
| 61 |
tpm, err := OpenTPM(nil) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 64 |
} |
| 65 |
defer tpm.Close() |
| 66 |
testCertificationParameters(t, tpm, ECDSA) |
| 67 |
} |
| 68 |
|
| 69 |
func testCertificationParameters(t *testing.T, tpm *TPM, akAlg Algorithm) { |
| 70 |
ak, err := tpm.NewAK(&AKConfig{Algorithm: akAlg}) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
akAttestParams := ak.AttestationParameters() |
| 75 |
pub, err := tpm2.DecodePublic(akAttestParams.Public) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
|
| 80 |
pk, err := pub.Key() |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
hash, err := pub.NameAlg.Hash() |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
correctOpts := VerifyOpts{ |
| 89 |
Public: pk, |
| 90 |
Hash: hash, |
| 91 |
} |
| 92 |
|
| 93 |
wrongKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
wrongHash := crypto.SHA512_256 |
| 98 |
if wrongHash == correctOpts.Hash { |
| 99 |
wrongHash = crypto.SHA256 |
| 100 |
} |
| 101 |
|
| 102 |
sk, err := tpm.NewKey(ak, nil) |
| 103 |
if err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
skCertParams := sk.CertificationParameters() |
| 107 |
|
| 108 |
for _, test := range []struct { |
| 109 |
name string |
| 110 |
p *CertificationParameters |
| 111 |
opts VerifyOpts |
| 112 |
err error |
| 113 |
}{ |
| 114 |
{ |
| 115 |
name: "OK", |
| 116 |
p: &skCertParams, |
| 117 |
opts: correctOpts, |
| 118 |
err: nil, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "wrong public key", |
| 122 |
p: &skCertParams, |
| 123 |
opts: VerifyOpts{ |
| 124 |
Public: wrongKey.Public, |
| 125 |
Hash: correctOpts.Hash, |
| 126 |
}, |
| 127 |
err: cmpopts.AnyError, |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "wrong hash function", |
| 131 |
p: &skCertParams, |
| 132 |
opts: VerifyOpts{ |
| 133 |
Public: correctOpts.Public, |
| 134 |
Hash: wrongHash, |
| 135 |
}, |
| 136 |
err: cmpopts.AnyError, |
| 137 |
}, |
| 138 |
{ |
| 139 |
name: "unavailable hash function", |
| 140 |
p: &skCertParams, |
| 141 |
opts: VerifyOpts{ |
| 142 |
Public: correctOpts.Public, |
| 143 |
Hash: crypto.BLAKE2b_384, |
| 144 |
}, |
| 145 |
err: cmpopts.AnyError, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "modified Public", |
| 149 |
p: &CertificationParameters{ |
| 150 |
Public: akAttestParams.Public, |
| 151 |
CreateAttestation: skCertParams.CreateAttestation, |
| 152 |
CreateSignature: skCertParams.CreateSignature, |
| 153 |
}, |
| 154 |
opts: correctOpts, |
| 155 |
err: cmpopts.AnyError, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "modified CreateAttestation", |
| 159 |
p: &CertificationParameters{ |
| 160 |
Public: skCertParams.Public, |
| 161 |
CreateAttestation: akAttestParams.CreateAttestation, |
| 162 |
CreateSignature: skCertParams.CreateSignature, |
| 163 |
}, |
| 164 |
opts: correctOpts, |
| 165 |
err: cmpopts.AnyError, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "modified CreateSignature", |
| 169 |
p: &CertificationParameters{ |
| 170 |
Public: skCertParams.Public, |
| 171 |
CreateAttestation: skCertParams.CreateAttestation, |
| 172 |
CreateSignature: akAttestParams.CreateSignature, |
| 173 |
}, |
| 174 |
opts: correctOpts, |
| 175 |
err: cmpopts.AnyError, |
| 176 |
}, |
| 177 |
} { |
| 178 |
t.Run(test.name, func(t *testing.T) { |
| 179 |
err := test.p.Verify(test.opts) |
| 180 |
if test.err == nil && err == nil { |
| 181 |
return |
| 182 |
} |
| 183 |
if got, want := err, test.err; !cmp.Equal(got, want, cmpopts.EquateErrors()) { |
| 184 |
t.Errorf("p.Verify() err = %v, want = %v", got, want) |
| 185 |
} |
| 186 |
}) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestSimKeyCertificationRSA(t *testing.T) { |
| 191 |
sim, tpm := setupSimulatedTPM(t) |
| 192 |
defer sim.Close() |
| 193 |
testKeyCertification(t, tpm, RSA) |
| 194 |
} |
| 195 |
|
| 196 |
func TestSimKeyCertificationECC(t *testing.T) { |
| 197 |
sim, tpm := setupSimulatedTPM(t) |
| 198 |
defer sim.Close() |
| 199 |
testKeyCertification(t, tpm, ECDSA) |
| 200 |
} |
| 201 |
|
| 202 |
func TestKeyCertificationRSA(t *testing.T) { |
| 203 |
if !*testLocal { |
| 204 |
t.SkipNow() |
| 205 |
} |
| 206 |
tpm, err := OpenTPM(nil) |
| 207 |
if err != nil { |
| 208 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 209 |
} |
| 210 |
defer tpm.Close() |
| 211 |
testKeyCertification(t, tpm, RSA) |
| 212 |
} |
| 213 |
|
| 214 |
func TestKeyCertificationECC(t *testing.T) { |
| 215 |
if !*testLocal { |
| 216 |
t.SkipNow() |
| 217 |
} |
| 218 |
tpm, err := OpenTPM(nil) |
| 219 |
if err != nil { |
| 220 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 221 |
} |
| 222 |
defer tpm.Close() |
| 223 |
testKeyCertification(t, tpm, ECDSA) |
| 224 |
} |
| 225 |
|
| 226 |
func extraData(t *testing.T, p CertificationParameters) []byte { |
| 227 |
t.Helper() |
| 228 |
ad, err := tpm2.DecodeAttestationData(p.CreateAttestation) |
| 229 |
if err != nil { |
| 230 |
t.Fatalf("failed to decode attestation data: %v", err) |
| 231 |
} |
| 232 |
return ad.ExtraData |
| 233 |
} |
| 234 |
|
| 235 |
func testKeyCertification(t *testing.T, tpm *TPM, akAlg Algorithm) { |
| 236 |
ak, err := tpm.NewAK(&AKConfig{Algorithm: akAlg}) |
| 237 |
if err != nil { |
| 238 |
t.Fatalf("NewAK() failed: %v", err) |
| 239 |
} |
| 240 |
akAttestParams := ak.AttestationParameters() |
| 241 |
pub, err := tpm2.DecodePublic(akAttestParams.Public) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("DecodePublic() failed: %v", err) |
| 244 |
} |
| 245 |
pk, err := pub.Key() |
| 246 |
if err != nil { |
| 247 |
t.Fatalf("pub.Key() failed: %v", err) |
| 248 |
} |
| 249 |
hash, err := pub.NameAlg.Hash() |
| 250 |
if err != nil { |
| 251 |
t.Fatalf("cannot access AK's hash function: %v", err) |
| 252 |
} |
| 253 |
verifyOpts := VerifyOpts{ |
| 254 |
Public: pk, |
| 255 |
Hash: hash, |
| 256 |
} |
| 257 |
for _, test := range []struct { |
| 258 |
name string |
| 259 |
opts *KeyConfig |
| 260 |
wantExtraData []byte |
| 261 |
err error |
| 262 |
}{ |
| 263 |
{ |
| 264 |
name: "default", |
| 265 |
opts: nil, |
| 266 |
err: nil, |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "ECDSAP256-SHA256", |
| 270 |
opts: &KeyConfig{ |
| 271 |
Algorithm: ECDSA, |
| 272 |
Size: 256, |
| 273 |
}, |
| 274 |
err: nil, |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: "ECDSAP384-SHA384", |
| 278 |
opts: &KeyConfig{ |
| 279 |
Algorithm: ECDSA, |
| 280 |
Size: 384, |
| 281 |
}, |
| 282 |
err: nil, |
| 283 |
}, |
| 284 |
{ |
| 285 |
name: "ECDSAP521-SHA512", |
| 286 |
opts: &KeyConfig{ |
| 287 |
Algorithm: ECDSA, |
| 288 |
Size: 521, |
| 289 |
}, |
| 290 |
err: nil, |
| 291 |
}, |
| 292 |
{ |
| 293 |
name: "RSA-1024, key too short", |
| 294 |
opts: &KeyConfig{ |
| 295 |
Algorithm: RSA, |
| 296 |
Size: 1024, |
| 297 |
}, |
| 298 |
err: cmpopts.AnyError, |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "RSA-2048", |
| 302 |
opts: &KeyConfig{ |
| 303 |
Algorithm: RSA, |
| 304 |
Size: 2048, |
| 305 |
}, |
| 306 |
err: nil, |
| 307 |
}, |
| 308 |
{ |
| 309 |
name: "QualifyingData-RSA", |
| 310 |
opts: &KeyConfig{ |
| 311 |
Algorithm: RSA, |
| 312 |
Size: 2048, |
| 313 |
QualifyingData: []byte("qualifying data"), |
| 314 |
}, |
| 315 |
wantExtraData: []byte("qualifying data"), |
| 316 |
err: nil, |
| 317 |
}, |
| 318 |
{ |
| 319 |
name: "QualifyingData-ECDSA", |
| 320 |
opts: &KeyConfig{ |
| 321 |
Algorithm: ECDSA, |
| 322 |
Size: 384, |
| 323 |
QualifyingData: []byte("qualifying data"), |
| 324 |
}, |
| 325 |
wantExtraData: []byte("qualifying data"), |
| 326 |
err: nil, |
| 327 |
}, |
| 328 |
} { |
| 329 |
t.Run(test.name, func(t *testing.T) { |
| 330 |
sk, err := tpm.NewKey(ak, test.opts) |
| 331 |
if err != nil { |
| 332 |
t.Fatalf("NewKey() failed: %v", err) |
| 333 |
} |
| 334 |
defer sk.Close() |
| 335 |
p := sk.CertificationParameters() |
| 336 |
if gotExtraData, wantExtraData := extraData(t, p), test.wantExtraData; !slices.Equal(gotExtraData, wantExtraData) { |
| 337 |
t.Errorf("ExtraData got = %v, want = %v", gotExtraData, wantExtraData) |
| 338 |
} |
| 339 |
err = p.Verify(verifyOpts) |
| 340 |
if test.err == nil && err == nil { |
| 341 |
return |
| 342 |
} |
| 343 |
if got, want := err, test.err; !cmp.Equal(got, want, cmpopts.EquateErrors()) { |
| 344 |
t.Errorf("p.Verify() err = %v, want = %v", got, want) |
| 345 |
} |
| 346 |
}) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestKeyActivation(t *testing.T) { |
| 351 |
sim, tpm := setupSimulatedTPM(t) |
| 352 |
defer sim.Close() |
| 353 |
|
| 354 |
ak, err := tpm.NewAK(nil) |
| 355 |
if err != nil { |
| 356 |
t.Fatalf("error creating a new AK using simulated TPM: %v", err) |
| 357 |
} |
| 358 |
akAttestParams := ak.AttestationParameters() |
| 359 |
pub, err := tpm2.DecodePublic(akAttestParams.Public) |
| 360 |
if err != nil { |
| 361 |
t.Fatalf("unable to decode public struct from AK attestation params: %v", err) |
| 362 |
} |
| 363 |
if pub.Type != tpm2.AlgRSA { |
| 364 |
t.Fatal("non-RSA verifying key") |
| 365 |
} |
| 366 |
|
| 367 |
eks, err := tpm.EKs() |
| 368 |
if err != nil { |
| 369 |
t.Fatalf("unexpected error retrieving EK from tpm: %v", err) |
| 370 |
} |
| 371 |
|
| 372 |
if len(eks) == 0 { |
| 373 |
t.Fatal("expected at least one EK from the simulated TPM") |
| 374 |
} |
| 375 |
|
| 376 |
pk := &rsa.PublicKey{E: int(pub.RSAParameters.Exponent()), N: pub.RSAParameters.Modulus()} |
| 377 |
hash, err := pub.RSAParameters.Sign.Hash.Hash() |
| 378 |
if err != nil { |
| 379 |
t.Fatalf("unable to compute hash signature from verifying key's RSA parameters: %v", err) |
| 380 |
} |
| 381 |
verifyOpts := VerifyOpts{ |
| 382 |
Public: pk, |
| 383 |
Hash: hash, |
| 384 |
} |
| 385 |
|
| 386 |
sk, err := tpm.NewKey(ak, nil) |
| 387 |
if err != nil { |
| 388 |
t.Fatalf("unable to create a new TPM-backed key to certify: %v", err) |
| 389 |
} |
| 390 |
|
| 391 |
skCertParams := sk.CertificationParameters() |
| 392 |
activateOpts, err := NewActivateOpts(pub, eks[0].Public) |
| 393 |
if err != nil { |
| 394 |
t.Fatalf("unable to create new ActivateOpts: %v", err) |
| 395 |
} |
| 396 |
|
| 397 |
wrongPub, err := tpm2.DecodePublic(skCertParams.Public) |
| 398 |
if err != nil { |
| 399 |
t.Fatalf("unable to decode public struct from CertificationParameters: %v", err) |
| 400 |
} |
| 401 |
|
| 402 |
wrongActivateOpts, err := NewActivateOpts(wrongPub, eks[0].Public) |
| 403 |
if err != nil { |
| 404 |
t.Fatalf("unable to create wrong ActivateOpts: %v", err) |
| 405 |
} |
| 406 |
|
| 407 |
for _, test := range []struct { |
| 408 |
name string |
| 409 |
p *CertificationParameters |
| 410 |
verifyOpts VerifyOpts |
| 411 |
activateOpts ActivateOpts |
| 412 |
generateErr error |
| 413 |
activateErr error |
| 414 |
}{ |
| 415 |
{ |
| 416 |
name: "OK", |
| 417 |
p: &skCertParams, |
| 418 |
verifyOpts: verifyOpts, |
| 419 |
activateOpts: *activateOpts, |
| 420 |
generateErr: nil, |
| 421 |
activateErr: nil, |
| 422 |
}, |
| 423 |
{ |
| 424 |
name: "invalid verify opts", |
| 425 |
p: &skCertParams, |
| 426 |
verifyOpts: VerifyOpts{}, |
| 427 |
activateOpts: *activateOpts, |
| 428 |
generateErr: cmpopts.AnyError, |
| 429 |
activateErr: nil, |
| 430 |
}, |
| 431 |
{ |
| 432 |
name: "invalid activate opts", |
| 433 |
p: &skCertParams, |
| 434 |
verifyOpts: verifyOpts, |
| 435 |
activateOpts: *wrongActivateOpts, |
| 436 |
generateErr: nil, |
| 437 |
activateErr: cmpopts.AnyError, |
| 438 |
}, |
| 439 |
} { |
| 440 |
t.Run(test.name, func(t *testing.T) { |
| 441 |
expectedSecret, encryptedCredentials, err := test.p.Generate(rand.Reader, test.verifyOpts, test.activateOpts) |
| 442 |
if test.generateErr != nil { |
| 443 |
if got, want := err, test.generateErr; !cmp.Equal(got, want, cmpopts.EquateErrors()) { |
| 444 |
t.Errorf("p.Generate() err = %v, want = %v", got, want) |
| 445 |
} |
| 446 |
|
| 447 |
return |
| 448 |
} |
| 449 |
if err != nil { |
| 450 |
t.Errorf("unexpected p.Generate() error: %v", err) |
| 451 |
return |
| 452 |
} |
| 453 |
|
| 454 |
actualSecret, err := ak.ActivateCredential(tpm, *encryptedCredentials) |
| 455 |
if test.activateErr != nil { |
| 456 |
if got, want := err, test.activateErr; !cmp.Equal(got, want, cmpopts.EquateErrors()) { |
| 457 |
t.Errorf("p.ActivateCredential() err = %v, want = %v", got, want) |
| 458 |
} |
| 459 |
|
| 460 |
return |
| 461 |
} |
| 462 |
if err != nil { |
| 463 |
t.Errorf("unexpected p.ActivateCredential() error: %v", err) |
| 464 |
return |
| 465 |
} |
| 466 |
|
| 467 |
if !bytes.Equal(expectedSecret, actualSecret) { |
| 468 |
t.Fatalf("Unexpected bytes decoded, expected %x, but got %x", expectedSecret, actualSecret) |
| 469 |
} |
| 470 |
}) |
| 471 |
} |
| 472 |
} |