-
Notifications
You must be signed in to change notification settings - Fork 365
/
tpmclient.int.c
2270 lines (1821 loc) · 80.6 KB
/
tpmclient.int.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
/* SPDX-License-Identifier: BSD-2-Clause */
/***********************************************************************;
* Copyright (c) 2015, Intel Corporation
* All rights reserved.
***********************************************************************;
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tss2_sys.h"
#include "tss2-tcti/tctildr.h"
#include "tss2-tcti/tcti-pcap.h"
#include "tss2_tcti_device.h"
#include "tss2_tcti_libtpms.h"
#ifdef TCTI_MSSIM
#include "tss2_tcti_mssim.h"
#endif /* TCTI_MSSIM */
#ifdef TCTI_SWTPM
#include "tss2_tcti_swtpm.h"
#endif /* TCTI_SWTPM */
#include "../integration/test-common.h"
#include "../integration/sys-util.h"
#include "../integration/session-util.h"
#include "util/tss2_endian.h"
#include "sysapi_util.h"
#define LOGMODULE testtpmclient
#include "util/log.h"
/*
* TPM indices and sizes
*/
#define NV_AUX_INDEX_SIZE 96
#define NV_PS_INDEX_SIZE 34
#define INDEX_AUX 0x01800003 /* NV Storage */
#define INDEX_LCP_OWN 0x01400001 /* Launch Policy Owner */
#define INDEX_LCP_SUP 0x01800001 /* Launch Policy Default (Supplier) */
#define TPM20_INDEX_TEST1 0x01500015
#define TPM20_INDEX_TEST2 0x01500016
#define TPM20_INDEX_PASSWORD_TEST 0x01500020
#define SESSIONS_COUNT 1
#define SET_PCR_SELECT_BIT( pcrSelection, pcr ) \
(pcrSelection).pcrSelect[( (pcr)/8 )] |= ( 1 << ( (pcr) % 8) );
#define CLEAR_PCR_SELECT_BITS( pcrSelection ) \
(pcrSelection).pcrSelect[0] = 0; \
(pcrSelection).pcrSelect[1] = 0; \
(pcrSelection).pcrSelect[2] = 0;
#define SET_PCR_SELECT_SIZE( pcrSelection, size ) \
(pcrSelection).sizeofSelect = size;
TSS2_SYS_CONTEXT *sysContext;
TSS2_TCTI_CONTEXT *resMgrTctiContext = 0;
#define INIT_SIMPLE_TPM2B_SIZE(type) (type).size = sizeof(type) - 2;
#define YES 1
#define NO 0
static void ErrorHandler(UINT32 rval, char *errorString, int errorStringSize)
{
UINT32 errorLevel = rval & TSS2_RC_LAYER_MASK;
char levelString[32];
switch (errorLevel)
{
case TSS2_TPM_RC_LAYER:
strcpy(levelString, "TPM");
break;
case TSS2_SYS_RC_LAYER:
strcpy(levelString, "System API");
break;
case TSS2_MU_RC_LAYER:
strcpy(levelString, "System API TPM encoded");
break;
case TSS2_TCTI_RC_LAYER:
strcpy(levelString, "TCTI");
break;
case TSS2_RESMGR_TPM_RC_LAYER:
strcpy(levelString, "Resource Mgr TPM encoded");
break;
case TSS2_RESMGR_RC_LAYER:
strcpy(levelString, "Resource Mgr");
break;
default:
strcpy(levelString, "Unknown Level");
break;
}
snprintf(errorString, errorStringSize, "%s Error: 0x%x\n", levelString, rval);
}
#define EXIT_SKIP 77
static void Cleanup_exit(int rc)
{
if (resMgrTctiContext != NULL) {
Tss2_Tcti_Finalize(resMgrTctiContext);
free(resMgrTctiContext);
resMgrTctiContext = NULL;
}
exit(rc == EXIT_SKIP ? EXIT_SKIP : 1);
}
static void Cleanup()
{
Cleanup_exit(1);
}
static void InitSysContextFailure()
{
LOG_ERROR("InitSysContext failed, exiting...");
Cleanup();
}
#define ERROR_STR_LEN 200
#define CheckPassed(rval) { \
char error_string[ERROR_STR_LEN]; \
if ((rval) != TPM2_RC_SUCCESS) { \
ErrorHandler((rval), error_string, ERROR_STR_LEN); \
LOG_INFO("passing case: \tFAILED! %s (%s@%u)", \
error_string, __FUNCTION__, __LINE__ ); \
Cleanup_exit(rval); \
} else { \
LOG_INFO("passing case: \tPASSED! (%s@%u)", \
__FUNCTION__, __LINE__); \
} \
}
#define CheckFailed(rval, expected_rval) { \
char error_string[ERROR_STR_LEN]; \
if ((rval) != (expected_rval)) { \
ErrorHandler((rval), error_string, ERROR_STR_LEN); \
LOG_INFO("\tfailing case: FAILED! %s Ret code s/b: 0x%x, but was: 0x%x (%s@%u)", \
error_string, (expected_rval), (rval), __FUNCTION__, __LINE__ ); \
Cleanup_exit(rval); \
} else { \
LOG_INFO("\tfailing case: PASSED! (%s@%u)", \
__FUNCTION__, __LINE__); \
} \
}
static TSS2_RC TpmReset()
{
TSS2_RC rval = TSS2_RC_SUCCESS;
TSS2_TCTI_CONTEXT *tcti = resMgrTctiContext;
/*
* Try to unwrap tctildr/pcap. It would be better to provide an API call for
* reset by tctildr, but for now, use this instead.
*/
uint64_t magic = 0;
while (magic == 0 || magic == TCTILDR_MAGIC || magic == TCTI_PCAP_MAGIC) {
magic = *((uint64_t*) tcti);
if (magic == TCTILDR_MAGIC) {
tcti = ((TSS2_TCTILDR_CONTEXT *) tcti)->tcti;
} else if (magic == TCTI_PCAP_MAGIC) {
tcti = ((TSS2_TCTI_PCAP_CONTEXT *) tcti)->tcti_child;
}
}
#ifdef TCTI_LIBTPMS
rval = Tss2_Tcti_Libtpms_Reset(tcti);
/* If TCTI is not libtpms, bad context is returned. */
if (rval != TSS2_TCTI_RC_BAD_CONTEXT) {
return rval;
} else {
LOG_WARNING("TPM Reset failed: wrong TCTI type retrying with swtpm...");
}
#endif /* TCTI_LIBTPMS */
#ifdef TCTI_SWTPM
rval = Tss2_Tcti_Swtpm_Reset(tcti);
/* If TCTI is not swtpm, bad context is returned. */
if (rval != TSS2_TCTI_RC_BAD_CONTEXT) {
return rval;
} else {
LOG_WARNING("TPM Reset failed: wrong TCTI type retrying with mssim...");
}
#endif /* TCTI_SWTPM */
#ifdef TCTI_MSSIM
rval = (TSS2_RC)tcti_platform_command( tcti, MS_SIM_POWER_OFF );
if (rval == TSS2_RC_SUCCESS) {
rval = (TSS2_RC)tcti_platform_command( tcti, MS_SIM_POWER_ON );
} else {
LOG_WARNING("TPM Reset failed: mssim returned 0x%x.", rval);
}
#endif /* TCTI_MSSIM */
if (rval == TSS2_TCTI_RC_BAD_CONTEXT) {
LOG_WARNING("TPM Reset failed: could not reset using known TCTI types. TCTI magic: %" PRIx64, *((uint64_t *) tcti));
rval = EXIT_SKIP;
}
return rval;
}
/*
* Initialize a SYS context using the TCTI context provided by the caller.
* This function allocates memory for the SYS context and returns it to the
* caller. This memory must be freed by the caller.
*/
TSS2_SYS_CONTEXT *
sys_init_from_tcti_ctx(TSS2_TCTI_CONTEXT * tcti_ctx)
{
TSS2_SYS_CONTEXT *sys_ctx;
TSS2_RC rc;
size_t size;
TSS2_ABI_VERSION abi_version = TEST_ABI_VERSION;
size = Tss2_Sys_GetContextSize(0);
sys_ctx = (TSS2_SYS_CONTEXT *) calloc(1, size);
if (sys_ctx == NULL) {
fprintf(stderr,
"Failed to allocate 0x%zx bytes for the SYS context\n", size);
return NULL;
}
rc = Tss2_Sys_Initialize(sys_ctx, size, tcti_ctx, &abi_version);
if (rc != TSS2_RC_SUCCESS) {
fprintf(stderr, "Failed to initialize SYS context: 0x%x\n", rc);
free(sys_ctx);
return NULL;
}
return sys_ctx;
}
static void TestDictionaryAttackLockReset()
{
UINT32 rval;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce={.size=0},
.hmac={.size=0}}}};
LOG_INFO("DICTIONARY ATTACK LOCK RESET TEST :" );
rval = Tss2_Sys_DictionaryAttackLockReset ( sysContext, TPM2_RH_LOCKOUT, &sessionsData, &sessionsDataOut );
CheckPassed( rval );
}
static void TestTpmStartup()
{
UINT32 rval;
LOG_INFO("STARTUP TESTS:" );
/*
* First test the one-call interface.
*/
/* First must do TPM reset. */
rval = TpmReset();
CheckPassed(rval);
/* This one should pass. */
rval = Tss2_Sys_Startup( sysContext, TPM2_SU_CLEAR );
CheckPassed(rval);
/* This one should fail. */
rval = Tss2_Sys_Startup( sysContext, TPM2_SU_CLEAR );
CheckFailed( rval, TPM2_RC_INITIALIZE );
/* Cycle power using simulator interface. */
rval = TpmReset();
CheckPassed(rval);
/*
* Now test the synchronous, non-one-call interface.
*/
rval = Tss2_Sys_Startup_Prepare( sysContext, TPM2_SU_CLEAR );
CheckPassed(rval);
/* Execute the command synchronously. */
rval = Tss2_Sys_Execute( sysContext );
CheckPassed( rval );
/* Cycle power using simulator interface. */
rval = TpmReset();
CheckPassed(rval);
/*
* Now test the asynchronous, non-one-call interface.
*/
rval = Tss2_Sys_Startup_Prepare( sysContext, TPM2_SU_CLEAR );
CheckPassed(rval);
/* Execute the command asynchronously. */
rval = Tss2_Sys_ExecuteAsync( sysContext );
CheckPassed(rval);
/*
* Get the command response. Wait a maximum of 20ms
* for response.
*/
rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
CheckPassed(rval);
}
static void TestTpmGetCapability()
{
UINT32 rval;
char manuID[5] = " ";
char *manuIDPtr = &manuID[0];
TPMI_YES_NO moreData;
TPMS_CAPABILITY_DATA capabilityData;
LOG_INFO("GET_CAPABILITY TESTS:" );
rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
CheckPassed( rval );
*((UINT32 *)manuIDPtr) = BE_TO_HOST_32(capabilityData.data.tpmProperties.tpmProperty[0].value);
LOG_INFO("\t\tcount: %d, property: %x, manuId: %s",
capabilityData.data.tpmProperties.count,
capabilityData.data.tpmProperties.tpmProperty[0].property,
manuID );
rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_COMMAND_SIZE, 1, &moreData, &capabilityData, 0 );
CheckPassed( rval );
LOG_INFO("\t\tcount: %d, property: %x, max cmd size: %d",
capabilityData.data.tpmProperties.count,
capabilityData.data.tpmProperties.tpmProperty[0].property,
capabilityData.data.tpmProperties.tpmProperty[0].value );
rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_COMMAND_SIZE, 40, &moreData, &capabilityData, 0 );
CheckPassed( rval );
LOG_INFO("\t\tcount: %d, property: %x, max cmd size: %d",
capabilityData.data.tpmProperties.count,
capabilityData.data.tpmProperties.tpmProperty[0].property,
capabilityData.data.tpmProperties.tpmProperty[0].value );
rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_RESPONSE_SIZE, 1, &moreData, &capabilityData, 0 );
CheckPassed( rval );
LOG_INFO("\t count: %d, property: %x, max response size: %d",
capabilityData.data.tpmProperties.count,
capabilityData.data.tpmProperties.tpmProperty[0].property,
capabilityData.data.tpmProperties.tpmProperty[0].value );
rval = Tss2_Sys_GetCapability( sysContext, 0, 0xff, TPM2_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
CheckFailed(rval, TPM2_RC_VALUE+TPM2_RC_1+TPM2_RC_P);
}
static void TestTpmClear()
{
UINT32 rval;
TPM2B_AUTH hmac = { .size = 0 };
TPM2B_NONCE nonce = { .size = 0 };
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
TSS2L_SYS_AUTH_COMMAND sessionsDataIn = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce=nonce,
.hmac=hmac}}};
LOG_INFO("CLEAR and CLEAR CONTROL TESTS:" );
rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, 0 );
CheckPassed( rval );
rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, YES, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, 0 );
CheckFailed( rval, TPM2_RC_DISABLED );
rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
CheckPassed( rval );
sessionsDataIn.auths[0].sessionAttributes = 0xff;
rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_9 + TPM2_RC_RESERVED_BITS );
rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_9 + TPM2_RC_RESERVED_BITS );
}
#define SESSIONS_ABOVE_MAX_ACTIVE 0
#define DEBUG_MAX_ACTIVE_SESSIONS 8
#define DEBUG_GAP_MAX 2*DEBUG_MAX_ACTIVE_SESSIONS
SESSION *sessions[SESSIONS_COUNT];
static void TestStartAuthSession()
{
UINT32 rval;
TPM2B_ENCRYPTED_SECRET encryptedSalt;
TPMT_SYM_DEF symmetric;
SESSION *authSession = NULL;
TPM2B_NONCE nonceCaller;
UINT16 i;
TPM2_HANDLE badSessionHandle = 0x03010000;
TPMS_AUTH_COMMAND sessionData;
TPM2B_NONCE nonce;
TPM2B_AUTH hmac;
/* Init sessionHandle */
sessionData.sessionHandle = badSessionHandle;
/* Init nonce. */
nonce.size = 0;
sessionData.nonce = nonce;
/* init hmac */
hmac.size = 0;
sessionData.hmac = hmac;
/* Init session attributes */
*( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
encryptedSalt.size = 0;
LOG_INFO("START_AUTH_SESSION TESTS:" );
symmetric.algorithm = TPM2_ALG_NULL;
symmetric.keyBits.sym = 0;
symmetric.mode.sym = 0;
nonceCaller.size = 0;
encryptedSalt.size = 0;
/* Init session */
rval = create_auth_session(&authSession, TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckPassed( rval );
rval = Tss2_Sys_FlushContext( sysContext, authSession->sessionHandle );
CheckPassed( rval );
end_auth_session( authSession );
/* Init session */
rval = create_auth_session(&authSession, TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, 0xff, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckFailed( rval, TPM2_RC_VALUE + TPM2_RC_P + TPM2_RC_3 );
/*
* Try starting a bunch to see if resource manager handles this correctly.
*/
for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
{
/* Init session struct */
rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckPassed( rval );
LOG_INFO("Number of sessions created: %d", i+1 );
}
/* clean up the sessions that I don't want here. */
for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *)); i++ )
{
rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
CheckPassed( rval );
end_auth_session(sessions[i]);
}
/* Now do some gap tests. */
rval = create_auth_session(&sessions[0], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckPassed( rval );
for( i = 1; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
{
rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckPassed( rval );
rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
CheckPassed( rval );
end_auth_session(sessions[i]);
}
end_auth_session(sessions[0]);
for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
{
rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, sysContext );
CheckPassed( rval );
rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
CheckPassed( rval );
end_auth_session(sessions[i]);
}
}
static void TestChangeEps()
{
UINT32 rval;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("CHANGE_EPS TESTS:" );
rval = Tss2_Sys_ChangeEPS( sysContext, TPM2_RH_PLATFORM, &sessionsData, &sessionsDataOut );
CheckPassed( rval );
}
static void TestChangePps()
{
UINT32 rval;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("CHANGE_PPS TESTS:" );
rval = Tss2_Sys_ChangePPS( sysContext, TPM2_RH_PLATFORM, &sessionsData, &sessionsDataOut );
CheckPassed( rval );
}
static void TestHierarchyChangeAuth()
{
UINT32 rval;
TPM2B_AUTH newAuth;
int i;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("HIERARCHY_CHANGE_AUTH TESTS:" );
newAuth.size = 0;
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
CheckPassed( rval );
/* Init new auth */
newAuth.size = 20;
for( i = 0; i < newAuth.size; i++ )
newAuth.buffer[i] = i;
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
CheckPassed( rval );
sessionsData.auths[0].hmac = newAuth;
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
CheckPassed( rval );
/* Init new auth */
newAuth.size = 0;
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
CheckPassed( rval );
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
CheckFailed( rval, TPM2_RC_1 + TPM2_RC_S + TPM2_RC_BAD_AUTH );
rval = Tss2_Sys_HierarchyChangeAuth( sysContext, 0, &sessionsData, &newAuth, 0 );
CheckFailed( rval, TPM2_RC_1 + TPM2_RC_VALUE );
}
#define PCR_0 0
#define PCR_1 1
#define PCR_2 2
#define PCR_3 3
#define PCR_4 4
#define PCR_5 5
#define PCR_6 6
#define PCR_7 7
#define PCR_8 8
#define PCR_9 9
#define PCR_10 10
#define PCR_11 11
#define PCR_12 12
#define PCR_13 13
#define PCR_14 14
#define PCR_15 15
#define PCR_16 16
#define PCR_17 17
#define PCR_18 18
#define PCR_SIZE 20
static void TestPcrExtend()
{
UINT32 rval;
UINT16 i, digestSize;
TPML_PCR_SELECTION pcrSelection;
UINT32 pcrUpdateCounterBeforeExtend;
UINT32 pcrUpdateCounterAfterExtend;
UINT8 pcrBeforeExtend[PCR_SIZE];
TPM2B_EVENT eventData;
TPML_DIGEST pcrValues;
TPML_DIGEST_VALUES digests;
TPML_PCR_SELECTION pcrSelectionOut;
UINT8 pcrAfterExtend[20];
TSS2_TCTI_CONTEXT *tctiContext;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("PCR_EXTEND, PCR_EVENT, PCR_ALLOCATE, and PCR_READ TESTS:" );
/* Init digests */
digests.count = 1;
digests.digests[0].hashAlg = TPM2_ALG_SHA1;
digestSize = GetDigestSize( digests.digests[0].hashAlg );
for( i = 0; i < digestSize; i++ )
{
digests.digests[0].digest.sha1[i] = (UINT8)(i % 256);
}
pcrSelection.count = 1;
pcrSelection.pcrSelections[0].hash = TPM2_ALG_SHA1;
pcrSelection.pcrSelections[0].sizeofSelect = 3;
/* Clear out PCR select bit field */
pcrSelection.pcrSelections[0].pcrSelect[0] = 0;
pcrSelection.pcrSelections[0].pcrSelect[1] = 0;
pcrSelection.pcrSelections[0].pcrSelect[2] = 0;
/* Now set the PCR you want to read */
SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_17 );
rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterBeforeExtend, &pcrSelectionOut, &pcrValues, 0 );
CheckPassed( rval );
if( pcrValues.digests[0].size <= PCR_SIZE &&
pcrValues.digests[0].size <= sizeof( pcrValues.digests[0].buffer ) )
memcpy( &( pcrBeforeExtend[0] ), &( pcrValues.digests[0].buffer[0] ), pcrValues.digests[0].size );
/* Set locality 3 to enable PCR extend with PCR 17 */
rval = Tss2_Sys_GetTctiContext(sysContext, &tctiContext);
CheckPassed(rval);
rval = Tss2_Tcti_SetLocality(tctiContext, 3);
CheckPassed(rval);
rval = Tss2_Sys_PCR_Extend( sysContext, PCR_17, &sessionsData, &digests, 0 );
CheckPassed( rval );
rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, &pcrSelectionOut, &pcrValues, 0 );
CheckPassed( rval );
memcpy( &( pcrAfterExtend[0] ), &( pcrValues.digests[0].buffer[0] ), pcrValues.digests[0].size );
if( pcrUpdateCounterBeforeExtend == pcrUpdateCounterAfterExtend )
{
LOG_ERROR("ERROR!! pcrUpdateCounter didn't change value" );
Cleanup();
}
if( 0 == memcmp( &( pcrBeforeExtend[0] ), &( pcrAfterExtend[0] ), 20 ) )
{
LOG_ERROR("ERROR!! PCR didn't change value" );
Cleanup();
}
pcrSelection.pcrSelections[0].sizeofSelect = 255;
rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, 0, 0, 0 );
CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
eventData.size = 4;
eventData.buffer[0] = 0;
eventData.buffer[1] = 0xff;
eventData.buffer[2] = 0x55;
eventData.buffer[3] = 0xaa;
rval = Tss2_Sys_PCR_Event( sysContext, PCR_18, &sessionsData, &eventData, &digests, 0 );
CheckPassed( rval );
/* Reset locality and check whether extend PCR 17 is no possible */
rval = Tss2_Tcti_SetLocality(tctiContext, 0);
CheckPassed( rval );
rval = Tss2_Sys_PCR_Extend( sysContext, PCR_17, &sessionsData, &digests, 0 );
CheckFailed( rval, TPM2_RC_LOCALITY );
}
static void TestShutdown()
{
UINT32 rval;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
LOG_INFO("SHUTDOWN TESTS:" );
rval = Tss2_Sys_Shutdown( sysContext, 0, TPM2_SU_STATE, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_Shutdown( sysContext, 0, TPM2_SU_CLEAR, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_Shutdown( sysContext, 0, 0xff, 0 );
CheckFailed( rval, TPM2_RC_VALUE+TPM2_RC_1+TPM2_RC_P );
}
static void TestNV()
{
UINT32 rval;
TPM2B_NV_PUBLIC publicInfo;
TPM2B_AUTH nvAuth;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
int i;
TPM2B_MAX_NV_BUFFER nvWriteData;
TPM2B_MAX_NV_BUFFER nvData;
TPM2B_NV_PUBLIC nvPublic;
TPM2B_NAME nvName;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("NV INDEX TESTS:" );
nvAuth.size = 20;
for( i = 0; i < nvAuth.size; i++ ) {
nvAuth.buffer[i] = (UINT8)i;
}
publicInfo.size = 0;
publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST1;
publicInfo.nvPublic.nameAlg = TPM2_ALG_SHA1;
/* First zero out attributes. */
*(UINT32 *)&( publicInfo.nvPublic.attributes ) = 0;
/* Now set the attributes. */
publicInfo.nvPublic.attributes |= TPMA_NV_PPREAD;
publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
publicInfo.nvPublic.attributes |= TPMA_NV_WRITE_STCLEAR;
publicInfo.nvPublic.attributes |= TPMA_NV_PLATFORMCREATE;
publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
publicInfo.nvPublic.authPolicy.size = 0;
publicInfo.nvPublic.dataSize = 32;
INIT_SIMPLE_TPM2B_SIZE( nvData );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_2 + TPM2_RC_HANDLE );
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
CheckPassed( rval );
nvPublic.size = 0;
INIT_SIMPLE_TPM2B_SIZE( nvName );
rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
CheckPassed( rval );
INIT_SIMPLE_TPM2B_SIZE( nvData );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_NV_UNINITIALIZED );
/* Should fail since index is already defined. */
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_NV_DEFINED );
nvWriteData.size = 4;
for( i = 0; i < nvWriteData.size; i++ )
nvWriteData.buffer[i] = 0xff - i;
#if 1
/*
* The following, at one point, was commented out so that NVDefine will
* work on successive invocations of client app.
*
* Noticed on 12/13/12, this doesn't seem to be necessary anymore.
* Maybe something else fixed it.
*
* Seems to be a bug in TPM 2.0 simulator that if:
* First pass of tpmclient.exe after restarting TPM 2.0 simulator will
* work fine.
* If NVWrite is done, subsequent invocations of tpmclient.exe will
* ALWAYS fail on the first call to Tpm2NVDefineSpace with 0x2cb error.
* Removing NVWrite fixes this.
* And restarting TPM 2.0 simulator will make it work the first time
* and fail subsequent times.
* Removing NVWrite works around this problem.
*/
rval = Tss2_Sys_NV_Write( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
CheckPassed( rval );
INIT_SIMPLE_TPM2B_SIZE( nvData );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_NV_WriteLock( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_NV_Write( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_NV_LOCKED );
#endif
/* Now undefine the index. */
rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
CheckPassed( rval );
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
CheckPassed( rval );
/* Now undefine the index so that next run will work correctly. */
rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
CheckPassed( rval );
publicInfo.nvPublic.attributes &= ~TPMA_NV_PPREAD;
publicInfo.nvPublic.attributes &= ~TPMA_NV_PPWRITE;
publicInfo.nvPublic.attributes |= TPMA_NV_OWNERREAD;
publicInfo.nvPublic.attributes |= TPMA_NV_OWNERWRITE;
publicInfo.nvPublic.attributes &= ~TPMA_NV_PLATFORMCREATE;
publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST2;
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
CheckPassed( rval );
nvPublic.size = 0;
INIT_SIMPLE_TPM2B_SIZE( nvName );
rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST2, 0, &nvPublic, &nvName, 0 );
CheckPassed( rval );
INIT_SIMPLE_TPM2B_SIZE( nvData );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST2, &sessionsData, 32, 0, &nvData, 0 );
CheckFailed( rval, TPM2_RC_NV_AUTHORIZATION );
/* Now undefine the index. */
rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
CheckPassed( rval );
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
CheckPassed( rval );
/* Now undefine the index so that next run will work correctly. */
rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
CheckPassed( rval );
}
static void TestHierarchyControl()
{
UINT32 rval;
TPM2B_NV_PUBLIC publicInfo;
TPM2B_AUTH nvAuth;
TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
int i;
TPM2B_NAME nvName;
TPM2B_NV_PUBLIC nvPublic;
TPM2B_MAX_NV_BUFFER nvData;
TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
.sessionHandle = TPM2_RH_PW,
.sessionAttributes = 0,
.nonce = {.size = 0},
.hmac = {.size = 0}}}};
LOG_INFO("HIERARCHY CONTROL TESTS:" );
nvAuth.size = 20;
for( i = 0; i < nvAuth.size; i++ ) {
nvAuth.buffer[i] = i;
}
publicInfo.size = 0;
publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST1;
publicInfo.nvPublic.nameAlg = TPM2_ALG_SHA1;
/* First zero out attributes. */
*(UINT32 *)&( publicInfo.nvPublic.attributes ) = 0;
/* Now set the attributes. */
publicInfo.nvPublic.attributes |= TPMA_NV_PPREAD;
publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
publicInfo.nvPublic.attributes |= TPMA_NV_WRITE_STCLEAR;
publicInfo.nvPublic.attributes |= TPMA_NV_PLATFORMCREATE;
publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
publicInfo.nvPublic.authPolicy.size = 0;
publicInfo.nvPublic.dataSize = 32;
rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
CheckPassed( rval );
nvPublic.size = 0;
INIT_SIMPLE_TPM2B_SIZE( nvName );
rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
CheckPassed( rval );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_NV_UNINITIALIZED );
rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, NO, &sessionsDataOut );
CheckPassed( rval );
rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_1 + TPM2_RC_HIERARCHY );
rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, YES, &sessionsDataOut );
CheckFailed( rval, TPM2_RC_1 + TPM2_RC_HIERARCHY );
/* Need to do TPM reset and Startup to re-enable platform hierarchy. */
rval = TpmReset();
CheckPassed(rval);
rval = Tss2_Sys_Startup ( sysContext, TPM2_SU_CLEAR );
CheckPassed( rval );
rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, YES, &sessionsDataOut );
CheckPassed( rval );
/* Now undefine the index so that next run will work correctly. */
rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
CheckPassed( rval );
}
typedef struct {
char name[50];
TSS2_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *trialPolicySession, TPM2B_DIGEST *policyDigest );
TSS2_RC (*createObjectFn )( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest );
TSS2_RC (*testPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession );
} POLICY_TEST_SETUP;
static TSS2_RC BuildPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession,
TSS2_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest ),
TPM2B_DIGEST *policyDigest, bool trialSession )
{
/*
* NOTE: this policySession will be either a trial or normal policy session
* depending on the value of the passed in trialSession parameter.
*/
TPM2B_ENCRYPTED_SECRET encryptedSalt = {0,};
TPMT_SYM_DEF symmetric;
TSS2_RC rval;
TPM2B_NONCE nonceCaller;
nonceCaller.size = 0;
/* Start policy session. */
symmetric.algorithm = TPM2_ALG_NULL;
rval = create_auth_session(policySession, TPM2_RH_NULL, 0, TPM2_RH_NULL, 0, &nonceCaller, &encryptedSalt, trialSession ? TPM2_SE_TRIAL : TPM2_SE_POLICY , &symmetric, TPM2_ALG_SHA256, sysContext );
if( rval != TPM2_RC_SUCCESS )
return rval;
/* Send policy command. */
rval = ( *buildPolicyFn )( sysContext, *policySession, policyDigest );
CheckPassed( rval );
/* Get policy hash. */
INIT_SIMPLE_TPM2B_SIZE( *policyDigest );
rval = Tss2_Sys_PolicyGetDigest( sysContext, (*policySession)->sessionHandle,
0, policyDigest, 0 );
CheckPassed( rval );
if( trialSession )
{
/* Need to flush the session here. */
rval = Tss2_Sys_FlushContext( sysContext, (*policySession)->sessionHandle );
CheckPassed( rval );
/* And remove the session from sessions table. */
end_auth_session( *policySession );
}
return rval;
}
static TSS2_RC CreateNVIndex( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest )
{
TSS2_RC rval = TPM2_RC_SUCCESS;
TPMA_LOCALITY locality;
TPM2B_ENCRYPTED_SECRET encryptedSalt = {0};