summaryrefslogtreecommitdiffstats
path: root/src/tpm2/Policy_spt.c
blob: 4c2de20bf00a84384babd3d6cfca6845a7194698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/********************************************************************************/
/*										*/
/*			  Policy Command Support    				*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*            $Id: Policy_spt.c 1594 2020-03-26 22:15:48Z kgoldman $		*/
/*										*/
/*  Licenses and Notices							*/
/*										*/
/*  1. Copyright Licenses:							*/
/*										*/
/*  - Trusted Computing Group (TCG) grants to the user of the source code in	*/
/*    this specification (the "Source Code") a worldwide, irrevocable, 		*/
/*    nonexclusive, royalty free, copyright license to reproduce, create 	*/
/*    derivative works, distribute, display and perform the Source Code and	*/
/*    derivative works thereof, and to grant others the rights granted herein.	*/
/*										*/
/*  - The TCG grants to the user of the other parts of the specification 	*/
/*    (other than the Source Code) the rights to reproduce, distribute, 	*/
/*    display, and perform the specification solely for the purpose of 		*/
/*    developing products based on such documents.				*/
/*										*/
/*  2. Source Code Distribution Conditions:					*/
/*										*/
/*  - Redistributions of Source Code must retain the above copyright licenses, 	*/
/*    this list of conditions and the following disclaimers.			*/
/*										*/
/*  - Redistributions in binary form must reproduce the above copyright 	*/
/*    licenses, this list of conditions	and the following disclaimers in the 	*/
/*    documentation and/or other materials provided with the distribution.	*/
/*										*/
/*  3. Disclaimers:								*/
/*										*/
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF	*/
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH	*/
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)	*/
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.		*/
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for 		*/
/*  information on specification licensing rights available through TCG 	*/
/*  membership agreements.							*/
/*										*/
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED 	*/
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR 	*/
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR 		*/
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY 		*/
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.		*/
/*										*/
/*  - Without limitation, TCG and its members and licensors disclaim all 	*/
/*    liability, including liability for infringement of any proprietary 	*/
/*    rights, relating to use of information in this specification and to the	*/
/*    implementation of this specification, and TCG disclaims all liability for	*/
/*    cost of procurement of substitute goods or services, lost profits, loss 	*/
/*    of use, loss of data or any incidental, consequential, direct, indirect, 	*/
/*    or special damages, whether under contract, tort, warranty or otherwise, 	*/
/*    arising in any way out of use or reliance upon this specification or any 	*/
/*    information herein.							*/
/*										*/
/*  (c) Copyright IBM Corp. and others, 2016 - 2020				*/
/*										*/
/********************************************************************************/

/* 7.4 Policy Command Support (Policy_spt.c) */
#include "Tpm.h"
#include "Policy_spt_fp.h"
#include "PolicySigned_fp.h"
#include "PolicySecret_fp.h"
#include "PolicyTicket_fp.h"
/* 7.4.1 PolicyParameterChecks() */
/* This function validates the common parameters of TPM2_PolicySiged() and TPM2_PolicySecret(). The
   common parameters are nonceTPM, expiration, and cpHashA. */
