Coverage Report

Created: 2025-07-12 06:35

/src/ibmswtpm2/src/Handle.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************************/
2
/*                    */
3
/*    fUnctions that return the type of a handle.         */
4
/*           Written by Ken Goldman       */
5
/*           IBM Thomas J. Watson Research Center     */
6
/*            $Id: Handle.c 1047 2017-07-20 18:27:34Z kgoldman $      */
7
/*                    */
8
/*  Licenses and Notices              */
9
/*                    */
10
/*  1. Copyright Licenses:              */
11
/*                    */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in  */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,    */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create  */
15
/*    derivative works, distribute, display and perform the Source Code and */
16
/*    derivative works thereof, and to grant others the rights granted herein.  */
17
/*                    */
18
/*  - The TCG grants to the user of the other parts of the specification  */
19
/*    (other than the Source Code) the rights to reproduce, distribute,   */
20
/*    display, and perform the specification solely for the purpose of    */
21
/*    developing products based on such documents.        */
22
/*                    */
23
/*  2. Source Code Distribution Conditions:         */
24
/*                    */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,  */
26
/*    this list of conditions and the following disclaimers.      */
27
/*                    */
28
/*  - Redistributions in binary form must reproduce the above copyright   */
29
/*    licenses, this list of conditions and the following disclaimers in the  */
30
/*    documentation and/or other materials provided with the distribution.  */
31
/*                    */
32
/*  3. Disclaimers:               */
33
/*                    */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.   */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for    */
39
/*  information on specification licensing rights available through TCG   */
40
/*  membership agreements.              */
41
/*                    */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED   */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR   */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR    */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY    */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.   */
47
/*                    */
48
/*  - Without limitation, TCG and its members and licensors disclaim all  */
49
/*    liability, including liability for infringement of any proprietary  */
50
/*    rights, relating to use of information in this specification and to the */
51
/*    implementation of this specification, and TCG disclaims all liability for */
52
/*    cost of procurement of substitute goods or services, lost profits, loss   */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,  */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,  */
55
/*    arising in any way out of use or reliance upon this specification or any  */
56
/*    information herein.             */
57
/*                    */
58
/*  (c) Copyright IBM Corp. and others, 2016, 2017        */
59
/*                    */
60
/********************************************************************************/
61
62
/* 9.6 Handle.c */
63
/* 9.6.1 Description */
64
/* This file contains the functions that return the type of a handle. */
65
/* 9.6.2 Includes */
66
#include "Tpm.h"
67
/* 9.6.3 Functions */
68
/* 9.6.3.1 HandleGetType() */
69
/* This function returns the type of a handle which is the MSO of the handle. */
70
TPM_HT
71
HandleGetType(
72
        TPM_HANDLE       handle         // IN: a handle to be checked
73
        )
74
4.30k
{
75
    // return the upper bytes of input data
76
4.30k
    return (TPM_HT)((handle & HR_RANGE_MASK) >> HR_SHIFT);
77
4.30k
}
78
/* 9.6.3.2 NextPermanentHandle() */
79
/* This function returns the permanent handle that is equal to the input value or is the next higher
80
   value. If there is no handle with the input value and there is no next higher value, it returns
81
   0: */
82
TPM_HANDLE
83
NextPermanentHandle(
84
        TPM_HANDLE       inHandle       // IN: the handle to check
85
        )
86
0
{
87
    // If inHandle is below the start of the range of permanent handles
88
    // set it to the start and scan from there
89
0
    if(inHandle < TPM_RH_FIRST)
90
0
  inHandle = TPM_RH_FIRST;
91
    // scan from input value until we find an implemented permanent handle
92
    // or go out of range
93
0
    for(; inHandle <= TPM_RH_LAST; inHandle++)
94
0
  {
95
0
      switch(inHandle)
96
0
    {
97
0
      case TPM_RH_OWNER:
98
0
      case TPM_RH_NULL:
99
0
      case TPM_RS_PW:
100
0
      case TPM_RH_LOCKOUT:
101
0
      case TPM_RH_ENDORSEMENT:
102
0
      case TPM_RH_PLATFORM:
103
0
      case TPM_RH_PLATFORM_NV:
104
#ifdef  VENDOR_PERMANENT
105
      case VENDOR_PERMANENT:
106
#endif
107
0
        return inHandle;
108
0
        break;
109
0
      default:
110
0
        break;
111
0
    }
112
0
  }
113
    // Out of range on the top
114
0
    return 0;
115
0
}
116
/* 9.6.3.3 PermanentCapGetHandles() */
117
/* This function returns a list of the permanent handles of PCR, started from handle. If handle is
118
   larger than the largest permanent handle, an empty list will be returned with more set to NO. */
