Skip to content

Commit

Permalink
Add getCapability and GetManufacturer (#84)
Browse files Browse the repository at this point in the history
These methods can be used to query the TPM for various information.
GetManufacturer specifically queries the Vendor ID.
  • Loading branch information
DenisKarch authored and Andrew Lytvynov committed Apr 9, 2019
1 parent ab30fe0 commit 20331ed
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
15 changes: 15 additions & 0 deletions tpm/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ func getPubKey(rw io.ReadWriter, keyHandle tpmutil.Handle, ca *commandAuth) (*pu
return &pk, &ra, ret, nil
}

// getCapability reads the requested capability and sub-capability from NVRAM
func getCapability(rw io.ReadWriter, cap, subcap uint32) ([]byte, error) {
subCapBytes, err := tpmutil.Pack(subcap)
if err != nil {
return nil, err
}
var b []byte
in := []interface{}{cap, subCapBytes}
out := []interface{}{&b}
if _, err := submitTPMRequest(rw, tagRQUCommand, ordGetCapability, in, out); err != nil {
return nil, err
}
return b, nil
}

func nvReadValue(rw io.ReadWriter, index, offset, len uint32, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
var b []byte
var ra responseAuth
Expand Down
8 changes: 7 additions & 1 deletion tpm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ const (

// Capability types.
const (
capHandle uint32 = 0x00000014
capProperty uint32 = 0x00000005
capHandle uint32 = 0x00000014
)

// SubCapabilities
const (
tpmCapPropManufacturer uint32 = 0x00000103
)

// Entity types. The LSB gives the entity type, and the MSB (currently fixed to
Expand Down
13 changes: 6 additions & 7 deletions tpm/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,10 @@ var OpenTPM = tpmutil.OpenTPM

// GetKeys gets the list of handles for currently-loaded TPM keys.
func GetKeys(rw io.ReadWriter) ([]tpmutil.Handle, error) {
var b []byte
subCap, err := tpmutil.Pack(rtKey)
b, err := getCapability(rw, capHandle, rtKey)
if err != nil {
return nil, err
}
in := []interface{}{capHandle, subCap}
out := []interface{}{&b}
if _, err := submitTPMRequest(rw, tagRQUCommand, ordGetCapability, in, out); err != nil {
return nil, err
}
var handles []tpmutil.Handle
if _, err := tpmutil.Unpack(b, &handles); err != nil {
return nil, err
Expand Down Expand Up @@ -981,6 +975,11 @@ func ReadPubEK(rw io.ReadWriter) ([]byte, error) {
return tpmutil.Pack(pk)
}

// GetManufacturer returns the manufacturer ID
func GetManufacturer(rw io.ReadWriter) ([]byte, error) {
return getCapability(rw, capProperty, tpmCapPropManufacturer)
}

// OwnerClear uses owner auth to clear the TPM. After this operation, the TPM
// can change ownership.
func OwnerClear(rw io.ReadWriter, ownerAuth digest) error {
Expand Down
12 changes: 12 additions & 0 deletions tpm/tpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func TestGetKeys(t *testing.T) {
t.Logf("Got %d keys: % d\n", len(handles), handles)
}

func TestGetManufacturer(t *testing.T) {
rwc := openTPMOrSkip(t)
defer rwc.Close()

vendorID, err := GetManufacturer(rwc)
if err != nil {
t.Fatal("Couldn't read VendorID from TPM:", err)
}

t.Logf("TPM VendorID: %v\n", vendorID)
}

func TestPcrExtend(t *testing.T) {
rwc := openTPMOrSkip(t)
defer rwc.Close()
Expand Down

0 comments on commit 20331ed

Please sign in to comment.