| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/google/go-cmp/cmp" |
| 7 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 8 |
. "github.com/google/go-tpm/tpm2" |
| 9 |
"github.com/google/go-tpm/tpm2/transport" |
| 10 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 11 |
) |
| 12 |
|
| 13 |
func ReadPublicName(t *testing.T, handle TPMHandle, thetpm transport.TPM) TPM2BName { |
| 14 |
readPublic := ReadPublic{ |
| 15 |
ObjectHandle: handle, |
| 16 |
} |
| 17 |
|
| 18 |
rspRP, err := readPublic.Execute(thetpm) |
| 19 |
if err != nil { |
| 20 |
t.Fatalf("Failed to read public: %v", err) |
| 21 |
} |
| 22 |
|
| 23 |
return rspRP.Name |
| 24 |
} |
| 25 |
|
| 26 |
func TestCombinedContext(t *testing.T) { |
| 27 |
thetpm, err := simulator.OpenSimulator() |
| 28 |
if err != nil { |
| 29 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 30 |
} |
| 31 |
defer thetpm.Close() |
| 32 |
|
| 33 |
createPrimary := CreatePrimary{ |
| 34 |
PrimaryHandle: TPMRHOwner, |
| 35 |
|
| 36 |
InPublic: New2B(TPMTPublic{ |
| 37 |
Type: TPMAlgRSA, |
| 38 |
NameAlg: TPMAlgSHA256, |
| 39 |
ObjectAttributes: TPMAObject{ |
| 40 |
SignEncrypt: true, |
| 41 |
FixedTPM: true, |
| 42 |
FixedParent: true, |
| 43 |
SensitiveDataOrigin: true, |
| 44 |
UserWithAuth: true, |
| 45 |
}, |
| 46 |
Parameters: NewTPMUPublicParms( |
| 47 |
TPMAlgRSA, |
| 48 |
&TPMSRSAParms{ |
| 49 |
Scheme: TPMTRSAScheme{ |
| 50 |
Scheme: TPMAlgRSASSA, |
| 51 |
Details: NewTPMUAsymScheme( |
| 52 |
TPMAlgRSASSA, &TPMSSigSchemeRSASSA{ |
| 53 |
HashAlg: TPMAlgSHA256, |
| 54 |
}, |
| 55 |
), |
| 56 |
}, |
| 57 |
KeyBits: 2048, |
| 58 |
}, |
| 59 |
), |
| 60 |
}), |
| 61 |
CreationPCR: TPMLPCRSelection{ |
| 62 |
PCRSelections: []TPMSPCRSelection{ |
| 63 |
{ |
| 64 |
Hash: TPMAlgSHA1, |
| 65 |
PCRSelect: PCClientCompatible.PCRs(7), |
| 66 |
}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
rspCP, err := createPrimary.Execute(thetpm) |
| 72 |
if err != nil { |
| 73 |
t.Fatalf("could not create key: %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
flushContextObject := FlushContext{FlushHandle: rspCP.ObjectHandle} |
| 77 |
defer flushContextObject.Execute(thetpm) |
| 78 |
|
| 79 |
contextSave := ContextSave{ |
| 80 |
SaveHandle: rspCP.ObjectHandle, |
| 81 |
} |
| 82 |
|
| 83 |
rspCS, err := contextSave.Execute(thetpm) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("ContextSave failed: %v", err) |
| 86 |
} |
| 87 |
|
| 88 |
contextLoad := ContextLoad{ |
| 89 |
Context: rspCS.Context, |
| 90 |
} |
| 91 |
|
| 92 |
rspCL, err := contextLoad.Execute(thetpm) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("ContextLoad failed: %v", err) |
| 95 |
} |
| 96 |
|
| 97 |
flushContextLoaded := FlushContext{FlushHandle: rspCL.LoadedHandle} |
| 98 |
defer flushContextLoaded.Execute(thetpm) |
| 99 |
|
| 100 |
rspCLName := ReadPublicName(t, rspCL.LoadedHandle, thetpm) |
| 101 |
rspCPName := ReadPublicName(t, rspCP.ObjectHandle, thetpm) |
| 102 |
|
| 103 |
if !cmp.Equal(rspCLName, rspCPName, cmpopts.IgnoreUnexported(rspCLName)) { |
| 104 |
t.Error("Mismatch between public returned from ContextLoad & CreateLoaded") |
| 105 |
} |
| 106 |
} |