Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/support/Entity.c

raw
1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 //** Description
36 // The functions in this file are used for accessing properties for handles of
37 // various types. Functions in other files require handles of a specific
38 // type but the functions in this file allow use of any handle type.
39
40 //** Includes
41
42 #include "Tpm.h"
43
44 //** Functions
45 //*** EntityGetLoadStatus()
46 // This function will check that all the handles access loaded entities.
47 // Return Type: TPM_RC
48 // TPM_RC_HANDLE handle type does not match
49 // TPM_RC_REFERENCE_Hx entity is not present
50 // TPM_RC_HIERARCHY entity belongs to a disabled hierarchy
51 // TPM_RC_OBJECT_MEMORY handle is an evict object but there is no
52 // space to load it to RAM
53 TPM_RC
54 EntityGetLoadStatus(
55 COMMAND *command // IN/OUT: command parsing structure
56 )
57 {
58 UINT32 i;
59 TPM_RC result = TPM_RC_SUCCESS;
60 //
61 for(i = 0; i < command->handleNum; i++)
62 {
63 TPM_HANDLE handle = command->handles[i];
64 switch(HandleGetType(handle))
65 {
66 // For handles associated with hierarchies, the entity is present
67 // only if the associated enable is SET.
68 case TPM_HT_PERMANENT:
69 switch(handle)
70 {
71 case TPM_RH_OWNER:
72 if(!gc.shEnable)
73 result = TPM_RC_HIERARCHY;
74 break;
75
76 #ifdef VENDOR_PERMANENT
77 case VENDOR_PERMANENT:
78 #endif
79 case TPM_RH_ENDORSEMENT:
80 if(!gc.ehEnable)
81 result = TPM_RC_HIERARCHY;
82 break;
83 case TPM_RH_PLATFORM:
84 if(!g_phEnable)
85 result = TPM_RC_HIERARCHY;
86 break;
87 // null handle, PW session handle and lockout
88 // handle are always available
89 case TPM_RH_NULL:
90 case TPM_RS_PW:
91 // Need to be careful for lockout. Lockout is always available
92 // for policy checks but not always available when authValue
93 // is being checked.
94 case TPM_RH_LOCKOUT:
95 break;
96 default:
97 // handling of the manufacture_specific handles
98 if(((TPM_RH)handle >= TPM_RH_AUTH_00)
99 && ((TPM_RH)handle <= TPM_RH_AUTH_FF))
100 // use the value that would have been returned from
101 // unmarshaling if it did the handle filtering
102 result = TPM_RC_VALUE;
103 else
104 FAIL(FATAL_ERROR_INTERNAL);
105 break;
106 }
107 break;
108 case TPM_HT_TRANSIENT:
109 // For a transient object, check if the handle is associated
110 // with a loaded object.
111 if(!IsObjectPresent(handle))
112 result = TPM_RC_REFERENCE_H0;
113 break;
114 case TPM_HT_PERSISTENT:
115 // Persistent object
116 // Copy the persistent object to RAM and replace the handle with the
117 // handle of the assigned slot. A TPM_RC_OBJECT_MEMORY,
118 // TPM_RC_HIERARCHY or TPM_RC_REFERENCE_H0 error may be returned by
119 // ObjectLoadEvict()
120 result = ObjectLoadEvict(&command->handles[i], command->index);
121 break;
122 case TPM_HT_HMAC_SESSION:
123 // For an HMAC session, see if the session is loaded
124 // and if the session in the session slot is actually
125 // an HMAC session.
126 if(SessionIsLoaded(handle))
127 {
128 SESSION *session;
129 session = SessionGet(handle);
130 // Check if the session is a HMAC session
131 if(session->attributes.isPolicy == SET)
132 result = TPM_RC_HANDLE;
133 }
134 else
135 result = TPM_RC_REFERENCE_H0;
136 break;
137 case TPM_HT_POLICY_SESSION:
138 // For a policy session, see if the session is loaded
139 // and if the session in the session slot is actually
140 // a policy session.
141 if(SessionIsLoaded(handle))
142 {
143 SESSION *session;
144 session = SessionGet(handle);
145 // Check if the session is a policy session
146 if(session->attributes.isPolicy == CLEAR)
147 result = TPM_RC_HANDLE;
148 }
149 else
150 result = TPM_RC_REFERENCE_H0;
151 break;
152 case TPM_HT_NV_INDEX:
153 // For an NV Index, use the TPM-specific routine
154 // to search the IN Index space.
155 result = NvIndexIsAccessible(handle);
156 break;
157 case TPM_HT_PCR:
158 // Any PCR handle that is unmarshaled successfully referenced
159 // a PCR that is defined.
160 break;
161 #if CC_AC_Send
162 case TPM_HT_AC:
163 // Use the TPM-specific routine to search for the AC
164 result = AcIsAccessible(handle);
165 break;
166 #endif
167 default:
168 // Any other handle type is a defect in the unmarshaling code.
169 FAIL(FATAL_ERROR_INTERNAL);
170 break;
171 }
172 if(result != TPM_RC_SUCCESS)
173 {
174 if(result == TPM_RC_REFERENCE_H0)
175 result = result + i;
176 else
177 result = RcSafeAddToResult(result, TPM_RC_H + g_rcIndex[i]);
178 break;
179 }
180 }
181 return result;
182 }
183
184 //*** EntityGetAuthValue()
185 // This function is used to access the 'authValue' associated with a handle.
186 // This function assumes that the handle references an entity that is accessible
187 // and the handle is not for a persistent objects. That is EntityGetLoadStatus()
188 // should have been called. Also, the accessibility of the authValue should have
189 // been verified by IsAuthValueAvailable().
190 //
191 // This function copies the authorization value of the entity to 'auth'.
192 // Return Type: UINT16
193 // count number of bytes in the authValue with 0's stripped
194 UINT16
195 EntityGetAuthValue(
196 TPMI_DH_ENTITY handle, // IN: handle of entity
197 TPM2B_AUTH *auth // OUT: authValue of the entity
198 )
199 {
200 TPM2B_AUTH *pAuth = NULL;
201
202 auth->t.size = 0;
203
204 switch(HandleGetType(handle))
205 {
206 case TPM_HT_PERMANENT:
207 {
208 switch(handle)
209 {
210 case TPM_RH_OWNER:
211 // ownerAuth for TPM_RH_OWNER
212 pAuth = &gp.ownerAuth;
213 break;
214 case TPM_RH_ENDORSEMENT:
215 // endorsementAuth for TPM_RH_ENDORSEMENT
216 pAuth = &gp.endorsementAuth;
217 break;
218 case TPM_RH_PLATFORM:
219 // platformAuth for TPM_RH_PLATFORM
220 pAuth = &gc.platformAuth;
221 break;
222 case TPM_RH_LOCKOUT:
223 // lockoutAuth for TPM_RH_LOCKOUT
224 pAuth = &gp.lockoutAuth;
225 break;
226 case TPM_RH_NULL:
227 // nullAuth for TPM_RH_NULL. Return 0 directly here
228 return 0;
229 break;
230 #ifdef VENDOR_PERMANENT
231 case VENDOR_PERMANENT:
232 // vendor authorization value
233 pAauth = &g_platformUniqueDetails;
234 #endif
235 default:
236 // If any other permanent handle is present it is
237 // a code defect.
238 FAIL(FATAL_ERROR_INTERNAL);
239 break;
240 }
241 break;
242 }
243 case TPM_HT_TRANSIENT:
244 // authValue for an object
245 // A persistent object would have been copied into RAM
246 // and would have an transient object handle here.
247 {
248 OBJECT *object;
249
250 object = HandleToObject(handle);
251 // special handling if this is a sequence object
252 if(ObjectIsSequence(object))
253 {
254 pAuth = &((HASH_OBJECT *)object)->auth;
255 }
256 else
257 {
258 // Authorization is available only when the private portion of
259 // the object is loaded. The check should be made before
260 // this function is called
261 pAssert(object->attributes.publicOnly == CLEAR);
262 pAuth = &object->sensitive.authValue;
263 }
264 }
265 break;
266 case TPM_HT_NV_INDEX:
267 // authValue for an NV index
268 {
269 NV_INDEX *nvIndex = NvGetIndexInfo(handle, NULL);
270 pAssert(nvIndex != NULL);
271 pAuth = &nvIndex->authValue;
272 }
273 break;
274 case TPM_HT_PCR:
275 // authValue for PCR
276 pAuth = PCRGetAuthValue(handle);
277 break;
278 default:
279 // If any other handle type is present here, then there is a defect
280 // in the unmarshaling code.
281 FAIL(FATAL_ERROR_INTERNAL);
282 break;
283 }
284 // Copy the authValue
285 MemoryCopy2B(&auth->b, &pAuth->b, sizeof(auth->t.buffer));
286 MemoryRemoveTrailingZeros(auth);
287 return auth->t.size;
288 }
289
290 //*** EntityGetAuthPolicy()
291 // This function is used to access the 'authPolicy' associated with a handle.
292 // This function assumes that the handle references an entity that is accessible
293 // and the handle is not for a persistent objects. That is EntityGetLoadStatus()
294 // should have been called. Also, the accessibility of the authPolicy should have
295 // been verified by IsAuthPolicyAvailable().
296 //
297 // This function copies the authorization policy of the entity to 'authPolicy'.
298 //
299 // The return value is the hash algorithm for the policy.
300 TPMI_ALG_HASH
301 EntityGetAuthPolicy(
302 TPMI_DH_ENTITY handle, // IN: handle of entity
303 TPM2B_DIGEST *authPolicy // OUT: authPolicy of the entity
304 )
305 {
306 TPMI_ALG_HASH hashAlg = TPM_ALG_NULL;
307 authPolicy->t.size = 0;
308
309 switch(HandleGetType(handle))
310 {
311 case TPM_HT_PERMANENT:
312 switch(handle)
313 {
314 case TPM_RH_OWNER:
315 // ownerPolicy for TPM_RH_OWNER
316 *authPolicy = gp.ownerPolicy;
317 hashAlg = gp.ownerAlg;
318 break;
319 case TPM_RH_ENDORSEMENT:
320 // endorsementPolicy for TPM_RH_ENDORSEMENT
321 *authPolicy = gp.endorsementPolicy;
322 hashAlg = gp.endorsementAlg;
323 break;
324 case TPM_RH_PLATFORM:
325 // platformPolicy for TPM_RH_PLATFORM
326 *authPolicy = gc.platformPolicy;
327 hashAlg = gc.platformAlg;
328 break;
329 case TPM_RH_LOCKOUT:
330 // lockoutPolicy for TPM_RH_LOCKOUT
331 *authPolicy = gp.lockoutPolicy;
332 hashAlg = gp.lockoutAlg;
333 break;
334 default:
335 return TPM_ALG_ERROR;
336 break;
337 }
338 break;
339 case TPM_HT_TRANSIENT:
340 // authPolicy for an object
341 {
342 OBJECT *object = HandleToObject(handle);
343 *authPolicy = object->publicArea.authPolicy;
344 hashAlg = object->publicArea.nameAlg;
345 }
346 break;
347 case TPM_HT_NV_INDEX:
348 // authPolicy for a NV index
349 {
350 NV_INDEX *nvIndex = NvGetIndexInfo(handle, NULL);
351 pAssert(nvIndex != 0);
352 *authPolicy = nvIndex->publicArea.authPolicy;
353 hashAlg = nvIndex->publicArea.nameAlg;
354 }
355 break;
356 case TPM_HT_PCR:
357 // authPolicy for a PCR
358 hashAlg = PCRGetAuthPolicy(handle, authPolicy);
359 break;
360 default:
361 // If any other handle type is present it is a code defect.
362 FAIL(FATAL_ERROR_INTERNAL);
363 break;
364 }
365 return hashAlg;
366 }
367
368 //*** EntityGetName()
369 // This function returns the Name associated with a handle.
370 TPM2B_NAME *
371 EntityGetName(
372 TPMI_DH_ENTITY handle, // IN: handle of entity
373 TPM2B_NAME *name // OUT: name of entity
374 )
375 {
376 switch(HandleGetType(handle))
377 {
378 case TPM_HT_TRANSIENT:
379 {
380 // Name for an object
381 OBJECT *object = HandleToObject(handle);
382 // an object with no nameAlg has no name
383 if(object->publicArea.nameAlg == TPM_ALG_NULL)
384 name->b.size = 0;
385 else
386 *name = object->name;
387 break;
388 }
389 case TPM_HT_NV_INDEX:
390 // Name for a NV index
391 NvGetNameByIndexHandle(handle, name);
392 break;
393 default:
394 // For all other types, the handle is the Name
395 name->t.size = sizeof(TPM_HANDLE);
396 UINT32_TO_BYTE_ARRAY(handle, name->t.name);
397 break;
398 }
399 return name;
400 }
401
402 //*** EntityGetHierarchy()
403 // This function returns the hierarchy handle associated with an entity.
404 // 1. A handle that is a hierarchy handle is associated with itself.
405 // 2. An NV index belongs to TPM_RH_PLATFORM if TPMA_NV_PLATFORMCREATE,
406 // is SET, otherwise it belongs to TPM_RH_OWNER
407 // 3. An object handle belongs to its hierarchy.
408 TPMI_RH_HIERARCHY
409 EntityGetHierarchy(
410 TPMI_DH_ENTITY handle // IN :handle of entity
411 )
412 {
413 TPMI_RH_HIERARCHY hierarchy = TPM_RH_NULL;
414
415 switch(HandleGetType(handle))
416 {
417 case TPM_HT_PERMANENT:
418 // hierarchy for a permanent handle
419 switch(handle)
420 {
421 case TPM_RH_PLATFORM:
422 case TPM_RH_ENDORSEMENT:
423 case TPM_RH_NULL:
424 hierarchy = handle;
425 break;
426 // all other permanent handles are associated with the owner
427 // hierarchy. (should only be TPM_RH_OWNER and TPM_RH_LOCKOUT)
428 default:
429 hierarchy = TPM_RH_OWNER;
430 break;
431 }
432 break;
433 case TPM_HT_NV_INDEX:
434 // hierarchy for NV index
435 {
436 NV_INDEX *nvIndex = NvGetIndexInfo(handle, NULL);
437 pAssert(nvIndex != NULL);
438
439 // If only the platform can delete the index, then it is
440 // considered to be in the platform hierarchy, otherwise it
441 // is in the owner hierarchy.
442 if(IS_ATTRIBUTE(nvIndex->publicArea.attributes, TPMA_NV,
443 PLATFORMCREATE))
444 hierarchy = TPM_RH_PLATFORM;
445 else
446 hierarchy = TPM_RH_OWNER;
447 }
448 break;
449 case TPM_HT_TRANSIENT:
450 // hierarchy for an object
451 {
452 OBJECT *object;
453 object = HandleToObject(handle);
454 if(object->attributes.ppsHierarchy)
455 {
456 hierarchy = TPM_RH_PLATFORM;
457 }
458 else if(object->attributes.epsHierarchy)
459 {
460 hierarchy = TPM_RH_ENDORSEMENT;
461 }
462 else if(object->attributes.spsHierarchy)
463 {
464 hierarchy = TPM_RH_OWNER;
465 }
466 }
467 break;
468 case TPM_HT_PCR:
469 hierarchy = TPM_RH_OWNER;
470 break;
471 default:
472 FAIL(FATAL_ERROR_INTERNAL);
473 break;
474 }
475 // this is unreachable but it provides a return value for the default
476 // case which makes the complier happy
477 return hierarchy;
478 }