Skip to content

Commit

Permalink
policyutil: ensure Policy.Execute indicates cpHash has been set when …
Browse files Browse the repository at this point in the history
…set by TPM2_PolicySigned
  • Loading branch information
chrisccoulson committed Apr 4, 2024
1 parent 5ea965f commit f6499c3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 6 additions & 4 deletions policyutil/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1498,8 +1498,8 @@ type PolicyExecuteResult struct {
policyNvWritten *bool
}

// CommandCode returns the command code if a TPM2_PolicyCommandCode assertion
// was executed.
// CommandCode returns the command code if a TPM2_PolicyCommandCode or
// TPM2_PolicyDuplicationSelect assertion was executed.
func (r *PolicyExecuteResult) CommandCode() (code tpm2.CommandCode, set bool) {
if r.policyCommandCode == nil {
return 0, false
Expand All @@ -1508,15 +1508,17 @@ func (r *PolicyExecuteResult) CommandCode() (code tpm2.CommandCode, set bool) {
}

// CpHash returns the command parameter hash if a TPM2_PolicyCpHash assertion
// was executed.
// was executed or a TPM2_PolicySecret or TPM2_PolicySigned assertion was executed
// with a cpHash.
func (r *PolicyExecuteResult) CpHash() (cpHashA tpm2.Digest, set bool) {
if len(r.policyCpHash) == 0 {
return nil, false
}
return r.policyCpHash, true
}

// CpHash returns the name hash if a TPM2_PolicyNameHash assertion was executed.
// NameHash returns the name hash if a TPM2_PolicyNameHash or TPM2_PolicyDuplicationSelect
// assertion was executed.
func (r *PolicyExecuteResult) NameHash() (nameHash tpm2.Digest, set bool) {
if len(r.policyNameHash) == 0 {
return nil, false
Expand Down
10 changes: 8 additions & 2 deletions policyutil/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1420,8 +1420,14 @@ func (s *policySuite) testPolicySigned(c *C, data *testExecutePolicySignedData)
c.Check(result.Path, Equals, "")
_, set := result.CommandCode()
c.Check(set, internal_testutil.IsFalse)
_, set = result.CpHash()
c.Check(set, internal_testutil.IsFalse)
if len(data.cpHashA) > 0 {
cpHash, set := result.CpHash()
c.Check(set, internal_testutil.IsTrue)
c.Check(cpHash, DeepEquals, data.cpHashA)
} else {
_, set = result.CpHash()
c.Check(set, internal_testutil.IsFalse)
}
_, set = result.NameHash()
c.Check(set, internal_testutil.IsFalse)
_, set = result.NvWritten()
Expand Down
10 changes: 10 additions & 0 deletions policyutil/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ func (s *recorderPolicySession) PolicySigned(authKey tpm2.ResourceContext, inclu
AuthName: authKey.Name(),
PolicyRef: policyRef,
})
if len(cpHashA) > 0 {
if err := s.PolicyCpHash(cpHashA); err != nil {
return nil, nil, err
}
}
return nil, nil, nil
}

Expand All @@ -634,6 +639,11 @@ func (s *recorderPolicySession) PolicySecret(authObject tpm2.ResourceContext, cp
AuthName: authObject.Name(),
PolicyRef: policyRef,
})
if len(cpHashA) > 0 {
if err := s.PolicyCpHash(cpHashA); err != nil {
return nil, nil, err
}
}
return nil, nil, nil
}

Expand Down

0 comments on commit f6499c3

Please sign in to comment.