1 // This file was extracted from the TCG Published
2 // Trusted Platform Module Library
3 // Part 4: Supporting Routines
4 // Family "2.0"
5 // Level 00 Revision 01.16
6 // October 30, 2014
7 
8 #include "Tpm.h"
9 #include "InternalRoutines.h"
10 //
11 //
12 //          Functions
13 //
14 //          HandleGetType()
15 //
16 //     This function returns the type of a handle which is the MSO of the handle.
17 //
18 TPM_HT
HandleGetType(TPM_HANDLE handle)19 HandleGetType(
20      TPM_HANDLE           handle             // IN: a handle to be checked
21      )
22 {
23      // return the upper bytes of input data
24      return (TPM_HT) ((handle & HR_RANGE_MASK) >> HR_SHIFT);
25 }
26 //
27 //
28 //          NextPermanentHandle()
29 //
30 //     This function returns the permanent handle that is equal to the input value or is the next higher value. If
31 //     there is no handle with the input value and there is no next higher value, it returns 0:
32 //
33 //     Return Value                      Meaning
34 //
35 TPM_HANDLE
NextPermanentHandle(TPM_HANDLE inHandle)36 NextPermanentHandle(
37      TPM_HANDLE           inHandle           // IN: the handle to check
38      )
39 {
40      // If inHandle is below the start of the range of permanent handles
41      // set it to the start and scan from there
42      if(inHandle < TPM_RH_FIRST)
43          inHandle = TPM_RH_FIRST;
44      // scan from input value untill we find an implemented permanent handle
45      // or go out of range
46      for(; inHandle <= TPM_RH_LAST; inHandle++)
47      {
48          switch (inHandle)
49          {
50              case TPM_RH_OWNER:
51              case TPM_RH_NULL:
52              case TPM_RS_PW:
53              case TPM_RH_LOCKOUT:
54              case TPM_RH_ENDORSEMENT:
55              case TPM_RH_PLATFORM:
56              case TPM_RH_PLATFORM_NV:
57      #ifdef VENDOR_PERMANENT
58              case VENDOR_PERMANENT:
59      #endif
60                  return inHandle;
61                   break;
62               default:
63                   break;
64          }
65      }
66      // Out of range on the top
67      return 0;
68 }
69 //
70 //
71 //          PermanentCapGetHandles()
72 //
73 //     This function returns a list of the permanent handles of PCR, started from handle. If handle is larger than
74 //     the largest permanent handle, an empty list will be returned with more set to NO.
75 //
76 //     Return Value                      Meaning
77 //
78 //     YES                               if there are more handles available
79 //     NO                                all the available handles has been returned
80 //
81 TPMI_YES_NO
PermanentCapGetHandles(TPM_HANDLE handle,UINT32 count,TPML_HANDLE * handleList)82 PermanentCapGetHandles(
83      TPM_HANDLE         handle,              // IN: start handle
84      UINT32             count,               // IN: count of returned handle
85      TPML_HANDLE       *handleList           // OUT: list of handle
86      )
87 {
88      TPMI_YES_NO       more = NO;
89      UINT32            i;
90      pAssert(HandleGetType(handle) == TPM_HT_PERMANENT);
91      // Initialize output handle list
92      handleList->count = 0;
93      // The maximum count of handles we may return is MAX_CAP_HANDLES
94      if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES;
95      // Iterate permanent handle range
96      for(i = NextPermanentHandle(handle);
97               i != 0; i = NextPermanentHandle(i+1))
98      {
99          if(handleList->count < count)
100          {
101               // If we have not filled up the return list, add this permanent
102               // handle to it
103               handleList->handle[handleList->count] = i;
104               handleList->count++;
105          }
106          else
107          {
108               // If the return list is full but we still have permanent handle
109               // available, report this and stop iterating
110               more = YES;
111               break;
112          }
113      }
114      return more;
115 }
116