119
/* Return Values Meaning */
120
/* YES if there are more handles available */
121
/* NO all the available handles has been returned */
122
TPMI_YES_NO
123
PermanentCapGetHandles(
124
           TPM_HANDLE       handle,        // IN: start handle
125
           UINT32           count,         // IN: count of returned handles
126
           TPML_HANDLE     *handleList     // OUT: list of handle
127
           )
128
0
{
129
0
    TPMI_YES_NO     more = NO;
130
0
    UINT32          i;
131
0
    pAssert(HandleGetType(handle) == TPM_HT_PERMANENT);
132
    // Initialize output handle list
133
0
    handleList->count = 0;
134
    // The maximum count of handles we may return is MAX_CAP_HANDLES
135
0
    if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES;
136
    // Iterate permanent handle range
137
0
    for(i = NextPermanentHandle(handle);
138
0
  i != 0; i = NextPermanentHandle(i + 1))
139
0
  {
140
0
      if(handleList->count < count)
141
0
    {
142
        // If we have not filled up the return list, add this permanent
143
        // handle to it
144
0
        handleList->handle[handleList->count] = i;
145
0
        handleList->count++;
146
0
    }
147
0
      else
148
0
    {
149
        // If the return list is full but we still have permanent handle
150
        // available, report this and stop iterating
151
0
        more = YES;
152
0
        break;
153
0
    }
154
0
  }
155
0
    return more;
156
0
}
157
/* 9.6.3.4 PermanentHandleGetPolicy() */
158
/* This function returns a list of the permanent handles of PCR, started from handle. If handle is
159
   larger than the largest permanent handle, an empty list will be returned with more set to NO. */
160
/* Return Values Meaning */
161
/* YES if there are more handles available */
162
/* NO all the available handles has been returned */
163
TPMI_YES_NO
164
PermanentHandleGetPolicy(
165
       TPM_HANDLE           handle,        // IN: start handle
166
       UINT32               count,         // IN: max count of returned handles
167
       TPML_TAGGED_POLICY  *policyList     // OUT: list of handle
168
       )
169
0
{
170
0
    TPMI_YES_NO     more = NO;
171
0
    pAssert(HandleGetType(handle) == TPM_HT_PERMANENT);
172
    // Initialize output handle list
173
0
    policyList->count = 0;
174
    // The maximum count of policies we may return is MAX_TAGGED_POLICIES
175
0
    if(count > MAX_TAGGED_POLICIES)
176
0
  count = MAX_TAGGED_POLICIES;
177
    // Iterate permanent handle range
178
0
    for(handle = NextPermanentHandle(handle);
179
0
  handle != 0;
180
0
  handle = NextPermanentHandle(handle + 1))
181
0
  {
182
0
      TPM2B_DIGEST    policyDigest;
183
0
      TPM_ALG_ID      policyAlg;
184
      // Check to see if this permanent handle has a policy
185
0
      policyAlg = EntityGetAuthPolicy(handle, &policyDigest);
186
0
      if(policyAlg == TPM_ALG_ERROR)
187
0
    continue;
188
0
      if(policyList->count < count)
189
0
    {
190
        // If we have not filled up the return list, add this
191
        // policy to the list;
192
0
        policyList->policies[policyList->count].handle = handle;
193
0
        policyList->policies[policyList->count].policyHash.hashAlg = policyAlg;
194
0
        MemoryCopy(&policyList->policies[policyList->count].policyHash.digest,
195
0
             policyDigest.t.buffer, policyDigest.t.size);
196
0
        policyList->count++;
197
0
    }
198
0
      else
199
0
    {
200
        // If the return list is full but we still have permanent handle
201
        // available, report this and stop iterating
202
0
        more = YES;
203
0
        break;
204
0
    }
205
0
  }
206
0
    return more;
207
0
}