TPM_RC
PolicyParameterChecks(
		      SESSION         *session,
		      UINT64           authTimeout,
		      TPM2B_DIGEST    *cpHashA,
		      TPM2B_NONCE     *nonce,
		      TPM_RC           blameNonce,
		      TPM_RC           blameCpHash,
		      TPM_RC           blameExpiration
		      )
{
    // Validate that input nonceTPM is correct if present
    if(nonce != NULL && nonce->t.size != 0)
	{
	    if(!MemoryEqual2B(&nonce->b, &session->nonceTPM.b))
		return TPM_RCS_NONCE + blameNonce;
	}
    // If authTimeout is set (expiration != 0...
    if(authTimeout != 0)
	{
	    // Validate input expiration.
	    // Cannot compare time if clock stop advancing.  A TPM_RC_NV_UNAVAILABLE
	    // or TPM_RC_NV_RATE error may be returned here.
	    RETURN_IF_NV_IS_NOT_AVAILABLE;
	    // if the time has already passed or the time epoch has changed then the
	    // time value is no longer good.
	    if((authTimeout < g_time)
	       || (session->epoch != g_timeEpoch))
		return TPM_RCS_EXPIRED + blameExpiration;
	}
    // If the cpHash is present, then check it
    if(cpHashA != NULL && cpHashA->t.size != 0)
	{
	    // The cpHash input has to have the correct size
	    if(cpHashA->t.size != session->u2.policyDigest.t.size)
		return TPM_RCS_SIZE + blameCpHash;
	    // If the cpHash has already been set, then this input value
	    // must match the current value.
	    if(session->u1.cpHash.b.size != 0
	       && !MemoryEqual2B(&cpHashA->b, &session->u1.cpHash.b))
		return TPM_RC_CPHASH;
	}
    return TPM_RC_SUCCESS;
}
/* 7.4.2 PolicyContextUpdate() */
/* Update policy hash Update the policyDigest in policy session by extending policyRef and
   objectName to it. This will also update the cpHash if it is present. */
void
PolicyContextUpdate(
		    TPM_CC           commandCode,   // IN: command code
		    TPM2B_NAME      *name,          // IN: name of entity
		    TPM2B_NONCE     *ref,           // IN: the reference data
		    TPM2B_DIGEST    *cpHash,        // IN: the cpHash (optional)
		    UINT64           policyTimeout, // IN: the timeout value for the policy
		    SESSION         *session        // IN/OUT: policy session to be updated
		    )
{
    HASH_STATE           hashState;
    // Start hash
    CryptHashStart(&hashState, session->authHashAlg);
    // policyDigest size should always be the digest size of session hash algorithm.
    pAssert(session->u2.policyDigest.t.size
	    == CryptHashGetDigestSize(session->authHashAlg));
    // add old digest
    CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b);
    // add commandCode
    CryptDigestUpdateInt(&hashState, sizeof(commandCode), commandCode);
    // add name if applicable
    if(name != NULL)
	CryptDigestUpdate2B(&hashState, &name->b);
    // Complete the digest and get the results
    CryptHashEnd2B(&hashState, &session->u2.policyDigest.b);
    // If the policy reference is not null, do a second update to the digest.
    if(ref != NULL)
	{
	    // Start second hash computation
	    CryptHashStart(&hashState, session->authHashAlg);
	    // add policyDigest
	    CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b);
	    // add policyRef
	    CryptDigestUpdate2B(&hashState, &ref->b);
	    // Complete second digest
	    CryptHashEnd2B(&hashState, &session->u2.policyDigest.b);
	}
    // Deal with the cpHash. If the cpHash value is present
    // then it would have already been checked to make sure that
    // it is compatible with the current value so all we need
    // to do here is copy it and set the isCpHashDefined attribute
    if(cpHash != NULL && cpHash->t.size != 0)
	{
	    session->u1.cpHash = *cpHash;
	    session->attributes.isCpHashDefined = SET;
	}
    // update the timeout if it is specified
    if(policyTimeout != 0)
	{
	    // If the timeout has not been set, then set it to the new value
	    // than the current timeout then set it to the new value
	    if(session->timeout == 0 || session->timeout > policyTimeout)
		session->timeout = policyTimeout;
	}
    return;
}
/* 7.4.2.1 ComputeAuthTimeout() */
/* This function is used to determine what the authorization timeout value for the session should
   be. */
