blob: 084c0a7c663e23e0cde77d3574c921846716f868 [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001// 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"
10typedef UINT16 ATTRIBUTE_TYPE;
11//
12// The following file is produced from the command tables in part 3 of the specification. It defines the
13// attributes for each of the commands.
14//
15// NOTE: This file is currently produced by an automated process. Files produced from Part 2 or Part 3 tables through
16// automated processes are not included in the specification so that their is no ambiguity about the table
17// containing the information being the normative definition.
18//
19#include "CommandAttributeData.c"
20//
Namyoon Woo65994902019-10-31 10:38:51 -070021// SafeGetAttributesForCC()
22//
23// Helper function returning a command attribute value for the given command
24// code or extended command code.
25//
26// Return Value Meaning
27//
28// COMMAND_ATTRIBUTES command attribute for the given command
29//
30static COMMAND_ATTRIBUTES SafeGetAttributesForCC(TPM_CC commandCode) {
31 const COMMAND_ATTRIBUTES kUnimplementedAttr = 0;
32
33 if (commandCode & TPM_CCE_BIT_MASK) {
34 if (commandCode >= TPM_CCE_FIRST && commandCode <= TPM_CCE_LAST)
35 return s_commandAttributesExt[commandCode - TPM_CCE_FIRST];
36 } else {
37 if (commandCode >= TPM_CC_FIRST && commandCode <= TPM_CC_LAST)
38 return s_commandAttributes[commandCode - TPM_CC_FIRST];
39 }
40
41 return kUnimplementedAttr;
42}
43//
44//
Vadim Bendebury56797522015-05-20 10:32:25 -070045//
46// Command Attribute Functions
47//
48// CommandAuthRole()
49//
50// This function returns the authorization role required of a handle.
51//
52// Return Value Meaning
53//
54// AUTH_NONE no authorization is required
55// AUTH_USER user role authorization is required
56// AUTH_ADMIN admin role authorization is required
57// AUTH_DUP duplication role authorization is required
58//
59AUTH_ROLE
60CommandAuthRole(
61 TPM_CC commandCode, // IN: command code
62 UINT32 handleIndex // IN: handle index (zero based)
63 )
64{
Namyoon Woo65994902019-10-31 10:38:51 -070065 ATTRIBUTE_TYPE properties = SafeGetAttributesForCC(commandCode);
66
Vadim Bendebury56797522015-05-20 10:32:25 -070067 if(handleIndex > 1)
68 return AUTH_NONE;
69 if(handleIndex == 0) {
Vadim Bendebury56797522015-05-20 10:32:25 -070070 if(properties & HANDLE_1_USER) return AUTH_USER;
71 if(properties & HANDLE_1_ADMIN) return AUTH_ADMIN;
72 if(properties & HANDLE_1_DUP) return AUTH_DUP;
73 return AUTH_NONE;
74 }
Namyoon Woo65994902019-10-31 10:38:51 -070075 if(properties & HANDLE_2_USER)
Vadim Bendebury711a0442015-05-28 16:37:42 -070076 return AUTH_USER;
Vadim Bendebury56797522015-05-20 10:32:25 -070077 return AUTH_NONE;
78}
79//
80//
81// CommandIsImplemented()
82//
83// This function indicates if a command is implemented.
84//
85// Return Value Meaning
86//
87// TRUE if the command is implemented
88// FALSE if the command is not implemented
89//
90BOOL
91CommandIsImplemented(
92 TPM_CC commandCode // IN: command code
93 )
94{
Namyoon Woo65994902019-10-31 10:38:51 -070095 return (SafeGetAttributesForCC(commandCode) & IS_IMPLEMENTED) ? TRUE : FALSE;
Vadim Bendebury56797522015-05-20 10:32:25 -070096}
97//
98//
99// CommandGetAttribute()
100//
101// return a TPMA_CC structure for the given command code
102//
103TPMA_CC
104CommandGetAttribute(
105 TPM_CC commandCode // IN: command code
106 )
107{
108 UINT32 size = sizeof(s_ccAttr) / sizeof(s_ccAttr[0]);
109 UINT32 i;
110 for(i = 0; i < size; i++) {
111 if(s_ccAttr[i].commandIndex == (UINT16) commandCode)
112 return s_ccAttr[i];
113 }
114 // This function should be called in the way that the command code
115 // attribute is available.
116 FAIL(FATAL_ERROR_INTERNAL);
Vadim Bendebury711a0442015-05-28 16:37:42 -0700117
118 return s_ccAttr[0]; // Just to appease the compiler, never reached.
Vadim Bendebury56797522015-05-20 10:32:25 -0700119}
120//
121//
122// EncryptSize()
123//
124// This function returns the size of the decrypt size field. This function returns 0 if encryption is not allowed
125//
126// Return Value Meaning
127//
128// 0 encryption not allowed
129// 2 size field is two bytes
130// 4 size field is four bytes
131//
132int
133EncryptSize(
134 TPM_CC commandCode // IN: commandCode
135 )
136{
Namyoon Woo65994902019-10-31 10:38:51 -0700137 COMMAND_ATTRIBUTES ca = SafeGetAttributesForCC(commandCode);
Vadim Bendebury56797522015-05-20 10:32:25 -0700138 if(ca & ENCRYPT_2)
139 return 2;
140 if(ca & ENCRYPT_4)
141 return 4;
142 return 0;
143}
144//
145//
146// DecryptSize()
147//
148// This function returns the size of the decrypt size field. This function returns 0 if decryption is not allowed
149//
150// Return Value Meaning
151//
152// 0 encryption not allowed
153// 2 size field is two bytes
154// 4 size field is four bytes
155//
156int
157DecryptSize(
158 TPM_CC commandCode // IN: commandCode
159 )
160{
Namyoon Woo65994902019-10-31 10:38:51 -0700161 COMMAND_ATTRIBUTES ca = SafeGetAttributesForCC(commandCode);
Vadim Bendebury56797522015-05-20 10:32:25 -0700162 if(ca & DECRYPT_2)
163 return 2;
164 if(ca & DECRYPT_4)
165 return 4;
166 return 0;
167}
168//
169//
170// IsSessionAllowed()
171//
172// This function indicates if the command is allowed to have sessions.
173// This function must not be called if the command is not known to be implemented.
174//
175// Return Value Meaning
176//
177// TRUE session is allowed with this command
178// FALSE session is not allowed with this command
179//
180BOOL
181IsSessionAllowed(
182 TPM_CC commandCode // IN: the command to be checked
183 )
184{
Namyoon Woo65994902019-10-31 10:38:51 -0700185 return (SafeGetAttributesForCC(commandCode) & NO_SESSIONS) ? FALSE : TRUE;
Vadim Bendebury56797522015-05-20 10:32:25 -0700186}
187//
188//
189// IsHandleInResponse()
190//
191BOOL
192IsHandleInResponse(
193 TPM_CC commandCode
194 )
195{
Namyoon Woo65994902019-10-31 10:38:51 -0700196 return (SafeGetAttributesForCC(commandCode) & R_HANDLE) ? TRUE : FALSE;
Vadim Bendebury56797522015-05-20 10:32:25 -0700197}
198//
199//
200// IsWriteOperation()
201//
202// Checks to see if an operation will write to NV memory
203//
204BOOL
205IsWriteOperation(
206 TPM_CC command // IN: Command to check
207 )
208{
209 switch (command)
210 {
211 case TPM_CC_NV_Write:
212 case TPM_CC_NV_Increment:
213 case TPM_CC_NV_SetBits:
214 case TPM_CC_NV_Extend:
215 // Nv write lock counts as a write operation for authorization purposes.
216 // We check to see if the NV is write locked before we do the authorization
217 // If it is locked, we fail the command early.
218 case TPM_CC_NV_WriteLock:
219 return TRUE;
220 default:
221 break;
222 }
223 return FALSE;
224}
225//
226//
227// IsReadOperation()
228//
229// Checks to see if an operation will write to NV memory
230//
231BOOL
232IsReadOperation(
233 TPM_CC command // IN: Command to check
234 )
235{
236 switch (command)
237 {
238 case TPM_CC_NV_Read:
239 case TPM_CC_PolicyNV:
240 case TPM_CC_NV_Certify:
241 // Nv read lock counts as a read operation for authorization purposes.
242 // We check to see if the NV is read locked before we do the authorization
243 // If it is locked, we fail the command early.
244 case TPM_CC_NV_ReadLock:
245 return TRUE;
246 default:
247 break;
248 }
249 return FALSE;
250}
251//
252//
253// CommandCapGetCCList()
254//
255// This function returns a list of implemented commands and command attributes starting from the
256// command in commandCode.
257//
258//
259//
260//
261// Return Value Meaning
262//
263// YES more command attributes are available
264// NO no more command attributes are available
265//
266TPMI_YES_NO
267CommandCapGetCCList(
268 TPM_CC commandCode, // IN: start command code
269 UINT32 count, // IN: maximum count for number of entries in
270 // 'commandList'
271 TPML_CCA *commandList // OUT: list of TPMA_CC
272 )
273{
274 TPMI_YES_NO more = NO;
275 UINT32 i;
276 // initialize output handle list count
277 commandList->count = 0;
Namyoon Woo65994902019-10-31 10:38:51 -0700278 // The maximum count of commands that may be returned is MAX_CAP_CC_ALL.
279 if(count > MAX_CAP_CC_ALL) count = MAX_CAP_CC_ALL;
Vadim Bendebury56797522015-05-20 10:32:25 -0700280 // If the command code is smaller than TPM_CC_FIRST, start from TPM_CC_FIRST
281 if(commandCode < TPM_CC_FIRST) commandCode = TPM_CC_FIRST;
282 // Collect command attributes
Namyoon Woo65994902019-10-31 10:38:51 -0700283 for(i = commandCode; i <= TPM_CCE_LAST; i++)
Vadim Bendebury56797522015-05-20 10:32:25 -0700284 {
Namyoon Woo65994902019-10-31 10:38:51 -0700285 if (i > TPM_CC_LAST && i < TPM_CCE_FIRST)
286 {
287 i = TPM_CCE_FIRST;
288 }
Vadim Bendebury56797522015-05-20 10:32:25 -0700289 if(CommandIsImplemented(i))
290 {
291 if(commandList->count < count)
292 {
293 // If the list is not full, add the attributes for this command.
294 commandList->commandAttributes[commandList->count]
295 = CommandGetAttribute(i);
296 commandList->count++;
297 }
298 else
299 {
300 // If the list is full but there are more commands to report,
301 // indicate this and return.
302 more = YES;
303 break;
304 }
305 }
306 }
307 return more;
308}