/
SessionProcess.c
2270 lines (2075 loc) · 85.3 KB
/
SessionProcess.c
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Microsoft Reference Implementation for TPM 2.0
*
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and
* contributor rights, including patent rights, and no such rights are granted
* under this license.
*
* Copyright (c) Microsoft Corporation
*
* All rights reserved.
*
* BSD License
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//** Introduction
// This file contains the subsystem that process the authorization sessions
// including implementation of the Dictionary Attack logic. ExecCommand() uses
// ParseSessionBuffer() to process the authorization session area of a command and
// BuildResponseSession() to create the authorization session area of a response.
//** Includes and Data Definitions
#define SESSION_PROCESS_C
#include "Tpm.h"
//
//** Authorization Support Functions
//
//*** IsDAExempted()
// This function indicates if a handle is exempted from DA logic.
// A handle is exempted if it is
// 1. a primary seed handle,
// 2. an object with noDA bit SET,
// 3. an NV Index with TPMA_NV_NO_DA bit SET, or
// 4. a PCR handle.
//
// return type: BOOL
// TRUE handle is exempted from DA logic
// FALSE handle is not exempted from DA logic
BOOL
IsDAExempted(
TPM_HANDLE handle // IN: entity handle
)
{
BOOL result = FALSE;
switch(HandleGetType(handle))
{
case TPM_HT_PERMANENT:
// All permanent handles, other than TPM_RH_LOCKOUT, are exempt from
// DA protection.
result = (handle != TPM_RH_LOCKOUT);
break;
// When this function is called, a persistent object will have been loaded
// into an object slot and assigned a transient handle.
case TPM_HT_TRANSIENT:
{
result = (ObjectGetPublicAttributes(handle).noDA == SET);
break;
}
case TPM_HT_NV_INDEX:
{
NV_INDEX *nvIndex = NvGetIndexInfo(handle, NULL);
result = (nvIndex->publicArea.attributes.TPMA_NV_NO_DA == SET);
break;
}
case TPM_HT_PCR:
// PCRs are always exempted from DA.
result = TRUE;
break;
default:
break;
}
return result;
}
//*** IncrementLockout()
// This function is called after an authorization failure that involves use of
// an authValue. If the entity referenced by the handle is not exempt from DA
// protection, then the failedTries counter will be incremented.
//
// return type: TPM_RC
// TPM_RC_AUTH_FAIL authorization failure that caused DA lockout to increment
// TPM_RC_BAD_AUTH authorization failure did not cause DA lockout to increment
static TPM_RC
IncrementLockout(
UINT32 sessionIndex
)
{
TPM_HANDLE handle = s_associatedHandles[sessionIndex];
TPM_HANDLE sessionHandle = s_sessionHandles[sessionIndex];
SESSION *session = NULL;
// Don't increment lockout unless the handle associated with the session
// is DA protected or the session is bound to a DA protected entity.
if(sessionHandle == TPM_RS_PW)
{
if(IsDAExempted(handle))
return TPM_RC_BAD_AUTH;
}
else
{
session = SessionGet(sessionHandle);
// If the session is bound to lockout, then use that as the relevant
// handle. This means that an authorization failure with a bound session
// bound to lockoutAuth will take precedence over any other
// lockout check
if(session->attributes.isLockoutBound == SET)
handle = TPM_RH_LOCKOUT;
if(session->attributes.isDaBound == CLEAR
&& (IsDAExempted(handle) || session->attributes.includeAuth == CLEAR))
// If the handle was changed to TPM_RH_LOCKOUT, this will not return
// TPM_RC_BAD_AUTH
return TPM_RC_BAD_AUTH;
}
if(handle == TPM_RH_LOCKOUT)
{
pAssert(gp.lockOutAuthEnabled == TRUE);
// lockout is no longer enabled
gp.lockOutAuthEnabled = FALSE;
// For TPM_RH_LOCKOUT, if lockoutRecovery is 0, no need to update NV since
// the lockout authorization will be reset at startup.
if(gp.lockoutRecovery != 0)
{
if(NV_IS_AVAILABLE)
// Update NV.
NV_SYNC_PERSISTENT(lockOutAuthEnabled);
else
// No NV access for now. Put the TPM in pending mode.
s_DAPendingOnNV = TRUE;
}
}
else
{
if(gp.recoveryTime != 0)
{
gp.failedTries++;
if(NV_IS_AVAILABLE)
// Record changes to NV. NvWrite will SET g_updateNV
NV_SYNC_PERSISTENT(failedTries);
else
// No NV access for now. Put the TPM in pending mode.
s_DAPendingOnNV = TRUE;
}
}
// Register a DA failure and reset the timers.
DARegisterFailure(handle);
return TPM_RC_AUTH_FAIL;
}
//*** IsSessionBindEntity()
// This function indicates if the entity associated with the handle is the entity,
// to which this session is bound. The binding would occur by making the "bind"
// parameter in TPM2_StartAuthSession() not equal to TPM_RH_NULL. The binding only
// occurs if the session is an HMAC session. The bind value is a combination of
// the Name and the authValue of the entity.
//
// return type: BOOL
// TRUE handle points to the session start entity
// FALSE handle does not point to the session start entity
static BOOL
IsSessionBindEntity(
TPM_HANDLE associatedHandle, // IN: handle to be authorized
SESSION *session // IN: associated session
)
{
TPM2B_NAME entity; // The bind value for the entity
// If the session is not bound, return FALSE.
if(session->attributes.isBound)
{
// Compute the bind value for the entity.
SessionComputeBoundEntity(associatedHandle, &entity);
// Compare to the bind value in the session.
return MemoryEqual2B(&entity.b, &session->u1.boundEntity.b);
}
return FALSE;
}
//*** IsPolicySessionRequired()
// Checks if a policy session is required for a command. If a command requires
// DUP or ADMIN role authorization, then the handle that requires that role is the
// first handle in the command. This simplifies this checking. If a new command
// is created that requires multiple ADMIN role authorizations, then it will
// have to be special-cased in this function.
// A policy session is required if:
// 1. the command requires the DUP role,
// 2. the command requires the ADMIN role and the authorized entity
// is an object and its adminWithPolicy bit is SET, or
// 3. the command requires the ADMIN role and the authorized entity
// is a permanent handle or an NV Index.
// 4. The authorized entity is a PCR belonging to a policy group, and
// has its policy initialized
// return type: BOOL
// TRUE policy session is required
// FALSE policy session is not required
static BOOL
IsPolicySessionRequired(
COMMAND_INDEX commandIndex, // IN: command index
UINT32 sessionIndex // IN: session index
)
{
AUTH_ROLE role = CommandAuthRole(commandIndex, sessionIndex);
TPM_HT type = HandleGetType(s_associatedHandles[sessionIndex]);
if(role == AUTH_DUP)
return TRUE;
if(role == AUTH_ADMIN)
{
// We allow an exception for ADMIN role in a transient object. If the object
// allows ADMIN role actions with authorization, then policy is not
// required. For all other cases, there is no way to override the command
// requirement that a policy be used
if(type == TPM_HT_TRANSIENT)
{
OBJECT *object = HandleToObject(s_associatedHandles[sessionIndex]);
if(object->publicArea.objectAttributes.adminWithPolicy == CLEAR)
return FALSE;
}
return TRUE;
}
if(type == TPM_HT_PCR)
{
if(PCRPolicyIsAvailable(s_associatedHandles[sessionIndex]))
{
TPM2B_DIGEST policy;
TPMI_ALG_HASH policyAlg;
policyAlg = PCRGetAuthPolicy(s_associatedHandles[sessionIndex],
&policy);
if(policyAlg != TPM_ALG_NULL)
return TRUE;
}
}
return FALSE;
}
//*** IsAuthValueAvailable()
// This function indicates if authValue is available and allowed for USER role
// authorization of an entity.
//
// This function is similar to IsAuthPolicyAvailable() except that it does not
// check the size of the authValue as IsAuthPolicyAvailable() does (a null
// authValue is a valid authorization, but a null policy is not a valid policy).
//
// This function does not check that the handle reference is valid or if the entity
// is in an enabled hierarchy. Those checks are assumed to have been performed
// during the handle unmarshaling.
//
// return type: BOOL
// TRUE authValue is available
// FALSE authValue is not available
static BOOL
IsAuthValueAvailable(
TPM_HANDLE handle, // IN: handle of entity
COMMAND_INDEX commandIndex, // IN: command index
UINT32 sessionIndex // IN: session index
)
{
BOOL result = FALSE;
switch(HandleGetType(handle))
{
case TPM_HT_PERMANENT:
switch(handle)
{
// At this point hierarchy availability has already been
// checked so primary seed handles are always available here
case TPM_RH_OWNER:
case TPM_RH_ENDORSEMENT:
case TPM_RH_PLATFORM:
#ifdef VENDOR_PERMANENT
// This vendor defined handle associated with the
// manufacturer's shared secret
case VENDOR_PERMANENT:
#endif
// The DA checking has been performed on LockoutAuth but we
// bypass the DA logic if we are using lockout policy. The
// policy would allow execution to continue an lockoutAuth
// could be used, even if direct use of lockoutAuth is disabled
case TPM_RH_LOCKOUT:
// NullAuth is always available.
case TPM_RH_NULL:
result = TRUE;
break;
default:
// Otherwise authValue is not available.
break;
}
break;
case TPM_HT_TRANSIENT:
// A persistent object has already been loaded and the internal
// handle changed.
{
OBJECT *object;
object = HandleToObject(handle);
// authValue is always available for a sequence object.
// An alternative for this is to SET
// object->publicArea.objectAttributes.userWithAuth when the
// sequence is started.
if(ObjectIsSequence(object))
{
result = TRUE;
break;
}
// authValue is available for an object if it has its sensitive
// portion loaded and
// 1. userWithAuth bit is SET, or
// 2. ADMIN role is required
if(object->attributes.publicOnly == CLEAR
&& (object->publicArea.objectAttributes.userWithAuth == SET
|| (CommandAuthRole(commandIndex, sessionIndex) == AUTH_ADMIN
&& object->publicArea.objectAttributes.adminWithPolicy
== CLEAR)))
result = TRUE;
}
break;
case TPM_HT_NV_INDEX:
// NV Index.
{
NV_REF locator;
NV_INDEX *nvIndex = NvGetIndexInfo(handle, &locator);
TPMA_NV nvAttributes;
pAssert(nvIndex != 0);
nvAttributes = nvIndex->publicArea.attributes;
if(IsWriteOperation(commandIndex))
{
// AuthWrite can't be set for a PIN index
if(nvAttributes.TPMA_NV_AUTHWRITE == SET)
result = TRUE;
}
else
{
// A "read" operation
// For a PIN Index, the authValue is available as long as the
// Index has been written and the pinCount is less than pinLimit
if(IsNvPinFailIndex(nvAttributes)
|| IsNvPinPassIndex(nvAttributes))
{
NV_PIN pin;
if(nvAttributes.TPMA_NV_WRITTEN != SET)
break; // return false
// get the index values
pin.intVal = NvGetUINT64Data(nvIndex, locator);
if(pin.pin.pinCount < pin.pin.pinLimit)
result = TRUE;
}
// For non-PIN Indices, need to allow use of the authValue
else if(nvAttributes.TPMA_NV_AUTHREAD == SET)
result = TRUE;
}
}
break;
case TPM_HT_PCR:
// PCR handle.
// authValue is always allowed for PCR
result = TRUE;
break;
default:
// Otherwise, authValue is not available
break;
}
return result;
}
//*** IsAuthPolicyAvailable()
// This function indicates if an authPolicy is available and allowed.
//
// This function does not check that the handle reference is valid or if the entity
// is in an enabled hierarchy. Those checks are assumed to have been performed
// during the handle unmarshaling.
//
// return type: BOOL
// TRUE authPolicy is available
// FALSE authPolicy is not available
static BOOL
IsAuthPolicyAvailable(
TPM_HANDLE handle, // IN: handle of entity
COMMAND_INDEX commandIndex, // IN: command index
UINT32 sessionIndex // IN: session index
)
{
BOOL result = FALSE;
switch(HandleGetType(handle))
{
case TPM_HT_PERMANENT:
switch(handle)
{
// At this point hierarchy availability has already been checked.
case TPM_RH_OWNER:
if(gp.ownerPolicy.t.size != 0)
result = TRUE;
break;
case TPM_RH_ENDORSEMENT:
if(gp.endorsementPolicy.t.size != 0)
result = TRUE;
break;
case TPM_RH_PLATFORM:
if(gc.platformPolicy.t.size != 0)
result = TRUE;
break;
case TPM_RH_LOCKOUT:
if(gp.lockoutPolicy.t.size != 0)
result = TRUE;
break;
default:
break;
}
break;
case TPM_HT_TRANSIENT:
{
// Object handle.
// An evict object would already have been loaded and given a
// transient object handle by this point.
OBJECT *object = HandleToObject(handle);
// Policy authorization is not available for an object with only
// public portion loaded.
if(object->attributes.publicOnly == CLEAR)
{
// Policy authorization is always available for an object but
// is never available for a sequence.
if(!ObjectIsSequence(object))
result = TRUE;
}
break;
}
case TPM_HT_NV_INDEX:
// An NV Index.
{
NV_INDEX *nvIndex = NvGetIndexInfo(handle, NULL);
TPMA_NV attributes = nvIndex->publicArea.attributes;
// If the policy size is not zero, check if policy can be used.
if(nvIndex->publicArea.authPolicy.t.size != 0)
{
// If policy session is required for this handle, always
// uses policy regardless of the attributes bit setting
if(IsPolicySessionRequired(commandIndex, sessionIndex))
result = TRUE;
// Otherwise, the presence of the policy depends on the NV
// attributes.
else if(IsWriteOperation(commandIndex))
{
if(IsNv_TPMA_NV_POLICYWRITE(attributes))
result = TRUE;
}
else
{
if(IsNv_TPMA_NV_POLICYREAD(attributes))
result = TRUE;
}
}
}
break;
case TPM_HT_PCR:
// PCR handle.
if(PCRPolicyIsAvailable(handle))
result = TRUE;
break;
default:
break;
}
return result;
}
//** Session Parsing Functions
//*** ClearCpRpHashes()
void
ClearCpRpHashes(
COMMAND *command
)
{
#if ALG_SHA1
command->sha1CpHash.t.size = 0;
command->sha1RpHash.t.size = 0;
#endif
#if ALG_SHA256
command->sha256CpHash.t.size = 0;
command->sha256RpHash.t.size = 0;
#endif
#if ALG_SHA384
command->sha384CpHash.t.size = 0;
command->sha384RpHash.t.size = 0;
#endif
#if ALG_SHA512
command->sha512CpHash.t.size = 0;
command->sha512RpHash.t.size = 0;
#endif
#if ALG_SM3_256
command->sm3_256CpHash.t.size = 0;
command->sm3_256RpHash.t.size = 0;
#endif
}
//*** GetCpHashPointer()
// Function to get a pointer to the cpHash of the command
static TPM2B_DIGEST *
GetCpHashPointer(
COMMAND *command,
TPMI_ALG_HASH hashAlg
)
{
switch(hashAlg)
{
#if ALG_SHA1
case TPM_ALG_SHA1:
return (TPM2B_DIGEST *)&command->sha1CpHash;
#endif
#if ALG_SHA256
case TPM_ALG_SHA256:
return (TPM2B_DIGEST *)&command->sha256CpHash;
#endif
#if ALG_SHA384
case TPM_ALG_SHA384:
return (TPM2B_DIGEST *)&command->sha384CpHash;
#endif
#if ALG_SHA512
case TPM_ALG_SHA512:
return (TPM2B_DIGEST *)&command->sha512CpHash;
#endif
#if ALG_SM3_256
case TPM_ALG_SM3_256:
return (TPM2B_DIGEST *)&command->sm3_256CpHash;
#endif
default:
break;
}
return NULL;
}
//*** GetRpHashPointer()
// Function to get a pointer to the RpHash of the command
static TPM2B_DIGEST *
GetRpHashPointer(
COMMAND *command,
TPMI_ALG_HASH hashAlg
)
{
switch(hashAlg)
{
#if ALG_SHA1
case TPM_ALG_SHA1:
return (TPM2B_DIGEST *)&command->sha1RpHash;
#endif
#if ALG_SHA256
case TPM_ALG_SHA256:
return (TPM2B_DIGEST *)&command->sha256RpHash;
#endif
#if ALG_SHA384
case TPM_ALG_SHA384:
return (TPM2B_DIGEST *)&command->sha384RpHash;
#endif
#if ALG_SHA512
case TPM_ALG_SHA512:
return (TPM2B_DIGEST *)&command->sha512RpHash;
#endif
#if ALG_SM3_256
case TPM_ALG_SM3_256:
return (TPM2B_DIGEST *)&command->sm3_256RpHash;
#endif
default:
break;
}
return NULL;
}
//*** ComputeCpHash()
// This function computes the cpHash as defined in Part 2 and described in Part 1.
static TPM2B_DIGEST *
ComputeCpHash(
COMMAND *command, // IN: command parsing structure
TPMI_ALG_HASH hashAlg // IN: hash algorithm
)
{
UINT32 i;
HASH_STATE hashState;
TPM2B_NAME name;
TPM2B_DIGEST *cpHash;
// cpHash = hash(commandCode [ || authName1
// [ || authName2
// [ || authName 3 ]]]
// [ || parameters])
// A cpHash can contain just a commandCode only if the lone session is
// an audit session.
// Get pointer to the hash value
cpHash = GetCpHashPointer(command, hashAlg);
if(cpHash->t.size == 0)
{
cpHash->t.size = CryptHashStart(&hashState, hashAlg);
// Add commandCode.
CryptDigestUpdateInt(&hashState, sizeof(TPM_CC), command->code);
// Add authNames for each of the handles.
for(i = 0; i < command->handleNum; i++)
CryptDigestUpdate2B(&hashState, &EntityGetName(command->handles[i],
&name)->b);
// Add the parameters.
CryptDigestUpdate(&hashState, command->parameterSize,
command->parameterBuffer);
// Complete the hash.
CryptHashEnd2B(&hashState, &cpHash->b);
}
return cpHash;
}
//*** GetCpHash()
// This function is used to access a precomputed cpHash.
static TPM2B_DIGEST *
GetCpHash(
COMMAND *command,
TPMI_ALG_HASH hashAlg
)
{
TPM2B_DIGEST *cpHash = GetCpHashPointer(command, hashAlg);
pAssert(cpHash->t.size != 0);
return cpHash;
}
//*** CompareTemplateHash()
// This function computes the template hash and compares it to the session
// templateHash. It is the hash of the second parameter
// assuming that the command is TPM2_Create(), TPM2_CreatePrimary(), or
// TPM2_Derive()
static BOOL
CompareTemplateHash(
COMMAND *command, // IN: parsing structure
SESSION *session // IN: session data
)
{
BYTE *pBuffer = command->parameterBuffer;
INT32 pSize = command->parameterSize;
TPM2B_DIGEST tHash;
UINT16 size;
//
// Only try this for the three commands for which it is intended
if(command->code != TPM_CC_Create
&& command->code != TPM_CC_CreatePrimary
#ifdef TPM_CC_CreateLoaded
&& command->code != TPM_CC_CreateLoaded
#endif
)
return FALSE;
// Assume that the first parameter is a TPM2B and unmarshal the size field
// Note: this will not affect the parameter buffer and size in the calling
// function.
if(UINT16_Unmarshal(&size, &pBuffer, &pSize) != TPM_RC_SUCCESS)
return FALSE;
// reduce the space in the buffer.
// NOTE: this could make pSize go negative if the parameters are not correct but
// the unmarshaling code does not try to unmarshal if the remaining size is
// negative.
pSize -= size;
// Advance the pointer
pBuffer += size;
// Get the size of what should be the template
if(UINT16_Unmarshal(&size, &pBuffer, &pSize) != TPM_RC_SUCCESS)
return FALSE;
// See if this is reasonable
if(size > pSize)
return FALSE;
// Hash the template data
tHash.t.size = CryptHashBlock(session->authHashAlg, size, pBuffer,
sizeof(tHash.t.buffer), tHash.t.buffer);
return(MemoryEqual2B(&session->u1.templateHash.b, &tHash.b));
}
//*** ComparNameHash()
// This function computes the name hash and compares it to the nameHash in the
// session data.
BOOL
CompareNameHash(
COMMAND *command, // IN: main parsing structure
SESSION *session // IN: session structure with nameHash
)
{
HASH_STATE hashState;
TPM2B_DIGEST nameHash;
UINT32 i;
TPM2B_NAME name;
//
nameHash.t.size = CryptHashStart(&hashState, session->authHashAlg);
// Add names.
for(i = 0; i < command->handleNum; i++)
CryptDigestUpdate2B(&hashState, &EntityGetName(command->handles[i],
&name)->b);
// Complete hash.
CryptHashEnd2B(&hashState, &nameHash.b);
// and compare
return MemoryEqual(session->u1.nameHash.t.buffer, nameHash.t.buffer,
nameHash.t.size);
}
//*** CheckPWAuthSession()
// This function validates the authorization provided in a PWAP session. It
// compares the input value to authValue of the authorized entity. Argument
// sessionIndex is used to get handles handle of the referenced entities from
// s_inputAuthValues[] and s_associatedHandles[].
//
// return type: TPM_RC
// TPM_RC_AUTH_FAIL authorization fails and increments DA failure
// count
// TPM_RC_BAD_AUTH authorization fails but DA does not apply
//
static TPM_RC
CheckPWAuthSession(
UINT32 sessionIndex // IN: index of session to be processed
)
{
TPM2B_AUTH authValue;
TPM_HANDLE associatedHandle = s_associatedHandles[sessionIndex];
// Strip trailing zeros from the password.
MemoryRemoveTrailingZeros(&s_inputAuthValues[sessionIndex]);
// Get the authValue with trailing zeros removed
EntityGetAuthValue(associatedHandle, &authValue);
// Success if the values are identical.
if(MemoryEqual2B(&s_inputAuthValues[sessionIndex].b, &authValue.b))
{
return TPM_RC_SUCCESS;
}
else // if the digests are not identical
{
// Invoke DA protection if applicable.
return IncrementLockout(sessionIndex);
}
}
//*** ComputeCommandHMAC()
// This function computes the HMAC for an authorization session in a command.
/*(See part 1 specification -- this tag keeps this comment from showing up in
// merged document which is probably good because this comment doesn't look right.
// The sessionAuth value
// authHMAC := HMACsHash((sessionKey | authValue),
// (pHash | nonceNewer | nonceOlder | nonceTPMencrypt-only
// | nonceTPMaudit | sessionAttributes))
// Where:
// HMACsHash() The HMAC algorithm using the hash algorithm specified
// when the session was started.
//
// sessionKey A value that is computed in a protocol-dependent way,
// using KDFa. When used in an HMAC or KDF, the size field
// for this value is not included.
//
// authValue A value that is found in the sensitive area of an entity.
// When used in an HMAC or KDF, the size field for this
// value is not included.
//
// pHash Hash of the command (cpHash) using the session hash.
// When using a pHash in an HMAC computation, only the
// digest is used.
//
// nonceNewer A value that is generated by the entity using the
// session. A new nonce is generated on each use of the
// session. For a command, this will be nonceCaller.
// When used in an HMAC or KDF, the size field is not used.
//
// nonceOlder A TPM2B_NONCE that was received the previous time the
// session was used. For a command, this is nonceTPM.
// When used in an HMAC or KDF, the size field is not used.
//
// nonceTPMdecrypt The nonceTPM of the decrypt session is included in
// the HMAC, but only in the command.
//
// nonceTPMencrypt The nonceTPM of the encrypt session is included in
// the HMAC but only in the command.
//
// sessionAttributes A byte indicating the attributes associated with the
// particular use of the session.
*/
static TPM2B_DIGEST *
ComputeCommandHMAC(
COMMAND *command, // IN: primary control structure
UINT32 sessionIndex, // IN: index of session to be processed
TPM2B_DIGEST *hmac // OUT: authorization HMAC
)
{
TPM2B_TYPE(KEY, (sizeof(AUTH_VALUE) * 2));
TPM2B_KEY key;
BYTE marshalBuffer[sizeof(TPMA_SESSION)];
BYTE *buffer;
UINT32 marshalSize;
HMAC_STATE hmacState;
TPM2B_NONCE *nonceDecrypt;
TPM2B_NONCE *nonceEncrypt;
SESSION *session;
nonceDecrypt = NULL;
nonceEncrypt = NULL;
// Determine if extra nonceTPM values are going to be required.
// If this is the first session (sessionIndex = 0) and it is an authorization
// session that uses an HMAC, then check if additional session nonces are to be
// included.
if(sessionIndex == 0
&& s_associatedHandles[sessionIndex] != TPM_RH_UNASSIGNED)
{
// If there is a decrypt session and if this is not the decrypt session,
// then an extra nonce may be needed.
if(s_decryptSessionIndex != UNDEFINED_INDEX
&& s_decryptSessionIndex != sessionIndex)
{
// Will add the nonce for the decrypt session.
SESSION *decryptSession
= SessionGet(s_sessionHandles[s_decryptSessionIndex]);
nonceDecrypt = &decryptSession->nonceTPM;
}
// Now repeat for the encrypt session.
if(s_encryptSessionIndex != UNDEFINED_INDEX
&& s_encryptSessionIndex != sessionIndex
&& s_encryptSessionIndex != s_decryptSessionIndex)
{
// Have to have the nonce for the encrypt session.
SESSION *encryptSession
= SessionGet(s_sessionHandles[s_encryptSessionIndex]);
nonceEncrypt = &encryptSession->nonceTPM;
}
}
// Continue with the HMAC processing.
session = SessionGet(s_sessionHandles[sessionIndex]);
// Generate HMAC key.
MemoryCopy2B(&key.b, &session->sessionKey.b, sizeof(key.t.buffer));
// Check if the session has an associated handle and if the associated entity
// is the one to which the session is bound. If not, add the authValue of
// this entity to the HMAC key.
// If the session is bound to the object or the session is a policy session
// with no authValue required, do not include the authValue in the HMAC key.
// Note: For a policy session, its isBound attribute is CLEARED.
// Include the entity authValue if it is needed
if(session->attributes.includeAuth == SET)
{
TPM2B_AUTH authValue;
// Get the entity authValue with trailing zeros removed
EntityGetAuthValue(s_associatedHandles[sessionIndex], &authValue);
// add the authValue to the HMAC key
MemoryConcat2B(&key.b, &authValue.b, sizeof(key.t.buffer));
}
// if the HMAC key size is 0, a NULL string HMAC is allowed
if(key.t.size == 0
&& s_inputAuthValues[sessionIndex].t.size == 0)
{
hmac->t.size = 0;
return hmac;
}
// Start HMAC
hmac->t.size = CryptHmacStart2B(&hmacState, session->authHashAlg, &key.b);
// Add cpHash
CryptDigestUpdate2B(&hmacState.hashState,
&ComputeCpHash(command, session->authHashAlg)->b);
// Add nonces as required
CryptDigestUpdate2B(&hmacState.hashState, &s_nonceCaller[sessionIndex].b);
CryptDigestUpdate2B(&hmacState.hashState, &session->nonceTPM.b);
if(nonceDecrypt != NULL)
CryptDigestUpdate2B(&hmacState.hashState, &nonceDecrypt->b);
if(nonceEncrypt != NULL)
CryptDigestUpdate2B(&hmacState.hashState, &nonceEncrypt->b);
// Add sessionAttributes
buffer = marshalBuffer;
marshalSize = TPMA_SESSION_Marshal(&(s_attributes[sessionIndex]),
&buffer, NULL);
CryptDigestUpdate(&hmacState.hashState, marshalSize, marshalBuffer);
// Complete the HMAC computation
CryptHmacEnd2B(&hmacState, &hmac->b);
return hmac;
}
//*** CheckSessionHMAC()
// This function checks the HMAC of in a session. It uses ComputeCommandHMAC()
// to compute the expected HMAC value and then compares the result with the
// HMAC in the authorization session. The authorization is successful if they
// are the same.
//
// If the authorizations are not the same, IncrementLockout() is called. It will
// return TPM_RC_AUTH_FAIL if the failure caused the failureCount to increment.
// Otherwise, it will return TPM_RC_BAD_AUTH.
//
// return type: TPM_RC
// TPM_RC_AUTH_FAIL authorization failure caused failureCount increment
// TPM_RC_BAD_AUTH authorization failure did not cause failureCount
// increment
//
static TPM_RC
CheckSessionHMAC(
COMMAND *command, // IN: primary control structure
UINT32 sessionIndex // IN: index of session to be processed
)
{
TPM2B_DIGEST hmac; // authHMAC for comparing
// Compute authHMAC
ComputeCommandHMAC(command, sessionIndex, &hmac);
// Compare the input HMAC with the authHMAC computed above.
if(!MemoryEqual2B(&s_inputAuthValues[sessionIndex].b, &hmac.b))
{
// If an HMAC session has a failure, invoke the anti-hammering
// if it applies to the authorized entity or the session.
// Otherwise, just indicate that the authorization is bad.
return IncrementLockout(sessionIndex);
}
return TPM_RC_SUCCESS;
}
//*** CheckPolicyAuthSession()
// This function is used to validate the authorization in a policy session.
// This function performs the following comparisons to see if a policy
// authorization is properly provided. The check are:
// 1. compare policyDigest in session with authPolicy associated with
// the entity to be authorized;
// 2. compare timeout if applicable;
// 3. compare commandCode if applicable;
// 4. compare cpHash if applicable; and
// 5. see if PCR values have changed since computed.
//
// If all the above checks succeed, the handle is authorized.
// The order of these comparisons is not important because any failure will
// result in the same error code.
//
// return type: TPM_RC
// TPM_RC_PCR_CHANGED PCR value is not current
// TPM_RC_POLICY_FAIL policy session fails
// TPM_RC_LOCALITY command locality is not allowed
// TPM_RC_POLICY_CC CC doesn't match
// TPM_RC_EXPIRED policy session has expired
// TPM_RC_PP PP is required but not asserted
// TPM_RC_NV_UNAVAILABLE NV is not available for write
// TPM_RC_NV_RATE NV is rate limiting
static TPM_RC
CheckPolicyAuthSession(
COMMAND *command, // IN: primary parsing structure
UINT32 sessionIndex // IN: index of session to be processed
)
{
SESSION *session;
TPM2B_DIGEST authPolicy;
TPMI_ALG_HASH policyAlg;
UINT8 locality;