| 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 |
// This file contains the functions that return the type of a handle. |
| 37 |
|
| 38 |
//** Includes |
| 39 |
#include "Tpm.h" |
| 40 |
|
| 41 |
//** Functions |
| 42 |
|
| 43 |
//*** HandleGetType() |
| 44 |
// This function returns the type of a handle which is the MSO of the handle. |
| 45 |
TPM_HT |
| 46 |
HandleGetType( |
| 47 |
TPM_HANDLE handle // IN: a handle to be checked |
| 48 |
) |
| 49 |
{ |
| 50 |
// return the upper bytes of input data |
| 51 |
return (TPM_HT)((handle & HR_RANGE_MASK) >> HR_SHIFT); |
| 52 |
} |
| 53 |
|
| 54 |
//*** NextPermanentHandle() |
| 55 |
// This function returns the permanent handle that is equal to the input value or |
| 56 |
// is the next higher value. If there is no handle with the input value and there |
| 57 |
// is no next higher value, it returns 0: |
| 58 |
TPM_HANDLE |
| 59 |
NextPermanentHandle( |
| 60 |
TPM_HANDLE inHandle // IN: the handle to check |
| 61 |
) |
| 62 |
{ |
| 63 |
// If inHandle is below the start of the range of permanent handles |
| 64 |
// set it to the start and scan from there |
| 65 |
if(inHandle < TPM_RH_FIRST) |
| 66 |
inHandle = TPM_RH_FIRST; |
| 67 |
// scan from input value until we find an implemented permanent handle |
| 68 |
// or go out of range |
| 69 |
for(; inHandle <= TPM_RH_LAST; inHandle++) |
| 70 |
{ |
| 71 |
switch(inHandle) |
| 72 |
{ |
| 73 |
case TPM_RH_OWNER: |
| 74 |
case TPM_RH_NULL: |
| 75 |
case TPM_RS_PW: |
| 76 |
case TPM_RH_LOCKOUT: |
| 77 |
case TPM_RH_ENDORSEMENT: |
| 78 |
case TPM_RH_PLATFORM: |
| 79 |
case TPM_RH_PLATFORM_NV: |
| 80 |
#ifdef VENDOR_PERMANENT |
| 81 |
case VENDOR_PERMANENT: |
| 82 |
#endif |
| 83 |
return inHandle; |
| 84 |
break; |
| 85 |
default: |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
// Out of range on the top |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
//*** PermanentCapGetHandles() |
| 94 |
// This function returns a list of the permanent handles of PCR, started from |
| 95 |
// 'handle'. If 'handle' is larger than the largest permanent handle, an empty list |
| 96 |
// will be returned with 'more' set to NO. |
| 97 |
// Return Type: TPMI_YES_NO |
| 98 |
// YES if there are more handles available |
| 99 |
// NO all the available handles has been returned |
| 100 |
TPMI_YES_NO |
| 101 |
PermanentCapGetHandles( |
| 102 |
TPM_HANDLE handle, // IN: start handle |
| 103 |
UINT32 count, // IN: count of returned handles |
| 104 |
TPML_HANDLE *handleList // OUT: list of handle |
| 105 |
) |
| 106 |
{ |
| 107 |
TPMI_YES_NO more = NO; |
| 108 |
UINT32 i; |
| 109 |
|
| 110 |
pAssert(HandleGetType(handle) == TPM_HT_PERMANENT); |
| 111 |
|
| 112 |
// Initialize output handle list |
| 113 |
handleList->count = 0; |
| 114 |
|
| 115 |
// The maximum count of handles we may return is MAX_CAP_HANDLES |
| 116 |
if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES; |
| 117 |
|
| 118 |
// Iterate permanent handle range |
| 119 |
for(i = NextPermanentHandle(handle); |
| 120 |
i != 0; i = NextPermanentHandle(i + 1)) |
| 121 |
{ |
| 122 |
if(handleList->count < count) |
| 123 |
{ |
| 124 |
// If we have not filled up the return list, add this permanent |
| 125 |
// handle to it |
| 126 |
handleList->handle[handleList->count] = i; |
| 127 |
handleList->count++; |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
// If the return list is full but we still have permanent handle |
| 132 |
// available, report this and stop iterating |
| 133 |
more = YES; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
return more; |
| 138 |
} |
| 139 |
|
| 140 |
//*** PermanentHandleGetPolicy() |
| 141 |
// This function returns a list of the permanent handles of PCR, started from |
| 142 |
// 'handle'. If 'handle' is larger than the largest permanent handle, an empty list |
| 143 |
// will be returned with 'more' set to NO. |
| 144 |
// Return Type: TPMI_YES_NO |
| 145 |
// YES if there are more handles available |
| 146 |
// NO all the available handles has been returned |
| 147 |
TPMI_YES_NO |
| 148 |
PermanentHandleGetPolicy( |
| 149 |
TPM_HANDLE handle, // IN: start handle |
| 150 |
UINT32 count, // IN: max count of returned handles |
| 151 |
TPML_TAGGED_POLICY *policyList // OUT: list of handle |
| 152 |
) |
| 153 |
{ |
| 154 |
TPMI_YES_NO more = NO; |
| 155 |
|
| 156 |
pAssert(HandleGetType(handle) == TPM_HT_PERMANENT); |
| 157 |
|
| 158 |
// Initialize output handle list |
| 159 |
policyList->count = 0; |
| 160 |
|
| 161 |
// The maximum count of policies we may return is MAX_TAGGED_POLICIES |
| 162 |
if(count > MAX_TAGGED_POLICIES) |
| 163 |
count = MAX_TAGGED_POLICIES; |
| 164 |
|
| 165 |
// Iterate permanent handle range |
| 166 |
for(handle = NextPermanentHandle(handle); |
| 167 |
handle != 0; |
| 168 |
handle = NextPermanentHandle(handle + 1)) |
| 169 |
{ |
| 170 |
TPM2B_DIGEST policyDigest; |
| 171 |
TPM_ALG_ID policyAlg; |
| 172 |
// Check to see if this permanent handle has a policy |
| 173 |
policyAlg = EntityGetAuthPolicy(handle, &policyDigest); |
| 174 |
if(policyAlg == TPM_ALG_ERROR) |
| 175 |
continue; |
| 176 |
if(policyList->count < count) |
| 177 |
{ |
| 178 |
// If we have not filled up the return list, add this |
| 179 |
// policy to the list; |
| 180 |
policyList->policies[policyList->count].handle = handle; |
| 181 |
policyList->policies[policyList->count].policyHash.hashAlg = policyAlg; |
| 182 |
MemoryCopy(&policyList->policies[policyList->count].policyHash.digest, |
| 183 |
policyDigest.t.buffer, policyDigest.t.size); |
| 184 |
policyList->count++; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
// If the return list is full but we still have permanent handle |
| 189 |
// available, report this and stop iterating |
| 190 |
more = YES; |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
return more; |
| 195 |
} |