| 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 |
//** Includes |
| 36 |
#include "Tpm.h" |
| 37 |
#include "Policy_spt_fp.h" |
| 38 |
#include "PolicySigned_fp.h" |
| 39 |
#include "PolicySecret_fp.h" |
| 40 |
#include "PolicyTicket_fp.h" |
| 41 |
|
| 42 |
//** Functions |
| 43 |
//*** PolicyParameterChecks() |
| 44 |
// This function validates the common parameters of TPM2_PolicySiged() |
| 45 |
// and TPM2_PolicySecret(). The common parameters are 'nonceTPM', |
| 46 |
// 'expiration', and 'cpHashA'. |
| 47 |
TPM_RC |
| 48 |
PolicyParameterChecks( |
| 49 |
SESSION *session, |
| 50 |
UINT64 authTimeout, |
| 51 |
TPM2B_DIGEST *cpHashA, |
| 52 |
TPM2B_NONCE *nonce, |
| 53 |
TPM_RC blameNonce, |
| 54 |
TPM_RC blameCpHash, |
| 55 |
TPM_RC blameExpiration |
| 56 |
) |
| 57 |
{ |
| 58 |
// Validate that input nonceTPM is correct if present |
| 59 |
if(nonce != NULL && nonce->t.size != 0) |
| 60 |
{ |
| 61 |
if(!MemoryEqual2B(&nonce->b, &session->nonceTPM.b)) |
| 62 |
return TPM_RCS_NONCE + blameNonce; |
| 63 |
} |
| 64 |
// If authTimeout is set (expiration != 0... |
| 65 |
if(authTimeout != 0) |
| 66 |
{ |
| 67 |
// Validate input expiration. |
| 68 |
// Cannot compare time if clock stop advancing. A TPM_RC_NV_UNAVAILABLE |
| 69 |
// or TPM_RC_NV_RATE error may be returned here. |
| 70 |
RETURN_IF_NV_IS_NOT_AVAILABLE; |
| 71 |
|
| 72 |
// if the time has already passed or the time epoch has changed then the |
| 73 |
// time value is no longer good. |
| 74 |
if((authTimeout < g_time) |
| 75 |
|| (session->epoch != g_timeEpoch)) |
| 76 |
return TPM_RCS_EXPIRED + blameExpiration; |
| 77 |
} |
| 78 |
// If the cpHash is present, then check it |
| 79 |
if(cpHashA != NULL && cpHashA->t.size != 0) |
| 80 |
{ |
| 81 |
// The cpHash input has to have the correct size |
| 82 |
if(cpHashA->t.size != session->u2.policyDigest.t.size) |
| 83 |
return TPM_RCS_SIZE + blameCpHash; |
| 84 |
|
| 85 |
// If the cpHash has already been set, then this input value |
| 86 |
// must match the current value. |
| 87 |
if(session->u1.cpHash.b.size != 0 |
| 88 |
&& !MemoryEqual2B(&cpHashA->b, &session->u1.cpHash.b)) |
| 89 |
return TPM_RC_CPHASH; |
| 90 |
} |
| 91 |
return TPM_RC_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
//*** PolicyContextUpdate() |
| 95 |
// Update policy hash |
| 96 |
// Update the policyDigest in policy session by extending policyRef and |
| 97 |
// objectName to it. This will also update the cpHash if it is present. |
| 98 |
// Return Type: void |
| 99 |
void |
| 100 |
PolicyContextUpdate( |
| 101 |
TPM_CC commandCode, // IN: command code |
| 102 |
TPM2B_NAME *name, // IN: name of entity |
| 103 |
TPM2B_NONCE *ref, // IN: the reference data |
| 104 |
TPM2B_DIGEST *cpHash, // IN: the cpHash (optional) |
| 105 |
UINT64 policyTimeout, // IN: the timeout value for the policy |
| 106 |
SESSION *session // IN/OUT: policy session to be updated |
| 107 |
) |
| 108 |
{ |
| 109 |
HASH_STATE hashState; |
| 110 |
|
| 111 |
// Start hash |
| 112 |
CryptHashStart(&hashState, session->authHashAlg); |
| 113 |
|
| 114 |
|
| 115 |
// policyDigest size should always be the digest size of session hash algorithm. |
| 116 |
pAssert(session->u2.policyDigest.t.size |
| 117 |
== CryptHashGetDigestSize(session->authHashAlg)); |
| 118 |
|
| 119 |
// add old digest |
| 120 |
CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b); |
| 121 |
|
| 122 |
// add commandCode |
| 123 |
CryptDigestUpdateInt(&hashState, sizeof(commandCode), commandCode); |
| 124 |
|
| 125 |
// add name if applicable |
| 126 |
if(name != NULL) |
| 127 |
CryptDigestUpdate2B(&hashState, &name->b); |
| 128 |
|
| 129 |
// Complete the digest and get the results |
| 130 |
CryptHashEnd2B(&hashState, &session->u2.policyDigest.b); |
| 131 |
|
| 132 |
// If the policy reference is not null, do a second update to the digest. |
| 133 |
if(ref != NULL) |
| 134 |
{ |
| 135 |
|
| 136 |
// Start second hash computation |
| 137 |
CryptHashStart(&hashState, session->authHashAlg); |
| 138 |
|
| 139 |
// add policyDigest |
| 140 |
CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b); |
| 141 |
|
| 142 |
// add policyRef |
| 143 |
CryptDigestUpdate2B(&hashState, &ref->b); |
| 144 |
|
| 145 |
// Complete second digest |
| 146 |
CryptHashEnd2B(&hashState, &session->u2.policyDigest.b); |
| 147 |
} |
| 148 |
// Deal with the cpHash. If the cpHash value is present |
| 149 |
// then it would have already been checked to make sure that |
| 150 |
// it is compatible with the current value so all we need |
| 151 |
// to do here is copy it and set the isCpHashDefined attribute |
| 152 |
if(cpHash != NULL && cpHash->t.size != 0) |
| 153 |
{ |
| 154 |
session->u1.cpHash = *cpHash; |
| 155 |
session->attributes.isCpHashDefined = SET; |
| 156 |
} |
| 157 |
|
| 158 |
// update the timeout if it is specified |
| 159 |
if(policyTimeout != 0) |
| 160 |
{ |
| 161 |
// If the timeout has not been set, then set it to the new value |
| 162 |
// than the current timeout then set it to the new value |
| 163 |
if(session->timeout == 0 || session->timeout > policyTimeout) |
| 164 |
session->timeout = policyTimeout; |
| 165 |
} |
| 166 |
return; |
| 167 |
} |
| 168 |
//*** ComputeAuthTimeout() |
| 169 |
// This function is used to determine what the authorization timeout value for |
| 170 |
// the session should be. |
| 171 |
UINT64 |
| 172 |
ComputeAuthTimeout( |
| 173 |
SESSION *session, // IN: the session containing the time |
| 174 |
// values |
| 175 |
INT32 expiration, // IN: either the number of seconds from |
| 176 |
// the start of the session or the |
| 177 |
// time in g_timer; |
| 178 |
TPM2B_NONCE *nonce // IN: indicator of the time base |
| 179 |
) |
| 180 |
{ |
| 181 |
UINT64 policyTime; |
| 182 |
// If no expiration, policy time is 0 |
| 183 |
if(expiration == 0) |
| 184 |
policyTime = 0; |
| 185 |
else |
| 186 |
{ |
| 187 |
if(expiration < 0) |
| 188 |
expiration = -expiration; |
| 189 |
if(nonce->t.size == 0) |
| 190 |
// The input time is absolute Time (not Clock), but it is expressed |
| 191 |
// in seconds. To make sure that we don't time out too early, take the |
| 192 |
// current value of milliseconds in g_time and add that to the input |
| 193 |
// seconds value. |
| 194 |
policyTime = (((UINT64)expiration) * 1000) + g_time % 1000; |
| 195 |
else |
| 196 |
// The policy timeout is the absolute value of the expiration in seconds |
| 197 |
// added to the start time of the policy. |
| 198 |
policyTime = session->startTime + (((UINT64)expiration) * 1000); |
| 199 |
|
| 200 |
} |
| 201 |
return policyTime; |
| 202 |
} |
| 203 |
|
| 204 |
//*** PolicyDigestClear() |
| 205 |
// Function to reset the policyDigest of a session |
| 206 |
void |
| 207 |
PolicyDigestClear( |
| 208 |
SESSION *session |
| 209 |
) |
| 210 |
{ |
| 211 |
session->u2.policyDigest.t.size = CryptHashGetDigestSize(session->authHashAlg); |
| 212 |
MemorySet(session->u2.policyDigest.t.buffer, 0, |
| 213 |
session->u2.policyDigest.t.size); |
| 214 |
} |
| 215 |
|
| 216 |
BOOL |
| 217 |
PolicySptCheckCondition( |
| 218 |
TPM_EO operation, |
| 219 |
BYTE *opA, |
| 220 |
BYTE *opB, |
| 221 |
UINT16 size |
| 222 |
) |
| 223 |
{ |
| 224 |
// Arithmetic Comparison |
| 225 |
switch(operation) |
| 226 |
{ |
| 227 |
case TPM_EO_EQ: |
| 228 |
// compare A = B |
| 229 |
return (UnsignedCompareB(size, opA, size, opB) == 0); |
| 230 |
break; |
| 231 |
case TPM_EO_NEQ: |
| 232 |
// compare A != B |
| 233 |
return (UnsignedCompareB(size, opA, size, opB) != 0); |
| 234 |
break; |
| 235 |
case TPM_EO_SIGNED_GT: |
| 236 |
// compare A > B signed |
| 237 |
return (SignedCompareB(size, opA, size, opB) > 0); |
| 238 |
break; |
| 239 |
case TPM_EO_UNSIGNED_GT: |
| 240 |
// compare A > B unsigned |
| 241 |
return (UnsignedCompareB(size, opA, size, opB) > 0); |
| 242 |
break; |
| 243 |
case TPM_EO_SIGNED_LT: |
| 244 |
// compare A < B signed |
| 245 |
return (SignedCompareB(size, opA, size, opB) < 0); |
| 246 |
break; |
| 247 |
case TPM_EO_UNSIGNED_LT: |
| 248 |
// compare A < B unsigned |
| 249 |
return (UnsignedCompareB(size, opA, size, opB) < 0); |
| 250 |
break; |
| 251 |
case TPM_EO_SIGNED_GE: |
| 252 |
// compare A >= B signed |
| 253 |
return (SignedCompareB(size, opA, size, opB) >= 0); |
| 254 |
break; |
| 255 |
case TPM_EO_UNSIGNED_GE: |
| 256 |
// compare A >= B unsigned |
| 257 |
return (UnsignedCompareB(size, opA, size, opB) >= 0); |
| 258 |
break; |
| 259 |
case TPM_EO_SIGNED_LE: |
| 260 |
// compare A <= B signed |
| 261 |
return (SignedCompareB(size, opA, size, opB) <= 0); |
| 262 |
break; |
| 263 |
case TPM_EO_UNSIGNED_LE: |
| 264 |
// compare A <= B unsigned |
| 265 |
return (UnsignedCompareB(size, opA, size, opB) <= 0); |
| 266 |
break; |
| 267 |
case TPM_EO_BITSET: |
| 268 |
// All bits SET in B are SET in A. ((A&B)=B) |
| 269 |
{ |
| 270 |
UINT32 i; |
| 271 |
for(i = 0; i < size; i++) |
| 272 |
if((opA[i] & opB[i]) != opB[i]) |
| 273 |
return FALSE; |
| 274 |
} |
| 275 |
break; |
| 276 |
case TPM_EO_BITCLEAR: |
| 277 |
// All bits SET in B are CLEAR in A. ((A&B)=0) |
| 278 |
{ |
| 279 |
UINT32 i; |
| 280 |
for(i = 0; i < size; i++) |
| 281 |
if((opA[i] & opB[i]) != 0) |
| 282 |
return FALSE; |
| 283 |
} |
| 284 |
break; |
| 285 |
default: |
| 286 |
FAIL(FATAL_ERROR_INTERNAL); |
| 287 |
break; |
| 288 |
} |
| 289 |
return TRUE; |
| 290 |
} |