Skip to content

Commit 8f3ee81

Browse files
committed
refactor: handle provisioning
1 parent bb5085e commit 8f3ee81

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

tpm2/reflect.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,10 @@ func unmarshalParameter[C Command[R, *R], R any](buf *bytes.Buffer, cmd *C, i in
903903
return unmarshalStructField(buf, cmdValue, fieldIndex)
904904
}
905905

906-
// populateHandlesFromNames populates the handle fields of a command with NamedHandles
906+
// populateHandlesFromNames populates the handle fields of a command with handles
907907
// created from the provided names.
908908
//
909-
// Supports different handle types based on struct tags:
910-
// - gotpm:"handle" → NamedHandle (without handle value)
911-
// - gotpm:"handle,auth" → AuthHandle (without handle value and nil Auth)
909+
// All handle fields are populated with [UnmarshalledHandle]
912910
func populateHandlesFromNames[C Command[R, *R], R any](cmd *C, names []TPM2BName) error {
913911
cmdValue := reflect.ValueOf(cmd).Elem()
914912
cmdType := reflect.TypeOf(*cmd)
@@ -930,19 +928,8 @@ func populateHandlesFromNames[C Command[R, *R], R any](cmd *C, names []TPM2BName
930928
return fmt.Errorf("not enough names for handle field %d", i)
931929
}
932930

933-
// Create the appropriate handle type based on the "auth" tag
934-
var handleValue any
935-
if hasTag(field, "auth") {
936-
handleValue = AuthHandle{
937-
Handle: 0, // Handle value not available from CommandPreimage
938-
Name: names[nameIdx],
939-
Auth: nil, // No session available
940-
}
941-
} else {
942-
handleValue = NamedHandle{
943-
Handle: 0, // Handle value not available from CommandPreimage
944-
Name: names[nameIdx],
945-
}
931+
handleValue := UnmarshalledHandle{
932+
Name: names[nameIdx],
946933
}
947934

948935
cmdValue.Field(i).Set(reflect.ValueOf(handleValue))

tpm2/tpm2.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ func (h AuthHandle) KnownName() *TPM2BName {
5959
return h.Handle.KnownName()
6060
}
6161

62+
// invalidHandleValue is the sentinel value used for handles that have been
63+
// reconstructed from unmarshalling.
64+
// This value is intentionally invalid to prevent accidental use with a real TPM.
65+
const invalidHandleValue TPMHandle = 0xFFFFFFFF
66+
67+
// UnmarshalledHandle represents a handle reconstructed from unmarshalling.
68+
// This type is used for audit and inspection purposes where only the Name is
69+
// available, not the actual TPM handle value.
70+
//
71+
// The HandleValue() method returns [invalidHandleValue] to prevent accidental
72+
// use of these handles with a real TPM.
73+
type UnmarshalledHandle struct {
74+
Name TPM2BName
75+
}
76+
77+
// Returns invalidHandleValue since unmarshalled handles don't have real TPM handle values.
78+
func (h UnmarshalledHandle) HandleValue() uint32 {
79+
return uint32(invalidHandleValue)
80+
}
81+
82+
func (h UnmarshalledHandle) KnownName() *TPM2BName {
83+
return &h.Name
84+
}
85+
6286
// Command is an interface for any TPM command, parameterized by its response
6387
// type.
6488
type Command[R any, PR *R] interface {

0 commit comments

Comments
 (0)