summaryrefslogtreecommitdiff
path: root/Core/EM/SecurityPkg/AuthenticatedVariable/AuthVariable.c
blob: a8489f9fc8ce1eb35d657cfa7d6b6e17b6b8eb7a (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2015, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SecureBoot_WIN8/AuthenticatedVariable_efi/AuthVariable.c 90    3/09/15 4:28p Alexp $
//
// $Revision: 90 $
//
// $Date: 3/09/15 4:28p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SecureBoot_WIN8/AuthenticatedVariable_efi/AuthVariable.c $
// 
// 90    3/09/15 4:28p Alexp
// Update year 2015 in the file header
// 
// 88    10/08/14 3:16p Alexp
// Introduced global mCustomMode flag passing the status from
// PhysicalUserPresent()
// 
// 87    10/17/13 12:58p Alexp
// 
// 85    6/25/13 1:52p Alexp
// GetmSecureBootSupport() Init SecureBootVar structure with SDL defaults.
// 
// 84    5/17/13 4:37p Alexp
// EIP:122063 CSM related: failure to boot UEFI if DefaultKeyProvision is
// set without system reset
// change: SecureBoot variable is set once during AuthVar Init()
// 
// 83    4/15/13 11:07a Alexp
// Restore the code to install "SignatureSupport" variable. 
// It was mistakenly removed due to a discussin at USWG that 
// the Var should have been deprectated.
// 
// 82    3/06/13 6:52p Alexp
//  AuthVariableServiceInitSMM()
//   verify if gAmiSmmDigitalSignatureProtocol already installed before
//   installing a callback on protocol
// 
// 79    2/20/13 6:31p Alexp
// EIP[115120]:Secureboot Firmware does not verify duplication before
// Appending to the variable.
// Fix: Update FindInSignatureDb() to process combination of SigData and
// SigLists
// 
// 77    2/08/13 5:14p Alexp
// removed unused arguments from ProcessVarWithPk & ProcessVarWithKek
// 
// 76    12/18/12 10:58a Alexp
// code fixes according to "cppcheck" style & performance suggestions
// 
// 73    12/11/12 6:31p Alexp
// - Replaced TRACE macro with AVAR_TRACE. Stop debug traces in Virtual
//  address mode
// - VerifyVariable() Fix final check for IsPk and run
//  ValidateSignatureList()
// 
// 71    12/06/12 7:36p Alexp
// EIP#98995, EIP#102807 : WHCK tests fail
// -removed AuthVarLock logic, 
// -use PhysicalUserPresent() hook 
//   to unlock Secure Variables for write/erase
// 
// 70    12/04/12 6:54p Alexp
// EIP#98995, EIP#102807 : Secure Boot WHCK tests fail
// temp fix in PhysicalUserPresent()->return NOT_DETECTED if var not found
// 
// 69    11/26/12 10:46a Alexp
// replaced hardwired Var name L"SecureBootSetup" to generic Var define
// 
// 68    11/21/12 3:33p Alexp
// Fix for Win8 SecureBoot logo requirement: restore Secure Boot state
//  across flash updates.
//  Move all secure boot Setup settings to a separate varstore variable
//  in order to preserve settings across re-flash
// 
// 66    11/16/12 7:11p Alexp
// Fixed ValidateSignatureList() return status for Zero Data or Size
// 
// 65    11/09/12 4:30p Alexp
// EIP:104961 UEFI 2.3.1 SCTc test failed in Generic\EfiCompliant case. 
// Fix for SelfSigned variables.
// 
// 64    11/01/12 5:45p Alexp
// bug fix: ValidateSignatureList () returned Success 
//   even if no sig detected if file size = 0(  line 1015)
// 
// 63    10/22/12 10:32a Alexp
// fixed SetupMode overwrite while updating RO vars:SetupMode and
// SecureBoot.
// 
// 62    10/19/12 5:07p Alexp
// Implemented  R/O Variables support via fixed and OEM defined (eLink)
//  lists. Secure Boot RO Variable logic includes Vendor GUID check
//  Reserved Secure Boot RO variables are gEfiGlobalVariableGuid.
// 
// 58    9/25/12 11:20a Alexp
//  Add New feature: 
//   include facility to link external Physical User Presence
//   detect hooks via eLink:PhysicalUserPresenceDetect
// 
// 55    9/14/12 3:40p Alexp
// 1. removed unused code for setting Pk/Kek/db vars
//  2. optimized mAuthVarLock logic
// 
// 54    9/12/12 12:16p Alexp
// removed dependency on mSecureBootMode. AuthVar is locked always at
// ReadyTo boot
// 
// 53    8/29/12 5:02p Alexp
// SetVariable(): return status matched to UEFI Spec: if both
// Authenticated Attributes are set returns INVALID_PARAMETER (old
// UNSUPPORTED)
// 
// 52    8/27/12 6:55p Alexp
//  GetmSecureBootSupport(): 
//  Add input parameter SetupMode to disable  
//  Setup Flag:SecureBootSupport when platform in Setup Mode 
//  Logic enabled by DEFAULT_SECURE_BOOT_ENABLE = 0
// 
// 50    8/23/12 5:36p Alexp
// UEFI ECR change.
// Don't install SignatureSupport Variable. It's obsolete in UEFI 2.3.1+
// 
// 49    8/22/12 9:23a Alexp
// difined CUSTOM_BOOT_MODE = 2 to match value in Setup Variable
// 
// 47    7/27/12 4:36p Alexp
// Changed Auth Var rules override policy: no need for Admin var checking
// Further enhancement will introduce physical user presence detection
// 
// 45    7/25/12 6:37p Alexp
// enhance ValidateSignatureList() checking. SigList.Size field must be of
// valid size.
// 
// 44    5/17/12 4:21p Alexp
// Fix for SCT 5/12 test for Auth Variables. SCT ignores rules for
// reserved Secure Boot variables always be formatted as Authenticated
// Variable with proper attributes.
// 
// 42    4/30/12 10:53a Alexp
//  EIP88439: irrespective of SetupMode, only properly formatted 
//  Authenticated Variable can be erased
// 
// 40    4/10/12 6:53p Alexp
// comment clean up
// 
// 36    3/14/12 1:02p Alexp
// AuthServiceVirtualFixup: fixup addresses within AmiSigAPI 
// 
// 35    3/13/12 2:36p Alexp
// Add check for System in Admin mode while allowing Var Security override
// 
// 33    3/09/12 3:37p Alexp
// -add logic to process new Secure Boot mode-Custom
// -just a temp change - will be reviewed in later releases
// 
// 13    3/09/12 3:35p Alexp
// -add logic to process new Secure Boot mode-Custom
// -just a temp change - will be reviewed in later releases
// 
// 32    2/27/12 6:48p Alexp
// 
// 31    2/15/12 11:00a Alexp
// added traces for Signature List validation.
// 
// 30    2/03/12 9:56a Alexp
// EIP#82122. WHCK "Secure Boot Manual Logo Test" fails
// Fixed Append logic to process multiple sig data instances in a single
// Signature List block
// New logic will remove dupplicated certs and update new SigList header.
// 
// 29    12/29/11 6:52p Alexp
// Updated logic for handling cases with mAuthVarLock = FALSE.
// 
// 28    12/16/11 4:44p Alexp
// Add build time switch USER_MODE_POLICY_OVERRIDE
// AuthVariable logic will allow override/erase Secure Variables without
// enforcing Authentication rules until receiving EFI ReadyToBoot event.
// 
// 26    11/30/11 7:58p Alexp
// According to UEFI 2.3.1 Sec 7.2.1 : "db" vars can be validated either
// by KEK key or by PK key. 
// The fix adds the check for PK if none of KEKs can validate new "db"
// 
// 25    11/08/11 3:03p Alexp
// 1. ValidateSignatureList: add check for invalid parameter Data== NULL
// 2. VerifyVariable: ignore check for AuthAttribute match if in Setup
// Mode 
// 3. VerifyVariable: Always set TimeBased Attributes for reserved Sec
// variables
// 
// 24    11/02/11 10:53a Alexp
//  1. EIP#71452. Due to a bug in FindVariable() in NVRAMDXE.C SecureBootSetup
//      Variable cannot be located at first boot.
//      Changed GetmSecureBootSupport() to use 
//      DxeGetVariable instead of FindVariable
//  2. Made temp fixes to pass SCT 2.3.1 TW UEFI Plugfest build.
//       a. MC Based Variable. Length of the CertHdr reported wrongly 
//         22b vs 230 bytes
//       b. MC based Variable. CertType GUID is reported wrongly
//       c. Allow to set reserved Sec Variables KEK, db(x) without Auth
// attributes in Setup Mode
// 
// 22    8/24/11 8:45a Alexp
//  added Mailbox variable AuthVarMailbox to inform SMM AuthVariable code
//  of ReadyToBoot event. Needed to prevent erase of Signed Variables in
// runtime
// 
// 
// 19    8/22/11 11:11a Alexp
// UpdatePlatformMode() - moved mPlatformMode=Mode at the end
// VerifyVar - move back door code inside AuthAttribute branch
// 
// 17    8/18/11 5:59p Alexp
// renamed SecureBootMode to more appropriate SecureBootSupport
// renamed BeforeBoot to AuthVarLock. 
// Keep unlocked in DXE phase and locked while in SMM
// 
// 16    8/18/11 4:57p Alexp
// 1. Removed use of Mailbox varaible to communicate state to SMM copy of
// AuthVar service
// 2. Removed Callback event to erase Secure Variables.
// 3. Optimized logic to exchange local data between DXE and SMM copies of
// AuthVar 
// 4. Add provision to erase of Auth Variables before
// BdsConnect(ReadyToBoot) lock event
// 
// 15    8/16/11 7:18p Alexp
// added Mailbox variable AuthVarMAilbox to syncronize local state between
// DXE and SMM AuthVariable services
// 
// 14    8/12/11 1:49p Alexp
// comment out getplatfmode() in VerifyVariable call. Not needed as we do
// not want to change the SetupMode in runtime
// 
// 13    8/05/11 3:13p Alexp
// VerifyVariable->Set current time as TimeStamp for Signed Variables (PK,
// KEK..) if they are set in User mode
// 
// 12    8/04/11 7:15p Alexp
// 1. UpdatePlatformMode. Moved Get SecureBootSetup to Init function.
// 2. Fix detection of IsPk() in VerifyVariable for SetupMode control flow
// 
// 11    7/18/11 10:09a Alexp
// 1. Made gEfiGlobalVariableGuid static. Fixes link issues in older Aptio
// cores
// 2. Update Append verify with added check for the new data to be in
// SignatureList format
// 
// 10    6/30/11 4:02p Alexp
// added Callback event on Setup Mode change request form Security Setup
// Page.
// 
// 9     6/27/11 6:16p Alexp
// code optimization: moved ValidateSignatureList () call in the main
// VerifyVariable1(2) function. 
// 
// 8     6/24/11 7:03p Alexp
// fixed ValidateSignatureList () logic. Added Certificate RSA2048 to
// supported Signatures
// 
// 7     6/24/11 3:22p Alexp
// test PK, KEK, db(x) for valid Signatuer List header with
// ValidateSignatureList()
// 
// 6     6/24/11 2:24p Alexp
// 1. Call Auth Variable init in smm callback. need to initialize global
// variables for SetupMode and SecureBoot
// 2. Enable SignatureList header validation for PK-KEK-db payload
// 
// 5     6/23/11 6:19p Alexp
// Added ValidateSigList() function
// 
// 4     6/23/11 10:05a Alexp
// add dependency on ImageAuthentication token for SecureBoot Variable
// installation
// 
// 3     6/22/11 5:42p Alexp
// ignore Authenticated Variable rules while in non SecureBoot mode
// 
// 2     6/15/11 3:01p Alexp
// Add checks for variables with zero parameters: Attributes, Data, Size
// 
// 1     6/13/11 5:25p Alexp
// 
// 18    6/13/11 4:28p Alexp
// rearranged calls to VerifyVariable1 & 2 from main VerifyVariable()
// 
// 17    6/09/11 5:49p Alexp
// add AuthServiceVirtualFixup
// 
// 16    6/06/11 6:01p Alexp
// UpdatePlatformMode: update both SetupMode and SecureBoot at once
// 
// 15    6/03/11 4:12p Alexp
//  bug fix: when changing SetupMode check for PK file attribute to have
// Auth flag
// set TimeBased attrib for non-signed generic Secured variables (Win8
// w/a)
// 
// 
// 13    6/02/11 5:50p Alexp
// fix a bug with self signed key hash compare
// 
// 12    6/02/11 1:06p Alexp
// fix failed case with Append db logic if certificate already existed 
// 
// 11    6/01/11 1:06p Alexp
//  removed GetSecureVars hook. Use UpdatePlatformMode() instead
//  cleaned Append logic 
// 
// 9     5/25/11 8:34p Alexp
// draft fix for TimeBased auth variables. More work w Msft needed
// 
// 8     5/19/11 4:59p Alexp
// Major code revamp to be able to handle of handling Secure vars in Setup
// Mode
// TBD: TimeBased certificates from Msft fail to process. Not compiled as
// Authenticode format 
// 
// 7     5/17/11 12:48p Alexp
// fix WDK level4 compiler warnings
// 
// 6     5/13/11 3:43p Alexp
// add dependency on Core rev.
// 
// 5     5/12/11 5:53p Alexp
// fix tobuld for Aptio core 4.6.4. and older.
// 
// 4     5/11/11 7:20p Alexp
// VarData init for FindVariable
// 
// 3     5/10/11 5:07p Alexp
// removed local Hash Guid defines
// 
// 2     5/10/11 3:56p Alexp
// move FindVariable to NVRAMDXE.c
// 
// 1     5/09/11 10:00a Alexp
// 
// 9     5/05/11 12:10p Alexp
// make it as eModule, replace TRACE call to TRACE macro. Only dump TRACEs
// in Debug mode
// 
// 8     3/31/11 6:27p Alexp
// removed asserts on missing DB, KEK
// 
//**********************************************************************

#include "NVRAM.h"
#include <AmiDxeLib.h>
#include <AmiCspLib.h>
#include <Setup.h>
#include <SecureBootMod.h>

#include <Protocol/AmiDigitalSignature.h>

#include "AuthVariable.h"

//
// Global defines and variables
// 

BOOLEAN AVarRuntime = FALSE;

#define BDS_CONNECT_DRIVERS_PROTOCOL_GUID \
{0x3aa83745, 0x9454, 0x4f7a, 0xa7, 0xc0, 0x90, 0xdb, 0xd0, 0x2f, 0xab, 0x8e}
static EFI_GUID  gBdsConnectDriversProtocolGuid = BDS_CONNECT_DRIVERS_PROTOCOL_GUID;
static EFI_GUID  gEfiGlobalVariableGuid = EFI_GLOBAL_VARIABLE;
static EFI_GUID  mSignatureSupport[SIGSUPPORT_NUM] = {SIGSUPPORT_LIST};
static EFI_GUID  gSecureSetupGuid = SECURITY_FORM_SET_GUID;
static EFI_GUID  gSystemAccessGuid  = SYSTEM_ACCESS_GUID;  
static SECURE_BOOT_SETUP_VAR   mSecureBootSetup;

UINT8  PublicKeyHashArray[HASH_SHA256_LEN];
UINT8  mPlatformMode = SETUP_MODE;
UINT8  mSetupMode = USER_MODE;
UINT8  mCustomMode = 0;
static UINT8  mSecureBootSupport = NONSECURE_BOOT;

AMI_DIGITAL_SIGNATURE_PROTOCOL *mDigitalSigProtocol = NULL;

static CHAR16* ReservedReadOnlyVarNameList[] = {
 EFI_SETUP_MODE_NAME,
 EFI_SECURE_BOOT_NAME,
 EFI_SIGNATURE_SUPPORT_NAME,
 EFI_IMAGE_SECURITY_DATABASE_DEFAULT, 
 EFI_IMAGE_SECURITY_DATABASE1_DEFAULT,
 EFI_PLATFORM_KEY_NAME_DEFAULT,
 EFI_KEY_EXCHANGE_KEY_NAME_DEFAULT,
 NULL
};
static CHAR16* OemReadOnlyVariableList[] = {OEM_READONLY_VAR_LIST NULL};

#if USER_MODE_POLICY_OVERRIDE == 1

typedef BOOLEAN (HOOK_PHYSICAL_USER_PRESENCE_DETECT)(
    VOID 
);

extern HOOK_PHYSICAL_USER_PRESENCE_DETECT PHYSICAL_USER_PRESENCE_DETECT_LIST EndOfPhysicalUserPresentHook;

HOOK_PHYSICAL_USER_PRESENCE_DETECT* PhysicalUserPresentHookList[]=
    {PHYSICAL_USER_PRESENCE_DETECT_LIST NULL};

BOOLEAN PhysicalUserPresent( VOID )
{
    UINTN i;
    BOOLEAN Result = FALSE;
    for(i=0; PhysicalUserPresentHookList[i] && (Result == FALSE); i++) 
        Result = PhysicalUserPresentHookList[i]();
    return Result;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   AuthVarAdminUserPresent
//
//  Description: Default implementation for Physical User Presense detection.
//               Detects if Admin User signed in by checking "SystemAccess" Variable
//
//  Input:  NONE
//
//  Output: BOOLEAN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN AuthVarAdminUserPresent ( VOID  ) 
{
    SYSTEM_ACCESS SystemAccess = {SYSTEM_PASSWORD_USER};
    UINTN         Size         = sizeof(SYSTEM_ACCESS);
    UINT32        Attributes   = 0;
    EFI_STATUS    Status;
   // TBD. Replace Admin User mode with true Physical user presence detection
    Status = DxeGetVariable(L"SystemAccess", &gSystemAccessGuid, &Attributes, &Size, &SystemAccess);
    if (!EFI_ERROR(Status) 
        && !(Attributes & EFI_VARIABLE_NON_VOLATILE)
        && SystemAccess.Access==SYSTEM_PASSWORD_ADMIN)
    { 
        return TRUE;
    }

    return FALSE;
}
#endif

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   AuthServiceVirtualFixup
//
// Description: This function will be invoked to convert
//              runtime pointers to virtual address
//
// Input:   none
//
// Output:  VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID AuthServiceVirtualFixup()
{

    static BOOLEAN  bSigProtoVirtual = FALSE;
    VOID            **p;
    UINT8           i;

    //if mDigitalSigProtocol is NULL, nothing to fix up
    if (!mDigitalSigProtocol) return;
    
    // This function gets called from Nvramdxe.c, do nothing when 
    // the function is called second time.
    if (bSigProtoVirtual == TRUE) return;
    else bSigProtoVirtual = TRUE;
//AVAR_TRACE((-1,"AuthVarService mDigitalSig Virtual addr Fixup\n"));
    //Fixup mDigitalSigProtocol member functions
    for(i=0,p = (VOID**)mDigitalSigProtocol; p < (VOID**)(mDigitalSigProtocol + 1); i++,p++)
//    {
//AVAR_TRACE((-1,"mSigAPI[%d] before Virtual MemFixup = %lx (%lx), ", i, p));
        pRS->ConvertPointer(0, p);
//AVAR_TRACE((-1,"After = %lx\n", p));
//    }
    pRS->ConvertPointer(0,&mDigitalSigProtocol);
    AVarRuntime = TRUE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitSmmAuthServiceCallback
//
// Description: This function initialize mDigitalSigProtocol in SMM
//              
//
//  Input:      IN EFI_EVENT Event - Event that was triggered
//              IN VOID *Context - data pointer to information that is defined 
//              when the event is registered
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
static EFI_STATUS InitSmmAuthServiceCallback (IN EFI_EVENT Event, IN VOID *Context)
{
    return pBS->LocateProtocol(
            &gAmiSmmDigitalSignatureProtocolGuid, NULL, &mDigitalSigProtocol
            );
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitSmmAuthService
//
// Description: This function calls InitSmmAuthServiceCallback to initialaze 
//              DigitalSigProtocol in SMM by trying to Locate
//              DigitalSigProtocol. If Protocol is not installed yet 
//              RegisterProtocolCallback will be called.
//              
//
//  Input:      None
//
// Output:      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID AuthVariableServiceInitSMM ()
{
    EFI_STATUS      Status;
    EFI_EVENT       SmmHashEvent;
    VOID            *pSmm;

    // temp w/a: don't print debug traces from SMM
    AVarRuntime = TRUE;
    //
    // Authenticated variable initialize.
    //
    // Check PK database's existence to determine the value.
    // Then create  "SetupMode" with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set. 
    //
    // Update mPlatformMode  
    GetPlatformMode();

    // Update mSecureBootSupport global variable
    GetmSecureBootSupport(mPlatformMode);

    Status = InitSmmAuthServiceCallback (NULL, NULL);
    if (EFI_ERROR(Status))
        RegisterProtocolCallback(
            &gAmiSmmDigitalSignatureProtocolGuid,
            InitSmmAuthServiceCallback,
            NULL,
            &SmmHashEvent,
            &pSmm
        );
}

//------------------------------------------------------------------------------
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitAuthServiceCallback
//
// Description: This function initialize mDigitalSigProtocol not in SMM
//              
//
//  Input:      IN EFI_EVENT Event - Event that was triggered
//              IN VOID *Context - data pointer to information that is defined 
//              when the event is registered
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
static EFI_STATUS InitAuthServiceCallback (IN EFI_EVENT Event, IN VOID *Context)
{
    return pBS->LocateProtocol(
            &gAmiDigitalSignatureProtocolGuid, NULL, &mDigitalSigProtocol
            );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   AuthVariableServiceInit
//
// Description: This function calls InitAuthServiceCallback to initialaze 
//              DigitalSigProtocol not in SMM by trying to Locate
//              DigitalSigProtocol. If Protocol is not installed yet 
//              RegisterProtocolCallback will be called.
//              
//
//  Input:      None
//
// Output:      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID AuthVariableServiceInit ()
{
    EFI_EVENT       Event;
    VOID            *p;
    UINT8           Data;

    //
    // Create "SignatureSupport" with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set. 
    //
    DxeSetVariable(EFI_SIGNATURE_SUPPORT_NAME,&gEfiGlobalVariableGuid,
        EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
        SIGSUPPORT_NUM * sizeof(EFI_GUID), &mSignatureSupport
    );
    //
    // Check presence of PK database to determine the value.
    // Then create  "SetupMode" with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.   
    // Update mPlatformMode  
    GetPlatformMode();

    // Update mSecureBootSupport global variable
    GetmSecureBootSupport(mPlatformMode);

    // Set SecureBoot, both conditions must be met 
    Data = (mPlatformMode==USER_MODE && mSecureBootSupport==SECURE_BOOT)?1:0;
    DxeSetVariable(EFI_SECURE_BOOT_NAME,&gEfiGlobalVariableGuid, 
        EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS, 
        sizeof(UINT8), &Data);
   
    // Create "SetupMode" variable with RO access
    UpdatePlatformMode(mPlatformMode);

    if (EFI_ERROR(InitAuthServiceCallback (NULL, NULL)))
        RegisterProtocolCallback(
            &gAmiDigitalSignatureProtocolGuid,
            InitAuthServiceCallback,
            NULL,
            &Event,
            &p
        );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   StrSize16
//
//  Description:
//  This function returns UNICODE string size in bytes 
//
//  Input:
//  CHAR16 *String - Pointer to string
//
//  Output:
//  UINT32 - Size of string in bytes excluding the nul-terminator
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 StrSize16(CHAR16 *String)
{
    UINT32 Size = 0;//2;
    while(*String++)
        Size += 2;
    return Size;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   StrCmp16
//
//  Description:
//  This function compares two UNICODE strings 
//
//  Input:
//     IN CHAR16 *Dest - Pointer to destination string
//     IN CHAR16 *Src - Pointer to source string
//
//  Output:
//     INTN - Zero if strings are equal, non-zero otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
INTN StrCmp16(CHAR16 *Dest, CHAR16 *Src)
{
    return MemCmp(Dest, Src, StrSize16(Src));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   IsPkVar
//
//  Description:
//  This function compares two UNICODE strings 
//
//  Input:
//     CHAR16   - Pointer to UNICODE Variable Name
//     EFI_GUID - Pointer to Variable GUID
//
//  Output:
//     BOOLEAN - TRUE if strings are equal
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsPkVar(
    IN CHAR16   *VariableName,
    IN EFI_GUID *VendorGuid
)
{
    return ((guidcmp(VendorGuid, &gEfiGlobalVariableGuid) == 0) && 
            (StrCmp16 ((CHAR16 *)VariableName, (CHAR16 *)EFI_PLATFORM_KEY_NAME) == 0));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   IsKekVar
//
//  Description:
//  This function compares two UNICODE strings 
//
//  Input:
//     CHAR16   - Pointer to UNICODE Variable Name
//     EFI_GUID - Pointer to Variable GUID
//
//  Output:
//     BOOLEAN - TRUE if strings are equal
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsKekVar(
    IN CHAR16   *VariableName,
    IN EFI_GUID *VendorGuid
)
{
    return ((guidcmp(VendorGuid, &gEfiGlobalVariableGuid) == 0) && 
            (StrCmp16 ((CHAR16 *)VariableName, (CHAR16 *)EFI_KEY_EXCHANGE_KEY_NAME) == 0));
}    


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   IsReservedVariableName
//
// Description: 
//     Check if Variable name matches one from Predefined Read-Only Variable Name list
//
// Input:
//     CHAR16   - Pointer to UNICODE Variable Name
//     EFI_GUID *VendorGuid - Variable GUID
//
// Output:      
//     BOOLEAN 
//     TRUE if Variable name found in the Predefined Read-Only Variable Name lists
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
IsReservedVariableName(
    CHAR16 *VariableName,
    EFI_GUID *VendorGuid
)
{
    UINT8   Index;
    Index = 0;

    while(ReservedReadOnlyVarNameList[Index] != NULL)
    {
        if(StrCmp16 (VariableName, ReservedReadOnlyVarNameList[Index]) == 0)
        {
// Reserved Secure Boot RO variables are gEfiGlobalVariableGuid.
            return (guidcmp(VendorGuid, &gEfiGlobalVariableGuid) == 0);
        }
        Index++;
    }
    Index = 0;
    while(OemReadOnlyVariableList[Index] != NULL)
    {
        if(StrCmp16 (VariableName, OemReadOnlyVariableList[Index]) == 0)
           return TRUE;
        Index++;
    }

    return FALSE;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//  Procedure:   IsDbVar
//
//  Description:
//  This function compares two UNICODE strings 
//
//  Input:
//     IN CHAR16 *Dest - Pointer to destination string
//     IN CHAR16 *Src - Pointer to source string
//
//  Output:
//  INTN - Zero if strings are equal, non-zero otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsDbVar(
    IN EFI_GUID *VendorGuid
)
{
    return (!guidcmp (VendorGuid, &gEfiImageSecurityDatabaseGuid));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetPlatformMode
//
// Description: 
//              Initializes for authenticated varibale service.
//
// Input:
//              UINT8 *PlatformMode
//
// Output:      EFI_STATUS Function successfully executed.
//              UINT8 *PlatformMode
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetPlatformMode (  
    VOID
)
{
    EFI_STATUS  Status;
    UINT32      VarAttr;
    UINTN       VarDataSize;
    VOID        *Data;
    
    mPlatformMode = USER_MODE;

    //
    // Check PK database's existence to determine the value.
    //
    VarDataSize = 0;
    Status = FindVariable (
             EFI_PLATFORM_KEY_NAME, 
             &gEfiGlobalVariableGuid, 
             &VarAttr, 
             &VarDataSize,
             &Data
    );
    if (Status != EFI_SUCCESS || Data == NULL || VarDataSize == 0 ||
      !(VarAttr & UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES)
    ) {
        mPlatformMode = SETUP_MODE;
    }
    
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetmSecureBootSupport
//
// Description: 
//              Initializes for authenticated varibale service.
//
// Input:
//              SetupMode Disable SecureBoot control if Platform runs in SetupMode (no PK)
//          
// Output:      EFI_STATUS Function successfully executed.
//              EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resources.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetmSecureBootSupport (
    UINT8 SetupMode
)
{
    EFI_STATUS Status  = EFI_SUCCESS;
//#if defined(ImageVerification_SUPPORT) && ImageVerification_SUPPORT==1
    UINTN      DataSize=0;
    //
    // Get Setup variable, check SecureBoot and set the EFI Var
    //
    Status = DxeGetVariable(
        AMI_SECURE_BOOT_SETUP_VAR,
        &gSecureSetupGuid,
        NULL,
        &DataSize,
        &mSecureBootSetup
    );
    if(Status == EFI_BUFFER_TOO_SMALL)
        Status = DxeGetVariable(
            AMI_SECURE_BOOT_SETUP_VAR,
            &gSecureSetupGuid,
            NULL,
            &DataSize,
            &mSecureBootSetup
        );
    ASSERT_EFI_ERROR (Status);
    mSecureBootSupport = (mSecureBootSetup.SecureBootSupport);
// Disable SecureBoot Setup Option if system is in Setup mode 
#if DEFAULT_SECURE_BOOT_ENABLE == 0
    if(mSecureBootSupport == 1 &&
    (SetupMode == SETUP_MODE && mSecureBootSetup.DefaultKeyProvision == 0)
       )
    {
        mSecureBootSupport = NONSECURE_BOOT;
    }
#endif
//#else
//    mSecureBootSupport = NONSECURE_BOOT;
//#endif
  
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   UpdatePlatformMode
//
// Description: Update mPlatformMode & "SetupMode" Efi var
//
// Input:       Mode  SETUP_MODE or USER_MODE.
//
// Output:      none
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
UpdatePlatformMode( 
    IN  UINT8 Mode
)
{
AVAR_TRACE((TRACE_ALWAYS,"Update Setup Mode\nCurrent=%x, New=%x\n", mPlatformMode, Mode));
    //
    // update global mPlatformMode var
    //
    mPlatformMode = Mode;
    //
    // Set "SetupMode" variable.
    //
    // Setting of mSetupMode=SETUP_MODE will un-lock access to runtime R/O vars
    mSetupMode = SETUP_MODE;

    DxeSetVariable(EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, 
            EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
            sizeof(UINT8), &mPlatformMode);

// Re-lock access to runtime protected vars
    mSetupMode = USER_MODE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ValidateSignatureList
//
// Description: 
//              Validate the data payload begins with valid Signature List header
//              and based on the results returns Status.
//
// Input:
//              IN VOID *Data - pointer to the Var data
//              IN UINTN DataSize - size of Var data
//
// Output:      EFI_STATUS
//              UINTN RealDataSize - only the size of the combined length of Signature Lists
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ValidateSignatureList (
    IN VOID         *Data,
    IN UINTN        DataSize
)
{
    EFI_STATUS            Status;
    EFI_SIGNATURE_LIST   *SigList;
    UINTN                 Index;

    Status = EFI_SECURITY_VIOLATION;

    if(Data == NULL || DataSize == 0)
        return Status; // Sig not found

    SigList  = (EFI_SIGNATURE_LIST *)Data;

// loop till end of DataSize for all available SigLists

// Verify signature is one from SigDatabase list mSignatureSupport / sizeof(EFI_GUID)
// SigData begins with SigOwner GUID
// SignatureHdrSize = 0 for known Sig Types

    while ((DataSize > 0) && (DataSize >= SigList->SignatureListSize)) {

        for (Index = 0; Index < SIGSUPPORT_NUM; Index++) {
            if (!guidcmp ((EFI_GUID*) &(SigList->SignatureType), &mSignatureSupport[Index]))
                break;
        }
AVAR_TRACE((TRACE_ALWAYS,"SigList.Type-"));
        if(Index >= SIGSUPPORT_NUM)
            return EFI_SECURITY_VIOLATION; // Sig not found

AVAR_TRACE((TRACE_ALWAYS,"OK\nSigList.Size-"));
        if(SigList->SignatureListSize < 0x4c || // Min size for SHA2 Hash Certificate sig list
           SigList->SignatureListSize > NVRAM_SIZE)
            return EFI_SECURITY_VIOLATION; 

AVAR_TRACE((TRACE_ALWAYS,"OK\nSigList.HdrSize-"));
        if(SigList->SignatureHeaderSize != 0)
            return EFI_SECURITY_VIOLATION; // Sig not found
AVAR_TRACE((TRACE_ALWAYS,"OK\n"));
        DataSize -= SigList->SignatureListSize;
        SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize);

        Status = EFI_SUCCESS;
    }

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ProcessVarWithPk
//
// Description: Process variable with platform key for verification.
//
//
// Input:
//          VariableName                Name of Variable to be found.
//          VendorGuid                  Variable vendor GUID.
//          Data                        Data pointer.
//          DataSize                    Size of Data found. If size is less than the
//                                      data, this value contains the required size.
//          Variable                    The variable information which is used to 
//                                      keep track of variable usage.
//          Attributes                  Attribute value of the variable
//          IsPk                        Indicate whether it is to process pk.
//
// Output:      EFI_SUCCESS             Variable passed validation successfully.
//              EFI_INVALID_PARAMETER   Invalid parameter.
//              EFI_SECURITY_VIOLATION  The variable does NOT pass the validation. 
//                                      check carried out by the firmware. 
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
ProcessVarWithPk (
  IN  VOID               *Data,
  IN  UINTN              DataSize,
  IN  UINT32             Attributes,
  IN  BOOLEAN            IsPk
  )
{
  EFI_STATUS              Status = EFI_SUCCESS;
  EFI_SIGNATURE_LIST     *OldPkList;
  EFI_SIGNATURE_DATA     *OldPkData;
  UINT32                  VarAttr;
  UINT8                  *VarData;
  UINTN                   VarDataSize=0;

  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
    //
    // PK and KEK should set EFI_VARIABLE_NON_VOLATILE attribute.
    //
//AVAR_TRACE((TRACE_ALWAYS,"PK NOT_VOLITILE\n"));
    return EFI_INVALID_PARAMETER;
  }

  if (mPlatformMode == SETUP_MODE || mCustomMode == 1)
      return Status;
    //
    // Get platform key from variable.
    //
    Status = FindVariable (
               EFI_PLATFORM_KEY_NAME,
               &gEfiGlobalVariableGuid,
               &VarAttr,
               &VarDataSize,
               &VarData
               );
// PK should have been set when we were in SETUP_MODE. This condition is INVALID.
    if (EFI_ERROR (Status) || VarData==NULL || !VarDataSize)
        return EFI_SECURITY_VIOLATION;

    OldPkList = (EFI_SIGNATURE_LIST *) VarData;
    OldPkData = (EFI_SIGNATURE_DATA *) ((UINT8 *) OldPkList + sizeof (EFI_SIGNATURE_LIST) + OldPkList->SignatureHeaderSize);

// Authenticate
    Status    = VerifyDataPayload (Data, DataSize, OldPkData->SignatureData);
//AVAR_TRACE((TRACE_ALWAYS,"PK VerifyPayload %r\n",Status));

  return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ProcessVarWithKek
//
// Description: Process variable with key exchange key for verification.
//
//
// Input:
//          Data                        Data pointer.
//          DataSize                    Size of Data found. If size is less than the
//                                      data, this value contains the required size.
//          Variable                    The variable information which is used to 
//                                      keep track of variable usage.
//          Attributes                  Attribute value of the variable
//
// Output:      EFI_SUCCESS             Variable passed validation successfully.
//              EFI_INVALID_PARAMETER   Invalid parameter.
//              EFI_SECURITY_VIOLATION  The variable does NOT pass the validation. 
//                                      check carried out by the firmware. 
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
ProcessVarWithKek (
  IN  VOID         *Data,
  IN  UINTN        DataSize, 
  IN  UINT32       Attributes 
  )
{
// so far can be DB or DBx variables

  EFI_STATUS                      Status = EFI_SUCCESS;
  EFI_SIGNATURE_LIST              *KekList;
  EFI_SIGNATURE_DATA              *KekItem;
  UINT32                          KekCount;
  EFI_VARIABLE_AUTHENTICATION     *CertData;
  EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;
//  BOOLEAN                         IsFound;
  UINT32                          Index;
  UINT32                  VarAttr;
  UINT8                   *VarData;
  UINTN                   VarDataSize = 0;

  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
    //
    // PK and KEK should set EFI_VARIABLE_NON_VOLATILE attribute.
    //
    return EFI_INVALID_PARAMETER;
  }

//
// If in setup mode, no authentication needed.
//
  if (mPlatformMode == SETUP_MODE || mCustomMode == 1)
      return Status;

    CertData  = (EFI_VARIABLE_AUTHENTICATION *) Data;
    CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *)&(CertData->AuthInfo.CertData);
    //
    // Get Key database from KEK variable.
    //
    Status = FindVariable (
               EFI_KEY_EXCHANGE_KEY_NAME, 
               &gEfiGlobalVariableGuid, 
               &VarAttr, 
               &VarDataSize,
               &VarData
               );

    KekList     = (EFI_SIGNATURE_LIST *) VarData;
    if (EFI_ERROR(Status) || guidcmp ((EFI_GUID*) &(KekList->SignatureType), &gEfiCertRsa2048Guid))
      return EFI_SECURITY_VIOLATION;
    //
    // Enumerate all Kek items in this list to verify the variable certificate data.
    // If anyone is authenticated successfully, it means the variable is correct!
    //
//    IsFound   = FALSE;
//
// scan thru multiple Sig Lists if exist. Add 1 more loop....
// actually, KEK would have one list since KEK owner may club all Kek keys together before signing with PK
//    do {
        KekCount  = (KekList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - KekList->SignatureHeaderSize) / KekList->SignatureSize;
        KekItem   = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekList + sizeof (EFI_SIGNATURE_LIST) + KekList->SignatureHeaderSize);
        for (Index = 0; Index < KekCount; Index++) {
          if (MemCmp (KekItem->SignatureData, CertBlock->PublicKey, EFI_CERT_TYPE_RSA2048_SIZE) == 0) {
//            IsFound = TRUE;
            break;
          }
          KekItem = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekItem + KekList->SignatureSize);
        }
//        KekList = (EFI_SIGNATURE_LIST *) ((UINT8 *) KekList + KekList->SignatureListSize);
//    } while (!IsFound || (UINT8*)KekList < (VarData+VarDataSize));
//    if (!IsFound) {
   if (Index >= KekCount) {
      return EFI_SECURITY_VIOLATION;
    }
// Authenticate
    Status = VerifyDataPayload (Data, DataSize, CertBlock->PublicKey);
//AVAR_TRACE((TRACE_ALWAYS,"KEK VerifyPayload %r\n",Status));

  return Status;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VerifyDataPayload
//
// Description: 
//              Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256 type.
//              Follow the steps in UEFI2.2. This function does signature 
//              authetication and based on the results returns Status.
//
// Input:
//              IN VOID *Data - pointer to the Var data
//              IN UINTN DataSize - size of Var data
//              UINT8 *PubKey - PublicKey used for Sig verification.
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VerifyDataPayload (
    IN VOID *Data,
    IN UINTN DataSize, 
    IN UINT8 *PubKey
)
{
    EFI_STATUS Status;
    EFI_VARIABLE_AUTHENTICATION     *CertData;
    EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;
    UINT8 Digest[HASH_SHA256_LEN];
    UINT8 *Addr[2];
    UINTN   Len[2];
    UINTN   CertHdrSize;

    CRYPT_HANDLE   PublicKey;
    CRYPT_HANDLE   HashToVerify;

    CertData  = (EFI_VARIABLE_AUTHENTICATION *)Data;
//    CertHdrSize = AUTHINFO_SIZE(Data);
// SCT 2.3.1. TW UEFI Plugfest BUG with listing of a Cert Size in Cert->AuthInfo.Hdr.dwLength
    CertHdrSize = 0x230;

    CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256*)&(CertData->AuthInfo.CertData);

    //
    // Hash data payload with SHA256.
    //
    Addr[0] = (UINT8*) Data + (CertHdrSize) ;
    Len[0] = DataSize - (CertHdrSize);
    Addr[1] = (UINT8*)&(CertData->MonotonicCount);
    Len[1] = sizeof(UINT64);
    Status = mDigitalSigProtocol->Hash(mDigitalSigProtocol, 
                            &gEfiHashAlgorithmSha256Guid,
                            2, Addr, Len, (UINT8*)&Digest[0]);
    if (EFI_ERROR(Status)) 
        return Status;

    PublicKey.AlgGuid = gEfiCertRsa2048Guid;
    PublicKey.BlobSize = EFI_CERT_TYPE_RSA2048_SIZE;
    PublicKey.Blob = PubKey;//(UINT8*)&(CertData->AuthInfo.CertData.PublicKey);

    HashToVerify.AlgGuid = gEfiHashAlgorithmSha256Guid;
    HashToVerify.BlobSize = SHA256_DIGEST_SIZE;
    HashToVerify.Blob = &Digest[0];

    Status = mDigitalSigProtocol->Pkcs1Verify(
            mDigitalSigProtocol,
            &PublicKey,
            &HashToVerify,
            (UINT8*)&(CertBlock->Signature), 
            EFI_CERT_TYPE_RSA2048_SHA256_SIZE, EFI_CRYPT_RSASSA_PKCS1V15/*EFI_CRYPT_RSASSA_PSS*/);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   FindInSignatureDb 
//
// Description: 
//              For variables with GUID EFI_IMAGE_SECURITY_DATABASE_GUID 
//              (i.e. where the data buffer is formatted as EFI_SIGNATURE_LIST), 
//              the driver shall not perform an append of EFI_SIGNATURE_DATA values 
//              that are already part of the existing variable value (Note: This situation 
//              is not considered an error, and shall in itself not cause a status code other 
//              than EFI_SUCCESS to be returned or the timestamp associated with the variable not 
//              to be updated).
//
// Input:
//              EFI_GUID *VendorGuid   Variable vendor GUID.
//              IN UINT32 Attributes - Attributes of the Var
//              VOID  *Data - pointer to data block within AutVar Data
//              UINTN *DataSize - ptr to size of data block
//              VOID  *SigDb - current SigDb
//              UINTN  SigDbSize 
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS FindInSignatureDb (
    IN EFI_GUID     *VendorGuid,
    IN UINT32        Attributes,
    IN VOID         *Data,
    IN OUT UINTN    *DataSize,
    IN VOID         *SigDb,
    IN UINTN        SigDbSize
){
    EFI_SIGNATURE_LIST             *SigList;
    EFI_SIGNATURE_LIST             *SigListNew;
    EFI_SIGNATURE_DATA             *SigItem;
    EFI_SIGNATURE_DATA             *SigItemNew;
    UINT32                          SigCount;
    UINT32                          Index;
    UINT32                          SigCountNew;
    UINT32                          IndexNew;

    UINTN                          SigNewSize;
    BOOLEAN                        bSigMatch;
    // Is Append & SigDB
    if ( (SigDb && SigDbSize) &&
        ((Data != SigDb) && (Attributes & EFI_VARIABLE_APPEND_WRITE)) &&
        // Validate Signature List integrity 
        !EFI_ERROR(ValidateSignatureList (Data, *DataSize))
    ) {
        SigList     = (EFI_SIGNATURE_LIST *)SigDb;
        SigListNew  = (EFI_SIGNATURE_LIST *)Data;
AVAR_TRACE((TRACE_ALWAYS,"FindInDB(x)\nDataSize In %d (0x%X)\n",*DataSize,*DataSize));
        //
        // Enumerate all Sig items in this list to verify the variable certificate data.
        //
        //
        // scan through multiple Sig Lists in DB exist.
        while ((SigDbSize > 0) && (SigDbSize >= SigList->SignatureListSize)) {
            SigCount  = (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize;
            SigItem   = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) + SigList->SignatureHeaderSize);
//AVAR_TRACE((TRACE_ALWAYS,"Org SigList Count: %d, SigSize %X\n", SigCount, SigList->SignatureSize));
            // scan through multiple Sig Lists in NewSigList.
            for (Index = 1; Index <= SigCount; Index++) {
////AVAR_TRACE((TRACE_ALWAYS,"OrgCert %d, Data %X\n",Index, *(UINT32*)SigItem->SignatureData));
                SigListNew  = (EFI_SIGNATURE_LIST *)Data;
                SigNewSize  = *DataSize;
                while ((SigNewSize > 0) && (SigNewSize >= SigListNew->SignatureListSize)) {
                    bSigMatch = FALSE;
                    SigItemNew = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigListNew + sizeof (EFI_SIGNATURE_LIST) + SigListNew->SignatureHeaderSize);
                    SigCountNew  = (SigListNew->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigListNew->SignatureHeaderSize) / SigListNew->SignatureSize;
                    if (!guidcmp ((EFI_GUID*) &(SigList->SignatureType), (EFI_GUID*)&(SigListNew->SignatureType)) && 
                        SigList->SignatureSize == SigListNew->SignatureSize) {
//AVAR_TRACE((TRACE_ALWAYS,"New SigDb Size %X\nNew SigList Count: %d, SigSize %X\n", SigNewSize, SigNewCount, SigNewList->SignatureSize));
                      // loop through all instances of NewSigList->SigData. 
                      for (IndexNew = 1; IndexNew <= SigCountNew; IndexNew++) {
////AVAR_TRACE((TRACE_ALWAYS,"NewCert %d, Data %X\n",IndexNew, *(UINT32*)SigNewItem->SignatureData));
                            if (MemCmp (SigItem->SignatureData, SigItemNew->SignatureData, SigList->SignatureSize-sizeof(EFI_GUID)) == 0) {
//AVAR_TRACE((TRACE_ALWAYS,"---> match found!!!\n"));
//AVAR_TRACE((TRACE_ALWAYS,"OrgCert %4d, Data %X\n",Index, *(UINT32*)SigItem->SignatureData));
//AVAR_TRACE((TRACE_ALWAYS,"NewCert %4d, Data %X\n",IndexNew, *(UINT32*)SigNewItem->SignatureData));
                               if(SigCountNew == 1) {
                                // only 1 SigData per SigList - discard this SigList
                                      bSigMatch = TRUE;
//AVAR_TRACE((TRACE_ALWAYS,"Before: DataSize=%x\nAfter : DataSize=%x\n", *DataSize, *DataSize-SigNewList->SignatureListSize));
                                      // 1. Decrease *Datasize by SigNewList->SignatureSize 
                                      SigNewSize -= SigListNew->SignatureListSize;
                                      *DataSize -= SigListNew->SignatureListSize;
                                      // 2. replace this SigData block with data following it
                                      MemCpy (SigListNew, (void*)((UINTN)SigListNew+SigListNew->SignatureListSize), SigNewSize); 
                                      // 3. Skip to next SigListNew    
                                      break;
                                } else {
                                // more then 1 - discard this SigData
                                    // 1. replace this SigData block with data following it
                                    MemCpy (SigItemNew, (void*)((UINTN)SigItemNew+SigListNew->SignatureSize), ((UINTN)Data+*DataSize)-((UINTN)SigItemNew+SigListNew->SignatureSize));
                                    // 2. Decrease SigNewList->SignatureListSize by SigNewList->SignatureSize 
                                    SigListNew->SignatureListSize-=SigListNew->SignatureSize;
                                    *DataSize-=SigListNew->SignatureSize;
//AVAR_TRACE((TRACE_ALWAYS,"Upd SignatureListSize=%x, DataSize=%x\n",SigNewList->SignatureListSize, *DataSize));
                                    // 3. If this is last SigData element
                                    if((SigListNew->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigListNew->SignatureHeaderSize)==0)
                                    {  
//AVAR_TRACE((TRACE_ALWAYS,"SigList is Empty!\n"));
                                        break;
                                    }         
                                    // 4. Skip incrementing of SigNewItem
                                    continue;
                                }
                            } // if cmp 
                            SigItemNew = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigItemNew + SigListNew->SignatureSize);
                        } // for SigNewItem 
                    } // if guid
                    // Skip incrementing of SigNewList if bSigListMatch is found - we already on next siglist
                    if(!bSigMatch) {
                        SigNewSize -= SigListNew->SignatureListSize;
                        SigListNew = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigListNew + SigListNew->SignatureListSize);
                    }
                } // while SigNewList
                SigItem = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigItem + SigList->SignatureSize);
            } // for SigItem
            SigDbSize -= SigList->SignatureListSize;
            SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize);
        } // while SigList 

AVAR_TRACE((TRACE_ALWAYS,"DataSize Out: %d (0x%X)\n",*DataSize, *DataSize));
    if(*DataSize==0)
        return EFI_ALREADY_STARTED;

AVAR_TRACE((TRACE_ALWAYS,"APPEND OK!\n"));
    }

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VerifyVariable1
//
// Description: 
//              Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256 type.
//              Follow the steps in UEFI2.2.
//              This function is called every time variable with 
//              EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute is 
//              created, updated or deleted. This function does all necessary 
//              authetication checks and based on the results returns Status.
//              Also it returns the Mc Hash of PublicKey from Variable's AuthInfo Hdr
//
// Input:
//              CHAR16 *VariableName  Name of Variable to be found.
//              EFI_GUID *VendorGuid   Variable vendor GUID.
//              IN UINT32 Attributes - Attributes of the Var
//              VOID **Data - pointer to data block within AutVar Data
//              UINTN *DataSize - size of data block
//              VOID  *OldData - pointer to Existing in NVRAM data block 
//              UINTN  OldDataSize - size of data block
//              UINT64 ExtFlags.MonotonicCount - value of MC or TIME stamp 
//              UINT8  ExtFlags.KeyHash[32] - pointer to memory, allocated by caller, 
//                     where Hash of Public Key will be returned.
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VerifyVariable1 (
    IN CHAR16   *VariableName,
    IN EFI_GUID *VendorGuid,
    IN UINT32    Attributes,
    IN VOID    **Data,
    IN UINTN    *DataSize, 
    IN VOID     *OldData,
    IN UINTN     OldDataSize,
    IN OUT EXT_SEC_FLAGS *ExtFlags
){
    EFI_STATUS Status;

    VOID        *RealData;
    EFI_VARIABLE_AUTHENTICATION *CertData;
    EFI_CERT_BLOCK_RSA_2048_SHA256   *CertBlock;
    UINT8       *PubKey, *PubKeyHash;
    UINTN       CertHdrSize;
    UINTN       Size;
    BOOLEAN     IsSigListVar; 
    BOOLEAN     IsPk;

    RealData = *Data;
    Status = EFI_SUCCESS;
    IsSigListVar = TRUE;
    IsPk = FALSE;

// must have Auth attribute to go deeper
    if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == 0)
        return EFI_INVALID_PARAMETER;

    if(*DataSize < sizeof(EFI_VARIABLE_AUTHENTICATION))
        return EFI_SECURITY_VIOLATION;

// specific for EFI_VARIABLE_AUTHENTICATION mode variables
//--->>>
    CertData  = (EFI_VARIABLE_AUTHENTICATION *) *Data;
    CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256*)&(CertData->AuthInfo.CertData);
// SCT 2.3.1 TW Plugfest BUG with listing of a Cert Size in Cert->AuthInfo.Hdr.dwLength
// 0x22b vs 0x230
//    CertHdrSize = AUTHINFO_SIZE(CertData);//(CertData->AuthInfo.Hdr.Hdr.dwLength + sizeof(CertData->MonotonicCount));
    AVAR_TRACE((TRACE_ALWAYS,"VerifyVariable CertHdr Size 0x%x (expected(0x230)\n",AUTHINFO_SIZE(CertData)));

    CertHdrSize = 0x230;

    //
    // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.
    // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256.
    //
    if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||  
        guidcmp ((EFI_GUID*) &(CertData->AuthInfo.CertType), &gEfiCertTypeRsa2048Sha256Guid)
    ) {
        //
        // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.
        //
        AVAR_TRACE((TRACE_ALWAYS,"VerifyVariable AuthHdr GUID test Fails\nWinCert_Type\nExpected  %x\nReceived %x\nGUID\nExpected  %g\nReceived %g\n",WIN_CERT_TYPE_EFI_GUID, CertData->AuthInfo.Hdr.wCertificateType, gEfiCertTypeRsa2048Sha256Guid, &(CertData->AuthInfo.CertType)));
// SCT 2.3.1 TW Plugfest uses wrong GUID
            return EFI_SECURITY_VIOLATION;
    } 

    if(*DataSize < CertHdrSize)
    {
AVAR_TRACE((TRACE_ALWAYS,"VerifyVariable DataSize test fails: DataSize(%x) < AuthHdrSize (%x)\n", *DataSize, CertHdrSize));
        return EFI_SECURITY_VIOLATION;
    }
    //
    // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.
    //
AVAR_TRACE((TRACE_ALWAYS,"Check MC:\nOld=%x\nNew=%x\n",ExtFlags->Mc, CertData->MonotonicCount));
    if (((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&
        (OldData && ExtFlags->Mc >= CertData->MonotonicCount)
    ) {
        AVAR_TRACE((TRACE_ALWAYS,"Failed\n"));
        return EFI_SECURITY_VIOLATION;
    }
AVAR_TRACE((TRACE_ALWAYS,"Pass\n"));

// AppendWrite: Only update Timestamp if New one is greater then current
    if( (Attributes & EFI_VARIABLE_APPEND_WRITE) && 
        (OldData && ExtFlags->Mc > CertData->MonotonicCount)
       );
    else
        ExtFlags->Mc = CertData->MonotonicCount;
    //
    // Process PK, KEK, Sig db separately.
    //
    if (IsPkVar(VariableName, VendorGuid)){
      IsPk = TRUE;
      Status = ProcessVarWithPk (*Data, *DataSize, Attributes, TRUE);
    } else if (IsKekVar(VariableName, VendorGuid)) {
      Status = ProcessVarWithPk (*Data, *DataSize, Attributes, FALSE);
    } else if (IsDbVar(VendorGuid)) {
      Status = ProcessVarWithKek (*Data, *DataSize, Attributes);
      // verify process db(x) with one of KEK keys or if not found within KEK - with PK
      if (Status == EFI_SECURITY_VIOLATION) 
        Status = ProcessVarWithPk (*Data, *DataSize, Attributes, FALSE);
    } else {
        IsSigListVar = FALSE;
        PubKey = &(CertBlock->PublicKey[0]);
        PubKeyHash = &PublicKeyHashArray[0];
        // Verify SelfSigned variable is signed with a valid Key
        Status = VerifyDataPayload (*Data, *DataSize, PubKey);
        if (!EFI_ERROR(Status)) {

            Size = RSA2048_PUB_KEY_LEN;
            Status = mDigitalSigProtocol->Hash(mDigitalSigProtocol, 
                        &gEfiHashAlgorithmSha256Guid,
                        1, &PubKey, (const UINTN*)&Size, PubKeyHash); 

            if (OldData && MemCmp(&ExtFlags->KeyHash[0], PubKeyHash, HASH_SHA256_LEN)){
//AVAR_TRACE((TRACE_ALWAYS,"Self Signed MC Var Key Compare FAILED!\n"));
                return EFI_SECURITY_VIOLATION;
            }
            // Setting key Hash for self signed variables
            MemCpy(&ExtFlags->KeyHash[0], PubKeyHash, HASH_SHA256_LEN);
        }
    }
    if (EFI_ERROR(Status))  
        return EFI_SECURITY_VIOLATION;

    *DataSize   =  *DataSize - CertHdrSize;
    *Data       = (UINT8*)RealData + CertHdrSize;

    if(IsSigListVar == TRUE) {

        // Validate Signature List integrity 
        if(*DataSize && EFI_ERROR(ValidateSignatureList (*Data, *DataSize)))
            return EFI_SECURITY_VIOLATION;
        //
        // If delete PK in user mode -> change to setup mode.
        // If enroll PK in setup mode -> change to user mode.
        //
        if(IsPk) {
            if (*DataSize == 0) 
                UpdatePlatformMode (SETUP_MODE);
            else
                UpdatePlatformMode (USER_MODE);
        }
    }
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VerifyVariable
//
// Description: 
//              This function is called every time variable with 
//              EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and 
//              EFI_VARIABLE_AUTHENTICATED_TIME_BASED_ACCESS attributes 
//              created, updated or deleted. This function does all necessary 
//              authetication checks and based on the results returns Status.
//              Also it returns the Mc Hash of PublicKey from Variable's AuthInfo Hdr
//
// Input:
//              CHAR16 *VariableName  Name of Variable to be found.
//              EFI_GUID *VendorGuid   Variable vendor GUID.
//              IN UINT32 Attributes - Attributes of the Var
//              VOID **Data - pointer to data block within AutVar Data
//              UINTN *DataSize - size of data block
//              VOID  *OldData - pointer to Existing in NVRAM data block 
//              UINTN  OldDataSize - size of data block
//              UINT64 ExtFlags.MonotonicCount - value of MC or TIME stamp 
//              UINT8  ExtFlags.KeyHash[32] - pointer to memory, allocated by caller, 
//                     where Hash of Public Key will be returned.
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VerifyVariable (
    IN CHAR16   *VariableName,
    IN EFI_GUID *VendorGuid,
    IN UINT32   *Attributes,
    IN VOID    **Data,
    IN UINTN    *DataSize,
    IN VOID     *OldData,
    IN UINTN     OldDataSize,
    IN OUT EXT_SEC_FLAGS *ExtSecFlags
){
    EFI_STATUS  Status;
    UINT32      AuthAttributes; 
    UINT64      OldMC;

    Status = EFI_SUCCESS; 
    mCustomMode = 0;

//AVAR_TRACE((TRACE_ALWAYS,"Var Name: %S, Attr=%X, Data=%X, Size=%d\n", VariableName, *Attributes, *Data, *DataSize));

    // bypass Var R/O check when updating 
    // SetupMode and SecureBoot variables after changing of a PK
    if(mSetupMode == SETUP_MODE) {
// Faking presense of NV attribute for SetupMode in order to meet UEFI requirement 
// to display SetupMode state even in runtime (after exit boot services)
//        AuthAttributes = *Attributes & (~EFI_VARIABLE_NON_VOLATILE);
//        *Attributes = AuthAttributes;
//        *Attributes &= (UINT32)(~EFI_VARIABLE_NON_VOLATILE);
        // Re-lock access to runtime protected vars
        mSetupMode = USER_MODE;
        return Status;//EFI_SUCCESS;
    }

    // existing reserved variables are RO!!!
    if(OldData && OldDataSize && IsReservedVariableName(VariableName, VendorGuid))
        return EFI_WRITE_PROTECTED;

    AuthAttributes = ExtSecFlags->AuthFlags & UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES;

    while ((*Attributes & UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES)
            // Old Variable with no attributes can be erased after proper AuthHeader validation
            // EIP88439: irrespective of SetupMode, only properly formatted Auth Variable can be erased
            || (AuthAttributes) 
    ){
    // get mPlatformMode 
        GetPlatformMode ();

    // check if both attributes are set  
        if ((*Attributes & UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES) == 
                           UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES
        )
        { Status = EFI_INVALID_PARAMETER; break;}

#if USER_MODE_POLICY_OVERRIDE == 1
        // ignore Variable Authentication rules while in Physical User Presence
        mCustomMode = PhysicalUserPresent();
        AVAR_TRACE((TRACE_ALWAYS,"Physical User %s\n",
                 (mCustomMode?"detected - suppress Variable Authentication":"not detected")));
        if (mCustomMode) {
            if(*DataSize==0 || *Data==NULL) {
                if(IsPkVar(VariableName, VendorGuid))
                    UpdatePlatformMode(SETUP_MODE);
    
                Status = EFI_SUCCESS; 
                break;
            }
        }
#endif
//    Old - nonAuth, New - nonAuth - exit with EFI_SUCCESS
//    Old - nonAuth, New - Auth    - Continue with new Auth attr
//    Old - Auth,    New - nonAuth - if *Attribs=0 - Erase in progress if in SetupMode
//                                   else EFI_SECURITY_VIOLATION
//    Old - Auth,    New - Auth    - Continue if AuthAttr matching
//                                   else EFI_SECURITY_VIOLATION
// OldVar AuthAttributes mismatch
        if( AuthAttributes && *Attributes &&
          !(AuthAttributes & (*Attributes & UEFI23_1_AUTHENTICATED_VARIABLE_ATTRIBUTES))
        )
        // Attribute mismatch
        { Status = EFI_SECURITY_VIOLATION; break;}

        // else in process of erasing or Setting AuthVar

        AuthAttributes |= *Attributes;
        OldMC = ExtSecFlags->Mc;

        if(*DataSize==0 || *Data==NULL)
        { Status = EFI_SECURITY_VIOLATION; break;}

        if(!mDigitalSigProtocol)
        { Status = EFI_UNSUPPORTED; break;}

//AVAR_TRACE((TRACE_ALWAYS,"Verify AuthVar: %S, Attr=%X, Data=%X, Size=%d\n", VariableName, *Attributes, *Data, *DataSize));
        if (AuthAttributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
            Status = VerifyVariable2(VariableName, VendorGuid, AuthAttributes, Data, DataSize, OldData, OldDataSize, ExtSecFlags);
        else
            Status = VerifyVariable1(VariableName, VendorGuid, AuthAttributes, Data, DataSize, OldData, OldDataSize, ExtSecFlags);
//AVAR_TRACE((TRACE_ALWAYS, "Exit - %r, Data - %X, Size %d, Attributes %x\n",Status, *Data, *DataSize, *Attributes));
        if (EFI_ERROR(Status)) 
            break;

      // Find out if New Var is a Signature Db and the Sig already present in current Sig DB Variable
      // bail out SetVar if present - nothing to change
        Status = FindInSignatureDb(VendorGuid, *Attributes, *Data, DataSize, OldData, OldDataSize);
        if (EFI_ERROR(Status)) {
            // Only update the Timestamp if new Sig found in OldSig list
            if(OldMC != ExtSecFlags->Mc)
            {
               *DataSize = 0;
                Status = EFI_SUCCESS;
                
            }
            // else  Variable not changed, abort the SetVar
        } 

        break;
    } // end while loop

    return Status; // variable not changed
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2015, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************