summaryrefslogtreecommitdiffstats
path: root/src/tpm2/Session.c
blob: 991a1460b2e8c7f460a52f049e85baa304bac91f (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/********************************************************************************/
/*										*/
/*			    Manage the session context counter 			*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*            $Id: Session.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				*/
/*										*/
/********************************************************************************/


/* 8.9.2 Includes, Defines, and Local Variables */
#define SESSION_C
#include "Tpm.h"

/* 8.9.3	File Scope Function -- ContextIdSetOldest() */

static void
ContextIdSetOldest(
		   void
		   )
{
    CONTEXT_SLOT    lowBits;
    CONTEXT_SLOT    entry;
    CONTEXT_SLOT    smallest = CONTEXT_SLOT_MASKED(~0);	// libtpms changed
    UINT32  i;
    pAssert(s_ContextSlotMask == 0xff || s_ContextSlotMask == 0xffff); // libtpms added
    // Set oldestSaveContext to a value indicating none assigned
    s_oldestSavedSession = MAX_ACTIVE_SESSIONS + 1;
    lowBits = CONTEXT_SLOT_MASKED(gr.contextCounter);	// libtpms changed
    for(i = 0; i < MAX_ACTIVE_SESSIONS; i++)
	{
	    entry = gr.contextArray[i];
	    // only look at entries that are saved contexts
	    if(entry > MAX_LOADED_SESSIONS)
		{
		    // Use a less than or equal in case the oldest
		    // is brand new (= lowBits-1) and equal to our initial
		    // value for smallest.
		    if(CONTEXT_SLOT_MASKED(entry - lowBits) <= smallest)	// libtpms changed
			{
			    smallest = CONTEXT_SLOT_MASKED(entry - lowBits);	// libtpms changed
			    s_oldestSavedSession = i;
			}
		}
	}
    // When we finish, either the s_oldestSavedSession still has its initial
    // value, or it has the index of the oldest saved context.
}
/* 8.9.4 Startup Function -- SessionStartup() */
/* This function initializes the session subsystem on TPM2_Startup(). */
BOOL
SessionStartup(
	       STARTUP_TYPE     type
	       )
{
    UINT32               i;
    // Initialize session slots.  At startup, all the in-memory session slots
    // are cleared and marked as not occupied
    for(i = 0; i < MAX_LOADED_SESSIONS; i++)
	s_sessions[i].occupied = FALSE;   // session slot is not occupied
    // The free session slots the number of maximum allowed loaded sessions
    s_freeSessionSlots = MAX_LOADED_SESSIONS;
    // Initialize context ID data.  On a ST_SAVE or hibernate sequence, it will
    // scan the saved array of session context counts, and clear any entry that
    // references a session that was in memory during the state save since that
    // memory was not preserved over the ST_SAVE.
    if(type == SU_RESUME || type == SU_RESTART)
	{
	    // On ST_SAVE we preserve the contexts that were saved but not the ones
	    // in memory
	    for(i = 0; i < MAX_ACTIVE_SESSIONS; i++)
		{
		    // If the array value is unused or references a loaded session then
		    // that loaded session context is lost and the array entry is
		    // reclaimed.
		    if(gr.contextArray[i] <= MAX_LOADED_SESSIONS)
			gr.contextArray[i] = 0;
		}
	    // Find the oldest session in context ID data and set it in
	    // s_oldestSavedSession
	    ContextIdSetOldest();
	}
    else
	{
	    // For STARTUP_CLEAR, clear out the contextArray
	    for(i = 0; i < MAX_ACTIVE_SESSIONS; i++)
		gr.contextArray[i] = 0;
	    // reset the context counter
	    gr.contextCounter = MAX_LOADED_SESSIONS + 1;
	    // Initialize oldest saved session
	    s_oldestSavedSession = MAX_ACTIVE_SESSIONS + 1;

	    // Initialize the context slot mask for UINT16
	    s_ContextSlotMask = 0xffff;	// libtpms added
	}
    return TRUE;
}
/* 8.9.5 Access Functions */
/* 8.9.5.1 SessionIsLoaded() */
/* This function test a session handle references a loaded session.  The handle must have previously
   been checked to make sure that it is a valid handle for an authorization session. */
/* NOTE: A PWAP authorization does not have a session. */
/* Return Values Meaning */
/* TRUE if session is loaded */
/* FALSE if it is not loaded */
BOOL
SessionIsLoaded(
		TPM_HANDLE       handle         // IN: session handle
		)
{
    pAssert(HandleGetType(handle) == TPM_HT_POLICY_SESSION
	    || HandleGetType(handle) == TPM_HT_HMAC_SESSION);
    handle = handle & HR_HANDLE_MASK;
    // if out of range of possible active session, or not assigned to a loaded
    // session return false
    if(handle >= MAX_ACTIVE_SESSIONS
       || gr.contextArray[handle] == 0
       || gr.contextArray[handle] > MAX_LOADED_SESSIONS)
	return FALSE;
    return TRUE;
}
/* 8.9.5.2 SessionIsSaved() */
/* This function test a session handle references a saved session.  The handle must have previously
   been checked to make sure that it is a valid handle for an authorization session. */
/* NOTE: A password authorization does not have a session. */
/* This function requires that the handle be a valid session handle. */
/* Return Values Meaning */
/* TRUE if session is saved */
/* FALSE if it is not saved */
BOOL
SessionIsSaved(
	       TPM_HANDLE       handle         // IN: session handle
	       )
{
    pAssert(HandleGetType(handle) == TPM_HT_POLICY_SESSION
	    || HandleGetType(handle) == TPM_HT_HMAC_SESSION);
    handle = handle & HR_HANDLE_MASK;
    // if out of range of possible active session, or not assigned, or
    // assigned to a loaded session, return false
    if(handle >= MAX_ACTIVE_SESSIONS
       || gr.contextArray[handle] == 0
       || gr.contextArray[handle] <= MAX_LOADED_SESSIONS
       )
	return FALSE;
    return TRUE;
}
/* 8.9.5.3 SequenceNumberForSavedContextIsValid() */
BOOL
SequenceNumberForSavedContextIsValid(
				      TPMS_CONTEXT    *context        // IN: pointer to a context
				      // structure to be validated
				      )
{
#define MAX_CONTEXT_GAP ((UINT64)(CONTEXT_SLOT_MASKED(~0) + 1)) /* libtpms changed */
    pAssert(s_ContextSlotMask == 0xff || s_ContextSlotMask == 0xffff); // libtpms added

    TPM_HANDLE           handle = context->savedHandle & HR_HANDLE_MASK;
    if(// Handle must be with the range of active sessions
       handle >= MAX_ACTIVE_SESSIONS
       // the array entry must be for a saved context
       || gr.contextArray[handle] <= MAX_LOADED_SESSIONS
       // the array entry must agree with the sequence number
       || gr.contextArray[handle] != CONTEXT_SLOT_MASKED(context->sequence) // libtpms changed
       // the provided sequence number has to be less than the current counter
       || context->sequence > gr.contextCounter
       // but not so much that it could not be a valid sequence number
       || gr.contextCounter - context->sequence > MAX_CONTEXT_GAP)
	return FALSE;
    return TRUE;
}
/* 8.9.5.4 SessionPCRValueIsCurrent() */
/* This function is used to check if PCR values have been updated since the last time they were
   checked in a policy session. */
/* This function requires the session is loaded. */
/* Return Values Meaning */
/* TRUE if PCR value is current */
/* FALSE if PCR value is not current */
BOOL
SessionPCRValueIsCurrent(
			 SESSION         *session        // IN: session structure
			 )
{
    if(session->pcrCounter != 0
       && session->pcrCounter != gr.pcrCounter
       )
	return FALSE;
    else
	return TRUE;
}
/* 8.9.5.5 SessionGet() */
/* This function returns a pointer to the session object associated with a session handle. */
/* The function requires that the session is loaded. */
SESSION *
SessionGet(
	   TPM_HANDLE       handle         // IN: session handle
	   )
{
    size_t          slotIndex;
    CONTEXT_SLOT    sessionIndex;
    pAssert(HandleGetType(handle) == TPM_HT_POLICY_SESSION
	    || HandleGetType(handle) == TPM_HT_HMAC_SESSION
	    );
    slotIndex = handle & HR_HANDLE_MASK;
    pAssert(slotIndex < MAX_ACTIVE_SESSIONS);
    // get the contents of the session array.  Because session is loaded, we
    // should always get a valid sessionIndex
    sessionIndex = gr.contextArray[slotIndex] - 1;
    pAssert(sessionIndex < MAX_LOADED_SESSIONS);
    return &s_sessions[sessionIndex].session;
}
/* 8.9.6 Utility Functions */
/* 8.9.6.1 ContextIdSessionCreate() */
/* This function is called when a session is created.  It will check to see if the current gap would
   prevent a context from being saved.  If so it will return TPM_RC_CONTEXT_GAP.  Otherwise, it will
   try to find an open slot in contextArray, set contextArray to the slot. This routine requires
   that the caller has determined the session array index for the session. */
/* TPM_RC_CONTEXT_GAP can't assign a new contextID until the oldest saved session context is
   recycled */
/* TPM_RC_SESSION_HANDLE there is no slot available in the context array for tracking of this
   session context */
static TPM_RC
ContextIdSessionCreate(
		       TPM_HANDLE      *handle, /* OUT: receives the assigned handle. This will be
						   an index that must be adjusted by the caller
						   according to the type of the session created */
		       UINT32           sessionIndex   /* IN: The session context array entry that
							  will be occupied by the created session */
		       )
{
    pAssert(sessionIndex < MAX_LOADED_SESSIONS);
    // check to see if creating the context is safe
    // Is this going to be an assignment for the last session context
    // array entry?  If so, then there will be no room to recycle the
    // oldest context if needed.  If the gap is not at maximum, then
    // it will be possible to save a context if it becomes necessary.
    if(s_oldestSavedSession < MAX_ACTIVE_SESSIONS
       && s_freeSessionSlots == 1)
	{
	    // See if the gap is at maximum
	    // The current value of the contextCounter will be assigned to the next
	    // saved context. If the value to be assigned would make the same as an
	    // existing context, then we can't use it because of the ambiguity it would
	    // create.
	    if(CONTEXT_SLOT_MASKED(gr.contextCounter) // libtpms changed
	       == gr.contextArray[s_oldestSavedSession])
		return TPM_RC_CONTEXT_GAP;
	}
    // Find an unoccupied entry in the contextArray
    for(*handle = 0; *handle < MAX_ACTIVE_SESSIONS; (*handle)++)
	{
	    if(gr.contextArray[*handle] == 0)
		{
		    // indicate that the session associated with this handle
		    // references a loaded session
		    gr.contextArray[*handle] = CONTEXT_SLOT_MASKED(sessionIndex + 1); // libtpms changed
		    return TPM_RC_SUCCESS;
		}
	}
    return TPM_RC_SESSION_HANDLES;
}
/* 8.9.6.2 SessionCreate() */
/* This function does the detailed work for starting an authorization session. This is done in a
   support routine rather than in the action code because the session management may differ in
   implementations.  This implementation uses a fixed memory allocation to hold sessions and a fixed
   allocation to hold the contextID for the saved contexts. */
/* Error Returns Meaning */
/* TPM_RC_CONTEXT_GAP need to recycle sessions */
/* TPM_RC_SESSION_HANDLE active session space is full */
/* TPM_RC_SESSION_MEMORY loaded session space is full */
TPM_RC
SessionCreate(
	      TPM_SE           sessionType,   // IN: the session type
	      TPMI_ALG_HASH    authHash,      // IN: the hash algorithm
	      TPM2B_NONCE     *nonceCaller,   // IN: initial nonceCaller
	      TPMT_SYM_DEF    *symmetric,     // IN: the symmetric algorithm
	      TPMI_DH_ENTITY   bind,          // IN: the bind object
	      TPM2B_DATA      *seed,          // IN: seed data
	      TPM_HANDLE      *sessionHandle, // OUT: the session handle
	      TPM2B_NONCE     *nonceTpm       // OUT: the session nonce
	      )
{
    TPM_RC               result = TPM_RC_SUCCESS;
    CONTEXT_SLOT         slotIndex;
    SESSION             *session = NULL;
    pAssert(sessionType == TPM_SE_HMAC
	    || sessionType == TPM_SE_POLICY
	    || sessionType == TPM_SE_TRIAL);
    // If there are no open spots in the session array, then no point in searching
    if(s_freeSessionSlots == 0)
	return TPM_RC_SESSION_MEMORY;
    // Find a space for loading a session
    for(slotIndex = 0; slotIndex < MAX_LOADED_SESSIONS; slotIndex++)
	{
	    // Is this available?
	    if(s_sessions[slotIndex].occupied == FALSE)
		{
		    session = &s_sessions[slotIndex].session;
		    break;
		}
	}
    // if no spot found, then this is an internal error
    if(slotIndex >= MAX_LOADED_SESSIONS) {		// libtpms changed
	FAIL(FATAL_ERROR_INTERNAL);
	// should never get here due to longjmp	in FAIL()  libtpms added begin; cppcheck
	return TPM_RC_FAILURE;
    }							// libtpms added end
    // Call context ID function to get a handle.  TPM_RC_SESSION_HANDLE may be
    // returned from ContextIdHandelAssign()
    result = ContextIdSessionCreate(sessionHandle, slotIndex);
    if(result != TPM_RC_SUCCESS)
	return result;
    //*** Only return from this point on is TPM_RC_SUCCESS
    // Can now indicate that the session array entry is occupied.
    s_freeSessionSlots--;
    s_sessions[slotIndex].occupied = TRUE;
    // Initialize the session data
    MemorySet(session, 0, sizeof(SESSION));
    // Initialize internal session data
    session->authHashAlg = authHash;
    // Initialize session type
    if(sessionType == TPM_SE_HMAC)
	{
	    *sessionHandle += HMAC_SESSION_FIRST;
	}
    else
	{
	    *sessionHandle += POLICY_SESSION_FIRST;
	    // For TPM_SE_POLICY or TPM_SE_TRIAL
	    session->attributes.isPolicy = SET;
	    if(sessionType == TPM_SE_TRIAL)
		session->attributes.isTrialPolicy = SET;
	    SessionSetStartTime(session);
	    // Initialize policyDigest.  policyDigest is initialized with a string of 0
	    // of session algorithm digest size. Since the session is already clear.
	    // Just need to set the size
	    session->u2.policyDigest.t.size =
		CryptHashGetDigestSize(session->authHashAlg);
	}
    // Create initial session nonce
    session->nonceTPM.t.size = nonceCaller->t.size;
    CryptRandomGenerate(session->nonceTPM.t.size, session->nonceTPM.t.buffer);
    MemoryCopy2B(&nonceTpm->b, &session->nonceTPM.b,
		 sizeof(nonceTpm->t.buffer));
    // Set up session parameter encryption algorithm
    session->symmetric = *symmetric;
    // If there is a bind object or a session secret, then need to compute
    // a sessionKey.
    if(bind != TPM_RH_NULL || seed->t.size != 0)
	{
	    // sessionKey = KDFa(hash, (authValue || seed), "ATH", nonceTPM,
	    //                      nonceCaller, bits)
	    // The HMAC key for generating the sessionSecret can be the concatenation
	    // of an authorization value and a seed value
	    TPM2B_TYPE(KEY, (sizeof(TPMT_HA) + sizeof(seed->t.buffer)));
	    TPM2B_KEY            key;
	    // Get hash size, which is also the length of sessionKey
	    session->sessionKey.t.size = CryptHashGetDigestSize(session->authHashAlg);
	    // Get authValue of associated entity
	    EntityGetAuthValue(bind, (TPM2B_AUTH *)&key);
	    pAssert(key.t.size + seed->t.size <= sizeof(key.t.buffer));
	    // Concatenate authValue and seed
	    MemoryConcat2B(&key.b, &seed->b, sizeof(key.t.buffer));
	    // Compute the session key
	    CryptKDFa(session->authHashAlg, &key.b, SESSION_KEY, &session->nonceTPM.b,
		      &nonceCaller->b,
		      session->sessionKey.t.size * 8, session->sessionKey.t.buffer,
		      NULL, FALSE);
	}
    // Copy the name of the entity that the HMAC session is bound to
    // Policy session is not bound to an entity
    if(bind != TPM_RH_NULL && sessionType == TPM_SE_HMAC)
	{
	    session->attributes.isBound = SET;
	    SessionComputeBoundEntity(bind, &session->u1.boundEntity);
	}
    // If there is a bind object and it is subject to DA, then use of this session
    // is subject to DA regardless of how it is used.
    session->attributes.isDaBound = (bind != TPM_RH_NULL)
				    && (IsDAExempted(bind) == FALSE);
    // If the session is bound, then check to see if it is bound to lockoutAuth
    session->attributes.isLockoutBound = (session->attributes.isDaBound == SET)
					 && (bind == TPM_RH_LOCKOUT);
    return TPM_RC_SUCCESS;
}
/* 8.9.6.3 SessionContextSave() */
/* This function is called when a session context is to be saved.  The contextID of the saved
   session is returned.  If no contextID can be assigned, then the routine returns
   TPM_RC_CONTEXT_GAP. If the function completes normally, the session slot will be freed. */
/* This function requires that handle references a loaded session. Otherwise, it should not be
   called at the first place. */
/* Error Returns Meaning */
/* TPM_RC_CONTEXT_GAP a contextID could not be assigned. */
/* TPM_RC_TOO_MANY_CONTEXTS the counter maxed out */
TPM_RC
SessionContextSave(
		   TPM_HANDLE           handle,        // IN: session handle
		   CONTEXT_COUNTER     *contextID      // OUT: assigned contextID
		   )
{
    UINT32                      contextIndex;
    CONTEXT_SLOT                slotIndex;
    pAssert(SessionIsLoaded(handle));
    pAssert(s_ContextSlotMask == 0xff || s_ContextSlotMask == 0xffff); // libtpms added
    // check to see if the gap is already maxed out
    // Need to have a saved session
    if(s_oldestSavedSession < MAX_ACTIVE_SESSIONS
       // if the oldest saved session has the same value as the low bits
       // of the contextCounter, then the GAP is maxed out.
       && gr.contextArray[s_oldestSavedSession] == CONTEXT_SLOT_MASKED(gr.contextCounter)) // libtpms changed
	return TPM_RC_CONTEXT_GAP;
    // if the caller wants the context counter, set it
    if(contextID != NULL)
	*contextID = gr.contextCounter;
    contextIndex = handle & HR_HANDLE_MASK;
    pAssert(contextIndex < MAX_ACTIVE_SESSIONS);
    // Extract the session slot number referenced by the contextArray
    // because we are going to overwrite this with the low order
    // contextID value.
    slotIndex = gr.contextArray[contextIndex] - 1;
    // Set the contextID for the contextArray
    gr.contextArray[contextIndex] = CONTEXT_SLOT_MASKED(gr.contextCounter); // libtpms changed
    // Increment the counter
    gr.contextCounter++;
    // In the unlikely event that the 64-bit context counter rolls over...
    if(gr.contextCounter == 0)
	{
	    // back it up
	    gr.contextCounter--;
	    // return an error
	    return TPM_RC_TOO_MANY_CONTEXTS;
	}
    // if the low-order bits wrapped, need to advance the value to skip over
    // the values used to indicate that a session is loaded
    if(CONTEXT_SLOT_MASKED(gr.contextCounter) == 0) // libtpms changed
	gr.contextCounter += MAX_LOADED_SESSIONS + 1;
    // If no other sessions are saved, this is now the oldest.
    if(s_oldestSavedSession >= MAX_ACTIVE_SESSIONS)
	s_oldestSavedSession = contextIndex;
    // Mark the session slot as unoccupied
    s_sessions[slotIndex].occupied = FALSE;
    // and indicate that there is an additional open slot
    s_freeSessionSlots++;
    return TPM_RC_SUCCESS;
}
/* 8.9.6.4 SessionContextLoad() */
/* This function is used to load a session from saved context.  The session handle must be for a
   saved context. */
/* If the gap is at a maximum, then the only session that can be loaded is the oldest session,
   otherwise TPM_RC_CONTEXT_GAP is returned. */
/* This function requires that handle references a valid saved session. */
/* Error Returns Meaning */
/* TPM_RC_SESSION_MEMORY no free session slots */
/* TPM_RC_CONTEXT_GAP the gap count is maximum and this is not the oldest saved context */
TPM_RC
SessionContextLoad(
		   SESSION_BUF     *session,       // IN: session structure from saved context
		   TPM_HANDLE      *handle         // IN/OUT: session handle
		   )
{
    UINT32              contextIndex;
    CONTEXT_SLOT        slotIndex;
    pAssert(s_ContextSlotMask == 0xff || s_ContextSlotMask == 0xffff); // libtpms added
    pAssert(HandleGetType(*handle) == TPM_HT_POLICY_SESSION
	    || HandleGetType(*handle) == TPM_HT_HMAC_SESSION);
    // Don't bother looking if no openings
    if(s_freeSessionSlots == 0)
	return TPM_RC_SESSION_MEMORY;
    // Find a free session slot to load the session
    for(slotIndex = 0; slotIndex < MAX_LOADED_SESSIONS; slotIndex++)
	if(s_sessions[slotIndex].occupied == FALSE) break;
    // if no spot found, then this is an internal error
    pAssert(slotIndex < MAX_LOADED_SESSIONS);
    // libtpms: besides the s_freeSessionSlots guard add another array index guard
    if (slotIndex >= MAX_LOADED_SESSIONS) {	// libtpms added begin; cppcheck
	FAIL(FATAL_ERROR_INTERNAL);
	// should never get here due to longjmp	in FAIL()
	return TPM_RC_FAILURE;
    }						// libtpms added end
    contextIndex = *handle & HR_HANDLE_MASK;   // extract the index
    // If there is only one slot left, and the gap is at maximum, the only session
    // context that we can safely load is the oldest one.
    if(s_oldestSavedSession < MAX_ACTIVE_SESSIONS
       && s_freeSessionSlots == 1
       && CONTEXT_SLOT_MASKED(gr.contextCounter) == gr.contextArray[s_oldestSavedSession] // libtpms changed
       && contextIndex != s_oldestSavedSession)
	return TPM_RC_CONTEXT_GAP;
    pAssert(contextIndex < MAX_ACTIVE_SESSIONS);
    // set the contextArray value to point to the session slot where
    // the context is loaded
    gr.contextArray[contextIndex] = slotIndex + 1;
    // if this was the oldest context, find the new oldest
    if(contextIndex == s_oldestSavedSession)
	ContextIdSetOldest();
    // Copy session data to session slot
    MemoryCopy(&s_sessions[slotIndex].session, session, sizeof(SESSION));
    // Set session slot as occupied
    s_sessions[slotIndex].occupied = TRUE;
    // Reduce the number of open spots
    s_freeSessionSlots--;
    return TPM_RC_SUCCESS;
}
/* 8.9.6.5 SessionFlush() */
/* This function is used to flush a session referenced by its handle.  If the session associated
   with handle is loaded, the session array entry is marked as available. */
/* This function requires that handle be a valid active session. */
void
SessionFlush(
	     TPM_HANDLE       handle         // IN: loaded or saved session handle
	     )
{
    CONTEXT_SLOT         slotIndex;
    UINT32               contextIndex;   // Index into contextArray
    pAssert((HandleGetType(handle) == TPM_HT_POLICY_SESSION
	     || HandleGetType(handle) == TPM_HT_HMAC_SESSION
	     )
	    && (SessionIsLoaded(handle) || SessionIsSaved(handle))
	    );
    // Flush context ID of this session
    // Convert handle to an index into the contextArray
    contextIndex = handle & HR_HANDLE_MASK;
    pAssert(contextIndex < sizeof(gr.contextArray) / sizeof(gr.contextArray[0]));
    // Get the current contents of the array
    slotIndex = gr.contextArray[contextIndex];
    // Mark context array entry as available
    gr.contextArray[contextIndex] = 0;
    // Is this a saved session being flushed
    if(slotIndex > MAX_LOADED_SESSIONS)
	{
	    // Flushing the oldest session?
	    if(contextIndex == s_oldestSavedSession)
		// If so, find a new value for oldest.
		ContextIdSetOldest();
	}
    else
	{
	    // Adjust slot index to point to session array index
	    slotIndex -= 1;
	    // Free session array index
	    s_sessions[slotIndex].occupied = FALSE;
	    s_freeSessionSlots++;
	}
    return;
}
/* 8.9.6.6 SessionComputeBoundEntity() */
/* This function computes the binding value for a session.  The binding value for a reserved handle
   is the handle itself.  For all the other entities, the authValue at the time of binding is
   included to prevent squatting. For those values, the Name and the authValue are concatenated into
   the bind buffer.  If they will not both fit, the will be overlapped by XORing() bytes.  If XOR is
   required, the bind value will be full. */
void
SessionComputeBoundEntity(
			  TPMI_DH_ENTITY       entityHandle,  // IN: handle of entity
			  TPM2B_NAME          *bind           // OUT: binding value
			  )
{
    TPM2B_AUTH           auth;
    BYTE                *pAuth = auth.t.buffer;
    UINT16               i;
    // Get name
    EntityGetName(entityHandle, bind);
    //    // The bound value of a reserved handle is the handle itself
    //    if(bind->t.size == sizeof(TPM_HANDLE)) return;
    // For all the other entities, concatenate the authorization value to the name.
    // Get a local copy of the authorization value because some overlapping
    // may be necessary.
    EntityGetAuthValue(entityHandle, &auth);
    // Make sure that the extra space is zeroed
    MemorySet(&bind->t.name[bind->t.size], 0, sizeof(bind->t.name) - bind->t.size);
    // XOR the authValue at the end of the name
    for(i = sizeof(bind->t.name) - auth.t.size; i < sizeof(bind->t.name); i++)
	bind->t.name[i] ^= *pAuth++;
    // Set the bind value to the maximum size
    bind->t.size = sizeof(bind->t.name);
    return;
}
/* 8.9.6.7 SessionSetStartTime() */
/* This function is used to initialize the session timing */
void
SessionSetStartTime(
		    SESSION         *session        // IN: the session to update
		    )
{
    session->startTime = g_time;
    session->epoch = g_timeEpoch;
    session->timeout = 0;
}
/* 8.9.6.8 SessionResetPolicyData() */
/* This function is used to reset the policy data without changing the nonce or the start time of
   the session. */
void
SessionResetPolicyData(
		       SESSION         *session        // IN: the session to reset
		       )
{
    SESSION_ATTRIBUTES      oldAttributes;
    pAssert(session != NULL);
    // Will need later
    oldAttributes = session->attributes;
    // No command
    session->commandCode = 0;
    // No locality selected
    MemorySet(&session->commandLocality, 0, sizeof(session->commandLocality));
    // The cpHash size to zero
    session->u1.cpHash.b.size = 0;
    // No timeout
    session->timeout = 0;
    // Reset the pcrCounter
    session->pcrCounter = 0;
    // Reset the policy hash
    MemorySet(&session->u2.policyDigest.t.buffer, 0,
	      session->u2.policyDigest.t.size);
    // Reset the session attributes
    MemorySet(&session->attributes, 0, sizeof(SESSION_ATTRIBUTES));
    // Restore the policy attributes
    session->attributes.isPolicy = SET;
    session->attributes.isTrialPolicy = oldAttributes.isTrialPolicy;
    // Restore the bind attributes
    session->attributes.isDaBound = oldAttributes.isDaBound;
    session->attributes.isLockoutBound = oldAttributes.isLockoutBound;
}
/* 8.9.6.9 SessionCapGetLoaded() */
/* This function returns a list of handles of loaded session, started from input handle */
/* Handle must be in valid loaded session handle range, but does not have to point to a loaded
   session. */
/* Return Values Meaning */
/* YES if there are more handles available */
/* NO all the available handles has been returned */
TPMI_YES_NO
SessionCapGetLoaded(
		    TPMI_SH_POLICY   handle,        // IN: start handle
		    UINT32           count,         // IN: count of returned handles
		    TPML_HANDLE     *handleList     // OUT: list of handle
		    )
{
    TPMI_YES_NO     more = NO;
    UINT32          i;
    pAssert(HandleGetType(handle) == TPM_HT_LOADED_SESSION);
    // Initialize output handle list
    handleList->count = 0;
    // The maximum count of handles we may return is MAX_CAP_HANDLES
    if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES;
    // Iterate session context ID slots to get loaded session handles
    for(i = handle & HR_HANDLE_MASK; i < MAX_ACTIVE_SESSIONS; i++)
	{
	    // If session is active
	    if(gr.contextArray[i] != 0)
		{
		    // If session is loaded
		    if(gr.contextArray[i] <= MAX_LOADED_SESSIONS)
			{
			    if(handleList->count < count)
				{
				    SESSION         *session;
				    // If we have not filled up the return list, add this
				    // session handle to it
				    // assume that this is going to be an HMAC session
				    handle = i + HMAC_SESSION_FIRST;
				    session = SessionGet(handle);
				    if(session->attributes.isPolicy)
					handle = i + POLICY_SESSION_FIRST;
				    handleList->handle[handleList->count] = handle;
				    handleList->count++;
				}
			    else
				{
				    // If the return list is full but we still have loaded object
				    // available, report this and stop iterating
				    more = YES;
				    break;
				}
			}
		}
	}
    return more;
}
/* 8.9.6.10 SessionCapGetSaved() */
/* This function returns a list of handles for saved session, starting at handle. */
/* Handle must be in a valid handle range, but does not have to point to a saved session */
/* Return Values Meaning */
/* YES if there are more handles available */
/* NO all the available handles has been returned */
TPMI_YES_NO
SessionCapGetSaved(
		   TPMI_SH_HMAC     handle,        // IN: start handle
		   UINT32           count,         // IN: count of returned handles
		   TPML_HANDLE     *handleList     // OUT: list of handle
		   )
{
    TPMI_YES_NO     more = NO;
    UINT32          i;
#ifdef  TPM_HT_SAVED_SESSION
    pAssert(HandleGetType(handle) == TPM_HT_SAVED_SESSION);
#else
    pAssert(HandleGetType(handle) == TPM_HT_ACTIVE_SESSION);
#endif
    // Initialize output handle list
    handleList->count = 0;
    // The maximum count of handles we may return is MAX_CAP_HANDLES
    if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES;
    // Iterate session context ID slots to get loaded session handles
    for(i = handle & HR_HANDLE_MASK; i < MAX_ACTIVE_SESSIONS; i++)
	{
	    // If session is active
	    if(gr.contextArray[i] != 0)
		{
		    // If session is saved
		    if(gr.contextArray[i] > MAX_LOADED_SESSIONS)
			{
			    if(handleList->count < count)
				{
				    // If we have not filled up the return list, add this
				    // session handle to it
				    handleList->handle[handleList->count] = i + HMAC_SESSION_FIRST;
				    handleList->count++;
				}
			    else
				{
				    // If the return list is full but we still have loaded object
				    // available, report this and stop iterating
				    more = YES;
				    break;
				}
			}
		}
	}
    return more;
}
/* 8.9.6.11 SessionCapGetLoadedNumber() */
/* This function return the number of authorization sessions currently loaded into TPM RAM. */
UINT32
SessionCapGetLoadedNumber(
			  void
			  )
{
    return MAX_LOADED_SESSIONS - s_freeSessionSlots;
}
/* 8.9.6.12 SessionCapGetLoadedAvail() */
/* This function returns the number of additional authorization sessions, of any type, that could be
   loaded into TPM RAM. */
/* NOTE: In other implementations, this number may just be an estimate. The only requirement for the
   estimate is, if it is one or more, then at least one session must be loadable. */
UINT32
SessionCapGetLoadedAvail(
			 void
			 )
{
    return s_freeSessionSlots;
}
/* 8.9.6.13 SessionCapGetActiveNumber() */
/* This function returns the number of active authorization sessions currently being tracked by the
   TPM. */
UINT32
SessionCapGetActiveNumber(
			  void
			  )
{
    UINT32              i;
    UINT32              num = 0;
    // Iterate the context array to find the number of non-zero slots
    for(i = 0; i < MAX_ACTIVE_SESSIONS; i++)
	{
	    if(gr.contextArray[i] != 0) num++;
	}
    return num;
}
/* 8.9.6.14 SessionCapGetActiveAvail() */
/* This function returns the number of additional authorization sessions, of any type, that could be
   created. This not the number of slots for sessions, but the number of additional sessions that
   the TPM is capable of tracking. */
UINT32
SessionCapGetActiveAvail(
			 void
			 )
{
    UINT32              i;
    UINT32              num = 0;
    // Iterate the context array to find the number of zero slots
    for(i = 0; i < MAX_ACTIVE_SESSIONS; i++)
	{
	    if(gr.contextArray[i] == 0) num++;
	}
    return num;
}