1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package tpm2test
import (
"errors"
"testing"
. "github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/transport"
"github.com/google/go-tpm/tpm2/transport/simulator"
)
// Test creating a sealed data blob on the standard-template EK using its policy.
func TestEKPolicy(t *testing.T) {
templates := map[string]TPMTPublic{
"RSA": RSAEKTemplate,
"ECC": ECCEKTemplate,
}
// Run the whole test for each of RSA and ECC EKs.
for name, ekTemplate := range templates {
t.Run(name, func(t *testing.T) {
ekTest(t, ekTemplate)
})
}
}
func ekPolicy(t transport.TPM, handle TPMISHPolicy, nonceTPM TPM2BNonce) error {
cmd := PolicySecret{
AuthHandle: TPMRHEndorsement,
PolicySession: handle,
NonceTPM: nonceTPM,
}
_, err := cmd.Execute(t)
return err
}
// This function tests a lot of combinations of authorizing the EK policy.
func ekTest(t *testing.T, ekTemplate TPMTPublic) {
type ekTestCase struct {
name string
// Use Policy instead of PolicySession, passing the callback instead of
// managing it ourselves?
jitPolicySession bool
// Use the policy session for decrypt? (Incompatible with decryptAnotherSession)
decryptPolicySession bool
// Use another session for decrypt? (Incompatible with decryptPolicySession)
decryptAnotherSession bool
// Use a bound session?
bound bool
// Use a salted session?
salted bool
}
var cases []ekTestCase
for jit := 0; jit < 2; jit++ {
for decryptPol := 0; decryptPol < 2; decryptPol++ {
for decryptAnother := 0; decryptAnother < 2; decryptAnother++ {
if decryptPol != 0 && decryptAnother != 0 {
continue
}
for bound := 0; bound < 2; bound++ {
for salted := 0; salted < 2; salted++ {
nextCase := ekTestCase{
name: "test",
jitPolicySession: jit != 0,
decryptPolicySession: decryptPol != 0,
decryptAnotherSession: decryptAnother != 0,
bound: bound != 0,
salted: salted != 0,
}
if nextCase.jitPolicySession {
nextCase.name += "-jit"
} else {
nextCase.name += "-standalone"
}
if nextCase.decryptPolicySession {
nextCase.name += "-decrypt-same"
}
if nextCase.decryptAnotherSession {
nextCase.name += "-decrypt-another"
}
if nextCase.bound {
nextCase.name += "-bound"
}
if nextCase.salted {
nextCase.name += "-salted"
}
cases = append(cases, nextCase)
}
}
}
}
}
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}
defer thetpm.Close()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// Create the EK
createEKCmd := CreatePrimary{
PrimaryHandle: TPMRHEndorsement,
InPublic: New2B(ekTemplate),
}
createEKRsp, err := createEKCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
outPub, err := createEKRsp.OutPublic.Contents()
if err != nil {
t.Fatalf("%v", err)
}
switch outPub.Type {
case TPMAlgRSA:
rsa, err := outPub.Unique.RSA()
if err != nil {
t.Fatalf("%v", err)
}
t.Logf("EK pub:\n%x\n", rsa.Buffer)
case TPMAlgECC:
ecc, err := outPub.Unique.ECC()
if err != nil {
t.Fatalf("%v", err)
}
t.Logf("EK pub:\n%x\n%x\n", ecc.X, ecc.Y)
}
t.Logf("EK name: %x", createEKRsp.Name)
defer func() {
// Flush the EK
flush := FlushContext{FlushHandle: createEKRsp.ObjectHandle}
if _, err := flush.Execute(thetpm); err != nil {
t.Errorf("%v", err)
}
}()
// Exercise the EK's auth policy (PolicySecret[RH_ENDORSEMENT])
// by creating an object under it
data := []byte("secrets")
createBlobCmd := Create{
ParentHandle: NamedHandle{
Handle: createEKRsp.ObjectHandle,
Name: createEKRsp.Name,
},
InSensitive: TPM2BSensitiveCreate{
Sensitive: &TPMSSensitiveCreate{
Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{
Buffer: data,
}),
},
},
InPublic: New2B(TPMTPublic{
Type: TPMAlgKeyedHash,
NameAlg: TPMAlgSHA256,
ObjectAttributes: TPMAObject{
FixedTPM: true,
FixedParent: true,
UserWithAuth: true,
NoDA: true,
},
}),
}
var sessions []Session
if c.decryptAnotherSession {
sessions = append(sessions, HMAC(TPMAlgSHA1, 16, AESEncryption(128, EncryptIn)))
}
var options []AuthOption
if c.decryptPolicySession {
options = append(options, AESEncryption(128, EncryptIn))
}
if c.bound {
options = append(options, Bound(createEKRsp.ObjectHandle, createEKRsp.Name, nil))
}
if c.salted {
options = append(options, Salted(createEKRsp.ObjectHandle, *outPub))
}
var s Session
if c.jitPolicySession {
// Use the convenience function to pass a policy callback.
s = Policy(TPMAlgSHA256, 16, ekPolicy, options...)
} else {
// Set up a session we have to execute and clean up ourselves.
var cleanup func() error
var err error
s, cleanup, err = PolicySession(thetpm, TPMAlgSHA256, 16, options...)
if err != nil {
t.Fatalf("creating session: %v", err)
}
// Clean up the session at the end of the test.
defer func() {
if err := cleanup(); err != nil {
t.Fatalf("cleaning up policy session: %v", err)
}
}()
// Execute the same callback ourselves.
if err = ekPolicy(thetpm, s.Handle(), s.NonceTPM()); err != nil {
t.Fatalf("executing EK policy: %v", err)
}
}
createBlobCmd.ParentHandle = AuthHandle{
Handle: createEKRsp.ObjectHandle,
Name: createEKRsp.Name,
Auth: s,
}
if _, err := createBlobCmd.Execute(thetpm, sessions...); err != nil {
t.Fatalf("%v", err)
}
if !c.jitPolicySession {
// If we're not using a "just-in-time" session with a callback,
// we have to re-initialize the session.
if err = ekPolicy(thetpm, s.Handle(), s.NonceTPM()); err != nil {
t.Fatalf("executing EK policy: %v", err)
}
}
// Try again and make sure it succeeds again.
if _, err = createBlobCmd.Execute(thetpm, sessions...); err != nil {
t.Fatalf("%v", err)
}
if !c.jitPolicySession {
// Finally, for non-JIT policy sessions, make sure we fail if
// we don't re-initialize the session.
// This is because after using a policy session, it's as if
// PolicyRestart was called.
_, err = createBlobCmd.Execute(thetpm, sessions...)
if !errors.Is(err, TPMRCPolicyFail) {
t.Errorf("want TPM_RC_POLICY_FAIL, got %v", err)
}
var fmt1 TPMFmt1Error
if !errors.As(err, &fmt1) {
t.Errorf("want a Fmt1Error, got %v", err)
} else if isSession, session := fmt1.Session(); !isSession || session != 1 {
t.Errorf("want TPM_RC_POLICY_FAIL on session 1, got %v", err)
}
}
})
}
}
|