UINT64
ComputeAuthTimeout(
		   SESSION         *session,               // IN: the session containing the time
		   //     values
		   INT32            expiration,            // IN: either the number of seconds from
		   //     the start of the session or the
		   //     time in g_timer;
		   TPM2B_NONCE     *nonce                  // IN: indicator of the time base
		   )
{
    UINT64           policyTime;
    // If no expiration, policy time is 0
    if(expiration == 0)
	policyTime = 0;
    else
	{
	    if(expiration < 0) {
	        if (expiration == (INT32)0x80000000) /* libtpms changed begin; ubsan */
	            expiration++;                    /* libtpms changed end */
		expiration = -expiration;
	    }
	    if(nonce->t.size == 0)
		// The input time is absolute Time (not Clock), but it is expressed
		// in seconds. To make sure that we don't time out too early, take the
		// current value of milliseconds in g_time and add that to the input
		// seconds value.
		policyTime = (((UINT64)expiration) * 1000) + g_time % 1000;
	    else
		// The policy timeout is the absolute value of the expiration in seconds
		// added to the start time of the policy.
		policyTime = session->startTime + (((UINT64)expiration) * 1000);
	}
    return policyTime;
}
/* 7.4.2.2 PolicyDigestClear() */
/* Function to reset the policyDigest of a session */
void
PolicyDigestClear(
		  SESSION         *session
		  )
{
    session->u2.policyDigest.t.size = CryptHashGetDigestSize(session->authHashAlg);
    MemorySet(session->u2.policyDigest.t.buffer, 0,
	      session->u2.policyDigest.t.size);
}

/* 7.4.2.5	PolicySptCheckCondition() */
/* Checks to see if the condition in the policy is satisfied. */

BOOL
PolicySptCheckCondition(
			TPM_EO          operation,
			BYTE            *opA,
			BYTE            *opB,
			UINT16           size
			)
{
    // Arithmetic Comparison
    switch(operation)
	{
	  case TPM_EO_EQ:
	    // compare A = B
	    return (UnsignedCompareB(size, opA, size, opB) == 0);
	    break;
	  case TPM_EO_NEQ:
	    // compare A != B
	    return (UnsignedCompareB(size, opA, size, opB) != 0);
	    break;
	  case TPM_EO_SIGNED_GT:
	    // compare A > B signed
	    return (SignedCompareB(size, opA, size, opB) > 0);
	    break;
	  case TPM_EO_UNSIGNED_GT:
	    // compare A > B unsigned
	    return (UnsignedCompareB(size, opA, size, opB) > 0);
	    break;
	  case TPM_EO_SIGNED_LT:
	    // compare A < B signed
	    return (SignedCompareB(size, opA, size, opB) < 0);
	    break;
	  case TPM_EO_UNSIGNED_LT:
	    // compare A < B unsigned
	    return (UnsignedCompareB(size, opA, size, opB) < 0);
	    break;
	  case TPM_EO_SIGNED_GE:
	    // compare A >= B signed
	    return (SignedCompareB(size, opA, size, opB) >= 0);
	    break;
	  case TPM_EO_UNSIGNED_GE:
	    // compare A >= B unsigned
	    return (UnsignedCompareB(size, opA, size, opB) >= 0);
	    break;
	  case TPM_EO_SIGNED_LE:
	    // compare A <= B signed
	    return (SignedCompareB(size, opA, size, opB) <= 0);
	    break;
	  case TPM_EO_UNSIGNED_LE:
	    // compare A <= B unsigned
	    return (UnsignedCompareB(size, opA, size, opB) <= 0);
	    break;
	  case TPM_EO_BITSET:
	    // All bits SET in B are SET in A. ((A&B)=B)
	      {
		  UINT32 i;
		  for(i = 0; i < size; i++)
		      if((opA[i] & opB[i]) != opB[i])
			  return FALSE;
	      }
	      break;
	  case TPM_EO_BITCLEAR:
	    // All bits SET in B are CLEAR in A. ((A&B)=0)
	      {
		  UINT32 i;
		  for(i = 0; i < size; i++)
		      if((opA[i] & opB[i]) != 0)
			  return FALSE;
	      }
	      break;
	  default:
	    FAIL(FATAL_ERROR_INTERNAL);
	    break;
	}
    return TRUE;
}