| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/rand" |
| 6 |
"crypto/rsa" |
| 7 |
"crypto/x509" |
| 8 |
"io" |
| 9 |
"math/big" |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/google/go-tpm/legacy/tpm2" |
| 15 |
"github.com/google/go-tpm/tpmutil" |
| 16 |
|
| 17 |
"github.com/google/go-tpm-tools/client" |
| 18 |
"github.com/google/go-tpm-tools/internal/test" |
| 19 |
) |
| 20 |
|
| 21 |
func TestNameMatchesPublicArea(t *testing.T) { |
| 22 |
rwc := test.GetTPM(t) |
| 23 |
defer client.CheckedClose(t, rwc) |
| 24 |
ek, err := client.EndorsementKeyRSA(rwc) |
| 25 |
if err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
defer ek.Close() |
| 29 |
|
| 30 |
matches, err := ek.Name().MatchesPublic(ek.PublicArea()) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
if !matches { |
| 35 |
t.Fatal("Returned name and computed name do not match") |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestCreateSigningKeysInHierarchies(t *testing.T) { |
| 40 |
rwc := test.GetTPM(t) |
| 41 |
defer client.CheckedClose(t, rwc) |
| 42 |
template := client.AKTemplateRSA() |
| 43 |
|
| 44 |
// We are not authorized to create keys in the Platform Hierarchy |
| 45 |
for _, hierarchy := range []tpmutil.Handle{tpm2.HandleOwner, tpm2.HandleEndorsement, tpm2.HandleNull} { |
| 46 |
key, err := client.NewKey(rwc, hierarchy, template) |
| 47 |
if err != nil { |
| 48 |
t.Errorf("Hierarchy %+v: %s", hierarchy, err) |
| 49 |
} else { |
| 50 |
key.Close() |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestCachedRSAKeys(t *testing.T) { |
| 56 |
rwc := test.GetTPM(t) |
| 57 |
defer client.CheckedClose(t, rwc) |
| 58 |
keys := []struct { |
| 59 |
name string |
| 60 |
getKey func(io.ReadWriter) (*client.Key, error) |
| 61 |
}{ |
| 62 |
{"SRK", client.StorageRootKeyRSA}, |
| 63 |
{"EK", client.EndorsementKeyRSA}, |
| 64 |
} |
| 65 |
|
| 66 |
for _, k := range keys { |
| 67 |
t.Run(k.name, func(t *testing.T) { |
| 68 |
// Get the key the first time and persist |
| 69 |
srk, err := k.getKey(rwc) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
defer srk.Close() |
| 74 |
|
| 75 |
pub := srk.PublicKey() |
| 76 |
if tpm2.FlushContext(rwc, srk.Handle()) == nil { |
| 77 |
t.Error("Trying to flush persistent keys should fail.") |
| 78 |
} |
| 79 |
|
| 80 |
// Get the cached key (should be the same) |
| 81 |
srk, err = k.getKey(rwc) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
defer srk.Close() |
| 86 |
|
| 87 |
if !reflect.DeepEqual(srk.PublicKey(), pub) { |
| 88 |
t.Errorf("Expected pub key: %v got: %v", pub, srk.PublicKey()) |
| 89 |
} |
| 90 |
|
| 91 |
// We should still get the same key if we evict the handle |
| 92 |
if err := tpm2.EvictControl(rwc, "", tpm2.HandleOwner, srk.Handle(), srk.Handle()); err != nil { |
| 93 |
t.Errorf("Evicting control failed: %v", err) |
| 94 |
} |
| 95 |
srk, err = k.getKey(rwc) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
defer srk.Close() |
| 100 |
|
| 101 |
if !reflect.DeepEqual(srk.PublicKey(), pub) { |
| 102 |
t.Errorf("Expected pub key: %v got: %v", pub, srk.PublicKey()) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestKeyCreation(t *testing.T) { |
| 109 |
rwc := test.GetTPM(t) |
| 110 |
defer client.CheckedClose(t, rwc) |
| 111 |
|
| 112 |
keys := []struct { |
| 113 |
name string |
| 114 |
getKey func(io.ReadWriter) (*client.Key, error) |
| 115 |
}{ |
| 116 |
{"SRK-ECC", client.StorageRootKeyECC}, |
| 117 |
{"EK-ECC", client.EndorsementKeyECC}, |
| 118 |
{"AK-ECC", client.AttestationKeyECC}, |
| 119 |
{"SRK-RSA", client.StorageRootKeyRSA}, |
| 120 |
{"EK-RSA", client.EndorsementKeyRSA}, |
| 121 |
{"AK-RSA", client.AttestationKeyRSA}, |
| 122 |
} |
| 123 |
|
| 124 |
for _, k := range keys { |
| 125 |
t.Run(k.name, func(t *testing.T) { |
| 126 |
key, err := k.getKey(rwc) |
| 127 |
if err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} |
| 130 |
key.Close() |
| 131 |
}) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func BenchmarkKeyCreation(b *testing.B) { |
| 136 |
rwc := test.GetTPM(b) |
| 137 |
defer client.CheckedClose(b, rwc) |
| 138 |
|
| 139 |
benchmarks := []struct { |
| 140 |
name string |
| 141 |
getKey func(io.ReadWriter) (*client.Key, error) |
| 142 |
}{ |
| 143 |
{"SRK-ECC-Cached", client.StorageRootKeyECC}, |
| 144 |
{"EK-ECC-Cached", client.EndorsementKeyECC}, |
| 145 |
{"AK-ECC-Cached", client.AttestationKeyECC}, |
| 146 |
|
| 147 |
{"SRK-ECC", func(rw io.ReadWriter) (*client.Key, error) { |
| 148 |
return client.NewKey(rw, tpm2.HandleOwner, client.SRKTemplateECC()) |
| 149 |
}}, |
| 150 |
{"EK-ECC", func(rw io.ReadWriter) (*client.Key, error) { |
| 151 |
return client.NewKey(rw, tpm2.HandleEndorsement, client.DefaultEKTemplateECC()) |
| 152 |
}}, |
| 153 |
{"AK-ECC", func(rw io.ReadWriter) (*client.Key, error) { |
| 154 |
return client.NewKey(rw, tpm2.HandleOwner, client.AKTemplateECC()) |
| 155 |
}}, |
| 156 |
|
| 157 |
{"SRK-RSA-Cached", client.StorageRootKeyRSA}, |
| 158 |
{"EK-RSA-Cached", client.EndorsementKeyRSA}, |
| 159 |
{"AK-RSA-Cached", client.AttestationKeyRSA}, |
| 160 |
|
| 161 |
{"SRK-RSA", func(rw io.ReadWriter) (*client.Key, error) { |
| 162 |
return client.NewKey(rw, tpm2.HandleEndorsement, client.SRKTemplateRSA()) |
| 163 |
}}, |
| 164 |
{"EK-RSA", func(rw io.ReadWriter) (*client.Key, error) { |
| 165 |
return client.NewKey(rw, tpm2.HandleOwner, client.DefaultEKTemplateRSA()) |
| 166 |
}}, |
| 167 |
{"AK-RSA", func(rw io.ReadWriter) (*client.Key, error) { |
| 168 |
return client.NewKey(rw, tpm2.HandleOwner, client.AKTemplateRSA()) |
| 169 |
}}, |
| 170 |
} |
| 171 |
|
| 172 |
for _, bm := range benchmarks { |
| 173 |
b.Run(bm.name, func(b *testing.B) { |
| 174 |
// Don't count time to populate the cache |
| 175 |
b.StopTimer() |
| 176 |
key, err := bm.getKey(rwc) |
| 177 |
if err != nil { |
| 178 |
b.Fatal(err) |
| 179 |
} |
| 180 |
key.Close() |
| 181 |
b.StartTimer() |
| 182 |
|
| 183 |
for i := 0; i < b.N; i++ { |
| 184 |
key, err := bm.getKey(rwc) |
| 185 |
if err != nil { |
| 186 |
b.Fatal(err) |
| 187 |
} |
| 188 |
key.Close() |
| 189 |
} |
| 190 |
}) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Returns an x509 Certificate for the provided pubkey, signed with the provided parent certificate and key. |
| 195 |
// If the provided fields are nil, will create a self-signed certificate. |
| 196 |
func getTestCert(t *testing.T, pubKey crypto.PublicKey, parentCert *x509.Certificate, parentKey *rsa.PrivateKey) (*x509.Certificate, *rsa.PrivateKey) { |
| 197 |
t.Helper() |
| 198 |
|
| 199 |
certKey, _ := rsa.GenerateKey(rand.Reader, 2048) |
| 200 |
|
| 201 |
template := &x509.Certificate{ |
| 202 |
SerialNumber: big.NewInt(1), |
| 203 |
NotBefore: time.Now(), |
| 204 |
NotAfter: time.Now().AddDate(10, 0, 0), |
| 205 |
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, |
| 206 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 207 |
BasicConstraintsValid: true, |
| 208 |
IsCA: true, |
| 209 |
MaxPathLenZero: true, |
| 210 |
} |
| 211 |
|
| 212 |
if pubKey == nil && parentCert == nil && parentKey == nil { |
| 213 |
pubKey = certKey.Public() |
| 214 |
parentCert = template |
| 215 |
parentKey = certKey |
| 216 |
} |
| 217 |
|
| 218 |
certBytes, err := x509.CreateCertificate(rand.Reader, template, parentCert, pubKey, parentKey) |
| 219 |
if err != nil { |
| 220 |
t.Fatalf("Unable to create test certificate: %v", err) |
| 221 |
} |
| 222 |
|
| 223 |
cert, err := x509.ParseCertificate(certBytes) |
| 224 |
if err != nil { |
| 225 |
t.Fatalf("Unable to parse test certificate: %v", err) |
| 226 |
} |
| 227 |
|
| 228 |
return cert, certKey |
| 229 |
} |
| 230 |
|
| 231 |
func TestSetCert(t *testing.T) { |
| 232 |
rwc := test.GetTPM(t) |
| 233 |
defer client.CheckedClose(t, rwc) |
| 234 |
|
| 235 |
key, err := client.AttestationKeyECC(rwc) |
| 236 |
if err != nil { |
| 237 |
t.Fatalf("Unable to create key: %v", err) |
| 238 |
} |
| 239 |
|
| 240 |
ca, caKey := getTestCert(t, nil, nil, nil) |
| 241 |
akCert, _ := getTestCert(t, key.PublicKey(), ca, caKey) |
| 242 |
|
| 243 |
if err = key.SetCert(akCert); err != nil { |
| 244 |
t.Errorf("SetCert() returned error: %v", err) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestSetCertFailsIfCertificateIsNotForKey(t *testing.T) { |
| 249 |
rwc := test.GetTPM(t) |
| 250 |
defer client.CheckedClose(t, rwc) |
| 251 |
key, err := client.AttestationKeyECC(rwc) |
| 252 |
if err != nil { |
| 253 |
t.Fatalf("Unable to create key: %v", err) |
| 254 |
} |
| 255 |
|
| 256 |
otherKey, _ := rsa.GenerateKey(rand.Reader, 2048) |
| 257 |
|
| 258 |
ca, caKey := getTestCert(t, nil, nil, nil) |
| 259 |
akCert, _ := getTestCert(t, otherKey.Public(), ca, caKey) |
| 260 |
|
| 261 |
if err = key.SetCert(akCert); err == nil { |
| 262 |
t.Error("SetCert() returned successfully, expected error") |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestLoadCachedKey(t *testing.T) { |
| 267 |
rwc := test.GetTPM(t) |
| 268 |
defer client.CheckedClose(t, rwc) |
| 269 |
|
| 270 |
createdKey, err := client.NewKey(rwc, tpm2.HandleNull, client.SRKTemplateRSA()) |
| 271 |
if err != nil { |
| 272 |
t.Fatalf("NewKey() returned error: %v", err) |
| 273 |
} |
| 274 |
defer createdKey.Close() |
| 275 |
|
| 276 |
handles := []struct { |
| 277 |
name string |
| 278 |
handle tpmutil.Handle |
| 279 |
errExpected bool |
| 280 |
}{ |
| 281 |
{"successful retrieval with handle", createdKey.Handle(), false}, |
| 282 |
{"error for bad handle", tpmutil.Handle(0x0), true}, |
| 283 |
} |
| 284 |
|
| 285 |
for _, k := range handles { |
| 286 |
t.Run(k.name, func(t *testing.T) { |
| 287 |
loadedKey, err := client.LoadCachedKey(rwc, createdKey.Handle(), client.NullSession{}) |
| 288 |
if k.errExpected && err == nil { |
| 289 |
t.Fatal("LoadCachedKey() returned successfully, expected error") |
| 290 |
} else if !k.errExpected && err != nil { |
| 291 |
t.Fatalf("LoadCachedKey() returned error: %v", err) |
| 292 |
} else if k.errExpected { |
| 293 |
return |
| 294 |
} |
| 295 |
defer loadedKey.Close() |
| 296 |
|
| 297 |
if !reflect.DeepEqual(createdKey, loadedKey) { |
| 298 |
t.Errorf("Loaded key does not match created key") |
| 299 |
} |
| 300 |
}) |
| 301 |
} |
| 302 |
} |