diff --git a/testutil/suites.go b/testutil/suites.go index 9caa6b2..c983fb0 100644 --- a/testutil/suites.go +++ b/testutil/suites.go @@ -82,18 +82,21 @@ type CommandRecordC struct { *CommandRecord } +// Deprecated: use [CommandRecord.CmdCode]. func (r *CommandRecordC) GetCommandCode(c *C) tpm2.CommandCode { code, err := r.CommandRecord.GetCommandCode() c.Assert(err, IsNil) return code } +// Deprecated: use [CommandRecord.CmdHandles], [CommandRecord.CmdAuthArea] and [CommandRecord.CpBytes]. func (r *CommandRecordC) UnmarshalCommand(c *C) (handles tpm2.HandleList, authArea []tpm2.AuthCommand, parameters []byte) { handles, authArea, parameters, err := r.CommandRecord.UnmarshalCommand() c.Assert(err, IsNil) return handles, authArea, parameters } +// Deprecated: use [CommandRecord.RspCode], [CommandRecord.RspHandle], [CommandRecord.RpBytes] and [CommandRecord.RspAuthArea]. func (r *CommandRecordC) UnmarshalResponse(c *C) (rc tpm2.ResponseCode, handle tpm2.Handle, parameters []byte, authArea []tpm2.AuthResponse) { rc, handle, parameters, authArea, err := r.CommandRecord.UnmarshalResponse() c.Assert(err, IsNil) diff --git a/testutil/suites_test.go b/testutil/suites_test.go index 962ca91..95988b3 100644 --- a/testutil/suites_test.go +++ b/testutil/suites_test.go @@ -367,36 +367,34 @@ func (s *tpmTestSuiteProper) TestCommandLog(c *C) { c.Assert(s.CommandLog(), internal_testutil.LenEquals, 2) - c.Check(s.CommandLog()[0].GetCommandCode(c), Equals, tpm2.CommandSelfTest) - cHandles, cAuthArea, cpBytes := s.CommandLog()[0].UnmarshalCommand(c) - c.Check(cHandles, internal_testutil.LenEquals, 0) - c.Check(cAuthArea, internal_testutil.LenEquals, 0) + cmd := s.CommandLog()[0] + c.Check(cmd.CmdCode, Equals, tpm2.CommandSelfTest) + c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0) + c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0) var fullTest bool - _, err = mu.UnmarshalFromBytes(cpBytes, &fullTest) + _, err = mu.UnmarshalFromBytes(cmd.CpBytes, &fullTest) c.Check(err, IsNil) c.Check(fullTest, internal_testutil.IsTrue) - rc, rHandle, rpBytes, rAuthArea := s.CommandLog()[0].UnmarshalResponse(c) - c.Check(rc, Equals, tpm2.ResponseSuccess) - c.Check(rHandle, Equals, tpm2.HandleUnassigned) - c.Check(rpBytes, internal_testutil.LenEquals, 0) - c.Check(rAuthArea, internal_testutil.LenEquals, 0) + c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess) + c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned) + c.Check(cmd.RpBytes, internal_testutil.LenEquals, 0) + c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0) - c.Check(s.CommandLog()[1].GetCommandCode(c), Equals, tpm2.CommandGetTestResult) - cHandles, cAuthArea, cpBytes = s.CommandLog()[1].UnmarshalCommand(c) - c.Check(cHandles, internal_testutil.LenEquals, 0) - c.Check(cAuthArea, internal_testutil.LenEquals, 0) - c.Check(cpBytes, internal_testutil.LenEquals, 0) + cmd = s.CommandLog()[1] + c.Check(cmd.CmdCode, Equals, tpm2.CommandGetTestResult) + c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0) + c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0) + c.Check(cmd.CpBytes, internal_testutil.LenEquals, 0) - rc, rHandle, rpBytes, rAuthArea = s.CommandLog()[1].UnmarshalResponse(c) - c.Check(rc, Equals, tpm2.ResponseSuccess) - c.Check(rHandle, Equals, tpm2.HandleUnassigned) - c.Check(rAuthArea, internal_testutil.LenEquals, 0) + c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess) + c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned) + c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0) var outData2 tpm2.MaxBuffer var testResult2 tpm2.ResponseCode - _, err = mu.UnmarshalFromBytes(rpBytes, &outData2, &testResult2) + _, err = mu.UnmarshalFromBytes(cmd.RpBytes, &outData2, &testResult2) c.Check(err, IsNil) c.Check(outData2, DeepEquals, outData) c.Check(testResult2, Equals, testResult) @@ -407,7 +405,7 @@ func (s *tpmTestSuiteProper) TestLastCommand(c *C) { _, _, err := s.TPM.GetTestResult() c.Check(err, IsNil) - c.Check(s.LastCommand(c).GetCommandCode(c), Equals, tpm2.CommandGetTestResult) + c.Check(s.LastCommand(c).CmdCode, Equals, tpm2.CommandGetTestResult) } func (s *tpmTestSuiteProper) TestForgetCommands(c *C) { diff --git a/testutil/transport.go b/testutil/transport.go index 1f44346..ec88524 100644 --- a/testutil/transport.go +++ b/testutil/transport.go @@ -200,7 +200,6 @@ type daParams struct { type cmdContext struct { info *commandInfo - command []byte code tpm2.CommandCode handles tpm2.HandleList authArea []tpm2.AuthCommand @@ -218,21 +217,31 @@ var savedObjects []*savedObject // CommandRecord provides information about a command executed via // the Transport interface. type CommandRecord struct { - cmdInfo *commandInfo - commandPacket tpm2.CommandPacket - responsePacket tpm2.ResponsePacket + CmdCode tpm2.CommandCode + CmdHandles tpm2.HandleList + CmdAuthArea []tpm2.AuthCommand + CpBytes []byte + + RspCode tpm2.ResponseCode + RspHandle tpm2.Handle // Will be tpm2.HandleUnassigned if no handle was returned + RpBytes []byte + RspAuthArea []tpm2.AuthResponse } // GetCommandCode returns the command code associated with this record. +// +// Deprecated: use the CmdCode field. func (r *CommandRecord) GetCommandCode() (tpm2.CommandCode, error) { - return r.commandPacket.GetCommandCode() + return r.CmdCode, nil } // UnmarshalCommand unmarshals the command packet associated with this // record, returning the handles, auth area and parameters. The parameters // will still be in the TPM wire format. +// +// Deprecated: use the CmdHandles, CmdAuthArea and CpBytes fields. func (r *CommandRecord) UnmarshalCommand() (handles tpm2.HandleList, authArea []tpm2.AuthCommand, parameters []byte, err error) { - return r.commandPacket.Unmarshal(r.cmdInfo.cmdHandles) + return r.CmdHandles, r.CmdAuthArea, r.CpBytes, nil } // UnmarshalResponse unmarshals the response packet associated with this @@ -240,17 +249,10 @@ func (r *CommandRecord) UnmarshalCommand() (handles tpm2.HandleList, authArea [] // The parameters will still be in the TPM wire format. For commands that // don't respond with a handle, the returned handle will be // tpm2.HandleUnassigned. +// +// Deprecated: use the RspCode, RspHandle, RpBytes and RspAuthArea fields. func (r *CommandRecord) UnmarshalResponse() (rc tpm2.ResponseCode, handle tpm2.Handle, parameters []byte, authArea []tpm2.AuthResponse, err error) { - handle = tpm2.HandleUnassigned - var pHandle *tpm2.Handle - if r.cmdInfo.rspHandle { - pHandle = &handle - } - rc, parameters, authArea, err = r.responsePacket.Unmarshal(pHandle) - if err != nil { - return 0, handle, nil, nil, err - } - return rc, handle, parameters, authArea, nil + return r.RspCode, r.RspHandle, r.RpBytes, r.RspAuthArea, nil } // TCTI is a special proxy inteface used for testing, which wraps a real interface. @@ -310,14 +312,14 @@ func (t *Transport) processCommandDoneIfPossible() error { return nil } - var rHandle tpm2.Handle + rHandle := tpm2.HandleUnassigned var pHandle *tpm2.Handle // Try to unpack the response packet if cmd.info.rspHandle { pHandle = &rHandle } - rc, rpBytes, _, err := tpm2.ReadResponsePacket(bytes.NewReader(cmd.response.Bytes()), pHandle) + rc, rpBytes, rAuthArea, err := tpm2.ReadResponsePacket(bytes.NewReader(cmd.response.Bytes()), pHandle) if err != nil { return fmt.Errorf("cannot unmarshal response: %w", err) } @@ -325,7 +327,16 @@ func (t *Transport) processCommandDoneIfPossible() error { t.currentCmd = nil if !t.disableCommandLogging { - t.CommandLog = append(t.CommandLog, &CommandRecord{cmd.info, cmd.command, cmd.response.Bytes()}) + t.CommandLog = append(t.CommandLog, &CommandRecord{ + CmdCode: cmd.code, + CmdHandles: cmd.handles, + CmdAuthArea: cmd.authArea, + CpBytes: cmd.pBytes, + RspCode: rc, + RspHandle: rHandle, + RpBytes: rpBytes, + RspAuthArea: rAuthArea, + }) } if rc != tpm2.ResponseSuccess { @@ -697,7 +708,6 @@ func (t *Transport) sendCommand(data []byte) (int, error) { t.currentCmd = &cmdContext{ info: &cmdInfo, - command: data, code: hdr.CommandCode, handles: handles, authArea: authArea, diff --git a/testutil/transport_test.go b/testutil/transport_test.go index d44fe50..4c78739 100644 --- a/testutil/transport_test.go +++ b/testutil/transport_test.go @@ -129,16 +129,15 @@ func (s *transportSuite) TestCommandLog(c *C) { c.Check(s.CommandLog(), internal_testutil.LenEquals, 2) - cmd := s.CommandLog()[0].GetCommandCode(c) - c.Check(cmd, Equals, tpm2.CommandLoadExternal) - cmdHandles, cmdAuthArea, cpBytes := s.CommandLog()[0].UnmarshalCommand(c) - c.Check(cmdHandles, internal_testutil.LenEquals, 0) - c.Check(cmdAuthArea, internal_testutil.LenEquals, 0) + cmd := s.CommandLog()[0] + c.Check(cmd.CmdCode, Equals, tpm2.CommandLoadExternal) + c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0) + c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0) var inSensitiveBytes []byte var inPublicBytes []byte var hierarchy tpm2.Handle - _, err = mu.UnmarshalFromBytes(cpBytes, &inSensitiveBytes, &inPublicBytes, &hierarchy) + _, err = mu.UnmarshalFromBytes(cmd.CpBytes, &inSensitiveBytes, &inPublicBytes, &hierarchy) c.Check(err, IsNil) var inPublic *tpm2.Public _, err = mu.UnmarshalFromBytes(inPublicBytes, &inPublic) @@ -148,39 +147,36 @@ func (s *transportSuite) TestCommandLog(c *C) { c.Check(inPublic, TPMValueDeepEquals, &public) c.Check(hierarchy, Equals, tpm2.HandleOwner) - rc, rHandle, rpBytes, rspAuthArea := s.CommandLog()[0].UnmarshalResponse(c) - c.Check(rHandle, Equals, object.Handle()) - c.Check(rc, Equals, tpm2.ResponseSuccess) - c.Check(rspAuthArea, internal_testutil.LenEquals, 0) + c.Check(cmd.RspHandle, Equals, object.Handle()) + c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess) + c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0) var name tpm2.Name - _, err = mu.UnmarshalFromBytes(rpBytes, &name) + _, err = mu.UnmarshalFromBytes(cmd.RpBytes, &name) c.Check(err, IsNil) c.Check(name, DeepEquals, object.Name()) - cmd = s.CommandLog()[1].GetCommandCode(c) - c.Check(cmd, Equals, tpm2.CommandGetCapability) - cmdHandles, cmdAuthArea, cpBytes = s.CommandLog()[1].UnmarshalCommand(c) - c.Check(cmdHandles, internal_testutil.LenEquals, 0) - c.Check(cmdAuthArea, internal_testutil.LenEquals, 0) + cmd = s.CommandLog()[1] + c.Check(cmd.CmdCode, Equals, tpm2.CommandGetCapability) + c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0) + c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0) var capability tpm2.Capability var property uint32 var propertyCount uint32 - _, err = mu.UnmarshalFromBytes(cpBytes, &capability, &property, &propertyCount) + _, err = mu.UnmarshalFromBytes(cmd.CpBytes, &capability, &property, &propertyCount) c.Check(err, IsNil) c.Check(capability, Equals, tpm2.CapabilityHandles) c.Check(property, Equals, uint32(object.Handle())) c.Check(propertyCount, Equals, uint32(1)) - rc, rHandle, rpBytes, rspAuthArea = s.CommandLog()[1].UnmarshalResponse(c) - c.Check(rc, Equals, tpm2.ResponseSuccess) - c.Check(rHandle, Equals, tpm2.HandleUnassigned) - c.Check(rspAuthArea, internal_testutil.LenEquals, 0) + c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess) + c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned) + c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0) var moreData bool var capabilityData tpm2.CapabilityData - _, err = mu.UnmarshalFromBytes(rpBytes, &moreData, &capabilityData) + _, err = mu.UnmarshalFromBytes(cmd.RpBytes, &moreData, &capabilityData) c.Check(err, IsNil) c.Check(moreData, Equals, false) c.Check(capabilityData, DeepEquals, tpm2.CapabilityData{